ATLAS Offline Software
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 
7 #include "AtlasHepMC/GenEvent.h"
8 #include "AtlasHepMC/IO_HEPEVT.h"
12 #include "GaudiKernel/DataSvc.h"
13 
14 #include "StoreGate/StoreGateSvc.h"
15 
16 
17 HepMCReadFromFile::HepMCReadFromFile(const std::string& name, ISvcLocator* pSvcLocator) :
18  GenBase(name, pSvcLocator)
19 {
20  declareProperty("InputFile", m_input_file="events.hepmc");
21  m_event_number = 0;
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
35  m_event_number = 0;
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
75  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 
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
GenEvent.h
HepMCReadFromFile::m_input_file
std::string m_input_file
Definition: HepMCReadFromFile.h:24
HepMCReadFromFile::HepMCReadFromFile
HepMCReadFromFile(const std::string &name, ISvcLocator *pSvcLocator)
Definition: HepMCReadFromFile.cxx:17
AthCommonDataStore< AthCommonMsg< Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
HepMCReadFromFile::m_hepmcio
std::unique_ptr< HepMC::IO_GenEvent > m_hepmcio
Definition: HepMCReadFromFile.h:31
GenBase::m_mcEventKey
std::string m_mcEventKey
StoreGate key for the MC event collection (defaults to GEN_EVENT)
Definition: GenBase.h:137
HepMCReadFromFile::initialize
virtual StatusCode initialize() override
Definition: HepMCReadFromFile.cxx:26
HepMCReadFromFile::execute
virtual StatusCode execute() override
Definition: HepMCReadFromFile.cxx:40
McEventCollection
McEventCollection
Definition: GeneratorObjectsTPCnv.cxx:60
HepMCReadFromFile::finalize
virtual StatusCode finalize() override
Definition: HepMCReadFromFile.cxx:95
MM
@ MM
Definition: RegSelEnums.h:38
AthCommonMsg< Algorithm >::msgLvl
bool msgLvl(const MSG::Level lvl) const
Definition: AthCommonMsg.h:30
HEPEVT_Wrapper.h
LArG4FSStartPointFilter.evt
evt
Definition: LArG4FSStartPointFilter.py:42
ReaderFactory.h
AthCommonDataStore< AthCommonMsg< Algorithm > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
GenBase
Base class for common behaviour of MC truth algorithms.
Definition: GenBase.h:47
McEventCollection.h
HepMCReadFromFile::m_event_number
int m_event_number
Definition: HepMCReadFromFile.h:25
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
HepMCReadFromFile::m_sum_xs
double m_sum_xs
Definition: HepMCReadFromFile.h:26
CHECK
#define CHECK(...)
Evaluate an expression and check for errors.
Definition: Control/AthenaKernel/AthenaKernel/errorcheck.h:422
McEventCollection
This defines the McEventCollection, which is really just an ObjectVector of McEvent objects.
Definition: McEventCollection.h:33
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
DataVector::push_back
value_type push_back(value_type pElem)
Add an element to the end of the collection.
IO_HEPEVT.h
AthCommonMsg< Algorithm >::msg
MsgStream & msg() const
Definition: AthCommonMsg.h:24
merge.status
status
Definition: merge.py:17
python.Constants.VERBOSE
int VERBOSE
Definition: Control/AthenaCommon/python/Constants.py:14
StoreGateSvc.h
GenBase::initialize
virtual StatusCode initialize() override
Definition: GenBase.cxx:17
HepMCReadFromFile.h
GenCrossSection.h