ATLAS Offline Software
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 
21 namespace 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
39  }
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
72  SG::ThinningHandle<xAOD::TrackParticleContainer> tracks (m_inDetSGKey, ctx);
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 
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
DerivationFramework::HITrackParticleThinningTool::m_vertex_key
std::string m_vertex_key
Definition: HITrackParticleThinningTool.h:54
ptmax
double ptmax
Definition: dependence.cxx:60
DerivationFramework::HITrackParticleThinningTool::finalize
StatusCode finalize() override
Definition: HITrackParticleThinningTool.cxx:59
ThinningHandle.h
Handle for requesting thinning for a data object.
ParticleTest.tp
tp
Definition: ParticleTest.py:25
SG::ConstAccessor< float >
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
DerivationFramework::HITrackParticleThinningTool::m_trkSelTool
ToolHandle< InDet::IInDetTrackSelectionTool > m_trkSelTool
track selection tool which can be optionally used for N_trk and sum pt cuts
Definition: HITrackParticleThinningTool.h:56
python.utils.AtlRunQueryLookup.mask
string mask
Definition: AtlRunQueryLookup.py:460
SG::ThinningHandle
Handle for requesting thinning for a data object.
Definition: ThinningHandle.h:84
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
beamspotman.n
n
Definition: beamspotman.py:731
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
DerivationFramework::HITrackParticleThinningTool::doThinning
virtual StatusCode doThinning() const override
Definition: HITrackParticleThinningTool.cxx:66
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
xAOD::VxType::PriVtx
@ PriVtx
Primary vertex.
Definition: TrackingPrimitives.h:571
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
DerivationFramework
THE reconstruction tool.
Definition: ParticleSortingAlg.h:24
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
HITrackParticleThinningTool.h
DerivationFramework::HITrackParticleThinningTool::~HITrackParticleThinningTool
~HITrackParticleThinningTool()
Definition: HITrackParticleThinningTool.cxx:38
DerivationFramework::HITrackParticleThinningTool::HITrackParticleThinningTool
HITrackParticleThinningTool(const std::string &t, const std::string &n, const IInterface *p)
Definition: HITrackParticleThinningTool.cxx:23
Trk::vertex
@ vertex
Definition: MeasurementType.h:21
VertexContainer.h
xAOD::Vertex_v1
Class describing a Vertex.
Definition: Vertex_v1.h:42
SG::ConstAccessor::isAvailable
bool isAvailable(const ELT &e) const
Test to see if this variable exists in the store.
asg::AcceptData
Definition: AcceptData.h:30
DerivationFramework::HITrackParticleThinningTool::m_vertex_scheme
std::string m_vertex_scheme
Definition: HITrackParticleThinningTool.h:55
TrackParticleContainer.h
DerivationFramework::HITrackParticleThinningTool::initialize
StatusCode initialize() override
Definition: HITrackParticleThinningTool.cxx:42