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 // Additional includes for dealing with event numbers
12 #include "EventInfo/EventInfo.h"
13 #include "EventInfo/EventID.h"
14 
15 WriteHepMC::WriteHepMC(const std::string& name, ISvcLocator* pSvcLocator)
16  : GenBase(name, pSvcLocator)
17 {
18  declareProperty("OutputFile", m_outfile="events.hepmc");
19  declareProperty("Precision", m_precision=8);
20  declareProperty("Format", m_format="hepmc2");
21  declareProperty("Units", m_units="MEVMM");
22 }
23 
24 
27  if (m_units.size() != 5) {
28  return StatusCode::FAILURE;
29  }
30 #ifdef HEPMC3
31  m_momentumunit = HepMC3::Units::momentum_unit(m_units.substr(0,3));
33  if (m_format == "hepmc2") {
34  auto writer = new HepMC3::WriterAsciiHepMC2(m_outfile);
35  writer->set_precision(m_precision);
36  m_hepmcio.reset(writer);
37  }
38  if (m_format == "hepmc3") {
39  auto writer = new HepMC3::WriterAscii(m_outfile);
40  writer->set_precision(m_precision);
41  m_hepmcio.reset(writer);
42  }
43 #else
44  m_momentumunit = (m_units.substr(0,3) == "MEV") ? HepMC::Units::MEV : HepMC::Units::GEV;
45  m_lengthunit = (m_units.substr(3,2) == "CM") ? HepMC::Units::CM : HepMC::Units::MM;
46  m_hepmcio.reset( new HepMC::IO_GenEvent(m_outfile) );
47  m_hepmcio->precision(m_precision);
48 #endif
49  return StatusCode::SUCCESS;
50 }
51 
52 
54  // Just write out the first (i.e. signal) event in the collection
55 #ifdef HEPMC3
56  HepMC3::GenEvent ev (*event_const());
57  ev.set_units(m_momentumunit,m_lengthunit);
58  if (ev.event_number()==1){
59  // Get the event number. Full fall back - just set it to 1.
60  int event_number = 1;
61  // Grab the contact for the current thread so we can use ReadHandles nicely
62  const EventContext& context = Gaudi::Hive::currentContext();
63  // First attempt: xAOD::EventInfo (new-style EVNT)
64  SG::ReadHandle<xAOD::EventInfo> mc_ei{"McEventInfo", context};
65  if (!mc_ei.isValid()){
66  // Second attempt: old-style EventInfo
67  SG::ReadHandle<EventInfo> og_mc_ei{"McEventInfo", context};
68  if (!og_mc_ei.isValid()){
69  // Give up and let people know that we fell through
70  ATH_MSG_WARNING("No McEventInfo found in SG - no event numbers available");
71  } else {
72  // Second one hit - get the event number from the OG event info
73  event_number = og_mc_ei->event_ID()->event_number();
74  }
75  } else {
76  // First one hit - get the event number from the xAOD event info
77  event_number = mc_ei->eventNumber();
78  }
79  ev.set_event_number(event_number);
80  }
81  m_hepmcio->write_event(ev);
82 #else
83  HepMC::GenEvent ev (*event_const());
84  ev.use_units(m_momentumunit,m_lengthunit);
85  if (ev.event_number()==1){
86  // Get the event number. Full fall back - just set it to 1.
87  int event_number = 1;
88  // Grab the contact for the current thread so we can use ReadHandles nicely
89  const EventContext& context = Gaudi::Hive::currentContext();
90  // First attempt: xAOD::EventInfo (new-style EVNT)
91  SG::ReadHandle<xAOD::EventInfo> mc_ei{"McEventInfo", context};
92  if (!mc_ei.isValid()){
93  // Second attempt: old-style EventInfo
94  SG::ReadHandle<EventInfo> og_mc_ei{"McEventInfo", context};
95  if (!og_mc_ei.isValid()){
96  // Give up and let people know that we fell through
97  ATH_MSG_WARNING("No McEventInfo found in SG - no event numbers available");
98  } else {
99  // Second one hit - get the event number from the OG event info
100  event_number = og_mc_ei->event_ID()->event_number();
101  }
102  } else {
103  // First one hit - get the event number from the xAOD event info
104  event_number = mc_ei->eventNumber();
105  }
106  ev.set_event_number(event_number);
107  }
108  m_hepmcio->write_event(&ev);
109 #endif
110  return StatusCode::SUCCESS;
111 }
ReadHandleKey.h
Property holding a SG store/key/clid from which a ReadHandle is made.
WriteHepMC::initialize
StatusCode initialize() override
Definition: WriteHepMC.cxx:25
WriteHepMC::m_lengthunit
HepMC::Units::LengthUnit m_lengthunit
Definition: WriteHepMC.h:32
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:67
WriteHepMC::m_outfile
std::string m_outfile
Definition: WriteHepMC.h:20
MM
@ MM
Definition: RegSelEnums.h:38
WriteHepMC::m_format
std::string m_format
Definition: WriteHepMC.h:22
WriteHepMC::execute
StatusCode execute() override
Definition: WriteHepMC.cxx:53
length_unit
constexpr double length_unit
Definition: ActsDetectorElement.cxx:51
WriteHepMC::m_hepmcio
std::unique_ptr< HepMC::IO_GenEvent > m_hepmcio
Definition: WriteHepMC.h:30
WriteHepMC::m_precision
int m_precision
Definition: WriteHepMC.h:21
EventID.h
This class provides a unique identification for each event, in terms of run/event number and/or a tim...
WriteHepMC::m_momentumunit
HepMC::Units::MomentumUnit m_momentumunit
Definition: WriteHepMC.h:31
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:145
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
WriteHepMC::WriteHepMC
WriteHepMC(const std::string &name, ISvcLocator *pSvcLocator)
Definition: WriteHepMC.cxx:15
WriteHepMC::m_units
std::string m_units
Definition: WriteHepMC.h:23
EventInfo.h
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
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