ATLAS Offline Software
JetTruthNtupleTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 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  ITHistSvc *tHistSvc;
64  StatusCode sc = service("THistSvc", tHistSvc);
65  if (sc.isFailure()) {
66  ATH_MSG_ERROR ( "Unable to retrieve pointer to THistSvc" );
67  return sc;
68  }
69  // ---------------------------
70  // create tree and register it to THistSvc
71  m_nt = new TTree(TString(m_ntupleTreeName), "Track Validation JetTruth");
72  // NB: we must not delete the tree, this is done by THistSvc
73  std::string fullNtupleName = "/"+m_ntupleFileName+"/"+m_ntupleTreeName;
74  sc = tHistSvc->regTree(fullNtupleName, m_nt);
75  if (sc.isFailure()) {
76  ATH_MSG_ERROR ( "Unable to register TTree : " << m_ntupleTreeName );
77  return sc;
78  }
79 
80  //-----------------
81  // add items
82  // event info:
83  m_nt->Branch("RunNumber", &m_runNumber ); // Run number
84  m_nt->Branch("EventNumber", &m_eventNumber ); // Event number
85 
86  m_nt->Branch( "truth_jetMultiplicity", &m_mc_jetMultiplicity );
87  m_nt->Branch( "truth_jetSphericity", &m_mc_jetSphericity );
88  m_nt->Branch( "truth_jetThrust", &m_mc_jetThrust );
89  m_nt->Branch( "truth_jetEnergy", &m_mc_jetEnergy );
90  m_nt->Branch( "truth_jetMomentum", &m_mc_jetMomentum );
91 
92  m_genParticleLinkIndex = new std::vector<unsigned int>();
93  m_nt->Branch( "GenParticleLinkIndex", &m_genParticleLinkIndex);// link to particles
94  m_mc_trackToJetAngle = new std::vector<float>();
95  m_nt->Branch( "truth_trackToJetAngle", &m_mc_trackToJetAngle); // truth_trackToJetAngle
96 
97  ATH_MSG_VERBOSE ( "added items to ntuple" );
98 
99  return StatusCode::SUCCESS;
100 
101 }
102 
103 
108 
109  ATH_MSG_INFO ( "finalize() in " << name() );
110  delete m_genParticleLinkIndex;
111  delete m_mc_trackToJetAngle;
112 
113  return StatusCode::SUCCESS;
114 }
115 
120  const std::vector< Trk::GenParticleJet >& jets,
121  const int& nTruthRecordsAtCurrentEvent
122  // const std::vector<unsigned int>& /*particleToJetIndex*/
123  ) {
124  ATH_MSG_VERBOSE ( "in writeJetTruthData(...)" );
125 
126  // ---------------------------------------
127  // fill event data
129  if (!evt.isValid()) {
130  ATH_MSG_WARNING ( "Could not retrieve event info" );
131  return StatusCode::FAILURE;
132  }
133  m_runNumber=evt->runNumber();
134  m_eventNumber=evt->eventNumber();
135 
136  for (std::vector<Trk::GenParticleJet>::const_iterator itJet = jets.begin();
137  itJet < jets.end(); ++itJet ) {
138 
139  m_mc_jetMultiplicity = itJet->getNumParticles();
140  m_mc_jetMomentum = itJet->getMomentum().mag();
141  m_mc_jetEnergy = itJet->getEnergy();
142  m_mc_jetSphericity = itJet->getSphericity();
143  m_mc_jetThrust = itJet->getThrust();
144 
145  auto particles = itJet->getParticles();
146  m_mc_trackToJetAngle->reserve(particles.size());
147  m_genParticleLinkIndex->reserve(particles.size());
148  std::vector<int> indices = itJet->getIndicesInEvent();
149  std::vector<int>::iterator itIdx = indices.begin();
150  m_genParticleLinkIndex->clear();
151  m_mc_trackToJetAngle->clear();
152  for (auto itPrt = particles.begin();
153  itPrt < particles.end(); ++itPrt, ++itIdx) {
154 
155  HepGeom::Vector3D<double> tempMomentum((*itPrt)->momentum().px(),
156  (*itPrt)->momentum().py(),
157  (*itPrt)->momentum().pz());
158  // attention: push_back calls need a clear() to avoid growth with each entry
159  m_mc_trackToJetAngle->push_back((float)tempMomentum.angle(itJet->getMomentum()));
160  m_genParticleLinkIndex->push_back(nTruthRecordsAtCurrentEvent+1+(unsigned int)(*itIdx));
161  }
162  m_nt->Fill();
163  m_numberOfTreeEntries++;
164  }
165  return StatusCode::SUCCESS;
166 }
167 
168 
170  return m_numberOfTreeEntries;
171 }
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
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:119
GenParticle.h
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
Trk::JetTruthNtupleTool::initialize
StatusCode initialize()
Definition: JetTruthNtupleTool.cxx:57
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
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:107
TrackTruth.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
LArG4FSStartPointFilter.particles
list particles
Definition: LArG4FSStartPointFilter.py:84
GenParticleJet.h
declareProperty
#define declareProperty(n, p, h)
Definition: BaseFakeBkgTool.cxx:15
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:169