Skip to content

Commit

Permalink
Cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
Dylan Teague committed Nov 9, 2020
1 parent c5d57f8 commit db98fc4
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ LHEWeightProductProducer::LHEWeightProductProducer(const edm::ParameterSet& iCon
produces<GenWeightInfoProduct, edm::Transition::BeginLuminosityBlock>();
weightHelper_.setFailIfInvalidXML(iConfig.getUntrackedParameter<bool>("failIfInvalidXML", false));
weightHelper_.setDebug(iConfig.getUntrackedParameter<bool>("debug", false));
weightHelper_.setGuessPSWeightIdx(iConfig.getUntrackedParameter<bool>("guessPSWeightIdx", false));
}

LHEWeightProductProducer::~LHEWeightProductProducer() {}
Expand Down
2 changes: 0 additions & 2 deletions GeneratorInterface/Core/src/GenWeightHelper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ namespace gen {
return;

for (std::string weightName : weightNames) {
std::cout << weightName << std::endl;
if (weightName.find("LHE") != std::string::npos) {
// Parse as usual, this is the SUSY workflow
std::vector<std::string> info;
Expand All @@ -32,7 +31,6 @@ namespace gen {
attributes[boost::algorithm::trim_copy(subInfo[0])] = boost::algorithm::trim_copy(subInfo[1]);
}
}
std::cout << "group: " << attributes["group"] << std::endl;
if (attributes["group"] != curGroup) {
curGroup = attributes["group"];
groupIndex++;
Expand Down
41 changes: 1 addition & 40 deletions GeneratorInterface/Core/src/WeightHelper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -292,46 +292,7 @@ namespace gen {
auto& wgtPS = dynamic_cast<gen::PartonShowerWeightGroupInfo&>(wgt);
std::vector<std::string> labels = wgtPS.weightLabels();
wgtPS.cacheWeightIndicesByLabel();

auto vars = {std::make_pair<bool, bool>(true, true),
std::make_pair<bool, bool>(true, false),
std::make_pair<bool, bool>(false, true),
std::make_pair<bool, bool>(false, false)};
typedef gen::PSVarType varType;
typedef gen::PSSplittingType sptType;
std::vector<std::pair<varType, sptType>> ps_pairs = {
{varType::def, sptType::combined},
{varType::red, sptType::combined},
{varType::con, sptType::combined},
{varType::muR, sptType::g2gg},
{varType::muR, sptType::g2qq},
{varType::muR, sptType::q2qg},
{varType::muR, sptType::x2xg},
{varType::cNS, sptType::g2gg},
{varType::cNS, sptType::g2qq},
{varType::cNS, sptType::q2qg},
{varType::cNS, sptType::x2xg},
};
std::map<varType, std::string> varTypeMap = {
{varType::muR, "muR"},
{varType::cNS, "cNS"},
{varType::con, "con"},
{varType::def, "def"},
{varType::red, "red"},
};
std::map<sptType, std::string> splTypeMap = {
{sptType::combined, "combined"}, {sptType::g2gg, "g2gg"}, {sptType::x2xg, "x2xg"}, {sptType::g2qq, "g2qq"}};

for (auto [vartype, spttype] : ps_pairs) {
for (auto var : vars) {
int idx = wgtPS.variationIndex(var.first, var.second, vartype, spttype);
if (idx == -1)
continue;
std::cout << varTypeMap[vartype] << " " << splTypeMap[spttype] << "(" << var.first << ", " << var.second
<< "): " << idx << " - " << labels.at(idx) << std::endl;
}
}
std::cout << "Name is pythiaSynax? " << wgtPS.nameIsPythiaSyntax() << std::endl;
wgtPS.printVariables();
}
}
}
Expand Down
33 changes: 18 additions & 15 deletions PhysicsTools/NanoAOD/interface/GenWeightCounters.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,24 @@ namespace genCounter {
//incPSOnly(w0, wPS);
}

void mergeSumMap(const Counter& other) {
num_ += other.num_;
sumw_ += other.sumw_;
sumw2_ += other.sumw2_;
//if weightMap_ for "this" is empty, create map elements with empty
//vectors before merging
if (weightSumMap_.empty() && !other.weightSumMap_.empty()) {
for (auto& wmap : other.weightSumMap_) {
std::vector<long double> temp;
weightSumMap_.insert({wmap.first, temp});
void mergeSumMap(const Counter& other) {
num_ += other.num_;
sumw_ += other.sumw_;
sumw2_ += other.sumw2_;
//if weightMap_ for "this" is empty, create map elements with empty
//vectors before merging
if(weightSumMap_.empty() && !other.weightSumMap_.empty()) {
for(auto& wmap : other.weightSumMap_) {
std::vector<long double> temp;
weightSumMap_.insert({wmap.first, temp});
}
}

for(auto& wmap : weightSumMap_) {
if (other.weightSumMap_.find(wmap.first) != other.weightSumMap_.end())
mergeSumVectors(wmap.second, other.weightSumMap_.at(wmap.first));
}
}
for (auto& wmap : weightSumMap_) {
mergeSumVectors(wmap.second, other.weightSumMap_.at(wmap.first));
}
}

//private:
// the counters
Expand All @@ -77,8 +79,9 @@ namespace genCounter {
std::string active_label = "";

void mergeSumMap(const CounterMap& other) {
for (const auto& y : other.countermap)
for (const auto& y : other.countermap) {
countermap[y.first].mergeSumMap(y.second);
}
active_el = nullptr;
}

Expand Down
9 changes: 2 additions & 7 deletions PhysicsTools/NanoAOD/plugins/LHEWeightsTableProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -284,26 +284,21 @@ void LHEWeightsTableProducer::addWeightGroupToTable(std::map<gen::WeightType, st
label.append(std::to_string(lheWeightTables[weightType].size())); //to append the start index of this set
label.append("]; ");
auto& weights = allWeights.at(groupInfo.index);
//std::cout << "Group name is " << groupInfo.group->name() << " is it wellFormed? " << groupInfo.group->isWellFormed() << std::endl;
if (weightType == gen::WeightType::kScaleWeights) {
if (groupInfo.group->isWellFormed() && false) {
if (groupInfo.group->isWellFormed()) {
const auto scaleGroup = *static_cast<const gen::ScaleWeightGroupInfo*>(groupInfo.group.get());
std::cout << "They're well formed, will be ordered as expected\n";
weights = orderedScaleWeights(weights, scaleGroup);
label.append(
"[1] is mur=0.5 muf=1; [2] is mur=0.5 muf=2; [3] is mur=1 muf=0.5 ;"
" [4] is mur=1 muf=1; [5] is mur=1 muf=2; [6] is mur=2 muf=0.5;"
" [7] is mur=2 muf=1 ; [8] is mur=2 muf=2)");
} else {
std::cout << "NOT WELL FORMED!\n";
size_t nstore = std::min<size_t>(gen::ScaleWeightGroupInfo::MIN_SCALE_VARIATIONS, weights.size());
weights = std::vector(weights.begin(), weights.begin() + nstore);
label.append("WARNING: Unexpected format found. Contains first " + std::to_string(nstore) +
" elements of weights vector, unordered");
}
// TODO: Handle storeAllWeights and !isWellFormed
} else if (!storeAllPSweights_ && weightType == gen::WeightType::kPartonShowerWeights &&
groupInfo.group->isWellFormed()) {
} else if (!storeAllPSweights_ && weightType == gen::WeightType::kPartonShowerWeights) { // && groupInfo.group->isWellFormed()
const auto psGroup = *static_cast<const gen::PartonShowerWeightGroupInfo*>(groupInfo.group.get());
weights = preferredPSweights(weights, psGroup);
label.append(
Expand Down
2 changes: 1 addition & 1 deletion PhysicsTools/NanoAOD/python/nanogen_cff.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
lheWeights = cms.EDProducer("LHEWeightProductProducer",
lheSourceLabels = cms.vstring(["externalLHEProducer", "source"]),
failIfInvalidXML = cms.untracked.bool(True),

guessPSWeightIdx = cms.untracked.bool(True)
#lheWeightSourceLabels = cms.vstring(["externalLHEProducer", "source"])
)
'''
Expand Down
Original file line number Diff line number Diff line change
@@ -1,48 +1,35 @@
#ifndef SimDataFormats_GeneratorProducts_PartonShowerWeightGroupInfo_h
#define SimDataFormats_GeneratorProducts_PartonShowerWeightGroupInfo_h

#include <map>

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

namespace gen {
enum class PSVarType { muR, cNS, con, def, red, alphaS, LAST };
enum class PSSplittingType { combined, g2gg, x2xg, g2qq, q2qg };
typedef std::pair<PSVarType, PSSplittingType> PSPair;

struct PSPairHash {
std::size_t operator()(const PSPair &pair) const {
return static_cast<std::size_t>(pair.first) * static_cast<std::size_t>(PSVarType::LAST) +
static_cast<std::size_t>(pair.second);
}
};

class PartonShowerWeightGroupInfo : public WeightGroupInfo {
public:
PartonShowerWeightGroupInfo() : PartonShowerWeightGroupInfo("") {}
PartonShowerWeightGroupInfo(std::string header, std::string name) : WeightGroupInfo(header, name) {
weightType_ = WeightType::kPartonShowerWeights;
nameIsPythiaSyntax_ = false;
}
PartonShowerWeightGroupInfo(std::string header, std::string name);
PartonShowerWeightGroupInfo(std::string header) : PartonShowerWeightGroupInfo(header, header) {}
PartonShowerWeightGroupInfo() : PartonShowerWeightGroupInfo("") {}
PartonShowerWeightGroupInfo(const PartonShowerWeightGroupInfo &other) { copy(other); }
~PartonShowerWeightGroupInfo() override {}
virtual ~PartonShowerWeightGroupInfo() override {}
void copy(const PartonShowerWeightGroupInfo &other);
PartonShowerWeightGroupInfo *clone() const override;
virtual PartonShowerWeightGroupInfo *clone() const override;
void setNameIsPythiaSyntax(bool isPythiaSyntax) { nameIsPythiaSyntax_ = isPythiaSyntax; }
bool nameIsPythiaSyntax() const { return nameIsPythiaSyntax_; }
int variationIndex(bool isISR, bool isUp, PSVarType variationType, PSSplittingType splittingType) const;
std::string variationName(bool isISR, bool isUp, PSVarType variationType, PSSplittingType splittingType) const;
int variationIndex(bool isISR, bool isUp, PSVarType variationType) const;
static void setGuessPSWeightIdx(bool guessPSWeightIdx) { guessPSWeightIdx_ = guessPSWeightIdx_; }
static void setGuessPSWeightIdx(bool guessPSWeightIdx) { guessPSWeightIdx_ = guessPSWeightIdx; }
int psWeightIdxGuess(const std::string &varName) const;
void printVariables() const;

private:
bool nameIsPythiaSyntax_ = false;
static inline bool guessPSWeightIdx_ = false;

const std::vector<std::string> expectedPythiaSyntax = {
const std::vector<std::string> expectedOrderPythiaSyntax_ = {
"fsr:murfac=0.707", "fsr:murfac=1.414", "fsr:murfac=0.5", "fsr:murfac=2.0",
"fsr:murfac=0.25", "fsr:murfac=4.0", "fsr:g2gg:murfac=0.5", "fsr:g2gg:murfac=2.0",
"fsr:g2qq:murfac=0.5", "fsr:g2qq:murfac=2.0", "fsr:q2qg:murfac=0.5", "fsr:q2qg:murfac=2.0",
Expand All @@ -55,7 +42,7 @@ namespace gen {
"isr:g2gg:cns=-2.0", "isr:g2gg:cns=2.0", "isr:g2qq:cns=-2.0", "isr:g2qq:cns=2.0",
"isr:q2qg:cns=-2.0", "isr:q2qg:cns=2.0", "isr:x2xg:cns=-2.0", "isr:x2xg:cns=2.0",
};
const std::vector<std::string> expectedOrder = {
const std::vector<std::string> expectedOrder_ = {
"isrRedHi", "fsrRedHi", "isrRedLo", "fsrRedLo", "isrDefHi",
"fsrDefHi", "isrDefLo", "fsrDefLo", "isrConHi", "fsrConHi",
"isrConLo", "fsrConLo", "fsr_G2GG_muR_dn", "fsr_G2GG_muR_up", "fsr_G2QQ_muR_dn",
Expand Down
75 changes: 38 additions & 37 deletions SimDataFormats/GeneratorProducts/interface/WeightGroupInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,45 +46,46 @@ namespace gen {
: isWellFormed_(false), headerEntry_(header), name_(name), firstId_(-1), lastId_(-1) {}
WeightGroupInfo(std::string header)
: isWellFormed_(false), headerEntry_(header), name_(header), firstId_(-1), lastId_(-1) {}
WeightGroupInfo(const WeightGroupInfo& other) { copy(other); }
WeightGroupInfo& operator=(const WeightGroupInfo& other) {
copy(other);
return *this;
}
virtual ~WeightGroupInfo(){};
void copy(const WeightGroupInfo& other);
virtual WeightGroupInfo* clone() const;
WeightMetaInfo weightMetaInfo(int weightEntry) const;
WeightMetaInfo weightMetaInfoByGlobalIndex(std::string wgtId, int weightEntry) const;
int weightVectorEntry(std::string& wgtId) const;
bool containsWeight(std::string& wgtId, int weightEntry) const;
int weightVectorEntry(std::string& wgtId, int weightEntry) const;
void addContainedId(int weightEntry, std::string id, std::string label);
std::vector<WeightMetaInfo> containedIds() const;
bool indexInRange(int index) const;
WeightGroupInfo(const WeightGroupInfo& other) {
copy(other); }
WeightGroupInfo& operator=(const WeightGroupInfo& other) {
copy(other);
return *this;
}
virtual ~WeightGroupInfo(){};
void copy(const WeightGroupInfo& other);
virtual WeightGroupInfo* clone() const;
WeightMetaInfo weightMetaInfo(int weightEntry) const;
WeightMetaInfo weightMetaInfoByGlobalIndex(std::string wgtId, int weightEntry) const;
int weightVectorEntry(std::string& wgtId) const;
bool containsWeight(std::string& wgtId, int weightEntry) const;
int weightVectorEntry(std::string& wgtId, int weightEntry) const;
void addContainedId(int weightEntry, std::string id, std::string label);
std::vector<WeightMetaInfo> containedIds() const;
bool indexInRange(int index) const;

void setName(std::string name) { name_ = name; }
void setDescription(std::string description) { description_ = description; }
void appendDescription(std::string description) { description_ += description; }
void setHeaderEntry(std::string header) { headerEntry_ = header; }
void setWeightType(WeightType type) { weightType_ = type; }
void setFirstId(int firstId) { firstId_ = firstId; }
void setLastId(int lastId) { lastId_ = lastId; }
// Call before doing lots of searches by label
void cacheWeightIndicesByLabel();
void setName(std::string name) { name_ = name; }
void setDescription(std::string description) { description_ = description; }
void appendDescription(std::string description) { description_ += description; }
void setHeaderEntry(std::string header) { headerEntry_ = header; }
void setWeightType(WeightType type) { weightType_ = type; }
void setFirstId(int firstId) { firstId_ = firstId; }
void setLastId(int lastId) { lastId_ = lastId; }
// Call before doing lots of searches by label
void cacheWeightIndicesByLabel();

std::string name() const { return name_; }
std::string description() const { return description_; }
std::string headerEntry() const { return headerEntry_; }
WeightType weightType() const { return weightType_; }
std::vector<WeightMetaInfo> idsContained() const { return idsContained_; }
size_t nIdsContained() const { return idsContained_.size(); }
int firstId() const { return firstId_; }
int lastId() const { return lastId_; }
// Store whether the group was fully parsed succesfully
void setIsWellFormed(bool wellFormed) { isWellFormed_ = wellFormed; }
bool isWellFormed() const { return isWellFormed_; }
int weightIndexFromLabel(std::string weightLabel) const;
std::string name() const { return name_; }
std::string description() const { return description_; }
std::string headerEntry() const { return headerEntry_; }
WeightType weightType() const { return weightType_; }
std::vector<WeightMetaInfo> idsContained() const { return idsContained_; }
size_t nIdsContained() const { return idsContained_.size(); }
int firstId() const { return firstId_; }
int lastId() const { return lastId_; }
// Store whether the group was fully parsed succesfully
void setIsWellFormed(bool wellFormed) { isWellFormed_ = wellFormed; }
bool isWellFormed() const { return isWellFormed_; }
int weightIndexFromLabel(std::string weightLabel) const;
std::vector<std::string> weightLabels() const;

protected:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ namespace gen {
// Guess PS idx if not in label list
if (wgtIdx == -1 && guessPSWeightIdx_)
wgtIdx = psWeightIdxGuess(varName);

return wgtIdx;
}

Expand Down Expand Up @@ -96,15 +95,27 @@ namespace gen {
int PartonShowerWeightGroupInfo::psWeightIdxGuess(const std::string& varName) const {
int wgtIdx;
if (nameIsPythiaSyntax_) {
auto wgtIter = std::find(expectedPythiaSyntax.begin(), expectedPythiaSyntax.end(), varName);
wgtIdx = wgtIter - expectedPythiaSyntax.begin() + 2;
auto wgtIter = std::find(expectedOrderPythiaSyntax_.begin(), expectedOrderPythiaSyntax_.end(), varName);
wgtIdx = wgtIter - expectedOrderPythiaSyntax_.begin() + 2;
} else {
auto wgtIter = std::find(expectedOrder.begin(), expectedOrder.end(), varName);
wgtIdx = wgtIter - expectedOrder.begin() + 2;
auto wgtIter = std::find(expectedOrder_.begin(), expectedOrder_.end(), varName);
wgtIdx = wgtIter - expectedOrder_.begin() + 2;
}
if (wgtIdx >= (int)containedIds().size())
wgtIdx = -1;
return wgtIdx;
}

void PartonShowerWeightGroupInfo::printVariables() const {
const auto& variations = (nameIsPythiaSyntax_) ? expectedOrderPythiaSyntax_ : expectedOrder_;
for (auto varName: variations) {
int wgtIdx = weightIndexFromLabel(varName);
// Guess PS idx if not in label list
if (wgtIdx == -1 && guessPSWeightIdx_)
wgtIdx = psWeightIdxGuess(varName);
if (wgtIdx != -1)
std::cout << varName << " : " << wgtIdx << std::endl;
}
}

} // namespace gen

0 comments on commit db98fc4

Please sign in to comment.