ATLAS Offline Software
Loading...
Searching...
No Matches
MuonTrackParticleThinning.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3*/
4
6// MuonTrackParticleThinning.cxx, (c) ATLAS Detector software
8// Author: James Catmore (James.Catmore@cern.ch)
9// This is a trivial example of an implementation of a thinning tool
10// which removes all ID tracks which do not pass a user-defined cut
11
17#include <vector>
18#include <string>
19
20// Constructor
22 const std::string& n,
23 const IInterface* p ) :
24base_class(t,n,p)
25{
26}
27
28// Destructor
30
31// Athena initialize and finalize
33{
34 // Decide which collections need to be checked for ID TrackParticles
35 ATH_MSG_VERBOSE("initialize() ...");
36 ATH_CHECK( m_inDetSGKey.initialize (m_streamName) );
37 ATH_MSG_INFO("Using " << m_inDetSGKey.key() << "as the source collection for inner detector track particles");
38 if (m_muonKey.key().empty()) {
39 ATH_MSG_FATAL("No muon collection provided for thinning.");
40 return StatusCode::FAILURE;
41 } else { ATH_MSG_INFO("Inner detector track particles associated with objects in " << m_muonKey.key() << " will be retained in this format with the rest being thinned away");}
42 ATH_CHECK(m_muonKey.initialize());
43 // Set up the text-parsing machinery for selectiong the muon directly according to user cuts
44 if (!m_selectionString.empty()) {
45 ATH_CHECK(initializeParser(m_selectionString) );
46 }
47 return StatusCode::SUCCESS;
48}
49
51{
52 ATH_MSG_VERBOSE("finalize() ...");
53 ATH_MSG_INFO("Processed "<< m_ntot <<" tracks, "<< m_npass<< " were retained ");
54 ATH_CHECK( finalizeParser() );
55 return StatusCode::SUCCESS;
56}
57
58// The thinning itself
59StatusCode DerivationFramework::MuonTrackParticleThinning::doThinning(const EventContext& ctx) const
60{
61
62 // Retrieve main TrackParticle collection
64 (m_inDetSGKey, ctx);
65
66 // Check the event contains tracks
67 unsigned int nTracks = importedTrackParticles->size();
68 if (nTracks==0) return StatusCode::SUCCESS;
69
70 // Set up a mask with the same entries as the full TrackParticle collection
71 std::vector<bool> mask;
72 mask.assign(nTracks,false); // default: don't keep any tracks
73 m_ntot += nTracks;
74
75 // Retrieve containers
76 // ... muons
78 if (!importedMuons.isValid()) {
79 ATH_MSG_ERROR("No muon collection with name " << m_muonKey.key() << " found in StoreGate!");
80 return StatusCode::FAILURE;
81 }
82 unsigned int nMuons(importedMuons->size());
83 std::vector<const xAOD::Muon*> muToCheck; muToCheck.clear();
84 // Execute the text parser if requested
85 if (m_selectionString!="") {
86 std::vector<int> entries = m_parser->evaluateAsVector();
87 unsigned int nEntries = entries.size();
88 // check the sizes are compatible
89 if (nMuons != nEntries ) {
90 ATH_MSG_ERROR("Sizes incompatible! Are you sure your selection string used muons??");
91 return StatusCode::FAILURE;
92 } else {
93 // identify which muons to keep for the thinning check
94 for (unsigned int i=0; i<nMuons; ++i) if (entries[i]==1) muToCheck.push_back((*importedMuons)[i]);
95 }
96 }
97
99 if (m_selectionString=="") { // check all muons as user didn't provide a selection string
100 for (const auto *muIt : *importedMuons) {
101 if (muIt->trackParticle(xAOD::Muon::TrackParticleType::InnerDetectorTrackParticle) != nullptr) {
102 //This line prevents SiliconAssociatedForwardMuon from being used unless we're skimming InDetForwardTrackParticles
103 //since their track links point to this container while all others point to InDetTrackParticles
104 if (muIt->muonType()==xAOD::Muon::MuonType::SiliconAssociatedForwardMuon && m_inDetSGKey.key() != "InDetForwardTrackParticles")
105 {
106 ATH_MSG_DEBUG("Skipping Forward Muon since we are not skimming InDetForwardParticles");
107 }
108 else{
109 ATH_MSG_DEBUG("Simming Muon tracks in " << m_inDetSGKey << " "<< muIt->muonType());
110 int index = muIt->trackParticle(xAOD::Muon::TrackParticleType::InnerDetectorTrackParticle)->index();
111 mask[index] = true;
112 }
113 }
114 if (m_coneSize>0.0) tInC.select(muIt,m_coneSize,importedTrackParticles.cptr(),mask); // check tracks in a cone around the muon if req'd
115 }
116 } else { // check only muons passing user selection string
117 for (auto & muIt : muToCheck) {
118 if (muIt->trackParticle(xAOD::Muon::TrackParticleType::InnerDetectorTrackParticle) != nullptr) {
119
120 if (muIt->muonType()==xAOD::Muon::MuonType::SiliconAssociatedForwardMuon && m_inDetSGKey.key() != "InDetForwardTrackParticles")
121 {
122 ATH_MSG_DEBUG("Skipping Forward Muon since we are not skimming InDetForwardParticles");
123 }
124 else{
125 ATH_MSG_DEBUG("Simming Muon tracks in " << m_inDetSGKey << " "<< muIt->muonType());
126 int index = muIt->trackParticle(xAOD::Muon::TrackParticleType::InnerDetectorTrackParticle)->index();
127 mask[index] = true;
128 }
129 }
130 if (m_coneSize>0.0) tInC.select(muIt,m_coneSize,importedTrackParticles.cptr(),mask); // check tracks in a cone around the muon if req'd
131 }
132 }
133
134 // Count up the mask contents
135 unsigned int n_pass=0;
136 for (unsigned int i=0; i<nTracks; ++i) {
137 if (mask[i]) ++n_pass;
138 }
139 m_npass+=n_pass;
140 // Execute the thinning service based on the mask. Finish.
141 importedTrackParticles.keep (mask);
142
143 return StatusCode::SUCCESS;
144}
145
#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_DEBUG(x)
Handle for requesting thinning for a data object.
virtual StatusCode doThinning(const EventContext &ctx) const override
SG::ReadHandleKey< xAOD::MuonContainer > m_muonKey
MuonTrackParticleThinning(const std::string &t, const std::string &n, const IInterface *p)
SG::ThinningHandleKey< xAOD::TrackParticleContainer > m_inDetSGKey
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).
Handle for requesting thinning for a data object.
double entries
Definition listroot.cxx:49
Definition index.py:1
void select(const xAOD::IParticle *particle, float coneSize, const xAOD::TrackParticleContainer *tracks, std::vector< bool > &mask)