Major update of the HFE package (comments inside the code
[u/mrichter/AliRoot.git] / PWG3 / hfe / AliHFEtools.cxx
CommitLineData
70da6c5a 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// Toolkit containing various usefull things
17// Usable everywhere in the hfe software package
18// For more information see the cxx file
19//
20// Authors
21// All authors of the HFE group
22//
23#include <TMath.h>
24#include <TParticle.h>
25#include "TH1.h"
26#include "TH2.h"
27#include "THnSparse.h"
28#include "TAxis.h"
29
30#include "AliAODMCParticle.h"
3a72645a 31#include "AliAODpidUtil.h"
faee3b18 32#include "AliESDpid.h"
70da6c5a 33#include "AliLog.h"
3a72645a 34#include "AliTPCPIDResponse.h"
faee3b18 35#include "AliTOFPIDResponse.h"
70da6c5a 36
37#include "AliHFEtools.h"
38
39ClassImp(AliHFEtools)
40
faee3b18 41AliESDpid *AliHFEtools::fgDefaultPID = NULL;
3a72645a 42AliAODpidUtil *AliHFEtools::fgDefaultPIDaod = NULL;
69ac0e6f 43Int_t AliHFEtools::fgLogLevel = 0;
faee3b18 44
70da6c5a 45//__________________________________________
46AliHFEtools::AliHFEtools():
47 TObject()
48{
49}
50
51//__________________________________________
52Double_t *AliHFEtools::MakeLinearBinning(Int_t nBins, Double_t ymin, Double_t ymax){
53 //
54 // Helper function for linearly binned array
55 //
56 Double_t *bins = new Double_t[nBins + 1];
57 Double_t stepsize = (ymax - ymin) / static_cast<Double_t>(nBins);
58 bins[0] = ymin;
59 for(Int_t ibin = 1; ibin <= nBins; ibin++)
60 bins[ibin] = bins[ibin-1] + stepsize;
61 return bins;
62}
63
64//__________________________________________
65Double_t *AliHFEtools::MakeLogarithmicBinning(Int_t nBins, Double_t ymin, Double_t ymax){
66 //
67 // Helper function for logartimically binned array
68 //
69 Double_t *bins = new Double_t[nBins+1];
70 bins[0] = ymin;
71 Double_t factor = TMath::Power(ymax/ymin, 1./nBins);
72 for(Int_t ibin = 1; ibin <= nBins; ibin++){
73 bins[ibin] = factor * bins[ibin-1];
74 }
75 return bins;
76}
77
78//_________________________________________
79Bool_t AliHFEtools::BinLogAxis(TObject *o, Int_t dim){
80
81 //
82 // converts the axis (defined by the dimension) of THx or THnSparse
83 // object to Log scale. Number of bins and bin min and bin max are preserved
84 //
85
86
87 if(!o){
88 AliError("Input histogram is null pointer");
89 return kFALSE;
90 }
91
92 TAxis *axis = 0x0;
93 if(o->InheritsFrom("TH1")){
94 axis = (dynamic_cast<TH1F*>(o))->GetXaxis();
95 }
96 else if(o->InheritsFrom("TH2")){
97 if(0 == dim){
98 axis = (dynamic_cast<TH2F*>(o))->GetXaxis();
99 }
100 else if(1 == dim){
101 axis = (dynamic_cast<TH2F*>(o))->GetYaxis();
102 }
103 else{
104 AliError("Only dim = 0 or 1 possible for TH2F");
105 }
106 }
107 else if(o->InheritsFrom("THnSparse")){
108 axis = (dynamic_cast<THnSparse*>(o))->GetAxis(dim);
109 }
110 else{
111 AliError("Type of input object not recognized, please check your code or update this finction");
112 return kFALSE;
113 }
114 if(!axis){
115 AliError(Form("Axis '%d' could not be identified in the object \n", dim));
116 return kFALSE;
117 }
118
119 Int_t bins = axis->GetNbins();
120
121 Double_t from = axis->GetXmin();
122 if(from <= 0){
123 AliError(Form("Log binning not possible for this axis [min = %f]\n", from));
124 }
125 Double_t to = axis->GetXmax();
126 Double_t *newBins = new Double_t[bins+1];
127 newBins[0] = from;
128 Double_t factor = TMath::Power(to/from, 1./bins);
129 for(Int_t i=1; i<=bins; ++i){
130 newBins[i] = factor * newBins[i-1];
131 }
132 axis->Set(bins, newBins);
e3fc062d 133 delete newBins;
70da6c5a 134
135 return kTRUE;
136}
137
138//__________________________________________
3a72645a 139Float_t AliHFEtools::GetRapidity(const TParticle *part){
70da6c5a 140 //
141 // return rapidity
142 //
143 Float_t rapidity;
144 if(!((part->Energy() - part->Pz())*(part->Energy() + part->Pz())>0)) rapidity=-999;
145 else rapidity = 0.5*(TMath::Log((part->Energy()+part->Pz()) / (part->Energy()-part->Pz())));
146 return rapidity;
147}
148
149//__________________________________________
3a72645a 150Float_t AliHFEtools::GetRapidity(const AliAODMCParticle *part){
70da6c5a 151 // return rapidity
152
153 Float_t rapidity;
154 if(!((part->E() - part->Pz())*(part->E() + part->Pz())>0)) rapidity=-999;
155 else rapidity = 0.5*(TMath::Log((part->E()+part->Pz()) / (part->E()-part->Pz())));
156 return rapidity;
157}
158
faee3b18 159//__________________________________________
160AliESDpid* AliHFEtools::GetDefaultPID(Bool_t isMC){
161 //
162 // Get the default PID as singleton instance
163 //
164 if(!fgDefaultPID){
165 fgDefaultPID = new AliESDpid;
166 Double_t tres = isMC ? 80. : 130.;
167 fgDefaultPID->GetTOFResponse().SetTimeResolution(tres);
168
169 // TPC Bethe Bloch parameters
170 Double_t alephParameters[5];
171 if(isMC){
172 // simulation
173 alephParameters[0] = 2.15898e+00/50.;
174 alephParameters[1] = 1.75295e+01;
175 alephParameters[2] = 3.40030e-09;
176 alephParameters[3] = 1.96178e+00;
177 alephParameters[4] = 3.91720e+00;
178 } else {
179 alephParameters[0] = 0.0283086/0.97;
180 //alephParameters[0] = 0.0283086;
181 alephParameters[1] = 2.63394e+01;
182 alephParameters[2] = 5.04114e-11;
183 alephParameters[3] = 2.12543e+00;
184 alephParameters[4] = 4.88663e+00;
185 }
186 fgDefaultPID->GetTPCResponse().SetBetheBlochParameters(alephParameters[0],alephParameters[1],alephParameters[2], alephParameters[3],alephParameters[4]);
67fe7bd0 187 fgDefaultPID->GetTPCResponse().SetSigma(3.79301e-03, 2.21280e+04);
faee3b18 188
189 }
190 if(fgLogLevel){
67fe7bd0 191 printf("Error - You are using the default PID: You should use the PID coming from the tender\n");
192 printf("Error - Arrrrrrrrr...\n");
193 printf("Error - Please rethink your program logic. Using default PID is really dangerous\n");
194 printf("Error - TOF PID is adapted to Monte Carlo\n");
faee3b18 195 }
196 return fgDefaultPID;
197}
198
199//__________________________________________
3a72645a 200AliAODpidUtil* AliHFEtools::GetDefaultAODPID(Bool_t isMC){
201 //
202 // Get the default PID as singleton instance
203 //
204 if(!fgDefaultPID){
205 fgDefaultPIDaod = new AliAODpidUtil;
206 Double_t tres = isMC ? 80. : 130.;
207 fgDefaultPIDaod->GetTOFResponse().SetTimeResolution(tres);
208
209 // TPC Bethe Bloch parameters
210 Double_t alephParameters[5];
211 if(isMC){
212 // simulation
213 alephParameters[0] = 2.15898e+00/50.;
214 alephParameters[1] = 1.75295e+01;
215 alephParameters[2] = 3.40030e-09;
216 alephParameters[3] = 1.96178e+00;
217 alephParameters[4] = 3.91720e+00;
218 } else {
219 alephParameters[0] = 0.0283086/0.97;
220 //alephParameters[0] = 0.0283086;
221 alephParameters[1] = 2.63394e+01;
222 alephParameters[2] = 5.04114e-11;
223 alephParameters[3] = 2.12543e+00;
224 alephParameters[4] = 4.88663e+00;
225 }
226 fgDefaultPIDaod->GetTPCResponse().SetBetheBlochParameters(alephParameters[0],alephParameters[1],alephParameters[2], alephParameters[3],alephParameters[4]);
227 fgDefaultPIDaod->GetTPCResponse().SetSigma(3.79301e-03, 2.21280e+04);
228 }
229 if(fgLogLevel){
230 printf("Error - You are using the default PID: You should use the PID coming from the tender\n");
231 printf("Error - Arrrrrrrrr...\n");
232 printf("Error - Please rethink your program logic. Using default PID is really dangerous\n");
233 printf("Error - TOF PID is adapted to Monte Carlo\n");
234 }
235 return fgDefaultPIDaod;
236}
237
238//__________________________________________
faee3b18 239void AliHFEtools::DestroyDefaultPID(){
240 //
241 // Destroy default PID object if existing
242 //
243 if(fgDefaultPID) delete fgDefaultPID;
244 fgDefaultPID = NULL;
3a72645a 245 if(fgDefaultPIDaod) delete fgDefaultPIDaod;
246 fgDefaultPIDaod = NULL;
247}
248
249//__________________________________________
250Int_t AliHFEtools::GetPdg(const AliVParticle *track){
251 //
252 // Get MC PDG code (only MC particles supported)
253 //
254 Int_t pdg = 0;
255 if(!TString(track->IsA()->GetName()).CompareTo("AliMCParticle")){
256 const AliMCParticle *mctrack = dynamic_cast<const AliMCParticle *>(track);
257 pdg = mctrack->Particle()->GetPdgCode();
258 } else if(!TString(track->IsA()->GetName()).CompareTo("AliAODMCParticle")){
259 const AliAODMCParticle *aodmctrack = dynamic_cast<const AliAODMCParticle *>(track);
260 pdg = aodmctrack->GetPdgCode();
261 }
262 return pdg;
263}
264
265//__________________________________________
266Int_t AliHFEtools::PDG2AliPID(Int_t pdg){
267 //
268 // Helper function to convert MC PID codes into AliPID codes
269 //
270 Int_t pid = -1;
271 switch(TMath::Abs(pdg)){
272 case 11: pid = AliPID::kElectron; break;
273 case 13: pid = AliPID::kMuon; break;
274 case 211: pid = AliPID::kPion; break;
275 case 321: pid = AliPID::kKaon; break;
276 case 2212: pid = AliPID::kProton; break;
277 default: pid = -1; break;
278 };
279 return pid;
faee3b18 280}