Belle II Software prerelease-11-00-00a
HepevtReader Class Reference

Class to read Hepevt files and store the content in a MCParticle graph. More...

#include <HepevtReader.h>

Collaboration diagram for HepevtReader:

Public Member Functions

 BELLE2_DEFINE_EXCEPTION (HepEvtCouldNotOpenFileError, "Could not open file %1% !")
 Exception is thrown if the HepEvt file could not be opened.
 
 BELLE2_DEFINE_EXCEPTION (HepEvtInvalidDaughterIndicesError, "Line %1%: Invalid daughter indices d1=%2%, d2=%3%, N=%4% (0<=d1<=d2<=N required)")
 Exception is thrown if the given indices of the daughters are not valid.
 
 BELLE2_DEFINE_EXCEPTION (HepEvtHeaderNotValidError, "Line %1%: Event header not understood: %2%")
 Exception is thrown if the header specifying the event header could not be parsed.
 
 BELLE2_DEFINE_EXCEPTION (HepEvtConvertFieldError, "Line %1%: Could not convert field %2%: %3%")
 Exception is thrown if a field in the HepEvt file could not be converted to a number.
 
 BELLE2_DEFINE_EXCEPTION (HepEvtParticleFormatError, "Line %1%: Particle format not understood, got %2% fields !")
 Exception is thrown if the format of a line of the HepEvt file could not be parsed.
 
 BELLE2_DEFINE_EXCEPTION (HepEvtEmptyEventError, "Line %1%: Number of particles in event is %2% ! (This could mean EOF is reached.) ")
 Exception is thrown if the number of particles for this event is 0 or less.
 
 HepevtReader ()
 Constructor.
 
 ~HepevtReader ()
 Destructor.
 
void open (const std::string &filename)
 Opens an ascii file and prepares it for reading.
 
void closeCurrentInputFile ()
 Closes the current input file to allow opening the next one.
 
int getEvent (MCParticleGraph &graph, double &weight)
 Reads the next event and stores the result in the given MCParticle graph.
 
bool skipEvents (int n)
 Skips a given number of events.
 
int countEvents (const std::string &filename)
 Count events in the file by reading through it.
 

Public Attributes

int m_nVirtual
 The number of particles in each event with a set Virtual flag.
 
bool m_wrongSignPz
 Bool to indicate that HER and LER were swapped.
 

Protected Types

typedef boost::tokenizer< boost::char_separator< char > > tokenizer
 Just a typedef for simple use of the boost::tokenizer to split the lines.
 

Protected Member Functions

std::string getLine ()
 Returns the current line from the Hepevt ascii file.
 
int readEventHeader (int &eventID, double &eventWeight)
 Reads the event header from the hepevt file.
 
void readParticle (MCParticleGraph::GraphParticle &particle)
 Reads the information for a single particle from the Hepevt file.
 

Protected Attributes

int m_lineNr
 The current line number within the ascii file.
 
std::ifstream m_input
 The input stream of the ascii file.
 

Static Protected Attributes

static const boost::char_separator< char > sep
 The characters at which to split, defaults to ",; \t".
 

Detailed Description

Class to read Hepevt files and store the content in a MCParticle graph.

The reader supports retrieving the Hepevt information from an ascii text file. Both formats, the short and the long LUND 7 format are supported.

The events are read sequentially with the option of skipping a certain number of events. Random access of events is not possible.

Definition at line 33 of file HepevtReader.h.

Member Typedef Documentation

◆ tokenizer

typedef boost::tokenizer<boost::char_separator<char> > tokenizer
protected

Just a typedef for simple use of the boost::tokenizer to split the lines.

Definition at line 100 of file HepevtReader.h.

Constructor & Destructor Documentation

◆ HepevtReader()

HepevtReader ( )
inline

Constructor.

Definition at line 55 of file HepevtReader.h.

55: m_nVirtual(0), m_wrongSignPz(false), m_lineNr(0) {}

◆ ~HepevtReader()

