ATLAS Offline Software
Loading...
Searching...
No Matches
TrackParticleThinningBase.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
5// Author: James Catmore (James.Catmore@cern.ch)
6// Removes all ID tracks which do not pass a user-defined cut
7
13#include "GaudiKernel/ThreadLocalContext.h"
14#include <vector>
15#include <string>
17
18// Athena initialize and finalize
20{
21 ATH_MSG_VERBOSE("initialize() ...");
22
23 //check xAOD::InDetTrackParticle collection
24 ATH_CHECK( m_inDetSGKey.initialize (m_streamName) );
25 ATH_MSG_INFO("Using " << m_inDetSGKey << "as the source collection for inner detector track particles");
26 //check availability of xAOD::TrackStateValidation and xAOD::TrackMeasurementValidation containers
28 ATH_MSG_INFO("Pixel states collection as source for thinning: " << m_statesPixSGKey.key());
29 ATH_MSG_INFO("Pixel measurements collection as source for thinning: " << m_measurementsPixSGKey.key());
30 ATH_MSG_INFO("SCT states collection as source for thinning: " << m_statesSctSGKey.key());
31 ATH_MSG_INFO("SCT measurements collection as source for thinning: " << m_measurementsSctSGKey.key());
32 ATH_MSG_INFO("TRT states collection as source for thinning: " << m_statesTrtSGKey.key());
33 ATH_MSG_INFO("TRT measurements collection as source for thinning: " << m_measurementsTrtSGKey.key());
34 }
41
42 ATH_CHECK(m_SCTDetEleCollKey.initialize( !m_SCTDetEleCollKey.key().empty() ));
43
44 return StatusCode::SUCCESS;
45}
46
48{
49 ATH_MSG_VERBOSE("finalize() ...");
50 ATH_MSG_INFO("Processed "<< m_ntot <<" tracks, "<< m_npass<< " were retained ");
52 ATH_MSG_INFO("Pixel state objects thinning, Total / Passed (Efficiency): "
54 << " (" << (m_ntot_pix_states == 0 ? 0 : static_cast<float>(m_npass_pix_states) / m_ntot_pix_states) << ")");
55 ATH_MSG_INFO("Pixel measurements objects thinning, Total / Passed (Efficiency): "
57 << " (" << (m_ntot_pix_measurements == 0 ? 0 : static_cast<float>(m_npass_pix_measurements) / m_ntot_pix_measurements) << ")");
58 ATH_MSG_INFO("SCT state objects thinning, Total / Passed (Efficiency): "
60 << " (" << (m_ntot_sct_states == 0 ? 0 : static_cast<float>(m_npass_sct_states) / m_ntot_sct_states) << ")");
61 ATH_MSG_INFO("SCT measurements objects thinning, Total / Passed (Efficiency): "
63 << " (" << (m_ntot_sct_measurements == 0 ? 0 : static_cast<float>(m_npass_sct_measurements) / m_ntot_sct_measurements) << ")");
64 ATH_MSG_INFO("TRT state objects thinning, Total / Passed (Efficiency): "
66 << " (" << (m_ntot_trt_states == 0 ? 0 : static_cast<float>(m_npass_trt_states) / m_ntot_trt_states) << ")");
67 ATH_MSG_INFO("TRT measurements objects thinning, Total / Passed (Efficiency): "
69 << " (" << (m_ntot_trt_measurements == 0 ? 0 : static_cast<float>(m_npass_trt_measurements) / m_ntot_trt_measurements) << ")");
70 }
71 return StatusCode::SUCCESS;
72}
73
75{
76 return std::vector<int>(trackParticles->size(), 0);
77}
78
79// The thinning itself
81{
82 const EventContext& ctx = Gaudi::Hive::currentContext();
83
84 // Retrieve main TrackParticle collection
86 (m_inDetSGKey, ctx);
87
88 // Check the event contains tracks
89 unsigned int nTracks = importedTrackParticles->size();
90 if (nTracks==0) return StatusCode::SUCCESS;
91
92 // Set up a mask with the same entries as the full TrackParticle collection
93 std::vector<bool> mask;
94 mask.assign(nTracks,false); // default: don't keep any tracks
95 m_ntot += nTracks;
96
97 // Execute the text parser and update the mask
98 std::vector<int> entries = updateMask(importedTrackParticles.cptr());
99 unsigned int nEntries = entries.size();
100 // check the sizes are compatible
101 if (nTracks != nEntries ) {
102 ATH_MSG_ERROR("Sizes incompatible! Are you sure your selection string used ID TrackParticles?");
103 return StatusCode::FAILURE;
104 } else {
105 // set mask
106 for (unsigned int i=0; i<nTracks; ++i) if (entries[i]==1) mask[i]=true;
107 }
108 // Count the mask
109 m_npass += std::count (mask.begin(), mask.end(), true);
110
111 // Execute the thinning service based on the mask.
112 importedTrackParticles.keep (mask);
113
114 //If thinning hits on track, look over States and Measurements collections as well
115 if (m_thinHitsOnTrack) {
116 filterTrackHits (ctx,
118 *importedTrackParticles,
119 mask,
126 filterTrackHits (ctx,
128 *importedTrackParticles,
129 mask,
136 filterTrackHits (ctx,
138 *importedTrackParticles,
139 mask,
146 }
147
148 return StatusCode::SUCCESS;
149}
150
151
153(const EventContext& ctx,
154 MeasurementType detTypeToSelect,
155 const xAOD::TrackParticleContainer& inputTrackParticles,
156 const std::vector<bool>& inputMask,
159 std::atomic<unsigned int>& ntot_states,
160 std::atomic<unsigned int>& ntot_measurements,
161 std::atomic<unsigned int>& npass_states,
162 std::atomic<unsigned int>& npass_measurements) const
163{
164 std::vector<bool> maskStates;
165 std::vector<bool> maskMeasurements;
166
167 selectTrackHits (inputTrackParticles, inputMask, detTypeToSelect,
168 maskStates, maskMeasurements);
169
170 auto count = [] (const std::vector<bool>& m)
171 { return std::count (m.begin(), m.end(), true); };
172 npass_states += count (maskStates);
173 npass_measurements += count (maskMeasurements);
174
175 if (!statesKey.empty()) {
177 (statesKey, ctx);
178 unsigned int size_states = importedStates->size();
179 if (size_states == 0) {
180 ATH_MSG_WARNING("States container is empty: " << statesKey.key());
181 }
182 else {
183 ntot_states += size_states;
184 if (maskStates.size() > size_states) {
185 ATH_MSG_ERROR("States mask size mismatch " << maskStates.size() <<
186 " > " << size_states);
187 }
188 maskStates.resize (size_states);
189 importedStates.keep (maskStates);
190 }
191 }
192
193 if (!measurementsKey.empty()) {
195 (measurementsKey, ctx);
196 unsigned int size_measurements = importedMeasurements->size();
197 if (size_measurements == 0) {
198 ATH_MSG_WARNING("Measurements container is empty: " << measurementsKey.key());
199 }
200 else {
201 ntot_measurements += size_measurements;
202 if (maskMeasurements.size() > size_measurements) {
203 ATH_MSG_ERROR("Measurements mask size mismatch " << maskMeasurements.size() <<
204 " > " << size_measurements);
205 }
206 maskMeasurements.resize (size_measurements);
207 importedMeasurements.keep (maskMeasurements);
208 }
209 }
210}
211
212
214 const std::vector<bool>& inputMask,
215 MeasurementType detTypeToSelect,
216 std::vector<bool>& outputStatesMask, std::vector<bool>& outputMeasurementsMask) const
217{
218 // loop over track particles, consider only the ones pre-selected by the mask
219 int trkIndex=-1;
220 for (const xAOD::TrackParticle* trkIt : inputTrackParticles) {
221 trkIndex++;
222 if (not inputMask[trkIndex]) continue;
223
224 // loop over the TrackStateValidation objects, and add them to the outputStatesMask
225 using StatesOnTrack = std::vector<ElementLink<xAOD::TrackStateValidationContainer>>;
226 static const SG::ConstAccessor< StatesOnTrack > msosLinkAcc( "msosLink" );
227 if( ! msosLinkAcc.isAvailable(*trkIt) ) {
228 ATH_MSG_DEBUG("Cannot find TrackState link from xAOD::TrackParticle. Skipping track.");
229 continue;
230 }
231 const StatesOnTrack& measurementsOnTrack = msosLinkAcc(*trkIt);
232 for( const ElementLink<xAOD::TrackStateValidationContainer>& trkState_el : measurementsOnTrack) {
233 if (not trkState_el.isValid()) {
234 ATH_MSG_DEBUG("Cannot find a valid link to TrackStateValidation object for track index: " << trkIndex);
235 continue; //not a valid link
236 }
237 if ((*trkState_el)->detType() != detTypeToSelect) {
238 ATH_MSG_VERBOSE("Discarding TrackState as not of correct type " << detTypeToSelect);
239 continue;
240 }
241 if (trkState_el.index() >= outputStatesMask.size()) {
242 outputStatesMask.resize (trkState_el.index()+1);
243 }
244 outputStatesMask[trkState_el.index()] = true;
245
246 // get the corresponding TrackMeasurementValidation object, if any, and add it to the outputMeasurementsMask
247 const ElementLink<xAOD::TrackMeasurementValidationContainer> trkMeasurement_el = (*trkState_el)->trackMeasurementValidationLink();
248 if (not trkMeasurement_el.isValid()) {
249 ATH_MSG_VERBOSE("Cannot find a valid link to TrackMeasurementValidation object from track state for track index: " << trkIndex
250 << ", trackState index: " << trkState_el.index());
251 continue; //not a valid link
252 }
253 if (*trkMeasurement_el == nullptr) {
254 ATH_MSG_VERBOSE("Invalid pointer to TrackMeasurementValidation object from track state for track index: " << trkIndex
255 << ", trackState index: " << trkState_el.index());
256 continue; //not linking to a valid object -- is it necessary?
257 }
258 if (trkMeasurement_el.index() >= outputMeasurementsMask.size()) {
259 outputMeasurementsMask.resize (trkMeasurement_el.index()+1);
260 }
261 outputMeasurementsMask[trkMeasurement_el.index()] = true;
262 }
263 } // end loop over xAOD::TrackParticle container
264}
#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)
Helper class to provide constant type-safe access to aux data.
Handle for requesting thinning for a data object.
size_type size() const noexcept
Returns the number of elements in the collection.
SG::ThinningHandleKey< xAOD::TrackStateValidationContainer > m_statesPixSGKey
SG::ThinningHandleKey< xAOD::TrackStateValidationContainer > m_statesTrtSGKey
SG::ThinningHandleKey< xAOD::TrackStateValidationContainer > m_statesSctSGKey
virtual StatusCode doThinning() const override final
SG::ThinningHandleKey< xAOD::TrackParticleContainer > m_inDetSGKey
void selectTrackHits(const xAOD::TrackParticleContainer &inputTrackParticles, const std::vector< bool > &inputMask, MeasurementType detTypeToSelect, std::vector< bool > &outputStatesMask, std::vector< bool > &outputMeasurementsMask) const
Select TrackStateValidation and TrackMeasurementValidation objects that are used in the (thinned) tra...
virtual std::vector< int > updateMask(const xAOD::TrackParticleContainer *) const
void filterTrackHits(const EventContext &ctx, MeasurementType detTypeToSelect, const xAOD::TrackParticleContainer &inputTrackParticles, const std::vector< bool > &inputMask, const SG::ThinningHandleKey< xAOD::TrackStateValidationContainer > &statesKey, const SG::ThinningHandleKey< xAOD::TrackMeasurementValidationContainer > &measurementsKey, std::atomic< unsigned int > &ntot_states, std::atomic< unsigned int > &ntot_measurements, std::atomic< unsigned int > &npass_states, std::atomic< unsigned int > &npass_measurements) const
SG::ReadCondHandleKey< InDetDD::SiDetectorElementCollection > m_SCTDetEleCollKey
SG::ThinningHandleKey< xAOD::TrackMeasurementValidationContainer > m_measurementsSctSGKey
SG::ThinningHandleKey< xAOD::TrackMeasurementValidationContainer > m_measurementsPixSGKey
SG::ThinningHandleKey< xAOD::TrackMeasurementValidationContainer > m_measurementsTrtSGKey
Helper class to provide constant type-safe access to aux data.
bool isAvailable(const ELT &e) const
Test to see if this variable exists in the store.
void keep(size_t ndx)
Mark that index ndx in the container should be kept (not thinned away).
HandleKey object for adding thinning to an object.
Handle for requesting thinning for a data object.
const std::string & key() const
Return the StoreGate ID for the referenced object.
bool empty() const
Test if the key is blank.
int count(std::string s, const std::string &regx)
count how many occurances of a regx are in a string
Definition hcg.cxx:146
double entries
Definition listroot.cxx:49
TrackParticle_v1 TrackParticle
Reference the current persistent version:
TrackParticleContainer_v1 TrackParticleContainer
Definition of the current "TrackParticle container version".