CMS 3D CMS Logo

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