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 
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("BiasedTauDecayer:filter", false);
85  fMasterGen->settings.addFlag("BiasedTauDecayer:eMuDecays", true);
86 
87  //add settings for resonance decay filter
88  fMasterGen->settings.addFlag("ResonanceDecayFilter:filter", false);
89  fMasterGen->settings.addFlag("ResonanceDecayFilter:exclusive", false);
90  fMasterGen->settings.addFlag("ResonanceDecayFilter:eMuAsEquivalent", false);
91  fMasterGen->settings.addFlag("ResonanceDecayFilter:eMuTauAsEquivalent", false);
92  fMasterGen->settings.addFlag("ResonanceDecayFilter:allNuAsEquivalent", false);
93  fMasterGen->settings.addFlag("ResonanceDecayFilter:udscAsEquivalent", false);
94  fMasterGen->settings.addFlag("ResonanceDecayFilter:udscbAsEquivalent", false);
95  fMasterGen->settings.addFlag("ResonanceDecayFilter:wzAsEquivalent", false);
96  fMasterGen->settings.addMVec("ResonanceDecayFilter:mothers", std::vector<int>(), false, false, 0, 0);
97  fMasterGen->settings.addMVec("ResonanceDecayFilter:daughters", std::vector<int>(), false, false, 0, 0);
98 
99  //add settings for PT filter
100  fMasterGen->settings.addFlag("PTFilter:filter", false);
101  fMasterGen->settings.addMode("PTFilter:quarkToFilter", 5, true, true, 3, 6);
102  fMasterGen->settings.addParm("PTFilter:scaleToFilter", 0.4, true, true, 0.0, 10.);
103  fMasterGen->settings.addParm("PTFilter:quarkRapidity", 10.0, true, true, 0.0, 10.);
104  fMasterGen->settings.addParm("PTFilter:quarkPt", -.1, true, true, -.1, 100.);
105 
106  //add settings for powheg resonance scale calculation
107  fMasterGen->settings.addFlag("POWHEGres:calcScales", false);
108  fMasterGen->settings.addFlag("POWHEG:bb4l", false);
109  fMasterGen->settings.addFlag("POWHEG:bb4l:FSREmission:onlyDistance1", false);
110  fMasterGen->settings.addFlag("POWHEG:bb4l:FSREmission:veto", false);
111  fMasterGen->settings.addFlag("POWHEG:bb4l:FSREmission:dryRun", false);
112  fMasterGen->settings.addFlag("POWHEG:bb4l:FSREmission:vetoAtPL", false);
113  fMasterGen->settings.addFlag("POWHEG:bb4l:FSREmission:vetoQED", false);
114  fMasterGen->settings.addFlag("POWHEG:bb4l:PartonLevel:veto", false);
115  fMasterGen->settings.addFlag("POWHEG:bb4l:PartonLevel:excludeFSRConflicting", false);
116  fMasterGen->settings.addFlag("POWHEG:bb4l:DEBUG", false);
117  fMasterGen->settings.addFlag("POWHEG:bb4l:ScaleResonance:veto", false);
118  fMasterGen->settings.addFlag("POWHEG:bb4l:FSREmission:vetoDipoleFrame", false);
119  fMasterGen->settings.addFlag("POWHEG:bb4l:FSREmission:pTpythiaVeto", false);
120  fMasterGen->settings.addParm("POWHEG:bb4l:pTminVeto", 10.0, true, true, 0.0, 10.);
121 
122  fMasterGen->setRndmEnginePtr(&p8RndmEngine_);
123  fDecayer->setRndmEnginePtr(&p8RndmEngine_);
124 
125  fMasterGen->readString("Next:numberShowEvent = 0");
126  fDecayer->readString("Next:numberShowEvent = 0");
127 
128  edm::ParameterSet currentParameters;
129  if (randomIndex() >= 0) {
130  std::vector<edm::ParameterSet> randomizedParameters =
131  fParameters.getParameter<std::vector<edm::ParameterSet> >("RandomizedParameters");
132  currentParameters = randomizedParameters[randomIndex()];
133  } else {
134  currentParameters = fParameters;
135  }
136 
137  ParameterCollector pCollector = currentParameters.getParameter<edm::ParameterSet>("PythiaParameters");
138 
139  for (ParameterCollector::const_iterator line = pCollector.begin(); line != pCollector.end(); ++line) {
140  if (line->find("Random:") != std::string::npos)
141  throw cms::Exception("PythiaError") << "Attempted to set random number "
142  "using Pythia commands. Please use "
143  "the RandomNumberGeneratorService."
144  << std::endl;
145 
146  if (!fMasterGen->readString(*line))
147  throw cms::Exception("PythiaError") << "Pythia 8 did not accept \"" << *line << "\"." << std::endl;
148 
149  if (line->find("ParticleDecays:") != std::string::npos) {
150  if (!fDecayer->readString(*line))
151  throw cms::Exception("PythiaError") << "Pythia 8 Decayer did not accept \"" << *line << "\"." << std::endl;
152  }
153  }
154 
155  slhafile_.clear();
156 
157  if (currentParameters.exists("SLHAFileForPythia8")) {
158  std::string slhafilenameshort = currentParameters.getParameter<std::string>("SLHAFileForPythia8");
159  edm::FileInPath f1(slhafilenameshort);
160 
161  fMasterGen->settings.mode("SLHA:readFrom", 2);
162  fMasterGen->settings.word("SLHA:file", f1.fullPath());
163  } else if (currentParameters.exists("SLHATableForPythia8")) {
164  std::string slhatable = currentParameters.getParameter<std::string>("SLHATableForPythia8");
165 
166  char tempslhaname[] = "pythia8SLHAtableXXXXXX";
167  int fd = mkstemp(tempslhaname);
168  write(fd, slhatable.c_str(), slhatable.size());
169  close(fd);
170 
171  slhafile_ = tempslhaname;
172 
173  fMasterGen->settings.mode("SLHA:readFrom", 2);
174  fMasterGen->settings.word("SLHA:file", slhafile_);
175  }
176 
177  return true;
178  }
179 
180  bool Py8InterfaceBase::declareStableParticles(const std::vector<int>& pdgIds) {
181  for (size_t i = 0; i < pdgIds.size(); i++) {
182  // FIXME: need to double check if PID's are the same in Py6 & Py8,
183  // because the HepPDT translation tool is actually for **Py6**
184  //
185  // well, actually it looks like Py8 operates in PDT id's rather than Py6's
186  //
187  // int PyID = HepPID::translatePDTtoPythia( pdgIds[i] );
188  int PyID = pdgIds[i];
189  std::ostringstream pyCard;
190  pyCard << PyID << ":mayDecay=false";
191 
192  if (fMasterGen->particleData.isParticle(PyID)) {
193  fMasterGen->readString(pyCard.str());
194  } else {
195  edm::LogWarning("DataNotUnderstood") << "Pythia8 does not "
196  << "recognize particle id = " << PyID << std::endl;
197  }
198  // alternative:
199  // set the 2nd input argument warn=false
200  // - this way Py8 will NOT print warnings about unknown particle code(s)
201  // fMasterPtr->readString( pyCard.str(), false )
202  }
203 
204  return true;
205  }
206 
207  bool Py8InterfaceBase::declareSpecialSettings(const std::vector<std::string>& settings) {
208  for (unsigned int iss = 0; iss < settings.size(); iss++) {
209  if (settings[iss].find("QED-brem-off") != std::string::npos) {
210  fMasterGen->readString("TimeShower:QEDshowerByL=off");
211  } else {
212  size_t fnd1 = settings[iss].find("Pythia8:");
213  if (fnd1 != std::string::npos) {
214  std::string value = settings[iss].substr(fnd1 + 8);
215  fDecayer->readString(value);
216  }
217  }
218  }
219  return true;
220  }
221 
223  fMasterGen->stat();
224  return;
225  }
226 
227 } // namespace gen
gen::Py8InterfaceBase::fMasterGen
std::unique_ptr< Pythia8::Pythia > fMasterGen
Definition: Py8InterfaceBase.h:44
mps_fire.i
i
Definition: mps_fire.py:355
MessageLogger.h
funct::false
false
Definition: Factorize.h:34
gen::Py8InterfaceBase::ascii_io
HepMC::IO_AsciiParticles * ascii_io
Definition: Py8InterfaceBase.h:54
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
gen::ParameterCollector
Definition: ParameterCollector.h:26
gen::Py8InterfaceBase::p8RndmEngine_
P8RndmEngine p8RndmEngine_
Definition: Py8InterfaceBase.h:67
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
gen::Py8InterfaceBase::useEvtGen
bool useEvtGen
Definition: Py8InterfaceBase.h:58
gen::Py8InterfaceBase::pythiaHepMCVerbosityParticles
bool pythiaHepMCVerbosityParticles
Definition: Py8InterfaceBase.h:52
gen
Definition: PythiaDecays.h:13
gen::Py8InterfaceBase::evtgenUserFiles
std::vector< std::string > evtgenUserFiles
Definition: Py8InterfaceBase.h:62
gen::Py8InterfaceBase::pythiaPylistVerbosity
unsigned int pythiaPylistVerbosity
Definition: Py8InterfaceBase.h:50
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
edm::LogWarning
Definition: MessageLogger.h:141
edm::ParameterSet::exists
bool exists(std::string const &parameterName) const
checks if a parameter exists
Definition: ParameterSet.cc:674
gen::Py8InterfaceBase::declareStableParticles
bool declareStableParticles(const std::vector< int > &)
Definition: Py8InterfaceBase.cc:180
edm::ParameterSet
Definition: ParameterSet.h:36
edm::LogError
Definition: MessageLogger.h:183
gen::Py8InterfaceBase::evtgenPdlFile
std::string evtgenPdlFile
Definition: Py8InterfaceBase.h:61
gen::Py8InterfaceBase::maxEventsToPrint
unsigned int maxEventsToPrint
Definition: Py8InterfaceBase.h:53
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
writeEcalDQMStatus.write
write
Definition: writeEcalDQMStatus.py:48
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
Py8InterfaceBase.h
gen::Py8InterfaceBase::pythiaHepMCVerbosity
bool pythiaHepMCVerbosity
Definition: Py8InterfaceBase.h:51
Exception.h
gen::Py8InterfaceBase::declareSpecialSettings
bool declareSpecialSettings(const std::vector< std::string > &)
Definition: Py8InterfaceBase.cc:207
MillePedeFileConverter_cfg.out
out
Definition: MillePedeFileConverter_cfg.py:31
gen::Py8InterfaceBase::fParameters
edm::ParameterSet fParameters
Definition: Py8InterfaceBase.h:48
cms::Exception
Definition: Exception.h:70
beamvalidation.exit
def exit(msg="")
Definition: beamvalidation.py:53
gen::Py8InterfaceBase::statistics
virtual void statistics()
Definition: Py8InterfaceBase.cc:222
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:45
edm::FileInPath::fullPath
std::string fullPath() const
Definition: FileInPath.cc:163
gen::Py8InterfaceBase::evtgenDecFile
std::string evtgenDecFile
Definition: Py8InterfaceBase.h:60
gen::Py8InterfaceBase::readSettings
bool readSettings(int)
Definition: Py8InterfaceBase.cc:78
gen::Py8InterfaceBase::slhafile_
std::string slhafile_
Definition: Py8InterfaceBase.h:64