Skip to content

Commit

Permalink
Undid some part of the sync with 10_4_X
Browse files Browse the repository at this point in the history
  • Loading branch information
guitargeek committed Dec 5, 2018
1 parent 89fe032 commit 38f3ee4
Show file tree
Hide file tree
Showing 14 changed files with 206 additions and 21 deletions.
39 changes: 39 additions & 0 deletions 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 <vector>
#include <string>

#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<const GBRForest> createGBRForest(const std::string &weightFile);
static std::unique_ptr<const GBRForest> createGBRForest(const edm::FileInPath &weightFile);

// Overloaded versions which are taking string vectors by reference to strore the variable names in
static std::unique_ptr<const GBRForest> createGBRForest(const std::string &weightFile, std::vector<std::string> &varNames);
static std::unique_ptr<const GBRForest> createGBRForest(const edm::FileInPath &weightFile, std::vector<std::string> &varNames);

};

#endif
5 changes: 2 additions & 3 deletions RecoEgamma/EgammaTools/plugins/EGRegressionModifierV2.cc
Expand Up @@ -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 <vdt/vdtMath.h>

Expand Down Expand Up @@ -362,7 +361,7 @@ void EGRegressionModifierV2::modifyObject(reco::GsfElectron& ele) const {
const edm::Ptr<reco::CaloCluster>& 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();
Expand Down Expand Up @@ -562,7 +561,7 @@ void EGRegressionModifierV2::modifyObject(reco::Photon& pho) const {
const edm::Ptr<reco::CaloCluster>& 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();
Expand Down
4 changes: 2 additions & 2 deletions 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
4 changes: 2 additions & 2 deletions 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
3 changes: 1 addition & 2 deletions 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"
Expand Down Expand Up @@ -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;

Expand Down
151 changes: 151 additions & 0 deletions RecoEgamma/EgammaTools/src/GBRForestTools.cc
@@ -0,0 +1,151 @@
#include "RecoEgamma/EgammaTools/interface/GBRForestTools.h"

#include <iostream>
#include <fstream>

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<nth ; ++i) {
std::size_t pos = haystack.find(needle, found);
if (pos == std::string::npos) return -1;
else found = pos+1;
}
return found;
}

// To get the substring between the n1th and n2th quotation mark in a string
std::string get_quoted_substring(const std::string &str, int n1, int n2)
{
int pos = strpos(str, '"', n1);
int count = strpos(str, '"', n2) - pos;
return str.substr(pos, count - 1);
}

};

std::unique_ptr<const GBRForest> GBRForestTools::createGBRForest(const std::string &weightFile,
std::vector<std::string> &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<const GBRForest> GBRForestTools::createGBRForest(const edm::FileInPath &weightFile,
std::vector<std::string> &varNames){

std::string method;

unsigned int NVar = 0;
unsigned int NSpec = 0;

std::vector<float> dumbVars;
std::vector<float> dumbSpecs;

varNames.clear();
std::vector<std::string> 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("<Weights ") != std::string::npos) break;

// Method name
else if (line.find("<MethodSetup Method=") != std::string::npos) {
method = get_quoted_substring(line, 1, 2);
}

// Number of variables
else if (line.find("<Variables NVar=") != std::string::npos) {
NVar = std::atoi(get_quoted_substring(line, 1, 2).c_str());
}

// Number of spectators
else if (line.find("<Spectators NSpec=") != std::string::npos) {
NSpec = std::atoi(get_quoted_substring(line, 1, 2).c_str());
}

// If variable
else if (line.find("<Variable ") != std::string::npos) {
unsigned int pos = line.find("Expression=");
varNames.push_back(get_quoted_substring(line.substr(pos, line.length() - pos), 1, 2));
dumbVars.push_back(0);
}

// If spectator
else if (line.find("Spectator ") != std::string::npos) {
unsigned int pos = line.find("Expression=");
specNames.push_back(get_quoted_substring(line.substr(pos, line.length() - pos), 1, 2));
dumbSpecs.push_back(0);
}
}

//
// Create the reader
//
TMVA::Reader* mvaReader = new TMVA::Reader("!Color:Silent:!Error");

//
// Configure all variables and spectators. Note: the order and names
// must match what is found in the xml weights file!
//
for(size_t i = 0; i < NVar; ++i){
mvaReader->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<TMVA::MethodBDT*>( mvaReader->FindMVA(method) );
std::unique_ptr<const GBRForest> gbrForest = std::make_unique<const GBRForest>(GBRForest(bdt));
delete mvaReader;

return gbrForest;
}

std::unique_ptr<const GBRForest> GBRForestTools::createGBRForest(const std::string &weightFile){
std::vector<std::string> varNames;
return GBRForestTools::createGBRForest(weightFile, varNames);
}

std::unique_ptr<const GBRForest> GBRForestTools::createGBRForest(const edm::FileInPath &weightFile){
std::vector<std::string> varNames;
return GBRForestTools::createGBRForest(weightFile, varNames);
}
3 changes: 1 addition & 2 deletions RecoEgamma/EgammaTools/src/SCEnergyCorrectorSemiParm.cc
Expand Up @@ -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"
Expand Down Expand Up @@ -104,7 +103,7 @@ std::pair<double, double> 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;
Expand Down
1 change: 0 additions & 1 deletion RecoEgamma/ElectronIdentification/BuildFile.xml
Expand Up @@ -4,7 +4,6 @@
<use name="CondFormats/EgammaObjects"/>
<use name="RecoEcal/EgammaCoreTools"/>
<use name="DataFormats/TrackReco"/>
<use name="CommonTools/MVAUtils"/>
<use name="MagneticField/Records"/>
<use name="MagneticField/Engine"/>
<use name="RecoEgamma/EgammaTools"/>
Expand Down
Expand Up @@ -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"

Expand Down
6 changes: 3 additions & 3 deletions RecoEgamma/ElectronIdentification/src/ElectronMVAEstimator.cc
Expand Up @@ -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_{}
Expand All @@ -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) );
}
}

Expand Down
Expand Up @@ -60,7 +60,7 @@ void ElectronMVAEstimatorRun2::init(const std::vector<std::string> &weightFileNa
// when the vector clear() is called in the destructor

std::vector<std::string> variableNamesInCategory;
gbrForests_.push_back(createGBRForest(weightFileNames[i], variableNamesInCategory));
gbrForests_.push_back(GBRForestTools::createGBRForest(weightFileNames[i], variableNamesInCategory));

nVariables_.push_back(variableNamesInCategory.size());

Expand Down
Expand Up @@ -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)
{
Expand All @@ -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 ));
}
}

Expand Down
1 change: 0 additions & 1 deletion RecoEgamma/PhotonIdentification/plugins/BuildFile.xml
Expand Up @@ -7,7 +7,6 @@
<use name="Geometry/CaloGeometry"/>
<use name="PhysicsTools/SelectorUtils"/>
<use name="CommonTools/UtilAlgos"/>
<use name="CommonTools/MVAUtils"/>
<use name="CondFormats/DataRecord"/>
<library name="RecoEgammaPhotonIdentificationPlugins" file="*.cc">
<use name="RecoEgamma/PhotonIdentification"/>
Expand Down
@@ -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)
Expand Down

0 comments on commit 38f3ee4

Please sign in to comment.