diff --git a/GeneratorInterface/LHEInterface/interface/LHEWeightGroupReaderHelper.h b/GeneratorInterface/LHEInterface/interface/LHEWeightGroupReaderHelper.h new file mode 100644 index 0000000000000..25cebc478fab3 --- /dev/null +++ b/GeneratorInterface/LHEInterface/interface/LHEWeightGroupReaderHelper.h @@ -0,0 +1,128 @@ +#ifndef GeneratorInterface_LHEInterface_LHEWeightGroupReaderHelper_h +#define GeneratorInterface_LHEInterface_LHEWeightGroupReaderHelper_h + +#include +#include +#include +#include + +#include "SimDataFormats/GeneratorProducts/interface/WeightGroupInfo.h" +#include "SimDataFormats/GeneratorProducts/interface/PdfWeightGroupInfo.h" +#include "SimDataFormats/GeneratorProducts/interface/ScaleWeightGroupInfo.h" + + +class LHEWeightGroupReaderHelper { +public: + LHEWeightGroupReaderHelper() : curGroup(gen::kUnknownWeights) {} + + //// possibly add more versions of this functions for different inputs + void parseLHEFile(std::string filename); + + + gen::WeightGroupInfo* getScaleInfo() {return scaleInfo;} + edm::OwnVector getPdfVector() {return pdfVector;} + +private: + // Functions + std::regex createRegexSearch(std::vector); + std::map getTagsMap(std::string, std::regex); + + // Variables + gen::WeightType curWeight; + gen::WeightGroupInfo* scaleInfo; + edm::OwnVector pdfVector; + std::regex weightStart(".*.*"); + std::regex weightEnd(".*.*"); + std::regex weightContent("\\s*(.+)"); + +}; + +void +LHEWeightGroupReaderHelper::parseLHEFile(std::string filename) { + ifstream file; + file.open(filename); + + + //// may put in constructor, can have flag to specify these values + //// To make this class a little more flexible + std::vector weightGroup = {"name|type", "combine"}; + std::vector weightInfo = {"MUF", "id", "MUR", "PDF"}; + + std::regex groupTags = createRegexSearch(weightGroup); + std::regex infoTags = createRegexSearch(weightInfo); + /// end that comment + + + std::string line; + std::smatch m; + int index = 0; + while(getline(file, line)) { + if(std::regex_match(line, weightStart)) { + std::string groupLine = line; + std::string name = getTagsMap(line, groupTags)["name"]; + + if(name == "Central scale variation") + curWeight = gen::kScaleWeights; + else + curWeight = gen::kPdfWeights; + + /// file weights + + while(getline(file, line) && !std::regex_match(line, weightEnd)) { + auto tmp = getTagsMap(line, infoTags); + std::regex_search(line, m, weightContent); + std::string content = m[1].str(); + + gen::WeightGroupInfo* tmpWeight = nullptr; + if(curWeight == gen::kScaleWeights) + tmpWeight = new gen::ScaleWeightGroupInfo(groupLine); + else if(curWeight == gen::kPdfWeights) + tmpWeight = new gen::PdfWeightGroupInfo(groupLine); + + tmpWeight->setWeightType(curWeight); + tmpWeight->addContainedId(index, tmp["id"], line); + index++; + } + curWeight = gen::kUnknownWeights; + } + } + +} + + +std::map +LHEWeightGroupReaderHelper::getTagsMap(std::string s, std::regex r) { + std::smatch m; + + std::map retMap; + while(std::regex_search(s, m, r)) { + for(int i = 1; i < m.length() - 1; i += 2) { + if(m[i] != "") { + retMap[m[i]] = m[i + 1]; + } + } + s = m.suffix().str(); + } + + return retMap; +} + + + +std::regex +LHEWeightGroupReaderHelper::createRegexSearch(std::vector names) { + std::string s = "(?:"; + size_t numNames = names.size(); + for(int i=0; i < numNames; ++i) { + s += "\\s*(" + names[i] + ")=\"([^\"]+)\""; + if(i != numNames - 1) { + s += "|"; + } + } + s += ")"; + + return std::regex(s); +} + + +#endif diff --git a/GeneratorInterface/LHEInterface/plugins/ExternalLHEProducer.cc b/GeneratorInterface/LHEInterface/plugins/ExternalLHEProducer.cc index 0775cdacf6836..6a68f526fb3e9 100644 --- a/GeneratorInterface/LHEInterface/plugins/ExternalLHEProducer.cc +++ b/GeneratorInterface/LHEInterface/plugins/ExternalLHEProducer.cc @@ -58,6 +58,7 @@ Description: [one line class summary] #include "GeneratorInterface/LHEInterface/interface/LHEEvent.h" #include "GeneratorInterface/LHEInterface/interface/LHEReader.h" #include "GeneratorInterface/LHEInterface/interface/TestWeightInfo.h" +#include "GeneratorInterface/LHEInterface/interface/LHEWeightGroupReaderHelper.h" #include "FWCore/ServiceRegistry/interface/Service.h" #include "FWCore/Utilities/interface/RandomNumberGenerator.h" @@ -350,38 +351,50 @@ ExternalLHEProducer::beginRunProduce(edm::Run& run, edm::EventSetup const& es) unsigned int skip = 0; reader_ = std::make_unique(infiles, skip); + + + std::unique_ptr weightInfoProduct(new LHEWeightInfoProduct); - gen::WeightGroupInfo scaleInfo = getExampleScaleWeights(); + gen::WeightGroupInfo scaleInfo;// = getExampleScaleWeights(); + edm::OwnVector pdfSets;// = getExamplePdfWeights(); //gen::WeightGroupInfo scaleInfo = getExampleScaleWeightsOutOfOrder(); - edm::OwnVector pdfSets = getExamplePdfWeights(); + + // setup file reader + string LHEfilename ="cmsgrid_final.lhe"; + LHEWeightGroupReaderHelper reader; + reader.parseLHEFile(LHEfilename); + scaleInfo = *reader.getScaleInfo(); + pdfSet = reader.getPdfVector(); + + weightInfoProduct->addWeightGroupInfo(scaleInfo); for (auto pdfSet : pdfSets) - weightInfoProduct->addWeightGroupInfo(pdfSet); + weightInfoProduct->addWeightGroupInfo(pdfSet); weightGroups_ = weightInfoProduct->allWeightGroupsInfo(); run.put(std::move(weightInfoProduct)); nextEvent(); if (runInfoLast) { - runInfo = runInfoLast; + runInfo = runInfoLast; - std::unique_ptr product(new LHERunInfoProduct(*runInfo->getHEPRUP())); - std::for_each(runInfo->getHeaders().begin(), - runInfo->getHeaders().end(), - boost::bind(&LHERunInfoProduct::addHeader, - product.get(), _1)); - std::for_each(runInfo->getComments().begin(), - runInfo->getComments().end(), - boost::bind(&LHERunInfoProduct::addComment, - product.get(), _1)); + std::unique_ptr product(new LHERunInfoProduct(*runInfo->getHEPRUP())); + std::for_each(runInfo->getHeaders().begin(), + runInfo->getHeaders().end(), + boost::bind(&LHERunInfoProduct::addHeader, + product.get(), _1)); + std::for_each(runInfo->getComments().begin(), + runInfo->getComments().end(), + boost::bind(&LHERunInfoProduct::addComment, + product.get(), _1)); - // keep a copy around in case of merging - runInfoProducts.push_back(new LHERunInfoProduct(*product)); - wasMerged = false; + // keep a copy around in case of merging + runInfoProducts.push_back(new LHERunInfoProduct(*product)); + wasMerged = false; - run.put(std::move(product)); + run.put(std::move(product)); - runInfo.reset(); + runInfo.reset(); } }