11 #include <framework/gearbox/Unit.h>
12 #include <framework/gearbox/Const.h>
13 #include <framework/logging/Logger.h>
14 #include <generators/lhe/LHEReader.h>
15 #include <mdst/dataobjects/MCParticle.h>
18 #include <boost/lexical_cast.hpp>
19 #include <boost/algorithm/string.hpp>
20 #include <boost/foreach.hpp>
21 #include <boost/format.hpp>
24 #include <TLorentzVector.h>
29 const boost::char_separator<char> LHEReader::sep(
",; \t");
31 void LHEReader::open(
const string& filename)
34 m_input.open(filename.c_str());
35 if (!m_input)
throw(LHECouldNotOpenFileError() << filename);
43 int nparticles = readEventHeader(eventWeight);
44 if (nparticles <= 0) {
45 throw (LHEEmptyEventError() << m_lineNr << nparticles);
48 int first = graph.
size();
50 for (
int i = 0; i < nparticles; i++) {
54 double r, x = 0, y = 0, z = 0, t = 0;
56 for (
int i = 0; i < nparticles; ++i) {
58 int mother = readParticle(p);
67 if (m_meanDecayLength > 0) {
68 if (p.getPDG() == m_pdgDisplaced) {
69 TF1 fr(
"fr",
"exp(-x/[0])", 0, 1000000);
70 TLorentzVector p4 = p.get4Vector();
71 fr.SetRange(m_Rmin, m_Rmax);
72 fr.SetParameter(0, m_meanDecayLength * p4.Gamma());
74 x = r * p4.Px() / p4.P();
75 y = r * p4.Py() / p4.P();
76 z = r * p4.Pz() / p4.P();
77 p.setDecayVertex(TVector3(x, y, z));
78 t = (r / Const::speedOfLight) * (p4.E() / p4.P());
80 p.setValidVertex(
true);
84 if (graph[mother - 1].getPDG() == m_pdgDisplaced) {
85 p.setProductionVertex(TVector3(x, y, z));
86 p.setProductionTime(t);
87 p.setValidVertex(
true);
93 TLorentzVector p4 = p.get4Vector();
96 p4.SetPz(-1.0 * p4.Pz());
99 if (p.getPDG() == m_pdgDisplaced) {
100 v4.SetXYZT(p.getDecayVertex().X(), p.getDecayVertex().Y(), p.getDecayVertex().Z(), Const::speedOfLight * p.getDecayTime());
101 v4 = m_labboost * v4;
102 p.setDecayVertex(v4.X(), v4.Y(), v4.Z());
103 p.setDecayTime(v4.T() / Const::speedOfLight);
104 }
else if (mother > 0) {
105 if (graph[mother - 1].getPDG() == m_pdgDisplaced) {
106 v4.SetXYZT(p.getProductionVertex().X(), p.getProductionVertex().Y(), p.getProductionVertex().Z(),
107 Const::speedOfLight * p.getProductionTime());
108 v4 = m_labboost * v4;
109 p.setProductionVertex(v4.X(), v4.Y(), v4.Z());
110 p.setProductionTime(v4.T() / Const::speedOfLight);
117 if (i < m_indexVirtual && i >= m_indexInitial)
118 p.addStatus(MCParticle::c_IsVirtual);
120 if (i < m_indexInitial)
121 p.addStatus(MCParticle::c_Initial);
123 if (m_indexVirtual < m_indexInitial) B2WARNING(
"IsVirtual particle requested but is overwritten by Initial");
131 bool LHEReader::skipEvents(
int n)
135 for (
int i = 0; i < n; i++) {
137 int nparticles = readEventHeader(weight);
138 if (nparticles < 0)
return false;
139 for (
int j = 0; j < nparticles; j++) getLine();
149 std::string LHEReader::getLine()
153 getline(m_input, line);
155 size_t commentPos = line.find_first_of(
'#');
156 if (commentPos != string::npos) {
157 line = line.substr(0, commentPos);
161 }
while (line ==
"" && !m_input.eof());
168 int LHEReader::readEventHeader(
double& eventWeight)
174 getline(m_input, line2);
176 size_t commentPos = line2.find_first_of(
'#');
177 if (commentPos != string::npos) {
178 line2 = line2.substr(0, commentPos);
182 }
while (line2 !=
"<event>" && !m_input.eof());
186 string line = getLine();
188 if (line ==
"" || m_input.eof())
return -1;
190 vector<double> fields;
196 BOOST_FOREACH(
const string & tok, tokens) {
199 fields.push_back(boost::lexical_cast<double>(tok));
200 }
catch (boost::bad_lexical_cast& e) {
201 throw (LHEConvertFieldError() << m_lineNr << index << tok);
205 switch (fields.size()) {
208 nparticles =
static_cast<int>(fields[0]);
219 string line = getLine();
220 vector<double> fields;
226 BOOST_FOREACH(
const string & tok, tokens) {
229 fields.push_back(boost::lexical_cast<double>(tok));
230 }
catch (boost::bad_lexical_cast& e) {
231 throw (LHEConvertFieldError() << m_lineNr << index << tok);
235 switch (fields.size()) {
237 particle.addStatus(MCParticle::c_PrimaryParticle);
238 particle.setPDG(
static_cast<int>(fields[0]));
239 mother =
static_cast<int>(fields[2]);
240 particle.setMomentum(TVector3(&fields[6]));
241 particle.setMass(fields[10]);
244 throw (LHEParticleFormatError() << m_lineNr << fields.size());