ATLAS Offline Software
TauTrackParticleThinning.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 // TauTrackParticleThinning.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 
13 #include "xAODTau/TauxAODHelpers.h"
16 #include "GaudiKernel/ThreadLocalContext.h"
17 #include <vector>
18 #include <string>
19 
20 // Constructor
22  const std::string& n,
23  const IInterface* p ) :
24 base_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 << "as the source collection for inner detector track particles");
38  if (m_tauKey.key().empty()) {
39  ATH_MSG_FATAL("No tau collection provided for thinning.");
40  return StatusCode::FAILURE;
41  } else { ATH_MSG_INFO("Inner detector track particles associated with objects in " << m_tauKey.key() << " will be retained in this format with the rest being thinned away");}
42  ATH_CHECK(m_tauKey.initialize());
43  if (m_doTauTracksThinning) {
44  if (m_tauTracksSGKey.key().empty()) {
45  ATH_MSG_FATAL("No tau tracks collection provided for thinning, despite this option being requested.");
46  return StatusCode::FAILURE;
47  } else {
48  ATH_MSG_INFO("Tau track thinning requested; tau tracks with the SG key " << m_tauTracksSGKey.key() << " will be thinned if not associated with objects in " << m_tauKey.key());
49  ATH_CHECK( m_tauTracksSGKey.initialize (m_streamName) );
50  }
51  }
52 
53  // Set up the text-parsing machinery for selectiong the tau directly according to user cuts
54  if (!m_selectionString.empty()) {
55  ATH_CHECK(initializeParser(m_selectionString) );
56  }
57  return StatusCode::SUCCESS;
58 }
59 
61 {
62  ATH_MSG_VERBOSE("finalize() ...");
63  ATH_MSG_INFO("Processed "<< m_ntot <<" tracks, "<< m_npass<< " were retained ");
64  ATH_CHECK( finalizeParser() );
65  return StatusCode::SUCCESS;
66 }
67 
68 // The thinning itself
70 {
71  const EventContext& ctx = Gaudi::Hive::currentContext();
72 
73  // Retrieve main TrackParticle collection
75  (m_inDetSGKey, ctx);
76 
77  // Check the event contains tracks
78  unsigned int nTracks = importedTrackParticles->size();
79  if (nTracks==0) return StatusCode::SUCCESS;
80 
81  // Set up a mask with the same entries as the full TrackParticle collection
82  std::vector<bool> mask;
83  mask.assign(nTracks,false); // default: don't keep any tracks
84  m_ntot += nTracks;
85 
86  // Retrieve containers
87  // ... taus
88  SG::ReadHandle<xAOD::TauJetContainer> importedTaus(m_tauKey,ctx);
89  if (!importedTaus.isValid()) {
90  ATH_MSG_ERROR("No tau collection with name " << m_tauKey.key() << " found in StoreGate!");
91  return StatusCode::FAILURE;
92  }
93  unsigned int nTaus(importedTaus->size());
94  std::vector<const xAOD::TauJet*> tauToCheck; tauToCheck.clear();
95 
96  // Execute the text parser if requested
97  if (!m_selectionString.empty()) {
98  std::vector<int> entries = m_parser->evaluateAsVector();
99  unsigned int nEntries = entries.size();
100  // check the sizes are compatible
101  if (nTaus != nEntries ) {
102  ATH_MSG_ERROR("Sizes incompatible! Are you sure your selection string used taus??");
103  return StatusCode::FAILURE;
104  } else {
105  // identify which taus to keep for the thinning check
106  for (unsigned int i=0; i<nTaus; ++i) if (entries[i]==1) tauToCheck.push_back((*importedTaus)[i]);
107  }
108  } else {
109  // use all taus if no selection string is passed
110  for (unsigned int i=0; i<nTaus; ++i) tauToCheck.push_back((*importedTaus)[i]);
111  }
112 
113  // Set elements in the mask to true if there is a corresponding ElementLink from a reconstructed object
114  // ... taus
116  if (m_selectionString=="") { // check all taus as user didn't provide a selection string
117  for (const auto *tauIt : *importedTaus) {
118  if (m_coneSize>0.0) trIC.select(tauIt,m_coneSize,importedTrackParticles.cptr(),mask); // check tracks in a cone around the tau if req'd
119  for (unsigned int i=0; i<tauIt->nTracks(); ++i) {
121  mask[index] = true;
122  }
123  }
124  } else { // check only taus passing user selection string
125  for (auto & tauIt : tauToCheck) {
126  if (m_coneSize>0.0) trIC.select(tauIt,m_coneSize,importedTrackParticles.cptr(),mask); // check tracks in a cone around the tau if req'd
127  for (unsigned int i=0; i<tauIt->nTracks(); ++i) {
129  mask[index] = true;
130  }
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 
141  // Execute the thinning service based on the mask.
142  importedTrackParticles.keep (mask);
143 
144  // Apply thinning to tau track collection if requested
145  if( m_doTauTracksThinning ) {
147  (m_tauTracksSGKey, ctx);
148  if( importedTauTracks->empty() ) {
149  return StatusCode::SUCCESS;
150  }
151  std::vector< bool > mask_tautracks( importedTauTracks->size(), false );
152 
153  for( const xAOD::TauJet* tau : tauToCheck ) {
154  // Get all the associated charged tau tracks:
155  auto ttLinks = tau->tauTrackLinks(xAOD::TauJetParameters::TauTrackFlag::classifiedCharged );
156  // Process the links:
157  for( const auto& ttLink : ttLinks ) {
158  if( ! ttLink.isValid() ) {
159  continue;
160  }
161  if( ttLink.dataID() != m_tauTracksSGKey.key() ) {
162  ATH_MSG_FATAL( "Charged tau track does not come from "
163  "container \"" << m_tauTracksSGKey << "\"" );
164  return StatusCode::FAILURE;
165  }
166  // If it is, set the mask for it:
167  mask_tautracks.at( ttLink.index() ) = true;
168  }
169  // Select the tau tracks in a cone if it was requested (NOT RECOMMENDED):
170  if( m_coneSize > 0.0 ) {
171  trIC.select( tau, m_coneSize, importedTauTracks.cptr(), mask_tautracks );
172  }
173  }
174 
175  importedTauTracks.keep(mask_tautracks);
176 
177  }
178 
179  return StatusCode::SUCCESS;
180 
181 }
182 
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
DerivationFramework::TauTrackParticleThinning::~TauTrackParticleThinning
virtual ~TauTrackParticleThinning()
ThinningHandle.h
Handle for requesting thinning for a data object.
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
index
Definition: index.py:1
xAOD::TauJetParameters::classifiedCharged
@ classifiedCharged
Definition: TauDefs.h:406
DerivationFramework::TauTrackParticleThinning::doThinning
virtual StatusCode doThinning() const override
Definition: TauTrackParticleThinning.cxx:69
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
DerivationFramework::TauTrackParticleThinning::initialize
virtual StatusCode initialize() override
Definition: TauTrackParticleThinning.cxx:32
python.utils.AtlRunQueryLookup.mask
string mask
Definition: AtlRunQueryLookup.py:460
SG::ThinningHandle
Handle for requesting thinning for a data object.
Definition: ThinningHandle.h:84
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
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
lumiFormat.i
int i
Definition: lumiFormat.py:92
beamspotman.n
n
Definition: beamspotman.py:731
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
TauxAODHelpers.h
xAOD::TauJet_v3
Class describing a tau jet.
Definition: TauJet_v3.h:41
xAOD::TauHelpers::trackParticleLinks
std::vector< ElementLink< xAOD::TrackParticleContainer > > trackParticleLinks(const xAOD::TauJet *tau, xAOD::TauJetParameters::TauTrackFlag flag=xAOD::TauJetParameters::TauTrackFlag::classifiedCharged)
Definition: TauxAODHelpers.cxx:22
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
DerivationFramework::TauTrackParticleThinning::finalize
virtual StatusCode finalize() override
Definition: TauTrackParticleThinning.cxx:60
DeMoScan.index
string index
Definition: DeMoScan.py:362
TauTrackParticleThinning.h
DerivationFramework::TauTrackParticleThinning::TauTrackParticleThinning
TauTrackParticleThinning(const std::string &t, const std::string &n, const IInterface *p)
Definition: TauTrackParticleThinning.cxx:21
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.
TrackParticleContainer.h