CMS 3D CMS Logo

Pythia8Hadronizer.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <memory>
3 
4 #include <cstdint>
5 #include <memory>
6 #include <sstream>
7 #include <string>
8 #include <vector>
9 
10 #include "HepMC/GenEvent.h"
11 #include "HepMC/GenParticle.h"
12 
13 #include "Pythia8/Pythia.h"
14 #include "Pythia8Plugins/HepMC2.h"
15 
16 using namespace Pythia8;
17 
19 
22 
23 // PS matchning prototype
24 //
26 #include "Pythia8Plugins/JetMatching.h"
27 #include "Pythia8Plugins/aMCatNLOHooks.h"
28 
29 // Emission Veto Hooks
30 //
31 #include "Pythia8Plugins/PowhegHooks.h"
33 
34 // Resonance scale hook
37 
38 //biased tau decayer
40 
41 //decay filter hook
43 
44 //decay filter hook
46 
47 // EvtGen plugin
48 //
49 #include "Pythia8Plugins/EvtGen.h"
50 
56 
59 
64 
66 
67 #include "HepPID/ParticleIDTranslations.hh"
68 
71 
72 namespace CLHEP {
73  class HepRandomEngine;
74 }
75 
76 using namespace gen;
77 
79 public:
81  ~Pythia8Hadronizer() override;
82 
83  bool initializeForInternalPartons() override;
84  bool initializeForExternalPartons();
85 
86  bool generatePartonsAndHadronize() override;
87  bool hadronize();
88 
89  virtual bool residualDecay();
90 
91  void finalizeEvent() override;
92 
93  void statistics() override;
94 
95  const char *classname() const override { return "Pythia8Hadronizer"; }
96 
97  std::unique_ptr<GenLumiInfoHeader> getGenLumiInfoHeader() const override;
98 
99 private:
100  void doSetRandomEngine(CLHEP::HepRandomEngine *v) override { p8SetRandomEngine(v); }
101  std::vector<std::string> const &doSharedResources() const override { return p8SharedResources; }
102 
104  double comEnergy;
105 
107  std::shared_ptr<LHAupLesHouches> lhaUP;
108 
109  enum { PP, PPbar, ElectronPositron };
110  int fInitialState; // pp, ppbar, or e-e+
111 
112  double fBeam1PZ;
113  double fBeam2PZ;
114 
115  //helper class to allow multiple user hooks simultaneously
116  std::shared_ptr<UserHooksVector> fUserHooksVector;
118 
119  // Reweight user hooks
120  //
121  std::shared_ptr<UserHooks> fReweightUserHook;
122  std::shared_ptr<UserHooks> fReweightEmpUserHook;
123  std::shared_ptr<UserHooks> fReweightRapUserHook;
124  std::shared_ptr<UserHooks> fReweightPtHatRapUserHook;
125 
126  // PS matching prototype
127  //
128  std::shared_ptr<JetMatchingHook> fJetMatchingHook;
129  std::shared_ptr<Pythia8::JetMatchingMadgraph> fJetMatchingPy8InternalHook;
130  std::shared_ptr<Pythia8::amcnlo_unitarised_interface> fMergingHook;
131 
132  // Emission Veto Hooks
133  //
134  std::shared_ptr<PowhegHooks> fEmissionVetoHook;
135  std::shared_ptr<EmissionVetoHook1> fEmissionVetoHook1;
136 
137  // Resonance scale hook
138  std::shared_ptr<PowhegResHook> fPowhegResHook;
139  std::shared_ptr<PowhegHooksBB4L> fPowhegHooksBB4L;
140 
141  // biased tau decayer
142  std::shared_ptr<BiasedTauDecayer> fBiasedTauDecayer;
143 
144  //resonance decay filter hook
145  std::shared_ptr<ResonanceDecayFilterHook> fResonanceDecayFilterHook;
146 
147  //PT filter hook
148  std::shared_ptr<PTFilterHook> fPTFilterHook;
149 
150  //Generic customized hooks vector
151  std::shared_ptr<UserHooksVector> fCustomHooksVector;
152 
163 
164  static const std::vector<std::string> p8SharedResources;
165 
166  vector<float> DJR;
167  int nME;
169 
170  int nISRveto;
171  int nFSRveto;
172 };
173 
175 
178  comEnergy(params.getParameter<double>("comEnergy")),
179  LHEInputFileName(params.getUntrackedParameter<std::string>("LHEInputFileName", "")),
180  fInitialState(PP),
181  UserHooksSet(false),
182  nME(-1),
183  nMEFiltered(-1),
184  nISRveto(0),
185  nFSRveto(0) {
186  // J.Y.: the following 3 parameters are hacked "for a reason"
187  //
188  if (params.exists("PPbarInitialState")) {
189  if (fInitialState == PP) {
191  edm::LogImportant("GeneratorInterface|Pythia8Interface")
192  << "Pythia8 will be initialized for PROTON-ANTIPROTON INITIAL STATE. "
193  << "This is a user-request change from the DEFAULT PROTON-PROTON initial state.";
194  } else {
195  // probably need to throw on attempt to override ?
196  }
197  } else if (params.exists("ElectronPositronInitialState")) {
198  if (fInitialState == PP) {
200  edm::LogInfo("GeneratorInterface|Pythia8Interface")
201  << "Pythia8 will be initialized for ELECTRON-POSITRON INITIAL STATE. "
202  << "This is a user-request change from the DEFAULT PROTON-PROTON initial state.";
203  } else {
204  // probably need to throw on attempt to override ?
205  }
206  } else if (params.exists("ElectronProtonInitialState") || params.exists("PositronProtonInitialState")) {
207  // throw on unknown initial state !
208  throw edm::Exception(edm::errors::Configuration, "Pythia8Interface")
209  << " UNKNOWN INITIAL STATE. \n The allowed initial states are: PP, PPbar, ElectronPositron \n";
210  }
211 
212  // avoid filling weights twice (from v8.30x)
213  toHepMC.set_store_weights(false);
214 
215  // Reweight user hook
216  //
217  if (params.exists("reweightGen")) {
218  edm::LogInfo("Pythia8Interface") << "Start setup for reweightGen";
219  edm::ParameterSet rgParams = params.getParameter<edm::ParameterSet>("reweightGen");
220  fReweightUserHook.reset(
221  new PtHatReweightUserHook(rgParams.getParameter<double>("pTRef"), rgParams.getParameter<double>("power")));
222  edm::LogInfo("Pythia8Interface") << "End setup for reweightGen";
223  }
224  if (params.exists("reweightGenEmp")) {
225  edm::LogInfo("Pythia8Interface") << "Start setup for reweightGenEmp";
226  edm::ParameterSet rgeParams = params.getParameter<edm::ParameterSet>("reweightGenEmp");
227 
228  std::string tuneName = "";
229  if (rgeParams.exists("tune"))
230  tuneName = rgeParams.getParameter<std::string>("tune");
231  fReweightEmpUserHook.reset(new PtHatEmpReweightUserHook(tuneName));
232  edm::LogInfo("Pythia8Interface") << "End setup for reweightGenEmp";
233  }
234  if (params.exists("reweightGenRap")) {
235  edm::LogInfo("Pythia8Interface") << "Start setup for reweightGenRap";
236  edm::ParameterSet rgrParams = params.getParameter<edm::ParameterSet>("reweightGenRap");
237  fReweightRapUserHook.reset(new RapReweightUserHook(rgrParams.getParameter<std::string>("yLabSigmaFunc"),
238  rgrParams.getParameter<double>("yLabPower"),
239  rgrParams.getParameter<std::string>("yCMSigmaFunc"),
240  rgrParams.getParameter<double>("yCMPower"),
241  rgrParams.getParameter<double>("pTHatMin"),
242  rgrParams.getParameter<double>("pTHatMax")));
243  edm::LogInfo("Pythia8Interface") << "End setup for reweightGenRap";
244  }
245  if (params.exists("reweightGenPtHatRap")) {
246  edm::LogInfo("Pythia8Interface") << "Start setup for reweightGenPtHatRap";
247  edm::ParameterSet rgrParams = params.getParameter<edm::ParameterSet>("reweightGenPtHatRap");
248  fReweightPtHatRapUserHook.reset(new PtHatRapReweightUserHook(rgrParams.getParameter<std::string>("yLabSigmaFunc"),
249  rgrParams.getParameter<double>("yLabPower"),
250  rgrParams.getParameter<std::string>("yCMSigmaFunc"),
251  rgrParams.getParameter<double>("yCMPower"),
252  rgrParams.getParameter<double>("pTHatMin"),
253  rgrParams.getParameter<double>("pTHatMax")));
254  edm::LogInfo("Pythia8Interface") << "End setup for reweightGenPtHatRap";
255  }
256 
257  if (params.exists("useUserHook"))
258  throw edm::Exception(edm::errors::Configuration, "Pythia8Interface")
259  << " Obsolete parameter: useUserHook \n Please use the actual one instead \n";
260 
261  // PS matching prototype
262  //
263  if (params.exists("jetMatching")) {
264  edm::ParameterSet jmParams = params.getUntrackedParameter<edm::ParameterSet>("jetMatching");
265  std::string scheme = jmParams.getParameter<std::string>("scheme");
266  if (scheme == "Madgraph" || scheme == "MadgraphFastJet") {
267  fJetMatchingHook.reset(new JetMatchingHook(jmParams, &fMasterGen->info));
268  }
269  }
270 
271  // Pythia8Interface emission veto
272  //
273  if (params.exists("emissionVeto1")) {
274  EV1_nFinal = -1;
275  if (params.exists("EV1_nFinal"))
276  EV1_nFinal = params.getParameter<int>("EV1_nFinal");
277  EV1_vetoOn = true;
278  if (params.exists("EV1_vetoOn"))
279  EV1_vetoOn = params.getParameter<bool>("EV1_vetoOn");
280  EV1_maxVetoCount = 10;
281  if (params.exists("EV1_maxVetoCount"))
282  EV1_maxVetoCount = params.getParameter<int>("EV1_maxVetoCount");
283  EV1_pThardMode = 1;
284  if (params.exists("EV1_pThardMode"))
285  EV1_pThardMode = params.getParameter<int>("EV1_pThardMode");
286  EV1_pTempMode = 0;
287  if (params.exists("EV1_pTempMode"))
288  EV1_pTempMode = params.getParameter<int>("EV1_pTempMode");
289  if (EV1_pTempMode > 2 || EV1_pTempMode < 0)
290  throw edm::Exception(edm::errors::Configuration, "Pythia8Interface") << " Wrong value for EV1_pTempMode code\n";
291  EV1_emittedMode = 0;
292  if (params.exists("EV1_emittedMode"))
293  EV1_emittedMode = params.getParameter<int>("EV1_emittedMode");
294  EV1_pTdefMode = 1;
295  if (params.exists("EV1_pTdefMode"))
296  EV1_pTdefMode = params.getParameter<int>("EV1_pTdefMode");
297  EV1_MPIvetoOn = false;
298  if (params.exists("EV1_MPIvetoOn"))
299  EV1_MPIvetoOn = params.getParameter<bool>("EV1_MPIvetoOn");
300  EV1_QEDvetoMode = 0;
301  if (params.exists("EV1_QEDvetoMode"))
302  EV1_QEDvetoMode = params.getParameter<int>("EV1_QEDvetoMode");
303  EV1_nFinalMode = 0;
304  if (params.exists("EV1_nFinalMode"))
305  EV1_nFinalMode = params.getParameter<int>("EV1_nFinalMode");
307  EV1_vetoOn,
316  0));
317  }
318 
319  if (params.exists("UserCustomization")) {
320  fCustomHooksVector = std::make_shared<UserHooksVector>();
321  const std::vector<edm::ParameterSet> userParams =
322  params.getParameter<std::vector<edm::ParameterSet>>("UserCustomization");
323  for (const auto &pluginParams : userParams) {
324  (fCustomHooksVector->hooks)
325  .push_back(
326  CustomHookFactory::get()->create(pluginParams.getParameter<std::string>("pluginName"), pluginParams));
327  }
328  }
329 
330  if (params.exists("VinciaPlugin")) {
331  throw edm::Exception(edm::errors::Configuration, "Pythia8Interface")
332  << " Obsolete parameter: VinciaPlugin \n Please use the parameter PartonShowers:model instead \n";
333  }
334  if (params.exists("DirePlugin")) {
335  throw edm::Exception(edm::errors::Configuration, "Pythia8Interface")
336  << " Obsolete parameter: DirePlugin \n Please use the parameter PartonShowers:model instead \n";
337  }
338 }
339 
341 
343  bool status = false, status1 = false;
344 
345  if (lheFile_.empty()) {
346  if (fInitialState == PP) // default
347  {
348  fMasterGen->settings.mode("Beams:idA", 2212);
349  fMasterGen->settings.mode("Beams:idB", 2212);
350  } else if (fInitialState == PPbar) {
351  fMasterGen->settings.mode("Beams:idA", 2212);
352  fMasterGen->settings.mode("Beams:idB", -2212);
353  } else if (fInitialState == ElectronPositron) {
354  fMasterGen->settings.mode("Beams:idA", 11);
355  fMasterGen->settings.mode("Beams:idB", -11);
356  } else {
357  // throw on unknown initial state !
358  throw edm::Exception(edm::errors::Configuration, "Pythia8Interface")
359  << " UNKNOWN INITIAL STATE. \n The allowed initial states are: PP, PPbar, ElectronPositron \n";
360  }
361  fMasterGen->settings.parm("Beams:eCM", comEnergy);
362  } else {
363  fMasterGen->settings.mode("Beams:frameType", 4);
364  fMasterGen->settings.word("Beams:LHEF", lheFile_);
365  }
366 
367  if (!fUserHooksVector.get())
368  fUserHooksVector.reset(new UserHooksVector);
369  (fUserHooksVector->hooks).clear();
370 
371  if (fReweightUserHook.get())
372  (fUserHooksVector->hooks).push_back(fReweightUserHook);
373  if (fReweightEmpUserHook.get())
374  (fUserHooksVector->hooks).push_back(fReweightEmpUserHook);
375  if (fReweightRapUserHook.get())
376  (fUserHooksVector->hooks).push_back(fReweightRapUserHook);
377  if (fReweightPtHatRapUserHook.get())
378  (fUserHooksVector->hooks).push_back(fReweightPtHatRapUserHook);
379  if (fJetMatchingHook.get())
380  (fUserHooksVector->hooks).push_back(fJetMatchingHook);
381  if (fEmissionVetoHook1.get()) {
382  edm::LogInfo("Pythia8Interface") << "Turning on Emission Veto Hook 1 from CMSSW Pythia8Interface";
384  }
385 
386  if (fMasterGen->settings.mode("POWHEG:veto") > 0 || fMasterGen->settings.mode("POWHEG:MPIveto") > 0) {
387  if (fJetMatchingHook.get() || fEmissionVetoHook1.get())
388  throw edm::Exception(edm::errors::Configuration, "Pythia8Interface")
389  << " Attempt to turn on PowhegHooks by pythia8 settings but there are incompatible hooks on \n Incompatible "
390  "are : jetMatching, emissionVeto1 \n";
391 
392  if (!fEmissionVetoHook.get())
393  fEmissionVetoHook.reset(new PowhegHooks());
394 
395  edm::LogInfo("Pythia8Interface") << "Turning on Emission Veto Hook from pythia8 code";
397  }
398 
399  bool PowhegRes = fMasterGen->settings.flag("POWHEGres:calcScales");
400  if (PowhegRes) {
401  edm::LogInfo("Pythia8Interface") << "Turning on resonance scale setting from CMSSW Pythia8Interface";
402  if (!fPowhegResHook.get())
403  fPowhegResHook.reset(new PowhegResHook());
405  }
406 
407  bool PowhegBB4L = fMasterGen->settings.flag("POWHEG:bb4l");
408  if (PowhegBB4L) {
409  edm::LogInfo("Pythia8Interface") << "Turning on BB4l hook from CMSSW Pythia8Interface";
410  if (!fPowhegHooksBB4L.get())
411  fPowhegHooksBB4L.reset(new PowhegHooksBB4L());
413  }
414 
415  //adapted from main89.cc in pythia8 examples
416  bool internalMatching = fMasterGen->settings.flag("JetMatching:merge");
417  bool internalMerging = !(fMasterGen->settings.word("Merging:Process") == "void");
418 
419  if (internalMatching && internalMerging) {
420  throw edm::Exception(edm::errors::Configuration, "Pythia8Interface")
421  << " Only one jet matching/merging scheme can be used at a time. \n";
422  }
423 
424  if (internalMatching) {
425  if (!fJetMatchingPy8InternalHook.get())
426  fJetMatchingPy8InternalHook.reset(new Pythia8::JetMatchingMadgraph);
428  }
429 
430  if (internalMerging) {
431  int scheme = (fMasterGen->settings.flag("Merging:doUMEPSTree") || fMasterGen->settings.flag("Merging:doUMEPSSubt"))
432  ? 1
433  : ((fMasterGen->settings.flag("Merging:doUNLOPSTree") ||
434  fMasterGen->settings.flag("Merging:doUNLOPSSubt") ||
435  fMasterGen->settings.flag("Merging:doUNLOPSLoop") ||
436  fMasterGen->settings.flag("Merging:doUNLOPSSubtNLO"))
437  ? 2
438  : 0);
439  if (!fMergingHook.get())
440  fMergingHook.reset(new Pythia8::amcnlo_unitarised_interface(scheme));
442  }
443 
444  bool biasedTauDecayer = fMasterGen->settings.flag("BiasedTauDecayer:filter");
445  if (biasedTauDecayer) {
446  if (!fBiasedTauDecayer.get()) {
447  Pythia8::Info localInfo = fMasterGen->info;
448  fBiasedTauDecayer.reset(new BiasedTauDecayer(&localInfo, &(fMasterGen->settings)));
449  }
450  std::vector<int> handledParticles;
451  handledParticles.push_back(15);
452  fMasterGen->setDecayPtr(fBiasedTauDecayer, handledParticles);
453  }
454 
455  bool resonanceDecayFilter = fMasterGen->settings.flag("ResonanceDecayFilter:filter");
456  if (resonanceDecayFilter) {
459  }
460 
461  bool PTFilter = fMasterGen->settings.flag("PTFilter:filter");
462  if (PTFilter) {
463  fPTFilterHook.reset(new PTFilterHook);
465  }
466 
467  if (!(fUserHooksVector->hooks).empty() && !UserHooksSet) {
468  for (auto &fUserHook : fUserHooksVector->hooks) {
469  fMasterGen->addUserHooksPtr(fUserHook);
470  }
471  UserHooksSet = true;
472  }
473 
474  if (fCustomHooksVector.get()) {
475  edm::LogInfo("Pythia8Interface") << "Adding customized user hooks";
476  for (const auto &fUserHook : fCustomHooksVector->hooks) {
477  fMasterGen->addUserHooksPtr(fUserHook);
478  }
479  }
480 
481  edm::LogInfo("Pythia8Interface") << "Initializing MasterGen";
482  status = fMasterGen->init();
483 
484  //clean up temp file
485  if (!slhafile_.empty()) {
486  std::remove(slhafile_.c_str());
487  }
488 
489  if (pythiaPylistVerbosity > 10) {
490  if (pythiaPylistVerbosity == 11 || pythiaPylistVerbosity == 13)
491  fMasterGen->settings.listAll();
492  if (pythiaPylistVerbosity == 12 || pythiaPylistVerbosity == 13)
493  fMasterGen->particleData.listAll();
494  }
495 
496  // init decayer
497  fDecayer->settings.flag("ProcessLevel:all", false); // trick
498  fDecayer->settings.flag("ProcessLevel:resonanceDecays", true);
499  edm::LogInfo("Pythia8Interface") << "Initializing Decayer";
500  status1 = fDecayer->init();
501 
502  if (useEvtGen) {
503  edm::LogInfo("Pythia8Hadronizer") << "Creating and initializing pythia8 EvtGen plugin";
504  if (!evtgenDecays.get()) {
505  evtgenDecays.reset(new EvtGenDecays(fMasterGen.get(), evtgenDecFile, evtgenPdlFile));
506  for (unsigned int i = 0; i < evtgenUserFiles.size(); i++)
507  evtgenDecays->readDecayFile(evtgenUserFiles.at(i));
508  }
509  }
510 
511  return (status && status1);
512 }
513 
515  edm::LogInfo("Pythia8Interface") << "Initializing for external partons";
516 
517  bool status = false, status1 = false;
518 
519  if (!fUserHooksVector.get())
520  fUserHooksVector.reset(new UserHooksVector);
521  (fUserHooksVector->hooks).clear();
522 
523  if (fReweightUserHook.get())
524  (fUserHooksVector->hooks).push_back(fReweightUserHook);
525  if (fReweightEmpUserHook.get())
526  (fUserHooksVector->hooks).push_back(fReweightEmpUserHook);
527  if (fReweightRapUserHook.get())
528  (fUserHooksVector->hooks).push_back(fReweightRapUserHook);
529  if (fReweightPtHatRapUserHook.get())
530  (fUserHooksVector->hooks).push_back(fReweightPtHatRapUserHook);
531  if (fJetMatchingHook.get())
532  (fUserHooksVector->hooks).push_back(fJetMatchingHook);
533  if (fEmissionVetoHook1.get()) {
534  edm::LogInfo("Pythia8Interface") << "Turning on Emission Veto Hook 1 from CMSSW Pythia8Interface";
536  }
537 
538  if (fCustomHooksVector.get()) {
539  edm::LogInfo("Pythia8Interface") << "Adding customized user hook";
540  for (const auto &fUserHook : fCustomHooksVector->hooks) {
541  (fUserHooksVector->hooks).push_back(fUserHook);
542  }
543  }
544 
545  if (fMasterGen->settings.mode("POWHEG:veto") > 0 || fMasterGen->settings.mode("POWHEG:MPIveto") > 0) {
546  if (fJetMatchingHook.get() || fEmissionVetoHook1.get())
547  throw edm::Exception(edm::errors::Configuration, "Pythia8Interface")
548  << " Attempt to turn on PowhegHooks by pythia8 settings but there are incompatible hooks on \n Incompatible "
549  "are : jetMatching, emissionVeto1 \n";
550 
551  if (!fEmissionVetoHook.get())
552  fEmissionVetoHook.reset(new PowhegHooks());
553 
554  edm::LogInfo("Pythia8Interface") << "Turning on Emission Veto Hook from pythia8 code";
556  }
557 
558  bool PowhegRes = fMasterGen->settings.flag("POWHEGres:calcScales");
559  if (PowhegRes) {
560  edm::LogInfo("Pythia8Interface") << "Turning on resonance scale setting from CMSSW Pythia8Interface";
561  if (!fPowhegResHook.get())
562  fPowhegResHook.reset(new PowhegResHook());
564  }
565 
566  bool PowhegBB4L = fMasterGen->settings.flag("POWHEG:bb4l");
567  if (PowhegBB4L) {
568  edm::LogInfo("Pythia8Interface") << "Turning on BB4l hook from CMSSW Pythia8Interface";
569  if (!fPowhegHooksBB4L.get())
570  fPowhegHooksBB4L.reset(new PowhegHooksBB4L());
572  }
573 
574  //adapted from main89.cc in pythia8 examples
575  bool internalMatching = fMasterGen->settings.flag("JetMatching:merge");
576  bool internalMerging = !(fMasterGen->settings.word("Merging:Process") == "void");
577 
578  if (internalMatching && internalMerging) {
579  throw edm::Exception(edm::errors::Configuration, "Pythia8Interface")
580  << " Only one jet matching/merging scheme can be used at a time. \n";
581  }
582 
583  if (internalMatching) {
584  if (!fJetMatchingPy8InternalHook.get())
585  fJetMatchingPy8InternalHook.reset(new Pythia8::JetMatchingMadgraph);
587  }
588 
589  if (internalMerging) {
590  int scheme = (fMasterGen->settings.flag("Merging:doUMEPSTree") || fMasterGen->settings.flag("Merging:doUMEPSSubt"))
591  ? 1
592  : ((fMasterGen->settings.flag("Merging:doUNLOPSTree") ||
593  fMasterGen->settings.flag("Merging:doUNLOPSSubt") ||
594  fMasterGen->settings.flag("Merging:doUNLOPSLoop") ||
595  fMasterGen->settings.flag("Merging:doUNLOPSSubtNLO"))
596  ? 2
597  : 0);
598  if (!fMergingHook.get())
599  fMergingHook.reset(new Pythia8::amcnlo_unitarised_interface(scheme));
601  }
602 
603  bool biasedTauDecayer = fMasterGen->settings.flag("BiasedTauDecayer:filter");
604  if (biasedTauDecayer) {
605  if (!fBiasedTauDecayer.get()) {
606  Pythia8::Info localInfo = fMasterGen->info;
607  fBiasedTauDecayer.reset(new BiasedTauDecayer(&localInfo, &(fMasterGen->settings)));
608  }
609  std::vector<int> handledParticles;
610  handledParticles.push_back(15);
611  fMasterGen->setDecayPtr(fBiasedTauDecayer, handledParticles);
612  }
613 
614  bool resonanceDecayFilter = fMasterGen->settings.flag("ResonanceDecayFilter:filter");
615  if (resonanceDecayFilter) {
618  }
619 
620  bool PTFilter = fMasterGen->settings.flag("PTFilter:filter");
621  if (PTFilter) {
622  fPTFilterHook.reset(new PTFilterHook);
624  }
625 
626  if (!(fUserHooksVector->hooks).empty() && !UserHooksSet) {
627  for (auto &fUserHook : fUserHooksVector->hooks) {
628  fMasterGen->addUserHooksPtr(fUserHook);
629  }
630  UserHooksSet = true;
631  }
632 
633  if (!LHEInputFileName.empty()) {
634  edm::LogInfo("Pythia8Interface") << "Initialize direct pythia8 reading from LHE file " << LHEInputFileName;
635  edm::LogInfo("Pythia8Interface") << "Some LHE information can be not stored";
636  fMasterGen->settings.mode("Beams:frameType", 4);
637  fMasterGen->settings.word("Beams:LHEF", LHEInputFileName);
638  status = fMasterGen->init();
639 
640  } else {
641  lhaUP.reset(new LHAupLesHouches());
642  lhaUP->setScalesFromLHEF(fMasterGen->settings.flag("Beams:setProductionScalesFromLHEF"));
643  lhaUP->loadRunInfo(lheRunInfo());
644 
645  if (fJetMatchingHook.get()) {
646  fJetMatchingHook->init(lheRunInfo());
647  }
648 
649  fMasterGen->settings.mode("Beams:frameType", 5);
650  fMasterGen->setLHAupPtr(lhaUP);
651  edm::LogInfo("Pythia8Interface") << "Initializing MasterGen";
652  status = fMasterGen->init();
653  }
654 
655  //clean up temp file
656  if (!slhafile_.empty()) {
657  std::remove(slhafile_.c_str());
658  }
659 
660  if (pythiaPylistVerbosity > 10) {
661  if (pythiaPylistVerbosity == 11 || pythiaPylistVerbosity == 13)
662  fMasterGen->settings.listAll();
663  if (pythiaPylistVerbosity == 12 || pythiaPylistVerbosity == 13)
664  fMasterGen->particleData.listAll();
665  }
666 
667  // init decayer
668  fDecayer->settings.flag("ProcessLevel:all", false); // trick
669  fDecayer->settings.flag("ProcessLevel:resonanceDecays", true);
670  edm::LogInfo("Pythia8Interface") << "Initializing Decayer";
671  status1 = fDecayer->init();
672 
673  if (useEvtGen) {
674  edm::LogInfo("Pythia8Hadronizer") << "Creating and initializing pythia8 EvtGen plugin";
675  if (!evtgenDecays.get()) {
676  evtgenDecays.reset(new EvtGenDecays(fMasterGen.get(), evtgenDecFile, evtgenPdlFile));
677  for (unsigned int i = 0; i < evtgenUserFiles.size(); i++)
678  evtgenDecays->readDecayFile(evtgenUserFiles.at(i));
679  }
680  }
681 
682  return (status && status1);
683 }
684 
686  fMasterGen->stat();
687 
688  if (fEmissionVetoHook.get()) {
689  edm::LogPrint("Pythia8Interface") << "\n"
690  << "Number of ISR vetoed = " << nISRveto;
691  edm::LogPrint("Pythia8Interface") << "Number of FSR vetoed = " << nFSRveto;
692  }
693 
694  double xsec = fMasterGen->info.sigmaGen(); // cross section in mb
695  xsec *= 1.0e9; // translate to pb (CMS/Gen "convention" as of May 2009)
696  double err = fMasterGen->info.sigmaErr(); // cross section err in mb
697  err *= 1.0e9; // translate to pb (CMS/Gen "convention" as of May 2009)
699 }
700 
702  DJR.resize(0);
703  nME = -1;
704  nMEFiltered = -1;
705 
706  if (fJetMatchingHook.get()) {
707  fJetMatchingHook->resetMatchingStatus();
708  fJetMatchingHook->beforeHadronization(lheEvent());
709  }
710 
711  if (!fMasterGen->next())
712  return false;
713 
714  double mergeweight = fMasterGen.get()->info.mergingWeightNLO();
715  if (fMergingHook.get()) {
716  mergeweight *= fMergingHook->getNormFactor();
717  }
718 
719  //protect against 0-weight from ckkw or similar
720  if (std::abs(mergeweight) == 0.) {
721  event().reset();
722  return false;
723  }
724 
725  if (fJetMatchingPy8InternalHook.get()) {
726  const std::vector<double> djrmatch = fJetMatchingPy8InternalHook->getDJR();
727  //cap size of djr vector to save storage space (keep only up to first 6 elements)
728  unsigned int ndjr = std::min(djrmatch.size(), std::vector<double>::size_type(6));
729  for (unsigned int idjr = 0; idjr < ndjr; ++idjr) {
730  DJR.push_back(djrmatch[idjr]);
731  }
732 
733  nME = fJetMatchingPy8InternalHook->nMEpartons().first;
734  nMEFiltered = fJetMatchingPy8InternalHook->nMEpartons().second;
735  }
736 
737  if (evtgenDecays.get())
738  evtgenDecays->decay();
739 
740  event() = std::make_unique<HepMC::GenEvent>();
741  bool py8hepmc = toHepMC.fill_next_event(*(fMasterGen.get()), event().get());
742 
743  if (!py8hepmc) {
744  return false;
745  }
746 
747  // 0th weight is not filled anymore since v8.30x, pushback manually
748  event()->weights().push_back(fMasterGen->info.weight());
749 
750  //add ckkw/umeps/unlops merging weight
751  if (mergeweight != 1.) {
752  event()->weights()[0] *= mergeweight;
753  }
754 
755  if (fEmissionVetoHook.get()) {
756  nISRveto += fEmissionVetoHook->getNISRveto();
757  nFSRveto += fEmissionVetoHook->getNFSRveto();
758  }
759 
760  //fill additional weights for systematic uncertainties
761  if (fMasterGen->info.getWeightsDetailedSize() > 0) {
762  for (const string &key : fMasterGen->info.initrwgt->weightsKeys) {
763  double wgt = (*fMasterGen->info.weights_detailed)[key];
764  event()->weights().push_back(wgt);
765  }
766  } else if (fMasterGen->info.getWeightsCompressedSize() > 0) {
767  for (unsigned int i = 0; i < fMasterGen->info.getWeightsCompressedSize(); i++) {
768  double wgt = fMasterGen->info.getWeightsCompressedValue(i);
769  event()->weights().push_back(wgt);
770  }
771  }
772 
773  // fill shower weights
774  // http://home.thep.lu.se/~torbjorn/pythia82html/Variations.html
775  if (fMasterGen->info.nWeights() > 1) {
776  for (int i = 0; i < fMasterGen->info.nWeights(); ++i) {
777  double wgt = fMasterGen->info.weight(i);
778  event()->weights().push_back(wgt);
779  }
780  }
781 
782 #if 0
783  // VINCIA shower weights
784  // http://vincia.hepforge.org/current/share/Vincia/htmldoc/VinciaUncertainties.html
785  if (fvincia.get()) {
786  event()->weights()[0] *= fvincia->weight(0);
787  for (int iVar = 1; iVar < fvincia->nWeights(); iVar++) {
788  event()->weights().push_back(fvincia->weight(iVar));
789  }
790  }
791 
792  // Retrieve Dire shower weights
793  if (fDire.get()) {
794  fDire->weightsPtr->calcWeight(0.);
795  fDire->weightsPtr->reset();
796 
797  //Make sure the base weight comes first
798  event()->weights()[0] *= fDire->weightsPtr->getShowerWeight("base");
799 
800  unordered_map<string, double>::iterator it;
801  for (it = fDire->weightsPtr->getShowerWeights()->begin(); it != fDire->weightsPtr->getShowerWeights()->end();
802  it++) {
803  if (it->first == "base")
804  continue;
805  event()->weights().push_back(it->second);
806  }
807  }
808 #endif
809 
810  return true;
811 }
812 
814  DJR.resize(0);
815  nME = -1;
816  nMEFiltered = -1;
817  if (LHEInputFileName.empty())
818  lhaUP->loadEvent(lheEvent());
819 
820  if (fJetMatchingHook.get()) {
821  fJetMatchingHook->resetMatchingStatus();
822  fJetMatchingHook->beforeHadronization(lheEvent());
823  }
824 
825  bool py8next = fMasterGen->next();
826 
827  double mergeweight = fMasterGen.get()->info.mergingWeightNLO();
828  if (fMergingHook.get()) {
829  mergeweight *= fMergingHook->getNormFactor();
830  }
831 
832  //protect against 0-weight from ckkw or similar
833  if (!py8next || std::abs(mergeweight) == 0.) {
834  lheEvent()->count(lhef::LHERunInfo::kSelected, 1.0, mergeweight);
835  event().reset();
836  return false;
837  }
838 
839  if (fJetMatchingPy8InternalHook.get()) {
840  const std::vector<double> djrmatch = fJetMatchingPy8InternalHook->getDJR();
841  //cap size of djr vector to save storage space (keep only up to first 6 elements)
842  unsigned int ndjr = std::min(djrmatch.size(), std::vector<double>::size_type(6));
843  for (unsigned int idjr = 0; idjr < ndjr; ++idjr) {
844  DJR.push_back(djrmatch[idjr]);
845  }
846 
847  nME = fJetMatchingPy8InternalHook->nMEpartons().first;
848  nMEFiltered = fJetMatchingPy8InternalHook->nMEpartons().second;
849  }
850 
851  // update LHE matching statistics
852  //
853  lheEvent()->count(lhef::LHERunInfo::kAccepted, 1.0, mergeweight);
854 
855  if (evtgenDecays.get())
856  evtgenDecays->decay();
857 
858  event() = std::make_unique<HepMC::GenEvent>();
859 
860  bool py8hepmc = toHepMC.fill_next_event(*(fMasterGen.get()), event().get());
861 
862  if (!py8hepmc) {
863  return false;
864  }
865 
866  // 0th weight is not filled anymore since v8.30x, pushback manually
867  event()->weights().push_back(fMasterGen->info.weight());
868 
869  //add ckkw/umeps/unlops merging weight
870  if (mergeweight != 1.) {
871  event()->weights()[0] *= mergeweight;
872  }
873 
874  if (fEmissionVetoHook.get()) {
875  nISRveto += fEmissionVetoHook->getNISRveto();
876  nFSRveto += fEmissionVetoHook->getNFSRveto();
877  }
878 
879  // fill shower weights
880  // http://home.thep.lu.se/~torbjorn/pythia82html/Variations.html
881  if (fMasterGen->info.nWeights() > 1) {
882  for (int i = 0; i < fMasterGen->info.nWeights(); ++i) {
883  double wgt = fMasterGen->info.weight(i);
884  event()->weights().push_back(wgt);
885  }
886  }
887 
888  return true;
889 }
890 
892  Event *pythiaEvent = &(fMasterGen->event);
893 
894  int NPartsBeforeDecays = pythiaEvent->size();
895  int NPartsAfterDecays = event().get()->particles_size();
896 
897  if (NPartsAfterDecays == NPartsBeforeDecays)
898  return true;
899 
900  bool result = true;
901 
902  for (int ipart = NPartsAfterDecays; ipart > NPartsBeforeDecays; ipart--) {
903  HepMC::GenParticle *part = event().get()->barcode_to_particle(ipart);
904 
905  if (part->status() == 1 && (fDecayer->particleData).canDecay(part->pdg_id())) {
906  fDecayer->event.reset();
907  Particle py8part(part->pdg_id(),
908  93,
909  0,
910  0,
911  0,
912  0,
913  0,
914  0,
915  part->momentum().x(),
916  part->momentum().y(),
917  part->momentum().z(),
918  part->momentum().t(),
919  part->generated_mass());
920  HepMC::GenVertex *ProdVtx = part->production_vertex();
921  py8part.vProd(ProdVtx->position().x(), ProdVtx->position().y(), ProdVtx->position().z(), ProdVtx->position().t());
922  py8part.tau((fDecayer->particleData).tau0(part->pdg_id()));
923  fDecayer->event.append(py8part);
924  int nentries = fDecayer->event.size();
925  if (!fDecayer->event[nentries - 1].mayDecay())
926  continue;
927  fDecayer->next();
928  int nentries1 = fDecayer->event.size();
929  if (nentries1 <= nentries)
930  continue; //same number of particles, no decays...
931 
932  part->set_status(2);
933 
934  result = toHepMC.fill_next_event(*(fDecayer.get()), event().get(), -1, true, part);
935  }
936  }
937 
938  return result;
939 }
940 
942  bool lhe = lheEvent() != nullptr;
943 
944  // protection against empty weight container
945  if ((event()->weights()).empty())
946  (event()->weights()).push_back(1.);
947 
948  // now create the GenEventInfo product from the GenEvent and fill
949  // the missing pieces
950  eventInfo() = std::make_unique<GenEventInfoProduct>(event().get());
951 
952  // in pythia pthat is used to subdivide samples into different bins
953  // in LHE mode the binning is done by the external ME generator
954  // which is likely not pthat, so only filling it for Py6 internal mode
955  if (!lhe) {
956  eventInfo()->setBinningValues(std::vector<double>(1, fMasterGen->info.pTHat()));
957  }
958 
959  eventInfo()->setDJR(DJR);
960  eventInfo()->setNMEPartons(nME);
961  eventInfo()->setNMEPartonsFiltered(nMEFiltered);
962 
963  //******** Verbosity ********
964 
967  if (pythiaPylistVerbosity) {
968  fMasterGen->info.list();
969  fMasterGen->event.list();
970  }
971 
972  if (pythiaHepMCVerbosity) {
973  std::cout << "Event process = " << fMasterGen->info.code() << "\n"
974  << "----------------------" << std::endl;
975  event()->print();
976  }
978  std::cout << "Event process = " << fMasterGen->info.code() << "\n"
979  << "----------------------" << std::endl;
980  ascii_io->write_event(event().get());
981  }
982  }
983 }
984 
985 std::unique_ptr<GenLumiInfoHeader> Pythia8Hadronizer::getGenLumiInfoHeader() const {
986  auto genLumiInfoHeader = BaseHadronizer::getGenLumiInfoHeader();
987 
988  //fill lhe headers
989  //*FIXME* initrwgt header is corrupt due to pythia bug
990  for (const std::string &key : fMasterGen->info.headerKeys()) {
991  genLumiInfoHeader->lheHeaders().emplace_back(key, fMasterGen->info.header(key));
992  }
993 
994  //check, if it is not only nominal weight
995  int weights_number = fMasterGen->info.nWeights();
996  if (fMasterGen->info.initrwgt)
997  weights_number += fMasterGen->info.initrwgt->weightsKeys.size();
998  if (weights_number > 1) {
999  genLumiInfoHeader->weightNames().reserve(weights_number + 1);
1000  genLumiInfoHeader->weightNames().push_back("nominal");
1001  }
1002 
1003  //fill weight names
1004  if (fMasterGen->info.initrwgt) {
1005  for (const std::string &key : fMasterGen->info.initrwgt->weightsKeys) {
1006  std::string weightgroupname;
1007  for (const auto &wgtgrp : fMasterGen->info.initrwgt->weightgroups) {
1008  const auto &wgtgrpwgt = wgtgrp.second.weights.find(key);
1009  if (wgtgrpwgt != wgtgrp.second.weights.end()) {
1010  weightgroupname = wgtgrp.first;
1011  }
1012  }
1013 
1014  std::ostringstream weightname;
1015  weightname << "LHE, id = " << key << ", ";
1016  if (!weightgroupname.empty()) {
1017  weightname << "group = " << weightgroupname << ", ";
1018  }
1019  weightname << fMasterGen->info.initrwgt->weights[key].contents;
1020  genLumiInfoHeader->weightNames().push_back(weightname.str());
1021  }
1022  }
1023 
1024  //fill shower labels
1025  // http://home.thep.lu.se/~torbjorn/pythia82html/Variations.html
1026  // http://home.thep.lu.se/~torbjorn/doxygen/classPythia8_1_1Info.html
1027  if (fMasterGen->info.nWeights() > 1) {
1028  for (int i = 0; i < fMasterGen->info.nWeights(); ++i) {
1029  genLumiInfoHeader->weightNames().push_back(fMasterGen->info.weightLabel(i));
1030  }
1031  }
1032 
1033 #if 0
1034  // VINCIA shower weights
1035  // http://vincia.hepforge.org/current/share/Vincia/htmldoc/VinciaUncertainties.html
1036  if (fvincia.get()) {
1037  for (int iVar = 0; iVar < fvincia->nWeights(); iVar++) {
1038  genLumiInfoHeader->weightNames().push_back(fvincia->weightLabel(iVar));
1039  }
1040  }
1041 
1042  if (fDire.get()) {
1043  //Make sure the base weight comes first
1044  genLumiInfoHeader->weightNames().push_back("base");
1045 
1046  unordered_map<string, double>::iterator it;
1047  for (it = fDire->weightsPtr->getShowerWeights()->begin(); it != fDire->weightsPtr->getShowerWeights()->end();
1048  it++) {
1049  if (it->first == "base")
1050  continue;
1051  genLumiInfoHeader->weightNames().push_back(it->first);
1052  }
1053  }
1054 #endif
1055 
1056  return genLumiInfoHeader;
1057 }
1058 
1061 
1064 
1068 
edm::ConcurrentGeneratorFilter< Pythia8Hadronizer, ConcurrentExternalDecayDriver > Pythia8ConcurrentGeneratorFilter
std::shared_ptr< PowhegHooksBB4L > fPowhegHooksBB4L
virtual bool residualDecay()
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
double comEnergy
Center-of-Mass energy.
std::shared_ptr< PowhegHooks > fEmissionVetoHook
std::shared_ptr< EmissionVetoHook1 > fEmissionVetoHook1
std::shared_ptr< BiasedTauDecayer > fBiasedTauDecayer
std::shared_ptr< Pythia8::EvtGenDecays > evtgenDecays
std::unique_ptr< Pythia8::Pythia > fDecayer
edm::GeneratorFilter< Pythia8Hadronizer, ExternalDecayDriver > Pythia8GeneratorFilter
bool initializeForInternalPartons() override
HepMC::IO_AsciiParticles * ascii_io
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void statistics() override
bool exists(std::string const &parameterName) const
checks if a parameter exists
std::string LHEInputFileName
void count(LHERunInfo::CountMode count, double weight=1.0, double matchWeight=1.0)
Definition: LHEEvent.cc:184
std::string lheFile_
std::shared_ptr< UserHooks > fReweightRapUserHook
void setInternalXSec(const XSec &xsec)
uint16_t size_type
std::shared_ptr< LHAupLesHouches > lhaUP
std::vector< std::string > evtgenUserFiles
edm::ConcurrentHadronizerFilter< Pythia8Hadronizer, ConcurrentExternalDecayDriver > Pythia8ConcurrentHadronizerFilter
GenRunInfoProduct & runInfo()
lhef::LHEEvent * lheEvent()
static const std::vector< std::string > p8SharedResources
std::shared_ptr< UserHooksVector > fCustomHooksVector
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
Log< level::Error, true > LogImportant
std::shared_ptr< ResonanceDecayFilterHook > fResonanceDecayFilterHook
std::shared_ptr< UserHooksVector > fUserHooksVector
unsigned int pythiaPylistVerbosity
const char * classname() const override
std::shared_ptr< PTFilterHook > fPTFilterHook
std::shared_ptr< Pythia8::JetMatchingMadgraph > fJetMatchingPy8InternalHook
std::unique_ptr< HepMC::GenEvent > & event()
Log< level::Warning, true > LogPrint
lhef::LHERunInfo * lheRunInfo()
std::shared_ptr< JetMatchingHook > fJetMatchingHook
bool generatePartonsAndHadronize() override
Log< level::Info, false > LogInfo
void doSetRandomEngine(CLHEP::HepRandomEngine *v) override
std::shared_ptr< UserHooks > fReweightUserHook
std::shared_ptr< Pythia8::amcnlo_unitarised_interface > fMergingHook
part
Definition: HCALResponse.h:20
std::unique_ptr< GenEventInfoProduct > & eventInfo()
unsigned int maxEventsToPrint
def remove(d, key, TELL=False)
Definition: MatrixUtil.py:223
deadvectors [0] push_back({0.0175431, 0.538005, 6.80997, 13.29})
std::shared_ptr< UserHooks > fReweightEmpUserHook
Pythia8Hadronizer(const edm::ParameterSet &params)
vector< float > DJR
std::shared_ptr< PowhegResHook > fPowhegResHook
void clear(HadCaloObj &c)
Definition: data.h:124
HepMC::Pythia8ToHepMC toHepMC
std::unique_ptr< GenLumiInfoHeader > getGenLumiInfoHeader() const override
std::shared_ptr< UserHooks > fReweightPtHatRapUserHook
std::unique_ptr< Pythia8::Pythia > fMasterGen
std::vector< std::string > const & doSharedResources() const override
#define get
static const std::string kPythia8
void finalizeEvent() override
edm::HadronizerFilter< Pythia8Hadronizer, ExternalDecayDriver > Pythia8HadronizerFilter
~Pythia8Hadronizer() override