ATLAS Offline Software
Classes | Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
HepMcTupleWriterTool Class Reference

#include <HepMcTupleWriterTool.h>

Inheritance diagram for HepMcTupleWriterTool:
Collaboration diagram for HepMcTupleWriterTool:

Classes

struct  HepMcParticles
 A simple representation of a HepMc particle. More...
 

Public Member Functions

 HepMcTupleWriterTool (const std::string &type, const std::string &name, const IInterface *parent)
 Constructor with parameters: More...
 
virtual ~HepMcTupleWriterTool ()
 Destructor: More...
 
StatusCode initialize ()
 Athena Algorithm's Hooks. More...
 
StatusCode execute ()
 
StatusCode finalize ()
 
StatusCode write (const HepMC::GenEvent *evt)
 Process the HepMC::GenEvent through the I/O backend. More...
 

Protected Member Functions

 HepMcTupleWriterTool ()
 Default constructor: More...
 
void setupBackend (Gaudi::Details::PropertyBase &outputFileName)
 Method to configure the back-end to write out the HepMC::GenEvent. More...
 
void bookTuple ()
 book the tuple More...
 

Protected Attributes

HepMcParticles m_particles {}
 our cached particles More...
 
ServiceHandle< ITHistSvc > m_tupleSvc
 Pointer to @ ITHistSvc. More...
 
StringProperty m_mcEventsName
 Location of the McEventCollection to be written out If there is more than 1 HepMC::GenEvent in the McEventCollection we will send warning messages, and just write the first one. More...
 
StringProperty m_outputFileName
 Name of the output tuple file. More...
 
StringProperty m_outputStreamName
 Name of the output tuple stream. More...
 
TTree * m_tuple
 cached pointer to the tuple More...
 

Static Protected Attributes

static const int s_nMax = 2000
 maximum number of particles per event More...
 

Detailed Description

Definition at line 30 of file HepMcTupleWriterTool.h.

Constructor & Destructor Documentation

◆ HepMcTupleWriterTool() [1/2]

HepMcTupleWriterTool::HepMcTupleWriterTool ( const std::string &  type,
const std::string &  name,
const IInterface *  parent 
)

Constructor with parameters:

Constructors.

Definition at line 28 of file HepMcTupleWriterTool.cxx.

30  :
31  base_class( type, name, 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 }

◆ ~HepMcTupleWriterTool()

HepMcTupleWriterTool::~HepMcTupleWriterTool ( )
virtual

Destructor:

Destructor.

Definition at line 57 of file HepMcTupleWriterTool.cxx.

58 {
59  ATH_MSG_DEBUG("Calling destructor");
60 }

◆ HepMcTupleWriterTool() [2/2]

HepMcTupleWriterTool::HepMcTupleWriterTool ( )
protected

Default constructor:

Member Function Documentation

◆ bookTuple()

void HepMcTupleWriterTool::bookTuple ( )
protected

book the tuple

Definition at line 178 of file HepMcTupleWriterTool.cxx.

179 {
180  const std::string streamName = m_outputStreamName.value();
181  TTree* t = new TTree("hepmc","HepMC validation tuple");
182  if ( !m_tupleSvc->regTree( "/" + streamName + "/hepmc", t ).isSuccess() ) {
183  ATH_MSG_ERROR("Could not register HepMC validation tuple !!");
184  delete t; t = nullptr;
185  throw GaudiException( "Could not register HepMC validation tuple !!", name(), StatusCode::FAILURE );
186  }
187 
188  // booking branches
189  t->Branch( "nParts", &m_particles.m_nParticles, "nParts/I" );
190 
191  t->Branch( "px", m_particles.m_px.data(), "px[nParts]/D" );
192  t->Branch( "py", m_particles.m_py.data(), "py[nParts]/D" );
193  t->Branch( "pz", m_particles.m_pz.data(), "pz[nParts]/D" );
194  t->Branch( "m", m_particles.m_m.data(), "m[nParts]/D" );
195  t->Branch( "e", m_particles.m_ene.data(), "e[nParts]/D" );
196 
197  t->Branch( "pdgId", m_particles.m_pdgId.data(), "pdgId[nParts]/I" );
198  t->Branch( "sc", m_particles.m_status.data(), "sc[nParts]/I" );
199  t->Branch( "bc", m_particles.m_barcode.data(), "bc[nParts]/I" );
200 
201  m_tuple = t;
202 }

