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>
22 #include <TLorentzVector.h>
27 const boost::char_separator<char> LHEReader::sep(
",; \t");
29 void LHEReader::open(
const string& filename)
32 m_input.open(filename.c_str());
33 if (!m_input)
throw(LHECouldNotOpenFileError() << filename);
39 int nparticles = readEventHeader(eventWeight);
40 if (nparticles <= 0) {
41 throw (LHEEmptyEventError() << m_lineNr << nparticles);
44 int first = graph.
size();
46 for (
int i = 0; i < nparticles; i++) {
50 double r, x = 0, y = 0, z = 0, t = 0;
52 for (
int i = 0; i < nparticles; ++i) {
54 int mother = readParticle(p);
63 if (m_meanDecayLength > 0) {
64 if (p.getPDG() == m_pdgDisplaced) {
65 TF1 fr(
"fr",
"exp(-x/[0])", 0, 1000000);
66 TLorentzVector p4 = p.get4Vector();
67 fr.SetRange(m_Rmin, m_Rmax);
68 fr.SetParameter(0, m_meanDecayLength * p4.Gamma());
70 x = r * p4.Px() / p4.P();
71 y = r * p4.Py() / p4.P();
72 z = r * p4.Pz() / p4.P();
73 p.setDecayVertex(TVector3(x, y, z));
74 t = (r / Const::speedOfLight) * (p4.E() / p4.P());
76 p.setValidVertex(
true);
80 if (graph[mother - 1].getPDG() == m_pdgDisplaced) {
81 p.setProductionVertex(TVector3(x, y, z));
82 p.setProductionTime(t);
83 p.setValidVertex(
true);
89 TLorentzVector p4 = p.get4Vector();
90 p4.SetPz(-1.0 * p4.Pz());
96 if (i < m_indexVirtual && i >= m_indexInitial)
97 p.addStatus(MCParticle::c_IsVirtual);
99 if (i < m_indexInitial)
100 p.addStatus(MCParticle::c_Initial);
102 if (m_indexVirtual < m_indexInitial) B2WARNING(
"IsVirtual particle requested but is overwritten by Initial");
110 bool LHEReader::skipEvents(
int n)
114 for (
int i = 0; i < n; i++) {
116 int nparticles = readEventHeader(weight);
117 if (nparticles < 0)
return false;
118 for (
int j = 0; j < nparticles; j++) getLine();
128 std::string LHEReader::getLine()
132 getline(m_input, line);
134 size_t commentPos = line.find_first_of(
'#');
135 if (commentPos != string::npos) {
136 line = line.substr(0, commentPos);
140 }
while (line ==
"" && !m_input.eof());
147 int LHEReader::readEventHeader(
double& eventWeight)
153 getline(m_input, line2);
155 size_t commentPos = line2.find_first_of(
'#');
156 if (commentPos != string::npos) {
157 line2 = line2.substr(0, commentPos);
161 }
while (line2 !=
"<event>" && !m_input.eof());
165 string line = getLine();
167 if (line ==
"" || m_input.eof())
return -1;
169 vector<double> fields;
175 BOOST_FOREACH(
const string & tok, tokens) {
178 fields.push_back(boost::lexical_cast<double>(tok));
179 }
catch (boost::bad_lexical_cast& e) {
180 throw (LHEConvertFieldError() << m_lineNr << index << tok);
184 switch (fields.size()) {
187 nparticles =
static_cast<int>(fields[0]);
198 string line = getLine();
199 vector<double> fields;
205 BOOST_FOREACH(
const string & tok, tokens) {
208 fields.push_back(boost::lexical_cast<double>(tok));
209 }
catch (boost::bad_lexical_cast& e) {
210 throw (LHEConvertFieldError() << m_lineNr << index << tok);
214 switch (fields.size()) {
216 particle.addStatus(MCParticle::c_PrimaryParticle);
217 particle.setPDG(
static_cast<int>(fields[0]));
218 mother =
static_cast<int>(fields[2]);
219 particle.setMomentum(TVector3(&fields[6]));
220 particle.setMass(fields[10]);
223 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.