]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGHF/hfe/AliHFEpidTRD.cxx
Updating for p-A beam type
[u/mrichter/AliRoot.git] / PWGHF / hfe / AliHFEpidTRD.cxx
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 // Class for TRD PID
17 // Implements the abstract base class AliHFEpidbase 
18 // Make PID does the PID decision 
19 // Class further contains TRD specific cuts and QA histograms 
20 //  
21 // Authors: 
22 //   Markus Fasel <M.Fasel@gsi.de>  
23 // 
24 #include <TH1F.h>
25 #include <TH2F.h>
26 #include <THnSparse.h>
27 #include <TList.h>
28 #include <TString.h>
29
30 #include "AliAODPid.h"
31 #include "AliAODTrack.h"
32 #include "AliAODMCParticle.h"
33 #include "AliESDtrack.h"
34 #include "AliLog.h"
35 #include "AliMCParticle.h"
36 #include "AliOADBContainer.h"
37 #include "AliPID.h"
38 #include "AliPIDResponse.h"
39
40 #include "AliHFEOADBThresholdsTRD.h"
41 #include "AliHFEpidQAmanager.h"
42 #include "AliHFEpidTRD.h"
43
44 ClassImp(AliHFEpidTRD)
45
46 //___________________________________________________________________
47 AliHFEpidTRD::AliHFEpidTRD() :
48     AliHFEpidBase()
49   , fOADBThresholds(NULL)
50   , fMinP(0.5)
51   , fNTracklets(6)
52   , fCutNTracklets(0)
53   , fRunNumber(0)
54   , fElectronEfficiency(0.90)
55   , fTotalChargeInSlice0(kFALSE)
56 {
57   //
58   // default  constructor
59   // 
60   memset(fThreshParams, 0, sizeof(Double_t) * kThreshParams);
61 }
62
63 //___________________________________________________________________
64 AliHFEpidTRD::AliHFEpidTRD(const char* name) :
65     AliHFEpidBase(name)
66   , fOADBThresholds(NULL)
67   , fMinP(0.5)
68   , fNTracklets(6)
69   , fCutNTracklets(0)
70   , fRunNumber(0)
71   , fElectronEfficiency(0.91)
72   , fTotalChargeInSlice0(kFALSE)
73 {
74   //
75   // default  constructor
76   // 
77   memset(fThreshParams, 0, sizeof(Double_t) * kThreshParams);
78 }
79
80 //___________________________________________________________________
81 AliHFEpidTRD::AliHFEpidTRD(const AliHFEpidTRD &ref):
82     AliHFEpidBase("")
83   , fOADBThresholds(NULL)
84   , fMinP(ref.fMinP)
85   , fNTracklets(ref.fNTracklets)
86   , fCutNTracklets(ref.fCutNTracklets)
87   , fRunNumber(ref.fRunNumber)
88   , fElectronEfficiency(ref.fElectronEfficiency)
89   , fTotalChargeInSlice0(ref.fTotalChargeInSlice0)
90 {
91   //
92   // Copy constructor
93   //
94   memset(fThreshParams, 0, sizeof(Double_t) * kThreshParams);
95   ref.Copy(*this);
96 }
97
98 //___________________________________________________________________
99 AliHFEpidTRD &AliHFEpidTRD::operator=(const AliHFEpidTRD &ref){
100   //
101   // Assignment operator
102   //
103   if(this != &ref){
104     ref.Copy(*this);
105   }
106   return *this;
107 }
108
109 //___________________________________________________________________
110 void AliHFEpidTRD::Copy(TObject &ref) const {
111   //
112   // Performs the copying of the object
113   //
114   AliHFEpidTRD &target = dynamic_cast<AliHFEpidTRD &>(ref);
115   
116   target.fMinP = fMinP;
117   target.fNTracklets = fNTracklets;
118   target.fCutNTracklets = fCutNTracklets;
119   target.fRunNumber = fRunNumber;
120   target.fTotalChargeInSlice0 = fTotalChargeInSlice0;
121   target.fElectronEfficiency = fElectronEfficiency;
122   memcpy(target.fThreshParams, fThreshParams, sizeof(Double_t) * kThreshParams);
123   AliHFEpidBase::Copy(ref);
124 }
125
126 //___________________________________________________________________
127 AliHFEpidTRD::~AliHFEpidTRD(){
128   //
129   // Destructor
130   //
131 }
132
133 //______________________________________________________
134 Bool_t AliHFEpidTRD::InitializePID(Int_t run){
135   //
136   // InitializePID: Load TRD thresholds and create the electron efficiency axis
137   // to navigate 
138   //
139   AliDebug(1, Form("Initializing TRD PID for run %d", run));
140   if(InitParamsFromOADB(run)){
141     SetBit(kThresholdsInitialized);
142     return kTRUE;
143   }
144   AliDebug(1, Form("Threshold Parameters for %d tracklets and an electron efficiency %f loaded:", fNTracklets, fElectronEfficiency));
145   AliDebug(1, Form("Params: [%f|%f|%f|%f]", fThreshParams[0], fThreshParams[1], fThreshParams[2], fThreshParams[3]));
146   fRunNumber = run;
147   return kFALSE;
148 }
149
150 //______________________________________________________
151 Int_t AliHFEpidTRD::IsSelected(const AliHFEpidObject *track, AliHFEpidQAmanager *pidqa) const {
152   //
153   // Does PID for TRD alone:
154   // PID thresholds based on 90% Electron Efficiency level approximated by a linear 
155   // step function
156   //
157   if(!TestBit(kThresholdsInitialized)) {
158     AliDebug(1,"Threshold Parameters not available");
159     return 0;
160   }
161   AliDebug(2, "Applying TRD PID");
162   if(!fkPIDResponse){
163     AliDebug(2, "Cannot process track");
164     return 0;
165   }
166
167
168 /*
169   const AliESDtrack *esdt = dynamic_cast<const AliESDtrack *>(track->GetRecTrack());
170   printf("checking IdentifiedAsElectronTRD, number of Tracklets: %d\n", esdt->GetTRDntrackletsPID());
171   if(fkPIDResponse->IdentifiedAsElectronTRD(dynamic_cast<const AliVTrack *>(track->GetRecTrack()), 0.8)) printf("Track identified as electron\n");
172   else printf("Track rejected\n");
173 */
174   AliHFEpidObject::AnalysisType_t anatype = track->IsESDanalysis() ? AliHFEpidObject::kESDanalysis: AliHFEpidObject::kAODanalysis;
175   Double_t p = GetP(track->GetRecTrack(), anatype);
176   if(p < fMinP){ 
177     AliDebug(2, Form("Track momentum below %f", fMinP));
178     return 0;
179   }
180
181   if(pidqa) pidqa->ProcessTrack(track, AliHFEpid::kTRDpid, AliHFEdetPIDqa::kBeforePID); 
182
183   if(fCutNTracklets > 0){
184     AliDebug(1, Form("Number of tracklets cut applied: %d\n", fCutNTracklets));
185     Int_t ntracklets = track->GetRecTrack() ? track->GetRecTrack()->GetTRDntrackletsPID() : 0;
186     if(TestBit(kExactTrackletCut)){
187       AliDebug(1, Form("Exact cut applied: %d tracklets found\n", ntracklets));
188       if(ntracklets != fCutNTracklets) return 0;
189     } else {
190       AliDebug(1, Form("Greater Equal cut applied: %d tracklets found\n", ntracklets));
191       if(ntracklets < fCutNTracklets) return 0;
192     }
193   }
194   AliDebug(1,"Track selected\n");
195
196   Double_t electronLike = GetElectronLikelihood(track->GetRecTrack(), anatype);
197   Double_t threshold;
198   if(TestBit(kSelectCutOnTheFly)){ 
199     threshold = GetTRDthresholds(p, track->GetRecTrack()->GetTRDntrackletsPID());
200   } else {
201     threshold = GetTRDthresholds(p);
202   }
203   AliDebug(2, Form("Threshold: %f\n", threshold));
204   if(electronLike > threshold){
205     if(pidqa) pidqa->ProcessTrack(track, AliHFEpid::kTRDpid, AliHFEdetPIDqa::kAfterPID);
206     return 11;
207   }
208   return 211;
209
210 }
211
212 //___________________________________________________________________
213 Double_t AliHFEpidTRD::GetTRDthresholds(Double_t p, UInt_t nTracklets) const { 
214   //
215   // Return momentum dependent and electron efficiency dependent TRD thresholds
216   // Determine threshold based on the number of tracklets on the fly, electron efficiency not modified
217   // 
218   Double_t threshParams[4];
219   AliDebug(1, Form("Select cut for %d tracklets\n", nTracklets));
220   // Get threshold paramters for the given number of tracklets from OADB container
221   AliHFEOADBThresholdsTRD *thresholds = dynamic_cast<AliHFEOADBThresholdsTRD *>(fOADBThresholds->GetObject(fRunNumber));
222   if(!thresholds){
223     AliDebug(1, Form("Thresholds for run %d not in the OADB", fRunNumber));
224     return 0.;
225   }
226   if(!thresholds->GetThresholdParameters(nTracklets, fElectronEfficiency, threshParams)){
227     AliDebug(1, "loading thresholds failed\n");
228     return 0.;
229   }
230   Double_t threshold = 1. - threshParams[0] - threshParams[1] * p - threshParams[2] * TMath::Exp(-threshParams[3] * p);
231   return TMath::Max(TMath::Min(threshold, 0.99), 0.2); // truncate the threshold upperwards to 0.999 and lowerwards to 0.2 and exclude unphysical values
232 }
233
234 //___________________________________________________________________
235 Double_t AliHFEpidTRD::GetTRDthresholds(Double_t p) const { 
236   //
237   // Return momentum dependent and electron efficiency dependent TRD thresholds
238   // 
239   Double_t threshold = 1. - fThreshParams[0] - fThreshParams[1] * p - fThreshParams[2] * TMath::Exp(-fThreshParams[3] * p);
240   return TMath::Max(TMath::Min(threshold, 0.99), 0.2); // truncate the threshold upperwards to 0.999 and lowerwards to 0.2 and exclude unphysical values
241 }
242
243
244 //___________________________________________________________________
245 Bool_t AliHFEpidTRD::InitParamsFromOADB(Int_t run){
246   //
247   // The name of the function says it all
248   //
249   AliHFEOADBThresholdsTRD *thresholds = dynamic_cast<AliHFEOADBThresholdsTRD *>(fOADBThresholds->GetObject(run));
250   if(!thresholds){
251     AliDebug(1, Form("Thresholds for run %d not in the OADB", run));
252     return kFALSE;
253   }
254   return thresholds->GetThresholdParameters(fNTracklets, fElectronEfficiency, fThreshParams);
255 }
256
257 //___________________________________________________________________
258 void AliHFEpidTRD::RenormalizeElPi(const Double_t * const likein, Double_t * const likeout) const {
259   //
260   // Renormalize likelihoods for electrons and pions neglecting the 
261   // likelihoods for protons, kaons and muons
262   //
263   memset(likeout, 0, sizeof(Double_t) * AliPID::kSPECIES);
264   Double_t norm = likein[AliPID::kElectron] + likein[AliPID::kPion];
265   if(norm == 0.) norm = 1.;   // Safety
266   likeout[AliPID::kElectron] = likein[AliPID::kElectron] / norm;
267   likeout[AliPID::kPion] = likein[AliPID::kPion] / norm;
268 }
269
270 //___________________________________________________________________
271 Double_t AliHFEpidTRD::GetElectronLikelihood(const AliVTrack *track, AliHFEpidObject::AnalysisType_t anaType) const {
272   //
273   // Get TRD likelihoods for ESD respectively AOD tracks
274   //
275   Double_t pidProbs[AliPID::kSPECIES]; memset(pidProbs, 0, sizeof(Double_t) * AliPID::kSPECIES);
276   if(anaType == AliHFEpidObject::kESDanalysis){
277     const AliESDtrack *esdtrack = dynamic_cast<const AliESDtrack *>(track);
278     if(esdtrack) esdtrack->GetTRDpid(pidProbs);
279   } else {
280     fkPIDResponse->ComputeTRDProbability(track, AliPID::kSPECIES, pidProbs);
281   }
282   if(!IsRenormalizeElPi()) return pidProbs[AliPID::kElectron];
283   Double_t probsNew[AliPID::kSPECIES];
284   RenormalizeElPi(pidProbs, probsNew);
285   return probsNew[AliPID::kElectron];
286 }
287
288 //___________________________________________________________________
289 Double_t AliHFEpidTRD::GetP(const AliVParticle *track, AliHFEpidObject::AnalysisType_t anaType) const {
290   //
291   // Get the Momentum in the TRD
292   //
293   Double_t p = 0.;
294   if(anaType == AliHFEpidObject::kESDanalysis){
295     const AliESDtrack *esdtrack = dynamic_cast<const AliESDtrack *>(track);
296     if(esdtrack) p = esdtrack->GetOuterParam() ? esdtrack->GetOuterParam()->P() : esdtrack->P();
297   } else {
298     const AliAODTrack *aodtrack = dynamic_cast<const AliAODTrack *>(track);
299     if(aodtrack) p = aodtrack->P();
300   }
301   return p;
302 }
303
304 //___________________________________________________________________
305 Double_t AliHFEpidTRD::GetChargeLayer(const AliVParticle *track, UInt_t layer, AliHFEpidObject::AnalysisType_t anaType) const {
306   //
307   // Get the Charge in a single TRD layer
308   //
309   if(layer >= 6) return 0.;
310   Double_t charge = 0.;
311   if(anaType == AliHFEpidObject::kESDanalysis){
312     const AliESDtrack *esdtrack = dynamic_cast<const AliESDtrack *>(track);
313     if(esdtrack){
314       // Distinction between old and new reconstruction: in the new reconstruction, the total charge is stored in slice 0, slices 1 to 8 are used for the slices for 
315       // the neural network. 
316       if(fTotalChargeInSlice0)
317         charge = esdtrack->GetTRDslice(static_cast<UInt_t>(layer), 0);
318       else
319        for(Int_t islice = 0; islice < esdtrack->GetNumberOfTRDslices(); islice++) charge += esdtrack->GetTRDslice(static_cast<UInt_t>(layer), islice);
320     }
321   } else {
322     const AliAODTrack *aodtrack = dynamic_cast<const AliAODTrack *>(track);
323     AliAODPid *aoddetpid = aodtrack ? aodtrack->GetDetPid() : NULL;
324     if(aoddetpid){
325       if(fTotalChargeInSlice0)
326         charge = aoddetpid->GetTRDsignal()[layer * aoddetpid->GetTRDnSlices()];
327       else
328        for(Int_t islice = 0; islice < aoddetpid->GetTRDnSlices(); islice++) charge += aoddetpid->GetTRDsignal()[layer * aoddetpid->GetTRDnSlices() + islice];
329     }
330   }
331   return charge;
332 }
333
334 //___________________________________________________________________
335 void AliHFEpidTRD::GetTRDmomenta(const AliVTrack *track, Double_t *mom) const {
336   //
337   // Fill Array with momentum information at the TRD tracklet
338   //
339   for(Int_t itl = 0; itl < 6; itl++) 
340     mom[itl] = track->GetTRDmomentum(itl);
341 }
342
343 //___________________________________________________________________
344 Double_t AliHFEpidTRD::GetTRDSignalV1(const AliESDtrack *track, Float_t truncation) const {
345   //
346   // Calculation of the TRD Signal via truncated mean
347   // Method 1: Take all Slices available
348   // cut out 0s
349   // Order them in increasing order
350   // Cut out the upper third
351   // Calculate mean over the last 2/3 slices
352   //
353   const Int_t kNSlices = 48;
354   const Int_t kLastSlice = 6; // Slice 7 is taken out from the truncated mean calculation
355   const Double_t kVerySmall = 1e-12;
356   // Weight the slice to equalize the MPV of the dQ/dl-distribution per slice to the one in the first slice
357   // Pions are used as reference for the equalization
358   const Double_t kWeightSlice[8] = {1., 2.122, 1.8, 1.635, 1.595, 1.614, 1.16, 7.0};
359   const Double_t kWeightSliceNo0[8] = {1., 1., 1.271, 1.451, 1.531, 1.543, 1.553, 2.163};  // Weighting factors in case slice 0 stores the total charge
360   const Double_t *kWeightFactor = fTotalChargeInSlice0 ? kWeightSliceNo0 : kWeightSlice;
361   AliDebug(3, Form("Number of Tracklets: %d\n", track->GetTRDntrackletsPID()));
362   Double_t trdSlices[kNSlices], tmp[kNSlices];
363   Int_t indices[48];
364   Int_t icnt = 0;
365   for(Int_t idet = 0; idet < 6; idet++)
366     for(Int_t islice = fTotalChargeInSlice0 ? 1 : 0 ; islice <= kLastSlice; islice++){
367       AliDebug(2, Form("Chamber[%d], Slice[%d]: TRDSlice = %f", idet, islice, track->GetTRDslice(idet, islice)));
368       if(TMath::Abs(track->GetTRDslice(idet, islice)) < kVerySmall) continue;;
369       trdSlices[icnt++] = track->GetTRDslice(idet, islice) * kWeightFactor[islice];
370     }
371   AliDebug(1, Form("Number of Slices: %d\n", icnt));
372   if(icnt < 6) return 0.;   // We need at least 6 Slices for the truncated mean
373   TMath::Sort(icnt, trdSlices, indices, kFALSE);
374   memcpy(tmp, trdSlices, sizeof(Double_t) * icnt);
375   for(Int_t ien = 0; ien < icnt; ien++)
376     trdSlices[ien] = tmp[indices[ien]];
377   Double_t trdSignal = TMath::Mean(static_cast<Int_t>(static_cast<Float_t>(icnt) * truncation), trdSlices);
378   Double_t mom = track->GetOuterParam() ? track->GetOuterParam()->P() : -1;
379   AliDebug(3, Form("PID Meth. 1: p[%f], TRDSignal[%f]", mom, trdSignal));
380   return trdSignal;
381 }
382
383 //___________________________________________________________________
384 Double_t AliHFEpidTRD::GetTRDSignalV2(const AliESDtrack *track, Float_t truncation) const {
385   //
386   // Calculation of the TRD Signal via truncated mean
387   // Method 2: Take only first 5 slices per chamber
388   // Order them in increasing order
389   // Cut out upper half 
390   // Now do mean with the reamining 3 slices per chamber
391   //
392   const Double_t kWeightSlice[8] = {1., 2.122, 1.8, 1.635, 1.595, 1.614, 1.16, 7.0};
393   const Int_t kLayers = 6;
394   const Int_t kSlicesLow = 6;
395   const Int_t kSlicesHigh = 1;
396   const Double_t kVerySmall = 1e-12;
397   Double_t trdSlicesLowTime[kLayers*kSlicesLow], trdSlicesRemaining[kLayers*(kSlicesHigh + kSlicesLow)];
398   Int_t indices[kLayers*kSlicesLow];
399   Int_t cntLowTime=0, cntRemaining = 0;
400   for(Int_t idet = 0; idet < 6; idet++)
401     for(Int_t islice = fTotalChargeInSlice0 ? 1 : 0; islice < kSlicesLow+kSlicesHigh; islice++){
402       if(TMath::Abs(track->GetTRDslice(idet, islice)) < kVerySmall) continue;;
403       if(islice < kSlicesLow){
404         AliDebug(3, Form("Part 1, Det[%d], Slice[%d], TRDSlice: %f", idet, islice, track->GetTRDslice(idet, islice)));
405         trdSlicesLowTime[cntLowTime++] = track->GetTRDslice(idet, islice) * kWeightSlice[islice];
406       } else{
407         AliDebug(3, Form("Part 1, Det[%d], Slice[%d], TRDSlice: %f", idet, islice, track->GetTRDslice(idet, islice)));
408         trdSlicesRemaining[cntRemaining++] = track->GetTRDslice(idet, islice) * kWeightSlice[islice];
409       }
410     }
411   if(cntLowTime < 4 || cntRemaining < 2) return 0.; // Min. Number of Slices at high time is 2 (matches with 1 layer), for the truncated mean we need at least 4 Slices
412   TMath::Sort(cntLowTime, trdSlicesLowTime, indices, kFALSE);
413   // Fill the second array with the lower half of the first time bins
414   for(Int_t ien = 0; ien < static_cast<Int_t>(static_cast<Float_t>(cntLowTime) * truncation); ien++)
415     trdSlicesRemaining[cntRemaining++] = trdSlicesLowTime[indices[ien]];
416   Double_t trdSignal = TMath::Mean(cntRemaining, trdSlicesRemaining);
417   Double_t mom = track->GetOuterParam() ? track->GetOuterParam()->P() : -1;
418   AliDebug(3, Form("PID Meth. 2: p[%f], TRDSignal[%f]", mom, trdSignal));
419   return trdSignal;
420 }