Skip to content

Commit

Permalink
Avoid make_unique and be careful copying
Browse files Browse the repository at this point in the history
  • Loading branch information
kdlong committed Nov 5, 2020
1 parent a83a25d commit e1539ba
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 29 deletions.
1 change: 1 addition & 0 deletions GeneratorInterface/Core/src/WeightHelper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ namespace gen {
labels.at(FIRST_PSWEIGHT_ENTRY).find('=') != std::string::npos) {
wgtPS.setNameIsPythiaSyntax(true);
}
std::cout << "Name is pythiaSynax? " << wgtPS.nameIsPythiaSyntax() << std::endl;
}
if (!wgt.isWellFormed())
std::cout << "\033[0m";
Expand Down
2 changes: 2 additions & 0 deletions PhysicsTools/NanoAOD/plugins/LHEWeightsTableProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,14 @@ void LHEWeightsTableProducer::addWeightGroupToTable(std::map<gen::WeightType, st
if (weightType == gen::WeightType::kScaleWeights) {
if (groupInfo.group->isWellFormed() && false) {
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) +
Expand Down
11 changes: 1 addition & 10 deletions PhysicsTools/NanoAOD/test/testNanoWeights.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,7 @@ def variableAndNumber(varName, tree):
rtfile = ROOT.TFile(args.inputFile)
tree = rtfile.Get("Events")
tree.GetEntry(0)
types = ["ScaleWeight", "PdfWeight", "MEParamWeight", "UnknownWeight", ]
variables = ["LHE"+t for t in types]
variables.append("GenPartonShowerWeight")
variables.extend(["Gen"+t for t in types])
variables = ["LHEScaleWeight", "LHEPdfWeight", "MEParamWeight", "UnknownWeight", "PSWeight", ]

for varName in variables:
variableAndNumber(varName, tree)
i = 1
altName = varName + "AltSet%i" % i
while hasattr(tree, altName):
variableAndNumber(altName, tree)
i = i+1
altName = varName + "AltSet%i" % i
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
namespace gen {
struct WeightGroupData {
size_t index;
std::unique_ptr<gen::WeightGroupInfo> group;
std::unique_ptr<const gen::WeightGroupInfo> group;
};

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

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

const edm::OwnVector<gen::WeightGroupInfo>& allWeightGroupsInfo() 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::unique_ptr<const gen::WeightGroupInfo> containingWeightGroupInfo(int index) const;
std::unique_ptr<const gen::WeightGroupInfo> orderedWeightGroupInfo(int index) const;
std::vector<gen::WeightGroupData> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,20 @@ namespace gen {
PartonShowerWeightGroupInfo() : PartonShowerWeightGroupInfo("") {}
PartonShowerWeightGroupInfo(std::string header, std::string name) : WeightGroupInfo(header, name) {
weightType_ = WeightType::kPartonShowerWeights;
nameIsPythiaSyntax_ = false;
}
PartonShowerWeightGroupInfo(std::string header) : PartonShowerWeightGroupInfo(header, header) {}
PartonShowerWeightGroupInfo(const PartonShowerWeightGroupInfo &other) { copy(other); }
~PartonShowerWeightGroupInfo() override {}
void copy(const PartonShowerWeightGroupInfo &other);
PartonShowerWeightGroupInfo *clone() const override;
void setNameIsPythiaSyntax(bool isPythiaSyntax) { nameIsPythiaSyntax_ = isPythiaSyntax; }
bool nameIsPythiaSyntax(bool isPythiaSyntax) const { return nameIsPythiaSyntax_; }
bool nameIsPythiaSyntax() const { return nameIsPythiaSyntax_; }
int variationIndex(bool isISR, bool isUp, PSVarType variationType, PSSplittingType splittingType) const;
int variationIndex(bool isISR, bool isUp, PSVarType variationType) const;

private:
bool nameIsPythiaSyntax_ = false;
bool nameIsPythiaSyntax_;
};
} // namespace gen

Expand Down
24 changes: 12 additions & 12 deletions SimDataFormats/GeneratorProducts/src/GenWeightInfoProduct.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,36 @@ const edm::OwnVector<gen::WeightGroupInfo>& GenWeightInfoProduct::allWeightGroup
return weightGroupsInfo_;
}

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

const std::unique_ptr<gen::WeightGroupInfo> GenWeightInfoProduct::orderedWeightGroupInfo(int weightGroupIndex) const {
std::unique_ptr<const gen::WeightGroupInfo> GenWeightInfoProduct::orderedWeightGroupInfo(int weightGroupIndex) const {
if (weightGroupIndex >= static_cast<int>(weightGroupsInfo_.size()))
throw std::range_error("Weight index out of range!");
return std::make_unique<gen::WeightGroupInfo>(weightGroupsInfo_[weightGroupIndex]);
return std::unique_ptr<const 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++) {
const gen::WeightGroupInfo& group = weightGroupsInfo_[i];
if (weightGroupsInfo_[i].weightType() == type)
matchingGroups.push_back({i, std::make_unique<gen::WeightGroupInfo>(weightGroupsInfo_[i])});
matchingGroups.push_back({i, std::unique_ptr<const gen::WeightGroupInfo>(&group)});
}
return matchingGroups;
}

std::vector<std::unique_ptr<gen::WeightGroupInfo>> GenWeightInfoProduct::weightGroupsByType(gen::WeightType type) const {
std::vector<std::unique_ptr<gen::WeightGroupInfo>> matchingGroups;
std::vector<gen::WeightGroupData> GenWeightInfoProduct::weightGroupsByType(gen::WeightType type) const {
std::vector<gen::WeightGroupData> matchingGroups;
for (size_t i = 0; i < weightGroupsInfo_.size(); i++) {
const gen::WeightGroupInfo& group = weightGroupsInfo_[i];
if (weightGroupsInfo_[i].weightType() == type)
matchingGroups.push_back(std::make_unique<gen::WeightGroupInfo>(weightGroupsInfo_[i]));
matchingGroups.push_back({i, std::unique_ptr<const gen::WeightGroupInfo>(&group)});
}
return matchingGroups;
}
Expand All @@ -58,8 +60,7 @@ std::optional<gen::WeightGroupData> GenWeightInfoProduct::pdfGroupWithIndexByLHA
std::vector<gen::WeightGroupData> pdfGroups = weightGroupsAndIndicesByType(gen::WeightType::kPdfWeights);

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

Expand All @@ -75,8 +76,7 @@ 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 =
std::make_unique<gen::PdfWeightGroupInfo>(*static_cast<gen::PdfWeightGroupInfo*>(data.group.release()));
auto pdfGroup = std::unique_ptr<const gen::PdfWeightGroupInfo>(static_cast<const gen::PdfWeightGroupInfo*>(data.group.release()));
return pdfGroup->containsLhapdfId(lhaid);
});
if (matchingPdfSet != pdfGroups.end()) {
Expand Down

0 comments on commit e1539ba

Please sign in to comment.