ATLAS Offline Software
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
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",
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 
112 StatusCode 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 
122 void 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 }
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
HepMcWriterTool::initialize
StatusCode initialize()
Athena Algorithm's Hooks.
Definition: HepMcWriterTool.cxx:61
IO_GenEvent.h
HepMcWriterTool::finalize
StatusCode finalize()
Definition: HepMcWriterTool.cxx:79
python.AthDsoLogger.out
out
Definition: AthDsoLogger.py:70
LArG4FSStartPointFilter.evt
evt
Definition: LArG4FSStartPointFilter.py:42
physics_parameters.url
string url
Definition: physics_parameters.py:27
python.CaloAddPedShiftConfig.type
type
Definition: CaloAddPedShiftConfig.py:42
HepMcWriterTool::~HepMcWriterTool
virtual ~HepMcWriterTool()
Destructor:
Definition: HepMcWriterTool.cxx:49
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:82
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:88
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
McEventCollection.h
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
Amg::transform
Amg::Vector3D transform(Amg::Vector3D &v, Amg::Transform3D &tr)
Transform a point from a Trasformation3D.
Definition: GeoPrimitivesHelpers.h:156
DataVector::front
const T * front() const
Access the first element in the collection as an rvalue.
test_pyathena.parent
parent
Definition: test_pyathena.py:15
HepMcWriterTool::HepMcWriterTool
HepMcWriterTool()
Default constructor:
HepMcWriterTool::setupBackend
void setupBackend(Gaudi::Details::PropertyBase &ioBackendURL)
Method to configure the back-end to write out the HepMC::GenEvent.
Definition: HepMcWriterTool.cxx:122
McEventCollection
This defines the McEventCollection, which is really just an ObjectVector of McEvent objects.
Definition: McEventCollection.h:32
HepMcWriterTool::m_ioBackend
HepMC::IO_BaseClass * m_ioBackend
Abstract base class for the back-end.
Definition: HepMcWriterTool.h:95
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
HepMcWriterTool::write
StatusCode write(const HepMC::GenEvent *evt)
Process the HepMC::GenEvent through the I/O backend.
Definition: HepMcWriterTool.cxx:112
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
HepMcWriterTool::execute
StatusCode execute()
Definition: HepMcWriterTool.cxx:85
jobOptions.fileName
fileName
Definition: jobOptions.SuperChic_ALP2.py:39
HepMcWriterTool.h
python.compressB64.c
def c
Definition: compressB64.py:93
DataVector::empty
bool empty() const noexcept
Returns true if the collection is empty.