ATLAS Offline Software
HepMcReaderTool.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 // HepMcReaderTool.cxx
7 // Implementation file for class HepMcReaderTool
8 // Author: S.Binet<binet@cern.ch>
10 
11 
12 // STL includes
13 #include <algorithm>
14 #include <cctype>
15 
16 
17 //HepMC includes
19 #include "AtlasHepMC/IO_GenEvent.h"
20 
21 // McParticleTools includes
22 #include "HepMcReaderTool.h"
23 
24 static const char * const s_protocolSep = ":";
25 
26 
30 
34  const std::string& name,
35  const IInterface* parent ) :
36  base_class( type, name, parent ),
37  m_ioFrontend( nullptr )
38 {
39  //
40  // Property declaration
41  //
42 
43  declareProperty( "Input",
44  m_ioFrontendURL = "ascii:hepmc.genevent.txt",
45  "Name of the front-end we'll use to read in the HepMC::GenEvent."
46  "\nEx: ascii:hepmc.genevent.txt" );
47  m_ioFrontendURL.declareUpdateHandler( &HepMcReaderTool::setupFrontend,
48  this );
49 
50  declareProperty( "McEventsOutput",
51  m_mcEventsOutputName = "GEN_EVENT",
52  "Output location of the McEventCollection to read out" );
53 }
54 
58 {
59  ATH_MSG_DEBUG("Calling destructor");
60 
61  delete m_ioFrontend;
62  m_ioFrontend = nullptr;
63 }
64 
68 {
69  ATH_MSG_INFO("Initializing " << name() << "...");
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  // setup frontend
77  if ( nullptr == m_ioFrontend ) {
79  }
80 
81  return StatusCode::SUCCESS;
82 }
83 
85 {
86  ATH_MSG_INFO("Finalizing " << name() << "...");
87  return StatusCode::SUCCESS;
88 }
89 
91 {
92  // create a new McEventCollection and put it into StoreGate
93  McEventCollection * mcEvts = new McEventCollection;
94  if ( evtStore()->record( mcEvts, m_mcEventsOutputName ).isFailure() ) {
95  ATH_MSG_ERROR("Could not record a McEventCollection at ["<< m_mcEventsOutputName << "] !!");
96  return StatusCode::FAILURE;
97  }
98 
99  if ( evtStore()->setConst( mcEvts ).isFailure() ) {
100  ATH_MSG_WARNING("Could not setConst McEventCollection at ["<< m_mcEventsOutputName << "] !!");
101  }
102 
103  HepMC::GenEvent * evt = new HepMC::GenEvent;
104  mcEvts->push_back(evt);
105 
106  return read(evt);
107 }
108 
112 
114 {
115 #ifdef HEPMC3
116  m_ioFrontend->read_event(*evt);
117 #else
118  m_ioFrontend->fill_next_event(evt);
119 #endif
120 
121  return StatusCode::SUCCESS;
122 }
123 
124 void HepMcReaderTool::setupFrontend( Gaudi::Details::PropertyBase& /*prop*/ )
125 {
126  // defaults
127  std::string protocol = "ascii";
128  std::string fileName = "hepmc.genevent.txt";
129 
130  // reset internal state
131  if ( m_ioFrontend ) {
132  delete m_ioFrontend;
133  m_ioFrontend = nullptr;
134  }
135 
136  // caching URL
137  const std::string& url = m_ioFrontendURL.value();
138 
139  std::string::size_type protocolPos = url.find(s_protocolSep);
140 
141  if ( std::string::npos != protocolPos ) {
142  protocol = url.substr( 0, protocolPos );
143  fileName = url.substr( protocolPos+1, std::string::npos );
144  } else {
145  //protocol = "ascii";
146  fileName = url;
147  }
148 
149  // get the protocol name in lower cases
150  std::transform( protocol.begin(), protocol.end(), protocol.begin(), [](unsigned char c){ return std::tolower(c); } );
151 
152 #ifdef HEPMC3
153  if ( "ascii" == protocol ) {
154  m_ioFrontend = new HepMC3::ReaderAsciiHepMC2( fileName.c_str());
155 
156  } else {
157  msg(MSG::WARNING) << "UNKNOWN protocol [" << protocol << "] !!" << endmsg<< "Will use [ascii] instead..."<< endmsg;
158  protocol = "ascii";
159  m_ioFrontend = new HepMC3::ReaderAsciiHepMC2( fileName.c_str());
160  }
161 #else
162  if ( "ascii" == protocol ) {
163  m_ioFrontend = new HepMC::IO_GenEvent( fileName.c_str(), std::ios::in );
164 
165  } else {
166  msg(MSG::WARNING) << "UNKNOWN protocol [" << protocol << "] !!" << endmsg << "Will use [ascii] instead..."<< endmsg;
167  protocol = "ascii";
168  m_ioFrontend = new HepMC::IO_GenEvent( fileName.c_str(), std::ios::in );
169  }
170 #endif
171 
172  ATH_MSG_DEBUG("Using protocol [" << protocol << "] and write to ["<< fileName << "]");
173 }
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
HepMcReaderTool::execute
StatusCode execute()
Definition: HepMcReaderTool.cxx:90
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
HepMcReaderTool::m_ioFrontend
HepMC::IO_BaseClass * m_ioFrontend
Abstract base class for the back-end.
Definition: HepMcReaderTool.h:94
IO_GenEvent.h
McEventCollection
McEventCollection
Definition: GeneratorObjectsTPCnv.cxx:60
LArG4FSStartPointFilter.evt
evt
Definition: LArG4FSStartPointFilter.py:42
physics_parameters.url
string url
Definition: physics_parameters.py:27
python.CaloAddPedShiftConfig.type
type
Definition: CaloAddPedShiftConfig.py:42
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
McEventCollection.h
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
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
Amg::transform
Amg::Vector3D transform(Amg::Vector3D &v, Amg::Transform3D &tr)
Transform a point from a Trasformation3D.
Definition: GeoPrimitivesHelpers.h:156
test_pyathena.parent
parent
Definition: test_pyathena.py:15
HepMcReaderTool::read
StatusCode read(HepMC::GenEvent *evt)
Process the HepMC::GenEvent through the I/O frontend.
Definition: HepMcReaderTool.cxx:113
McEventCollection
This defines the McEventCollection, which is really just an ObjectVector of McEvent objects.
Definition: McEventCollection.h:32
HepMcReaderTool::initialize
StatusCode initialize()
Athena Algorithm's Hooks.
Definition: HepMcReaderTool.cxx:67
HepMcReaderTool::m_mcEventsOutputName
StringProperty m_mcEventsOutputName
Location of the McEventCollection to be read out.
Definition: HepMcReaderTool.h:87
HepMcReaderTool::HepMcReaderTool
HepMcReaderTool()
Default constructor:
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
DataVector::push_back
value_type push_back(value_type pElem)
Add an element to the end of the collection.
HepMcReaderTool::m_ioFrontendURL
StringProperty m_ioFrontendURL
URL of the I/O front-end (only "ASCII" for now...) glued with the name of the input file name.
Definition: HepMcReaderTool.h:83
HepMcReaderTool::finalize
StatusCode finalize()
Definition: HepMcReaderTool.cxx:84
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
jobOptions.fileName
fileName
Definition: jobOptions.SuperChic_ALP2.py:39
HepMcReaderTool.h
python.compressB64.c
def c
Definition: compressB64.py:93
python.AutoConfigFlags.msg
msg
Definition: AutoConfigFlags.py:7
HepMcReaderTool::setupFrontend
void setupFrontend(Gaudi::Details::PropertyBase &ioFrontendURL)
Method to configure the front-end to read out the HepMC::GenEvent.
Definition: HepMcReaderTool.cxx:124
HepMcReaderTool::~HepMcReaderTool
virtual ~HepMcReaderTool()
Destructor:
Definition: HepMcReaderTool.cxx:57