ATLAS Offline Software
HepMcTupleWriterTool.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 // HepMcTupleWriterTool.cxx
7 // Implementation file for class HepMcTupleWriterTool
8 // Author: S.Binet<binet@cern.ch>
10 
11 // FrameWork includes
12 #include "GaudiKernel/ITHistSvc.h"
13 #include "Gaudi/Property.h"
14 #include "GaudiKernel/GaudiException.h"
15 
16 // ROOT includes
17 #include "TTree.h"
18 
19 // CLHEP includes
21 #include "CLHEP/Units/SystemOfUnits.h"
22 
23 // McParticleTools includes
24 #include "HepMcTupleWriterTool.h"
25 
29  const std::string& name,
30  const IInterface* parent ) :
32  m_tupleSvc ( "THistSvc", name ),
33  m_tuple ( nullptr )
34 {
35  //
36  // Property declaration
37  //
38 
39  declareProperty( "Output",
40  m_outputFileName = "hepmc.root",
41  "Name of the output file which will contain the HepMC tuple"
42  "\nEx: hepmc.root" );
43  m_outputFileName.declareUpdateHandler( &HepMcTupleWriterTool::setupBackend, this );
44 
45  declareProperty( "OutputStream",
46  m_outputStreamName = "hepmc",
47  "Name of the stream which will contain the HepMC tuple"
48  "\nEx: hepmc" );
49 
50  declareProperty( "McEvents",
51  m_mcEventsName = "GEN_EVENT",
52  "Input location of the McEventCollection to write out" );
53 
54  declareInterface<IIOHepMcTool>(this);
55 }
56 
60 {
61  ATH_MSG_DEBUG("Calling destructor");
62 }
63 
67 {
68  ATH_MSG_INFO("Initializing " << name() << "...");
69 
70  // Get pointer to StoreGateSvc and cache it :
71  if ( !evtStore().retrieve().isSuccess() ) {
72  ATH_MSG_ERROR("Unable to retrieve pointer to StoreGateSvc");
73  return StatusCode::FAILURE;
74  }
75 
76  // Get pointer to ITHistSvc and cache it :
77  if ( !m_tupleSvc.retrieve().isSuccess() ) {
78  ATH_MSG_ERROR("Unable to retrieve pointer to ITHistSvc");
79  return StatusCode::FAILURE;
80  }
81 
82  // setup backend
84 
85  // book tuple
86  bookTuple();
87 
88  return StatusCode::SUCCESS;
89 }
90 
92 {
93  ATH_MSG_INFO("Finalizing " << name() << "...");
94  return StatusCode::SUCCESS;
95 }
96 
98 {
99  // retrieve the McEventCollection
100  const McEventCollection * mcEvts = nullptr;
101  if ( evtStore()->retrieve( mcEvts, m_mcEventsName ).isFailure() || nullptr == mcEvts ) {
102  ATH_MSG_ERROR("Could not retrieve a McEventCollection at [" << m_mcEventsName << "] !!");
103  return StatusCode::FAILURE;
104  }
105 
106  if ( mcEvts->empty() ) {
107  ATH_MSG_WARNING("McEventCollection at ["<<m_mcEventsName<<"] is EMPTY !!");
108  return StatusCode::FAILURE;
109  }
110 
111  const HepMC::GenEvent * evt = mcEvts->front();
112  if ( !evt ) {
113  ATH_MSG_ERROR("Retrieved NULL pointer to HepMC::GenEvent !!");
114  return StatusCode::FAILURE;
115  }
116 
117  return write(evt);
118 }
119 
123 
124 StatusCode HepMcTupleWriterTool::write( const HepMC::GenEvent* evt )
125 {
126  m_particles.m_nParticles = std::min<std::size_t>( s_nMax, evt->particles_size() );
127  std::size_t i = 0;
128  for (const auto& p: *evt)
129  {
130  if (i == static_cast<std::size_t>(m_particles.m_nParticles)) break;
131  i++;
132  const HepMC::FourVector mom = p->momentum();
133  m_particles.m_px [i] = mom.px();
134  m_particles.m_py [i] = mom.py();
135  m_particles.m_pz [i] = mom.pz();
136  m_particles.m_m [i] = mom.m();
137  m_particles.m_ene[i] = mom.e();
138 
139  m_particles.m_pdgId[i] = p->pdg_id();
140  m_particles.m_status[i] = p->status();
142  }
143  // commit event
144  m_tuple->Fill();
145 
146  return StatusCode::SUCCESS;
147 }
148 
152 
153 void HepMcTupleWriterTool::setupBackend( Gaudi::Details::PropertyBase& /*m_outputFileName*/ )
154 {
155  SmartIF<IProperty> tSvc{m_tupleSvc.get()};
156  if ( !tSvc ) {
157  ATH_MSG_ERROR("Could not retrieve THistSvc handle !!");
158  throw GaudiException( "Could not retrieve THistSvc", name(), StatusCode::FAILURE );
159  }
160 
161  const std::string streamName = m_outputStreamName.value();
162 
163  const std::string propName = "Output";
164  StringArrayProperty outputFileName;
165  outputFileName.assign( tSvc->getProperty( propName ) );
166  std::vector<std::string> updatedProp( outputFileName.value() );
167  updatedProp.push_back
168  ( streamName+" DATAFILE='"+m_outputFileName.value()+"' "
169  "TYP='ROOT' "
170  "OPT='RECREATE'" );
171  outputFileName.set( updatedProp );
172  outputFileName.setName( propName );
173  if ( !tSvc->setProperty( outputFileName ).isSuccess() ) {
174  ATH_MSG_ERROR("Could not configure the THistSvc's output filename ["<< m_outputFileName.value() << "] !!");
175  throw GaudiException( "Could not configure THistSvc output file !!", name(), StatusCode::FAILURE );
176  }
177 
178  }
179 
181 {
182  const std::string streamName = m_outputStreamName.value();
183  TTree* t = new TTree("hepmc","HepMC validation tuple");
184  if ( !m_tupleSvc->regTree( "/" + streamName + "/hepmc", t ).isSuccess() ) {
185  ATH_MSG_ERROR("Could not register HepMC validation tuple !!");
186  delete t; t = nullptr;
187  throw GaudiException( "Could not register HepMC validation tuple !!", name(), StatusCode::FAILURE );
188  }
189 
190  // booking branches
191  t->Branch( "nParts", &m_particles.m_nParticles, "nParts/I" );
192 
193  t->Branch( "px", m_particles.m_px.data(), "px[nParts]/D" );
194  t->Branch( "py", m_particles.m_py.data(), "py[nParts]/D" );
195  t->Branch( "pz", m_particles.m_pz.data(), "pz[nParts]/D" );
196  t->Branch( "m", m_particles.m_m.data(), "m[nParts]/D" );
197  t->Branch( "e", m_particles.m_ene.data(), "e[nParts]/D" );
198 
199  t->Branch( "pdgId", m_particles.m_pdgId.data(), "pdgId[nParts]/I" );
200  t->Branch( "sc", m_particles.m_status.data(), "sc[nParts]/I" );
201  t->Branch( "bc", m_particles.m_barcode.data(), "bc[nParts]/I" );
202 
203  m_tuple = t;
204 }
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
HepMcTupleWriterTool::m_outputStreamName
StringProperty m_outputStreamName
Name of the output tuple stream.
Definition: HepMcTupleWriterTool.h:114
HepMcTupleWriterTool::HepMcParticles::m_ene
std::array< double, s_nMax > m_ene
Definition: HepMcTupleWriterTool.h:93
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
HepMcTupleWriterTool::m_outputFileName
StringProperty m_outputFileName
Name of the output tuple file.
Definition: HepMcTupleWriterTool.h:111
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
HepMcTupleWriterTool::HepMcParticles::m_pdgId
std::array< int, s_nMax > m_pdgId
Definition: HepMcTupleWriterTool.h:94
HepMcTupleWriterTool::m_particles
HepMcParticles m_particles
our cached particles
Definition: HepMcTupleWriterTool.h:99
LArG4FSStartPointFilter.evt
evt
Definition: LArG4FSStartPointFilter.py:42
HepMcTupleWriterTool::HepMcTupleWriterTool
HepMcTupleWriterTool()
Default constructor:
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
HepMcTupleWriterTool::bookTuple
void bookTuple()
book the tuple
Definition: HepMcTupleWriterTool.cxx:180
HepMcTupleWriterTool::m_tupleSvc
ServiceHandle< ITHistSvc > m_tupleSvc
Pointer to @ ITHistSvc.
Definition: HepMcTupleWriterTool.h:102
HepMcTupleWriterTool::s_nMax
static const int s_nMax
maximum number of particles per event
Definition: HepMcTupleWriterTool.h:84
HepMcTupleWriterTool::HepMcParticles::m_py
std::array< double, s_nMax > m_py
Definition: HepMcTupleWriterTool.h:90
AthCommonDataStore< AthCommonMsg< AlgTool > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
HepMcTupleWriterTool::execute
StatusCode execute()
Definition: HepMcTupleWriterTool.cxx:97
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
ParticleGun_EoverP_Config.mom
mom
Definition: ParticleGun_EoverP_Config.py:63
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
HepMcTupleWriterTool::HepMcParticles::m_nParticles
int m_nParticles
Definition: HepMcTupleWriterTool.h:88
McEventCollection.h
lumiFormat.i
int i
Definition: lumiFormat.py:85
HepMcTupleWriterTool::setupBackend
void setupBackend(Gaudi::Details::PropertyBase &outputFileName)
Method to configure the back-end to write out the HepMC::GenEvent.
Definition: HepMcTupleWriterTool.cxx:153
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
HepMC::barcode
int barcode(const T *p)
Definition: Barcode.h:16
DataVector::front
const T * front() const
Access the first element in the collection as an rvalue.
test_pyathena.parent
parent
Definition: test_pyathena.py:15
McEventCollection
This defines the McEventCollection, which is really just an ObjectVector of McEvent objects.
Definition: McEventCollection.h:33
HepMcTupleWriterTool::m_mcEventsName
StringProperty m_mcEventsName
Location of the McEventCollection to be written out If there is more than 1 HepMC::GenEvent in the Mc...
Definition: HepMcTupleWriterTool.h:108
HepMcTupleWriterTool::HepMcParticles::m_m
std::array< double, s_nMax > m_m
Definition: HepMcTupleWriterTool.h:92
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
L1CaloPhase1Monitoring.propName
propName
Definition: L1CaloPhase1Monitoring.py:451
HepMcTupleWriterTool::finalize
StatusCode finalize()
Definition: HepMcTupleWriterTool.cxx:91
AthenaPoolExample_Copy.streamName
string streamName
Definition: AthenaPoolExample_Copy.py:39
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
HepMcTupleWriterTool::HepMcParticles::m_pz
std::array< double, s_nMax > m_pz
Definition: HepMcTupleWriterTool.h:91
HepMcTupleWriterTool::m_tuple
TTree * m_tuple
cached pointer to the tuple
Definition: HepMcTupleWriterTool.h:117
HepMcTupleWriterTool::write
StatusCode write(const HepMC::GenEvent *evt)
Process the HepMC::GenEvent through the I/O backend.
Definition: HepMcTupleWriterTool.cxx:124
HepMcTupleWriterTool::HepMcParticles::m_barcode
std::array< int, s_nMax > m_barcode
Definition: HepMcTupleWriterTool.h:96
AthAlgTool
Definition: AthAlgTool.h:26
HepMcTupleWriterTool::HepMcParticles::m_px
std::array< double, s_nMax > m_px
Definition: HepMcTupleWriterTool.h:89
HepMcTupleWriterTool::~HepMcTupleWriterTool
virtual ~HepMcTupleWriterTool()
Destructor:
Definition: HepMcTupleWriterTool.cxx:59
AthenaPoolExample_Copy.outputFileName
string outputFileName
Definition: AthenaPoolExample_Copy.py:40
DataVector::empty
bool empty() const noexcept
Returns true if the collection is empty.
HepMcTupleWriterTool::HepMcParticles::m_status
std::array< int, s_nMax > m_status
Definition: HepMcTupleWriterTool.h:95
HepMcTupleWriterTool.h
HepMcTupleWriterTool::initialize
StatusCode initialize()
Athena Algorithm's Hooks.
Definition: HepMcTupleWriterTool.cxx:66