~HepevtReader ( )
inline

Destructor.

Definition at line 60 of file HepevtReader.h.

60{ if (m_input) m_input.close(); }

Member Function Documentation

◆ closeCurrentInputFile()

void closeCurrentInputFile ( )
inline

Closes the current input file to allow opening the next one.

Definition at line 71 of file HepevtReader.h.

71{m_input.close();}

◆ countEvents()

int countEvents ( const std::string & filename)

Count events in the file by reading through it.

Parameters
filenameThe filename of the file to count events in.
Returns
The number of events in the file.

Definition at line 92 of file HepevtReader.cc.

93{
94 open(filename);
95
96 int count = 0;
97 int eventID;
98 double weight;
99
100 int nparticles;
101 while ((nparticles = readEventHeader(eventID, weight)) >= 0) {
102 count++;
103 for (int j = 0; j < nparticles; j++) getLine();
104 }
105
106 B2INFO("Counted " << count << " events in " << filename << ".");
107 return count;
108}
std::string getLine()
Returns the current line from the Hepevt ascii file.
void open(const std::string &filename)
Opens an ascii file and prepares it for reading.
int readEventHeader(int &eventID, double &eventWeight)
Reads the event header from the hepevt file.

◆ getEvent()

int getEvent ( MCParticleGraph & graph,
double & weight )

Reads the next event and stores the result in the given MCParticle graph.

Parameters
graphReference to the graph which should be filled with the information from the Hepevt file.
weightReference to the event weight which can be filled from the file.
Returns
event number if the event could be read and the number was provided in the file.

Definition at line 36 of file HepevtReader.cc.

37{
38 int eventID = -1;
39 int nparticles = readEventHeader(eventID, eventWeight);
40 if (nparticles <= 0) {
41 throw (HepEvtEmptyEventError() << m_lineNr << nparticles);
42 }
43
44 int first = graph.size();
45 //Make list of particles
46 for (int i = 0; i < nparticles; i++) {
47 graph.addParticle();
48 }
49 //Read particles from file
50 for (int i = 0; i < nparticles; ++i) {
51 MCParticleGraph::GraphParticle& p = graph[first + i];
52 readParticle(p);
53
54 if (m_wrongSignPz) { // this means we have to mirror Pz
55 ROOT::Math::PxPyPzEVector p4 = p.get4Vector();
56 p4.SetPz(-1.0 * p4.Pz());
57 p.set4Vector(p4);
58 }
59
60 //Check for sensible daughter indices
61 int d1 = p.getFirstDaughter();
62 int d2 = p.getLastDaughter();
63 if (d1 < 0 || d1 > nparticles || d2 < d1 || d2 > nparticles) {
64 throw (HepEvtInvalidDaughterIndicesError() << m_lineNr << d1 << d2 << nparticles);
65 }
66 if (d1 == 0) p.addStatus(MCParticle::c_StableInGenerator);
67 //Add decays
68 for (int index = d1; index <= d2; ++index) {
69 if (index > 0) p.decaysInto(graph[first + index - 1]);
70 }
71
72 //check if particle should be made virtual according to steering options:
73 if (i < m_nVirtual)
74 p.setVirtual();
75 }
76 return eventID;
77}
int m_lineNr
The current line number within the ascii file.
bool m_wrongSignPz
Bool to indicate that HER and LER were swapped.
void readParticle(MCParticleGraph::GraphParticle &particle)
Reads the information for a single particle from the Hepevt file.
int m_nVirtual
The number of particles in each event with a set Virtual flag.
size_t size() const
Return the number of particles in the graph.
@ c_StableInGenerator
bit 1: Particle is stable, i.e., not decaying in the generator.
Definition MCParticle.h:49
GraphParticle & addParticle()
Add new particle to the graph.

◆ getLine()

std::string getLine ( )
protected

Returns the current line from the Hepevt ascii file.

  • double eventWeight; /‍**< The event weight if provided in HepEvtfile else 1. *‍/ *‍/
    Returns
    The current line as a string.

