Skip to content

Commit

Permalink
reorganize various helper functions into namespaces in separate header
Browse files Browse the repository at this point in the history
  • Loading branch information
kpedro88 committed Feb 3, 2015
1 parent 8770465 commit 9707a77
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 170 deletions.
40 changes: 11 additions & 29 deletions Plotting/KCode/KBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

//custom headers
#include "KMap.h"
#include "KHelper.h"
#include "KBase.h"
#include "TreeClass.h"

Expand Down Expand Up @@ -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<int,int> tmp(run,event);
map<pair<int,int>,int>::iterator cmit = countmap.find(tmp);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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
Expand Down
184 changes: 184 additions & 0 deletions Plotting/KCode/KHelper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
#ifndef KHELPER_H
#define KHELPER_H

//custom headers
#include "KMap.h"

//ROOT headers
#include <TROOT.h>
#include <TMath.h>

//STL headers
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <cmath>

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<string>& 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<string> 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<string>& 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 <class O> 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>(string val){
return val;
}
//special case: special processing for colors
template <> Color_t getOptionValue<Color_t>(string val){
return processColor(val);
}
//handles vector and non-vector options
template <class O> void addOption(OptionMap* option, string name, string val, bool isvector){
if(isvector){
vector<O> vtmp;
//comma-separated values
vector<string> fields;
process(val,',',fields);
for(int i = 0; i < fields.size(); i++){
vtmp.push_back(getOptionValue<O>(fields[i]));
}
option->Set(name,vtmp);
}
else {
option->Set(name, getOptionValue<O>(val));
}
}
//helper functions
void processOption(string line, OptionMap* option){
//type:name[value]
vector<string> 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<bool>(option,name,val,isvector);
else if(type=="int" || type=="i") addOption<int>(option,name,val,isvector);
else if(type=="double" || type=="d") addOption<double>(option,name,val,isvector);
else if(type=="string" || type=="s") addOption<string>(option,name,val,isvector);
else if(type=="color" || type=="c") addOption<Color_t>(option,name,val,isvector);
}
}

}


#endif
Loading

0 comments on commit 9707a77

Please sign in to comment.