ATLAS Offline Software
Loading...
Searching...
No Matches
JetTruthNtupleTool.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// JetTruthNtupleTool.cxx
7// Source file for class JetTruthNtupleTool
9// (c) ATLAS Detector software
11
12#include "GaudiKernel/ITHistSvc.h"
13#include "GaudiKernel/ISvcLocator.h"
14
15// Trk
19// Truth
21
23
24#include <TGraphAsymmErrors.h>
25#include "TTree.h"
26
27// constructor
29 const std::string& t,
30 const std::string& n,
31 const IInterface* p )
32 :
33 AthAlgTool(t,n,p),
34 m_nt(nullptr),
39 m_mc_trackToJetAngle(nullptr),
45{
46 declareInterface<IJetTruthNtupleTool>(this);
47
48 // Declare the properties
49 declareProperty("NtupleFileName", m_ntupleFileName = "TRKVAL/Validation","Ntuple file handle");
50 declareProperty("NtupleTreeName", m_ntupleTreeName = "TruthJets", "Name of the ntuple tree");
51}
52
53// destructor
55
56// initialize
58
59 ATH_CHECK( m_evt.initialize() );
60
61 // ---------------------------
62 // retrieve pointer to THistSvc
63 SmartIF<ITHistSvc> tHistSvc{service("THistSvc")};
64 ATH_CHECK( tHistSvc.isValid() );
65
66 // ---------------------------
67 // create tree and register it to THistSvc
68 m_nt = new TTree(TString(m_ntupleTreeName), "Track Validation JetTruth");
69 // NB: we must not delete the tree, this is done by THistSvc
70 std::string fullNtupleName = "/"+m_ntupleFileName+"/"+m_ntupleTreeName;
71 ATH_CHECK( tHistSvc->regTree(fullNtupleName, m_nt) );
72
73 //-----------------
74 // add items
75 // event info:
76 m_nt->Branch("RunNumber", &m_runNumber ); // Run number
77 m_nt->Branch("EventNumber", &m_eventNumber ); // Event number
78
79 m_nt->Branch( "truth_jetMultiplicity", &m_mc_jetMultiplicity );
80 m_nt->Branch( "truth_jetSphericity", &m_mc_jetSphericity );
81 m_nt->Branch( "truth_jetThrust", &m_mc_jetThrust );
82 m_nt->Branch( "truth_jetEnergy", &m_mc_jetEnergy );
83 m_nt->Branch( "truth_jetMomentum", &m_mc_jetMomentum );
84
85 m_genParticleLinkIndex = new std::vector<unsigned int>();
86 m_nt->Branch( "GenParticleLinkIndex", &m_genParticleLinkIndex);// link to particles
87 m_mc_trackToJetAngle = new std::vector<float>();
88 m_nt->Branch( "truth_trackToJetAngle", &m_mc_trackToJetAngle); // truth_trackToJetAngle
89
90 ATH_MSG_VERBOSE ( "added items to ntuple" );
91
92 return StatusCode::SUCCESS;
93
94}
95
96
101
102 ATH_MSG_INFO ( "finalize() in " << name() );
105
106 return StatusCode::SUCCESS;
107}
108
113 const std::vector< Trk::GenParticleJet >& jets,
114 const int& nTruthRecordsAtCurrentEvent
115 // const std::vector<unsigned int>& /*particleToJetIndex*/
116 ) {
117 ATH_MSG_VERBOSE ( "in writeJetTruthData(...)" );
118
119 // ---------------------------------------
120 // fill event data
122 if (!evt.isValid()) {
123 ATH_MSG_WARNING ( "Could not retrieve event info" );
124 return StatusCode::FAILURE;
125 }
126 m_runNumber=evt->runNumber();
127 m_eventNumber=evt->eventNumber();
128
129 for (std::vector<Trk::GenParticleJet>::const_iterator itJet = jets.begin();
130 itJet < jets.end(); ++itJet ) {
131
132 m_mc_jetMultiplicity = itJet->getNumParticles();
133 m_mc_jetMomentum = itJet->getMomentum().mag();
134 m_mc_jetEnergy = itJet->getEnergy();
135 m_mc_jetSphericity = itJet->getSphericity();
136 m_mc_jetThrust = itJet->getThrust();
137
138 auto particles = itJet->getParticles();
139 m_mc_trackToJetAngle->reserve(particles.size());
140 m_genParticleLinkIndex->reserve(particles.size());
141 std::vector<int> indices = itJet->getIndicesInEvent();
142 std::vector<int>::iterator itIdx = indices.begin();
143 m_genParticleLinkIndex->clear();
144 m_mc_trackToJetAngle->clear();
145 for (auto itPrt = particles.begin();
146 itPrt < particles.end(); ++itPrt, ++itIdx) {
147
148 HepGeom::Vector3D<double> tempMomentum((*itPrt)->momentum().px(),
149 (*itPrt)->momentum().py(),
150 (*itPrt)->momentum().pz());
151 // attention: push_back calls need a clear() to avoid growth with each entry
152 m_mc_trackToJetAngle->push_back((float)tempMomentum.angle(itJet->getMomentum()));
153 m_genParticleLinkIndex->push_back(nTruthRecordsAtCurrentEvent+1+(unsigned int)(*itIdx));
154 }
155 m_nt->Fill();
157 }
158 return StatusCode::SUCCESS;
159}
160
161
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_INFO(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
AthAlgTool(const std::string &type, const std::string &name, const IInterface *parent)
Constructor with parameters:
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
TTree * m_nt
Pointer to the NTuple tree.
virtual unsigned int getNumberOfTreeRecords() const
std::string m_ntupleFileName
jobOption: Ntuple file and dir name
SG::ReadHandleKey< xAOD::EventInfo > m_evt
virtual StatusCode writeJetTruthData(const std::vector< Trk::GenParticleJet > &, const int &)
fill ntuple data for a given vector of truth jets
StatusCode finalize()
finalize
JetTruthNtupleTool(const std::string &, const std::string &, const IInterface *)
std::vector< unsigned int > * m_genParticleLinkIndex
std::vector< float > * m_mc_trackToJetAngle
std::string m_ntupleTreeName
jobOption: Ntuple tree name
std::pair< long int, long int > indices