8 #include "HepMC/GenEvent.h"
9 #include "HepMC/GenParticle.h"
11 #include "Pythia8/Pythia.h"
12 #include "Pythia8Plugins/HepMC2.h"
14 #include "Pythia8/Vincia.h"
15 #include "Dire/Dire.h"
26 #include "Pythia8Plugins/JetMatching.h"
27 #include "Pythia8Plugins/aMCatNLOHooks.h"
33 #include "Pythia8Plugins/PowhegHooks.h"
51 #include "Pythia8Plugins/EvtGen.h"
68 #include "HepPID/ParticleIDTranslations.hh"
74 class HepRandomEngine;
84 bool initializeForInternalPartons()
override;
85 bool initializeForExternalPartons();
87 bool generatePartonsAndHadronize()
override;
90 virtual bool residualDecay();
92 void finalizeEvent()
override;
96 const char *
classname()
const override {
return "Pythia8Hadronizer"; }
98 std::unique_ptr<GenLumiInfoHeader> getGenLumiInfoHeader()
const override;
101 std::unique_ptr<Pythia8::VinciaPlugin>
fvincia;
102 std::unique_ptr<Pythia8::Dire>
fDire;
105 std::vector<std::string>
const &
doSharedResources()
const override {
return p8SharedResources; }
111 std::unique_ptr<LHAupLesHouches>
lhaUP;
113 enum { PP,
PPbar, ElectronPositron };
179 LHEInputFileName(
params.getUntrackedParameter<
std::
string>(
"LHEInputFileName",
"")),
187 if (
params.exists(
"PPbarInitialState")) {
191 <<
"Pythia8 will be initialized for PROTON-ANTIPROTON INITIAL STATE. "
192 <<
"This is a user-request change from the DEFAULT PROTON-PROTON initial state.";
196 }
else if (
params.exists(
"ElectronPositronInitialState")) {
200 <<
"Pythia8 will be initialized for ELECTRON-POSITRON INITIAL STATE. "
201 <<
"This is a user-request change from the DEFAULT PROTON-PROTON initial state.";
205 }
else if (
params.exists(
"ElectronProtonInitialState") ||
params.exists(
"PositronProtonInitialState")) {
208 <<
" UNKNOWN INITIAL STATE. \n The allowed initial states are: PP, PPbar, ElectronPositron \n";
213 if (
params.exists(
"reweightGen")) {
214 edm::LogInfo(
"Pythia8Interface") <<
"Start setup for reweightGen";
218 edm::LogInfo(
"Pythia8Interface") <<
"End setup for reweightGen";
220 if (
params.exists(
"reweightGenEmp")) {
221 edm::LogInfo(
"Pythia8Interface") <<
"Start setup for reweightGenEmp";
225 if (rgeParams.
exists(
"tune"))
228 edm::LogInfo(
"Pythia8Interface") <<
"End setup for reweightGenEmp";
230 if (
params.exists(
"reweightGenRap")) {
231 edm::LogInfo(
"Pythia8Interface") <<
"Start setup for reweightGenRap";
239 edm::LogInfo(
"Pythia8Interface") <<
"End setup for reweightGenRap";
241 if (
params.exists(
"reweightGenPtHatRap")) {
242 edm::LogInfo(
"Pythia8Interface") <<
"Start setup for reweightGenPtHatRap";
250 edm::LogInfo(
"Pythia8Interface") <<
"End setup for reweightGenPtHatRap";
253 if (
params.exists(
"useUserHook"))
255 <<
" Obsolete parameter: useUserHook \n Please use the actual one instead \n";
259 if (
params.exists(
"jetMatching")) {
262 if (
scheme ==
"Madgraph" ||
scheme ==
"MadgraphFastJet") {
269 if (
params.exists(
"emissionVeto1")) {
271 if (
params.exists(
"EV1_nFinal"))
274 if (
params.exists(
"EV1_vetoOn"))
277 if (
params.exists(
"EV1_maxVetoCount"))
280 if (
params.exists(
"EV1_pThardMode"))
283 if (
params.exists(
"EV1_pTempMode"))
288 if (
params.exists(
"EV1_emittedMode"))
291 if (
params.exists(
"EV1_pTdefMode"))
294 if (
params.exists(
"EV1_MPIvetoOn"))
297 if (
params.exists(
"EV1_QEDvetoMode"))
300 if (
params.exists(
"EV1_nFinalMode"))
315 if (
params.exists(
"VinciaPlugin")) {
319 if (
params.exists(
"DirePlugin")) {
321 fDire.reset(
new Pythia8::Dire());
330 bool status =
false, status1 =
false;
339 fMasterGen->settings.mode(
"Beams:idB", -2212);
346 <<
" UNKNOWN INITIAL STATE. \n The allowed initial states are: PP, PPbar, ElectronPositron \n";
350 fMasterGen->settings.mode(
"Beams:frameType", 4);
367 edm::LogInfo(
"Pythia8Interface") <<
"Turning on Emission Veto Hook 1 from CMSSW Pythia8Interface";
371 if (
fMasterGen->settings.mode(
"POWHEG:veto") > 0 ||
fMasterGen->settings.mode(
"POWHEG:MPIveto") > 0) {
374 <<
" Attempt to turn on PowhegHooks by pythia8 settings but there are incompatible hooks on \n Incompatible "
375 "are : jetMatching, emissionVeto1 \n";
379 edm::LogInfo(
"Pythia8Interface") <<
"Turning on Emission Veto Hook from pythia8 code";
383 bool PowhegRes =
fMasterGen->settings.flag(
"POWHEGres:calcScales");
385 edm::LogInfo(
"Pythia8Interface") <<
"Turning on resonance scale setting from CMSSW Pythia8Interface";
390 bool PowhegBB4L =
fMasterGen->settings.flag(
"POWHEG:bb4l");
392 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) {
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"))
423 bool biasedTauDecayer =
fMasterGen->settings.flag(
"BiasedTauDecayer:filter");
424 if (biasedTauDecayer) {
430 std::vector<int> handledParticles;
431 handledParticles.push_back(15);
435 bool resonanceDecayFilter =
fMasterGen->settings.flag(
"ResonanceDecayFilter:filter");
436 if (resonanceDecayFilter) {
441 bool PTFilter =
fMasterGen->settings.flag(
"PTFilter:filter");
451 edm::LogInfo(
"Pythia8Interface") <<
"Initializing MasterGen";
455 }
else if (
fDire.get()) {
457 fDire->weightsPtr->setup();
478 fDecayer->settings.flag(
"ProcessLevel:all",
false);
479 fDecayer->settings.flag(
"ProcessLevel:resonanceDecays",
true);
480 edm::LogInfo(
"Pythia8Interface") <<
"Initializing Decayer";
484 edm::LogInfo(
"Pythia8Hadronizer") <<
"Creating and initializing pythia8 EvtGen plugin";
490 return (
status && status1);
494 edm::LogInfo(
"Pythia8Interface") <<
"Initializing for external partons";
496 bool status =
false, status1 =
false;
511 edm::LogInfo(
"Pythia8Interface") <<
"Turning on Emission Veto Hook 1 from CMSSW Pythia8Interface";
515 if (
fMasterGen->settings.mode(
"POWHEG:veto") > 0 ||
fMasterGen->settings.mode(
"POWHEG:MPIveto") > 0) {
518 <<
" Attempt to turn on PowhegHooks by pythia8 settings but there are incompatible hooks on \n Incompatible "
519 "are : jetMatching, emissionVeto1 \n";
523 edm::LogInfo(
"Pythia8Interface") <<
"Turning on Emission Veto Hook from pythia8 code";
527 bool PowhegRes =
fMasterGen->settings.flag(
"POWHEGres:calcScales");
529 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";
542 bool internalMatching =
fMasterGen->settings.flag(
"JetMatching:merge");
543 bool internalMerging = !(
fMasterGen->settings.word(
"Merging:Process") ==
"void");
545 if (internalMatching && internalMerging) {
547 <<
" Only one jet matching/merging scheme can be used at a time. \n";
550 if (internalMatching) {
555 if (internalMerging) {
558 : ((
fMasterGen->settings.flag(
"Merging:doUNLOPSTree") ||
559 fMasterGen->settings.flag(
"Merging:doUNLOPSSubt") ||
560 fMasterGen->settings.flag(
"Merging:doUNLOPSLoop") ||
561 fMasterGen->settings.flag(
"Merging:doUNLOPSSubtNLO"))
568 bool biasedTauDecayer =
fMasterGen->settings.flag(
"BiasedTauDecayer:filter");
569 if (biasedTauDecayer) {
575 std::vector<int> handledParticles;
576 handledParticles.push_back(15);
580 bool resonanceDecayFilter =
fMasterGen->settings.flag(
"ResonanceDecayFilter:filter");
581 if (resonanceDecayFilter) {
586 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";
643 return (
status && status1);
651 <<
"Number of ISR vetoed = " <<
nISRveto;
675 double mergeweight =
fMasterGen.get()->info.mergingWeightNLO();
690 for (
unsigned int idjr = 0; idjr < ndjr; ++idjr) {
691 DJR.push_back(djrmatch[idjr]);
709 if (mergeweight != 1.) {
710 event()->weights()[0] *= mergeweight;
719 if (
fMasterGen->info.getWeightsDetailedSize() > 0) {
720 for (
const string &
key :
fMasterGen->info.initrwgt->weightsKeys) {
722 event()->weights().push_back(wgt);
724 }
else if (
fMasterGen->info.getWeightsCompressedSize() > 0) {
725 for (
unsigned int i = 0;
i <
fMasterGen->info.getWeightsCompressedSize();
i++) {
726 double wgt =
fMasterGen->info.getWeightsCompressedValue(
i);
727 event()->weights().push_back(wgt);
736 event()->weights().push_back(wgt);
744 for (
int iVar = 1; iVar <
fvincia->nWeights(); iVar++) {
751 fDire->weightsPtr->calcWeight(0.);
752 fDire->weightsPtr->reset();
755 event()->weights()[0] *=
fDire->weightsPtr->getShowerWeight(
"base");
757 unordered_map<string, double>::iterator it;
758 for (it =
fDire->weightsPtr->getShowerWeights()->begin(); it !=
fDire->weightsPtr->getShowerWeights()->end();
760 if (it->first ==
"base")
762 event()->weights().push_back(it->second);
783 double mergeweight =
fMasterGen.get()->info.mergingWeightNLO();
789 if (!py8next ||
std::abs(mergeweight) == 0.) {
799 for (
unsigned int idjr = 0; idjr < ndjr; ++idjr) {
800 DJR.push_back(djrmatch[idjr]);
821 if (mergeweight != 1.) {
822 event()->weights()[0] *= mergeweight;
835 event()->weights().push_back(wgt);
845 int NPartsBeforeDecays = pythiaEvent->size();
846 int NPartsAfterDecays =
event().get()->particles_size();
848 if (NPartsAfterDecays == NPartsBeforeDecays)
853 for (
int ipart = NPartsAfterDecays; ipart > NPartsBeforeDecays; ipart--) {
856 if (
part->status() == 1 && (
fDecayer->particleData).canDecay(
part->pdg_id())) {
866 part->momentum().x(),
867 part->momentum().y(),
868 part->momentum().z(),
869 part->momentum().t(),
870 part->generated_mass());
871 HepMC::GenVertex *ProdVtx =
part->production_vertex();
872 py8part.vProd(ProdVtx->position().x(), ProdVtx->position().y(), ProdVtx->position().z(), ProdVtx->position().t());
873 py8part.tau((
fDecayer->particleData).tau0(
part->pdg_id()));
875 int nentries =
fDecayer->event.size();
876 if (!
fDecayer->event[nentries - 1].mayDecay())
879 int nentries1 =
fDecayer->event.size();
880 if (nentries1 <= nentries)
921 <<
"----------------------" << std::endl;
926 <<
"----------------------" << std::endl;
942 int weights_number =
fMasterGen->info.nWeights();
944 weights_number +=
fMasterGen->info.initrwgt->weightsKeys.size();
945 if (weights_number > 1) {
954 for (
const auto &wgtgrp :
fMasterGen->info.initrwgt->weightgroups) {
955 const auto &wgtgrpwgt = wgtgrp.second.weights.find(
key);
956 if (wgtgrpwgt != wgtgrp.second.weights.end()) {
957 weightgroupname = wgtgrp.first;
961 std::ostringstream weightname;
962 weightname <<
"LHE, id = " <<
key <<
", ";
963 if (!weightgroupname.empty()) {
964 weightname <<
"group = " << weightgroupname <<
", ";
966 weightname <<
fMasterGen->info.initrwgt->weights[
key].contents;
983 for (
int iVar = 0; iVar <
fvincia->nWeights(); iVar++) {
992 unordered_map<string, double>::iterator it;
993 for (it =
fDire->weightsPtr->getShowerWeights()->begin(); it !=
fDire->weightsPtr->getShowerWeights()->end();
995 if (it->first ==
"base")