CMS 3D CMS Logo

Pythia8Hadronizer.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <memory>
3 
4 #include <cstdint>
5 #include <memory>
6 #include <sstream>
7 #include <string>
8 #include <vector>
9 
10 #include "HepMC/GenEvent.h"
11 #include "HepMC/GenParticle.h"
12 
13 #include "Pythia8/Pythia.h"
14 #include "Pythia8Plugins/HepMC2.h"
15 
16 using namespace Pythia8;
17 
19 
22 
23 // PS matchning prototype
24 //
26 #include "Pythia8Plugins/JetMatching.h"
27 #include "Pythia8Plugins/aMCatNLOHooks.h"
28 
29 // Emission Veto Hooks
30 //
31 #include "Pythia8Plugins/PowhegHooks.h"
32 #include "Pythia8Plugins/PowhegHooksVincia.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<PowhegHooksVincia> fEmissionVetoHookVincia;
137  std::shared_ptr<EmissionVetoHook1> fEmissionVetoHook1;
138 
139  // Resonance scale hook
140  std::shared_ptr<PowhegResHook> fPowhegResHook;
141  std::shared_ptr<PowhegHooksBB4L> fPowhegHooksBB4L;
142 
143  // biased tau decayer
144  std::shared_ptr<BiasedTauDecayer> fBiasedTauDecayer;
145 
146  //resonance decay filter hook
147  std::shared_ptr<ResonanceDecayFilterHook> fResonanceDecayFilterHook;
148 
149  //PT filter hook
150  std::shared_ptr<PTFilterHook> fPTFilterHook;
151 
152  //Generic customized hooks vector
153  std::shared_ptr<UserHooksVector> fCustomHooksVector;
154 
165 
166  static const std::vector<std::string> p8SharedResources;
167 
168  vector<float> DJR;
169  int nME;
171 
172  int nISRveto;
173  int nFSRveto;
174 };
175 
177 
180  comEnergy(params.getParameter<double>("comEnergy")),
181  LHEInputFileName(params.getUntrackedParameter<std::string>("LHEInputFileName", "")),
182  fInitialState(PP),
183  UserHooksSet(false),
184  nME(-1),
185  nMEFiltered(-1),
186  nISRveto(0),
187  nFSRveto(0) {
188  ivhepmc = 2;
189  // J.Y.: the following 3 parameters are hacked "for a reason"
190  //
191  if (params.exists("PPbarInitialState")) {
192  if (fInitialState == PP) {
194  edm::LogImportant("GeneratorInterface|Pythia8Interface")
195  << "Pythia8 will be initialized for PROTON-ANTIPROTON INITIAL STATE. "
196  << "This is a user-request change from the DEFAULT PROTON-PROTON initial state.";
197  } else {
198  // probably need to throw on attempt to override ?
199  }
200  } else if (params.exists("ElectronPositronInitialState")) {
201  if (fInitialState == PP) {
203  edm::LogInfo("GeneratorInterface|Pythia8Interface")
204  << "Pythia8 will be initialized for ELECTRON-POSITRON INITIAL STATE. "
205  << "This is a user-request change from the DEFAULT PROTON-PROTON initial state.";
206  } else {
207  // probably need to throw on attempt to override ?
208  }
209  } else if (params.exists("ElectronProtonInitialState") || params.exists("PositronProtonInitialState")) {
210  // throw on unknown initial state !
211  throw edm::Exception(edm::errors::Configuration, "Pythia8Interface")
212  << " UNKNOWN INITIAL STATE. \n The allowed initial states are: PP, PPbar, ElectronPositron \n";
213  }
214 
215  // avoid filling weights twice (from v8.30x)
216  toHepMC.set_store_weights(false);
217 
218  // Reweight user hook
219  //
220  if (params.exists("reweightGen")) {
221  edm::LogInfo("Pythia8Interface") << "Start setup for reweightGen";
222  edm::ParameterSet rgParams = params.getParameter<edm::ParameterSet>("reweightGen");
223  fReweightUserHook.reset(
224  new PtHatReweightUserHook(rgParams.getParameter<double>("pTRef"), rgParams.getParameter<double>("power")));
225  edm::LogInfo("Pythia8Interface") << "End setup for reweightGen";
226  }
227  if (params.exists("reweightGenEmp")) {
228  edm::LogInfo("Pythia8Interface") << "Start setup for reweightGenEmp";
229  edm::ParameterSet rgeParams = params.getParameter<edm::ParameterSet>("reweightGenEmp");
230 
231  std::string tuneName = "";
232  if (rgeParams.exists("tune"))
233  tuneName = rgeParams.getParameter<std::string>("tune");
234  fReweightEmpUserHook.reset(new PtHatEmpReweightUserHook(tuneName));
235  edm::LogInfo("Pythia8Interface") << "End setup for reweightGenEmp";
236  }
237  if (params.exists("reweightGenRap")) {
238  edm::LogInfo("Pythia8Interface") << "Start setup for reweightGenRap";
239  edm::ParameterSet rgrParams = params.getParameter<edm::ParameterSet>("reweightGenRap");
240  fReweightRapUserHook.reset(new RapReweightUserHook(rgrParams.getParameter<std::string>("yLabSigmaFunc"),
241  rgrParams.getParameter<double>("yLabPower"),
242  rgrParams.getParameter<std::string>("yCMSigmaFunc"),
243  rgrParams.getParameter<double>("yCMPower"),
244  rgrParams.getParameter<double>("pTHatMin"),
245  rgrParams.getParameter<double>("pTHatMax")));
246  edm::LogInfo("Pythia8Interface") << "End setup for reweightGenRap";
247  }
248  if (params.exists("reweightGenPtHatRap")) {
249  edm::LogInfo("Pythia8Interface") << "Start setup for reweightGenPtHatRap";
250  edm::ParameterSet rgrParams = params.getParameter<edm::ParameterSet>("reweightGenPtHatRap");
251  fReweightPtHatRapUserHook.reset(new PtHatRapReweightUserHook(rgrParams.getParameter<std::string>("yLabSigmaFunc"),
252  rgrParams.getParameter<double>("yLabPower"),
253  rgrParams.getParameter<std::string>("yCMSigmaFunc"),
254  rgrParams.getParameter<double>("yCMPower"),
255  rgrParams.getParameter<double>("pTHatMin"),
256  rgrParams.getParameter<double>("pTHatMax")));
257  edm::LogInfo("Pythia8Interface") << "End setup for reweightGenPtHatRap";
258  }
259 
260  if (params.exists("useUserHook"))
261  throw edm::Exception(edm::errors::Configuration, "Pythia8Interface")
262  << " Obsolete parameter: useUserHook \n Please use the actual one instead \n";
263 
264  // PS matching prototype
265  //
266  if (params.exists("jetMatching")) {
267  edm::ParameterSet jmParams = params.getUntrackedParameter<edm::ParameterSet>("jetMatching");
268  std::string scheme = jmParams.getParameter<std::string>("scheme");
269  if (scheme == "Madgraph" || scheme == "MadgraphFastJet") {
270  fJetMatchingHook.reset(new JetMatchingHook(jmParams, &fMasterGen->info));
271  }
272  }
273 
274  // Pythia8Interface emission veto
275  //
276  if (params.exists("emissionVeto1")) {
277  EV1_nFinal = -1;
278  if (params.exists("EV1_nFinal"))
279  EV1_nFinal = params.getParameter<int>("EV1_nFinal");
280  EV1_vetoOn = true;
281  if (params.exists("EV1_vetoOn"))
282  EV1_vetoOn = params.getParameter<bool>("EV1_vetoOn");
283  EV1_maxVetoCount = 10;
284  if (params.exists("EV1_maxVetoCount"))
285  EV1_maxVetoCount = params.getParameter<int>("EV1_maxVetoCount");
286  EV1_pThardMode = 1;
287  if (params.exists("EV1_pThardMode"))
288  EV1_pThardMode = params.getParameter<int>("EV1_pThardMode");
289  EV1_pTempMode = 0;
290  if (params.exists("EV1_pTempMode"))
291  EV1_pTempMode = params.getParameter<int>("EV1_pTempMode");
292  if (EV1_pTempMode > 2 || EV1_pTempMode < 0)
293  throw edm::Exception(edm::errors::Configuration, "Pythia8Interface") << " Wrong value for EV1_pTempMode code\n";
294  EV1_emittedMode = 0;
295  if (params.exists("EV1_emittedMode"))
296  EV1_emittedMode = params.getParameter<int>("EV1_emittedMode");
297  EV1_pTdefMode = 1;
298  if (params.exists("EV1_pTdefMode"))
299  EV1_pTdefMode = params.getParameter<int>("EV1_pTdefMode");
300  EV1_MPIvetoOn = false;
301  if (params.exists("EV1_MPIvetoOn"))
302  EV1_MPIvetoOn = params.getParameter<bool>("EV1_MPIvetoOn");
303  EV1_QEDvetoMode = 0;
304  if (params.exists("EV1_QEDvetoMode"))
305  EV1_QEDvetoMode = params.getParameter<int>("EV1_QEDvetoMode");
306  EV1_nFinalMode = 0;
307  if (params.exists("EV1_nFinalMode"))
308  EV1_nFinalMode = params.getParameter<int>("EV1_nFinalMode");
310  EV1_vetoOn,
319  0));
320  }
321 
322  if (params.exists("UserCustomization")) {
323  fCustomHooksVector = std::make_shared<UserHooksVector>();
324  const std::vector<edm::ParameterSet> userParams =
325  params.getParameter<std::vector<edm::ParameterSet>>("UserCustomization");
326  for (const auto &pluginParams : userParams) {
327  (fCustomHooksVector->hooks)
328  .push_back(
329  CustomHookFactory::get()->create(pluginParams.getParameter<std::string>("pluginName"), pluginParams));
330  }
331  }
332 
333  if (params.exists("VinciaPlugin")) {
334  throw edm::Exception(edm::errors::Configuration, "Pythia8Interface")
335  << " Obsolete parameter: VinciaPlugin \n Please use the parameter PartonShowers:model instead \n";
336  }
337  if (params.exists("DirePlugin")) {
338  throw edm::Exception(edm::errors::Configuration, "Pythia8Interface")
339  << " Obsolete parameter: DirePlugin \n Please use the parameter PartonShowers:model instead \n";
340  }
341 }
342 
344 
346  bool status = false, status1 = false;
347 
348  if (lheFile_.empty()) {
349  if (fInitialState == PP) // default
350  {
351  fMasterGen->settings.mode("Beams:idA", 2212);
352  fMasterGen->settings.mode("Beams:idB", 2212);
353  } else if (fInitialState == PPbar) {
354  fMasterGen->settings.mode("Beams:idA", 2212);
355  fMasterGen->settings.mode("Beams:idB", -2212);
356  } else if (fInitialState == ElectronPositron) {
357  fMasterGen->settings.mode("Beams:idA", 11);
358  fMasterGen->settings.mode("Beams:idB", -11);
359  } else {
360  // throw on unknown initial state !
361  throw edm::Exception(edm::errors::Configuration, "Pythia8Interface")
362  << " UNKNOWN INITIAL STATE. \n The allowed initial states are: PP, PPbar, ElectronPositron \n";
363  }
364  fMasterGen->settings.parm("Beams:eCM", comEnergy);
365  } else {
366  fMasterGen->settings.mode("Beams:frameType", 4);
367  fMasterGen->settings.word("Beams:LHEF", lheFile_);
368  }
369 
370  if (!fUserHooksVector.get())
371  fUserHooksVector.reset(new UserHooksVector);
372  (fUserHooksVector->hooks).clear();
373 
374  if (fReweightUserHook.get())
375  (fUserHooksVector->hooks).push_back(fReweightUserHook);
376  if (fReweightEmpUserHook.get())
377  (fUserHooksVector->hooks).push_back(fReweightEmpUserHook);
378  if (fReweightRapUserHook.get())
379  (fUserHooksVector->hooks).push_back(fReweightRapUserHook);
380  if (fReweightPtHatRapUserHook.get())
381  (fUserHooksVector->hooks).push_back(fReweightPtHatRapUserHook);
382  if (fJetMatchingHook.get())
383  (fUserHooksVector->hooks).push_back(fJetMatchingHook);
384  if (fEmissionVetoHook1.get()) {
385  edm::LogInfo("Pythia8Interface") << "Turning on Emission Veto Hook 1 from CMSSW Pythia8Interface";
387  }
388 
389  bool VinciaShower = fMasterGen->settings.mode("PartonShowers:Model") == 2;
390 
391  if ((fMasterGen->settings.mode("POWHEG:veto") > 0 || fMasterGen->settings.mode("POWHEG:MPIveto") > 0) &&
392  !VinciaShower) {
393  if (fJetMatchingHook.get() || fEmissionVetoHook1.get())
394  throw edm::Exception(edm::errors::Configuration, "Pythia8Interface")
395  << " Attempt to turn on PowhegHooks by pythia8 settings but there are incompatible hooks on \n Incompatible "
396  "are : jetMatching, emissionVeto1 \n";
397 
398  if (!fEmissionVetoHook.get())
399  fEmissionVetoHook.reset(new PowhegHooks());
400 
401  edm::LogInfo("Pythia8Interface") << "Turning on Emission Veto Hook from pythia8 code";
403  }
404 
405  if (fMasterGen->settings.mode("POWHEG:veto") > 0 && VinciaShower) {
406  edm::LogInfo("Pythia8Interface") << "Turning on Vincia Emission Veto Hook from pythia8 code";
407  if (!fEmissionVetoHookVincia.get())
408  fEmissionVetoHookVincia.reset(new PowhegHooksVincia());
410  }
411 
412  bool PowhegRes = fMasterGen->settings.flag("POWHEGres:calcScales");
413  if (PowhegRes) {
414  edm::LogInfo("Pythia8Interface") << "Turning on resonance scale setting from CMSSW Pythia8Interface";
415  if (!fPowhegResHook.get())
416  fPowhegResHook.reset(new PowhegResHook());
418  }
419 
420  bool PowhegBB4L = fMasterGen->settings.flag("POWHEG:bb4l");
421  if (PowhegBB4L) {
422  edm::LogInfo("Pythia8Interface") << "Turning on BB4l hook from CMSSW Pythia8Interface";
423  if (!fPowhegHooksBB4L.get())
424  fPowhegHooksBB4L.reset(new PowhegHooksBB4L());
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  bool VinciaShower = fMasterGen->settings.mode("PartonShowers:Model") == 2;
559 
560  if ((fMasterGen->settings.mode("POWHEG:veto") > 0 || fMasterGen->settings.mode("POWHEG:MPIveto") > 0) &&
561  !VinciaShower) {
562  if (fJetMatchingHook.get() || fEmissionVetoHook1.get())
563  throw edm::Exception(edm::errors::Configuration, "Pythia8Interface")
564  << " Attempt to turn on PowhegHooks by pythia8 settings but there are incompatible hooks on \n Incompatible "
565  "are : jetMatching, emissionVeto1 \n";
566 
567  if (!fEmissionVetoHook.get())
568  fEmissionVetoHook.reset(new PowhegHooks());
569 
570  edm::LogInfo("Pythia8Interface") << "Turning on Emission Veto Hook from pythia8 code";
572  }
573 
574  if (fMasterGen->settings.mode("POWHEG:veto") > 0 && VinciaShower) {
575  edm::LogInfo("Pythia8Interface") << "Turning on Vincia Emission Veto Hook from pythia8 code";
576  if (!fEmissionVetoHookVincia.get())
577  fEmissionVetoHookVincia.reset(new PowhegHooksVincia());
579  }
580 
581  bool PowhegRes = fMasterGen->settings.flag("POWHEGres:calcScales");
582  if (PowhegRes) {
583  edm::LogInfo("Pythia8Interface") << "Turning on resonance scale setting from CMSSW Pythia8Interface";
584  if (!fPowhegResHook.get())
585  fPowhegResHook.reset(new PowhegResHook());
587  }
588 
589  bool PowhegBB4L = fMasterGen->settings.flag("POWHEG:bb4l");
590  if (PowhegBB4L) {
591  edm::LogInfo("Pythia8Interface") << "Turning on BB4l hook from CMSSW Pythia8Interface";
592  if (!fPowhegHooksBB4L.get())
593  fPowhegHooksBB4L.reset(new PowhegHooksBB4L());
595  }
596 
597  //adapted from main89.cc in pythia8 examples
598  bool internalMatching = fMasterGen->settings.flag("JetMatching:merge");
599  bool internalMerging = !(fMasterGen->settings.word("Merging:Process") == "void");
600 
601  if (internalMatching && internalMerging) {
602  throw edm::Exception(edm::errors::Configuration, "Pythia8Interface")
603  << " Only one jet matching/merging scheme can be used at a time. \n";
604  }
605 
606  if (internalMatching) {
607  if (!fJetMatchingPy8InternalHook.get())
608  fJetMatchingPy8InternalHook.reset(new Pythia8::JetMatchingMadgraph);
610  }
611 
612  if (internalMerging) {
613  int scheme = (fMasterGen->settings.flag("Merging:doUMEPSTree") || fMasterGen->settings.flag("Merging:doUMEPSSubt"))
614  ? 1
615  : ((fMasterGen->settings.flag("Merging:doUNLOPSTree") ||
616  fMasterGen->settings.flag("Merging:doUNLOPSSubt") ||
617  fMasterGen->settings.flag("Merging:doUNLOPSLoop") ||
618  fMasterGen->settings.flag("Merging:doUNLOPSSubtNLO"))
619  ? 2
620  : 0);
621  if (!fMergingHook.get())
622  fMergingHook.reset(new Pythia8::amcnlo_unitarised_interface(scheme));
624  }
625 
626  bool biasedTauDecayer = fMasterGen->settings.flag("BiasedTauDecayer:filter");
627  if (biasedTauDecayer) {
628  if (!fBiasedTauDecayer.get()) {
629  Pythia8::Info localInfo = fMasterGen->info;
630  fBiasedTauDecayer.reset(new BiasedTauDecayer(&localInfo, &(fMasterGen->settings)));
631  }
632  std::vector<int> handledParticles;
633  handledParticles.push_back(15);
634  fMasterGen->setDecayPtr(fBiasedTauDecayer, handledParticles);
635  }
636 
637  bool resonanceDecayFilter = fMasterGen->settings.flag("ResonanceDecayFilter:filter");
638  if (resonanceDecayFilter) {
641  }
642 
643  bool PTFilter = fMasterGen->settings.flag("PTFilter:filter");
644  if (PTFilter) {
645  fPTFilterHook.reset(new PTFilterHook);
647  }
648 
649  if (!(fUserHooksVector->hooks).empty() && !UserHooksSet) {
650  for (auto &fUserHook : fUserHooksVector->hooks) {
651  fMasterGen->addUserHooksPtr(fUserHook);
652  }
653  UserHooksSet = true;
654  }
655 
656  if (!LHEInputFileName.empty()) {
657  edm::LogInfo("Pythia8Interface") << "Initialize direct pythia8 reading from LHE file " << LHEInputFileName;
658  edm::LogInfo("Pythia8Interface") << "Some LHE information can be not stored";
659  fMasterGen->settings.mode("Beams:frameType", 4);
660  fMasterGen->settings.word("Beams:LHEF", LHEInputFileName);
661  status = fMasterGen->init();
662 
663  } else {
664  lhaUP.reset(new LHAupLesHouches());
665  lhaUP->setScalesFromLHEF(fMasterGen->settings.flag("Beams:setProductionScalesFromLHEF"));
666  lhaUP->loadRunInfo(lheRunInfo());
667 
668  if (fJetMatchingHook.get()) {
669  fJetMatchingHook->init(lheRunInfo());
670  }
671 
672  fMasterGen->settings.mode("Beams:frameType", 5);
673  fMasterGen->setLHAupPtr(lhaUP);
674  edm::LogInfo("Pythia8Interface") << "Initializing MasterGen";
675  status = fMasterGen->init();
676  }
677 
678  //clean up temp file
679  if (!slhafile_.empty()) {
680  std::remove(slhafile_.c_str());
681  }
682 
683  if (pythiaPylistVerbosity > 10) {
684  if (pythiaPylistVerbosity == 11 || pythiaPylistVerbosity == 13)
685  fMasterGen->settings.listAll();
686  if (pythiaPylistVerbosity == 12 || pythiaPylistVerbosity == 13)
687  fMasterGen->particleData.listAll();
688  }
689 
690  // init decayer
691  fDecayer->settings.flag("ProcessLevel:all", false); // trick
692  fDecayer->settings.flag("ProcessLevel:resonanceDecays", true);
693  edm::LogInfo("Pythia8Interface") << "Initializing Decayer";
694  status1 = fDecayer->init();
695 
696  if (useEvtGen) {
697  edm::LogInfo("Pythia8Hadronizer") << "Creating and initializing pythia8 EvtGen plugin";
698  if (!evtgenDecays.get()) {
699  evtgenDecays.reset(new EvtGenDecays(fMasterGen.get(), evtgenDecFile, evtgenPdlFile));
700  for (unsigned int i = 0; i < evtgenUserFiles.size(); i++)
701  evtgenDecays->readDecayFile(evtgenUserFiles.at(i));
702  }
703  }
704 
705  return (status && status1);
706 }
707 
709  fMasterGen->stat();
710 
711  if (fEmissionVetoHook.get()) {
712  edm::LogPrint("Pythia8Interface") << "\n"
713  << "Number of ISR vetoed = " << nISRveto;
714  edm::LogPrint("Pythia8Interface") << "Number of FSR vetoed = " << nFSRveto;
715  }
716 
717  double xsec = fMasterGen->info.sigmaGen(); // cross section in mb
718  xsec *= 1.0e9; // translate to pb (CMS/Gen "convention" as of May 2009)
719  double err = fMasterGen->info.sigmaErr(); // cross section err in mb
720  err *= 1.0e9; // translate to pb (CMS/Gen "convention" as of May 2009)
722 }
723 
725  DJR.resize(0);
726  nME = -1;
727  nMEFiltered = -1;
728 
729  if (fJetMatchingHook.get()) {
730  fJetMatchingHook->resetMatchingStatus();
731  fJetMatchingHook->beforeHadronization(lheEvent());
732  }
733 
734  if (!fMasterGen->next())
735  return false;
736 
737  double mergeweight = fMasterGen.get()->info.mergingWeightNLO();
738  if (fMergingHook.get()) {
739  mergeweight *= fMergingHook->getNormFactor();
740  }
741 
742  //protect against 0-weight from ckkw or similar
743  if (std::abs(mergeweight) == 0.) {
744  event().reset();
745  return false;
746  }
747 
748  if (fJetMatchingPy8InternalHook.get()) {
749  const std::vector<double> djrmatch = fJetMatchingPy8InternalHook->getDJR();
750  //cap size of djr vector to save storage space (keep only up to first 6 elements)
751  unsigned int ndjr = std::min(djrmatch.size(), std::vector<double>::size_type(6));
752  for (unsigned int idjr = 0; idjr < ndjr; ++idjr) {
753  DJR.push_back(djrmatch[idjr]);
754  }
755 
756  nME = fJetMatchingPy8InternalHook->nMEpartons().first;
757  nMEFiltered = fJetMatchingPy8InternalHook->nMEpartons().second;
758  }
759 
760  if (evtgenDecays.get())
761  evtgenDecays->decay();
762 
763  event() = std::make_unique<HepMC::GenEvent>();
764  bool py8hepmc = toHepMC.fill_next_event(*(fMasterGen.get()), event().get());
765 
766  if (!py8hepmc) {
767  return false;
768  }
769 
770  // 0th weight is not filled anymore since v8.30x, pushback manually
771  event()->weights().push_back(fMasterGen->info.weight());
772 
773  //add ckkw/umeps/unlops merging weight
774  if (mergeweight != 1.) {
775  event()->weights()[0] *= mergeweight;
776  }
777 
778  if (fEmissionVetoHook.get()) {
779  nISRveto += fEmissionVetoHook->getNISRveto();
780  nFSRveto += fEmissionVetoHook->getNFSRveto();
781  }
782 
783  //fill additional weights for systematic uncertainties
784  if (fMasterGen->info.getWeightsDetailedSize() > 0) {
785  for (const string &key : fMasterGen->info.initrwgt->weightsKeys) {
786  double wgt = (*fMasterGen->info.weights_detailed)[key];
787  event()->weights().push_back(wgt);
788  }
789  } else if (fMasterGen->info.getWeightsCompressedSize() > 0) {
790  for (unsigned int i = 0; i < fMasterGen->info.getWeightsCompressedSize(); i++) {
791  double wgt = fMasterGen->info.getWeightsCompressedValue(i);
792  event()->weights().push_back(wgt);
793  }
794  }
795 
796  // fill shower weights
797  // http://home.thep.lu.se/~torbjorn/pythia82html/Variations.html
798  if (fMasterGen->info.nWeights() > 1) {
799  for (int i = 0; i < fMasterGen->info.nWeights(); ++i) {
800  double wgt = fMasterGen->info.weight(i);
801  event()->weights().push_back(wgt);
802  }
803  }
804 
805 #if 0
806  // VINCIA shower weights
807  // http://vincia.hepforge.org/current/share/Vincia/htmldoc/VinciaUncertainties.html
808  if (fvincia.get()) {
809  event()->weights()[0] *= fvincia->weight(0);
810  for (int iVar = 1; iVar < fvincia->nWeights(); iVar++) {
811  event()->weights().push_back(fvincia->weight(iVar));
812  }
813  }
814 
815  // Retrieve Dire shower weights
816  if (fDire.get()) {
817  fDire->weightsPtr->calcWeight(0.);
818  fDire->weightsPtr->reset();
819 
820  //Make sure the base weight comes first
821  event()->weights()[0] *= fDire->weightsPtr->getShowerWeight("base");
822 
823  unordered_map<string, double>::iterator it;
824  for (it = fDire->weightsPtr->getShowerWeights()->begin(); it != fDire->weightsPtr->getShowerWeights()->end();
825  it++) {
826  if (it->first == "base")
827  continue;
828  event()->weights().push_back(it->second);
829  }
830  }
831 #endif
832 
833  return true;
834 }
835 
837  DJR.resize(0);
838  nME = -1;
839  nMEFiltered = -1;
840  if (LHEInputFileName.empty())
841  lhaUP->loadEvent(lheEvent());
842 
843  if (fJetMatchingHook.get()) {
844  fJetMatchingHook->resetMatchingStatus();
845  fJetMatchingHook->beforeHadronization(lheEvent());
846  }
847 
848  bool py8next = fMasterGen->next();
849 
850  double mergeweight = fMasterGen.get()->info.mergingWeightNLO();
851  if (fMergingHook.get()) {
852  mergeweight *= fMergingHook->getNormFactor();
853  }
854 
855  //protect against 0-weight from ckkw or similar
856  if (!py8next || std::abs(mergeweight) == 0.) {
857  lheEvent()->count(lhef::LHERunInfo::kSelected, 1.0, mergeweight);
858  event().reset();
859  return false;
860  }
861 
862  if (fJetMatchingPy8InternalHook.get()) {
863  const std::vector<double> djrmatch = fJetMatchingPy8InternalHook->getDJR();
864  //cap size of djr vector to save storage space (keep only up to first 6 elements)
865  unsigned int ndjr = std::min(djrmatch.size(), std::vector<double>::size_type(6));
866  for (unsigned int idjr = 0; idjr < ndjr; ++idjr) {
867  DJR.push_back(djrmatch[idjr]);
868  }
869 
870  nME = fJetMatchingPy8InternalHook->nMEpartons().first;
871  nMEFiltered = fJetMatchingPy8InternalHook->nMEpartons().second;
872  }
873 
874  // update LHE matching statistics
875  //
876  lheEvent()->count(lhef::LHERunInfo::kAccepted, 1.0, mergeweight);
877 
878  if (evtgenDecays.get())
879  evtgenDecays->decay();
880 
881  event() = std::make_unique<HepMC::GenEvent>();
882 
883  bool py8hepmc = toHepMC.fill_next_event(*(fMasterGen.get()), event().get());
884 
885  if (!py8hepmc) {
886  return false;
887  }
888 
889  // 0th weight is not filled anymore since v8.30x, pushback manually
890  event()->weights().push_back(fMasterGen->info.weight());
891 
892  //add ckkw/umeps/unlops merging weight
893  if (mergeweight != 1.) {
894  event()->weights()[0] *= mergeweight;
895  }
896 
897  if (fEmissionVetoHook.get()) {
898  nISRveto += fEmissionVetoHook->getNISRveto();
899  nFSRveto += fEmissionVetoHook->getNFSRveto();
900  }
901 
902  // fill shower weights
903  // http://home.thep.lu.se/~torbjorn/pythia82html/Variations.html
904  if (fMasterGen->info.nWeights() > 1) {
905  for (int i = 0; i < fMasterGen->info.nWeights(); ++i) {
906  double wgt = fMasterGen->info.weight(i);
907  event()->weights().push_back(wgt);
908  }
909  }
910 
911  return true;
912 }
913 
915  Event *pythiaEvent = &(fMasterGen->event);
916 
917  int NPartsBeforeDecays = pythiaEvent->size();
918  int NPartsAfterDecays = event().get()->particles_size();
919 
920  if (NPartsAfterDecays == NPartsBeforeDecays)
921  return true;
922 
923  bool result = true;
924 
925  for (int ipart = NPartsAfterDecays; ipart > NPartsBeforeDecays; ipart--) {
926  HepMC::GenParticle *part = event().get()->barcode_to_particle(ipart);
927 
928  if (part->status() == 1 && (fDecayer->particleData).canDecay(part->pdg_id())) {
929  fDecayer->event.reset();
930  Particle py8part(part->pdg_id(),
931  93,
932  0,
933  0,
934  0,
935  0,
936  0,
937  0,
938  part->momentum().x(),
939  part->momentum().y(),
940  part->momentum().z(),
941  part->momentum().t(),
942  part->generated_mass());
943  HepMC::GenVertex *ProdVtx = part->production_vertex();
944  py8part.vProd(ProdVtx->position().x(), ProdVtx->position().y(), ProdVtx->position().z(), ProdVtx->position().t());
945  py8part.tau((fDecayer->particleData).tau0(part->pdg_id()));
946  fDecayer->event.append(py8part);
947  int nentries = fDecayer->event.size();
948  if (!fDecayer->event[nentries - 1].mayDecay())
949  continue;
950  fDecayer->next();
951  int nentries1 = fDecayer->event.size();
952  if (nentries1 <= nentries)
953  continue; //same number of particles, no decays...
954 
955  part->set_status(2);
956 
957  result = toHepMC.fill_next_event(*(fDecayer.get()), event().get(), -1, true, part);
958  }
959  }
960 
961  return result;
962 }
963 
965  bool lhe = lheEvent() != nullptr;
966 
967  // protection against empty weight container
968  if ((event()->weights()).empty())
969  (event()->weights()).push_back(1.);
970 
971  // now create the GenEventInfo product from the GenEvent and fill
972  // the missing pieces
973  eventInfo() = std::make_unique<GenEventInfoProduct>(event().get());
974 
975  // in pythia pthat is used to subdivide samples into different bins
976  // in LHE mode the binning is done by the external ME generator
977  // which is likely not pthat, so only filling it for Py6 internal mode
978  if (!lhe) {
979  eventInfo()->setBinningValues(std::vector<double>(1, fMasterGen->info.pTHat()));
980  }
981 
982  eventInfo()->setDJR(DJR);
983  eventInfo()->setNMEPartons(nME);
984  eventInfo()->setNMEPartonsFiltered(nMEFiltered);
985 
986  //******** Verbosity ********
987 
990  if (pythiaPylistVerbosity) {
991  fMasterGen->info.list();
992  fMasterGen->event.list();
993  }
994 
995  if (pythiaHepMCVerbosity) {
996  std::cout << "Event process = " << fMasterGen->info.code() << "\n"
997  << "----------------------" << std::endl;
998  event()->print();
999  }
1001  std::cout << "Event process = " << fMasterGen->info.code() << "\n"
1002  << "----------------------" << std::endl;
1003  ascii_io->write_event(event().get());
1004  }
1005  }
1006 }
1007 
1008 std::unique_ptr<GenLumiInfoHeader> Pythia8Hadronizer::getGenLumiInfoHeader() const {
1009  auto genLumiInfoHeader = BaseHadronizer::getGenLumiInfoHeader();
1010 
1011  //fill lhe headers
1012  //*FIXME* initrwgt header is corrupt due to pythia bug
1013  for (const std::string &key : fMasterGen->info.headerKeys()) {
1014  genLumiInfoHeader->lheHeaders().emplace_back(key, fMasterGen->info.header(key));
1015  }
1016 
1017  //check, if it is not only nominal weight
1018  int weights_number = fMasterGen->info.nWeights();
1019  if (fMasterGen->info.initrwgt)
1020  weights_number += fMasterGen->info.initrwgt->weightsKeys.size();
1021  if (weights_number > 1) {
1022  genLumiInfoHeader->weightNames().reserve(weights_number + 1);
1023  genLumiInfoHeader->weightNames().push_back("nominal");
1024  }
1025 
1026  //fill weight names
1027  if (fMasterGen->info.initrwgt) {
1028  for (const std::string &key : fMasterGen->info.initrwgt->weightsKeys) {
1029  std::string weightgroupname;
1030  for (const auto &wgtgrp : fMasterGen->info.initrwgt->weightgroups) {
1031  const auto &wgtgrpwgt = wgtgrp.second.weights.find(key);
1032  if (wgtgrpwgt != wgtgrp.second.weights.end()) {
1033  weightgroupname = wgtgrp.first;
1034  }
1035  }
1036 
1037  std::ostringstream weightname;
1038  weightname << "LHE, id = " << key << ", ";
1039  if (!weightgroupname.empty()) {
1040  weightname << "group = " << weightgroupname << ", ";
1041  }
1042  weightname << fMasterGen->info.initrwgt->weights[key].contents;
1043  genLumiInfoHeader->weightNames().push_back(weightname.str());
1044  }
1045  }
1046 
1047  //fill shower labels
1048  // http://home.thep.lu.se/~torbjorn/pythia82html/Variations.html
1049  // http://home.thep.lu.se/~torbjorn/doxygen/classPythia8_1_1Info.html
1050  if (fMasterGen->info.nWeights() > 1) {
1051  for (int i = 0; i < fMasterGen->info.nWeights(); ++i) {
1052  genLumiInfoHeader->weightNames().push_back(fMasterGen->info.weightLabel(i));
1053  }
1054  }
1055 
1056 #if 0
1057  // VINCIA shower weights
1058  // http://vincia.hepforge.org/current/share/Vincia/htmldoc/VinciaUncertainties.html
1059  if (fvincia.get()) {
1060  for (int iVar = 0; iVar < fvincia->nWeights(); iVar++) {
1061  genLumiInfoHeader->weightNames().push_back(fvincia->weightLabel(iVar));
1062  }
1063  }
1064 
1065  if (fDire.get()) {
1066  //Make sure the base weight comes first
1067  genLumiInfoHeader->weightNames().push_back("base");
1068 
1069  unordered_map<string, double>::iterator it;
1070  for (it = fDire->weightsPtr->getShowerWeights()->begin(); it != fDire->weightsPtr->getShowerWeights()->end();
1071  it++) {
1072  if (it->first == "base")
1073  continue;
1074  genLumiInfoHeader->weightNames().push_back(it->first);
1075  }
1076  }
1077 #endif
1078 
1079  return genLumiInfoHeader;
1080 }
1081 
1084 
1087 
1091 
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
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