ATLAS Offline Software
Loading...
Searching...
No Matches
HITrackParticleThinningTool.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2018 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 return StatusCode::SUCCESS;
63 }
64
65 // The thinning itself
67 {
68 // Get the current event context
69 const EventContext& ctx = Gaudi::Hive::currentContext();
70
71 // Get the track container
73
74 m_ntot+=tracks->size();
75
76 //Define a primary vertex
77 const xAOD::Vertex *primary_vertex(nullptr);
78
79 // Retrieve vertex container
80 const xAOD::VertexContainer* vtxC(nullptr);
81 if (evtStore()->retrieve(vtxC, m_vertex_key).isFailure()) {
82 ATH_MSG_ERROR("Failed to retrieve VertexContainer with key: " << m_vertex_key);
83 return StatusCode::FAILURE;
84 }
85
86 // Variables to track the best vertex based on the scheme
87 float ptmax = 0.;
88 size_t ntrkmax = 0;
89
90 // Iterate through the vertices to find the primary vertex
91 for (auto vertex : *vtxC) {
92 if (!vertex || vertex->vertexType() != xAOD::VxType::PriVtx) {
93 continue;
94 }
95
96 if (m_vertex_scheme.compare("sumPt2") == 0) {
97 // Define the accessor for the sumPt2 variable
98 SG::ConstAccessor<float> sumPt2Acc("sumPt2");
99
100 // Check if the sumPt2 variable is available for the vertex
101 if (sumPt2Acc.isAvailable(*vertex)) {
102 float sumPT = sumPt2Acc(*vertex); // Access the sumPt2 variable
103 if (sumPT > ptmax) {
104 ptmax = sumPT;
105 primary_vertex = vertex;
106 }
107 }
108 } else {
109 size_t ntp = vertex->nTrackParticles();
110 if (ntp > ntrkmax) {
111 ntrkmax = ntp;
112 primary_vertex = vertex;
113 }
114 }
115 }
116
117 // Warn if not primary vertex is found
118 if (!primary_vertex) {
119 ATH_MSG_DEBUG("No primary vertex found.");
120 }
121
122 // Loop over tracks, see if they pass, set mask
123 std::vector<bool> mask;
124 mask.reserve(tracks->size());
125
126 for (auto tp : *tracks) {
127 if (tp) {
128 const xAOD::Vertex* vert_trk = primary_vertex;
129
130 asg::AcceptData acceptData = m_trkSelTool->accept(*tp, vert_trk);
131 mask.push_back(static_cast<bool>(acceptData));
132 } else {
133 mask.push_back(false);
134 }
135 }
136
137 tracks.keep (mask);
138
139 return StatusCode::SUCCESS;
140 }
141}
142
143
144
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(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.
@ PriVtx
Primary vertex.
VertexContainer_v1 VertexContainer
Definition of the current "Vertex container version".
Vertex_v1 Vertex
Define the latest version of the vertex class.