CMS 3D CMS Logo

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