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