1 |
fgolf |
1.1 |
// Input: 2 histogram
|
2 |
|
|
// Output: one histogram which is the efficiency:
|
3 |
|
|
// h1 : TOTAL NUMBER OF EVENTS
|
4 |
|
|
// h2 : NUMBER OF EVENTS THAT PASS
|
5 |
|
|
|
6 |
|
|
#include "TList.h"
|
7 |
|
|
#include "TIterator.h"
|
8 |
|
|
#include "TObject.h"
|
9 |
|
|
#include "TString.h"
|
10 |
|
|
#include "TDirectory.h"
|
11 |
|
|
|
12 |
|
|
#include <iostream>
|
13 |
|
|
|
14 |
|
|
#include "eff2.h"
|
15 |
|
|
|
16 |
|
|
// Method by pointer
|
17 |
|
|
TH2F* eff2(TH2F* h1, TH2F* h2, const char* name) {
|
18 |
|
|
|
19 |
|
|
// first, verify that all histograms have same binning
|
20 |
|
|
// nx is the number of visible bins
|
21 |
|
|
// nxtot = nx+2 includes underflow and overflow
|
22 |
|
|
Int_t nx = h1->GetNbinsX();
|
23 |
|
|
if (h2->GetNbinsX() != nx) {
|
24 |
|
|
std::cout << "Histograms must have same number of bins" << std::endl;
|
25 |
|
|
return 0;
|
26 |
|
|
}
|
27 |
|
|
|
28 |
|
|
// get the new histogram
|
29 |
|
|
TH2F* temp = (TH2F*) h1->Clone(name);
|
30 |
|
|
temp->SetTitle(name);
|
31 |
|
|
temp->Reset();
|
32 |
|
|
if (!temp->GetSumw2N())
|
33 |
|
|
temp->Sumw2();
|
34 |
|
|
|
35 |
|
|
// Do the calculation
|
36 |
|
|
temp->Divide(h2, h1, 1., 1., "B");
|
37 |
|
|
|
38 |
|
|
// Done
|
39 |
|
|
return temp;
|
40 |
|
|
}
|
41 |
|
|
|
42 |
|
|
|
43 |
|
|
// Method by name
|
44 |
|
|
TH2F* eff2(const char* name1, const char* name2, const char* name) {
|
45 |
|
|
|
46 |
|
|
// Get a list of object and their iterator
|
47 |
|
|
TList* list = gDirectory->GetList() ;
|
48 |
|
|
TIterator* iter = list->MakeIterator();
|
49 |
|
|
|
50 |
|
|
// Loop over objects, set the pointers
|
51 |
|
|
TObject* obj;
|
52 |
|
|
TH2F* h1 = 0;
|
53 |
|
|
TH2F* h2 = 0;
|
54 |
|
|
TString str1 = Form("%s", name1);
|
55 |
|
|
TString str2 = Form("%s", name2);
|
56 |
|
|
while ((obj = iter->Next())) {
|
57 |
|
|
TString objName = obj->GetName();
|
58 |
|
|
if (objName == str1) h1 = (TH2F*) obj;
|
59 |
|
|
if (objName == str2) h2 = (TH2F*) obj;
|
60 |
|
|
}
|
61 |
|
|
|
62 |
|
|
// quit if not found
|
63 |
|
|
if (h1 == 0) {
|
64 |
|
|
std::cout << "Histogram " << name1 << " not found" << std::endl;
|
65 |
|
|
return 0;
|
66 |
|
|
}
|
67 |
|
|
if (h2 == 0) {
|
68 |
|
|
std::cout << "Histogram " << name2 << " not found" << std::endl;
|
69 |
|
|
return 0;
|
70 |
|
|
}
|
71 |
|
|
|
72 |
|
|
// Call the method by pointer
|
73 |
|
|
TH2F* temp = eff2(h1, h2, name);
|
74 |
|
|
return temp;
|
75 |
|
|
}
|