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"
25
27// Public methods:
29
30// Constructors
33 const std::string& name,
34 ISvcLocator* pSvcLocator):
35
36 ExpressionParserUser< ::AthReentrantAlgorithm>( name, pSvcLocator )
37{
38}
39
40// Athena Algorithm's Hooks
43{
44
45 // Set up the text-parsing machinery for thinning the tracks directly according to user cuts
46 if (m_selectionString.empty()) {
47 ATH_MSG_FATAL("No inner detector track selection strings provided!");
48 return StatusCode::FAILURE;
49 }
50 ATH_MSG_INFO("Track thinning selection string: " << m_selectionString);
52
53 ATH_CHECK( m_inDetSGKey.initialize (m_streamName) );
54 ATH_MSG_INFO("Using " << m_inDetSGKey << "as the source collection for inner detector track particles");
55
57 ATH_MSG_INFO("Pixel states collection as source for thinning: " << m_statesPixSGKey.key());
58 ATH_MSG_INFO("Pixel measurements collection as source for thinning: " << m_measurementsPixSGKey.key());
59 }
61 ATH_MSG_INFO("SCT states collection as source for thinning: " << m_statesSctSGKey.key());
62 ATH_MSG_INFO("SCT measurements collection as source for thinning: " << m_measurementsSctSGKey.key());
63 }
65 ATH_MSG_INFO("TRT states collection as source for thinning: " << m_statesTrtSGKey.key());
66 ATH_MSG_INFO("TRT measurements collection as source for thinning: " << m_measurementsTrtSGKey.key());
67 }
68
69 ATH_MSG_DEBUG("Initializing measurement ThinningHandleKeys.");
73
74 ATH_MSG_DEBUG("Initializing MSOS ThinningHandleKeyArrays.");
78
79 ATH_CHECK(m_SCTDetEleCollKey.initialize( !m_SCTDetEleCollKey.key().empty() ));
80
81 return StatusCode::SUCCESS;
82}
83
85{
86 ATH_MSG_VERBOSE("finalize() ...");
87 ATH_MSG_INFO("Processed "<< m_ntot <<" tracks, "<< m_npass<< " had their hits retained ");
89 ATH_MSG_INFO("Pixel state objects thinning, Total / Passed (Efficiency): "
91 << " (" << (m_ntot_pix_states == 0 ? 0 : static_cast<float>(m_npass_pix_states) / m_ntot_pix_states) << ")");
92 ATH_MSG_INFO("Total pixel measurements objects from *all* track collections:: " << m_ntot_pix_measurements);
93 ATH_MSG_INFO("Pixel measurements objects from this track collection passing selection: " << m_npass_pix_measurements);
94 }
96 ATH_MSG_INFO("SCT state objects thinning, Total / Passed (Efficiency): "
98 << " (" << (m_ntot_sct_states == 0 ? 0 : static_cast<float>(m_npass_sct_states) / m_ntot_sct_states) << ")");
99 ATH_MSG_INFO("Total SCT measurements objects from *all* track collections:: " << m_ntot_sct_measurements);
100 ATH_MSG_INFO("SCT measurements objects from this track collection passing selection: " << m_npass_sct_measurements);
101 }
103 ATH_MSG_INFO("TRT state objects thinning, Total / Passed (Efficiency): "
105 << " (" << (m_ntot_trt_states == 0 ? 0 : static_cast<float>(m_npass_trt_states) / m_ntot_trt_states) << ")");
106 ATH_MSG_INFO("Total TRT measurements objects from *all* track collections:: " << m_ntot_trt_measurements);
107 ATH_MSG_INFO("TRT measurements objects from this track collection passing selection: " << m_npass_trt_measurements);
108 }
109
111
112 return StatusCode::SUCCESS;
113}
114
115// The thinning itself
116StatusCode ThinInDetClustersAlg::execute(const EventContext& ctx) const
117{
118
119 // Retrieve main TrackParticle collection
121 ATH_CHECK( importedTrackParticles.isValid() );
122
126 ATH_CHECK( importedMeasurements.isValid() );
127 unsigned int size_measurements = importedMeasurements->size();
128 m_ntot_pix_measurements += size_measurements;
129 }
132 ATH_CHECK( importedMeasurements.isValid() );
133 unsigned int size_measurements = importedMeasurements->size();
134 m_ntot_sct_measurements += size_measurements;
135 }
138 ATH_CHECK( importedMeasurements.isValid() );
139 unsigned int size_measurements = importedMeasurements->size();
140 m_ntot_trt_measurements += size_measurements;
141 }
143
144 // Check the event contains tracks
145 unsigned int nTracks = importedTrackParticles->size();
146 if (nTracks==0) return StatusCode::SUCCESS;
147
148 // Set up a mask with the same entries as the full TrackParticle collection
149 std::vector<bool> mask;
150 mask.assign(nTracks,false); // default: don't keep any tracks
151 m_ntot += nTracks;
152
153 // Execute the text parser and update the mask
154 if (m_parser) {
155 std::vector<int> entries = m_parser->evaluateAsVector();
156 unsigned int nEntries = entries.size();
157 // check the sizes are compatible
158 if (nTracks != nEntries ) {
159 ATH_MSG_ERROR("Sizes incompatible! Are you sure your selection string used ID TrackParticles?");
160 return StatusCode::FAILURE;
161 }
162 // set mask
163 for (unsigned int i=0; i<nTracks; ++i) {
164 if (entries[i]==1) {
165 mask[i]=true; // Set track mask
166 m_npass += 1; // Count passing tracks.
167 }
168 }
169 }
170
174 *importedTrackParticles,
175 mask,
181 ctx) );
182 }
186 *importedTrackParticles,
187 mask,
193 ctx) );
194 }
198 *importedTrackParticles,
199 mask,
205 ctx) );
206 }
207
208 return StatusCode::SUCCESS;
209}
210
212(MeasurementType detTypeToSelect,
213 const xAOD::TrackParticleContainer& inputTrackParticles,
214 const std::vector<bool>& inputMask,
217 std::atomic<unsigned int>& ntot_states,
218 std::atomic<unsigned int>& npass_states,
219 std::atomic<unsigned int>& npass_measurements,
220 const EventContext& ctx) const
221{
222 std::vector<bool> maskStates;
223 std::vector<bool> maskMeasurements;
224
225 selectTrackHits (inputTrackParticles, inputMask, detTypeToSelect,
226 maskStates, maskMeasurements);
227
228 auto count = [] (const std::vector<bool>& m)
229 { return std::count (m.begin(), m.end(), true); };
230 npass_states += count (maskStates);
231 npass_measurements += count (maskMeasurements);
232
233 if (!statesKey.empty()) {
234 SG::ThinningHandle<xAOD::TrackStateValidationContainer> importedStates(statesKey, ctx);
235 ATH_CHECK( importedStates.isValid() );
236 unsigned int size_states = importedStates->size();
237 if (size_states == 0) {
238 ATH_MSG_WARNING("States container is empty: " << statesKey.key());
239 }
240 else {
241 ntot_states += size_states;
242 if (maskStates.size() > size_states) {
243 ATH_MSG_ERROR("States mask size mismatch: mask size (" << maskStates.size() << ") > number of states (" << size_states << ").");
244 return StatusCode::FAILURE;
245 }
246 maskStates.resize (size_states);
247 importedStates.keep (maskStates);
248 }
249 }
250
251 if (!measurementsKey.empty()) {
252 SG::ThinningHandle<xAOD::TrackMeasurementValidationContainer> importedMeasurements(measurementsKey, ctx);
253 ATH_CHECK( importedMeasurements.isValid() );
254 unsigned int size_measurements = importedMeasurements->size();
255 if (size_measurements == 0) {
256 ATH_MSG_WARNING("Measurements container is empty: " << measurementsKey.key());
257 }
258 else {
259 if (maskMeasurements.size() > size_measurements) {
260 ATH_MSG_ERROR("Measurements mask size mismatch: mask size (" << maskMeasurements.size() << ") > number of measurements (" << size_measurements << ").");
261 return StatusCode::FAILURE;
262 }
263 maskMeasurements.resize (size_measurements);
264 importedMeasurements.keep (maskMeasurements);
265 }
266 }
267
268 return StatusCode::SUCCESS;
269}
270
272 const std::vector<bool>& inputMask,
273 MeasurementType detTypeToSelect,
274 std::vector<bool>& outputStatesMask, std::vector<bool>& outputMeasurementsMask) const
275{
276 using StatesOnTrack = std::vector<ElementLink<xAOD::TrackStateValidationContainer>>;
277
278 // loop over track particles, consider only the ones pre-selected by the mask
279 int trkIndex=-1;
280 for (const xAOD::TrackParticle* trkIt : inputTrackParticles) {
281 trkIndex++;
282
283 if (not inputMask[trkIndex]) continue;
284
285 // loop over the TrackStateValidation objects, and add them to the outputStatesMask
286 static const SG::AuxElement::ConstAccessor< StatesOnTrack > trackStateAcc("Reco_msosLink");
287 if( ! trackStateAcc.isAvailable( *trkIt ) ) {
288 ATH_MSG_INFO("Cannot find TrackState link from xAOD::TrackParticle. Skipping track.");
289 continue;
290 }
291 const StatesOnTrack& measurementsOnTrack = trackStateAcc(*trkIt);
292
293 for( const ElementLink<xAOD::TrackStateValidationContainer>& trkState_el : measurementsOnTrack) {
294 if (not trkState_el.isValid()) {
295 ATH_MSG_INFO("Cannot find a valid link to TrackStateValidation object for track index: " << trkIndex);
296 continue; //not a valid link
297 }
298 if ((*trkState_el)->detType() != detTypeToSelect) {
299 ATH_MSG_VERBOSE("Discarding TrackState as not of correct type " << detTypeToSelect);
300 continue;
301 }
302 if (trkState_el.index() >= outputStatesMask.size()) {
303 outputStatesMask.resize (trkState_el.index()+1);
304 }
305 outputStatesMask[trkState_el.index()] = true;
306
307 // get the corresponding TrackMeasurementValidation object, if any, and add it to the outputMeasurementsMask
308 const ElementLink<xAOD::TrackMeasurementValidationContainer> trkMeasurement_el = (*trkState_el)->trackMeasurementValidationLink();
309 if (not trkMeasurement_el.isValid()) {
310 ATH_MSG_VERBOSE("Cannot find a valid link to TrackMeasurementValidation object from track state for track index: " << trkIndex
311 << ", trackState index: " << trkState_el.index());
312 continue; //not a valid link
313 }
314 if (*trkMeasurement_el == nullptr) {
315 ATH_MSG_VERBOSE("Invalid pointer to TrackMeasurementValidation object from track state for track index: " << trkIndex
316 << ", trackState index: " << trkState_el.index());
317 continue; //not linking to a valid object -- is it necessary?
318 }
319 if (trkMeasurement_el.index() >= outputMeasurementsMask.size()) {
320 outputMeasurementsMask.resize (trkMeasurement_el.index()+1);
321 }
322 outputMeasurementsMask[trkMeasurement_el.index()] = true;
323 }
324 } // end loop over xAOD::TrackParticle container
325}
#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)
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: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".