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#ifdef HEPMC3
31 m_hepmcio = HepMC3::deduce_reader(m_input_file);
32#else
33 m_hepmcio.reset( new HepMC::IO_GenEvent(m_input_file.c_str(), std::ios::in) );
34#endif
36 return StatusCode::SUCCESS;
37}
38
39
41
42 McEventCollection* mcEvtColl = nullptr;
43
44 if ( evtStore()->contains<McEventCollection>(m_mcEventKey) && evtStore()->retrieve(mcEvtColl, m_mcEventKey).isSuccess() ) {
45 if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "found an McEventCollecion in store" << endmsg;
46 } else {
47 // McCollection doesn't exist. Create it (empty)
48 if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "create new McEventCollecion in store" << endmsg;
49 mcEvtColl = new McEventCollection;
50 StatusCode status = evtStore()->record( mcEvtColl, m_mcEventKey );
51 if (status.isFailure()) {
52 msg(MSG::ERROR) << "Could not record McEventCollection" << endmsg;
53 return status;
54 }
55 }
56#ifdef HEPMC3
57 HepMC3::GenEvent* evt = new HepMC3::GenEvent();
58 if (m_hepmcio) {
59 m_hepmcio->read_event(*evt);
60 if (!evt->run_info()) evt->set_run_info(m_hepmcio->run_info());
62 evt->set_event_number(m_event_number);
63 evt->set_units(HepMC3::Units::MEV, HepMC3::Units::MM);
64 mcEvtColl->push_back(evt);
65 const auto cs = evt->cross_section();
66 double xs = 0;
67 if ( cs ){
68 xs=cs->xsec();
69 }
70 m_sum_xs = m_sum_xs+xs;
71
72 }
73
74#else
76 HepMC::GenEvent* evt = m_hepmcio->read_next_event();
77 if (evt) {
79 evt->set_event_number(m_event_number);
80 evt->use_units(HepMC::Units::MEV,HepMC::Units::MM);
81 mcEvtColl->push_back(evt);
82
83 HepMC::GenCrossSection* cs=evt->cross_section();
84 double xs = 0;
85 if ( cs ){
86 xs=cs->cross_section();
87 }
88 m_sum_xs = m_sum_xs+xs;
89
90 }
91#endif
92 return StatusCode::SUCCESS;
93}
94
96
97 if (m_sum_xs >0) std::cout << "MetaData: cross-section (nb)= " << m_sum_xs/(1000*m_event_number) <<std::endl;
98
99 return StatusCode::SUCCESS;
100}
101
102
103
104
105
#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:137
virtual StatusCode initialize() override
Definition GenBase.cxx:17
GenBase(const std::string &name, ISvcLocator *pSvcLocator)
Constructor.
Definition GenBase.cxx:11
virtual StatusCode finalize() override
HepMCReadFromFile(const std::string &name, ISvcLocator *pSvcLocator)
virtual StatusCode execute() override
std::unique_ptr< HepMC::IO_GenEvent > 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:114
MsgStream & msg
Definition testRead.cxx:32