CMS 3D CMS Logo

Py8InterfaceBase.cc
Go to the documentation of this file.
1 #include <memory>
2 #include <string>
3 
6 
9 #include <filesystem>
10 
11 // EvtGen plugin
12 //
13 //#include "Pythia8Plugins/EvtGen.h"
14 
15 using namespace Pythia8;
16 
17 namespace gen {
18 
19  Py8InterfaceBase::Py8InterfaceBase(edm::ParameterSet const& ps)
20  : BaseHadronizer(ps), useEvtGen(false), evtgenDecays(nullptr) {
21  fParameters = ps;
22 
23  pythiaPylistVerbosity = ps.getUntrackedParameter<int>("pythiaPylistVerbosity", 0);
24  pythiaHepMCVerbosity = ps.getUntrackedParameter<bool>("pythiaHepMCVerbosity", false);
25  pythiaHepMCVerbosityParticles = ps.getUntrackedParameter<bool>("pythiaHepMCVerbosityParticles", false);
26  maxEventsToPrint = ps.getUntrackedParameter<int>("maxEventsToPrint", 0);
27 
29  ascii_io = new HepMC::IO_AsciiParticles("cout", std::ios::out);
30 
31  if (ps.exists("useEvtGenPlugin")) {
32  useEvtGen = true;
33  string evtgenpath(std::getenv("EVTGENDATA"));
34  evtgenDecFile = evtgenpath + string("/DECAY_2010.DEC");
35  evtgenPdlFile = evtgenpath + string("/evt.pdl");
36 
37  if (ps.exists("evtgenDecFile")) {
38  edm::FileInPath decay_table(ps.getParameter<std::string>("evtgenDecFile"));
39  evtgenDecFile = decay_table.fullPath();
40  }
41 
42  if (ps.exists("evtgenPdlFile")) {
43  edm::FileInPath pdt(ps.getParameter<std::string>("evtgenPdlFile"));
44  evtgenPdlFile = pdt.fullPath();
45  }
46 
47  if (ps.exists("evtgenUserFile")) {
48  std::vector<std::string> user_decays = ps.getParameter<std::vector<std::string> >("evtgenUserFile");
49  for (unsigned int i = 0; i < user_decays.size(); i++) {
50  edm::FileInPath user_decay(user_decays.at(i));
51  evtgenUserFiles.push_back(user_decay.fullPath());
52  }
53  //evtgenUserFiles = ps.getParameter< std::vector<std::string> >("evtgenUserFile");
54  }
55 
56  if (ps.exists("evtgenUserFileEmbedded")) {
57  std::vector<std::string> user_decay_lines =
58  ps.getParameter<std::vector<std::string> >("evtgenUserFileEmbedded");
59  char tempslhaname[] = "pythia8evtgenXXXXXX";
60  int fd = mkstemp(tempslhaname);
61 
62  for (unsigned int i = 0; i < user_decay_lines.size(); i++) {
63  user_decay_lines.at(i) += "\n";
64  write(fd, user_decay_lines.at(i).c_str(), user_decay_lines.at(i).size());
65  }
66  close(fd);
67  evtgenUserFiles.push_back(std::string(tempslhaname));
68  }
69  }
70  }
71 
73  //Pythia 8's default value for first argument to constructor
74  const string xmlDir = "../share/Pythia8/xmldoc";
75  bool printBanner = true;
76  if (fParameters.exists("printBanner")) {
77  printBanner = fParameters.getUntrackedParameter<bool>("printBanner");
78  }
79  if (!fMasterGen.get())
80  fMasterGen = std::make_unique<Pythia>(xmlDir, printBanner);
81  fDecayer = std::make_unique<Pythia>(xmlDir, printBanner);
82 
83  //add settings for resonance decay filter
84  fMasterGen->settings.addFlag("BiasedTauDecayer:filter", false);
85  fMasterGen->settings.addFlag("BiasedTauDecayer:eDecays", true);
86  fMasterGen->settings.addFlag("BiasedTauDecayer:muDecays", true);
87 
88  //add settings for resonance decay filter
89  fMasterGen->settings.addFlag("ResonanceDecayFilter:filter", false);
90  fMasterGen->settings.addFlag("ResonanceDecayFilter:exclusive", false);
91  fMasterGen->settings.addFlag("ResonanceDecayFilter:eMuAsEquivalent", false);
92  fMasterGen->settings.addFlag("ResonanceDecayFilter:eMuTauAsEquivalent", false);
93  fMasterGen->settings.addFlag("ResonanceDecayFilter:allNuAsEquivalent", false);
94  fMasterGen->settings.addFlag("ResonanceDecayFilter:udscAsEquivalent", false);
95  fMasterGen->settings.addFlag("ResonanceDecayFilter:udscbAsEquivalent", false);
96  fMasterGen->settings.addFlag("ResonanceDecayFilter:wzAsEquivalent", false);
97  fMasterGen->settings.addMVec("ResonanceDecayFilter:mothers", std::vector<int>(), false, false, 0, 0);
98  fMasterGen->settings.addMVec("ResonanceDecayFilter:daughters", std::vector<int>(), false, false, 0, 0);
99 
100  //add settings for PT filter
101  fMasterGen->settings.addFlag("PTFilter:filter", false);
102  fMasterGen->settings.addMode("PTFilter:quarkToFilter", 5, true, true, 3, 6);
103  fMasterGen->settings.addParm("PTFilter:scaleToFilter", 0.4, true, true, 0.0, 10.);
104  fMasterGen->settings.addParm("PTFilter:quarkRapidity", 10.0, true, true, 0.0, 10.);
105  fMasterGen->settings.addParm("PTFilter:quarkPt", -.1, true, true, -.1, 100.);
106 
107  //add settings for powheg resonance scale calculation
108  fMasterGen->settings.addFlag("POWHEGres:calcScales", false);
109  fMasterGen->settings.addFlag("POWHEG:bb4l", false);
110  fMasterGen->settings.addFlag("POWHEG:bb4l:FSREmission:onlyDistance1", false);
111  fMasterGen->settings.addFlag("POWHEG:bb4l:FSREmission:veto", false);
112  fMasterGen->settings.addFlag("POWHEG:bb4l:FSREmission:dryRun", false);
113  fMasterGen->settings.addFlag("POWHEG:bb4l:FSREmission:vetoAtPL", false);
114  fMasterGen->settings.addFlag("POWHEG:bb4l:FSREmission:vetoQED", false);
115  fMasterGen->settings.addFlag("POWHEG:bb4l:PartonLevel:veto", false);
116  fMasterGen->settings.addFlag("POWHEG:bb4l:PartonLevel:excludeFSRConflicting", false);
117  fMasterGen->settings.addFlag("POWHEG:bb4l:DEBUG", false);
118  fMasterGen->settings.addFlag("POWHEG:bb4l:ScaleResonance:veto", false);
119  fMasterGen->settings.addFlag("POWHEG:bb4l:FSREmission:vetoDipoleFrame", false);
120  fMasterGen->settings.addFlag("POWHEG:bb4l:FSREmission:pTpythiaVeto", false);
121  fMasterGen->settings.addParm("POWHEG:bb4l:pTminVeto", 10.0, true, true, 0.0, 10.);
122 
123  fMasterGen->setRndmEnginePtr(&p8RndmEngine_);
124  fDecayer->setRndmEnginePtr(&p8RndmEngine_);
125 
126  fMasterGen->readString("Next:numberShowEvent = 0");
127  fDecayer->readString("Next:numberShowEvent = 0");
128 
129  edm::ParameterSet currentParameters;
130  if (randomIndex() >= 0) {
131  std::vector<edm::ParameterSet> randomizedParameters =
132  fParameters.getParameter<std::vector<edm::ParameterSet> >("RandomizedParameters");
133  currentParameters = randomizedParameters[randomIndex()];
134  } else {
135  currentParameters = fParameters;
136  }
137 
138  ParameterCollector pCollector = currentParameters.getParameter<edm::ParameterSet>("PythiaParameters");
139 
140  for (ParameterCollector::const_iterator line = pCollector.begin(); line != pCollector.end(); ++line) {
141  if (line->find("Random:") != std::string::npos)
142  throw cms::Exception("PythiaError") << "Attempted to set random number "
143  "using Pythia commands. Please use "
144  "the RandomNumberGeneratorService."
145  << std::endl;
146 
147  if (!fMasterGen->readString(*line))
148  throw cms::Exception("PythiaError") << "Pythia 8 did not accept \"" << *line << "\"." << std::endl;
149 
150  if (line->find("ParticleDecays:") != std::string::npos) {
151  if (!fDecayer->readString(*line))
152  throw cms::Exception("PythiaError") << "Pythia 8 Decayer did not accept \"" << *line << "\"." << std::endl;
153  }
154  }
155 
156  slhafile_.clear();
157 
158  if (currentParameters.exists("SLHAFileForPythia8")) {
159  std::string slhafilenameshort = currentParameters.getParameter<std::string>("SLHAFileForPythia8");
160  edm::FileInPath f1(slhafilenameshort);
161 
162  fMasterGen->settings.mode("SLHA:readFrom", 2);
163  fMasterGen->settings.word("SLHA:file", f1.fullPath());
164  } else if (currentParameters.exists("SLHATableForPythia8")) {
165  std::string slhatable = currentParameters.getParameter<std::string>("SLHATableForPythia8");
166 
167  makeTmpSLHA(slhatable);
168  } else if (currentParameters.exists("SLHATreeForPythia8")) {
169  auto slhaReaderParams = currentParameters.getParameter<edm::ParameterSet>("SLHATreeForPythia8");
170  std::unique_ptr<SLHAReaderBase> reader =
171  SLHAReaderFactory::get()->create(slhaReaderParams.getParameter<std::string>("name"), slhaReaderParams);
172 
173  makeTmpSLHA(reader->getSLHA(currentParameters.getParameter<std::string>("ConfigDescription")));
174  }
175 
176  return true;
177  }
178 
180  char tempslhaname[] = "pythia8SLHAtableXXXXXX";
181  int fd = mkstemp(tempslhaname);
182  write(fd, slhatable.c_str(), slhatable.size());
183  close(fd);
184 
185  slhafile_ = tempslhaname;
186 
187  fMasterGen->settings.mode("SLHA:readFrom", 2);
188  fMasterGen->settings.word("SLHA:file", slhafile_);
189  }
190 
191  bool Py8InterfaceBase::declareStableParticles(const std::vector<int>& pdgIds) {
192  for (size_t i = 0; i < pdgIds.size(); i++) {
193  // FIXME: need to double check if PID's are the same in Py6 & Py8,
194  // because the HepPDT translation tool is actually for **Py6**
195  //
196  // well, actually it looks like Py8 operates in PDT id's rather than Py6's
197  //
198  // int PyID = HepPID::translatePDTtoPythia( pdgIds[i] );
199  int PyID = pdgIds[i];
200  std::ostringstream pyCard;
201  pyCard << PyID << ":mayDecay=false";
202 
203  if (fMasterGen->particleData.isParticle(PyID)) {
204  fMasterGen->readString(pyCard.str());
205  } else {
206  edm::LogWarning("DataNotUnderstood") << "Pythia8 does not "
207  << "recognize particle id = " << PyID << std::endl;
208  }
209  // alternative:
210  // set the 2nd input argument warn=false
211  // - this way Py8 will NOT print warnings about unknown particle code(s)
212  // fMasterPtr->readString( pyCard.str(), false )
213  }
214 
215  return true;
216  }
217 
218  bool Py8InterfaceBase::declareSpecialSettings(const std::vector<std::string>& settings) {
219  for (unsigned int iss = 0; iss < settings.size(); iss++) {
220  if (settings[iss].find("QED-brem-off") != std::string::npos) {
221  fMasterGen->readString("TimeShower:QEDshowerByL=off");
222  } else {
223  size_t fnd1 = settings[iss].find("Pythia8:");
224  if (fnd1 != std::string::npos) {
225  std::string value = settings[iss].substr(fnd1 + 8);
226  fDecayer->readString(value);
227  }
228  }
229  }
230  return true;
231  }
232 
234  fMasterGen->stat();
235  return;
236  }
237 
238 } // namespace gen
bool declareSpecialSettings(const std::vector< std::string > &)
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
std::unique_ptr< Pythia8::Pythia > fDecayer
void makeTmpSLHA(const std::string &)
std::string fullPath() const
Definition: FileInPath.cc:161
HepMC::IO_AsciiParticles * ascii_io
const_iterator begin() const
bool exists(std::string const &parameterName) const
checks if a parameter exists
reader
Definition: DQM.py:105
const_iterator end() const
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
std::vector< std::string > evtgenUserFiles
T getUntrackedParameter(std::string const &, T const &) const
P8RndmEngine p8RndmEngine_
int randomIndex() const
unsigned int pythiaPylistVerbosity
Definition: value.py:1
bool declareStableParticles(const std::vector< int > &)
unsigned int maxEventsToPrint
std::unique_ptr< Pythia8::Pythia > fMasterGen
#define get
Log< level::Warning, false > LogWarning
edm::ParameterSet fParameters
fd
Definition: ztee.py:136