ATLAS Offline Software
DiTauTrackParticleThinning.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 // DiTauTrackParticleThinning.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 
15 #include "GaudiKernel/ThreadLocalContext.h"
16 #include <vector>
17 #include <string>
18 
19 // Constructor
21  const std::string& n,
22  const IInterface* p ) :
23 base_class(t,n,p)
24 {}
25 
26 // Destructor
28 
29 // Athena initialize and finalize
31 {
32  // Decide which collections need to be checked for ID TrackParticles
33  ATH_MSG_VERBOSE("initialize() ...");
34  ATH_CHECK( m_ditauKey.initialize () );
35  ATH_CHECK( m_inDetSGKey.initialize (m_streamName) );
36  ATH_MSG_INFO("Using " << m_inDetSGKey.key() << "as the source collection for inner detector track particles");
37  ATH_MSG_INFO("Inner detector track particles associated with objects in " << m_ditauKey.key() << " will be retained in this format with the rest being thinned away");
38 
39  // Set up the text-parsing machinery for selectiong the ditau directly according to user cuts
40  if (!m_selectionString.empty()) {
41  ATH_CHECK(initializeParser(m_selectionString));
42  }
43  return StatusCode::SUCCESS;
44 }
45 
47 {
48  ATH_MSG_VERBOSE("finalize() ...");
49  ATH_MSG_INFO("Processed "<< m_ntot <<" tracks, "<< m_npass<< " were retained ");
50  ATH_CHECK( finalizeParser() );
51  return StatusCode::SUCCESS;
52 }
53 
54 // The thinning itself
56 {
57 
58  const EventContext& ctx = Gaudi::Hive::currentContext();
59 
60  // Retrieve main TrackParticle collection
62  (m_inDetSGKey, ctx);
63 
64  // Check the event contains tracks
65  unsigned int nTracks = importedTrackParticles->size();
66  if (nTracks==0) return StatusCode::SUCCESS;
67 
68  // Set up a mask with the same entries as the full TrackParticle collection
69  std::vector<bool> mask;
70  mask.assign(nTracks,false); // default: don't keep any tracks
71  m_ntot += nTracks;
72 
73  // Retrieve containers
74  // ... ditaus
75  SG::ReadHandle<xAOD::DiTauJetContainer> importedDiTaus(m_ditauKey,ctx);
76  if (!importedDiTaus.isValid()) {
77  ATH_MSG_ERROR("No ditau collection with name " << m_ditauKey.key() << " found in StoreGate!");
78  return StatusCode::FAILURE;
79  }
80  unsigned int nDiTaus(importedDiTaus->size());
81  std::vector<const xAOD::DiTauJet*> ditauToCheck; ditauToCheck.clear();
82 
83  // Execute the text parser if requested
84  if (!m_selectionString.empty()) {
85  std::vector<int> entries = m_parser->evaluateAsVector();
86  unsigned int nEntries = entries.size();
87  // check the sizes are compatible
88  if (nDiTaus != nEntries ) {
89  ATH_MSG_ERROR("Sizes incompatible! Are you sure your selection string used ditaus??");
90  return StatusCode::FAILURE;
91  } else {
92  // identify which ditaus to keep for the thinning check
93  for (unsigned int i=0; i<nDiTaus; ++i) if (entries[i]==1) ditauToCheck.push_back((*importedDiTaus)[i]);
94  }
95  }
96 
97  // Set elements in the mask to true if there is a corresponding ElementLink from a reconstructed object
98  // ... ditaus
99  if (m_selectionString=="") { // check all ditaus as user didn't provide a selection string
100  for (const auto *ditauIt : *importedDiTaus) {
101  for (unsigned int i=0; i<ditauIt->nTracks(); ++i) {
102  int index = ditauIt->trackLinks().at(i).index();
103  mask[index] = true;
104  }
105  for (unsigned int i=0; i<ditauIt->nIsoTracks(); ++i) {
106  int index = ditauIt->isoTrackLinks().at(i).index();
107  mask[index] = true;
108  }
109  }
110  } else { // check only ditaus passing user selection string
111  for (auto & ditauIt : ditauToCheck) {
112  for (unsigned int i=0; i<ditauIt->nTracks(); ++i) {
113  int index = ditauIt->trackLinks().at(i).index();
114  mask[index] = true;
115  }
116  for (unsigned int i=0; i<ditauIt->nIsoTracks(); ++i) {
117  int index = ditauIt->isoTrackLinks().at(i).index();
118  mask[index] = true;
119  }
120  }
121  }
122 
123  // Count up the mask contents
124  unsigned int n_pass=0;
125  for (unsigned int i=0; i<nTracks; ++i) {
126  if (mask[i]) ++n_pass;
127  }
128  m_npass += n_pass;
129 
130  // Execute the thinning service based on the mask. Finish.
131  importedTrackParticles.keep (mask);
132 
133  return StatusCode::SUCCESS;
134 }
135 
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
DiTauTrackParticleThinning.h
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
lumiFormat.i
int i
Definition: lumiFormat.py:85
beamspotman.n
n
Definition: beamspotman.py:731
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
DerivationFramework::DiTauTrackParticleThinning::~DiTauTrackParticleThinning
virtual ~DiTauTrackParticleThinning()
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
DeMoScan.index
string index
Definition: DeMoScan.py:364
DerivationFramework::DiTauTrackParticleThinning::finalize
virtual StatusCode finalize() override
Definition: DiTauTrackParticleThinning.cxx:46
DerivationFramework::DiTauTrackParticleThinning::DiTauTrackParticleThinning
DiTauTrackParticleThinning(const std::string &t, const std::string &n, const IInterface *p)
Definition: DiTauTrackParticleThinning.cxx:20
entries
double entries
Definition: listroot.cxx:49
DerivationFramework::DiTauTrackParticleThinning::initialize
virtual StatusCode initialize() override
Definition: DiTauTrackParticleThinning.cxx:30
DerivationFramework::DiTauTrackParticleThinning::doThinning
virtual StatusCode doThinning() const override
Definition: DiTauTrackParticleThinning.cxx:55
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