Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Undid some part of the sync with 10_4_X
- Loading branch information
1 parent
89fe032
commit 38f3ee4
Showing
14 changed files
with
206 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters