ATLAS Offline Software
Loading...
Searching...
No Matches
HGTDTruthTrackDecorationAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration.
3*/
4
7
15
16
17// ActsTrk
27
28// STL
30
32#include "Acts/Utilities/Helpers.hpp"
33
34
35namespace ActsTrk{
36
38 {
39 ATH_MSG_DEBUG("Initializing " << name() << "...");
40
42
45 ATH_CHECK( m_layerClusterMergedKey.initialize() );
47 ATH_CHECK( m_trackContainerKey.initialize() );
48 ATH_CHECK( m_hgtdTrackLinkKey.initialize() );
49 ATH_CHECK( m_truthParticleLinkKey.initialize() );
52
53 // Initialize surface accessor
56
57 return StatusCode::SUCCESS;
58 }
59
60 StatusCode HGTDTruthTrackDecorationAlg::execute(const EventContext& ctx) const
61 {
62 ATH_MSG_DEBUG("Executing " << name() << "...");
63
64 const xAOD::TrackParticleContainer* trackParticles {nullptr};
65 ATH_CHECK(SG::get(trackParticles, m_trackParticleContainerName, ctx));
66
67 ATH_MSG_DEBUG("Size of trackParticles collection " << trackParticles->size());
68 // ================================================== //
69 // ============ RETRIEVE MEASUREMENTS =============== //
70 // ================================================== //
71
72 ATH_MSG_DEBUG("Reading input collection with key " << m_uncalibratedMeasurementContainerKey_HGTD.key());
73
74 const xAOD::UncalibratedMeasurementContainer* uncalibratedMeasurementContainer{nullptr};
75 ATH_CHECK(SG::get(uncalibratedMeasurementContainer ,m_uncalibratedMeasurementContainerKey_HGTD, ctx));
76
77 ATH_MSG_DEBUG("Retrieved " << uncalibratedMeasurementContainer->size()
78 << " input elements from key " << m_uncalibratedMeasurementContainerKey_HGTD.key());
79
80
81 // ================================================== //
82 // ============ TRUTH MATCHING BLOCK ================ //
83 // ================================================== //
84
86
88
90 Acts::toUnderlying(xAOD::UncalibMeasType::nTypes)>
91 measurement_to_truth_association_maps{};
92
93 if (!m_hgtdClustersToTruth.key().empty()) {
94 const ActsTrk::MeasurementToTruthParticleAssociation* hgtdClustersToTruthAssociation{};
95 ATH_CHECK(SG::get(hgtdClustersToTruthAssociation, m_hgtdClustersToTruth, ctx));
96 measurement_to_truth_association_maps[Acts::toUnderlying(xAOD::UncalibMeasType::HGTDClusterType)]=hgtdClustersToTruthAssociation;
97 }
98 auto assocSize = [&measurement_to_truth_association_maps](xAOD::UncalibMeasType type) {
99 const ActsTrk::MeasurementToTruthParticleAssociation *assoc = measurement_to_truth_association_maps[Acts::toUnderlying(type)];
100 return assoc ? assoc->size() : 0ul;
101 };
102
103 ATH_MSG_DEBUG("Measurement association entries: " << assocSize(xAOD::UncalibMeasType::PixelClusterType)
104 << " + " << assocSize(xAOD::UncalibMeasType::StripClusterType)
105 << " + " << assocSize(xAOD::UncalibMeasType::HGTDClusterType)
106 );
107
108
110 if (!tracksContainer.isValid()) {
111 ATH_MSG_ERROR("No tracks for key " << m_trackContainerKey.key() );
112 return StatusCode::FAILURE;
113 }
114 // Create WriteDecorHandles for all decorations
119
121
122 for (const xAOD::TrackParticle* trackParticle : *trackParticles) {
123
124 // get the truth particle
125 ElementLink<xAOD::TruthParticleContainer> link_to_truth = truthParticleLink(*trackParticle);
126 const xAOD::TruthParticle* truthParticle = nullptr;
127 if (!link_to_truth.isValid()) {
128 layerClusterTruthClassHandle(*trackParticle) = {-1, -1, -1, -1};
129 layerClusterShadowedHandle(*trackParticle) = {false, false, false, false};
130 layerClusterMergedHandle(*trackParticle) = {false, false, false, false};
131 layerPrimaryExpectedHandle(*trackParticle) = {false, false, false, false};
132 ATH_MSG_WARNING("TrackParticle " << trackParticle->index() << ": invalid truth link");
133 continue;
134 }
135 else {
136 truthParticle = *link_to_truth;
137 }
138 // Check if the TrackParticle has a link to an hgtd extension track
139 if(hgtdTrackLink.isAvailable()){
140 ElementLink<ActsTrk::TrackContainer> link_to_track = hgtdTrackLink(*trackParticle);
141 if (!link_to_track.isValid()) {
142 ATH_MSG_DEBUG("TrackParticle " << trackParticle->index() << ": invalid extension link");
143 data.truthClassVec = {-1, -1, -1, -1};
144 data.isShadowedVec = {false, false, false, false};
145 data.isMergedVec = {false, false, false, false};
146 }
147 else{
148 std::optional<ActsTrk::TrackContainer::ConstTrackProxy> optional_track = *link_to_track;
149 const ActsTrk::TrackContainer::ConstTrackProxy& track = optional_track.value();
150 ATH_MSG_DEBUG("TrackParticle " << trackParticle->index() << ": extension with chi2 " << track.chi2() << " and nHits " << track.nMeasurements());
151 data = createTruthDecoration(truthParticle, track, measurement_to_truth_association_maps[Acts::toUnderlying(xAOD::UncalibMeasType::HGTDClusterType)]);
152 }
153 }
154
155
156 ATH_CHECK(isPrimaryExpected(truthParticle,
157 *uncalibratedMeasurementContainer,
158 measurement_to_truth_association_maps[Acts::toUnderlying(xAOD::UncalibMeasType::HGTDClusterType)],
159 data.primaryExistsVec));
160
161 layerClusterTruthClassHandle(*trackParticle) = data.truthClassVec;
162 layerClusterShadowedHandle(*trackParticle) = data.isShadowedVec;
163 layerClusterMergedHandle(*trackParticle) = data.isMergedVec;
164 layerPrimaryExpectedHandle(*trackParticle) = data.primaryExistsVec;
165
166 }
167 ATH_MSG_DEBUG("Finshed truth matching");
168
169 return StatusCode::SUCCESS;
170 }
171
173 const xAOD::TruthParticle* truthParticle,
174 const typename ActsTrk::TrackContainer::ConstTrackProxy trackProxy,
175 const ActsTrk::MeasurementToTruthParticleAssociation* association_map) const{
176
178
179 std::vector<int> truthClassPerLayer = {-1, -1, -1, -1};
180 std::vector<char> isShadowedPerLayer = {false, false, false, false};
181 std::vector<char> isMergedPerLayer = {false, false, false, false};
182 std::vector<char> isPrimaryExistsVec = {false, false, false, false};
183
184 for (auto state : trackProxy.trackStatesReversed()) {
185 auto flags = state.typeFlags();
186 if (flags.isMeasurement()) {
187 // Check if this is an HGTD hit
188 const auto& surface = state.referenceSurface();
189 Acts::GeometryIdentifier geoID = surface.geometryId();
190
191 std::size_t layerIndex = getHGTDLayerIndex(geoID);
192 ClusterTruthInfo cluster_truth_info;
193
194 assert( state.hasUncalibratedSourceLink() );
195 auto uncalibMeas = detail::xAODUncalibMeasCalibrator::unpack(state.getUncalibratedSourceLink());
196
197 if (association_map->at(uncalibMeas->index()).empty()) {
199 ATH_MSG_DEBUG(" \\__Layer "<< layerIndex << " SECONDARY: hit doesnt have truth particle associated with it");
200 }
201 else if(association_map->at(uncalibMeas->index()).size() == 1) {
202 const xAOD::TruthParticle *truth_particle = association_map->at(uncalibMeas->index()).at(0);
203 if(truthParticle->index() == truth_particle->index()){
205 ATH_MSG_DEBUG(" \\__Layer "<< layerIndex<< " TRUTH_PARTICLE: hit matches ITk track");
206 }
207 else{
209 ATH_MSG_DEBUG(" \\__Layer "<< layerIndex<< " UNRELATED_PARTICLE: hit doesnt match ITk track");
210 }
211 }
212 else {
213 cluster_truth_info.is_merged = true;
215 ATH_MSG_DEBUG(" \\__Layer "<< layerIndex<< " MERGED: hit with more than one contributing particle");
216 for (const xAOD::TruthParticle *truth_particle : association_map->at(uncalibMeas->index()) ) {
217 if(truthParticle->index() == truth_particle->index()){
219 ATH_MSG_DEBUG(" \\__TRUTH_PARTICLE: hit matches ITk track");
220 }
221 }
222 }
223
224
225 truthClassPerLayer[layerIndex] = (int) cluster_truth_info.origin;
226 isShadowedPerLayer[layerIndex] = cluster_truth_info.is_shadowed;
227 isMergedPerLayer[layerIndex] = cluster_truth_info.is_merged;
228 }
229 }
230
231 // Fill the data structure with results
232 data.truthClassVec = std::move(truthClassPerLayer);
233 data.isShadowedVec = std::move(isShadowedPerLayer);
234 data.isMergedVec = std::move(isMergedPerLayer);
235 data.primaryExistsVec = std::move(isPrimaryExistsVec);
236 return data;
237 }
238
240 const xAOD::TruthParticle* truthParticle,
241 const xAOD::UncalibratedMeasurementContainer measurementContainer,
243 std::vector<char> &isPrimaryExistsVec) const{
244
245 isPrimaryExistsVec = {false, false, false, false};
246
247 for(auto uncalibMeas: measurementContainer) {
248
249 auto measurementTruthParticles = association_map->at(uncalibMeas->index());
250 const Acts::Surface* surface = m_surfAcc.get(uncalibMeas);
251 Acts::GeometryIdentifier geoID = surface->geometryId();
252 std::size_t layerIndex = getHGTDLayerIndex(geoID);
253 if(measurementTruthParticles.size() > 0){
254 for(auto measTruthParticle : measurementTruthParticles){
255 if ( truthParticle->index() == measTruthParticle->index()){
256 isPrimaryExistsVec[layerIndex] = true;
257 ATH_MSG_DEBUG(" \\__HIT Exepected at " << layerIndex);
258 }
259 }
260 }
261 }
262
263 return StatusCode::SUCCESS;
264 }
265
266 std::size_t HGTDTruthTrackDecorationAlg::getHGTDLayerIndex(const Acts::GeometryIdentifier& geoID) const {
267 // Get volume and layer ID
268 std::uint32_t volume = geoID.volume();
269 std::uint32_t layer = geoID.layer();
270
271 // Check if we're in the positive or negative endcap
272 bool isPositiveEndcap = (volume == 25);
273 bool isNegativeEndcap = (volume == 2);
274
275 // Different mapping for different sides to maintain consistent physical ordering
276 if (isPositiveEndcap) {
277 // Mapping for positive endcap
278 switch(layer) {
279 case 2: return 0; // First HGTD layer (closest to IP)
280 case 4: return 1; // Second HGTD layer
281 case 6: return 2; // Third HGTD layer
282 case 8: return 3; // Fourth HGTD layer (farthest from IP)
283 default: return 99; // Invalid layer
284 }
285 } else if (isNegativeEndcap) {
286 // Mapping for negative endcap - potentially different ordering
287 switch(layer) {
288 case 2: return 3;
289 case 4: return 2;
290 case 6: return 1;
291 case 8: return 0;
292 default: return 99; // Invalid layer
293 }
294 } else {
295 return 99; // Not an HGTD volume
296 }
297 }
298
299}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
Handle class for reading a decoration on an object.
Handle class for adding a decoration to an object.
SG::ReadHandleKey< xAOD::TrackParticleContainer > m_trackParticleContainerName
PublicToolHandle< ActsTrk::ITrackingGeometryTool > m_trackingGeometryTool
SG::WriteDecorHandleKey< xAOD::TrackParticleContainer > m_layerClusterShadowedKey
SG::ReadHandleKey< MeasurementToTruthParticleAssociation > m_hgtdClustersToTruth
SG::ReadHandleKey< xAOD::UncalibratedMeasurementContainer > m_uncalibratedMeasurementContainerKey_HGTD
SG::WriteDecorHandleKey< xAOD::TrackParticleContainer > m_layerClusterMergedKey
StatusCode isPrimaryExpected(const xAOD::TruthParticle *truthParticle, const xAOD::UncalibratedMeasurementContainer measurementContainer, const ActsTrk::MeasurementToTruthParticleAssociation *association_map, std::vector< char > &isPrimaryExistsVec) const
Checks if truth particle produced hits at each one of the HGTD layers.
virtual StatusCode execute(const EventContext &) const override
SG::ReadHandleKey< ActsTrk::TrackContainer > m_trackContainerKey
SG::ReadDecorHandleKey< xAOD::TrackParticleContainer > m_truthParticleLinkKey
ActsTrk::detail::xAODUncalibMeasSurfAcc m_surfAcc
SG::WriteDecorHandleKey< xAOD::TrackParticleContainer > m_layerPrimaryExpectedKey
TruthTrackExtensionData createTruthDecoration(const xAOD::TruthParticle *truthParticle, const typename ActsTrk::TrackContainer::ConstTrackProxy trackProxy, const ActsTrk::MeasurementToTruthParticleAssociation *association_map) const
Generate TruthTrackExtensionData from extension.
SG::ReadDecorHandleKey< xAOD::TrackParticleContainer > m_hgtdTrackLinkKey
std::size_t getHGTDLayerIndex(const Acts::GeometryIdentifier &geoID) const
returns the index of HGTD layer where surfaces lies.
SG::WriteDecorHandleKey< xAOD::TrackParticleContainer > m_layerClusterTruthClassKey
static const xAOD::UncalibratedMeasurement * unpack(const Acts::SourceLink &sl)
Helper method to unpack an Acts source link to an uncalibrated measurement.
Helper class to access the Acts::surface associated with an Uncalibrated xAOD measurement.
size_type size() const noexcept
Returns the number of elements in the collection.
Handle class for reading a decoration on an object.
bool isAvailable()
Test to see if this variable exists in the store, for the referenced object.
virtual bool isValid() override final
Can the handle be successfully dereferenced?
Handle class for adding a decoration to an object.
The AlignStoreProviderAlg loads the rigid alignment corrections and pipes them through the readout ge...
const T * get(const ReadCondHandleKey< T > &key, const EventContext &ctx)
Convenience function to retrieve an object given a ReadCondHandleKey.
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
TrackParticle_v1 TrackParticle
Reference the current persistent version:
TruthParticle_v1 TruthParticle
Typedef to implementation.
UncalibMeasType
Define the type of the uncalibrated measurement.
TrackParticleContainer_v1 TrackParticleContainer
Definition of the current "TrackParticle container version".
UncalibratedMeasurementContainer_v1 UncalibratedMeasurementContainer
Define the version of the uncalibrated measurement container.
bool is_merged
A cluster is considered to be merged if more than one particle deposited energy in a given pad.
bool is_shadowed
Shadowing means that a deposit was left by the truth particle, but it was not the first deposit and i...
Data structure to hold truth information about the HGTD track extension.