ATLAS Offline Software
Loading...
Searching...
No Matches
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
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
15WriteHepMC::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));
32 m_lengthunit = HepMC3::Units::length_unit(m_units.substr(3,2));
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
53StatusCode WriteHepMC::execute() {
54 // Just write out the first (i.e. signal) event in the collection
55#ifdef HEPMC3
56 HepMC3::GenEvent ev (*event_const());
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());
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}
#define ATH_MSG_WARNING(x)
#define CHECK(...)
Evaluate an expression and check for errors.
This class provides a unique identification for each event, in terms of run/event number and/or a tim...
Property holding a SG store/key/clid from which a ReadHandle is made.
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
virtual StatusCode initialize() override
Definition GenBase.cxx:17
GenBase(const std::string &name, ISvcLocator *pSvcLocator)
Constructor.
Definition GenBase.cxx:11
const HepMC::GenEvent * event_const() const
Access the current signal event (const)
Definition GenBase.h:83
virtual bool isValid() override final
Can the handle be successfully dereferenced?
StatusCode execute() override
HepMC::Units::MomentumUnit m_momentumunit
Definition WriteHepMC.h:31
WriteHepMC(const std::string &name, ISvcLocator *pSvcLocator)
std::string m_outfile
Definition WriteHepMC.h:20
int m_precision
Definition WriteHepMC.h:21
StatusCode initialize() override
std::string m_units
Definition WriteHepMC.h:23
HepMC::Units::LengthUnit m_lengthunit
Definition WriteHepMC.h:32
std::string m_format
Definition WriteHepMC.h:22
std::unique_ptr< HepMC::IO_GenEvent > m_hepmcio
Definition WriteHepMC.h:30
int ev
Definition globals.cxx:25