]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG/muondep/AliAnalysisMuMuGraphUtil.cxx
ed6bb5968eb909eb993030ebd33a714eeae16246
[u/mrichter/AliRoot.git] / PWG / muondep / AliAnalysisMuMuGraphUtil.cxx
1 #include "AliAnalysisMuMuGraphUtil.h"
2
3 #include "TAxis.h"
4 #include "TCanvas.h"
5 #include "TPad.h"
6 #include "TGraphErrors.h"
7 #include "TString.h"
8 #include <vector>
9 #include <map>
10 #include "TMath.h"
11 #include "TObjArray.h"
12 #include "AliLog.h"
13 #include "TLegend.h"
14 #include "TH2F.h"
15 #include "TStyle.h"
16 #include "AliAnalysisTriggerScalers.h"
17 #include <set>
18 #include "AliAnalysisMuMuResult.h"
19
20 ClassImp(AliAnalysisMuMuGraphUtil)
21
22 //____________________________________________________________________________
23 AliAnalysisMuMuGraphUtil::AliAnalysisMuMuGraphUtil(const char* ocdbpath) : TObject(),
24 fOCDBPath(ocdbpath),
25 fAttLine(),
26 fAttMarker(),
27 fAttFill(),
28 fAttXaxis(),
29 fAttYaxis(),
30 fDrawOptions(),
31 fShouldDrawPeriods(kFALSE)
32 {
33   // default ctor
34   DefaultStyle();
35 }
36
37 //____________________________________________________________________________
38 TGraphErrors* AliAnalysisMuMuGraphUtil::Combine(TObjArray& graphs, Bool_t compact)
39 {
40   // make one graph out of several
41   // x axis is supposed to be run numbers and will end up ordered in the
42   // returned graph
43   
44   std::map<int, std::vector<double> > values;
45   std::map<int, std::vector<double> >::const_iterator it;
46
47   TIter next(&graphs);
48   TGraph* g;
49   
50   while ( ( g = static_cast<TGraph*>(next())) )
51   {
52     TGraphErrors* ge = dynamic_cast<TGraphErrors*>(g);
53     
54     for ( Int_t i = 0; i < g->GetN(); ++i )
55     {
56       Int_t runNumber = GetRunNumber(*g,i); // by doing this we "de-compact" the graph
57
58       it = values.find(runNumber);
59       if ( it != values.end() )
60       {
61         AliErrorClass(Form("Already got values for run %d !",runNumber));
62         StdoutToAliErrorClass(graphs.Print(););
63         return 0x0;
64       }
65
66       std::vector<double> quartet;
67     
68       quartet.push_back(runNumber);
69       quartet.push_back(g->GetY()[i]);
70       
71       if ( ge )
72       {
73         quartet.push_back(ge->GetEX()[i]);
74         quartet.push_back(ge->GetEY()[i]);
75       }
76       else
77       {
78         quartet.push_back(0.0);
79         quartet.push_back(0.0);
80       }
81       
82       values.insert( std::make_pair<int, std::vector<double> >(runNumber,quartet));      
83     }
84   }
85   
86   TGraphErrors* rv(0x0);
87   
88   if ( values.size() )
89   {
90     std::vector<double> vx;
91     std::vector<double> vy;
92     std::vector<double> vxerr;
93     std::vector<double> vyerr;
94     
95     for ( it = values.begin(); it != values.end(); ++it )
96     {
97       const std::vector<double>& q = it->second;
98       
99       vx.push_back(q[0]);
100       vy.push_back(q[1]);
101       vxerr.push_back(q[2]);
102       vyerr.push_back(q[3]);
103     }
104     
105     rv = new TGraphErrors(values.size(),&vx[0],&vy[0],&vxerr[0],&vyerr[0]);
106     rv->GetXaxis()->SetNoExponent();
107     
108     g = static_cast<TGraph*>(graphs.At(0));
109     
110     rv->SetName(g->GetName());
111     rv->SetTitle(g->GetTitle());
112     
113     if ( compact || IsCompact(*g) )
114     {
115       Compact(*rv);
116     }
117   }
118   
119   return rv;
120 }
121
122 //____________________________________________________________________________
123 void AliAnalysisMuMuGraphUtil::DefaultStyle()
124 {
125   // Define default color/styles to be used, for at least 2 graphs
126   // (here 5)
127   
128   Int_t colors[] = { 1, kGray+1, 4, 2, 6 };
129   
130   for ( Int_t i = 0; i < 5; ++i )
131   {
132     Int_t color = colors[i];
133     
134     fAttLine.push_back(TAttLine(color,1,1));
135     fAttFill.push_back(TAttFill(color,1001));
136     fAttMarker.push_back(TAttMarker(color,20+i,1));
137     fAttXaxis.push_back(TAttAxis());
138     
139     TAttAxis a;
140     
141     a.ResetAttAxis();
142     
143     a.SetLabelColor(color);
144     a.SetTitleColor(color);
145     
146     fAttYaxis.push_back(a);
147     
148     fDrawOptions.push_back("LP");
149   }
150 }
151
152 //____________________________________________________________________________
153 TCanvas* AliAnalysisMuMuGraphUtil::DrawWith2Scales(TGraph& g1, TGraph& g2, const char* canvasName)
154 {
155   TCanvas* c1 = new TCanvas(canvasName,canvasName);
156   c1->Draw();
157   
158   TPad* pad1 = new TPad("pad1","",0,0,1,1);
159   TPad* pad2 = new TPad("pad2","",0,0,1,1);
160   
161   g2.GetYaxis()->SetTitle(g2.GetTitle());
162   g1.GetYaxis()->SetTitle(g1.GetTitle());
163   
164   pad1->SetFillStyle(4000);
165   pad1->SetFrameFillStyle(0); //  transparent pad
166   
167   pad2->Draw();
168   pad2->cd();
169   
170   StyleGraph(g2,1);
171   
172   g2.Draw("abxy+");
173   
174   pad1->Draw();
175   pad1->cd();
176   
177   StyleGraph(g1,0);
178   
179   g1.Draw("alp");
180   
181   return c1;
182 }
183
184 //____________________________________________________________________________
185 void AliAnalysisMuMuGraphUtil::Compact(TGraph& g)
186 {
187   /// Compact (i.e. get the equivalent of 1 bin = 1 run number for an histogram)
188   /// the graph.
189   /// Only works if the x content of this graph represents run numbers. Otherwise
190   /// result is unpredictable ;-)
191   
192   if ( !g.GetN() ) return;
193   
194   Double_t x,y;
195   
196   std::vector<int> runs;
197   std::vector<double> bins;
198   
199   Int_t i(0);
200   
201   for ( i = 0; i < g.GetN(); ++i )
202   {
203     g.GetPoint(i,x,y);
204     runs.push_back(TMath::Nint(x));
205     bins.push_back(i);
206     g.SetPoint(i,i+0.5,y);
207   }
208   
209   bins.push_back(i);
210   
211   TAxis* axis = g.GetXaxis();
212   
213   axis->Set(g.GetN(),&bins[0]);
214   
215   for ( std::vector<int>::size_type j = 0; j < runs.size(); ++j )
216   {
217     axis->SetBinLabel(j+1,TString::Format("%d",runs[j]).Data());
218   }
219   
220   axis->LabelsOption("v");
221 }
222
223 //_____________________________________________________________________________
224 Int_t AliAnalysisMuMuGraphUtil::GetRunNumber(const TGraph& g, Int_t i)
225 {
226   // get the run number associated with bin i+1
227   // if graph is not compacted then run number = x-value
228   // otherwise we get it from the axis label
229  
230   Int_t runNumber = TMath::Nint(g.GetX()[i]);
231
232   TString runLabel = g.GetXaxis()->GetBinLabel(i+1);
233   
234   if ( runLabel.Length() )
235   {
236     runNumber = runLabel.Atoi();
237   }
238
239   return runNumber;
240 }
241
242 //_____________________________________________________________________________
243 void AliAnalysisMuMuGraphUtil::GetRuns(std::set<int>& runs, TGraph& graph) const
244 {
245   // extract the list of runs in graph's x-axis
246   
247   for ( Int_t i = 0; i < graph.GetN(); ++i )
248   {
249     runs.insert(GetRunNumber(graph,i));
250   }
251 }
252
253 //_____________________________________________________________________________
254 void AliAnalysisMuMuGraphUtil::GetYMinAndMax(TGraph& graph, Double_t& ymin, Double_t& ymax)
255 {
256   // find graph y-range
257   // note that ymin and ymax *must* be initialized correctly outside
258   // (this is done this way so that this method can be used easily
259   // to get the range of a set of graphs)
260   
261   Double_t x,y;
262   
263   for ( Int_t i = 0; i < graph.GetN(); ++i )
264   {
265     graph.GetPoint(i,x,y);
266     ymin = TMath::Min(ymin,y);
267     ymax = TMath::Max(ymax,y);
268   }
269   
270 }
271
272 //_____________________________________________________________________________
273 Bool_t AliAnalysisMuMuGraphUtil::IsCompact(TGraph& g)
274 {
275   // whether the graph is compact or not
276   Double_t delta(0.0);
277   
278   for ( Int_t i = 1; i < g.GetN(); ++i )
279   {
280     delta = TMath::Max(delta,g.GetX()[i] - g.GetX()[i-1]);
281   }
282   
283   Bool_t hasLabels(kFALSE);
284
285   for ( Int_t i = 1; ( i <= g.GetN() ) && ( !hasLabels ); ++i )
286   {
287     TString label(g.GetXaxis()->GetBinLabel(i));
288     if ( label.Length() ) hasLabels = kTRUE;
289   }
290   
291   return hasLabels && delta == 1.0;
292 }
293
294 //_____________________________________________________________________________
295 void AliAnalysisMuMuGraphUtil::PlotSameWithLegend(TObjArray& a,
296                                                   Double_t ymin, Double_t ymax) const
297 {
298   // plot on same canvas
299   if (!gPad) new TCanvas;
300   
301   Double_t xmin = TMath::Limits<Double_t>::Max();
302   Double_t xmax = TMath::Limits<Double_t>::Min();
303   
304   TIter next(&a);
305   TGraph* g;
306   
307   while ( ( g = static_cast<TGraph*>(next())))
308   {
309     xmin = TMath::Min(xmin,g->GetX()[0]);
310
311     xmax = TMath::Max(xmax,g->GetX()[g->GetN()-1]);
312
313   }
314
315   TH2* hframe = new TH2F("hframe","hframe",100,xmin,xmax,100,ymin,ymax);
316   
317   gStyle->SetOptTitle(0);
318   gStyle->SetOptStat(0);
319
320   hframe->GetXaxis()->SetNoExponent();
321
322 //  if ( IsCompact(g1) )
323 //  {
324 //    (*(hframe->GetXaxis()))=(*(g1.GetXaxis()));
325 //  }
326
327   hframe->Draw();
328
329   if ( fShouldDrawPeriods )
330   {
331     std::set<int> runs;
332   
333     next.Reset();
334     
335     while ( ( g = static_cast<TGraph*>(next())))
336     {
337       GetRuns(runs,*g);
338     }
339     
340     AliAnalysisTriggerScalers ts(runs,fOCDBPath);
341   
342     ts.DrawPeriods(ymin,ymax,kGray);
343     
344     hframe->Draw("axissame");
345   }
346
347   next.Reset();
348   
349   Int_t i(0);
350   TLegend* l = new TLegend(0.5,0.7,0.9,0.9); // fixme: how to get the legend position/size ?
351   l->SetFillColor(0);
352   
353   while ( ( g = static_cast<TGraph*>(next())))
354   {
355     StyleGraph(*g,i);
356     g->Draw(fDrawOptions[i].c_str());
357     ++i;
358     l->AddEntry(g,g->GetName(),fDrawOptions[0].c_str());
359   }
360   
361   l->Draw();  
362
363 }
364
365 //_____________________________________________________________________________
366 TGraph* AliAnalysisMuMuGraphUtil::RelDif(TGraph& ga, TGraph& gb)
367 {
368   // compute the relative difference between two graphs
369   
370   std::vector<double> vx;
371   std::vector<double> vxerr;
372   std::vector<double> vy;
373   std::vector<double> vyerr;
374
375   for ( Int_t i = 0; i < ga.GetN(); ++i )
376   {
377     Double_t xa,xb,ya,yb;
378   
379     ga.GetPoint(i,xa,ya);
380     gb.GetPoint(i,xb,yb);
381   
382     if ( xa != xb )
383     {
384       AliErrorClass(Form("Incompatible graphs : got xa=%e and xb=%e",xa,xb));
385       return 0x0;
386     }
387   
388     Double_t newvalue = 0.0;
389   
390     if ( TMath::Abs(xa) > 1E-12 )
391     {
392       newvalue = 100.0*( yb - ya ) / ya;
393     }
394   
395     Double_t yerr = 0.0;
396     
397     if ( dynamic_cast<TGraphErrors*>(&ga) && dynamic_cast<TGraphErrors*>(&gb) )
398     {
399         yerr = newvalue*AliAnalysisMuMuResult::ErrorAB(ya,ga.GetEY()[i],
400                                                        yb,gb.GetEY()[i]);
401     }
402   
403     vx.push_back(xa);
404     vxerr.push_back(0.5);
405     vy.push_back(newvalue);
406     vyerr.push_back(yerr);
407   }
408   
409   
410   return new TGraphErrors(vx.size(),&vx[0],&vy[0],&vxerr[0],&vyerr[0]);
411 }
412
413 //_____________________________________________________________________________
414 void AliAnalysisMuMuGraphUtil::StyleGraph(TGraph& g, UInt_t index) const
415 {
416   if ( index >= fAttFill.size() ) index = 0;
417   
418   static_cast<TAttFill&>(g) = fAttFill[index];
419   static_cast<TAttLine&>(g) = fAttLine[index];
420   static_cast<TAttMarker&>(g) = fAttMarker[index];
421   
422   g.GetYaxis()->SetLabelColor(fAttYaxis[index].GetLabelColor());
423   g.GetYaxis()->SetTitleColor(fAttYaxis[index].GetTitleColor());
424   
425   //static_cast<TAttAxis&>((*g.GetYaxis())) = fAttYaxis[index];
426 }
427
428 //_____________________________________________________________________________
429 void AliAnalysisMuMuGraphUtil::UnCompact(TGraph& g)
430 {
431   /// Reverse operation of the Compact method
432   /// Only works if the labels of this graph represents run numbers. Otherwise
433   /// result is unpredictable ;-)
434   
435   if ( !g.GetN() ) return;
436   
437   //  Int_t run1 = TString(g.GetXaxis()->GetBinLabel(1)).Atoi();
438   //  Int_t run2 = TString(g.GetXaxis()->GetBinLabel(g.GetN())).Atoi();
439   
440   std::vector<double> runs;
441   Int_t runNumber(-1);
442   
443   for ( Int_t i = 0; i < g.GetN(); ++i )
444   {
445     runNumber = TString(g.GetXaxis()->GetBinLabel(i+1)).Atoi();
446     runs.push_back(runNumber*1.0);
447   }
448   
449   runs.push_back(runNumber+1);
450   
451   g.GetXaxis()->Set(g.GetN(),&runs[0]);
452   
453   for ( Int_t i = 0; i < g.GetN(); ++i )
454   {
455     g.SetPoint(i,runs[i],g.GetY()[i]);
456   }
457   
458   g.GetXaxis()->SetNoExponent();
459   
460 }
461
462