ATLAS Offline Software
WriteHepMC.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include "TruthIO/WriteHepMC.h"
6 #ifdef HEPMC3
7 #include "HepMC3/WriterAscii.h"
8 #endif
9 
10 WriteHepMC::WriteHepMC(const std::string& name, ISvcLocator* pSvcLocator)
11  : GenBase(name, pSvcLocator)
12 {
13  declareProperty("OutputFile", m_outfile="events.hepmc");
14  declareProperty("Precision", m_precision=8);
15  declareProperty("Format", m_format="hepmc2");
16  declareProperty("Units", m_units="GEVMM");
17 }
18 
19 
22  if (m_units.size() != 5) {
23  return StatusCode::FAILURE;
24  }
25 #ifdef HEPMC3
26  m_momentumunit = HepMC3::Units::momentum_unit(m_units.substr(0,3));
28  if (m_format == "hepmc2") {
29  auto writer = new HepMC3::WriterAsciiHepMC2(m_outfile);
30  writer->set_precision(m_precision);
31  m_hepmcio.reset(writer);
32  }
33  if (m_format == "hepmc3") {
34  auto writer = new HepMC3::WriterAscii(m_outfile);
35  writer->set_precision(m_precision);
36  m_hepmcio.reset(writer);
37  }
38 #else
39  m_momentumunit = (m_units.substr(0,3) == "MEV") ? HepMC::Units::MEV : HepMC::Units::GEV;
40  m_lengthunit = (m_units.substr(3,2) == "CM") ? HepMC::Units::CM : HepMC::Units::MM;
41  m_hepmcio.reset( new HepMC::IO_GenEvent(m_outfile) );
42  m_hepmcio->precision(m_precision);
43 #endif
44  return StatusCode::SUCCESS;
45 }
46 
47 
49  // Just write out the first (i.e. signal) event in the collection
50 #ifdef HEPMC3
51  HepMC3::GenEvent ev (*event_const());
52  ev.set_units(m_momentumunit,m_lengthunit);
53  m_hepmcio->write_event(ev);
54 #else
55  HepMC::GenEvent ev (*event_const());
56  ev.use_units(m_momentumunit,m_lengthunit);
57  m_hepmcio->write_event(&ev);
58 #endif
59  return StatusCode::SUCCESS;
60 }
WriteHepMC::m_lengthunit
HepMC::Units::LengthUnit m_lengthunit
Definition: WriteHepMC.h:33
WriteHepMC::m_outfile
std::string m_outfile
Definition: WriteHepMC.h:21
MM
@ MM
Definition: RegSelEnums.h:38
WriteHepMC::m_format
std::string m_format
Definition: WriteHepMC.h:23
length_unit
constexpr double length_unit
Definition: ActsDetectorElement.cxx:51
WriteHepMC::m_hepmcio
std::unique_ptr< HepMC::IO_GenEvent > m_hepmcio
Definition: WriteHepMC.h:31
WriteHepMC::m_precision
int m_precision
Definition: WriteHepMC.h:22
WriteHepMC::m_momentumunit
HepMC::Units::MomentumUnit m_momentumunit
Definition: WriteHepMC.h:32
ev
int ev
Definition: globals.cxx:25
GenBase
Base class for common behaviour of MC truth algorithms.
Definition: GenBase.h:47
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
AthCommonDataStore< AthCommonMsg< Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
Definition: AthCommonDataStore.h:145
CHECK
#define CHECK(...)
Evaluate an expression and check for errors.
Definition: Control/AthenaKernel/AthenaKernel/errorcheck.h:422
GEV
#define GEV
Definition: PrintPhotonSF.cxx:25
columnar::CM
ColumnarModeXAOD CM
Definition: ObjectRange.h:141
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
WriteHepMC::WriteHepMC
WriteHepMC(const std::string &name, ISvcLocator *pSvcLocator)
Definition: WriteHepMC.cxx:10
WriteHepMC::initialize
StatusCode initialize()
Definition: WriteHepMC.cxx:20
WriteHepMC::execute
StatusCode execute()
Definition: WriteHepMC.cxx:48
WriteHepMC::m_units
std::string m_units
Definition: WriteHepMC.h:24
WriteHepMC.h
example.writer
writer
show summary of content
Definition: example.py:36
GenBase::initialize
virtual StatusCode initialize() override
Definition: GenBase.cxx:17
GenBase::event_const
const HepMC::GenEvent * event_const() const
Access the current signal event (const)
Definition: GenBase.h:83