ATLAS Offline Software
Loading...
Searching...
No Matches
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
18using 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//=============================================================================
41double 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//=============================================================================
51double Prompt::getDistance(const Amg::Vector3D &vtx1, const Amg::Vector3D &vtx2)
52{
53 return (vtx2 - vtx1).mag();
54}
55
56//=============================================================================
57double 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//=============================================================================
104void 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//=============================================================================
127std::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//=============================================================================
172std::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//=============================================================================
213std::string Prompt::truthAsStr(const xAOD::IParticle &particle)
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//=============================================================================
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//=============================================================================
244std::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}
#define x
#define min(a, b)
Definition cfImp.cxx:40
Class providing the definition of the 4-vector interface.
virtual double phi() const override final
The azimuthal angle ( ) of the particle (has range to .)
virtual double pt() const override final
The transverse momentum ( ) of the particle.
virtual double eta() const override final
The pseudorapidity ( ) of the particle.
float z() const
Returns the z position.
size_t nTrackParticles() const
Get the number of tracks associated with this vertex.
const TrackParticle * trackParticle(size_t i) const
Get the pointer to a given track that was used in vertex reco.
float y() const
Returns the y position.
float numberDoF() const
Returns the number of degrees of freedom of the vertex fit as float.
float chiSquared() const
Returns the of the vertex fit as float.
const Amg::Vector3D & position() const
Returns the 3-pos.
float x() const
Returns the x position.
const std::vector< float > & covariance() const
Returns the covariance matrix as a simple vector of values.
double xmax
Definition listroot.cxx:61
double xmin
Definition listroot.cxx:60
Eigen::Matrix< double, 3, 1 > Vector3D
bool GetAuxVar(const T1 &obj, T2 &value, const std::string &var_name)
Definition PromptUtils.h:83
std::string PrintResetStopWatch(TStopwatch &watch)
void fillTH1(TH1 *h, double val, double weight=1.0)
std::string trkAsStr(const xAOD::TrackParticle *trk)
std::string printPromptVertexAsStr(const xAOD::Vertex *vtx, MsgStream &msg)
double getVertexFitProb(const xAOD::Vertex *vtx)
std::string truthAsStr(const xAOD::IParticle &particle)
bool getVar(T1 &obj, T2 &value, const std::string &var_name)
Definition PromptUtils.h:62
std::string vtxAsStr(const xAOD::Vertex *vtx, bool print_tracks=false)
double getNormDist(const Amg::Vector3D &PrimVtx, const Amg::Vector3D &SecVtx, const std::vector< float > &ErrorMatrix, MsgStream &msg)
double getDistance(const xAOD::Vertex *vtx1, const xAOD::Vertex *vtx2)
Definition index.py:1
STL namespace.
void sort(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end)
Specialization of sort for DataVector/List.
TrackParticle_v1 TrackParticle
Reference the current persistent version:
Vertex_v1 Vertex
Define the latest version of the vertex class.
MsgStream & msg
Definition testRead.cxx:32