Skip to content

Commit

Permalink
Merge pull request cms-sw#16 from dteague/from-CMSSW_11_1_0_pre2
Browse files Browse the repository at this point in the history
GenWeight Parsing
  • Loading branch information
kdlong committed Aug 3, 2020
2 parents 34306be + 21e251a commit 260d8d4
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 99 deletions.
1 change: 1 addition & 0 deletions GeneratorInterface/Core/interface/GenWeightHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace gen {
void parseWeightGroupsFromNames(std::vector<std::string> weightNames);

private:
static inline std::string trim(std::string &s);
};
} // namespace gen

Expand Down
2 changes: 0 additions & 2 deletions GeneratorInterface/Core/interface/LHEWeightHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ namespace gen {
LHEWeightHelper() : WeightHelper(){};
void setHeaderLines(std::vector<std::string> headerLines);
void parseWeights();
void buildGroups();
bool isConsistent();
void swapHeaders();
std::unique_ptr<WeightGroupInfo> buildGroup(ParsedWeight& weight);

private:
std::vector<std::string> headerLines_;
Expand Down
11 changes: 10 additions & 1 deletion GeneratorInterface/Core/interface/WeightHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "SimDataFormats/GeneratorProducts/interface/WeightsInfo.h"
#include "SimDataFormats/GeneratorProducts/interface/UnknownWeightGroupInfo.h"
#include "SimDataFormats/GeneratorProducts/interface/PdfWeightGroupInfo.h"
#include "SimDataFormats/GeneratorProducts/interface/PartonShowerWeightGroupInfo.h"
#include "SimDataFormats/GeneratorProducts/interface/ScaleWeightGroupInfo.h"
#include "SimDataFormats/GeneratorProducts/interface/MEParamWeightGroupInfo.h"
#include "LHAPDF/LHAPDF.h"
Expand Down Expand Up @@ -45,9 +46,11 @@ namespace gen {
bool isScaleWeightGroup(const ParsedWeight& weight);
bool isMEParamWeightGroup(const ParsedWeight& weight);
bool isPdfWeightGroup(const ParsedWeight& weight);
bool isPartonShowerWeightGroup(const ParsedWeight& weight);
bool isOrphanPdfWeightGroup(ParsedWeight& weight);
void updateScaleInfo(const ParsedWeight& weight);
void updatePdfInfo(const ParsedWeight& weight);
void updatePartonShowerInfo(const ParsedWeight& weight);
void cleanupOrphanCentralWeight();

int getLhapdfId(const ParsedWeight& weight);
Expand All @@ -61,7 +64,13 @@ namespace gen {
{"mur", {"muR", "MUR", "mur", "renscfact"}},
{"pdf", {"PDF", "PDF set", "lhapdf", "pdf", "pdf set", "pdfset"}},
{"dyn", {"DYN_SCALE"}},
{"dyn_name", {"dyn_scale_choice"}}};
{"dyn_name", {"dyn_scale_choice"}},
{"up", {"_up", "Hi"}},
{"down", {"_dn", "Lo"}}};
void printWeights();
std::unique_ptr<WeightGroupInfo> buildGroup(ParsedWeight& weight);
void buildGroups();
std::string searchString(const std::string& label, const std::string& name);
};
} // namespace gen

Expand Down
51 changes: 40 additions & 11 deletions GeneratorInterface/Core/src/GenWeightHelper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,55 @@ namespace gen {
GenWeightHelper::GenWeightHelper() {}

void GenWeightHelper::parseWeightGroupsFromNames(std::vector<std::string> weightNames) {
int index = 0;

parsedWeights_.clear();
size_t index = 0;
size_t groupIndex = 0;
std::string curGroup = "";
// If size is 1, it's just the central weight
if (weightNames.size() <= 1)
return;

for (std::string weightName : weightNames) {
std::cout << weightName << std::endl;
}
for (std::string weightName : weightNames) {
if (weightName.find("LHE") != std::string::npos) {
// Parse as usual, this is the SUSY workflow
// std::vector<std::string> info;
// boost::split(info, weightName, boost::is_any_of(","));
weightGroups_.push_back(new gen::UnknownWeightGroupInfo("inGen"));
std::vector<std::string> info;
boost::split(info, weightName, boost::is_any_of(","));
std::unordered_map<std::string, std::string> attributes;
std::string text = info.back();
info.pop_back();
for (auto i : info) {
std::vector<std::string> subInfo;
boost::split(subInfo, i, boost::is_any_of("="));
if (subInfo.size() == 2) {
attributes[trim(subInfo[0])] = trim(subInfo[1]);
}
}
if (attributes["group"] != curGroup) {
curGroup = attributes["group"];
groupIndex++;
}
parsedWeights_.push_back({attributes["id"], index++, curGroup, text, attributes, groupIndex});
} else {
parsedWeights_.push_back(
{weightName, index++, weightName, weightName, std::unordered_map<std::string, std::string>(), groupIndex++});
if (isPartonShowerWeightGroup(parsedWeights_.back()))
parsedWeights_.back().wgtGroup_idx = -1; // all parton showers are grouped together
}
// Working on the not-so-nice assumption that all non-LHE gen weights are PS weights
else if (weightGroups_.size() == 0) {
weightGroups_.push_back(new gen::PartonShowerWeightGroupInfo("shower"));
}
auto& group = weightGroups_.back();
// No IDs for Gen weights
group.addContainedId(index++, "", weightName);
// else if (weightGroups_.size() == 0) {
// weightGroups_.push_back(new gen::PartonShowerWeightGroupInfo("shower"));
// }
}
buildGroups();
printWeights();
}

inline std::string GenWeightHelper::trim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
return s;
}
} // namespace gen
76 changes: 0 additions & 76 deletions GeneratorInterface/Core/src/LHEWeightHelper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,80 +124,4 @@ namespace gen {
}
}
}

