ATLAS Offline Software
Loading...
Searching...
No Matches
TruthAlgs.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
5// Header include
10
11#include <set>
12#include <tuple>
13
14#include "TH1D.h"
15#include "TNtuple.h"
16#include "TTree.h"
17#include "TROOT.h"
18//-------------------------------------------------
19
20using namespace std;
21
22namespace VKalVrtAthena {
23
24 //____________________________________________________________________________________________________
26 {
28 constexpr const char* NAME = "truthParticleLink";
29 static const SG::ConstAccessor< Link_t > acc (NAME);
30 if( ! acc.isAvailable( *trkPart ) ) {
31 return nullptr;
32 }
33 const Link_t& link = acc( *trkPart );
34 if( ! link.isValid() ) {
35 return nullptr;
36 }
37 return *link;
38 }
39
40
41
42 //____________________________________________________________________________________________________
44 {
45
46 enum vertexCatogory_tracks {
47 allTruthAssociated,
48 hasFakeTracks,
49 allFakeTracks
50 };
51
52 enum vertexCategory_vertex {
53 uniqueTruthVertex,
54 multipleTruthVertices,
55 noTruthVertex
56 };
57
58
59 // std::multiset allows to have multiplicity of the element
60 multiset<const xAOD::TruthVertex*> truth_vertices;
61
62 // std::multiset doesn't allow to have multiplicity of the element
63 set<const xAOD::TruthVertex*> truth_vertices_types;
64
65 vector<const xAOD::TrackParticle*> reco_tracks; // associated with truth and has prodVtx
66 vector<const xAOD::TrackParticle*> orphan_tracks; // associated with truth, but does not have prodVtx
67 vector<const xAOD::TrackParticle*> fake_tracks; // no association with truth ( fake )
68
69 // loop over tracks
70 ATH_MSG_VERBOSE( "categorizeVertexTruthTopology(): loop over tracks" );
72 static const SG::ConstAccessor< truthLink > truthParticleLinkAcc( "truthParticleLink" );
73 for( size_t itrk=0; itrk<vertex->nTrackParticles(); itrk++ ) {
74 const auto *trk = vertex->trackParticle( itrk );
75
76 ATH_MSG_VERBOSE( "categorizeVertexTruthTopology(): track loop itrk = " << itrk );
77
78
79 const truthLink& link = truthParticleLinkAcc(*trk);
80
81 if ( ! link ) {
82 fake_tracks.emplace_back( trk );
83 continue;
84 }
85
86 const xAOD::TruthParticle *truth = *link;
87 if( ! truth->hasProdVtx() ) {
88 orphan_tracks.emplace_back( trk );
89 continue;
90 }
91
92 reco_tracks.emplace_back( trk );
93
94 truth_vertices_types .insert( truth->prodVtx() );
95 truth_vertices .insert( truth->prodVtx() );
96
97 }
98
99
100 // Add truth track pattern to the reco vertex
101 ATH_MSG_VERBOSE( "categorizeVertexTruthTopology(): Add truth track pattern to the reco vertex" );
102 const static SG::Accessor<char> trkpatAcc( "truth_trk_pattern" );
103 if( reco_tracks.size() == vertex->nTrackParticles() ) {
104 trkpatAcc( *vertex ) = allTruthAssociated;
105 } else if( fake_tracks.size() == vertex->nTrackParticles() ) {
106 trkpatAcc( *vertex ) = allFakeTracks;
107 } else {
108 trkpatAcc( *vertex ) = hasFakeTracks;
109 }
110
111
112 // Histogramming - counting the number of appearing truth vertices connected
113 ATH_MSG_VERBOSE( "categorizeVertexTruthTopology(): Histogramming - counting the number of appearing truth vertices connected" );
114 vector< tuple<const xAOD::TruthVertex*, size_t> > truth_vertex_histogram;
115 for( const auto *v : truth_vertices_types ) {
116 size_t count = truth_vertices.count( v );
117 truth_vertex_histogram.emplace_back( v, count );
118 }
119
120 // Determine the truth vertex associated to this vertex by majority decision
121 ATH_MSG_VERBOSE( "categorizeVertexTruthTopology(): Determine the truth vertex associated to this vertex by majority decision" );
122 tuple<const xAOD::TruthVertex*, size_t> tmp_tuple( nullptr, 0 );
123 for( const auto& t : truth_vertex_histogram ) {
124 const size_t& size_tmp = get<1>( tmp_tuple );
125 const size_t& size_this = get<1>( t );
126 if( size_tmp < size_this ) tmp_tuple = t;
127 }
128
129 // Add truth track pattern to the reco vertex
130 ATH_MSG_VERBOSE( "categorizeVertexTruthTopology(): Add truth track pattern to the reco vertex" );
131 char truth_vtx_pattern = 0;
132 if( truth_vertices_types.empty() ) {
133 truth_vtx_pattern = noTruthVertex;
134 } else if( truth_vertices_types.size() == 1 ) {
135 truth_vtx_pattern = uniqueTruthVertex;
136 } else {
137 truth_vtx_pattern = multipleTruthVertices;
138 }
139 static const SG::Accessor<char> vtxpatAcc( "truth_vtx_pattern" );
140 vtxpatAcc(*vertex) = truth_vtx_pattern;
141
142
144 if( noTruthVertex != truth_vtx_pattern ) {
145
146 // Retrieve the truth vertex container for element link
147 ATH_MSG_VERBOSE( "categorizeVertexTruthTopology(): Retrieve the truth vertex container for element link" );
148 const xAOD::TruthVertexContainer* truthVertexContainer{};
149 ATH_CHECK( evtStore()->retrieve( truthVertexContainer, "TruthVertices") );
150
151 // create the element link
152 ATH_MSG_VERBOSE( "categorizeVertexTruthTopology(): create the element link" );
153 const auto *theVertex = get<0>( tmp_tuple );
154 if( theVertex ) {
155 // Add the truth vertex element link to the reco vertex
156 vtx_link.toIndexedElement(*truthVertexContainer,theVertex->index());
157 ATH_MSG_VERBOSE( "categorizeVertexTruthTopology(): Add the truth vertex element link to the reco vertex" );
158 }
159 }
160 // [JDC] a ElementLink decorator should be filled every event
161 // although using a null link
163 linkAcc( "truth_vtx_link" );
164 linkAcc(*vertex) = vtx_link;
165
166 return StatusCode::SUCCESS;
167 }
168
169} // end of namespace VKalVrtAthena
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_VERBOSE(x)
Helper class to provide constant type-safe access to aux data.
Helper class to provide type-safe access to aux data.
ElementLink< xAOD::TruthParticleContainer > Link_t
ServiceHandle< StoreGateSvc > & evtStore()
Helper class to provide type-safe access to aux data.
Helper class to provide constant type-safe access to aux data.
static const xAOD::TruthParticle * getTrkGenParticle(const xAOD::TrackParticle *)
Definition TruthAlgs.cxx:25
StatusCode categorizeVertexTruthTopology(xAOD::Vertex *vertex)
Definition TruthAlgs.cxx:43
bool hasProdVtx() const
Check for a production vertex on this particle.
const TruthVertex_v1 * prodVtx() const
The production vertex of this particle.
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
Definition hcg.cxx:132
int count(std::string s, const std::string &regx)
count how many occurances of a regx are in a string
Definition hcg.cxx:148
STL namespace.
TrackParticle_v1 TrackParticle
Reference the current persistent version:
TruthVertexContainer_v1 TruthVertexContainer
Declare the latest version of the truth vertex container.
Vertex_v1 Vertex
Define the latest version of the vertex class.
TruthParticle_v1 TruthParticle
Typedef to implementation.