1 #include "AliAnalysisMuMuGraphUtil.h"
6 #include "TGraphErrors.h"
11 #include "TObjArray.h"
16 #include "AliAnalysisTriggerScalers.h"
18 #include "AliAnalysisMuMuResult.h"
20 ClassImp(AliAnalysisMuMuGraphUtil)
22 //____________________________________________________________________________
23 AliAnalysisMuMuGraphUtil::AliAnalysisMuMuGraphUtil(const char* ocdbpath) : TObject(),
31 fShouldDrawPeriods(kFALSE)
37 //____________________________________________________________________________
38 TGraphErrors* AliAnalysisMuMuGraphUtil::Combine(TObjArray& graphs, Bool_t compact)
40 // make one graph out of several
41 // x axis is supposed to be run numbers and will end up ordered in the
44 std::map<int, std::vector<double> > values;
45 std::map<int, std::vector<double> >::const_iterator it;
50 while ( ( g = static_cast<TGraph*>(next())) )
52 TGraphErrors* ge = dynamic_cast<TGraphErrors*>(g);
54 for ( Int_t i = 0; i < g->GetN(); ++i )
56 Int_t runNumber = GetRunNumber(*g,i); // by doing this we "de-compact" the graph
58 it = values.find(runNumber);
59 if ( it != values.end() )
61 AliErrorClass(Form("Already got values for run %d !",runNumber));
62 StdoutToAliErrorClass(graphs.Print(););
66 std::vector<double> quartet;
68 quartet.push_back(runNumber);
69 quartet.push_back(g->GetY()[i]);
73 quartet.push_back(ge->GetEX()[i]);
74 quartet.push_back(ge->GetEY()[i]);
78 quartet.push_back(0.0);
79 quartet.push_back(0.0);
82 values.insert( std::make_pair<int, std::vector<double> >(runNumber,quartet));
86 TGraphErrors* rv(0x0);
90 std::vector<double> vx;
91 std::vector<double> vy;
92 std::vector<double> vxerr;
93 std::vector<double> vyerr;
95 for ( it = values.begin(); it != values.end(); ++it )
97 const std::vector<double>& q = it->second;
101 vxerr.push_back(q[2]);
102 vyerr.push_back(q[3]);
105 rv = new TGraphErrors(values.size(),&vx[0],&vy[0],&vxerr[0],&vyerr[0]);
106 rv->GetXaxis()->SetNoExponent();
108 g = static_cast<TGraph*>(graphs.At(0));
110 rv->SetName(g->GetName());
111 rv->SetTitle(g->GetTitle());
113 if ( compact || IsCompact(*g) )
122 //____________________________________________________________________________
123 void AliAnalysisMuMuGraphUtil::DefaultStyle()
125 // Define default color/styles to be used, for at least 2 graphs
128 Int_t colors[] = { 1, kGray+1, 4, 2, 6 };
130 for ( Int_t i = 0; i < 5; ++i )
132 Int_t color = colors[i];
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());
143 a.SetLabelColor(color);
144 a.SetTitleColor(color);
146 fAttYaxis.push_back(a);
148 fDrawOptions.push_back("LP");
152 //____________________________________________________________________________
153 TCanvas* AliAnalysisMuMuGraphUtil::DrawWith2Scales(TGraph& g1, TGraph& g2, const char* canvasName)
155 TCanvas* c1 = new TCanvas(canvasName,canvasName);
158 TPad* pad1 = new TPad("pad1","",0,0,1,1);
159 TPad* pad2 = new TPad("pad2","",0,0,1,1);
161 g2.GetYaxis()->SetTitle(g2.GetTitle());
162 g1.GetYaxis()->SetTitle(g1.GetTitle());
164 pad1->SetFillStyle(4000);
165 pad1->SetFrameFillStyle(0); // transparent pad
184 //____________________________________________________________________________
185 void AliAnalysisMuMuGraphUtil::Compact(TGraph& g)
187 /// Compact (i.e. get the equivalent of 1 bin = 1 run number for an histogram)
189 /// Only works if the x content of this graph represents run numbers. Otherwise
190 /// result is unpredictable ;-)
192 if ( !g.GetN() ) return;
196 std::vector<int> runs;
197 std::vector<double> bins;
201 for ( i = 0; i < g.GetN(); ++i )
204 runs.push_back(TMath::Nint(x));
206 g.SetPoint(i,i+0.5,y);
211 TAxis* axis = g.GetXaxis();
213 axis->Set(g.GetN(),&bins[0]);
215 for ( std::vector<int>::size_type j = 0; j < runs.size(); ++j )
217 axis->SetBinLabel(j+1,TString::Format("%d",runs[j]).Data());
220 axis->LabelsOption("v");
223 //_____________________________________________________________________________
224 Int_t AliAnalysisMuMuGraphUtil::GetRunNumber(const TGraph& g, Int_t i)
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
230 Int_t runNumber = TMath::Nint(g.GetX()[i]);
232 TString runLabel = g.GetXaxis()->GetBinLabel(i+1);
234 if ( runLabel.Length() )
236 runNumber = runLabel.Atoi();
242 //_____________________________________________________________________________
243 void AliAnalysisMuMuGraphUtil::GetRuns(std::set<int>& runs, TGraph& graph) const
245 // extract the list of runs in graph's x-axis
247 for ( Int_t i = 0; i < graph.GetN(); ++i )
249 runs.insert(GetRunNumber(graph,i));
253 //_____________________________________________________________________________
254 void AliAnalysisMuMuGraphUtil::GetYMinAndMax(TGraph& graph, Double_t& ymin, Double_t& ymax)
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)
263 for ( Int_t i = 0; i < graph.GetN(); ++i )
265 graph.GetPoint(i,x,y);
266 ymin = TMath::Min(ymin,y);
267 ymax = TMath::Max(ymax,y);
272 //_____________________________________________________________________________
273 Bool_t AliAnalysisMuMuGraphUtil::IsCompact(TGraph& g)
275 // whether the graph is compact or not
278 for ( Int_t i = 1; i < g.GetN(); ++i )
280 delta = TMath::Max(delta,g.GetX()[i] - g.GetX()[i-1]);
283 Bool_t hasLabels(kFALSE);
285 for ( Int_t i = 1; ( i <= g.GetN() ) && ( !hasLabels ); ++i )
287 TString label(g.GetXaxis()->GetBinLabel(i));
288 if ( label.Length() ) hasLabels = kTRUE;
291 return hasLabels && delta == 1.0;
294 //_____________________________________________________________________________
295 void AliAnalysisMuMuGraphUtil::PlotSameWithLegend(TObjArray& a,
296 Double_t ymin, Double_t ymax) const
298 // plot on same canvas
299 if (!gPad) new TCanvas;
301 Double_t xmin = TMath::Limits<Double_t>::Max();
302 Double_t xmax = TMath::Limits<Double_t>::Min();
307 while ( ( g = static_cast<TGraph*>(next())))
309 xmin = TMath::Min(xmin,g->GetX()[0]);
311 xmax = TMath::Max(xmax,g->GetX()[g->GetN()-1]);
315 TH2* hframe = new TH2F("hframe","hframe",100,xmin,xmax,100,ymin,ymax);
317 gStyle->SetOptTitle(0);
318 gStyle->SetOptStat(0);
320 hframe->GetXaxis()->SetNoExponent();
322 // if ( IsCompact(g1) )
324 // (*(hframe->GetXaxis()))=(*(g1.GetXaxis()));
329 if ( fShouldDrawPeriods )
335 while ( ( g = static_cast<TGraph*>(next())))
340 AliAnalysisTriggerScalers ts(runs,fOCDBPath);
342 ts.DrawPeriods(ymin,ymax,kGray);
344 hframe->Draw("axissame");
350 TLegend* l = new TLegend(0.5,0.7,0.9,0.9); // fixme: how to get the legend position/size ?
353 while ( ( g = static_cast<TGraph*>(next())))
356 g->Draw(fDrawOptions[i].c_str());
358 l->AddEntry(g,g->GetName(),fDrawOptions[0].c_str());
365 //_____________________________________________________________________________
366 TGraph* AliAnalysisMuMuGraphUtil::RelDif(TGraph& ga, TGraph& gb)
368 // compute the relative difference between two graphs
370 std::vector<double> vx;
371 std::vector<double> vxerr;
372 std::vector<double> vy;
373 std::vector<double> vyerr;
375 for ( Int_t i = 0; i < ga.GetN(); ++i )
377 Double_t xa,xb,ya,yb;
379 ga.GetPoint(i,xa,ya);
380 gb.GetPoint(i,xb,yb);
384 AliErrorClass(Form("Incompatible graphs : got xa=%e and xb=%e",xa,xb));
388 Double_t newvalue = 0.0;
390 if ( TMath::Abs(xa) > 1E-12 )
392 newvalue = 100.0*( yb - ya ) / ya;
397 if ( dynamic_cast<TGraphErrors*>(&ga) && dynamic_cast<TGraphErrors*>(&gb) )
399 yerr = newvalue*AliAnalysisMuMuResult::ErrorAB(ya,ga.GetEY()[i],
404 vxerr.push_back(0.5);
405 vy.push_back(newvalue);
406 vyerr.push_back(yerr);
410 return new TGraphErrors(vx.size(),&vx[0],&vy[0],&vxerr[0],&vyerr[0]);
413 //_____________________________________________________________________________
414 void AliAnalysisMuMuGraphUtil::StyleGraph(TGraph& g, UInt_t index) const
416 if ( index >= fAttFill.size() ) index = 0;
418 static_cast<TAttFill&>(g) = fAttFill[index];
419 static_cast<TAttLine&>(g) = fAttLine[index];
420 static_cast<TAttMarker&>(g) = fAttMarker[index];
422 g.GetYaxis()->SetLabelColor(fAttYaxis[index].GetLabelColor());
423 g.GetYaxis()->SetTitleColor(fAttYaxis[index].GetTitleColor());
425 //static_cast<TAttAxis&>((*g.GetYaxis())) = fAttYaxis[index];
428 //_____________________________________________________________________________
429 void AliAnalysisMuMuGraphUtil::UnCompact(TGraph& g)
431 /// Reverse operation of the Compact method
432 /// Only works if the labels of this graph represents run numbers. Otherwise
433 /// result is unpredictable ;-)
435 if ( !g.GetN() ) return;
437 // Int_t run1 = TString(g.GetXaxis()->GetBinLabel(1)).Atoi();
438 // Int_t run2 = TString(g.GetXaxis()->GetBinLabel(g.GetN())).Atoi();
440 std::vector<double> runs;
443 for ( Int_t i = 0; i < g.GetN(); ++i )
445 runNumber = TString(g.GetXaxis()->GetBinLabel(i+1)).Atoi();
446 runs.push_back(runNumber*1.0);
449 runs.push_back(runNumber+1);
451 g.GetXaxis()->Set(g.GetN(),&runs[0]);
453 for ( Int_t i = 0; i < g.GetN(); ++i )
455 g.SetPoint(i,runs[i],g.GetY()[i]);
458 g.GetXaxis()->SetNoExponent();