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  //adapted from main89.cc in pythia8 examples
595  bool internalMatching = fMasterGen->settings.flag("JetMatching:merge");
596  bool internalMerging = !(fMasterGen->settings.word("Merging:Process") == "void");
597 
598  if (internalMatching && internalMerging) {
599  throw edm::Exception(edm::errors::Configuration, "Pythia8Interface")
600  << " Only one jet matching/merging scheme can be used at a time. \n";
601  }
602 
603  if (internalMatching) {
604  if (!fJetMatchingPy8InternalHook.get())
605  fJetMatchingPy8InternalHook.reset(new Pythia8::JetMatchingMadgraph);
607  }
608 
609  if (internalMerging) {
610  int scheme = (fMasterGen->settings.flag("Merging:doUMEPSTree") || fMasterGen->settings.flag("Merging:doUMEPSSubt"))
611  ? 1
612  : ((fMasterGen->settings.flag("Merging:doUNLOPSTree") ||
613  fMasterGen->settings.flag("Merging:doUNLOPSSubt") ||
614  fMasterGen->settings.flag("Merging:doUNLOPSLoop") ||
615  fMasterGen->settings.flag("Merging:doUNLOPSSubtNLO"))
616  ? 2
617  : 0);
618  if (!fMergingHook.get())
619  fMergingHook.reset(new Pythia8::amcnlo_unitarised_interface(scheme));
621  }
622 
623  bool biasedTauDecayer = fMasterGen->settings.flag("BiasedTauDecayer:filter");
624  if (biasedTauDecayer) {
625  if (!fBiasedTauDecayer.get()) {
626  Pythia8::Info localInfo = fMasterGen->info;
627  fBiasedTauDecayer.reset(new BiasedTauDecayer(&localInfo, &(fMasterGen->settings)));
628  }
629  std::vector<int> handledParticles;
630  handledParticles.push_back(15);
631  fMasterGen->setDecayPtr(fBiasedTauDecayer, handledParticles);
632  }
633 
634  bool resonanceDecayFilter = fMasterGen->settings.flag("ResonanceDecayFilter:filter");
635  if (resonanceDecayFilter) {
638  }
639 
640  bool PTFilter = fMasterGen->settings.flag("PTFilter:filter");
641  if (PTFilter) {
642  fPTFilterHook.reset(new PTFilterHook);
644  }
645 
646  if (!(fUserHooksVector->hooks).empty() && !UserHooksSet) {
647  for (auto &fUserHook : fUserHooksVector->hooks) {
648  fMasterGen->addUserHooksPtr(fUserHook);
649  }
650  UserHooksSet = true;
651  }
652 
653  if (!LHEInputFileName.empty()) {
654  edm::LogInfo("Pythia8Interface") << "Initialize direct pythia8 reading from LHE file " << LHEInputFileName;
655  edm::LogInfo("Pythia8Interface") << "Some LHE information can be not stored";
656  fMasterGen->settings.mode("Beams:frameType", 4);
657  fMasterGen->settings.word("Beams:LHEF", LHEInputFileName);
658  status = fMasterGen->init();
659 
660  } else {
661  lhaUP.reset(new LHAupLesHouches());
662  lhaUP->setScalesFromLHEF(fMasterGen->settings.flag("Beams:setProductionScalesFromLHEF"));
663  lhaUP->loadRunInfo(lheRunInfo());
664 
665  if (fJetMatchingHook.get()) {
666  fJetMatchingHook->init(lheRunInfo());
667  }
668 
669  fMasterGen->settings.mode("Beams:frameType", 5);
670  fMasterGen->setLHAupPtr(lhaUP);
671  edm::LogInfo("Pythia8Interface") << "Initializing MasterGen";
672  status = fMasterGen->init();
673  }
674 
675  //clean up temp file
676  if (!slhafile_.empty()) {
677  std::remove(slhafile_.c_str());
678  }
679 
680  if (pythiaPylistVerbosity > 10) {
681  if (pythiaPylistVerbosity == 11 || pythiaPylistVerbosity == 13)
682  fMasterGen->settings.listAll();
683  if (pythiaPylistVerbosity == 12 || pythiaPylistVerbosity == 13)
684  fMasterGen->particleData.listAll();
685  }
686 
687  // init decayer
688  fDecayer->settings.flag("ProcessLevel:all", false); // trick
689  fDecayer->settings.flag("ProcessLevel:resonanceDecays", true);
690  edm::LogInfo("Pythia8Interface") << "Initializing Decayer";
691  status1 = fDecayer->init();
692 
693  if (useEvtGen) {
694  edm::LogInfo("Pythia8Hadronizer") << "Creating and initializing pythia8 EvtGen plugin";
695  if (!evtgenDecays.get()) {
696  evtgenDecays.reset(new EvtGenDecays(fMasterGen.get(), evtgenDecFile, evtgenPdlFile));
697  for (unsigned int i = 0; i < evtgenUserFiles.size(); i++)
698  evtgenDecays->readDecayFile(evtgenUserFiles.at(i));
699  }
700  }
701 
702  return (status && status1);
703 }
704 
706  fMasterGen->stat();
707 
708  if (fEmissionVetoHook.get()) {
709  edm::LogPrint("Pythia8Interface") << "\n"
710  << "Number of ISR vetoed = " << nISRveto;
711  edm::LogPrint("Pythia8Interface") << "Number of FSR vetoed = " << nFSRveto;
712  }
713 
714  double xsec = fMasterGen->info.sigmaGen(); // cross section in mb
715  xsec *= 1.0e9; // translate to pb (CMS/Gen "convention" as of May 2009)
716  double err = fMasterGen->info.sigmaErr(); // cross section err in mb
717  err *= 1.0e9; // translate to pb (CMS/Gen "convention" as of May 2009)
719 }
720 
722  DJR.resize(0);
723  nME = -1;
724  nMEFiltered = -1;
725 
726  if (fJetMatchingHook.get()) {
727  fJetMatchingHook->resetMatchingStatus();
728  fJetMatchingHook->beforeHadronization(lheEvent());
729  }
730 
731  if (!fMasterGen->next())
732  return false;
733 
734  double mergeweight = fMasterGen.get()->info.mergingWeightNLO();
735  if (fMergingHook.get()) {
736  mergeweight *= fMergingHook->getNormFactor();
737  }
738 
739  //protect against 0-weight from ckkw or similar
740  if (std::abs(mergeweight) == 0.) {
741  event().reset();
742  return false;
743  }
744 
745  if (fJetMatchingPy8InternalHook.get()) {
746  const std::vector<double> djrmatch = fJetMatchingPy8InternalHook->getDJR();
747  //cap size of djr vector to save storage space (keep only up to first 6 elements)
748  unsigned int ndjr = std::min(djrmatch.size(), std::vector<double>::size_type(6));
749  for (unsigned int idjr = 0; idjr < ndjr; ++idjr) {
750  DJR.push_back(djrmatch[idjr]);
751  }
752 
753  nME = fJetMatchingPy8InternalHook->nMEpartons().first;
754  nMEFiltered = fJetMatchingPy8InternalHook->nMEpartons().second;
755  }
756 
757  if (evtgenDecays.get())
758  evtgenDecays->decay();
759 
760  event() = std::make_unique<HepMC::GenEvent>();
761  bool py8hepmc = toHepMC.fill_next_event(*(fMasterGen.get()), event().get());
762 
763  if (!py8hepmc) {
764  return false;
765  }
766 
767  // 0th weight is not filled anymore since v8.30x, pushback manually
768  event()->weights().push_back(fMasterGen->info.weight());
769 
770  //add ckkw/umeps/unlops merging weight
771  if (mergeweight != 1.) {
772  event()->weights()[0] *= mergeweight;
773  }
774 
775  if (fEmissionVetoHook.get()) {
776  nISRveto += fEmissionVetoHook->getNISRveto();
777  nFSRveto += fEmissionVetoHook->getNFSRveto();
778  }
779 
780  //fill additional weights for systematic uncertainties
781  if (fMasterGen->info.getWeightsDetailedSize() > 0) {
782  for (const string &key : fMasterGen->info.initrwgt->weightsKeys) {
783  double wgt = (*fMasterGen->info.weights_detailed)[key];
784  event()->weights().push_back(wgt);
785  }
786  } else if (fMasterGen->info.getWeightsCompressedSize() > 0) {
787  for (unsigned int i = 0; i < fMasterGen->info.getWeightsCompressedSize(); i++) {
788  double wgt = fMasterGen->info.getWeightsCompressedValue(i);
789  event()->weights().push_back(wgt);
790  }
791  }
792 
793  // fill shower weights
794  // http://home.thep.lu.se/~torbjorn/pythia82html/Variations.html
795  if (fMasterGen->info.nWeights() > 1) {
796  for (int i = 0; i < fMasterGen->info.nWeights(); ++i) {
797  double wgt = fMasterGen->info.weight(i);
798  event()->weights().push_back(wgt);
799  }
800  }
801 
802 #if 0
803  // VINCIA shower weights
804  // http://vincia.hepforge.org/current/share/Vincia/htmldoc/VinciaUncertainties.html
805  if (fvincia.get()) {
806  event()->weights()[0] *= fvincia->weight(0);
807  for (int iVar = 1; iVar < fvincia->nWeights(); iVar++) {
808  event()->weights().push_back(fvincia->weight(iVar));
809  }
810  }
811 
812  // Retrieve Dire shower weights
813  if (fDire.get()) {
814  fDire->weightsPtr->calcWeight(0.);
815  fDire->weightsPtr->reset();
816 
817  //Make sure the base weight comes first
818  event()->weights()[0] *= fDire->weightsPtr->getShowerWeight("base");
819 
820  unordered_map<string, double>::iterator it;
821  for (it = fDire->weightsPtr->getShowerWeights()->begin(); it != fDire->weightsPtr->getShowerWeights()->end();
822  it++) {
823  if (it->first == "base")
824  continue;
825  event()->weights().push_back(it->second);
826  }
827  }
828 #endif
829 
830  return true;
831 }
832 
834  DJR.resize(0);
835  nME = -1;
836  nMEFiltered = -1;
837  if (LHEInputFileName.empty())
838  lhaUP->loadEvent(lheEvent());
839 
840  if (fJetMatchingHook.get()) {
841  fJetMatchingHook->resetMatchingStatus();
842  fJetMatchingHook->beforeHadronization(lheEvent());
843  }
844 
845  bool py8next = fMasterGen->next();
846 
847  double mergeweight = fMasterGen.get()->info.mergingWeightNLO();
848  if (fMergingHook.get()) {
849  mergeweight *= fMergingHook->getNormFactor();
850  }
851 
852  //protect against 0-weight from ckkw or similar
853  if (!py8next || std::abs(mergeweight) == 0.) {
854  lheEvent()->count(lhef::LHERunInfo::kSelected, 1.0, mergeweight);
855  event().reset();
856  return false;
857  }
858 
859  if (fJetMatchingPy8InternalHook.get()) {
860  const std::vector<double> djrmatch = fJetMatchingPy8InternalHook->getDJR();
861  //cap size of djr vector to save storage space (keep only up to first 6 elements)
862  unsigned int ndjr = std::min(djrmatch.size(), std::vector<double>::size_type(6));
863  for (unsigned int idjr = 0; idjr < ndjr; ++idjr) {
864  DJR.push_back(djrmatch[idjr]);
865  }
866 
867  nME = fJetMatchingPy8InternalHook->nMEpartons().first;
868  nMEFiltered = fJetMatchingPy8InternalHook->nMEpartons().second;
869  }
870 
871  // update LHE matching statistics
872  //
873  lheEvent()->count(lhef::LHERunInfo::kAccepted, 1.0, mergeweight);
874 
875  if (evtgenDecays.get())
876  evtgenDecays->decay();
877 
878  event() = std::make_unique<HepMC::GenEvent>();
879 
880  bool py8hepmc = toHepMC.fill_next_event(*(fMasterGen.get()), event().get());
881 
882  if (!py8hepmc) {
883  return false;
884  }
885 
886  // 0th weight is not filled anymore since v8.30x, pushback manually
887  event()->weights().push_back(fMasterGen->info.weight());
888 
889  //add ckkw/umeps/unlops merging weight
890  if (mergeweight != 1.) {
891  event()->weights()[0] *= mergeweight;
892  }
893 
894  if (fEmissionVetoHook.get()) {
895  nISRveto += fEmissionVetoHook->getNISRveto();
896  nFSRveto += fEmissionVetoHook->getNFSRveto();
897  }
898 
899  // fill shower weights
900  // http://home.thep.lu.se/~torbjorn/pythia82html/Variations.html
901  if (fMasterGen->info.nWeights() > 1) {
902  for (int i = 0; i < fMasterGen->info.nWeights(); ++i) {
903  double wgt = fMasterGen->info.weight(i);
904  event()->weights().push_back(wgt);
905  }
906  }
907 
908  return true;
909 }
910 
912  Event *pythiaEvent = &(fMasterGen->event);
913 
914  int NPartsBeforeDecays = pythiaEvent->size();
915  int NPartsAfterDecays = event().get()->particles_size();
916 
917  if (NPartsAfterDecays == NPartsBeforeDecays)
918  return true;
919 
920  bool result = true;
921 
922  for (int ipart = NPartsAfterDecays; ipart > NPartsBeforeDecays; ipart--) {
923  HepMC::GenParticle *part = event().get()->barcode_to_particle(ipart);
924 
925  if (part->status() == 1 && (fDecayer->particleData).canDecay(part->pdg_id())) {
926  fDecayer->event.reset();
927  Particle py8part(part->pdg_id(),
928  93,
929  0,
930  0,
931  0,
932  0,
933  0,
934  0,
935  part->momentum().x(),
936  part->momentum().y(),
937  part->momentum().z(),
938  part->momentum().t(),
939  part->generated_mass());
940  HepMC::GenVertex *ProdVtx = part->production_vertex();
941  py8part.vProd(ProdVtx->position().x(), ProdVtx->position().y(), ProdVtx->position().z(), ProdVtx->position().t());
942  py8part.tau((fDecayer->particleData).tau0(part->pdg_id()));
943  fDecayer->event.append(py8part);
944  int nentries = fDecayer->event.size();
945  if (!fDecayer->event[nentries - 1].mayDecay())
946  continue;
947  fDecayer->next();
948  int nentries1 = fDecayer->event.size();
949  if (nentries1 <= nentries)
950  continue; //same number of particles, no decays...
951 
952  part->set_status(2);
953 
954  result = toHepMC.fill_next_event(*(fDecayer.get()), event().get(), -1, true, part);
955  }
956  }
957 
958  return result;
959 }
960 
962  bool lhe = lheEvent() != nullptr;
963 
964  // protection against empty weight container
965  if ((event()->weights()).empty())
966  (event()->weights()).push_back(1.);
967 
968  // now create the GenEventInfo product from the GenEvent and fill
969  // the missing pieces
970  eventInfo() = std::make_unique<GenEventInfoProduct>(event().get());
971 
972  // in pythia pthat is used to subdivide samples into different bins
973  // in LHE mode the binning is done by the external ME generator
974  // which is likely not pthat, so only filling it for Py6 internal mode
975  if (!lhe) {
976  eventInfo()->setBinningValues(std::vector<double>(1, fMasterGen->info.pTHat()));
977  }
978 
979  eventInfo()->setDJR(DJR);
980  eventInfo()->setNMEPartons(nME);
981  eventInfo()->setNMEPartonsFiltered(nMEFiltered);
982 
983  //******** Verbosity ********
984 
987  if (pythiaPylistVerbosity) {
988  fMasterGen->info.list();
989  fMasterGen->event.list();
990  }
991 
992  if (pythiaHepMCVerbosity) {
993  std::cout << "Event process = " << fMasterGen->info.code() << "\n"
994  << "----------------------" << std::endl;
995  event()->print();
996  }
998  std::cout << "Event process = " << fMasterGen->info.code() << "\n"
999  << "----------------------" << std::endl;
1000  ascii_io->write_event(event().get());
1001  }
1002  }
1003 }
1004 
1005 std::unique_ptr<GenLumiInfoHeader> Pythia8Hadronizer::getGenLumiInfoHeader() const {
1006  auto genLumiInfoHeader = BaseHadronizer::getGenLumiInfoHeader();
1007 
1008  //fill lhe headers
1009  //*FIXME* initrwgt header is corrupt due to pythia bug
1010  for (const std::string &key : fMasterGen->info.headerKeys()) {
1011  genLumiInfoHeader->lheHeaders().emplace_back(key, fMasterGen->info.header(key));
1012  }
1013 
1014  //check, if it is not only nominal weight
1015  int weights_number = fMasterGen->info.nWeights();
1016  if (fMasterGen->info.initrwgt)
1017  weights_number += fMasterGen->info.initrwgt->weightsKeys.size();
1018  if (weights_number > 1) {
1019  genLumiInfoHeader->weightNames().reserve(weights_number + 1);
1020  genLumiInfoHeader->weightNames().push_back("nominal");
1021  }
1022 
1023  //fill weight names
1024  if (fMasterGen->info.initrwgt) {
1025  for (const std::string &key : fMasterGen->info.initrwgt->weightsKeys) {
1026  std::string weightgroupname;
1027  for (const auto &wgtgrp : fMasterGen->info.initrwgt->weightgroups) {
1028  const auto &wgtgrpwgt = wgtgrp.second.weights.find(key);
1029  if (wgtgrpwgt != wgtgrp.second.weights.end()) {
1030  weightgroupname = wgtgrp.first;
1031  }
1032  }
1033 
1034  std::ostringstream weightname;
1035  weightname << "LHE, id = " << key << ", ";
1036  if (!weightgroupname.empty()) {
1037  weightname << "group = " << weightgroupname << ", ";
1038  }
1039  weightname << fMasterGen->info.initrwgt->weights[key].contents;
1040  genLumiInfoHeader->weightNames().push_back(weightname.str());
1041  }
1042  }
1043 
1044  //fill shower labels
1045  // http://home.thep.lu.se/~torbjorn/pythia82html/Variations.html
1046  // http://home.thep.lu.se/~torbjorn/doxygen/classPythia8_1_1Info.html
1047  if (fMasterGen->info.nWeights() > 1) {
1048  for (int i = 0; i < fMasterGen->info.nWeights(); ++i) {
1049  genLumiInfoHeader->weightNames().push_back(fMasterGen->info.weightLabel(i));
1050  }
1051  }
1052 
1053 #if 0
1054  // VINCIA shower weights
1055  // http://vincia.hepforge.org/current/share/Vincia/htmldoc/VinciaUncertainties.html
1056  if (fvincia.get()) {
1057  for (int iVar = 0; iVar < fvincia->nWeights(); iVar++) {
1058  genLumiInfoHeader->weightNames().push_back(fvincia->weightLabel(iVar));
1059  }
1060  }
1061 
1062  if (fDire.get()) {
1063  //Make sure the base weight comes first
1064  genLumiInfoHeader->weightNames().push_back("base");
1065 
1066  unordered_map<string, double>::iterator it;
1067  for (it = fDire->weightsPtr->getShowerWeights()->begin(); it != fDire->weightsPtr->getShowerWeights()->end();
1068  it++) {
1069  if (it->first == "base")
1070  continue;
1071  genLumiInfoHeader->weightNames().push_back(it->first);
1072  }
1073  }
1074 #endif
1075 
1076  return genLumiInfoHeader;
1077 }
1078 
1081 
1084 
1088 
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
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:184
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