CMS 3D CMS Logo

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