10 #include "HepMC/GenEvent.h"
11 #include "HepMC/GenParticle.h"
13 #include "Pythia8/Pythia.h"
14 #include "Pythia8Plugins/HepMC2.h"
25 #include "Pythia8Plugins/JetMatching.h"
26 #include "Pythia8Plugins/aMCatNLOHooks.h"
30 #include "Pythia8Plugins/PowhegHooks.h"
48 #include "Pythia8Plugins/EvtGen.h"
65 #include "HepPID/ParticleIDTranslations.hh"
71 class HepRandomEngine;
81 bool initializeForInternalPartons()
override;
82 bool initializeForExternalPartons();
84 bool generatePartonsAndHadronize()
override;
87 virtual bool residualDecay();
89 void finalizeEvent()
override;
93 const char *
classname()
const override {
return "Pythia8Hadronizer"; }
95 std::unique_ptr<GenLumiInfoHeader> getGenLumiInfoHeader()
const override;
99 std::vector<std::string>
const &
doSharedResources()
const override {
return p8SharedResources; }
105 std::shared_ptr<LHAupLesHouches>
lhaUP;
107 enum { PP,
PPbar, ElectronPositron };
174 LHEInputFileName(
params.getUntrackedParameter<
std::
string>(
"LHEInputFileName",
"")),
183 if (
params.exists(
"PPbarInitialState")) {
187 <<
"Pythia8 will be initialized for PROTON-ANTIPROTON INITIAL STATE. "
188 <<
"This is a user-request change from the DEFAULT PROTON-PROTON initial state.";
192 }
else if (
params.exists(
"ElectronPositronInitialState")) {
196 <<
"Pythia8 will be initialized for ELECTRON-POSITRON INITIAL STATE. "
197 <<
"This is a user-request change from the DEFAULT PROTON-PROTON initial state.";
201 }
else if (
params.exists(
"ElectronProtonInitialState") ||
params.exists(
"PositronProtonInitialState")) {
204 <<
" UNKNOWN INITIAL STATE. \n The allowed initial states are: PP, PPbar, ElectronPositron \n";
209 if (
params.exists(
"reweightGen")) {
210 edm::LogInfo(
"Pythia8Interface") <<
"Start setup for reweightGen";
214 edm::LogInfo(
"Pythia8Interface") <<
"End setup for reweightGen";
216 if (
params.exists(
"reweightGenEmp")) {
217 edm::LogInfo(
"Pythia8Interface") <<
"Start setup for reweightGenEmp";
221 if (rgeParams.
exists(
"tune"))
224 edm::LogInfo(
"Pythia8Interface") <<
"End setup for reweightGenEmp";
226 if (
params.exists(
"reweightGenRap")) {
227 edm::LogInfo(
"Pythia8Interface") <<
"Start setup for reweightGenRap";
235 edm::LogInfo(
"Pythia8Interface") <<
"End setup for reweightGenRap";
237 if (
params.exists(
"reweightGenPtHatRap")) {
238 edm::LogInfo(
"Pythia8Interface") <<
"Start setup for reweightGenPtHatRap";
246 edm::LogInfo(
"Pythia8Interface") <<
"End setup for reweightGenPtHatRap";
249 if (
params.exists(
"useUserHook"))
251 <<
" Obsolete parameter: useUserHook \n Please use the actual one instead \n";
255 if (
params.exists(
"jetMatching")) {
258 if (
scheme ==
"Madgraph" ||
scheme ==
"MadgraphFastJet") {
265 if (
params.exists(
"emissionVeto1")) {
267 if (
params.exists(
"EV1_nFinal"))
270 if (
params.exists(
"EV1_vetoOn"))
273 if (
params.exists(
"EV1_maxVetoCount"))
276 if (
params.exists(
"EV1_pThardMode"))
279 if (
params.exists(
"EV1_pTempMode"))
284 if (
params.exists(
"EV1_emittedMode"))
287 if (
params.exists(
"EV1_pTdefMode"))
290 if (
params.exists(
"EV1_MPIvetoOn"))
293 if (
params.exists(
"EV1_QEDvetoMode"))
296 if (
params.exists(
"EV1_nFinalMode"))
311 if (
params.exists(
"VinciaPlugin")) {
313 <<
" Obsolete parameter: VinciaPlugin \n Please use the parameter PartonShowers:model instead \n";
315 if (
params.exists(
"DirePlugin")) {
317 <<
" Obsolete parameter: DirePlugin \n Please use the parameter PartonShowers:model instead \n";
324 bool status =
false, status1 =
false;
333 fMasterGen->settings.mode(
"Beams:idB", -2212);
340 <<
" UNKNOWN INITIAL STATE. \n The allowed initial states are: PP, PPbar, ElectronPositron \n";
344 fMasterGen->settings.mode(
"Beams:frameType", 4);
363 edm::LogInfo(
"Pythia8Interface") <<
"Turning on Emission Veto Hook 1 from CMSSW Pythia8Interface";
367 if (
fMasterGen->settings.mode(
"POWHEG:veto") > 0 ||
fMasterGen->settings.mode(
"POWHEG:MPIveto") > 0) {
370 <<
" Attempt to turn on PowhegHooks by pythia8 settings but there are incompatible hooks on \n Incompatible "
371 "are : jetMatching, emissionVeto1 \n";
376 edm::LogInfo(
"Pythia8Interface") <<
"Turning on Emission Veto Hook from pythia8 code";
380 bool PowhegRes =
fMasterGen->settings.flag(
"POWHEGres:calcScales");
382 edm::LogInfo(
"Pythia8Interface") <<
"Turning on resonance scale setting from CMSSW Pythia8Interface";
388 bool PowhegBB4L =
fMasterGen->settings.flag(
"POWHEG:bb4l");
390 edm::LogInfo(
"Pythia8Interface") <<
"Turning on BB4l hook from CMSSW Pythia8Interface";
397 bool internalMatching =
fMasterGen->settings.flag(
"JetMatching:merge");
398 bool internalMerging = !(
fMasterGen->settings.word(
"Merging:Process") ==
"void");
400 if (internalMatching && internalMerging) {
402 <<
" Only one jet matching/merging scheme can be used at a time. \n";
405 if (internalMatching) {
411 if (internalMerging) {
414 : ((
fMasterGen->settings.flag(
"Merging:doUNLOPSTree") ||
415 fMasterGen->settings.flag(
"Merging:doUNLOPSSubt") ||
416 fMasterGen->settings.flag(
"Merging:doUNLOPSLoop") ||
417 fMasterGen->settings.flag(
"Merging:doUNLOPSSubtNLO"))
425 bool biasedTauDecayer =
fMasterGen->settings.flag(
"BiasedTauDecayer:filter");
426 if (biasedTauDecayer) {
431 std::vector<int> handledParticles;
432 handledParticles.push_back(15);
436 bool resonanceDecayFilter =
fMasterGen->settings.flag(
"ResonanceDecayFilter:filter");
437 if (resonanceDecayFilter) {
442 bool PTFilter =
fMasterGen->settings.flag(
"PTFilter:filter");
453 edm::LogInfo(
"Pythia8Interface") <<
"Initializing MasterGen";
469 fDecayer->settings.flag(
"ProcessLevel:all",
false);
470 fDecayer->settings.flag(
"ProcessLevel:resonanceDecays",
true);
471 edm::LogInfo(
"Pythia8Interface") <<
"Initializing Decayer";
475 edm::LogInfo(
"Pythia8Hadronizer") <<
"Creating and initializing pythia8 EvtGen plugin";
483 return (
status && status1);
487 edm::LogInfo(
"Pythia8Interface") <<
"Initializing for external partons";
489 bool status =
false, status1 =
false;
506 edm::LogInfo(
"Pythia8Interface") <<
"Turning on Emission Veto Hook 1 from CMSSW Pythia8Interface";
510 if (
fMasterGen->settings.mode(
"POWHEG:veto") > 0 ||
fMasterGen->settings.mode(
"POWHEG:MPIveto") > 0) {
513 <<
" Attempt to turn on PowhegHooks by pythia8 settings but there are incompatible hooks on \n Incompatible "
514 "are : jetMatching, emissionVeto1 \n";
519 edm::LogInfo(
"Pythia8Interface") <<
"Turning on Emission Veto Hook from pythia8 code";
523 bool PowhegRes =
fMasterGen->settings.flag(
"POWHEGres:calcScales");
525 edm::LogInfo(
"Pythia8Interface") <<
"Turning on resonance scale setting from CMSSW Pythia8Interface";
531 bool PowhegBB4L =
fMasterGen->settings.flag(
"POWHEG:bb4l");
533 edm::LogInfo(
"Pythia8Interface") <<
"Turning on BB4l hook from CMSSW Pythia8Interface";
540 bool internalMatching =
fMasterGen->settings.flag(
"JetMatching:merge");
541 bool internalMerging = !(
fMasterGen->settings.word(
"Merging:Process") ==
"void");
543 if (internalMatching && internalMerging) {
545 <<
" Only one jet matching/merging scheme can be used at a time. \n";
548 if (internalMatching) {
554 if (internalMerging) {
557 : ((
fMasterGen->settings.flag(
"Merging:doUNLOPSTree") ||
558 fMasterGen->settings.flag(
"Merging:doUNLOPSSubt") ||
559 fMasterGen->settings.flag(
"Merging:doUNLOPSLoop") ||
560 fMasterGen->settings.flag(
"Merging:doUNLOPSSubtNLO"))
568 bool biasedTauDecayer =
fMasterGen->settings.flag(
"BiasedTauDecayer:filter");
569 if (biasedTauDecayer) {
574 std::vector<int> handledParticles;
575 handledParticles.push_back(15);
579 bool resonanceDecayFilter =
fMasterGen->settings.flag(
"ResonanceDecayFilter:filter");
580 if (resonanceDecayFilter) {
585 bool PTFilter =
fMasterGen->settings.flag(
"PTFilter:filter");
598 edm::LogInfo(
"Pythia8Interface") <<
"Some LHE information can be not stored";
599 fMasterGen->settings.mode(
"Beams:frameType", 4);
605 lhaUP->setScalesFromLHEF(
fMasterGen->settings.flag(
"Beams:setProductionScalesFromLHEF"));
612 fMasterGen->settings.mode(
"Beams:frameType", 5);
614 edm::LogInfo(
"Pythia8Interface") <<
"Initializing MasterGen";
631 fDecayer->settings.flag(
"ProcessLevel:all",
false);
632 fDecayer->settings.flag(
"ProcessLevel:resonanceDecays",
true);
633 edm::LogInfo(
"Pythia8Interface") <<
"Initializing Decayer";
637 edm::LogInfo(
"Pythia8Hadronizer") <<
"Creating and initializing pythia8 EvtGen plugin";
645 return (
status && status1);
653 <<
"Number of ISR vetoed = " <<
nISRveto;
677 double mergeweight =
fMasterGen.get()->info.mergingWeightNLO();
692 for (
unsigned int idjr = 0; idjr < ndjr; ++idjr) {
693 DJR.push_back(djrmatch[idjr]);
703 event() = std::make_unique<HepMC::GenEvent>();
711 if (mergeweight != 1.) {
712 event()->weights()[0] *= mergeweight;
721 if (
fMasterGen->info.getWeightsDetailedSize() > 0) {
722 for (
const string &
key :
fMasterGen->info.initrwgt->weightsKeys) {
724 event()->weights().push_back(wgt);
726 }
else if (
fMasterGen->info.getWeightsCompressedSize() > 0) {
727 for (
unsigned int i = 0;
i <
fMasterGen->info.getWeightsCompressedSize();
i++) {
728 double wgt =
fMasterGen->info.getWeightsCompressedValue(
i);
729 event()->weights().push_back(wgt);
738 event()->weights().push_back(wgt);
746 event()->weights()[0] *= fvincia->weight(0);
747 for (
int iVar = 1; iVar < fvincia->nWeights(); iVar++) {
748 event()->weights().push_back(fvincia->weight(iVar));
754 fDire->weightsPtr->calcWeight(0.);
755 fDire->weightsPtr->reset();
758 event()->weights()[0] *= fDire->weightsPtr->getShowerWeight(
"base");
760 unordered_map<string, double>::iterator it;
761 for (it = fDire->weightsPtr->getShowerWeights()->begin(); it != fDire->weightsPtr->getShowerWeights()->end();
763 if (it->first ==
"base")
765 event()->weights().push_back(it->second);
787 double mergeweight =
fMasterGen.get()->info.mergingWeightNLO();
793 if (!py8next ||
std::abs(mergeweight) == 0.) {
803 for (
unsigned int idjr = 0; idjr < ndjr; ++idjr) {
804 DJR.push_back(djrmatch[idjr]);
818 event() = std::make_unique<HepMC::GenEvent>();
825 if (mergeweight != 1.) {
826 event()->weights()[0] *= mergeweight;
839 event()->weights().push_back(wgt);
849 int NPartsBeforeDecays = pythiaEvent->size();
850 int NPartsAfterDecays =
event().get()->particles_size();
852 if (NPartsAfterDecays == NPartsBeforeDecays)
857 for (
int ipart = NPartsAfterDecays; ipart > NPartsBeforeDecays; ipart--) {
860 if (
part->status() == 1 && (
fDecayer->particleData).canDecay(
part->pdg_id())) {
870 part->momentum().x(),
871 part->momentum().y(),
872 part->momentum().z(),
873 part->momentum().t(),
874 part->generated_mass());
875 HepMC::GenVertex *ProdVtx =
part->production_vertex();
876 py8part.vProd(ProdVtx->position().x(), ProdVtx->position().y(), ProdVtx->position().z(), ProdVtx->position().t());
877 py8part.tau((
fDecayer->particleData).tau0(
part->pdg_id()));
879 int nentries =
fDecayer->event.size();
880 if (!
fDecayer->event[nentries - 1].mayDecay())
883 int nentries1 =
fDecayer->event.size();
884 if (nentries1 <= nentries)
901 (
event()->weights()).push_back(1.);
929 <<
"----------------------" << std::endl;
934 <<
"----------------------" << std::endl;
950 int weights_number =
fMasterGen->info.nWeights();
952 weights_number +=
fMasterGen->info.initrwgt->weightsKeys.size();
953 if (weights_number > 1) {
962 for (
const auto &wgtgrp :
fMasterGen->info.initrwgt->weightgroups) {
963 const auto &wgtgrpwgt = wgtgrp.second.weights.find(
key);
964 if (wgtgrpwgt != wgtgrp.second.weights.end()) {
965 weightgroupname = wgtgrp.first;
969 std::ostringstream weightname;
970 weightname <<
"LHE, id = " <<
key <<
", ";
971 if (!weightgroupname.empty()) {
972 weightname <<
"group = " << weightgroupname <<
", ";
974 weightname <<
fMasterGen->info.initrwgt->weights[
key].contents;
992 for (
int iVar = 0; iVar < fvincia->nWeights(); iVar++) {
1001 unordered_map<string, double>::iterator it;
1002 for (it = fDire->weightsPtr->getShowerWeights()->begin(); it != fDire->weightsPtr->getShowerWeights()->end();
1004 if (it->first ==
"base")