Skip to content

Commit

Permalink
MuonHLTSeedMVAClassifier: improved identification of the MVA files
Browse files Browse the repository at this point in the history
  • Loading branch information
mmusich committed May 28, 2024
1 parent e2e21e7 commit fb54b44
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 26 deletions.
18 changes: 18 additions & 0 deletions RecoMuon/TrackerSeedGenerator/interface/SeedMvaEstimator.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ namespace edm {
class FileInPath;
}

namespace {
enum inputIndexes {
kTsosErr0, // 0
kTsosErr2, // 1
kTsosErr5, // 2
kTsosDxdz, // 3
kTsosDydz, // 4
kTsosQbp, // 5
kDRdRL1SeedP, // 6
kDPhidRL1SeedP, // 7
kLastL1, // 8

kDRdRL2SeedP = 8, // 8
kDPhidRL2SeedP, // 9
kLastL2, // 10
};
} // namespace

class SeedMvaEstimator {
public:
SeedMvaEstimator(const edm::FileInPath& weightsfile,
Expand Down
47 changes: 39 additions & 8 deletions RecoMuon/TrackerSeedGenerator/plugins/MuonHLTSeedMVAClassifier.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// system include files
#include <memory>
#include <cmath>
#include <tinyxml2.h>

// user include files
#include "FWCore/Framework/interface/Frameworkfwd.h"
Expand All @@ -23,6 +24,8 @@
#include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
#include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"

#include "CommonTools/MVAUtils/interface/TMVAZipReader.h"

// TrajectorySeed
#include "DataFormats/TrajectorySeed/interface/TrajectorySeed.h"
#include "DataFormats/TrajectorySeed/interface/TrajectorySeedCollection.h"
Expand All @@ -44,6 +47,7 @@ class MuonHLTSeedMVAClassifier : public edm::stream::EDProducer<> {
~MuonHLTSeedMVAClassifier() override = default;

static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
bool checkMVAFileConsistency(const std::string& weightsFileFullPath, bool isFromL1) const;

private:
void produce(edm::Event&, const edm::EventSetup&) override;
Expand Down Expand Up @@ -87,6 +91,38 @@ class MuonHLTSeedMVAClassifier : public edm::stream::EDProducer<> {
const reco::RecoChargedCandidateCollection& l2Muons);
};

bool MuonHLTSeedMVAClassifier::checkMVAFileConsistency(const std::string& weightsFileFullPath,
const bool isFromL1) const {
tinyxml2::XMLDocument xmlDoc;
if (reco::details::hasEnding(weightsFileFullPath, ".xml")) {
xmlDoc.LoadFile(weightsFileFullPath.c_str());
} else {
edm::LogError("MuonHLTSeedMVAClassifier") << "unsupported file extension, it should be a .xml file!";
return false;
}
tinyxml2::XMLElement* root = xmlDoc.FirstChildElement("MethodSetup");
if (root == nullptr) {
edm::LogError("MuonHLTSeedMVAClassifier") << "could not retrieve the MethodSetup node from XML file!";
return false;
}

const auto& vars = root->FirstChildElement("Variables");
size_t n = 0;
if (vars != nullptr) {
for (tinyxml2::XMLElement* e = vars->FirstChildElement("Variable"); e != nullptr;
e = e->NextSiblingElement("Variable")) {
++n;
}
} else {
edm::LogError("MuonHLTSeedMVAClassifier") << "could not retrieve the Variables node from XML file!";
return false;
}

LogTrace("MuonHLTSeedMVAClassifier") << "MVA file:" << weightsFileFullPath.c_str() << " n Var:" << n;
bool condition = (isFromL1 && (n == inputIndexes::kLastL1)) || (!isFromL1 && (n == inputIndexes::kLastL2));
return condition;
}

MuonHLTSeedMVAClassifier::MuonHLTSeedMVAClassifier(const edm::ParameterSet& iConfig)
: seedToken_(consumes<TrajectorySeedCollection>(iConfig.getParameter<edm::InputTag>("src"))),
l1MuonToken_(consumes<l1t::MuonBxCollection>(iConfig.getParameter<edm::InputTag>("L1Muon"))),
Expand All @@ -111,14 +147,9 @@ MuonHLTSeedMVAClassifier::MuonHLTSeedMVAClassifier(const edm::ParameterSet& iCon
const auto& mvaFileBPath = mvaFileB_.fullPath();
const auto& mvaFileEPath = mvaFileE_.fullPath();

if (!isFromL1_ and
((mvaFileBPath.find("FromL1") != std::string::npos) or (mvaFileEPath.find("FromL1") != std::string::npos))) {
throw cms::Exception("ConfigurationError")
<< " isFromL1 parameter is False, but using FromL1 MVA files.\n Please check your configuration";
} else if (isFromL1_ and ((mvaFileBPath.find("FromL1") == std::string::npos) or
(mvaFileEPath.find("FromL1") == std::string::npos))) {
throw cms::Exception("ConfigurationError")
<< " isFromL1 parameter is True, but not using FromL1 MVA files.\n Please check your configuration";
if (!checkMVAFileConsistency(mvaFileBPath, isFromL1_) || !checkMVAFileConsistency(mvaFileEPath, isFromL1_)) {
throw cms::Exception("ConfigurationError") << " MVA files appear to be not consistent with the value of isFromL1 "
"parameter.\n Please check your configuration.";
}

if (!rejectAll_) {
Expand Down
18 changes: 0 additions & 18 deletions RecoMuon/TrackerSeedGenerator/src/SeedMvaEstimator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,6 @@ SeedMvaEstimator::SeedMvaEstimator(const edm::FileInPath& weightsfile,

SeedMvaEstimator::~SeedMvaEstimator() {}

namespace {
enum inputIndexes {
kTsosErr0, // 0
kTsosErr2, // 1
kTsosErr5, // 2
kTsosDxdz, // 3
kTsosDydz, // 4
kTsosQbp, // 5
kDRdRL1SeedP, // 6
kDPhidRL1SeedP, // 7
kLastL1, // 8

kDRdRL2SeedP = 8, // 8
kDPhidRL2SeedP, // 9
kLastL2, // 10
};
} // namespace

void SeedMvaEstimator::getL1MuonVariables(const GlobalVector& global_p,
const l1t::MuonBxCollection& l1Muons,
float& dR2dRL1SeedP,
Expand Down

0 comments on commit fb54b44

Please sign in to comment.