Definition at line 115 of file HepevtReader.cc.

116{
117 std::string line;
118 do {
119 getline(m_input, line);
120 m_lineNr++;
121 size_t commentPos = line.find_first_of('#');
122 if (commentPos != string::npos) {
123 line = line.substr(0, commentPos);
124 }
125 boost::trim(line);
126 } while (line == "" && !m_input.eof());
127 return line;
128}
std::ifstream m_input
The input stream of the ascii file.

◆ open()

void open ( const std::string & filename)

Opens an ascii file and prepares it for reading.

Parameters
filenameThe filename of the Hepevt ascii file which should be read.

Definition at line 26 of file HepevtReader.cc.

27{
28 m_lineNr = 0;
29 if (m_input.is_open()) m_input.close();
30 m_input.clear();
31 m_input.open(filename.c_str());
32 if (!m_input) throw(HepEvtCouldNotOpenFileError() << filename);
33}

◆ readEventHeader()

int readEventHeader ( int & eventID,
double & eventWeight )
protected

Reads the event header from the hepevt file.

Parameters
eventIDreference to the eventID which can be read from the file
eventWeightreference to the eventWeight which can be read from the file
Returns
The number of particles for the current event.

Definition at line 131 of file HepevtReader.cc.

132{
133 //Get number of particles from file
134 int nparticles = -1;
135 string line = getLine();
136 if (line == "" || m_input.eof()) return -1;
137
138 vector<double> fields;
139 fields.reserve(15);
140
141 tokenizer tokens(line, sep);
142 int index(0);
143
144 for (const string& tok : tokens) {
145 ++index;
146 try {
147 fields.push_back(boost::lexical_cast<double>(tok));
148 } catch (boost::bad_lexical_cast& e) {
149 throw (HepEvtConvertFieldError() << m_lineNr << index << tok);
150 }
151 }
152
153 switch (fields.size()) {
154 case 1:
155 nparticles = static_cast<int>(fields[0]);
156 break;
157 case 2:
158 nparticles = static_cast<int>(fields[1]);
159 eventID = static_cast<int>(fields[0]);
160 break;
161 case 3:
162 nparticles = static_cast<int>(fields[1]);
163 eventID = static_cast<int>(fields[0]);
164 eventWeight = fields[2];
165 break;
166 default:
167 nparticles = static_cast<int>(fields[0]);
168 break;
169 }
170 return nparticles;
171}
static const boost::char_separator< char > sep
The characters at which to split, defaults to ",; \t".
boost::tokenizer< boost::char_separator< char > > tokenizer
Just a typedef for simple use of the boost::tokenizer to split the lines.

◆ readParticle()

void readParticle ( MCParticleGraph::GraphParticle & particle)
protected

Reads the information for a single particle from the Hepevt file.

Parameters
particleReference to the particle which will be filled with the information from the Hepevt file.

Definition at line 174 of file HepevtReader.cc.

