ATLAS Offline Software
PerigeeParametersNtupleTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 // PerigeeParametersNtupleTool.cxx
7 // Source file for class PerigeeParametersNtupleTool
9 // (c) ATLAS Detector software
11 // Sebastian.Fleischmann -at- cern.ch, Wolfgang.Liebig -at- cern.ch
13 #include <cmath>
14 
15 #include "TTree.h"
16 // Trk
18 //#include "TrkValInterfaces/IMeasurementOnTrackNtupleTool.h"
20 #include "TrkTrack/Track.h"
24 //Truth
26 #include "AtlasHepMC/GenParticle.h"
27 
28 // constructor
30  const std::string& t,
31  const std::string& n,
32  const IInterface* p )
33  :
34  AthAlgTool(t,n,p),
35  m_extrapolator(""), // ("Trk::Extrapolator/AtlasExtrapolator"),
36  m_doTruth(true),
37  m_Rec_d0{},
38  m_Rec_z0{},
39  m_Rec_phi0{},
40  m_Rec_theta{},
41  m_Rec_eta{},
42  m_Rec_qOverP{},
43 
44  m_errord0{},
45  m_errorz0{},
46  m_errorphi0{},
47  m_errortheta0{},
48  m_errorqoverp{},
49 
50 
51  m_mc_d0{},
52  m_mc_z0{},
53  m_mc_phi0{},
54  m_mc_theta{},
55  m_mc_qOverP{},
56  m_mc_qOverPt{},
57  m_mc_eta{},
58 
59  m_mc_diff_d0{},
60  m_mc_diff_z0{},
61  m_mc_diff_phi0{},
62  m_mc_diff_theta{},
63  m_mc_diff_qOverP{},
64 
65  m_mc_pull_d0{},
66  m_mc_pull_z0{},
67  m_mc_pull_phi0{},
68  m_mc_pull_theta{},
69  m_mc_pull_qOverP{},
70 
71  m_mc_particleID{},
72  m_mc_uniqueID{},
73  m_mc_truthTreeIndex{},
74  m_mc_energy{},
75 
76  m_mc_prob{}
77 
78 {
79  declareInterface<ITrackValidationNtupleTool>(this);
80  declareProperty("ExtrapolatorTool", m_extrapolator, "Extrapolator, eg for tracks without Perigee");
81  declareProperty("DoTruth", m_doTruth, "Toggle if to Write truth data");
82 
83 }
84 
85 // destructor
87 
88 
90 // initialize
93 
94  ATH_MSG_DEBUG ("nothing specific initialized in " << name());
95  return StatusCode::SUCCESS;
96 }
97 
99 // finalize
102 
103  ATH_MSG_DEBUG ("nothing specific finalized in " << name());
104  return StatusCode::SUCCESS;
105 }
106 
108  if (!tree) return StatusCode::FAILURE;
109  //-----------------
110  // add items *** Note: Documentation is in the header file, doxygen and wikis! ***
111  //
112  // reconstructed track parameters
113  tree->Branch("RecD0", &m_Rec_d0 );
114  tree->Branch("RecZ0", &m_Rec_z0 );
115  tree->Branch("RecPhi0", &m_Rec_phi0 );
116  tree->Branch("RecTheta", &m_Rec_theta );
117  tree->Branch("RecEta", &m_Rec_eta );
118  tree->Branch("RecQoverP", &m_Rec_qOverP );
119 
120  // errors of reconstructed track parameters
121  tree->Branch("RecErrD0", &m_errord0 );
122  tree->Branch("RecErrZ0", &m_errorz0 );
123  tree->Branch("RecErrPhi0", &m_errorphi0 );
124  tree->Branch("RecErrTheta", &m_errortheta0 );
125  tree->Branch("RecErrQoverP", &m_errorqoverp );
126 
127  // Truth entries
128  if ( m_doTruth ){
129  tree->Branch( "trk_Mc_d0", &m_mc_d0 );
130  tree->Branch( "trk_Mc_z0", &m_mc_z0 );
131  tree->Branch( "trk_Mc_phi0", &m_mc_phi0 );
132  tree->Branch( "trk_Mc_theta", &m_mc_theta );
133  tree->Branch( "trk_Mc_qOverP", &m_mc_qOverP );
134  tree->Branch( "trk_Mc_qOverPt", &m_mc_qOverPt );
135  tree->Branch( "trk_Mc_eta", &m_mc_eta );
136 
137  tree->Branch( "trk_Mc_diff_d0", &m_mc_diff_d0 );
138  tree->Branch( "trk_Mc_diff_z0", &m_mc_diff_z0 );
139  tree->Branch( "trk_Mc_diff_phi0", &m_mc_diff_phi0 );
140  tree->Branch( "trk_Mc_diff_theta", &m_mc_diff_theta );
141  tree->Branch( "trk_Mc_diff_qOverP", &m_mc_diff_qOverP );
142 
143  tree->Branch( "trk_Mc_pull_d0", &m_mc_pull_d0 );
144  tree->Branch( "trk_Mc_pull_z0", &m_mc_pull_z0 );
145  tree->Branch( "trk_Mc_pull_phi0", &m_mc_pull_phi0 );
146  tree->Branch( "trk_Mc_pull_theta", &m_mc_pull_theta );
147  tree->Branch( "trk_Mc_pull_qOverP", &m_mc_pull_qOverP );
148 
149  tree->Branch( "trk_Mc_particleID", &m_mc_particleID );
150  tree->Branch( "trk_Mc_barcode", &m_mc_uniqueID ); // TODO update variable name to be consistent
151  tree->Branch( "trk_Mc_energy", &m_mc_energy );
152  tree->Branch( "trk_Mc_prob", &m_mc_prob );
153  tree->Branch( "trk_Mc_truthTreeIndex",&m_mc_truthTreeIndex );
154  }
155 
156  // general track properties other than perigee are given through
157  // Trk::TrackInformationNtupleTool
158 
159  ATH_MSG_VERBOSE ("added branches to ntuple");
160  return StatusCode::SUCCESS;
161 }
162 
167  const Trk::Track& track,
168  const int /*iterationIndex*/,
169  const unsigned int /*fitStatCode*/ ) {
170  //const Trk::FitterStatusCode /*fitStatCode*/ ) const {
171 
172  ATH_MSG_VERBOSE ("in fillTrackData(trk, indx)");
173  const EventContext& ctx = Gaudi::Hive::currentContext();
174  //----------------------------------------------
175  // fill track parameters in ntuple
176  const Trk::Perigee* perpars = track.perigeeParameters();
177  if (perpars != nullptr && fillTrackPerigee(perpars).isFailure()) {
178  msg(MSG::WARNING) << "Perigee parameters could not be written to ntuple" << endmsg;
179  }
180  if (perpars == nullptr) {
181  if ( // track.info().author() == Trk::Track::SiSPSeededTrackFinder &&
182  !m_extrapolator.empty() ) {
183  ATH_MSG_VERBOSE ("try extrapolate SiSPSeeded track to perigee");
184  const Trk::PerigeeSurface perSurf;
185  std::unique_ptr<const Trk::TrackParameters> tmp = m_extrapolator->extrapolateTrack(
186  ctx, track, perSurf, Trk::anyDirection, false, Trk::nonInteracting);
187  if (tmp && tmp->associatedSurface().type() == Trk::SurfaceType::Perigee) {
188  perpars = static_cast<const Trk::Perigee *> (tmp.release());
189  }
190  if (perpars != nullptr && fillTrackPerigee(perpars).isFailure()) {
191  msg(MSG::WARNING) << "Newly made perigee parameters could not be "
192  << "written to ntuple" << endmsg;
193  }
194  if (perpars != nullptr) delete perpars;
195  } else
196  msg(MSG::WARNING) << "No perigee parameters, but they are the main validation object!"
197  << endmsg;
198  }
199  return StatusCode::SUCCESS;
200 }
201 
207 {
208 
209  //----------------------------------------------
210  // fill track parameters in ntuple
211  const Trk::Perigee* perpars = particle.perigee();
212  if (perpars != nullptr && fillTrackPerigee(perpars).isFailure())
213  ATH_MSG_WARNING ("Perigee parameters could not be written to ntuple");
214  return StatusCode::SUCCESS;
215 }
216 
218 // reset variables
221  // reset the counters
222 }
223 
224 StatusCode Trk::PerigeeParametersNtupleTool::fillTrackTruthData ( const TrackParameters*& truePerigee, const TrackTruth& trackTruth, const int indexInTruthTree )
225 {
226 
227  if ( !m_doTruth ) return StatusCode::SUCCESS;
228 
229  ATH_MSG_VERBOSE ("Starting fillTrackTruthData()");
230 
231  const HepMcParticleLink& particleLink = trackTruth.particleLink();
232 
233  m_mc_uniqueID = HepMC::uniqueID(particleLink);
234  m_mc_prob=trackTruth.probability();
235  m_mc_truthTreeIndex = indexInTruthTree;
236 
237  HepMC::ConstGenParticlePtr genParticle = particleLink.cptr();
238 
239  if ( genParticle ){
240  m_mc_particleID = genParticle->pdg_id();
241  m_mc_energy = genParticle->momentum().e();
242  }
243 
244  if (truePerigee == nullptr) return StatusCode::SUCCESS; // fake fakes don't have true perigee
245 
246  m_mc_d0 = truePerigee->parameters()[Trk::d0];
247  m_mc_z0 = truePerigee->parameters()[Trk::z0];
248  m_mc_phi0 = truePerigee->parameters()[Trk::phi0];
249  m_mc_theta = truePerigee->parameters()[Trk::theta];
250  m_mc_qOverP = truePerigee->parameters()[Trk::qOverP];
251 
252  if ( std::sin( m_mc_theta ) != 0. )
253  m_mc_qOverPt = m_mc_qOverP / std::sin( m_mc_theta );
254 
255  m_mc_eta = truePerigee->eta();
256 
257  m_mc_diff_d0 = m_Rec_d0 - m_mc_d0;
258  m_mc_diff_z0 = m_Rec_z0 - m_mc_z0;
259  m_mc_diff_phi0 = m_Rec_phi0 - m_mc_phi0;
260  m_mc_diff_theta = m_Rec_theta - m_mc_theta;
261  m_mc_diff_qOverP = m_Rec_qOverP - m_mc_qOverP;
262 
263  m_mc_pull_d0 = m_errord0 ? m_mc_diff_d0 / m_errord0 : 0.;
264  m_mc_pull_z0 = m_errorz0 ? m_mc_diff_z0 / m_errorz0 : 0.;
265  m_mc_pull_phi0 = m_errorphi0 ? m_mc_diff_phi0 / m_errorphi0 : 0.;
266  m_mc_pull_theta = m_errortheta0 ? m_mc_diff_theta / m_errortheta0 : 0.;
267  m_mc_pull_qOverP = m_errorqoverp ? m_mc_diff_qOverP / m_errorqoverp : 0.;
268 
269  return StatusCode::SUCCESS;
270 
271 }
272 
277 
278  ATH_MSG_VERBOSE ("in fillTrackPerigee");
279 
280  // // get track parameters
281  // const Trk::Perigee *perigee = track->perigeeParameters();
282  if (!perigee) {
283  msg(MSG::WARNING) << "Something is wrong - track has no perigee at all!" << endmsg;
284  m_Rec_d0 = 0;
285  m_Rec_z0 = 0;
286  m_Rec_phi0 = 0;
287  m_Rec_theta = 0;
288  m_Rec_qOverP = 0;
289  return StatusCode::FAILURE;
290  }
291 
292  ATH_MSG_VERBOSE("Fill Perigee Parameters now");
293  // track parameters
294  m_Rec_d0 = perigee->parameters()[Trk::d0];
295  m_Rec_z0 = perigee->parameters()[Trk::z0];
296  m_Rec_phi0 = perigee->parameters()[Trk::phi0];
297  m_Rec_theta = perigee->parameters()[Trk::theta];
298  m_Rec_eta = perigee->eta();
299  m_Rec_qOverP = perigee->parameters()[Trk::qOverP];
300  ATH_MSG_DEBUG ("Trackparameters: q/p=" << m_Rec_qOverP);
301 
302  // get measured track parameters for errors
303  if (perigee->covariance()) {
304 
305  // error of track parameters
306  m_errord0 = Amg::error(*perigee->covariance(),Trk::d0);
307  m_errorz0 = Amg::error(*perigee->covariance(),Trk::z0);
308  m_errorphi0 = Amg::error(*perigee->covariance(),Trk::phi0);
309  m_errortheta0 = Amg::error(*perigee->covariance(),Trk::theta);
310  m_errorqoverp = Amg::error(*perigee->covariance(),Trk::qOverP);
311 
312 
313  } // end if measPerigee
314 
315  // TO-DO treat condition of no measured pars
316 
317  ATH_MSG_DEBUG ("Trackparameters: d0=" << m_Rec_d0 << ", z0=" << m_Rec_z0 << ", phi0=" << m_Rec_phi0 << ", theta=" << m_Rec_theta);
318 
319  return StatusCode::SUCCESS;
320 }
Trk::anyDirection
@ anyDirection
Definition: PropDirection.h:22
Trk::ParticleSwitcher::particle
constexpr ParticleHypothesis particle[PARTICLEHYPOTHESES]
the array of masses
Definition: ParticleHypothesis.h:76
TrackParameters.h
Trk::Track
The ATLAS Track class.
Definition: Tracking/TrkEvent/TrkTrack/TrkTrack/Track.h:73
Trk::PerigeeSurface
Definition: PerigeeSurface.h:43
Trk::ParametersT
Dummy class used to allow special convertors to be called for surfaces owned by a detector element.
Definition: EMErrorDetail.h:25
TrackParticleBase.h
EventPrimitivesHelpers.h
tree
TChain * tree
Definition: tile_monitor.h:30
Trk::z0
@ z0
Definition: ParamDefs.h:64
IExtrapolator.h
Trk::PerigeeParametersNtupleTool::fillTrackTruthData
virtual StatusCode fillTrackTruthData(const TrackParameters *&, const TrackTruth &, const int)
fill data about the truth match (score, parameter-pulls etc)
Definition: PerigeeParametersNtupleTool.cxx:224
Trk::PerigeeParametersNtupleTool::finalize
StatusCode finalize()
Definition: PerigeeParametersNtupleTool.cxx:101
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
Trk::PerigeeParametersNtupleTool::PerigeeParametersNtupleTool
PerigeeParametersNtupleTool(const std::string &, const std::string &, const IInterface *)
Definition: PerigeeParametersNtupleTool.cxx:29
GenParticle.h
Track.h
Trk::PerigeeParametersNtupleTool::initialize
StatusCode initialize()
Definition: PerigeeParametersNtupleTool.cxx:92
Trk::TrackParticleBase
Definition: TrackParticleBase.h:41
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
beamspotman.n
n
Definition: beamspotman.py:731
Trk::theta
@ theta
Definition: ParamDefs.h:66
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
HepMC::uniqueID
int uniqueID(const T &p)
Definition: MagicNumbers.h:116
Trk::PerigeeParametersNtupleTool::addNtupleItems
virtual StatusCode addNtupleItems(TTree *tree)
add branches to the tree Should be called once (per track collection and tree) dunring the initialisa...
Definition: PerigeeParametersNtupleTool.cxx:107
Trk::ParametersBase
Definition: ParametersBase.h:55
DeMoUpdate.tmp
string tmp
Definition: DeMoUpdate.py:1167
TrackTruth::particleLink
const HepMcParticleLink & particleLink() const
Definition: TrackTruth.h:26
PerigeeParametersNtupleTool.h
Trk::PerigeeParametersNtupleTool::fillTrackData
virtual StatusCode fillTrackData(const Trk::Track &, const int iterationIndex, const unsigned int fitStatCode)
fill ntuple data of a given track without writing the record.
Definition: PerigeeParametersNtupleTool.cxx:166
HepMC::ConstGenParticlePtr
const GenParticle * ConstGenParticlePtr
Definition: GenParticle.h:38
Trk::SurfaceType::Perigee
@ Perigee
TrackTruth.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
Trk::d0
@ d0
Definition: ParamDefs.h:63
Trk::PerigeeParametersNtupleTool::fillTrackParticleData
virtual StatusCode fillTrackParticleData(const Trk::TrackParticleBase &)
fill ntuple data of a given TrackParticle without writing the record.
Definition: PerigeeParametersNtupleTool.cxx:206
Amg::error
double error(const Amg::MatrixX &mat, int index)
return diagonal error of the matrix caller should ensure the matrix is symmetric and the index is in ...
Definition: EventPrimitivesHelpers.h:40
Trk::nonInteracting
@ nonInteracting
Definition: ParticleHypothesis.h:25
Trk::PerigeeParametersNtupleTool::resetVariables
virtual void resetVariables()
reset the variables after writing the record to disk ntuple
Definition: PerigeeParametersNtupleTool.cxx:220
TrackTruth
MC particle associated with a reco track + the quality of match.
Definition: TrackTruth.h:14
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
TrackTruth::probability
float probability() const
Definition: TrackTruth.h:28
Trk::PerigeeParametersNtupleTool::fillTrackPerigee
StatusCode fillTrackPerigee(const Trk::Perigee *perigee)
fill the perigee in ntuple
Definition: PerigeeParametersNtupleTool.cxx:276
Trk::qOverP
@ qOverP
perigee
Definition: ParamDefs.h:67
Trk::PerigeeParametersNtupleTool::~PerigeeParametersNtupleTool
~PerigeeParametersNtupleTool()
Trk::ParametersBase::eta
double eta() const
Access method for pseudorapidity - from momentum.
xAOD::track
@ track
Definition: TrackingPrimitives.h:512
drawFromPickle.sin
sin
Definition: drawFromPickle.py:36
AthAlgTool
Definition: AthAlgTool.h:26
python.AutoConfigFlags.msg
msg
Definition: AutoConfigFlags.py:7
Trk::phi0
@ phi0
Definition: ParamDefs.h:65