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  // avoid filling weights twice (from v8.30x)
209  toHepMC.set_store_weights(false);
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  throw edm::Exception(edm::errors::Configuration, "Pythia8Interface")
317  << " Obsolete parameter: VinciaPlugin \n Please use the parameter PartonShowers:model instead \n";
318  }
319  if (params.exists("DirePlugin")) {
320  throw edm::Exception(edm::errors::Configuration, "Pythia8Interface")
321  << " Obsolete parameter: DirePlugin \n Please use the parameter PartonShowers:model instead \n";
322  }
323 }
324 
326 
328  bool status = false, status1 = false;
329 
330  if (lheFile_.empty()) {
331  if (fInitialState == PP) // default
332  {
333  fMasterGen->settings.mode("Beams:idA", 2212);
334  fMasterGen->settings.mode("Beams:idB", 2212);
335  } else if (fInitialState == PPbar) {
336  fMasterGen->settings.mode("Beams:idA", 2212);
337  fMasterGen->settings.mode("Beams:idB", -2212);
338  } else if (fInitialState == ElectronPositron) {
339  fMasterGen->settings.mode("Beams:idA", 11);
340  fMasterGen->settings.mode("Beams:idB", -11);
341  } else {
342  // throw on unknown initial state !
343  throw edm::Exception(edm::errors::Configuration, "Pythia8Interface")
344  << " UNKNOWN INITIAL STATE. \n The allowed initial states are: PP, PPbar, ElectronPositron \n";
345  }
346  fMasterGen->settings.parm("Beams:eCM", comEnergy);
347  } else {
348  fMasterGen->settings.mode("Beams:frameType", 4);
349  fMasterGen->settings.word("Beams:LHEF", lheFile_);
350  }
351 
352  if (!fUserHooksVector.get())
353  fUserHooksVector.reset(new UserHooksVector);
354  (fUserHooksVector->hooks).clear();
355 
356  if (fReweightUserHook.get())
357  (fUserHooksVector->hooks).push_back(fReweightUserHook);
358  if (fReweightEmpUserHook.get())
359  (fUserHooksVector->hooks).push_back(fReweightEmpUserHook);
360  if (fReweightRapUserHook.get())
361  (fUserHooksVector->hooks).push_back(fReweightRapUserHook);
362  if (fReweightPtHatRapUserHook.get())
363  (fUserHooksVector->hooks).push_back(fReweightPtHatRapUserHook);
364  if (fJetMatchingHook.get())
365  (fUserHooksVector->hooks).push_back(fJetMatchingHook);
366  if (fEmissionVetoHook1.get()) {
367  edm::LogInfo("Pythia8Interface") << "Turning on Emission Veto Hook 1 from CMSSW Pythia8Interface";
368  (fUserHooksVector->hooks).push_back(fEmissionVetoHook1);
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  if (!fEmissionVetoHook.get())
378  fEmissionVetoHook.reset(new PowhegHooks());
379 
380  edm::LogInfo("Pythia8Interface") << "Turning on Emission Veto Hook from pythia8 code";
381  (fUserHooksVector->hooks).push_back(fEmissionVetoHook);
382  }
383 
384  bool PowhegRes = fMasterGen->settings.flag("POWHEGres:calcScales");
385  if (PowhegRes) {
386  edm::LogInfo("Pythia8Interface") << "Turning on resonance scale setting from CMSSW Pythia8Interface";
387  if (!fPowhegResHook.get())
388  fPowhegResHook.reset(new PowhegResHook());
389  (fUserHooksVector->hooks).push_back(fPowhegResHook);
390  }
391 
392  bool PowhegBB4L = fMasterGen->settings.flag("POWHEG:bb4l");
393  if (PowhegBB4L) {
394  edm::LogInfo("Pythia8Interface") << "Turning on BB4l hook from CMSSW Pythia8Interface";
395  if (!fPowhegHooksBB4L.get())
396  fPowhegHooksBB4L.reset(new PowhegHooksBB4L());
397  (fUserHooksVector->hooks).push_back(fPowhegHooksBB4L);
398  }
399 
400  //adapted from main89.cc in pythia8 examples
401  bool internalMatching = fMasterGen->settings.flag("JetMatching:merge");
402  bool internalMerging = !(fMasterGen->settings.word("Merging:Process") == "void");
403 
404  if (internalMatching && internalMerging) {
405  throw edm::Exception(edm::errors::Configuration, "Pythia8Interface")
406  << " Only one jet matching/merging scheme can be used at a time. \n";
407  }
408 
409  if (internalMatching) {
410  if (!fJetMatchingPy8InternalHook.get())
411  fJetMatchingPy8InternalHook.reset(new Pythia8::JetMatchingMadgraph);
412  (fUserHooksVector->hooks).push_back(fJetMatchingPy8InternalHook);
413  }
414 
415  if (internalMerging) {
416  int scheme = (fMasterGen->settings.flag("Merging:doUMEPSTree") || fMasterGen->settings.flag("Merging:doUMEPSSubt"))
417  ? 1
418  : ((fMasterGen->settings.flag("Merging:doUNLOPSTree") ||
419  fMasterGen->settings.flag("Merging:doUNLOPSSubt") ||
420  fMasterGen->settings.flag("Merging:doUNLOPSLoop") ||
421  fMasterGen->settings.flag("Merging:doUNLOPSSubtNLO"))
422  ? 2
423  : 0);
424  if (!fMergingHook.get())
425  fMergingHook.reset(new Pythia8::amcnlo_unitarised_interface(scheme));
426  (fUserHooksVector->hooks).push_back(fMergingHook);
427  }
428 
429  bool biasedTauDecayer = fMasterGen->settings.flag("BiasedTauDecayer:filter");
430  if (biasedTauDecayer) {
431  if (!fBiasedTauDecayer.get()) {
432  Pythia8::Info localInfo = fMasterGen->info;
433  fBiasedTauDecayer.reset(new BiasedTauDecayer(&localInfo, &(fMasterGen->settings)));
434  }
435  std::vector<int> handledParticles;
436  handledParticles.push_back(15);
437  fMasterGen->setDecayPtr(fBiasedTauDecayer, handledParticles);
438  }
439 
440  bool resonanceDecayFilter = fMasterGen->settings.flag("ResonanceDecayFilter:filter");
441  if (resonanceDecayFilter) {
443  (fUserHooksVector->hooks).push_back(fResonanceDecayFilterHook);
444  }
445 
446  bool PTFilter = fMasterGen->settings.flag("PTFilter:filter");
447  if (PTFilter) {
448  fPTFilterHook.reset(new PTFilterHook);
449  (fUserHooksVector->hooks).push_back(fPTFilterHook);
450  }
451 
452  if (!(fUserHooksVector->hooks).empty() && !UserHooksSet) {
453  for (auto &fUserHook : fUserHooksVector->hooks) {
454  fMasterGen->addUserHooksPtr(fUserHook);
455  }
456  UserHooksSet = true;
457  }
458 
459  edm::LogInfo("Pythia8Interface") << "Initializing MasterGen";
460  status = fMasterGen->init();
461 
462  //clean up temp file
463  if (!slhafile_.empty()) {
464  std::remove(slhafile_.c_str());
465  }
466 
467  if (pythiaPylistVerbosity > 10) {
468  if (pythiaPylistVerbosity == 11 || pythiaPylistVerbosity == 13)
469  fMasterGen->settings.listAll();
470  if (pythiaPylistVerbosity == 12 || pythiaPylistVerbosity == 13)
471  fMasterGen->particleData.listAll();
472  }
473 
474  // init decayer
475  fDecayer->settings.flag("ProcessLevel:all", false); // trick
476  fDecayer->settings.flag("ProcessLevel:resonanceDecays", true);
477  edm::LogInfo("Pythia8Interface") << "Initializing Decayer";
478  status1 = fDecayer->init();
479 
480  if (useEvtGen) {
481  edm::LogInfo("Pythia8Hadronizer") << "Creating and initializing pythia8 EvtGen plugin";
482  if (!evtgenDecays.get()) {
483  evtgenDecays.reset(new EvtGenDecays(fMasterGen.get(), evtgenDecFile, evtgenPdlFile));
484  for (unsigned int i = 0; i < evtgenUserFiles.size(); i++)
485  evtgenDecays->readDecayFile(evtgenUserFiles.at(i));
486  }
487  }
488 
489  return (status && status1);
490 }
491 
493  edm::LogInfo("Pythia8Interface") << "Initializing for external partons";
494 
495  bool status = false, status1 = false;
496 
497  if (!fUserHooksVector.get())
498  fUserHooksVector.reset(new UserHooksVector);
499  (fUserHooksVector->hooks).clear();
500 
501  if (fReweightUserHook.get())
502  (fUserHooksVector->hooks).push_back(fReweightUserHook);
503  if (fReweightEmpUserHook.get())
504  (fUserHooksVector->hooks).push_back(fReweightEmpUserHook);
505  if (fReweightRapUserHook.get())
506  (fUserHooksVector->hooks).push_back(fReweightRapUserHook);
507  if (fReweightPtHatRapUserHook.get())
508  (fUserHooksVector->hooks).push_back(fReweightPtHatRapUserHook);
509  if (fJetMatchingHook.get())
510  (fUserHooksVector->hooks).push_back(fJetMatchingHook);
511  if (fEmissionVetoHook1.get()) {
512  edm::LogInfo("Pythia8Interface") << "Turning on Emission Veto Hook 1 from CMSSW Pythia8Interface";
513  (fUserHooksVector->hooks).push_back(fEmissionVetoHook1);
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  if (!fEmissionVetoHook.get())
523  fEmissionVetoHook.reset(new PowhegHooks());
524 
525  edm::LogInfo("Pythia8Interface") << "Turning on Emission Veto Hook from pythia8 code";
526  (fUserHooksVector->hooks).push_back(fEmissionVetoHook);
527  }
528 
529  bool PowhegRes = fMasterGen->settings.flag("POWHEGres:calcScales");
530  if (PowhegRes) {
531  edm::LogInfo("Pythia8Interface") << "Turning on resonance scale setting from CMSSW Pythia8Interface";
532  if (!fPowhegResHook.get())
533  fPowhegResHook.reset(new PowhegResHook());
534  (fUserHooksVector->hooks).push_back(fPowhegResHook);
535  }
536 
537  bool PowhegBB4L = fMasterGen->settings.flag("POWHEG:bb4l");
538  if (PowhegBB4L) {
539  edm::LogInfo("Pythia8Interface") << "Turning on BB4l hook from CMSSW Pythia8Interface";
540  if (!fPowhegHooksBB4L.get())
541  fPowhegHooksBB4L.reset(new PowhegHooksBB4L());
542  (fUserHooksVector->hooks).push_back(fPowhegHooksBB4L);
543  }
544 
545  //adapted from main89.cc in pythia8 examples
546  bool internalMatching = fMasterGen->settings.flag("JetMatching:merge");
547  bool internalMerging = !(fMasterGen->settings.word("Merging:Process") == "void");
548 
549  if (internalMatching && internalMerging) {
550  throw edm::Exception(edm::errors::Configuration, "Pythia8Interface")
551  << " Only one jet matching/merging scheme can be used at a time. \n";
552  }
553 
554  if (internalMatching) {
555  if (!fJetMatchingPy8InternalHook.get())
556  fJetMatchingPy8InternalHook.reset(new Pythia8::JetMatchingMadgraph);
557  (fUserHooksVector->hooks).push_back(fJetMatchingPy8InternalHook);
558  }
559 
560  if (internalMerging) {
561  int scheme = (fMasterGen->settings.flag("Merging:doUMEPSTree") || fMasterGen->settings.flag("Merging:doUMEPSSubt"))
562  ? 1
563  : ((fMasterGen->settings.flag("Merging:doUNLOPSTree") ||
564  fMasterGen->settings.flag("Merging:doUNLOPSSubt") ||
565  fMasterGen->settings.flag("Merging:doUNLOPSLoop") ||
566  fMasterGen->settings.flag("Merging:doUNLOPSSubtNLO"))
567  ? 2
568  : 0);
569  if (!fMergingHook.get())
570  fMergingHook.reset(new Pythia8::amcnlo_unitarised_interface(scheme));
571  (fUserHooksVector->hooks).push_back(fMergingHook);
572  }
573 
574  bool biasedTauDecayer = fMasterGen->settings.flag("BiasedTauDecayer:filter");
575  if (biasedTauDecayer) {
576  if (!fBiasedTauDecayer.get()) {
577  Pythia8::Info localInfo = fMasterGen->info;
578  fBiasedTauDecayer.reset(new BiasedTauDecayer(&localInfo, &(fMasterGen->settings)));
579  }
580  std::vector<int> handledParticles;
581  handledParticles.push_back(15);
582  fMasterGen->setDecayPtr(fBiasedTauDecayer, handledParticles);
583  }
584 
585  bool resonanceDecayFilter = fMasterGen->settings.flag("ResonanceDecayFilter:filter");
586  if (resonanceDecayFilter) {
588  (fUserHooksVector->hooks).push_back(fResonanceDecayFilterHook);
589  }
590 
591  bool PTFilter = fMasterGen->settings.flag("PTFilter:filter");
592  if (PTFilter) {
593  fPTFilterHook.reset(new PTFilterHook);
594  (fUserHooksVector->hooks).push_back(fPTFilterHook);
595  }
596 
597  if (!(fUserHooksVector->hooks).empty() && !UserHooksSet) {
598  for (auto &fUserHook : fUserHooksVector->hooks) {
599  fMasterGen->addUserHooksPtr(fUserHook);
600  }
601  UserHooksSet = true;
602  }
603 
604  if (!LHEInputFileName.empty()) {
605  edm::LogInfo("Pythia8Interface") << "Initialize direct pythia8 reading from LHE file " << LHEInputFileName;
606  edm::LogInfo("Pythia8Interface") << "Some LHE information can be not stored";
607  fMasterGen->settings.mode("Beams:frameType", 4);
608  fMasterGen->settings.word("Beams:LHEF", LHEInputFileName);
609  status = fMasterGen->init();
610 
611  } else {
612  lhaUP.reset(new LHAupLesHouches());
613  lhaUP->setScalesFromLHEF(fMasterGen->settings.flag("Beams:setProductionScalesFromLHEF"));
614  lhaUP->loadRunInfo(lheRunInfo());
615 
616  if (fJetMatchingHook.get()) {
617  fJetMatchingHook->init(lheRunInfo());
618  }
619 
620  fMasterGen->settings.mode("Beams:frameType", 5);
621  fMasterGen->setLHAupPtr(lhaUP);
622  edm::LogInfo("Pythia8Interface") << "Initializing MasterGen";
623  status = fMasterGen->init();
624  }
625 
626  //clean up temp file
627  if (!slhafile_.empty()) {
628  std::remove(slhafile_.c_str());
629  }
630 
631  if (pythiaPylistVerbosity > 10) {
632  if (pythiaPylistVerbosity == 11 || pythiaPylistVerbosity == 13)
633  fMasterGen->settings.listAll();
634  if (pythiaPylistVerbosity == 12 || pythiaPylistVerbosity == 13)
635  fMasterGen->particleData.listAll();
636  }
637 
638  // init decayer
639  fDecayer->settings.flag("ProcessLevel:all", false); // trick
640  fDecayer->settings.flag("ProcessLevel:resonanceDecays", true);
641  edm::LogInfo("Pythia8Interface") << "Initializing Decayer";
642  status1 = fDecayer->init();
643 
644  if (useEvtGen) {
645  edm::LogInfo("Pythia8Hadronizer") << "Creating and initializing pythia8 EvtGen plugin";
646  if (!evtgenDecays.get()) {
647  evtgenDecays.reset(new EvtGenDecays(fMasterGen.get(), evtgenDecFile, evtgenPdlFile));
648  for (unsigned int i = 0; i < evtgenUserFiles.size(); i++)
649  evtgenDecays->readDecayFile(evtgenUserFiles.at(i));
650  }
651  }
652 
653  return (status && status1);
654 }
655 
657  fMasterGen->stat();
658 
659  if (fEmissionVetoHook.get()) {
660  edm::LogPrint("Pythia8Interface") << "\n"
661  << "Number of ISR vetoed = " << nISRveto;
662  edm::LogPrint("Pythia8Interface") << "Number of FSR vetoed = " << nFSRveto;
663  }
664 
665  double xsec = fMasterGen->info.sigmaGen(); // cross section in mb
666  xsec *= 1.0e9; // translate to pb (CMS/Gen "convention" as of May 2009)
667  double err = fMasterGen->info.sigmaErr(); // cross section err in mb
668  err *= 1.0e9; // translate to pb (CMS/Gen "convention" as of May 2009)
670 }
671 
673  DJR.resize(0);
674  nME = -1;
675  nMEFiltered = -1;
676 
677  if (fJetMatchingHook.get()) {
678  fJetMatchingHook->resetMatchingStatus();
679  fJetMatchingHook->beforeHadronization(lheEvent());
680  }
681 
682  if (!fMasterGen->next())
683  return false;
684 
685  double mergeweight = fMasterGen.get()->info.mergingWeightNLO();
686  if (fMergingHook.get()) {
687  mergeweight *= fMergingHook->getNormFactor();
688  }
689 
690  //protect against 0-weight from ckkw or similar
691  if (std::abs(mergeweight) == 0.) {
692  event().reset();
693  return false;
694  }
695 
696  if (fJetMatchingPy8InternalHook.get()) {
697  const std::vector<double> djrmatch = fJetMatchingPy8InternalHook->getDJR();
698  //cap size of djr vector to save storage space (keep only up to first 6 elements)
699  unsigned int ndjr = std::min(djrmatch.size(), std::vector<double>::size_type(6));
700  for (unsigned int idjr = 0; idjr < ndjr; ++idjr) {
701  DJR.push_back(djrmatch[idjr]);
702  }
703 
704  nME = fJetMatchingPy8InternalHook->nMEpartons().first;
705  nMEFiltered = fJetMatchingPy8InternalHook->nMEpartons().second;
706  }
707 
708  if (evtgenDecays.get())
709  evtgenDecays->decay();
710 
711  event() = std::make_unique<HepMC::GenEvent>();
712  bool py8hepmc = toHepMC.fill_next_event(*(fMasterGen.get()), event().get());
713 
714  if (!py8hepmc) {
715  return false;
716  }
717 
718  // 0th weight is not filled anymore since v8.30x, pushback manually
719  event()->weights().push_back(fMasterGen->info.weight());
720 
721  //add ckkw/umeps/unlops merging weight
722  if (mergeweight != 1.) {
723  event()->weights()[0] *= mergeweight;
724  }
725 
726  if (fEmissionVetoHook.get()) {
727  nISRveto += fEmissionVetoHook->getNISRveto();
728  nFSRveto += fEmissionVetoHook->getNFSRveto();
729  }
730 
731  //fill additional weights for systematic uncertainties
732  if (fMasterGen->info.getWeightsDetailedSize() > 0) {
733  for (const string &key : fMasterGen->info.initrwgt->weightsKeys) {
734  double wgt = (*fMasterGen->info.weights_detailed)[key];
735  event()->weights().push_back(wgt);
736  }
737  } else if (fMasterGen->info.getWeightsCompressedSize() > 0) {
738  for (unsigned int i = 0; i < fMasterGen->info.getWeightsCompressedSize(); i++) {
739  double wgt = fMasterGen->info.getWeightsCompressedValue(i);
740  event()->weights().push_back(wgt);
741  }
742  }
743 
744  // fill shower weights
745  // http://home.thep.lu.se/~torbjorn/pythia82html/Variations.html
746  if (fMasterGen->info.nWeights() > 1) {
747  for (int i = 0; i < fMasterGen->info.nWeights(); ++i) {
748  double wgt = fMasterGen->info.weight(i);
749  event()->weights().push_back(wgt);
750  }
751  }
752 
753 #if 0
754  // VINCIA shower weights
755  // http://vincia.hepforge.org/current/share/Vincia/htmldoc/VinciaUncertainties.html
756  if (fvincia.get()) {
757  event()->weights()[0] *= fvincia->weight(0);
758  for (int iVar = 1; iVar < fvincia->nWeights(); iVar++) {
759  event()->weights().push_back(fvincia->weight(iVar));
760  }
761  }
762 
763  // Retrieve Dire shower weights
764  if (fDire.get()) {
765  fDire->weightsPtr->calcWeight(0.);
766  fDire->weightsPtr->reset();
767 
768  //Make sure the base weight comes first
769  event()->weights()[0] *= fDire->weightsPtr->getShowerWeight("base");
770 
771  unordered_map<string, double>::iterator it;
772  for (it = fDire->weightsPtr->getShowerWeights()->begin(); it != fDire->weightsPtr->getShowerWeights()->end();
773  it++) {
774  if (it->first == "base")
775  continue;
776  event()->weights().push_back(it->second);
777  }
778  }
779 #endif
780 
781  return true;
782 }
783 
785  DJR.resize(0);
786  nME = -1;
787  nMEFiltered = -1;
788  if (LHEInputFileName.empty())
789  lhaUP->loadEvent(lheEvent());
790 
791  if (fJetMatchingHook.get()) {
792  fJetMatchingHook->resetMatchingStatus();
793  fJetMatchingHook->beforeHadronization(lheEvent());
794  }
795 
796  bool py8next = fMasterGen->next();
797 
798  double mergeweight = fMasterGen.get()->info.mergingWeightNLO();
799  if (fMergingHook.get()) {
800  mergeweight *= fMergingHook->getNormFactor();
801  }
802 
803  //protect against 0-weight from ckkw or similar
804  if (!py8next || std::abs(mergeweight) == 0.) {
805  lheEvent()->count(lhef::LHERunInfo::kSelected, 1.0, mergeweight);
806  event().reset();
807  return false;
808  }
809 
810  if (fJetMatchingPy8InternalHook.get()) {
811  const std::vector<double> djrmatch = fJetMatchingPy8InternalHook->getDJR();
812  //cap size of djr vector to save storage space (keep only up to first 6 elements)
813  unsigned int ndjr = std::min(djrmatch.size(), std::vector<double>::size_type(6));
814  for (unsigned int idjr = 0; idjr < ndjr; ++idjr) {
815  DJR.push_back(djrmatch[idjr]);
816  }
817 
818  nME = fJetMatchingPy8InternalHook->nMEpartons().first;
819  nMEFiltered = fJetMatchingPy8InternalHook->nMEpartons().second;
820  }
821 
822  // update LHE matching statistics
823  //
824  lheEvent()->count(lhef::LHERunInfo::kAccepted, 1.0, mergeweight);
825 
826  if (evtgenDecays.get())
827  evtgenDecays->decay();
828 
829  event() = std::make_unique<HepMC::GenEvent>();
830  bool py8hepmc = toHepMC.fill_next_event(*(fMasterGen.get()), event().get());
831  if (!py8hepmc) {
832  return false;
833  }
834 
835  // 0th weight is not filled anymore since v8.30x, pushback manually
836  event()->weights().push_back(fMasterGen->info.weight());
837 
838  //add ckkw/umeps/unlops merging weight
839  if (mergeweight != 1.) {
840  event()->weights()[0] *= mergeweight;
841  }
842 
843  if (fEmissionVetoHook.get()) {
844  nISRveto += fEmissionVetoHook->getNISRveto();
845  nFSRveto += fEmissionVetoHook->getNFSRveto();
846  }
847 
848  // fill shower weights
849  // http://home.thep.lu.se/~torbjorn/pythia82html/Variations.html
850  if (fMasterGen->info.nWeights() > 1) {
851  for (int i = 0; i < fMasterGen->info.nWeights(); ++i) {
852  double wgt = fMasterGen->info.weight(i);
853  event()->weights().push_back(wgt);
854  }
855  }
856 
857  return true;
858 }
859 
861  Event *pythiaEvent = &(fMasterGen->event);
862 
863  int NPartsBeforeDecays = pythiaEvent->size();
864  int NPartsAfterDecays = event().get()->particles_size();
865 
866  if (NPartsAfterDecays == NPartsBeforeDecays)
867  return true;
868 
869  bool result = true;
870 
871  for (int ipart = NPartsAfterDecays; ipart > NPartsBeforeDecays; ipart--) {
872  HepMC::GenParticle *part = event().get()->barcode_to_particle(ipart);
873 
874  if (part->status() == 1 && (fDecayer->particleData).canDecay(part->pdg_id())) {
875  fDecayer->event.reset();
876  Particle py8part(part->pdg_id(),
877  93,
878  0,
879  0,
880  0,
881  0,
882  0,
883  0,
884  part->momentum().x(),
885  part->momentum().y(),
886  part->momentum().z(),
887  part->momentum().t(),
888  part->generated_mass());
889  HepMC::GenVertex *ProdVtx = part->production_vertex();
890  py8part.vProd(ProdVtx->position().x(), ProdVtx->position().y(), ProdVtx->position().z(), ProdVtx->position().t());
891  py8part.tau((fDecayer->particleData).tau0(part->pdg_id()));
892  fDecayer->event.append(py8part);
893  int nentries = fDecayer->event.size();
894  if (!fDecayer->event[nentries - 1].mayDecay())
895  continue;
896  fDecayer->next();
897  int nentries1 = fDecayer->event.size();
898  if (nentries1 <= nentries)
899  continue; //same number of particles, no decays...
900 
901  part->set_status(2);
902 
903  result = toHepMC.fill_next_event(*(fDecayer.get()), event().get(), -1, true, part);
904  }
905  }
906 
907  return result;
908 }
909 
911  bool lhe = lheEvent() != nullptr;
912 
913  // protection against empty weight container
914  if ((event()->weights()).empty())
915  (event()->weights()).push_back(1.);
916 
917  // now create the GenEventInfo product from the GenEvent and fill
918  // the missing pieces
919  eventInfo() = std::make_unique<GenEventInfoProduct>(event().get());
920 
921  // in pythia pthat is used to subdivide samples into different bins
922  // in LHE mode the binning is done by the external ME generator
923  // which is likely not pthat, so only filling it for Py6 internal mode
924  if (!lhe) {
925  eventInfo()->setBinningValues(std::vector<double>(1, fMasterGen->info.pTHat()));
926  }
927 
928  eventInfo()->setDJR(DJR);
929  eventInfo()->setNMEPartons(nME);
930  eventInfo()->setNMEPartonsFiltered(nMEFiltered);
931 
932  //******** Verbosity ********
933 
936  if (pythiaPylistVerbosity) {
937  fMasterGen->info.list();
938  fMasterGen->event.list();
939  }
940 
941  if (pythiaHepMCVerbosity) {
942  std::cout << "Event process = " << fMasterGen->info.code() << "\n"
943  << "----------------------" << std::endl;
944  event()->print();
945  }
947  std::cout << "Event process = " << fMasterGen->info.code() << "\n"
948  << "----------------------" << std::endl;
949  ascii_io->write_event(event().get());
950  }
951  }
952 }
953 
954 std::unique_ptr<GenLumiInfoHeader> Pythia8Hadronizer::getGenLumiInfoHeader() const {
955  auto genLumiInfoHeader = BaseHadronizer::getGenLumiInfoHeader();
956 
957  //fill lhe headers
958  //*FIXME* initrwgt header is corrupt due to pythia bug
959  for (const std::string &key : fMasterGen->info.headerKeys()) {
960  genLumiInfoHeader->lheHeaders().emplace_back(key, fMasterGen->info.header(key));
961  }
962 
963  //check, if it is not only nominal weight
964  int weights_number = fMasterGen->info.nWeights();
965  if (fMasterGen->info.initrwgt)
966  weights_number += fMasterGen->info.initrwgt->weightsKeys.size();
967  if (weights_number > 1) {
968  genLumiInfoHeader->weightNames().reserve(weights_number + 1);
969  genLumiInfoHeader->weightNames().push_back("nominal");
970  }
971 
972  //fill weight names
973  if (fMasterGen->info.initrwgt) {
974  for (const std::string &key : fMasterGen->info.initrwgt->weightsKeys) {
975  std::string weightgroupname;
976  for (const auto &wgtgrp : fMasterGen->info.initrwgt->weightgroups) {
977  const auto &wgtgrpwgt = wgtgrp.second.weights.find(key);
978  if (wgtgrpwgt != wgtgrp.second.weights.end()) {
979  weightgroupname = wgtgrp.first;
980  }
981  }
982 
983  std::ostringstream weightname;
984  weightname << "LHE, id = " << key << ", ";
985  if (!weightgroupname.empty()) {
986  weightname << "group = " << weightgroupname << ", ";
987  }
988  weightname << fMasterGen->info.initrwgt->weights[key].contents;
989  genLumiInfoHeader->weightNames().push_back(weightname.str());
990  }
991  }
992 
993  //fill shower labels
994  // http://home.thep.lu.se/~torbjorn/pythia82html/Variations.html
995  // http://home.thep.lu.se/~torbjorn/doxygen/classPythia8_1_1Info.html
996  if (fMasterGen->info.nWeights() > 1) {
997  for (int i = 0; i < fMasterGen->info.nWeights(); ++i) {
998  genLumiInfoHeader->weightNames().push_back(fMasterGen->info.weightLabel(i));
999  }
1000  }
1001 
1002 #if 0
1003  // VINCIA shower weights
1004  // http://vincia.hepforge.org/current/share/Vincia/htmldoc/VinciaUncertainties.html
1005  if (fvincia.get()) {
1006  for (int iVar = 0; iVar < fvincia->nWeights(); iVar++) {
1007  genLumiInfoHeader->weightNames().push_back(fvincia->weightLabel(iVar));
1008  }
1009  }
1010 
1011  if (fDire.get()) {
1012  //Make sure the base weight comes first
1013  genLumiInfoHeader->weightNames().push_back("base");
1014 
1015  unordered_map<string, double>::iterator it;
1016  for (it = fDire->weightsPtr->getShowerWeights()->begin(); it != fDire->weightsPtr->getShowerWeights()->end();
1017  it++) {
1018  if (it->first == "base")
1019  continue;
1020  genLumiInfoHeader->weightNames().push_back(it->first);
1021  }
1022  }
1023 #endif
1024 
1025  return genLumiInfoHeader;
1026 }
1027 
1030 
1033 
1037 
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