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 "GaudiKernel/ThreadLocalContext.h"
18#include <vector>
19#include <string>
20
21// Constructor
23 const std::string& n,
24 const IInterface* p ) :
25base_class(t,n,p)
26{
27}
28
29// Destructor
31
32// Athena initialize and finalize
34{
35 // Decide which collections need to be checked for ID TrackParticles
36 ATH_MSG_VERBOSE("initialize() ...");
37 ATH_CHECK( m_inDetSGKey.initialize (m_streamName) );
38 ATH_MSG_INFO("Using " << m_inDetSGKey.key() << "as the source collection for inner detector track particles");
39 if (m_muonKey.key().empty()) {
40 ATH_MSG_FATAL("No muon collection provided for thinning.");
41 return StatusCode::FAILURE;
42 } 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");}
43 ATH_CHECK(m_muonKey.initialize());
44 // Set up the text-parsing machinery for selectiong the muon directly according to user cuts
45 if (!m_selectionString.empty()) {
46 ATH_CHECK(initializeParser(m_selectionString) );
47 }
48 return StatusCode::SUCCESS;
49}
50
52{
53 ATH_MSG_VERBOSE("finalize() ...");
54 ATH_MSG_INFO("Processed "<< m_ntot <<" tracks, "<< m_npass<< " were retained ");
55 ATH_CHECK( finalizeParser() );
56 return StatusCode::SUCCESS;
57}
58
59// The thinning itself
60StatusCode DerivationFramework::MuonTrackParticleThinning::doThinning(const EventContext& ctx) const
61{
62
63 // Retrieve main TrackParticle collection
65 (m_inDetSGKey, ctx);
66
67 // Check the event contains tracks
68 unsigned int nTracks = importedTrackParticles->size();
69 if (nTracks==0) return StatusCode::SUCCESS;
70
71 // Set up a mask with the same entries as the full TrackParticle collection
72 std::vector<bool> mask;
73 mask.assign(nTracks,false); // default: don't keep any tracks
74 m_ntot += nTracks;
75
76 // Retrieve containers
77 // ... muons
79 if (!importedMuons.isValid()) {
80 ATH_MSG_ERROR("No muon collection with name " << m_muonKey.key() << " found in StoreGate!");
81 return StatusCode::FAILURE;
82 }
83 unsigned int nMuons(importedMuons->size());
84 std::vector<const xAOD::Muon*> muToCheck; muToCheck.clear();
85 // Execute the text parser if requested
86 if (m_selectionString!="") {
87 std::vector<int> entries = m_parser->evaluateAsVector();
88 unsigned int nEntries = entries.size();
89 // check the sizes are compatible
90 if (nMuons != nEntries ) {
91 ATH_MSG_ERROR("Sizes incompatible! Are you sure your selection string used muons??");
92 return StatusCode::FAILURE;
93 } else {
94 // identify which muons to keep for the thinning check
95 for (unsigned int i=0; i<nMuons; ++i) if (entries[i]==1) muToCheck.push_back((*importedMuons)[i]);
96 }
97 }
98
100 if (m_selectionString=="") { // check all muons as user didn't provide a selection string
101 for (const auto *muIt : *importedMuons) {
102 if (muIt->inDetTrackParticleLink().isValid()) {
103 //This line prevents SiliconAssociatedForwardMuon from being used unless we're skimming InDetForwardTrackParticles
104 //since their track links point to this container while all others point to InDetTrackParticles
105 if (muIt->muonType()==xAOD::Muon::SiliconAssociatedForwardMuon && m_inDetSGKey.key() != "InDetForwardTrackParticles")
106 {
107 ATH_MSG_DEBUG("Skipping Forward Muon since we are not skimming InDetForwardParticles");
108 }
109 else{
110 ATH_MSG_DEBUG("Simming Muon tracks in " << m_inDetSGKey << " "<< muIt->muonType());
111 int index = muIt->inDetTrackParticleLink().index();
112 mask[index] = true;
113 }
114 }
115 if (m_coneSize>0.0) tInC.select(muIt,m_coneSize,importedTrackParticles.cptr(),mask); // check tracks in a cone around the muon if req'd
116 }
117 } else { // check only muons passing user selection string
118 for (auto & muIt : muToCheck) {
119 if (muIt->inDetTrackParticleLink().isValid()) {
120
121 if (muIt->muonType()==xAOD::Muon::SiliconAssociatedForwardMuon && m_inDetSGKey.key() != "InDetForwardTrackParticles")
122 {
123 ATH_MSG_DEBUG("Skipping Forward Muon since we are not skimming InDetForwardParticles");
124 }
125 else{
126 ATH_MSG_DEBUG("Simming Muon tracks in " << m_inDetSGKey << " "<< muIt->muonType());
127 int index = muIt->inDetTrackParticleLink().index();
128 mask[index] = true;
129 }
130 }
131 if (m_coneSize>0.0) tInC.select(muIt,m_coneSize,importedTrackParticles.cptr(),mask); // check tracks in a cone around the muon if req'd
132 }
133 }
134
135 // Count up the mask contents
136 unsigned int n_pass=0;
137 for (unsigned int i=0; i<nTracks; ++i) {
138 if (mask[i]) ++n_pass;
139 }
140 m_npass+=n_pass;
141 // Execute the thinning service based on the mask. Finish.
142 importedTrackParticles.keep (mask);
143
144 return StatusCode::SUCCESS;
145}
146
#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)