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#include "GaudiKernel/PhysicalConstants.h"
17
18#include "TDatabasePDG.h"
19#include "TParticlePDG.h"
20#include "TrkParameters/TrackParameters.h" // Contains typedef to Trk::CurvilinearParameters
21#include <cmath>
22
23// ref:
24// https://svnweb.cern.ch/trac/atlasoff/browser/Tracking/TrkEvent/TrkParametersBase/trunk/TrkParametersBase/CurvilinearParametersT.h
25
26InDetPhysValTruthDecoratorAlg::InDetPhysValTruthDecoratorAlg(const std::string& name, ISvcLocator* pSvcLocator) :
27 AthReentrantAlgorithm(name, pSvcLocator)
28{
29}
30
34
35StatusCode
37 ATH_CHECK(m_extrapolator.retrieve());
38 ATH_CHECK(m_beamSpotDecoKey.initialize());
39 ATH_CHECK( m_truthPixelClusterName.initialize() );
40 ATH_CHECK( m_truthSCTClusterName.initialize() );
41 ATH_CHECK( m_truthSelectionTool.retrieve( EnableTool { not m_truthSelectionTool.name().empty() } ) );
42 if (not m_truthSelectionTool.name().empty() ) {
43 m_cutFlow = CutFlow(m_truthSelectionTool->nCuts() );
44 }
45
46 ATH_CHECK( m_truthParticleName.initialize());
48
49 ATH_CHECK(m_truthEventName.initialize(!m_truthEventName.key().empty()));
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 decor_names[kDecorTime]="time";
63
65 assert( m_decor.size() == kNDecorators);
66 return StatusCode::SUCCESS;
67}
68
69StatusCode
71 if (not m_truthSelectionTool.name().empty()) {
72 std::lock_guard<std::mutex> lock(m_mutex);
73 ATH_MSG_DEBUG( "Truth selection cut flow : " << m_cutFlow.report(m_truthSelectionTool->names()) );
74 }
76 ATH_MSG_INFO( "Clusters which reference missing / thinned truth particles : " << m_nMissingTruthParticles );
77 }
78 return StatusCode::SUCCESS;
79}
80
81StatusCode
82InDetPhysValTruthDecoratorAlg::execute(const EventContext &ctx) const {
84 if ((not ptruth.isValid())) {
85 return StatusCode::FAILURE;
86 }
87
88 std::size_t ptruth_size=ptruth->size();
89
90 std::vector<unsigned int> truthIndexMap;
91 if (!m_truthParticleIndexDecor.empty()) {
93 unsigned int max_size=0;
94 assert( ptruth_size < std::numeric_limits<unsigned int>::max());
95 for (const xAOD::TruthParticle *truth_particle : *ptruth) {
96 max_size = std::max( max_size, decor_index(*truth_particle) );
97 }
98 if (max_size>=std::numeric_limits<unsigned int>::max()) {
99 ATH_MSG_ERROR("Truth index exceed max allowed range.");
100 return StatusCode::FAILURE;
101 }
102 ++max_size;
103 truthIndexMap.resize( max_size, std::numeric_limits<unsigned int>::max());
104 unsigned int new_index=0;
105 for (const xAOD::TruthParticle *truth_particle : *ptruth) {
106 truthIndexMap.at( decor_index(*truth_particle) ) = new_index;
107 ++new_index;
108 }
109 }
110 else {
111 truthIndexMap.reserve(ptruth_size);
112 for (unsigned int i=0; i<ptruth_size; ++i) {
113 truthIndexMap.push_back(i);
114 }
115 }
116
117 std::vector< IDPVM::OptionalDecoration<xAOD::TruthParticleContainer,float> >
118 float_decor( IDPVM::createDecoratorsIfNeeded(*ptruth, m_decor, ctx, msgLvl(MSG::DEBUG)) );
119
121 std::vector< std::array<uint16_t, kNClusterTypes> > tp_clustercount;
122 tp_clustercount.resize(ptruth_size,std::array<uint16_t,kNClusterTypes>{});
123 unsigned int missing_truth_particle=0u;
124 //Loop over the pixel and sct clusters to fill the truth barcode - cluster count maps
127 //only decorate the truth particles with truth silicon hits if both containers are available
128 if (sctClusters.isValid() && pixelClusters.isValid()) {
129 for (const auto *const sct : *sctClusters) {
130 const xAOD::TrackMeasurementValidation* sctCluster = sct;
131 static const SG::AuxElement::ConstAccessor< std::vector<unsigned int> > truthIndexAcc("truth_index");
132 if (truthIndexAcc.isAvailable(*sctCluster)) {
133 const std::vector<unsigned int> &truth_indices = truthIndexAcc(*sctCluster);
134 for (auto index : truth_indices) {
135 if (index != std::numeric_limits<unsigned int>::max()) {
136 if (index < truthIndexMap.size() && truthIndexMap[index] != std::numeric_limits<unsigned int>::max()) {
137 ++tp_clustercount.at(truthIndexMap[index])[kSCT];
138 }
139 else {
140 ++missing_truth_particle;
141 }
142 }
143 }
144 }
145 } // Loop over SCT clusters
146
147 for (const auto *const pix : *pixelClusters) {
148 const xAOD::TrackMeasurementValidation* pixCluster = pix;
149 static const SG::AuxElement::ConstAccessor< std::vector<unsigned int> > truthIndexAcc("truth_index");
150 if (truthIndexAcc.isAvailable(*pixCluster)) {
151 const std::vector<unsigned int> &truth_indices = truthIndexAcc(*pixCluster);
152 for (auto index : truth_indices) {
153 if (index != std::numeric_limits<unsigned int>::max()) {
154 if (index < truthIndexMap.size() && truthIndexMap[index] != std::numeric_limits<unsigned int>::max()) {
155 ++tp_clustercount.at(truthIndexMap[index])[kPixel];
156 }
157 else {
158 ++missing_truth_particle;
159 }
160 }
161 }
162 }
163 } // Loop over PIX clusters
164 }
165 m_nMissingTruthParticles += missing_truth_particle;
166
167 if (not float_decor.empty()) {
171 Amg::Vector3D beamPos = Amg::Vector3D(beamPosX(0), beamPosY(0), beamPosZ(0));
172
173 if ( m_truthSelectionTool.get() ) {
174 CutFlow tmp_cut_flow(m_truthSelectionTool->nCuts());
175 for (const xAOD::TruthParticle *truth_particle : *ptruth) {
176 auto passed = m_truthSelectionTool->accept(truth_particle);
177 tmp_cut_flow.update( passed.missingCuts() );
178 if (not passed) continue;
179 decorateTruth(*truth_particle, float_decor, beamPos, tp_clustercount);
180 }
181 std::lock_guard<std::mutex> lock(m_mutex);
182 m_cutFlow.merge(std::move(tmp_cut_flow));
183 }
184 else {
185 for (const xAOD::TruthParticle *truth_particle : *ptruth) {
186 decorateTruth(*truth_particle, float_decor, beamPos, tp_clustercount);
187 }
188 }
189 }
190
191 if (!decorateTruthTime(float_decor)) {
192 return StatusCode::FAILURE;
193 }
194
195 return StatusCode::SUCCESS;
196}
197
198bool
201 const Amg::Vector3D& beamPos,
202 const std::vector<std::array<uint16_t,kNClusterTypes> > &counts) const {
203 ATH_MSG_VERBOSE("Decorate truth with d0 etc");
204 if (particle.isNeutral()) {
205 return false;
206 }
207 const EventContext& ctx = Gaudi::Hive::currentContext();
208 const Amg::Vector3D momentum(particle.px(), particle.py(), particle.pz());
209 const int pid(particle.pdgId());
210 double charge = particle.charge();
211
212 if (std::isnan(charge)) {
213 ATH_MSG_DEBUG("charge not found on particle with pid " << pid);
214 return false;
215 }
216
217 // @TODO float?
218 float nSiHits = std::accumulate(counts.at(particle.index()).begin(), counts[particle.index()].end(), 0u);
219
220 IDPVM::decorateOrRejectQuietly(particle,float_decor[kDecorNSilHits], nSiHits);
221
222 const xAOD::TruthVertex* ptruthVertex(nullptr);
223 try{
224 ptruthVertex = particle.prodVtx();
225 } catch (const std::exception& e) {
226 if (not m_errorEmitted) {
227 ATH_MSG_WARNING("A non existent production vertex was requested in calculating the track parameters d0 etc");
228 }
229 m_errorEmitted = true;
230 return false;
231 }
232 if (!ptruthVertex) {
233 ATH_MSG_DEBUG("A production vertex pointer was retrieved, but it is NULL");
234 return false;
235 }
236 const auto xPos = ptruthVertex->x();
237 const auto yPos = ptruthVertex->y();
238 const auto z_truth = ptruthVertex->z();
239 const Amg::Vector3D position(xPos, yPos, z_truth);
240 const float prodR_truth = std::sqrt(xPos * xPos + yPos * yPos);
241 // delete ptruthVertex;ptruthVertex=0;
242 const Trk::CurvilinearParameters cParameters(position, momentum, charge);
243
244 Trk::PerigeeSurface persf(beamPos);
245
246 std::unique_ptr<const Trk::TrackParameters> tP ( m_extrapolator->extrapolate(ctx,
247 cParameters,
248 persf, Trk::anyDirection, false) );
249 if (tP) {
250 float d0_truth = tP->parameters()[Trk::d0];
251 float theta_truth = tP->parameters()[Trk::theta];
252 float z0_truth = tP->parameters()[Trk::z0];
253 float phi_truth = tP->parameters()[Trk::phi];
254 float qOverP_truth = tP->parameters()[Trk::qOverP]; // P or Pt ??
255 float z0st_truth = z0_truth * std::sin(theta_truth);
256
257 // 'safeDecorator' used to prevent a crash in case of adding something which pre-exists.
258 // behaviour chosen is to reject quietly
259 IDPVM::decorateOrRejectQuietly(particle,float_decor[kDecorD0],d0_truth);
260 IDPVM::decorateOrRejectQuietly(particle,float_decor[kDecorZ0],z0_truth);
261 IDPVM::decorateOrRejectQuietly(particle,float_decor[kDecorPhi],phi_truth);
262 IDPVM::decorateOrRejectQuietly(particle,float_decor[kDecorTheta],theta_truth);
263 IDPVM::decorateOrRejectQuietly(particle,float_decor[kDecorZ0st],z0st_truth);
264 IDPVM::decorateOrRejectQuietly(particle,float_decor[kDecorQOverP],qOverP_truth);
265 IDPVM::decorateOrRejectQuietly(particle,float_decor[kDecorProdR],prodR_truth);
266 IDPVM::decorateOrRejectQuietly(particle,float_decor[kDecorProdZ],z_truth);
267
268 return true;
269 } else {
270 ATH_MSG_DEBUG("The TrackParameters pointer for this TruthParticle is NULL");
271 return false;
272 }
273}
274
275bool
277
278 const EventContext& ctx = Gaudi::Hive::currentContext();
279
280 const xAOD::TruthVertex* truthVtx = nullptr;
281 float truthTime;
282
283 // First HS event
284 if (!m_truthEventName.key().empty()) {
285 ATH_MSG_VERBOSE("Getting TruthEventContainer");
287 if (!truthEventContainer.isPresent()) {
288 ATH_MSG_WARNING("TruthEventContainer name was specified, but no container is present");
289 return true;
290 }
291 const xAOD::TruthEvent* event = (truthEventContainer.isValid()) ? truthEventContainer->at(0) : nullptr;
292 if (event) {
293 truthVtx = event->signalProcessVertex();
294 truthTime = (truthVtx) ? truthVtx->t() / Gaudi::Units::c_light : -9999.;
295 for (const auto& link : event->truthParticleLinks()) {
296 if (link.isValid()) {
297 IDPVM::decorateOrRejectQuietly(**link, float_decor[kDecorTime], truthTime);
298 }
299 }
300 }
301 else {
302 ATH_MSG_ERROR("No valid TruthEvent!");
303 return false;
304 }
305 }
306
307 // Then PU events
308 if (!m_truthPileupEventName.key().empty()) {
309 ATH_MSG_VERBOSE("Getting TruthPileupEventContainer");
311 if (!truthPileupEventContainer.isPresent()) {
312 ATH_MSG_WARNING("TruthPileupEventContainer name was specified, but no container is present");
313 return true;
314 }
315 if (truthPileupEventContainer.isValid()) {
316 for (const auto event : *truthPileupEventContainer) {
317 truthVtx = nullptr;
318 for (std::size_t i = 0; i < event->nTruthVertices(); i++) {
319 truthVtx = event->truthVertex(i);
320 if (truthVtx) {
321 break;
322 }
323 }
324 truthTime = (truthVtx) ? truthVtx->t() / Gaudi::Units::c_light: -9999.;
325 for (const auto& link : event->truthParticleLinks()) {
326 if (link.isValid()) {
327 IDPVM::decorateOrRejectQuietly(**link, float_decor[kDecorTime], truthTime);
328 }
329 }
330 }
331 }
332 else {
333 ATH_MSG_ERROR("TruthPileupEventContainer is invalid!");
334 return false;
335 }
336 }
337
338 return true;
339
340}
341
351
365
#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
SG::ReadHandleKey< xAOD::TruthEventContainer > m_truthEventName
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::TruthPileupEventContainer > m_truthPileupEventName
bool decorateTruthTime(std::vector< std::pair< SG::WriteDecorHandle< xAOD::TruthParticleContainer, float >, bool > > &float_decor) const
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:570
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?
bool isPresent() const
Is the referenced object present in SG?
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 t() const
Vertex time.
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
TruthEvent_v1 TruthEvent
Typedef to implementation.
Definition TruthEvent.h:17
TruthParticle_v1 TruthParticle
Typedef to implementation.
implementation file for function of same name