ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
Generators
TruthIO
src
HepMcReaderTool.cxx
Go to the documentation of this file.
1
/*
2
Copyright (C) 2002-2026 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
18
#include "
GeneratorObjects/McEventCollection.h
"
19
#include "
AtlasHepMC/IO_GenEvent.h
"
20
#include "
AtlasHepMC/ReaderFactory.h
"
21
22
// McParticleTools includes
23
#include "
HepMcReaderTool.h
"
24
25
static
const
char
*
const
s_protocolSep
=
":"
;
26
29
HepMcReaderTool::HepMcReaderTool
(
const
std::string&
type
,
const
std::string& name,
const
IInterface* parent ) :
30
base_class(
type
, name, parent )
31
{
32
//
33
// Property declaration
34
//
35
36
declareProperty(
"Input"
,
m_ioFrontendURL
=
"auto:hepmc.genevent.txt"
,
"Name of the front-end we'll use to read in the HepMC::GenEvent.\nEx: ascii:hepmc.genevent.txt"
);
37
m_ioFrontendURL
.declareUpdateHandler( &
HepMcReaderTool::setupFrontend
,
this
);
38
declareProperty(
"McEventsOutput"
,
m_mcEventsOutputName
=
"GEN_EVENT"
,
"Output location of the McEventCollection to read out"
);
39
}
40
43
HepMcReaderTool::~HepMcReaderTool
()
44
{
45
ATH_MSG_DEBUG
(
"Calling destructor"
);
46
}
47
50
StatusCode
HepMcReaderTool::initialize
()
51
{
52
ATH_MSG_INFO
(
"Initializing "
<< name() <<
"..."
);
53
// Get pointer to StoreGateSvc and cache it :
54
if
( !evtStore().retrieve().isSuccess() ) {
55
ATH_MSG_ERROR
(
"Unable to retrieve pointer to StoreGateSvc"
);
56
return
StatusCode::FAILURE;
57
}
58
59
// setup frontend
60
if
( !
m_ioFrontend
) {
61
setupFrontend
(
m_ioFrontendURL
);
62
}
63
64
return
StatusCode::SUCCESS;
65
}
66
67
StatusCode
HepMcReaderTool::finalize
()
68
{
69
ATH_MSG_INFO
(
"Finalizing "
<< name() <<
"..."
);
70
return
StatusCode::SUCCESS;
71
}
72
73
StatusCode
HepMcReaderTool::execute
()
74
{
75
// create a new McEventCollection and put it into StoreGate
76
McEventCollection
* mcEvts =
new
McEventCollection
;
77
if
( evtStore()->record( mcEvts,
m_mcEventsOutputName
).isFailure() ) {
78
ATH_MSG_ERROR
(
"Could not record a McEventCollection at ["
<<
m_mcEventsOutputName
<<
"] !!"
);
79
return
StatusCode::FAILURE;
80
}
81
82
if
( evtStore()->setConst( mcEvts ).isFailure() ) {
83
ATH_MSG_WARNING
(
"Could not setConst McEventCollection at ["
<<
m_mcEventsOutputName
<<
"] !!"
);
84
}
85
86
HepMC::GenEvent
* evt =
new
HepMC::GenEvent
;
87
mcEvts->
push_back
(evt);
88
89
return
read
(evt);
90
}
91
92
StatusCode
HepMcReaderTool::read
(
HepMC::GenEvent
* evt )
93
{
94
m_ioFrontend
->read_event(*evt);
95
return
StatusCode::SUCCESS;
96
}
97
98
void
HepMcReaderTool::setupFrontend
( Gaudi::Details::PropertyBase&
/*prop*/
)
99
{
100
// defaults
101
std::string protocol =
"auto"
;
102
std::string fileName =
"hepmc.genevent.txt"
;
103
104
// reset internal state
105
m_ioFrontend
=
nullptr
;
106
107
// caching URL
108
const
std::string& url =
m_ioFrontendURL
.value();
109
110
std::string::size_type protocolPos = url.find(
s_protocolSep
);
111
112
if
( std::string::npos != protocolPos ) {
113
protocol = url.substr( 0, protocolPos );
114
fileName = url.substr( protocolPos + 1, std::string::npos );
115
}
else
{
116
fileName = url;
117
}
118
119
// get the protocol name in lower cases
120
std::transform( protocol.begin(), protocol.end(), protocol.begin(), [](
unsigned
char
c){ return std::tolower(c); } );
121
122
if
(
"auto"
== protocol ) {
123
m_ioFrontend
= HepMC3::deduce_reader( fileName.c_str());
124
}
else
if
(
"ascii"
== protocol ) {
125
m_ioFrontend
= std::make_shared<HepMC3::ReaderAsciiHepMC2>( fileName.c_str());
126
}
else
{
127
msg
(MSG::WARNING) <<
"UNKNOWN protocol ["
<< protocol <<
"] !!"
<<
endmsg
<<
"Will use [ascii] instead..."
<<
endmsg
;
128
protocol =
"ascii"
;
129
m_ioFrontend
= std::make_shared<HepMC3::ReaderAsciiHepMC2>( fileName.c_str());
130
}
131
ATH_MSG_DEBUG
(
"Using protocol ["
<< protocol <<
"] and write to ["
<< fileName <<
"]"
);
132
}
endmsg
#define endmsg
Definition
AnalysisConfig_Ntuple.cxx:54
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition
AthMsgStreamMacros.h:33
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition
AthMsgStreamMacros.h:31
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition
AthMsgStreamMacros.h:32
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition
AthMsgStreamMacros.h:29
s_protocolSep
static const char *const s_protocolSep
Definition
HepMcFloatWriterTool.cxx:27
HepMcReaderTool.h
IO_GenEvent.h
McEventCollection.h
ReaderFactory.h
DataVector::push_back
value_type push_back(value_type pElem)
Add an element to the end of the collection.
HepMcReaderTool::setupFrontend
void setupFrontend(Gaudi::Details::PropertyBase &ioFrontendURL)
Method to configure the front-end to read out the HepMC::GenEvent.
Definition
HepMcReaderTool.cxx:98
HepMcReaderTool::initialize
StatusCode initialize()
Athena Algorithm's Hooks.
Definition
HepMcReaderTool.cxx:50
HepMcReaderTool::m_mcEventsOutputName
StringProperty m_mcEventsOutputName
Location of the McEventCollection to be read out.
Definition
HepMcReaderTool.h:69
HepMcReaderTool::read
StatusCode read(HepMC::GenEvent *evt)
Process the HepMC::GenEvent through the I/O frontend.
Definition
HepMcReaderTool.cxx:92
HepMcReaderTool::m_ioFrontend
std::shared_ptr< HepMC3::Reader > m_ioFrontend
Abstract base class for the back-end.
Definition
HepMcReaderTool.h:73
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:65
HepMcReaderTool::finalize
StatusCode finalize()
Definition
HepMcReaderTool.cxx:67
HepMcReaderTool::execute
StatusCode execute()
Definition
HepMcReaderTool.cxx:73
HepMcReaderTool::~HepMcReaderTool
virtual ~HepMcReaderTool()
Destructor:
Definition
HepMcReaderTool.cxx:43
HepMcReaderTool::HepMcReaderTool
HepMcReaderTool()
Default constructor:
McEventCollection
This defines the McEventCollection, which is really just an ObjectVector of McEvent objectsFile: Gene...
Definition
McEventCollection.h:31
HepMC::GenEvent
HepMC3::GenEvent GenEvent
Definition
GenEvent.h:39
type
msg
MsgStream & msg
Definition
testRead.cxx:32
Generated on
for ATLAS Offline Software by
1.17.0