void LHEWeightHelper::buildGroups() {
weightGroups_.clear();
size_t currentGroupIdx = -1;
for (auto& weight : parsedWeights_) {
if (currentGroupIdx != weight.wgtGroup_idx) {
weightGroups_.push_back(*buildGroup(weight));
currentGroupIdx = weight.wgtGroup_idx;
}

// split PDF groups
if (weightGroups_.back().weightType() == gen::WeightType::kPdfWeights) {
auto& pdfGroup = dynamic_cast<gen::PdfWeightGroupInfo&>(weightGroups_.back());
int lhaid = getLhapdfId(weight);
if (lhaid > 0 && !pdfGroup.isIdInParentSet(lhaid) && pdfGroup.getParentLhapdfId() > 0) {
weightGroups_.push_back(*buildGroup(weight));
}
}
WeightGroupInfo& group = weightGroups_.back();
group.addContainedId(weight.index, weight.id, weight.content);
if (group.weightType() == gen::WeightType::kScaleWeights)
updateScaleInfo(weight);
else if (group.weightType() == gen::WeightType::kPdfWeights)
updatePdfInfo(weight);
}
cleanupOrphanCentralWeight();
// checks
for (auto& wgt : weightGroups_) {
if (!wgt.isWellFormed())
std::cout << "\033[1;31m";
std::cout << std::boolalpha << wgt.name() << " (" << wgt.firstId() << "-" << wgt.lastId()
<< "): " << wgt.isWellFormed() << std::endl;
if (wgt.weightType() == gen::WeightType::kScaleWeights) {
auto& wgtScale = dynamic_cast<gen::ScaleWeightGroupInfo&>(wgt);
std::cout << wgtScale.centralIndex() << " ";
std::cout << wgtScale.muR1muF2Index() << " ";
std::cout << wgtScale.muR1muF05Index() << " ";
std::cout << wgtScale.muR2muF1Index() << " ";
std::cout << wgtScale.muR2muF2Index() << " ";
std::cout << wgtScale.muR2muF05Index() << " ";
std::cout << wgtScale.muR05muF1Index() << " ";
std::cout << wgtScale.muR05muF2Index() << " ";
std::cout << wgtScale.muR05muF05Index() << " \n";
for (auto name : wgtScale.getDynNames()) {
std::cout << name << ": ";
std::cout << wgtScale.getScaleIndex(1.0, 1.0, name) << " ";
std::cout << wgtScale.getScaleIndex(1.0, 2.0, name) << " ";
std::cout << wgtScale.getScaleIndex(1.0, 0.5, name) << " ";
std::cout << wgtScale.getScaleIndex(2.0, 1.0, name) << " ";
std::cout << wgtScale.getScaleIndex(2.0, 2.0, name) << " ";
std::cout << wgtScale.getScaleIndex(2.0, 0.5, name) << " ";
std::cout << wgtScale.getScaleIndex(0.5, 1.0, name) << " ";
std::cout << wgtScale.getScaleIndex(0.5, 2.0, name) << " ";
std::cout << wgtScale.getScaleIndex(0.5, 0.5, name) << "\n";
}

} else if (wgt.weightType() == gen::WeightType::kPdfWeights) {
std::cout << wgt.description() << "\n";
}
if (!wgt.isWellFormed())
std::cout << "\033[0m";
}
}

