Skip to content

Commit

Permalink
Use unique_ptr rather than raw ptrs
Browse files Browse the repository at this point in the history
Still to be determined how to handle producer that really needs
shared_ptr
  • Loading branch information
kdlong committed Oct 6, 2020
1 parent 41c79e2 commit 5d2297c
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ void GenWeightProductProducer::beginLuminosityBlockProduce(edm::LuminosityBlock&
weightHelper_.addUnassociatedGroup();

for (auto& weightGroup : weightHelper_.weightGroups()) {
weightInfoProduct->addWeightGroupInfo(weightGroup.clone());
weightInfoProduct->addWeightGroupInfo(std::make_unique<gen::WeightGroupInfo>(*weightGroup.clone()));
}
iLumi.put(std::move(weightInfoProduct));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ void LHEWeightProductProducer::beginLuminosityBlockProduce(edm::LuminosityBlock&

auto weightInfoProduct = std::make_unique<GenWeightInfoProduct>();
for (auto& weightGroup : weightHelper_.weightGroups()) {
weightInfoProduct->addWeightGroupInfo(weightGroup.clone());
weightInfoProduct->addWeightGroupInfo(std::make_unique<gen::WeightGroupInfo>(*weightGroup.clone()));
}
lumi.put(std::move(weightInfoProduct));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ void ExternalLHEProducer::endRunProduce(edm::Run& run, edm::EventSetup const& es
void ExternalLHEProducer::beginLuminosityBlockProduce(edm::LuminosityBlock& lumi, edm::EventSetup const& es) {
auto weightInfoProduct = std::make_unique<GenWeightInfoProduct>();
for (auto& weightGroup : weightHelper_.weightGroups()) {
weightInfoProduct->addWeightGroupInfo(weightGroup.clone());
weightInfoProduct->addWeightGroupInfo(std::make_unique<gen::WeightGroupInfo>(*weightGroup.clone()));
}
lumi.put(std::move(weightInfoProduct));
}
Expand Down
39 changes: 25 additions & 14 deletions PhysicsTools/NanoAOD/plugins/LHEWeightsTableProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
#include <tinyxml2.h>

namespace {
typedef std::vector<gen::WeightGroupData> WeightGroupDataContainer;
typedef std::array<std::vector<gen::WeightGroupData>, 2> WeightGroupsToStore;
typedef std::vector<gen::SharedWeightGroupData> WeightGroupDataContainer;
typedef std::array<std::vector<gen::SharedWeightGroupData>, 2> WeightGroupsToStore;
} // namespace
using CounterMap = genCounter::CounterMap;
using Counter = genCounter::Counter;
Expand Down Expand Up @@ -80,11 +80,13 @@ public edm::global::EDProducer<edm::LuminosityBlockCache<WeightGroupsToStore>,
WeightGroupsToStore weightsToStore;
for (auto weightType : gen::allWeightTypes) {
if (foundLheWeights) {
auto lheWeights = weightDataPerType(lheWeightInfoHandle, weightType, storePerType[weightType]);
weightsToStore.at(inLHE).insert(weightsToStore.at(inLHE).end(), lheWeights.begin(), lheWeights.end());
auto lheWeights = weightDataPerType(lheWeightInfoHandle, weightType, storePerType[weightType]);
for (auto& w : lheWeights)
weightsToStore.at(inLHE).push_back({w.index, std::move(w.group)});
}
auto genWeights = weightDataPerType(genWeightInfoHandle, weightType, storePerType[weightType]);
weightsToStore.at(inGen).insert(weightsToStore.at(inGen).end(), genWeights.begin(), genWeights.end());
for (auto& w : genWeights)
weightsToStore.at(inGen).push_back({w.index, std::move(w.group)});
}
return std::make_shared<WeightGroupsToStore>(weightsToStore);
}
Expand Down Expand Up @@ -277,7 +279,7 @@ 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, dynamic_cast<const gen::ScaleWeightGroupInfo*>(groupInfo.group));
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;"
Expand All @@ -287,7 +289,7 @@ void LHEWeightsTableProducer::addWeightGroupToTable(std::map<gen::WeightType, st
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");
} else if (!storeAllPSweights_ && weightType == gen::WeightType::kPartonShowerWeights && groupInfo.group->isWellFormed()) {
weights = getPreferredPSweights(weights, dynamic_cast<const gen::PartonShowerWeightGroupInfo*>(groupInfo.group));
weights = getPreferredPSweights(weights, static_cast<const gen::PartonShowerWeightGroupInfo*>(groupInfo.group.get()));
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");
}
//else
Expand All @@ -306,18 +308,27 @@ void LHEWeightsTableProducer::addWeightGroupToTable(std::map<gen::WeightType, st
WeightGroupDataContainer LHEWeightsTableProducer::weightDataPerType(edm::Handle<GenWeightInfoProduct>& weightsHandle,
gen::WeightType weightType,
int& maxStore) const {
WeightGroupDataContainer group;
WeightGroupDataContainer groups;
std::vector<gen::WeightGroupData> allgroups;
if (weightType == gen::WeightType::kPdfWeights && pdfIds_.size() > 0) {
group = weightsHandle->pdfGroupsWithIndicesByLHAIDs(pdfIds_);
allgroups = weightsHandle->pdfGroupsWithIndicesByLHAIDs(pdfIds_);
} else
group = weightsHandle->weightGroupsAndIndicesByType(weightType);
allgroups = weightsHandle->weightGroupsAndIndicesByType(weightType);

if (maxStore < 0 || static_cast<int>(group.size()) <= maxStore) {
int toStore = maxStore;
if (maxStore < 0 || static_cast<int>(groups.size()) <= maxStore) {
// Modify size in case one type of weight is present in multiple products
maxStore -= group.size();
return group;
maxStore -= groups.size();
toStore = groups.size();
}
return std::vector(group.begin(), group.begin() + maxStore);

WeightGroupDataContainer out;
for (int i = 0; i < toStore; i++) {
auto& group = groups.at(i);
gen::SharedWeightGroupData temp = {group.index, std::move(group.group)};
out.push_back(temp);
}
return out;
}

std::vector<double> LHEWeightsTableProducer::orderedScaleWeights(const std::vector<double>& scaleWeights,
Expand Down
15 changes: 10 additions & 5 deletions SimDataFormats/GeneratorProducts/interface/GenWeightInfoProduct.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
namespace gen {
struct WeightGroupData {
size_t index;
const gen::WeightGroupInfo* group;
std::unique_ptr<gen::WeightGroupInfo> group;
};

struct SharedWeightGroupData {
size_t index;
std::shared_ptr<gen::WeightGroupInfo> group;
};
} // namespace gen

Expand All @@ -31,14 +36,14 @@ class GenWeightInfoProduct {
GenWeightInfoProduct& operator=(GenWeightInfoProduct&& other);

const edm::OwnVector<gen::WeightGroupInfo>& allWeightGroupsInfo() const;
const gen::WeightGroupInfo* containingWeightGroupInfo(int index) const;
const gen::WeightGroupInfo* orderedWeightGroupInfo(int index) const;
std::vector<gen::WeightGroupInfo*> weightGroupsByType(gen::WeightType type) const;
const std::unique_ptr<gen::WeightGroupInfo> containingWeightGroupInfo(int index) const;
const std::unique_ptr<gen::WeightGroupInfo> orderedWeightGroupInfo(int index) const;
std::vector<std::unique_ptr<gen::WeightGroupInfo>> weightGroupsByType(gen::WeightType type) const;
std::vector<int> weightGroupIndicesByType(gen::WeightType type) const;
std::vector<gen::WeightGroupData> weightGroupsAndIndicesByType(gen::WeightType type) const;
std::optional<gen::WeightGroupData> pdfGroupWithIndexByLHAID(int lhaid) const;
std::vector<gen::WeightGroupData> pdfGroupsWithIndicesByLHAIDs(const std::vector<int>& lhaids) const;
void addWeightGroupInfo(gen::WeightGroupInfo* info);
void addWeightGroupInfo(std::unique_ptr<gen::WeightGroupInfo> info);
const int numberOfGroups() const { return weightGroupsInfo_.size(); }

private:
Expand Down
33 changes: 18 additions & 15 deletions SimDataFormats/GeneratorProducts/src/GenWeightInfoProduct.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,46 +22,48 @@ const edm::OwnVector<gen::WeightGroupInfo>& GenWeightInfoProduct::allWeightGroup
return weightGroupsInfo_;
}

const gen::WeightGroupInfo* GenWeightInfoProduct::containingWeightGroupInfo(int index) const {
const std::unique_ptr<gen::WeightGroupInfo> GenWeightInfoProduct::containingWeightGroupInfo(int index) const {
for (const auto& weightGroup : weightGroupsInfo_) {
if (weightGroup.indexInRange(index))
return &weightGroup;
return std::make_unique<gen::WeightGroupInfo>(weightGroup);
}
throw std::domain_error("Failed to find containing weight group");
}

const gen::WeightGroupInfo* GenWeightInfoProduct::orderedWeightGroupInfo(int weightGroupIndex) const {
const std::unique_ptr<gen::WeightGroupInfo> GenWeightInfoProduct::orderedWeightGroupInfo(int weightGroupIndex) const {
if (weightGroupIndex >= static_cast<int>(weightGroupsInfo_.size()))
throw std::range_error("Weight index out of range!");
return &weightGroupsInfo_[weightGroupIndex];
return std::make_unique<gen::WeightGroupInfo>(weightGroupsInfo_[weightGroupIndex]);
}

std::vector<gen::WeightGroupData> GenWeightInfoProduct::weightGroupsAndIndicesByType(gen::WeightType type) const {
std::vector<gen::WeightGroupData> matchingGroups;
for (size_t i = 0; i < weightGroupsInfo_.size(); i++) {
if (weightGroupsInfo_[i].weightType() == type)
matchingGroups.push_back({i, weightGroupsInfo_[i].clone()});
matchingGroups.push_back({i, std::make_unique<gen::WeightGroupInfo>(weightGroupsInfo_[i])});
}
return matchingGroups;
}

std::vector<gen::WeightGroupInfo*> GenWeightInfoProduct::weightGroupsByType(gen::WeightType type) const {
std::vector<gen::WeightGroupInfo*> matchingGroups;
std::vector<std::unique_ptr<gen::WeightGroupInfo>> GenWeightInfoProduct::weightGroupsByType(gen::WeightType type) const {
std::vector<std::unique_ptr<gen::WeightGroupInfo>> matchingGroups;
for (size_t i = 0; i < weightGroupsInfo_.size(); i++) {
if (weightGroupsInfo_[i].weightType() == type)
matchingGroups.push_back(weightGroupsInfo_[i].clone());
matchingGroups.push_back(std::make_unique<gen::WeightGroupInfo>(weightGroupsInfo_[i]));
}
return matchingGroups;
}

std::optional<gen::WeightGroupData> GenWeightInfoProduct::pdfGroupWithIndexByLHAID(int lhaid) const {
auto pdfGroups = weightGroupsAndIndicesByType(gen::WeightType::kPdfWeights);
std::vector<gen::WeightGroupData> pdfGroups = weightGroupsAndIndicesByType(gen::WeightType::kPdfWeights);

auto matchingPdfSet = std::find_if(pdfGroups.begin(), pdfGroups.end(), [lhaid](gen::WeightGroupData& data) {
auto pdfGroup = dynamic_cast<const gen::PdfWeightGroupInfo*>(data.group);
auto pdfGroup = std::make_unique<gen::PdfWeightGroupInfo>(*static_cast<gen::PdfWeightGroupInfo*>(data.group.release()));
return pdfGroup->containsLhapdfId(lhaid);
});
return matchingPdfSet != pdfGroups.end() ? std::optional<gen::WeightGroupData>(*matchingPdfSet) : std::nullopt;

return matchingPdfSet == pdfGroups.end() ? std::nullopt :
std::optional<gen::WeightGroupData>({matchingPdfSet->index, std::move(matchingPdfSet->group)});
}

std::vector<gen::WeightGroupData> GenWeightInfoProduct::pdfGroupsWithIndicesByLHAIDs(
Expand All @@ -71,11 +73,12 @@ std::vector<gen::WeightGroupData> GenWeightInfoProduct::pdfGroupsWithIndicesByLH

for (auto lhaid : lhaids) {
auto matchingPdfSet = std::find_if(pdfGroups.begin(), pdfGroups.end(), [lhaid](gen::WeightGroupData& data) {
auto pdfGroup = dynamic_cast<const gen::PdfWeightGroupInfo*>(data.group);
auto pdfGroup = std::make_unique<gen::PdfWeightGroupInfo>(*static_cast<gen::PdfWeightGroupInfo*>(data.group.release()));
return pdfGroup->containsLhapdfId(lhaid);
});
if (matchingPdfSet != pdfGroups.end())
pdfGroups.push_back(*matchingPdfSet);
if (matchingPdfSet != pdfGroups.end()) {
pdfGroups.push_back({matchingPdfSet->index, std::move(matchingPdfSet->group)});
}
}

return pdfGroups;
Expand All @@ -90,4 +93,4 @@ std::vector<int> GenWeightInfoProduct::weightGroupIndicesByType(gen::WeightType
return matchingGroupIndices;
}

void GenWeightInfoProduct::addWeightGroupInfo(gen::WeightGroupInfo* info) { weightGroupsInfo_.push_back(info); }
void GenWeightInfoProduct::addWeightGroupInfo(std::unique_ptr<gen::WeightGroupInfo> info) { weightGroupsInfo_.push_back(std::move(info)); }

0 comments on commit 5d2297c

Please sign in to comment.