1 |
/*************************************************
|
2 |
Plot given histograms together (up to 4)
|
3 |
|
4 |
Parameters to draw 2 histograms:
|
5 |
plotHists(hist1, hist2, label1, label2, Title, xTitle, yTitle)
|
6 |
|
7 |
Example use (first step is to prevent a bug in ROOT crashing):
|
8 |
root [1] .O0
|
9 |
root [2] .L plotHistsAndRatio.C
|
10 |
root [3] plotHists(photonEt_Barrel,photonEt_Barrel3TrkCut,"Photon E_{T}","E_{T}","entries/20 GeV Bin","#gamma","#gamma track cut")
|
11 |
|
12 |
Michael Anderson
|
13 |
March 18, 2009
|
14 |
*************************************************/
|
15 |
|
16 |
#include "TH1.h"
|
17 |
#include <vector>
|
18 |
#include <cmath>
|
19 |
|
20 |
TCanvas* plotHists(vector<TH1*> histogramsToPlot, vector<TString> histogramLabels, TString title="", TString xTitle="", TString yTitle="", bool areaNormalize=false) {
|
21 |
|
22 |
|
23 |
//*************************************************
|
24 |
// Variables
|
25 |
bool plotLogY = true; // false = no log; true = log
|
26 |
|
27 |
vector<int> histColors;
|
28 |
histColors.push_back(kBlack); // change colors as you like
|
29 |
histColors.push_back(kBlue);
|
30 |
histColors.push_back(kRed);
|
31 |
histColors.push_back(kGreen-1);
|
32 |
|
33 |
float histLineWidth = 2.0;
|
34 |
|
35 |
bool useMarkers = true; // search online for TAttMarker
|
36 |
vector<int> histMarkerStyles; // to see all available markers
|
37 |
histMarkerStyles.push_back( 20 );
|
38 |
histMarkerStyles.push_back( 22 );
|
39 |
histMarkerStyles.push_back( 21 );
|
40 |
histMarkerStyles.push_back( 23 );
|
41 |
float histMarkerSize = 0.9;
|
42 |
|
43 |
int plotWidth=600;
|
44 |
int plotHeight=600;
|
45 |
|
46 |
// END of Variables
|
47 |
//*************************************************
|
48 |
|
49 |
|
50 |
// Create the canvas
|
51 |
TCanvas *c1 = new TCanvas("c1", "c1",0,0,plotWidth,plotHeight);
|
52 |
|
53 |
// Turn off stats box
|
54 |
gStyle->SetOptStat(0);
|
55 |
|
56 |
// Create copies of histograms provided
|
57 |
// and area normalize, if requested
|
58 |
vector<TH1*> hists;
|
59 |
for (int i=0; i<histogramsToPlot.size(); i++) {
|
60 |
hists.push_back( (TH1*)histogramsToPlot[i]->Clone() );
|
61 |
if (areaNormalize) {
|
62 |
Double_t integral = hists[i]->Integral();
|
63 |
if (integral>0.0) hists[i]->Scale(1/integral);
|
64 |
}
|
65 |
}
|
66 |
|
67 |
|
68 |
// Set histogram plotting variables
|
69 |
// and add them to the histogram stack & legend
|
70 |
THStack *tempStack = new THStack();
|
71 |
TLegend *infoBox = new TLegend(0.65, 0.70, 0.90, 0.85, "");
|
72 |
for (int i=0; i<histogramsToPlot.size(); i++) {
|
73 |
|
74 |
hists[i]->SetLineColor(histColors[i]);
|
75 |
hists[i]->SetLineWidth(histLineWidth);
|
76 |
|
77 |
if (useMarkers) {
|
78 |
hists[i]->SetMarkerStyle(histMarkerStyles[i]);
|
79 |
hists[i]->SetMarkerColor(histColors[i]);
|
80 |
hists[i]->SetMarkerSize(histMarkerSize);
|
81 |
}
|
82 |
|
83 |
infoBox->AddEntry(hists[i],histogramLabels[i],"LP");
|
84 |
|
85 |
tempStack->Add(hists[i]);
|
86 |
}
|
87 |
|
88 |
// Draw the stack of histograms
|
89 |
tempStack->Draw("nostack");
|
90 |
|
91 |
// Set title/label sizes and offsets
|
92 |
tempStack->GetXaxis()->SetTitleOffset(0.9);
|
93 |
tempStack->GetYaxis()->SetTitleOffset(1.2);
|
94 |
tempStack->GetXaxis()->SetLabelSize(0.04);
|
95 |
tempStack->GetYaxis()->SetLabelSize(0.05);
|
96 |
tempStack->GetXaxis()->SetTitleSize(0.06);
|
97 |
tempStack->GetYaxis()->SetTitleSize(0.05);
|
98 |
|
99 |
// Set axis titles
|
100 |
tempStack->SetTitle(title+";"+xTitle+";"+yTitle);
|
101 |
|
102 |
// Draw legend as long as the labels aren't empty
|
103 |
infoBox->SetShadowColor(0); // 0 = transparent
|
104 |
infoBox->SetLineColor(0);
|
105 |
infoBox->SetFillStyle(0);
|
106 |
if (histogramLabels[0]!="") {
|
107 |
infoBox->Draw();
|
108 |
}
|
109 |
|
110 |
c1->SetLogy(plotLogY);
|
111 |
|
112 |
return c1;
|
113 |
}
|
114 |
|
115 |
|
116 |
|
117 |
TCanvas* plotHists(TH1* hist1, TH1* hist2, TString title="", TString xTitle="", TString yTitle="", TString label1="", TString label2="", bool areaNormalize=false) {
|
118 |
vector<TH1*> histogramsToPlot;
|
119 |
histogramsToPlot.push_back( hist1 );
|
120 |
histogramsToPlot.push_back( hist2 );
|
121 |
vector<TString> histogramLabels;
|
122 |
histogramLabels.push_back( label1 );
|
123 |
histogramLabels.push_back( label2 );
|
124 |
TCanvas* c1 = plotHists(histogramsToPlot, histogramLabels, title, xTitle, yTitle, areaNormalize);
|
125 |
return c1;
|
126 |
}
|
127 |
|
128 |
TCanvas* plotHists(TH1* hist1, TH1* hist2, TH1* hist3, TString title="", TString xTitle="", TString yTitle="", TString label1="", TString label2="", TString label3="", bool areaNormalize=false) {
|
129 |
vector<TH1*> histogramsToPlot;
|
130 |
histogramsToPlot.push_back( hist1 );
|
131 |
histogramsToPlot.push_back( hist2 );
|
132 |
histogramsToPlot.push_back( hist3 );
|
133 |
vector<TString> histogramLabels;
|
134 |
histogramLabels.push_back( label1 );
|
135 |
histogramLabels.push_back( label2 );
|
136 |
histogramLabels.push_back( label3 );
|
137 |
TCanvas* c1 = plotHists(histogramsToPlot, histogramLabels, title, xTitle, yTitle, areaNormalize);
|
138 |
return c1;
|
139 |
}
|
140 |
|
141 |
TCanvas* plotHists(TH1* hist1, TH1* hist2, TH1* hist3, TH1* hist4, TString title="", TString xTitle="", TString yTitle="", TString label1="", TString label2="", TString label3="", TString label4="", bool areaNormalize=false) {
|
142 |
vector<TH1*> histogramsToPlot;
|
143 |
histogramsToPlot.push_back( hist1 );
|
144 |
histogramsToPlot.push_back( hist2 );
|
145 |
histogramsToPlot.push_back( hist3 );
|
146 |
histogramsToPlot.push_back( hist4 );
|
147 |
vector<TString> histogramLabels;
|
148 |
histogramLabels.push_back( label1 );
|
149 |
histogramLabels.push_back( label2 );
|
150 |
histogramLabels.push_back( label3 );
|
151 |
histogramLabels.push_back( label4 );
|
152 |
TCanvas* c1 = plotHists(histogramsToPlot, histogramLabels, title, xTitle, yTitle, areaNormalize);
|
153 |
return c1;
|
154 |
}
|