ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
Generators
TruthIO
src
HepMcWriterTool.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
// HepMcWriterTool.cxx
7
// Implementation file for class HepMcWriterTool
8
// Author: S.Binet<binet@cern.ch>
10
11
// STL includes
12
#include <algorithm>
13
#include <cctype>
14
15
// HepMC includes
16
#include "
GeneratorObjects/McEventCollection.h
"
17
#include "
AtlasHepMC/IO_GenEvent.h
"
18
19
// McParticleTools includes
20
#include "
HepMcWriterTool.h
"
21
22
static
const
char
*
const
s_protocolSep
=
":"
;
23
27
HepMcWriterTool::HepMcWriterTool
(
const
std::string&
type
,
const
std::string& name,
const
IInterface* parent ) :
28
base_class(
type
, name, parent ),
29
m_ioBackend
( nullptr )
30
{
31
//
32
// Property declaration
33
//
34
35
declareProperty(
"Output"
,
m_ioBackendURL
=
"ascii:hepmc.genevent.txt"
,
"Name of the back-end we'll use to write out the HepMC::GenEvent.\nEx: ascii:hepmc.genevent.txt"
);
36
m_ioBackendURL
.declareUpdateHandler( &
HepMcWriterTool::setupBackend
,
this
);
37
declareProperty(
"McEvents"
,
m_mcEventsName
=
"GEN_EVENT"
,
"Input location of the McEventCollection to write out"
);
38
}
39
42
HepMcWriterTool::~HepMcWriterTool
()
43
{
44
ATH_MSG_DEBUG
(
"Calling destructor"
);
45
}
46
49
StatusCode
HepMcWriterTool::initialize
()
50
{
51
ATH_MSG_INFO
(
"Initializing "
<< name() <<
"..."
);
52
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 backend
60
if
(!
m_ioBackend
) {
61
setupBackend
(
m_ioBackendURL
);
62
}
63
64
return
StatusCode::SUCCESS;
65
}
66
67
StatusCode
HepMcWriterTool::finalize
()
68
{
69
ATH_MSG_INFO
(
"Finalizing "
<< name() <<
"..."
);
70
return
StatusCode::SUCCESS;
71
}
72
73
StatusCode
HepMcWriterTool::execute
()
74
{
75
// retrieve the McEventCollection
76
const
McEventCollection
* mcEvts =
nullptr
;
77
if
( evtStore()->retrieve( mcEvts,
m_mcEventsName
).isFailure() ||
nullptr
== mcEvts ) {
78
ATH_MSG_ERROR
(
"Could not retrieve a McEventCollection at ["
<<
m_mcEventsName
<<
"] !!"
);
79
return
StatusCode::FAILURE;
80
}
81
82
if
( mcEvts->
empty
() ) {
83
ATH_MSG_WARNING
(
"McEventCollection at ["
<<
m_mcEventsName
<<
"] is EMPTY !!"
);
84
return
StatusCode::FAILURE;
85
}
86
87
const
HepMC::GenEvent
* evt = mcEvts->
front
();
88
if
( !evt ) {
89
ATH_MSG_ERROR
(
"Retrieved NULL pointer to HepMC::GenEvent !!"
);
90
return
StatusCode::FAILURE;
91
}
92
93
return
write
(evt);
94
}
95
99
100
StatusCode
HepMcWriterTool::write
(
const
HepMC::GenEvent
* evt )
101
{
102
m_ioBackend
->write_event(*evt);
103
return
StatusCode::SUCCESS;
104
}
105
106
void
HepMcWriterTool::setupBackend
( Gaudi::Details::PropertyBase&
/*prop*/
)
107
{
108
// defaults
109
std::string protocol =
"asciiv3"
;
110
std::string fileName =
"hepmc.genevent.txt"
;
111
112
// reset internal state
113
m_ioBackend
=
nullptr
;
114
115
// caching URL
116
const
std::string& url =
m_ioBackendURL
.value();
117
118
std::string::size_type protocolPos = url.find(
s_protocolSep
);
119
120
if
( std::string::npos != protocolPos ) {
121
protocol = url.substr( 0, protocolPos );
122
fileName = url.substr( protocolPos + 1, std::string::npos );
123
}
else
{
124
fileName = url;
125
}
126
127
// get the protocol name in lower cases
128
std::transform( protocol.begin(), protocol.end(), protocol.begin(), [](
unsigned
char
c){ return std::tolower(c); } );
129
if
(
"ascii"
== protocol ) {
130
m_ioBackend
= std::make_shared<HepMC3::WriterAsciiHepMC2>( fileName.c_str());
131
}
else
if
(
"asciiv3"
== protocol ) {
132
m_ioBackend
= std::make_shared<HepMC3::WriterAscii>( fileName.c_str());
133
}
else
{
134
ATH_MSG_WARNING
(
"UNKNOWN protocol ["
<< protocol <<
"] !!"
<<
endmsg
<<
"Will use [ascii] instead..."
);
135
protocol =
"asciiv3"
;
136
m_ioBackend
= std::make_shared<HepMC3::WriterAscii>( fileName.c_str());
137
}
138
ATH_MSG_DEBUG
(
"Using protocol ["
<< protocol <<
"] and write to ["
<< fileName <<
"]"
);
139
}
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
HepMcWriterTool.h
IO_GenEvent.h
McEventCollection.h
DataVector::front
const T * front() const
Access the first element in the collection as an rvalue.
DataVector::empty
bool empty() const noexcept
Returns true if the collection is empty.
HepMcWriterTool::finalize
StatusCode finalize()
Definition
HepMcWriterTool.cxx:67
HepMcWriterTool::m_ioBackendURL
StringProperty m_ioBackendURL
URL of the I/O back-end (only "ASCII" for now...) glued with the name of the output file name.
Definition
HepMcWriterTool.h:63
HepMcWriterTool::m_ioBackend
std::shared_ptr< HepMC3::Writer > m_ioBackend
Abstract base class for the back-end.
Definition
HepMcWriterTool.h:73
HepMcWriterTool::write
StatusCode write(const HepMC::GenEvent *evt)
Process the HepMC::GenEvent through the I/O backend.
Definition
HepMcWriterTool.cxx:100
HepMcWriterTool::HepMcWriterTool
HepMcWriterTool()
Default constructor:
HepMcWriterTool::execute
StatusCode execute()
Definition
HepMcWriterTool.cxx:73
HepMcWriterTool::~HepMcWriterTool
virtual ~HepMcWriterTool()
Destructor:
Definition
HepMcWriterTool.cxx:42
HepMcWriterTool::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
HepMcWriterTool.h:69
HepMcWriterTool::setupBackend
void setupBackend(Gaudi::Details::PropertyBase &ioBackendURL)
Method to configure the back-end to write out the HepMC::GenEvent.
Definition
HepMcWriterTool.cxx:106
HepMcWriterTool::initialize
StatusCode initialize()
Athena Algorithm's Hooks.
Definition
HepMcWriterTool.cxx:49
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
Generated on
for ATLAS Offline Software by
1.17.0