std::unique_ptr<WeightGroupInfo> LHEWeightHelper::buildGroup(ParsedWeight& weight) {
if (isScaleWeightGroup(weight))
return std::make_unique<ScaleWeightGroupInfo>(weight.groupname);
else if (isPdfWeightGroup(weight))
return std::make_unique<PdfWeightGroupInfo>(weight.groupname);
else if (isMEParamWeightGroup(weight))
return std::make_unique<MEParamWeightGroupInfo>(weight.groupname);
else if (isOrphanPdfWeightGroup(weight))
return std::make_unique<PdfWeightGroupInfo>(weight.groupname);

return std::make_unique<UnknownWeightGroupInfo>(weight.groupname);
}
} // namespace gen
115 changes: 115 additions & 0 deletions GeneratorInterface/Core/src/WeightHelper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ namespace gen {
return LHAPDF::lookupLHAPDFID(name) != -1;
}

bool WeightHelper::isPartonShowerWeightGroup(const ParsedWeight& weight) {
// In case of mixed case (or is this necessary?)
const std::string& name = boost::to_lower_copy(weight.groupname);
return name.find("isr") != std::string::npos || name.find("fsr") != std::string::npos;
}

bool WeightHelper::isOrphanPdfWeightGroup(ParsedWeight& weight) {
std::string lhaidText = searchAttributes("pdf", weight);
try {
Expand Down Expand Up @@ -53,6 +59,14 @@ namespace gen {
return "";
}

std::string WeightHelper::searchString(const std::string& label, const std::string& name) {
for (const auto& lab : attributeNames_.at(label)) {
if (name.find(lab) != std::string::npos)
return name.substr(0, name.find(lab));
}
return "";
}

std::string WeightHelper::searchAttributesByRegex(const std::string& label, const ParsedWeight& weight) const {
auto& content = weight.content;
std::smatch match;
Expand Down Expand Up @@ -143,6 +157,17 @@ namespace gen {
pdfGroup.addLhaid(lhaid);
}

void WeightHelper::updatePartonShowerInfo(const ParsedWeight& weight) {
auto& psGroup = dynamic_cast<gen::PartonShowerWeightGroupInfo&>(weightGroups_.back());
bool isUp = true;
std::string subName = searchString("up", weight.id);
if (subName.empty()) {
isUp = false;
subName = searchString("down", weight.id);
}
psGroup.updateWeight(weight.index, weight.id, subName, isUp);
}

// TODO: Could probably recycle this code better
std::unique_ptr<GenWeightProduct> WeightHelper::weightProduct(std::vector<double> weights, float w0) {
auto weightProduct = std::make_unique<GenWeightProduct>(w0);
Expand Down Expand Up @@ -224,4 +249,94 @@ namespace gen {
throw std::range_error("Unmatched Generator weight! ID was " + wgtId + " index was " + std::to_string(weightIndex) +
"\nNot found in any of " + std::to_string(weightGroups_.size()) + " weightGroups.");
}

void WeightHelper::printWeights() {
// checks
for (auto& wgt : weightGroups_) {
if (!wgt.isWellFormed())
std::cout << "\033[1;31m";
std::cout << std::boolalpha << wgt.name() << " (" << wgt.firstId() << "-" << wgt.lastId()
<< "): " << wgt.isWellFormed() << std::endl;
if (wgt.weightType() == gen::WeightType::kScaleWeights) {
auto& wgtScale = dynamic_cast<gen::ScaleWeightGroupInfo&>(wgt);
std::cout << wgtScale.centralIndex() << " ";
std::cout << wgtScale.muR1muF2Index() << " ";
std::cout << wgtScale.muR1muF05Index() << " ";
std::cout << wgtScale.muR2muF1Index() << " ";
std::cout << wgtScale.muR2muF2Index() << " ";
std::cout << wgtScale.muR2muF05Index() << " ";
std::cout << wgtScale.muR05muF1Index() << " ";
std::cout << wgtScale.muR05muF2Index() << " ";
std::cout << wgtScale.muR05muF05Index() << " \n";
for (auto name : wgtScale.getDynNames()) {
std::cout << name << ": ";
std::cout << wgtScale.getScaleIndex(1.0, 1.0, name) << " ";
std::cout << wgtScale.getScaleIndex(1.0, 2.0, name) << " ";
std::cout << wgtScale.getScaleIndex(1.0, 0.5, name) << " ";
std::cout << wgtScale.getScaleIndex(2.0, 1.0, name) << " ";
std::cout << wgtScale.getScaleIndex(2.0, 2.0, name) << " ";
std::cout << wgtScale.getScaleIndex(2.0, 0.5, name) << " ";
std::cout << wgtScale.getScaleIndex(0.5, 1.0, name) << " ";
std::cout << wgtScale.getScaleIndex(0.5, 2.0, name) << " ";
std::cout << wgtScale.getScaleIndex(0.5, 0.5, name) << "\n";
}

} else if (wgt.weightType() == gen::WeightType::kPdfWeights) {
std::cout << wgt.description() << "\n";
} else if (wgt.weightType() == gen::WeightType::kPartonShowerWeights) {
auto& wgtPS = dynamic_cast<gen::PartonShowerWeightGroupInfo&>(wgt);
for (auto group : wgtPS.getWeightNames()) {
std::cout << group << ": up " << wgtPS.getUpIndex(group);
std::cout << " - down " << wgtPS.getDownIndex(group) << std::endl;
}
}
if (!wgt.isWellFormed())
std::cout << "\033[0m";
}
}

std::unique_ptr<WeightGroupInfo> WeightHelper::buildGroup(ParsedWeight& weight) {
if (isScaleWeightGroup(weight))
return std::make_unique<ScaleWeightGroupInfo>(weight.groupname);
else if (isPdfWeightGroup(weight))
return std::make_unique<PdfWeightGroupInfo>(weight.groupname);
else if (isMEParamWeightGroup(weight))
return std::make_unique<MEParamWeightGroupInfo>(weight.groupname);
else if (isPartonShowerWeightGroup(weight))
return std::make_unique<PartonShowerWeightGroupInfo>("shower");
else if (isOrphanPdfWeightGroup(weight))
return std::make_unique<PdfWeightGroupInfo>(weight.groupname);

return std::make_unique<UnknownWeightGroupInfo>(weight.groupname);
}

void WeightHelper::buildGroups() {
weightGroups_.clear();
size_t currentGroupIdx = -1;
for (auto& weight : parsedWeights_) {
if (currentGroupIdx != weight.wgtGroup_idx) {
weightGroups_.push_back(*buildGroup(weight));
currentGroupIdx = weight.wgtGroup_idx;
}

// split PDF groups
if (weightGroups_.back().weightType() == gen::WeightType::kPdfWeights) {
auto& pdfGroup = dynamic_cast<gen::PdfWeightGroupInfo&>(weightGroups_.back());
int lhaid = getLhapdfId(weight);
if (lhaid > 0 && !pdfGroup.isIdInParentSet(lhaid) && pdfGroup.getParentLhapdfId() > 0) {
weightGroups_.push_back(*buildGroup(weight));
}
}
WeightGroupInfo& group = weightGroups_.back();
group.addContainedId(weight.index, weight.id, weight.content);
if (group.weightType() == gen::WeightType::kScaleWeights)
updateScaleInfo(weight);
else if (group.weightType() == gen::WeightType::kPdfWeights)
updatePdfInfo(weight);
else if (group.weightType() == gen::WeightType::kPartonShowerWeights)
updatePartonShowerInfo(weight);
}
cleanupOrphanCentralWeight();
}

} // namespace gen
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef SimDataFormats_GeneratorProducts_PartonShowerWeightGroupInfo_h
#define SimDataFormats_GeneratorProducts_PartonShowerWeightGroupInfo_h

#include <unordered_map>

#include "SimDataFormats/GeneratorProducts/interface/WeightGroupInfo.h"

namespace gen {
Expand All @@ -15,7 +17,15 @@ namespace gen {
virtual ~PartonShowerWeightGroupInfo() override {}
void copy(const PartonShowerWeightGroupInfo &other);
virtual PartonShowerWeightGroupInfo *clone() const override;
void updateWeight(int globalIndex, std::string id, std::string subName, bool isUp);

size_t getUpIndex(std::string weightName) { return weightNameToUpDown[weightName].first; }
size_t getDownIndex(std::string weightName) { return weightNameToUpDown[weightName].second; }
std::vector<std::string> getWeightNames() const { return weightNames; }

private:
std::unordered_map<std::string, std::pair<size_t, size_t>> weightNameToUpDown;
std::vector<std::string> weightNames;
// Is a variation of the functional form of the dynamic scale
};
} // namespace gen
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "SimDataFormats/GeneratorProducts/interface/PartonShowerWeightGroupInfo.h"

namespace gen {
void PartonShowerWeightGroupInfo::copy(const PartonShowerWeightGroupInfo& other) { WeightGroupInfo::copy(other); }

PartonShowerWeightGroupInfo* PartonShowerWeightGroupInfo::clone() const {
return new PartonShowerWeightGroupInfo(*this);
}

void PartonShowerWeightGroupInfo::updateWeight(int globalIndex, std::string id, std::string subName, bool isUp) {
size_t localIndex = weightMetaInfoByGlobalIndex(id, globalIndex).localIndex;
if (weightNameToUpDown.find(subName) == weightNameToUpDown.end()) {
weightNames.push_back(subName);
weightNameToUpDown[subName] = std::pair<size_t, size_t>();
}
if (isUp)
weightNameToUpDown[subName].first = localIndex;
else
weightNameToUpDown[subName].second = localIndex;
}

} // namespace gen
Loading

0 comments on commit 260d8d4

Please sign in to comment.