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
8// Additional includes for dealing with event numbers
11#include "EventInfo/EventInfo.h"
12#include "EventInfo/EventID.h"
13
14WriteHepMC::WriteHepMC(const std::string& name, ISvcLocator* pSvcLocator)
15 : GenBase(name, pSvcLocator)
16{
17 declareProperty("OutputFile", m_outfile="events.hepmc");
18 declareProperty("Precision", m_precision=8);
19 declareProperty("Format", m_format="hepmc3");
20 declareProperty("Units", m_units="MEVMM");
21}
22
23
26 if (m_units.size() != 5) {
27 return StatusCode::FAILURE;
28 }
29 m_momentumunit = HepMC3::Units::momentum_unit(m_units.substr(0,3));
30 m_lengthunit = HepMC3::Units::length_unit(m_units.substr(3,2));
31 if (m_format == "hepmc2" || m_format == "ascii") {
32 auto writer = std::make_shared<HepMC3::WriterAsciiHepMC2>(m_outfile);
33 writer->set_precision(m_precision);
35
36 }
37 if (m_format == "hepmc3" || m_format == "asciiv3") {
38 auto writer = std::make_shared<HepMC3::WriterAscii>(m_outfile);
39 writer->set_precision(m_precision);
41 }
42 return StatusCode::SUCCESS;
43}
44
45
46StatusCode WriteHepMC::execute(const EventContext& ctx) {
47 // Just write out the first (i.e. signal) event in the collection
48 HepMC3::GenEvent ev (*event_const(ctx));
50 if (ev.event_number()==1){
51 // Get the event number. Full fall back - just set it to 1.
52 int event_number = 1;
53 // First attempt: xAOD::EventInfo (new-style EVNT)
54 SG::ReadHandle<xAOD::EventInfo> mc_ei{"McEventInfo", ctx};
55 if (!mc_ei.isValid()){
56 // Second attempt: old-style EventInfo
57 SG::ReadHandle<EventInfo> og_mc_ei{"McEventInfo", ctx};
58 if (!og_mc_ei.isValid()){
59 // Give up and let people know that we fell through
60 ATH_MSG_WARNING("No McEventInfo found in SG - no event numbers available");
61 } else {
62 // Second one hit - get the event number from the OG event info
63 event_number = og_mc_ei->event_ID()->event_number();
64 }
65 } else {
66 // First one hit - get the event number from the xAOD event info
67 event_number = mc_ei->eventNumber();
68 }
69 ev.set_event_number(event_number);
70 }
71 m_hepmcio->write_event(ev);
72 return StatusCode::SUCCESS;
73}
#define ATH_MSG_WARNING(x)
#define CHECK(...)
Evaluate an expression and check for errors.
std::shared_ptr< HepMC3::Writer > writer
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
const HepMC::GenEvent * event_const(const EventContext &ctx) const
Access the current signal event (const).
Definition GenBase.h:81
GenBase(const std::string &name, ISvcLocator *pSvcLocator)
Constructor.
Definition GenBase.cxx:11
virtual bool isValid() override final
Can the handle be successfully dereferenced?
StatusCode execute(const EventContext &ctx) override
Execute method.
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
HepMC3::Units::MomentumUnit m_momentumunit
Definition WriteHepMC.h:26
std::shared_ptr< HepMC3::Writer > m_hepmcio
Definition WriteHepMC.h:25
std::string m_format
Definition WriteHepMC.h:22
HepMC3::Units::LengthUnit m_lengthunit
Definition WriteHepMC.h:27
int ev
Definition globals.cxx:25