ATLAS Offline Software
Loading...
Searching...
No Matches
VertexTrackIsolation.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3*/
4
6
7#include <string>
8#include <vector>
9
11#include "TLorentzVector.h"
14#include "xAODPrimitives/IsolationHelpers.h" //For the definition of Iso::conesize
17
18using namespace std;
19namespace DerivationFramework {
20
21VertexTrackIsolation::VertexTrackIsolation(const std::string& t, const std::string& n, const IInterface* p)
22 : base_class(t, n, p),
23 m_trackIsoTool("xAOD::TrackIsolationTool"),
24 m_trackContainerName("InDetTrackParticles"),
26 m_cones(),
27 m_vertexType(7),
28 m_doIsoPerTrk(false),
30 m_fixElecExclusion(false),
31 m_includeV0(false) {
32 ATH_MSG_DEBUG("in constructor");
33
34 // Declare tools
35 declareProperty("TrackIsoTool", m_trackIsoTool);
36 declareProperty("TrackContainer", m_trackContainerName);
37 declareProperty("InputVertexContainer", m_vertexContainerName);
38 declareProperty("PassFlags", m_passFlags);
39 declareProperty("IsolationTypes", m_cones);
40 declareProperty("DoVertexTypes", m_vertexType);
41
42 declareProperty("DoIsoPerTrk", m_doIsoPerTrk,
43 "New property to deal with track isolation per track, the default option "
44 "(m_doIsoPerTrk=false) preserves the old behavior");
45 declareProperty("RemoveDuplicate", m_removeDuplicate, "Used with DoIsoPerTrk");
46 declareProperty("FixElecExclusion", m_fixElecExclusion);
47 declareProperty("IncludeV0", m_includeV0);
48}
49
50// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
51
53
54 ATH_MSG_DEBUG("in initialize()");
55
56 CHECK(m_trackIsoTool.retrieve());
57 // Check that flags were given to tag the correct vertices
58
59 // Control the IsolationType sequence
60 if (m_cones.empty()) {
61 ATH_MSG_INFO("Setting ptcones to default");
62
63 if (m_doIsoPerTrk)
68 }
69
70 return StatusCode::SUCCESS;
71}
72
73// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
74
76 // everything all right
77 return StatusCode::SUCCESS;
78}
79
80// check if the two vertices are composed of the same set of tracks
81bool VertexTrackIsolation::isSame(const xAOD::Vertex* theVtx1, const xAOD::Vertex* theVtx2) const {
82 assert(theVtx1);
83 assert(theVtx2);
84 if (theVtx1 == theVtx2)
85 return true;
86 if (theVtx1->nTrackParticles() != theVtx2->nTrackParticles())
87 return false;
88
89 if (m_removeDuplicate == 2 && theVtx1->nTrackParticles() == 4) { // a special case with sub-structure
90 bool firstTwoAreSame =
91 std::set<const xAOD::TrackParticle*>({theVtx1->trackParticle(0), theVtx1->trackParticle(1)}) ==
92 std::set<const xAOD::TrackParticle*>(
93 {theVtx2->trackParticle(0), theVtx2->trackParticle(1)}); // the 1st pair of tracks
94 bool lastTwoAreSame =
95 std::set<const xAOD::TrackParticle*>({theVtx1->trackParticle(2), theVtx1->trackParticle(3)}) ==
96 std::set<const xAOD::TrackParticle*>(
97 {theVtx2->trackParticle(2), theVtx2->trackParticle(3)}); // the 2nd pair of tracks
98 if (firstTwoAreSame && lastTwoAreSame)
99 return true;
100 else
101 return false;
102 } else { // the general case
103 std::set<const xAOD::TrackParticle*> vtxset1;
104 std::set<const xAOD::TrackParticle*> vtxset2;
105 for (size_t i = 0; i < theVtx1->nTrackParticles(); i++)
106 vtxset1.insert(theVtx1->trackParticle(i));
107 for (size_t i = 0; i < theVtx2->nTrackParticles(); i++)
108 vtxset2.insert(theVtx2->trackParticle(i));
109 return vtxset1 == vtxset2;
110 }
111}
112
114 const std::vector<const xAOD::Vertex*>& theColl) const {
115 for (const auto vtxPtr : theColl) {
116 if (isSame(vtxPtr, theVtx))
117 return true;
118 }
119 return false;
120}
121
122// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
123
124StatusCode VertexTrackIsolation::addBranches(const EventContext&) const {
125
126 const xAOD::TrackParticleContainer* idTrackParticleContainer{};
127 const xAOD::VertexContainer* vertexContainer{};
128
130 CHECK(evtStore()->retrieve(idTrackParticleContainer, m_trackContainerName)); // FIXME Use Handles
131 } else {
132 ATH_MSG_ERROR("Failed loading IdTrackparticleContainer container");
133 return StatusCode::FAILURE;
134 }
135
136 // load vertices
138 CHECK(evtStore()->retrieve(vertexContainer, m_vertexContainerName)); // FIXME Use Handles
139 } else {
140 ATH_MSG_ERROR("Failed loading vertex container");
141 return StatusCode::FAILURE;
142 }
143
144 std::vector<const xAOD::Vertex*> outVtxContainer;
145
146 // Convert m_cones (done per-event to avoid needing extra public dependency)
147
148 std::vector<xAOD::Iso::IsolationType> cones(m_cones.size());
149
150 for (unsigned int i = 0; i < m_cones.size(); i++)
151 cones[i] = xAOD::Iso::IsolationType(m_cones[i]);
152
153 // The provided IsolationTypes are re-ordered internally
154 std::sort(cones.begin(), cones.end(), [](xAOD::Iso::IsolationType i, xAOD::Iso::IsolationType j) {
155 return xAOD::Iso::coneSize(i) > xAOD::Iso::coneSize(j);
156 });
157
158 // loop over vertices
159 for (auto vertex : *vertexContainer) {
160
161 bool passed = false;
162 for (std::vector<std::string>::const_iterator flagItr = m_passFlags.begin(); flagItr != m_passFlags.end();
163 ++flagItr) {
164 SG::Accessor<Char_t> flagAcc(*flagItr);
165 if (flagAcc.isAvailable(*vertex) && flagAcc(*vertex) != 0) {
166 passed = true;
167 break;
168 }
169 } // end of loop over flags
170 // if no passFlags given, decorate everything
171 if (passed || m_passFlags.empty()) {
172 if (!m_doIsoPerTrk) { // for legacy
173 // Removed warning was here
174 } else {
175 if (m_removeDuplicate) {
176 if (isContainedIn(vertex, outVtxContainer))
177 continue;
178 outVtxContainer.push_back(vertex);
179 }
180 }
181
182 TLorentzVector candidate;
183
184 std::set<const xAOD::TrackParticle*> exclusionset;
185
186 for (auto part : vertex->trackParticleLinks()) { // Loop over tracks linked to vertex
187
188 candidate += (*part)->p4();
189
190 // if you have electron tracks that are GSF, need to get original ID
191 // trackparticle for excluding this particle
192 if (m_fixElecExclusion) {
194 exclusionset.insert(partID);
195 } else
196 exclusionset.insert(*part); // If it crashes use the direct TP from the vertex
197 }
198 // No! the above candidate will fail acceptance of isolation() because
199 // it's neither a muon nor a TrackParticle
200
201 // Add in V0->XX particles
202 if (m_includeV0) {
204 V0VertexLinksAcc("V0VertexLinks");
205 auto V0VertLink = V0VertexLinksAcc(*vertex);
206 const xAOD::Vertex* V0Vert = V0VertLink.at(0).getDataPtr()->at(0);
207 for (auto part : V0Vert->trackParticleLinks()) {
208 candidate += (*part)->p4();
209 exclusionset.insert(*part);
210 }
211 }
212
213 // Make a dummy TrackParticle, otherwise TrackIsolationTool cannot deal
214 // with it
215 xAOD::TrackParticle candidate_slyTrack;
216 candidate_slyTrack.makePrivateStore();
217 candidate_slyTrack.setDefiningParameters(0, 0., candidate.Phi(), candidate.Theta(), 0. /*1./candidate.P()*/);
218 // The precision goes down a bit, but it's a matter of 10e-7 with our
219 // values of interest
220
221 // Make a correctionlist such that the given exclusionset will be removed
222 // from the used tracks There is no danger that the input particle will be
223 // excluded, as it is not part of inDetTrackContainer
224 xAOD::TrackCorrection corrlist;
225 corrlist.trackbitset.set(static_cast<unsigned int>(xAOD::Iso::coreTrackPtr));
226
227 const string vtxType_name[3] = {"SumPt", "A0", "Z0"};
228
229 xAOD::BPhysHelper vertex_h(vertex); // Use the BPhysHelper to access vertex quantities
230
231 // Loop over refitted primary vertex choice
232 for (unsigned int vertex_type = 0; vertex_type <= xAOD::BPhysHelper::PV_MIN_Z0; vertex_type++) {
233
234 if ((m_vertexType & (1 << vertex_type)) == 0)
235 continue; // Stop if the type of vertex is not required
236
237 ATH_MSG_DEBUG("List of cone types");
238 for (unsigned int i = 0; i < cones.size(); i++) {
239 ATH_MSG_DEBUG("cone type = " << xAOD::Iso::toString(xAOD::Iso::IsolationType(cones[i])));
240 }
241
242 const xAOD::Vertex* refVtx = vertex_h.pv(static_cast<xAOD::BPhysHelper::pv_type>(vertex_type)); // Fix the cast
243 if(refVtx == nullptr){
244 ATH_MSG_WARNING("Could not retrieve Vertex type " << vertex_type << ", check vertex type setting. Comes from container " << m_vertexContainerName);
245 continue;
246 }
248
249 if (!m_doIsoPerTrk) {
250 m_trackIsoTool->trackIsolation(result, candidate_slyTrack, cones, corrlist, refVtx, &exclusionset,
251 idTrackParticleContainer);
252
253 // Decorate the vertex with all the isolation values
254 for (unsigned int i = 0; i < cones.size(); i++) {
255
256 string variableName = xAOD::Iso::toString(xAOD::Iso::IsolationType(cones[i]));
257 variableName += vtxType_name[vertex_type];
258
259 SG::Decorator<float> isolation(variableName);
260 isolation(*vertex) = result.ptcones[i];
261 }
262 } else {
263 for (size_t i = 0; i < vertex->nTrackParticles(); i++) {
264 m_trackIsoTool->trackIsolation(result, *vertex->trackParticle(i), cones, corrlist, refVtx, &exclusionset,
265 idTrackParticleContainer);
266
267 for (unsigned int j = 0; j < cones.size(); j++) {
268 string variableName = xAOD::Iso::toString(xAOD::Iso::IsolationType(cones[j]));
269 variableName += vtxType_name[vertex_type];
270 variableName += "_trk";
271 variableName += std::to_string(i + 1);
272 SG::Decorator<float> isolation(variableName);
273 isolation(*vertex) = result.ptcones[j];
274 }
275 }
276 }
277 }
278
279 } // End of if passed
280 } // end of loop over vertices
281
282 return StatusCode::SUCCESS;
283}
284
285} // End of namespace DerivationFramework
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
: B-physics xAOD helpers.
Helper class to provide constant type-safe access to aux data.
#define CHECK(...)
Evaluate an expression and check for errors.
bool passed(DecisionID id, const DecisionIDContainer &)
checks if required decision ID is in the set of IDs in the container
VertexTrackIsolation(const std::string &t, const std::string &n, const IInterface *p)
bool isSame(const xAOD::Vertex *theVtx1, const xAOD::Vertex *theVtx2) const
bool isContainedIn(const xAOD::Vertex *theVtx, const std::vector< const xAOD::Vertex * > &theColl) const
virtual StatusCode addBranches(const EventContext &ctx) const
ToolHandle< xAOD::ITrackIsolationTool > m_trackIsoTool
Helper class to provide type-safe access to aux data.
void makePrivateStore()
Create a new (empty) private store for this object.
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.
Helper class to provide type-safe access to aux data.
Definition Decorator.h:59
const xAOD::Vertex * pv(const pv_type vertexType=BPhysHelper::PV_MIN_A0)
Get the refitted collision vertex of type pv_type.
pv_type
: Enum type of the PV
void setDefiningParameters(float d0, float z0, float phi0, float theta, float qOverP)
Set the defining parameters.
size_t nTrackParticles() const
Get the number of tracks associated with this vertex.
const TrackParticleLinks_t & trackParticleLinks() const
Get all the particles associated with the vertex.
const TrackParticle * trackParticle(size_t i) const
Get the pointer to a given track that was used in vertex reco.
bool contains(const std::string &s, const std::string &regx)
does a string contain the substring
Definition hcg.cxx:114
THE reconstruction tool.
STL namespace.
void sort(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end)
Specialization of sort for DataVector/List.
const xAOD::TrackParticle * getOriginalTrackParticleFromGSF(const xAOD::TrackParticle *trkPar)
Helper function for getting the "Original" Track Particle (i.e before GSF) via the GSF Track Particle...
IsolationType
Overall enumeration for isolation types in xAOD files.
@ ptcone20
Track isolation.
std::string toString(const IsoType &iso)
TrackParticle_v1 TrackParticle
Reference the current persistent version:
VertexContainer_v1 VertexContainer
Definition of the current "Vertex container version".
Vertex_v1 Vertex
Define the latest version of the vertex class.
TrackParticleContainer_v1 TrackParticleContainer
Definition of the current "TrackParticle container version".
Iso::IsolationTrackCorrectionBitset trackbitset