ATLAS Offline Software
ReadHepEvtFromAscii.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 #ifndef HEPMC3
5 
7 
8 
11 #include "AtlasHepMC/GenEvent.h"
12 #include "AtlasHepMC/IO_HEPEVT.h"
14 
15 #include "GaudiKernel/DataSvc.h"
16 
17 #include "StoreGate/StoreGateSvc.h"
18 #include <set>
19 #include <array>
20 
21 
23 {
24  const size_t max_e_buffer_size=512;
25  char buf_e[max_e_buffer_size];
26  bool eventline=false;
27  int im=0, pm=0;
28  while(!eventline)
29  {
30  m_file.getline(buf_e,max_e_buffer_size);
31  if( strlen(buf_e) == 0 ) return false;
32  std::stringstream st_e(buf_e);
33  char attr=' ';
34  eventline=false;
35  while (!eventline)
36  {
37  if (!(st_e>>attr)) break;
38  if (attr==' ') continue;
39  else eventline=false;
40  if (attr=='E')
41  {
42  eventline=static_cast<bool>(st_e>>im>>pm);
43  }
44  }
45  }
46  HepMC::HEPEVT_Wrapper::set_event_number(im);
47  HepMC::HEPEVT_Wrapper::set_number_entries(pm);
48  return eventline;
49 }
51 {
52  static constexpr size_t max_p_buffer_size=512;
53  static constexpr size_t max_v_buffer_size=512;
54  char buf_p[max_p_buffer_size];
55  char buf_v[max_v_buffer_size];
56  std::array<int, 6> intcodes {};
57  std::array<double, 5> fltcodes1 {};
58  std::array<double, 4> fltcodes2 {};
59  m_file.getline(buf_p,max_p_buffer_size);
60  if( strlen(buf_p) == 0 ) return false;
61  m_file.getline(buf_v,max_v_buffer_size);
62  if( strlen(buf_v) == 0 ) return false;
63  std::stringstream st_p(buf_p);
64  std::stringstream st_v(buf_v);
65  if (!static_cast<bool>(st_p>>intcodes[0]>>intcodes[1]>>intcodes[2]>>intcodes[3]>>intcodes[4]>>intcodes[5]>>fltcodes1[0]>>fltcodes1[1]>>fltcodes1[2]>>fltcodes1[3]>>fltcodes1[4])) { return false;}
66  if (!static_cast<bool>(st_v>>fltcodes2[0]>>fltcodes2[1]>>fltcodes2[2]>>fltcodes2[3])) { return false;}
67  HepMC::HEPEVT_Wrapper::set_status(i,intcodes[0]);
68  HepMC::HEPEVT_Wrapper::set_id(i,intcodes[1]);
69  HepMC::HEPEVT_Wrapper::set_parents(i,intcodes[2],std::max(intcodes[2],intcodes[3]));/* Pythia writes second mother 0*/
70  HepMC::HEPEVT_Wrapper::set_children(i,intcodes[4],intcodes[5]);
71  HepMC::HEPEVT_Wrapper::set_momentum(i,fltcodes1[0],fltcodes1[1],fltcodes1[2],fltcodes1[3]);
72  HepMC::HEPEVT_Wrapper::set_mass(i,fltcodes1[4]);
73  HepMC::HEPEVT_Wrapper::set_position(i,fltcodes2[0],fltcodes2[1],fltcodes2[2],fltcodes2[3]);
74  return true;
75 }
76 
77 ReadHepEvtFromAscii::ReadHepEvtFromAscii(const std::string& name, ISvcLocator* pSvcLocator)
78  : AthAlgorithm(name, pSvcLocator)
79 {
80  // Set users' request
81  declareProperty("McEventKey", m_key = "GEN_EVENT");
82  declareProperty("HepEvtFile", m_input_file = "events.ascii");
83 }
84 
86 
87  msg(MSG::INFO) << ">>> ReadHepEvtFromAscii from Initialize" << endmsg;
88 
89 #ifdef HEPMC3
90 //These things are not present
91 #else
92 
93  HepMC::HEPEVT_Wrapper::set_sizeof_int(4);
94  HepMC::HEPEVT_Wrapper::set_sizeof_real(8);
95  HepMC::HEPEVT_Wrapper::set_max_number_entries(10000);
96 
97 #endif
98  // Initialize input file
99  m_file=std::ifstream(m_input_file.c_str());
100  if( !m_file.is_open() ) return StatusCode::FAILURE;
101  // Initialization terminated
102  return StatusCode::SUCCESS;
103 }
105 
106  msg(MSG::INFO) << ">>> ReadHepEvtFromAscii from execute" << endmsg;
107 
108  McEventCollection* mcEvtColl;
109 
110  if ( evtStore()->contains<McEventCollection>(m_key) && evtStore()->retrieve(mcEvtColl, m_key).isSuccess() ) {
111  if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "found an McEventCollecion in store" << endmsg;
112  } else {
113  // McCollection doesn't exist. Create it (empty)
114  if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "create new McEventCollecion in store" << endmsg;
115  mcEvtColl = new McEventCollection;
116  StatusCode status = evtStore()->record( mcEvtColl, m_key );
117  if (status.isFailure()) {
118  msg(MSG::ERROR) << "Could not record McEventCollection" << endmsg;
119  return status;
120  }
121  }
122 
123  HepMC::GenEvent* evt = new HepMC::GenEvent();
124  HepMC::HEPEVT_Wrapper::zero_everything();
125  bool fileok=read_hepevt_event_header();
126  for (int i=1; (i<=HepMC::HEPEVT_Wrapper::number_entries())&&fileok; i++) fileok=read_hepevt_particle(i);
127  if (!fileok) return StatusCode::FAILURE;
128 
129 #ifdef HEPMC3
130  HepMC::HEPEVT_Wrapper::HEPEVT_to_GenEvent(evt);
131 #else
132  HepMC::IO_HEPEVT hepio;
133  hepio.set_print_inconsistency_errors(0);
134  hepio.fill_next_event(evt);
135 #endif
136 
137  // Convert GeV to MeV
138 #ifdef HEPMC3
139  for ( auto p: evt->particles() ){
140 #else
141  for ( auto ip = evt->particles_begin(); ip != evt->particles_end(); ++ip ){
142  auto p=*ip;
143 #endif
144  HepMC::FourVector newMomentum(0.,0.,0.,0.);
145  newMomentum.setPx( p->momentum().px() * 1000. );
146  newMomentum.setPy( p->momentum().py() * 1000. );
147  newMomentum.setPz( p->momentum().pz() * 1000. );
148  newMomentum.setE( p->momentum().e() * 1000. );
149  p->set_momentum(newMomentum);
150 
151  }
153  mcEvtColl->push_back(evt);
154 
155  // End of execution for each event
156  return StatusCode::SUCCESS;
157 }
159 
160  msg(MSG::INFO) << ">>> ReadHepEvtFromAscii from finalize" << endmsg;
161 
162  if(m_file.is_open()) m_file.close();
163  // End of finalization step
164  return StatusCode::SUCCESS;
165 
166 }
167 #endif
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
GenEvent.h
ReadHepEvtFromAscii.h
max
constexpr double max()
Definition: ap_fixedTest.cxx:33
McEventCollection
McEventCollection
Definition: GeneratorObjectsTPCnv.cxx:60
python.atlas_oh.im
im
Definition: atlas_oh.py:167
AthCommonMsg< Algorithm >::msgLvl
bool msgLvl(const MSG::Level lvl) const
Definition: AthCommonMsg.h:30
HEPEVT_Wrapper.h
ReadHepEvtFromAscii::finalize
virtual StatusCode finalize() override
Definition: ReadHepEvtFromAscii.cxx:158
LArG4FSStartPointFilter.evt
evt
Definition: LArG4FSStartPointFilter.py:42
HepMC::fillBarcodesAttribute
void fillBarcodesAttribute(GenEvent *)
Definition: GenEvent.h:626
ReadHepEvtFromAscii::read_hepevt_event_header
bool read_hepevt_event_header()
This class is only needed for HepMC2-based builds.
Definition: ReadHepEvtFromAscii.cxx:22
AthCommonDataStore< AthCommonMsg< Algorithm > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
ReadHepEvtFromAscii::m_key
std::string m_key
Definition: ReadHepEvtFromAscii.h:20
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:209
McEventCollection.h
lumiFormat.i
int i
Definition: lumiFormat.py:85
ReadHepEvtFromAscii::read_hepevt_particle
bool read_hepevt_particle(int i)
Definition: ReadHepEvtFromAscii.cxx:50
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
ReadHepEvtFromAscii::m_input_file
std::string m_input_file
Definition: ReadHepEvtFromAscii.h:21
AthCommonDataStore< AthCommonMsg< Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
Definition: AthCommonDataStore.h:145
find_tgc_unfilled_channelids.ip
ip
Definition: find_tgc_unfilled_channelids.py:3
ReadHepEvtFromAscii::ReadHepEvtFromAscii
ReadHepEvtFromAscii(const std::string &name, ISvcLocator *pSvcLocator)
Definition: ReadHepEvtFromAscii.cxx:77
McEventCollection
This defines the McEventCollection, which is really just an ObjectVector of McEvent objects.
Definition: McEventCollection.h:32
AthAlgorithm
Definition: AthAlgorithm.h:47
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
DataVector::push_back
value_type push_back(value_type pElem)
Add an element to the end of the collection.
ReadHepEvtFromAscii::initialize
virtual StatusCode initialize() override
Definition: ReadHepEvtFromAscii.cxx:85
python.Constants.INFO
int INFO
Definition: Control/AthenaCommon/python/Constants.py:15
IO_HEPEVT.h
AthCommonMsg< Algorithm >::msg
MsgStream & msg() const
Definition: AthCommonMsg.h:24
ReadHepEvtFromAscii::m_file
std::ifstream m_file
Definition: ReadHepEvtFromAscii.h:22
merge.status
status
Definition: merge.py:16
ReadHepEvtFromAscii::execute
virtual StatusCode execute() override
Definition: ReadHepEvtFromAscii.cxx:104
python.Constants.VERBOSE
int VERBOSE
Definition: Control/AthenaCommon/python/Constants.py:13
StoreGateSvc.h