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
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()) {
163 Amg::Vector3D beamPos = Amg::Vector3D(beamPosX(0), beamPosY(0), beamPosZ(0));
164 for (const xAOD::TruthParticle *truth_particle : *ptruth) {
165 decorateTruth(*truth_particle, float_decor, beamPos, tp_clustercount);
166 }
167 if (!decorateTruthTime(float_decor)) {
168 return StatusCode::FAILURE;
169 }
170 }
171
172 return StatusCode::SUCCESS;
173}
174
175bool
178 const Amg::Vector3D& beamPos,
179 const std::vector<std::array<uint16_t,kNClusterTypes> > &counts) const {
180 ATH_MSG_VERBOSE("Decorate truth with d0 etc");
181 if (particle.isNeutral()) {
182 return false;
183 }
184 const EventContext& ctx = Gaudi::Hive::currentContext();
185 const Amg::Vector3D momentum(particle.px(), particle.py(), particle.pz());
186 const int pid(particle.pdgId());
187 double charge = particle.charge();
188
189 if (std::isnan(charge)) {
190 ATH_MSG_DEBUG("charge not found on particle with pid " << pid);
191 return false;
192 }
193
194 // @TODO float?
195 float nSiHits = std::accumulate(counts.at(particle.index()).begin(), counts[particle.index()].end(), 0u);
196
197 IDPVM::decorateOrRejectQuietly(particle,float_decor[kDecorNSilHits], nSiHits);
198
199 const xAOD::TruthVertex* ptruthVertex(nullptr);
200 try{
201 ptruthVertex = particle.prodVtx();
202 } catch (const std::exception& e) {
203 if (not m_errorEmitted) {
204 ATH_MSG_WARNING("A non existent production vertex was requested in calculating the track parameters d0 etc");
205 }
206 m_errorEmitted = true;
207 return false;
208 }
209 if (!ptruthVertex) {
210 ATH_MSG_DEBUG("A production vertex pointer was retrieved, but it is NULL");
211 return false;
212 }
213 const auto xPos = ptruthVertex->x();
214 const auto yPos = ptruthVertex->y();
215 const auto z_truth = ptruthVertex->z();
216 const Amg::Vector3D position(xPos, yPos, z_truth);
217 const float prodR_truth = std::sqrt(xPos * xPos + yPos * yPos);
218 // delete ptruthVertex;ptruthVertex=0;
219 const Trk::CurvilinearParameters cParameters(position, momentum, charge);
220
221 Trk::PerigeeSurface persf(beamPos);
222
223 std::unique_ptr<const Trk::TrackParameters> tP ( m_extrapolator->extrapolate(ctx,
224 cParameters,
225 persf, Trk::anyDirection, false) );
226 if (tP) {
227 float d0_truth = tP->parameters()[Trk::d0];
228 float theta_truth = tP->parameters()[Trk::theta];
229 float z0_truth = tP->parameters()[Trk::z0];
230 float phi_truth = tP->parameters()[Trk::phi];
231 float qOverP_truth = tP->parameters()[Trk::qOverP]; // P or Pt ??
232 float z0st_truth = z0_truth * std::sin(theta_truth);
233
234 // 'safeDecorator' used to prevent a crash in case of adding something which pre-exists.
235 // behaviour chosen is to reject quietly
236 IDPVM::decorateOrRejectQuietly(particle,float_decor[kDecorD0],d0_truth);
237 IDPVM::decorateOrRejectQuietly(particle,float_decor[kDecorZ0],z0_truth);
238 IDPVM::decorateOrRejectQuietly(particle,float_decor[kDecorPhi],phi_truth);
239 IDPVM::decorateOrRejectQuietly(particle,float_decor[kDecorTheta],theta_truth);
240 IDPVM::decorateOrRejectQuietly(particle,float_decor[kDecorZ0st],z0st_truth);
241 IDPVM::decorateOrRejectQuietly(particle,float_decor[kDecorQOverP],qOverP_truth);
242 IDPVM::decorateOrRejectQuietly(particle,float_decor[kDecorProdR],prodR_truth);
243 IDPVM::decorateOrRejectQuietly(particle,float_decor[kDecorProdZ],z_truth);
244
245 return true;
246 } else {
247 ATH_MSG_DEBUG("The TrackParameters pointer for this TruthParticle is NULL");
248 return false;
249 }
250}
251
252bool
254
255 const EventContext& ctx = Gaudi::Hive::currentContext();
256
257 const xAOD::TruthVertex* truthVtx = nullptr;
258 float truthTime;
259
260 // First HS event
261 if (!m_truthEventName.key().empty()) {
262 ATH_MSG_VERBOSE("Getting TruthEventContainer");
264 if (!truthEventContainer.isPresent()) {
265 ATH_MSG_WARNING("TruthEventContainer name was specified, but no container is present");
266 return true;
267 }
268 const xAOD::TruthEvent* event = (truthEventContainer.isValid()) ? truthEventContainer->at(0) : nullptr;
269 if (event) {
270 truthVtx = event->signalProcessVertex();
271 truthTime = (truthVtx) ? truthVtx->t() / Gaudi::Units::c_light : -9999.;
272 for (const auto& link : event->truthParticleLinks()) {
273 if (link.isValid()) {
274 IDPVM::decorateOrRejectQuietly(**link, float_decor[kDecorTime], truthTime);
275 }
276 }
277 }
278 else {
279 ATH_MSG_ERROR("No valid TruthEvent!");
280 return false;
281 }
282 }
283
284 // Then PU events
285 if (!m_truthPileupEventName.key().empty()) {
286 ATH_MSG_VERBOSE("Getting TruthPileupEventContainer");
288 if (!truthPileupEventContainer.isPresent()) {
289 ATH_MSG_WARNING("TruthPileupEventContainer name was specified, but no container is present");
290 return true;
291 }
292 if (truthPileupEventContainer.isValid()) {
293 for (const auto event : *truthPileupEventContainer) {
294 truthVtx = nullptr;
295 for (std::size_t i = 0; i < event->nTruthVertices(); i++) {
296 truthVtx = event->truthVertex(i);
297 if (truthVtx) {
298 break;
299 }
300 }
301 truthTime = (truthVtx) ? truthVtx->t() / Gaudi::Units::c_light: -9999.;
302 for (const auto& link : event->truthParticleLinks()) {
303 if (link.isValid()) {
304 IDPVM::decorateOrRejectQuietly(**link, float_decor[kDecorTime], truthTime);
305 }
306 }
307 }
308 }
309 else {
310 ATH_MSG_ERROR("TruthPileupEventContainer is invalid!");
311 return false;
312 }
313 }
314
315 return true;
316
317}
318
328
342
#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
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
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
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