ATLAS Offline Software
ResidualValidationNtupleHelper.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 // ResidualValidationNtupleHelper.cxx
7 // Source file for class ResidualValidationNtupleHelper
9 // (c) ATLAS Detector software
11 // Sebastian.Fleischmann@cern.ch
13 
14 
15 #include "TBranch.h"
16 #include "TTree.h"
17 // Trk
20 
23 
26 //#include "TrkEventPrimitives/ErrorMatrix.h"
27 //#include "TrkEventPrimitives/CovarianceMatrix.h"
28 //#include "TrkParameters/MeasuredTrackParameters.h"
29 
30 // constructor
32  const std::string& t,
33  const std::string& n,
34  const IInterface* p )
35  :
36  AthAlgTool(t,n,p),
37  m_trkParametersWarning(false),
38  m_residualPullCalculator("Trk::ResidualPullCalculator/ResidualPullCalculator"),
39  m_isUnbiased(nullptr),
40  m_PixPullPhi(nullptr), m_PixPullEta(nullptr), m_PixResPhi(nullptr), m_PixResEta(nullptr),
41  m_SCTPull(nullptr), m_SCTRes(nullptr),
42  m_TRTPull(nullptr), m_TRTRes(nullptr),
43  m_MDTPull(nullptr), m_MDTRes(nullptr),
44  m_RPCPull(nullptr), m_RPCRes(nullptr),
45  m_TGCPull(nullptr), m_TGCRes(nullptr),
46  m_CSCPull(nullptr), m_CSCRes(nullptr)
47 {
48  declareInterface<IValidationNtupleHelperTool>(this);
49  // Declare the properties
50  declareProperty("IgnoreMissingTrackCovariance", m_ignoreMissTrkCov = false);
51  declareProperty("ResidualPullCalculatorTool", m_residualPullCalculator, "Tool to calculate residuals and pulls");
52  // declareProperty("NtupleDirectoryName", m_ntupleDirName = "FitterValidation");
53  // declareProperty("NtupleTreeName", m_ntupleTreeName = "Validation");
54  // declareProperty("DoPixels", mjo_doPixels = true);
55  // declareProperty("DoTRT", mjo_doTRT = true);
56 }
57 
58 // destructor
60 
65 
66 
68  if (sc.isFailure()) return sc;
69 
70  // get the ResidualPullCalculator
71  sc = m_residualPullCalculator.retrieve();
72  if (sc.isFailure()) {
73  ATH_MSG_ERROR ("Could not retrieve "<< m_residualPullCalculator <<" (to calculate residuals,pulls) ");
74  return sc;
75  }
76  m_PixPullPhi = new std::vector<float>();
77  m_PixPullEta = new std::vector<float>();
78  m_PixResPhi = new std::vector<float>();
79  m_PixResEta = new std::vector<float>();
80  m_SCTPull = new std::vector<float>();
81  m_SCTRes = new std::vector<float>();
82  m_TRTPull = new std::vector<float>();
83  m_TRTRes = new std::vector<float>();
84  m_MDTPull = new std::vector<float>();
85  m_MDTRes = new std::vector<float>();
86  m_RPCPull = new std::vector<float>();
87  m_RPCRes = new std::vector<float>();
88  m_TGCPull = new std::vector<float>();
89  m_TGCRes = new std::vector<float>();
90  m_CSCPull = new std::vector<float>();
91  m_CSCRes = new std::vector<float>();
92 
93  return sc;
94 }
95 
100 
101  delete m_PixPullPhi; delete m_PixPullEta;
102  delete m_PixResPhi; delete m_PixResEta;
103  delete m_SCTPull; delete m_SCTRes;
104  delete m_TRTPull; delete m_TRTRes;
105  delete m_MDTPull; delete m_MDTRes;
106  delete m_RPCPull; delete m_RPCRes;
107  delete m_TGCPull; delete m_TGCRes;
108  delete m_CSCPull; delete m_CSCRes;
109 
110  ATH_MSG_INFO ("finalize() successful in " << name() );
111  return StatusCode::SUCCESS;
112 }
113 
118  TTree* tree,
119  const int& detectorType ) {
120 
121  TBranch* trackStatesUnbiasedBranch = tree->GetBranch("TrackStatesUnbiased");
122  if (!trackStatesUnbiasedBranch) {
123  ATH_MSG_ERROR ( "Unable to get Branch TrackStatesUnbiased in ntuple" );
124  return StatusCode::FAILURE;
125  }
126  void* variableAdr = static_cast<void*>(trackStatesUnbiasedBranch->GetAddress());
127  if (!variableAdr) {
128  ATH_MSG_ERROR ("Unable to get variable address of Branch TrackStatesUnbiased");
129  return StatusCode::FAILURE;
130  }
131  ATH_MSG_VERBOSE ("Address of unbiasedState tree variable: " << variableAdr );
132  m_isUnbiased = static_cast<int*>(variableAdr);
133 
134 // TBranch* nHitsBranch = m_nt->GetBranch("nHits");
135 // variableAdr = static_cast<void*>(nHitsBranch->GetAddress());
136 // m_hits = static_cast<int*>(variableAdr);
137 
138  // -------------------------------------------
139  // retrieve and add items from/to the ntuple
140  // if (detectorType.test(Pixel)) {
141  if (detectorType==Trk::TrackState::Pixel) {
142  ATH_MSG_INFO ( "adding Pixel residual/pull variables to ntuple ");
143  // add items for pixel
144  tree->Branch("PixPullPhi", &m_PixPullPhi);
145  tree->Branch("PixPullEta", &m_PixPullEta);
146  tree->Branch("PixResPhi", &m_PixResPhi);
147  tree->Branch("PixResEta", &m_PixResEta);
148  }
149 
150  //if (detectorType.test(TRT)) {
151  if (detectorType==Trk::TrackState::TRT) {
152  ATH_MSG_INFO ("adding TRT residual/pull variables to ntuple ");
153  tree->Branch("TRTPull", &m_TRTPull);
154  tree->Branch("TRTRes", &m_TRTRes);
155  }
156  if (detectorType==Trk::TrackState::SCT) {
157  ATH_MSG_INFO ( "adding SCT residual/pull variables to ntuple ");
158  tree->Branch("SCTPull", &m_SCTPull);
159  tree->Branch("SCTRes", &m_SCTRes);
160  }
161 
162  if (detectorType==Trk::TrackState::MDT) {
163  ATH_MSG_INFO ( "adding MDT residual/pull variables to ntuple ");
164  // add items for MDT
165  tree->Branch("MDTPull", &m_MDTPull);
166  tree->Branch("MDTRes", &m_MDTRes);
167  }
168 
169  if (detectorType==Trk::TrackState::RPC) {
170  ATH_MSG_INFO ("adding RPC residual/pull variables to ntuple ");
171  ATH_MSG_INFO ("Reminder: these variables do not distinguish RPC phi from eta hits.");
172  // add items for RPC
173  tree->Branch("RPCPull", &m_RPCPull);
174  tree->Branch("RPCRes", &m_RPCRes);
175  }
176 
177  if (detectorType==Trk::TrackState::TGC) {
178  ATH_MSG_INFO ( "adding TGC residual/pull variables to ntuple ");
179  ATH_MSG_INFO ( "Reminder: these variables do not distinguish TGC phi from eta hits.");
180  // add items for TGC
181  tree->Branch("TGCPull", &m_TGCPull);
182  tree->Branch("TGCRes", &m_TGCRes);
183  }
184 
185  if (detectorType==Trk::TrackState::CSC) {
186  ATH_MSG_INFO ( "adding CSC residual/pull variables to ntuple ");
187  ATH_MSG_INFO ( "Reminder: these variables do not distinguish CSC phi from eta hits.");
188  // add items for CSC
189  tree->Branch("CSCPull", &m_CSCPull);
190  tree->Branch("CSCRes", &m_CSCRes);
191  }
192 
193  ATH_MSG_VERBOSE ( "added items to ntuple") ;
194  return StatusCode::SUCCESS;
195 }
196 
201  const Trk::MeasurementBase* measurement,
202  const Trk::TrackParameters* trkParameters,
203  //const NTuple::Item<long>& numberOfHits,
204  //const std::bitset<8>& detectorType
205  const int& detectorType,
206  const bool& isOutlier ) {
207 
208  //const Trk::MeasuredTrackParameters *measuredTrkParameters = dynamic_cast<const Trk::MeasuredTrackParameters*>(trkParameters);
209  if (!trkParameters) {
210  if (!isOutlier) {
211  // outliers usually do have not track parameters, so do not warn for them!
212  if(!m_trkParametersWarning) {
213  // warn once only!
214  ATH_MSG_WARNING ("No TrackParameters: Residuals cannot be filled (filling dummy values).");
215  ATH_MSG_WARNING (" This is possible for slimmed tracks; if residuals are needed choose another track collection");
216  ATH_MSG_WARNING (" (further warnings will be suppressed)");
217  m_trkParametersWarning = true;
218  }
219  }
220  fillValues(detectorType);
221  return StatusCode::SUCCESS;
222  } // end if (!trkPar)
223  /* open up to more measurement flavours
224  const Trk::RIO_OnTrack *ROT = dynamic_cast<const Trk::RIO_OnTrack*>(measurement);
225  if (!ROT) {
226  msg(MSG::WARNING) << "No RIO_OnTrack: Residuals cannot be calculated (filling dummy values)." << endmsg;
227  fillValues(detectorType);
228  return StatusCode::SUCCESS;
229  } */
230  std::optional<Trk::ResidualPull> residualPull=std::nullopt;
231  if (detectorType!=TrackState::unidentified) {
232  residualPull = m_residualPullCalculator->residualPull(measurement, trkParameters,
234  } else {
235  ATH_MSG_WARNING ("Detector type could not be identified, can not calculate residuals!");
236  fillValues(detectorType);
237  return StatusCode::FAILURE;
238  }
239  if (!residualPull) {
240  ATH_MSG_WARNING ("Residual Pull Calculator did not succeed!");
241  fillValues(detectorType);
242  return StatusCode::FAILURE;
243  }
244 
245  if (detectorType==TrackState::Pixel) {
246  if ((residualPull->isPullValid()) || m_ignoreMissTrkCov ) {
247  fillValues( detectorType,
248  residualPull->residual()[Trk::distPhi],
249  residualPull->pull()[Trk::distPhi],
250  residualPull->residual()[Trk::distEta],
251  residualPull->pull()[Trk::distEta] );
252  } else {
253  givePullWarning();
254  fillValues( detectorType,
255  residualPull->residual()[Trk::distPhi],
256  -1000.,
257  residualPull->residual()[Trk::distEta],
258  -1000. );
259  }
260  } else {
261  if ((residualPull->isPullValid()) || m_ignoreMissTrkCov ) {
262  fillValues( detectorType,
263  residualPull->residual()[Trk::loc1],
264  residualPull->pull()[Trk::loc1] );
265  } else {
266  givePullWarning();
267  fillValues( detectorType,
268  residualPull->residual()[Trk::loc1],
269  -1000. );
270  }
271  }
272 
273  return StatusCode::SUCCESS;
274 }
275 
277  const int& detectorType ) {
278  if (detectorType==TrackState::Pixel) {
279  m_PixPullPhi->clear();
280  m_PixPullEta->clear();
281  m_PixResPhi->clear();
282  m_PixResEta->clear();
283  } else if (detectorType==TrackState::SCT) {
284  m_SCTPull->clear();
285  m_SCTRes->clear();
286  } else if (detectorType==TrackState::TRT) {
287  m_TRTRes->clear();
288  m_TRTPull->clear();
289  } else if (detectorType==TrackState::MDT) {
290  m_MDTRes->clear();
291  m_MDTPull->clear();
292  } else if (detectorType==TrackState::RPC) {
293  m_RPCRes->clear();
294  m_RPCPull->clear();
295  } else if (detectorType==TrackState::TGC) {
296  m_TGCRes->clear();
297  m_TGCPull->clear();
298  } else if (detectorType==TrackState::CSC) {
299  m_CSCRes->clear();
300  m_CSCPull->clear();
301  }
302  return StatusCode::SUCCESS;
303 }
304 
305 
308  const int&) {
309  // we do nothing with holes
310  return StatusCode::SUCCESS;
311 }
Trk::TrackState::RPC
@ RPC
Definition: TrackStateDefs.h:33
ResidualValidationNtupleHelper.h
Trk::TrackState::Pixel
@ Pixel
Definition: TrackStateDefs.h:28
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
Trk::TrackState::TRT
@ TRT
Definition: TrackStateDefs.h:30
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
Trk::TrackState::MDT
@ MDT
Definition: TrackStateDefs.h:31
initialize
void initialize()
Definition: run_EoverP.cxx:894
tree
TChain * tree
Definition: tile_monitor.h:30
Trk::ResidualValidationNtupleHelper::ResidualValidationNtupleHelper
ResidualValidationNtupleHelper(const std::string &, const std::string &, const IInterface *)
Definition: ResidualValidationNtupleHelper.cxx:31
Trk::ResidualPull::Unbiased
@ Unbiased
RP with track state that has measurement not included.
Definition: ResidualPull.h:57
TrackStateData.h
Trk::TrackState::TGC
@ TGC
Definition: TrackStateDefs.h:34
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
Trk::ResidualValidationNtupleHelper::fillMeasurementData
virtual StatusCode fillMeasurementData(const Trk::MeasurementBase *, const Trk::TrackParameters *, const int &detectorType, const bool &isOutlier)
fill residual data
Definition: ResidualValidationNtupleHelper.cxx:200
Trk::TrackState::unidentified
@ unidentified
Definition: TrackStateDefs.h:27
Trk::ResidualValidationNtupleHelper::initialize
StatusCode initialize()
initialize
Definition: ResidualValidationNtupleHelper.cxx:64
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
Trk::ResidualValidationNtupleHelper::finalize
StatusCode finalize()
finalize
Definition: ResidualValidationNtupleHelper.cxx:99
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
Trk::ResidualValidationNtupleHelper::m_residualPullCalculator
ToolHandle< Trk::IResidualPullCalculator > m_residualPullCalculator
The residual and pull calculator tool.
Definition: ResidualValidationNtupleHelper.h:86
ResidualPull.h
Trk::distEta
@ distEta
readout for silicon
Definition: ParamDefs.h:57
beamspotman.n
n
Definition: beamspotman.py:731
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
Trk::ParametersBase
Definition: ParametersBase.h:55
Trk::ResidualValidationNtupleHelper::addNtupleItems
virtual StatusCode addNtupleItems(TTree *tree, const int &detectorType)
add items to the ntuple and configure the helper tool: should be called once (per detector type) by t...
Definition: ResidualValidationNtupleHelper.cxx:117
Trk::ResidualValidationNtupleHelper::fillHoleData
virtual StatusCode fillHoleData(const Trk::TrackStateOnSurface &, const int &)
fill special data about holes on track (here: do nothing)
Definition: ResidualValidationNtupleHelper.cxx:306
Trk::distPhi
@ distPhi
Definition: ParamDefs.h:56
Trk::MeasurementBase
Definition: MeasurementBase.h:58
Trk::TrackStateOnSurface
represents the track state (measurement, material, fit parameters and quality) at a surface.
Definition: TrackStateOnSurface.h:71
Trk::ResidualValidationNtupleHelper::resetVariables
virtual StatusCode resetVariables(const int &detectorType)
reset ntuple variables
Definition: ResidualValidationNtupleHelper.cxx:276
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
RIO_OnTrack.h
Trk::ResidualValidationNtupleHelper::m_ignoreMissTrkCov
bool m_ignoreMissTrkCov
Definition: ResidualValidationNtupleHelper.h:88
LocalParameters.h
IResidualPullCalculator.h
Trk::ResidualPull::Biased
@ Biased
RP with track state including the hit.
Definition: ResidualPull.h:55
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
Trk::ResidualValidationNtupleHelper::~ResidualValidationNtupleHelper
~ResidualValidationNtupleHelper()
Trk::TrackState::CSC
@ CSC
Definition: TrackStateDefs.h:32
Trk::TrackState::SCT
@ SCT
Definition: TrackStateDefs.h:29
AthAlgTool
Definition: AthAlgTool.h:26
Trk::loc1
@ loc1
Definition: ParamDefs.h:40