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