ATLAS Offline Software
Loading...
Searching...
No Matches
HepMCReadFromFile.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3*/
4
12#include "GaudiKernel/DataSvc.h"
13
15
16
17HepMCReadFromFile::HepMCReadFromFile(const std::string& name, ISvcLocator* pSvcLocator) :
18 GenBase(name, pSvcLocator)
19{
20 declareProperty("InputFile", m_input_file="events.hepmc");
22 m_sum_xs = 0;
23}
24
25
28
29 // Initialize input file and event number
30 m_hepmcio = HepMC3::deduce_reader(m_input_file);
32 return StatusCode::SUCCESS;
33}
34
35
36StatusCode HepMCReadFromFile::execute(const EventContext& /*ctx*/) {
37
38 McEventCollection* mcEvtColl = nullptr;
39
40 if ( evtStore()->contains<McEventCollection>(m_mcEventKey) && evtStore()->retrieve(mcEvtColl, m_mcEventKey).isSuccess() ) {
41 if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "found an McEventCollecion in store" << endmsg;
42 } else {
43 // McCollection doesn't exist. Create it (empty)
44 if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "create new McEventCollecion in store" << endmsg;
45 mcEvtColl = new McEventCollection;
46 StatusCode status = evtStore()->record( mcEvtColl, m_mcEventKey );
47 if (status.isFailure()) {
48 msg(MSG::ERROR) << "Could not record McEventCollection" << endmsg;
49 return status;
50 }
51 }
52 HepMC3::GenEvent* evt = new HepMC3::GenEvent();
53 if (m_hepmcio) {
54 m_hepmcio->read_event(*evt);
55 if (!evt->run_info()) evt->set_run_info(m_hepmcio->run_info());
57 evt->set_event_number(m_event_number);
58 evt->set_units(HepMC3::Units::MEV, HepMC3::Units::MM);
59 mcEvtColl->push_back(evt);
60 const auto cs = evt->cross_section();
61 double xs = 0;
62 if ( cs ){
63 xs=cs->xsec();
64 }
65 m_sum_xs = m_sum_xs+xs;
66
67 }
68 return StatusCode::SUCCESS;
69}
70
72
73 if (m_sum_xs >0) std::cout << "MetaData: cross-section (nb)= " << m_sum_xs/(1000*m_event_number) <<std::endl;
74
75 return StatusCode::SUCCESS;
76}
77
78
79
80
81
#define endmsg
#define CHECK(...)
Evaluate an expression and check for errors.
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
bool msgLvl(const MSG::Level lvl) const
value_type push_back(value_type pElem)
Add an element to the end of the collection.
std::string m_mcEventKey
StoreGate key for the MC event collection (defaults to GEN_EVENT).
Definition GenBase.h:134
virtual StatusCode initialize() override
Definition GenBase.cxx:17
GenBase(const std::string &name, ISvcLocator *pSvcLocator)
Constructor.
Definition GenBase.cxx:11
virtual StatusCode execute(const EventContext &ctx) override
Execute method.
virtual StatusCode finalize() override
HepMCReadFromFile(const std::string &name, ISvcLocator *pSvcLocator)
std::shared_ptr< HepMC3::Reader > m_hepmcio
virtual StatusCode initialize() override
This defines the McEventCollection, which is really just an ObjectVector of McEvent objectsFile: Gene...
bool contains(const std::string &s, const std::string &regx)
does a string contain the substring
Definition hcg.cxx:116
MsgStream & msg
Definition testRead.cxx:32