ATLAS Offline Software
Loading...
Searching...
No Matches
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
16#include <vector>
17#include <string>
18
19// Constructor
21 const std::string& n,
22 const IInterface* p ) :
23base_class(t,n,p)
24{
25}
26
27// Destructor
29
30// Athena initialize and finalize
32{
33 // Decide which collections need to be checked for ID TrackParticles
34 ATH_MSG_VERBOSE("initialize() ...");
35 ATH_CHECK( m_inDetSGKey.initialize (m_streamName) );
36 ATH_MSG_INFO("Using " << m_inDetSGKey << "as the source collection for inner detector track particles");
37 if (m_tauKey.key().empty()) {
38 ATH_MSG_FATAL("No tau collection provided for thinning.");
39 return StatusCode::FAILURE;
40 } 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");}
41 ATH_CHECK(m_tauKey.initialize());
43 if (m_tauTracksSGKey.key().empty()) {
44 ATH_MSG_FATAL("No tau tracks collection provided for thinning, despite this option being requested.");
45 return StatusCode::FAILURE;
46 } else {
47 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 }
50 }
51
52 // Set up the text-parsing machinery for selectiong the tau directly according to user cuts
53 if (!m_selectionString.empty()) {
54 ATH_CHECK(initializeParser(m_selectionString) );
55 }
56 return StatusCode::SUCCESS;
57}
58
60{
61 ATH_MSG_VERBOSE("finalize() ...");
62 ATH_MSG_INFO("Processed "<< m_ntot <<" tracks, "<< m_npass<< " were retained ");
63 ATH_CHECK( finalizeParser() );
64 return StatusCode::SUCCESS;
65}
66
67// The thinning itself
68StatusCode DerivationFramework::TauTrackParticleThinning::doThinning(const EventContext& ctx) const
69{
70
71 // Retrieve main TrackParticle collection
73 (m_inDetSGKey, ctx);
74
75 // Check the event contains tracks
76 unsigned int nTracks = importedTrackParticles->size();
77 if (nTracks==0) return StatusCode::SUCCESS;
78
79 // Set up a mask with the same entries as the full TrackParticle collection
80 std::vector<bool> mask;
81 mask.assign(nTracks,false); // default: don't keep any tracks
82 m_ntot += nTracks;
83
84 // Retrieve containers
85 // ... taus
87 if (!importedTaus.isValid()) {
88 ATH_MSG_ERROR("No tau collection with name " << m_tauKey.key() << " found in StoreGate!");
89 return StatusCode::FAILURE;
90 }
91 unsigned int nTaus(importedTaus->size());
92 std::vector<const xAOD::TauJet*> tauToCheck; tauToCheck.clear();
93
94 // Execute the text parser if requested
95 if (!m_selectionString.empty()) {
96 std::vector<int> entries = m_parser->evaluateAsVector();
97 unsigned int nEntries = entries.size();
98 // check the sizes are compatible
99 if (nTaus != nEntries ) {
100 ATH_MSG_ERROR("Sizes incompatible! Are you sure your selection string used taus??");
101 return StatusCode::FAILURE;
102 } else {
103 // identify which taus to keep for the thinning check
104 for (unsigned int i=0; i<nTaus; ++i) if (entries[i]==1) tauToCheck.push_back((*importedTaus)[i]);
105 }
106 } else {
107 // use all taus if no selection string is passed
108 for (unsigned int i=0; i<nTaus; ++i) tauToCheck.push_back((*importedTaus)[i]);
109 }
110
111 // Set elements in the mask to true if there is a corresponding ElementLink from a reconstructed object
112 // ... taus
114 if (m_selectionString=="") { // check all taus as user didn't provide a selection string
115 for (const auto *tauIt : *importedTaus) {
116 if (m_coneSize>0.0) trIC.select(tauIt,m_coneSize,importedTrackParticles.cptr(),mask); // check tracks in a cone around the tau if req'd
117 for (unsigned int i=0; i<tauIt->nTracks(); ++i) {
119 mask[index] = true;
120 }
121 }
122 } else { // check only taus passing user selection string
123 for (auto & tauIt : tauToCheck) {
124 if (m_coneSize>0.0) trIC.select(tauIt,m_coneSize,importedTrackParticles.cptr(),mask); // check tracks in a cone around the tau if req'd
125 for (unsigned int i=0; i<tauIt->nTracks(); ++i) {
127 mask[index] = true;
128 }
129 }
130 }
131
132 // Count up the mask contents
133 unsigned int n_pass=0;
134 for (unsigned int i=0; i<nTracks; ++i) {
135 if (mask[i]) ++n_pass;
136 }
137 m_npass += n_pass;
138
139 // Execute the thinning service based on the mask.
140 importedTrackParticles.keep (mask);
141
142 // Apply thinning to tau track collection if requested
145 (m_tauTracksSGKey, ctx);
146 if( importedTauTracks->empty() ) {
147 return StatusCode::SUCCESS;
148 }
149 std::vector< bool > mask_tautracks( importedTauTracks->size(), false );
150
151 for( const xAOD::TauJet* tau : tauToCheck ) {
152 // Get all the associated charged tau tracks:
153 auto ttLinks = tau->tauTrackLinks(xAOD::TauJetParameters::TauTrackFlag::classifiedCharged );
154 // Process the links:
155 for( const auto& ttLink : ttLinks ) {
156 if( ! ttLink.isValid() ) {
157 continue;
158 }
159 if( ttLink.dataID() != m_tauTracksSGKey.key() ) {
160 ATH_MSG_FATAL( "Charged tau track does not come from "
161 "container \"" << m_tauTracksSGKey << "\"" );
162 return StatusCode::FAILURE;
163 }
164 // If it is, set the mask for it:
165 mask_tautracks.at( ttLink.index() ) = true;
166 }
167 // Select the tau tracks in a cone if it was requested (NOT RECOMMENDED):
168 if( m_coneSize > 0.0 ) {
169 trIC.select( tau, m_coneSize, importedTauTracks.cptr(), mask_tautracks );
170 }
171 }
172
173 importedTauTracks.keep(mask_tautracks);
174
175 }
176
177 return StatusCode::SUCCESS;
178
179}
180
#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)
Handle for requesting thinning for a data object.
TauTrackParticleThinning(const std::string &t, const std::string &n, const IInterface *p)
SG::ThinningHandleKey< xAOD::TrackParticleContainer > m_inDetSGKey
virtual StatusCode doThinning(const EventContext &ctx) const override
SG::ReadHandleKey< xAOD::TauJetContainer > m_tauKey
SG::ThinningHandleKey< xAOD::TauTrackContainer > m_tauTracksSGKey
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
std::vector< ElementLink< xAOD::TrackParticleContainer > > trackParticleLinks(const xAOD::TauJet *tau, xAOD::TauJetParameters::TauTrackFlag flag=xAOD::TauJetParameters::TauTrackFlag::classifiedCharged)
TauJet_v3 TauJet
Definition of the current "tau version".
void select(const xAOD::IParticle *particle, float coneSize, const xAOD::TrackParticleContainer *tracks, std::vector< bool > &mask)