Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 166 additions & 0 deletions SelectionOptimization/VarCut.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
#include "VarCut.hh"
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <cstring>
#include "assert.h"

using std::vector;
using std::string;

const int UNDEFCUT = -999;

Expand Down Expand Up @@ -33,6 +41,108 @@ TCut *VarCut::getCut(){
return cut;
}

TCut *VarCut::getCutsForMVAcomparison(){

TCut *cut = 0;

// Die if something appears uninitialized
for(int i=0; i<Vars::nVariables; i++){
if( _cuts[i] == UNDEFCUT ){
printf("VarCut:: not all cuts are set! Die!\n");
assert(0);
}
}

cut = new TCut("");
for(int i=0; i<Vars::nVariables; i++){
// The += adds all cuts with &&:
if( i==getVariableIndex("relIsoWithEA") || i==getVariableIndex("d0") || i==getVariableIndex("dz") || i==8 )
continue;
(*cut) += TString::Format(" %s < %f ",
Vars::variables[i]->nameTmva.Data(),
_cuts[i]);
}

return cut;
}


TCut *VarCut::getCutDB(){

TCut *cut = 0;

// Die if something appears uninitialized
for(int i=0; i<VarsDB::nVariables; i++){
if( _cuts[i] == UNDEFCUT ){
printf("VarCut:: not all cuts are set! Die!\n");
assert(0);
}
}

cut = new TCut("");
for(int i=0; i<VarsDB::nVariables; i++){
// The += adds all cuts with &&:
(*cut) += TString::Format(" %s < %f ",
VarsDB::variables[i]->nameTmva.Data(),
_cuts[i]);
}

return cut;
}


TCut *VarCut::getCutNminusOne(int varIndex){

TCut *cut = 0;

// Die if something appears uninitialized
for(int i=0; i<Vars::nVariables; i++){
if( _cuts[i] == UNDEFCUT ){
printf("VarCut:: not all cuts are set! Die!\n");
assert(0);
}


}

cut = new TCut("");
for(int i=0; i<Vars::nVariables; i++){
// The += adds all cuts with &&:
if(i == varIndex) continue;
(*cut) += TString::Format(" %s < %f ",
Vars::variables[i]->nameTmva.Data(),
_cuts[i]);
}

return cut;
}


TCut *VarCut::getCutOneVar(int varIndex){

TCut *cut = 0;

// Die if something appears uninitialized
for(int i=0; i<Vars::nVariables; i++){
if( _cuts[i] == UNDEFCUT ){
printf("VarCut:: not all cuts are set! Die!\n");
assert(0);
}
}

cut = new TCut("");
for(int i=0; i<Vars::nVariables; i++){
// The += adds all cuts with &&:
if(i != varIndex) continue;
(*cut) += TString::Format(" %s < %f ",
Vars::variables[i]->nameTmva.Data(),
_cuts[i]);
}

return cut;
}


void VarCut::setCutValue(TString varName, float val){

int index = getVariableIndex(varName);
Expand Down Expand Up @@ -73,6 +183,24 @@ float VarCut::getCutValue(TString variable){
return cutVal;
}


float VarCut::getCutValueTmvaName(TString variable){

float cutVal = UNDEFCUT;
int index = getVariableIndexTmvaName(variable);

if( index != -1 ){
cutVal = _cuts[index];
}else{
printf("VarCut::getCutValue: requested variable is not known!!!\n");
}

return cutVal;
}




int VarCut::getVariableIndex(TString variable){

int index = -1;
Expand Down Expand Up @@ -124,4 +252,42 @@ void VarCut::print(){
}


vector <string> VarCut::printTable(){

string::size_type maxlen = strlen(Vars::variables[8]->nameTmva.Data()) + 6 ; // because it is "| " on the left plus " < |" on the right

vector<string> ret;

// std::cout<<maxlen;
//maxlen =30

for(int iVar =0; iVar < Vars::nVariables; ++iVar){

std::string currStr, cutStr;
char currStr_buff[100], cutStr_buff[100];

if(iVar==0){
currStr =
"| Variables"
+ string(maxlen - strlen("| Variables "),' ') + string("|");
ret.push_back(currStr);

}

sprintf(currStr_buff, "| %s" ,Vars::variables[iVar]->nameTmva.Data() );
currStr = currStr_buff;
currStr += string(maxlen- strlen(Vars::variables[iVar]->nameTmva.Data()) -6,' '); //exclude 2 spaces + 4 for " < |"
currStr += string(" < |");

sprintf(cutStr_buff, " %1.6f" , _cuts[iVar] );
cutStr= cutStr_buff;
cutStr += string(maxlen - 29,' '); //exclude 2 spaces + 8 chars number
cutStr += string("|");
currStr += cutStr;
//currStr length is 11 " '8digits' |"

ret.push_back(currStr);
} //end of for-loop

return ret;
} //end of printTable fcn
15 changes: 14 additions & 1 deletion SelectionOptimization/VarCut.hh
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@

#include "TCut.h"
#include <TObject.h>

#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include "Variables.hh"
#include "VariablesDB.hh"

using std::vector;
using std::string;

class VarCut :public TObject {

Expand All @@ -23,9 +30,14 @@ public:

// Look up cut value for given variable (regular name)
float getCutValue(TString var);
float getCutValueTmvaName(TString var);

// Get the full TCut object with cuts on all variables
TCut* getCut();
TCut *getCutsForMVAcomparison();
TCut* getCutDB();
TCut* getCutNminusOne(int varIndex);
TCut* getCutOneVar(int varIndex);

// Get index of the variable in the internal array from its name,
// using the regular name and the name known to TMVA (may include abs())
Expand All @@ -37,6 +49,7 @@ public:

// Input/output
void print(); // print to stdout
vector<string> printTable(); // print to stdout

private:
// The actual list of variables for which cuts are stored here
Expand Down
Loading