ATLAS Offline Software
Loading...
Searching...
No Matches
JetTrackParticleThinning.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// JetTrackParticleThinning.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
16#include "GaudiKernel/ThreadLocalContext.h"
17#include <vector>
18#include <string>
19
20// Constructor
22 const std::string& n,
23 const IInterface* p ) :
24base_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_jetKey.key().empty()) {
39 ATH_MSG_FATAL("No jet collection provided for thinning.");
40 return StatusCode::FAILURE;
41 }
42 ATH_CHECK( m_jetKey.initialize() );
43 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");
44
45 // Set up the text-parsing machinery for selectiong the jet directly according to user cuts
46 if (!m_selectionString.empty() || !m_trackSelectionString.empty()) {
47 // order must match enum order EJetTrPThinningParser
48 ATH_CHECK( initializeParser( {m_selectionString, m_trackSelectionString } ));
49 }
50 return StatusCode::SUCCESS;
51}
52
54{
55 ATH_MSG_VERBOSE("finalize() ...");
56 ATH_MSG_INFO("Processed "<< m_ntot <<" tracks, "<< m_npass<< " were retained ");
57 return StatusCode::SUCCESS;
58}
59
60// The thinning itself
61StatusCode DerivationFramework::JetTrackParticleThinning::doThinning(const EventContext& ctx) const
62{
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 // ... jets
80 if (!importedJets.isValid()) {
81 ATH_MSG_ERROR("No jet collection with name " << m_jetKey.key() << " found in StoreGate!");
82 return StatusCode::FAILURE;
83 }
84 unsigned int nJets(importedJets->size());
85 std::vector<const xAOD::Jet*> jetToCheck; jetToCheck.clear();
86
87 // Execute the text parser if requested
88 if (!m_selectionString.empty()) {
89 std::vector<int> entries = m_parser[kJetSelection]->evaluateAsVector();
90 unsigned int nEntries = entries.size();
91 // check the sizes are compatible
92 if (nJets != nEntries ) {
93 ATH_MSG_ERROR("Sizes incompatible! Are you sure your selection string used jets??");
94 return StatusCode::FAILURE;
95 } else {
96 // identify which jets to keep for the thinning check
97 for (unsigned int i=0; i<nJets; ++i) if (entries[i]==1) jetToCheck.push_back((*importedJets)[i]);
98 }
99 }
100
101 // Set elements in the mask to true if there is a corresponding ElementLink from a reconstructed object
102 // ... jets
103 if (m_selectionString.empty()) { // check all jets as user didn't provide a selection string
104 for (const auto *jetIt : *importedJets) {
105 std::vector<const xAOD::TrackParticle*> jetTracks;
106 bool haveJetTracks = jetIt->getAssociatedObjects(xAOD::JetAttribute::GhostTrack, jetTracks);
107 if ( !haveJetTracks ) {ATH_MSG_WARNING("Associated tracks not found");}
108 else {
109 for (auto & jetTrack : jetTracks) {
110 int index = jetTrack->index();
111 mask[index] = true;
112 }
113 }
114 }
115 } else { // check only jets passing user selection string
116 for (auto & jetIt : jetToCheck) {
117 std::vector<const xAOD::TrackParticle*> jetTracks;
118 bool haveJetTracks = jetIt->getAssociatedObjects(xAOD::JetAttribute::GhostTrack, jetTracks);
119 if ( !haveJetTracks ) {ATH_MSG_WARNING("Associated tracks not found");}
120 else {
121 for (auto & jetTrack : jetTracks) {
122 int index = jetTrack->index();
123 mask[index] = true;
124 }
125 }
126 }
127 }
128
129 // Apply a track selection string.
130 if (!m_trackSelectionString.empty()) {
131 std::vector<int> entries = m_parser[kTrackThinning]->evaluateAsVector();
132 unsigned int nEntries = entries.size();
133 // check the sizes are compatible
134 if (nTracks != nEntries ) {
135 ATH_MSG_ERROR("Sizes incompatible! Are you sure your track selection string used tracks??");
136 return StatusCode::FAILURE;
137 } else {
138 // identify which jets to keep for the thinning check
139 for (unsigned int i=0; i<nEntries; ++i) {
140 if (!entries[i]) mask[i] = false;
141 }
142 }
143 }
144
145 // Count up the mask contents
146 unsigned int n_pass=0;
147 for (unsigned int i=0; i<nTracks; ++i) {
148 if (mask[i]) ++n_pass;
149 }
150 m_npass += n_pass;
151
152 // Execute the thinning service based on the mask. Finish.
153 importedTrackParticles.keep (mask);
154
155 return StatusCode::SUCCESS;
156}
157
#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_WARNING(x)
Handle for requesting thinning for a data object.
JetTrackParticleThinning(const std::string &t, const std::string &n, const IInterface *p)
SG::ReadHandleKey< xAOD::JetContainer > m_jetKey
virtual StatusCode doThinning(const EventContext &ctx) const override
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