ATLAS Offline Software
JetLargeD0TrackParticleThinning.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 // JetLargeD0TrackParticleThinning.cxx, (c) ATLAS Detector software
8 // Author: Jackson Burzynski (jackson.carl.burzynski@cern.ch)
9 // This tool associates InDetLargeD0TrackParticles to jets using a
10 // user-defined DeltaR matching.
11 // n.b. The standard JetTrackParticleThinning tool will not work
12 // for LargeD0 tracks due to the fact that they are not considered
13 // when running the ghost association.
14 //
15 // TODO: implement jet-origin correction under the assumption that
16 // the jet originates from a secondary vertex instead of the PV
17 
19 #include "xAODJet/JetContainer.h"
22 #include "GaudiKernel/ThreadLocalContext.h"
23 #include <vector>
24 #include <string>
25 
26 // Constructor
28  const std::string& n,
29  const IInterface* p ) :
30 base_class(t,n,p)
31 {
32 }
33 
34 // Destructor
36 }
37 
38 // Athena initialize and finalize
40 {
41  // Decide which collections need to be checked for ID TrackParticles
42  ATH_MSG_VERBOSE("initialize() ...");
43  ATH_CHECK( m_inDetSGKey.initialize (m_streamName) );
44  ATH_MSG_INFO("Using " << m_inDetSGKey << "as the source collection for inner detector track particles");
45  if (m_jetKey.key().empty()) {
46  ATH_MSG_FATAL("No jet collection provided for thinning.");
47  return StatusCode::FAILURE;
48  }
49  ATH_CHECK( m_jetKey.initialize() );
50  ATH_MSG_INFO("Inner detector track particles associated with objects in " << m_jetKey.key() << " will be retained in this format with the rest being thinned away");
51 
52  // Set up the text-parsing machinery for selectiong the jet directly according to user cuts
53  if (!m_selectionString.empty() || !m_trackSelectionString.empty()) {
54  // order must match enum order EJetTrPThinningParser
55  ATH_CHECK( initializeParser( {m_selectionString, m_trackSelectionString } ));
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  return StatusCode::SUCCESS;
65 }
66 
67 // The thinning itself
69 {
70  const EventContext& ctx = Gaudi::Hive::currentContext();
71 
72  // Retrieve main TrackParticle collection
74  (m_inDetSGKey, ctx);
75  SG::ReadHandle<xAOD::TrackParticleContainer> tracks(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  // ... jets
88  SG::ReadHandle<xAOD::JetContainer> importedJets(m_jetKey,ctx);
89  if (!importedJets.isValid()) {
90  ATH_MSG_ERROR("No jet collection with name " << m_jetKey.key() << " found in StoreGate!");
91  return StatusCode::FAILURE;
92  }
93  unsigned int nJets(importedJets->size());
94  std::vector<const xAOD::Jet*> jetToCheck; jetToCheck.clear();
95 
96  // Execute the text parser if requested
97  if (!m_selectionString.empty()) {
98  std::vector<int> entries = m_parser[kJetSelection]->evaluateAsVector();
99  unsigned int nEntries = entries.size();
100  // check the sizes are compatible
101  if (nJets != nEntries ) {
102  ATH_MSG_ERROR("Sizes incompatible! Are you sure your selection string used jets??");
103  return StatusCode::FAILURE;
104  } else {
105  // identify which jets to keep for the thinning check
106  for (unsigned int i=0; i<nJets; ++i) if (entries[i]==1) jetToCheck.push_back((*importedJets)[i]);
107  }
108  }
109 
110  // Set elements in the mask to true if they are matched to a reconstructed object
111  // ... jets
112  static const SG::AuxElement::ConstAccessor<std::vector<ElementLink<DataVector<xAOD::IParticle> > > > ghostTrackLRT ("GhostTrackLRT");
113 
114  if (m_selectionString.empty()) { // check all jets as user didn't provide a selection string
115  for (xAOD::JetContainer::const_iterator jetIt=importedJets->begin(); jetIt!=importedJets->end(); ++jetIt) {
116  const std::vector< ElementLink<DataVector<xAOD::IParticle> > > &jetTrackLinks = ghostTrackLRT( **jetIt );
117  for (const auto &jetTrkIt : jetTrackLinks) {
118  const xAOD::TrackParticle* trackPart = dynamic_cast<const xAOD::TrackParticle*>(*jetTrkIt);
119  int index = trackPart->index();
120  mask[index] = true;
121  }
122  }
123  } else { // check only jets passing user selection string
124  for (std::vector<const xAOD::Jet*>::const_iterator jetIt=jetToCheck.begin(); jetIt!=jetToCheck.end(); ++jetIt) {
125  const std::vector< ElementLink<DataVector<xAOD::IParticle> > > &jetTrackLinks = ghostTrackLRT( **jetIt );
126  for (const auto &jetTrkIt : jetTrackLinks) {
127  const xAOD::TrackParticle* trackPart = dynamic_cast<const xAOD::TrackParticle*>(*jetTrkIt);
128  int index = trackPart->index();
129  mask[index] = true;
130  }
131  }
132  }
133 
134  // Apply a track selection string.
135  if (!m_trackSelectionString.empty()) {
136  std::vector<int> entries = m_parser[kTrackThinning]->evaluateAsVector();
137  unsigned int nEntries = entries.size();
138  // check the sizes are compatible
139  if (nTracks != nEntries ) {
140  ATH_MSG_ERROR("Sizes incompatible! Are you sure your track selection string used tracks??");
141  return StatusCode::FAILURE;
142  } else {
143  // identify which jets to keep for the thinning check
144  for (unsigned int i=0; i<nEntries; ++i) {
145  if (!entries[i]) mask[i] = false;
146  }
147  }
148  }
149 
150  // Count up the mask contents
151  unsigned int n_pass=0;
152  for (unsigned int i=0; i<nTracks; ++i) {
153  if (mask[i]) ++n_pass;
154  }
155  m_npass += n_pass;
156 
157  // Execute the thinning service based on the mask. Finish.
158  importedTrackParticles.keep (mask);
159 
160  return StatusCode::SUCCESS;
161 }
162 
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
DataModel_detail::const_iterator
Const iterator class for DataVector/DataList.
Definition: DVLIterator.h:82
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
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
DerivationFramework::JetLargeD0TrackParticleThinning::~JetLargeD0TrackParticleThinning
virtual ~JetLargeD0TrackParticleThinning()
Definition: JetLargeD0TrackParticleThinning.cxx:35
DerivationFramework::JetLargeD0TrackParticleThinning::initialize
virtual StatusCode initialize() override
Definition: JetLargeD0TrackParticleThinning.cxx:39
SG::ConstAccessor
Helper class to provide constant type-safe access to aux data.
Definition: ConstAccessor.h:54
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::JetLargeD0TrackParticleThinning::finalize
virtual StatusCode finalize() override
Definition: JetLargeD0TrackParticleThinning.cxx:60
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
DerivationFramework::JetLargeD0TrackParticleThinning::doThinning
virtual StatusCode doThinning() const override
Definition: JetLargeD0TrackParticleThinning.cxx:68
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
SG::AuxElement::index
size_t index() const
Return the index of this element within its container.
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
DerivationFramework::kTrackThinning
@ kTrackThinning
Definition: JetTrackParticleThinning.h:27
DerivationFramework::kJetSelection
@ kJetSelection
Definition: JetTrackParticleThinning.h:27
DerivationFramework::JetLargeD0TrackParticleThinning::JetLargeD0TrackParticleThinning
JetLargeD0TrackParticleThinning(const std::string &t, const std::string &n, const IInterface *p)
Definition: JetLargeD0TrackParticleThinning.cxx:27
DataVector::end
const_iterator end() const noexcept
Return a const_iterator pointing past the end of the collection.
DeMoScan.index
string index
Definition: DeMoScan.py:362
JetContainer.h
entries
double entries
Definition: listroot.cxx:49
JetLargeD0TrackParticleThinning.h
xAOD::TrackParticle_v1
Class describing a TrackParticle.
Definition: TrackParticle_v1.h:43
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
DataVector::begin
const_iterator begin() const noexcept
Return a const_iterator pointing at the beginning of the collection.