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"
66 #include "HepPID/ParticleIDTranslations.hh"
72 class HepRandomEngine;
82 bool initializeForInternalPartons()
override;
83 bool initializeForExternalPartons();
85 bool generatePartonsAndHadronize()
override;
88 virtual bool residualDecay();
90 void finalizeEvent()
override;
94 const char *
classname()
const override {
return "Pythia8Hadronizer"; }
96 std::unique_ptr<GenLumiInfoHeader> getGenLumiInfoHeader()
const override;
100 std::vector<std::string>
const &
doSharedResources()
const override {
return p8SharedResources; }
106 std::shared_ptr<LHAupLesHouches>
lhaUP;
108 enum { PP,
PPbar, ElectronPositron };
175 LHEInputFileName(
params.getUntrackedParameter<
std::
string>(
"LHEInputFileName",
"")),
184 if (
params.exists(
"PPbarInitialState")) {
188 <<
"Pythia8 will be initialized for PROTON-ANTIPROTON INITIAL STATE. "
189 <<
"This is a user-request change from the DEFAULT PROTON-PROTON initial state.";
193 }
else if (
params.exists(
"ElectronPositronInitialState")) {
197 <<
"Pythia8 will be initialized for ELECTRON-POSITRON INITIAL STATE. "
198 <<
"This is a user-request change from the DEFAULT PROTON-PROTON initial state.";
202 }
else if (
params.exists(
"ElectronProtonInitialState") ||
params.exists(
"PositronProtonInitialState")) {
205 <<
" UNKNOWN INITIAL STATE. \n The allowed initial states are: PP, PPbar, ElectronPositron \n";
210 if (
params.exists(
"reweightGen")) {
211 edm::LogInfo(
"Pythia8Interface") <<
"Start setup for reweightGen";
215 edm::LogInfo(
"Pythia8Interface") <<
"End setup for reweightGen";
217 if (
params.exists(
"reweightGenEmp")) {
218 edm::LogInfo(
"Pythia8Interface") <<
"Start setup for reweightGenEmp";
222 if (rgeParams.
exists(
"tune"))
225 edm::LogInfo(
"Pythia8Interface") <<
"End setup for reweightGenEmp";
227 if (
params.exists(
"reweightGenRap")) {
228 edm::LogInfo(
"Pythia8Interface") <<
"Start setup for reweightGenRap";
236 edm::LogInfo(
"Pythia8Interface") <<
"End setup for reweightGenRap";
238 if (
params.exists(
"reweightGenPtHatRap")) {
239 edm::LogInfo(
"Pythia8Interface") <<
"Start setup for reweightGenPtHatRap";
247 edm::LogInfo(
"Pythia8Interface") <<
"End setup for reweightGenPtHatRap";
250 if (
params.exists(
"useUserHook"))
252 <<
" Obsolete parameter: useUserHook \n Please use the actual one instead \n";
256 if (
params.exists(
"jetMatching")) {
259 if (
scheme ==
"Madgraph" ||
scheme ==
"MadgraphFastJet") {
266 if (
params.exists(
"emissionVeto1")) {
268 if (
params.exists(
"EV1_nFinal"))
271 if (
params.exists(
"EV1_vetoOn"))
274 if (
params.exists(
"EV1_maxVetoCount"))
277 if (
params.exists(
"EV1_pThardMode"))
280 if (
params.exists(
"EV1_pTempMode"))
285 if (
params.exists(
"EV1_emittedMode"))
288 if (
params.exists(
"EV1_pTdefMode"))
291 if (
params.exists(
"EV1_MPIvetoOn"))
294 if (
params.exists(
"EV1_QEDvetoMode"))
297 if (
params.exists(
"EV1_nFinalMode"))
312 if (
params.exists(
"VinciaPlugin")) {
314 <<
" Obsolete parameter: VinciaPlugin \n Please use the parameter PartonShowers:model instead \n";
316 if (
params.exists(
"DirePlugin")) {
318 <<
" Obsolete parameter: DirePlugin \n Please use the parameter PartonShowers:model instead \n";
325 bool status =
false, status1 =
false;
334 fMasterGen->settings.mode(
"Beams:idB", -2212);
341 <<
" UNKNOWN INITIAL STATE. \n The allowed initial states are: PP, PPbar, ElectronPositron \n";
345 fMasterGen->settings.mode(
"Beams:frameType", 4);
364 edm::LogInfo(
"Pythia8Interface") <<
"Turning on Emission Veto Hook 1 from CMSSW Pythia8Interface";
368 if (
fMasterGen->settings.mode(
"POWHEG:veto") > 0 ||
fMasterGen->settings.mode(
"POWHEG:MPIveto") > 0) {
371 <<
" Attempt to turn on PowhegHooks by pythia8 settings but there are incompatible hooks on \n Incompatible "
372 "are : jetMatching, emissionVeto1 \n";
377 edm::LogInfo(
"Pythia8Interface") <<
"Turning on Emission Veto Hook from pythia8 code";
381 bool PowhegRes =
fMasterGen->settings.flag(
"POWHEGres:calcScales");
383 edm::LogInfo(
"Pythia8Interface") <<
"Turning on resonance scale setting from CMSSW Pythia8Interface";
389 bool PowhegBB4L =
fMasterGen->settings.flag(
"POWHEG:bb4l");
391 edm::LogInfo(
"Pythia8Interface") <<
"Turning on BB4l hook from CMSSW Pythia8Interface";
398 bool internalMatching =
fMasterGen->settings.flag(
"JetMatching:merge");
399 bool internalMerging = !(
fMasterGen->settings.word(
"Merging:Process") ==
"void");
401 if (internalMatching && internalMerging) {
403 <<
" Only one jet matching/merging scheme can be used at a time. \n";
406 if (internalMatching) {
412 if (internalMerging) {
415 : ((
fMasterGen->settings.flag(
"Merging:doUNLOPSTree") ||
416 fMasterGen->settings.flag(
"Merging:doUNLOPSSubt") ||
417 fMasterGen->settings.flag(
"Merging:doUNLOPSLoop") ||
418 fMasterGen->settings.flag(
"Merging:doUNLOPSSubtNLO"))
426 bool biasedTauDecayer =
fMasterGen->settings.flag(
"BiasedTauDecayer:filter");
427 if (biasedTauDecayer) {
432 std::vector<int> handledParticles;
433 handledParticles.push_back(15);
437 bool resonanceDecayFilter =
fMasterGen->settings.flag(
"ResonanceDecayFilter:filter");
438 if (resonanceDecayFilter) {
443 bool PTFilter =
fMasterGen->settings.flag(
"PTFilter:filter");
456 edm::LogInfo(
"Pythia8Interface") <<
"Initializing MasterGen";
472 fDecayer->settings.flag(
"ProcessLevel:all",
false);
473 fDecayer->settings.flag(
"ProcessLevel:resonanceDecays",
true);
474 edm::LogInfo(
"Pythia8Interface") <<
"Initializing Decayer";
478 edm::LogInfo(
"Pythia8Hadronizer") <<
"Creating and initializing pythia8 EvtGen plugin";
486 return (
status && status1);
490 edm::LogInfo(
"Pythia8Interface") <<
"Initializing for external partons";
492 bool status =
false, status1 =
false;
509 edm::LogInfo(
"Pythia8Interface") <<
"Turning on Emission Veto Hook 1 from CMSSW Pythia8Interface";
513 if (
fMasterGen->settings.mode(
"POWHEG:veto") > 0 ||
fMasterGen->settings.mode(
"POWHEG:MPIveto") > 0) {
516 <<
" Attempt to turn on PowhegHooks by pythia8 settings but there are incompatible hooks on \n Incompatible "
517 "are : jetMatching, emissionVeto1 \n";
522 edm::LogInfo(
"Pythia8Interface") <<
"Turning on Emission Veto Hook from pythia8 code";
526 bool PowhegRes =
fMasterGen->settings.flag(
"POWHEGres:calcScales");
528 edm::LogInfo(
"Pythia8Interface") <<
"Turning on resonance scale setting from CMSSW Pythia8Interface";
534 bool PowhegBB4L =
fMasterGen->settings.flag(
"POWHEG:bb4l");
536 edm::LogInfo(
"Pythia8Interface") <<
"Turning on BB4l hook from CMSSW Pythia8Interface";
543 bool internalMatching =
fMasterGen->settings.flag(
"JetMatching:merge");
544 bool internalMerging = !(
fMasterGen->settings.word(
"Merging:Process") ==
"void");
546 if (internalMatching && internalMerging) {
548 <<
" Only one jet matching/merging scheme can be used at a time. \n";
551 if (internalMatching) {
557 if (internalMerging) {
560 : ((
fMasterGen->settings.flag(
"Merging:doUNLOPSTree") ||
561 fMasterGen->settings.flag(
"Merging:doUNLOPSSubt") ||
562 fMasterGen->settings.flag(
"Merging:doUNLOPSLoop") ||
563 fMasterGen->settings.flag(
"Merging:doUNLOPSSubtNLO"))
571 bool biasedTauDecayer =
fMasterGen->settings.flag(
"BiasedTauDecayer:filter");
572 if (biasedTauDecayer) {
577 std::vector<int> handledParticles;
578 handledParticles.push_back(15);
582 bool resonanceDecayFilter =
fMasterGen->settings.flag(
"ResonanceDecayFilter:filter");
583 if (resonanceDecayFilter) {
588 bool PTFilter =
fMasterGen->settings.flag(
"PTFilter:filter");
603 edm::LogInfo(
"Pythia8Interface") <<
"Some LHE information can be not stored";
604 fMasterGen->settings.mode(
"Beams:frameType", 4);
610 lhaUP->setScalesFromLHEF(
fMasterGen->settings.flag(
"Beams:setProductionScalesFromLHEF"));
617 fMasterGen->settings.mode(
"Beams:frameType", 5);
619 edm::LogInfo(
"Pythia8Interface") <<
"Initializing MasterGen";
636 fDecayer->settings.flag(
"ProcessLevel:all",
false);
637 fDecayer->settings.flag(
"ProcessLevel:resonanceDecays",
true);
638 edm::LogInfo(
"Pythia8Interface") <<
"Initializing Decayer";
642 edm::LogInfo(
"Pythia8Hadronizer") <<
"Creating and initializing pythia8 EvtGen plugin";
650 return (
status && status1);
658 <<
"Number of ISR vetoed = " <<
nISRveto;
682 double mergeweight =
fMasterGen.get()->info.mergingWeightNLO();
697 for (
unsigned int idjr = 0; idjr < ndjr; ++idjr) {
698 DJR.push_back(djrmatch[idjr]);
708 event() = std::make_unique<HepMC::GenEvent>();
716 if (mergeweight != 1.) {
717 event()->weights()[0] *= mergeweight;
726 if (
fMasterGen->info.getWeightsDetailedSize() > 0) {
727 for (
const string &
key :
fMasterGen->info.initrwgt->weightsKeys) {
729 event()->weights().push_back(wgt);
731 }
else if (
fMasterGen->info.getWeightsCompressedSize() > 0) {
732 for (
unsigned int i = 0;
i <
fMasterGen->info.getWeightsCompressedSize();
i++) {
733 double wgt =
fMasterGen->info.getWeightsCompressedValue(
i);
734 event()->weights().push_back(wgt);
743 event()->weights().push_back(wgt);
751 event()->weights()[0] *= fvincia->weight(0);
752 for (
int iVar = 1; iVar < fvincia->nWeights(); iVar++) {
753 event()->weights().push_back(fvincia->weight(iVar));
759 fDire->weightsPtr->calcWeight(0.);
760 fDire->weightsPtr->reset();
763 event()->weights()[0] *= fDire->weightsPtr->getShowerWeight(
"base");
765 unordered_map<string, double>::iterator it;
766 for (it = fDire->weightsPtr->getShowerWeights()->begin(); it != fDire->weightsPtr->getShowerWeights()->end();
768 if (it->first ==
"base")
770 event()->weights().push_back(it->second);
792 double mergeweight =
fMasterGen.get()->info.mergingWeightNLO();
798 if (!py8next ||
std::abs(mergeweight) == 0.) {
808 for (
unsigned int idjr = 0; idjr < ndjr; ++idjr) {
809 DJR.push_back(djrmatch[idjr]);
823 event() = std::make_unique<HepMC::GenEvent>();
830 if (mergeweight != 1.) {
831 event()->weights()[0] *= mergeweight;
844 event()->weights().push_back(wgt);
854 int NPartsBeforeDecays = pythiaEvent->size();
855 int NPartsAfterDecays =
event().get()->particles_size();
857 if (NPartsAfterDecays == NPartsBeforeDecays)
862 for (
int ipart = NPartsAfterDecays; ipart > NPartsBeforeDecays; ipart--) {
865 if (
part->status() == 1 && (
fDecayer->particleData).canDecay(
part->pdg_id())) {
875 part->momentum().x(),
876 part->momentum().y(),
877 part->momentum().z(),
878 part->momentum().t(),
879 part->generated_mass());
880 HepMC::GenVertex *ProdVtx =
part->production_vertex();
881 py8part.vProd(ProdVtx->position().x(), ProdVtx->position().y(), ProdVtx->position().z(), ProdVtx->position().t());
882 py8part.tau((
fDecayer->particleData).tau0(
part->pdg_id()));
884 int nentries =
fDecayer->event.size();
885 if (!
fDecayer->event[nentries - 1].mayDecay())
888 int nentries1 =
fDecayer->event.size();
889 if (nentries1 <= nentries)
906 (
event()->weights()).push_back(1.);
934 <<
"----------------------" << std::endl;
939 <<
"----------------------" << std::endl;
955 int weights_number =
fMasterGen->info.nWeights();
957 weights_number +=
fMasterGen->info.initrwgt->weightsKeys.size();
958 if (weights_number > 1) {
967 for (
const auto &wgtgrp :
fMasterGen->info.initrwgt->weightgroups) {
968 const auto &wgtgrpwgt = wgtgrp.second.weights.find(
key);
969 if (wgtgrpwgt != wgtgrp.second.weights.end()) {
970 weightgroupname = wgtgrp.first;
974 std::ostringstream weightname;
975 weightname <<
"LHE, id = " <<
key <<
", ";
976 if (!weightgroupname.empty()) {
977 weightname <<
"group = " << weightgroupname <<
", ";
979 weightname <<
fMasterGen->info.initrwgt->weights[
key].contents;
997 for (
int iVar = 0; iVar < fvincia->nWeights(); iVar++) {
1006 unordered_map<string, double>::iterator it;
1007 for (it = fDire->weightsPtr->getShowerWeights()->begin(); it != fDire->weightsPtr->getShowerWeights()->end();
1009 if (it->first ==
"base")