ATLAS Offline Software
PromptUtils.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // Local
7 
8 // C/C++
9 #include <cmath>
10 #include <cstdlib>
11 #include <iomanip>
12 #include <sstream>
13 #include <sys/stat.h>
14 #include <stdint.h>
15 #include <stdexcept>
16 
17 
18 using namespace std;
19 
20 //=============================================================================
22 {
23  //
24  // Get xAOD::Vertex fit probability
25  //
26  double fit_prob = -1;
27 
28  if(!vtx) {
29  return fit_prob;
30  }
31 
32  if(vtx->numberDoF() > 0 && vtx->chiSquared() > 0) {
33  fit_prob = TMath::Prob(vtx->chiSquared(), vtx->numberDoF());
34  }
35 
36  return fit_prob;
37 }
38 
39 
40 //=============================================================================
41 double Prompt::getDistance(const xAOD::Vertex *vtx1, const xAOD::Vertex *vtx2)
42 {
43  if((!vtx1) || (!vtx2)) {
44  return 9999.0;
45  }
46 
47  return Prompt::getDistance(vtx1->position(), vtx2->position());
48 }
49 
50 //=============================================================================
51 double Prompt::getDistance(const Amg::Vector3D &vtx1, const Amg::Vector3D &vtx2)
52 {
53  return (vtx2 - vtx1).mag();
54 }
55 
56 //=============================================================================
57 double Prompt::getNormDist(const Amg::Vector3D &vtx1, const Amg::Vector3D &vtx2, const std::vector<float> &errorMatrix, MsgStream &msg)
58 {
59  double significance = -10;
60 
61  Eigen::Matrix<double, 3, 3, 0, 3, 3> PrimCovMtx; //Create matrix
62 
63  if(errorMatrix.empty()) {
64  msg << "getNormDist - Error matrix is empty" << std::endl;
65  return significance;
66  }
67 
68  PrimCovMtx(0,0) = static_cast<double> (errorMatrix[0]);
69  PrimCovMtx(0,1) = static_cast<double> (errorMatrix[1]);
70  PrimCovMtx(1,0) = static_cast<double> (errorMatrix[1]);
71  PrimCovMtx(1,1) = static_cast<double> (errorMatrix[2]);
72  PrimCovMtx(0,2) = static_cast<double> (errorMatrix[3]);
73  PrimCovMtx(2,0) = static_cast<double> (errorMatrix[3]);
74  PrimCovMtx(1,2) = static_cast<double> (errorMatrix[4]);
75  PrimCovMtx(2,1) = static_cast<double> (errorMatrix[4]);
76  PrimCovMtx(2,2) = static_cast<double> (errorMatrix[5]);
77 
78  if(PrimCovMtx.determinant() == 0) {
79  msg << "getNormDist - Matrix can not be inversed" << std::endl;
80  return significance;
81  }
82 
83  Eigen::Matrix<double, 3, 3,0, 3, 3> WgtMtx = PrimCovMtx.inverse();
84 
85  Eigen::Vector3d dist;
86  Amg::Vector3D vtxDiff = vtx1 - vtx2;
87  dist[0] = vtxDiff.x();
88  dist[1] = vtxDiff.y();
89  dist[2] = vtxDiff.z();
90 
91  significance = dist.transpose() * WgtMtx * dist;
92 
93  if(significance < 0) {
94  msg << "getNormDist - significance is negative" << std::endl;
95  return significance;
96  }
97 
98  significance = std::sqrt(significance);
99 
100  return significance;
101 }
102 
103 //=============================================================================
104 void Prompt::fillTH1(TH1 *h, double val, double weight)
105 {
106  //
107  // Read new event entry
108  //
109  if(!h) {
110  return;
111  }
112 
113  const double xmax = h->GetXaxis()->GetXmax();
114  const double xmin = h->GetXaxis()->GetXmin();
115 
116  double x = val;
117  if (!(val < xmax)){
118  x = h->GetXaxis()->GetBinCenter(h->GetNbinsX());
119  } else if (!(val > xmin)) {
120  x = h->GetXaxis()->GetBinCenter(1);
121  }
122 
123  h->Fill(x, weight);
124 }
125 
126 //=============================================================================
127 std::string Prompt::printPromptVertexAsStr(const xAOD::Vertex *vtx, MsgStream &msg)
128 {
129  std::stringstream str;
130 
131  str << "xAOD::Vertex pointer = " << vtx<< std::endl;
132 
133  if(vtx) {
134  float chisquared = -9999;
135  float numberdof = -9999;
136  int index = -999;
137 
138  if(!getVar(vtx, index, "SecondaryVertexIndex")) {
139  msg << "printPromptVertexAsStr -- not valid vtx SecondaryVertexIndex!!!" << std::endl;
140  }
141 
142  if(!getVar(vtx, chisquared, "chiSquared")) {
143  msg << "printPromptVertexAsStr -- not valid vtx chiSquared!!!" << std::endl;
144  }
145 
146  if(!getVar(vtx, numberdof, "numberDoF")) {
147  msg << "printPromptVertexAsStr -- not valid vtx numberDoF!!!" << std::endl;
148  }
149 
150  str << " index " << index << std::endl;
151  str << " position " << vtx->position () << std::endl;
152  str << " x " << vtx->x () << std::endl;
153  str << " y " << vtx->y () << std::endl;
154  str << " z " << vtx->z () << std::endl;
155  str << " chiSquared " << chisquared << std::endl;
156  str << " numberDoF " << numberdof << std::endl;
157 
158  str << " covariance.size() = " << vtx->covariance().size() << std::endl;
159  str << " covariance = [";
160 
161  for(const float val: vtx->covariance()) {
162  str << val << ", ";
163  }
164 
165  str << "]";
166  }
167 
168  return str.str();
169 }
170 
171 //=============================================================================
172 std::string Prompt::vtxAsStr(const xAOD::Vertex *vtx, bool print_tracks)
173 {
174  if(!vtx) {
175  return "vtxAsStr - null pointer";
176  }
177 
178  stringstream str;
179 
180  float distToPV = -1;
181  float sigToPV = -1;
182 
183  Prompt::GetAuxVar(*vtx, distToPV, "distToPriVtx");
184  Prompt::GetAuxVar(*vtx, sigToPV, "normDistToPriVtx");
185 
186  str << "xAOD::Vertex - " << vtx << ": ntrack=" << vtx->nTrackParticles()
187  << ", chi2/ndof=" << vtx->chiSquared() << "/" << vtx->numberDoF()
188  << ", prob=" << Prompt::getVertexFitProb(vtx)
189  << ", (x, y, z)=(" << vtx->x() << ", " << vtx->y() << ", " << vtx->z() << ")"
190  << ", distToPV=" << distToPV
191  << ", sigToPV=" << sigToPV;
192 
193  if(print_tracks) {
194  str << endl;
195 
196  std::vector<const xAOD::TrackParticle *> tracks;
197 
198  for(unsigned i = 0; i < vtx->nTrackParticles(); ++i) {
199  tracks.push_back(vtx->trackParticle(i));
200  }
201 
202  std::sort(tracks.begin(), tracks.end(), Prompt::SortByIDTrackPt());
203 
204  for(unsigned i = 0; i < tracks.size(); ++i) {
205  str << " xAOD::Vertex track[" << i << "] " << trkAsStr(tracks.at(i)) << endl;
206  }
207  }
208 
209  return str.str();
210 }
211 
212 //=============================================================================
214 {
215  int truthOrigin = -1;
216  int truthType = -1;
217 
218  Prompt::GetAuxVar(particle, truthOrigin, "truthOrigin");
219  Prompt::GetAuxVar(particle, truthType, "truthType");
220 
221  std::stringstream str;
222  str << "truthType=" << truthType << ", truthOrigin=" << truthOrigin;
223 
224  return str.str();
225 }
226 
227 //=============================================================================
228 std::string Prompt::trkAsStr(const xAOD::TrackParticle *trk)
229 {
230  if(!trk) {
231  return "trkAsStr - null pointer";
232  }
233 
234  stringstream str;
235 
236  str << "xAOD::TrackParticle - " << trk << ": pT=" << trk->pt()
237  << ", eta=" << trk->eta()
238  << ", phi=" << trk->phi();
239 
240  return str.str();
241 }
242 
243 //=============================================================================
244 std::string Prompt::PrintResetStopWatch(TStopwatch &watch)
245 {
246  watch.Stop();
247 
248  double realt = watch.RealTime();
249  double cput = watch.CpuTime();
250 
251  watch.Reset();
252  watch.Start();
253 
254  const int hours = static_cast<int>(realt/3600.0);
255  const int min = static_cast<int>(realt/60.0) - 60*hours;
256 
257  realt -= hours * 3600;
258  realt -= min * 60;
259 
260  if (realt < 0) realt = 0;
261  if (cput < 0) cput = 0;
262 
263  const int sec = static_cast<int>(realt);
264 
265  std::stringstream str;
266  str << "Real time "
267  << setw(2) << setfill('0') << hours
268  << ":" << setw(2) << setfill('0') << min
269  << ":" << setw(2) << setfill('0') << sec
270  << " CPU time " << setprecision(3) << fixed << cput;
271 
272  return str.str();
273 }
xAOD::TrackParticle_v1::pt
virtual double pt() const override final
The transverse momentum ( ) of the particle.
Definition: TrackParticle_v1.cxx:73
xAOD::Vertex_v1::x
float x() const
Returns the x position.
Trk::ParticleSwitcher::particle
constexpr ParticleHypothesis particle[PARTICLEHYPOTHESES]
the array of masses
Definition: ParticleHypothesis.h:76
PromptUtils.h
xAOD::Vertex_v1::nTrackParticles
size_t nTrackParticles() const
Get the number of tracks associated with this vertex.
Definition: Vertex_v1.cxx:270
Prompt::GetAuxVar
bool GetAuxVar(const T1 &obj, T2 &value, const std::string &var_name)
Definition: PromptUtils.h:93
Prompt::getVertexFitProb
double getVertexFitProb(const xAOD::Vertex *vtx)
Definition: PromptUtils.cxx:21
index
Definition: index.py:1
xAOD::TrackParticle_v1::eta
virtual double eta() const override final
The pseudorapidity ( ) of the particle.
Definition: TrackParticle_v1.cxx:77
min
constexpr double min()
Definition: ap_fixedTest.cxx:26
Prompt::getVar
bool getVar(T1 &obj, T2 &value, const std::string &var_name)
Definition: PromptUtils.h:72
Prompt::trkAsStr
std::string trkAsStr(const xAOD::TrackParticle *trk)
Definition: PromptUtils.cxx:228
xAOD::Vertex_v1::position
const Amg::Vector3D & position() const
Returns the 3-pos.
Prompt::vtxAsStr
std::string vtxAsStr(const xAOD::Vertex *vtx, bool print_tracks=false)
Definition: PromptUtils.cxx:172
xAOD::IParticle
Class providing the definition of the 4-vector interface.
Definition: Event/xAOD/xAODBase/xAODBase/IParticle.h:41
Prompt::PrintResetStopWatch
std::string PrintResetStopWatch(TStopwatch &watch)
Definition: PromptUtils.cxx:244
x
#define x
IDTPM::truthType
int truthType(const U &p)
Definition: TrackParametersHelper.h:274
dqt_zlumi_pandas.weight
int weight
Definition: dqt_zlumi_pandas.py:189
IDTPM::truthOrigin
int truthOrigin(const U &p)
Definition: TrackParametersHelper.h:283
Prompt::getNormDist
double getNormDist(const Amg::Vector3D &PrimVtx, const Amg::Vector3D &SecVtx, const std::vector< float > &ErrorMatrix, MsgStream &msg)
Definition: PromptUtils.cxx:57
Prompt::getDistance
double getDistance(const xAOD::Vertex *vtx1, const xAOD::Vertex *vtx2)
Definition: PromptUtils.cxx:41
lumiFormat.i
int i
Definition: lumiFormat.py:85
xmin
double xmin
Definition: listroot.cxx:60
h
Prompt::printPromptVertexAsStr
std::string printPromptVertexAsStr(const xAOD::Vertex *vtx, MsgStream &msg)
Definition: PromptUtils.cxx:127
xAOD::Vertex_v1::trackParticle
const TrackParticle * trackParticle(size_t i) const
Get the pointer to a given track that was used in vertex reco.
Definition: Vertex_v1.cxx:249
xAOD::Vertex_v1::z
float z() const
Returns the z position.
Prompt::truthAsStr
std::string truthAsStr(const xAOD::IParticle &particle)
Definition: PromptUtils.cxx:213
Prompt::SortByIDTrackPt
Definition: PromptUtils.h:67
Amg::Vector3D
Eigen::Matrix< double, 3, 1 > Vector3D
Definition: GeoPrimitives.h:47
taskman.hours
hours
Definition: taskman.py:650
xAOD::Vertex_v1::numberDoF
float numberDoF() const
Returns the number of degrees of freedom of the vertex fit as float.
xAOD::Vertex_v1::covariance
const std::vector< float > & covariance() const
Returns the covariance matrix as a simple vector of values.
xAOD::Vertex_v1::chiSquared
float chiSquared() const
Returns the of the vertex fit as float.
xAOD::Vertex_v1
Class describing a Vertex.
Definition: Vertex_v1.h:42
python.CaloScaleNoiseConfig.str
str
Definition: CaloScaleNoiseConfig.py:78
std::sort
void sort(typename std::reverse_iterator< DataModel_detail::iterator< DVL > > beg, typename std::reverse_iterator< DataModel_detail::iterator< DVL > > end, const Compare &comp)
Specialization of sort for DataVector/List.
Definition: DVL_algorithms.h:623
Prompt::fillTH1
void fillTH1(TH1 *h, double val, double weight=1.0)
Definition: PromptUtils.cxx:104
Pythia8_RapidityOrderMPI.val
val
Definition: Pythia8_RapidityOrderMPI.py:14
xAOD::Vertex_v1::y
float y() const
Returns the y position.
xmax
double xmax
Definition: listroot.cxx:61
str
Definition: BTagTrackIpAccessor.cxx:11
xAOD::TrackParticle_v1
Class describing a TrackParticle.
Definition: TrackParticle_v1.h:43
python.AutoConfigFlags.msg
msg
Definition: AutoConfigFlags.py:7
xAOD::TrackParticle_v1::phi
virtual double phi() const override final
The azimuthal angle ( ) of the particle (has range to .)