ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/FGolf/Tools/makeProjectionPlots.C
Revision: 1.1
Committed: Fri Jul 8 09:17:38 2011 UTC (13 years, 10 months ago) by fgolf
Content type: text/plain
Branch: MAIN
CVS Tags: ss_summer2011approvalV2, ss_summer2011approval
Log Message:
first commit

File Contents

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