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