ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/FGolf/Tools/makeProjectionPlots.cc
Revision: 1.2
Committed: Mon May 28 22:02:41 2012 UTC (12 years, 11 months ago) by fgolf
Content type: text/plain
Branch: MAIN
Changes since 1.1: +5 -1 lines
Log Message:
fix some includes

File Contents

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