From 9cf380390b768593252c7f2dc158340a67357756 Mon Sep 17 00:00:00 2001 From: Kevin Date: Tue, 13 Jan 2015 15:20:01 -0500 Subject: [PATCH] simplify corrections to use ROOT objects - remove old database code from MM --- Plotting/KCode/KBuilder.h | 36 ++- Plotting/KCode/KParser.h | 12 + Plotting/database/DataBaseManager.cc | 360 --------------------------- Plotting/database/DataBaseManager.hh | 87 ------- Plotting/database/elIDMedium.db | 42 ---- Plotting/database/example.C | 6 - Plotting/database/muIDTight.db | 51 ---- 7 files changed, 27 insertions(+), 567 deletions(-) delete mode 100644 Plotting/database/DataBaseManager.cc delete mode 100644 Plotting/database/DataBaseManager.hh delete mode 100644 Plotting/database/elIDMedium.db delete mode 100644 Plotting/database/example.C delete mode 100644 Plotting/database/muIDTight.db diff --git a/Plotting/KCode/KBuilder.h b/Plotting/KCode/KBuilder.h index 1ade6c53..f4a66c63 100644 --- a/Plotting/KCode/KBuilder.h +++ b/Plotting/KCode/KBuilder.h @@ -8,14 +8,13 @@ #include "TreeClass.h" //custom headers for weighting -#include "../database/DataBaseManager.cc" #include "../btag/GetBtagScale.C" //ROOT headers #include #include #include -#include +#include #include #include @@ -983,20 +982,11 @@ void KBaseData::Build(){ class KBuilderMC : public KBuilder { public: //constructors - KBuilderMC() : KBuilder() { InitWeight(); } - KBuilderMC(KBase* MyBase_, TTree* tree_) : KBuilder(MyBase_,tree_) { InitWeight(); } + KBuilderMC() : KBuilder() { } + KBuilderMC(KBase* MyBase_, TTree* tree_) : KBuilder(MyBase_,tree_) { } //destructor virtual ~KBuilderMC() {} - //helper function - void InitWeight(){ - TFile* pufile = new TFile("/data/users/pedrok/LQ2012/puWeightsLQ.root","READ"); //puWeights - puWeights = (TH1F*)pufile->Get("pileup"); - - dbm = new DataBaseManager(); //for muon efficiency corrections - dbm->loadDb("muID","muIDTight.db"); - } - //functions for histo creation bool Cut(){ bool goodEvent = KBuilder::Cut(); @@ -1018,9 +1008,17 @@ class KBuilderMC : public KBuilder { //check option in case correction types are disabled globally //(enabled by default) - if(option->Get("pucorr",true)) w *= puWeights->GetBinContent(puWeights->GetXaxis()->FindBin(trueNInteraction)); - - if(option->Get("mucorr",true)) w *= dbm->getDBValue("muID",fabs(MuonEta),MuonPt); + if(option->Get("pucorr",true)) { + TH1F* puWeights; + option->Get("puWeights",puWeights); + w *= puWeights->GetBinContent(puWeights->GetXaxis()->FindBin(trueNInteraction)); + } + + if(option->Get("mucorr",true)) { + TH2F* muIDTight; + option->Get("muIDTight",muIDTight); + w *= muIDTight->GetBinContent(muIDTight->FindBin(fabs(MuonEta),MuonPt)); + } if(option->Get("btagcorr",true)) { int bSF = 0; @@ -1044,11 +1042,7 @@ class KBuilderMC : public KBuilder { return w; } - - private: - //member variables - TH1F* puWeights; - DataBaseManager* dbm; + }; void KBaseMC::Build(){ diff --git a/Plotting/KCode/KParser.h b/Plotting/KCode/KParser.h index a93ad7b8..75d8fa9c 100644 --- a/Plotting/KCode/KParser.h +++ b/Plotting/KCode/KParser.h @@ -337,6 +337,18 @@ KManager::KManager(string in, string dir) : input(in),treedir(dir),option(0),num //final checks and initializations MyRatio = new KSetRatio(option); if(option->Get("calcfaketau",false)) FakeTauEstimationInit(); + //store correction root files centrally + //todo: make file location and histo name configurable + if(option->Get("pucorr",true)) { + TFile* pufile = new TFile("corrections/puWeightsLQ.root","READ"); //puWeights + TH1F* puWeights = (TH1F*)pufile->Get("pileup"); + option->Set("puWeights",puWeights); + } + if(option->Get("mucorr",true)) { + TFile* mufile = new TFile("corrections/muIDTight.root","READ"); //puWeights + TH1F* muIDTight = (TH1F*)mufile->Get("muIDTight"); + option->Set("muIDTight",muIDTight); + } } #endif \ No newline at end of file diff --git a/Plotting/database/DataBaseManager.cc b/Plotting/database/DataBaseManager.cc deleted file mode 100644 index c7bee11f..00000000 --- a/Plotting/database/DataBaseManager.cc +++ /dev/null @@ -1,360 +0,0 @@ -#include "DataBaseManager.hh" - - - -using namespace std; - -DataBaseManager::DataBaseManager() { -} - -DataBaseManager::~DataBaseManager() { -} - -void -DataBaseManager::loadDb(string key,string dbName) { - readDb(key,dbName); -} - -void -DataBaseManager::loadDb(string key,string dbName, string hname) { - readDbHisto(key,dbName,hname); -} - - -void -DataBaseManager::readDb(string key, string dbName) { - - bool header=true; - int nV=0; - int nE=0; - int nN=0; - - string ndb= "/data/users/pedrok/LQ2012/"+dbName; - ifstream fDb( ndb.c_str(), ios::in ); - - if(fDb) - { - string line; - while(getline(fDb, line)) - { - //cout << line << endl; - - istringstream iss(line); - vector tks; - copy(istream_iterator(iss), - istream_iterator(), - back_inserter >(tks)); - - if(line.size()<2 || line.substr(0,2)=="//") { - - if(header) { - - nN=tks.size(); - for(int is=0;is > tmp(nV,vector(0,0)); - _cDbLim[key] = tmp; - - header=false; - } - - continue; - } - else if(header) { - cout<<" Error, no header in the database file : "< tks; - copy(istream_iterator(iss), - istream_iterator(), - back_inserter >(tks)); - - if(line.size()<2 || line.substr(0,2)=="//") { - continue; - } - - int* vbin=new int[nV]; - for(int i=0;i( atof(tks[i].c_str() ), _cDbLim[key][i]); - } - - - _mDBs[ key ]->SetBinContent(vbin, atof(tks[nV].c_str()) ); //value - - switch(nE) { - - case 1: - _mDBEHs[ key ]->SetBinContent(vbin, atof(tks[nV+1].c_str()) ); - _mDBELs[ key ]->SetBinContent(vbin, atof(tks[nV+1].c_str()) ); - break; - - case 2: - _mDBEHs[ key ]->SetBinContent(vbin, atof(tks[nV+1].c_str()) ); - _mDBELs[ key ]->SetBinContent(vbin, atof(tks[nV+2].c_str()) ); - break; - - case 3: - { - float em = sqrt( pow( atof(tks[nV+1].c_str()), 2) + pow( atof(tks[nV+3].c_str()), 2) ); - float eM = sqrt( pow( atof(tks[nV+2].c_str()), 2) + pow( atof(tks[nV+3].c_str()), 2) ); - _mDBEHs[ key ]->SetBinContent(vbin, em ); - _mDBELs[ key ]->SetBinContent(vbin, eM ); - break; - } - - default: - cout<<"Error, database format unrecognized ( "<Get(hname.c_str()); - - TString cName = obj->ClassName(); - - int nV=0; - bool graph=false; - - if( ((string)cName).find("2")!=(size_t)-1) { //2D histo - nV=2; - } - if( ((string)cName).find("3")!=(size_t)-1) { //3D histo - nV=3; - } - if( ((string)cName).find("Graph")!=(size_t)-1) {//graph... - nV=1; graph=true; - } - else{ //1D histo, or profile, or something strange that noone does but the W guys - nV=1; - } - - vector > tmp(nV,vector(0,0)); - _cDbLim[key] = tmp; - - if(!graph) { - for(int i=1;i<((TH1*)obj)->GetNbinsX()+2;i++) { - _cDbLim[key][0].push_back( ((TH1*)obj)->GetXaxis()->GetBinLowEdge(i) ); - } - } - else { - double x,y,ex; - x = y = ex = 0; - for(int i=0;i<((TGraph*)obj)->GetN();i++) { - ((TGraph*)obj)->GetPoint(i,x,y); - ex = ((TGraph*)obj)->GetErrorXlow(i); - _cDbLim[key][0].push_back( x-ex ); - } - _cDbLim[key][0].push_back( x+ex ); - } - - if(nV>=2) - for(int i=1;i<((TH2*)obj)->GetNbinsY()+2;i++) { - _cDbLim[key][1].push_back( ((TH2*)obj)->GetYaxis()->GetBinLowEdge(i) ); - } - if(nV>=3) - for(int i=1;i<((TH3*)obj)->GetNbinsZ()+2;i++) { - _cDbLim[key][2].push_back( ((TH3*)obj)->GetZaxis()->GetBinLowEdge(i) ); - } - - //ok, now create and fill the DB - - //ugly :( why ROOT does not support vectors... - int* nBins=new int(nV); - double* min=new double(nV); - double* max=new double(nV); - - for(int i=0;iSetBinContent( vbin, (iGetBinContent(vbin[0]+1 ):1. ); - _mDBEHs[ key ]->SetBinContent(vbin, (iGetBinError( vbin[0]+1 ):1. ); - _mDBELs[ key ]->SetBinContent(vbin, (iGetBinError( vbin[0]+1 ):1. ); - } - } - else { - int nB= _cDbLim[key][0].size(); - double x,y,ey; - for(int i=0;iGetPoint(i,x,y); - ey = ((TGraph*)obj)->GetErrorY(i); - _mDBs[ key ]->SetBinContent( vbin, (iSetBinContent(vbin, (iSetBinContent(vbin, (iSetBinContent( vbin, (iGetBinContent( vbin[0]+1, vbin[1]+1 ):1. ); - _mDBEHs[ key ]->SetBinContent( vbin, (iGetBinError( vbin[0]+1, vbin[1]+1 ):1. ); - _mDBELs[ key ]->SetBinContent( vbin, (iGetBinError( vbin[0]+1, vbin[1]+1 ):1. ); - }//i - }//j - break; - } - case 3: - { - int nBx= _cDbLim[key][0].size(); - int nBy= _cDbLim[key][1].size(); - int nBz= _cDbLim[key][2].size(); - for(int i=0;iSetBinContent( vbin, (iGetBinContent( vbin[0]+1, vbin[1]+1, vbin[2]+1 ):1. ); - _mDBEHs[ key ]->SetBinContent( vbin, (iGetBinError( vbin[0]+1, vbin[1]+1, vbin[2]+1 ):1. ); - _mDBELs[ key ]->SetBinContent( vbin, (iGetBinError( vbin[0]+1, vbin[1]+1, vbin[2]+1 ):1. ); - }//k - }//j - }//i - break; - } - default: - cout<<" error in root db treatment... "<Close(); - -} - -float -DataBaseManager::getDBValue(string key, float v1, float v2, float v3, float v4, - float v5, float v6,float v7, float v8, float v9, float v10) { - - float vals[10]={v1,v2,v3,v4,v5,v6,v7,v8,v9,v10}; - int vbin[10]={0,0,0,0,0,0,0,0,0,0}; - - for(size_t i=0;i<_cDbLim[key].size();i++) { - if(vals[i]!=-1000000) { - vbin[i] = findBin( vals[i], _cDbLim[key][i] ); - if(vbin[i]==-1) return 1; - } - } - - return _mDBs[ key ]->GetBinContent( vbin ); -} - - -float -DataBaseManager::getDBErrH(string key, float v1, float v2, float v3, float v4, - float v5, float v6,float v7, float v8, float v9, float v10) { - - float vals[10]={v1,v2,v3,v4,v5,v6,v7,v8,v9,v10}; - int vbin[10]={0,0,0,0,0,0,0,0,0,0}; - - for(size_t i=0;i<_cDbLim[key].size();i++) { - if(vals[i]!=-1000000) { - vbin[i] = findBin( vals[i], _cDbLim[key][i] ); - if(vbin[i]==-1) return 1; - } - - } - - return _mDBEHs[ key ]->GetBinContent( vbin ); -} - -float -DataBaseManager::getDBErrL(string key, float v1, float v2, float v3, float v4, - float v5, float v6,float v7, float v8, float v9, float v10) { - - float vals[10]={v1,v2,v3,v4,v5,v6,v7,v8,v9,v10}; - int vbin[10]={0,0,0,0,0,0,0,0,0,0}; - - for(size_t i=0;i<_cDbLim[key].size();i++) { - if(vals[i]!=-1000000) { - vbin[i] = findBin( vals[i], _cDbLim[key][i] ); - if(vbin[i]==-1) return 1; - } - - } - - return _mDBELs[ key ]->GetBinContent( vbin ); -} diff --git a/Plotting/database/DataBaseManager.hh b/Plotting/database/DataBaseManager.hh deleted file mode 100644 index a558db38..00000000 --- a/Plotting/database/DataBaseManager.hh +++ /dev/null @@ -1,87 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "THnSparse.h" -#include "TFile.h" -#include "TObject.h" -#include "TH1.h" -#include "TH2.h" -#include "TH3.h" -#include "TGraph.h" - -//#include "utils/StatUtils.hh" - - -template < typename T > inline -unsigned int findBin(T v, vector vals) { - bool find=false; - - size_t bm=0; - size_t bM=vals.size()-1; - if(v< vals[ bm ]) return -1; - if(v>= vals[ bM ]) return -1; - size_t n=0; - while(!find) { - if(v < vals[ bm+(bM-bm)/2 ] ) { - bM = bm+(bM-bm)/2; - } - else { - bm = bm+(bM-bm)/2; - } - if( fabs((int)(bM-bm))==1 ) { - return bm; - } - - if(n>(size_t)vals.size()) return -1; - n++; - } - return -1; -}; - - - - -class DataBaseManager { - -public: - - DataBaseManager(); - ~DataBaseManager(); - - void loadDb(std::string key, std::string dbName); - void loadDb(std::string key, std::string dbName, std::string hname); - float getDBValue(std::string key, float v1=-1000000, float v2=-1000000, - float v3=-1000000, float v4=-1000000, float v5=-1000000, - float v6=-1000000,float v7=-1000000, float v8=-1000000, - float v9=-1000000, float v10=-1000000); - - float getDBErrL(std::string key, float v1=-1000000, float v2=-1000000, - float v3=-1000000, float v4=-1000000, float v5=-1000000, - float v6=-1000000,float v7=-1000000, float v8=-1000000, - float v9=-1000000, float v10=-1000000); - - float getDBErrH(std::string key, float v1=-1000000, float v2=-1000000, - float v3=-1000000, float v4=-1000000, float v5=-1000000, - float v6=-1000000,float v7=-1000000, float v8=-1000000, - float v9=-1000000, float v10=-1000000); - - -private: - - void readDb(std::string key, std::string dbName); - void readDbHisto(std::string key, std::string dbName, std::string hname); - - std::map > > _cDbLim; - std::map _mDBs; - std::map _mDBEHs; - std::map _mDBELs; - -}; diff --git a/Plotting/database/elIDMedium.db b/Plotting/database/elIDMedium.db deleted file mode 100644 index c56d3fed..00000000 --- a/Plotting/database/elIDMedium.db +++ /dev/null @@ -1,42 +0,0 @@ -//v v c e e e -0.0 10 0.859 0.023 0.023 0.040 -0.0 15 0.962 0.010 0.010 0.032 -0.0 20 1.004 0.003 0.003 0.003 -0.0 30 1.003 0.001 0.001 0.002 -0.0 40 1.007 0.001 0.001 0.001 -0.0 50 1.007 0.001 0.001 0.002 -0.0 200 1. 0. 0. 0. -0.8 10 0.942 0.025 0.025 0.071 -0.8 15 0.951 0.011 0.011 0.030 -0.8 20 0.975 0.011 0.013 0.006 -0.8 30 0.984 0.001 0.001 0.001 -0.8 40 0.992 0.001 0.001 0.001 -0.8 50 0.995 0.002 0.001 0.001 -0.8 200 1. 0. 0. 0. -1.442 10 1.099 0.125 0.112 0.160 -1.442 15 0.975 0.050 0.048 0.043 -1.442 20 1.034 0.015 0.014 0.003 -1.442 30 1.006 0.007 0.007 0.002 -1.442 40 0.991 0.003 0.003 0.004 -1.442 50 0.993 0.005 0.005 0.002 -1.442 200 1. 0. 0. 0. -1.556 10 0.908 0.046 0.045 0.041 -1.556 15 0.972 0.020 0.020 0.006 -1.556 20 0.983 0.006 0.006 0.009 -1.556 30 0.990 0.003 0.003 0.001 -1.556 40 1.006 0.002 0.001 0.002 -1.556 50 1.007 0.003 0.003 0.000 -1.556 200 1. 0. 0. 0. -2.0 10 1.050 0.022 0.022 0.014 -2.0 15 0.963 0.019 0.019 0.017 -2.0 20 1.025 0.006 0.006 0.005 -2.0 30 1.022 0.003 0.003 0.002 -2.0 40 1.013 0.001 0.001 0.003 -2.0 50 1.009 0.002 0.002 0.001 -2.0 200 1. 0. 0. 0. -2.5 10 1. 0. 0. 0. -2.5 20 1. 0. 0. 0. -2.5 30 1. 0. 0. 0. -2.5 40 1. 0. 0. 0. -2.5 50 1. 0. 0. 0. -2.5 200 1. 0. 0. 0. \ No newline at end of file diff --git a/Plotting/database/example.C b/Plotting/database/example.C deleted file mode 100644 index 94d22afa..00000000 --- a/Plotting/database/example.C +++ /dev/null @@ -1,6 +0,0 @@ -DataBaseManager* dbm; -dbm = new DataBaseManager(); //for muon efficiency corrections -dbm->loadDb("muID","muIDTight.db"); - -//for each event -if(dbm) w *= dbm->getDBValue("muID",fabs(MuonEta),MuonPt); \ No newline at end of file diff --git a/Plotting/database/muIDTight.db b/Plotting/database/muIDTight.db deleted file mode 100644 index 9befd76f..00000000 --- a/Plotting/database/muIDTight.db +++ /dev/null @@ -1,51 +0,0 @@ -//v v c e -0 10 0.94705 0.0027568 -0 20 0.974978 0.00153776 -0 25 0.997129 0.000938598 -0 30 0.993863 0.000621591 -0 35 0.993442 0.000432531 -0 40 0.994101 0.000222255 -0 50 0.995544 0.000406804 -0 60 0.999036 0.000470748 -0 90 1.00104 0.000906931 -0 140 1.0003 0.00182609 -0 300 1.01977 0.0122983 -0 500 1. 0. -0.9 10 0.951836 0.00317019 -0.9 20 0.988368 0.00232118 -0.9 25 1.00083 0.00164994 -0.9 30 0.998546 0.00116786 -0.9 35 0.99914 0.000748493 -0.9 40 0.998176 0.000352468 -0.9 50 0.998696 0.000655149 -0.9 60 0.999132 0.000786614 -0.9 90 0.999559 0.00147137 -0.9 140 0.996767 0.00323217 -0.9 300 1.00784 0.0260855 -0.9 500 1. 0. -1.2 10 0.980045 0.00162376 -1.2 20 0.997342 0.00120934 -1.2 25 1.00784 0.000880102 -1.2 30 1.00685 0.000673421 -1.2 35 1.0037 0.000466233 -1.2 40 1.00209 0.000207917 -1.2 50 1.00125 0.000385633 -1.2 60 1.00065 0.000477601 -1.2 90 0.999878 0.000975745 -1.2 140 0.99989 0.00238302 -1.2 300 1.01392 0.0181738 -1.2 500 1. 0. -2.1 500 1. 0. -2.1 10 1. 0. -2.1 20 1. 0. -2.1 25 1. 0. -2.1 30 1. 0. -2.1 35 1. 0. -2.1 40 1. 0. -2.1 50 1. 0. -2.1 60 1. 0. -2.1 90 1. 0. -2.1 140 1. 0. -2.1 300 1. 0. -2.1 500 1. 0. -2.1 500 1. 0. \ No newline at end of file