ATLAS Offline Software
Loading...
Searching...
No Matches
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
18
19// McParticleTools includes
20#include "HepMcWriterTool.h"
21
22static const char * const s_protocolSep = ":";
23
27HepMcWriterTool::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",
36 m_ioBackendURL = "ascii:hepmc.genevent.txt",
37 "Name of the back-end we'll use to write out the HepMC::GenEvent."
38 "\nEx: ascii:hepmc.genevent.txt" );
39 m_ioBackendURL.declareUpdateHandler( &HepMcWriterTool::setupBackend,
40 this );
41
42 declareProperty( "McEvents",
43 m_mcEventsName = "GEN_EVENT",
44 "Input location of the McEventCollection to write out" );
45}
46
50{
51 ATH_MSG_DEBUG("Calling destructor");
52
53 if ( m_ioBackend ) {
54 delete m_ioBackend;
55 m_ioBackend = nullptr;
56 }
57}
58
62{
63 ATH_MSG_INFO("Initializing " << name() << "...");
64
65 // Get pointer to StoreGateSvc and cache it :
66 if ( !evtStore().retrieve().isSuccess() ) {
67 ATH_MSG_ERROR("Unable to retrieve pointer to StoreGateSvc");
68 return StatusCode::FAILURE;
69 }
70
71 // setup backend
72 if ( nullptr == m_ioBackend ) {
74 }
75
76 return StatusCode::SUCCESS;
77}
78
80{
81 ATH_MSG_INFO("Finalizing " << name() << "...");
82 return StatusCode::SUCCESS;
83}
84
86{
87 // retrieve the McEventCollection
88 const McEventCollection * mcEvts = nullptr;
89 if ( evtStore()->retrieve( mcEvts, m_mcEventsName ).isFailure() || nullptr == mcEvts ) {
90 ATH_MSG_ERROR("Could not retrieve a McEventCollection at [" << m_mcEventsName << "] !!");
91 return StatusCode::FAILURE;
92 }
93
94 if ( mcEvts->empty() ) {
95 ATH_MSG_WARNING("McEventCollection at ["<<m_mcEventsName<<"] is EMPTY !!");
96 return StatusCode::FAILURE;
97 }
98
99 const HepMC::GenEvent * evt = mcEvts->front();
100 if ( !evt ) {
101 ATH_MSG_ERROR("Retrieved NULL pointer to HepMC::GenEvent !!");
102 return StatusCode::FAILURE;
103 }
104
105 return write(evt);
106}
107
111
112StatusCode HepMcWriterTool::write( const HepMC::GenEvent* evt )
113{
114#ifdef HEPMC3
115 m_ioBackend->write_event(*evt);
116#else
117 m_ioBackend->write_event(evt);
118#endif
119 return StatusCode::SUCCESS;
120}
121
122void HepMcWriterTool::setupBackend( Gaudi::Details::PropertyBase& /*prop*/ )
123{
124 // defaults
125 std::string protocol = "ascii";
126 std::string fileName = "hepmc.genevent.txt";
127
128 // reset internal state
129 if ( m_ioBackend ) {
130 delete m_ioBackend;
131 m_ioBackend = nullptr;
132 }
133
134 // caching URL
135 const std::string& url = m_ioBackendURL.value();
136
137 std::string::size_type protocolPos = url.find(s_protocolSep);
138
139 if ( std::string::npos != protocolPos ) {
140 protocol = url.substr( 0, protocolPos );
141 fileName = url.substr( protocolPos+1, std::string::npos );
142 } else {
143 //protocol = "ascii";
144 fileName = url;
145 }
146
147 // get the protocol name in lower cases
148 std::transform( protocol.begin(), protocol.end(), protocol.begin(), [](unsigned char c){ return std::tolower(c); } );
149#ifdef HEPMC3
150 if ( "ascii" == protocol ) {
151 m_ioBackend = new HepMC3::WriterAsciiHepMC2( fileName.c_str());
152
153 } else {
154 ATH_MSG_WARNING("UNKNOWN protocol [" << protocol << "] !!" << endmsg << "Will use [ascii] instead...");
155 protocol = "ascii";
156 m_ioBackend = new HepMC3::WriterAsciiHepMC2( fileName.c_str());
157 }
158#else
159 if ( "ascii" == protocol ) {
160 m_ioBackend = new HepMC::IO_GenEvent( fileName.c_str(), std::ios::out | std::ios::trunc );
161
162 } else {
163 ATH_MSG_WARNING("UNKNOWN protocol [" << protocol << "] !!" << endmsg << "Will use [ascii] instead...");
164 protocol = "ascii";
165 m_ioBackend = new HepMC::IO_GenEvent( fileName.c_str(), std::ios::out | std::ios::trunc );
166 }
167#endif
168 ATH_MSG_DEBUG("Using protocol [" << protocol << "] and write to ["<< fileName << "]");
169}
#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
const T * front() const
Access the first element in the collection as an rvalue.
bool empty() const noexcept
Returns true if the collection is empty.
StatusCode finalize()
StringProperty m_ioBackendURL
URL of the I/O back-end (only "ASCII" for now...) glued with the name of the output file name.
StatusCode write(const HepMC::GenEvent *evt)
Process the HepMC::GenEvent through the I/O backend.
HepMcWriterTool()
Default constructor:
StatusCode execute()
HepMC::IO_BaseClass * m_ioBackend
Abstract base class for the back-end.
virtual ~HepMcWriterTool()
Destructor:
StringProperty m_mcEventsName
Location of the McEventCollection to be written out If there is more than 1 HepMC::GenEvent in the Mc...
void setupBackend(Gaudi::Details::PropertyBase &ioBackendURL)
Method to configure the back-end to write out the HepMC::GenEvent.
StatusCode initialize()
Athena Algorithm's Hooks.
This defines the McEventCollection, which is really just an ObjectVector of McEvent objectsFile: Gene...