ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/joshmt/MiscUtil.cxx
Revision: 1.9
Committed: Fri Sep 9 08:51:11 2011 UTC (13 years, 7 months ago) by joshmt
Content type: text/plain
Branch: MAIN
Changes since 1.8: +37 -0 lines
Log Message:
some new stuff

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 joshmt 1.5 #include "TH1D.h"
8 joshmt 1.1 #include "TString.h"
9 joshmt 1.3 #include <iostream>
10 joshmt 1.9 #include "TMath.h"
11    
12     #include <cassert>
13 joshmt 1.1
14     namespace jmt {
15    
16     //======misc utilities======
17     //gets rid of = > < from cuts in order to be better included in file names
18     TString fortranize(TString cut) {
19    
20 joshmt 1.8 cut.ReplaceAll(" ",""); //remove all spaces
21    
22 joshmt 1.1 cut.ReplaceAll("==","eq");
23     cut.ReplaceAll(">=","gte");
24     cut.ReplaceAll("<=","lte");
25    
26     cut.ReplaceAll(">","gt");
27     cut.ReplaceAll("<","lt");
28 joshmt 1.4
29 joshmt 1.8 cut.ReplaceAll("&&","and");
30     cut.ReplaceAll("||","or");
31    
32 joshmt 1.4 cut.ReplaceAll("/","Over");
33     cut.ReplaceAll("+","Plus");
34     cut.ReplaceAll("*","Times");
35    
36     //this is pretty ugly
37     cut.ReplaceAll("(","L");
38     cut.ReplaceAll(")","R");
39    
40 joshmt 1.1 return cut;
41     }
42    
43 joshmt 1.6 //utility function for making output more readable
44     TString format_nevents(double n,double e, const bool moreDigits=false) {
45     const TString latexMode_=true; //hard code for now
46     const TString pm = latexMode_ ? "\\pm ": " +/- ";
47    
48     const int eCutoff = moreDigits ? 10 : 1;
49     const int extraDigits = moreDigits ? 1:0;
50    
51     TString mathmode = latexMode_ ? "$" : "";
52    
53     char out[100];
54     if (e >= eCutoff || e < 0.00001) { //show whole numbers only
55     sprintf(out,"%s%.0f%s%.0f%s",mathmode.Data(),n,pm.Data(),e,mathmode.Data());
56     }
57     else {
58     int nfig = ceil(fabs(log10(e))) + extraDigits;
59     TString form="%s%.";
60     form+=nfig; form+="f%s%.";
61     form+=nfig; form+="f%s";
62     sprintf(out,form.Data(),mathmode.Data(),n,pm.Data(),e,mathmode.Data());
63     }
64     return TString(out);
65     }
66    
67    
68 joshmt 1.7 Double_t weightedMean(Int_t n, Double_t *a, Double_t *e, const bool returnError=false) {
69 joshmt 1.9 using namespace std;
70 joshmt 1.3
71     Double_t numerator=0;
72     Double_t denominator=0;
73    
74     for (Int_t i=0; i<n; ++i) {
75     Double_t esq = e[i]*e[i];
76 joshmt 1.7 if (esq == 0) {cout<<"Error is zero!"<<endl; return -1e9;}
77 joshmt 1.3 numerator += a[i] / esq;
78     denominator += 1.0 / esq;
79     }
80    
81     cout<<"mean = "<<numerator / denominator<<" +/- "<<sqrt(1/denominator)<<endl;
82 joshmt 1.7 return returnError ? sqrt(1/denominator) : numerator / denominator;
83 joshmt 1.3 }
84    
85     //implemented in TMath in later versions of root, so i'm using the TMath name
86     bool AreEqualAbs( const double & a, const double & b, const double epsilon=0.001) {
87    
88     return ( fabs(a-b) < epsilon ) ;
89    
90     }
91    
92 joshmt 1.9 //deal with the annoyance of logical booleans that are stored in an ntuple as double
93     bool doubleToBool( double a) {
94     using namespace std;
95    
96     int i = TMath::Nint(a);
97     if (i==0) return false;
98     else if (i==1) return true;
99    
100     cout<<"[doubleToBool] Something weird going on! "<<a<<"\t"<<i<<endl;
101     if (i>1) return true;
102     return false;
103     }
104    
105     //delta phi calculations are pretty commonplace, so put it here
106     double deltaPhi(double phi1, double phi2) {
107     double result = phi1 - phi2;
108     while (result > TMath::Pi()) result -= 2*TMath::Pi();
109     while (result <= -TMath::Pi()) result += 2*TMath::Pi();
110     return fabs(result);
111     }
112    
113     double deltaR2(double eta1, double phi1, double eta2, double phi2) {
114     double deta = eta1 - eta2;
115     double dphi = deltaPhi(phi1, phi2);
116     return deta*deta + dphi*dphi;
117     }
118    
119     double deltaR(double eta1, double phi1, double eta2, double phi2) {
120     return TMath::Sqrt(deltaR2 (eta1, phi1, eta2, phi2));
121     }
122    
123 joshmt 1.3 // == error propagation ==
124    
125     //because not all versions of ROOT have it built in
126     double errOnIntegral(const TH1D* h, int binlow=1, int binhigh=-1) {
127    
128     if (binhigh == -1) binhigh = h->GetNbinsX();
129    
130     double err=0;
131     for (int i = binlow; i<= binhigh; i++) {
132     err += h->GetBinError(i) * h->GetBinError(i);
133     }
134    
135     if (err<0) return err;
136     return sqrt(err);
137     }
138    
139     //return error on a/b
140     double errAoverB(double a, double aerr, double b, double berr) {
141 joshmt 1.9 using namespace std;
142 joshmt 1.3 if (b==0 || a==0) {
143     cout<<"Warning in errAoverB -- a or b is zero!"<<endl;
144     return -1;
145     }
146     return (a/b)*sqrt( pow( aerr/a, 2) + pow( berr/b, 2) );
147     }
148    
149     //return error on a*b
150     double errAtimesB(double a, double aerr, double b, double berr) {
151 joshmt 1.9 using namespace std;
152 joshmt 1.3 if (b==0 || a==0) {
153     cout<<"Warning in errAtimesB -- a or b is zero!"<<endl;
154     return -1;
155     }
156    
157     return a*b*sqrt( pow( aerr/a, 2) + pow( berr/b, 2) );
158     }
159    
160 joshmt 1.2 //======== container for run, lumisection, event number =========
161     class eventID {
162     public:
163     eventID();
164     eventID(ULong64_t RunNumber, ULong64_t LumiSection, ULong64_t EventNumber);
165     ~eventID();
166    
167     bool operator< (const eventID & id) const;
168     bool operator== (const eventID & id) const;
169     bool operator!= (const eventID & id) const;
170    
171     ULong64_t run;
172     ULong64_t ls;
173     ULong64_t ev;
174    
175     };
176    
177     eventID::eventID() : run(0),ls(0),ev(0) {}
178     eventID::eventID(ULong64_t RunNumber, ULong64_t LumiSection, ULong64_t EventNumber) :
179     run(RunNumber),ls(LumiSection),ev(EventNumber) {}
180     eventID::~eventID() {}
181    
182     bool eventID::operator== (const eventID & id) const {
183    
184     if (ev == id.ev &&
185     ls == id.ls &&
186     run == id.run) return true;
187    
188     return false;
189     }
190    
191     bool eventID::operator!= (const eventID & id) const {
192     return !(*this == id);
193     }
194    
195     bool eventID::operator< (const eventID & id) const {
196    
197     if (run < id.run) return true;
198     else if (run > id.run) return false;
199     else { //if run is equal
200    
201     //now compare lumi section
202     if ( ls < id.ls ) return true;
203     else if (ls > id.ls) return false;
204     else { //if ls is equal
205    
206     if ( ev < id.ev ) return true;
207     else if (ev > id.ev) return false;
208    
209     else { //these are the same event!
210     return false;
211     }
212     }
213     }
214    
215     //this shouldn't happen
216     assert(0);
217     return false;
218     }
219    
220     } //end of namespace
221    
222