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// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
16
17 if( m_useTJVA) {
18 ATH_MSG_INFO("using TJVA to determine tau vertex");
20 ATH_CHECK( m_trkVertexAssocTool.retrieve() );
21 }
22
23 return StatusCode::SUCCESS;
24}
25
26// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
28 const xAOD::VertexContainer* vertexContainer) const
29{
30 const xAOD::VertexContainer * vxContainer = nullptr;
31
32 if (!m_vertexInputContainer.empty()) {
34 if (!vertexInHandle.isValid()) {
35 ATH_MSG_ERROR ("Could not retrieve HiveDataObj with key " << vertexInHandle.key());
36 return StatusCode::FAILURE;
37 }
38 vxContainer = vertexInHandle.cptr();
39 }
40 else {
41 if (vertexContainer != nullptr) {
42 vxContainer = vertexContainer;
43 }
44 else {
45 ATH_MSG_WARNING ("No Vertex Container in trigger");
46 return StatusCode::FAILURE;
47 }
48 }
49
50 ATH_MSG_VERBOSE("size of VxPrimaryContainer is: " << vxContainer->size() );
51 if (vxContainer->empty()) return StatusCode::SUCCESS;
52
53 // find default PrimaryVertex (needed if TJVA is switched off or fails)
54 const xAOD::Vertex* primaryVertex = nullptr;
55 if (inTrigger()) { // trigger: find default PrimaryVertex (highest sum pt^2)
56 primaryVertex = (*vxContainer)[0];
57 }
58 else { // offline: the first and only primary vertex candidate is picked
59 for (const auto vertex : *vxContainer) {
60 if (vertex->vertexType() == xAOD::VxType::PriVtx) {
61 primaryVertex = vertex;
62 break;
63 }
64 }
65 }
66
67 // associate vertex to tau
68 if (primaryVertex) pTau.setVertex(vxContainer, primaryVertex);
69
70 //stop here if TJVA is disabled
71 if (!m_useTJVA) return StatusCode::SUCCESS;
72
73 // additional protection in case TJVA is run online - not supported for Run3
74 if ( m_useTJVA && inTrigger()){
75 ATH_MSG_ERROR("TJVA not available for online");
76 return StatusCode::FAILURE;
77 }
78
79 // try to find new PV with TJVA
80 ATH_MSG_DEBUG("TJVA enabled -> try to find new PV for the tau candidate");
81
82 float maxJVF = -100.;
83 ElementLink<xAOD::VertexContainer> newPrimaryVertexLink = getPV_TJVA(pTau, *vxContainer, maxJVF );
84 if (newPrimaryVertexLink.isValid()) {
85 // set new primary vertex
86 // will overwrite default one which was set above
87 pTau.setVertexLink(newPrimaryVertexLink);
88 // save highest JVF value
89 pTau.setDetail(xAOD::TauJetParameters::TauJetVtxFraction,static_cast<float>(maxJVF));
90 ATH_MSG_DEBUG("TJVA vertex found and set");
91 }
92 else {
93 ATH_MSG_WARNING("couldn't find new PV for TJVA");
94 }
95
96 return StatusCode::SUCCESS;
97}
98
99// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
102 const xAOD::VertexContainer& vertices,
103 float& maxJVF) const
104{
105 const xAOD::Jet* pJetSeed = pTau.jet();
106 std::vector<const xAOD::TrackParticle*> tracksForTJVA;
107
108 // 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
109
110 std::vector<const xAOD::TrackParticle*> assocTracks;
111 if (! pJetSeed->getAssociatedObjects(m_assocTracksName, assocTracks)) {
112 ATH_MSG_ERROR("Could not retrieve the AssociatedObjects named \""<< m_assocTracksName <<"\" from jet");
114 }
115
116 // Store tracks that meet TJVA track selection criteria and are between deltaR of 0.2 with the jet seed
117 // To be included in the TJVA calculation
118 // Maybe not as efficient as deleting unwanted tracks from assocTrack but quicker and safer for now.
119 float sumTrackAll = 0.0;
120 for ( auto xTrack : assocTracks ) {
121 if ( (xTrack->p4().DeltaR(pJetSeed->p4())<m_dDeltaRMax) && m_TrackSelectionToolForTJVA->accept(*xTrack) ) {
122 if (!inEleRM()) {
123 tracksForTJVA.push_back(xTrack);
124 }
125 else {
126 static const SG::ConstAccessor<ElementLink<xAOD::TrackParticleContainer>> acc_originalTrack("ERMOriginalTrack");
127 if (!acc_originalTrack.isAvailable(*xTrack)) {
128 ATH_MSG_WARNING("Original ERM track link is not available, skipping track");
129 continue;
130 }
131 auto original_id_track_link = acc_originalTrack(*xTrack);
132 if (!original_id_track_link.isValid()) {
133 ATH_MSG_WARNING("Original ERM track link is not valid, skipping track");
134 continue;
135 }
136 tracksForTJVA.push_back(*original_id_track_link);
137 }
138 sumTrackAll += xTrack->pt();
139 }
140 }
141
142 if (this->msgLevel() <= MSG::DEBUG){
143 ATH_MSG_DEBUG("tracksForTJVA # = " << tracksForTJVA.size() << ", sum pT = " << sumTrackAll);
144
145 for (uint i = 0; i < tracksForTJVA.size(); i++){
146 ATH_MSG_DEBUG("tracksForTJVA[" << i << "].pt = " << tracksForTJVA[i]->pt());
147 }
148 }
149
151
152 // Get track vertex map
153 std::vector<const xAOD::Vertex*> vertVec;
154 for (const xAOD::Vertex* vert : vertices) {
155 vertVec.push_back(vert);
156 }
157
158 if (this->msgLevel() <= MSG::DEBUG){
159 ATH_MSG_DEBUG("Vertex # = " << vertVec.size());
160 for (uint i = 0; i < vertVec.size(); i++){
161 ATH_MSG_DEBUG("vertVec[" << i << "].z = " << vertVec[i]->z());
162 }
163 }
164
165 // Tool returns map between vertex and tracks associated to that vertex (based on selection criteria set in config)
166 trktovxmap = m_trkVertexAssocTool->getMatchMap(tracksForTJVA, vertVec);
167
168 if (this->msgLevel() <= MSG::DEBUG){
169 for (const auto& [vtx, trks] : trktovxmap){
170 std::stringstream ss;
171 for (auto ass_trk : trks){
172 for (uint i=0; i < tracksForTJVA.size(); i++){
173 if (ass_trk->p4() == tracksForTJVA[i]->p4()) {
174 ss << i << ", ";
175 break;
176 }
177 }
178 }
179 ATH_MSG_DEBUG("Vtx[" << vtx->index() << "] associated with trks [" << ss.str() << "]");
180 }
181 }
182
183 // Get the highest JVF vertex and store maxJVF for later use
184 // Note: the official JetMomentTools/JetVertexFractionTool doesn't provide any possibility to access the JVF value, but just the vertex.
185 maxJVF=-100.;
186 // Store sum(deltaz(track-vertex)) and jet vertex fraction scores
187 std::vector<float> sumDz;
188 std::vector<float> v_jvf;
189 size_t maxIndex = 0;
190 for (const xAOD::Vertex* vert : vertices) {
191 float jvf = 0.0;
192 std::vector<const xAOD::TrackParticle*> tracks = trktovxmap[vert];
193 // get jet vertex fraction and sumdeltaZ scores
194 std::pair<float, float> spair = getVertexScores(tracks, vert->z());
195 jvf = (sumTrackAll!=0. ? spair.first/sumTrackAll : 0.);
196 v_jvf.push_back(jvf);
197 sumDz.push_back(spair.second);
198
199 if (jvf > maxJVF) {
200 maxJVF = jvf;
201 maxIndex = vert->index();
202 }
203 }
204
205 ATH_MSG_DEBUG("First TJVA");
206 ATH_MSG_DEBUG("TJVA vtx found at z: " << vertices.at(maxIndex)->z() << " i_vtx = " << maxIndex << "jvf = " << maxJVF);
207 ATH_MSG_DEBUG("highest pt vtx found at z (i=0): " << vertices.at(0)->z());
208
209 float min_sumDz = 99999999.;
210 for (const xAOD::Vertex* vert : vertices) {
211 ATH_MSG_DEBUG("i_vtx=" << vert->index() << ", z=" << vert->z() << ", JVF=" << v_jvf[vert->index()] << ", sumDz=" << sumDz[vert->index()]);
212 if ( v_jvf[vert->index()] == maxJVF ){
213 // in case of 0 tracks, first vertex will have sumDz=0, and be selected
214 if (sumDz[vert->index()] < min_sumDz){
215 min_sumDz = sumDz[vert->index()];
216 maxIndex = vert->index();
217 }
218 }
219 }
220
221
222 ATH_MSG_DEBUG("Final TJVA");
223 ATH_MSG_DEBUG("TJVA vtx found at z: " << vertices.at(maxIndex)->z() << " i_vtx = " << maxIndex);
224
225 return ElementLink<xAOD::VertexContainer> (vertices, maxIndex);
226}
227
228// get sum of pT from tracks associated to vertex (tracks from track to vertex map) (pair first)
229// sum over tracks associated to vertex of deltaZ(track-vertex) (pair second)
230std::pair<float,float> TauVertexFinder::getVertexScores(const std::vector<const xAOD::TrackParticle*>& tracks, float vx_z) const{
231
232 float sumTrackPV = 0.;
233 float sumDeltaZ = 0.;
234 for (auto trk : tracks){
235 sumTrackPV += trk->pt();
236 sumDeltaZ += std::abs(trk->z0() - vx_z + trk->vz());
237 }
238
239 return std::make_pair(sumTrackPV, sumDeltaZ);
240}
241#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
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".