ATLAS Offline Software
Loading...
Searching...
No Matches
HITrackParticleThinningTool.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
6// HITrackParticleThinningTool.cxx, (c) ATLAS Detector software
8
9
13#include "GaudiKernel/ThreadLocalContext.h"
14
15// need to find this for the new version
17
18#include <vector>
19
20
21namespace DerivationFramework
22{
24 const std::string& n,
25 const IInterface* p ) :
26
27 base_class(t,n,p),
28 m_ntot(0),
29 m_npass(0)
30 {
31 //declareProperty("InDetTrackParticlesKey", m_TP_key="InDetTrackParticles");
32 declareProperty("PrimaryVertexKey", m_vertex_key="PrimaryVertices");
33 declareProperty("PrimaryVertexSelection", m_vertex_scheme="sumPt2");
34 declareProperty("TrackSelectionTool", m_trkSelTool, "Track selection tool" );
35 }
36
37 // Destructor
40
41 // Athena initialize and finalize
43 if(!m_trkSelTool)
44 {
45 ATH_MSG_ERROR("InDetTrackSelectionTool not set. Cannot initialize.");
46 return StatusCode::FAILURE;
47 }
48
49 // Validate m_vertex_scheme
50 if (m_vertex_scheme != "sumPt2" && m_vertex_scheme != "nTracks") {
51 ATH_MSG_ERROR("Invalid PrimaryVertexSelection: " << m_vertex_scheme);
52 return StatusCode::FAILURE;
53 }
54
55 ATH_CHECK( m_inDetSGKey.initialize(m_streamName) );
56 return StatusCode::SUCCESS;
57 }
58
60 {
61 ATH_CHECK(m_trkSelTool->finalize());
62 ATH_MSG_INFO("Processed " << m_ntot << " tracks, " << m_npass << " were retained.");
63 return StatusCode::SUCCESS;
64 }
65
66 // The thinning itself
68 {
69 // Get the current event context
70 const EventContext& ctx = Gaudi::Hive::currentContext();
71
72 // Get the track container
74
75 //Define a primary vertex
76 const xAOD::Vertex *primary_vertex(nullptr);
77
78 // Retrieve vertex container
79 const xAOD::VertexContainer* vtxC(nullptr);
80 if (evtStore()->retrieve(vtxC, m_vertex_key).isFailure()) {
81 ATH_MSG_ERROR("Failed to retrieve VertexContainer with key: " << m_vertex_key);
82 return StatusCode::FAILURE;
83 }
84
85 // Check event contains tracks, if none then thinning complete!
86 unsigned int nTracks = tracks->size();
87 if (nTracks==0) return StatusCode::SUCCESS;
88
89 // Variables to track the best vertex based on the scheme
90 float ptmax = 0.;
91 size_t ntrkmax = 0;
92
93 // Iterate through the vertices to find the primary vertex
94 for (auto vertex : *vtxC) {
95 if (!vertex || vertex->vertexType() != xAOD::VxType::PriVtx) {
96 continue;
97 }
98
99 if (m_vertex_scheme.compare("sumPt2") == 0) {
100 // Define the accessor for the sumPt2 variable
101 SG::ConstAccessor<float> sumPt2Acc("sumPt2");
102
103 // Check if the sumPt2 variable is available for the vertex
104 if (sumPt2Acc.isAvailable(*vertex)) {
105 float sumPT = sumPt2Acc(*vertex); // Access the sumPt2 variable
106 if (sumPT > ptmax) {
107 ptmax = sumPT;
108 primary_vertex = vertex;
109 }
110 }
111 } else {
112 size_t ntp = vertex->nTrackParticles();
113 if (ntp > ntrkmax) {
114 ntrkmax = ntp;
115 primary_vertex = vertex;
116 }
117 }
118 }
119
120 // Warn if not primary vertex is found
121 if (!primary_vertex) {
122 ATH_MSG_DEBUG("No primary vertex found.");
123 }
124
125 // Loop over tracks, see if they pass, set mask
126 std::vector<bool> mask(nTracks,false); // reject all tracks by default
127 m_ntot += nTracks;
128
129 for (auto tp : *tracks) {
130 if (tp) {
131 const xAOD::Vertex* vert_trk = primary_vertex;
132
133 asg::AcceptData acceptData = m_trkSelTool->accept(*tp, vert_trk);
134 int index = tp->index();
135 mask[index] = static_cast<bool>(acceptData);
136 }
137 }
138
139 // Count up mask contents
140 unsigned int n_pass=0;
141 for (unsigned int i=0; i<nTracks; ++i) {
142 if (mask[i]) ++n_pass;
143 }
144 m_npass += n_pass;
145
146 tracks.keep (mask);
147
148 return StatusCode::SUCCESS;
149 }
150}
151
152
153
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_DEBUG(x)
Handle for requesting thinning for a data object.
SG::ThinningHandleKey< xAOD::TrackParticleContainer > m_inDetSGKey
ToolHandle< InDet::IInDetTrackSelectionTool > m_trkSelTool
track selection tool which can be optionally used for N_trk and sum pt cuts
HITrackParticleThinningTool(const std::string &t, const std::string &n, const IInterface *p)
Helper class to provide constant type-safe access to aux data.
bool isAvailable(const ELT &e) const
Test to see if this variable exists in the store.
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 ptmax
THE reconstruction tool.
Definition index.py:1
@ PriVtx
Primary vertex.
VertexContainer_v1 VertexContainer
Definition of the current "Vertex container version".
Vertex_v1 Vertex
Define the latest version of the vertex class.