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 <vector>
14#include <string>
16
17// Athena initialize and finalize
19{
20 ATH_MSG_VERBOSE("initialize() ...");
21
22 //check xAOD::InDetTrackParticle collection
23 ATH_CHECK( m_inDetSGKey.initialize (m_streamName) );
24 ATH_MSG_INFO("Using " << m_inDetSGKey << "as the source collection for inner detector track particles");
25 //check availability of xAOD::TrackStateValidation and xAOD::TrackMeasurementValidation containers
27 ATH_MSG_INFO("Pixel states collection as source for thinning: " << m_statesPixSGKey.key());
28 ATH_MSG_INFO("Pixel measurements collection as source for thinning: " << m_measurementsPixSGKey.key());
29 ATH_MSG_INFO("SCT states collection as source for thinning: " << m_statesSctSGKey.key());
30 ATH_MSG_INFO("SCT measurements collection as source for thinning: " << m_measurementsSctSGKey.key());
31 ATH_MSG_INFO("TRT states collection as source for thinning: " << m_statesTrtSGKey.key());
32 ATH_MSG_INFO("TRT measurements collection as source for thinning: " << m_measurementsTrtSGKey.key());
33 }
40
41 ATH_CHECK(m_SCTDetEleCollKey.initialize( !m_SCTDetEleCollKey.key().empty() ));
42
43 return StatusCode::SUCCESS;
44}
45
47{
48 ATH_MSG_VERBOSE("finalize() ...");
49 ATH_MSG_INFO("Processed "<< m_ntot <<" tracks, "<< m_npass<< " were retained ");
51 ATH_MSG_INFO("Pixel state objects thinning, Total / Passed (Efficiency): "
53 << " (" << (m_ntot_pix_states == 0 ? 0 : static_cast<float>(m_npass_pix_states) / m_ntot_pix_states) << ")");
54 ATH_MSG_INFO("Pixel measurements objects thinning, Total / Passed (Efficiency): "
56 << " (" << (m_ntot_pix_measurements == 0 ? 0 : static_cast<float>(m_npass_pix_measurements) / m_ntot_pix_measurements) << ")");
57 ATH_MSG_INFO("SCT state objects thinning, Total / Passed (Efficiency): "
59 << " (" << (m_ntot_sct_states == 0 ? 0 : static_cast<float>(m_npass_sct_states) / m_ntot_sct_states) << ")");
60 ATH_MSG_INFO("SCT measurements objects thinning, Total / Passed (Efficiency): "
62 << " (" << (m_ntot_sct_measurements == 0 ? 0 : static_cast<float>(m_npass_sct_measurements) / m_ntot_sct_measurements) << ")");
63 ATH_MSG_INFO("TRT state objects thinning, Total / Passed (Efficiency): "
65 << " (" << (m_ntot_trt_states == 0 ? 0 : static_cast<float>(m_npass_trt_states) / m_ntot_trt_states) << ")");
66 ATH_MSG_INFO("TRT measurements objects thinning, Total / Passed (Efficiency): "
68 << " (" << (m_ntot_trt_measurements == 0 ? 0 : static_cast<float>(m_npass_trt_measurements) / m_ntot_trt_measurements) << ")");
69 }
70 return StatusCode::SUCCESS;
71}
72
74{
75 return std::vector<int>(trackParticles->size(), 0);
76}
77
78// The thinning itself
79StatusCode DerivationFramework::TrackParticleThinningBase::doThinning(const EventContext& ctx) const
80{
81
82 // Retrieve main TrackParticle collection
84 (m_inDetSGKey, ctx);
85
86 // Check the event contains tracks
87 unsigned int nTracks = importedTrackParticles->size();
88 if (nTracks==0) return StatusCode::SUCCESS;
89
90 // Set up a mask with the same entries as the full TrackParticle collection
91 std::vector<bool> mask;
92 mask.assign(nTracks,false); // default: don't keep any tracks
93 m_ntot += nTracks;
94
95 // Execute the text parser and update the mask
96 std::vector<int> entries = updateMask(importedTrackParticles.cptr());
97 unsigned int nEntries = entries.size();
98 // check the sizes are compatible
99 if (nTracks != nEntries ) {
100 ATH_MSG_ERROR("Sizes incompatible! Are you sure your selection string used ID TrackParticles?");
101 return StatusCode::FAILURE;
102 } else {
103 // set mask
104 for (unsigned int i=0; i<nTracks; ++i) if (entries[i]==1) mask[i]=true;
105 }
106 // Count the mask
107 m_npass += std::count (mask.begin(), mask.end(), true);
108
109 // Execute the thinning service based on the mask.
110 importedTrackParticles.keep (mask);
111
112 //If thinning hits on track, look over States and Measurements collections as well
113 if (m_thinHitsOnTrack) {
114 filterTrackHits (ctx,
116 *importedTrackParticles,
117 mask,
124 filterTrackHits (ctx,
126 *importedTrackParticles,
127 mask,
134 filterTrackHits (ctx,
136 *importedTrackParticles,
137 mask,
144 }
145
146 return StatusCode::SUCCESS;
147}
148
149
151(const EventContext& ctx,
152 MeasurementType detTypeToSelect,
153 const xAOD::TrackParticleContainer& inputTrackParticles,
154 const std::vector<bool>& inputMask,
157 std::atomic<unsigned int>& ntot_states,
158 std::atomic<unsigned int>& ntot_measurements,
159 std::atomic<unsigned int>& npass_states,
160 std::atomic<unsigned int>& npass_measurements) const
161{
162 std::vector<bool> maskStates;
163 std::vector<bool> maskMeasurements;
164
165 selectTrackHits (inputTrackParticles, inputMask, detTypeToSelect,
166 maskStates, maskMeasurements);
167
168 auto count = [] (const std::vector<bool>& m)
169 { return std::count (m.begin(), m.end(), true); };
170 npass_states += count (maskStates);
171 npass_measurements += count (maskMeasurements);
172
173 if (!statesKey.empty()) {
175 (statesKey, ctx);
176 unsigned int size_states = importedStates->size();
177 if (size_states == 0) {
178 ATH_MSG_WARNING("States container is empty: " << statesKey.key());
179 }
180 else {
181 ntot_states += size_states;
182 if (maskStates.size() > size_states) {
183 ATH_MSG_ERROR("States mask size mismatch " << maskStates.size() <<
184 " > " << size_states);
185 }
186 maskStates.resize (size_states);
187 importedStates.keep (maskStates);
188 }
189 }
190
191 if (!measurementsKey.empty()) {
193 (measurementsKey, ctx);
194 unsigned int size_measurements = importedMeasurements->size();
195 if (size_measurements == 0) {
196 ATH_MSG_WARNING("Measurements container is empty: " << measurementsKey.key());
197 }
198 else {
199 ntot_measurements += size_measurements;
200 if (maskMeasurements.size() > size_measurements) {
201 ATH_MSG_ERROR("Measurements mask size mismatch " << maskMeasurements.size() <<
202 " > " << size_measurements);
203 }
204 maskMeasurements.resize (size_measurements);
205 importedMeasurements.keep (maskMeasurements);
206 }
207 }
208}
209
210
212 const std::vector<bool>& inputMask,
213 MeasurementType detTypeToSelect,
214 std::vector<bool>& outputStatesMask, std::vector<bool>& outputMeasurementsMask) const
215{
216 // loop over track particles, consider only the ones pre-selected by the mask
217 int trkIndex=-1;
218 for (const xAOD::TrackParticle* trkIt : inputTrackParticles) {
219 trkIndex++;
220 if (not inputMask[trkIndex]) continue;
221
222 // loop over the TrackStateValidation objects, and add them to the outputStatesMask
223 using StatesOnTrack = std::vector<ElementLink<xAOD::TrackStateValidationContainer>>;
224 static const SG::ConstAccessor< StatesOnTrack > msosLinkAcc( "msosLink" );
225 if( ! msosLinkAcc.isAvailable(*trkIt) ) {
226 ATH_MSG_DEBUG("Cannot find TrackState link from xAOD::TrackParticle. Skipping track.");
227 continue;
228 }
229 const StatesOnTrack& measurementsOnTrack = msosLinkAcc(*trkIt);
230 for( const ElementLink<xAOD::TrackStateValidationContainer>& trkState_el : measurementsOnTrack) {
231 if (not trkState_el.isValid()) {
232 ATH_MSG_DEBUG("Cannot find a valid link to TrackStateValidation object for track index: " << trkIndex);
233 continue; //not a valid link
234 }
235 if ((*trkState_el)->detType() != detTypeToSelect) {
236 ATH_MSG_VERBOSE("Discarding TrackState as not of correct type " << detTypeToSelect);
237 continue;
238 }
239 if (trkState_el.index() >= outputStatesMask.size()) {
240 outputStatesMask.resize (trkState_el.index()+1);
241 }
242 outputStatesMask[trkState_el.index()] = true;
243
244 // get the corresponding TrackMeasurementValidation object, if any, and add it to the outputMeasurementsMask
245 const ElementLink<xAOD::TrackMeasurementValidationContainer> trkMeasurement_el = (*trkState_el)->trackMeasurementValidationLink();
246 if (not trkMeasurement_el.isValid()) {
247 ATH_MSG_VERBOSE("Cannot find a valid link to TrackMeasurementValidation object from track state for track index: " << trkIndex
248 << ", trackState index: " << trkState_el.index());
249 continue; //not a valid link
250 }
251 if (*trkMeasurement_el == nullptr) {
252 ATH_MSG_VERBOSE("Invalid pointer to TrackMeasurementValidation object from track state for track index: " << trkIndex
253 << ", trackState index: " << trkState_el.index());
254 continue; //not linking to a valid object -- is it necessary?
255 }
256 if (trkMeasurement_el.index() >= outputMeasurementsMask.size()) {
257 outputMeasurementsMask.resize (trkMeasurement_el.index()+1);
258 }
259 outputMeasurementsMask[trkMeasurement_el.index()] = true;
260 }
261 } // end loop over xAOD::TrackParticle container
262}
#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
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
virtual StatusCode doThinning(const EventContext &ctx) const override final
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:148
double entries
Definition listroot.cxx:49
TrackParticle_v1 TrackParticle
Reference the current persistent version:
TrackParticleContainer_v1 TrackParticleContainer
Definition of the current "TrackParticle container version".