ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
Generators
TruthIO
src
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
#include "
AtlasHepMC/IO_GenEvent.h
"
7
#include "
AtlasHepMC/ReaderFactory.h
"
8
// Additional includes for dealing with event numbers
9
#include "
StoreGate/ReadHandleKey.h
"
10
#include "
xAODEventInfo/EventInfo.h
"
11
#include "EventInfo/EventInfo.h"
12
#include "
EventInfo/EventID.h
"
13
14
WriteHepMC::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
24
StatusCode
WriteHepMC::initialize
() {
25
CHECK
(
GenBase::initialize
());
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
);
34
m_hepmcio
=
writer
;
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
);
40
m_hepmcio
=
writer
;
41
}
42
return
StatusCode::SUCCESS;
43
}
44
45
46
StatusCode
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));
49
ev
.set_units(
m_momentumunit
,
m_lengthunit
);
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
}
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition
AthMsgStreamMacros.h:32
CHECK
#define CHECK(...)
Evaluate an expression and check for errors.
Definition
Control/AthenaKernel/AthenaKernel/errorcheck.h:422
writer
std::shared_ptr< HepMC3::Writer > writer
EventID.h
This class provides a unique identification for each event, in terms of run/event number and/or a tim...
IO_GenEvent.h
ReaderFactory.h
ReadHandleKey.h
Property holding a SG store/key/clid from which a ReadHandle is made.
WriteHepMC.h
AthCommonAlgorithm< Gaudi::Algorithm >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
Definition
AthCommonDataStore.h:145
GenBase::initialize
virtual StatusCode initialize() override
Definition
GenBase.cxx:17
GenBase::event_const
const HepMC::GenEvent * event_const(const EventContext &ctx) const
Access the current signal event (const).
Definition
GenBase.h:79
GenBase::GenBase
GenBase(const std::string &name, ISvcLocator *pSvcLocator)
Constructor.
Definition
GenBase.cxx:11
SG::ReadHandle
Definition
StoreGate/StoreGate/ReadHandle.h:67
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
WriteHepMC::execute
StatusCode execute(const EventContext &ctx) override
Execute method.
Definition
WriteHepMC.cxx:46
WriteHepMC::WriteHepMC
WriteHepMC(const std::string &name, ISvcLocator *pSvcLocator)
Definition
WriteHepMC.cxx:14
WriteHepMC::m_outfile
std::string m_outfile
Definition
WriteHepMC.h:20
WriteHepMC::m_precision
int m_precision
Definition
WriteHepMC.h:21
WriteHepMC::initialize
StatusCode initialize() override
Definition
WriteHepMC.cxx:24
WriteHepMC::m_units
std::string m_units
Definition
WriteHepMC.h:23
WriteHepMC::m_momentumunit
HepMC3::Units::MomentumUnit m_momentumunit
Definition
WriteHepMC.h:26
WriteHepMC::m_hepmcio
std::shared_ptr< HepMC3::Writer > m_hepmcio
Definition
WriteHepMC.h:25
WriteHepMC::m_format
std::string m_format
Definition
WriteHepMC.h:22
WriteHepMC::m_lengthunit
HepMC3::Units::LengthUnit m_lengthunit
Definition
WriteHepMC.h:27
ev
int ev
Definition
globals.cxx:25
EventInfo.h
Generated on
for ATLAS Offline Software by
1.17.0