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