Skip to content

Commit

Permalink
Merge pull request cms-sw#19 from kdlong/ImprovedParsingMerge
Browse files Browse the repository at this point in the history
Improved parsing merge
  • Loading branch information
kdlong authored Aug 4, 2020
2 parents 260d8d4 + e159802 commit d9a125f
Show file tree
Hide file tree
Showing 13 changed files with 200 additions and 161 deletions.
4 changes: 0 additions & 4 deletions GeneratorInterface/Core/interface/GenWeightHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ namespace gen {
class GenWeightHelper : public WeightHelper {
public:
GenWeightHelper();

void parseWeightGroupsFromNames(std::vector<std::string> weightNames);

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

Expand Down
12 changes: 6 additions & 6 deletions GeneratorInterface/Core/interface/WeightHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
namespace gen {
struct ParsedWeight {
std::string id;
size_t index;
int index;
std::string groupname;
std::string content;
std::unordered_map<std::string, std::string> attributes;
size_t wgtGroup_idx;
int wgtGroup_idx;
};

class WeightHelper {
Expand All @@ -48,12 +48,12 @@ namespace gen {
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 updateScaleInfo(const ParsedWeight& weight, int index);
void updatePdfInfo(const ParsedWeight& weight, int index);
void updatePartonShowerInfo(const ParsedWeight& weight, int index);
void cleanupOrphanCentralWeight();

int getLhapdfId(const ParsedWeight& weight);
int getLhapdfId(const ParsedWeight& weight, gen::PdfWeightGroupInfo& pdfGroup);
std::string searchAttributes(const std::string& label, const ParsedWeight& weight) const;
std::string searchAttributesByTag(const std::string& label, const ParsedWeight& weight) const;
std::string searchAttributesByRegex(const std::string& label, const ParsedWeight& weight) const;
Expand Down
58 changes: 42 additions & 16 deletions GeneratorInterface/Core/plugins/LHEWeightProductProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "GeneratorInterface/Core/interface/LHEWeightHelper.h"

#include "FWCore/ServiceRegistry/interface/Service.h"
#include "FWCore/Utilities/interface/transform.h"

class LHEWeightProductProducer : public edm::one::EDProducer<edm::BeginLuminosityBlockProducer, edm::one::WatchRuns> {
public:
Expand All @@ -32,11 +33,12 @@ class LHEWeightProductProducer : public edm::one::EDProducer<edm::BeginLuminosit

private:
gen::LHEWeightHelper weightHelper_;
std::string lheLabel_;
edm::EDGetTokenT<LHERunInfoProduct> lheRunInfoToken_;
edm::EDGetTokenT<LHEEventProduct> lheEventToken_;
const edm::EDGetTokenT<GenWeightInfoProduct> lheWeightInfoToken_;
std::vector<std::string> lheLabels_;
std::vector<edm::EDGetTokenT<LHEEventProduct>> lheEventTokens_;
std::vector<edm::EDGetTokenT<LHERunInfoProduct>> lheRunInfoTokens_;
std::vector<edm::EDGetTokenT<GenWeightInfoProduct>> lheWeightInfoTokens_;
bool foundWeightProduct_ = false;
bool hasLhe_ = false;

void produce(edm::Event&, const edm::EventSetup&) override;
void beginLuminosityBlockProduce(edm::LuminosityBlock& lumi, edm::EventSetup const& es) override;
Expand All @@ -46,10 +48,13 @@ class LHEWeightProductProducer : public edm::one::EDProducer<edm::BeginLuminosit

// TODO: Accept a vector of strings (source, externalLHEProducer) exit if neither are found
LHEWeightProductProducer::LHEWeightProductProducer(const edm::ParameterSet& iConfig)
: lheLabel_(iConfig.getParameter<std::string>("lheSourceLabel")),
lheRunInfoToken_(consumes<LHERunInfoProduct, edm::InRun>(lheLabel_)),
lheEventToken_(consumes<LHEEventProduct>(lheLabel_)),
lheWeightInfoToken_(consumes<GenWeightInfoProduct, edm::InLumi>(lheLabel_)) {
: lheLabels_(iConfig.getParameter<std::vector<std::string>>("lheSourceLabels")),
lheEventTokens_(edm::vector_transform(lheLabels_,
[this](const std::string& tag) { return mayConsume<LHEEventProduct>(tag); })),
lheRunInfoTokens_(edm::vector_transform(lheLabels_,
[this](const std::string& tag) { return mayConsume<LHERunInfoProduct, edm::InRun>(tag); })),
lheWeightInfoTokens_(edm::vector_transform(lheLabels_,
[this](const std::string& tag) { return mayConsume<GenWeightInfoProduct, edm::InLumi>(tag); })) {
produces<GenWeightProduct>();
produces<GenWeightInfoProduct, edm::Transition::BeginLuminosityBlock>();
}
Expand All @@ -58,19 +63,33 @@ LHEWeightProductProducer::~LHEWeightProductProducer() {}

// ------------ method called to produce the data ------------
void LHEWeightProductProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
if (foundWeightProduct_)
if (foundWeightProduct_ || !hasLhe_)
return;

edm::Handle<LHEEventProduct> lheEventInfo;
iEvent.getByToken(lheEventToken_, lheEventInfo);
// Read weights from LHEEventProduct
for (auto& token : lheEventTokens_) {
iEvent.getByToken(token, lheEventInfo);
if (lheEventInfo.isValid()) {
break;
}
}

auto weightProduct = weightHelper_.weightProduct(lheEventInfo->weights(), lheEventInfo->originalXWGTUP());
iEvent.put(std::move(weightProduct));
}

void LHEWeightProductProducer::beginRun(edm::Run const& run, edm::EventSetup const& es) {
edm::Handle<LHERunInfoProduct> lheRunInfoHandle;
run.getByLabel(lheLabel_, lheRunInfoHandle);
for (auto& label : lheLabels_) {
run.getByLabel(label, lheRunInfoHandle);
if (lheRunInfoHandle.isValid()) {
hasLhe_ = true;
break;
}
}
if (!hasLhe_)
return;


typedef std::vector<LHERunInfoProduct::Header>::const_iterator header_cit;
LHERunInfoProduct::Header headerWeightInfo;
Expand All @@ -88,11 +107,18 @@ void LHEWeightProductProducer::endRun(edm::Run const& run, edm::EventSetup const

void LHEWeightProductProducer::beginLuminosityBlockProduce(edm::LuminosityBlock& lumi, edm::EventSetup const& es) {
edm::Handle<GenWeightInfoProduct> lheWeightInfoHandle;
lumi.getByToken(lheWeightInfoToken_, lheWeightInfoHandle);
if (lheWeightInfoHandle.isValid()) {
foundWeightProduct_ = true;
return;

for (auto& token : lheWeightInfoTokens_) {
lumi.getByToken(token, lheWeightInfoHandle);
if (lheWeightInfoHandle.isValid()) {
foundWeightProduct_ = true;
return;
}
}

if (!hasLhe_)
return;

weightHelper_.parseWeights();

auto weightInfoProduct = std::make_unique<GenWeightInfoProduct>();
Expand Down
34 changes: 14 additions & 20 deletions GeneratorInterface/Core/src/GenWeightHelper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ namespace gen {

void GenWeightHelper::parseWeightGroupsFromNames(std::vector<std::string> weightNames) {
parsedWeights_.clear();
size_t index = 0;
size_t groupIndex = 0;
int index = 0;
int groupIndex = -1;
int showerGroupIndex = -1;
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
Expand All @@ -30,32 +28,28 @@ namespace gen {
std::vector<std::string> subInfo;
boost::split(subInfo, i, boost::is_any_of("="));
if (subInfo.size() == 2) {
attributes[trim(subInfo[0])] = trim(subInfo[1]);
attributes[boost::algorithm::trim_copy(subInfo[0])] = boost::algorithm::trim_copy(subInfo[1]);
}
}
if (attributes["group"] != curGroup) {
curGroup = attributes["group"];
groupIndex++;
}
parsedWeights_.push_back({attributes["id"], index++, curGroup, text, attributes, groupIndex});
// Gen Weights can't have an ID, because they are just a std::vector<float> in the event
attributes["id"] = "";
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
{"", index, weightName, weightName, std::unordered_map<std::string, std::string>(), groupIndex});
if (isPartonShowerWeightGroup(parsedWeights_.back())) {
if (showerGroupIndex < 0)
showerGroupIndex = ++groupIndex;
parsedWeights_.back().wgtGroup_idx = showerGroupIndex; // 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"));
// }
index++;
}
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
4 changes: 2 additions & 2 deletions GeneratorInterface/Core/src/LHEWeightHelper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ namespace gen {

std::vector<std::string> nameAlts_ = {"name", "type"};

size_t weightIndex = 0;
size_t groupIndex = 0;
int weightIndex = 0;
int groupIndex = 0;
//for (auto* e = root->FirstChildElement(); e != nullptr; e = e->NextSiblingElement()) {
for (auto* e = xmlDoc.RootElement(); e != nullptr; e = e->NextSiblingElement()) {
std::string groupName = "";
Expand Down
50 changes: 28 additions & 22 deletions GeneratorInterface/Core/src/WeightHelper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ namespace gen {
}

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;
// But "Nominal" and "Baseline" weights in the PS group
return name.find("isr") != std::string::npos || name.find("fsr") != std::string::npos ||
name.find("nominal") != std::string::npos || name.find("baseline") != std::string::npos;
}

bool WeightHelper::isOrphanPdfWeightGroup(ParsedWeight& weight) {
Expand All @@ -29,7 +30,7 @@ namespace gen {
auto pairLHA = LHAPDF::lookupPDF(stoi(lhaidText));
// require pdf set to exist and it to be the first entry (ie 0)
// possibly change this requirement
if (pairLHA.first != "" && pairLHA.second == 0) {
if (!pairLHA.first.empty() && pairLHA.second == 0) {
weight.groupname = std::string(pairLHA.first);
return true;
}
Expand Down Expand Up @@ -82,8 +83,8 @@ namespace gen {
return "";
}

void WeightHelper::updateScaleInfo(const ParsedWeight& weight) {
auto& group = weightGroups_.back();
void WeightHelper::updateScaleInfo(const ParsedWeight& weight, int index) {
auto& group = weightGroups_[index];
auto& scaleGroup = dynamic_cast<gen::ScaleWeightGroupInfo&>(group);
std::string muRText = searchAttributes("mur", weight);
std::string muFText = searchAttributes("muf", weight);
Expand Down Expand Up @@ -117,8 +118,7 @@ namespace gen {
}
}

int WeightHelper::getLhapdfId(const ParsedWeight& weight) {
auto& pdfGroup = dynamic_cast<gen::PdfWeightGroupInfo&>(weightGroups_.back());
int WeightHelper::getLhapdfId(const ParsedWeight& weight, gen::PdfWeightGroupInfo& pdfGroup) {
std::string lhaidText = searchAttributes("pdf", weight);
if (!lhaidText.empty()) {
try {
Expand All @@ -134,9 +134,9 @@ namespace gen {
return -1;
}

void WeightHelper::updatePdfInfo(const ParsedWeight& weight) {
auto& pdfGroup = dynamic_cast<gen::PdfWeightGroupInfo&>(weightGroups_.back());
int lhaid = getLhapdfId(weight);
void WeightHelper::updatePdfInfo(const ParsedWeight& weight, int index) {
auto& pdfGroup = dynamic_cast<gen::PdfWeightGroupInfo&>(weightGroups_[index]);
int lhaid = getLhapdfId(weight, pdfGroup);
if (pdfGroup.getParentLhapdfId() < 0) {
int parentId = lhaid - LHAPDF::lookupPDF(lhaid).second;
pdfGroup.setParentLhapdfInfo(parentId);
Expand All @@ -157,8 +157,8 @@ namespace gen {
pdfGroup.addLhaid(lhaid);
}

void WeightHelper::updatePartonShowerInfo(const ParsedWeight& weight) {
auto& psGroup = dynamic_cast<gen::PartonShowerWeightGroupInfo&>(weightGroups_.back());
void WeightHelper::updatePartonShowerInfo(const ParsedWeight& weight, int index) {
auto& psGroup = dynamic_cast<gen::PartonShowerWeightGroupInfo&>(weightGroups_[index]);
bool isUp = true;
std::string subName = searchString("up", weight.id);
if (subName.empty()) {
Expand All @@ -177,7 +177,8 @@ namespace gen {
// Just add an empty product
if (weights.size() > 1) {
for (unsigned int i = 0; i < weights.size(); i++) {
addWeightToProduct(weightProduct, weights.at(i), "", i, weightGroupIndex);
std::string id = std::to_string(i);
addWeightToProduct(weightProduct, weights.at(i), id, i, weightGroupIndex);
}
}
return std::move(weightProduct);
Expand Down Expand Up @@ -312,29 +313,34 @@ namespace gen {

void WeightHelper::buildGroups() {
weightGroups_.clear();
size_t currentGroupIdx = -1;
int currentGroupIdx = -1;
for (auto& weight : parsedWeights_) {
if (currentGroupIdx != weight.wgtGroup_idx) {
if (weight.wgtGroup_idx < static_cast<int>(weightGroups_.size())) {
currentGroupIdx = weight.wgtGroup_idx;
}
else if ((currentGroupIdx+1) == weight.wgtGroup_idx) {
weightGroups_.push_back(*buildGroup(weight));
currentGroupIdx = weight.wgtGroup_idx;
}
else
throw std::range_error("Invalid group index " + currentGroupIdx);

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

0 comments on commit d9a125f

Please sign in to comment.