ATLAS Offline Software
Loading...
Searching...
No Matches
ThinInDetClustersAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3*/
4
6// ThinInDetClustersAlg.cxx, (c) ATLAS Detector software
8// Removes clusters associated with ID tracks which do not pass a user-defined cut
9// Does NOT remove the tracks themselves
10
15
16// STL includes
17#include <vector>
18#include <string>
19
20// FrameWork includes
21#include "Gaudi/Property.h"
22#include "GaudiKernel/ThreadLocalContext.h"
26
28// Public methods:
30
31// Constructors
34 const std::string& name,
35 ISvcLocator* pSvcLocator):
36
37 ExpressionParserUser< ::AthReentrantAlgorithm>( name, pSvcLocator )
38{
39}
40
41// Athena Algorithm's Hooks
44{
45
46 // Set up the text-parsing machinery for thinning the tracks directly according to user cuts
47 if (m_selectionString.empty()) {
48 ATH_MSG_FATAL("No inner detector track selection strings provided!");
49 return StatusCode::FAILURE;
50 }
51 ATH_MSG_INFO("Track thinning selection string: " << m_selectionString);
53
54 ATH_CHECK( m_inDetSGKey.initialize (m_streamName) );
55 ATH_MSG_INFO("Using " << m_inDetSGKey << "as the source collection for inner detector track particles");
56
58 ATH_MSG_INFO("Pixel states collection as source for thinning: " << m_statesPixSGKey.key());
59 ATH_MSG_INFO("Pixel measurements collection as source for thinning: " << m_measurementsPixSGKey.key());
60 }
62 ATH_MSG_INFO("SCT states collection as source for thinning: " << m_statesSctSGKey.key());
63 ATH_MSG_INFO("SCT measurements collection as source for thinning: " << m_measurementsSctSGKey.key());
64 }
66 ATH_MSG_INFO("TRT states collection as source for thinning: " << m_statesTrtSGKey.key());
67 ATH_MSG_INFO("TRT measurements collection as source for thinning: " << m_measurementsTrtSGKey.key());
68 }
69
70 ATH_MSG_DEBUG("Initializing measurement ThinningHandleKeys.");
74
75 ATH_MSG_DEBUG("Initializing MSOS ThinningHandleKeyArrays.");
79
80 ATH_CHECK(m_SCTDetEleCollKey.initialize( !m_SCTDetEleCollKey.key().empty() ));
81
82 return StatusCode::SUCCESS;
83}
84
86{
87 ATH_MSG_VERBOSE("finalize() ...");
88 ATH_MSG_INFO("Processed "<< m_ntot <<" tracks, "<< m_npass<< " had their hits retained ");
90 ATH_MSG_INFO("Pixel state objects thinning, Total / Passed (Efficiency): "
92 << " (" << (m_ntot_pix_states == 0 ? 0 : static_cast<float>(m_npass_pix_states) / m_ntot_pix_states) << ")");
93 ATH_MSG_INFO("Total pixel measurements objects from *all* track collections:: " << m_ntot_pix_measurements);
94 ATH_MSG_INFO("Pixel measurements objects from this track collection passing selection: " << m_npass_pix_measurements);
95 }
97 ATH_MSG_INFO("SCT state objects thinning, Total / Passed (Efficiency): "
99 << " (" << (m_ntot_sct_states == 0 ? 0 : static_cast<float>(m_npass_sct_states) / m_ntot_sct_states) << ")");
100 ATH_MSG_INFO("Total SCT measurements objects from *all* track collections:: " << m_ntot_sct_measurements);
101 ATH_MSG_INFO("SCT measurements objects from this track collection passing selection: " << m_npass_sct_measurements);
102 }
104 ATH_MSG_INFO("TRT state objects thinning, Total / Passed (Efficiency): "
106 << " (" << (m_ntot_trt_states == 0 ? 0 : static_cast<float>(m_npass_trt_states) / m_ntot_trt_states) << ")");
107 ATH_MSG_INFO("Total TRT measurements objects from *all* track collections:: " << m_ntot_trt_measurements);
108 ATH_MSG_INFO("TRT measurements objects from this track collection passing selection: " << m_npass_trt_measurements);
109 }
110
112
113 return StatusCode::SUCCESS;
114}
115
116// The thinning itself
117StatusCode ThinInDetClustersAlg::execute(const EventContext& ctx) const
118{
119
120 // Retrieve main TrackParticle collection
122 ATH_CHECK( importedTrackParticles.isValid() );
123
127 ATH_CHECK( importedMeasurements.isValid() );
128 unsigned int size_measurements = importedMeasurements->size();
129 m_ntot_pix_measurements += size_measurements;
130 }
133 ATH_CHECK( importedMeasurements.isValid() );
134 unsigned int size_measurements = importedMeasurements->size();
135 m_ntot_sct_measurements += size_measurements;
136 }
139 ATH_CHECK( importedMeasurements.isValid() );
140 unsigned int size_measurements = importedMeasurements->size();
141 m_ntot_trt_measurements += size_measurements;
142 }
144
145 // Check the event contains tracks
146 unsigned int nTracks = importedTrackParticles->size();
147 if (nTracks==0) return StatusCode::SUCCESS;
148
149 // Set up a mask with the same entries as the full TrackParticle collection
150 std::vector<bool> mask;
151 mask.assign(nTracks,false); // default: don't keep any tracks
152 m_ntot += nTracks;
153
154 // Execute the text parser and update the mask
155 if (m_parser) {
156 std::vector<int> entries = m_parser->evaluateAsVector();
157 unsigned int nEntries = entries.size();
158 // check the sizes are compatible
159 if (nTracks != nEntries ) {
160 ATH_MSG_ERROR("Sizes incompatible! Are you sure your selection string used ID TrackParticles?");
161 return StatusCode::FAILURE;
162 }
163 // set mask
164 for (unsigned int i=0; i<nTracks; ++i) {
165 if (entries[i]==1) {
166 mask[i]=true; // Set track mask
167 m_npass += 1; // Count passing tracks.
168 }
169 }
170 }
171
175 *importedTrackParticles,
176 mask,
182 ctx) );
183 }
187 *importedTrackParticles,
188 mask,
194 ctx) );
195 }
199 *importedTrackParticles,
200 mask,
206 ctx) );
207 }
208
209 return StatusCode::SUCCESS;
210}
211
213(MeasurementType detTypeToSelect,
214 const xAOD::TrackParticleContainer& inputTrackParticles,
215 const std::vector<bool>& inputMask,
218 std::atomic<unsigned int>& ntot_states,
219 std::atomic<unsigned int>& npass_states,
220 std::atomic<unsigned int>& npass_measurements,
221 const EventContext& ctx) const
222{
223 std::vector<bool> maskStates;
224 std::vector<bool> maskMeasurements;
225
226 selectTrackHits (inputTrackParticles, inputMask, detTypeToSelect,
227 maskStates, maskMeasurements);
228
229 auto count = [] (const std::vector<bool>& m)
230 { return std::count (m.begin(), m.end(), true); };
231 npass_states += count (maskStates);
232 npass_measurements += count (maskMeasurements);
233
234 if (!statesKey.empty()) {
235 SG::ThinningHandle<xAOD::TrackStateValidationContainer> importedStates(statesKey, ctx);
236 ATH_CHECK( importedStates.isValid() );
237 unsigned int size_states = importedStates->size();
238 if (size_states == 0) {
239 ATH_MSG_WARNING("States container is empty: " << statesKey.key());
240 }
241 else {
242 ntot_states += size_states;
243 if (maskStates.size() > size_states) {
244 ATH_MSG_ERROR("States mask size mismatch: mask size (" << maskStates.size() << ") > number of states (" << size_states << ").");
245 return StatusCode::FAILURE;
246 }
247 maskStates.resize (size_states);
248 importedStates.keep (maskStates);
249 }
250 }
251
252 if (!measurementsKey.empty()) {
253 SG::ThinningHandle<xAOD::TrackMeasurementValidationContainer> importedMeasurements(measurementsKey, ctx);
254 ATH_CHECK( importedMeasurements.isValid() );
255 unsigned int size_measurements = importedMeasurements->size();
256 if (size_measurements == 0) {
257 ATH_MSG_WARNING("Measurements container is empty: " << measurementsKey.key());
258 }
259 else {
260 if (maskMeasurements.size() > size_measurements) {
261 ATH_MSG_ERROR("Measurements mask size mismatch: mask size (" << maskMeasurements.size() << ") > number of measurements (" << size_measurements << ").");
262 return StatusCode::FAILURE;
263 }
264 maskMeasurements.resize (size_measurements);
265 importedMeasurements.keep (maskMeasurements);
266 }
267 }
268
269 return StatusCode::SUCCESS;
270}
271
273 const std::vector<bool>& inputMask,
274 MeasurementType detTypeToSelect,
275 std::vector<bool>& outputStatesMask, std::vector<bool>& outputMeasurementsMask) const
276{
277 using StatesOnTrack = std::vector<ElementLink<xAOD::TrackStateValidationContainer>>;
278
279 // loop over track particles, consider only the ones pre-selected by the mask
280 int trkIndex=-1;
281 for (const xAOD::TrackParticle* trkIt : inputTrackParticles) {
282 trkIndex++;
283
284 if (not inputMask[trkIndex]) continue;
285
286 // loop over the TrackStateValidation objects, and add them to the outputStatesMask
287 static const SG::AuxElement::ConstAccessor< StatesOnTrack > trackStateAcc("Reco_msosLink");
288 if( ! trackStateAcc.isAvailable( *trkIt ) ) {
289 ATH_MSG_INFO("Cannot find TrackState link from xAOD::TrackParticle. Skipping track.");
290 continue;
291 }
292 const StatesOnTrack& measurementsOnTrack = trackStateAcc(*trkIt);
293
294 for( const ElementLink<xAOD::TrackStateValidationContainer>& trkState_el : measurementsOnTrack) {
295 if (not trkState_el.isValid()) {
296 ATH_MSG_INFO("Cannot find a valid link to TrackStateValidation object for track index: " << trkIndex);
297 continue; //not a valid link
298 }
299 if ((*trkState_el)->detType() != detTypeToSelect) {
300 ATH_MSG_VERBOSE("Discarding TrackState as not of correct type " << detTypeToSelect);
301 continue;
302 }
303 if (trkState_el.index() >= outputStatesMask.size()) {
304 outputStatesMask.resize (trkState_el.index()+1);
305 }
306 outputStatesMask[trkState_el.index()] = true;
307
308 // get the corresponding TrackMeasurementValidation object, if any, and add it to the outputMeasurementsMask
309 const ElementLink<xAOD::TrackMeasurementValidationContainer> trkMeasurement_el = (*trkState_el)->trackMeasurementValidationLink();
310 if (not trkMeasurement_el.isValid()) {
311 ATH_MSG_VERBOSE("Cannot find a valid link to TrackMeasurementValidation object from track state for track index: " << trkIndex
312 << ", trackState index: " << trkState_el.index());
313 continue; //not a valid link
314 }
315 if (*trkMeasurement_el == nullptr) {
316 ATH_MSG_VERBOSE("Invalid pointer to TrackMeasurementValidation object from track state for track index: " << trkIndex
317 << ", trackState index: " << trkState_el.index());
318 continue; //not linking to a valid object -- is it necessary?
319 }
320 if (trkMeasurement_el.index() >= outputMeasurementsMask.size()) {
321 outputMeasurementsMask.resize (trkMeasurement_el.index()+1);
322 }
323 outputMeasurementsMask[trkMeasurement_el.index()] = true;
324 }
325 } // end loop over xAOD::TrackParticle container
326}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_FATAL(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
Handle for requesting thinning for a data object.
An algorithm that can be simultaneously executed in multiple threads.
std::conditional< 1==1, std::unique_ptr< ExpressionParsing::ExpressionParser >, std::array< std::unique_ptr< ExpressionParsing::ExpressionParser >, 1 > >::type m_parser
StatusCode initializeParser(const ExpressionParsing::SelectionArg< 1 > &selection_string)
SG::ConstAccessor< T, ALLOC > ConstAccessor
Definition AuxElement.h:569
bool isAvailable(const ELT &e) const
Test to see if this variable exists in the store.
virtual bool isValid() override final
Can the handle be successfully dereferenced?
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.
MeasurementType
Track state types Subset of enum MeasurementType from Athena: Tracking/TrkEvent/TrkEventPrimitives/Tr...
StringProperty m_selectionString
Expressions for object thinning selections Default InDetTrackParticles (also accomplished through fla...
std::atomic< unsigned int > m_ntot_sct_states
std::atomic< unsigned int > m_npass_pix_states
std::atomic< unsigned int > m_npass_sct_measurements
SG::ThinningHandleKey< xAOD::TrackMeasurementValidationContainer > m_measurementsPixSGKey
virtual StatusCode finalize() override
Athena algorithm's finalize hook.
SG::ThinningHandleKey< xAOD::TrackStateValidationContainer > m_statesSctSGKey
BooleanProperty m_thinPixelHitsOnTrack
Thinning logic.
BooleanProperty m_thinTRTHitsOnTrack
SG::ThinningHandleKey< xAOD::TrackStateValidationContainer > m_statesTrtSGKey
std::atomic< unsigned int > m_npass_trt_measurements
std::atomic< unsigned int > m_npass
ThinInDetClustersAlg(const std::string &name, ISvcLocator *pSvcLocator)
Constructor with parameters:
std::atomic< unsigned int > m_ntot_sct_measurements
std::atomic< unsigned int > m_ntot_pix_states
Counters and keys for xAOD::TrackStateValidation and xAOD::TrackMeasurementValidation containers.
SG::ThinningHandleKey< xAOD::TrackParticleContainer > m_inDetSGKey
SGKey for TrackParticleContainer.
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...
SG::ThinningHandleKey< xAOD::TrackMeasurementValidationContainer > m_measurementsTrtSGKey
std::atomic< unsigned int > m_npass_sct_states
std::atomic< unsigned int > m_ntot_pix_measurements
virtual StatusCode initialize() override
Athena algorithm's initalize hook.
std::atomic< unsigned int > m_npass_pix_measurements
std::atomic< unsigned int > m_ntot_trt_states
std::atomic< unsigned int > m_npass_trt_states
SG::ThinningHandleKey< xAOD::TrackStateValidationContainer > m_statesPixSGKey
SG::ReadCondHandleKey< InDetDD::SiDetectorElementCollection > m_SCTDetEleCollKey
For P->T converter of SCT_Clusters.
std::atomic< unsigned int > m_ntot_trt_measurements
StatusCode filterTrackHits(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 > &npass_states, std::atomic< unsigned int > &npass_measurements, const EventContext &ctx) const
StringProperty m_streamName
Stream for object thinning selections.
virtual StatusCode execute(const EventContext &ctx) const override
Athena algorithm's execute hook.
BooleanProperty m_thinSCTHitsOnTrack
std::atomic< unsigned int > m_ntot
Counters and keys for xAOD::TrackParticle container.
SG::ThinningHandleKey< xAOD::TrackMeasurementValidationContainer > m_measurementsSctSGKey
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".