ATLAS Offline Software
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 
11 #include "ThinInDetClustersAlg.h"
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< ::AthAlgorithm>( 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 
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  }
103  if (m_thinTRTHitsOnTrack) {
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
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  }
183  if (m_thinSCTHitsOnTrack) {
185  TrkState_SCT,
186  *importedTrackParticles,
187  mask,
193  }
194  if (m_thinTRTHitsOnTrack) {
196  TrkState_TRT,
197  *importedTrackParticles,
198  mask,
204  }
205 
206  return StatusCode::SUCCESS;
207 }
208 
210 (MeasurementType detTypeToSelect,
211  const xAOD::TrackParticleContainer& inputTrackParticles,
212  const std::vector<bool>& inputMask,
215  unsigned int& ntot_states,
216  unsigned int& npass_states,
217  unsigned int& npass_measurements) const
218 {
219  std::vector<bool> maskStates;
220  std::vector<bool> maskMeasurements;
221 
222  selectTrackHits (inputTrackParticles, inputMask, detTypeToSelect,
223  maskStates, maskMeasurements);
224 
225  auto count = [] (const std::vector<bool>& m)
226  { return std::count (m.begin(), m.end(), true); };
227  npass_states += count (maskStates);
228  npass_measurements += count (maskMeasurements);
229 
230  if (!statesKey.empty()) {
232  ATH_CHECK( importedStates.isValid() );
233  unsigned int size_states = importedStates->size();
234  if (size_states == 0) {
235  ATH_MSG_WARNING("States container is empty: " << statesKey.key());
236  }
237  else {
238  ntot_states += size_states;
239  if (maskStates.size() > size_states) {
240  ATH_MSG_ERROR("States mask size mismatch: mask size (" << maskStates.size() << ") > number of states (" << size_states << ").");
241  return StatusCode::FAILURE;
242  }
243  maskStates.resize (size_states);
244  importedStates.keep (maskStates);
245  }
246  }
247 
248  if (!measurementsKey.empty()) {
249  SG::ThinningHandle<xAOD::TrackMeasurementValidationContainer> importedMeasurements(measurementsKey);
250  ATH_CHECK( importedMeasurements.isValid() );
251  unsigned int size_measurements = importedMeasurements->size();
252  if (size_measurements == 0) {
253  ATH_MSG_WARNING("Measurements container is empty: " << measurementsKey.key());
254  }
255  else {
256  if (maskMeasurements.size() > size_measurements) {
257  ATH_MSG_ERROR("Measurements mask size mismatch: mask size (" << maskMeasurements.size() << ") > number of measurements (" << size_measurements << ").");
258  return StatusCode::FAILURE;
259  }
260  maskMeasurements.resize (size_measurements);
261  importedMeasurements.keep (maskMeasurements);
262  }
263  }
264 
265  return StatusCode::SUCCESS;
266 }
267 
269  const std::vector<bool>& inputMask,
270  MeasurementType detTypeToSelect,
271  std::vector<bool>& outputStatesMask, std::vector<bool>& outputMeasurementsMask) const
272 {
273  using StatesOnTrack = std::vector<ElementLink<xAOD::TrackStateValidationContainer>>;
274 
275  // loop over track particles, consider only the ones pre-selected by the mask
276  int trkIndex=-1;
277  for (const xAOD::TrackParticle* trkIt : inputTrackParticles) {
278  trkIndex++;
279 
280  if (not inputMask[trkIndex]) continue;
281 
282  // loop over the TrackStateValidation objects, and add them to the outputStatesMask
283  static const SG::AuxElement::ConstAccessor< StatesOnTrack > trackStateAcc("Reco_msosLink");
284  if( ! trackStateAcc.isAvailable( *trkIt ) ) {
285  ATH_MSG_INFO("Cannot find TrackState link from xAOD::TrackParticle. Skipping track.");
286  continue;
287  }
288  const StatesOnTrack& measurementsOnTrack = trackStateAcc(*trkIt);
289 
290  for( const ElementLink<xAOD::TrackStateValidationContainer>& trkState_el : measurementsOnTrack) {
291  if (not trkState_el.isValid()) {
292  ATH_MSG_INFO("Cannot find a valid link to TrackStateValidation object for track index: " << trkIndex);
293  continue; //not a valid link
294  }
295  if ((*trkState_el)->detType() != detTypeToSelect) {
296  ATH_MSG_VERBOSE("Discarding TrackState as not of correct type " << detTypeToSelect);
297  continue;
298  }
299  if (trkState_el.index() >= outputStatesMask.size()) {
300  outputStatesMask.resize (trkState_el.index()+1);
301  }
302  outputStatesMask[trkState_el.index()] = true;
303 
304  // get the corresponding TrackMeasurementValidation object, if any, and add it to the outputMeasurementsMask
305  const ElementLink<xAOD::TrackMeasurementValidationContainer> trkMeasurement_el = (*trkState_el)->trackMeasurementValidationLink();
306  if (not trkMeasurement_el.isValid()) {
307  ATH_MSG_VERBOSE("Cannot find a valid link to TrackMeasurementValidation object from track state for track index: " << trkIndex
308  << ", trackState index: " << trkState_el.index());
309  continue; //not a valid link
310  }
311  if (*trkMeasurement_el == nullptr) {
312  ATH_MSG_VERBOSE("Invalid pointer to TrackMeasurementValidation object from track state for track index: " << trkIndex
313  << ", trackState index: " << trkState_el.index());
314  continue; //not linking to a valid object -- is it necessary?
315  }
316  if (trkMeasurement_el.index() >= outputMeasurementsMask.size()) {
317  outputMeasurementsMask.resize (trkMeasurement_el.index()+1);
318  }
319  outputMeasurementsMask[trkMeasurement_el.index()] = true;
320  }
321  } // end loop over xAOD::TrackParticle container
322 }
ThinInDetClustersAlg::m_statesSctSGKey
SG::ThinningHandleKey< xAOD::TrackStateValidationContainer > m_statesSctSGKey
Definition: ThinInDetClustersAlg.h:90
ThinInDetClustersAlg::m_ntot_pix_measurements
unsigned int m_ntot_pix_measurements
Definition: ThinInDetClustersAlg.h:82
ExpressionParserUserBase< ::AthAlgorithm, 1 >::finalizeParser
StatusCode finalizeParser()
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
python.SystemOfUnits.m
int m
Definition: SystemOfUnits.py:91
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
SG::ThinningHandleKey
HandleKey object for adding thinning to an object.
Definition: ThinningHandleKey.h:38
ThinningHandle.h
Handle for requesting thinning for a data object.
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
ThinInDetClustersAlg::m_selectionString
StringProperty m_selectionString
Expressions for object thinning selections Default InDetTrackParticles (also accomplished through fla...
Definition: ThinInDetClustersAlg.h:70
ExpressionParserUserBase< ::AthAlgorithm, 1 >::m_parser
std::conditional< NUM_PARSER==1, std::unique_ptr< ExpressionParsing::ExpressionParser >, std::array< std::unique_ptr< ExpressionParsing::ExpressionParser >, NUM_PARSER > >::type m_parser
Definition: ExpressionParserUser.h:100
ThinInDetClustersAlg::m_npass_sct_states
unsigned int m_npass_sct_states
Definition: ThinInDetClustersAlg.h:88
ThinInDetClustersAlg::MeasurementType
MeasurementType
Track state types Subset of enum MeasurementType from Athena: Tracking/TrkEvent/TrkEventPrimitives/Tr...
Definition: ThinInDetClustersAlg.h:112
ThinInDetClustersAlg::m_thinTRTHitsOnTrack
BooleanProperty m_thinTRTHitsOnTrack
Definition: ThinInDetClustersAlg.h:60
SG::ConstAccessor
Helper class to provide constant type-safe access to aux data.
Definition: ConstAccessor.h:55
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
SG::VarHandleKey::key
const std::string & key() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:141
ThinInDetClustersAlg::m_ntot_pix_states
unsigned int m_ntot_pix_states
Counters and keys for xAOD::TrackStateValidation and xAOD::TrackMeasurementValidation containers.
Definition: ThinInDetClustersAlg.h:77
ThinInDetClustersAlg::execute
virtual StatusCode execute() override
Athena algorithm's execute hook.
Definition: ThinInDetClustersAlg.cxx:117
SG::VarHandleKey::empty
bool empty() const
Test if the key is blank.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:150
ThinInDetClustersAlg::selectTrackHits
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...
Definition: ThinInDetClustersAlg.cxx:268
ThinInDetClustersAlg::finalize
virtual StatusCode finalize() override
Athena algorithm's finalize hook.
Definition: ThinInDetClustersAlg.cxx:85
ThinInDetClustersAlg::m_ntot
unsigned int m_ntot
Counters and keys for xAOD::TrackParticle container.
Definition: ThinInDetClustersAlg.h:52
ThinInDetClustersAlg::m_ntot_trt_states
unsigned int m_ntot_trt_states
Definition: ThinInDetClustersAlg.h:97
XMLtoHeader.count
count
Definition: XMLtoHeader.py:85
python.utils.AtlRunQueryLookup.mask
string mask
Definition: AtlRunQueryLookup.py:460
SG::ThinningHandle
Handle for requesting thinning for a data object.
Definition: ThinningHandle.h:84
ThinInDetClustersAlg.h
ThinInDetClustersAlg::m_npass_trt_states
unsigned int m_npass_trt_states
Definition: ThinInDetClustersAlg.h:98
ThinInDetClustersAlg::m_statesPixSGKey
SG::ThinningHandleKey< xAOD::TrackStateValidationContainer > m_statesPixSGKey
Definition: ThinInDetClustersAlg.h:80
ThinInDetClustersAlg::initialize
virtual StatusCode initialize() override
Athena algorithm's initalize hook.
Definition: ThinInDetClustersAlg.cxx:43
SG::ThinningHandleBase::keep
void keep(size_t ndx)
Mark that index ndx in the container should be kept (not thinned away).
Definition: ThinningHandleBase.cxx:68
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
ThinInDetClustersAlg::TrkState_TRT
@ TrkState_TRT
Definition: ThinInDetClustersAlg.h:116
ThinInDetClustersAlg::m_npass_pix_states
unsigned int m_npass_pix_states
Definition: ThinInDetClustersAlg.h:78
ThinInDetClustersAlg::ThinInDetClustersAlg
ThinInDetClustersAlg(const std::string &name, ISvcLocator *pSvcLocator)
Constructor with parameters:
Definition: ThinInDetClustersAlg.cxx:33
ThinInDetClustersAlg::m_ntot_sct_states
unsigned int m_ntot_sct_states
Definition: ThinInDetClustersAlg.h:87
lumiFormat.i
int i
Definition: lumiFormat.py:85
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
ExpressionParserUser.icc
ThinInDetClustersAlg::m_measurementsPixSGKey
SG::ThinningHandleKey< xAOD::TrackMeasurementValidationContainer > m_measurementsPixSGKey
Definition: ThinInDetClustersAlg.h:85
ThinInDetClustersAlg::m_thinSCTHitsOnTrack
BooleanProperty m_thinSCTHitsOnTrack
Definition: ThinInDetClustersAlg.h:58
ThinInDetClustersAlg::filterTrackHits
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, unsigned int &ntot_states, unsigned int &npass_states, unsigned int &npass_measurements) const
Definition: ThinInDetClustersAlg.cxx:210
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
ThinInDetClustersAlg::m_ntot_sct_measurements
unsigned int m_ntot_sct_measurements
Definition: ThinInDetClustersAlg.h:92
ThinInDetClustersAlg::m_streamName
StringProperty m_streamName
Stream for object thinning selections.
Definition: ThinInDetClustersAlg.h:64
ThinInDetClustersAlg::TrkState_SCT
@ TrkState_SCT
Definition: ThinInDetClustersAlg.h:115
DataVector
Derived DataVector<T>.
Definition: DataVector.h:794
AthAlgorithm
Definition: AthAlgorithm.h:47
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
ExpressionParserUser<::AthAlgorithm >::initializeParser
StatusCode initializeParser(const ExpressionParsing::SelectionArg< 1 > &selection_string)
ThinInDetClustersAlg::m_inDetSGKey
SG::ThinningHandleKey< xAOD::TrackParticleContainer > m_inDetSGKey
SGKey for TrackParticleContainer.
Definition: ThinInDetClustersAlg.h:74
ThinInDetClustersAlg::m_statesTrtSGKey
SG::ThinningHandleKey< xAOD::TrackStateValidationContainer > m_statesTrtSGKey
Definition: ThinInDetClustersAlg.h:100
ExpressionParserUser
Definition: ExpressionParserUser.h:107
ThinInDetClustersAlg::m_npass_sct_measurements
unsigned int m_npass_sct_measurements
Definition: ThinInDetClustersAlg.h:93
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
ThinInDetClustersAlg::m_measurementsTrtSGKey
SG::ThinningHandleKey< xAOD::TrackMeasurementValidationContainer > m_measurementsTrtSGKey
Definition: ThinInDetClustersAlg.h:105
SG::CondHandleKey::initialize
StatusCode initialize(bool used=true)
TrackStateValidationContainer.h
ThinInDetClustersAlg::TrkState_Pixel
@ TrkState_Pixel
Definition: ThinInDetClustersAlg.h:114
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
ThinInDetClustersAlg::m_ntot_trt_measurements
unsigned int m_ntot_trt_measurements
Definition: ThinInDetClustersAlg.h:102
entries
double entries
Definition: listroot.cxx:49
ThinInDetClustersAlg::m_npass_trt_measurements
unsigned int m_npass_trt_measurements
Definition: ThinInDetClustersAlg.h:103
ThinInDetClustersAlg::m_thinPixelHitsOnTrack
BooleanProperty m_thinPixelHitsOnTrack
Thinning logic.
Definition: ThinInDetClustersAlg.h:56
TrackMeasurementValidationContainer.h
SG::ConstAccessor::isAvailable
bool isAvailable(const ELT &e) const
Test to see if this variable exists in the store.
ThinInDetClustersAlg::m_npass
unsigned int m_npass
Definition: ThinInDetClustersAlg.h:53
ThinInDetClustersAlg::m_SCTDetEleCollKey
SG::ReadCondHandleKey< InDetDD::SiDetectorElementCollection > m_SCTDetEleCollKey
For P->T converter of SCT_Clusters.
Definition: ThinInDetClustersAlg.h:108
ExpressionParser.h
xAOD::TrackParticle_v1
Class describing a TrackParticle.
Definition: TrackParticle_v1.h:43
ThinInDetClustersAlg::m_measurementsSctSGKey
SG::ThinningHandleKey< xAOD::TrackMeasurementValidationContainer > m_measurementsSctSGKey
Definition: ThinInDetClustersAlg.h:95
dqBeamSpot.nEntries
int nEntries
Definition: dqBeamSpot.py:73
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
TrackParticleContainer.h
ThinInDetClustersAlg::m_npass_pix_measurements
unsigned int m_npass_pix_measurements
Definition: ThinInDetClustersAlg.h:83