ATLAS Offline Software
Loading...
Searching...
No Matches
InDetPhysValTruthDecoratorAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 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
42 ATH_CHECK( m_truthParticleName.initialize());
44
45 ATH_CHECK(m_truthEventName.initialize(!m_truthEventName.key().empty()));
47
48 std::vector<std::string> decor_names(kNDecorators);
49 decor_names[kDecorD0]="d0";
50 decor_names[kDecorZ0]="z0";
51 decor_names[kDecorPhi]="phi";
52 decor_names[kDecorTheta]="theta";
53 decor_names[kDecorZ0st]="z0st";
54 decor_names[kDecorQOverP]="qOverP";
55 decor_names[kDecorProdR]="prodR";
56 decor_names[kDecorProdZ]="prodZ";
57 decor_names[kDecorNSilHits]="nSilHits";
58 decor_names[kDecorTime]="time";
59
61 assert( m_decor.size() == kNDecorators);
62 return StatusCode::SUCCESS;
63}
64
65StatusCode
68 ATH_MSG_INFO( "Clusters which reference missing / thinned truth particles : " << m_nMissingTruthParticles );
69 }
70 return StatusCode::SUCCESS;
71}
72
73StatusCode
74InDetPhysValTruthDecoratorAlg::execute(const EventContext &ctx) const {
76 if ((not ptruth.isValid())) {
77 return StatusCode::FAILURE;
78 }
79
80 std::size_t ptruth_size=ptruth->size();
81
82 std::vector<unsigned int> truthIndexMap;
83 if (!m_truthParticleIndexDecor.empty()) {
85 unsigned int max_size=0;
86 assert( ptruth_size < std::numeric_limits<unsigned int>::max());
87 for (const xAOD::TruthParticle *truth_particle : *ptruth) {
88 max_size = std::max( max_size, decor_index(*truth_particle) );
89 }
90 if (max_size>=std::numeric_limits<unsigned int>::max()) {
91 ATH_MSG_ERROR("Truth index exceed max allowed range.");
92 return StatusCode::FAILURE;
93 }
94 ++max_size;
95 truthIndexMap.resize( max_size, std::numeric_limits<unsigned int>::max());
96 unsigned int new_index=0;
97 for (const xAOD::TruthParticle *truth_particle : *ptruth) {
98 truthIndexMap.at( decor_index(*truth_particle) ) = new_index;
99 ++new_index;
100 }
101 }
102 else {
103 truthIndexMap.reserve(ptruth_size);
104 for (unsigned int i=0; i<ptruth_size; ++i) {
105 truthIndexMap.push_back(i);
106 }
107 }
108
109 std::vector< IDPVM::OptionalDecoration<xAOD::TruthParticleContainer,float> >
110 float_decor( IDPVM::createDecoratorsIfNeeded(*ptruth, m_decor, ctx, msgLvl(MSG::DEBUG)) );
111
113 std::vector< std::array<uint16_t, kNClusterTypes> > tp_clustercount;
114 tp_clustercount.resize(ptruth_size,std::array<uint16_t,kNClusterTypes>{});
115 unsigned int missing_truth_particle=0u;
116 //Loop over the pixel and sct clusters to fill the truth barcode - cluster count maps
119 //only decorate the truth particles with truth silicon hits if both containers are available
120 if (sctClusters.isValid() && pixelClusters.isValid()) {
121 for (const auto *const sct : *sctClusters) {
122 const xAOD::TrackMeasurementValidation* sctCluster = sct;
123 static const SG::AuxElement::ConstAccessor< std::vector<unsigned int> > truthIndexAcc("truth_index");
124 if (truthIndexAcc.isAvailable(*sctCluster)) {
125 const std::vector<unsigned int> &truth_indices = truthIndexAcc(*sctCluster);
126 for (auto index : truth_indices) {
127 if (index != std::numeric_limits<unsigned int>::max()) {
128 if (index < truthIndexMap.size() && truthIndexMap[index] != std::numeric_limits<unsigned int>::max()) {
129 ++tp_clustercount.at(truthIndexMap[index])[kSCT];
130 }
131 else {
132 ++missing_truth_particle;
133 }
134 }
135 }
136 }
137 } // Loop over SCT clusters
138
139 for (const auto *const pix : *pixelClusters) {
140 const xAOD::TrackMeasurementValidation* pixCluster = pix;
141 static const SG::AuxElement::ConstAccessor< std::vector<unsigned int> > truthIndexAcc("truth_index");
142 if (truthIndexAcc.isAvailable(*pixCluster)) {
143 const std::vector<unsigned int> &truth_indices = truthIndexAcc(*pixCluster);
144 for (auto index : truth_indices) {
145 if (index != std::numeric_limits<unsigned int>::max()) {
146 if (index < truthIndexMap.size() && truthIndexMap[index] != std::numeric_limits<unsigned int>::max()) {
147 ++tp_clustercount.at(truthIndexMap[index])[kPixel];
148 }
149 else {
150 ++missing_truth_particle;
151 }
152 }
153 }
154 }
155 } // Loop over PIX clusters
156 }
157 m_nMissingTruthParticles += missing_truth_particle;
158
159 if (not float_decor.empty()) {
160 Amg::Vector3D perigeePos;
162 if (m_useTruthPVAsPerigee && truthVtx) {
163 perigeePos = Amg::Vector3D(truthVtx->x(), truthVtx->y(), truthVtx->z());
164 }
165 else {
169 perigeePos = Amg::Vector3D(beamPosX(0), beamPosY(0), beamPosZ(0));
170 }
171 for (const xAOD::TruthParticle *truth_particle : *ptruth) {
172 decorateTruth(*truth_particle, float_decor, perigeePos, tp_clustercount);
173 }
174 if (m_decoTime) {
175 if (!decorateTruthTime(float_decor)) {
176 return StatusCode::FAILURE;
177 }
178 }
179 }
180
181 return StatusCode::SUCCESS;
182}
183
184bool
187 const Amg::Vector3D& perigeePos,
188 const std::vector<std::array<uint16_t,kNClusterTypes> > &counts) const {
189 ATH_MSG_VERBOSE("Decorate truth with d0 etc");
190 if (particle.isNeutral()) {
191 return false;
192 }
193 const EventContext& ctx = Gaudi::Hive::currentContext();
194 const Amg::Vector3D momentum(particle.px(), particle.py(), particle.pz());
195 const int pid(particle.pdgId());
196 double charge = particle.charge();
197
198 if (std::isnan(charge)) {
199 ATH_MSG_DEBUG("charge not found on particle with pid " << pid);
200 return false;
201 }
202
203 // @TODO float?
204 float nSiHits = std::accumulate(counts.at(particle.index()).begin(), counts[particle.index()].end(), 0u);
205
206 IDPVM::decorateOrRejectQuietly(particle,float_decor[kDecorNSilHits], nSiHits);
207
208 const xAOD::TruthVertex* ptruthVertex(nullptr);
209 try{
210 ptruthVertex = particle.prodVtx();
211 } catch (const std::exception& e) {
212 if (not m_errorEmitted) {
213 ATH_MSG_WARNING("A non existent production vertex was requested in calculating the track parameters d0 etc");
214 }
215 m_errorEmitted = true;
216 return false;
217 }
218 if (!ptruthVertex) {
219 ATH_MSG_DEBUG("A production vertex pointer was retrieved, but it is NULL");
220 return false;
221 }
222 const auto xPos = ptruthVertex->x();
223 const auto yPos = ptruthVertex->y();
224 const auto z_truth = ptruthVertex->z();
225 const Amg::Vector3D position(xPos, yPos, z_truth);
226 const float prodR_truth = std::sqrt(xPos * xPos + yPos * yPos);
227 // delete ptruthVertex;ptruthVertex=0;
228 const Trk::CurvilinearParameters cParameters(position, momentum, charge);
229
230 Trk::PerigeeSurface persf(perigeePos);
231
232 std::unique_ptr<const Trk::TrackParameters> tP ( m_extrapolator->extrapolate(ctx,
233 cParameters,
234 persf, Trk::anyDirection, false) );
235 if (tP) {
236 float d0_truth = tP->parameters()[Trk::d0];
237 float theta_truth = tP->parameters()[Trk::theta];
238 float z0_truth = tP->parameters()[Trk::z0];
239 float phi_truth = tP->parameters()[Trk::phi];
240 float qOverP_truth = tP->parameters()[Trk::qOverP]; // P or Pt ??
241 float z0st_truth = z0_truth * std::sin(theta_truth);
242
243 // 'safeDecorator' used to prevent a crash in case of adding something which pre-exists.
244 // behaviour chosen is to reject quietly
245 IDPVM::decorateOrRejectQuietly(particle,float_decor[kDecorD0],d0_truth);
246 IDPVM::decorateOrRejectQuietly(particle,float_decor[kDecorZ0],z0_truth);
247 IDPVM::decorateOrRejectQuietly(particle,float_decor[kDecorPhi],phi_truth);
248 IDPVM::decorateOrRejectQuietly(particle,float_decor[kDecorTheta],theta_truth);
249 IDPVM::decorateOrRejectQuietly(particle,float_decor[kDecorZ0st],z0st_truth);
250 IDPVM::decorateOrRejectQuietly(particle,float_decor[kDecorQOverP],qOverP_truth);
251 IDPVM::decorateOrRejectQuietly(particle,float_decor[kDecorProdR],prodR_truth);
252 IDPVM::decorateOrRejectQuietly(particle,float_decor[kDecorProdZ],z_truth);
253
254 return true;
255 } else {
256 ATH_MSG_DEBUG("The TrackParameters pointer for this TruthParticle is NULL");
257 return false;
258 }
259}
260
261const xAOD::TruthEvent*
263 const EventContext& ctx = Gaudi::Hive::currentContext();
264 const xAOD::TruthEvent* event = nullptr;
265 if (!m_truthEventName.key().empty()) {
266 ATH_MSG_VERBOSE("Getting TruthEventContainer");
268 if (truthEventContainer.isPresent()) {
269 event = (truthEventContainer.isValid()) ? truthEventContainer->at(0) : nullptr;
270 }
271 else {
272 ATH_MSG_WARNING("TruthEventContainer name was specified, but no container is present");
273 }
274 }
275 else {
276 ATH_MSG_ERROR("No valid TruthEvent!");
277 }
278 return event;
279}
280
281bool
283
284 const EventContext& ctx = Gaudi::Hive::currentContext();
285
286 const xAOD::TruthEvent* event = nullptr;
287 const xAOD::TruthVertex* truthVtx = nullptr;
288 float truthTime;
289
290 // First HS event
291 if (!m_truthEventName.key().empty()) {
292 event = getTruthHSEvent();
293 if (event) {
294 truthVtx = event->signalProcessVertex();
295 truthTime = (truthVtx) ? truthVtx->t() / Gaudi::Units::c_light : -9999.;
296 for (const auto& link : event->truthParticleLinks()) {
297 if (link.isValid()) {
298 IDPVM::decorateOrRejectQuietly(**link, float_decor[kDecorTime], truthTime);
299 }
300 }
301 }
302 }
303
304 // Then PU events
305 if (!m_truthPileupEventName.key().empty()) {
306 ATH_MSG_VERBOSE("Getting TruthPileupEventContainer");
308 if (!truthPileupEventContainer.isPresent()) {
309 ATH_MSG_WARNING("TruthPileupEventContainer name was specified, but no container is present");
310 return true;
311 }
312 if (truthPileupEventContainer.isValid()) {
313 for (const auto event : *truthPileupEventContainer) {
314 truthVtx = nullptr;
315 for (std::size_t i = 0; i < event->nTruthVertices(); i++) {
316 truthVtx = event->truthVertex(i);
317 if (truthVtx) {
318 break;
319 }
320 }
321 truthTime = (truthVtx) ? truthVtx->t() / Gaudi::Units::c_light: -9999.;
322 for (const auto& link : event->truthParticleLinks()) {
323 if (link.isValid()) {
324 IDPVM::decorateOrRejectQuietly(**link, float_decor[kDecorTime], truthTime);
325 }
326 }
327 }
328 }
329 else {
330 ATH_MSG_ERROR("TruthPileupEventContainer is invalid!");
331 return false;
332 }
333 }
334
335 return true;
336
337}
338
348
362
#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:1003
header file for class of same name
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.
SG::ReadHandleKey< xAOD::TruthEventContainer > m_truthEventName
bool decorateTruth(const xAOD::TruthParticle &particle, std::vector< std::pair< SG::WriteDecorHandle< xAOD::TruthParticleContainer, float >, bool > > &float_decor, const Amg::Vector3D &perigeePos, const std::vector< std::array< uint16_t, kNClusterTypes > > &counts) const
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
const xAOD::TruthEvent * getTruthHSEvent() const
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.
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.
const TruthVertex * signalProcessVertex() const
Pointer to a vertex representing the primary beam interaction point.
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