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", 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
43{
44 ATH_MSG_DEBUG("Calling destructor");
45}
46
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 ) {
62 }
63
64 return StatusCode::SUCCESS;
65}
66
68{
69 ATH_MSG_INFO("Finalizing " << name() << "...");
70 return StatusCode::SUCCESS;
71}
72
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
101{
102 m_ioBackend->write_event(*evt);
103 return StatusCode::SUCCESS;
104}
105
106void 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}
#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.
std::shared_ptr< HepMC3::Writer > m_ioBackend
Abstract base class for the back-end.
StatusCode write(const HepMC::GenEvent *evt)
Process the HepMC::GenEvent through the I/O backend.
HepMcWriterTool()
Default constructor:
StatusCode execute()
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...
HepMC3::GenEvent GenEvent
Definition GenEvent.h:39