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 <Pythia.h>
12 #include <HepMCInterface.h>
13 
15 
17 
18 // PS matchning prototype
19 //
21 
22 
23 // Emission Veto Hooks
24 //
27 
33 
36 
40 
42 
43 #include "HepPID/ParticleIDTranslations.hh"
44 
46 
47 namespace CLHEP {
48  class HepRandomEngine;
49 }
50 
51 using namespace gen;
52 using namespace Pythia8;
53 
55 
56  public:
57 
58  Pythia8Hadronizer(const edm::ParameterSet &params);
60 
61  bool initializeForInternalPartons() override;
62  bool initializeForExternalPartons();
63 
64  bool generatePartonsAndHadronize() override;
65  bool hadronize();
66  void finalizeEvent() override;
67 
68  void statistics() override;
69 
70  const char *classname() const override { return "Pythia8Hadronizer"; }
71 
72  private:
73 
74  virtual void doSetRandomEngine(CLHEP::HepRandomEngine* v) override { p8SetRandomEngine(v); }
75  virtual std::vector<std::string> const& doSharedResources() const override { return p8SharedResources; }
76 
78  double comEnergy;
79 
81  std::auto_ptr<LHAupLesHouches> lhaUP;
82 
83  enum { PP, PPbar, ElectronPositron };
84  int fInitialState ; // pp, ppbar, or e-e+
85 
86  double fBeam1PZ;
87  double fBeam2PZ;
88 
89  // Reweight user hook
90  //
91  UserHooks* fReweightUserHook;
92 
93  // PS matching protot6ype
94  //
96 
97  // Emission Veto Hooks
98  //
101 
110 
111  static const std::vector<std::string> p8SharedResources;
112 };
113 
115 
117  BaseHadronizer(params), Py8InterfaceBase(params),
118  comEnergy(params.getParameter<double>("comEnergy")),
119  LHEInputFileName(params.getUntrackedParameter<string>("LHEInputFileName","")),
120  fInitialState(PP),
121  fReweightUserHook(0),
122  fJetMatchingHook(0),
123  fEmissionVetoHook(0),fEmissionVetoHook1(0)
124 {
125 
126  // J.Y.: the following 3 parameters are hacked "for a reason"
127  //
128  if ( params.exists( "PPbarInitialState" ) )
129  {
130  if ( fInitialState == PP )
131  {
133  edm::LogInfo("GeneratorInterface|Pythia6Interface")
134  << "Pythia6 will be initialized for PROTON-ANTIPROTON INITIAL STATE. "
135  << "This is a user-request change from the DEFAULT PROTON-PROTON initial state." << std::endl;
136  std::cout << "Pythia6 will be initialized for PROTON-ANTIPROTON INITIAL STATE." << std::endl;
137  std::cout << "This is a user-request change from the DEFAULT PROTON-PROTON initial state." << std::endl;
138  }
139  else
140  {
141  // probably need to throw on attempt to override ?
142  }
143  }
144  else if ( params.exists( "ElectronPositronInitialState" ) )
145  {
146  if ( fInitialState == PP )
147  {
149  edm::LogInfo("GeneratorInterface|Pythia6Interface")
150  << "Pythia6 will be initialized for ELECTRON-POSITRON INITIAL STATE. "
151  << "This is a user-request change from the DEFAULT PROTON-PROTON initial state." << std::endl;
152  std::cout << "Pythia6 will be initialized for ELECTRON-POSITRON INITIAL STATE." << std::endl;
153  std::cout << "This is a user-request change from the DEFAULT PROTON-PROTON initial state." << std::endl;
154  }
155  else
156  {
157  // probably need to throw on attempt to override ?
158  }
159  }
160  else if ( params.exists( "ElectronProtonInitialState" ) || params.exists( "PositronProtonInitialState" ) )
161  {
162  // throw on unknown initial state !
163  throw edm::Exception(edm::errors::Configuration,"Pythia8Interface")
164  <<" UNKNOWN INITIAL STATE. \n The allowed initial states are: PP, PPbar, ElectronPositron \n";
165  }
166 
167  if( params.exists( "SLHAFileForPythia8" ) ) {
168  std::string slhafilenameshort = params.getParameter<string>("SLHAFileForPythia8");
169  edm::FileInPath f1( slhafilenameshort );
170  std::string slhafilename = f1.fullPath();
171  std::string pythiacommandslha = std::string("SLHA:file = ") + slhafilename;
172  fMasterGen->readString(pythiacommandslha);
174  line != fParameters.end(); ++line ) {
175  if (line->find("SLHA:file") != std::string::npos)
176  throw cms::Exception("PythiaError") << "Attempted to set SLHA file name twice, "
177  << "using Pythia8 card SLHA:file and Pythia8Interface card SLHAFileForPythia8"
178  << std::endl;
179  }
180  }
181 
182  // Reweight user hook
183  //
184  if( params.exists( "reweightGen" ) )
186 
187  if( params.exists( "useUserHook" ) )
188  throw edm::Exception(edm::errors::Configuration,"Pythia8Interface")
189  <<" Obsolete parameter: useUserHook \n Please use the actual one instead \n";
190 
191  // PS matching prototype
192  //
193  if ( params.exists("jetMatching") )
194  {
195  edm::ParameterSet jmParams =
196  params.getUntrackedParameter<edm::ParameterSet>("jetMatching");
197  std::string scheme = jmParams.getParameter<std::string>("scheme");
198  if ( scheme == "Madgraph" || scheme == "MadgraphFastJet" )
199  {
200  fJetMatchingHook = new JetMatchingHook( jmParams, &fMasterGen->info );
201  }
202  }
203 
204  // Emission vetos
205  //
206  if ( params.exists("emissionVeto") )
207  {
209  }
210 
211  if ( params.exists("emissionVeto1") )
212  {
213  EV1_nFinal = -1;
214  if(params.exists("EV1_nFinal")) EV1_nFinal = params.getParameter<int>("EV1_nFinal");
215  EV1_vetoOn = true;
216  if(params.exists("EV1_vetoOn")) EV1_vetoOn = params.getParameter<bool>("EV1_vetoOn");
217  EV1_maxVetoCount = 10;
218  if(params.exists("EV1_maxVetoCount")) EV1_maxVetoCount = params.getParameter<int>("EV1_maxVetoCount");
219  EV1_pThardMode = 1;
220  if(params.exists("EV1_pThardMode")) EV1_pThardMode = params.getParameter<int>("EV1_pThardMode");
221  EV1_pTempMode = 0;
222  if(params.exists("EV1_pTempMode")) EV1_pTempMode = params.getParameter<int>("EV1_pTempMode");
223  if(EV1_pTempMode > 2 || EV1_pTempMode < 0)
224  throw edm::Exception(edm::errors::Configuration,"Pythia8Interface")
225  <<" Wrong value for EV1_pTempMode code\n";
226  EV1_emittedMode = 0;
227  if(params.exists("EV1_emittedMode")) EV1_emittedMode = params.getParameter<int>("EV1_emittedMode");
228  EV1_pTdefMode = 1;
229  if(params.exists("EV1_pTdefMode")) EV1_pTdefMode = params.getParameter<int>("EV1_pTdefMode");
230  EV1_MPIvetoOn = false;
231  if(params.exists("EV1_MPIvetoOn")) EV1_MPIvetoOn = params.getParameter<bool>("EV1_MPIvetoOn");
235  }
236 
237  int NHooks=0;
238  if(fReweightUserHook) NHooks++;
239  if(fJetMatchingHook) NHooks++;
240  if(fEmissionVetoHook) NHooks++;
241  if(fEmissionVetoHook1) NHooks++;
242  if(NHooks > 1)
243  throw edm::Exception(edm::errors::Configuration,"Pythia8Interface")
244  <<" Too many User Hooks. \n Please choose one from: reweightGen, jetMatching, emissionVeto \n";
245 
246  if(fReweightUserHook) fMasterGen->setUserHooksPtr(fReweightUserHook);
247  if(fJetMatchingHook) fMasterGen->setUserHooksPtr(fJetMatchingHook);
249  cout << "Turning on Emission Veto Hook";
250  if(fEmissionVetoHook1) cout << " 1";
251  cout << endl;
252  int nversion = (int)(1000.*(fMasterGen->settings.parm("Pythia:versionNumber") - 8.));
253  if(nversion < 157) {
254  cout << "obsolete pythia8 version for this Emission Veto code" << endl;
255  cout << "Please update pythia8 version using the instructions here:" << endl;
256  cout << "https://twiki.cern.ch/twiki/bin/view/CMS/Pythia8Interface" << endl;
257  cout << "or try to use tag V00-01-28 of this interface" << endl;
258  throw edm::Exception(edm::errors::Configuration,"Pythia8Interface")
259  <<" Obsolete pythia8 version for this Emission Veto code\n";
260  }
261  if(fEmissionVetoHook) fMasterGen->setUserHooksPtr(fEmissionVetoHook);
262  if(fEmissionVetoHook1) fMasterGen->setUserHooksPtr(fEmissionVetoHook1);
263  }
264 }
265 
266 
268 {
269 // do we need to delete UserHooks/JetMatchingHook here ???
270 
273 }
274 
276 {
277 
278  // pythiaEvent = &pythia->event;
279 
280  if ( fInitialState == PP ) // default
281  {
282  fMasterGen->init(2212, 2212, comEnergy);
283  }
284  else if ( fInitialState == PPbar )
285  {
286  fMasterGen->init(2212, -2212, comEnergy);
287  }
288  else if ( fInitialState == ElectronPositron )
289  {
290  fMasterGen->init(11, -11, comEnergy);
291  }
292  else
293  {
294  // throw on unknown initial state !
295  throw edm::Exception(edm::errors::Configuration,"Pythia8Interface")
296  <<" UNKNOWN INITIAL STATE. \n The allowed initial states are: PP, PPbar, ElectronPositron \n";
297  }
298 
299  fMasterGen->settings.listChanged();
300 
301  if ( pythiaPylistVerbosity > 10 )
302  {
303  if ( pythiaPylistVerbosity == 11 || pythiaPylistVerbosity == 13 )
304  fMasterGen->settings.listAll();
305  if ( pythiaPylistVerbosity == 12 || pythiaPylistVerbosity == 13 )
306  fMasterGen->particleData.listAll();
307  }
308 
309  return true;
310 }
311 
312 
314 {
315 
316  std::cout << "Initializing for external partons" << std::endl;
317 
318  // pythiaEvent = &pythia->event;
319 
320  if(LHEInputFileName != string()) {
321 
322  cout << endl;
323  cout << "LHE Input File Name = " << LHEInputFileName << endl;
324  cout << endl;
326 
327  } else {
328 
329  lhaUP.reset(new LHAupLesHouches());
330  lhaUP->loadRunInfo(lheRunInfo());
331 
332  if ( fJetMatchingHook )
333  {
335  }
336 
337  fMasterGen->init(lhaUP.get());
338 
339  }
340 
341  if ( pythiaPylistVerbosity > 10 )
342  {
343  if ( pythiaPylistVerbosity == 11 || pythiaPylistVerbosity == 13 )
344  fMasterGen->settings.listAll();
345  if ( pythiaPylistVerbosity == 12 || pythiaPylistVerbosity == 13 )
346  fMasterGen->particleData.listAll();
347  }
348 
349  return true;
350 }
351 
352 #if 0
353 // naive Pythia8 HepMC status fixup
354 static int getStatus(const HepMC::GenParticle *p)
355 {
356  int status = p->status();
357  if (status > 0)
358  return status;
359  else if (status > -30 && status < 0)
360  return 3;
361  else
362  return 2;
363 }
364 #endif
365 
366 
368 {
369  fMasterGen->statistics();
370 
371  double xsec = fMasterGen->info.sigmaGen(); // cross section in mb
372  xsec *= 1.0e9; // translate to pb (CMS/Gen "convention" as of May 2009)
373  runInfo().setInternalXSec(xsec);
374 }
375 
376 
378 {
379 
380  if (!fMasterGen->next()) return false;
381 
382  event().reset(new HepMC::GenEvent);
383  return toHepMC.fill_next_event( *(fMasterGen.get()), event().get());
384 
385 }
386 
387 
389 {
390  if(LHEInputFileName == string()) lhaUP->loadEvent(lheEvent());
391 
392  if ( fJetMatchingHook )
393  {
396  }
397 
398  bool py8next = fMasterGen->next();
399 
400  if (!py8next)
401  {
403  event().reset();
404  return false;
405  }
406 
407  // update LHE matching statistics
408  //
410 
411  event().reset(new HepMC::GenEvent);
412  return toHepMC.fill_next_event( *(fMasterGen.get()), event().get());
413 
414 }
415 
416 
418 {
419  bool lhe = lheEvent() != 0;
420 
421  // now create the GenEventInfo product from the GenEvent and fill
422  // the missing pieces
423  eventInfo().reset( new GenEventInfoProduct( event().get() ) );
424 
425  // in pythia pthat is used to subdivide samples into different bins
426  // in LHE mode the binning is done by the external ME generator
427  // which is likely not pthat, so only filling it for Py6 internal mode
428  if (!lhe) {
429  eventInfo()->setBinningValues(std::vector<double>(1, fMasterGen->info.pTHat()));
430  }
431 
432  //******** Verbosity ********
433 
434  if (maxEventsToPrint > 0 &&
437  if (pythiaPylistVerbosity) {
438  fMasterGen->info.list(std::cout);
439  fMasterGen->event.list(std::cout);
440  }
441 
442  if (pythiaHepMCVerbosity) {
443  std::cout << "Event process = "
444  << fMasterGen->info.code() << "\n"
445  << "----------------------" << std::endl;
446  event()->print();
447  }
448  }
449 }
450 
453 
454 
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
ParameterCollector fParameters
double comEnergy
Center-of-Mass energy.
std::auto_ptr< Pythia8::Pythia > fMasterGen
EmissionVetoHook1 * fEmissionVetoHook1
edm::GeneratorFilter< Pythia8Hadronizer, ExternalDecayDriver > Pythia8GeneratorFilter
bool initializeForInternalPartons() override
UserHooks * fReweightUserHook
EmissionVetoHook * fEmissionVetoHook
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
void statistics() override
bool exists(std::string const &parameterName) const
checks if a parameter exists
HepMC::I_Pythia8 toHepMC
void count(LHERunInfo::CountMode count, double weight=1.0, double matchWeight=1.0)
Definition: LHEEvent.cc:212
void setInternalXSec(const XSec &xsec)
virtual void beforeHadronization(lhef::LHEEvent *lhee)
double v[5][pyjets_maxn]
std::auto_ptr< HepMC::GenEvent > & event()
virtual std::vector< std::string > const & doSharedResources() const override
GenRunInfoProduct & runInfo()
std::auto_ptr< LHAupLesHouches > lhaUP
lhef::LHEEvent * lheEvent()
static const std::vector< std::string > p8SharedResources
unsigned int pythiaPylistVerbosity
const char * classname() const override
std::auto_ptr< GenEventInfoProduct > & eventInfo()
lhef::LHERunInfo * lheRunInfo()
bool generatePartonsAndHadronize() override
virtual void init(lhef::LHERunInfo *runInfo)
JetMatchingHook * fJetMatchingHook
unsigned int maxEventsToPrint
Pythia8Hadronizer(const edm::ParameterSet &params)
void resetMatchingStatus()
const_iterator end() const
const_iterator begin() const
tuple cout
Definition: gather_cfg.py:121
static const std::string kPythia8
void finalizeEvent() override
tuple status
Definition: ntuplemaker.py:245
virtual void doSetRandomEngine(CLHEP::HepRandomEngine *v) override
edm::HadronizerFilter< Pythia8Hadronizer, ExternalDecayDriver > Pythia8HadronizerFilter