ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/RootMacros/plotHistsAndRatio.C
Revision: 1.2
Committed: Sun Mar 22 19:24:39 2009 UTC (16 years, 1 month ago) by anderson
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +115 -44 lines
Log Message:
Displays histograms and ratio with a denomintor histogram on same canas

File Contents

# User Rev Content
1 anderson 1.1 /*************************************************
2 anderson 1.2 Plot histograms together, then plot
3 anderson 1.1 their ratio on a histogram below.
4    
5 anderson 1.2 Must provied at least 1 histogram
6     to be the "denomintator", and
7     up to 3 histograms to be "numerators".
8    
9     All the plots will be shown together in a
10     big plot, and below that
11     will be a plot showing the ratio of all
12     the "numerators" to the "denominator".
13    
14 anderson 1.1
15 anderson 1.2 Example use (first step is to prevent a bug in ROOT crashing):
16     root [1] .O0
17     root [2] .L plotHistsAndRatio.C
18     root [3] plotHistsAndRatio(photonEt_BarrelPIDcut,photonEt_Barrel,"Photon E_{T}","E_{T} (GeV)","entries/20 GeV bin")
19 anderson 1.1
20     Michael Anderson
21     March 18, 2009
22     *************************************************/
23    
24 anderson 1.2 #include "TH1F.h"
25     #include <vector>
26     #include <cmath>
27    
28     TCanvas* plotHistsAndRatio(TH1F* numeratorHist, TH1F* denominatorHist, TString title="", TString xTitle="", TString yTitle="") {
29     vector<TH1F*> numeratorHistograms;
30     numeratorHistograms.push_back( numeratorHist );
31     TCanvas* c1 = plotHistsAndRatio(numeratorHistograms, denominatorHist, title, xTitle, yTitle);
32     return c1;
33     }
34    
35     TCanvas* plotHistsAndRatio(TH1F* numeratorHist1, TH1F* numeratorHist2, TH1F* denominatorHist, TString title="", TString xTitle="", TString yTitle="") {
36     vector<TH1F*> numeratorHistograms;
37     numeratorHistograms.push_back( numeratorHist1 );
38     numeratorHistograms.push_back( numeratorHist2 );
39     TCanvas* c1 = plotHistsAndRatio(numeratorHistograms, denominatorHist, title, xTitle, yTitle);
40     return c1;
41     }
42    
43     TCanvas* plotHistsAndRatio(TH1F* numeratorHist1, TH1F* numeratorHist2, TH1F* numeratorHist3, TH1F* denominatorHist, TString title="", TString xTitle="", TString yTitle="") {
44     vector<TH1F*> numeratorHistograms;
45     numeratorHistograms.push_back( numeratorHist1 );
46     numeratorHistograms.push_back( numeratorHist2 );
47     numeratorHistograms.push_back( numeratorHist3 );
48     TCanvas *c1 = plotHistsAndRatio(numeratorHistograms, denominatorHist, title, xTitle, yTitle);
49     return c1;
50     }
51    
52     TCanvas* plotHistsAndRatio(vector<TH1F*> numeratorHistograms, TH1F* denominatorHist, TString title="", TString xTitle="", TString yTitle="") {
53 anderson 1.1
54 anderson 1.2 int numberOfNumeratorHists = numeratorHistograms.size();
55     if (numberOfNumeratorHists>3) {
56     cout << "Too many histograms for numerator (currently only supports up to 3)" << endl;
57     exit;
58     }
59     if (!denominatorHist) {
60     cout << "denominatorHist provided does not exist" << endl;
61     exit;
62     }
63 anderson 1.1
64     //*************************************************
65     // Variables
66 anderson 1.2 bool topPlotLogY = 1; // 0 = no log; 1= log
67     TString yTitle2 = "ratio"; // bottom plot y axis title
68 anderson 1.1
69 anderson 1.2 vector<int> histColors;
70     histColors.push_back(kBlue); // change colors as you like
71     histColors.push_back(kRed);
72     histColors.push_back(kGreen-1);
73 anderson 1.1
74 anderson 1.2 int histDenominatorColor = kBlack;
75 anderson 1.1
76 anderson 1.2 float defaultRatioYmin = 1.02;
77     float defaultRatioYmax = 0.60;
78 anderson 1.1 // END of Variables
79     //*************************************************
80    
81 anderson 1.2 TCanvas *c1 = new TCanvas("c1", "c1",0,0,600,500);
82     c1->Range(0,0,1,1);
83 anderson 1.1
84 anderson 1.2 vector<TH1F*> hists;
85     for (int i=0; i<numberOfNumeratorHists; i++) {
86     hists.push_back( (TH1F*)numeratorHistograms[i]->Clone() );
87     }
88     TH1F* denominatorHistogram = (TH1F*)denominatorHist->Clone();
89    
90    
91     // Create ratio histograms
92     vector<TH1F*> hist_over_denomHist;
93     for (int i=0; i<numberOfNumeratorHists; i++) {
94     hist_over_denomHist.push_back( (TH1F*)numeratorHistograms[i]->Clone() );
95     hist_over_denomHist[i]->GetTitle();
96     hist_over_denomHist[i]->Divide(denominatorHistogram);
97     }
98 anderson 1.1
99    
100     //*************************************************
101     // Bottom plot
102     TPad *c1_1 = new TPad("c1_1", "newpad",0.01,0.0.01,0.99,0.32);
103     c1_1->Draw();
104     c1_1->cd();
105     c1_1->SetTopMargin(0.01);
106     c1_1->SetBottomMargin(0.3);
107     c1_1->SetRightMargin(0.1);
108     c1_1->SetFillStyle(0);
109    
110 anderson 1.2
111     hist_over_denomHist[0]->Draw();
112     hist_over_denomHist[0]->SetLineWidth(1);
113     hist_over_denomHist[0]->SetLineColor(histColors[0]);
114     hist_over_denomHist[0]->SetMinimum(defaultRatioYmin);
115     hist_over_denomHist[0]->SetMaximum(defaultRatioYmax);
116     hist_over_denomHist[0]->GetYaxis()->SetNdivisions(5);
117     hist_over_denomHist[0]->SetTitle(";"+xTitle+";"+yTitle2);
118     hist_over_denomHist[0]->GetXaxis()->SetTitleSize(0.14);
119     hist_over_denomHist[0]->GetXaxis()->SetLabelSize(0.14);
120     hist_over_denomHist[0]->GetYaxis()->SetLabelSize(0.11);
121     hist_over_denomHist[0]->GetYaxis()->SetTitleSize(0.14);
122     hist_over_denomHist[0]->GetYaxis()->SetTitleOffset(0.28);
123     for (int i=1; i<numberOfNumeratorHists; i++) {
124     hist_over_denomHist[i]->SetLineWidth(1);
125     hist_over_denomHist[i]->SetLineColor(histColors[i]);
126     hist_over_denomHist[i]->Draw("same");
127     }
128 anderson 1.1 // End bottom plot
129     //*************************************************
130    
131    
132     //*************************************************
133     // Top Plot
134     c1->cd();
135     c1_2 = new TPad("c1_2", "newpad",0.01,0.33,0.99,0.99);
136     c1_2->Draw();
137     c1_2->cd();
138     c1_2->SetTopMargin(0.1);
139     c1_2->SetBottomMargin(0.01);
140     c1_2->SetRightMargin(0.1);
141     c1_1->SetFillStyle(0);
142    
143 anderson 1.2 denominatorHistogram->SetLineWidth(2);
144     denominatorHistogram->SetLineColor(histDenominatorColor);
145     denominatorHistogram->Draw();
146     denominatorHistogram->SetLabelSize(0.0);
147     denominatorHistogram->GetXaxis()->SetTitleSize(0.00);
148     denominatorHistogram->GetYaxis()->SetLabelSize(0.07);
149     denominatorHistogram->GetYaxis()->SetTitleSize(0.08);
150     denominatorHistogram->GetYaxis()->SetTitleOffset(0.76);
151     denominatorHistogram->SetTitle(title+";;"+yTitle);
152    
153     for (int i=0; i<numberOfNumeratorHists; i++) {
154     hists[i]->SetLineWidth(2);
155     hists[i]->SetLineColor(histColors[i]);
156     hists[i]->Draw("same");
157     }
158    
159 anderson 1.1 c1_2->SetLogy(topPlotLogY);
160 anderson 1.2 // End bottom plot
161     //*************************************************
162    
163     return c1;
164 anderson 1.1 }