Skip to content

Commit

Permalink
Make debugging configurable, fix bug with adding weights in nano
Browse files Browse the repository at this point in the history
  • Loading branch information
kdlong committed Nov 3, 2020
1 parent f381ae8 commit 93ecbf8
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 20 deletions.
3 changes: 2 additions & 1 deletion GeneratorInterface/Core/interface/WeightHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ namespace gen {
int addWeightToProduct(
std::unique_ptr<GenWeightProduct>& product, double weight, std::string name, int weightNum, int groupIndex);
int findContainingWeightGroup(std::string wgtId, int weightIndex, int previousGroupIndex);
void setDebug(bool value) { debug_ = value; }

protected:
// 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; //true;
bool debug_ = false;
const unsigned int FIRST_PSWEIGHT_ENTRY = 2;
const unsigned int DEFAULT_PSWEIGHT_LENGTH = 46;
std::string model_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ GenWeightProductProducer::GenWeightProductProducer(const edm::ParameterSet& iCon
genEventToken_(consumes<GenEventInfoProduct>(iConfig.getParameter<edm::InputTag>("genInfo"))),
genLumiInfoHeadTag_(
mayConsume<GenLumiInfoHeader, edm::InLumi>(iConfig.getParameter<edm::InputTag>("genLumiInfoHeader"))) {
weightHelper_.setDebug(iConfig.getUntrackedParameter<bool>("debug", false));
produces<GenWeightProduct>();
produces<GenWeightInfoProduct, edm::Transition::BeginLuminosityBlock>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ LHEWeightProductProducer::LHEWeightProductProducer(const edm::ParameterSet& iCon
produces<GenWeightProduct>();
produces<GenWeightInfoProduct, edm::Transition::BeginLuminosityBlock>();
weightHelper_.setFailIfInvalidXML(iConfig.getUntrackedParameter<bool>("failIfInvalidXML", false));
weightHelper_.setDebug(iConfig.getUntrackedParameter<bool>("debug", false));
}

LHEWeightProductProducer::~LHEWeightProductProducer() {}
Expand Down
10 changes: 5 additions & 5 deletions GeneratorInterface/Core/src/WeightHelper.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "GeneratorInterface/Core/interface/WeightHelper.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include <regex>

namespace gen {
Expand Down Expand Up @@ -222,7 +223,7 @@ namespace gen {
}
int entry = !isUnassociated ? group.weightVectorEntry(name, weightNum) : group.nIdsContained();
if (debug_)
std::cout << "Adding weight " << entry << " to group " << groupIndex << std::endl;
std::cout << "Adding weight " << entry << " to group " << groupIndex;
product->addWeight(weight, groupIndex, entry);
return groupIndex;
}
Expand Down Expand Up @@ -300,10 +301,9 @@ namespace gen {
}

std::unique_ptr<WeightGroupInfo> WeightHelper::buildGroup(ParsedWeight& weight) {
if (debug_) {
std::cout << "Building group for weight group " << weight.groupname << " weight content is " << weight.content
<< std::endl;
}
if (debug_)
std::cout << "Building group for weight group " << weight.groupname << " weight content is " << weight.content;

if (isScaleWeightGroup(weight))
return std::make_unique<ScaleWeightGroupInfo>(weight.groupname);
else if (isPdfWeightGroup(weight))
Expand Down
30 changes: 18 additions & 12 deletions PhysicsTools/NanoAOD/plugins/LHEWeightsTableProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,26 @@ public edm::global::EDProducer<edm::LuminosityBlockCache<WeightGroupsToStore>,
iLumi.getByToken(genWeightInfoToken_, genWeightInfoHandle);

std::unordered_map<gen::WeightType, int> storePerType;
for (size_t i = 0; i < weightgroups_.size(); i++)
for (size_t i = 0; i < weightgroups_.size(); i++) {
std::cout << i << " maxPerType " << maxGroupsPerType_.at(i) << std::endl;
storePerType[weightgroups_.at(i)] = maxGroupsPerType_.at(i);
}

WeightGroupsToStore weightsToStore;
for (auto weightType : gen::allWeightTypes) {
if (foundLheWeights) {
std::cout << "At least found the product\n";
auto lheWeights = weightDataPerType(lheWeightInfoHandle, weightType, storePerType[weightType]);
for (auto& w : lheWeights)
for (auto& w : lheWeights) {
std::cout << "Found an lhe group with name " << w.group->name() << std::endl;
weightsToStore.at(inLHE).push_back({w.index, std::move(w.group)});
}
}
auto genWeights = weightDataPerType(genWeightInfoHandle, weightType, storePerType[weightType]);
for (auto& w : genWeights)
for (auto& w : genWeights) {
std::cout << "Found a gen group with name " << w.group->name() << std::endl;
weightsToStore.at(inGen).push_back({w.index, std::move(w.group)});
}
}
return std::make_shared<WeightGroupsToStore>(weightsToStore);
}
Expand Down Expand Up @@ -279,11 +286,11 @@ void LHEWeightsTableProducer::addWeightGroupToTable(std::map<gen::WeightType, st
auto& weights = allWeights.at(groupInfo.index);
if (weightType == gen::WeightType::kScaleWeights)
if (groupInfo.group->isWellFormed()) {
weights = orderedScaleWeights(weights, static_cast<const gen::ScaleWeightGroupInfo*>(groupInfo.group.get()));
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)");
weights = orderedScaleWeights(weights, static_cast<const gen::ScaleWeightGroupInfo*>(groupInfo.group.get()));
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 {
size_t nstore = std::min<size_t>(gen::ScaleWeightGroupInfo::MIN_SCALE_VARIATIONS, weights.size());
weights = std::vector(weights.begin(), weights.begin()+nstore);
Expand All @@ -307,12 +314,11 @@ void LHEWeightsTableProducer::addWeightGroupToTable(std::map<gen::WeightType, st
WeightGroupDataContainer LHEWeightsTableProducer::weightDataPerType(edm::Handle<GenWeightInfoProduct>& weightsHandle,
gen::WeightType weightType,
int& maxStore) const {
WeightGroupDataContainer groups;
std::vector<gen::WeightGroupData> allgroups;
std::vector<gen::WeightGroupData> groups;
if (weightType == gen::WeightType::kPdfWeights && pdfIds_.size() > 0) {
allgroups = weightsHandle->pdfGroupsWithIndicesByLHAIDs(pdfIds_);
groups = weightsHandle->pdfGroupsWithIndicesByLHAIDs(pdfIds_);
} else
allgroups = weightsHandle->weightGroupsAndIndicesByType(weightType);
groups = weightsHandle->weightGroupsAndIndicesByType(weightType);

int toStore = maxStore;
if (maxStore < 0 || static_cast<int>(groups.size()) <= maxStore) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ namespace gen {
: WeightGroupInfo(header, name), muIndices_(MIN_SCALE_VARIATIONS, -1), dynVec_(MIN_SCALE_VARIATIONS) {
weightType_ = WeightType::kScaleWeights;
isFunctionalFormVar_ = false;
isWellFormed_ = true;
}
ScaleWeightGroupInfo(std::string header) : ScaleWeightGroupInfo(header, header) {}
ScaleWeightGroupInfo(const ScaleWeightGroupInfo& other) { copy(other); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ namespace gen {
std::vector<std::string> weightLabels() const;

protected:
bool isWellFormed_;
bool isWellFormed_ = false;
std::string headerEntry_;
std::string name_;
std::string description_;
Expand Down

0 comments on commit 93ecbf8

Please sign in to comment.