ATLAS Offline Software
Loading...
Searching...
No Matches
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
20
21// McParticleTools includes
22#include "HepMcReaderTool.h"
23
24static 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" );
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
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
113StatusCode HepMcReaderTool::read( HepMC::GenEvent* evt )
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
124void 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}
#define endmsg
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
static const char *const s_protocolSep
value_type push_back(value_type pElem)
Add an element to the end of the collection.
void setupFrontend(Gaudi::Details::PropertyBase &ioFrontendURL)
Method to configure the front-end to read out the HepMC::GenEvent.
StatusCode initialize()
Athena Algorithm's Hooks.
StringProperty m_mcEventsOutputName
Location of the McEventCollection to be read out.
StatusCode read(HepMC::GenEvent *evt)
Process the HepMC::GenEvent through the I/O frontend.
StringProperty m_ioFrontendURL
URL of the I/O front-end (only "ASCII" for now...) glued with the name of the input file name.
StatusCode finalize()
StatusCode execute()
HepMC::IO_BaseClass * m_ioFrontend
Abstract base class for the back-end.
virtual ~HepMcReaderTool()
Destructor:
HepMcReaderTool()
Default constructor:
This defines the McEventCollection, which is really just an ObjectVector of McEvent objectsFile: Gene...
MsgStream & msg
Definition testRead.cxx:32