ATLAS Offline Software
HepMcWriterTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 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 ) :
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  declareInterface<IIOHepMcTool>(this);
47 }
48 
52 {
53  ATH_MSG_DEBUG("Calling destructor");
54 
55  if ( m_ioBackend ) {
56  delete m_ioBackend;
57  m_ioBackend = nullptr;
58  }
59 }
60 
64 {
65  ATH_MSG_INFO("Initializing " << name() << "...");
66 
67  // Get pointer to StoreGateSvc and cache it :
68  if ( !evtStore().retrieve().isSuccess() ) {
69  ATH_MSG_ERROR("Unable to retrieve pointer to StoreGateSvc");
70  return StatusCode::FAILURE;
71  }
72 
73  // setup backend
74  if ( nullptr == m_ioBackend ) {
76  }
77 
78  return StatusCode::SUCCESS;
79 }
80 
82 {
83  ATH_MSG_INFO("Finalizing " << name() << "...");
84  return StatusCode::SUCCESS;
85 }
86 
88 {
89  // retrieve the McEventCollection
90  const McEventCollection * mcEvts = nullptr;
91  if ( evtStore()->retrieve( mcEvts, m_mcEventsName ).isFailure() || nullptr == mcEvts ) {
92  ATH_MSG_ERROR("Could not retrieve a McEventCollection at [" << m_mcEventsName << "] !!");
93  return StatusCode::FAILURE;
94  }
95 
96  if ( mcEvts->empty() ) {
97  ATH_MSG_WARNING("McEventCollection at ["<<m_mcEventsName<<"] is EMPTY !!");
98  return StatusCode::FAILURE;
99  }
100 
101  const HepMC::GenEvent * evt = mcEvts->front();
102  if ( !evt ) {
103  ATH_MSG_ERROR("Retrieved NULL pointer to HepMC::GenEvent !!");
104  return StatusCode::FAILURE;
105  }
106 
107  return write(evt);
108 }
109 
113 
114 StatusCode HepMcWriterTool::write( const HepMC::GenEvent* evt )
115 {
116 #ifdef HEPMC3
117  m_ioBackend->write_event(*evt);
118 #else
119  m_ioBackend->write_event(evt);
120 #endif
121  return StatusCode::SUCCESS;
122 }
123 
124 void HepMcWriterTool::setupBackend( Gaudi::Details::PropertyBase& /*prop*/ )
125 {
126  // defaults
127  std::string protocol = "ascii";
128  std::string fileName = "hepmc.genevent.txt";
129 
130  // reset internal state
131  if ( m_ioBackend ) {
132  delete m_ioBackend;
133  m_ioBackend = nullptr;
134  }
135 
136  // caching URL
137  const std::string& url = m_ioBackendURL.value();
138 
139  std::string::size_type protocolPos = url.find(s_protocolSep);
140 
141  if ( std::string::npos != protocolPos ) {
142  protocol = url.substr( 0, protocolPos );
143  fileName = url.substr( protocolPos+1, std::string::npos );
144  } else {
145  //protocol = "ascii";
146  fileName = url;
147  }
148 
149  // get the protocol name in lower cases
150  std::transform( protocol.begin(), protocol.end(), protocol.begin(), [](unsigned char c){ return std::tolower(c); } );
151 #ifdef HEPMC3
152  if ( "ascii" == protocol ) {
153  m_ioBackend = new HepMC3::WriterAsciiHepMC2( fileName.c_str());
154 
155  } else {
156  ATH_MSG_WARNING("UNKNOWN protocol [" << protocol << "] !!" << endmsg << "Will use [ascii] instead...");
157  protocol = "ascii";
158  m_ioBackend = new HepMC3::WriterAsciiHepMC2( fileName.c_str());
159  }
160 #else
161  if ( "ascii" == protocol ) {
162  m_ioBackend = new HepMC::IO_GenEvent( fileName.c_str(), std::ios::out | std::ios::trunc );
163 
164  } else {
165  ATH_MSG_WARNING("UNKNOWN protocol [" << protocol << "] !!" << endmsg << "Will use [ascii] instead...");
166  protocol = "ascii";
167  m_ioBackend = new HepMC::IO_GenEvent( fileName.c_str(), std::ios::out | std::ios::trunc );
168  }
169 #endif
170  ATH_MSG_DEBUG("Using protocol [" << protocol << "] and write to ["<< fileName << "]");
171 }
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:63
IO_GenEvent.h
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
HepMcWriterTool::finalize
StatusCode finalize()
Definition: HepMcWriterTool.cxx:81
python.AthDsoLogger.out
out
Definition: AthDsoLogger.py:71
LArG4FSStartPointFilter.evt
evt
Definition: LArG4FSStartPointFilter.py:42
physics_parameters.url
string url
Definition: physics_parameters.py:27
HepMcWriterTool::~HepMcWriterTool
virtual ~HepMcWriterTool()
Destructor:
Definition: HepMcWriterTool.cxx:51
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
AthCommonDataStore< AthCommonMsg< AlgTool > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
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
FortranAlgorithmOptions.fileName
fileName
Definition: FortranAlgorithmOptions.py:13
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:124
McEventCollection
This defines the McEventCollection, which is really just an ObjectVector of McEvent objects.
Definition: McEventCollection.h:33
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:192
HepMcWriterTool::write
StatusCode write(const HepMC::GenEvent *evt)
Process the HepMC::GenEvent through the I/O backend.
Definition: HepMcWriterTool.cxx:114
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
HepMcWriterTool::execute
StatusCode execute()
Definition: HepMcWriterTool.cxx:87
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
AthAlgTool
Definition: AthAlgTool.h:26
HepMcWriterTool.h
python.compressB64.c
def c
Definition: compressB64.py:93
DataVector::empty
bool empty() const noexcept
Returns true if the collection is empty.