ATLAS Offline Software
Loading...
Searching...
No Matches
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
6
7
8
11#include "AtlasHepMC/GenEvent.h"
14
15#include "GaudiKernel/DataSvc.h"
16
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
77ReadHepEvtFromAscii::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
#define endmsg
AthAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Constructor with parameters:
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
bool msgLvl(const MSG::Level lvl) const
MsgStream & msg() const
value_type push_back(value_type pElem)
Add an element to the end of the collection.
This defines the McEventCollection, which is really just an ObjectVector of McEvent objectsFile: Gene...
ReadHepEvtFromAscii(const std::string &name, ISvcLocator *pSvcLocator)
virtual StatusCode initialize() override
virtual StatusCode execute() override
virtual StatusCode finalize() override
bool read_hepevt_event_header()
This class is only needed for HepMC2-based builds.
bool contains(const std::string &s, const std::string &regx)
does a string contain the substring
Definition hcg.cxx:114
void fillBarcodesAttribute(GenEvent *)
Definition GenEvent.h:628