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
14// need to find this for the new version
16
17#include <vector>
18
19
20namespace DerivationFramework
21{
23 const std::string& n,
24 const IInterface* p ) :
25
26 base_class(t,n,p),
27 m_ntot(0),
28 m_npass(0)
29 {
30 //declareProperty("InDetTrackParticlesKey", m_TP_key="InDetTrackParticles");
31 declareProperty("PrimaryVertexKey", m_vertex_key="PrimaryVertices");
32 declareProperty("PrimaryVertexSelection", m_vertex_scheme="sumPt2");
33 declareProperty("TrackSelectionTool", m_trkSelTool, "Track selection tool" );
34 }
35
36 // Destructor
39
40 // Athena initialize and finalize
42 if(!m_trkSelTool)
43 {
44 ATH_MSG_ERROR("InDetTrackSelectionTool not set. Cannot initialize.");
45 return StatusCode::FAILURE;
46 }
47
48 // Validate m_vertex_scheme
49 if (m_vertex_scheme != "sumPt2" && m_vertex_scheme != "nTracks") {
50 ATH_MSG_ERROR("Invalid PrimaryVertexSelection: " << m_vertex_scheme);
51 return StatusCode::FAILURE;
52 }
53
54 ATH_CHECK( m_inDetSGKey.initialize(m_streamName) );
55 return StatusCode::SUCCESS;
56 }
57
59 {
60 ATH_CHECK(m_trkSelTool->finalize());
61 ATH_MSG_INFO("Processed " << m_ntot << " tracks, " << m_npass << " were retained.");
62 return StatusCode::SUCCESS;
63 }
64
65 // The thinning itself
66 StatusCode DerivationFramework::HITrackParticleThinningTool::doThinning(const EventContext& ctx) const
67 {
68 // Get the current event context
69
70 // Get the track container
72
73 //Define a primary vertex
74 const xAOD::Vertex *primary_vertex(nullptr);
75
76 // Retrieve vertex container
77 const xAOD::VertexContainer* vtxC(nullptr);
78 if (evtStore()->retrieve(vtxC, m_vertex_key).isFailure()) {
79 ATH_MSG_ERROR("Failed to retrieve VertexContainer with key: " << m_vertex_key);
80 return StatusCode::FAILURE;
81 }
82
83 // Check event contains tracks, if none then thinning complete!
84 unsigned int nTracks = tracks->size();
85 if (nTracks==0) return StatusCode::SUCCESS;
86
87 // Variables to track the best vertex based on the scheme
88 float ptmax = 0.;
89 size_t ntrkmax = 0;
90
91 // Iterate through the vertices to find the primary vertex
92 for (auto vertex : *vtxC) {
93 if (!vertex || vertex->vertexType() != xAOD::VxType::PriVtx) {
94 continue;
95 }
96
97 if (m_vertex_scheme.compare("sumPt2") == 0) {
98 // Define the accessor for the sumPt2 variable
99 SG::ConstAccessor<float> sumPt2Acc("sumPt2");
100
101 // Check if the sumPt2 variable is available for the vertex
102 if (sumPt2Acc.isAvailable(*vertex)) {
103 float sumPT = sumPt2Acc(*vertex); // Access the sumPt2 variable
104 if (sumPT > ptmax) {
105 ptmax = sumPT;
106 primary_vertex = vertex;
107 }
108 }
109 } else {
110 size_t ntp = vertex->nTrackParticles();
111 if (ntp > ntrkmax) {
112 ntrkmax = ntp;
113 primary_vertex = vertex;
114 }
115 }
116 }
117
118 // Warn if not primary vertex is found
119 if (!primary_vertex) {
120 ATH_MSG_DEBUG("No primary vertex found.");
121 }
122
123 // Loop over tracks, see if they pass, set mask
124 std::vector<bool> mask(nTracks,false); // reject all tracks by default
125 m_ntot += nTracks;
126
127 for (auto tp : *tracks) {
128 if (tp) {
129 const xAOD::Vertex* vert_trk = primary_vertex;
130
131 asg::AcceptData acceptData = m_trkSelTool->accept(*tp, vert_trk);
132 int index = tp->index();
133 mask[index] = static_cast<bool>(acceptData);
134 }
135 }
136
137 // Count up mask contents
138 unsigned int n_pass=0;
139 for (unsigned int i=0; i<nTracks; ++i) {
140 if (mask[i]) ++n_pass;
141 }
142 m_npass += n_pass;
143
144 tracks.keep (mask);
145
146 return StatusCode::SUCCESS;
147 }
148}
149
150
151
#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.
virtual StatusCode doThinning(const EventContext &ctx) const override
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.