13 #include "HepMC/GenEvent.h"
14 #include "HepMC/GenVertex.h"
15 #include "HepMC/GenParticle.h"
16 #include "HepMC/SimpleVector.h"
29 }
while (std::isspace(ch));
30 if (ch != std::istream::traits_type::eof())
41 readAttemptCounter(0),
51 throw cms::Exception(
"InvalidFormat") <<
"Les Houches file contained invalid"
58 int idwtup = runInfo->getHEPRUP()->IDWTUP;
60 edm::LogWarning(
"Generator|LHEInterface") <<
"Non-allowed negative event weight encountered." << std::endl;
65 edm::LogInfo(
"Generator|LHEInterface") <<
"Event weight not set to one for abs(IDWTUP) == 3" << std::endl;
76 throw cms::Exception(
"InvalidFormat") <<
"Les Houches file contained invalid event"
78 << (
i + 1) <<
"." << std::endl;
83 std::getline(in, line);
84 std::istringstream
ss(line);
88 pdf = std::make_unique<PDF>();
89 ss >>
pdf->id.first >>
pdf->id.second >>
pdf->x.first >>
pdf->x.second >>
pdf->scalePDF >>
pdf->xPDF.first >>
92 edm::LogWarning(
"Generator|LHEInterface") <<
"Les Houches event contained"
93 " unparseable PDF information."
103 edm::LogWarning(
"Generator|LHEInterface") <<
"Les Houches file contained spurious"
104 " content after event data."
109 : runInfo(runInfo), hepeup(hepeup), counted(
false), readAttemptCounter(0), npLO_(-99), npNLO_(-99) {}
114 const std::vector<std::string> &comments)
117 pdf(pdf ? new
PDF(*pdf) : nullptr),
120 readAttemptCounter(0),
126 hepeup(product.hepeup()),
127 pdf(product.pdf() ? new
PDF(*product.pdf()) : nullptr),
129 comments(product.comments_begin(), product.comments_end()),
131 readAttemptCounter(0),
132 originalXWGTUP_(product.originalXWGTUP()),
133 scales_(product.scales()),
134 npLO_(product.npLO()),
135 npNLO_(product.npNLO()) {}
141 if (index < 0 || index >= hepeup.
NUP) {
143 <<
"removeParticle: Index " << (index + 1) <<
" out of bounds." << std::endl;
160 for (
int i = 0;
i < hepeup.
NUP;
i++) {
161 if (hepeup.
MOTHUP[
i].first == index) {
162 if (hepeup.
MOTHUP[
i].second > 0)
164 <<
"removeParticle: Particle " << (
i + 2) <<
" has two mothers." << std::endl;
168 if (hepeup.
MOTHUP[
i].first > index)
170 if (hepeup.
MOTHUP[
i].second > index)
179 std::find(ids.begin(), ids.end(),
id) != ids.end())
186 edm::LogWarning(
"Generator|LHEInterface") <<
"LHEEvent::count called twice on same event!" << std::endl;
195 info->set_id1(
pdf->id.first);
196 info->set_id2(
pdf->id.second);
197 info->set_x1(
pdf->x.first);
198 info->set_x2(
pdf->x.second);
199 info->set_pdf1(
pdf->xPDF.first);
200 info->set_pdf2(
pdf->xPDF.second);
201 info->set_scalePDF(
pdf->scalePDF);
208 info->set_pdf1(-1.0);
209 info->set_pdf2(-1.0);
214 info->set_pdf1(-1.0);
215 info->set_pdf2(-1.0);
239 edm::LogWarning(
"Generator|LHEInterface") <<
"Les Houches Event does not contain any partons. "
240 <<
"Not much to convert.";
246 std::vector<HepMC::GenVertex *> genVertices;
249 for (
unsigned int i = 0;
i < nup;
i++)
253 for (
unsigned int i = 0;
i < nup;
i++) {
267 HepMC::GenVertex *current_vtx = in_par->end_vertex();
270 current_vtx =
new HepMC::GenVertex(HepMC::FourVector(0, 0, 0, cTau));
273 genVertices.push_back(current_vtx);
276 for (
unsigned int j = mother1;
j <= mother2;
j++)
277 if (!genParticles.at(
j)->end_vertex())
278 current_vtx->add_particle_in(genParticles.at(
j));
281 current_vtx->add_particle_out(genParticles.at(
i));
295 HepMC::FourVector(0.0, 0.0, +heprup->EBMUP.first, heprup->EBMUP.first), heprup->IDBMUP.first);
297 HepMC::FourVector(0.0, 0.0, -heprup->EBMUP.second, heprup->EBMUP.second), heprup->IDBMUP.second);
301 HepMC::GenVertex *v1 =
new HepMC::GenVertex();
302 HepMC::GenVertex *v2 =
new HepMC::GenVertex();
303 v1->add_particle_in(b1);
304 v2->add_particle_in(b2);
306 hepmc->add_vertex(v1);
307 hepmc->add_vertex(v2);
308 hepmc->set_beam_particles(b1, b2);
311 if (genParticles.size() >= 2) {
312 if (!genParticles.at(0)->production_vertex() && !genParticles.at(1)->production_vertex()) {
313 v1->add_particle_out(genParticles.at(0));
314 v2->add_particle_out(genParticles.at(1));
316 edm::LogWarning(
"Generator|LHEInterface") <<
"Initial partons do already have a"
317 " production vertex. "
319 <<
"Beam particles not connected.";
321 edm::LogWarning(
"Generator|LHEInterface") <<
"Can't find any initial partons to be"
322 " connected to the beam particles.";
324 for (std::vector<HepMC::GenVertex *>::const_iterator iter = genVertices.begin(); iter != genVertices.end(); ++iter)
325 hepmc->add_vertex(*iter);
328 for (
unsigned int i = 0;
i < nup;
i++) {
329 if (!genParticles.at(
i)->parent_event()) {
330 edm::LogWarning(
"Generator|LHEInterface") <<
"Not all LHE particles could be stored"
331 " stored in the HepMC event. "
333 <<
"Check the mother-daughter relations"
334 " in the given LHE input file.";
339 hepmc->set_signal_process_vertex(const_cast<HepMC::GenVertex *>(
findSignalVertex(hepmc.get(),
false)));
351 particle->set_generated_mass(
hepeup.
PUP.at(i)[4]);
352 particle->set_status(status > 0 ? (status == 2 ? 3 : status) : 3);
358 double px = 0, py = 0, pz = 0, E = 0;
360 for (HepMC::GenEvent::particle_const_iterator iter = event->particles_begin(); iter !=
event->particles_end();
362 int status = (*iter)->status();
363 HepMC::FourVector fv = (*iter)->momentum();
366 if (status == 3 && *iter != event->beam_particles().first && *iter !=
event->beam_particles().second) {
382 if (px * px + py * py + pz * pz + E * E > 0.1) {
384 <<
"Energy-momentum badly conserved. " << std::setprecision(3) <<
"sum p_i = [" << std::setw(7) << E <<
", "
385 << std::setw(7) << px <<
", " << std::setw(7) << py <<
", " << std::setw(7) << pz <<
"]";
394 double largestMass2 = -9.0e-30;
395 const HepMC::GenVertex *vertex =
nullptr;
396 for (HepMC::GenEvent::vertex_const_iterator iter = event->vertices_begin(); iter !=
event->vertices_end(); ++iter) {
397 if ((*iter)->particles_in_size() < 2)
399 if ((*iter)->particles_out_size() < 1 ||
400 ((*iter)->particles_out_size() == 1 &&
401 (!(*(*iter)->particles_out_const_begin())->end_vertex() ||
402 !(*(*iter)->particles_out_const_begin())->end_vertex()->particles_out_size())))
405 double px = 0.0, py = 0.0, pz = 0.0, E = 0.0;
406 bool hadStatus3 =
false;
407 for (HepMC::GenVertex::particles_out_const_iterator iter2 = (*iter)->particles_out_const_begin();
408 iter2 != (*iter)->particles_out_const_end();
410 hadStatus3 = hadStatus3 || (*iter2)->status() == 3;
411 px += (*iter2)->momentum().px();
412 py += (*iter2)->momentum().py();
413 pz += (*iter2)->momentum().pz();
414 E += (*iter2)->momentum().e();
416 if (status3 && !hadStatus3)
419 double mass2 = E * E - (px * px + py * py + pz * pz);
420 if (mass2 > largestMass2) {
422 largestMass2 = mass2;
430 HepMC::FourVector &_time,
431 std::set<const HepMC::GenVertex *> &
visited) {
432 HepMC::FourVector time = _time;
433 HepMC::FourVector curTime = vertex->position();
434 bool needsFixup = curTime.t() < time.t();
436 if (!visited.insert(vertex).second && !needsFixup)
440 vertex->set_position(time);
444 for (HepMC::GenVertex::particles_out_const_iterator iter = vertex->particles_out_const_begin();
445 iter != vertex->particles_out_const_end();
447 HepMC::GenVertex *endVertex = (*iter)->end_vertex();
454 std::set<const HepMC::GenVertex *>
visited;
455 HepMC::FourVector zeroTime;
static void fixSubTree(HepMC::GenVertex *vertex, HepMC::FourVector &_time, std::set< const HepMC::GenVertex * > &visited)
double cTau(int pdgID, const HepPDT::ParticleDataTable *pdt)
LHEEvent(const std::shared_ptr< LHERunInfo > &runInfo, std::istream &in)
uint16_t *__restrict__ id
std::vector< std::string > comments
std::vector< std::pair< int, int > > ICOLUP
const HEPRUP * getHEPRUP() const
void fillEventInfo(HepMC::GenEvent *hepmc) const
std::pair< double, double > EBMUP
const std::shared_ptr< LHERunInfo > runInfo
static void fixHepMCEventTimeOrdering(HepMC::GenEvent *event)
void removeResonances(const std::vector< int > &ids)
void count(LHERunInfo::CountMode count, double weight=1.0, double matchWeight=1.0)
std::vector< double > VTIMUP
Log< level::Error, false > LogError
std::pair< double, double > XPDWUP
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
HepMC::GenParticle * makeHepMCParticle(unsigned int i) const
static void removeParticle(lhef::HEPEUP &hepeup, int index)
std::vector< FiveVector > PUP
std::vector< double > SPINUP
void fillPdfInfo(HepMC::PdfInfo *info) const
Abs< T >::type abs(const T &t)
std::unique_ptr< HepMC::GenEvent > asHepMCEvent() const
std::vector< std::pair< int, int > > MOTHUP
Log< level::Info, false > LogInfo
static const HepMC::GenVertex * findSignalVertex(const HepMC::GenEvent *event, bool status3=true)
std::unique_ptr< PDF > pdf
static bool checkHepMCTree(const HepMC::GenEvent *event)
VertexRefVector::iterator vertex_iterator
iterator over a vector of references to Vertex objects in the same collection
Log< level::Warning, false > LogWarning
static constexpr float b2
static int skipWhitespace(std::istream &in)
static constexpr float b1