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>
16 #include <boost/lexical_cast.hpp>
17 #include <boost/algorithm/string.hpp>
18 #include <boost/foreach.hpp>
19 #include <boost/format.hpp>
26 const boost::char_separator<char> LHEReader::sep(
",; \t");
28 void LHEReader::open(
const string& filename)
31 m_input.open(filename.c_str());
32 if (!m_input)
throw(LHECouldNotOpenFileError() << filename);
38 int nparticles = readEventHeader(eventWeight);
39 if (nparticles <= 0) {
40 throw (LHEEmptyEventError() << m_lineNr << nparticles);
43 int first = graph.
size();
45 for (
int i = 0; i < nparticles; i++) {
49 double r, x = 0, y = 0, z = 0, t = 0;
51 for (
int i = 0; i < nparticles; ++i) {
53 int mother = readParticle(p);
63 ROOT::Math::PxPyPzEVector p4 = p.get4Vector();
64 p4.SetPz(-1.0 * p4.Pz());
65 p4.SetPx(-1.0 * p4.Px());
70 if (m_meanDecayLength > 0) {
71 if (p.getPDG() == m_pdgDisplaced) {
72 TF1 fr(
"fr",
"exp(-x/[0])", 0, 1000000);
73 ROOT::Math::PxPyPzEVector p4 = p.get4Vector();
74 fr.SetRange(m_Rmin, m_Rmax);
75 fr.SetParameter(0, m_meanDecayLength * p4.Gamma());
77 x = r * p4.Px() / p4.P();
78 y = r * p4.Py() / p4.P();
79 z = r * p4.Pz() / p4.P();
80 p.setDecayVertex(x, y, z);
81 t = (r / Const::speedOfLight) * (p4.E() / p4.P());
83 p.setValidVertex(
true);
87 if (graph[mother - 1].getPDG() == m_pdgDisplaced) {
88 p.setProductionVertex(x, y, z);
89 p.setProductionTime(t);
90 p.setValidVertex(
true);
98 if (i < m_indexVirtual && i >= m_indexInitial)
99 p.addStatus(MCParticle::c_IsVirtual);
101 if (i < m_indexInitial)
102 p.addStatus(MCParticle::c_Initial);
104 if (m_indexVirtual < m_indexInitial) B2WARNING(
"IsVirtual particle requested but is overwritten by Initial");
112 bool LHEReader::skipEvents(
int n)
116 for (
int i = 0; i < n; i++) {
118 int nparticles = readEventHeader(weight);
119 if (nparticles < 0)
return false;
120 for (
int j = 0; j < nparticles; j++) getLine();
130 std::string LHEReader::getLine()
134 getline(m_input, line);
136 size_t commentPos = line.find_first_of(
'#');
137 if (commentPos != string::npos) {
138 line = line.substr(0, commentPos);
142 }
while (line ==
"" && !m_input.eof());
149 int LHEReader::readEventHeader(
double& eventWeight)
155 getline(m_input, line2);
157 size_t commentPos = line2.find_first_of(
'#');
158 if (commentPos != string::npos) {
159 line2 = line2.substr(0, commentPos);
163 }
while (line2 !=
"<event>" && !m_input.eof());
167 string line = getLine();
169 if (line ==
"" || m_input.eof())
return -1;
171 vector<double> fields;
177 BOOST_FOREACH(
const string & tok, tokens) {
180 fields.push_back(boost::lexical_cast<double>(tok));
181 }
catch (boost::bad_lexical_cast& e) {
182 throw (LHEConvertFieldError() << m_lineNr << index << tok);
186 switch (fields.size()) {
189 nparticles =
static_cast<int>(fields[0]);
200 string line = getLine();
201 vector<double> fields;
207 BOOST_FOREACH(
const string & tok, tokens) {
210 fields.push_back(boost::lexical_cast<double>(tok));
211 }
catch (boost::bad_lexical_cast& e) {
212 throw (LHEConvertFieldError() << m_lineNr << index << tok);
216 switch (fields.size()) {
218 particle.addStatus(MCParticle::c_PrimaryParticle);
219 particle.setPDG(
static_cast<int>(fields[0]));
220 mother =
static_cast<int>(fields[2]);
221 particle.setMomentum(ROOT::Math::XYZVector(fields[6], fields[7], fields[8]));
222 particle.setEnergy(fields[9]);
223 particle.setMass(fields[10]);
226 throw (LHEParticleFormatError() << m_lineNr << fields.size());
boost::tokenizer< boost::char_separator< char > > tokenizer
Just a typedef for simple use of the boost::tokenizer to split the lines.
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.
GraphParticle & addParticle()
Add new particle to the graph.
Abstract base class for different kinds of events.