◆ execute()

StatusCode HepMcTupleWriterTool::execute ( )

Definition at line 95 of file HepMcTupleWriterTool.cxx.

96 {
97  // retrieve the McEventCollection
98  const McEventCollection * mcEvts = nullptr;
99  if ( evtStore()->retrieve( mcEvts, m_mcEventsName ).isFailure() || nullptr == mcEvts ) {
100  ATH_MSG_ERROR("Could not retrieve a McEventCollection at [" << m_mcEventsName << "] !!");
101  return StatusCode::FAILURE;
102  }
103 
104  if ( mcEvts->empty() ) {
105  ATH_MSG_WARNING("McEventCollection at ["<<m_mcEventsName<<"] is EMPTY !!");
106  return StatusCode::FAILURE;
107  }
108 
109  const HepMC::GenEvent * evt = mcEvts->front();
110  if ( !evt ) {
111  ATH_MSG_ERROR("Retrieved NULL pointer to HepMC::GenEvent !!");
112  return StatusCode::FAILURE;
113  }
114 
115  return write(evt);
116 }

◆ finalize()

StatusCode HepMcTupleWriterTool::finalize ( )

Definition at line 89 of file HepMcTupleWriterTool.cxx.

90 {
91  ATH_MSG_INFO("Finalizing " << name() << "...");
92  return StatusCode::SUCCESS;
93 }

◆ initialize()

StatusCode HepMcTupleWriterTool::initialize ( )

Athena Algorithm's Hooks.

Definition at line 64 of file HepMcTupleWriterTool.cxx.

65 {
66  ATH_MSG_INFO("Initializing " << name() << "...");
67 
68  // Get pointer to StoreGateSvc and cache it :
69  if ( !evtStore().retrieve().isSuccess() ) {
70  ATH_MSG_ERROR("Unable to retrieve pointer to StoreGateSvc");
71  return StatusCode::FAILURE;
72  }
73 
74  // Get pointer to ITHistSvc and cache it :
75  if ( !m_tupleSvc.retrieve().isSuccess() ) {
76  ATH_MSG_ERROR("Unable to retrieve pointer to ITHistSvc");
77  return StatusCode::FAILURE;
78  }
79 
80  // setup backend
82 
83  // book tuple
84  bookTuple();
85 
86  return StatusCode::SUCCESS;
87 }

◆ setupBackend()

void HepMcTupleWriterTool::setupBackend ( Gaudi::Details::PropertyBase &  outputFileName)
protected

Method to configure the back-end to write out the HepMC::GenEvent.

Non-const methods:

Definition at line 151 of file HepMcTupleWriterTool.cxx.

152 {
153  SmartIF<IProperty> tSvc{m_tupleSvc.get()};
154  if ( !tSvc ) {
155  ATH_MSG_ERROR("Could not retrieve THistSvc handle !!");
156  throw GaudiException( "Could not retrieve THistSvc", name(), StatusCode::FAILURE );
157  }
158 
159  const std::string streamName = m_outputStreamName.value();
160 
161  const std::string propName = "Output";
162  StringArrayProperty outputFileName;
163  outputFileName.assign( tSvc->getProperty( propName ) );
164  std::vector<std::string> updatedProp( outputFileName.value() );
165  updatedProp.push_back
166  ( streamName+" DATAFILE='"+m_outputFileName.value()+"' "
167  "TYP='ROOT' "
168  "OPT='RECREATE'" );
169  outputFileName.set( updatedProp );
170  outputFileName.setName( propName );
171  if ( !tSvc->setProperty( outputFileName ).isSuccess() ) {
172  ATH_MSG_ERROR("Could not configure the THistSvc's output filename ["<< m_outputFileName.value() << "] !!");
173  throw GaudiException( "Could not configure THistSvc output file !!", name(), StatusCode::FAILURE );
174  }
175 
176  }

