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
61{
62 const EventContext& ctx = Gaudi::Hive::currentContext();
63
64 // Retrieve main TrackParticle collection
66 (m_inDetSGKey, ctx);
67
68 // Check the event contains tracks
69 unsigned int nTracks = importedTrackParticles->size();
70 if (nTracks==0) return StatusCode::SUCCESS;
71
72 // Set up a mask with the same entries as the full TrackParticle collection
73 std::vector<bool> mask;
74 mask.assign(nTracks,false); // default: don't keep any tracks
75 m_ntot += nTracks;
76
77 // Retrieve containers
78 // ... muons
80 if (!importedMuons.isValid()) {
81 ATH_MSG_ERROR("No muon collection with name " << m_muonKey.key() << " found in StoreGate!");
82 return StatusCode::FAILURE;
83 }
84 unsigned int nMuons(importedMuons->size());
85 std::vector<const xAOD::Muon*> muToCheck; muToCheck.clear();
86 // Execute the text parser if requested
87 if (m_selectionString!="") {
88 std::vector<int> entries = m_parser->evaluateAsVector();
89 unsigned int nEntries = entries.size();
90 // check the sizes are compatible
91 if (nMuons != nEntries ) {
92 ATH_MSG_ERROR("Sizes incompatible! Are you sure your selection string used muons??");
93 return StatusCode::FAILURE;
94 } else {
95 // identify which muons to keep for the thinning check
96 for (unsigned int i=0; i<nMuons; ++i) if (entries[i]==1) muToCheck.push_back((*importedMuons)[i]);
97 }
98 }
99
101 if (m_selectionString=="") { // check all muons as user didn't provide a selection string
102 for (const auto *muIt : *importedMuons) {
103 if (muIt->inDetTrackParticleLink().isValid()) {
104 //This line prevents SiliconAssociatedForwardMuon from being used unless we're skimming InDetForwardTrackParticles
105 //since their track links point to this container while all others point to InDetTrackParticles
106 if (muIt->muonType()==xAOD::Muon::SiliconAssociatedForwardMuon && m_inDetSGKey.key() != "InDetForwardTrackParticles")
107 {
108 ATH_MSG_DEBUG("Skipping Forward Muon since we are not skimming InDetForwardParticles");
109 }
110 else{
111 ATH_MSG_DEBUG("Simming Muon tracks in " << m_inDetSGKey << " "<< muIt->muonType());
112 int index = muIt->inDetTrackParticleLink().index();
113 mask[index] = true;
114 }
115 }
116 if (m_coneSize>0.0) tInC.select(muIt,m_coneSize,importedTrackParticles.cptr(),mask); // check tracks in a cone around the muon if req'd
117 }
118 } else { // check only muons passing user selection string
119 for (auto & muIt : muToCheck) {
120 if (muIt->inDetTrackParticleLink().isValid()) {
121
122 if (muIt->muonType()==xAOD::Muon::SiliconAssociatedForwardMuon && m_inDetSGKey.key() != "InDetForwardTrackParticles")
123 {
124 ATH_MSG_DEBUG("Skipping Forward Muon since we are not skimming InDetForwardParticles");
125 }
126 else{
127 ATH_MSG_DEBUG("Simming Muon tracks in " << m_inDetSGKey << " "<< muIt->muonType());
128 int index = muIt->inDetTrackParticleLink().index();
129 mask[index] = true;
130 }
131 }
132 if (m_coneSize>0.0) tInC.select(muIt,m_coneSize,importedTrackParticles.cptr(),mask); // check tracks in a cone around the muon if req'd
133 }
134 }
135
136 // Count up the mask contents
137 unsigned int n_pass=0;
138 for (unsigned int i=0; i<nTracks; ++i) {
139 if (mask[i]) ++n_pass;
140 }
141 m_npass+=n_pass;
142 // Execute the thinning service based on the mask. Finish.
143 importedTrackParticles.keep (mask);
144
145 return StatusCode::SUCCESS;
146}
147
#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.
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)