ATLAS Offline Software
Loading...
Searching...
No Matches
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
21using namespace std;
22
23namespace VKalVrtAthena {
24
25 //____________________________________________________________________________________________________
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 //____________________________________________________________________________________________________
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
#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.
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:26
StatusCode categorizeVertexTruthTopology(xAOD::Vertex *vertex)
Definition TruthAlgs.cxx:44
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:130
int count(std::string s, const std::string &regx)
count how many occurances of a regx are in a string
Definition hcg.cxx:146
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.