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