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");
455 edm::LogInfo(
"Pythia8Interface") <<
"Initializing MasterGen";
471 fDecayer->settings.flag(
"ProcessLevel:all",
false);
472 fDecayer->settings.flag(
"ProcessLevel:resonanceDecays",
true);
473 edm::LogInfo(
"Pythia8Interface") <<
"Initializing Decayer";
477 edm::LogInfo(
"Pythia8Hadronizer") <<
"Creating and initializing pythia8 EvtGen plugin";
485 return (
status && status1);
489 edm::LogInfo(
"Pythia8Interface") <<
"Initializing for external partons";
491 bool status =
false, status1 =
false;
508 edm::LogInfo(
"Pythia8Interface") <<
"Turning on Emission Veto Hook 1 from CMSSW Pythia8Interface";
512 if (
fMasterGen->settings.mode(
"POWHEG:veto") > 0 ||
fMasterGen->settings.mode(
"POWHEG:MPIveto") > 0) {
515 <<
" Attempt to turn on PowhegHooks by pythia8 settings but there are incompatible hooks on \n Incompatible "
516 "are : jetMatching, emissionVeto1 \n";
521 edm::LogInfo(
"Pythia8Interface") <<
"Turning on Emission Veto Hook from pythia8 code";
525 bool PowhegRes =
fMasterGen->settings.flag(
"POWHEGres:calcScales");
527 edm::LogInfo(
"Pythia8Interface") <<
"Turning on resonance scale setting from CMSSW Pythia8Interface";
533 bool PowhegBB4L =
fMasterGen->settings.flag(
"POWHEG:bb4l");
535 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) {
556 if (internalMerging) {
559 : ((
fMasterGen->settings.flag(
"Merging:doUNLOPSTree") ||
560 fMasterGen->settings.flag(
"Merging:doUNLOPSSubt") ||
561 fMasterGen->settings.flag(
"Merging:doUNLOPSLoop") ||
562 fMasterGen->settings.flag(
"Merging:doUNLOPSSubtNLO"))
570 bool biasedTauDecayer =
fMasterGen->settings.flag(
"BiasedTauDecayer:filter");
571 if (biasedTauDecayer) {
576 std::vector<int> handledParticles;
577 handledParticles.push_back(15);
581 bool resonanceDecayFilter =
fMasterGen->settings.flag(
"ResonanceDecayFilter:filter");
582 if (resonanceDecayFilter) {
587 bool PTFilter =
fMasterGen->settings.flag(
"PTFilter:filter");
602 edm::LogInfo(
"Pythia8Interface") <<
"Some LHE information can be not stored";
603 fMasterGen->settings.mode(
"Beams:frameType", 4);
609 lhaUP->setScalesFromLHEF(
fMasterGen->settings.flag(
"Beams:setProductionScalesFromLHEF"));
616 fMasterGen->settings.mode(
"Beams:frameType", 5);
618 edm::LogInfo(
"Pythia8Interface") <<
"Initializing MasterGen";
635 fDecayer->settings.flag(
"ProcessLevel:all",
false);
636 fDecayer->settings.flag(
"ProcessLevel:resonanceDecays",
true);
637 edm::LogInfo(
"Pythia8Interface") <<
"Initializing Decayer";
641 edm::LogInfo(
"Pythia8Hadronizer") <<
"Creating and initializing pythia8 EvtGen plugin";
649 return (
status && status1);
657 <<
"Number of ISR vetoed = " <<
nISRveto;
681 double mergeweight =
fMasterGen.get()->info.mergingWeightNLO();
696 for (
unsigned int idjr = 0; idjr < ndjr; ++idjr) {
697 DJR.push_back(djrmatch[idjr]);
707 event() = std::make_unique<HepMC::GenEvent>();
715 if (mergeweight != 1.) {
716 event()->weights()[0] *= mergeweight;
725 if (
fMasterGen->info.getWeightsDetailedSize() > 0) {
726 for (
const string &
key :
fMasterGen->info.initrwgt->weightsKeys) {
728 event()->weights().push_back(wgt);
730 }
else if (
fMasterGen->info.getWeightsCompressedSize() > 0) {
731 for (
unsigned int i = 0;
i <
fMasterGen->info.getWeightsCompressedSize();
i++) {
732 double wgt =
fMasterGen->info.getWeightsCompressedValue(
i);
733 event()->weights().push_back(wgt);
742 event()->weights().push_back(wgt);
750 event()->weights()[0] *= fvincia->weight(0);
751 for (
int iVar = 1; iVar < fvincia->nWeights(); iVar++) {
752 event()->weights().push_back(fvincia->weight(iVar));
758 fDire->weightsPtr->calcWeight(0.);
759 fDire->weightsPtr->reset();
762 event()->weights()[0] *= fDire->weightsPtr->getShowerWeight(
"base");
764 unordered_map<string, double>::iterator it;
765 for (it = fDire->weightsPtr->getShowerWeights()->begin(); it != fDire->weightsPtr->getShowerWeights()->end();
767 if (it->first ==
"base")
769 event()->weights().push_back(it->second);
791 double mergeweight =
fMasterGen.get()->info.mergingWeightNLO();
797 if (!py8next ||
std::abs(mergeweight) == 0.) {
807 for (
unsigned int idjr = 0; idjr < ndjr; ++idjr) {
808 DJR.push_back(djrmatch[idjr]);
822 event() = std::make_unique<HepMC::GenEvent>();
829 if (mergeweight != 1.) {
830 event()->weights()[0] *= mergeweight;
843 event()->weights().push_back(wgt);
853 int NPartsBeforeDecays = pythiaEvent->size();
854 int NPartsAfterDecays =
event().get()->particles_size();
856 if (NPartsAfterDecays == NPartsBeforeDecays)
861 for (
int ipart = NPartsAfterDecays; ipart > NPartsBeforeDecays; ipart--) {
864 if (
part->status() == 1 && (
fDecayer->particleData).canDecay(
part->pdg_id())) {
874 part->momentum().x(),
875 part->momentum().y(),
876 part->momentum().z(),
877 part->momentum().t(),
878 part->generated_mass());
879 HepMC::GenVertex *ProdVtx =
part->production_vertex();
880 py8part.vProd(ProdVtx->position().x(), ProdVtx->position().y(), ProdVtx->position().z(), ProdVtx->position().t());
881 py8part.tau((
fDecayer->particleData).tau0(
part->pdg_id()));
883 int nentries =
fDecayer->event.size();
884 if (!
fDecayer->event[nentries - 1].mayDecay())
887 int nentries1 =
fDecayer->event.size();
888 if (nentries1 <= nentries)
905 (
event()->weights()).push_back(1.);
933 <<
"----------------------" << std::endl;
938 <<
"----------------------" << std::endl;
954 int weights_number =
fMasterGen->info.nWeights();
956 weights_number +=
fMasterGen->info.initrwgt->weightsKeys.size();
957 if (weights_number > 1) {
966 for (
const auto &wgtgrp :
fMasterGen->info.initrwgt->weightgroups) {
967 const auto &wgtgrpwgt = wgtgrp.second.weights.find(
key);
968 if (wgtgrpwgt != wgtgrp.second.weights.end()) {
969 weightgroupname = wgtgrp.first;
973 std::ostringstream weightname;
974 weightname <<
"LHE, id = " <<
key <<
", ";
975 if (!weightgroupname.empty()) {
976 weightname <<
"group = " << weightgroupname <<
", ";
978 weightname <<
fMasterGen->info.initrwgt->weights[
key].contents;
996 for (
int iVar = 0; iVar < fvincia->nWeights(); iVar++) {
1005 unordered_map<string, double>::iterator it;
1006 for (it = fDire->weightsPtr->getShowerWeights()->begin(); it != fDire->weightsPtr->getShowerWeights()->end();
1008 if (it->first ==
"base")