◆ write()

StatusCode HepMcTupleWriterTool::write ( const HepMC::GenEvent *  evt)

Process the HepMC::GenEvent through the I/O backend.

Non-const methods:

Definition at line 122 of file HepMcTupleWriterTool.cxx.

123 {
124  m_particles.m_nParticles = std::min<std::size_t>( s_nMax, evt->particles_size() );
125  std::size_t i = 0;
126  for (const auto& p: *evt)
127  {
128  if (i == static_cast<std::size_t>(m_particles.m_nParticles)) break;
129  i++;
130  const HepMC::FourVector mom = p->momentum();
131  m_particles.m_px [i] = mom.px();
132  m_particles.m_py [i] = mom.py();
133  m_particles.m_pz [i] = mom.pz();
134  m_particles.m_m [i] = mom.m();
135  m_particles.m_ene[i] = mom.e();
136 
137  m_particles.m_pdgId[i] = p->pdg_id();
138  m_particles.m_status[i] = p->status();
140  }
141  // commit event
142  m_tuple->Fill();
143 
144  return StatusCode::SUCCESS;
145 }

Member Data Documentation

◆ m_mcEventsName

StringProperty HepMcTupleWriterTool::m_mcEventsName
protected

Location of the McEventCollection to be written out If there is more than 1 HepMC::GenEvent in the McEventCollection we will send warning messages, and just write the first one.

Definition at line 108 of file HepMcTupleWriterTool.h.

◆ m_outputFileName

StringProperty HepMcTupleWriterTool::m_outputFileName
protected

Name of the output tuple file.

Definition at line 111 of file HepMcTupleWriterTool.h.

◆ m_outputStreamName

StringProperty HepMcTupleWriterTool::m_outputStreamName
protected

Name of the output tuple stream.

Definition at line 114 of file HepMcTupleWriterTool.h.

◆ m_particles

HepMcParticles HepMcTupleWriterTool::m_particles {}
protected

our cached particles

Definition at line 99 of file HepMcTupleWriterTool.h.

◆ m_tuple

TTree* HepMcTupleWriterTool::m_tuple
protected

cached pointer to the tuple

Definition at line 117 of file HepMcTupleWriterTool.h.

◆ m_tupleSvc

ServiceHandle<ITHistSvc> HepMcTupleWriterTool::m_tupleSvc
protected

Pointer to @ ITHistSvc.

Definition at line 102 of file HepMcTupleWriterTool.h.

◆ s_nMax

const int HepMcTupleWriterTool::s_nMax = 2000
staticprotected

maximum number of particles per event

Definition at line 84 of file HepMcTupleWriterTool.h.


The documentation for this class was generated from the following files:
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
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
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
HepMcTupleWriterTool::bookTuple
void bookTuple()
book the tuple
Definition: HepMcTupleWriterTool.cxx:178
HepMcTupleWriterTool::m_tupleSvc
ServiceHandle< ITHistSvc > m_tupleSvc
Pointer to @ ITHistSvc.
Definition: HepMcTupleWriterTool.h:102
python.CaloAddPedShiftConfig.type
type
Definition: CaloAddPedShiftConfig.py:42
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
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:209
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
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:151
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:32
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
WriteHiveWithMetaData.streamName
string streamName
Definition: WriteHiveWithMetaData.py:22
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:240
L1CaloPhase1Monitoring.propName
propName
Definition: L1CaloPhase1Monitoring.py:558
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
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:122
HepMcTupleWriterTool::HepMcParticles::m_barcode
std::array< int, s_nMax > m_barcode
Definition: HepMcTupleWriterTool.h:96
HepMcTupleWriterTool::HepMcParticles::m_px
std::array< double, s_nMax > m_px
Definition: HepMcTupleWriterTool.h:89
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