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