Skip to content

Commit

Permalink
use new option format for histos, plus some general upgrades to KPlot
Browse files Browse the repository at this point in the history
  • Loading branch information
kpedro88 committed Jan 31, 2015
1 parent 3acb5b4 commit 912bff6
Show file tree
Hide file tree
Showing 8 changed files with 205 additions and 203 deletions.
136 changes: 71 additions & 65 deletions Plotting/KCode/KManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

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

//STL headers
#include <string>
Expand All @@ -29,22 +28,22 @@ class KManager {
public:
friend class KParser;
//constructor
KManager() : input(""),treedir("tree"),MyParser(0),MyRatio(0),option(0),s_numer(""),s_denom(""),s_yieldref(""),numer(0),denom(0),yieldref(0),doPrint(false),varbins(0),nbins(0),parsed(false) {}
KManager() : input(""),treedir("tree"),MyParser(0),MyRatio(0),globalOpt(0),s_numer(""),s_denom(""),s_yieldref(""),numer(0),denom(0),yieldref(0),doPrint(false),varbins(0),nbins(0),parsed(false) {}
KManager(string in, string dir="tree"); //implemented in KParser.h to avoid circular dependency
//destructor
virtual ~KManager() {}
virtual void FakeTauEstimationInit(){
string tfr_name = "";
TFile* tfr_file = 0;
if(option->Get("tfr_file",tfr_name)) tfr_file = TFile::Open(tfr_name.c_str(),"READ");
if(globalOpt->Get("tfr_file",tfr_name)) tfr_file = TFile::Open(tfr_name.c_str(),"READ");
if(tfr_file) {
TGraphAsymmErrors* tfr_data = (TGraphAsymmErrors*)tfr_file->Get("data");
TGraphAsymmErrors* tfr_mc = 0;
if(option->Get("wjet_tfr",false)) tfr_mc = (TGraphAsymmErrors*)tfr_file->Get("W + jets");
else if(option->Get("ttbar_tfr",false)) tfr_mc = (TGraphAsymmErrors*)tfr_file->Get("ttbar");
if(globalOpt->Get("wjet_tfr",false)) tfr_mc = (TGraphAsymmErrors*)tfr_file->Get("W + jets");
else if(globalOpt->Get("ttbar_tfr",false)) tfr_mc = (TGraphAsymmErrors*)tfr_file->Get("ttbar");
else tfr_mc = (TGraphAsymmErrors*)tfr_file->Get("Z + jets");
option->Set("tfr_data",tfr_data);
option->Set("tfr_mc",tfr_mc);
globalOpt->Set("tfr_data",tfr_data);
globalOpt->Set("tfr_mc",tfr_mc);
}
}
//where the magic happens
Expand All @@ -55,21 +54,59 @@ class KManager {
//setup root output file if requested
TFile* out_file = 0;
string rootfilename = "";
if(option->Get<string>("rootfile",rootfilename)) out_file = TFile::Open((rootfilename+".root").c_str(),"RECREATE");
if(globalOpt->Get<string>("rootfile",rootfilename)) out_file = TFile::Open((rootfilename+".root").c_str(),"RECREATE");

//check for intlumi
//if it has not been set by the user, get it from data
double intlumi = 0;
if(!(option->Get<double>("luminorm",intlumi))) {
if(!(globalOpt->Get<double>("luminorm",intlumi))) {
for(unsigned s = 0; s < MySets.size(); s++){
double tmp = MySets[s]->GetIntLumi();
if(tmp>0) { intlumi = tmp; }
}
if(intlumi==0) intlumi = 1; //default value
KOption<double>* otmp = new KOption<double>(intlumi);
option->Add("luminorm",otmp);
globalOpt->Add("luminorm",otmp);
}

//get special status options
globalOpt->Get("numer",s_numer);
globalOpt->Get("denom",s_denom);
globalOpt->Get("yieldref",s_yieldref);

//check for special status sets
for(unsigned s = 0; s < MySets.size(); s++){
if(s_numer.size()>0 && numer==0) numer = MySets[s]->CheckSpecial(s_numer);
if(s_denom.size()>0 && denom==0) denom = MySets[s]->CheckSpecial(s_denom);
if(s_yieldref.size()>0 && yieldref==0) yieldref = MySets[s]->CheckSpecial(s_yieldref);
}

//create plots from local options
OMMit om;
bool ratio_requested = false;
for(om = MyPlotOptions.GetTable().begin(); om != MyPlotOptions.GetTable().end(); om++){
if(om->second->Get("ratio",true)) ratio_requested = true; //ratios turned on by default
if(!numer || !denom) om->second->Set("ratio",false); //disable ratios if components not available
KPlot* ptmp = new KPlot(om->first,om->second,globalOpt);
if(ptmp->GetBuilt()) MyPlots.Add(om->first,ptmp);
else {
cout << "Input error: unable to build histo " << om->first << ". Check binning options." << endl;
delete ptmp;
}
}
if(numer && denom){
//add children to ratio
MyRatio->AddNumerator(numer);
MyRatio->AddDenominator(denom);
}
else if(ratio_requested){
cout << "Input error: ratio requested, but ";
if(!numer && !denom) cout << "numer and denom ";
else if(!numer) cout << "numer ";
else if(!denom) cout << "denom ";
cout << "not set. Ratio will not be drawn." << endl;
}

//load histos into sets
PMit p;
for(p = MyPlots.GetTable().begin(); p != MyPlots.GetTable().end(); p++){
Expand All @@ -78,51 +115,37 @@ class KManager {
}
}

//get special status options
option->Get("numer",s_numer);
option->Get("denom",s_denom);
option->Get("yieldref",s_yieldref);

//build everything & check for special status
//build everything
for(unsigned s = 0; s < MySets.size(); s++){
MySets[s]->Build();
if(s_numer.size()>0 && numer==0) numer = MySets[s]->CheckSpecial(s_numer);
if(s_denom.size()>0 && denom==0) denom = MySets[s]->CheckSpecial(s_denom);
if(s_yieldref.size()>0 && yieldref==0) yieldref = MySets[s]->CheckSpecial(s_yieldref);
}

//add children to ratio, in case it is needed
MyRatio->AddNumerator(numer);
MyRatio->AddDenominator(denom);

//fake tau estimation is calculated during build
if(option->Get("calcfaketau",false)){
if(globalOpt->Get("calcfaketau",false)){
double ft_norm = 0;
option->Get("ft_norm",ft_norm);
globalOpt->Get("ft_norm",ft_norm);
double ft_err = 0;
option->Get("ft_err",ft_err);
globalOpt->Get("ft_err",ft_err);
//finish error calc
ft_err = sqrt(ft_err);

int prcsn;
if(option->Get("yieldprecision",prcsn)) cout << fixed << setprecision(prcsn);
if(globalOpt->Get("yieldprecision",prcsn)) cout << fixed << setprecision(prcsn);
cout << "fake tau norm = " << ft_norm << " +/- " << ft_err << endl;
}

//draw each plot - normalization, legend, ratio
for(p = MyPlots.GetTable().begin(); p != MyPlots.GetTable().end(); p++){
//set lumi for pave
p->second->SetLumi(intlumi);
//get drawing objects from KPlot
TCanvas* can = p->second->GetCanvas();
TPad* pad1 = p->second->GetPad1();

//create legend
string chan_label = "";
option->Get("chan_label",chan_label);
globalOpt->Get("chan_label",chan_label);
KLegend* kleg = new KLegend(pad1,chan_label);
double ymin = 1;
if(option->Get("ymin",ymin)) kleg->SetManualYmin(ymin);
if(globalOpt->Get("ymin",ymin)) kleg->SetManualYmin(ymin);
p->second->SetLegend(kleg);

//select current histogram in sets
Expand All @@ -132,33 +155,33 @@ class KManager {

//check if normalization to yield is desired (disabled by default)
//BEFORE printing yields
if(p->second->GetOption()->Get("yieldnorm",false) && yieldref){
if(p->second->GetLocalOpt()->Get("yieldnorm",false) && yieldref){
double yield = yieldref->GetYield();
if(yield>0){
for(unsigned s = 0; s < MySets.size(); s++){
if(MySets[s] != yieldref) MySets[s]->Normalize(yield);
}
}
}
else if(p->second->GetOption()->Get("yieldnorm",false) && yieldref){
else if(p->second->GetLocalOpt()->Get("yieldnorm",false) && !yieldref){
cout << "Input error: normalization to yield requested, but yieldref not set. Normalization will not be performed." << endl;
}

//print yield if enabled
//BEFORE bindivide if requested
if(option->Get("printyield",false)) cout << p->first << " yield:" << endl;
if(globalOpt->Get("printyield",false)) cout << p->first << " yield:" << endl;
for(unsigned s = 0; s < MySets.size(); s++){
if(option->Get("printyield",false)) {
if(globalOpt->Get("printyield",false)) {
int prcsn;
if(option->Get("yieldprecision",prcsn)) cout << fixed << setprecision(prcsn);
if(globalOpt->Get("yieldprecision",prcsn)) cout << fixed << setprecision(prcsn);
MySets[s]->PrintYield();
}
}
if(option->Get("printyield",false)) cout << endl;
if(globalOpt->Get("printyield",false)) cout << endl;

//check if division by bin width is desired (disabled by default)
//AFTER printing yields
if(p->second->GetOption()->Get("bindivide",false)){
if(p->second->GetLocalOpt()->Get("bindivide",false)){
for(unsigned s = 0; s < MySets.size(); s++){
MySets[s]->BinDivide();
}
Expand All @@ -182,17 +205,6 @@ class KManager {

//draw blank histo for axes
p->second->DrawHist();
//horizontal error bars for histograms are DISABLED by default
//but auto-enabled for variable-binned histograms (per CMS style guidelines)
//this could eventually be moved to KPlot once the global/local option map update is done
//(note: TExec cannot be the first thing drawn on a pad)
TExec* exec = 0;
string execname = "exec_" + p->first;
if(option->Get("horizerrbars",false) || p->second->GetHisto()->GetXaxis()->IsVariableBinSize()){
exec = new TExec(execname.c_str(),"gStyle->SetErrorX(0.5);");
}
else exec = new TExec(execname.c_str(),"gStyle->SetErrorX(0);");
exec->Draw();
//draw sets (reverse order, so first set is on top)
for(int s = MySets.size()-1; s >= 0; s--){
MySets[s]->Draw(pad1);
Expand All @@ -207,14 +219,14 @@ class KManager {
p->second->GetHisto()->Draw("sameaxis"); //draw again so axes on top
p->second->DrawText();

//ratio (enabled by default, i.e. noratio disabled by default)
if(!(p->second->GetOption()->Get("noratio",false)) && numer && denom){
//ratio (enabled by default, auto-disabled above if components not set)
if(p->second->GetLocalOpt()->Get("ratio",true)){
TPad* pad2 = p->second->GetPad2();

//reset ratio name if provided by user
//default, data/mc, is set in KPlot
string rationame;
if(option->Get<string>("rationame",rationame)) p->second->GetRatio()->GetYaxis()->SetTitle(rationame.c_str());
if(globalOpt->Get<string>("rationame",rationame)) p->second->GetRatio()->GetYaxis()->SetTitle(rationame.c_str());

p->second->DrawRatio();

Expand All @@ -223,30 +235,23 @@ class KManager {
MyRatio->Draw(pad2);
p->second->DrawLine();
}
else if(!(p->second->GetOption()->Get("noratio",false)) && (!numer || !denom)){
cout << "Input error: ratio requested, but ";
if(!numer && !denom) cout << "numer and denom ";
else if(!numer) cout << "numer ";
else if(!denom) cout << "denom ";
cout << "not set. Ratio will not be drawn." << endl;
}

//print formats given as a vector option
vector<string> printformat;
if(doPrint && option->Get<vector<string> >("printformat",printformat)){
if(doPrint && globalOpt->Get<vector<string> >("printformat",printformat)){
for(int j = 0; j < printformat.size(); j++){
string pformat = printformat[j];
string oname = p->first;
string suff = "";
if(option->Get("printsuffix",suff)) oname += "_" + suff;
if(globalOpt->Get("printsuffix",suff)) oname += "_" + suff;
oname += "." + pformat;
can->Print(oname.c_str(),pformat.c_str());
}
}
}

//close all root files
if(option->Get("closefiles",false)){
if(globalOpt->Get("closefiles",false)){
out_file->Close();
for(unsigned s = 0; s < MySets.size(); s++){
MySets[s]->CloseFile();
Expand All @@ -257,10 +262,10 @@ class KManager {
//accessors
bool GetPrint() { return doPrint; }
void SetPrint(bool p) { doPrint = p; }
OptionMap* GetOption() { return option; }
OptionMap* GetGlobalOpt() { return globalOpt; }
void ListOptions() {
OMit it;
for(it = option->GetTable().begin(); it != option->GetTable().end(); it++){
for(it = globalOpt->GetTable().begin(); it != globalOpt->GetTable().end(); it++){
cout << it->first /*<< ": " << it->second->value*/ << endl;
}
}
Expand All @@ -272,9 +277,10 @@ class KManager {
string input, treedir;
KParser* MyParser;
PlotMap MyPlots;
OptionMapMap MyPlotOptions;
vector<KBase*> MySets;
KSetRatio* MyRatio;
OptionMap* option;
OptionMap* globalOpt;
string s_numer, s_denom, s_yieldref; //names for special sets
KBase *numer, *denom, *yieldref; //pointers to special sets
bool doPrint;
Expand Down
2 changes: 2 additions & 0 deletions Plotting/KCode/KMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,11 @@ typedef map<string,TH1F*>::iterator HMit;
typedef map<string,THStack*>::iterator SMit;
typedef map<string,TGraphAsymmErrors*>::iterator EMit;
typedef map<string,KPlot*>::iterator PMit;
typedef map<string,OptionMap*>::iterator OMMit;
typedef KMap<TH1F*> HistoMap;
typedef KMap<THStack*> StackMap;
typedef KMap<TGraphAsymmErrors*> ErrorMap;
typedef KMap<KPlot*> PlotMap;
typedef KMap<OptionMap*> OptionMapMap;

#endif
Loading

0 comments on commit 912bff6

Please sign in to comment.