ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/joshmt/MiscUtil.cxx
Revision: 1.3
Committed: Wed May 4 13:14:08 2011 UTC (14 years ago) by joshmt
Content type: text/plain
Branch: MAIN
Changes since 1.2: +59 -0 lines
Log Message:
stuff for errors, etc

File Contents

# User Rev Content
1 joshmt 1.1 /*
2     a place for very simple functions that don't depend on anything else
3    
4     can be easily #included into other code
5     */
6    
7     #include "TString.h"
8 joshmt 1.3 #include <iostream>
9 joshmt 1.1
10     namespace jmt {
11    
12     //======misc utilities======
13     //gets rid of = > < from cuts in order to be better included in file names
14     TString fortranize(TString cut) {
15    
16     cut.ReplaceAll("==","eq");
17     cut.ReplaceAll(">=","gte");
18     cut.ReplaceAll("<=","lte");
19    
20     cut.ReplaceAll(">","gt");
21     cut.ReplaceAll("<","lt");
22    
23     return cut;
24     }
25    
26 joshmt 1.3 void weightedMean(Int_t n, Double_t *a, Double_t *e) {
27    
28     Double_t numerator=0;
29     Double_t denominator=0;
30    
31     for (Int_t i=0; i<n; ++i) {
32     Double_t esq = e[i]*e[i];
33     if (esq == 0) {cout<<"Error is zero!"<<endl; return;}
34     numerator += a[i] / esq;
35     denominator += 1.0 / esq;
36     }
37    
38     cout<<"mean = "<<numerator / denominator<<" +/- "<<sqrt(1/denominator)<<endl;
39    
40     }
41    
42     //implemented in TMath in later versions of root, so i'm using the TMath name
43     bool AreEqualAbs( const double & a, const double & b, const double epsilon=0.001) {
44    
45     return ( fabs(a-b) < epsilon ) ;
46    
47     }
48    
49     // == error propagation ==
50    
51     //because not all versions of ROOT have it built in
52     double errOnIntegral(const TH1D* h, int binlow=1, int binhigh=-1) {
53    
54     if (binhigh == -1) binhigh = h->GetNbinsX();
55    
56     double err=0;
57     for (int i = binlow; i<= binhigh; i++) {
58     err += h->GetBinError(i) * h->GetBinError(i);
59     }
60    
61     if (err<0) return err;
62     return sqrt(err);
63     }
64    
65     //return error on a/b
66     double errAoverB(double a, double aerr, double b, double berr) {
67     if (b==0 || a==0) {
68     cout<<"Warning in errAoverB -- a or b is zero!"<<endl;
69     return -1;
70     }
71     return (a/b)*sqrt( pow( aerr/a, 2) + pow( berr/b, 2) );
72     }
73    
74     //return error on a*b
75     double errAtimesB(double a, double aerr, double b, double berr) {
76     if (b==0 || a==0) {
77     cout<<"Warning in errAtimesB -- a or b is zero!"<<endl;
78     return -1;
79     }
80    
81     return a*b*sqrt( pow( aerr/a, 2) + pow( berr/b, 2) );
82     }
83    
84 joshmt 1.2 //======== container for run, lumisection, event number =========
85     class eventID {
86     public:
87     eventID();
88     eventID(ULong64_t RunNumber, ULong64_t LumiSection, ULong64_t EventNumber);
89     ~eventID();
90    
91     bool operator< (const eventID & id) const;
92     bool operator== (const eventID & id) const;
93     bool operator!= (const eventID & id) const;
94    
95     ULong64_t run;
96     ULong64_t ls;
97     ULong64_t ev;
98    
99     };
100    
101     eventID::eventID() : run(0),ls(0),ev(0) {}
102     eventID::eventID(ULong64_t RunNumber, ULong64_t LumiSection, ULong64_t EventNumber) :
103     run(RunNumber),ls(LumiSection),ev(EventNumber) {}
104     eventID::~eventID() {}
105    
106     bool eventID::operator== (const eventID & id) const {
107    
108     if (ev == id.ev &&
109     ls == id.ls &&
110     run == id.run) return true;
111    
112     return false;
113     }
114    
115     bool eventID::operator!= (const eventID & id) const {
116     return !(*this == id);
117     }
118    
119     bool eventID::operator< (const eventID & id) const {
120    
121     if (run < id.run) return true;
122     else if (run > id.run) return false;
123     else { //if run is equal
124    
125     //now compare lumi section
126     if ( ls < id.ls ) return true;
127     else if (ls > id.ls) return false;
128     else { //if ls is equal
129    
130     if ( ev < id.ev ) return true;
131     else if (ev > id.ev) return false;
132    
133     else { //these are the same event!
134     return false;
135     }
136     }
137     }
138    
139     //this shouldn't happen
140     assert(0);
141     return false;
142     }
143    
144     } //end of namespace
145    
146