From 9707a7753043392bc3541ece849032a6ac43ebbf Mon Sep 17 00:00:00 2001 From: Kevin Pedro Date: Tue, 3 Feb 2015 12:16:17 -0600 Subject: [PATCH] reorganize various helper functions into namespaces in separate header --- Plotting/KCode/KBuilder.h | 40 +++------ Plotting/KCode/KHelper.h | 184 ++++++++++++++++++++++++++++++++++++++ Plotting/KCode/KParser.h | 148 ++---------------------------- 3 files changed, 202 insertions(+), 170 deletions(-) create mode 100644 Plotting/KCode/KHelper.h diff --git a/Plotting/KCode/KBuilder.h b/Plotting/KCode/KBuilder.h index 69241c20..c5319823 100644 --- a/Plotting/KCode/KBuilder.h +++ b/Plotting/KCode/KBuilder.h @@ -4,6 +4,7 @@ //custom headers #include "KMap.h" +#include "KHelper.h" #include "KBase.h" #include "TreeClass.h" @@ -45,26 +46,7 @@ class KBuilder : public TreeClass { //destructor virtual ~KBuilder() {} - //helper functions - double phi(double x, double y) { - double phi_ = atan2(y, x); - return (phi_>=0) ? phi_ : phi_ + 2*TMath::Pi(); - } - double dPhi(double phi1, double phi2) { - double phi1_= phi( cos(phi1), sin(phi1) ); - double phi2_= phi( cos(phi2), sin(phi2) ); - double dphi_= phi1_-phi2_; - if( dphi_> TMath::Pi() ) dphi_-=2*TMath::Pi(); - if( dphi_<-TMath::Pi() ) dphi_+=2*TMath::Pi(); - - return dphi_; - } - double DeltaR(double phi1, double eta1, double phi2, double eta2){ - double dphi = dPhi(phi1,phi2); - double deta = eta2 - eta1; - double dR2 = dphi*dphi + deta*deta; - return sqrt(dR2); - } + bool CheckDoubleCount(){ pair tmp(run,event); map,int>::iterator cmit = countmap.find(tmp); @@ -346,7 +328,7 @@ class KBuilder : public TreeClass { } } else if(filter==1){ //w+jets control region - double MT=sqrt(2*MuonPt*PFMETPatType1*(1-cos(dPhi(MuonPhi,PFMETPhiPatType1)))); + double MT=sqrt(2*MuonPt*PFMETPatType1*(1-cos(KMath::DeltaPhi(MuonPhi,PFMETPhiPatType1)))); goodEvent &= (MuonPt > 29) //1 lepton with pT > 29 GeV && (HPSTauPt->at(0) > 40) //1 hadronic tau with pT > 40 GeV && (!(globalOpt->Get("jetveto",true)) || (PFJetMultiplicity == 0 || PFJetPt->at(0) < 30)) //0 jets with pT > 30 GeV - jet veto for fakes @@ -526,23 +508,23 @@ class KBuilder : public TreeClass { htmp->Fill(MassMuonTau,w); } else if(stmp=="Tmass"){//transverse mass to check W+jet contribution: MT=sqrt(2*pt_lepton*pt_met*cos(1-deltaphi(met,lepton)) - double MT=sqrt(2*MuonPt*PFMETPatType1*(1-cos(dPhi(MuonPhi,PFMETPhiPatType1)))); + double MT=sqrt(2*MuonPt*PFMETPatType1*(1-cos(KMath::DeltaPhi(MuonPhi,PFMETPhiPatType1)))); htmp->Fill(MT,w); } else if(stmp=="Tmassiso"){//transverse mass to check W+jet contribution (isolated taus): MT=sqrt(2*pt_lepton*pt_met*cos(1-deltaphi(met,lepton)) - double MT=sqrt(2*MuonPt*PFMETPatType1*(1-cos(dPhi(MuonPhi,PFMETPhiPatType1)))); + double MT=sqrt(2*MuonPt*PFMETPatType1*(1-cos(KMath::DeltaPhi(MuonPhi,PFMETPhiPatType1)))); if(HPSTaubyLooseCombinedIsolationDeltaBetaCorr3Hits->at(0)) htmp->Fill(MT,w); } else if(stmp=="Tmassjet"){//transverse mass w/ leading jet if(PFJetPt->size()>0){ - double MTj=sqrt(2*PFJetPt->at(0)*PFMETPatType1*(1-cos(dPhi(PFJetPhi->at(0),PFMETPhiPatType1)))); + double MTj=sqrt(2*PFJetPt->at(0)*PFMETPatType1*(1-cos(KMath::DeltaPhi(PFJetPhi->at(0),PFMETPhiPatType1)))); htmp->Fill(MTj,w); } } else if(stmp=="Tmasstauantiisoall"){//transverse mass w/ all anti-iso taus for(int t = 0; t < HPSTauPt->size(); t++){ if(!FakeTauGenDecision[t]) continue; - double MTt=sqrt(2*HPSTauPt->at(t)*PFMETPatType1*(1-cos(dPhi(HPSTauPhi->at(t),PFMETPhiPatType1)))); + double MTt=sqrt(2*HPSTauPt->at(t)*PFMETPatType1*(1-cos(KMath::DeltaPhi(HPSTauPhi->at(t),PFMETPhiPatType1)))); htmp->Fill(MTt,w); } } @@ -553,16 +535,16 @@ class KBuilder : public TreeClass { htmp->Fill(PFMETPatType1,w); } else if(stmp=="dphi"){//dphi of muon and tau - htmp->Fill(dPhi(MuonPhi,HPSTauPhi->at(0)),w); + htmp->Fill(KMath::DeltaPhi(MuonPhi,HPSTauPhi->at(0)),w); } else if(stmp=="dphimu"){//angular correlation w/ MET and muon - htmp->Fill(dPhi(PFMETPhiPatType1,MuonPhi),w); + htmp->Fill(KMath::DeltaPhi(PFMETPhiPatType1,MuonPhi),w); } else if(stmp=="dphitau"){//angular correlation w/ MET and tau - htmp->Fill(dPhi(PFMETPhiPatType1,HPSTauPhi->at(0)),w); + htmp->Fill(KMath::DeltaPhi(PFMETPhiPatType1,HPSTauPhi->at(0)),w); } else if(stmp=="dphijet"){//angular correlation w/ MET and leading jet - if(PFJetPt->size()>0) htmp->Fill(dPhi(PFMETPhiPatType1,PFJetPhi->at(0)),w); + if(PFJetPt->size()>0) htmp->Fill(KMath::DeltaPhi(PFMETPhiPatType1,PFJetPhi->at(0)),w); } else if(stmp=="ptmu"){//muon pT if(globalOpt->Get("calcfaketau",false)){ //data-driven shape estimation for fake tau ST diff --git a/Plotting/KCode/KHelper.h b/Plotting/KCode/KHelper.h new file mode 100644 index 00000000..1b9bbeab --- /dev/null +++ b/Plotting/KCode/KHelper.h @@ -0,0 +1,184 @@ +#ifndef KHELPER_H +#define KHELPER_H + +//custom headers +#include "KMap.h" + +//ROOT headers +#include +#include + +//STL headers +#include +#include +#include +#include +#include + +using namespace std; + +namespace KMath { + //helper functions + double phi(double x, double y) { + double phi_ = atan2(y, x); + return (phi_>=0) ? phi_ : phi_ + 2*TMath::Pi(); + } + double DeltaPhi(double phi1, double phi2) { + double phi1_= phi( cos(phi1), sin(phi1) ); + double phi2_= phi( cos(phi2), sin(phi2) ); + double dphi_= phi1_-phi2_; + if( dphi_> TMath::Pi() ) dphi_-=2*TMath::Pi(); + if( dphi_<-TMath::Pi() ) dphi_+=2*TMath::Pi(); + + return dphi_; + } + double DeltaR(double phi1, double eta1, double phi2, double eta2){ + double dphi = DeltaPhi(phi1,phi2); + double deta = eta2 - eta1; + double dR2 = dphi*dphi + deta*deta; + return sqrt(dR2); + } +} + +namespace KText { + //generalization for processing a line + void process(string line, char delim, vector& fields){ + stringstream ss(line); + string field; + while(getline(ss,field,delim)){ + fields.push_back(field); + } + } + //finds the first location of a given char in a string + int searchLine(string line, char val){ + for(int i = 0; i < line.size(); i++){ + if(line[i]==val) return i; + } + return -1; + } + //turns text input into ROOT colors: kXXX +/- # + Color_t processColor(string line){ + //color input is space-separated + vector fields; + process(line,' ',fields); + + Color_t color = 0; + //mapping of strings to Color_t enums (from Rtypes.h) + if(fields[0]=="kWhite") color = kWhite; + else if(fields[0]=="kBlack") color = kBlack; + else if(fields[0]=="kGray") color = kGray; + else if(fields[0]=="kRed") color = kRed; + else if(fields[0]=="kGreen") color = kGreen; + else if(fields[0]=="kBlue") color = kBlue; + else if(fields[0]=="kYellow") color = kYellow; + else if(fields[0]=="kMagenta") color = kMagenta; + else if(fields[0]=="kCyan") color = kCyan; + else if(fields[0]=="kOrange") color = kOrange; + else if(fields[0]=="kSpring") color = kSpring; + else if(fields[0]=="kTeal") color = kTeal; + else if(fields[0]=="kAzure") color = kAzure; + else if(fields[0]=="kViolet") color = kViolet; + else if(fields[0]=="kPink") color = kPink; + + //check for addition or subtraction + if(fields.size()==3){ + int mod; + stringstream ms(fields[2]); + ms >> mod; + + if(fields[1]=="+") color += mod; + else if(fields[1]=="-") color -= mod; + } + + return color; + } + //splits option format into three components: type:name[value] + bool splitOption(string line, vector& fields){ + //first component: type + int colon = searchLine(line, ':'); + if(colon>-1) { + fields.push_back(line.substr(0,colon)); + line.erase(0,colon+1); + } + else return false; + + //second component: name + int lbracket = searchLine(line, '['); + if(lbracket>-1){ + fields.push_back(line.substr(0,lbracket)); + line.erase(0,lbracket+1); + } + else return false; + + //third component: value + if(line[line.size()-1]==']') line.erase(line.size()-1,1); + fields.push_back(line); + return true; + } + //handles default type cases + //special cases declared below + template O getOptionValue(string val){ + stringstream sval(val); + O tmp; + sval >> tmp; + return tmp; + } + //function template specializations + //special case: just take the string directly + template <> string getOptionValue(string val){ + return val; + } + //special case: special processing for colors + template <> Color_t getOptionValue(string val){ + return processColor(val); + } + //handles vector and non-vector options + template void addOption(OptionMap* option, string name, string val, bool isvector){ + if(isvector){ + vector vtmp; + //comma-separated values + vector fields; + process(val,',',fields); + for(int i = 0; i < fields.size(); i++){ + vtmp.push_back(getOptionValue(fields[i])); + } + option->Set(name,vtmp); + } + else { + option->Set(name, getOptionValue(val)); + } + } + //helper functions + void processOption(string line, OptionMap* option){ + //type:name[value] + vector fields; + bool goodOption = splitOption(line,fields); + if(!goodOption) { + cout << "Improperly formatted option:" << endl; + cout << line << endl; + cout << "This option will not be added." << endl; + return; + } + + //currently anticipated option types: full/abbrev. + //bool/b, int/i, double/d, string/s, color/c, + //vbool/vb, vint/vi, vdouble/vd, vstring/vs, vcolor/vc (vectors) + //others could easily be added... + if(fields.size()>=3){ + string type = fields[0]; string name = fields[1]; string val = fields[2]; + bool isvector = type[0]=='v'; + if(isvector) type.erase(0,1); + + //match strings to types + if(type=="bool" || type=="b") addOption(option,name,val,isvector); + else if(type=="int" || type=="i") addOption(option,name,val,isvector); + else if(type=="double" || type=="d") addOption(option,name,val,isvector); + else if(type=="string" || type=="s") addOption(option,name,val,isvector); + else if(type=="color" || type=="c") addOption(option,name,val,isvector); + } + } + +} + + +#endif \ No newline at end of file diff --git a/Plotting/KCode/KParser.h b/Plotting/KCode/KParser.h index cb3b0d64..b0107391 100644 --- a/Plotting/KCode/KParser.h +++ b/Plotting/KCode/KParser.h @@ -3,6 +3,7 @@ //custom headers #include "KManager.h" +#include "KHelper.h" //ROOT headers #include @@ -51,7 +52,7 @@ class KParser { else if(line.compare(0,5,"HISTO")==0) { intype = "HISTO"; continue; } //otherwise, process line according to input type - if(intype=="OPTION") processOption(line,MyManager->globalOpt); + if(intype=="OPTION") KText::processOption(line,MyManager->globalOpt); else if(intype=="SET") processSet(line); else if(intype=="HISTO") processHisto(line); } @@ -63,82 +64,6 @@ class KParser { } return parsed; } - //helper functions - void processOption(string line, OptionMap* option){ - //type:name[value] - vector fields; - bool goodOption = splitOption(line,fields); - if(!goodOption) { - cout << "Improperly formatted option:" << endl; - cout << line << endl; - cout << "This option will not be added." << endl; - return; - } - - //currently anticipated option types: full/abbrev. - //bool/b, int/i, double/d, string/s, color/c, - //vbool/vb, vint/vi, vdouble/vd, vstring/vs, vcolor/vc (vectors) - //others could easily be added... - if(fields.size()>=3){ - string type = fields[0]; string name = fields[1]; string val = fields[2]; - bool isvector = type[0]=='v'; - if(isvector) type.erase(0,1); - - //match strings to types - if(type=="bool" || type=="b") addOption(option,name,val,isvector); - else if(type=="int" || type=="i") addOption(option,name,val,isvector); - else if(type=="double" || type=="d") addOption(option,name,val,isvector); - else if(type=="string" || type=="s") addOption(option,name,val,isvector); - else if(type=="color" || type=="c") addOption(option,name,val,isvector); - } - } - //handles vector and non-vector options - template void addOption(OptionMap* option, string name, string val, bool isvector){ - if(isvector){ - vector vtmp; - //comma-separated values - vector fields; - process(val,',',fields); - for(int i = 0; i < fields.size(); i++){ - vtmp.push_back(getOptionValue(fields[i])); - } - option->Set(name,vtmp); - } - else { - option->Set(name, getOptionValue(val)); - } - } - //handles default type cases - //special cases declared below (namespace scope, outside class declaration) - template O getOptionValue(string val){ - stringstream sval(val); - O tmp; - sval >> tmp; - return tmp; - } - //splits option format into three components: type:name[value] - bool splitOption(string line, vector& fields){ - //first component: type - int colon = searchLine(line, ':'); - if(colon>-1) { - fields.push_back(line.substr(0,colon)); - line.erase(0,colon+1); - } - else return false; - - //second component: name - int lbracket = searchLine(line, '['); - if(lbracket>-1){ - fields.push_back(line.substr(0,lbracket)); - line.erase(0,lbracket+1); - } - else return false; - - //third component: value - if(line[line.size()-1]==']') line.erase(line.size()-1,1); - fields.push_back(line); - return true; - } void processSet(string line){ //cout << line << endl; int indent = 0; @@ -149,7 +74,7 @@ class KParser { //tab separated input vector fields; - process(line,'\t',fields); + KText::process(line,'\t',fields); //debug: print the contents of curr_sets /*map::iterator dit; @@ -188,7 +113,7 @@ class KParser { OptionMap* omap = new OptionMap(); //for local options //process local options before constructing objects for(int i = 3; i < fields.size(); i++){ - processOption(fields[i],omap); + KText::processOption(fields[i],omap); } KBase* tmp = 0; @@ -218,72 +143,23 @@ class KParser { return; } } - Color_t processColor(string line){ - //color input is space-separated - vector fields; - process(line,' ',fields); - - Color_t color = 0; - //mapping of strings to Color_t enums (from Rtypes.h) - if(fields[0]=="kWhite") color = kWhite; - else if(fields[0]=="kBlack") color = kBlack; - else if(fields[0]=="kGray") color = kGray; - else if(fields[0]=="kRed") color = kRed; - else if(fields[0]=="kGreen") color = kGreen; - else if(fields[0]=="kBlue") color = kBlue; - else if(fields[0]=="kYellow") color = kYellow; - else if(fields[0]=="kMagenta") color = kMagenta; - else if(fields[0]=="kCyan") color = kCyan; - else if(fields[0]=="kOrange") color = kOrange; - else if(fields[0]=="kSpring") color = kSpring; - else if(fields[0]=="kTeal") color = kTeal; - else if(fields[0]=="kAzure") color = kAzure; - else if(fields[0]=="kViolet") color = kViolet; - else if(fields[0]=="kPink") color = kPink; - - //check for addition or subtraction - if(fields.size()==3){ - int mod; - stringstream ms(fields[2]); - ms >> mod; - - if(fields[1]=="+") color += mod; - else if(fields[1]=="-") color -= mod; - } - - return color; - } void processHisto(string line){ //tab separated input vector fields; - process(line,'\t',fields); + KText::process(line,'\t',fields); string name = fields[0]; OptionMap* omap = new OptionMap(); //everything after the name is an option for(int i = 1; i < fields.size(); i++){ - processOption(fields[i],omap); + KText::processOption(fields[i],omap); } //store local plot options for later use MyManager->MyPlotOptions.Add(name,omap); } - //generalization for processing a line - void process(string line, char delim, vector& fields){ - stringstream ss(line); - string field; - while(getline(ss,field,delim)){ - fields.push_back(field); - } - } - //finds the first location of a given char in a string - int searchLine(string line, char val){ - for(int i = 0; i < line.size(); i++){ - if(line[i]==val) return i; - } - return -1; - } + private: //member variables @@ -292,16 +168,6 @@ class KParser { }; -//function template specializations -//special case: just take the string directly -template <> string KParser::getOptionValue(string val){ - return val; -} -//special case: special processing for colors -template <> Color_t KParser::getOptionValue(string val){ - return processColor(val); -} - //implementation of KManager constructor //note: dir="tree" default KManager::KManager(string in, string dir) : input(in),treedir(dir),globalOpt(0),numer(0),denom(0),yieldref(0),doPrint(false),varbins(0),nbins(0),parsed(false) {