ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitPhysics/FakeMods/src/FakeRate.cc
Revision: 1.1
Committed: Tue Jun 30 10:47:17 2009 UTC (15 years, 10 months ago) by loizides
Content type: text/plain
Branch: MAIN
Log Message:
Added FakeMods.

File Contents

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