Belle II Software development
LHEReader Class Reference

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

#include <LHEReader.h>

Public Member Functions

 BELLE2_DEFINE_EXCEPTION (LHECouldNotOpenFileError, "Could not open file %1% !")
 Exception is thrown if the LHE file could not be opened.
 
 BELLE2_DEFINE_EXCEPTION (LHEInvalidDaughterIndicesError, "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 (LHEHeaderNotValidError, "Line %1%: Event header not understood: %2%")
 Exception is thrown if the header specifying the event header could not be parsed.
 
 BELLE2_DEFINE_EXCEPTION (LHEConvertFieldError, "Line %1%: Could not convert field %2%: %3%")
 Exception is thrown if a field in the LHE file could not be converted to a number.
 
 BELLE2_DEFINE_EXCEPTION (LHEParticleFormatError, "Line %1%: Particle format not understood, got %2% fields !")
 Exception is thrown if the format of a line of the LHE file could not be parsed.
 
 BELLE2_DEFINE_EXCEPTION (LHEEmptyEventError, "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.
 
 LHEReader ()
 Constructor.
 
 ~LHEReader ()
 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.
 
void setInitialIndex (int index)
 Set the maximum index of particles in each event that must be set as c_Initial (1-based).
 
void setVirtualIndex (int index)
 Set the maximum index of particles in each event that must be set as c_IsVirtual (1-based).
 

Public Attributes

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 LHE ascii file.
 
int readEventHeader (double &eventWeight)
 Reads the event header from the hepevt file.
 
int readParticle (MCParticleGraph::GraphParticle &particle)
 Reads the information for a single particle from the LHE 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.
 
int m_indexInitial
 Maximum index of particles in each event that must be set as c_Initial (1-based).
 
int m_indexVirtual
 Maximum index of particles in each event that must be set as c_IsVirtual (1-based).
 

Static Protected Attributes

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

Detailed Description

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

The class is very similar to the HepEvt Reader

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 31 of file LHEReader.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 106 of file LHEReader.h.

Constructor & Destructor Documentation

◆ LHEReader()

LHEReader ( )
inline

Constructor.

Definition at line 52 of file LHEReader.h.

52 :
53 m_wrongSignPz(false),
54 m_lineNr(0),
55 m_indexInitial(0),
56 m_indexVirtual(0)
57 {}

◆ ~LHEReader()

~LHEReader ( )
inline

Destructor.

Definition at line 62 of file LHEReader.h.

62{ 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 73 of file LHEReader.h.

73{m_input.close();}

◆ 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 LHE 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 32 of file LHEReader.cc.

33{
34 int nparticles = readEventHeader(eventWeight);
35 if (nparticles <= 0) {
36 throw (LHEEmptyEventError() << m_lineNr << nparticles);
37 }
38
39 int first = graph.size();
40 //Make list of particles
41 for (int i = 0; i < nparticles; i++) {
42 graph.addParticle();
43 }
44
45 //Read particles from file
46 for (int i = 0; i < nparticles; ++i) {
47 MCParticleGraph::GraphParticle& p = graph[first + i];
48 int mother = readParticle(p);
49
50 // add the mother
51 if (mother > 0) {
52 MCParticleGraph::GraphParticle* q = &graph[mother - 1];
53 p.comesFrom(*q);
54 }
55
56 // if positron goes to positive z-direction, we have to rotate along y-axis by 180deg
57 if (m_wrongSignPz) {
58 ROOT::Math::PxPyPzEVector p4 = p.get4Vector();
59 p4.SetPz(-1.0 * p4.Pz());
60 p4.SetPx(-1.0 * p4.Px());
61 p.set4Vector(p4);
62 }
63
64 // initial 2 (e+/e-), virtual 3 (Z/gamma*)
65 // check if particle should be made virtual according to steering options:
66 if (i < m_indexVirtual && i >= m_indexInitial)
67 p.addStatus(MCParticle::c_IsVirtual);
68
69 if (i < m_indexInitial)
70 p.addStatus(MCParticle::c_Initial);
71
72 if (m_indexVirtual < m_indexInitial) B2WARNING("IsVirtual particle requested but is overwritten by Initial");
73
74 }
75 return -1;
76}
int readParticle(MCParticleGraph::GraphParticle &particle)
Reads the information for a single particle from the LHE file.
Definition LHEReader.cc:158
int m_lineNr
The current line number within the ascii file.
Definition LHEReader.h:110
int m_indexVirtual
Maximum index of particles in each event that must be set as c_IsVirtual (1-based).
Definition LHEReader.h:134
int m_indexInitial
Maximum index of particles in each event that must be set as c_Initial (1-based).
Definition LHEReader.h:132
bool m_wrongSignPz
Bool to indicate that HER and LER were swapped.
Definition LHEReader.h:102
int readEventHeader(double &eventWeight)
Reads the event header from the hepevt file.
Definition LHEReader.cc:111
size_t size() const
Return the number of particles in the graph.
@ c_Initial
bit 5: Particle is initial such as e+ or e- and not going to Geant4
Definition MCParticle.h:57
@ c_IsVirtual
bit 4: Particle is virtual and not going to Geant4.
Definition MCParticle.h:55
GraphParticle & addParticle()
Add new particle to the graph.

◆ getLine()

std::string getLine ( )
protected

Returns the current line from the LHE ascii file.

Returns
The current line as a string.

Definition at line 94 of file LHEReader.cc.

95{
96 std::string line;
97 do {
98 getline(m_input, line);
99 m_lineNr++;
100 size_t commentPos = line.find_first_of('#');
101 if (commentPos != string::npos) {
102 line = line.substr(0, commentPos);
103 }
104 boost::trim(line);
105
106 } while (line == "" && !m_input.eof());
107
108 return line;
109}
std::ifstream m_input
The input stream of the ascii file.
Definition LHEReader.h:111

◆ open()

void open ( const std::string & filename)

Opens an ascii file and prepares it for reading.

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

Definition at line 25 of file LHEReader.cc.

26{
27 m_lineNr = 0;
28 m_input.open(filename.c_str());
29 if (!m_input) throw(LHECouldNotOpenFileError() << filename);
30}

◆ readEventHeader()

int readEventHeader ( double & eventWeight)
protected

Reads the event header from the hepevt file.

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

Definition at line 111 of file LHEReader.cc.

112{
113
114 // Search for next <event>
115 std::string line2;
116 do {
117 getline(m_input, line2);
118 m_lineNr++;
119 size_t commentPos = line2.find_first_of('#');
120 if (commentPos != string::npos) {
121 line2 = line2.substr(0, commentPos);
122 }
123 boost::trim(line2);
124
125 } while (line2 != "<event>" && !m_input.eof());
126
127 //Get number of particles from file
128 int nparticles = -1;
129 string line = getLine();
130
131 if (line == "" || m_input.eof()) return -1;
132
133 vector<double> fields;
134 fields.reserve(15);
135
136 tokenizer tokens(line, sep);
137 int index(0);
138
139 for (const string& tok : tokens) {
140 ++index;
141 try {
142 fields.push_back(boost::lexical_cast<double>(tok));
143 } catch (boost::bad_lexical_cast& e) {
144 throw (LHEConvertFieldError() << m_lineNr << index << tok);
145 }
146 }
147
148 if (fields.size() < 3) {
149 nparticles = static_cast<int>(fields[0]);
150 eventWeight = 1.0;
151 } else {
152 nparticles = static_cast<int>(fields[0]);
153 eventWeight = static_cast<double>(fields[2]);
154 }
155 return nparticles;
156}
std::string getLine()
Returns the current line from the LHE ascii file.
Definition LHEReader.cc:94
static const boost::char_separator< char > sep
The characters at which to split, defaults to ",; \t".
Definition LHEReader.h:108
boost::tokenizer< boost::char_separator< char > > tokenizer
Just a typedef for simple use of the boost::tokenizer to split the lines.
Definition LHEReader.h:106

◆ readParticle()

int readParticle ( MCParticleGraph::GraphParticle & particle)
protected

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

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

Definition at line 158 of file LHEReader.cc.

159{
160 int mother = -1;
161
162 string line = getLine();
163 vector<double> fields;
164 fields.reserve(15);
165
166 tokenizer tokens(line, sep);
167 int index(0);
168
169 for (const string& tok : tokens) {
170 ++index;
171 try {
172 fields.push_back(boost::lexical_cast<double>(tok));
173 } catch (boost::bad_lexical_cast& e) {
174 throw (LHEConvertFieldError() << m_lineNr << index << tok);
175 }
176 }
177
178 switch (fields.size()) {
179 case 13:
180 particle.addStatus(MCParticle::c_PrimaryParticle);
181 particle.setPDG(static_cast<int>(fields[0]));
182 mother = static_cast<int>(fields[2]);
183 particle.setMomentum(ROOT::Math::XYZVector(fields[6], fields[7], fields[8]));
184 particle.setEnergy(fields[9]);
185 particle.setMass(fields[10]);
186 break;
187 default:
188 throw (LHEParticleFormatError() << m_lineNr << fields.size());
189 }
190
191 return mother;
192}
@ c_PrimaryParticle
bit 0: Particle is primary particle.
Definition MCParticle.h:47

◆ setInitialIndex()

void setInitialIndex ( int index)
inline

Set the maximum index of particles in each event that must be set as c_Initial (1-based).

Parameters
[in]indexMaximum index for c_Initial.

Definition at line 94 of file LHEReader.h.

94{ m_indexInitial = index; }

◆ setVirtualIndex()

void setVirtualIndex ( int index)
inline

Set the maximum index of particles in each event that must be set as c_IsVirtual (1-based).

Parameters
[in]indexMaximum index for c_IsVirtual.

Definition at line 100 of file LHEReader.h.

100{ m_indexVirtual = index; }

◆ 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 79 of file LHEReader.cc.

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

Member Data Documentation

◆ m_indexInitial

int m_indexInitial
protected

Maximum index of particles in each event that must be set as c_Initial (1-based).

Definition at line 132 of file LHEReader.h.

◆ m_indexVirtual

int m_indexVirtual
protected

Maximum index of particles in each event that must be set as c_IsVirtual (1-based).

Definition at line 134 of file LHEReader.h.

◆ m_input

std::ifstream m_input
protected

The input stream of the ascii file.

Definition at line 111 of file LHEReader.h.

◆ m_lineNr

int m_lineNr
protected

The current line number within the ascii file.

Definition at line 110 of file LHEReader.h.

◆ m_wrongSignPz

bool m_wrongSignPz

Bool to indicate that HER and LER were swapped.

Definition at line 102 of file LHEReader.h.

◆ sep

const boost::char_separator< char > sep
staticprotected

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

Definition at line 108 of file LHEReader.h.


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