diff --git a/RecoEgamma/EgammaTools/interface/GBRForestTools.h b/RecoEgamma/EgammaTools/interface/GBRForestTools.h new file mode 100644 index 0000000000000..c72812070a2ea --- /dev/null +++ b/RecoEgamma/EgammaTools/interface/GBRForestTools.h @@ -0,0 +1,39 @@ +//-------------------------------------------------------------------------------------------------- +// +// GRBForestTools +// +// Utility to read a TMVA weights file with a BDT into a GRBForest. +// +// Author: Jonas Rembser +//-------------------------------------------------------------------------------------------------- + + +#ifndef RecoEgamma_EgammaTools_GBRForestTools_h +#define RecoEgamma_EgammaTools_GBRForestTools_h + +#include +#include + +#include "CondFormats/EgammaObjects/interface/GBRForest.h" +#include "FWCore/ParameterSet/interface/FileInPath.h" + +#include "TMVA/MethodBDT.h" +#include "TMVA/Reader.h" + +#include "CommonTools/Utils/interface/TMVAZipReader.h" + +class GBRForestTools +{ + public: + GBRForestTools() {} + + static std::unique_ptr createGBRForest(const std::string &weightFile); + static std::unique_ptr createGBRForest(const edm::FileInPath &weightFile); + + // Overloaded versions which are taking string vectors by reference to strore the variable names in + static std::unique_ptr createGBRForest(const std::string &weightFile, std::vector &varNames); + static std::unique_ptr createGBRForest(const edm::FileInPath &weightFile, std::vector &varNames); + +}; + +#endif diff --git a/RecoEgamma/EgammaTools/plugins/EGRegressionModifierV2.cc b/RecoEgamma/EgammaTools/plugins/EGRegressionModifierV2.cc index afa8014e21c50..c0cc834ce04aa 100644 --- a/RecoEgamma/EgammaTools/plugins/EGRegressionModifierV2.cc +++ b/RecoEgamma/EgammaTools/plugins/EGRegressionModifierV2.cc @@ -11,7 +11,6 @@ #include "CondFormats/DataRecord/interface/GBRDWrapperRcd.h" #include "CondFormats/EgammaObjects/interface/GBRForestD.h" #include "RecoEgamma/EgammaTools/interface/EcalClusterLocal.h" -#include "RecoEcal/EgammaCoreTools/interface/EcalTools.h" #include @@ -362,7 +361,7 @@ void EGRegressionModifierV2::modifyObject(reco::GsfElectron& ele) const { const edm::Ptr& theseed = the_sc->seed(); // skip HGCAL for now - if( EcalTools::isHGCalDet(theseed->seed().det()) ) return; + if( theseed->seed().det() == DetId::Forward ) return; const int numberOfClusters = the_sc->clusters().size(); const bool missing_clusters = !the_sc->clusters()[numberOfClusters-1].isAvailable(); @@ -562,7 +561,7 @@ void EGRegressionModifierV2::modifyObject(reco::Photon& pho) const { const edm::Ptr& theseed = the_sc->seed(); // skip HGCAL for now - if( EcalTools::isHGCalDet(theseed->seed().det()) ) return; + if( theseed->seed().det() == DetId::Forward ) return; const int numberOfClusters = the_sc->clusters().size(); const bool missing_clusters = !the_sc->clusters()[numberOfClusters-1].isAvailable(); diff --git a/RecoEgamma/EgammaTools/python/hgcalElectronIDValueMap_cff.py b/RecoEgamma/EgammaTools/python/hgcalElectronIDValueMap_cff.py index bb5db38ad3ceb..dd4da071e2d2f 100644 --- a/RecoEgamma/EgammaTools/python/hgcalElectronIDValueMap_cff.py +++ b/RecoEgamma/EgammaTools/python/hgcalElectronIDValueMap_cff.py @@ -1,6 +1,6 @@ import FWCore.ParameterSet.Config as cms -from RecoLocalCalo.HGCalRecProducers.HGCalRecHit_cfi import dEdX +from RecoLocalCalo.HGCalRecProducers.HGCalRecHit_cfi import dEdX_weights # cfi from HGCalElectronIDValueMapProducer::fillDescriptions() from RecoEgamma.EgammaTools.hgcalElectronIDValueMap_cfi import * -hgcalElectronIDValueMap.dEdXWeights = dEdX.weights +hgcalElectronIDValueMap.dEdXWeights = dEdX_weights diff --git a/RecoEgamma/EgammaTools/python/hgcalPhotonIDValueMap_cff.py b/RecoEgamma/EgammaTools/python/hgcalPhotonIDValueMap_cff.py index f3c18e35f0278..dbefc5da6757d 100644 --- a/RecoEgamma/EgammaTools/python/hgcalPhotonIDValueMap_cff.py +++ b/RecoEgamma/EgammaTools/python/hgcalPhotonIDValueMap_cff.py @@ -1,6 +1,6 @@ import FWCore.ParameterSet.Config as cms -from RecoLocalCalo.HGCalRecProducers.HGCalRecHit_cfi import dEdX +from RecoLocalCalo.HGCalRecProducers.HGCalRecHit_cfi import dEdX_weights # cfi from HGCalPhotonIDValueMapProducer::fillDescriptions() from RecoEgamma.EgammaTools.hgcalPhotonIDValueMap_cfi import * -hgcalPhotonIDValueMap.dEdXWeights = dEdX.weights +hgcalPhotonIDValueMap.dEdXWeights = dEdX_weights diff --git a/RecoEgamma/EgammaTools/src/EcalRegressionData.cc b/RecoEgamma/EgammaTools/src/EcalRegressionData.cc index b4c6dd339e7a0..17f2b69a81cf0 100644 --- a/RecoEgamma/EgammaTools/src/EcalRegressionData.cc +++ b/RecoEgamma/EgammaTools/src/EcalRegressionData.cc @@ -1,6 +1,5 @@ #include "RecoEgamma/EgammaTools/interface/EcalRegressionData.h" -#include "RecoEcal/EgammaCoreTools/interface/EcalTools.h" #include "RecoEcal/EgammaCoreTools/interface/EcalClusterTools.h" #include "DataFormats/Math/interface/deltaR.h" #include "Geometry/CaloTopology/interface/CaloTopology.h" @@ -56,7 +55,7 @@ void EcalRegressionData::fill(const reco::SuperCluster& superClus, isEB_ = ( seedid.subdetId()==EcalBarrel ); // skip HGCal - if( EcalTools::isHGCalDet(seedid.det()) ) return; + if( seedid.det() == DetId::Forward ) return; const EcalRecHitCollection* recHits = isEB_ ? ebRecHits : eeRecHits; diff --git a/RecoEgamma/EgammaTools/src/GBRForestTools.cc b/RecoEgamma/EgammaTools/src/GBRForestTools.cc new file mode 100644 index 0000000000000..cdcd42646924a --- /dev/null +++ b/RecoEgamma/EgammaTools/src/GBRForestTools.cc @@ -0,0 +1,151 @@ +#include "RecoEgamma/EgammaTools/interface/GBRForestTools.h" + +#include +#include + +namespace { + + // Will return position of n-th occurence of a char in a string. + int strpos(const std::string &haystack, char needle, unsigned int nth) + { + int found = 0; + for (unsigned int i=0 ; i GBRForestTools::createGBRForest(const std::string &weightFile, + std::vector &varNames){ + edm::FileInPath weightFileEdm(weightFile); + return GBRForestTools::createGBRForest(weightFileEdm, varNames); +} + +// Creates a pointer to new GBRForest corresponding to a TMVA weights file +std::unique_ptr GBRForestTools::createGBRForest(const edm::FileInPath &weightFile, + std::vector &varNames){ + + std::string method; + + unsigned int NVar = 0; + unsigned int NSpec = 0; + + std::vector dumbVars; + std::vector dumbSpecs; + + varNames.clear(); + std::vector specNames; + + std::string line; + std::ifstream f; + std::string tmpstr; + + bool gzipped = false; + + // + // Set up the input buffers, for gzipped or raw xml file + // + if (reco::details::hasEnding(weightFile.fullPath(), ".xml")) { + f.open(weightFile.fullPath()); + tmpstr = ""; + } else if (reco::details::hasEnding(weightFile.fullPath(), ".gz") || reco::details::hasEnding(weightFile.fullPath(), ".gzip")) { + gzipped = true; + char *buffer = reco::details::readGzipFile(weightFile.fullPath()); + tmpstr = std::string(buffer); + free(buffer); + } + std::stringstream is(tmpstr); + + bool isend; + + while(true) { + + if (gzipped) isend = !std::getline(is, line); + else isend = !std::getline(f, line); + + if (isend) break; + + // Terminate reading of weights file + if (line.find("AddVariable(varNames[i], &dumbVars[i]); + } + + for(size_t i = 0; i < NSpec; ++i){ + mvaReader->AddSpectator(specNames[i], &dumbSpecs[i]); + } + + // + // Book the method and set up the weights file + // + + reco::details::loadTMVAWeights(mvaReader, method, weightFile.fullPath()); + + TMVA::MethodBDT* bdt = dynamic_cast( mvaReader->FindMVA(method) ); + std::unique_ptr gbrForest = std::make_unique(GBRForest(bdt)); + delete mvaReader; + + return gbrForest; +} + +std::unique_ptr GBRForestTools::createGBRForest(const std::string &weightFile){ + std::vector varNames; + return GBRForestTools::createGBRForest(weightFile, varNames); +} + +std::unique_ptr GBRForestTools::createGBRForest(const edm::FileInPath &weightFile){ + std::vector varNames; + return GBRForestTools::createGBRForest(weightFile, varNames); +} diff --git a/RecoEgamma/EgammaTools/src/SCEnergyCorrectorSemiParm.cc b/RecoEgamma/EgammaTools/src/SCEnergyCorrectorSemiParm.cc index ecec2d51f3df9..aaf24dcb139ce 100644 --- a/RecoEgamma/EgammaTools/src/SCEnergyCorrectorSemiParm.cc +++ b/RecoEgamma/EgammaTools/src/SCEnergyCorrectorSemiParm.cc @@ -4,7 +4,6 @@ #include "RecoEcal/EgammaCoreTools/interface/EcalClusterTools.h" #include "DataFormats/EcalDetId/interface/EcalSubdetector.h" #include "DataFormats/VertexReco/interface/Vertex.h" -#include "RecoEcal/EgammaCoreTools/interface/EcalTools.h" #include "FWCore/Utilities/interface/isFinite.h" #include "DataFormats/Math/interface/deltaPhi.h" @@ -104,7 +103,7 @@ std::pair SCEnergyCorrectorSemiParm::getCorrections(const reco:: p.second=-1; // protect against HGCal, don't mod the object - if( EcalTools::isHGCalDet(sc.seed()->seed().det()) ) return p; + if( sc.seed()->seed().det() == DetId::Forward ) return p; const reco::CaloCluster &seedCluster = *(sc.seed()); const bool iseb = seedCluster.hitsAndFractions()[0].first.subdetId() == EcalBarrel; diff --git a/RecoEgamma/ElectronIdentification/BuildFile.xml b/RecoEgamma/ElectronIdentification/BuildFile.xml index 289dfc278b9cb..f7589cf2db2d7 100644 --- a/RecoEgamma/ElectronIdentification/BuildFile.xml +++ b/RecoEgamma/ElectronIdentification/BuildFile.xml @@ -4,7 +4,6 @@ - diff --git a/RecoEgamma/ElectronIdentification/interface/ElectronMVAEstimatorRun2.h b/RecoEgamma/ElectronIdentification/interface/ElectronMVAEstimatorRun2.h index 43a845a3ee0b1..bd0c598328cae 100644 --- a/RecoEgamma/ElectronIdentification/interface/ElectronMVAEstimatorRun2.h +++ b/RecoEgamma/ElectronIdentification/interface/ElectronMVAEstimatorRun2.h @@ -3,7 +3,7 @@ #include "DataFormats/PatCandidates/interface/Electron.h" #include "RecoEgamma/EgammaTools/interface/AnyMVAEstimatorRun2Base.h" -#include "CommonTools/MVAUtils/interface/GBRForestTools.h" +#include "RecoEgamma/EgammaTools/interface/GBRForestTools.h" #include "RecoEgamma/EgammaTools/interface/MVAVariableManager.h" #include "RecoEgamma/EgammaTools/interface/ThreadSafeStringCut.h" diff --git a/RecoEgamma/ElectronIdentification/src/ElectronMVAEstimator.cc b/RecoEgamma/ElectronIdentification/src/ElectronMVAEstimator.cc index 2cfaafb7ef422..2c5fbef1ca6e1 100644 --- a/RecoEgamma/ElectronIdentification/src/ElectronMVAEstimator.cc +++ b/RecoEgamma/ElectronIdentification/src/ElectronMVAEstimator.cc @@ -3,7 +3,7 @@ #include "DataFormats/TrackReco/interface/TrackFwd.h" #include "DataFormats/GsfTrackReco/interface/GsfTrack.h" #include "DataFormats/GsfTrackReco/interface/GsfTrackFwd.h" -#include "CommonTools/MVAUtils/interface/GBRForestTools.h" +#include "RecoEgamma/EgammaTools/interface/GBRForestTools.h" ElectronMVAEstimator::ElectronMVAEstimator(): cfg_{} @@ -15,13 +15,13 @@ ElectronMVAEstimator::ElectronMVAEstimator(const std::string& fileName): // Taken from Daniele (his mail from the 30/11) // tmvaReader.BookMVA("BDTSimpleCat","../Training/weights_Root527b_3Depth_DanVarConvRej_2PtBins_10Pt_800TPrune5_Min100Events_NoBjets_half/TMVA_BDTSimpleCat.weights.xm"); // training of the 7/12 with Nvtx added - gbr_.push_back( createGBRForest(fileName) ); + gbr_.push_back( GBRForestTools::createGBRForest(fileName) ); } ElectronMVAEstimator::ElectronMVAEstimator(const Configuration & cfg):cfg_(cfg) { for(const auto& weightsfile : cfg_.vweightsfiles) { - gbr_.push_back( createGBRForest(weightsfile) ); + gbr_.push_back( GBRForestTools::createGBRForest(weightsfile) ); } } diff --git a/RecoEgamma/ElectronIdentification/src/ElectronMVAEstimatorRun2.cc b/RecoEgamma/ElectronIdentification/src/ElectronMVAEstimatorRun2.cc index 82a357d7254ff..2acd48b8efe22 100644 --- a/RecoEgamma/ElectronIdentification/src/ElectronMVAEstimatorRun2.cc +++ b/RecoEgamma/ElectronIdentification/src/ElectronMVAEstimatorRun2.cc @@ -60,7 +60,7 @@ void ElectronMVAEstimatorRun2::init(const std::vector &weightFileNa // when the vector clear() is called in the destructor std::vector variableNamesInCategory; - gbrForests_.push_back(createGBRForest(weightFileNames[i], variableNamesInCategory)); + gbrForests_.push_back(GBRForestTools::createGBRForest(weightFileNames[i], variableNamesInCategory)); nVariables_.push_back(variableNamesInCategory.size()); diff --git a/RecoEgamma/ElectronIdentification/src/SoftElectronMVAEstimator.cc b/RecoEgamma/ElectronIdentification/src/SoftElectronMVAEstimator.cc index 935b4b0048392..144f15dcd07b0 100644 --- a/RecoEgamma/ElectronIdentification/src/SoftElectronMVAEstimator.cc +++ b/RecoEgamma/ElectronIdentification/src/SoftElectronMVAEstimator.cc @@ -3,7 +3,7 @@ #include "DataFormats/TrackReco/interface/TrackFwd.h" #include "DataFormats/GsfTrackReco/interface/GsfTrack.h" #include "DataFormats/GsfTrackReco/interface/GsfTrackFwd.h" -#include "CommonTools/MVAUtils/interface/GBRForestTools.h" +#include "RecoEgamma/EgammaTools/interface/GBRForestTools.h" SoftElectronMVAEstimator::SoftElectronMVAEstimator(const Configuration & cfg):cfg_(cfg) { @@ -18,7 +18,7 @@ SoftElectronMVAEstimator::SoftElectronMVAEstimator(const Configuration & cfg):cf for(auto& weightsfile : cfg_.vweightsfiles) { // Taken from Daniele (his mail from the 30/11) // training of the 7/12 with Nvtx added - gbr_.push_back(createGBRForest( weightsfile )); + gbr_.push_back(GBRForestTools::createGBRForest( weightsfile )); } } diff --git a/RecoEgamma/PhotonIdentification/plugins/BuildFile.xml b/RecoEgamma/PhotonIdentification/plugins/BuildFile.xml index 5044557c4eb77..dc26d96bbc9f7 100644 --- a/RecoEgamma/PhotonIdentification/plugins/BuildFile.xml +++ b/RecoEgamma/PhotonIdentification/plugins/BuildFile.xml @@ -7,7 +7,6 @@ - diff --git a/RecoEgamma/PhotonIdentification/plugins/PhotonMVAEstimator.cc b/RecoEgamma/PhotonIdentification/plugins/PhotonMVAEstimator.cc index 47e24a865c756..27b94cbd59275 100644 --- a/RecoEgamma/PhotonIdentification/plugins/PhotonMVAEstimator.cc +++ b/RecoEgamma/PhotonIdentification/plugins/PhotonMVAEstimator.cc @@ -1,6 +1,6 @@ #include "RecoEgamma/PhotonIdentification/interface/PhotonMVAEstimator.h" #include "FWCore/MessageLogger/interface/MessageLogger.h" -#include "CommonTools/MVAUtils/interface/GBRForestTools.h" +#include "RecoEgamma/EgammaTools/interface/GBRForestTools.h" PhotonMVAEstimator::PhotonMVAEstimator(const edm::ParameterSet& conf) : AnyMVAEstimatorRun2Base(conf)