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  m_sgSvc(0)
80 {
81  // Set users' request
82  declareProperty("McEventKey", m_key = "GEN_EVENT");
83  declareProperty("HepEvtFile", m_input_file = "events.ascii");
84 }
85 
87 
88  msg(MSG::INFO) << ">>> ReadHepEvtFromAscii from Initialize" << endmsg;
89 
90  StatusCode sc = service("StoreGateSvc", m_sgSvc);
91 
92  if (sc.isFailure()) {
93  msg(MSG::ERROR) << "Could not find StoreGateSvc" << endmsg;
94  return sc;
95  }
96 #ifdef HEPMC3
97 //These things are not present
98 #else
99 
100  HepMC::HEPEVT_Wrapper::set_sizeof_int(4);
101  HepMC::HEPEVT_Wrapper::set_sizeof_real(8);
102  HepMC::HEPEVT_Wrapper::set_max_number_entries(10000);
103 
104 #endif
105  // Initialize input file
106  m_file=std::ifstream(m_input_file.c_str());
107  if( !m_file.is_open() ) return StatusCode::FAILURE;
108  // Initialization terminated
109  return StatusCode::SUCCESS;
110 }
112 
113  msg(MSG::INFO) << ">>> ReadHepEvtFromAscii from execute" << endmsg;
114 
115  McEventCollection* mcEvtColl;
116 
117  if ( m_sgSvc->contains<McEventCollection>(m_key) && m_sgSvc->retrieve(mcEvtColl, m_key).isSuccess() ) {
118  if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "found an McEventCollecion in store" << endmsg;
119  } else {
120  // McCollection doesn't exist. Create it (empty)
121  if (msgLvl(MSG::VERBOSE)) msg(MSG::VERBOSE) << "create new McEventCollecion in store" << endmsg;
122  mcEvtColl = new McEventCollection;
123  StatusCode status = m_sgSvc->record( mcEvtColl, m_key );
124  if (status.isFailure()) {
125  msg(MSG::ERROR) << "Could not record McEventCollection" << endmsg;
126  return status;
127  }
128  }
129 
130  HepMC::GenEvent* evt = new HepMC::GenEvent();
131  HepMC::HEPEVT_Wrapper::zero_everything();
132  bool fileok=read_hepevt_event_header();
133  for (int i=1; (i<=HepMC::HEPEVT_Wrapper::number_entries())&&fileok; i++) fileok=read_hepevt_particle(i);
134  if (!fileok) return StatusCode::FAILURE;
135 
136 #ifdef HEPMC3
137  HepMC::HEPEVT_Wrapper::HEPEVT_to_GenEvent(evt);
138 #else
139  HepMC::IO_HEPEVT hepio;
140  hepio.set_print_inconsistency_errors(0);
141  hepio.fill_next_event(evt);
142 #endif
143 
144  // Convert GeV to MeV
145 #ifdef HEPMC3
146  for ( auto p: evt->particles() ){
147 #else
148  for ( auto ip = evt->particles_begin(); ip != evt->particles_end(); ++ip ){
149  auto p=*ip;
150 #endif
151  HepMC::FourVector newMomentum(0.,0.,0.,0.);
152  newMomentum.setPx( p->momentum().px() * 1000. );
153  newMomentum.setPy( p->momentum().py() * 1000. );
154  newMomentum.setPz( p->momentum().pz() * 1000. );
155  newMomentum.setE( p->momentum().e() * 1000. );
156  p->set_momentum(newMomentum);
157 
158  }
160  mcEvtColl->push_back(evt);
161 
162  // End of execution for each event
163  return StatusCode::SUCCESS;
164 }
166 
167  msg(MSG::INFO) << ">>> ReadHepEvtFromAscii from finalize" << endmsg;
168 
169  if(m_file.is_open()) m_file.close();
170  // End of finalization step
171  return StatusCode::SUCCESS;
172 
173 }
174 #endif
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
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
max
#define max(a, b)
Definition: cfImp.cxx:41
ReadHepEvtFromAscii.h
ReadHepEvtFromAscii::m_sgSvc
StoreGateSvc * m_sgSvc
Definition: ReadHepEvtFromAscii.h:19
AthCommonDataStore< AthCommonMsg< Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
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
ReadHepEvtFromAscii::finalize
StatusCode finalize()
Definition: ReadHepEvtFromAscii.cxx:165
HEPEVT_Wrapper.h
LArG4FSStartPointFilter.evt
evt
Definition: LArG4FSStartPointFilter.py:42
HepMC::fillBarcodesAttribute
void fillBarcodesAttribute(GenEvent *)
Definition: GenEvent.h:504
ReadHepEvtFromAscii::read_hepevt_event_header
bool read_hepevt_event_header()
This class is only needed for HepMC2-based builds.
Definition: ReadHepEvtFromAscii.cxx:22
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
StoreGateSvc::retrieve
StatusCode retrieve(const T *&ptr) const
Retrieve the default object into a const T*.
ReadHepEvtFromAscii::m_key
std::string m_key
Definition: ReadHepEvtFromAscii.h:22
McEventCollection.h
lumiFormat.i
int i
Definition: lumiFormat.py:92
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:23
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
ReadHepEvtFromAscii::initialize
StatusCode initialize()
Definition: ReadHepEvtFromAscii.cxx:86
McEventCollection
This defines the McEventCollection, which is really just an ObjectVector of McEvent objects.
Definition: McEventCollection.h:33
AthAlgorithm
Definition: AthAlgorithm.h:47
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
ReadHepEvtFromAscii::execute
StatusCode execute()
Definition: ReadHepEvtFromAscii.cxx:111
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
ReadHepEvtFromAscii::m_file
std::ifstream m_file
Definition: ReadHepEvtFromAscii.h:24
merge.status
status
Definition: merge.py:17
python.Constants.VERBOSE
int VERBOSE
Definition: Control/AthenaCommon/python/Constants.py:14
StoreGateSvc.h