ATLAS Offline Software
Loading...
Searching...
No Matches
InDetPhysValTruthDecoratorAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3*/
4
9
11#include "safeDecorator.h"
12#include <limits>
13
16
17#include "TDatabasePDG.h"
18#include "TParticlePDG.h"
19#include "TrkParameters/TrackParameters.h" // Contains typedef to Trk::CurvilinearParameters
20#include <cmath>
21
22// ref:
23// https://svnweb.cern.ch/trac/atlasoff/browser/Tracking/TrkEvent/TrkParametersBase/trunk/TrkParametersBase/CurvilinearParametersT.h
24
25
26
27
28
29InDetPhysValTruthDecoratorAlg::InDetPhysValTruthDecoratorAlg(const std::string& name, ISvcLocator* pSvcLocator) :
30 AthReentrantAlgorithm(name, pSvcLocator)
31{
32}
33
37
38StatusCode
40 ATH_CHECK(m_extrapolator.retrieve());
41 ATH_CHECK(m_beamSpotDecoKey.initialize());
42 ATH_CHECK( m_truthPixelClusterName.initialize() );
43 ATH_CHECK( m_truthSCTClusterName.initialize() );
44 ATH_CHECK( m_truthSelectionTool.retrieve( EnableTool { not m_truthSelectionTool.name().empty() } ) );
45 if (not m_truthSelectionTool.name().empty() ) {
46 m_cutFlow = CutFlow(m_truthSelectionTool->nCuts() );
47 }
48
49 ATH_CHECK( m_truthParticleName.initialize());
51
52 std::vector<std::string> decor_names(kNDecorators);
53 decor_names[kDecorD0]="d0";
54 decor_names[kDecorZ0]="z0";
55 decor_names[kDecorPhi]="phi";
56 decor_names[kDecorTheta]="theta";
57 decor_names[kDecorZ0st]="z0st";
58 decor_names[kDecorQOverP]="qOverP";
59 decor_names[kDecorProdR]="prodR";
60 decor_names[kDecorProdZ]="prodZ";
61 decor_names[kDecorNSilHits]="nSilHits";
62
64 assert( m_decor.size() == kNDecorators);
65 return StatusCode::SUCCESS;
66}
67
68StatusCode
70 if (not m_truthSelectionTool.name().empty()) {
71 std::lock_guard<std::mutex> lock(m_mutex);
72 ATH_MSG_DEBUG( "Truth selection cut flow : " << m_cutFlow.report(m_truthSelectionTool->names()) );
73 }
75 ATH_MSG_INFO( "Clusters which reference missing / thinned truth particles : " << m_nMissingTruthParticles );
76 }
77 return StatusCode::SUCCESS;
78}
79
80StatusCode
81InDetPhysValTruthDecoratorAlg::execute(const EventContext &ctx) const {
83 if ((not ptruth.isValid())) {
84 return StatusCode::FAILURE;
85 }
86 std::size_t ptruth_size=ptruth->size();
87
88 std::vector<unsigned int> truthIndexMap;
89 if (!m_truthParticleIndexDecor.empty()) {
91 unsigned int max_size=0;
92 assert( ptruth_size < std::numeric_limits<unsigned int>::max());
93 for (const xAOD::TruthParticle *truth_particle : *ptruth) {
94 max_size = std::max( max_size, decor_index(*truth_particle) );
95 }
96 if (max_size>=std::numeric_limits<unsigned int>::max()) {
97 ATH_MSG_ERROR("Truth index exceed max allowed range.");
98 return StatusCode::FAILURE;
99 }
100 ++max_size;
101 truthIndexMap.resize( max_size, std::numeric_limits<unsigned int>::max());
102 unsigned int new_index=0;
103 for (const xAOD::TruthParticle *truth_particle : *ptruth) {
104 truthIndexMap.at( decor_index(*truth_particle) ) = new_index;
105 ++new_index;
106 }
107 }
108 else {
109 truthIndexMap.reserve(ptruth_size);
110 for (unsigned int i=0; i<ptruth_size; ++i) {
111 truthIndexMap.push_back(i);
112 }
113 }
114
115 std::vector< IDPVM::OptionalDecoration<xAOD::TruthParticleContainer,float> >
116 float_decor( IDPVM::createDecoratorsIfNeeded(*ptruth, m_decor, ctx, msgLvl(MSG::DEBUG)) );
117
119 std::vector< std::array<uint16_t, kNClusterTypes> > tp_clustercount;
120 tp_clustercount.resize(ptruth_size,std::array<uint16_t,kNClusterTypes>{});
121 unsigned int missing_truth_particle=0u;
122 //Loop over the pixel and sct clusters to fill the truth barcode - cluster count maps
125 //only decorate the truth particles with truth silicon hits if both containers are available
126 if (sctClusters.isValid() && pixelClusters.isValid()) {
127 for (const auto *const sct : *sctClusters) {
128 const xAOD::TrackMeasurementValidation* sctCluster = sct;
129 static const SG::AuxElement::ConstAccessor< std::vector<unsigned int> > truthIndexAcc("truth_index");
130 if (truthIndexAcc.isAvailable(*sctCluster)) {
131 const std::vector<unsigned int> &truth_indices = truthIndexAcc(*sctCluster);
132 for (auto index : truth_indices) {
133 if (index != std::numeric_limits<unsigned int>::max()) {
134 if (index < truthIndexMap.size() && truthIndexMap[index] != std::numeric_limits<unsigned int>::max()) {
135 ++tp_clustercount.at(truthIndexMap[index])[kSCT];
136 }
137 else {
138 ++missing_truth_particle;
139 }
140 }
141 }
142 }
143 } // Loop over SCT clusters
144
145 for (const auto *const pix : *pixelClusters) {
146 const xAOD::TrackMeasurementValidation* pixCluster = pix;
147 static const SG::AuxElement::ConstAccessor< std::vector<unsigned int> > truthIndexAcc("truth_index");
148 if (truthIndexAcc.isAvailable(*pixCluster)) {
149 const std::vector<unsigned int> &truth_indices = truthIndexAcc(*pixCluster);
150 for (auto index : truth_indices) {
151 if (index != std::numeric_limits<unsigned int>::max()) {
152 if (index < truthIndexMap.size() && truthIndexMap[index] != std::numeric_limits<unsigned int>::max()) {
153 ++tp_clustercount.at(truthIndexMap[index])[kPixel];
154 }
155 else {
156 ++missing_truth_particle;
157 }
158 }
159 }
160 }
161 } // Loop over PIX clusters
162 }
163 m_nMissingTruthParticles += missing_truth_particle;
164
165 if (not float_decor.empty()) {
169 Amg::Vector3D beamPos = Amg::Vector3D(beamPosX(0), beamPosY(0), beamPosZ(0));
170
171 if ( m_truthSelectionTool.get() ) {
172 CutFlow tmp_cut_flow(m_truthSelectionTool->nCuts());
173 for (const xAOD::TruthParticle *truth_particle : *ptruth) {
174 auto passed = m_truthSelectionTool->accept(truth_particle);
175 tmp_cut_flow.update( passed.missingCuts() );
176 if (not passed) continue;
177 decorateTruth(*truth_particle, float_decor, beamPos, tp_clustercount);
178 }
179 std::lock_guard<std::mutex> lock(m_mutex);
180 m_cutFlow.merge(std::move(tmp_cut_flow));
181 }
182 else {
183 for (const xAOD::TruthParticle *truth_particle : *ptruth) {
184 decorateTruth(*truth_particle, float_decor, beamPos, tp_clustercount);
185 }
186 }
187 }
188 return StatusCode::SUCCESS;
189}
190
191
192
193bool
196 const Amg::Vector3D& beamPos,
197 const std::vector<std::array<uint16_t,kNClusterTypes> > &counts) const {
198 ATH_MSG_VERBOSE("Decorate truth with d0 etc");
199 if (particle.isNeutral()) {
200 return false;
201 }
202 const EventContext& ctx = Gaudi::Hive::currentContext();
203 const Amg::Vector3D momentum(particle.px(), particle.py(), particle.pz());
204 const int pid(particle.pdgId());
205 double charge = particle.charge();
206
207 if (std::isnan(charge)) {
208 ATH_MSG_DEBUG("charge not found on particle with pid " << pid);
209 return false;
210 }
211
212 // @TODO float?
213 float nSiHits = std::accumulate(counts.at(particle.index()).begin(), counts[particle.index()].end(), 0u);
214
215 IDPVM::decorateOrRejectQuietly(particle,float_decor[kDecorNSilHits], nSiHits);
216
217 const xAOD::TruthVertex* ptruthVertex(nullptr);
218 try{
219 ptruthVertex = particle.prodVtx();
220 } catch (const std::exception& e) {
221 if (not m_errorEmitted) {
222 ATH_MSG_WARNING("A non existent production vertex was requested in calculating the track parameters d0 etc");
223 }
224 m_errorEmitted = true;
225 return false;
226 }
227 if (!ptruthVertex) {
228 ATH_MSG_DEBUG("A production vertex pointer was retrieved, but it is NULL");
229 return false;
230 }
231 const auto xPos = ptruthVertex->x();
232 const auto yPos = ptruthVertex->y();
233 const auto z_truth = ptruthVertex->z();
234 const Amg::Vector3D position(xPos, yPos, z_truth);
235 const float prodR_truth = std::sqrt(xPos * xPos + yPos * yPos);
236 // delete ptruthVertex;ptruthVertex=0;
237 const Trk::CurvilinearParameters cParameters(position, momentum, charge);
238
239 Trk::PerigeeSurface persf(beamPos);
240
241 std::unique_ptr<const Trk::TrackParameters> tP ( m_extrapolator->extrapolate(ctx,
242 cParameters,
243 persf, Trk::anyDirection, false) );
244 if (tP) {
245 float d0_truth = tP->parameters()[Trk::d0];
246 float theta_truth = tP->parameters()[Trk::theta];
247 float z0_truth = tP->parameters()[Trk::z0];
248 float phi_truth = tP->parameters()[Trk::phi];
249 float qOverP_truth = tP->parameters()[Trk::qOverP]; // P or Pt ??
250 float z0st_truth = z0_truth * std::sin(theta_truth);
251
252 // 'safeDecorator' used to prevent a crash in case of adding something which pre-exists.
253 // behaviour chosen is to reject quietly
254 IDPVM::decorateOrRejectQuietly(particle,float_decor[kDecorD0],d0_truth);
255 IDPVM::decorateOrRejectQuietly(particle,float_decor[kDecorZ0],z0_truth);
256 IDPVM::decorateOrRejectQuietly(particle,float_decor[kDecorPhi],phi_truth);
257 IDPVM::decorateOrRejectQuietly(particle,float_decor[kDecorTheta],theta_truth);
258 IDPVM::decorateOrRejectQuietly(particle,float_decor[kDecorZ0st],z0st_truth);
259 IDPVM::decorateOrRejectQuietly(particle,float_decor[kDecorQOverP],qOverP_truth);
260 IDPVM::decorateOrRejectQuietly(particle,float_decor[kDecorProdR],prodR_truth);
261 IDPVM::decorateOrRejectQuietly(particle,float_decor[kDecorProdZ],z_truth);
262
263 return true;
264 } else {
265 ATH_MSG_DEBUG("The TrackParameters pointer for this TruthParticle is NULL");
266 return false;
267 }
268}
269
279
293
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
double charge(const T &p)
Definition AtlasPID.h:997
header file for class of same name
bool passed(DecisionID id, const DecisionIDContainer &)
checks if required decision ID is in the set of IDs in the container
Handle class for reading a decoration on an object.
bool msgLvl(const MSG::Level lvl) const
An algorithm that can be simultaneously executed in multiple threads.
void update(bool)
Definition CutFlow.h:192
PublicToolHandle< IAthSelectionTool > m_truthSelectionTool
SG::ReadDecorHandleKey< xAOD::TruthParticleContainer > m_truthParticleIndexDecor
PublicToolHandle< Trk::IExtrapolator > m_extrapolator
std::vector< std::pair< SG::WriteDecorHandleKey< xAOD::TruthParticleContainer >, SG::AuxElement::ConstAccessor< float > > > m_decor
virtual StatusCode execute(const EventContext &ctx) const
SG::ReadHandleKey< xAOD::TrackMeasurementValidationContainer > m_truthPixelClusterName
TruthPixelClusterContainer and TruthSCTClusterContainer needed for truth silicon hit cut.
std::atomic< std::size_t > m_nMissingTruthParticles
InDetPhysValTruthDecoratorAlg(const std::string &name, ISvcLocator *pSvcLocator)
SG::ReadHandleKey< xAOD::TrackMeasurementValidationContainer > m_truthSCTClusterName
SG::ReadDecorHandleKeyArray< xAOD::EventInfo > m_beamSpotDecoKey
Gaudi::Property< std::string > m_prefix
SG::ReadHandleKey< xAOD::TruthParticleContainer > m_truthParticleName
TruthParticle container's name needed to create decorators.
bool decorateTruth(const xAOD::TruthParticle &particle, std::vector< std::pair< SG::WriteDecorHandle< xAOD::TruthParticleContainer, float >, bool > > &float_decor, const Amg::Vector3D &beamPos, const std::vector< std::array< uint16_t, kNClusterTypes > > &counts) const
SG::ConstAccessor< T, ALLOC > ConstAccessor
Definition AuxElement.h:569
bool isAvailable(const ELT &e) const
Test to see if this variable exists in the store.
Handle class for reading a decoration on an object.
virtual bool isValid() override final
Can the handle be successfully dereferenced?
Class describing the Line to which the Perigee refers to.
float z() const
Vertex longitudinal distance along the beam line form the origin.
float y() const
Vertex y displacement.
float x() const
Vertex x displacement.
Eigen::Matrix< double, 3, 1 > Vector3D
void createDecoratorKeysAndAccessor(T_Parent &parent, const SG::ReadHandleKey< T_Cont > &container_key, const std::string &prefix, const std::vector< std::string > &decor_names, std::vector< WriteKeyAccessorPair< T_Cont, T > > &decor_out)
void decorateOrRejectQuietly(const T_Cont_Elm &particle, OptionalDecoration< T_Cont, T > &decorator, const T &value)
std::vector< OptionalDecoration< T_Cont, T > > createDecoratorsIfNeeded(const T_Cont &container, const std::vector< WriteKeyAccessorPair< T_Cont, T > > &keys, const EventContext &ctx, bool verbose=false)
std::pair< SG::WriteDecorHandle< ContainerType, VariableType >, bool > OptionalDecoration
@ anyDirection
CurvilinearParametersT< TrackParametersDim, Charged, PlaneSurface > CurvilinearParameters
@ theta
Definition ParamDefs.h:66
@ qOverP
perigee
Definition ParamDefs.h:67
@ phi
Definition ParamDefs.h:75
@ d0
Definition ParamDefs.h:63
@ z0
Definition ParamDefs.h:64
Definition index.py:1
TrackMeasurementValidation_v1 TrackMeasurementValidation
Reference the current persistent version:
TruthVertex_v1 TruthVertex
Typedef to implementation.
Definition TruthVertex.h:15
TruthParticle_v1 TruthParticle
Typedef to implementation.
implementation file for function of same name