ATLAS Offline Software
Loading...
Searching...
No Matches
McAodTupleWriterTool.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 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
28
32
36 const std::string& name,
37 const IInterface* parent ) :
38 AthAlgTool( type, name, 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
179void McAodTupleWriterTool::setupBackend( Gaudi::Details::PropertyBase& /*m_outputFileName*/ )
180{
181 if ( m_tupleSvc.retrieve().isFailure() ) {
182 ATH_MSG_ERROR("Could not retrieve THistSvc handle !!");
183 throw GaudiException( "Could not retrieve THistSvc",
184 name(),
185 StatusCode::FAILURE );
186 }
187
188 SmartIF<IProperty> tSvc(m_tupleSvc.get());
189 const std::string streamName = m_outputStreamName.value();
190
191 const std::string propName = "Output";
192 StringArrayProperty outputFileName;
193 outputFileName.assign( tSvc->getProperty( propName ) );
194 std::vector<std::string> updatedProp( outputFileName.value() );
195 updatedProp.push_back
196 ( streamName+" DATAFILE='"+m_outputFileName.value()+"' "
197 "TYP='ROOT' "
198 "OPT='RECREATE'"
199 );
200 outputFileName.set( updatedProp );
201 outputFileName.setName( std::move(propName) );
202 if ( !tSvc->setProperty( outputFileName ).isSuccess() ) {
203 ATH_MSG_ERROR("Could not configure the THistSvc's output filename ["
204 << m_outputFileName.value() << "] !!");
205 throw GaudiException( "Could not configure THistSvc output file !!",
206 name(),
207 StatusCode::FAILURE );
208 }
209
210 }
211
213{
214 delete m_tuple;
215 const std::string streamName = m_outputStreamName.value();
216 TTree* t = new TTree("mcaod","McAod validation tuple");
217 if ( !m_tupleSvc->regTree( "/" + streamName + "/mcaod", t ).isSuccess() ) {
218 ATH_MSG_ERROR("Could not register McAod validation tuple !!");
219 delete t; t = nullptr;
220 throw GaudiException( "Could not register McAod validation tuple !!",
221 name(),
222 StatusCode::FAILURE );
223 }
224
225 // booking branches
226 t->Branch( "nParts", &m_particles.m_nParticles, "nParts/i" );
227
228 t->Branch( "px", m_particles.m_px.data(), "px[nParts]/D" );
229 t->Branch( "py", m_particles.m_py.data(), "py[nParts]/D" );
230 t->Branch( "pz", m_particles.m_pz.data(), "pz[nParts]/D" );
231 t->Branch( "m", m_particles.m_m.data(), "m[nParts]/D" );
232 t->Branch( "e", m_particles.m_ene.data(), "e[nParts]/D" );
233
234 t->Branch( "pdgId", m_particles.m_pdgId.data(), "pdgId[nParts]/I" );
235 t->Branch( "sc", m_particles.m_status.data(), "sc[nParts]/I" );
236 t->Branch( "bc", m_particles.m_barcode.data(), "bc[nParts]/I" );
237
238 t->Branch( "etcone10", m_particles.m_etcone10.data(), "etcone10[nParts]/D" );
239 t->Branch( "etcone20", m_particles.m_etcone20.data(), "etcone20[nParts]/D" );
240 t->Branch( "etcone30", m_particles.m_etcone30.data(), "etcone30[nParts]/D" );
241 t->Branch( "etcone40", m_particles.m_etcone40.data(), "etcone40[nParts]/D" );
242 t->Branch( "etcone45", m_particles.m_etcone45.data(), "etcone45[nParts]/D" );
243 t->Branch( "etcone50", m_particles.m_etcone50.data(), "etcone50[nParts]/D" );
244 t->Branch( "etcone60", m_particles.m_etcone60.data(), "etcone60[nParts]/D" );
245 t->Branch( "etcone70", m_particles.m_etcone70.data(), "etcone70[nParts]/D" );
246
247 m_tuple = t;
248}
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
static const std::string outputFileName
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)
ServiceHandle< StoreGateSvc > & evtStore()
StatusCode write(const TruthParticleContainer *mcAod)
Process the TruthParticleContainer through the I/O backend.
virtual ~McAodTupleWriterTool()
Destructor:
McAodParticles m_particles
our cached particles
TTree * m_tuple
cached pointer to the tuple
McAodTupleWriterTool()
Default constructor:
StringProperty m_outputStreamName
Name of the output tuple stream.
static const int s_nMax
maximum number of particles per event
StringProperty m_outputFileName
Name of the output tuple file.
ServiceHandle< ITHistSvc > m_tupleSvc
Pointer to @ ITHistSvc.
StringProperty m_truthParticlesName
Location of the TruthParticleContainer to be written out.
void setupBackend(Gaudi::Details::PropertyBase &outputFileName)
Method to configure the back-end to write out the HepMC::GenEvent.
void bookTuple()
book the tuple
StatusCode initialize()
Athena Algorithm's Hooks.