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