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 "TTree.h"
14 // Trk
16 //#include "TrkValInterfaces/IMeasurementOnTrackNtupleTool.h"
18 #include "TrkTrack/Track.h"
22 //Truth
24 #include "AtlasHepMC/GenParticle.h"
25 
26 // constructor
28  const std::string& t,
29  const std::string& n,
30  const IInterface* p )
31  :
32  AthAlgTool(t,n,p),
33  m_extrapolator(""), // ("Trk::Extrapolator/AtlasExtrapolator"),
34  m_doTruth(true),
35  m_Rec_d0{},
36  m_Rec_z0{},
37  m_Rec_phi0{},
38  m_Rec_theta{},
39  m_Rec_eta{},
40  m_Rec_qOverP{},
41 
42  m_errord0{},
43  m_errorz0{},
44  m_errorphi0{},
45  m_errortheta0{},
46  m_errorqoverp{},
47 
48 
49  m_mc_d0{},
50  m_mc_z0{},
51  m_mc_phi0{},
52  m_mc_theta{},
53  m_mc_qOverP{},
54  m_mc_qOverPt{},
55  m_mc_eta{},
56 
57  m_mc_diff_d0{},
58  m_mc_diff_z0{},
59  m_mc_diff_phi0{},
60  m_mc_diff_theta{},
61  m_mc_diff_qOverP{},
62 
63  m_mc_pull_d0{},
64  m_mc_pull_z0{},
65  m_mc_pull_phi0{},
66  m_mc_pull_theta{},
67  m_mc_pull_qOverP{},
68 
69  m_mc_particleID{},
70  m_mc_barcode{},
71  m_mc_truthTreeIndex{},
72  m_mc_energy{},
73 
74  m_mc_prob{}
75 
76 {
77  declareInterface<ITrackValidationNtupleTool>(this);
78  declareProperty("ExtrapolatorTool", m_extrapolator, "Extrapolator, eg for tracks without Perigee");
79  declareProperty("DoTruth", m_doTruth, "Toggle if to Write truth data");
80 
81 }
82 
83 // destructor
85 
86 
88 // initialize
91 
92  ATH_MSG_DEBUG ("nothing specific initialized in " << name());
93  return StatusCode::SUCCESS;
94 }
95 
97 // finalize
100 
101  ATH_MSG_DEBUG ("nothing specific finalized in " << name());
102  return StatusCode::SUCCESS;
103 }
104 
106  if (!tree) return StatusCode::FAILURE;
107  //-----------------
108  // add items *** Note: Documentation is in the header file, doxygen and wikis! ***
109  //
110  // reconstructed track parameters
111  tree->Branch("RecD0", &m_Rec_d0 );
112  tree->Branch("RecZ0", &m_Rec_z0 );
113  tree->Branch("RecPhi0", &m_Rec_phi0 );
114  tree->Branch("RecTheta", &m_Rec_theta );
115  tree->Branch("RecEta", &m_Rec_eta );
116  tree->Branch("RecQoverP", &m_Rec_qOverP );
117 
118  // errors of reconstructed track parameters
119  tree->Branch("RecErrD0", &m_errord0 );
120  tree->Branch("RecErrZ0", &m_errorz0 );
121  tree->Branch("RecErrPhi0", &m_errorphi0 );
122  tree->Branch("RecErrTheta", &m_errortheta0 );
123  tree->Branch("RecErrQoverP", &m_errorqoverp );
124 
125  // Truth entries
126  if ( m_doTruth ){
127  tree->Branch( "trk_Mc_d0", &m_mc_d0 );
128  tree->Branch( "trk_Mc_z0", &m_mc_z0 );
129  tree->Branch( "trk_Mc_phi0", &m_mc_phi0 );
130  tree->Branch( "trk_Mc_theta", &m_mc_theta );
131  tree->Branch( "trk_Mc_qOverP", &m_mc_qOverP );
132  tree->Branch( "trk_Mc_qOverPt", &m_mc_qOverPt );
133  tree->Branch( "trk_Mc_eta", &m_mc_eta );
134 
135  tree->Branch( "trk_Mc_diff_d0", &m_mc_diff_d0 );
136  tree->Branch( "trk_Mc_diff_z0", &m_mc_diff_z0 );
137  tree->Branch( "trk_Mc_diff_phi0", &m_mc_diff_phi0 );
138  tree->Branch( "trk_Mc_diff_theta", &m_mc_diff_theta );
139  tree->Branch( "trk_Mc_diff_qOverP", &m_mc_diff_qOverP );
140 
141  tree->Branch( "trk_Mc_pull_d0", &m_mc_pull_d0 );
142  tree->Branch( "trk_Mc_pull_z0", &m_mc_pull_z0 );
143  tree->Branch( "trk_Mc_pull_phi0", &m_mc_pull_phi0 );
144  tree->Branch( "trk_Mc_pull_theta", &m_mc_pull_theta );
145  tree->Branch( "trk_Mc_pull_qOverP", &m_mc_pull_qOverP );
146 
147  tree->Branch( "trk_Mc_particleID", &m_mc_particleID );
148  tree->Branch( "trk_Mc_barcode", &m_mc_barcode );
149  tree->Branch( "trk_Mc_energy", &m_mc_energy );
150  tree->Branch( "trk_Mc_prob", &m_mc_prob );
151  tree->Branch( "trk_Mc_truthTreeIndex",&m_mc_truthTreeIndex );
152  }
153 
154  // general track properties other than perigee are given through
155  // Trk::TrackInformationNtupleTool
156 
157  ATH_MSG_VERBOSE ("added branches to ntuple");
158  return StatusCode::SUCCESS;
159 }
160 
165  const Trk::Track& track,
166  const int /*iterationIndex*/,
167  const unsigned int /*fitStatCode*/ ) {
168  //const Trk::FitterStatusCode /*fitStatCode*/ ) const {
169 
170  ATH_MSG_VERBOSE ("in fillTrackData(trk, indx)");
171  const EventContext& ctx = Gaudi::Hive::currentContext();
172  //----------------------------------------------
173  // fill track parameters in ntuple
174  const Trk::Perigee* perpars = track.perigeeParameters();
175  if (perpars != nullptr && fillTrackPerigee(perpars).isFailure()) {
176  msg(MSG::WARNING) << "Perigee parameters could not be written to ntuple" << endmsg;
177  }
178  if (perpars == nullptr) {
179  if ( // track.info().author() == Trk::Track::SiSPSeededTrackFinder &&
180  !m_extrapolator.empty() ) {
181  ATH_MSG_VERBOSE ("try extrapolate SiSPSeeded track to perigee");
182  const Trk::PerigeeSurface perSurf;
183  std::unique_ptr<const Trk::TrackParameters> tmp = m_extrapolator->extrapolateTrack(
184  ctx, track, perSurf, Trk::anyDirection, false, Trk::nonInteracting);
185  if (tmp && tmp->associatedSurface().type() == Trk::SurfaceType::Perigee) {
186  perpars = static_cast<const Trk::Perigee *> (tmp.release());
187  }
188  if (perpars != nullptr && fillTrackPerigee(perpars).isFailure()) {
189  msg(MSG::WARNING) << "Newly made perigee parameters could not be "
190  << "written to ntuple" << endmsg;
191  }
192  if (perpars != nullptr) delete perpars;
193  } else
194  msg(MSG::WARNING) << "No perigee parameters, but they are the main validation object!"
195  << endmsg;
196  }
197  return StatusCode::SUCCESS;
198 }
199 
205 {
206 
207  //----------------------------------------------
208  // fill track parameters in ntuple
209  const Trk::Perigee* perpars = particle.perigee();
210  if (perpars != nullptr && fillTrackPerigee(perpars).isFailure())
211  ATH_MSG_WARNING ("Perigee parameters could not be written to ntuple");
212  return StatusCode::SUCCESS;
213 }
214 
216 // reset variables
219  // reset the counters
220 }
221 
222 StatusCode Trk::PerigeeParametersNtupleTool::fillTrackTruthData ( const TrackParameters*& truePerigee, const TrackTruth& trackTruth, const int indexInTruthTree )
223 {
224 
225  if ( !m_doTruth ) return StatusCode::SUCCESS;
226 
227  ATH_MSG_VERBOSE ("Starting fillTrackTruthData()");
228 
229  const HepMcParticleLink& particleLink = trackTruth.particleLink();
230 
231  m_mc_barcode = particleLink.barcode();
232  m_mc_prob=trackTruth.probability();
233  m_mc_truthTreeIndex = indexInTruthTree;
234 
235  HepMC::ConstGenParticlePtr genParticle = particleLink.cptr();
236 
237  if ( genParticle ){
238  m_mc_particleID = genParticle->pdg_id();
239  m_mc_energy = genParticle->momentum().e();
240  }
241 
242  if (truePerigee == nullptr) return StatusCode::SUCCESS; // fake fakes don't have true perigee
243 
244  m_mc_d0 = truePerigee->parameters()[Trk::d0];
245  m_mc_z0 = truePerigee->parameters()[Trk::z0];
246  m_mc_phi0 = truePerigee->parameters()[Trk::phi0];
247  m_mc_theta = truePerigee->parameters()[Trk::theta];
248  m_mc_qOverP = truePerigee->parameters()[Trk::qOverP];
249 
250  if ( sin( m_mc_theta ) != 0. )
251  m_mc_qOverPt = m_mc_qOverP / sin( m_mc_theta );
252 
253  m_mc_eta = truePerigee->eta();
254 
255  m_mc_diff_d0 = m_Rec_d0 - m_mc_d0;
256  m_mc_diff_z0 = m_Rec_z0 - m_mc_z0;
257  m_mc_diff_phi0 = m_Rec_phi0 - m_mc_phi0;
258  m_mc_diff_theta = m_Rec_theta - m_mc_theta;
259  m_mc_diff_qOverP = m_Rec_qOverP - m_mc_qOverP;
260 
261  m_mc_pull_d0 = m_errord0 ? m_mc_diff_d0 / m_errord0 : 0.;
262  m_mc_pull_z0 = m_errorz0 ? m_mc_diff_z0 / m_errorz0 : 0.;
263  m_mc_pull_phi0 = m_errorphi0 ? m_mc_diff_phi0 / m_errorphi0 : 0.;
264  m_mc_pull_theta = m_errortheta0 ? m_mc_diff_theta / m_errortheta0 : 0.;
265  m_mc_pull_qOverP = m_errorqoverp ? m_mc_diff_qOverP / m_errorqoverp : 0.;
266 
267  return StatusCode::SUCCESS;
268 
269 }
270 
275 
276  ATH_MSG_VERBOSE ("in fillTrackPerigee");
277 
278  // // get track parameters
279  // const Trk::Perigee *perigee = track->perigeeParameters();
280  if (!perigee) {
281  msg(MSG::WARNING) << "Something is wrong - track has no perigee at all!" << endmsg;
282  m_Rec_d0 = 0;
283  m_Rec_z0 = 0;
284  m_Rec_phi0 = 0;
285  m_Rec_theta = 0;
286  m_Rec_qOverP = 0;
287  return StatusCode::FAILURE;
288  }
289 
290  ATH_MSG_VERBOSE("Fill Perigee Parameters now");
291  // track parameters
292  m_Rec_d0 = perigee->parameters()[Trk::d0];
293  m_Rec_z0 = perigee->parameters()[Trk::z0];
294  m_Rec_phi0 = perigee->parameters()[Trk::phi0];
295  m_Rec_theta = perigee->parameters()[Trk::theta];
296  m_Rec_eta = perigee->eta();
297  m_Rec_qOverP = perigee->parameters()[Trk::qOverP];
298  ATH_MSG_DEBUG ("Trackparameters: q/p=" << m_Rec_qOverP);
299 
300  // get measured track parameters for errors
301  if (perigee->covariance()) {
302 
303  // error of track parameters
304  m_errord0 = Amg::error(*perigee->covariance(),Trk::d0);
305  m_errorz0 = Amg::error(*perigee->covariance(),Trk::z0);
306  m_errorphi0 = Amg::error(*perigee->covariance(),Trk::phi0);
307  m_errortheta0 = Amg::error(*perigee->covariance(),Trk::theta);
308  m_errorqoverp = Amg::error(*perigee->covariance(),Trk::qOverP);
309 
310 
311  } // end if measPerigee
312 
313  // TO-DO treat condition of no measured pars
314 
315  ATH_MSG_DEBUG ("Trackparameters: d0=" << m_Rec_d0 << ", z0=" << m_Rec_z0 << ", phi0=" << m_Rec_phi0 << ", theta=" << m_Rec_theta);
316 
317  return StatusCode::SUCCESS;
318 }
Trk::anyDirection
@ anyDirection
Definition: PropDirection.h:22
Trk::ParticleSwitcher::particle
constexpr ParticleHypothesis particle[PARTICLEHYPOTHESES]
the array of masses
Definition: ParticleHypothesis.h:76
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
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:70
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:222
Trk::PerigeeParametersNtupleTool::finalize
StatusCode finalize()
Definition: PerigeeParametersNtupleTool.cxx:99
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:27
GenParticle.h
Track.h
Trk::PerigeeParametersNtupleTool::initialize
StatusCode initialize()
Definition: PerigeeParametersNtupleTool.cxx:90
Trk::TrackParticleBase
Definition: TrackParticleBase.h:41
beamspotman.n
n
Definition: beamspotman.py:731
Trk::theta
@ theta
Definition: ParamDefs.h:72
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
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:105
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:164
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:192
Trk::d0
@ d0
Definition: ParamDefs.h:69
Trk::PerigeeParametersNtupleTool::fillTrackParticleData
virtual StatusCode fillTrackParticleData(const Trk::TrackParticleBase &)
fill ntuple data of a given TrackParticle without writing the record.
Definition: PerigeeParametersNtupleTool.cxx:204
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:218
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:274
Trk::qOverP
@ qOverP
perigee
Definition: ParamDefs.h:73
declareProperty
#define declareProperty(n, p, h)
Definition: BaseFakeBkgTool.cxx:15
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:71