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