test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 //decay filter hook
35 
36 // EvtGen plugin
37 //
38 #include "Pythia8Plugins/EvtGen.h"
39 
45 
48 
51 
53 
54 #include "HepPID/ParticleIDTranslations.hh"
55 
57 
58 namespace CLHEP {
59  class HepRandomEngine;
60 }
61 
62 using namespace gen;
63 
64 
66 
67  public:
68 
69  Pythia8Hadronizer(const edm::ParameterSet &params);
71 
72  bool initializeForInternalPartons() override;
73  bool initializeForExternalPartons();
74 
75  bool generatePartonsAndHadronize() override;
76  bool hadronize();
77 
78  virtual bool residualDecay();
79 
80  void finalizeEvent() override;
81 
82  void statistics() override;
83 
84  const char *classname() const override { return "Pythia8Hadronizer"; }
85 
86  GenLumiInfoHeader *getGenLumiInfoHeader() const override;
87 
88  private:
89 
90  virtual void doSetRandomEngine(CLHEP::HepRandomEngine* v) override { p8SetRandomEngine(v); }
91  virtual std::vector<std::string> const& doSharedResources() const override { return p8SharedResources; }
92 
94  double comEnergy;
95 
97  std::auto_ptr<LHAupLesHouches> lhaUP;
98 
99  enum { PP, PPbar, ElectronPositron };
100  int fInitialState ; // pp, ppbar, or e-e+
101 
102  double fBeam1PZ;
103  double fBeam2PZ;
104 
105  //helper class to allow multiple user hooks simultaneously
106  std::auto_ptr<MultiUserHook> fMultiUserHook;
107 
108  // Reweight user hooks
109  //
110  std::auto_ptr<UserHooks> fReweightUserHook;
111  std::auto_ptr<UserHooks> fReweightRapUserHook;
112  std::auto_ptr<UserHooks> fReweightPtHatRapUserHook;
113 
114  // PS matching prototype
115  //
116  std::auto_ptr<JetMatchingHook> fJetMatchingHook;
117  std::auto_ptr<Pythia8::JetMatchingMadgraph> fJetMatchingPy8InternalHook;
118  std::auto_ptr<Pythia8::amcnlo_unitarised_interface> fMergingHook;
119 
120  // Emission Veto Hooks
121  //
122  std::auto_ptr<PowhegHooks> fEmissionVetoHook;
123  std::auto_ptr<EmissionVetoHook1> fEmissionVetoHook1;
124 
125  //resonance decay filter hook
126  std::auto_ptr<ResonanceDecayFilterHook> fResonanceDecayFilterHook;
127 
136 
137  static const std::vector<std::string> p8SharedResources;
138 
139  vector<float> DJR;
140  int nME;
142 
143  int nISRveto;
144  int nFSRveto;
145 
146  std::vector<std::string> fSortedWeightKeys;
147 
148 };
149 
151 
153  Py8InterfaceBase(params),
154  comEnergy(params.getParameter<double>("comEnergy")),
155  LHEInputFileName(params.getUntrackedParameter<std::string>("LHEInputFileName","")),
156  fInitialState(PP),
157  nME(-1), nMEFiltered(-1), nISRveto(0), nFSRveto(0)
158 {
159 
160  // J.Y.: the following 3 parameters are hacked "for a reason"
161  //
162  if ( params.exists( "PPbarInitialState" ) )
163  {
164  if ( fInitialState == PP )
165  {
167  edm::LogImportant("GeneratorInterface|Pythia8Interface")
168  << "Pythia8 will be initialized for PROTON-ANTIPROTON INITIAL STATE. "
169  << "This is a user-request change from the DEFAULT PROTON-PROTON initial state.";
170  }
171  else
172  {
173  // probably need to throw on attempt to override ?
174  }
175  }
176  else if ( params.exists( "ElectronPositronInitialState" ) )
177  {
178  if ( fInitialState == PP )
179  {
181  edm::LogInfo("GeneratorInterface|Pythia8Interface")
182  << "Pythia8 will be initialized for ELECTRON-POSITRON 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( "ElectronProtonInitialState" ) || params.exists( "PositronProtonInitialState" ) )
191  {
192  // throw on unknown initial state !
193  throw edm::Exception(edm::errors::Configuration,"Pythia8Interface")
194  <<" UNKNOWN INITIAL STATE. \n The allowed initial states are: PP, PPbar, ElectronPositron \n";
195  }
196 
197  // Reweight user hook
198  //
199  if( params.exists( "reweightGen" ) )
201  if( params.exists( "reweightGenRap" ) )
202  {
203  edm::LogInfo("Pythia8Interface") << "Start setup for reweightGenRap";
204  edm::ParameterSet rgrParams =
205  params.getParameter<edm::ParameterSet>("reweightGenRap");
206  fReweightRapUserHook.reset(
207  new RapReweightUserHook(rgrParams.getParameter<std::string>("yLabSigmaFunc"),
208  rgrParams.getParameter<double>("yLabPower"),
209  rgrParams.getParameter<std::string>("yCMSigmaFunc"),
210  rgrParams.getParameter<double>("yCMPower"),
211  rgrParams.getParameter<double>("pTHatMin"),
212  rgrParams.getParameter<double>("pTHatMax"))
213  );
214  edm::LogInfo("Pythia8Interface") << "End setup for reweightGenRap";
215  }
216  if( params.exists( "reweightGenPtHatRap" ) )
217  {
218  edm::LogInfo("Pythia8Interface") << "Start setup for reweightGenPtHatRap";
219  edm::ParameterSet rgrParams =
220  params.getParameter<edm::ParameterSet>("reweightGenPtHatRap");
222  new PtHatRapReweightUserHook(rgrParams.getParameter<std::string>("yLabSigmaFunc"),
223  rgrParams.getParameter<double>("yLabPower"),
224  rgrParams.getParameter<std::string>("yCMSigmaFunc"),
225  rgrParams.getParameter<double>("yCMPower"),
226  rgrParams.getParameter<double>("pTHatMin"),
227  rgrParams.getParameter<double>("pTHatMax"))
228  );
229  edm::LogInfo("Pythia8Interface") << "End setup for reweightGenPtHatRap";
230  }
231 
232  if( params.exists( "useUserHook" ) )
233  throw edm::Exception(edm::errors::Configuration,"Pythia8Interface")
234  <<" Obsolete parameter: useUserHook \n Please use the actual one instead \n";
235 
236  // PS matching prototype
237  //
238  if ( params.exists("jetMatching") )
239  {
240  edm::ParameterSet jmParams =
241  params.getUntrackedParameter<edm::ParameterSet>("jetMatching");
242  std::string scheme = jmParams.getParameter<std::string>("scheme");
243  if ( scheme == "Madgraph" || scheme == "MadgraphFastJet" )
244  {
245  fJetMatchingHook.reset(new JetMatchingHook( jmParams, &fMasterGen->info ));
246  }
247  }
248 
249  // Pythia8Interface emission veto
250  //
251  if ( params.exists("emissionVeto1") )
252  {
253  EV1_nFinal = -1;
254  if(params.exists("EV1_nFinal")) EV1_nFinal = params.getParameter<int>("EV1_nFinal");
255  EV1_vetoOn = true;
256  if(params.exists("EV1_vetoOn")) EV1_vetoOn = params.getParameter<bool>("EV1_vetoOn");
257  EV1_maxVetoCount = 10;
258  if(params.exists("EV1_maxVetoCount")) EV1_maxVetoCount = params.getParameter<int>("EV1_maxVetoCount");
259  EV1_pThardMode = 1;
260  if(params.exists("EV1_pThardMode")) EV1_pThardMode = params.getParameter<int>("EV1_pThardMode");
261  EV1_pTempMode = 0;
262  if(params.exists("EV1_pTempMode")) EV1_pTempMode = params.getParameter<int>("EV1_pTempMode");
263  if(EV1_pTempMode > 2 || EV1_pTempMode < 0)
264  throw edm::Exception(edm::errors::Configuration,"Pythia8Interface")
265  <<" Wrong value for EV1_pTempMode code\n";
266  EV1_emittedMode = 0;
267  if(params.exists("EV1_emittedMode")) EV1_emittedMode = params.getParameter<int>("EV1_emittedMode");
268  EV1_pTdefMode = 1;
269  if(params.exists("EV1_pTdefMode")) EV1_pTdefMode = params.getParameter<int>("EV1_pTdefMode");
270  EV1_MPIvetoOn = false;
271  if(params.exists("EV1_MPIvetoOn")) EV1_MPIvetoOn = params.getParameter<bool>("EV1_MPIvetoOn");
275  }
276 
277 }
278 
279 
281 {
282 
283 }
284 
286 {
287 
288  bool status = false, status1 = false;
289 
290  if (lheFile_.empty()) {
291  if ( fInitialState == PP ) // default
292  {
293  fMasterGen->settings.mode("Beams:idA", 2212);
294  fMasterGen->settings.mode("Beams:idB", 2212);
295  }
296  else if ( fInitialState == PPbar )
297  {
298  fMasterGen->settings.mode("Beams:idA", 2212);
299  fMasterGen->settings.mode("Beams:idB", -2212);
300  }
301  else if ( fInitialState == ElectronPositron )
302  {
303  fMasterGen->settings.mode("Beams:idA", 11);
304  fMasterGen->settings.mode("Beams:idB", -11);
305  }
306  else
307  {
308  // throw on unknown initial state !
309  throw edm::Exception(edm::errors::Configuration,"Pythia8Interface")
310  <<" UNKNOWN INITIAL STATE. \n The allowed initial states are: PP, PPbar, ElectronPositron \n";
311  }
312  fMasterGen->settings.parm("Beams:eCM", comEnergy);
313  }
314  else {
315  fMasterGen->settings.mode("Beams:frameType", 4);
316  fMasterGen->settings.word("Beams:LHEF", lheFile_);
317  }
318 
319  fMultiUserHook.reset(new MultiUserHook);
320 
321  if(fReweightUserHook.get()) fMultiUserHook->addHook(fReweightUserHook.get());
322  if(fReweightRapUserHook.get()) fMultiUserHook->addHook(fReweightRapUserHook.get());
324  if(fJetMatchingHook.get()) fMultiUserHook->addHook(fJetMatchingHook.get());
325  if(fEmissionVetoHook1.get()) {
326  edm::LogInfo("Pythia8Interface") << "Turning on Emission Veto Hook 1 from CMSSW Pythia8Interface";
327  fMultiUserHook->addHook(fEmissionVetoHook1.get());
328  }
329 
330  if (fMasterGen->settings.mode("POWHEG:veto") > 0 || fMasterGen->settings.mode("POWHEG:MPIveto") > 0) {
331 
332  if(fJetMatchingHook.get() || fEmissionVetoHook1.get())
333  throw edm::Exception(edm::errors::Configuration,"Pythia8Interface")
334  <<" Attempt to turn on PowhegHooks by pythia8 settings but there are incompatible hooks on \n Incompatible are : jetMatching, emissionVeto1 \n";
335 
336  fEmissionVetoHook.reset(new PowhegHooks());
337 
338  edm::LogInfo("Pythia8Interface") << "Turning on Emission Veto Hook from pythia8 code";
339  fMultiUserHook->addHook(fEmissionVetoHook.get());
340  }
341 
342  //adapted from main89.cc in pythia8 examples
343  bool internalMatching = fMasterGen->settings.flag("JetMatching:merge");
344  bool internalMerging = !(fMasterGen->settings.word("Merging:Process").compare("void")==0);
345 
346  if (internalMatching && internalMerging) {
347  throw edm::Exception(edm::errors::Configuration,"Pythia8Interface")
348  <<" Only one jet matching/merging scheme can be used at a time. \n";
349  }
350 
351  if (internalMatching) {
352  fJetMatchingPy8InternalHook.reset(new Pythia8::JetMatchingMadgraph);
354  }
355 
356  if (internalMerging) {
357  int scheme = ( fMasterGen->settings.flag("Merging:doUMEPSTree")
358  || fMasterGen->settings.flag("Merging:doUMEPSSubt")) ?
359  1 :
360  ( ( fMasterGen->settings.flag("Merging:doUNLOPSTree")
361  || fMasterGen->settings.flag("Merging:doUNLOPSSubt")
362  || fMasterGen->settings.flag("Merging:doUNLOPSLoop")
363  || fMasterGen->settings.flag("Merging:doUNLOPSSubtNLO")) ?
364  2 :
365  0 );
366  fMergingHook.reset(new Pythia8::amcnlo_unitarised_interface(scheme));
367  fMultiUserHook->addHook(fMergingHook.get());
368  }
369 
370  bool resonanceDecayFilter = fMasterGen->settings.flag("ResonanceDecayFilter:filter");
371  if (resonanceDecayFilter) {
374  }
375 
376  if (fMultiUserHook->nHooks()>0) {
377  fMasterGen->setUserHooksPtr(fMultiUserHook.get());
378  }
379 
380  edm::LogInfo("Pythia8Interface") << "Initializing MasterGen";
381  status = fMasterGen->init();
382 
383  //clean up temp file
384  if (!slhafile_.empty()) {
385  std::remove(slhafile_.c_str());
386  }
387 
388  if ( pythiaPylistVerbosity > 10 )
389  {
390  if ( pythiaPylistVerbosity == 11 || pythiaPylistVerbosity == 13 )
391  fMasterGen->settings.listAll();
392  if ( pythiaPylistVerbosity == 12 || pythiaPylistVerbosity == 13 )
393  fMasterGen->particleData.listAll();
394  }
395 
396  // init decayer
397  fDecayer->settings.flag("ProcessLevel:all", false ); // trick
398  fDecayer->settings.flag("ProcessLevel:resonanceDecays", true );
399  edm::LogInfo("Pythia8Interface") << "Initializing Decayer";
400  status1 = fDecayer->init();
401 
402  if (useEvtGen) {
403  edm::LogInfo("Pythia8Interface") << "Creating and initializing pythia8 EvtGen plugin";
404 
405  evtgenDecays.reset(new EvtGenDecays(fMasterGen.get(), evtgenDecFile.c_str(), evtgenPdlFile.c_str()));
406 
407  for (unsigned int i=0; i<evtgenUserFiles.size(); i++) {
408  edm::FileInPath evtgenUserFile(evtgenUserFiles.at(i));
409  evtgenDecays->readDecayFile(evtgenUserFile.fullPath().c_str());
410  }
411 
412  }
413 
414  //keep track of lhe weights
415  //*FIXME* Sort them numerically since pythia does not preserve the original order
416  //This will fail if weight names are not parseable as integers/
417  //To be improved with future pythia release
418  fSortedWeightKeys.clear();
419  if (fMasterGen->info.initrwgt) {
420  fSortedWeightKeys.reserve(fMasterGen->info.initrwgt->weights.size());
421 
422  std::vector<std::pair<int,std::string> > fWeightKeysTmp;
423  fWeightKeysTmp.reserve(fMasterGen->info.initrwgt->weights.size());
424 
425  for (const auto &wgt : fMasterGen->info.initrwgt->weights) {
426  fWeightKeysTmp.emplace_back(std::stoi(wgt.first),wgt.first);
427  }
428 
429  std::sort(fWeightKeysTmp.begin(),fWeightKeysTmp.end());
430 
431  for (const auto &wgt : fWeightKeysTmp) {
432  fSortedWeightKeys.push_back(wgt.second);
433  }
434  }
435 
436  return (status&&status1);
437 }
438 
439 
441 {
442 
443  edm::LogInfo("Pythia8Interface") << "Initializing for external partons";
444 
445  bool status = false, status1 = false;
446 
447  fMultiUserHook.reset(new MultiUserHook);
448 
449  if(fReweightUserHook.get()) fMultiUserHook->addHook(fReweightUserHook.get());
450  if(fReweightRapUserHook.get()) fMultiUserHook->addHook(fReweightRapUserHook.get());
452  if(fJetMatchingHook.get()) fMultiUserHook->addHook(fJetMatchingHook.get());
453  if(fEmissionVetoHook1.get()) {
454  edm::LogInfo("Pythia8Interface") << "Turning on Emission Veto Hook 1 from CMSSW Pythia8Interface";
455  fMultiUserHook->addHook(fEmissionVetoHook1.get());
456  }
457 
458  if (fMasterGen->settings.mode("POWHEG:veto") > 0 || fMasterGen->settings.mode("POWHEG:MPIveto") > 0) {
459 
460  if(fJetMatchingHook.get() || fEmissionVetoHook1.get())
461  throw edm::Exception(edm::errors::Configuration,"Pythia8Interface")
462  <<" Attempt to turn on PowhegHooks by pythia8 settings but there are incompatible hooks on \n Incompatible are : jetMatching, emissionVeto1 \n";
463 
464  fEmissionVetoHook.reset(new PowhegHooks());
465 
466  edm::LogInfo("Pythia8Interface") << "Turning on Emission Veto Hook from pythia8 code";
467  fMultiUserHook->addHook(fEmissionVetoHook.get());
468  }
469 
470  //adapted from main89.cc in pythia8 examples
471  bool internalMatching = fMasterGen->settings.flag("JetMatching:merge");
472  bool internalMerging = !(fMasterGen->settings.word("Merging:Process").compare("void")==0);
473 
474  if (internalMatching && internalMerging) {
475  throw edm::Exception(edm::errors::Configuration,"Pythia8Interface")
476  <<" Only one jet matching/merging scheme can be used at a time. \n";
477  }
478 
479  if (internalMatching) {
480  fJetMatchingPy8InternalHook.reset(new Pythia8::JetMatchingMadgraph);
482  }
483 
484  if (internalMerging) {
485  int scheme = ( fMasterGen->settings.flag("Merging:doUMEPSTree")
486  || fMasterGen->settings.flag("Merging:doUMEPSSubt")) ?
487  1 :
488  ( ( fMasterGen->settings.flag("Merging:doUNLOPSTree")
489  || fMasterGen->settings.flag("Merging:doUNLOPSSubt")
490  || fMasterGen->settings.flag("Merging:doUNLOPSLoop")
491  || fMasterGen->settings.flag("Merging:doUNLOPSSubtNLO")) ?
492  2 :
493  0 );
494  fMergingHook.reset(new Pythia8::amcnlo_unitarised_interface(scheme));
495  fMultiUserHook->addHook(fMergingHook.get());
496  }
497 
498  bool resonanceDecayFilter = fMasterGen->settings.flag("ResonanceDecayFilter:filter");
499  if (resonanceDecayFilter) {
502  }
503 
504  if (fMultiUserHook->nHooks()>0) {
505  fMasterGen->setUserHooksPtr(fMultiUserHook.get());
506  }
507 
508  if(LHEInputFileName != std::string()) {
509 
510  edm::LogInfo("Pythia8Interface") << "Initialize direct pythia8 reading from LHE file "
511  << LHEInputFileName;
512  edm::LogInfo("Pythia8Interface") << "Some LHE information can be not stored";
513  fMasterGen->settings.mode("Beams:frameType", 4);
514  fMasterGen->settings.word("Beams:LHEF", LHEInputFileName);
515  status = fMasterGen->init();
516 
517  } else {
518 
519  lhaUP.reset(new LHAupLesHouches());
520  lhaUP->setScalesFromLHEF(fMasterGen->settings.flag("Beams:setProductionScalesFromLHEF"));
521  lhaUP->loadRunInfo(lheRunInfo());
522 
523  if ( fJetMatchingHook.get() )
524  {
525  fJetMatchingHook->init ( lheRunInfo() );
526  }
527 
528  fMasterGen->settings.mode("Beams:frameType", 5);
529  fMasterGen->setLHAupPtr(lhaUP.get());
530  edm::LogInfo("Pythia8Interface") << "Initializing MasterGen";
531  status = fMasterGen->init();
532  }
533 
534  //clean up temp file
535  if (!slhafile_.empty()) {
536  std::remove(slhafile_.c_str());
537  }
538 
539  if ( pythiaPylistVerbosity > 10 )
540  {
541  if ( pythiaPylistVerbosity == 11 || pythiaPylistVerbosity == 13 )
542  fMasterGen->settings.listAll();
543  if ( pythiaPylistVerbosity == 12 || pythiaPylistVerbosity == 13 )
544  fMasterGen->particleData.listAll();
545  }
546 
547  // init decayer
548  fDecayer->settings.flag("ProcessLevel:all", false ); // trick
549  fDecayer->settings.flag("ProcessLevel:resonanceDecays", true );
550  edm::LogInfo("Pythia8Interface") << "Initializing Decayer";
551  status1 = fDecayer->init();
552 
553  if (useEvtGen) {
554  edm::LogInfo("Pythia8Interface") << "Creating and initializing pythia8 EvtGen plugin";
555 
556  std::string evtgenpath(getenv("EVTGENDATA"));
557  evtgenDecays.reset(new EvtGenDecays(fMasterGen.get(), evtgenDecFile.c_str(), evtgenPdlFile.c_str()));
558 
559  for (unsigned int i=0; i<evtgenUserFiles.size(); i++) {
560  edm::FileInPath evtgenUserFile(evtgenUserFiles.at(i));
561  evtgenDecays->readDecayFile(evtgenUserFile.fullPath().c_str());
562  }
563 
564  }
565 
566  return (status&&status1);
567 }
568 
569 
571 {
572  fMasterGen->stat();
573 
574  if(fEmissionVetoHook.get()) {
575  edm::LogPrint("Pythia8Interface") << "\n"
576  << "Number of ISR vetoed = " << nISRveto;
577  edm::LogPrint("Pythia8Interface")
578  << "Number of FSR vetoed = " << nFSRveto;
579  }
580 
581  double xsec = fMasterGen->info.sigmaGen(); // cross section in mb
582  xsec *= 1.0e9; // translate to pb (CMS/Gen "convention" as of May 2009)
583  double err = fMasterGen->info.sigmaErr(); // cross section err in mb
584  err *= 1.0e9; // translate to pb (CMS/Gen "convention" as of May 2009)
586 }
587 
588 
590 {
591 
592  DJR.resize(0);
593  nME = -1;
594  nMEFiltered = -1;
595 
596  if ( fJetMatchingHook.get() )
597  {
598  fJetMatchingHook->resetMatchingStatus();
599  fJetMatchingHook->beforeHadronization( lheEvent() );
600  }
601 
602  if (!fMasterGen->next()) return false;
603 
604  double mergeweight = fMasterGen.get()->info.mergingWeightNLO();
605  if (fMergingHook.get()) {
606  mergeweight *= fMergingHook->getNormFactor();
607  }
608 
609  //protect against 0-weight from ckkw or similar
610  if (std::abs(mergeweight)==0.)
611  {
612  event().reset();
613  return false;
614  }
615 
616  if (fJetMatchingPy8InternalHook.get()) {
617  const std::vector<double> djrmatch = fJetMatchingPy8InternalHook->getDJR();
618  //cap size of djr vector to save storage space (keep only up to first 6 elements)
619  unsigned int ndjr = std::min(djrmatch.size(), std::vector<double>::size_type(6));
620  for (unsigned int idjr=0; idjr<ndjr; ++idjr) {
621  DJR.push_back(djrmatch[idjr]);
622  }
623 
624  nME=fJetMatchingPy8InternalHook->nMEpartons().first;
625  nMEFiltered=fJetMatchingPy8InternalHook->nMEpartons().second;
626  }
627 
628  if (evtgenDecays.get()) evtgenDecays->decay();
629 
630  event().reset(new HepMC::GenEvent);
631  bool py8hepmc = toHepMC.fill_next_event( *(fMasterGen.get()), event().get());
632 
633  if (!py8hepmc) {
634  return false;
635  }
636 
637  //add ckkw/umeps/unlops merging weight
638  if (mergeweight!=1.) {
639  event()->weights()[0] *= mergeweight;
640  }
641 
642  if (fEmissionVetoHook.get()) {
643  nISRveto += fEmissionVetoHook->getNISRveto();
644  nFSRveto += fEmissionVetoHook->getNFSRveto();
645  }
646 
647  //fill additional weights for systematic uncertainties
648  //this is a hack because pythia does not currently provide ordered access to the weights
649  //*FIXME* to be improved with future pythia version
650  for (const string &key : fSortedWeightKeys) {
651  double wgt = (*fMasterGen->info.weights_detailed)[key];
652  event()->weights().push_back(wgt);
653  }
654 
655  return true;
656 
657 }
658 
659 
661 {
662  DJR.resize(0);
663  nME = -1;
664  nMEFiltered = -1;
665  if(LHEInputFileName == std::string()) lhaUP->loadEvent(lheEvent());
666 
667  if ( fJetMatchingHook.get() )
668  {
669  fJetMatchingHook->resetMatchingStatus();
670  fJetMatchingHook->beforeHadronization( lheEvent() );
671  }
672 
673  bool py8next = fMasterGen->next();
674 
675  double mergeweight = fMasterGen.get()->info.mergingWeightNLO();
676  if (fMergingHook.get()) {
677  mergeweight *= fMergingHook->getNormFactor();
678  }
679 
680 
681  //protect against 0-weight from ckkw or similar
682  if (!py8next || std::abs(mergeweight)==0.)
683  {
684  lheEvent()->count( lhef::LHERunInfo::kSelected, 1.0, mergeweight );
685  event().reset();
686  return false;
687  }
688 
689  if (fJetMatchingPy8InternalHook.get()) {
690  const std::vector<double> djrmatch = fJetMatchingPy8InternalHook->getDJR();
691  //cap size of djr vector to save storage space (keep only up to first 6 elements)
692  unsigned int ndjr = std::min(djrmatch.size(), std::vector<double>::size_type(6));
693  for (unsigned int idjr=0; idjr<ndjr; ++idjr) {
694  DJR.push_back(djrmatch[idjr]);
695  }
696 
697  nME=fJetMatchingPy8InternalHook->nMEpartons().first;
698  nMEFiltered=fJetMatchingPy8InternalHook->nMEpartons().second;
699  }
700 
701  // update LHE matching statistics
702  //
703  lheEvent()->count( lhef::LHERunInfo::kAccepted, 1.0, mergeweight );
704 
705  if (evtgenDecays.get()) evtgenDecays->decay();
706 
707  event().reset(new HepMC::GenEvent);
708  bool py8hepmc = toHepMC.fill_next_event( *(fMasterGen.get()), event().get());
709  if (!py8hepmc) {
710  return false;
711  }
712 
713  //add ckkw/umeps/unlops merging weight
714  if (mergeweight!=1.) {
715  event()->weights()[0] *= mergeweight;
716  }
717 
718  if (fEmissionVetoHook.get()) {
719  nISRveto += fEmissionVetoHook->getNISRveto();
720  nFSRveto += fEmissionVetoHook->getNFSRveto();
721  }
722 
723  return true;
724 
725 }
726 
727 
729 {
730 
731  Event* pythiaEvent = &(fMasterGen->event);
732 
733  int NPartsBeforeDecays = pythiaEvent->size();
734  int NPartsAfterDecays = event().get()->particles_size();
735 
736  if(NPartsAfterDecays == NPartsBeforeDecays) return true;
737 
738  bool result = true;
739 
740  for ( int ipart=NPartsAfterDecays; ipart>NPartsBeforeDecays; ipart-- )
741  {
742 
743  HepMC::GenParticle* part = event().get()->barcode_to_particle( ipart );
744 
745  if ( part->status() == 1 && (fDecayer->particleData).canDecay(part->pdg_id()) )
746  {
747  fDecayer->event.reset();
748  Particle py8part( part->pdg_id(), 93, 0, 0, 0, 0, 0, 0,
749  part->momentum().x(),
750  part->momentum().y(),
751  part->momentum().z(),
752  part->momentum().t(),
753  part->generated_mass() );
754  HepMC::GenVertex* ProdVtx = part->production_vertex();
755  py8part.vProd( ProdVtx->position().x(), ProdVtx->position().y(),
756  ProdVtx->position().z(), ProdVtx->position().t() );
757  py8part.tau( (fDecayer->particleData).tau0( part->pdg_id() ) );
758  fDecayer->event.append( py8part );
759  int nentries = fDecayer->event.size();
760  if ( !fDecayer->event[nentries-1].mayDecay() ) continue;
761  fDecayer->next();
762  int nentries1 = fDecayer->event.size();
763  if ( nentries1 <= nentries ) continue; //same number of particles, no decays...
764 
765  part->set_status(2);
766 
767  result = toHepMC.fill_next_event( *(fDecayer.get()), event().get(), -1, true, part);
768 
769  }
770  }
771 
772  return result;
773 
774 }
775 
776 
778 {
779  bool lhe = lheEvent() != 0;
780 
781  // now create the GenEventInfo product from the GenEvent and fill
782  // the missing pieces
783  eventInfo().reset( new GenEventInfoProduct( event().get() ) );
784 
785  // in pythia pthat is used to subdivide samples into different bins
786  // in LHE mode the binning is done by the external ME generator
787  // which is likely not pthat, so only filling it for Py6 internal mode
788  if (!lhe) {
789  eventInfo()->setBinningValues(std::vector<double>(1, fMasterGen->info.pTHat()));
790  }
791 
792  eventInfo()->setDJR(DJR);
793  eventInfo()->setNMEPartons(nME);
794  eventInfo()->setNMEPartonsFiltered(nMEFiltered);
795 
796  //******** Verbosity ********
797 
798  if (maxEventsToPrint > 0 &&
802  if (pythiaPylistVerbosity) {
803  fMasterGen->info.list(std::cout);
804  fMasterGen->event.list(std::cout);
805  }
806 
807  if (pythiaHepMCVerbosity) {
808  std::cout << "Event process = "
809  << fMasterGen->info.code() << "\n"
810  << "----------------------" << std::endl;
811  event()->print();
812  }
814  std::cout << "Event process = "
815  << fMasterGen->info.code() << "\n"
816  << "----------------------" << std::endl;
817  ascii_io->write_event(event().get());
818  }
819  }
820 }
821 
823  GenLumiInfoHeader *genLumiInfoHeader = BaseHadronizer::getGenLumiInfoHeader();
824 
825  //fill lhe headers
826  //*FIXME* initrwgt header is corrupt due to pythia bug
827  for (const std::string &key : fMasterGen->info.headerKeys()) {
828  genLumiInfoHeader->lheHeaders().emplace_back(key,fMasterGen->info.header(key));
829  }
830 
831  //fill weight names
832  //*FIXME* to be improved with future pythia version to avoid need
833  //for re-sorting weights
834  //Note that weight group names are not available in all cases currently
835  //due to an issue in the weightgroup handling in pythia
836  genLumiInfoHeader->weightNames().reserve(fSortedWeightKeys.size() + 1);
837  genLumiInfoHeader->weightNames().push_back("nominal");
838  for (const std::string &key : fSortedWeightKeys) {
839  std::string weightgroupname;
840  for (const auto &wgtgrp : fMasterGen->info.initrwgt->weightgroups) {
841  if (wgtgrp.second.weights.count(key)) {
842  if (!wgtgrp.first.empty()) {
843  weightgroupname = wgtgrp.first;
844  }
845  else if (wgtgrp.second.attributes.count("type")) {
846  weightgroupname = wgtgrp.second.attributes.find("type")->second;
847  }
848  break;
849  }
850  }
851 
852  std::ostringstream weightname;
853  weightname << "LHE, id = " << key << ", ";
854  if (!weightgroupname.empty()) {
855  weightname << weightgroupname << ", ";
856  }
857  weightname<< fMasterGen->info.initrwgt->weights[key].contents;
858  genLumiInfoHeader->weightNames().push_back(weightname.str());
859  }
860 
861  return genLumiInfoHeader;
862 }
863 
866 
867 
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
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
HepMC::IO_AsciiParticles * ascii_io
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
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
GenLumiInfoHeader * getGenLumiInfoHeader() const override
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()
virtual std::vector< std::string > const & doSharedResources() const override
std::vector< std::string > fSortedWeightKeys
std::vector< std::string > evtgenUserFiles
tuple result
Definition: mps_fire.py:83
def gen
run2 Cosmic #### Run 256259 @ 0T 2015C### Run 272133 @ 3.8T 2016B###
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
unsigned int pythiaPylistVerbosity
const char * classname() const override
T min(T a, T b)
Definition: MathUtil.h:58
string key
FastSim: produces sample of signal events, overlayed with premixed minbias events.
std::auto_ptr< GenEventInfoProduct > & eventInfo()
lhef::LHERunInfo * lheRunInfo()
bool generatePartonsAndHadronize() override
std::auto_ptr< UserHooks > fReweightUserHook
part
Definition: HCALResponse.h:20
unsigned int maxEventsToPrint
std::auto_ptr< UserHooks > fReweightRapUserHook
std::auto_ptr< Pythia8::Pythia > fDecayer
Pythia8Hadronizer(const edm::ParameterSet &params)
vector< float > DJR
HepMC::Pythia8ToHepMC toHepMC
tuple cout
Definition: gather_cfg.py:145
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
tuple status
Definition: mps_update.py:57