ATLAS Offline Software
TruthAlgs.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 // Header include
10 
11 #include <iostream>
12 #include <set>
13 #include <tuple>
14 
15 #include "TH1D.h"
16 #include "TNtuple.h"
17 #include "TTree.h"
18 #include "TROOT.h"
19 //-------------------------------------------------
20 
21 using namespace std;
22 
23 namespace VKalVrtAthena {
24 
25  //____________________________________________________________________________________________________
26  const xAOD::TruthParticle* VrtSecInclusive::getTrkGenParticle ( const xAOD::TrackParticle *trkPart )
27  {
29  constexpr const char* NAME = "truthParticleLink";
30  static const SG::ConstAccessor< Link_t > acc (NAME);
31  if( ! acc.isAvailable( *trkPart ) ) {
32  return nullptr;
33  }
34  const Link_t& link = acc( *trkPart );
35  if( ! link.isValid() ) {
36  return nullptr;
37  }
38  return *link;
39  }
40 
41 
42 
43  //____________________________________________________________________________________________________
44  StatusCode VrtSecInclusive::categorizeVertexTruthTopology( xAOD::Vertex *vertex )
45  {
46 
47  enum vertexCatogory_tracks {
48  allTruthAssociated,
49  hasFakeTracks,
50  allFakeTracks
51  };
52 
53  enum vertexCategory_vertex {
54  uniqueTruthVertex,
55  multipleTruthVertices,
56  noTruthVertex
57  };
58 
59 
60  // std::multiset allows to have multiplicity of the element
61  multiset<const xAOD::TruthVertex*> truth_vertices;
62 
63  // std::multiset doesn't allow to have multiplicity of the element
64  set<const xAOD::TruthVertex*> truth_vertices_types;
65 
66  vector<const xAOD::TrackParticle*> reco_tracks; // associated with truth and has prodVtx
67  vector<const xAOD::TrackParticle*> orphan_tracks; // associated with truth, but does not have prodVtx
68  vector<const xAOD::TrackParticle*> fake_tracks; // no association with truth ( fake )
69 
70  // loop over tracks
71  ATH_MSG_VERBOSE( "categorizeVertexTruthTopology(): loop over tracks" );
72  for( size_t itrk=0; itrk<vertex->nTrackParticles(); itrk++ ) {
73  const auto *trk = vertex->trackParticle( itrk );
74 
75  ATH_MSG_VERBOSE( "categorizeVertexTruthTopology(): track loop itrk = " << itrk );
77  static const SG::ConstAccessor< truthLink > truthParticleLinkAcc( "truthParticleLink" );
78  const truthLink& link = truthParticleLinkAcc(*trk);
79 
80  if ( ! link ) {
81  fake_tracks.emplace_back( trk );
82  continue;
83  }
84 
85  const xAOD::TruthParticle *truth = *link;
86  if( ! truth->hasProdVtx() ) {
87  orphan_tracks.emplace_back( trk );
88  continue;
89  }
90 
91  reco_tracks.emplace_back( trk );
92 
93  truth_vertices_types .insert( truth->prodVtx() );
94  truth_vertices .insert( truth->prodVtx() );
95 
96  }
97 
98 
99  // Add truth track pattern to the reco vertex
100  ATH_MSG_VERBOSE( "categorizeVertexTruthTopology(): Add truth track pattern to the reco vertex" );
101  const static SG::Accessor<char> trkpatAcc( "truth_trk_pattern" );
102  if( reco_tracks.size() == vertex->nTrackParticles() ) {
103  trkpatAcc( *vertex ) = allTruthAssociated;
104  } else if( fake_tracks.size() == vertex->nTrackParticles() ) {
105  trkpatAcc( *vertex ) = allFakeTracks;
106  } else {
107  trkpatAcc( *vertex ) = hasFakeTracks;
108  }
109 
110 
111  // Histogramming - counting the number of appearing truth vertices connected
112  ATH_MSG_VERBOSE( "categorizeVertexTruthTopology(): Histogramming - counting the number of appearing truth vertices connected" );
113  vector< tuple<const xAOD::TruthVertex*, size_t> > truth_vertex_histogram;
114  for( const auto *v : truth_vertices_types ) {
115  size_t count = truth_vertices.count( v );
116  truth_vertex_histogram.emplace_back( v, count );
117  }
118 
119  // Determine the truth vertex associated to this vertex by majority decision
120  ATH_MSG_VERBOSE( "categorizeVertexTruthTopology(): Determine the truth vertex associated to this vertex by majority decision" );
121  tuple<const xAOD::TruthVertex*, size_t> tmp_tuple( nullptr, 0 );
122  for( const auto& t : truth_vertex_histogram ) {
123  const size_t& size_tmp = get<1>( tmp_tuple );
124  const size_t& size_this = get<1>( t );
125  if( size_tmp < size_this ) tmp_tuple = t;
126  }
127 
128  // Add truth track pattern to the reco vertex
129  ATH_MSG_VERBOSE( "categorizeVertexTruthTopology(): Add truth track pattern to the reco vertex" );
130  char truth_vtx_pattern = 0;
131  if( truth_vertices_types.empty() ) {
132  truth_vtx_pattern = noTruthVertex;
133  } else if( truth_vertices_types.size() == 1 ) {
134  truth_vtx_pattern = uniqueTruthVertex;
135  } else {
136  truth_vtx_pattern = multipleTruthVertices;
137  }
138  static const SG::Accessor<char> vtxpatAcc( "truth_vtx_pattern" );
139  vtxpatAcc(*vertex) = truth_vtx_pattern;
140 
141 
143  if( noTruthVertex != truth_vtx_pattern ) {
144 
145  // Retrieve the truth vertex container for element link
146  ATH_MSG_VERBOSE( "categorizeVertexTruthTopology(): Retrieve the truth vertex container for element link" );
147  const xAOD::TruthVertexContainer* truthVertexContainer ( nullptr );
148  ATH_CHECK( evtStore()->retrieve( truthVertexContainer, "TruthVertices") );
149 
150  // create the element link
151  ATH_MSG_VERBOSE( "categorizeVertexTruthTopology(): create the element link" );
152  const auto *theVertex = get<0>( tmp_tuple );
153  if( theVertex ) {
154  // Add the truth vertex element link to the reco vertex
155  vtx_link.toIndexedElement(*truthVertexContainer,theVertex->index());
156  ATH_MSG_VERBOSE( "categorizeVertexTruthTopology(): Add the truth vertex element link to the reco vertex" );
157  }
158  }
159  // [JDC] a ElementLink decorator should be filled every event
160  // although using a null link
162  linkAcc( "truth_vtx_link" );
163  linkAcc(*vertex) = vtx_link;
164 
165  return StatusCode::SUCCESS;
166  }
167 
168 } // end of namespace VKalVrtAthena
169 
170 
171 
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
SG::Accessor< char >
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
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
NtupleVars.h
XMLtoHeader.count
count
Definition: XMLtoHeader.py:85
VrtSecInclusive.h
VKalVrtAthena
Definition: AANT_Tools.cxx:24
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
xAOD::TruthParticle_v1
Class describing a truth particle in the MC record.
Definition: TruthParticle_v1.h:41
xAOD::TruthParticle_v1::hasProdVtx
bool hasProdVtx() const
Check for a production vertex on this particle.
Definition: TruthParticle_v1.cxx:74
AthenaPoolTestRead.acc
acc
Definition: AthenaPoolTestRead.py:16
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
xAOD::TruthParticle_v1::prodVtx
const TruthVertex_v1 * prodVtx() const
The production vertex of this particle.
Definition: TruthParticle_v1.cxx:80
Accessor.h
Helper class to provide type-safe access to aux data.
python.PyAthena.v
v
Definition: PyAthena.py:157
Trk::vertex
@ vertex
Definition: MeasurementType.h:21
xAOD::Vertex_v1
Class describing a Vertex.
Definition: Vertex_v1.h:42
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.