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