Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions GeneratorInterface/Core/interface/WeightHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ namespace gen {
std::unique_ptr<GenWeightProduct> weightProduct(std::vector<T> weights, float w0);

void setModel(std::string model) { model_ = model; }
void setGuessPSWeightIdx(bool guessPSWeightIdx) {
PartonShowerWeightGroupInfo::setGuessPSWeightIdx(guessPSWeightIdx);
}
void addUnassociatedGroup() {
weightGroups_.push_back(std::make_unique<UnknownWeightGroupInfo>("unassociated"));
weightGroups_.back().setDescription("Weights missing or with invalid header meta data");
Expand Down Expand Up @@ -62,6 +65,7 @@ namespace gen {
void updateScaleInfo(gen::ScaleWeightGroupInfo& scaleGroup, const ParsedWeight& weight);
void updateMEParamInfo(const ParsedWeight& weight, int index);
void updatePdfInfo(gen::PdfWeightGroupInfo& pdfGroup, const ParsedWeight& weight);
void updatePartonShowerInfo(gen::PartonShowerWeightGroupInfo& psGroup, const ParsedWeight& weight);
void cleanupOrphanCentralWeight();
bool splitPdfWeight(ParsedWeight& weight);

Expand Down
5 changes: 3 additions & 2 deletions GeneratorInterface/Core/plugins/GenWeightProductProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ GenWeightProductProducer::GenWeightProductProducer(const edm::ParameterSet& iCon
mayConsume<GenLumiInfoHeader, edm::InLumi>(iConfig.getParameter<edm::InputTag>("genLumiInfoHeader"))) {
produces<GenWeightProduct>();
produces<GenWeightInfoProduct, edm::Transition::BeginLuminosityBlock>();
weightHelper_.setGuessPSWeightIdx(iConfig.getUntrackedParameter<bool>("guessPSWeightIdx", false));
}

GenWeightProductProducer::~GenWeightProductProducer() {}
Expand Down Expand Up @@ -82,8 +83,8 @@ void GenWeightProductProducer::beginLuminosityBlockProduce(edm::LuminosityBlock&

auto weightInfoProduct = std::make_unique<GenWeightInfoProduct>();
if (weightHelper_.weightGroups().size() == 0)
weightHelper_.addUnassociatedGroup();
weightHelper_.addUnassociatedGroup();

for (auto& weightGroup : weightHelper_.weightGroups()) {
weightInfoProduct->addWeightGroupInfo(std::make_unique<gen::WeightGroupInfo>(*weightGroup.clone()));
}
Expand Down
2 changes: 1 addition & 1 deletion GeneratorInterface/Core/src/GenWeightHelper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace gen {
{"", index, weightName, weightName, std::unordered_map<std::string, std::string>(), groupIndex});
if (isPartonShowerWeightGroup(parsedWeights_.back())) {
if (showerGroupIndex < 0) {
showerGroupIndex = ++groupIndex;
showerGroupIndex = ++groupIndex;
}
parsedWeights_.back().wgtGroup_idx = showerGroupIndex; // all parton showers are grouped together
}
Expand Down
74 changes: 55 additions & 19 deletions GeneratorInterface/Core/src/WeightHelper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ namespace gen {
try {
muR = std::stof(muRText);
muF = std::stof(muFText);
} catch (...) {
} catch (std::invalid_argument& e) {
if (debug_)
std::cout << "Tried to convert (" << muR << ", " << muF << ") to a int" << std::endl;
scaleGroup.setIsWellFormed(false);
scaleGroup.setWeightIsCorrupt();
return;
/// do something
}
Expand All @@ -101,9 +101,9 @@ namespace gen {
try {
int dynNum = std::stoi(dynNumText);
scaleGroup.setDyn(weight.index, weight.id, muR, muF, dynNum, dynType);
} catch (...) {
} catch (std::invalid_argument& e) {
std::cout << "Tried to convert (" << dynNumText << ") a int" << std::endl;
scaleGroup.setIsWellFormed(false);
scaleGroup.setWeightIsCorrupt();
/// do something here
}
}
Expand All @@ -112,8 +112,8 @@ namespace gen {
std::string lhaidText = searchAttributes("pdf", weight);
try {
scaleGroup.setLhaid(std::stoi(lhaidText));
} catch (...) {
scaleGroup.setLhaid(-2);
} catch (std::invalid_argument& e) {
scaleGroup.setLhaid(-1);
// do something here
}
}
Expand Down Expand Up @@ -157,6 +157,13 @@ namespace gen {
pdfGroup.addLhaid(lhaid);
}

void WeightHelper::updatePartonShowerInfo(gen::PartonShowerWeightGroupInfo& psGroup, const ParsedWeight& weight) {
if (psGroup.containedIds().size() == DEFAULT_PSWEIGHT_LENGTH)
psGroup.setIsWellFormed(true);
if (weight.content.find(":") != std::string::npos && weight.content.find("=") != std::string::npos)
psGroup.setNameIsPythiaSyntax(true);
}

bool WeightHelper::splitPdfWeight(ParsedWeight& weight) {
if (weightGroups_[weight.wgtGroup_idx].weightType() == gen::WeightType::kPdfWeights) {
auto& pdfGroup = dynamic_cast<gen::PdfWeightGroupInfo&>(weightGroups_[weight.wgtGroup_idx]);
Expand Down Expand Up @@ -252,8 +259,6 @@ namespace gen {
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) {
Expand Down Expand Up @@ -284,18 +289,48 @@ namespace gen {
std::cout << wgt.description() << "\n";
} else if (wgt.weightType() == gen::WeightType::kPartonShowerWeights) {
auto& wgtPS = dynamic_cast<gen::PartonShowerWeightGroupInfo&>(wgt);
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);
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;
}
}
}
if (!wgt.isWellFormed())
std::cout << "\033[0m";
}
}

Expand Down Expand Up @@ -341,9 +376,10 @@ namespace gen {
group.addContainedId(weight.index, weight.id, weight.content);
if (group.weightType() == gen::WeightType::kScaleWeights)
updateScaleInfo(dynamic_cast<gen::ScaleWeightGroupInfo&>(group), weight);
else if (group.weightType() == gen::WeightType::kPdfWeights) {
else if (group.weightType() == gen::WeightType::kPdfWeights)
updatePdfInfo(dynamic_cast<gen::PdfWeightGroupInfo&>(group), weight);
}
else if (group.weightType() == gen::WeightType::kPartonShowerWeights)
updatePartonShowerInfo(dynamic_cast<gen::PartonShowerWeightGroupInfo&>(group), weight);
}
cleanupOrphanCentralWeight();
}
Expand Down
6 changes: 4 additions & 2 deletions PhysicsTools/NanoAOD/python/nanogen_cff.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@

genWeights = cms.EDProducer("GenWeightProductProducer",
genInfo = cms.InputTag("generator"),
genLumiInfoHeader = cms.InputTag("generator"))
genLumiInfoHeader = cms.InputTag("generator"),
guessPSWeightIdx = cms.untracked.bool(True))

lheWeights = cms.EDProducer("LHEWeightProductProducer",
lheSourceLabels = cms.vstring(["externalLHEProducer", "source"]),
failIfInvalidXML = cms.untracked.bool(True)
failIfInvalidXML = cms.untracked.bool(True),

#lheWeightSourceLabels = cms.vstring(["externalLHEProducer", "source"])
)
'''
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,57 @@
#ifndef SimDataFormats_GeneratorProducts_PartonShowerWeightGroupInfo_h
#define SimDataFormats_GeneratorProducts_PartonShowerWeightGroupInfo_h

#include <unordered_map>

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

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

class PartonShowerWeightGroupInfo : public WeightGroupInfo {
public:
PartonShowerWeightGroupInfo() : PartonShowerWeightGroupInfo("") {}
PartonShowerWeightGroupInfo(std::string header, std::string name) : WeightGroupInfo(header, name) {
weightType_ = WeightType::kPartonShowerWeights;
}
PartonShowerWeightGroupInfo(std::string header, std::string name);
PartonShowerWeightGroupInfo(std::string header) : PartonShowerWeightGroupInfo(header, header) {}
PartonShowerWeightGroupInfo() : PartonShowerWeightGroupInfo("") {}
PartonShowerWeightGroupInfo(const PartonShowerWeightGroupInfo &other) { copy(other); }
virtual ~PartonShowerWeightGroupInfo() override {}
void copy(const PartonShowerWeightGroupInfo &other);
virtual PartonShowerWeightGroupInfo *clone() const override;
void setNameIsPythiaSyntax(bool isPythiaSyntax) { nameIsPythiaSyntax_ = isPythiaSyntax; }
bool nameIsPythiaSyntax(bool isPythiaSyntax) 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_; }
int psWeightIdxGuess(const std::string &varName) const;

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

const std::vector<std::string> expectedPythiaSyntax = {
"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",
"fsr:x2xg:murfac=0.5", "fsr:x2xg:murfac=2.0", "fsr:g2gg:cns=-2.0", "fsr:g2gg:cns=2.0",
"fsr:g2qq:cns=-2.0", "fsr:g2qq:cns=2.0", "fsr:q2qg:cns=-2.0", "fsr:q2qg:cns=2.0",
"fsr:x2xg:cns=-2.0", "fsr:x2xg:cns=2.0", "isr:murfac=0.707", "isr:murfac=1.414",
"isr:murfac=0.5", "isr:murfac=2.0", "isr:murfac=0.25", "isr:murfac=4.0",
"isr:g2gg:murfac=0.5", "isr:g2gg:murfac=2.0", "isr:g2qq:murfac=0.5", "isr:g2qq:murfac=2.0",
"isr:q2qg:murfac=0.5", "isr:q2qg:murfac=2.0", "isr:x2xg:murfac=0.5", "isr:x2xg:murfac=2.0",
"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 = {
"isrRedHi", "fsrRedHi", "isrRedLo", "fsrRedLo", "isrDefHi",
"fsrDefHi", "isrDefLo", "fsrDefLo", "isrConHi", "fsrConHi",
"isrConLo", "fsrConLo", "fsr_G2GG_muR_dn", "fsr_G2GG_muR_up", "fsr_G2QQ_muR_dn",
"fsr_G2QQ_muR_up", "fsr_Q2QG_muR_dn", "fsr_Q2QG_muR_up", "fsr_X2XG_muR_dn", "fsr_X2XG_muR_up",
"fsr_G2GG_cNS_dn", "fsr_G2GG_cNS_up", "fsr_G2QQ_cNS_dn", "fsr_G2QQ_cNS_up", "fsr_Q2QG_cNS_dn",
"fsr_Q2QG_cNS_up", "fsr_X2XG_cNS_dn", "fsr_X2XG_cNS_up", "isr_G2GG_muR_dn", "isr_G2GG_muR_up",
"isr_G2QQ_muR_dn", "isr_G2QQ_muR_up", "isr_Q2QG_muR_dn", "isr_Q2QG_muR_up", "isr_X2XG_muR_dn",
"isr_X2XG_muR_up", "isr_G2GG_cNS_dn", "isr_G2GG_cNS_up", "isr_G2QQ_cNS_dn", "isr_G2QQ_cNS_up",
"isr_Q2QG_cNS_dn", "isr_Q2QG_cNS_up", "isr_X2XG_cNS_dn", "isr_X2XG_cNS_up",
};
};
} // namespace gen

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace gen {
std::vector<size_t> muIndices_;
bool containsCentral_ = false;
int lhaid_ = -1;
bool hasAllWeights = false;
bool weightIsCorrupt_ = false;
// Dyn_scale
std::vector<std::string> dynNames_;
std::vector<std::vector<size_t>> dynVec_;
Expand Down Expand Up @@ -48,7 +48,10 @@ namespace gen {
virtual ScaleWeightGroupInfo* clone() const override;
bool containsCentralWeight() const { return containsCentral_; }
void addContainedId(int globalIndex, std::string id, std::string label, float muR, float muF);
bool isWellFormed() { return isWellFormed_ && hasAllWeights; }
void setWeightIsCorrupt() {
isWellFormed_ = false;
weightIsCorrupt_ = true;
}

void setMuRMuFIndex(int globalIndex, std::string id, float muR, float muF);
void setDyn(int globalIndex, std::string id, float muR, float muF, size_t dynNum, std::string dynName);
Expand Down
Loading