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
11#include "GaudiKernel/DataSvc.h"
12
14
15
16HepMCReadFromFile::HepMCReadFromFile(const std::string& name, ISvcLocator* pSvcLocator) :
17 GenBase(name, pSvcLocator)
18{
19 declareProperty("InputFile", m_input_file="events.hepmc");
21 m_sum_xs = 0;
22}
23
24
27
28 // Initialize input file and event number
29 m_hepmcio = HepMC3::deduce_reader(m_input_file);
31 return StatusCode::SUCCESS;
32}
33
34
35StatusCode HepMCReadFromFile::execute(const EventContext& /*ctx*/) {
36
37 McEventCollection* mcEvtColl = nullptr;
38
39 if ( evtStore()->contains<McEventCollection>(m_mcEventKey) && evtStore()->retrieve(mcEvtColl, m_mcEventKey).isSuccess() ) {
40 if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "found an McEventCollecion in store" << endmsg;
41 } else {
42 // McCollection doesn't exist. Create it (empty)
43 if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "create new McEventCollecion in store" << endmsg;
44 mcEvtColl = new McEventCollection;
45 StatusCode status = evtStore()->record( mcEvtColl, m_mcEventKey );
46 if (status.isFailure()) {
47 msg(MSG::ERROR) << "Could not record McEventCollection" << endmsg;
48 return status;
49 }
50 }
51 HepMC3::GenEvent* evt = new HepMC3::GenEvent();
52 if (m_hepmcio) {
53 m_hepmcio->read_event(*evt);
54 if (!evt->run_info()) evt->set_run_info(m_hepmcio->run_info());
56 evt->set_event_number(m_event_number);
57 evt->set_units(HepMC3::Units::MEV, HepMC3::Units::MM);
58 mcEvtColl->push_back(evt);
59 const auto cs = evt->cross_section();
60 double xs = 0;
61 if ( cs ){
62 xs=cs->xsec();
63 }
64 m_sum_xs = m_sum_xs+xs;
65
66 }
67 return StatusCode::SUCCESS;
68}
69
71
72 if (m_sum_xs >0) std::cout << "MetaData: cross-section (nb)= " << m_sum_xs/(1000*m_event_number) <<std::endl;
73
74 return StatusCode::SUCCESS;
75}
76
77
78
79
80
#define endmsg
#define CHECK(...)
Evaluate an expression and check for errors.
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
ServiceHandle< StoreGateSvc > & evtStore()
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:108
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