Skip to content

Commit

Permalink
Parton shower weights handle new naming scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
kdlong committed Oct 4, 2020
1 parent a073bf0 commit 8ca625f
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 29 deletions.
2 changes: 2 additions & 0 deletions GeneratorInterface/Core/interface/WeightHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ namespace gen {
// TODO: Make this only print from one thread a la
// https://github.com/kdlong/cmssw/blob/master/PhysicsTools/NanoAOD/plugins/GenWeightsTableProducer.cc#L1069
bool debug_ = true;
const unsigned int FIRST_PSWEIGHT_ENTRY= 2;
const unsigned int DEFAULT_PSWEIGHT_LENGTH = 46;
std::string model_;
std::vector<ParsedWeight> parsedWeights_;
std::map<std::string, std::string> currWeightAttributeMap_;
Expand Down
3 changes: 2 additions & 1 deletion GeneratorInterface/Core/src/GenWeightHelper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ namespace gen {
parsedWeights_.push_back(
{"", index, weightName, weightName, std::unordered_map<std::string, std::string>(), groupIndex});
if (isPartonShowerWeightGroup(parsedWeights_.back())) {
if (showerGroupIndex < 0)
if (showerGroupIndex < 0) {
showerGroupIndex = ++groupIndex;
}
parsedWeights_.back().wgtGroup_idx = showerGroupIndex; // all parton showers are grouped together
}
}
Expand Down
11 changes: 8 additions & 3 deletions GeneratorInterface/Core/src/WeightHelper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,14 @@ namespace gen {
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.upIndex(group);
std::cout << " - down " << wgtPS.downIndex(group) << std::endl;
if (wgtPS.containedIds().size() == DEFAULT_PSWEIGHT_LENGTH)
wgtPS.setIsWellFormed(true);

wgtPS.cacheWeightIndicesByLabel();
std::vector<std::string> labels = wgtPS.weightLabels();
if (labels.size() > FIRST_PSWEIGHT_ENTRY && labels.at(FIRST_PSWEIGHT_ENTRY).find(":") != std::string::npos &&
labels.at(FIRST_PSWEIGHT_ENTRY).find("=") != std::string::npos) {
wgtPS.setNameIsPythiaSyntax(true);
}
}
if (!wgt.isWellFormed())
Expand Down
16 changes: 8 additions & 8 deletions PhysicsTools/NanoAOD/plugins/LHEWeightsTableProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,10 @@ void LHEWeightsTableProducer::addWeightGroupToTable(std::map<gen::WeightType, st
gen::WeightType weightType = groupInfo.group->weightType();
std::string name = weightTypeNames_.at(weightType);
std::string label = "[" + std::to_string(typeCount[weightType]) + "] " + groupInfo.group->name();
auto& weights = allWeights.at(groupInfo.index);
label.append("[");
label.append(std::to_string(lheWeightTables[weightType].size()));//to append the start index of this set
label.append("]; ");
lheWeightTables[weightType].insert(lheWeightTables[weightType].end(), weights.begin(), weights.end());
weightVecsizes[weightType].emplace_back(weights.size());
auto& weights = allWeights.at(groupInfo.index);
if (weightType == gen::WeightType::kScaleWeights && groupInfo.group->isWellFormed() &&
groupInfo.group->nIdsContained() < 10) {
weights = orderedScaleWeights(weights, dynamic_cast<const gen::ScaleWeightGroupInfo*>(groupInfo.group));
Expand All @@ -289,6 +287,8 @@ void LHEWeightsTableProducer::addWeightGroupToTable(std::map<gen::WeightType, st
weights = getPreferredPSweights(weights, dynamic_cast<const gen::PartonShowerWeightGroupInfo*>(groupInfo.group));
label.append("PS weights (w_var / w_nominal); [0] is ISR=0.5 FSR=1; [1] is ISR=1 FSR=0.5; [2] is ISR=2 FSR=1; [3] is ISR=1 FSR=2");
}
lheWeightTables[weightType].insert(lheWeightTables[weightType].end(), weights.begin(), weights.end());
weightVecsizes[weightType].emplace_back(weights.size());
//else
// label.append(groupInfo.group->description());
if(weightlabels[weightType] == "")
Expand Down Expand Up @@ -336,11 +336,11 @@ std::vector<double> LHEWeightsTableProducer::getPreferredPSweights(const std::ve
const gen::PartonShowerWeightGroupInfo* pswV) const {
std::vector<double> psTosave;

double baseline = psWeights.at(pswV->weightIndexFromLabel("Baseline"));//at 1
psTosave.emplace_back( psWeights.at(pswV->weightIndexFromLabel("isrDefHi"))/baseline ); // at 6
psTosave.emplace_back( psWeights.at(pswV->weightIndexFromLabel("fsrDefHi"))/baseline ); // at 7
psTosave.emplace_back( psWeights.at(pswV->weightIndexFromLabel("isrDefLo"))/baseline ); // at 8
psTosave.emplace_back( psWeights.at(pswV->weightIndexFromLabel("fsrDefLo"))/baseline ); // at 9
double baseline = psWeights.at(pswV->weightIndexFromLabel("Baseline"));
psTosave.emplace_back(psWeights.at(pswV->variationIndex(true, true, gen::PSVarType::def))/baseline);
psTosave.emplace_back(psWeights.at(pswV->variationIndex(false, true, gen::PSVarType::def))/baseline);
psTosave.emplace_back(psWeights.at(pswV->variationIndex(true, false, gen::PSVarType::def))/baseline);
psTosave.emplace_back(psWeights.at(pswV->variationIndex(false, false, gen::PSVarType::def))/baseline);
return psTosave;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#include "SimDataFormats/GeneratorProducts/interface/WeightGroupInfo.h"

namespace gen {
enum class PSVarType { muR, cNS, con, def, red};
enum class PSSplittingType { combined, g2gg, x2xg, g2qq };

class PartonShowerWeightGroupInfo : public WeightGroupInfo {
public:
PartonShowerWeightGroupInfo() : PartonShowerWeightGroupInfo("") {}
Expand All @@ -17,22 +20,13 @@ namespace gen {
virtual ~PartonShowerWeightGroupInfo() override {}
void copy(const PartonShowerWeightGroupInfo &other);
virtual PartonShowerWeightGroupInfo *clone() const override;

// TODO: replace these general functions with specific ones
int upIndex(std::string weightName) {
int index = weightIndexFromLabel(weightName+"Hi");
return index >= 0 ? index : weightIndexFromLabel(weightName+"_up");
}
int downIndex(std::string weightName) {
int index = weightIndexFromLabel(weightName+"Low");
return index >= 0 ? index : weightIndexFromLabel(weightName+"_dn");
}
std::vector<std::string> getWeightNames() const { return weightNames; }
void setNameIsPythiaSyntax(bool isPythiaSyntax) { nameIsPythiaSyntax_ = isPythiaSyntax; }
bool nameIsPythiaSyntax(bool isPythiaSyntax) const { return nameIsPythiaSyntax_; }
int variationIndex(bool isISR, bool isUp, PSVarType variationType, PSSplittingType splittingType) const;
int variationIndex(bool isISR, bool isUp, PSVarType variationType) const;

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
bool nameIsPythiaSyntax_ = false;
};
} // namespace gen

Expand Down
5 changes: 3 additions & 2 deletions SimDataFormats/GeneratorProducts/interface/WeightGroupInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ namespace gen {
public:
WeightGroupInfo() : WeightGroupInfo("") {}
WeightGroupInfo(std::string header, std::string name)
: isWellFormed_(true), headerEntry_(header), name_(name), firstId_(-1), lastId_(-1) {}
: isWellFormed_(false), headerEntry_(header), name_(name), firstId_(-1), lastId_(-1) {}
WeightGroupInfo(std::string header)
: isWellFormed_(true), headerEntry_(header), name_(header), firstId_(-1), lastId_(-1) {}
: isWellFormed_(false), headerEntry_(header), name_(header), firstId_(-1), lastId_(-1) {}
WeightGroupInfo(const WeightGroupInfo& other) { copy(other); }
WeightGroupInfo& operator=(const WeightGroupInfo& other) {
copy(other);
Expand Down Expand Up @@ -85,6 +85,7 @@ namespace gen {
void setIsWellFormed(bool wellFormed) { isWellFormed_ = wellFormed; }
bool isWellFormed() const { return isWellFormed_; }
int weightIndexFromLabel(std::string weightLabel) const;
std::vector<std::string> weightLabels() const;

protected:
bool isWellFormed_;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,76 @@
#include "SimDataFormats/GeneratorProducts/interface/PartonShowerWeightGroupInfo.h"
#include <exception>
#include <iostream>

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

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

int PartonShowerWeightGroupInfo::variationIndex(bool isISR, bool isUp, PSVarType variationType) const {
return variationIndex(isISR, isUp, variationType, PSSplittingType::combined);
}

int PartonShowerWeightGroupInfo::variationIndex(bool isISR, bool isUp, PSVarType variationType,
PSSplittingType splittingType) const {
std::string label = isISR ? "isr" : "fsr";

if ((variationType == PSVarType::con || variationType == PSVarType::def || variationType == PSVarType::red)
&& splittingType != PSSplittingType::combined)
throw std::invalid_argument("VariationType must be muR or CNS if subprocess is specified");

std::string variation;
switch(variationType) {
case PSVarType::con:
variation = !nameIsPythiaSyntax_ ? "Con" : (isUp ? "murfac=4.0" : "murfac=0.25");
break;
case PSVarType::def:
variation = !nameIsPythiaSyntax_ ? "Def" : (isUp ? "murfac=2.0" : "murfac=0.5");
break;
case PSVarType::red:
variation = !nameIsPythiaSyntax_ ? "Red" : (isUp ? "murfac=1.414" : "murfac=0.707");
case PSVarType::muR:
variation = !nameIsPythiaSyntax_ ? "muR" : (isUp ? "murfac=2.0" : "murfac=0.5");
break;
case PSVarType::cNS:
variation = !nameIsPythiaSyntax_ ? "cNS" : (isUp ? "cns=2.0" : "murfac=-2.0");
break;
}

std::string splitting;
switch(splittingType) {
case PSSplittingType::g2gg:
splitting = !nameIsPythiaSyntax_ ? "G2GG" : "g2gg";
break;
case PSSplittingType::g2qq:
splitting = !nameIsPythiaSyntax_ ? "G2QQ" : "g2qq";
break;
case PSSplittingType::x2xg:
splitting = !nameIsPythiaSyntax_ ? "X2XG" : "x2xg";
break;
default:
break;
}

if (nameIsPythiaSyntax_) {
std::string app = splittingType != PSSplittingType::combined ? splitting + ":" + variation : variation;
label += ":" + app;
}
else {
if (splittingType != PSSplittingType::combined) {
label += variation + "_" + splitting + "_" + variation + (isUp ? "_up" : "_dn");
}
else
label += variation + (isUp ? "Hi" : "Lo");
}

return weightIndexFromLabel(label);

}

} // namespace gen
8 changes: 8 additions & 0 deletions SimDataFormats/GeneratorProducts/src/WeightGroupInfo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ namespace gen {
weightLabelsToIndices_[weight.label] = weight.localIndex;
}

std::vector<std::string> WeightGroupInfo::weightLabels() const {
std::vector<std::string> labels;
labels.reserve(idsContained_.size());
for (const auto& weight : idsContained_)
labels.push_back(weight.label);
return labels;
}

int WeightGroupInfo::weightIndexFromLabel(std::string weightLabel) const {
if (!weightLabelsToIndices_.empty()) {
if (weightLabelsToIndices_.find(weightLabel) != weightLabelsToIndices_.end())
Expand Down

0 comments on commit 8ca625f

Please sign in to comment.