CMS 3D CMS Logo

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