ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/FGolf/Tools/eff2.C
Revision: 1.1
Committed: Mon Nov 8 03:15:09 2010 UTC (14 years, 5 months ago) by fgolf
Content type: text/plain
Branch: MAIN
Log Message:
basically just divides two histograms

File Contents

# User Rev Content
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     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     TH2F* eff2(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     TH2F* h1 = 0;
52     TH2F* 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 = (TH2F*) obj;
58     if (objName == str2) h2 = (TH2F*) 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     TH2F* temp = eff2(h1, h2, name);
73     return temp;
74     }