ATLAS Offline Software
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 
22 #include "AtlasHepMC/GenParticle.h"
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),
35  m_numberOfTreeEntries(0),
36  m_runNumber{},
37  m_eventNumber{},
38  m_genParticleLinkIndex(nullptr),
39  m_mc_trackToJetAngle(nullptr),
40  m_mc_jetMultiplicity{},
41  m_mc_jetSphericity{},
42  m_mc_jetThrust{},
43  m_mc_jetEnergy{},
44  m_mc_jetMomentum{}
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() );
103  delete m_genParticleLinkIndex;
104  delete m_mc_trackToJetAngle;
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();
156  m_numberOfTreeEntries++;
157  }
158  return StatusCode::SUCCESS;
159 }
160 
161 
163  return m_numberOfTreeEntries;
164 }
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
TrackParameters.h
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
Trk::indices
std::pair< long int, long int > indices
Definition: AlSymMatBase.h:24
LArG4FSStartPointFilter.evt
evt
Definition: LArG4FSStartPointFilter.py:42
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
Trk::JetTruthNtupleTool::writeJetTruthData
virtual StatusCode writeJetTruthData(const std::vector< Trk::GenParticleJet > &, const int &)
fill ntuple data for a given vector of truth jets
Definition: JetTruthNtupleTool.cxx:112
GenParticle.h
Trk::JetTruthNtupleTool::initialize
StatusCode initialize()
Definition: JetTruthNtupleTool.cxx:57
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
beamspotman.n
n
Definition: beamspotman.py:731
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
Trk::JetTruthNtupleTool::JetTruthNtupleTool
JetTruthNtupleTool(const std::string &, const std::string &, const IInterface *)
Definition: JetTruthNtupleTool.cxx:28
JetTruthNtupleTool.h
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
Trk::JetTruthNtupleTool::~JetTruthNtupleTool
~JetTruthNtupleTool()
Trk::JetTruthNtupleTool::finalize
StatusCode finalize()
finalize
Definition: JetTruthNtupleTool.cxx:100
TrackTruth.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
LArG4FSStartPointFilter.particles
list particles
Definition: LArG4FSStartPointFilter.py:84
GenParticleJet.h
defineDB.jets
list jets
Definition: JetTagCalibration/share/defineDB.py:24
AthAlgTool
Definition: AthAlgTool.h:26
Trk::JetTruthNtupleTool::getNumberOfTreeRecords
virtual unsigned int getNumberOfTreeRecords() const
Definition: JetTruthNtupleTool.cxx:162