175{
176 string line = getLine();
177 vector<double> fields;
178 fields.reserve(15);
179
180 tokenizer tokens(line, sep);
181 int index(0);
182
183 for (const string& tok : tokens) {
184 ++index;
185 try {
186 fields.push_back(boost::lexical_cast<double>(tok));
187 } catch (boost::bad_lexical_cast& e) {
188 throw (HepEvtConvertFieldError() << m_lineNr << index << tok);
189 }
190 }
191
192 switch (fields.size()) {
193 case 8:
194 particle.setStatus(MCParticle::c_PrimaryParticle);
195 particle.setPDG(static_cast<int>(fields[1]));
196 particle.setFirstDaughter(static_cast<int>(fields[2]));
197 particle.setLastDaughter(static_cast<int>(fields[3]));
198 particle.setMomentum(ROOT::Math::XYZVector(fields[4], fields[5], fields[6]));
199 particle.setMass(fields[7]);
200 break;
201 case 6:
202 particle.setStatus(MCParticle::c_PrimaryParticle);
203 particle.setPDG(static_cast<int>(fields[5]));
204 particle.setFirstDaughter(0);
205 particle.setLastDaughter(0);
206 particle.setMomentum(ROOT::Math::XYZVector(fields[0], fields[1], fields[2]));
207 particle.setMass(fields[4]);
208 particle.setEnergy(fields[3]);
209 break;
210 //modified Pit
211 case 9:
212 particle.setStatus(MCParticle::c_PrimaryParticle);
213 particle.setPDG(static_cast<int>(fields[8]));
214 particle.setFirstDaughter(0);
215 particle.setLastDaughter(0);
216 particle.setProductionVertex(ROOT::Math::XYZVector(fields[0], fields[1], fields[2])*Unit::mm);
217 particle.setMomentum(ROOT::Math::XYZVector(fields[3], fields[4], fields[5]));
218 particle.setMass(fields[6]);
219 particle.setEnergy(fields[7]);
220 break;
221 //end modified Pit
222 case 15:
223 particle.setStatus(MCParticle::c_PrimaryParticle);
224 particle.setPDG(static_cast<int>(fields[1]));
225 particle.setFirstDaughter(static_cast<int>(fields[4]));
226 particle.setLastDaughter(static_cast<int>(fields[5]));
227 particle.setMomentum(ROOT::Math::XYZVector(fields[6], fields[7], fields[8]));
228 //particle.setEnergy(fields[9]);
229 particle.setMass(fields[10]);
230 particle.setProductionVertex(ROOT::Math::XYZVector(fields[11], fields[12], fields[13])*Unit::mm);
231 particle.setProductionTime(fields[14]*Unit::mm / Const::speedOfLight);
232 particle.setValidVertex(true);
233 {
234 //Warn if energy in Hepevt file differs from calculated energy by more than 0.1%
235 const double& E = particle.getEnergy();
236 double dE = fabs(fields[9] - E) / E;
237 if (dE > 1e-3) {
238 B2WARNING(boost::format("line %d: Energy of particle does not match with expected energy: %.6e != %.6e")
239 % m_lineNr % fields[9] % E);
240 B2WARNING(boost::format("delta E = %.2f%% -> ignoring given value") % (dE * 100));
241 }
242 }
243 break;
244 default:
245 throw (HepEvtParticleFormatError() << m_lineNr << fields.size());
246 }
247}
R E
internal precision of FFTW codelets
static const double speedOfLight
[cm/ns]
Definition Const.h:695
@ c_PrimaryParticle
bit 0: Particle is primary particle.
Definition MCParticle.h:47
static const double mm
[millimeters]
Definition Unit.h:70

◆ skipEvents()

bool skipEvents ( int n)

Skips a given number of events.

Parameters
nThe number of events which should be skipped.
Returns
True if the events could be skipped.

Definition at line 80 of file HepevtReader.cc.

81{
82 int eventID;
83 double weight;
84 for (int i = 0; i < n; i++) {
85 int nparticles = readEventHeader(eventID, weight);
86 if (nparticles < 0) return false;
87 for (int j = 0; j < nparticles; j++) getLine();
88 }
89 return true;
90}

Member Data Documentation

◆ m_input

std::ifstream m_input
protected

The input stream of the ascii file.

Definition at line 105 of file HepevtReader.h.

◆ m_lineNr

int m_lineNr
protected

The current line number within the ascii file.

Definition at line 104 of file HepevtReader.h.

◆ m_nVirtual

int m_nVirtual

The number of particles in each event with a set Virtual flag.

Definition at line 95 of file HepevtReader.h.

◆ m_wrongSignPz

bool m_wrongSignPz

Bool to indicate that HER and LER were swapped.

Definition at line 96 of file HepevtReader.h.

◆ sep

const boost::char_separator< char > sep
staticprotected

The characters at which to split, defaults to ",; \t".

Definition at line 102 of file HepevtReader.h.


The documentation for this class was generated from the following files: