Skip to content

Commit

Permalink
Add PartonShower parsing for GenWeights
Browse files Browse the repository at this point in the history
  • Loading branch information
Dylan Teague committed Jul 31, 2020
1 parent 3bbd4de commit ba5be6c
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 3 deletions.
8 changes: 7 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,10 +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
6 changes: 4 additions & 2 deletions GeneratorInterface/Core/src/GenWeightHelper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,17 @@ namespace gen {
parsedWeights_.push_back({attributes["id"], index++, curGroup, text, attributes, groupIndex});
} else {
parsedWeights_.push_back(
{"", index++, weightName, weightName, std::unordered_map<std::string, std::string>(), groupIndex++});
{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"));
// }
}
buildGroups();
//printWeights();
printWeights();
}

inline std::string GenWeightHelper::trim(std::string &s) {
Expand Down
35 changes: 35 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 @@ -258,6 +283,12 @@ namespace gen {

} 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";
Expand All @@ -271,6 +302,8 @@ namespace gen {
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);

Expand Down Expand Up @@ -300,6 +333,8 @@ namespace gen {
updateScaleInfo(weight);
else if (group.weightType() == gen::WeightType::kPdfWeights)
updatePdfInfo(weight);
else if (group.weightType() == gen::WeightType::kPartonShowerWeights)
updatePartonShowerInfo(weight);
}
cleanupOrphanCentralWeight();
}
Expand Down
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
13 changes: 13 additions & 0 deletions SimDataFormats/GeneratorProducts/src/PartonShowerWeights.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,17 @@ namespace gen {
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

0 comments on commit ba5be6c

Please sign in to comment.