]>
Commit | Line | Data |
---|---|---|
7c11f313 | 1 | #if !defined(__CINT__) || defined(__MAKECINT__) |
2 | #include <TCanvas.h> | |
3 | #include <TString.h> | |
4 | #include <TLatex.h> | |
5 | #include <TMath.h> | |
6 | #include <TPad.h> | |
7 | #include <TList.h> | |
8 | #include <TH1.h> | |
9 | #include <TGraph.h> | |
10 | #include <TF1.h> | |
11 | #include <TStyle.h> | |
12 | #include <TFrame.h> | |
13 | #include <TPaveStats.h> | |
14 | #endif | |
15 | ||
16 | TH1* GetBaseHisto(TPad* pad=0); | |
17 | TFrame* GetFrame(TPad* pad=0); | |
18 | void SetStatPad(TH1* hst,float x1,float x2,float y1,float y2); | |
19 | TPaveStats* GetStatPad(TH1* hst); | |
20 | void SetHStyle(TH1* hst,int col=kRed,int mark=20,float mrsize=0.7); | |
21 | void SetGStyle(TGraph* hst,int col=kRed,int mark=20,float mrsize=0.7); | |
22 | TH1* Cumulate(TH1* histo, Bool_t doErr=kTRUE, const char* copyName=0); | |
23 | TLatex* AddLabel(const char*txt,float x=0.1,float y=0.9,int color=kBlack,float size=0.04); | |
24 | void wAv(double v1,double v2, double err1=0,double err2=0, double* wv=0,double *we=0); | |
25 | void wSum(double v1,double v2, double err1=0,double err2=0, double* wv=0,double *we=0); | |
26 | ||
27 | void SaveCanvas(TCanvas* canv,const char* path="canv",const Option_t *option="ecg") | |
28 | { | |
29 | TString name; | |
30 | TString opt = option; opt.ToLower(); | |
31 | // | |
32 | TString name0 = path; | |
33 | if (name0.IsNull()) name0 = "defCanv"; | |
34 | // | |
35 | TFrame* fr = GetFrame(); | |
36 | if (fr) fr->SetBorderMode(0); | |
37 | if (opt.Contains("c")) { | |
38 | name = name0; name+=".C"; | |
39 | canv->Print(name.Data()); | |
40 | } | |
41 | // | |
42 | if (opt.Contains("e")) { | |
43 | name = name0; name+=".eps"; | |
44 | canv->Print(name.Data()); | |
45 | } | |
46 | // | |
47 | if (opt.Contains("g")) { | |
48 | name = name0; name+=".gif"; | |
49 | canv->Print(name.Data()); | |
50 | } | |
51 | // | |
52 | if (opt.Contains("p")) { | |
53 | name = name0; name+=".ps"; | |
54 | canv->Print(name.Data()); | |
55 | } | |
56 | // | |
57 | } | |
58 | ||
59 | ||
60 | TLatex* AddLabel(const char*txt,float x,float y,int color,float size) | |
61 | { | |
62 | TLatex* lt = new TLatex(x,y,txt); | |
63 | lt->SetNDC(); | |
64 | lt->SetTextColor(color); | |
65 | lt->SetTextSize(size); | |
66 | lt->Draw(); | |
67 | return lt; | |
68 | } | |
69 | ||
70 | TH1* GetBaseHisto(TPad* pad) | |
71 | { | |
72 | if (!pad) pad = (TPad*)gPad; | |
73 | if (!pad) return 0; | |
74 | TList* lst = pad->GetListOfPrimitives(); | |
75 | int size = lst->GetSize(); | |
76 | TH1* hst=0; | |
77 | for (int i=0;i<size;i++) { | |
78 | TObject* obj = lst->At(i); | |
79 | if (!obj) continue; | |
80 | if (obj->InheritsFrom("TH1")) {hst = (TH1*)obj; break;} | |
81 | } | |
82 | return hst; | |
83 | } | |
84 | ||
85 | TFrame* GetFrame(TPad* pad) | |
86 | { | |
87 | if (!pad) pad = (TPad*)gPad; | |
88 | if (!pad) return 0; | |
89 | TList* lst = pad->GetListOfPrimitives(); | |
90 | int size = lst->GetSize(); | |
91 | TFrame* frm=0; | |
92 | for (int i=0;i<size;i++) { | |
93 | TObject* obj = lst->At(i); | |
94 | if (!obj) continue; | |
95 | if (obj->InheritsFrom("TFrame")) {frm = (TFrame*)obj; break;} | |
96 | } | |
97 | return frm; | |
98 | } | |
99 | ||
100 | TPaveStats* GetStatPad(TH1* hst) | |
101 | { | |
102 | TList *lst = hst->GetListOfFunctions(); | |
103 | if (!lst) return 0; | |
104 | int nf = lst->GetSize(); | |
105 | for (int i=0;i<nf;i++) { | |
106 | TPaveStats *fnc = (TPaveStats*) lst->At(i); | |
107 | if (fnc->InheritsFrom("TPaveStats")) return fnc; | |
108 | } | |
109 | return 0; | |
110 | // | |
111 | } | |
112 | ||
113 | ||
114 | void SetStatPad(TH1* hst,float x1,float x2,float y1,float y2) | |
115 | { | |
116 | TPaveStats* pad = GetStatPad(hst); | |
117 | if (!pad) return; | |
118 | pad->SetX1NDC( x1 ); | |
119 | pad->SetX2NDC( x2 ); | |
120 | pad->SetY1NDC( y1 ); | |
121 | pad->SetY2NDC( y2 ); | |
122 | // | |
123 | gPad->Modified(); | |
124 | } | |
125 | ||
126 | void SetHStyle(TH1* hst,int col,int mark,float mrsize) | |
127 | { | |
128 | hst->SetLineColor(col); | |
129 | hst->SetMarkerColor(col); | |
130 | hst->SetFillColor(col); | |
131 | hst->SetMarkerStyle(mark); | |
132 | hst->SetMarkerSize(mrsize); | |
133 | TList *lst = hst->GetListOfFunctions(); | |
134 | if (lst) { | |
135 | int nf = lst->GetSize(); | |
136 | for (int i=0;i<nf;i++) { | |
137 | TObject *fnc = lst->At(i); | |
138 | if (fnc->InheritsFrom("TF1")) { | |
139 | ((TF1*)fnc)->SetLineColor(col); | |
140 | ((TF1*)fnc)->SetLineWidth(1); | |
141 | ((TF1*)fnc)->ResetBit(TF1::kNotDraw); | |
142 | } | |
143 | else if (fnc->InheritsFrom("TPaveStats")) { | |
144 | ((TPaveStats*)fnc)->SetTextColor(col); | |
145 | } | |
146 | } | |
147 | } | |
148 | } | |
149 | ||
150 | void SetGStyle(TGraph* hst,int col,int mark,float mrsize) | |
151 | { | |
152 | hst->SetLineColor(col); | |
153 | hst->SetMarkerColor(col); | |
154 | hst->SetFillColor(col); | |
155 | hst->SetMarkerStyle(mark); | |
156 | hst->SetMarkerSize(mrsize); | |
157 | TList *lst = hst->GetListOfFunctions(); | |
158 | if (lst) { | |
159 | int nf = lst->GetSize(); | |
160 | for (int i=0;i<nf;i++) { | |
161 | TObject *fnc = lst->At(i); | |
162 | if (fnc->InheritsFrom("TF1")) { | |
163 | ((TF1*)fnc)->SetLineColor(col); | |
164 | ((TF1*)fnc)->SetLineWidth(1); | |
165 | ((TF1*)fnc)->ResetBit(TF1::kNotDraw); | |
166 | } | |
167 | else if (fnc->InheritsFrom("TPaveStats")) { | |
168 | ((TPaveStats*)fnc)->SetTextColor(col); | |
169 | } | |
170 | } | |
171 | } | |
172 | } | |
173 | ||
174 | TH1* Cumulate(TH1* histo, Bool_t doErr, const char* copyName) | |
175 | { | |
176 | // create cumulative histo | |
177 | TString nname = copyName; | |
178 | if (nname.IsNull()) { | |
179 | nname = histo->GetName(); | |
180 | nname += "_cml"; | |
181 | } | |
182 | TH1* cml = (TH1*) histo->Clone(nname.Data()); | |
183 | int nb = histo->GetNbinsX(); | |
184 | double sm = 0; | |
185 | double sme = 0; | |
186 | // | |
187 | for (int i=1;i<=nb;i++) { | |
188 | sm += histo->GetBinContent(i); | |
189 | cml->SetBinContent(i,sm); | |
190 | if (!doErr) continue; | |
191 | double ee = histo->GetBinError(i); | |
192 | sme += ee*ee; | |
193 | cml->SetBinError(i, sme>0 ? TMath::Sqrt(sme):0.); | |
194 | } | |
195 | return cml; | |
196 | } | |
197 | ||
198 | ||
199 | void wAv(double v1,double v2, double err1,double err2, double* wv,double *we) | |
200 | { | |
201 | // weighted average | |
202 | double sum=0,err=0; | |
203 | if (err1<=0 || err2<=0) { | |
204 | sum = v1+v2; | |
205 | } | |
206 | else { | |
207 | sum = v1/(err1*err1) + v2/(err2*err2); | |
208 | err = 1./(err1*err1) + 1./(err2*err2); | |
209 | sum /= err; | |
210 | err = 1./TMath::Sqrt(err); | |
211 | } | |
212 | printf("wAv %+e(%+e) | %+e(%+e) -> %+e +- %e\n",v1,err1,v2,err2,sum,err); | |
213 | if (wv) *wv = sum; | |
214 | if (we) *we = err; | |
215 | } | |
216 | ||
217 | ||
218 | void wSum(double v1,double v2, double err1,double err2, double* wv,double *we) | |
219 | { | |
220 | // sum with errors | |
221 | double sum=0,err=0; | |
222 | sum = v1+v2; | |
223 | if (err1>0 && err2>0) err = TMath::Sqrt(err1*err1 + err2*err2); | |
224 | printf("wSum %+e(%+e) + %+e(%+e) -> %+e +- %e\n",v1,err1,v2,err2,sum,err); | |
225 | if (wv) *wv = sum; | |
226 | if (we) *we = err; | |
227 | } |