Skip to content

Commit

Permalink
First tests with parsing of lhe file. Need to incorp EDM lhe reader
Browse files Browse the repository at this point in the history
  • Loading branch information
dteague committed Aug 15, 2019
1 parent 9e06271 commit 071c069
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 18 deletions.
128 changes: 128 additions & 0 deletions GeneratorInterface/LHEInterface/interface/LHEWeightGroupReaderHelper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#ifndef GeneratorInterface_LHEInterface_LHEWeightGroupReaderHelper_h
#define GeneratorInterface_LHEInterface_LHEWeightGroupReaderHelper_h

#include <string>
#include <vector>
#include <map>
#include <regex>

#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<gen::WeightGroupInfo> getPdfVector() {return pdfVector;}

private:
// Functions
std::regex createRegexSearch(std::vector<std::string>);
std::map<std::string, std::string> getTagsMap(std::string, std::regex);

// Variables
gen::WeightType curWeight;
gen::WeightGroupInfo* scaleInfo;
edm::OwnVector<gen::WeightGroupInfo> pdfVector;
std::regex weightStart(".*<weightgroup.+>.*");
std::regex weightEnd(".*</weightgroup>.*");
std::regex weightContent("<weight.*>\\s*(.+)</weight>");

};

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<std::string> weightGroup = {"name|type", "combine"};
std::vector<std::string> 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<std::string, std::string>
LHEWeightGroupReaderHelper::getTagsMap(std::string s, std::regex r) {
std::smatch m;

std::map<std::string, std::string> 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<std::string> 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
49 changes: 31 additions & 18 deletions GeneratorInterface/LHEInterface/plugins/ExternalLHEProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -350,38 +351,50 @@ ExternalLHEProducer::beginRunProduce(edm::Run& run, edm::EventSetup const& es)
unsigned int skip = 0;
reader_ = std::make_unique<lhef::LHEReader>(infiles, skip);




std::unique_ptr<LHEWeightInfoProduct> weightInfoProduct(new LHEWeightInfoProduct);
gen::WeightGroupInfo scaleInfo = getExampleScaleWeights();
gen::WeightGroupInfo scaleInfo;// = getExampleScaleWeights();
edm::OwnVector<gen::WeightGroupInfo> pdfSets;// = getExamplePdfWeights();
//gen::WeightGroupInfo scaleInfo = getExampleScaleWeightsOutOfOrder();
edm::OwnVector<gen::WeightGroupInfo> 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<LHERunInfoProduct> 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<LHERunInfoProduct> 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();
}
}

Expand Down

0 comments on commit 071c069

Please sign in to comment.