CMS 3D CMS Logo

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