Skip to content

Commit

Permalink
Merge pull request cms-sw#18 from dteague/validXML
Browse files Browse the repository at this point in the history
Add option for returning from LHEweight parsing if not valid xml
  • Loading branch information
kdlong authored Aug 6, 2020
2 parents d9a125f + 4ff6aa0 commit 4570e6f
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 6 deletions.
3 changes: 2 additions & 1 deletion GeneratorInterface/Core/interface/LHEWeightHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ namespace gen {
void parseWeights();
bool isConsistent();
void swapHeaders();

void setFailIfInvalidXML(bool value) { failIfInvalidXML_ = value; }
private:
std::vector<std::string> headerLines_;
bool failIfInvalidXML_ = false;
};
} // namespace gen

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ LHEWeightProductProducer::LHEWeightProductProducer(const edm::ParameterSet& iCon
[this](const std::string& tag) { return mayConsume<GenWeightInfoProduct, edm::InLumi>(tag); })) {
produces<GenWeightProduct>();
produces<GenWeightInfoProduct, edm::Transition::BeginLuminosityBlock>();
weightHelper_.setFailIfInvalidXML(iConfig.getUntrackedParameter<bool>("failIfInvalidXML", false));
}

LHEWeightProductProducer::~LHEWeightProductProducer() {}
Expand Down
12 changes: 10 additions & 2 deletions GeneratorInterface/Core/src/LHEWeightHelper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <iostream>
#include <stdexcept>

using namespace tinyxml2;

Expand All @@ -11,7 +12,11 @@ namespace gen {
void LHEWeightHelper::parseWeights() {
parsedWeights_.clear();

if (!isConsistent()) {
if (!isConsistent() && failIfInvalidXML_) {
throw std::runtime_error( "XML in LHE is not consistent: Most likely, tags were swapped.\n" \
"To turn on fault fixing, use 'setFailIfInvalidXML(false)'\n" \
"WARNING: the tag swapping may lead to weights associated with the incorrect group");
} else if (!isConsistent()) {
swapHeaders();
}

Expand All @@ -29,7 +34,10 @@ namespace gen {
if (xmlError != 0) {
std::cerr << "Error in lhe xml file" << std::endl;
xmlDoc.PrintError();
return;
if(failIfInvalidXML_)
throw std::runtime_error("XML is unreadable because of above error.");
else
return;
}

std::vector<std::string> nameAlts_ = {"name", "type"};
Expand Down
5 changes: 3 additions & 2 deletions GeneratorInterface/Core/test/testGenWeightProducer_cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
'keep GenWeightInfoProduct_test*Weights*_*_*',])
)

#process.testLHEWeights = cms.EDProducer("LHEWeightProductProducer",
# lheSourceLabel = cms.string("externalLHEProducer"))
# process.testLHEWeights = cms.EDProducer("LHEWeightProductProducer",
# lheSourceLabel = cms.string("externalLHEProducer"),
# failIfValidXML = cms.untracked.bool(True))

process.testGenWeights = cms.EDProducer("GenWeightProductProducer",
genInfo = cms.InputTag("generator"),
Expand Down
3 changes: 2 additions & 1 deletion PhysicsTools/NanoAOD/python/nanogen_cff.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
genLumiInfoHeader = cms.InputTag("generator"))

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

Expand Down

0 comments on commit 4570e6f

Please sign in to comment.