ATLAS Offline Software
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 ) :
25 base_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
79  SG::ReadHandle<xAOD::MuonContainer> importedMuons( m_muonKey, ctx);
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 
SGxAODProxyLoader.h
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
ThinningHandle.h
Handle for requesting thinning for a data object.
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
index
Definition: index.py:1
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
python.utils.AtlRunQueryLookup.mask
string mask
Definition: AtlRunQueryLookup.py:460
SG::ThinningHandle
Handle for requesting thinning for a data object.
Definition: ThinningHandle.h:84
DerivationFramework::MuonTrackParticleThinning::doThinning
virtual StatusCode doThinning() const override
Definition: MuonTrackParticleThinning.cxx:60
DerivationFramework::MuonTrackParticleThinning::MuonTrackParticleThinning
MuonTrackParticleThinning(const std::string &t, const std::string &n, const IInterface *p)
Definition: MuonTrackParticleThinning.cxx:22
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
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
DerivationFramework::MuonTrackParticleThinning::~MuonTrackParticleThinning
virtual ~MuonTrackParticleThinning()
SGNTUPProxyLoader.h
lumiFormat.i
int i
Definition: lumiFormat.py:85
beamspotman.n
n
Definition: beamspotman.py:731
DerivationFramework::MuonTrackParticleThinning::finalize
virtual StatusCode finalize() override
Definition: MuonTrackParticleThinning.cxx:51
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
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
MuonTrackParticleThinning.h
MultipleProxyLoader.h
DeMoScan.index
string index
Definition: DeMoScan.py:364
entries
double entries
Definition: listroot.cxx:49
DerivationFramework::TracksInCone
Definition: TracksInCone.h:19
DerivationFramework::TracksInCone::select
void select(const xAOD::IParticle *particle, float coneSize, const xAOD::TrackParticleContainer *tracks, std::vector< bool > &mask)
Definition: TracksInCone.h:21
dqBeamSpot.nEntries
int nEntries
Definition: dqBeamSpot.py:73
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
DerivationFramework::MuonTrackParticleThinning::initialize
virtual StatusCode initialize() override
Definition: MuonTrackParticleThinning.cxx:33