ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/FGolf/Tools/makeProjectionPlots.cc
Revision: 1.3
Committed: Sat Jul 14 22:51:11 2012 UTC (12 years, 10 months ago) by fgolf
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +110 -105 lines
Log Message:
update axis label

File Contents

# User Rev Content
1 fgolf 1.1 #include <iostream>
2    
3     #include "makeProjectionPlots.h"
4 fgolf 1.2 #include "eff.h"
5 fgolf 1.1
6     #include "TH1.h"
7     #include "TAxis.h"
8     #include "TArrayD.h"
9     #include "TCanvas.h"
10 fgolf 1.3 #include "TString.h"
11 fgolf 1.1
12 fgolf 1.2 #include <math.h>
13    
14     using namespace std;
15    
16 fgolf 1.1 TH1F* makeProjectionPlots (TH2F* hist, std::string axis) {
17    
18 fgolf 1.3 if (axis != "X" && axis != "Y") {
19     std::cout << "You've requested a projection onto a non-existent axis. Exiting." << std::endl;
20     exit (1);
21     }
22    
23     // need to add 2 to number of bins to account for underflow/overflow
24     int nxbins = hist->GetNbinsX();
25     int nybins = hist->GetNbinsY();
26     if (axis == "Y") {
27     nxbins = hist->GetNbinsY();
28     nybins = hist->GetNbinsX();
29     }
30    
31     TAxis* xaxis = hist->GetXaxis();
32     if (axis == "Y")
33     xaxis = hist->GetYaxis();
34    
35     const TArrayD* xbins = xaxis->GetXbins();
36     const int xsize = xbins->GetSize();
37     Double_t binsx[xsize];
38     for (int ibin = 0; ibin < xsize; ibin++)
39     binsx[ibin] = xbins->At(ibin);
40    
41     TH1F* xproj = new TH1F("proj", "proj", xsize-1, binsx);
42     xproj->GetXaxis()->ImportAttributes(xaxis);
43    
44     // for each bin in x, sum column in y
45     for (int ix = 1; ix < nxbins+1; ix++) {
46    
47     Double_t sum = 0.;
48     Double_t error = 0.;
49     for (int iy = 1; iy < nybins+1; iy++) {
50 fgolf 1.1
51 fgolf 1.3 int bin = hist->GetBin(ix, iy); // get absolute bin number from x,y bin count
52     if (axis == "Y")
53     bin = hist->GetBin(iy, ix);
54     sum += hist->GetBinContent(bin);
55     error += hist->GetBinError(bin) * hist->GetBinError(bin);
56     }
57     xproj->SetBinContent(ix, sum/nybins);
58     xproj->SetBinError(ix, sqrt(error));
59     }
60 fgolf 1.1
61 fgolf 1.3 return xproj;
62 fgolf 1.1 }
63    
64     TH1F* makeProjectionPlots (TH2F* numhist, TH2F* denhist, std::string axis) {
65    
66 fgolf 1.3 if (axis != "X" && axis != "Y") {
67     std::cout << "You've requested a projection onto a non-existent axis. Exiting." << std::endl;
68     exit (1);
69     }
70    
71     // need to add 2 to number of bins to account for underflow/overflow
72     int nxbins = numhist->GetNbinsX();
73     int nybins = numhist->GetNbinsY();
74     if (axis == "Y") {
75     nxbins = numhist->GetNbinsY();
76     nybins = numhist->GetNbinsX();
77     }
78    
79     TAxis* xaxis = numhist->GetXaxis();
80     if (axis == "Y")
81     xaxis = numhist->GetYaxis();
82    
83     const TArrayD* xbins = xaxis->GetXbins();
84     double* binsx = NULL;
85     int xsize = xbins->GetSize();
86     binsx = new double[xsize];
87     for (int ibin = 0; ibin < xsize; ibin++)
88     binsx[ibin] = xbins->At(ibin);
89    
90     TH1F* numproj = new TH1F("numproj", "numproj", xsize-1, binsx);
91     numproj->GetXaxis()->ImportAttributes(xaxis);
92    
93     TH1F* denproj = new TH1F("denproj", "denproj", xsize-1, binsx);
94     denproj->GetXaxis()->ImportAttributes(xaxis);
95    
96     // for each bin in x, sum column in y
97     for (int ix = 1; ix < nxbins+1; ix++) {
98    
99     Double_t numsum = 0.;
100     Double_t numerror = 0.;
101     Double_t densum = 0.;
102     Double_t denerror = 0.;
103     for (int iy = 1; iy < nybins+1; iy++) {
104 fgolf 1.1
105 fgolf 1.3 int bin = numhist->GetBin(ix, iy); // get absolute bin number from x,y bin count
106     if (axis == "Y")
107     bin = numhist->GetBin(iy, ix);
108     numsum += numhist->GetBinContent(bin);
109     numerror += numhist->GetBinError(bin) * numhist->GetBinError(bin);
110     densum += denhist->GetBinContent(bin);
111     denerror += denhist->GetBinError(bin) * denhist->GetBinError(bin);
112     }
113     numproj->SetBinContent(ix, numsum);
114     numproj->SetBinError(ix, sqrt(numerror));
115     denproj->SetBinContent(ix, densum);
116     denproj->SetBinError(ix, sqrt(denerror));
117     }
118    
119     TString histname = numhist->GetName();
120     TString axisname = axis;
121     axisname.ToLower();
122     histname += Form("_%s%s", axisname.Data(), "proj");
123     TH1F* proj = eff(denproj, numproj, histname.Data());
124     proj->GetXaxis()->ImportAttributes(xaxis);
125 fgolf 1.1
126 fgolf 1.3 delete numproj;
127     delete denproj;
128 fgolf 1.1
129 fgolf 1.3 return proj;
130 fgolf 1.1 }
131    
132     void compareRFhists (TH2F* old_hist, TH2F* new_hist) {
133    
134 fgolf 1.3 TH2F* diff_hist = (TH2F*)new_hist->Clone();
135     diff_hist->Add(old_hist, -1.);
136 fgolf 1.1
137 fgolf 1.3 TCanvas* cx = new TCanvas();
138     cx->cd();
139     diff_hist->Draw("colz");
140 fgolf 1.1 }
141    
142     void clearHists (TH2F* hist) {
143    
144 fgolf 1.3 delete hist;
145 fgolf 1.1 }