Belle II Software development
LHEReader.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#include <framework/gearbox/Unit.h>
10#include <framework/gearbox/Const.h>
11#include <framework/logging/Logger.h>
12#include <generators/lhe/LHEReader.h>
13#include <mdst/dataobjects/MCParticle.h>
14
15#include <string>
16#include <boost/lexical_cast.hpp>
17#include <boost/algorithm/string.hpp>
18#include <boost/format.hpp>
19
20#include <TF1.h>
21
22using namespace std;
23using namespace Belle2;
24
25const boost::char_separator<char> LHEReader::sep(",; \t");
26
27void LHEReader::open(const string& filename)
28{
29 m_lineNr = 0;
30 m_input.open(filename.c_str());
31 if (!m_input) throw(LHECouldNotOpenFileError() << filename);
32}
33
34
35int LHEReader::getEvent(MCParticleGraph& graph, double& eventWeight)
36{
37 int nparticles = readEventHeader(eventWeight);
38 if (nparticles <= 0) {
39 throw (LHEEmptyEventError() << m_lineNr << nparticles);
40 }
41
42 int first = graph.size();
43 //Make list of particles
44 for (int i = 0; i < nparticles; i++) {
45 graph.addParticle();
46 }
47
48 double r, x = 0, y = 0, z = 0, t = 0;
49 //Read particles from file
50 for (int i = 0; i < nparticles; ++i) {
51 MCParticleGraph::GraphParticle& p = graph[first + i];
52 int mother = readParticle(p);
53
54 // add the mother
55 if (mother > 0) {
56 MCParticleGraph::GraphParticle* q = &graph[mother - 1];
57 p.comesFrom(*q);
58 }
59
60 // if positron goes to positive z-direction, we have to rotate along y-axis by 180deg
61 if (m_wrongSignPz) {
62 ROOT::Math::PxPyPzEVector p4 = p.get4Vector();
63 p4.SetPz(-1.0 * p4.Pz());
64 p4.SetPx(-1.0 * p4.Px());
65 p.set4Vector(p4);
66 }
67
68 //move vertex position of selected particle and its daughters
69 if (m_meanDecayLength > 0) {
70 if (p.getPDG() == m_pdgDisplaced) {
71 TF1 fr("fr", "exp(-x/[0])", 0, 1000000);
72 ROOT::Math::PxPyPzEVector p4 = p.get4Vector();
73 fr.SetRange(m_Rmin, m_Rmax);
74 fr.SetParameter(0, m_meanDecayLength * p4.Gamma());
75 r = fr.GetRandom();
76 x = r * p4.Px() / p4.P();
77 y = r * p4.Py() / p4.P();
78 z = r * p4.Pz() / p4.P();
79 p.setDecayVertex(x, y, z);
80 t = (r / Const::speedOfLight) * (p4.E() / p4.P());
81 p.setDecayTime(t);
82 p.setValidVertex(true);
83 }
84
85 if (mother > 0) {
86 if (graph[mother - 1].getPDG() == m_pdgDisplaced) {
87 p.setProductionVertex(x, y, z);
88 p.setProductionTime(t);
89 p.setValidVertex(true);
90 }
91 }
92 }
93
94
95 // initial 2 (e+/e-), virtual 3 (Z/gamma*)
96 // check if particle should be made virtual according to steering options:
97 if (i < m_indexVirtual && i >= m_indexInitial)
98 p.addStatus(MCParticle::c_IsVirtual);
99
100 if (i < m_indexInitial)
101 p.addStatus(MCParticle::c_Initial);
102
103 if (m_indexVirtual < m_indexInitial) B2WARNING("IsVirtual particle requested but is overwritten by Initial");
104
105 }
106// return eventID;
107 return -1;
108}
109
110
112{
113// int eventID;
114 double weight;
115 for (int i = 0; i < n; i++) {
116// int nparticles = readEventHeader(eventID, weight);
117 int nparticles = readEventHeader(weight);
118 if (nparticles < 0) return false;
119 for (int j = 0; j < nparticles; j++) getLine();
120 }
121 return true;
122}
123
124
125//===================================================================
126// Protected methods
127//===================================================================
128
130{
131 std::string line;
132 do {
133 getline(m_input, line);
134 m_lineNr++;
135 size_t commentPos = line.find_first_of('#');
136 if (commentPos != string::npos) {
137 line = line.substr(0, commentPos);
138 }
139 boost::trim(line);
140
141 } while (line == "" && !m_input.eof());
142
143 return line;
144}
145
146
147// int LHEReader::readEventHeader(int& eventID, double& eventWeight)
148int LHEReader::readEventHeader(double& eventWeight)
149{
150
151 // Search for next <event>
152 std::string line2;
153 do {
154 getline(m_input, line2);
155 m_lineNr++;
156 size_t commentPos = line2.find_first_of('#');
157 if (commentPos != string::npos) {
158 line2 = line2.substr(0, commentPos);
159 }
160 boost::trim(line2);
161
162 } while (line2 != "<event>" && !m_input.eof());
163
164 //Get number of particles from file
165 int nparticles = -1;
166 string line = getLine();
167
168 if (line == "" || m_input.eof()) return -1;
169
170 vector<double> fields;
171 fields.reserve(15);
172
173 tokenizer tokens(line, sep);
174 int index(0);
175
176 for (const string& tok : tokens) {
177 ++index;
178 try {
179 fields.push_back(boost::lexical_cast<double>(tok));
180 } catch (boost::bad_lexical_cast& e) {
181 throw (LHEConvertFieldError() << m_lineNr << index << tok);
182 }
183 }
184
185 switch (fields.size()) {
186 default:
187 eventWeight = 1.0;
188 nparticles = static_cast<int>(fields[0]); //other fields in LHE contain effective couplings
189 break;
190 }
191 return nparticles;
192}
193
194
196{
197 int mother = -1;
198
199 string line = getLine();
200 vector<double> fields;
201 fields.reserve(15);
202
203 tokenizer tokens(line, sep);
204 int index(0);
205
206 for (const string& tok : tokens) {
207 ++index;
208 try {
209 fields.push_back(boost::lexical_cast<double>(tok));
210 } catch (boost::bad_lexical_cast& e) {
211 throw (LHEConvertFieldError() << m_lineNr << index << tok);
212 }
213 }
214
215 switch (fields.size()) {
216 case 13:
217 particle.addStatus(MCParticle::c_PrimaryParticle);
218 particle.setPDG(static_cast<int>(fields[0]));
219 mother = static_cast<int>(fields[2]);
220 particle.setMomentum(ROOT::Math::XYZVector(fields[6], fields[7], fields[8]));
221 particle.setEnergy(fields[9]);
222 particle.setMass(fields[10]);
223 break;
224 default:
225 throw (LHEParticleFormatError() << m_lineNr << fields.size());
226 }
227
228 return mother;
229}
static const double speedOfLight
[cm/ns]
Definition: Const.h:695
int readParticle(MCParticleGraph::GraphParticle &particle)
Reads the information for a single particle from the LHE file.
Definition: LHEReader.cc:195
bool skipEvents(int n)
Skips a given number of events.
Definition: LHEReader.cc:111
std::string getLine()
Returns the current line from the LHE ascii file.
Definition: LHEReader.cc:129
double m_Rmin
Minimum of vertex distance to IP.
Definition: LHEReader.h:104
int m_pdgDisplaced
PDG code of the displaced particle being studied.
Definition: LHEReader.h:106
static const boost::char_separator< char > sep
The characters at which to split, defaults to ",; \t".
Definition: LHEReader.h:112
int m_lineNr
The current line number within the ascii file.
Definition: LHEReader.h:114
int m_indexVirtual
Maximum index of particles in each event that must be set as c_IsVirtual (1-based).
Definition: LHEReader.h:142
double m_Rmax
Maximum of vertex distance to IP.
Definition: LHEReader.h:105
void open(const std::string &filename)
Opens an ascii file and prepares it for reading.
Definition: LHEReader.cc:27
std::ifstream m_input
The input stream of the ascii file.
Definition: LHEReader.h:115
boost::tokenizer< boost::char_separator< char > > tokenizer
Just a typedef for simple use of the boost::tokenizer to split the lines.
Definition: LHEReader.h:110
int m_indexInitial
Maximum index of particles in each event that must be set as c_Initial (1-based).
Definition: LHEReader.h:140
int getEvent(MCParticleGraph &graph, double &weight)
Reads the next event and stores the result in the given MCParticle graph.
Definition: LHEReader.cc:35
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:148
double m_meanDecayLength
Mean lifetime*c of displaced particle.
Definition: LHEReader.h:103
Class to represent Particle data in graph.
Class to build, validate and sort a particle decay chain.
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_PrimaryParticle
bit 0: Particle is primary particle.
Definition: MCParticle.h:47
@ c_IsVirtual
bit 4: Particle is virtual and not going to Geant4.
Definition: MCParticle.h:55
GraphParticle & addParticle()
Add new particle to the graph.
Abstract base class for different kinds of events.
STL namespace.