ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/FGolf/Tools/eff.C
Revision: 1.1
Committed: Fri Mar 18 20:52:57 2011 UTC (14 years, 2 months ago) by fgolf
Content type: text/plain
Branch: MAIN
CVS Tags: synchMay2011v1, ss20May2011
Log Message:
calculate efficiencies

File Contents

# Content
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 "eff.h"
15
16 // Method by pointer
17 TH1F* eff(TH1F* h1, TH1F* 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 TH1F* temp = (TH1F*) h1->Clone(name);
30 temp->SetTitle(name);
31 temp->Reset();
32 temp->Sumw2();
33
34 // Do the calculation
35 temp->Divide(h2, h1, 1., 1., "B");
36
37 // Done
38 return temp;
39 }
40
41
42 // Method by name
43 TH1F* eff(const char* name1, const char* name2, const char* name) {
44
45 // Get a list of object and their iterator
46 TList* list = gDirectory->GetList() ;
47 TIterator* iter = list->MakeIterator();
48
49 // Loop over objects, set the pointers
50 TObject* obj;
51 TH1F* h1 = 0;
52 TH1F* h2 = 0;
53 TString str1 = Form("%s", name1);
54 TString str2 = Form("%s", name2);
55 while (obj = iter->Next()) {
56 TString objName = obj->GetName();
57 if (objName == str1) h1 = (TH1F*) obj;
58 if (objName == str2) h2 = (TH1F*) obj;
59 }
60
61 // quit if not found
62 if (h1 == 0) {
63 std::cout << "Histogram " << name1 << " not found" << std::endl;
64 return 0;
65 }
66 if (h2 == 0) {
67 std::cout << "Histogram " << name2 << " not found" << std::endl;
68 return 0;
69 }
70
71 // Call the method by pointer
72 TH1F* temp = eff(h1, h2, name);
73 return temp;
74 }