]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGLF/SPECTRA/Nuclei/B2/macros/DrawDir.C
Fix DCA templates for TPC+TOF
[u/mrichter/AliRoot.git] / PWGLF / SPECTRA / Nuclei / B2 / macros / DrawDir.C
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 // Draw histograms/graphs with same name located in different directories
17 // author: Eulogio Serradilla <eulogio.serradilla@cern.ch>
18
19 #include <TROOT.h>
20 #include <TFile.h>
21 #include <TString.h>
22 #include <TMath.h>
23 #include <TCanvas.h>
24 #include <TPad.h>
25 #include <TLegend.h>
26 #include <TH1.h>
27 #include <TGraphErrors.h>
28 #include <TKey.h>
29 #include <TLine.h>
30 #include <TStyle.h>
31
32 TGraphErrors* Divide(const TGraphErrors* grX1, const TGraphErrors* grX2, const TString& name);
33
34 void DrawDir(const TString& inputFile,
35              const TString& graphName,
36              const TString& refdir="",
37              Double_t xmin = 0,
38              Double_t xmax = 3.5,
39              Double_t ymin = 0,
40              Double_t ymax = 1.,
41              const TString& xtitle = "",
42              const TString& ytitle = "",
43              Int_t option = 1,
44              const TString& canvasName = "c0",
45              const TString& canvasTitle = "DrawDir")
46 {
47 //
48 // draw histograms/graphs with same name located in different directories
49 // if option = 0 no comparison,
50 // if option = 1 draw a bottom pad with the comparison, and
51 // if option = 2 draw the comparison in a different canvas
52 //
53         const Int_t kMax = 10; // maximum number of subdirectories
54         
55         const Int_t kColor[kMax]  = { kRed, kBlue, kOrange+1, kGreen-2, kGreen+2, kAzure, kViolet+10, kAzure+2, kOrange+2, kSpring-7 };
56         
57         const Int_t kMarker[kMax] = { kFullCircle, kFullCircle, kFullCircle, kFullCircle, kOpenCircle, kOpenSquare, kOpenTriangleUp, kOpenDiamond, kOpenCross, kFullStar };
58         
59         TFile* finput = new TFile(inputFile.Data());
60         if (finput->IsZombie()) exit(1);
61         
62         // iterate over the list of keys
63         
64         TObject* obj[kMax];
65         TString* subdir[kMax];
66         obj[0] = 0; // reference object
67         Int_t nDir = 0;
68         if(option>0) nDir = 1;
69         
70         TIter next(finput->GetListOfKeys());
71         TKey* key = 0;
72         while( (key = (TKey*)next()) && (nDir < kMax) )
73         {
74                 TString classname = key->GetClassName();
75                 if(classname == "TDirectoryFile")
76                 {
77                         TObject* i = 0;
78                         finput->GetObject(Form("%s/%s;1", key->GetName(), graphName.Data()),i);
79                         if(i == 0)
80                         {
81                                 finput->Error("GetObject","%s/%s not found", key->GetName(), graphName.Data());
82                                 exit(1);
83                         }
84                         else if(i->InheritsFrom("TH1") || i->InheritsFrom("TGraph"))
85                         {
86                                 Int_t j = 0;
87                                 
88                                 if(option==0) j = nDir++;
89                                 else if(TString(key->GetName()) != refdir) j = nDir++;
90                                 
91                                 obj[j] = i;
92                                 (dynamic_cast<TAttLine*>(i))->SetLineColor(kColor[j]);
93                                 (dynamic_cast<TAttMarker*>(i))->SetMarkerColor(kColor[j]);
94                                 (dynamic_cast<TAttMarker*>(i))->SetMarkerStyle(kMarker[j]);
95                                 subdir[j] = new TString(key->GetName());
96                         }
97                         else
98                         {
99                                 finput->Warning("GetObject", "%s does not contain %s", key->GetName(), graphName.Data());
100                         }
101                 }
102         }
103         
104         // compare w.r.t. reference data
105         
106         TGraphErrors* grDiv[kMax];
107         
108         if(option > 0)
109         {
110                 if(obj[0] == 0)
111                 {
112                         finput->Error("GetObject", "reference directory %s not found", refdir.Data());
113                         for(Int_t i=0; i < nDir; ++i) delete subdir[i];
114                         exit(1);
115                 }
116                 
117                 TGraphErrors* grY = 0;
118                 
119                 if(obj[0]->InheritsFrom("TH1"))
120                 {
121                         grY = new TGraphErrors(dynamic_cast<TH1*>(obj[0]));
122                 }
123                 else if(obj[0]->InheritsFrom("TGraph"))
124                 {
125                         grY = new TGraphErrors(*dynamic_cast<TGraphErrors*>(obj[0]));
126                 }
127                 
128                 TGraphErrors* grX = 0;
129                 
130                 for(Int_t i=1; i < nDir; ++i)
131                 {
132                         if(obj[i]->InheritsFrom("TH1"))
133                         {
134                                 grX = new TGraphErrors(dynamic_cast<TH1*>(obj[i]));
135                         }
136                         else if(obj[i]->InheritsFrom("TGraph"))
137                         {
138                                 grX = new TGraphErrors(*dynamic_cast<TGraphErrors*>(obj[i]));
139                         }
140                         
141                         grDiv[i] = Divide(grX, grY, Form("%s_%s_Ratio", subdir[i]->Data(), obj[i]->GetName()));
142                         
143                         grDiv[i]->SetLineColor(kColor[i]);
144                         grDiv[i]->SetMarkerColor(kColor[i]);
145                         grDiv[i]->SetMarkerStyle(kMarker[i]);
146                         
147                         delete grX;
148                 }
149                 
150                 delete grY;
151         }
152         
153         // draw
154         
155         TStyle* st = new TStyle();
156         
157         st->SetOptTitle(0);
158         st->SetOptStat(0);
159         
160         st->SetPadTickX(1);
161         st->SetPadTickY(1);
162         st->SetPadGridX(1);
163         st->SetPadGridY(1);
164         
165         st->SetCanvasColor(0);
166         st->SetFrameBorderMode(0);
167         st->SetFrameFillColor(0);
168         st->SetTitleFillColor(0);
169         st->SetLabelFont(62,"XYZ");
170         st->SetTitleFont(62,"XYZ");
171         
172         st->cd();
173         gROOT->ForceStyle();
174         
175         TCanvas* c0 = new TCanvas(canvasName.Data(), canvasTitle.Data());
176         
177         if(option == 1) // create a top pad
178         {
179                 TPad* top = new TPad("top", "Variable", 0, 0.25, 1, 1, 0, 0, 0);
180                 
181                 top->SetBottomMargin(0.);
182                 top->Draw();
183                 
184                 top->cd();
185                 
186                 TH1F* frm = top->DrawFrame(xmin, ymin , xmax, ymax);
187                 frm->GetYaxis()->SetTitle(ytitle);
188         }
189         else // only draw the frame
190         {
191                 TH1F* frm = c0->DrawFrame(xmin, ymin , xmax, ymax);
192                 frm->GetXaxis()->SetTitle(xtitle);
193                 frm->GetYaxis()->SetTitle(ytitle);
194         }
195         
196         // draw objects in current pad
197         for(Int_t i=0; i < nDir; ++i)
198         {
199                 if(obj[i]->InheritsFrom("TH1"))
200                 {
201                         obj[i]->Draw("same");
202                 }
203                 else if(obj[i]->InheritsFrom("TGraph"))
204                 {
205                         obj[i]->Draw("PZ");
206                 }
207         }
208         
209         // build a legend
210         TLegend* legend = new TLegend(0.5718391,0.6991525,0.8390805,0.8368644,0,"brNDC");
211         legend->SetTextSize(0.03);
212         legend->SetFillColor(0);
213         legend->SetBorderSize(0);
214         
215         for(Int_t i=0; i < nDir; ++i)
216         {
217                 legend->AddEntry(obj[i], subdir[i]->Data(), "lp");
218         }
219         
220         legend->Draw();
221         
222         if(option == 1) // create a bottom pad
223         {
224                 c0->cd();
225                 
226                 TPad* bottom = new TPad("bottom", "ratio", 0, 0, 1, 0.25, 0, 0, 0);
227                 
228                 bottom->SetBottomMargin(0.3);
229                 bottom->SetTopMargin(0.);
230                 bottom->Draw();
231                 
232                 bottom->cd();
233                 
234                 TH1F* frm = bottom->DrawFrame(xmin, 0.7 , xmax, 1.3);
235                 
236                 frm->GetXaxis()->SetLabelSize(0.13);
237                 frm->GetXaxis()->SetTitle(xtitle);
238                 frm->GetXaxis()->SetTitleSize(0.13);
239                 frm->GetYaxis()->SetTitle("Ratio");
240                 frm->GetYaxis()->SetLabelSize(0.1);
241                 frm->GetYaxis()->SetTitleSize(0.12);
242                 frm->GetYaxis()->SetTitleOffset(0.3);
243         }
244         else if(option == 2) // create a new canvas
245         {
246                 TCanvas* c1 = new TCanvas(Form("%s.Ratio",canvasName.Data()), Form("%s ratio",canvasTitle.Data()));
247                 
248                 TH1F* frm = c1->DrawFrame(xmin, 0.5 ,xmax, 1.5);
249                 frm->GetXaxis()->SetTitle(xtitle);
250                 frm->GetYaxis()->SetTitle("Ratio");
251         }
252         
253         // draw comparison
254         if(nDir>0 && option > 0)
255         {
256                 for(Int_t i=1; i<nDir; ++i)
257                 {
258                         grDiv[i]->Draw("PZ");
259                 }
260                 
261                 // draw a red line for the reference
262                 TLine* ref = new TLine(xmin,1,xmax,1);
263                 ref->SetLineWidth(1);
264                 ref->SetLineColor(kColor[0]);
265                 ref->Draw();
266                 
267                 if(option == 2)
268                 {
269                         TLegend* legendRatio = new TLegend(0.5718391,0.6991525,0.8390805,0.8368644,0,"brNDC");
270                         legendRatio->SetTextSize(0.03);
271                         legendRatio->SetFillColor(0);
272                         legendRatio->SetBorderSize(0);
273                         
274                         legendRatio->AddEntry(ref, subdir[0]->Data(), "l");
275                         for(Int_t i=1; i < nDir; ++i)
276                         {
277                                 legendRatio->AddEntry(grDiv[i], subdir[i]->Data(), "lp");
278                         }
279                         
280                         legendRatio->Draw();
281                 }
282         }
283         
284         // release memory
285 }
286
287 Double_t GuessErrorY(const TGraphErrors* gr, Double_t x0)
288 {
289 //
290 // estimate error of gr(x0) with the closest point to x0
291 //
292         for(Int_t i=0; i<gr->GetN(); ++i)
293         {
294                 Double_t x, y;
295                 gr->GetPoint(i, x, y);
296                 if(x >= x0) return gr->GetErrorY(i);
297         }
298         
299         return 0;
300 }
301
302 TGraphErrors* Divide(const TGraphErrors* grX1, const TGraphErrors* grX2, const TString& name)
303 {
304 //
305 // grX1/grX2 using grX2 as reference
306 //
307         TGraphErrors* grQ = new TGraphErrors();
308         grQ->SetName(name.Data());
309         
310         Double_t xmin = 0;
311         Double_t xmax = 0;
312         Double_t y1   = 0;
313         grX1->GetPoint(0, xmin, y1);
314         grX1->GetPoint(grX1->GetN()-1, xmax, y1);
315         
316         for(Int_t i=0, j=0; i < grX2->GetN(); ++i)
317         {
318                 Double_t x2, y2;
319                 grX2->GetPoint(i, x2, y2);
320                 
321                 if(x2 < xmin) continue;
322                 if(x2 > xmax) break;
323                 
324                 y1 = grX1->Eval(x2);
325                 
326                 if(y1 == 0 || y2 == 0) continue;
327                 
328                 Double_t r = y1/y2;
329                 
330                 Double_t erry1 = GuessErrorY(grX1, x2);
331                 Double_t erry2 = grX2->GetErrorY(i);
332                 Double_t err = r*TMath::Sqrt(TMath::Power(erry1/y1,2)+TMath::Power(erry2/y2,2));
333                 
334                 grQ->SetPoint(j, x2, r);
335                 grQ->SetPointError(j++, grX2->GetErrorX(i), err);
336         }
337         
338         return grQ;
339 }