CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
28  if (pythiaHepMCVerbosityParticles)
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  if (!fMasterGen.get())
74  fMasterGen = std::make_unique<Pythia>();
75  fDecayer = std::make_unique<Pythia>();
76 
77  //add settings for resonance decay filter
78  fMasterGen->settings.addFlag("BiasedTauDecayer:filter", false);
79  fMasterGen->settings.addFlag("BiasedTauDecayer:eDecays", true);
80  fMasterGen->settings.addFlag("BiasedTauDecayer:muDecays", true);
81 
82  //add settings for resonance decay filter
83  fMasterGen->settings.addFlag("ResonanceDecayFilter:filter", false);
84  fMasterGen->settings.addFlag("ResonanceDecayFilter:exclusive", false);
85  fMasterGen->settings.addFlag("ResonanceDecayFilter:eMuAsEquivalent", false);
86  fMasterGen->settings.addFlag("ResonanceDecayFilter:eMuTauAsEquivalent", false);
87  fMasterGen->settings.addFlag("ResonanceDecayFilter:allNuAsEquivalent", false);
88  fMasterGen->settings.addFlag("ResonanceDecayFilter:udscAsEquivalent", false);
89  fMasterGen->settings.addFlag("ResonanceDecayFilter:udscbAsEquivalent", false);
90  fMasterGen->settings.addFlag("ResonanceDecayFilter:wzAsEquivalent", false);
91  fMasterGen->settings.addMVec("ResonanceDecayFilter:mothers", std::vector<int>(), false, false, 0, 0);
92  fMasterGen->settings.addMVec("ResonanceDecayFilter:daughters", std::vector<int>(), false, false, 0, 0);
93 
94  //add settings for PT filter
95  fMasterGen->settings.addFlag("PTFilter:filter", false);
96  fMasterGen->settings.addMode("PTFilter:quarkToFilter", 5, true, true, 3, 6);
97  fMasterGen->settings.addParm("PTFilter:scaleToFilter", 0.4, true, true, 0.0, 10.);
98  fMasterGen->settings.addParm("PTFilter:quarkRapidity", 10.0, true, true, 0.0, 10.);
99  fMasterGen->settings.addParm("PTFilter:quarkPt", -.1, true, true, -.1, 100.);
100 
101  //add settings for powheg resonance scale calculation
102  fMasterGen->settings.addFlag("POWHEGres:calcScales", false);
103  fMasterGen->settings.addFlag("POWHEG:bb4l", false);
104  fMasterGen->settings.addFlag("POWHEG:bb4l:FSREmission:onlyDistance1", false);
105  fMasterGen->settings.addFlag("POWHEG:bb4l:FSREmission:veto", false);
106  fMasterGen->settings.addFlag("POWHEG:bb4l:FSREmission:dryRun", false);
107  fMasterGen->settings.addFlag("POWHEG:bb4l:FSREmission:vetoAtPL", false);
108  fMasterGen->settings.addFlag("POWHEG:bb4l:FSREmission:vetoQED", false);
109  fMasterGen->settings.addFlag("POWHEG:bb4l:PartonLevel:veto", false);
110  fMasterGen->settings.addFlag("POWHEG:bb4l:PartonLevel:excludeFSRConflicting", false);
111  fMasterGen->settings.addFlag("POWHEG:bb4l:DEBUG", false);
112  fMasterGen->settings.addFlag("POWHEG:bb4l:ScaleResonance:veto", false);
113  fMasterGen->settings.addFlag("POWHEG:bb4l:FSREmission:vetoDipoleFrame", false);
114  fMasterGen->settings.addFlag("POWHEG:bb4l:FSREmission:pTpythiaVeto", false);
115  fMasterGen->settings.addParm("POWHEG:bb4l:pTminVeto", 10.0, true, true, 0.0, 10.);
116 
117  fMasterGen->setRndmEnginePtr(&p8RndmEngine_);
118  fDecayer->setRndmEnginePtr(&p8RndmEngine_);
119 
120  fMasterGen->readString("Next:numberShowEvent = 0");
121  fDecayer->readString("Next:numberShowEvent = 0");
122 
123  edm::ParameterSet currentParameters;
124  if (randomIndex() >= 0) {
125  std::vector<edm::ParameterSet> randomizedParameters =
126  fParameters.getParameter<std::vector<edm::ParameterSet> >("RandomizedParameters");
127  currentParameters = randomizedParameters[randomIndex()];
128  } else {
129  currentParameters = fParameters;
130  }
131 
132  ParameterCollector pCollector = currentParameters.getParameter<edm::ParameterSet>("PythiaParameters");
133 
134  for (ParameterCollector::const_iterator line = pCollector.begin(); line != pCollector.end(); ++line) {
135  if (line->find("Random:") != std::string::npos)
136  throw cms::Exception("PythiaError") << "Attempted to set random number "
137  "using Pythia commands. Please use "
138  "the RandomNumberGeneratorService."
139  << std::endl;
140 
141  if (!fMasterGen->readString(*line))
142  throw cms::Exception("PythiaError") << "Pythia 8 did not accept \"" << *line << "\"." << std::endl;
143 
144  if (line->find("ParticleDecays:") != std::string::npos) {
145  if (!fDecayer->readString(*line))
146  throw cms::Exception("PythiaError") << "Pythia 8 Decayer did not accept \"" << *line << "\"." << std::endl;
147  }
148  }
149 
150  slhafile_.clear();
151 
152  if (currentParameters.exists("SLHAFileForPythia8")) {
153  std::string slhafilenameshort = currentParameters.getParameter<std::string>("SLHAFileForPythia8");
154  edm::FileInPath f1(slhafilenameshort);
155 
156  fMasterGen->settings.mode("SLHA:readFrom", 2);
157  fMasterGen->settings.word("SLHA:file", f1.fullPath());
158  } else if (currentParameters.exists("SLHATableForPythia8")) {
159  std::string slhatable = currentParameters.getParameter<std::string>("SLHATableForPythia8");
160 
161  makeTmpSLHA(slhatable);
162  } else if (currentParameters.exists("SLHATreeForPythia8")) {
163  auto slhaReaderParams = currentParameters.getParameter<edm::ParameterSet>("SLHATreeForPythia8");
164  std::unique_ptr<SLHAReaderBase> reader =
165  SLHAReaderFactory::get()->create(slhaReaderParams.getParameter<std::string>("name"), slhaReaderParams);
166 
167  makeTmpSLHA(reader->getSLHA(currentParameters.getParameter<std::string>("ConfigDescription")));
168  }
169 
170  return true;
171  }
172 
174  char tempslhaname[] = "pythia8SLHAtableXXXXXX";
175  int fd = mkstemp(tempslhaname);
176  write(fd, slhatable.c_str(), slhatable.size());
177  close(fd);
178 
179  slhafile_ = tempslhaname;
180 
181  fMasterGen->settings.mode("SLHA:readFrom", 2);
182  fMasterGen->settings.word("SLHA:file", slhafile_);
183  }
184 
185  bool Py8InterfaceBase::declareStableParticles(const std::vector<int>& pdgIds) {
186  for (size_t i = 0; i < pdgIds.size(); i++) {
187  // FIXME: need to double check if PID's are the same in Py6 & Py8,
188  // because the HepPDT translation tool is actually for **Py6**
189  //
190  // well, actually it looks like Py8 operates in PDT id's rather than Py6's
191  //
192  // int PyID = HepPID::translatePDTtoPythia( pdgIds[i] );
193  int PyID = pdgIds[i];
194  std::ostringstream pyCard;
195  pyCard << PyID << ":mayDecay=false";
196 
197  if (fMasterGen->particleData.isParticle(PyID)) {
198  fMasterGen->readString(pyCard.str());
199  } else {
200  edm::LogWarning("DataNotUnderstood") << "Pythia8 does not "
201  << "recognize particle id = " << PyID << std::endl;
202  }
203  // alternative:
204  // set the 2nd input argument warn=false
205  // - this way Py8 will NOT print warnings about unknown particle code(s)
206  // fMasterPtr->readString( pyCard.str(), false )
207  }
208 
209  return true;
210  }
211 
212  bool Py8InterfaceBase::declareSpecialSettings(const std::vector<std::string>& settings) {
213  for (unsigned int iss = 0; iss < settings.size(); iss++) {
214  if (settings[iss].find("QED-brem-off") != std::string::npos) {
215  fMasterGen->readString("TimeShower:QEDshowerByL=off");
216  } else {
217  size_t fnd1 = settings[iss].find("Pythia8:");
218  if (fnd1 != std::string::npos) {
219  std::string value = settings[iss].substr(fnd1 + 8);
220  fDecayer->readString(value);
221  }
222  }
223  }
224  return true;
225  }
226 
228  fMasterGen->stat();
229  return;
230  }
231 
232 } // namespace gen
bool declareSpecialSettings(const std::vector< std::string > &)
T getUntrackedParameter(std::string const &, T const &) const
std::unique_ptr< Pythia8::Pythia > fDecayer
void makeTmpSLHA(const std::string &)
HepMC::IO_AsciiParticles * ascii_io
bool exists(std::string const &parameterName) const
checks if a parameter exists
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
P8RndmEngine p8RndmEngine_
tuple fd
Definition: ztee.py:136
tuple reader
Definition: DQM.py:105
unsigned int pythiaPylistVerbosity
bool declareStableParticles(const std::vector< int > &)
unsigned int maxEventsToPrint
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
int randomIndex() const
const_iterator end() const
const_iterator begin() const
std::string fullPath() const
Definition: FileInPath.cc:161
std::unique_ptr< Pythia8::Pythia > fMasterGen
#define get
Log< level::Warning, false > LogWarning
edm::ParameterSet fParameters