]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/qaRec/AliTRDpidChecker.cxx
136f65b8dc1afa45d3082d47e42d477cd8203a34
[u/mrichter/AliRoot.git] / TRD / qaRec / AliTRDpidChecker.cxx
1 #include "TPDGCode.h"
2 #include "TCanvas.h"
3 #include "TF1.h"
4 #include "TH1F.h"
5 #include "TH1D.h"
6 #include "TH2F.h"
7 #include "TProfile.h"
8 #include "TProfile2D.h"
9 #include "TGraph.h"
10 #include "TGraphErrors.h"
11
12 #include <TClonesArray.h>
13 #include <TObjArray.h>
14 #include <TList.h>
15
16 // #include "AliPID.h"
17 #include "AliESDEvent.h"
18 #include "AliESDInputHandler.h"
19 #include "AliTrackReference.h"
20
21 #include "AliAnalysisTask.h"
22 // #include "AliAnalysisManager.h"
23
24 #include "AliTRDtrackerV1.h"
25 #include "AliTRDtrackV1.h"
26 #include "AliTRDcluster.h"
27 #include "AliTRDReconstructor.h"
28 #include "AliCDBManager.h"
29 // #include "../Cal/AliTRDCalPID.h"
30 #include "AliTRDpidUtil.h"
31
32
33 #include "AliTRDpidChecker.h"
34 #include "AliTRDtrackInfo/AliTRDtrackInfo.h"
35
36 // calculate pion efficiency at 90% electron efficiency for 11 momentum bins
37 // this task should be used with simulated data only
38
39 ClassImp(AliTRDpidChecker)
40
41 //________________________________________________________________________
42 AliTRDpidChecker::AliTRDpidChecker() 
43   :AliTRDrecoTask("PID", "PID Checker")
44   ,fReconstructor(0x0)
45 {
46   //
47   // Default constructor
48   //
49
50   fReconstructor = new AliTRDReconstructor();
51   fReconstructor->SetRecoParam(AliTRDrecoParam::GetLowFluxParam());
52 }
53
54
55 //________________________________________________________________________
56 AliTRDpidChecker::~AliTRDpidChecker() 
57 {
58   if(fReconstructor) delete fReconstructor;
59 }
60
61
62 //________________________________________________________________________
63 void AliTRDpidChecker::CreateOutputObjects()
64 {
65   // Create histograms
66   // Called once
67
68   OpenFile(0, "RECREATE");
69   Int_t xBins = AliPID::kSPECIES*AliTRDCalPID::kNMom; 
70   fContainer = new TObjArray();
71   fContainer -> Expand(kGraphNN + 1);
72
73   const Float_t epsilon = 1./(2*(AliTRDpidUtil::kBins-1));     // get nice histos with bin center at 0 and 1
74
75   // histos of the electron probability of all 5 particle species and 11 momenta for the 2-dim LQ method 
76   fContainer->AddAt(
77     new TH2F("PID_LQ", "", 
78       xBins, -0.5, xBins - 0.5,
79       AliTRDpidUtil::kBins, 0.-epsilon, 1.+epsilon)
80   ,kLQlikelihood);
81
82
83   // histos of the electron probability of all 5 particle species and 11 momenta for the neural network method
84   fContainer->AddAt(
85     new TH2F("PID_NN", "", 
86       xBins, -0.5, xBins - 0.5,
87       AliTRDpidUtil::kBins, 0.-epsilon, 1.+epsilon)
88   ,kNNlikelihood);
89
90   // histos of the dE/dx distribution for all 5 particle species and 11 momenta 
91   fContainer->AddAt(
92     new TH2F("dEdx", "", 
93       xBins, -0.5, xBins - 0.5,
94       200, 0, 10000)
95     ,kdEdx);
96
97   // histos of the pulse height distribution for all 5 particle species and 11 momenta 
98   fContainer->AddAt(
99     new TProfile2D("PH", "", 
100       xBins, -0.5, xBins - 0.5,
101       AliTRDtrackerV1::GetNTimeBins(), -0.5, AliTRDtrackerV1::GetNTimeBins() - 0.5)
102     ,kPH);
103
104
105   // momentum distributions - absolute and in momentum bins
106   fContainer->AddAt(new TH1F("hMom", "momentum distribution", 100, 0., 12.),kMomentum);
107   fContainer->AddAt(new TH1F("hMomBin", "momentum distribution in momentum bins", AliTRDCalPID::kNMom, 0.5, 11.5),kMomentumBin);
108
109
110   // TGraph of the pion efficiencies
111
112   TGraphErrors *gEffisLQ = 0x0;
113   TGraphErrors *gEffisNN = 0x0;
114
115   fContainer->AddAt(gEffisLQ = new TGraphErrors(), kGraphLQ);
116   gEffisLQ->SetLineColor(kBlue);
117   gEffisLQ->SetMarkerColor(kBlue);
118   gEffisLQ->SetMarkerStyle(29);
119
120   fContainer -> AddAt(gEffisNN = new TGraphErrors(),kGraphNN);
121   gEffisNN->SetLineColor(kRed);
122   gEffisNN->SetMarkerColor(kRed);
123   gEffisNN->SetMarkerStyle(29);
124
125   gEffisLQ -> SetNameTitle("gEffisLQErr", "Efficiencies and Errors of the 2-dim LQ method");
126   gEffisNN -> SetNameTitle("gEffisNNErr", "Efficiencies and Errors of the NN method");
127
128 }
129
130 //________________________________________________________________________
131 void AliTRDpidChecker::Exec(Option_t *) 
132 {
133   // Main loop
134   // Called for each event
135
136
137 //   if(!AliTracker::GetFieldMap()){
138 //     AliMagFMaps* field = new AliMagFMaps("Maps","Maps", 2, 1., 10., AliMagFMaps::k5kG);
139 //     AliTracker::SetFieldMap(field, kTRUE);
140 //   }
141
142   TH2F *hPIDLQ;
143   TH2F *hPIDNN;
144   TH2F *hdEdx;
145   TProfile2D *hPH;
146
147   hPIDLQ = (TH2F*)fContainer->At(kLQlikelihood);
148   hPIDNN = (TH2F*)fContainer->At(kNNlikelihood);
149   hdEdx  = (TH2F*)fContainer->At(kdEdx);
150   hPH    = (TProfile2D*)fContainer->At(kPH);
151   
152   TH1F *hMom    = (TH1F*)fContainer->At(kMomentum);     
153   TH1F *hMomBin = (TH1F*)fContainer->At(kMomentumBin);  
154   
155   Int_t labelsacc[10000]; 
156   memset(labelsacc, 0, sizeof(Int_t) * 10000);
157   
158   Float_t mom;
159   ULong_t status;
160   Int_t nTRD = 0;
161   Float_t *fdEdx;       
162
163   AliTRDtrackInfo     *track = 0x0;
164   AliTRDtrackV1    *TRDtrack = 0x0;
165   AliTrackReference     *ref = 0x0;
166   AliExternalTrackParam *esd = 0x0;
167
168   AliTRDseedV1 *TRDtracklet[AliTRDgeometry::kNlayer];
169   for(Int_t iChamb = 0; iChamb < AliTRDgeometry::kNlayer; iChamb++)
170     TRDtracklet[iChamb] = 0x0;
171
172   AliTRDcluster *TRDcluster = 0x0;
173
174   AliTRDpidUtil *util = new AliTRDpidUtil();
175   for(Int_t itrk=0; itrk<fTracks->GetEntriesFast(); itrk++){
176     track = (AliTRDtrackInfo*)fTracks->UncheckedAt(itrk);
177     if(!track->HasESDtrack()) continue;
178     status = track->GetStatus();
179     if(!(status&AliESDtrack::kTPCout)) continue;
180
181     if(!(TRDtrack = track->GetTRDtrack())) continue; 
182     //&&(track->GetNumberOfClustersRefit()
183
184     // use only tracks that hit 6 chambers
185     if(!(TRDtrack->GetNumberOfTracklets() == AliTRDgeometry::kNlayer)) continue;
186      
187     ref = track->GetTrackRef(0);
188     esd = track->GetOuterParam();
189     mom = ref ? ref->P(): esd->P();
190
191     labelsacc[nTRD] = track->GetLabel();
192     nTRD++;
193       
194
195     // set the 11 momentum bins
196     Int_t iMomBin = -1;
197     iMomBin = util -> GetMomentumBin(mom);
198     if(fDebugLevel>=4) Printf("MomBin[%d] MomTot[%f]", iMomBin, mom);
199
200
201     // fill momentum histo to have the momentum distribution
202     hMom -> Fill(mom);
203     hMomBin -> Fill(iMomBin);
204
205
206     // set the reconstructor
207     TRDtrack -> SetReconstructor(fReconstructor);
208
209     
210     // if no monte carlo data available -> use TRDpid
211     if(!HasMCdata()){
212       fReconstructor -> SetOption("nn");
213       TRDtrack -> CookPID();
214       if(TRDtrack -> GetPID(0) > TRDtrack -> GetPID(1) + TRDtrack -> GetPID(2)  + TRDtrack -> GetPID(3) + TRDtrack -> GetPID(4)){
215         track -> SetPDG(kElectron);
216       }
217       else if(TRDtrack -> GetPID(4) > TRDtrack -> GetPID(2)  && TRDtrack -> GetPID(4) > TRDtrack -> GetPID(3)  && TRDtrack -> GetPID(4) > TRDtrack -> GetPID(1)){
218         track -> SetPDG(kProton);
219       }
220       else if(TRDtrack -> GetPID(3) > TRDtrack -> GetPID(1)  && TRDtrack -> GetPID(3) > TRDtrack -> GetPID(2)){
221         track -> SetPDG(kKPlus);
222       }
223       else if(TRDtrack -> GetPID(1) > TRDtrack -> GetPID(2)){
224         track -> SetPDG(kMuonPlus);
225       }
226       else{
227         track -> SetPDG(kPiPlus);
228       }
229     }
230
231
232     // calculate the probabilities for electron probability using 2-dim LQ, the deposited charge per chamber and the pulse height spectra and fill histograms
233     fReconstructor -> SetOption("!nn");
234     TRDtrack -> CookPID();
235
236     if(fDebugLevel>=4) Printf("PIDmethod[%d] Slices[%d] PDG[%d] LQLike[%f]", fReconstructor->GetPIDMethod(), fReconstructor->GetNdEdxSlices(), track->GetPDG(), TRDtrack -> GetPID(0));
237
238
239     Float_t SumdEdx[AliTRDgeometry::kNlayer];
240     for(Int_t iChamb = 0; iChamb < AliTRDgeometry::kNlayer; iChamb++){
241       TRDtracklet[iChamb] = TRDtrack -> GetTracklet(iChamb);
242       SumdEdx[iChamb] = 0.;
243       fdEdx = TRDtracklet[iChamb] -> GetdEdx();
244       SumdEdx[iChamb] += fdEdx[0] + fdEdx[1] + fdEdx[2]; 
245     }
246
247     switch(track->GetPDG()){
248     case kElectron:
249     case kPositron:
250       hPIDLQ -> Fill(AliPID::kElectron * AliTRDCalPID::kNMom + iMomBin, TRDtrack -> GetPID(0));
251       for(Int_t iChamb = 0; iChamb < AliTRDgeometry::kNlayer; iChamb++){
252         hdEdx -> Fill(AliPID::kElectron * AliTRDCalPID::kNMom + iMomBin, SumdEdx[iChamb]);
253         for(Int_t iClus = 0; iClus < AliTRDtrackerV1::GetNTimeBins(); iClus++){
254           if(!(TRDcluster = (AliTRDcluster*)TRDtracklet[iChamb] -> GetClusters(iClus)))
255             continue;
256           hPH -> Fill(AliPID::kElectron * AliTRDCalPID::kNMom + iMomBin, TRDcluster -> GetLocalTimeBin(), TRDtracklet[iChamb] -> GetdQdl(iClus));
257         }
258       }
259       break;
260     case kMuonPlus:
261     case kMuonMinus:
262       hPIDLQ -> Fill(AliPID::kMuon * AliTRDCalPID::kNMom + iMomBin, TRDtrack -> GetPID(0));
263       for(Int_t iChamb = 0; iChamb < AliTRDgeometry::kNlayer; iChamb++){
264         hdEdx -> Fill(AliPID::kMuon * AliTRDCalPID::kNMom + iMomBin, SumdEdx[iChamb]);
265         for(Int_t iClus = 0; iClus < AliTRDtrackerV1::GetNTimeBins(); iClus++){
266           if(!(TRDcluster = (AliTRDcluster*)TRDtracklet[iChamb] -> GetClusters(iClus)))
267             continue;
268           hPH -> Fill(AliPID::kMuon * AliTRDCalPID::kNMom + iMomBin, TRDcluster -> GetLocalTimeBin(), TRDtracklet[iChamb] -> GetdQdl(iClus));
269         }
270       }
271       break;
272     case kPiPlus:
273     case kPiMinus:
274       hPIDLQ -> Fill(AliPID::kPion * AliTRDCalPID::kNMom + iMomBin, TRDtrack -> GetPID(0));
275       for(Int_t iChamb = 0; iChamb < AliTRDgeometry::kNlayer; iChamb++){
276         hdEdx -> Fill(AliPID::kPion * AliTRDCalPID::kNMom + iMomBin, SumdEdx[iChamb]);
277         for(Int_t iClus = 0; iClus < AliTRDtrackerV1::GetNTimeBins(); iClus++){
278           if(!(TRDcluster = (AliTRDcluster*)TRDtracklet[iChamb] -> GetClusters(iClus)))
279             continue;
280           hPH -> Fill(AliPID::kPion * AliTRDCalPID::kNMom + iMomBin, TRDcluster -> GetLocalTimeBin(), TRDtracklet[iChamb] -> GetdQdl(iClus));
281         }
282       }
283       break;
284     case kKPlus:
285     case kKMinus:
286       hPIDLQ -> Fill(AliPID::kKaon * AliTRDCalPID::kNMom + iMomBin, TRDtrack -> GetPID(0));
287       for(Int_t iChamb = 0; iChamb < AliTRDgeometry::kNlayer; iChamb++){
288         hdEdx -> Fill(AliPID::kKaon * AliTRDCalPID::kNMom + iMomBin, SumdEdx[iChamb]);
289         for(Int_t iClus = 0; iClus < AliTRDtrackerV1::GetNTimeBins(); iClus++){
290           if(!(TRDcluster = (AliTRDcluster*)TRDtracklet[iChamb] -> GetClusters(iClus)))
291             continue;
292           hPH -> Fill(AliPID::kKaon * AliTRDCalPID::kNMom + iMomBin, TRDcluster -> GetLocalTimeBin(), TRDtracklet[iChamb] -> GetdQdl(iClus));
293         }
294       }
295       break;
296     case kProton:
297     case kProtonBar:
298       hPIDLQ -> Fill(AliPID::kProton * AliTRDCalPID::kNMom + iMomBin, TRDtrack -> GetPID(0));
299       for(Int_t iChamb = 0; iChamb < AliTRDgeometry::kNlayer; iChamb++){
300         hdEdx -> Fill(AliPID::kProton * AliTRDCalPID::kNMom + iMomBin, SumdEdx[iChamb]);
301         for(Int_t iClus = 0; iClus < AliTRDtrackerV1::GetNTimeBins(); iClus++){
302           if(!(TRDcluster = (AliTRDcluster*)TRDtracklet[iChamb] -> GetClusters(iClus)))
303             continue;
304           hPH -> Fill(AliPID::kProton * AliTRDCalPID::kNMom + iMomBin, TRDcluster -> GetLocalTimeBin(), TRDtracklet[iChamb] -> GetdQdl(iClus));
305         }
306       }
307       break;
308     }
309
310
311     // calculate the probabilities and fill histograms for electrons using NN
312     fReconstructor -> SetOption("nn");
313     TRDtrack->CookPID();
314
315
316     if(fDebugLevel>=4) Printf("PIDmethod[%d] Slices[%d] PDG[%d] NNLike[%f]", fReconstructor->GetPIDMethod(), fReconstructor->GetNdEdxSlices(), track->GetPDG(), TRDtrack -> GetPID(0));
317
318
319     switch(track->GetPDG()){
320     case kElectron:
321     case kPositron:
322       hPIDNN -> Fill(AliPID::kElectron * AliTRDCalPID::kNMom + iMomBin, TRDtrack -> GetPID(0));
323       break;
324     case kMuonPlus:
325     case kMuonMinus:
326       hPIDNN -> Fill(AliPID::kMuon * AliTRDCalPID::kNMom + iMomBin, TRDtrack -> GetPID(0));
327       break;
328     case kPiPlus:
329     case kPiMinus:
330       hPIDNN -> Fill(AliPID::kPion * AliTRDCalPID::kNMom + iMomBin, TRDtrack -> GetPID(0));
331       break;
332     case kKPlus:
333     case kKMinus:
334       hPIDNN -> Fill(AliPID::kKaon * AliTRDCalPID::kNMom + iMomBin, TRDtrack -> GetPID(0));
335       break;
336     case kProton:
337     case kProtonBar:
338       hPIDNN -> Fill(AliPID::kProton * AliTRDCalPID::kNMom + iMomBin, TRDtrack -> GetPID(0));
339       break;
340     }
341
342   }
343
344   util -> Delete();
345   PostData(0, fContainer);
346 }
347
348
349 //________________________________________________________
350 void AliTRDpidChecker::GetRefFigure(Int_t ifig)
351 {
352   Bool_t FIRST = kTRUE;
353   TH1 *h1 = 0x0;
354   TH2 *h2 = 0x0;
355   switch(ifig){
356   case 0:
357     ((TGraphErrors*)fContainer->At(kGraphStart))->Draw("apl");
358     ((TGraphErrors*)fContainer->At(kGraphStart+1))->Draw("pl");
359     gPad->SetLogy();
360     break;
361   case 1:
362     // save 2.0 GeV projection as reference
363     FIRST = kTRUE;
364     h2 = (TH2F*)(fContainer->At(kdEdx));
365     for(Int_t is = AliPID::kSPECIES-1; is>=0; is--){
366       Int_t bin = is*AliTRDCalPID::kNMom+4;
367       h1 = h2->ProjectionY("px", bin, bin);
368       if(!h1->GetEntries()) continue;
369       h1->Scale(1./h1->Integral());
370       h1->SetLineColor(AliTRDCalPID::GetPartColor(is));
371       h1->DrawClone(FIRST ? "c" : "samec");
372       FIRST = kFALSE;
373     }
374     gPad->SetLogy();
375     break;
376   case 2:
377     // save 2.0 GeV projection as reference
378     FIRST = kTRUE;
379     h2 = (TH2F*)(fContainer->At(kPH));
380     for(Int_t is=0; is<AliPID::kSPECIES; is++){
381       Int_t bin = is*AliTRDCalPID::kNMom+4;
382       h1 = h2->ProjectionY("py", bin, bin);
383       if(!h1->GetEntries()) continue;
384       h1->SetMarkerStyle(24);
385       h1->SetMarkerColor(AliTRDCalPID::GetPartColor(is));
386       h1->SetLineColor(AliTRDCalPID::GetPartColor(is));
387       h1->DrawClone(FIRST ? "e2" : "same e2");
388       FIRST = kFALSE;
389     }
390     gPad->SetLogy(0);
391     break;
392   }
393 }
394
395 //________________________________________________________________________
396 Bool_t AliTRDpidChecker::PostProcess()
397 {
398   // Draw result to the screen
399   // Called once at the end of the query
400
401   if (!fContainer) {
402     Printf("ERROR: list not available");
403     return kFALSE;
404   }
405 //   return kTRUE; // testing protection
406
407
408   // container for the pion efficiencies and the errors
409   Double_t  PionEffiLQ[AliTRDCalPID::kNMom], 
410             PionEffiErrorLQ[AliTRDCalPID::kNMom], 
411             EleEffiLQ[AliTRDCalPID::kNMom],
412             ThresholdLQ[AliTRDCalPID::kNMom];
413
414   Double_t  PionEffiNN[AliTRDCalPID::kNMom],
415             PionEffiErrorNN[AliTRDCalPID::kNMom],
416             EleEffiNN[AliTRDCalPID::kNMom],
417             ThresholdNN[AliTRDCalPID::kNMom];
418
419   Float_t mom = 0.;
420
421   TH1D *Histo1=0x0, *Histo2=0x0;
422
423   TH2F *hPIDLQ=0x0, *hPIDNN=0x0;
424   hPIDLQ = (TH2F*)fContainer->At(kLQlikelihood);
425   hPIDNN = (TH2F*)fContainer->At(kNNlikelihood);
426
427   // calculate the pion efficiencies and the errors for 90% electron efficiency (2-dim LQ)
428   for(Int_t iMom = 0; iMom < AliTRDCalPID::kNMom; iMom++){
429
430     AliTRDpidUtil *util = new AliTRDpidUtil();
431     mom = AliTRDCalPID::GetMomentum(iMom);
432
433     Histo1 = hPIDLQ -> ProjectionY("LQ_ele",AliTRDCalPID::kNMom*AliPID::kElectron+iMom+1,AliTRDCalPID::kNMom*AliPID::kElectron+iMom+1);
434     Histo2 = hPIDLQ -> ProjectionY("LQ_pio",AliTRDCalPID::kNMom*AliPID::kPion+iMom+1,AliTRDCalPID::kNMom*AliPID::kPion+iMom+1);
435
436     util -> CalculatePionEffi(Histo1, Histo2);
437
438     PionEffiLQ[iMom] = util -> GetPionEfficiency();
439     PionEffiErrorLQ[iMom] = util -> GetError();
440     EleEffiLQ[iMom] = util -> GetCalcElectronEfficiency();
441     ThresholdLQ[iMom] = util -> GetThreshold();
442
443     if(fDebugLevel>=1) Printf("Pion Efficiency for 2-dim LQ is : %f +/- %f\n\n", PionEffiLQ[iMom], PionEffiErrorLQ[iMom]);
444     util -> Delete();
445   }
446   
447
448
449   // calculate the pion efficiencies and the errors for 90% electron efficiency (NN)
450   for(Int_t iMom = 0; iMom < AliTRDCalPID::kNMom; iMom++){
451
452     AliTRDpidUtil *util = new AliTRDpidUtil();
453     mom = AliTRDCalPID::GetMomentum(iMom);
454
455     Histo1 = hPIDNN -> ProjectionY("NN_ele",AliTRDCalPID::kNMom*AliPID::kElectron+iMom+1,AliTRDCalPID::kNMom*AliPID::kElectron+iMom+1);
456     Histo2 = hPIDNN -> ProjectionY("NN_pio",AliTRDCalPID::kNMom*AliPID::kPion+iMom+1,AliTRDCalPID::kNMom*AliPID::kPion+iMom+1);
457
458     util -> CalculatePionEffi(Histo1, Histo2);
459
460     PionEffiNN[iMom] = util -> GetPionEfficiency();
461     PionEffiErrorNN[iMom] = util -> GetError();
462     EleEffiNN[iMom] = util -> GetCalcElectronEfficiency();
463     ThresholdNN[iMom] = util -> GetThreshold();
464
465     if(fDebugLevel>=1) Printf("Pion Efficiency for NN is : %f +/- %f\n\n", PionEffiNN[iMom], PionEffiErrorNN[iMom]);
466
467     util -> Delete();
468   }
469   
470
471   // create TGraph to plot the pion efficiencies
472   TGraphErrors *gEffisLQ=0x0, *gEffisNN=0x0;
473   gEffisLQ = (TGraphErrors*)fContainer->At(kGraphLQ);
474   gEffisNN = (TGraphErrors*)fContainer->At(kGraphNN);
475
476
477   for(Int_t iBin = 0; iBin < AliTRDCalPID::kNMom; iBin++){
478
479     Float_t momentum = AliTRDCalPID::GetMomentum(iBin);
480     gEffisLQ->SetPoint(iBin, momentum, PionEffiLQ[iBin]);
481     gEffisLQ->SetPointError(iBin, 0., PionEffiErrorLQ[iBin]);
482
483     gEffisNN->SetPoint(iBin, momentum, PionEffiNN[iBin]);
484     gEffisNN->SetPointError(iBin, 0., PionEffiErrorNN[iBin]);
485   }
486
487
488   fNRefFigures = 3/*1*/;
489   return kTRUE; // testing protection
490 }
491
492
493 //________________________________________________________________________
494 void AliTRDpidChecker::Terminate(Option_t *) 
495 {
496   // Draw result to the screen
497   // Called once at the end of the query
498
499   fContainer = dynamic_cast<TObjArray*>(GetOutputData(0));
500   if (!fContainer) {
501     Printf("ERROR: list not available");
502     return;
503   }
504 }
505
506