ATLAS Offline Software
McAodTupleWriterTool.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 // McAodTupleWriterTool.cxx
7 // Implementation file for class McAodTupleWriterTool
8 // Author: S.Binet<binet@cern.ch>
10 
11 
12 // STL includes
13 //#include <sstream>
14 
15 // FrameWork includes
16 #include "GaudiKernel/ITHistSvc.h"
17 #include "Gaudi/Property.h"
18 #include "GaudiKernel/GaudiException.h"
19 
20 // ROOT includes
21 #include "TTree.h"
22 
23 // McParticleEvent includes
25 
26 // McParticleTools includes
27 #include "McAodTupleWriterTool.h"
28 
32 
36  const std::string& name,
37  const IInterface* parent ) :
39  m_tupleSvc ( "THistSvc", name ),
40  m_tuple ( nullptr )
41 {
42  //
43  // Property declaration
44  //
45 
46  declareProperty( "Output",
47  m_outputFileName = "mcaod.root",
48  "Name of the output file which will contain the McAod "
49  "tuple. Ex: mcaod.root" );
51  this );
52 
53  declareProperty( "OutputStream",
54  m_outputStreamName = "mcaod",
55  "Name of the stream which will contain the McAod tuple. "
56  "Ex: mcaod" );
57 
58  declareProperty( "TruthParticles",
59  m_truthParticlesName = "GEN_EVENT",
60  "Input location of the TruthParticleContainer to write "
61  "out." );
62 
63  declareInterface<IIOMcAodTool>(this);
64 }
65 
69 {
70  ATH_MSG_DEBUG("Calling destructor");
71 
72 }
73 
77 {
78  ATH_MSG_INFO("Initializing " << name() << "...");
79  // Get pointer to StoreGateSvc and cache it :
80  if ( !evtStore().retrieve().isSuccess() ) {
81  ATH_MSG_ERROR("Unable to retrieve pointer to StoreGateSvc");
82  return StatusCode::FAILURE;
83  }
84 
85  // Get pointer to ITHistSvc and cache it :
86  if ( !m_tupleSvc.retrieve().isSuccess() ) {
87  ATH_MSG_ERROR("Unable to retrieve pointer to ITHistSvc");
88  return StatusCode::FAILURE;
89  }
90 
91  // setup backend
93 
94  // book tuple
95  bookTuple();
96 
97  return StatusCode::SUCCESS;
98 }
99 
101 {
102  ATH_MSG_INFO("Finalizing " << name() << "...");
103  return StatusCode::SUCCESS;
104 }
105 
107 {
108  // retrieve the TruthParticleContainer
109  const TruthParticleContainer * mc = nullptr;
110  if ( evtStore()->retrieve( mc, m_truthParticlesName ).isFailure() ||
111  nullptr == mc ) {
112  ATH_MSG_ERROR("Could not retrieve a TruthParticleContainer at ["
113  << m_truthParticlesName << "] !!");
114  return StatusCode::FAILURE;
115  }
116 
117  if ( mc->empty() ) {
118  ATH_MSG_WARNING("TruthParticleContainer at [" << m_truthParticlesName
119  << "] is EMPTY !!");
120  return StatusCode::FAILURE;
121  }
122 
123  return write(mc);
124 }
125 
129 
133 
135 {
136  m_particles.m_nParticles = std::min<std::size_t>( s_nMax, mc->size() );
137  for ( std::size_t i = 0; i != m_particles.m_nParticles; ++i ) {
138  const TruthParticle * p = (*mc)[i];
139  m_particles.m_px [i] = p->px();
140  m_particles.m_py [i] = p->py();
141  m_particles.m_pz [i] = p->pz();
142  m_particles.m_m [i] = p->m();
143  m_particles.m_ene[i] = p->e();
144 
145  m_particles.m_pdgId[i] = p->pdgId();
146  m_particles.m_status[i] = p->status();
147  m_particles.m_barcode[i] = p->barcode();
148 
149  using namespace TruthParticleParameters;
150 
151  m_particles.m_etcone10[i] = p->etIsol(etcone10);
152  m_particles.m_etcone20[i] = p->etIsol(etcone20);
153  m_particles.m_etcone30[i] = p->etIsol(etcone30);
154  m_particles.m_etcone40[i] = p->etIsol(etcone40);
155  m_particles.m_etcone45[i] = p->etIsol(etcone );
156  m_particles.m_etcone50[i] = p->etIsol(etcone50);
157  m_particles.m_etcone60[i] = p->etIsol(etcone60);
158  m_particles.m_etcone70[i] = p->etIsol(etcone70);
159  }
160 
161  // commit event
162  m_tuple->Fill();
163 
164  return StatusCode::SUCCESS;
165 }
166 
170 
174 
178 
179 void McAodTupleWriterTool::setupBackend( Gaudi::Details::PropertyBase& /*m_outputFileName*/ )
180 {
181  const bool createIf = false;
182  IProperty * tSvc = nullptr;
183  if ( !service( m_tupleSvc.name(), tSvc, createIf ).isSuccess() ) {
184  ATH_MSG_ERROR("Could not retrieve THistSvc handle !!");
185  throw GaudiException( "Could not retrieve THistSvc",
186  name(),
187  StatusCode::FAILURE );
188  }
189 
190  const std::string streamName = m_outputStreamName.value();
191 
192  const std::string propName = "Output";
193  StringArrayProperty outputFileName;
194  outputFileName.assign( tSvc->getProperty( propName ) );
195  std::vector<std::string> updatedProp( outputFileName.value() );
196  updatedProp.push_back
197  ( streamName+" DATAFILE='"+m_outputFileName.value()+"' "
198  "TYP='ROOT' "
199  "OPT='RECREATE'"
200  );
201  outputFileName.set( updatedProp );
202  outputFileName.setName( propName );
203  if ( !tSvc->setProperty( outputFileName ).isSuccess() ) {
204  ATH_MSG_ERROR("Could not configure the THistSvc's output filename ["
205  << m_outputFileName.value() << "] !!");
206  throw GaudiException( "Could not configure THistSvc output file !!",
207  name(),
208  StatusCode::FAILURE );
209  }
210 
211  }
212 
214 {
215  delete m_tuple;
216  const std::string streamName = m_outputStreamName.value();
217  TTree* t = new TTree("mcaod","McAod validation tuple");
218  if ( !m_tupleSvc->regTree( "/" + streamName + "/mcaod", t ).isSuccess() ) {
219  ATH_MSG_ERROR("Could not register McAod validation tuple !!");
220  delete t; t = nullptr;
221  throw GaudiException( "Could not register McAod validation tuple !!",
222  name(),
223  StatusCode::FAILURE );
224  }
225 
226  // booking branches
227  t->Branch( "nParts", &m_particles.m_nParticles, "nParts/i" );
228 
229  t->Branch( "px", m_particles.m_px.data(), "px[nParts]/D" );
230  t->Branch( "py", m_particles.m_py.data(), "py[nParts]/D" );
231  t->Branch( "pz", m_particles.m_pz.data(), "pz[nParts]/D" );
232  t->Branch( "m", m_particles.m_m.data(), "m[nParts]/D" );
233  t->Branch( "e", m_particles.m_ene.data(), "e[nParts]/D" );
234 
235  t->Branch( "pdgId", m_particles.m_pdgId.data(), "pdgId[nParts]/I" );
236  t->Branch( "sc", m_particles.m_status.data(), "sc[nParts]/I" );
237  t->Branch( "bc", m_particles.m_barcode.data(), "bc[nParts]/I" );
238 
239  t->Branch( "etcone10", m_particles.m_etcone10.data(), "etcone10[nParts]/D" );
240  t->Branch( "etcone20", m_particles.m_etcone20.data(), "etcone20[nParts]/D" );
241  t->Branch( "etcone30", m_particles.m_etcone30.data(), "etcone30[nParts]/D" );
242  t->Branch( "etcone40", m_particles.m_etcone40.data(), "etcone40[nParts]/D" );
243  t->Branch( "etcone45", m_particles.m_etcone45.data(), "etcone45[nParts]/D" );
244  t->Branch( "etcone50", m_particles.m_etcone50.data(), "etcone50[nParts]/D" );
245  t->Branch( "etcone60", m_particles.m_etcone60.data(), "etcone60[nParts]/D" );
246  t->Branch( "etcone70", m_particles.m_etcone70.data(), "etcone70[nParts]/D" );
247 
248  m_tuple = t;
249 }
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
McAodTupleWriterTool::McAodParticles::m_status
std::array< int, s_nMax > m_status
Definition: McAodTupleWriterTool.h:103
McAodTupleWriterTool::setupBackend
void setupBackend(Gaudi::Details::PropertyBase &outputFileName)
Method to configure the back-end to write out the HepMC::GenEvent.
Definition: McAodTupleWriterTool.cxx:179
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
McAodTupleWriterTool.h
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
McAodTupleWriterTool::McAodParticles::m_etcone50
std::array< double, s_nMax > m_etcone50
Definition: McAodTupleWriterTool.h:111
TruthParticleContainer.h
McAodTupleWriterTool::McAodParticles::m_etcone20
std::array< double, s_nMax > m_etcone20
Definition: McAodTupleWriterTool.h:107
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
McAodTupleWriterTool::McAodParticles::m_etcone60
std::array< double, s_nMax > m_etcone60
Definition: McAodTupleWriterTool.h:112
McAodTupleWriterTool::McAodParticles::m_pdgId
std::array< int, s_nMax > m_pdgId
Definition: McAodTupleWriterTool.h:102
McAodTupleWriterTool::m_outputFileName
StringProperty m_outputFileName
Name of the output tuple file.
Definition: McAodTupleWriterTool.h:126
TruthParticle
Definition: PhysicsAnalysis/TruthParticleID/McParticleEvent/McParticleEvent/TruthParticle.h:58
McAodTupleWriterTool::McAodParticles::m_py
std::array< double, s_nMax > m_py
Definition: McAodTupleWriterTool.h:98
xAOD::Iso::etcone40
@ etcone40
Definition: IsolationType.h:34
McAodTupleWriterTool::m_tuple
TTree * m_tuple
cached pointer to the tuple
Definition: McAodTupleWriterTool.h:132
xAOD::Iso::etcone
@ etcone
Calorimeter isolation.
Definition: IsolationFlavour.h:19
McAodTupleWriterTool::McAodParticles::m_etcone70
std::array< double, s_nMax > m_etcone70
Definition: McAodTupleWriterTool.h:113
xAOD::Iso::etcone30
@ etcone30
Definition: IsolationType.h:33
TruthParticleParameters
Definition: TruthParticleParamDefs.h:17
McAodTupleWriterTool::McAodParticles::m_barcode
std::array< int, s_nMax > m_barcode
Definition: McAodTupleWriterTool.h:104
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
TruthParticleContainer
Definition: PhysicsAnalysis/TruthParticleID/McParticleEvent/McParticleEvent/TruthParticleContainer.h:42
McAodTupleWriterTool::McAodParticles::m_pz
std::array< double, s_nMax > m_pz
Definition: McAodTupleWriterTool.h:99
AthCommonDataStore< AthCommonMsg< AlgTool > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
McAodTupleWriterTool::finalize
StatusCode finalize()
Definition: McAodTupleWriterTool.cxx:100
mc
Definition: mc.PG_single_nu_valid.py:1
McAodTupleWriterTool::m_outputStreamName
StringProperty m_outputStreamName
Name of the output tuple stream.
Definition: McAodTupleWriterTool.h:129
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
TruthParticleParameters::etcone70
@ etcone70
Definition: TruthParticleParamDefs.h:28
McAodTupleWriterTool::McAodTupleWriterTool
McAodTupleWriterTool()
Default constructor:
lumiFormat.i
int i
Definition: lumiFormat.py:92
McAodTupleWriterTool::s_nMax
static const int s_nMax
maximum number of particles per event
Definition: McAodTupleWriterTool.h:92
McAodTupleWriterTool::McAodParticles::m_ene
std::array< double, s_nMax > m_ene
Definition: McAodTupleWriterTool.h:101
xAOD::Iso::etcone20
@ etcone20
Calorimeter isolation.
Definition: IsolationType.h:32
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
McAodTupleWriterTool::bookTuple
void bookTuple()
book the tuple
Definition: McAodTupleWriterTool.cxx:213
McAodTupleWriterTool::McAodParticles::m_etcone10
std::array< double, s_nMax > m_etcone10
Definition: McAodTupleWriterTool.h:106
McAodTupleWriterTool::McAodParticles::m_m
std::array< double, s_nMax > m_m
Definition: McAodTupleWriterTool.h:100
test_pyathena.parent
parent
Definition: test_pyathena.py:15
TruthParticleParameters::etcone60
@ etcone60
Definition: TruthParticleParamDefs.h:27
McAodTupleWriterTool::initialize
StatusCode initialize()
Athena Algorithm's Hooks.
Definition: McAodTupleWriterTool.cxx:76
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
L1CaloPhase1Monitoring.propName
propName
Definition: L1CaloPhase1Monitoring.py:349
McAodTupleWriterTool::~McAodTupleWriterTool
virtual ~McAodTupleWriterTool()
Destructor:
Definition: McAodTupleWriterTool.cxx:68
McAodTupleWriterTool::m_tupleSvc
ServiceHandle< ITHistSvc > m_tupleSvc
Pointer to @ ITHistSvc.
Definition: McAodTupleWriterTool.h:119
McAodTupleWriterTool::McAodParticles::m_etcone40
std::array< double, s_nMax > m_etcone40
Definition: McAodTupleWriterTool.h:109
McAodTupleWriterTool::McAodParticles::m_nParticles
unsigned int m_nParticles
Definition: McAodTupleWriterTool.h:96
AthenaPoolExample_Copy.streamName
string streamName
Definition: AthenaPoolExample_Copy.py:39
McAodTupleWriterTool::m_particles
McAodParticles m_particles
our cached particles
Definition: McAodTupleWriterTool.h:116
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
McAodTupleWriterTool::m_truthParticlesName
StringProperty m_truthParticlesName
Location of the TruthParticleContainer to be written out.
Definition: McAodTupleWriterTool.h:123
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
McAodTupleWriterTool::write
StatusCode write(const TruthParticleContainer *mcAod)
Process the TruthParticleContainer through the I/O backend.
Definition: McAodTupleWriterTool.cxx:134
McAodTupleWriterTool::McAodParticles::m_px
std::array< double, s_nMax > m_px
Definition: McAodTupleWriterTool.h:97
McAodTupleWriterTool::McAodParticles::m_etcone30
std::array< double, s_nMax > m_etcone30
Definition: McAodTupleWriterTool.h:108
AthAlgTool
Definition: AthAlgTool.h:26
McAodTupleWriterTool::McAodParticles::m_etcone45
std::array< double, s_nMax > m_etcone45
Definition: McAodTupleWriterTool.h:110
python.Dumpers.etcone10
int etcone10
Definition: Dumpers.py:41
McAodTupleWriterTool::execute
StatusCode execute()
Definition: McAodTupleWriterTool.cxx:106
AthenaPoolExample_Copy.outputFileName
string outputFileName
Definition: AthenaPoolExample_Copy.py:40
TruthParticleParameters::etcone50
@ etcone50
Definition: TruthParticleParamDefs.h:26