-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First tests with parsing of lhe file. Need to incorp EDM lhe reader
- Loading branch information
Showing
2 changed files
with
159 additions
and
18 deletions.
There are no files selected for viewing
128 changes: 128 additions & 0 deletions
128
GeneratorInterface/LHEInterface/interface/LHEWeightGroupReaderHelper.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters