Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-03-09 04:50:17

0001 #include <memory>
0002 #include <string>
0003 
0004 #include "GeneratorInterface/Pythia8Interface/interface/Py8InterfaceBase.h"
0005 #include "GeneratorInterface/Pythia8Interface/interface/SLHAReaderBase.h"
0006 
0007 #include "FWCore/Utilities/interface/Exception.h"
0008 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0009 #include <filesystem>
0010 
0011 // EvtGen plugin
0012 //
0013 //#include "Pythia8Plugins/EvtGen.h"
0014 
0015 using namespace Pythia8;
0016 
0017 namespace gen {
0018 
0019   Py8InterfaceBase::Py8InterfaceBase(edm::ParameterSet const& ps)
0020       : BaseHadronizer(ps), useEvtGen(false), evtgenDecays(nullptr) {
0021     fParameters = ps;
0022 
0023     pythiaPylistVerbosity = ps.getUntrackedParameter<int>("pythiaPylistVerbosity", 0);
0024     pythiaHepMCVerbosity = ps.getUntrackedParameter<bool>("pythiaHepMCVerbosity", false);
0025     pythiaHepMCVerbosityParticles = ps.getUntrackedParameter<bool>("pythiaHepMCVerbosityParticles", false);
0026     maxEventsToPrint = ps.getUntrackedParameter<int>("maxEventsToPrint", 0);
0027 
0028     if (pythiaHepMCVerbosityParticles)
0029       ascii_io = new HepMC::IO_AsciiParticles("cout", std::ios::out);
0030 
0031     if (ps.exists("useEvtGenPlugin")) {
0032       useEvtGen = true;
0033       string evtgenpath(std::getenv("EVTGENDATA"));
0034       evtgenDecFile = evtgenpath + string("/DECAY_2010.DEC");
0035       evtgenPdlFile = evtgenpath + string("/evt.pdl");
0036 
0037       if (ps.exists("evtgenDecFile")) {
0038         edm::FileInPath decay_table(ps.getParameter<std::string>("evtgenDecFile"));
0039         evtgenDecFile = decay_table.fullPath();
0040       }
0041 
0042       if (ps.exists("evtgenPdlFile")) {
0043         edm::FileInPath pdt(ps.getParameter<std::string>("evtgenPdlFile"));
0044         evtgenPdlFile = pdt.fullPath();
0045       }
0046 
0047       if (ps.exists("evtgenUserFile")) {
0048         std::vector<std::string> user_decays = ps.getParameter<std::vector<std::string> >("evtgenUserFile");
0049         for (unsigned int i = 0; i < user_decays.size(); i++) {
0050           edm::FileInPath user_decay(user_decays.at(i));
0051           evtgenUserFiles.push_back(user_decay.fullPath());
0052         }
0053         //evtgenUserFiles = ps.getParameter< std::vector<std::string> >("evtgenUserFile");
0054       }
0055 
0056       if (ps.exists("evtgenUserFileEmbedded")) {
0057         std::vector<std::string> user_decay_lines =
0058             ps.getParameter<std::vector<std::string> >("evtgenUserFileEmbedded");
0059         char tempslhaname[] = "pythia8evtgenXXXXXX";
0060         int fd = mkstemp(tempslhaname);
0061 
0062         for (unsigned int i = 0; i < user_decay_lines.size(); i++) {
0063           user_decay_lines.at(i) += "\n";
0064           write(fd, user_decay_lines.at(i).c_str(), user_decay_lines.at(i).size());
0065         }
0066         close(fd);
0067         evtgenUserFiles.push_back(std::string(tempslhaname));
0068       }
0069     }
0070   }
0071 
0072   bool Py8InterfaceBase::readSettings(int) {
0073     if (!fMasterGen.get())
0074       fMasterGen = std::make_unique<Pythia>();
0075     fDecayer = std::make_unique<Pythia>();
0076 
0077     //add settings for resonance decay filter
0078     fMasterGen->settings.addFlag("BiasedTauDecayer:filter", false);
0079     fMasterGen->settings.addFlag("BiasedTauDecayer:eDecays", true);
0080     fMasterGen->settings.addFlag("BiasedTauDecayer:muDecays", true);
0081 
0082     //add settings for resonance decay filter
0083     fMasterGen->settings.addFlag("ResonanceDecayFilter:filter", false);
0084     fMasterGen->settings.addFlag("ResonanceDecayFilter:exclusive", false);
0085     fMasterGen->settings.addFlag("ResonanceDecayFilter:eMuAsEquivalent", false);
0086     fMasterGen->settings.addFlag("ResonanceDecayFilter:eMuTauAsEquivalent", false);
0087     fMasterGen->settings.addFlag("ResonanceDecayFilter:allNuAsEquivalent", false);
0088     fMasterGen->settings.addFlag("ResonanceDecayFilter:udscAsEquivalent", false);
0089     fMasterGen->settings.addFlag("ResonanceDecayFilter:udscbAsEquivalent", false);
0090     fMasterGen->settings.addFlag("ResonanceDecayFilter:wzAsEquivalent", false);
0091     fMasterGen->settings.addMVec("ResonanceDecayFilter:mothers", std::vector<int>(), false, false, 0, 0);
0092     fMasterGen->settings.addMVec("ResonanceDecayFilter:daughters", std::vector<int>(), false, false, 0, 0);
0093 
0094     //add settings for PT filter
0095     fMasterGen->settings.addFlag("PTFilter:filter", false);
0096     fMasterGen->settings.addMode("PTFilter:quarkToFilter", 5, true, true, 3, 6);
0097     fMasterGen->settings.addParm("PTFilter:scaleToFilter", 0.4, true, true, 0.0, 10.);
0098     fMasterGen->settings.addParm("PTFilter:quarkRapidity", 10.0, true, true, 0.0, 10.);
0099     fMasterGen->settings.addParm("PTFilter:quarkPt", -.1, true, true, -.1, 100.);
0100 
0101     //add settings for powheg resonance scale calculation
0102     fMasterGen->settings.addFlag("POWHEGres:calcScales", false);
0103     fMasterGen->settings.addFlag("POWHEG:bb4l", false);
0104     fMasterGen->settings.addFlag("POWHEG:bb4l:FSREmission:onlyDistance1", false);
0105     fMasterGen->settings.addFlag("POWHEG:bb4l:FSREmission:veto", false);
0106     fMasterGen->settings.addFlag("POWHEG:bb4l:FSREmission:dryRun", false);
0107     fMasterGen->settings.addFlag("POWHEG:bb4l:FSREmission:vetoAtPL", false);
0108     fMasterGen->settings.addFlag("POWHEG:bb4l:FSREmission:vetoQED", false);
0109     fMasterGen->settings.addFlag("POWHEG:bb4l:PartonLevel:veto", false);
0110     fMasterGen->settings.addFlag("POWHEG:bb4l:PartonLevel:excludeFSRConflicting", false);
0111     fMasterGen->settings.addFlag("POWHEG:bb4l:DEBUG", false);
0112     fMasterGen->settings.addFlag("POWHEG:bb4l:ScaleResonance:veto", false);
0113     fMasterGen->settings.addFlag("POWHEG:bb4l:FSREmission:vetoDipoleFrame", false);
0114     fMasterGen->settings.addFlag("POWHEG:bb4l:FSREmission:pTpythiaVeto", false);
0115     fMasterGen->settings.addParm("POWHEG:bb4l:pTminVeto", 10.0, true, true, 0.0, 10.);
0116 
0117     fMasterGen->setRndmEnginePtr(&p8RndmEngine_);
0118     fDecayer->setRndmEnginePtr(&p8RndmEngine_);
0119 
0120     fMasterGen->readString("Next:numberShowEvent = 0");
0121     fDecayer->readString("Next:numberShowEvent = 0");
0122 
0123     edm::ParameterSet currentParameters;
0124     if (randomIndex() >= 0) {
0125       std::vector<edm::ParameterSet> randomizedParameters =
0126           fParameters.getParameter<std::vector<edm::ParameterSet> >("RandomizedParameters");
0127       currentParameters = randomizedParameters[randomIndex()];
0128     } else {
0129       currentParameters = fParameters;
0130     }
0131 
0132     ParameterCollector pCollector = currentParameters.getParameter<edm::ParameterSet>("PythiaParameters");
0133 
0134     for (ParameterCollector::const_iterator line = pCollector.begin(); line != pCollector.end(); ++line) {
0135       if (line->find("Random:") != std::string::npos)
0136         throw cms::Exception("PythiaError") << "Attempted to set random number "
0137                                                "using Pythia commands. Please use "
0138                                                "the RandomNumberGeneratorService."
0139                                             << std::endl;
0140 
0141       if (!fMasterGen->readString(*line))
0142         throw cms::Exception("PythiaError") << "Pythia 8 did not accept \"" << *line << "\"." << std::endl;
0143 
0144       if (line->find("ParticleDecays:") != std::string::npos) {
0145         if (!fDecayer->readString(*line))
0146           throw cms::Exception("PythiaError") << "Pythia 8 Decayer did not accept \"" << *line << "\"." << std::endl;
0147       }
0148     }
0149 
0150     slhafile_.clear();
0151 
0152     if (currentParameters.exists("SLHAFileForPythia8")) {
0153       std::string slhafilenameshort = currentParameters.getParameter<std::string>("SLHAFileForPythia8");
0154       edm::FileInPath f1(slhafilenameshort);
0155 
0156       fMasterGen->settings.mode("SLHA:readFrom", 2);
0157       fMasterGen->settings.word("SLHA:file", f1.fullPath());
0158     } else if (currentParameters.exists("SLHATableForPythia8")) {
0159       std::string slhatable = currentParameters.getParameter<std::string>("SLHATableForPythia8");
0160 
0161       makeTmpSLHA(slhatable);
0162     } else if (currentParameters.exists("SLHATreeForPythia8")) {
0163       auto slhaReaderParams = currentParameters.getParameter<edm::ParameterSet>("SLHATreeForPythia8");
0164       std::unique_ptr<SLHAReaderBase> reader =
0165           SLHAReaderFactory::get()->create(slhaReaderParams.getParameter<std::string>("name"), slhaReaderParams);
0166 
0167       makeTmpSLHA(reader->getSLHA(currentParameters.getParameter<std::string>("ConfigDescription")));
0168     }
0169 
0170     return true;
0171   }
0172 
0173   void Py8InterfaceBase::makeTmpSLHA(const std::string& slhatable) {
0174     char tempslhaname[] = "pythia8SLHAtableXXXXXX";
0175     int fd = mkstemp(tempslhaname);
0176     write(fd, slhatable.c_str(), slhatable.size());
0177     close(fd);
0178 
0179     slhafile_ = tempslhaname;
0180 
0181     fMasterGen->settings.mode("SLHA:readFrom", 2);
0182     fMasterGen->settings.word("SLHA:file", slhafile_);
0183   }
0184 
0185   bool Py8InterfaceBase::declareStableParticles(const std::vector<int>& pdgIds) {
0186     for (size_t i = 0; i < pdgIds.size(); i++) {
0187       // FIXME: need to double check if PID's are the same in Py6 & Py8,
0188       //        because the HepPDT translation tool is actually for **Py6**
0189       //
0190       // well, actually it looks like Py8 operates in PDT id's rather than Py6's
0191       //
0192       //    int PyID = HepPID::translatePDTtoPythia( pdgIds[i] );
0193       int PyID = pdgIds[i];
0194       std::ostringstream pyCard;
0195       pyCard << PyID << ":mayDecay=false";
0196 
0197       if (fMasterGen->particleData.isParticle(PyID)) {
0198         fMasterGen->readString(pyCard.str());
0199       } else {
0200         edm::LogWarning("DataNotUnderstood") << "Pythia8 does not "
0201                                              << "recognize particle id = " << PyID << std::endl;
0202       }
0203       // alternative:
0204       // set the 2nd input argument warn=false
0205       // - this way Py8 will NOT print warnings about unknown particle code(s)
0206       // fMasterPtr->readString( pyCard.str(), false )
0207     }
0208 
0209     return true;
0210   }
0211 
0212   bool Py8InterfaceBase::declareSpecialSettings(const std::vector<std::string>& settings) {
0213     for (unsigned int iss = 0; iss < settings.size(); iss++) {
0214       if (settings[iss].find("QED-brem-off") != std::string::npos) {
0215         fMasterGen->readString("TimeShower:QEDshowerByL=off");
0216       } else {
0217         size_t fnd1 = settings[iss].find("Pythia8:");
0218         if (fnd1 != std::string::npos) {
0219           std::string value = settings[iss].substr(fnd1 + 8);
0220           fDecayer->readString(value);
0221         }
0222       }
0223     }
0224     return true;
0225   }
0226 
0227   void Py8InterfaceBase::statistics() {
0228     fMasterGen->stat();
0229     return;
0230   }
0231 
0232 }  // namespace gen