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 // PID Response class for the TRD detector
17 // Based on 1D Likelihood approach
18 // Calculation of probabilities using Bayesian approach
19 // Attention: This method is only used to separate electrons from pions
22 // Markus Fasel <M.Fasel@gsi.de>
23 // Anton Andronic <A.Andronic@gsi.de>
30 #include <TObjArray.h>
34 #include <TDirectory.h>
38 #include "AliTRDPIDResponse.h"
40 ClassImp(AliTRDPIDResponse)
42 const Double_t AliTRDPIDResponse::fgkPBins[kNPBins] = {1, 2, 3, 4, 5, 6};
44 //____________________________________________________________
45 AliTRDPIDResponse::AliTRDPIDResponse():
48 ,fGainNormalisationFactor(1.)
52 // Default constructor
54 for(Int_t ispec = 0; ispec < AliPID::kSPECIES; ispec++)
55 for(Int_t ipbin = 0; ipbin < kNPBins; ipbin++)
56 fMapRefHists[ispec][ipbin] = -1;
59 //____________________________________________________________
60 AliTRDPIDResponse::AliTRDPIDResponse(const AliTRDPIDResponse &ref):
62 ,fReferences(ref.fReferences)
63 ,fGainNormalisationFactor(ref.fGainNormalisationFactor)
64 ,fPIDmethod(ref.fPIDmethod)
68 // Flat copy of the reference histos
69 // For creating a deep copy call SetOwner
71 Int_t size = (AliPID::kSPECIES)*(kNPBins);
72 memcpy(fMapRefHists, ref.fMapRefHists, sizeof(Double_t) * size);
75 //____________________________________________________________
76 AliTRDPIDResponse &AliTRDPIDResponse::operator=(const AliTRDPIDResponse &ref){
78 // Assignment operator
79 // Performs a flat copy of the reference histos
81 if(this == &ref) return *this;
84 TObject::operator=(ref);
87 fReferences->Delete();
90 SetBit(kIsOwner, kFALSE);
91 } else if(fReferences) delete fReferences;
92 fReferences = ref.fReferences;
93 Int_t size = (AliPID::kSPECIES)*(kNPBins);
94 memcpy(fMapRefHists, ref.fMapRefHists, sizeof(Double_t) * size);
95 fPIDmethod = ref.fPIDmethod;
100 //____________________________________________________________
101 AliTRDPIDResponse::~AliTRDPIDResponse(){
107 if(fReferences) fReferences->Delete();
109 if(fReferences) delete fReferences;
112 //____________________________________________________________
113 Bool_t AliTRDPIDResponse::Load(const Char_t * filename){
115 // Load References into the toolkit
117 AliDebug(1, "Loading reference histos from root file");
118 TDirectory *owd = gDirectory;// store old working directory
121 filename = Form("%s/STEER/LQ1dRef_v1.root",gSystem->ExpandPathName("$ALICE_ROOT"));
122 TFile *in = TFile::Open(filename);
124 AliError("Ref file not available.");
129 fReferences = new TObjArray(AliPID::kSPECIES*kNPBins);
130 fReferences->SetOwner();
131 TIter iter(in->GetListOfKeys());
132 TKey *histkey = NULL;
134 Int_t arrayPos = 0, pbin = 0;
135 AliPID::EParticleType species;
138 while((histkey = dynamic_cast<TKey *>(iter()))){
139 tmp = histkey->ReadObj();
140 histname = tmp->GetName();
141 if(histname.BeginsWith("fHQel")){ // Electron histogram
142 histname.ReplaceAll("fHQel_p","");
143 species = AliPID::kElectron;
144 } else if(histname.BeginsWith("fHQmu")){ // Muon histogram
145 histname.ReplaceAll("fHQmu_p","");
146 species = AliPID::kMuon;
147 } else if(histname.BeginsWith("fHQpi")){ // Pion histogram
148 histname.ReplaceAll("fHQpi_p","");
149 species = AliPID::kPion;
150 }else if(histname.BeginsWith("fHQka")){ // Kaon histogram
151 histname.ReplaceAll("fHQka_p","");
152 species = AliPID::kKaon;
153 }else if(histname.BeginsWith("fHQpr")){ // Proton histogram
154 histname.ReplaceAll("fHQpr_p","");
155 species = AliPID::kProton;
157 pbin = histname.Atoi() - 1;
158 AliDebug(1, Form("Species %d, Histname %s, Pbin %d, Position in container %d", species, histname.Data(), pbin, arrayPos));
159 fMapRefHists[species][pbin] = arrayPos;
160 fReferences->AddAt((hnew =new TH1F(*dynamic_cast<TH1F *>(tmp))), arrayPos);
161 hnew->SetDirectory(gROOT);
166 AliDebug(2, Form("Successfully loaded %d Reference Histograms", arrayPos));
167 SetBit(kIsOwner, kTRUE);
172 //____________________________________________________________
173 Bool_t AliTRDPIDResponse::Load(const TObjArray *histos){
175 // Load Reference into the PID Response (from OCDB/OADB)
177 AliDebug(1, "Setting references (from the database)");
178 if(fReferences) fReferences->Clear();
180 fReferences = new TObjArray(AliPID::kSPECIES*kNPBins);
181 fReferences->SetOwner();
183 Int_t arrayPos = 0, pbin = 0;
184 AliPID::EParticleType species;
188 while((tmp = iter())){
189 histname = tmp->GetName();
190 if(histname.BeginsWith("fHQel")){ // Electron histogram
191 histname.ReplaceAll("fHQel_p","");
192 species = AliPID::kElectron;
193 } else if(histname.BeginsWith("fHQmu")){ // Muon histogram
194 histname.ReplaceAll("fHQmu_p","");
195 species = AliPID::kMuon;
196 } else if(histname.BeginsWith("fHQpi")){ // Pion histogram
197 histname.ReplaceAll("fHQpi_p","");
198 species = AliPID::kPion;
199 }else if(histname.BeginsWith("fHQka")){ // Kaon histogram
200 histname.ReplaceAll("fHQka_p","");
201 species = AliPID::kKaon;
202 }else if(histname.BeginsWith("fHQpr")){ // Proton histogram
203 histname.ReplaceAll("fHQpr_p","");
204 species = AliPID::kProton;
206 pbin = histname.Atoi() - 1;
207 AliDebug(1, Form("Species %d, Histname %s, Pbin %d, Position in container %d", species, histname.Data(), pbin, arrayPos));
208 fMapRefHists[species][pbin] = arrayPos;
209 fReferences->AddAt((hnew = new TH1F(*dynamic_cast<TH1F *>(tmp))), arrayPos);
210 hnew->SetDirectory(gROOT);
214 AliError("Failed loading References");
217 AliDebug(2, Form("Successfully loaded %d Reference Histograms", arrayPos));
218 SetBit(kIsOwner, kTRUE);
222 //____________________________________________________________
223 Bool_t AliTRDPIDResponse::GetResponse(Int_t n, Double_t *dedx, Float_t *p, Double_t prob[AliPID::kSPECIES], Bool_t kNorm) const
226 // Calculate TRD likelihood values for the track based on dedx and
227 // momentum values. The likelihoods are calculated by query the
228 // reference data depending on the PID method selected
231 // n - number of dedx slices/chamber
232 // dedx - array of dedx slices organized layer wise
233 // p - array of momentum measurements organized layer wise
236 // prob - probabilities allocated by TRD for particle specis
237 // kNorm - switch to normalize probabilities to 1. By default true. If false return not normalized prob.
240 // true if calculation success
244 AliWarning("Missing reference data. PID calculation not possible.");
248 for(Int_t is(AliPID::kSPECIES); is--;) prob[is]=.2;
249 Double_t prLayer[AliPID::kSPECIES];
250 Double_t DE[10], s(0.);
251 for(Int_t il(kNlayer); il--;){
252 memset(prLayer, 0, AliPID::kSPECIES*sizeof(Double_t));
253 if(!CookdEdx(n, &dedx[il*n], &DE[0])) continue;
256 for(Int_t is(AliPID::kSPECIES); is--;){
257 if((DE[0] > 0.) && (p[il] > 0.)) prLayer[is] = GetProbabilitySingleLayer(is, p[il], DE[0]);
258 AliDebug(3, Form("Probability for Species %d in Layer %d: %f", is, il, prLayer[is]));
262 AliDebug(2, Form("Null all species prob layer[%d].", il));
265 for(Int_t is(AliPID::kSPECIES); is--;){
266 if(kNorm) prLayer[is] /= s;
267 prob[is] *= prLayer[is];
270 if(!kNorm) return kTRUE;
273 for(Int_t is(AliPID::kSPECIES); is--;) s+=prob[is];
275 AliDebug(2, "Null total prob.");
278 for(Int_t is(AliPID::kSPECIES); is--;) prob[is]/=s;
282 //____________________________________________________________
283 Double_t AliTRDPIDResponse::GetProbabilitySingleLayer(Int_t species, Double_t plocal, Double_t dEdx) const {
285 // Get the non-normalized probability for a certain particle species as coming
286 // from the reference histogram
287 // Interpolation between momentum bins
289 AliDebug(1, Form("Make Probability for Species %d with Momentum %f", species, plocal));
290 Int_t pbin = GetLowerMomentumBin(plocal);
291 AliDebug(1, Form("Bin %d", pbin));
292 Double_t pLayer = 0.;
293 // Do Interpolation exept for underflow and overflow
294 if(pbin >= 0 && pbin < kNPBins-1){
295 TH1 *refHistUpper = NULL, *refHistLower = NULL;
296 if(fMapRefHists[species][pbin] >= 0)
297 refHistLower = dynamic_cast<TH1 *>(fReferences->UncheckedAt(fMapRefHists[species][pbin]));
298 if(fMapRefHists[species][pbin+1] >= 0)
299 refHistUpper = dynamic_cast<TH1 *>(fReferences->UncheckedAt(fMapRefHists[species][pbin+1]));
300 AliDebug(1, Form("Reference Histos (Upper/Lower): [%p|%p]", refHistUpper, refHistLower));
302 if (refHistLower && refHistUpper ) {
303 Double_t pLower = refHistLower->GetBinContent(refHistLower->GetXaxis()->FindBin(dEdx));
304 Double_t pUpper = refHistUpper->GetBinContent(refHistUpper->GetXaxis()->FindBin(dEdx));
306 pLayer = pLower + (pUpper - pLower)/(fgkPBins[pbin+1]-fgkPBins[pbin]) * (plocal - fgkPBins[pbin]);
313 if(fMapRefHists[species][0] >= 0){
314 refHist = dynamic_cast<TH1 *>(fReferences->UncheckedAt(fMapRefHists[species][0]));
318 if(fMapRefHists[species][kNPBins-1] > 0)
319 refHist = dynamic_cast<TH1 *>(fReferences->UncheckedAt(fMapRefHists[species][kNPBins-1]));
322 pLayer = refHist->GetBinContent(refHist->GetXaxis()->FindBin(dEdx));
324 AliDebug(1, Form("Probability %f", pLayer));
328 //____________________________________________________________
329 Int_t AliTRDPIDResponse::GetLowerMomentumBin(Double_t p) const {
331 // Get the momentum bin for a given momentum value
334 if(p > fgkPBins[kNPBins-1]) return kNPBins-1;
335 for(Int_t ibin = 0; ibin < kNPBins - 1; ibin++){
336 if(p >= fgkPBins[ibin] && p < fgkPBins[ibin+1]){
344 //____________________________________________________________
345 void AliTRDPIDResponse::SetOwner(){
347 // Make Deep Copy of the Reference Histograms
349 if(!fReferences || IsOwner()) return;
350 TObjArray *ctmp = new TObjArray();
351 for(Int_t ien = 0; ien < fReferences->GetEntriesFast(); ien++)
352 ctmp->AddAt(fReferences->UncheckedAt(ien)->Clone(), ien);
355 SetBit(kIsOwner, kTRUE);
358 //____________________________________________________________
359 Bool_t AliTRDPIDResponse::CookdEdx(Int_t nSlice, Double_t *in, Double_t *out) const
368 for(Int_t islice = 0; islice < nSlice; islice++) out[0] += in[islice] * fGainNormalisationFactor;
369 if(out[0] < 1e-6) return kFALSE;