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 "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_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());
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());
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
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
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
#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
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)