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  m_sgSvc(0)
20 {
21  declareProperty("InputFile", m_input_file="events.hepmc");
22  m_event_number = 0;
23  m_sum_xs = 0;
24 }
25 
26 
29 
30  StatusCode sc = service("StoreGateSvc", m_sgSvc);
31  if (sc.isFailure()) {
32  msg(MSG::ERROR) << "Could not find StoreGateSvc" << endmsg;
33  return sc;
34  }
35 
36  // Initialize input file and event number
37 #ifdef HEPMC3
38  m_hepmcio = HepMC3::deduce_reader(m_input_file);
39 #else
40  m_hepmcio.reset( new HepMC::IO_GenEvent(m_input_file.c_str(), std::ios::in) );
41 #endif
42  m_event_number = 0;
43  return StatusCode::SUCCESS;
44 }
45 
46 
48 
49  McEventCollection* mcEvtColl = nullptr;
50 
51  if ( m_sgSvc->contains<McEventCollection>(m_mcEventKey) && m_sgSvc->retrieve(mcEvtColl, m_mcEventKey).isSuccess() ) {
52  if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "found an McEventCollecion in store" << endmsg;
53  } else {
54  // McCollection doesn't exist. Create it (empty)
55  if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "create new McEventCollecion in store" << endmsg;
56  mcEvtColl = new McEventCollection;
57  StatusCode status = m_sgSvc->record( mcEvtColl, m_mcEventKey );
58  if (status.isFailure()) {
59  msg(MSG::ERROR) << "Could not record McEventCollection" << endmsg;
60  return status;
61  }
62  }
63 #ifdef HEPMC3
64  HepMC3::GenEvent* evt = new HepMC3::GenEvent();
65  m_hepmcio->read_event(*evt);
66  if (m_hepmcio) {
67  if (!evt->run_info()) evt->set_run_info(m_hepmcio->run_info());
69  evt->set_event_number(m_event_number);
70  evt->set_units(HepMC3::Units::MEV, HepMC3::Units::MM);
71  mcEvtColl->push_back(evt);
72  const auto cs = evt->cross_section();
73  double xs = 0;
74  if ( cs ){
75  xs=cs->xsec();
76  }
77  m_sum_xs = m_sum_xs+xs;
78 
79  }
80 
81 #else
82  HepMC::GenEvent* evt = m_hepmcio->read_next_event();
84  if (evt) {
86  evt->set_event_number(m_event_number);
87  evt->use_units(HepMC::Units::MEV,HepMC::Units::MM);
88  mcEvtColl->push_back(evt);
89 
90  HepMC::GenCrossSection* cs=evt->cross_section();
91  double xs = 0;
92  if ( cs ){
93  xs=cs->cross_section();
94  }
95  m_sum_xs = m_sum_xs+xs;
96 
97  }
98 #endif
99  return StatusCode::SUCCESS;
100 }
101 
103 
104  if (m_sum_xs >0) std::cout << "MetaData: cross-section (nb)= " << m_sum_xs/(1000*m_event_number) <<std::endl;
105 
106  return StatusCode::SUCCESS;
107 }
108 
109 
110 
111 
112 
StoreGateSvc::record
StatusCode record(T *p2BRegistered, const TKEY &key)
Record an object with a key.
StoreGateSvc::contains
bool contains(const TKEY &key) const
Look up a keyed object in TDS (compare also tryRetrieve) returns false if object not available in TDS...
GenEvent.h
HepMCReadFromFile::m_input_file
std::string m_input_file
Definition: HepMCReadFromFile.h:24
HepMCReadFromFile::finalize
StatusCode finalize()
Definition: HepMCReadFromFile.cxx:102
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::initialize
StatusCode initialize()
Definition: HepMCReadFromFile.cxx:27
HepMCReadFromFile::m_hepmcio
std::unique_ptr< HepMC::IO_GenEvent > m_hepmcio
Definition: HepMCReadFromFile.h:33
GenBase::m_mcEventKey
std::string m_mcEventKey
StoreGate key for the MC event collection (defaults to GEN_EVENT)
Definition: GenBase.h:137
McEventCollection
McEventCollection
Definition: GeneratorObjectsTPCnv.cxx:60
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
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
StoreGateSvc::retrieve
StatusCode retrieve(const T *&ptr) const
Retrieve the default object into a const T*.
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:195
DataVector::push_back
value_type push_back(value_type pElem)
Add an element to the end of the collection.
HepMCReadFromFile::execute
StatusCode execute()
Definition: HepMCReadFromFile.cxx:47
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
HepMCReadFromFile::m_sgSvc
StoreGateSvc * m_sgSvc
Definition: HepMCReadFromFile.h:28