AliPhysics  d37ed96 (d37ed96)
SaveCanvas.C
Go to the documentation of this file.
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 <TH2.h>
10 #include <TGraph.h>
11 #include <TF1.h>
12 #include <TStyle.h>
13 #include <TFrame.h>
14 #include <TPaveStats.h>
15 #endif
16 
17 TH1* GetBaseHisto(TPad* pad=0);
18 TFrame* GetFrame(TPad* pad=0);
19 TPaveStats* SetStatPad(TH1* hst,float x1,float x2,float y1,float y2,Int_t stl=-1,Int_t col=-1);
20 TPaveStats* GetStatPad(TH1* hst);
21 void SetHStyle(TH1* hst,int col=kRed,int mark=20,float mrsize=0.7);
22 void SetGStyle(TGraph* hst,int col=kRed,int mark=20,float mrsize=0.7);
23 TH1* Cumulate(TH1* histo, Bool_t doErr=kTRUE, const char* copyName=0);
24 TLatex* AddLabel(const char*txt,float x=0.1,float y=0.9,int color=kBlack,float size=0.04);
25 void wAv(double v1,double v2, double err1=0,double err2=0, double* wv=0,double *we=0);
26 void wSum(double v1,double v2, double err1=0,double err2=0, double* wv=0,double *we=0);
27 
28 void SaveCanvas(TCanvas* canv,const char* path="canv",const Option_t *option="ecn")
29 {
30  TString name;
31  TString opt = option; opt.ToLower();
32  //
33  TString name0 = path;
34  if (name0.IsNull()) name0 = "defCanv";
35  //
36  TFrame* fr = GetFrame();
37  if (fr) fr->SetBorderMode(0);
38  if (opt.Contains("c")) {
39  name = name0; name+=".C";
40  canv->Print(name.Data());
41  }
42  //
43  if (opt.Contains("e")) {
44  name = name0; name+=".eps";
45  canv->Print(name.Data());
46  }
47  //
48  if (opt.Contains("n")) {
49  name = name0; name+=".png";
50  canv->Print(name.Data());
51  }
52  //
53  if (opt.Contains("p")) {
54  name = name0; name+=".ps";
55  canv->Print(name.Data());
56  }
57  //
58 }
59 
60 
61 TLatex* AddLabel(const char*txt,float x,float y,int color,float size)
62 {
63  TLatex* lt = new TLatex(x,y,txt);
64  lt->SetNDC();
65  lt->SetTextColor(color);
66  lt->SetTextSize(size);
67  lt->Draw();
68  return lt;
69 }
70 
71 TH1* GetBaseHisto(TPad* pad)
72 {
73  if (!pad) pad = (TPad*)gPad;
74  if (!pad) return 0;
75  TList* lst = pad->GetListOfPrimitives();
76  int size = lst->GetSize();
77  TH1* hst=0;
78  for (int i=0;i<size;i++) {
79  TObject* obj = lst->At(i);
80  if (!obj) continue;
81  if (obj->InheritsFrom("TH1")) {hst = (TH1*)obj; break;}
82  }
83  return hst;
84 }
85 
86 TFrame* GetFrame(TPad* pad)
87 {
88  if (!pad) pad = (TPad*)gPad;
89  if (!pad) return 0;
90  TList* lst = pad->GetListOfPrimitives();
91  int size = lst->GetSize();
92  TFrame* frm=0;
93  for (int i=0;i<size;i++) {
94  TObject* obj = lst->At(i);
95  if (!obj) continue;
96  if (obj->InheritsFrom("TFrame")) {frm = (TFrame*)obj; break;}
97  }
98  return frm;
99 }
100 
101 TPaveStats* GetStatPad(TH1* hst)
102 {
103  TList *lst = hst->GetListOfFunctions();
104  if (!lst) return 0;
105  int nf = lst->GetSize();
106  for (int i=0;i<nf;i++) {
107  TPaveStats *fnc = (TPaveStats*) lst->At(i);
108  if (fnc->InheritsFrom("TPaveStats")) return fnc;
109  }
110  return 0;
111  //
112 }
113 
114 
115 TPaveStats* SetStatPad(TH1* hst,float x1,float x2,float y1,float y2, Int_t stl, Int_t col)
116 {
117  TPaveStats* pad = GetStatPad(hst);
118  if (!pad) return 0;
119  pad->SetX1NDC( x1 );
120  pad->SetX2NDC( x2 );
121  pad->SetY1NDC( y1 );
122  pad->SetY2NDC( y2 );
123  if (stl>=0) pad->SetFillStyle(stl);
124  if (col>=0) pad->SetFillColor(col);
125  //
126  gPad->Modified();
127  return pad;
128 }
129 
130 void SetHStyle(TH1* hst,int col,int mark,float mrsize)
131 {
132  hst->SetLineColor(col);
133  hst->SetMarkerColor(col);
134  // hst->SetFillColor(col);
135  hst->SetMarkerStyle(mark);
136  hst->SetMarkerSize(mrsize);
137  TList *lst = hst->GetListOfFunctions();
138  if (lst) {
139  int nf = lst->GetSize();
140  for (int i=0;i<nf;i++) {
141  TObject *fnc = lst->At(i);
142  if (fnc->InheritsFrom("TF1")) {
143  ((TF1*)fnc)->SetLineColor(col);
144  ((TF1*)fnc)->SetLineWidth(1);
145  ((TF1*)fnc)->ResetBit(TF1::kNotDraw);
146  }
147  else if (fnc->InheritsFrom("TPaveStats")) {
148  ((TPaveStats*)fnc)->SetTextColor(col);
149  }
150  }
151  }
152 }
153 
154 void SetGStyle(TGraph* hst,int col,int mark,float mrsize)
155 {
156  hst->SetLineColor(col);
157  hst->SetMarkerColor(col);
158  hst->SetFillColor(col);
159  hst->SetMarkerStyle(mark);
160  hst->SetMarkerSize(mrsize);
161  TList *lst = hst->GetListOfFunctions();
162  if (lst) {
163  int nf = lst->GetSize();
164  for (int i=0;i<nf;i++) {
165  TObject *fnc = lst->At(i);
166  if (fnc->InheritsFrom("TF1")) {
167  ((TF1*)fnc)->SetLineColor(col);
168  ((TF1*)fnc)->SetLineWidth(1);
169  ((TF1*)fnc)->ResetBit(TF1::kNotDraw);
170  }
171  else if (fnc->InheritsFrom("TPaveStats")) {
172  ((TPaveStats*)fnc)->SetTextColor(col);
173  }
174  }
175  }
176 }
177 
178 TH1* Cumulate(TH1* histo, Bool_t doErr, const char* copyName)
179 {
180  // create cumulative histo
181  TString nname = copyName;
182  if (nname.IsNull()) {
183  nname = histo->GetName();
184  nname += "_cml";
185  }
186  TH1* cml = (TH1*) histo->Clone(nname.Data());
187  int nb = histo->GetNbinsX();
188  double sm = 0;
189  double sme = 0;
190  //
191  for (int i=1;i<=nb;i++) {
192  sm += histo->GetBinContent(i);
193  cml->SetBinContent(i,sm);
194  if (!doErr) continue;
195  double ee = histo->GetBinError(i);
196  sme += ee*ee;
197  cml->SetBinError(i, sme>0 ? TMath::Sqrt(sme):0.);
198  }
199  return cml;
200 }
201 
202 
203 void wAv(double v1,double v2, double err1,double err2, double* wv,double *we)
204 {
205  // weighted average
206  double sum=0,err=0;
207  if (err1<=0 || err2<=0) {
208  sum = v1+v2;
209  }
210  else {
211  sum = v1/(err1*err1) + v2/(err2*err2);
212  err = 1./(err1*err1) + 1./(err2*err2);
213  sum /= err;
214  err = 1./TMath::Sqrt(err);
215  }
216  printf("wAv %+e(%+e) | %+e(%+e) -> %+e +- %e\n",v1,err1,v2,err2,sum,err);
217  if (wv) *wv = sum;
218  if (we) *we = err;
219 }
220 
221 
222 void wSum(double v1,double v2, double err1,double err2, double* wv,double *we)
223 {
224  // sum with errors
225  double sum=0,err=0;
226  sum = v1+v2;
227  if (err1>0 && err2>0) err = TMath::Sqrt(err1*err1 + err2*err2);
228  printf("wSum %+e(%+e) + %+e(%+e) -> %+e +- %e\n",v1,err1,v2,err2,sum,err);
229  if (wv) *wv = sum;
230  if (we) *we = err;
231 }
232 
233 //_________________________________________________
234 Double_t* defLogAx(double xMn,double xMx, int nbin)
235 {
236  // get array for log axis
237  if (xMn<=0 || xMx<=xMn || nbin<2) {
238  printf("Wrong axis request: %f %f %d\n",xMn,xMx,nbin);
239  return 0;
240  }
241  double dx = log(xMx/xMn)/nbin;
242  double *xax = new Double_t[nbin+1];
243  for (int i=0;i<=nbin;i++) xax[i]= xMn*exp(dx*i);
244  return xax;
245 }
246 
247 //_________________________________________________
248 TH2* manip2d(TH2* histo, Bool_t cumul, const char* newname, Bool_t over, Bool_t under)
249 {
250  // normalize each XYbin to total of X bin
251  // cumul=ktRUE: cumulative of each X bin, normalized to 1
252  // over, under - include over/under flows
253  //
254  if (!histo) return 0;
255  if (!histo->InheritsFrom("TH2")) {printf("Not a TH2\n"); return 0;}
256  //
257  TString nmstr = newname;
258  if (nmstr.IsNull()) nmstr = Form("%s_nrm%s",histo->GetName(),cumul ? "_cml" : "");
259  TH2* cln = (TH2*)histo->Clone(nmstr.Data());
260  //
261  TAxis* xax = histo->GetXaxis();
262  TAxis* yax = histo->GetYaxis();
263  int yb0 = under ? 0:1;
264  int yb1 = over ? yax->GetNbins() : 1+yax->GetNbins();;
265  //
266  for (int ix=0;ix<=xax->GetNbins()+1;ix++) {
267  double totb = 0;
268  for (int iy=0;iy<=yax->GetNbins()+1;iy++) {
269  double val = histo->GetBinContent(ix,iy);
270  totb += val;
271  }
272  if (totb<=0) continue;
273  //
274  // normalize X bin to 1
275  for (int iy=yb0;iy<=yb1;iy++) {
276  double val = histo->GetBinContent(ix,iy);
277  double err = histo->GetBinError(ix,iy);
278  cln->SetBinContent(ix,iy,val/totb);
279  cln->SetBinError(ix,iy,err/totb);
280  }
281  //
282  if (!cumul) continue;
283  //
284  for (int iy=yb0+1;iy<=yb1;iy++) {
285  double val = cln->GetBinContent(ix,iy);
286  double err = cln->GetBinError(ix,iy);
287  double valP = cln->GetBinContent(ix,iy-1);
288  double errP = cln->GetBinError(ix,iy-1);
289  err = err*err+errP*errP;
290  err = err >0 ? TMath::Sqrt(err) : 0;
291  cln->SetBinContent(ix,iy,val + valP);
292  cln->SetBinError(ix,iy,err);
293  }
294  }
295  //
296  return cln;
297 }
Int_t color[]
print message on plot with ok/not ok
double Double_t
Definition: External.C:58
TLatex * AddLabel(const char *txt, float x=0.1, float y=0.9, int color=kBlack, float size=0.04)
Definition: SaveCanvas.C:61
void SaveCanvas(TCanvas *canv, const char *path="canv", const Option_t *option="ecn")
Definition: SaveCanvas.C:28
Double_t * defLogAx(double xMn, double xMx, int nbin)
Definition: SaveCanvas.C:234
TPaveStats * SetStatPad(TH1 *hst, float x1, float x2, float y1, float y2, Int_t stl=-1, Int_t col=-1)
Definition: SaveCanvas.C:115
void SetGStyle(TGraph *hst, int col=kRed, int mark=20, float mrsize=0.7)
Definition: SaveCanvas.C:154
int Int_t
Definition: External.C:63
TH1 * Cumulate(TH1 *histo, Bool_t doErr=kTRUE, const char *copyName=0)
Definition: SaveCanvas.C:178
void wAv(double v1, double v2, double err1=0, double err2=0, double *wv=0, double *we=0)
Definition: SaveCanvas.C:203
TH1 * GetBaseHisto(TPad *pad=0)
Definition: SaveCanvas.C:71
TH2 * manip2d(TH2 *histo, Bool_t cumul, const char *newname, Bool_t over, Bool_t under)
Definition: SaveCanvas.C:248
Definition: External.C:220
const char Option_t
Definition: External.C:48
bool Bool_t
Definition: External.C:53
TPaveStats * GetStatPad(TH1 *hst)
Definition: SaveCanvas.C:101
void wSum(double v1, double v2, double err1=0, double err2=0, double *wv=0, double *we=0)
Definition: SaveCanvas.C:222
Definition: External.C:196
void SetHStyle(TH1 *hst, int col=kRed, int mark=20, float mrsize=0.7)
Definition: SaveCanvas.C:130
TFrame * GetFrame(TPad *pad=0)
Definition: SaveCanvas.C:86