ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitPhysics/FakeMods/src/FakeRate.cc
Revision: 1.2
Committed: Mon Jul 13 11:27:13 2009 UTC (15 years, 9 months ago) by loizides
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_010
Changes since 1.1: +2 -1 lines
Log Message:
Fix compiler warnings... Still this code needs further cleanup.

File Contents

# User Rev Content
1 loizides 1.2 // $Id: FakeRate.cc,v 1.1 2009/06/30 10:47:17 loizides Exp $
2 loizides 1.1
3     #include "MitPhysics/FakeMods/interface/FakeRate.h"
4 loizides 1.2 #include <TMath.h>
5 loizides 1.1 #include <TFile.h>
6     #include <TH1.h>
7     #include <TH2.h>
8     #include <TH3.h>
9     #include <TF1.h>
10     #include <TF2.h>
11    
12     using namespace mithep;
13     using namespace std;
14    
15     ClassImp(mithep::FakeRate)
16    
17     //--------------------------------------------------------------------------------------------------
18     Bool_t FakeRate::Init()
19     {
20     //Load all histogram files.
21    
22     //Get the root file which is storing the fake rates
23     TFile *ElectronFRFile = new TFile(fElectronFRFilename);
24     if (!ElectronFRFile) {
25     cerr << "The Electron FakeRate file : " << fElectronFRFilename << " could not be opened!\n";
26     return false;
27     }
28     TFile *MuonFRFile = new TFile(fMuonFRFilename);
29     if (!MuonFRFile) {
30     cerr << "The Muon FakeRate file : " << fMuonFRFilename << " could not be opened!\n";
31     return false;
32     }
33    
34     // *****************************************************************************************
35     // Load Histogram / Fit Functions from the file
36     // *****************************************************************************************
37     if (fUse2DFakeRate) {
38    
39     fElectronFakeRateHist_PtEta = (TH2F*)(ElectronFRFile->Get(fElectronFRHistName));
40     fMuonFakeRateHist_PtEta = (TH2F*)(MuonFRFile->Get(fMuonFRHistName));
41     if (!fElectronFakeRateHist_PtEta) {
42     cout << "Error: Histogram " << fElectronFRHistName << " cannot be loaded from file "
43     << fElectronFRFilename << endl;
44     }
45     if (!fMuonFakeRateHist_PtEta) {
46     cout << "Error: Histogram " << fMuonFRHistName << " cannot be loaded. from file"
47     << fMuonFRFilename << endl;
48     }
49    
50     fElectronFakeRateHist_PtEta->SetDirectory(0);
51     fMuonFakeRateHist_PtEta->SetDirectory(0);
52    
53     if (fUseFitFunction) {
54     //Currently unsupported
55     cerr << "Error: Using 2D Fake Rates with Fit Function is not currently supported.\n";
56     return false;
57    
58     //Once supported, below code will load the functions.
59     TF2 *ElectronFakeRateFit_PtEta_temp = (TF2*)(ElectronFRFile->Get(fElectronFRFunctionName));
60     TF2 *MuonFakeRateFit_PtEta_temp = (TF2*)(MuonFRFile->Get(fMuonFRFunctionName));
61     if (!ElectronFakeRateFit_PtEta_temp) {
62     cout << "Error: Function " << fElectronFRFunctionName << " cannot be loaded from file "
63     << fElectronFRFilename << endl;
64     }
65     if (!MuonFakeRateFit_PtEta_temp) {
66     cout << "Error: Function " << fMuonFRFunctionName << " cannot be loaded. from file"
67     << fMuonFRFilename << endl;
68     }
69     fElectronFakeRateFit_PtEta = (TF2*)(ElectronFakeRateFit_PtEta_temp->Clone());
70     fMuonFakeRateFit_PtEta = (TF2*)(MuonFakeRateFit_PtEta_temp->Clone());
71     }
72    
73     } else {
74    
75     fElectronFakeRateHist_Pt = (TH1F*)(ElectronFRFile->Get(fElectronFRHistName));
76     fMuonFakeRateHist_Pt = (TH1F*)(MuonFRFile->Get(fMuonFRHistName));
77     if (!fElectronFakeRateHist_Pt) {
78     cout << "Error: Histogram " << fElectronFRHistName << " cannot be loaded from file "
79     << fElectronFRFilename << endl;
80     }
81     if (!fMuonFakeRateHist_Pt) {
82     cout << "Error: Histogram " << fMuonFRHistName << " cannot be loaded. from file"
83     << fMuonFRFilename << endl;
84     }
85     fElectronFakeRateHist_Pt->SetDirectory(0);
86     fMuonFakeRateHist_Pt->SetDirectory(0);
87    
88     if (fUseFitFunction) {
89     TF1 *ElectronFakeRateFit_Pt_temp = (TF1*)(ElectronFRFile->Get(fElectronFRFunctionName));
90     TF1 *MuonFakeRateFit_Pt_temp = (TF1*)(MuonFRFile->Get(fMuonFRFunctionName));
91     if (!ElectronFakeRateFit_Pt_temp) {
92     cout << "Error: Function " << fElectronFRFunctionName << " cannot be loaded from file "
93     << fElectronFRFilename << endl;
94     }
95     if (!MuonFakeRateFit_Pt_temp) {
96     cout << "Error: Function " << fMuonFRFunctionName << " cannot be loaded. from file"
97     << fMuonFRFilename << endl;
98     }
99     fElectronFakeRateFit_Pt = (TF1*)(ElectronFakeRateFit_Pt_temp->Clone());
100     fMuonFakeRateFit_Pt = (TF1*)(MuonFakeRateFit_Pt_temp->Clone());
101     }
102     }
103    
104    
105     fElectronFakeRateHist_PtEta_sysError = (TH2F*)(ElectronFRFile->Get("RecoElectronFakeRate_PtEta_Madgraph_SysErrors"));
106     fMuonFakeRateHist_PtEta_sysError = (TH2F*)(MuonFRFile->Get("TrackerMuonFakeRate_PtEta_Madgraph_SysErrors"));
107     if (!fElectronFakeRateHist_PtEta_sysError) {
108     cout << "Error: Histogram RecoElectronFakeRate_PtEta_Madgraph_SysErrors cannot be loaded from file "
109     << endl;
110     }
111     if (!fMuonFakeRateHist_PtEta_sysError) {
112     cout << "Error: Histogram TrackerMuonFakeRate_PtEta_Madgraph_SysErrors cannot be loaded. from file"
113     << endl;
114     }
115     fElectronFakeRateHist_PtEta_sysError->SetDirectory(0);
116     fMuonFakeRateHist_PtEta_sysError->SetDirectory(0);
117    
118    
119     ElectronFRFile->Close();
120     MuonFRFile->Close();
121     delete ElectronFRFile;
122     delete MuonFRFile;
123    
124     return true;
125     }
126    
127    
128     //--------------------------------------------------------------------------------------------------
129     Double_t FakeRate::ElectronFakeRate(Double_t pt, Double_t eta, Double_t phi)
130     {
131     // Calculate the electron fake rate given pt, eta, and phi
132     Double_t prob = 0.0;
133    
134     if (fIsInit) {
135     if (fUse2DFakeRate) {
136     if (fUseFitFunction) {
137     cerr << "Error: Using 2D Fake Rates with Fit Function is not currently supported.\n";
138     } else {
139     if (fElectronFakeRateHist_PtEta) {
140     Int_t ptbin = fElectronFakeRateHist_PtEta->GetXaxis()->FindFixBin(pt);
141     Int_t etabin = fElectronFakeRateHist_PtEta->GetYaxis()->FindFixBin(eta);
142     prob = fElectronFakeRateHist_PtEta->GetBinContent(ptbin,etabin);
143     } else {
144     cerr << "Error: fElectronFakeRateHist_PtEta was not loaded properly.\n";
145     assert(false);
146     }
147     }
148     } else {
149     if (fUseFitFunction) {
150     if (fElectronFakeRateFit_Pt) {
151     prob = fElectronFakeRateFit_Pt->Eval(pt);
152     } else {
153     cerr << "Error: fElectronFakeRateFit_Pt was not loaded properly.\n";
154     assert(false);
155     }
156     } else {
157     if (fElectronFakeRateHist_Pt) {
158     Int_t ptbin = fElectronFakeRateHist_Pt->GetXaxis()->FindFixBin(pt);
159     prob = fElectronFakeRateHist_Pt->GetBinContent(ptbin);
160     } else {
161     cerr << "Error: fElectronFakeRateHist_Pt was not loaded properly.\n";
162     assert(false);
163     }
164     }
165     }
166     } else {
167     cerr << "Error: FakeRate was not properly initialized. \n";
168     assert(false);
169     }
170     return prob;
171     }
172    
173     //--------------------------------------------------------------------------------------------------
174     Double_t FakeRate::ElectronFakeRateError(Double_t pt, Double_t eta, Double_t phi)
175     {
176     // Calculate the electron fake rate given pt, eta, and phi
177     Double_t error = 0.0;
178    
179     if (fIsInit) {
180     if (fUse2DFakeRate) {
181     if (fUseFitFunction) {
182     cerr << "Error: Using 2D Fake Rates with Fit Function is not currently supported.\n";
183     } else {
184     if (fElectronFakeRateHist_PtEta_sysError) {
185     Int_t ptbin = fElectronFakeRateHist_PtEta_sysError->GetXaxis()->FindFixBin(pt);
186     Int_t etabin = fElectronFakeRateHist_PtEta_sysError->GetYaxis()->FindFixBin(eta);
187     error = fElectronFakeRateHist_PtEta_sysError->GetBinContent(ptbin,etabin);
188    
189     if (isnan(error)) {
190     cerr << "Error: ElectronFakeRateError(" << ptbin << "," << etabin << ") = NAN.\n";
191     error = 0;
192     }
193     } else {
194     cerr << "Error: fElectronFakeRateHist_PtEta_sysError was not loaded properly.\n";
195     assert(false);
196     }
197     }
198     }
199     } else {
200     cerr << "Error: FakeRate was not properly initialized. \n";
201     assert(false);
202     }
203     return error;
204     }
205    
206    
207     //--------------------------------------------------------------------------------------------------
208     Double_t FakeRate::MuonFakeRate(Double_t pt, Double_t eta, Double_t phi)
209     {
210     // Calculate the muon fake rate given pt, eta, and phi
211     Double_t prob = 0.0;
212    
213     if (fIsInit) {
214     if (fUse2DFakeRate) {
215     if (fUseFitFunction) {
216     cerr << "Error: Using 2D Fake Rates with Fit Function is not currently supported.\n";
217     } else {
218     if (fMuonFakeRateHist_PtEta) {
219     Int_t ptbin = fMuonFakeRateHist_PtEta->GetXaxis()->FindFixBin(pt);
220     Int_t etabin = fMuonFakeRateHist_PtEta->GetYaxis()->FindFixBin(eta);
221     prob = fMuonFakeRateHist_PtEta->GetBinContent(ptbin,etabin);
222     } else {
223     cerr << "Error: fMuonFakeRateHist_PtEta was not loaded properly.\n";
224     assert(false);
225     }
226     }
227     } else {
228     if (fUseFitFunction) {
229     if (fMuonFakeRateFit_Pt) {
230     prob = fMuonFakeRateFit_Pt->Eval(pt);
231     } else {
232     cerr << "Error: fMuonFakeRateFit_Pt was not loaded properly.\n";
233     assert(false);
234     }
235     } else {
236     if (fMuonFakeRateHist_Pt) {
237     Int_t ptbin = fMuonFakeRateHist_Pt->GetXaxis()->FindFixBin(pt);
238     prob = fMuonFakeRateHist_Pt->GetBinContent(ptbin);
239     } else {
240     cerr << "Error: fMuonFakeRateHist_Pt was not loaded properly.\n";
241     assert(false);
242     }
243     }
244     }
245     } else {
246     cerr << "Error: FakeRate was not properly initialized. \n";
247     assert(false);
248     }
249     return prob;
250     }
251    
252     //--------------------------------------------------------------------------------------------------
253     Double_t FakeRate::MuonFakeRateError(Double_t pt, Double_t eta, Double_t phi)
254     {
255     // Calculate the muon fake rate given pt, eta, and phi
256     Double_t error = 0.0;
257    
258     if (fIsInit) {
259     if (fUse2DFakeRate) {
260     if (fUseFitFunction) {
261     cerr << "Error: Using 2D Fake Rates with Fit Function is not currently supported.\n";
262     } else {
263     if (fMuonFakeRateHist_PtEta_sysError) {
264     Int_t ptbin = fMuonFakeRateHist_PtEta_sysError->GetXaxis()->FindFixBin(pt);
265     Int_t etabin = fMuonFakeRateHist_PtEta_sysError->GetYaxis()->FindFixBin(eta);
266     error = fMuonFakeRateHist_PtEta_sysError->GetBinContent(ptbin,etabin);
267    
268     if (isnan(error)) {
269     cerr << "Error: ElectronFakeRateError(" << ptbin << "," << etabin << ") = NAN.\n";
270     error = 0;
271     }
272     } else {
273     cerr << "Error: fMuonFakeRateHist_PtEta_sysError was not loaded properly.\n";
274     assert(false);
275     }
276     }
277     }
278     } else {
279     cerr << "Error: FakeRate was not properly initialized. \n";
280     assert(false);
281     }
282     return error;
283     }