ATLAS Offline Software
HepMcReaderTool.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 // 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 ) :
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  declareInterface<IIOHepMcTool>(this);
55 }
56 
60 {
61  ATH_MSG_DEBUG("Calling destructor");
62 
63  delete m_ioFrontend;
64  m_ioFrontend = nullptr;
65 }
66 
70 {
71  ATH_MSG_INFO("Initializing " << name() << "...");
72  // Get pointer to StoreGateSvc and cache it :
73  if ( !evtStore().retrieve().isSuccess() ) {
74  ATH_MSG_ERROR("Unable to retrieve pointer to StoreGateSvc");
75  return StatusCode::FAILURE;
76  }
77 
78  // setup frontend
79  if ( nullptr == m_ioFrontend ) {
81  }
82 
83  return StatusCode::SUCCESS;
84 }
85 
87 {
88  ATH_MSG_INFO("Finalizing " << name() << "...");
89  return StatusCode::SUCCESS;
90 }
91 
93 {
94  // create a new McEventCollection and put it into StoreGate
95  McEventCollection * mcEvts = new McEventCollection;
96  if ( evtStore()->record( mcEvts, m_mcEventsOutputName ).isFailure() ) {
97  ATH_MSG_ERROR("Could not record a McEventCollection at ["<< m_mcEventsOutputName << "] !!");
98  return StatusCode::FAILURE;
99  }
100 
101  if ( evtStore()->setConst( mcEvts ).isFailure() ) {
102  ATH_MSG_WARNING("Could not setConst McEventCollection at ["<< m_mcEventsOutputName << "] !!");
103  }
104 
105  HepMC::GenEvent * evt = new HepMC::GenEvent;
106  mcEvts->push_back(evt);
107 
108  return read(evt);
109 }
110 
114 
116 {
117 #ifdef HEPMC3
118  m_ioFrontend->read_event(*evt);
119 #else
120  m_ioFrontend->fill_next_event(evt);
121 #endif
122 
123  return StatusCode::SUCCESS;
124 }
125 
126 void HepMcReaderTool::setupFrontend( Gaudi::Details::PropertyBase& /*prop*/ )
127 {
128  // defaults
129  std::string protocol = "ascii";
130  std::string fileName = "hepmc.genevent.txt";
131 
132  // reset internal state
133  if ( m_ioFrontend ) {
134  delete m_ioFrontend;
135  m_ioFrontend = nullptr;
136  }
137 
138  // caching URL
139  const std::string& url = m_ioFrontendURL.value();
140 
141  std::string::size_type protocolPos = url.find(s_protocolSep);
142 
143  if ( std::string::npos != protocolPos ) {
144  protocol = url.substr( 0, protocolPos );
145  fileName = url.substr( protocolPos+1, std::string::npos );
146  } else {
147  //protocol = "ascii";
148  fileName = url;
149  }
150 
151  // get the protocol name in lower cases
152  std::transform( protocol.begin(), protocol.end(), protocol.begin(), [](unsigned char c){ return std::tolower(c); } );
153 
154 #ifdef HEPMC3
155  if ( "ascii" == protocol ) {
156  m_ioFrontend = new HepMC3::ReaderAsciiHepMC2( fileName.c_str());
157 
158  } else {
159  msg(MSG::WARNING) << "UNKNOWN protocol [" << protocol << "] !!" << endmsg<< "Will use [ascii] instead..."<< endmsg;
160  protocol = "ascii";
161  m_ioFrontend = new HepMC3::ReaderAsciiHepMC2( fileName.c_str());
162  }
163 #else
164  if ( "ascii" == protocol ) {
165  m_ioFrontend = new HepMC::IO_GenEvent( fileName.c_str(), std::ios::in );
166 
167  } else {
168  msg(MSG::WARNING) << "UNKNOWN protocol [" << protocol << "] !!" << endmsg << "Will use [ascii] instead..."<< endmsg;
169  protocol = "ascii";
170  m_ioFrontend = new HepMC::IO_GenEvent( fileName.c_str(), std::ios::in );
171  }
172 #endif
173 
174  ATH_MSG_DEBUG("Using protocol [" << protocol << "] and write to ["<< fileName << "]");
175 }
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
HepMcReaderTool::execute
StatusCode execute()
Definition: HepMcReaderTool.cxx:92
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
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
McEventCollection
McEventCollection
Definition: GeneratorObjectsTPCnv.cxx:60
LArG4FSStartPointFilter.evt
evt
Definition: LArG4FSStartPointFilter.py:42
physics_parameters.url
string url
Definition: physics_parameters.py:27
AthCommonDataStore< AthCommonMsg< AlgTool > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
FortranAlgorithmOptions.fileName
fileName
Definition: FortranAlgorithmOptions.py:13
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:115
McEventCollection
This defines the McEventCollection, which is really just an ObjectVector of McEvent objects.
Definition: McEventCollection.h:33
HepMcReaderTool::initialize
StatusCode initialize()
Athena Algorithm's Hooks.
Definition: HepMcReaderTool.cxx:69
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:192
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:86
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
AthCommonMsg< AlgTool >::msg
MsgStream & msg() const
Definition: AthCommonMsg.h:24
AthAlgTool
Definition: AthAlgTool.h:26
HepMcReaderTool.h
python.compressB64.c
def c
Definition: compressB64.py:93
HepMcReaderTool::setupFrontend
void setupFrontend(Gaudi::Details::PropertyBase &ioFrontendURL)
Method to configure the front-end to read out the HepMC::GenEvent.
Definition: HepMcReaderTool.cxx:126
HepMcReaderTool::~HepMcReaderTool
virtual ~HepMcReaderTool()
Destructor:
Definition: HepMcReaderTool.cxx:59