CMS 3D CMS Logo

LHEEvent.cc
Go to the documentation of this file.
1 #include <algorithm>
2 #include <iomanip>
3 #include <iostream>
4 #include <memory>
5 
6 #include <cmath>
7 #include <cstring>
8 #include <memory>
9 #include <sstream>
10 #include <string>
11 #include <vector>
12 
13 #include "HepMC/GenEvent.h"
14 #include "HepMC/GenVertex.h"
15 #include "HepMC/GenParticle.h"
16 #include "HepMC/SimpleVector.h"
17 
19 
21 
24 
25 static int skipWhitespace(std::istream &in) {
26  int ch;
27  do {
28  ch = in.get();
29  } while (std::isspace(ch));
30  if (ch != std::istream::traits_type::eof())
31  in.putback(ch);
32  return ch;
33 }
34 
35 namespace lhef {
36 
37  LHEEvent::LHEEvent(const std::shared_ptr<LHERunInfo> &runInfo, std::istream &in)
38  : runInfo(runInfo),
39  weights_(0),
40  counted(false),
41  readAttemptCounter(0),
42  npLO_(-99),
43  npNLO_(-99),
44  evtnum_(-1)
45 
46  {
47  hepeup.NUP = 0;
48  hepeup.XPDWUP.first = hepeup.XPDWUP.second = 0.0;
49 
51  if (!in.good())
52  throw cms::Exception("InvalidFormat") << "Les Houches file contained invalid"
53  " event header."
54  << std::endl;
55 
56  // store the original value of XWGTUP for the user
58 
59  int idwtup = runInfo->getHEPRUP()->IDWTUP;
60  if (idwtup >= 0 && hepeup.XWGTUP < 0) {
61  edm::LogWarning("Generator|LHEInterface") << "Non-allowed negative event weight encountered." << std::endl;
63  }
64 
65  if (std::abs(idwtup) == 3 && std::abs(hepeup.XWGTUP) != 1.) {
66  edm::LogInfo("Generator|LHEInterface") << "Event weight not set to one for abs(IDWTUP) == 3" << std::endl;
67  hepeup.XWGTUP = hepeup.XWGTUP > 0. ? 1.0 : -1.0;
68  }
69 
70  hepeup.resize();
71 
72  for (int i = 0; i < hepeup.NUP; i++) {
73  in >> hepeup.IDUP[i] >> hepeup.ISTUP[i] >> hepeup.MOTHUP[i].first >> hepeup.MOTHUP[i].second >>
74  hepeup.ICOLUP[i].first >> hepeup.ICOLUP[i].second >> hepeup.PUP[i][0] >> hepeup.PUP[i][1] >>
75  hepeup.PUP[i][2] >> hepeup.PUP[i][3] >> hepeup.PUP[i][4] >> hepeup.VTIMUP[i] >> hepeup.SPINUP[i];
76  if (!in.good())
77  throw cms::Exception("InvalidFormat") << "Les Houches file contained invalid event"
78  " in particle line "
79  << (i + 1) << "." << std::endl;
80  }
81 
82  while (skipWhitespace(in) == '#') {
84  std::getline(in, line);
85  std::istringstream ss(line);
87  ss >> tag;
88  if (tag == "#pdf") {
89  pdf = std::make_unique<PDF>();
90  ss >> pdf->id.first >> pdf->id.second >> pdf->x.first >> pdf->x.second >> pdf->scalePDF >> pdf->xPDF.first >>
91  pdf->xPDF.second;
92  if (ss.bad()) {
93  edm::LogWarning("Generator|LHEInterface") << "Les Houches event contained"
94  " unparseable PDF information."
95  << std::endl;
96  pdf.reset();
97  } else
98  continue;
99  }
100  comments.push_back(line + "\n");
101  }
102 
103  if (!in.eof())
104  edm::LogWarning("Generator|LHEInterface") << "Les Houches file contained spurious"
105  " content after event data."
106  << std::endl;
107  }
108 
109  LHEEvent::LHEEvent(const std::shared_ptr<LHERunInfo> &runInfo, const HEPEUP &hepeup)
110  : runInfo(runInfo), hepeup(hepeup), counted(false), readAttemptCounter(0), npLO_(-99), npNLO_(-99), evtnum_(-1) {}
111 
112  LHEEvent::LHEEvent(const std::shared_ptr<LHERunInfo> &runInfo,
113  const HEPEUP &hepeup,
114  const LHEEventProduct::PDF *pdf,
115  const std::vector<std::string> &comments)
116  : runInfo(runInfo),
117  hepeup(hepeup),
118  pdf(pdf ? new PDF(*pdf) : nullptr),
119  comments(comments),
120  counted(false),
121  readAttemptCounter(0),
122  npLO_(-99),
123  npNLO_(-99),
124  evtnum_(-1) {}
125 
126  LHEEvent::LHEEvent(const std::shared_ptr<LHERunInfo> &runInfo, const LHEEventProduct &product)
127  : runInfo(runInfo),
128  hepeup(product.hepeup()),
129  pdf(product.pdf() ? new PDF(*product.pdf()) : nullptr),
130  weights_(product.weights()),
131  comments(product.comments_begin(), product.comments_end()),
132  counted(false),
133  readAttemptCounter(0),
134  originalXWGTUP_(product.originalXWGTUP()),
135  scales_(product.scales()),
136  npLO_(product.npLO()),
137  npNLO_(product.npNLO()),
138  evtnum_(product.evtnum()) {}
139 
141 
142  void LHEEvent::removeParticle(HEPEUP &hepeup, int index) {
143  index--;
144  if (index < 0 || index >= hepeup.NUP) {
145  edm::LogError("Generator|LHEInterface")
146  << "removeParticle: Index " << (index + 1) << " out of bounds." << std::endl;
147  return;
148  }
149 
150  std::pair<int, int> mo = hepeup.MOTHUP[index];
151 
152  hepeup.IDUP.erase(hepeup.IDUP.begin() + index);
153  hepeup.ISTUP.erase(hepeup.ISTUP.begin() + index);
154  hepeup.MOTHUP.erase(hepeup.MOTHUP.begin() + index);
155  hepeup.ICOLUP.erase(hepeup.ICOLUP.begin() + index);
156  hepeup.PUP.erase(hepeup.PUP.begin() + index);
157  hepeup.VTIMUP.erase(hepeup.VTIMUP.begin() + index);
158  hepeup.SPINUP.erase(hepeup.SPINUP.begin() + index);
159 
160  hepeup.NUP--;
161 
162  index++;
163  for (int i = 0; i < hepeup.NUP; i++) {
164  if (hepeup.MOTHUP[i].first == index) {
165  if (hepeup.MOTHUP[i].second > 0)
166  edm::LogError("Generator|LHEInterface")
167  << "removeParticle: Particle " << (i + 2) << " has two mothers." << std::endl;
168  hepeup.MOTHUP[i] = mo;
169  }
170 
171  if (hepeup.MOTHUP[i].first > index)
172  hepeup.MOTHUP[i].first--;
173  if (hepeup.MOTHUP[i].second > index)
174  hepeup.MOTHUP[i].second--;
175  }
176  }
177 
178  void LHEEvent::removeResonances(const std::vector<int> &ids) {
179  for (int i = 1; i <= hepeup.NUP; i++) {
180  int id = std::abs(hepeup.IDUP[i - 1]);
181  if (hepeup.MOTHUP[i - 1].first > 0 && hepeup.MOTHUP[i - 1].second > 0 &&
182  std::find(ids.begin(), ids.end(), id) != ids.end())
183  removeParticle(hepeup, i--);
184  }
185  }
186 
187  void LHEEvent::count(LHERunInfo::CountMode mode, double weight, double matchWeight) {
188  if (counted)
189  edm::LogWarning("Generator|LHEInterface") << "LHEEvent::count called twice on same event!" << std::endl;
190 
191  runInfo->count(hepeup.IDPRUP, mode, hepeup.XWGTUP, weight, matchWeight);
192 
193  counted = true;
194  }
195 
196  void LHEEvent::fillPdfInfo(HepMC::PdfInfo *info) const {
197  if (pdf.get()) {
198  info->set_id1(pdf->id.first);
199  info->set_id2(pdf->id.second);
200  info->set_x1(pdf->x.first);
201  info->set_x2(pdf->x.second);
202  info->set_pdf1(pdf->xPDF.first);
203  info->set_pdf2(pdf->xPDF.second);
204  info->set_scalePDF(pdf->scalePDF);
205  } else if (hepeup.NUP >= 2) {
206  const HEPRUP *heprup = getHEPRUP();
207  info->set_id1(hepeup.IDUP[0] == 21 ? 0 : hepeup.IDUP[0]);
208  info->set_id2(hepeup.IDUP[1] == 21 ? 0 : hepeup.IDUP[1]);
209  info->set_x1(std::abs(hepeup.PUP[0][2] / heprup->EBMUP.first));
210  info->set_x2(std::abs(hepeup.PUP[1][2] / heprup->EBMUP.second));
211  info->set_pdf1(-1.0);
212  info->set_pdf2(-1.0);
213  info->set_scalePDF(hepeup.SCALUP);
214  } else {
215  info->set_x1(-1.0);
216  info->set_x2(-1.0);
217  info->set_pdf1(-1.0);
218  info->set_pdf2(-1.0);
219  info->set_scalePDF(hepeup.SCALUP);
220  }
221  }
222 
224  event->set_signal_process_id(hepeup.IDPRUP);
225  event->set_event_scale(hepeup.SCALUP);
226  event->set_alphaQED(hepeup.AQEDUP);
227  event->set_alphaQCD(hepeup.AQCDUP);
228  }
229 
230  std::unique_ptr<HepMC::GenEvent> LHEEvent::asHepMCEvent() const {
231  std::unique_ptr<HepMC::GenEvent> hepmc(new HepMC::GenEvent);
232 
233  hepmc->set_signal_process_id(hepeup.IDPRUP);
234  hepmc->set_event_scale(hepeup.SCALUP);
235  hepmc->set_alphaQED(hepeup.AQEDUP);
236  hepmc->set_alphaQCD(hepeup.AQCDUP);
237 
238  unsigned int nup = hepeup.NUP; // particles in event
239 
240  // any particles in HEPEUP block?
241  if (!nup) {
242  edm::LogWarning("Generator|LHEInterface") << "Les Houches Event does not contain any partons. "
243  << "Not much to convert.";
244  return hepmc;
245  }
246 
247  // stores (pointers to) converted particles
248  std::vector<HepMC::GenParticle *> genParticles;
249  std::vector<HepMC::GenVertex *> genVertices;
250 
251  // I. convert particles
252  for (unsigned int i = 0; i < nup; i++)
253  genParticles.push_back(makeHepMCParticle(i));
254 
255  // II. loop again to build vertices
256  for (unsigned int i = 0; i < nup; i++) {
257  unsigned int mother1 = hepeup.MOTHUP.at(i).first;
258  unsigned int mother2 = hepeup.MOTHUP.at(i).second;
259  double cTau = hepeup.VTIMUP.at(i); // decay time
260 
261  // current particle has a mother? --- Sorry, parent! We're PC.
262  if (mother1) {
263  mother1--; // FORTRAN notation!
264  if (mother2)
265  mother2--;
266  else
267  mother2 = mother1;
268 
269  HepMC::GenParticle *in_par = genParticles.at(mother1);
270  HepMC::GenVertex *current_vtx = in_par->end_vertex(); // vertex of first mother
271 
272  if (!current_vtx) {
273  current_vtx = new HepMC::GenVertex(HepMC::FourVector(0, 0, 0, cTau));
274 
275  // add vertex to event
276  genVertices.push_back(current_vtx);
277  }
278 
279  for (unsigned int j = mother1; j <= mother2; j++) // set mother-daughter relations
280  if (!genParticles.at(j)->end_vertex())
281  current_vtx->add_particle_in(genParticles.at(j));
282 
283  // connect THIS outgoing particle to current vertex
284  current_vtx->add_particle_out(genParticles.at(i));
285  }
286  }
287 
288  checkHepMCTree(hepmc.get());
289 
290  // III. restore color flow
291  // ok, nobody knows how to do it so far...
292 
293  // IV. fill run information
294  const HEPRUP *heprup = getHEPRUP();
295 
296  // set beam particles
298  HepMC::FourVector(0.0, 0.0, +heprup->EBMUP.first, heprup->EBMUP.first), heprup->IDBMUP.first);
300  HepMC::FourVector(0.0, 0.0, -heprup->EBMUP.second, heprup->EBMUP.second), heprup->IDBMUP.second);
301  b1->set_status(3);
302  b2->set_status(3);
303 
304  HepMC::GenVertex *v1 = new HepMC::GenVertex();
305  HepMC::GenVertex *v2 = new HepMC::GenVertex();
306  v1->add_particle_in(b1);
307  v2->add_particle_in(b2);
308 
309  hepmc->add_vertex(v1);
310  hepmc->add_vertex(v2);
311  hepmc->set_beam_particles(b1, b2);
312 
313  // first two particles have to be the hard partons going into the interaction
314  if (genParticles.size() >= 2) {
315  if (!genParticles.at(0)->production_vertex() && !genParticles.at(1)->production_vertex()) {
316  v1->add_particle_out(genParticles.at(0));
317  v2->add_particle_out(genParticles.at(1));
318  } else
319  edm::LogWarning("Generator|LHEInterface") << "Initial partons do already have a"
320  " production vertex. "
321  << std::endl
322  << "Beam particles not connected.";
323  } else
324  edm::LogWarning("Generator|LHEInterface") << "Can't find any initial partons to be"
325  " connected to the beam particles.";
326 
327  for (std::vector<HepMC::GenVertex *>::const_iterator iter = genVertices.begin(); iter != genVertices.end(); ++iter)
328  hepmc->add_vertex(*iter);
329 
330  // do some more consistency checks
331  for (unsigned int i = 0; i < nup; i++) {
332  if (!genParticles.at(i)->parent_event()) {
333  edm::LogWarning("Generator|LHEInterface") << "Not all LHE particles could be stored"
334  " stored in the HepMC event. "
335  << std::endl
336  << "Check the mother-daughter relations"
337  " in the given LHE input file.";
338  break;
339  }
340  }
341 
342  hepmc->set_signal_process_vertex(const_cast<HepMC::GenVertex *>(findSignalVertex(hepmc.get(), false)));
343 
344  return hepmc;
345  }
346 
348  HepMC::GenParticle *particle = new HepMC::GenParticle(
349  HepMC::FourVector(hepeup.PUP.at(i)[0], hepeup.PUP.at(i)[1], hepeup.PUP.at(i)[2], hepeup.PUP.at(i)[3]),
350  hepeup.IDUP.at(i));
351 
352  int status = hepeup.ISTUP.at(i);
353 
354  particle->set_generated_mass(hepeup.PUP.at(i)[4]);
355  particle->set_status(status > 0 ? (status == 2 ? 3 : status) : 3);
356 
357  return particle;
358  }
359 
361  double px = 0, py = 0, pz = 0, E = 0;
362 
363  for (HepMC::GenEvent::particle_const_iterator iter = event->particles_begin(); iter != event->particles_end();
364  iter++) {
365  int status = (*iter)->status();
366  HepMC::FourVector fv = (*iter)->momentum();
367 
368  // incoming particles
369  if (status == 3 && *iter != event->beam_particles().first && *iter != event->beam_particles().second) {
370  px -= fv.px();
371  py -= fv.py();
372  pz -= fv.pz();
373  E -= fv.e();
374  }
375 
376  // outgoing particles
377  if (status == 1) {
378  px += fv.px();
379  py += fv.py();
380  pz += fv.pz();
381  E += fv.e();
382  }
383  }
384 
385  if (px * px + py * py + pz * pz + E * E > 0.1) {
386  edm::LogWarning("Generator|LHEInterface")
387  << "Energy-momentum badly conserved. " << std::setprecision(3) << "sum p_i = [" << std::setw(7) << E << ", "
388  << std::setw(7) << px << ", " << std::setw(7) << py << ", " << std::setw(7) << pz << "]";
389 
390  return false;
391  }
392 
393  return true;
394  }
395 
396  const HepMC::GenVertex *LHEEvent::findSignalVertex(const HepMC::GenEvent *event, bool status3) {
397  double largestMass2 = -9.0e-30;
398  const HepMC::GenVertex *vertex = nullptr;
399  for (HepMC::GenEvent::vertex_const_iterator iter = event->vertices_begin(); iter != event->vertices_end(); ++iter) {
400  if ((*iter)->particles_in_size() < 2)
401  continue;
402  if ((*iter)->particles_out_size() < 1 ||
403  ((*iter)->particles_out_size() == 1 &&
404  (!(*(*iter)->particles_out_const_begin())->end_vertex() ||
405  !(*(*iter)->particles_out_const_begin())->end_vertex()->particles_out_size())))
406  continue;
407 
408  double px = 0.0, py = 0.0, pz = 0.0, E = 0.0;
409  bool hadStatus3 = false;
410  for (HepMC::GenVertex::particles_out_const_iterator iter2 = (*iter)->particles_out_const_begin();
411  iter2 != (*iter)->particles_out_const_end();
412  ++iter2) {
413  hadStatus3 = hadStatus3 || (*iter2)->status() == 3;
414  px += (*iter2)->momentum().px();
415  py += (*iter2)->momentum().py();
416  pz += (*iter2)->momentum().pz();
417  E += (*iter2)->momentum().e();
418  }
419  if (status3 && !hadStatus3)
420  continue;
421 
422  double mass2 = E * E - (px * px + py * py + pz * pz);
423  if (mass2 > largestMass2) {
424  vertex = *iter;
425  largestMass2 = mass2;
426  }
427  }
428 
429  return vertex;
430  }
431 
432  static void fixSubTree(HepMC::GenVertex *vertex,
433  HepMC::FourVector &_time,
434  std::set<const HepMC::GenVertex *> &visited) {
435  HepMC::FourVector time = _time;
436  HepMC::FourVector curTime = vertex->position();
437  bool needsFixup = curTime.t() < time.t();
438 
439  if (!visited.insert(vertex).second && !needsFixup)
440  return;
441 
442  if (needsFixup)
443  vertex->set_position(time);
444  else
445  time = curTime;
446 
447  for (HepMC::GenVertex::particles_out_const_iterator iter = vertex->particles_out_const_begin();
448  iter != vertex->particles_out_const_end();
449  ++iter) {
450  HepMC::GenVertex *endVertex = (*iter)->end_vertex();
451  if (endVertex)
452  fixSubTree(endVertex, time, visited);
453  }
454  }
455 
457  std::set<const HepMC::GenVertex *> visited;
458  HepMC::FourVector zeroTime;
459  for (HepMC::GenEvent::vertex_iterator iter = event->vertices_begin(); iter != event->vertices_end(); ++iter)
460  fixSubTree(*iter, zeroTime, visited);
461  }
462 
463 } // namespace lhef
HEPEUP hepeup
Definition: LHEEvent.h:88
weight_default_t b1[25]
Definition: b1.h:9
static const TGPicture * info(bool iBackgroundIsBlack)
double cTau(int pdgID, const HepPDT::ParticleDataTable *pdt)
LHEEvent(const std::shared_ptr< LHERunInfo > &runInfo, std::istream &in)
Definition: LHEEvent.cc:37
std::vector< std::string > comments
Definition: LHEEvent.h:91
std::vector< std::pair< int, int > > ICOLUP
Definition: LesHouches.h:240
std::pair< double, double > EBMUP
Definition: LesHouches.h:82
const std::shared_ptr< LHERunInfo > runInfo
Definition: LHEEvent.h:86
static void fixHepMCEventTimeOrdering(HepMC::GenEvent *event)
Definition: LHEEvent.cc:456
void removeResonances(const std::vector< int > &ids)
Definition: LHEEvent.cc:178
Definition: weight.py:1
void count(LHERunInfo::CountMode count, double weight=1.0, double matchWeight=1.0)
Definition: LHEEvent.cc:187
std::vector< double > VTIMUP
Definition: LesHouches.h:252
Log< level::Error, false > LogError
std::pair< double, double > XPDWUP
Definition: LesHouches.h:202
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
std::unique_ptr< HepMC::GenEvent > asHepMCEvent() const
Definition: LHEEvent.cc:230
void fillEventInfo(HepMC::GenEvent *hepmc) const
Definition: LHEEvent.cc:223
void resize(int nup)
Definition: LesHouches.h:161
static void removeParticle(lhef::HEPEUP &hepeup, int index)
Definition: LHEEvent.cc:142
std::vector< FiveVector > PUP
Definition: LesHouches.h:246
std::vector< double > SPINUP
Definition: LesHouches.h:259
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
weight_default_t b2[10]
Definition: b2.h:9
std::vector< int > ISTUP
Definition: LesHouches.h:228
const HEPRUP * getHEPRUP() const
Definition: LHEEvent.h:39
std::vector< std::pair< int, int > > MOTHUP
Definition: LesHouches.h:234
std::vector< int > IDUP
Definition: LesHouches.h:223
Log< level::Info, false > LogInfo
double AQCDUP
Definition: LesHouches.h:218
void fillPdfInfo(HepMC::PdfInfo *info) const
Definition: LHEEvent.cc:196
static void fixSubTree(HepMC::GenVertex *vertex, HepMC::FourVector &_time, std::set< const HepMC::GenVertex *> &visited)
Definition: LHEEvent.cc:432
HepMC::GenParticle * makeHepMCParticle(unsigned int i) const
Definition: LHEEvent.cc:347
static const HepMC::GenVertex * findSignalVertex(const HepMC::GenEvent *event, bool status3=true)
Definition: LHEEvent.cc:396
std::unique_ptr< PDF > pdf
Definition: LHEEvent.h:89
bool counted
Definition: LHEEvent.h:92
double AQEDUP
Definition: LesHouches.h:213
static bool checkHepMCTree(const HepMC::GenEvent *event)
Definition: LHEEvent.cc:360
VertexRefVector::iterator vertex_iterator
iterator over a vector of references to Vertex objects in the same collection
Definition: VertexFwd.h:19
Log< level::Warning, false > LogWarning
static int skipWhitespace(std::istream &in)
Definition: LHEEvent.cc:25
double XWGTUP
Definition: LesHouches.h:194
Definition: event.py:1
double originalXWGTUP_
Definition: LHEEvent.h:94
double SCALUP
Definition: LesHouches.h:208