1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
16 // Toolkit containing various usefull things
17 // Usable everywhere in the hfe software package
18 // For more information see the cxx file
21 // All authors of the HFE group
25 #include <TParticle.h>
31 #include "TGraphErrors.h"
32 #include "TGraphAsymmErrors.h"
33 #include "THnSparse.h"
41 #include "AliAODMCParticle.h"
42 #include "AliAODpidUtil.h"
43 #include "AliESDpid.h"
45 #include "AliTPCPIDResponse.h"
46 #include "AliTOFPIDResponse.h"
48 #include "AliHFEtools.h"
52 AliPIDResponse *AliHFEtools::fgDefaultPID = NULL;
53 Int_t AliHFEtools::fgLogLevel = 0;
55 //__________________________________________
56 AliHFEtools::AliHFEtools():
61 //__________________________________________
62 Double_t *AliHFEtools::MakeLinearBinning(Int_t nBins, Double_t ymin, Double_t ymax){
64 // Helper function for linearly binned array
66 Double_t *bins = new Double_t[nBins + 1];
67 Double_t stepsize = (ymax - ymin) / static_cast<Double_t>(nBins);
69 for(Int_t ibin = 1; ibin <= nBins; ibin++)
70 bins[ibin] = bins[ibin-1] + stepsize;
74 //__________________________________________
75 void AliHFEtools::FillLinearBinning(TArrayD &bins, Int_t nBins, Double_t ymin, Double_t ymax){
77 // Helper function for linearly binned array
79 Double_t stepsize = (ymax - ymin) / static_cast<Double_t>(nBins);
81 for(Int_t ibin = 1; ibin <= nBins; ibin++)
82 bins[ibin] = bins[ibin-1] + stepsize;
85 //__________________________________________
86 Double_t *AliHFEtools::MakeLogarithmicBinning(Int_t nBins, Double_t ymin, Double_t ymax){
88 // Helper function for logartimically binned array
90 Double_t *bins = new Double_t[nBins+1];
92 Double_t factor = TMath::Power(ymax/ymin, 1./nBins);
93 for(Int_t ibin = 1; ibin <= nBins; ibin++){
94 bins[ibin] = factor * bins[ibin-1];
99 //__________________________________________
100 void AliHFEtools::FillLogarithmicBinning(TArrayD &bins, Int_t nBins, Double_t ymin, Double_t ymax){
102 // Helper function for logartimically binned array
105 Double_t factor = TMath::Power(ymax/ymin, 1./nBins);
106 for(Int_t ibin = 1; ibin <= nBins; ibin++)
107 bins[ibin] = factor * bins[ibin-1];
110 //_________________________________________
111 Bool_t AliHFEtools::BinLogAxis(TObject *o, Int_t dim){
114 // converts the axis (defined by the dimension) of THx or THnSparse
115 // object to Log scale. Number of bins and bin min and bin max are preserved
120 AliError("Input histogram is null pointer");
125 if(o->InheritsFrom("TH1")){
126 TH1 *h1 = dynamic_cast<TH1F*>(o);
127 if(h1) axis = h1->GetXaxis();
129 else if(o->InheritsFrom("TH2")){
130 TH2 *h2 = dynamic_cast<TH2F*>(o);
132 axis = h2->GetXaxis();
134 else if(h2 && 1 == dim){
135 axis = h2->GetYaxis();
138 AliError("Only dim = 0 or 1 possible for TH2F");
141 else if(o->InheritsFrom("THnSparse")){
142 THnSparse *hs = dynamic_cast<THnSparse*>(o);
143 if(hs) axis = hs->GetAxis(dim);
146 AliError("Type of input object not recognized, please check your code or update this finction");
150 AliError(Form("Axis '%d' could not be identified in the object \n", dim));
154 Int_t bins = axis->GetNbins();
156 Double_t from = axis->GetXmin();
158 AliError(Form("Log binning not possible for this axis [min = %f]\n", from));
160 Double_t to = axis->GetXmax();
161 TArrayD newBins(bins+1);
163 Double_t factor = TMath::Power(to/from, 1./bins);
164 for(Int_t i=1; i<=bins; ++i){
165 newBins[i] = factor * newBins[i-1];
167 axis->Set(bins, newBins.GetArray());
172 //__________________________________________
173 Float_t AliHFEtools::GetRapidity(const TParticle *part){
178 if(!((part->Energy() - part->Pz())*(part->Energy() + part->Pz())>0)) rapidity=-999;
179 else rapidity = 0.5*(TMath::Log((part->Energy()+part->Pz()) / (part->Energy()-part->Pz())));
183 //__________________________________________
184 Float_t AliHFEtools::GetRapidity(const AliAODMCParticle *part){
188 if(!((part->E() - part->Pz())*(part->E() + part->Pz())>0)) rapidity=-999;
189 else rapidity = 0.5*(TMath::Log((part->E()+part->Pz()) / (part->E()-part->Pz())));
193 //__________________________________________
194 AliPIDResponse* AliHFEtools::GetDefaultPID(Bool_t isMC, Bool_t isESD){
196 // Get the default PID as singleton instance
200 if(isESD) fgDefaultPID = new AliESDpid;
201 else fgDefaultPID = new AliAODpidUtil;
202 Double_t tres = isMC ? 80. : 130.;
203 fgDefaultPID->GetTOFResponse().SetTimeResolution(tres);
205 // TPC Bethe Bloch parameters
206 Double_t alephParameters[5];
209 alephParameters[0] = 2.15898e+00/50.;
210 alephParameters[1] = 1.75295e+01;
211 alephParameters[2] = 3.40030e-09;
212 alephParameters[3] = 1.96178e+00;
213 alephParameters[4] = 3.91720e+00;
215 alephParameters[0] = 0.0283086/0.97;
216 //alephParameters[0] = 0.0283086;
217 alephParameters[1] = 2.63394e+01;
218 alephParameters[2] = 5.04114e-11;
219 alephParameters[3] = 2.12543e+00;
220 alephParameters[4] = 4.88663e+00;
222 fgDefaultPID->GetTPCResponse().SetBetheBlochParameters(alephParameters[0],alephParameters[1],alephParameters[2], alephParameters[3],alephParameters[4]);
223 fgDefaultPID->GetTPCResponse().SetSigma(3.79301e-03, 2.21280e+04);
227 printf("Error - You are using the default PID: You should use the PID coming from the tender\n");
228 printf("Error - Arrrrrrrrr...\n");
229 printf("Error - Please rethink your program logic. Using default PID is really dangerous\n");
230 printf("Error - TOF PID is adapted to Monte Carlo\n");
236 //__________________________________________
237 void AliHFEtools::DestroyDefaultPID(){
239 // Destroy default PID object if existing
241 if(fgDefaultPID) delete fgDefaultPID;
245 //__________________________________________
246 Int_t AliHFEtools::GetPdg(const AliVParticle *track){
248 // Get MC PDG code (only MC particles supported)
251 if(!TString(track->IsA()->GetName()).CompareTo("AliMCParticle")){
252 const AliMCParticle *mctrack = dynamic_cast<const AliMCParticle *>(track);
253 pdg = mctrack ? mctrack->Particle()->GetPdgCode() : 0;
254 } else if(!TString(track->IsA()->GetName()).CompareTo("AliAODMCParticle")){
255 const AliAODMCParticle *aodmctrack = dynamic_cast<const AliAODMCParticle *>(track);
256 pdg = aodmctrack ? aodmctrack->GetPdgCode() : 0;
261 //__________________________________________
262 Int_t AliHFEtools::PDG2AliPID(Int_t pdg){
264 // Helper function to convert MC PID codes into AliPID codes
267 switch(TMath::Abs(pdg)){
268 case 11: pid = AliPID::kElectron; break;
269 case 13: pid = AliPID::kMuon; break;
270 case 211: pid = AliPID::kPion; break;
271 case 321: pid = AliPID::kKaon; break;
272 case 2212: pid = AliPID::kProton; break;
273 default: pid = -1; break;
278 //__________________________________________
279 TH1D* AliHFEtools::GraphErrorsToHist(TGraphErrors* g, Double_t firstBinWidth, Bool_t exchange, Int_t markerstyle,Int_t markercolor,Float_t markersize)
281 // Creates a TH1D from TGraph g. The binwidth of the first bin has to
282 // specified. The others bins are calculated automatically. Supports also Graphs
283 // with non constant x steps. The axis of the Graph can be exchanged if
284 // exchange=kTRUE (modifies the Graph).
286 TH1D* result = GraphToHist(g,firstBinWidth,exchange, markerstyle,markercolor,markersize);
287 if( result == 0) return result;
289 //------------------------------------------
290 // setup the final hist
291 Int_t nBinX = g->GetN();
292 Double_t* err = g->GetEY(); // error y is still ok even if exchanged
293 for(Int_t i = 0; i < nBinX; i ++){
294 result->SetBinError(i + 1, err[i]);
297 AliHFEtools::ExchangeXYGraph(g); // undo what has been done in GraphToHist
298 AliHFEtools::ExchangeXYGraphErrors(g); // now exchange including errors
304 //__________________________________________
305 Bool_t AliHFEtools::ExchangeXYGraph(TGraph* g)
307 // exchanges x-values and y-values.
308 if(g==0) return kFALSE;
309 Int_t nbin=g->GetN();
311 for(Int_t i = 0; i < nbin; i ++)
320 //__________________________________________
321 Bool_t AliHFEtools::ExchangeXYGraphErrors(TGraphErrors* g)
323 // exchanges x-values and y-values and
324 // corresponding errors.
325 if(g==0) return kFALSE;
326 Int_t nbin=g->GetN();
329 for(Int_t i = 0; i < nbin; i ++)
332 ex = g->GetErrorX(i);
333 ey = g->GetErrorY(i);
335 g->SetPointError(i,ey,ex);
342 //__________________________________________
343 TH1D* AliHFEtools::GraphToHist(TGraph* g, Double_t firstBinWidth, Bool_t exchange, Int_t markerstyle,Int_t markercolor,Float_t markersize)
345 // Creates a TH1D from TGraph g. The binwidth of the first bin has to
346 // specified. The others bins are calculated automatically. Supports also Graphs
347 // with non constant x steps. The axis of the Graph can be exchanged if
348 // exchange=kTRUE (modifies the Graph).
352 if(g == 0) return result;
353 if(firstBinWidth == -1) return result;
355 myname = g->GetName();
357 if(exchange) AliHFEtools::ExchangeXYGraph(g);
359 Int_t nBinX = g->GetN();
360 Double_t* x = g->GetX();
361 Double_t* y = g->GetY();
363 if(nBinX < 1) return result;
365 //------------------------------------------
366 // create the Matrix for the equation system
367 // and init the values
369 Int_t nDim = nBinX - 1;
370 TMatrixD a(nDim,nDim);
373 Double_t* aA = a.GetMatrixArray();
374 Double_t* aB = b.GetMatrixArray();
375 memset(aA,0,nDim * nDim * sizeof(Double_t));
376 memset(aB,0,nDim * sizeof(Double_t));
377 //------------------------------------------
379 //------------------------------------------
380 // setup equation system
381 // width for 1st bin is given therefore
382 // we shift bin parameter (column) by one to the left
383 // to reduce the matrix size
385 Double_t* xAxis = new Double_t [nBinX + 1];
386 Double_t* binW = new Double_t [nBinX ];
387 binW[0] = firstBinWidth;
389 aB[0] = x[1] - x[0] - 0.5 * binW[0];
392 for(Int_t col = 1; col < nDim ; col ++)
395 aB[col] = x[col + 1] - x[ col ];
396 aA[row * nDim + col - 1 ] = 0.5;
397 aA[row * nDim + col ] = 0.5;
399 //------------------------------------------
401 //------------------------------------------
402 // solve the equations
405 //------------------------------------------
407 //------------------------------------------
408 // calculate the bin boundaries
409 xAxis[0] = x[0] - 0.5 * binW[0];
410 memcpy(&binW[1],c.GetMatrixArray(),nDim * sizeof(Double_t));
411 for(Int_t col = 0; col < nBinX ; col ++) {
412 xAxis[col + 1] = x[col] + 0.5 * binW[col];
414 //------------------------------------------
416 //------------------------------------------
417 // setup the final hist
418 result = new TH1D(myname.Data(),myname.Data(),nBinX, xAxis);
419 for(Int_t i = 0; i < nBinX; i ++){
420 result->SetBinContent(i + 1, y[i]);
422 result->SetMarkerColor(markercolor);
423 result->SetMarkerStyle(markerstyle);
424 result->SetMarkerSize(markersize);
425 //------------------------------------------
434 //__________________________________________
435 void AliHFEtools::BinParameterisation(const TF1 &fun, const TArrayD &xbins, TArrayD &bincontent){
437 // Calculate binned version of a function defined as the integral of x*f(x) in
438 // the integration range xmin,xmax, where xmin and xmax are the bin limits, divided
439 // by the binwidth. The function is important in case of steeply falling functions
442 // fun: the function to be binned
443 // xbins: the bin limits
444 // bincontent: the binned parameterisation
446 TString expression(Form("x*%s", fun.GetName()));
447 Double_t xmin(0), xmax(0);
448 fun.GetRange(xmin,xmax);
450 xmin = TMath::Min(xmin, xbins[0]);
451 xmax = TMath::Max(xmax, xbins[xbins.GetSize()-1]);
452 TF1 helper("helper",expression.Data(),xmin,xmax); // make function x*f(x)
453 if(bincontent.GetSize() != xbins.GetSize()-1)
454 bincontent.Set(xbins.GetSize()-1); // Adapt array to number of bins
456 for(Int_t ib = 0; ib < xbins.GetSize()-1; ib++){
459 bincontent[ib] = (helper.Integral(xmin, xmax))/(xmax - xmin);
466 //_________________________________________________________________________
467 //Function AliHFEtools::GetHFEResultList() - opens file from argument and returns TList Object containing String "Results"
468 //_________________________________________________________________________
469 TList *AliHFEtools::GetHFEResultList(const TString str){
471 TFile *f = TFile::Open(str.Data());
472 if(!f || f->IsZombie()){
473 printf("Could not read file %s\n",str.Data());
478 TIter next(f->GetListOfKeys());
479 while ((k = dynamic_cast<TKey *>(next()))){
480 TString s(k->GetName());
481 if(s.Contains("Results")) break;
484 printf("Output container not found\n");
485 f->Close(); delete f;
488 TList *returnlist = dynamic_cast<TList *>(k->ReadObj());
489 f->Close(); delete f;
494 //_________________________________________________________________________
495 //Function AliHFEtools::GetHFEQAList() - opens file from argument and returns TList Object containing String "QA"
496 //_________________________________________________________________________
497 TList *AliHFEtools::GetHFEQAList(const TString str){
499 TFile *f = TFile::Open(str.Data());
500 if(!f || f->IsZombie()){
501 printf("Could not read file %s\n",str.Data());
506 TIter next(f->GetListOfKeys());
507 while ((k = dynamic_cast<TKey *>(next()))){
508 TString s(k->GetName());
509 if(s.Contains("QA")) break;
512 printf("Output container not found\n");
513 f->Close(); delete f;
516 TList *returnlist = dynamic_cast<TList *>(k->ReadObj());
517 f->Close(); delete f;
521 //__________________________________________
522 void AliHFEtools::NormaliseBinWidth(TH1 *histo){
524 // Helper function to correct histograms for the bin width
526 Double_t binwidth(0.);
527 for(Int_t ipt = 1; ipt <= histo->GetNbinsX(); ipt++){
528 binwidth = histo->GetBinWidth(ipt);
529 histo->SetBinContent(ipt, histo->GetBinContent(ipt)/binwidth);
530 histo->SetBinError(ipt, histo->GetBinError(ipt)/binwidth);
534 //__________________________________________
535 void AliHFEtools::NormaliseBinWdith(TGraphErrors *graph){
537 // Helper function to correct graphs with symmetric errors
540 Double_t binwidth(0.);
541 Double_t *ypoints = graph->GetY(),
542 *yerrors = graph->GetEY();
543 for(int ipt = 0; ipt < graph->GetN(); ipt++){
544 binwidth = 2*graph->GetEX()[ipt];
545 ypoints[ipt] /= binwidth;
546 yerrors[ipt] /= binwidth;
550 //__________________________________________
551 void AliHFEtools::NormaliseBinWdithAsymm(TGraphAsymmErrors *graph){
553 // Helper function to correct graphs with asymmetric errors
556 Double_t binwidth(0.);
557 Double_t *ypoints = graph->GetY(),
558 *yerrorslow = graph->GetEYlow(),
559 *yerrorshigh = graph->GetEYhigh();
560 for(int ipt = 0; ipt < graph->GetN(); ipt++){
561 binwidth = graph->GetEXlow()[ipt] + graph->GetEXhigh()[ipt];
562 ypoints[ipt] /= binwidth;
563 yerrorslow[ipt] /= binwidth;
564 yerrorshigh[ipt] /= binwidth;