CMS 3D CMS Logo

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