ATLAS Offline Software
Loading...
Searching...
No Matches
TauVertexFinder.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3*/
4
5#ifndef XAOD_ANALYSIS
6#include "TauVertexFinder.h"
7
8TauVertexFinder::TauVertexFinder(const std::string& name ) :
9 TauRecToolBase(name) {}
10
12
13// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
17
18 if( m_useTJVA) {
19 ATH_MSG_INFO("using TJVA to determine tau vertex");
21 ATH_CHECK( m_trkVertexAssocTool.retrieve() );
22 }
23
24 return StatusCode::SUCCESS;
25}
26
27// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
29 const xAOD::VertexContainer* vertexContainer) const
30{
31 const xAOD::VertexContainer * vxContainer = nullptr;
32
33 if (!m_vertexInputContainer.empty()) {
35 if (!vertexInHandle.isValid()) {
36 ATH_MSG_ERROR ("Could not retrieve HiveDataObj with key " << vertexInHandle.key());
37 return StatusCode::FAILURE;
38 }
39 vxContainer = vertexInHandle.cptr();
40 }
41 else {
42 if (vertexContainer != nullptr) {
43 vxContainer = vertexContainer;
44 }
45 else {
46 ATH_MSG_WARNING ("No Vertex Container in trigger");
47 return StatusCode::FAILURE;
48 }
49 }
50
51 ATH_MSG_VERBOSE("size of VxPrimaryContainer is: " << vxContainer->size() );
52 if (vxContainer->empty()) return StatusCode::SUCCESS;
53
54 // find default PrimaryVertex (needed if TJVA is switched off or fails)
55 const xAOD::Vertex* primaryVertex = nullptr;
56 if (inTrigger()) { // trigger: find default PrimaryVertex (highest sum pt^2)
57 primaryVertex = (*vxContainer)[0];
58 }
59 else { // offline: the first and only primary vertex candidate is picked
60 for (const auto vertex : *vxContainer) {
61 if (vertex->vertexType() == xAOD::VxType::PriVtx) {
62 primaryVertex = vertex;
63 break;
64 }
65 }
66 }
67
68 // associate vertex to tau
69 if (primaryVertex) pTau.setVertex(vxContainer, primaryVertex);
70
71 //stop here if TJVA is disabled
72 if (!m_useTJVA) return StatusCode::SUCCESS;
73
74 // additional protection in case TJVA is run online - not supported for Run3
75 if ( m_useTJVA && inTrigger()){
76 ATH_MSG_ERROR("TJVA not available for online");
77 return StatusCode::FAILURE;
78 }
79
80 // try to find new PV with TJVA
81 ATH_MSG_DEBUG("TJVA enabled -> try to find new PV for the tau candidate");
82
83 float maxJVF = -100.;
84 ElementLink<xAOD::VertexContainer> newPrimaryVertexLink = getPV_TJVA(pTau, *vxContainer, maxJVF );
85 if (newPrimaryVertexLink.isValid()) {
86 // set new primary vertex
87 // will overwrite default one which was set above
88 pTau.setVertexLink(newPrimaryVertexLink);
89 // save highest JVF value
90 pTau.setDetail(xAOD::TauJetParameters::TauJetVtxFraction,static_cast<float>(maxJVF));
91 ATH_MSG_DEBUG("TJVA vertex found and set");
92 }
93 else {
94 ATH_MSG_WARNING("couldn't find new PV for TJVA");
95 }
96
97 return StatusCode::SUCCESS;
98}
99
100// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
103 const xAOD::VertexContainer& vertices,
104 float& maxJVF) const
105{
106 const xAOD::Jet* pJetSeed = pTau.jet();
107 std::vector<const xAOD::TrackParticle*> tracksForTJVA;
108
109 // the implementation follows closely the example given in modifyJet(...) in https://svnweb.cern.ch/trac/atlasoff/browser/Reconstruction/Jet/JetMomentTools/trunk/Root/JetVertexFractionTool.cxx#15
110
111 std::vector<const xAOD::TrackParticle*> assocTracks;
112 if (! pJetSeed->getAssociatedObjects(m_assocTracksName, assocTracks)) {
113 ATH_MSG_ERROR("Could not retrieve the AssociatedObjects named \""<< m_assocTracksName <<"\" from jet");
115 }
116
117 // Store tracks that meet TJVA track selection criteria and are between deltaR of 0.2 with the jet seed
118 // To be included in the TJVA calculation
119 // Maybe not as efficient as deleting unwanted tracks from assocTrack but quicker and safer for now.
120 float sumTrackAll = 0.0;
121 for ( auto xTrack : assocTracks ) {
122 if ( (xTrack->p4().DeltaR(pJetSeed->p4())<m_dDeltaRMax) && m_TrackSelectionToolForTJVA->accept(*xTrack) ) {
123 if (!inEleRM()) {
124 tracksForTJVA.push_back(xTrack);
125 }
126 else {
127 static const SG::ConstAccessor<ElementLink<xAOD::TrackParticleContainer>> acc_originalTrack("ERMOriginalTrack");
128 if (!acc_originalTrack.isAvailable(*xTrack)) {
129 ATH_MSG_WARNING("Original ERM track link is not available, skipping track");
130 continue;
131 }
132 auto original_id_track_link = acc_originalTrack(*xTrack);
133 if (!original_id_track_link.isValid()) {
134 ATH_MSG_WARNING("Original ERM track link is not valid, skipping track");
135 continue;
136 }
137 tracksForTJVA.push_back(*original_id_track_link);
138 }
139 sumTrackAll += xTrack->pt();
140 }
141 }
142
143 if (this->msgLevel() <= MSG::DEBUG){
144 ATH_MSG_DEBUG("tracksForTJVA # = " << tracksForTJVA.size() << ", sum pT = " << sumTrackAll);
145
146 for (uint i = 0; i < tracksForTJVA.size(); i++){
147 ATH_MSG_DEBUG("tracksForTJVA[" << i << "].pt = " << tracksForTJVA[i]->pt());
148 }
149 }
150
152
153 // Get track vertex map
154 std::vector<const xAOD::Vertex*> vertVec;
155 for (const xAOD::Vertex* vert : vertices) {
156 vertVec.push_back(vert);
157 }
158
159 if (this->msgLevel() <= MSG::DEBUG){
160 ATH_MSG_DEBUG("Vertex # = " << vertVec.size());
161 for (uint i = 0; i < vertVec.size(); i++){
162 ATH_MSG_DEBUG("vertVec[" << i << "].z = " << vertVec[i]->z());
163 }
164 }
165
166 // Tool returns map between vertex and tracks associated to that vertex (based on selection criteria set in config)
167 trktovxmap = m_trkVertexAssocTool->getMatchMap(tracksForTJVA, vertVec);
168
169 if (this->msgLevel() <= MSG::DEBUG){
170 for (const auto& [vtx, trks] : trktovxmap){
171 std::stringstream ss;
172 for (auto ass_trk : trks){
173 for (uint i=0; i < tracksForTJVA.size(); i++){
174 if (ass_trk->p4() == tracksForTJVA[i]->p4()) {
175 ss << i << ", ";
176 break;
177 }
178 }
179 }
180 ATH_MSG_DEBUG("Vtx[" << vtx->index() << "] associated with trks [" << ss.str() << "]");
181 }
182 }
183
184 // Get the highest JVF vertex and store maxJVF for later use
185 // Note: the official JetMomentTools/JetVertexFractionTool doesn't provide any possibility to access the JVF value, but just the vertex.
186 maxJVF=-100.;
187 // Store sum(deltaz(track-vertex)) and jet vertex fraction scores
188 std::vector<float> sumDz;
189 std::vector<float> v_jvf;
190 size_t maxIndex = 0;
191 for (const xAOD::Vertex* vert : vertices) {
192 float jvf = 0.0;
193 std::vector<const xAOD::TrackParticle*> tracks = trktovxmap[vert];
194 // get jet vertex fraction and sumdeltaZ scores
195 std::pair<float, float> spair = getVertexScores(tracks, vert->z());
196 jvf = (sumTrackAll!=0. ? spair.first/sumTrackAll : 0.);
197 v_jvf.push_back(jvf);
198 sumDz.push_back(spair.second);
199
200 if (jvf > maxJVF) {
201 maxJVF = jvf;
202 maxIndex = vert->index();
203 }
204 }
205
206 ATH_MSG_DEBUG("First TJVA");
207 ATH_MSG_DEBUG("TJVA vtx found at z: " << vertices.at(maxIndex)->z() << " i_vtx = " << maxIndex << "jvf = " << maxJVF);
208 ATH_MSG_DEBUG("highest pt vtx found at z (i=0): " << vertices.at(0)->z());
209
210 float min_sumDz = 99999999.;
211 for (const xAOD::Vertex* vert : vertices) {
212 ATH_MSG_DEBUG("i_vtx=" << vert->index() << ", z=" << vert->z() << ", JVF=" << v_jvf[vert->index()] << ", sumDz=" << sumDz[vert->index()]);
213 if ( v_jvf[vert->index()] == maxJVF ){
214 // in case of 0 tracks, first vertex will have sumDz=0, and be selected
215 if (sumDz[vert->index()] < min_sumDz){
216 min_sumDz = sumDz[vert->index()];
217 maxIndex = vert->index();
218 }
219 }
220 }
221
222
223 ATH_MSG_DEBUG("Final TJVA");
224 ATH_MSG_DEBUG("TJVA vtx found at z: " << vertices.at(maxIndex)->z() << " i_vtx = " << maxIndex);
225
226 return ElementLink<xAOD::VertexContainer> (vertices, maxIndex);
227}
228
229// get sum of pT from tracks associated to vertex (tracks from track to vertex map) (pair first)
230// sum over tracks associated to vertex of deltaZ(track-vertex) (pair second)
231std::pair<float,float> TauVertexFinder::getVertexScores(const std::vector<const xAOD::TrackParticle*>& tracks, float vx_z) const{
232
233 float sumTrackPV = 0.;
234 float sumDeltaZ = 0.;
235 for (auto trk : tracks){
236 sumTrackPV += trk->pt();
237 sumDeltaZ += std::abs(trk->z0() - vx_z + trk->vz());
238 }
239
240 return std::make_pair(sumTrackPV, sumDeltaZ);
241}
242#endif
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
unsigned int uint
static Double_t ss
#define z
const T * at(size_type n) const
Access an element, as an rvalue.
size_type size() const noexcept
Returns the number of elements in the collection.
bool empty() const noexcept
Returns true if the collection is empty.
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.
virtual bool isValid() override final
Can the handle be successfully dereferenced?
const_pointer_type cptr()
Dereference the pointer.
virtual const std::string & key() const override final
Return the StoreGate ID for the referenced object.
TauRecToolBase(const std::string &name)
bool inEleRM() const
bool inTrigger() const
ElementLink< xAOD::VertexContainer > getPV_TJVA(const xAOD::TauJet &tauJet, const xAOD::VertexContainer &vertices, float &maxJVF) const
TauVertexFinder(const std::string &name)
Constructor and Destructor.
StatusCode initialize() override
Algorithm functions.
std::pair< float, float > getVertexScores(const std::vector< const xAOD::TrackParticle * > &tracks, float vx_z) const
ToolHandle< CP::ITrackVertexAssociationTool > m_trkVertexAssocTool
Gaudi::Property< std::string > m_assocTracksName
Gaudi::Property< double > m_dDeltaRMax
Gaudi::Property< bool > m_useTJVA
ToolHandle< InDet::IInDetTrackSelectionTool > m_TrackSelectionToolForTJVA
SG::ReadHandleKey< xAOD::VertexContainer > m_vertexInputContainer
StatusCode executeVertexFinder(xAOD::TauJet &pTau, const xAOD::VertexContainer *vertexContainer=nullptr) const override
SG::ReadHandleKey< xAOD::TrackParticleContainer > m_trackPartInputContainer
virtual FourMom_t p4() const
The full 4-momentum of the particle.
Definition Jet_v1.cxx:71
std::vector< const T * > getAssociatedObjects(const std::string &name) const
get associated objects as a vector<object> this compact form throws an exception if the object is not...
void setVertexLink(const VertexLink_t &vertexLink)
void setDetail(TauJetParameters::Detail detail, int value)
void setVertex(const xAOD::VertexContainer *cont, const xAOD::Vertex *vertex)
const Jet * jet() const
float z() const
Returns the z position.
@ TauJetVtxFraction
@Tau Jet Vertex Fraction
Definition TauDefs.h:262
@ PriVtx
Primary vertex.
Jet_v1 Jet
Definition of the current "jet version".
std::map< const xAOD::Vertex *, xAOD::TrackVertexAssociationList > TrackVertexAssociationMap
VertexContainer_v1 VertexContainer
Definition of the current "Vertex container version".
Vertex_v1 Vertex
Define the latest version of the vertex class.
TauJet_v3 TauJet
Definition of the current "tau version".