Update (Chiara)
[u/mrichter/AliRoot.git] / PWG3 / vertexingHF / AliAnalysisTaskSEHFQA.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-2009, 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 /* $Id$ */
17
18 /////////////////////////////////////////////////////////////
19 //
20 // AliAnalysisTaskSE for HF quality assurance
21 //
22 // Author: Chiara Bianchin, chiara.bianchin@pd.infn.it
23 /////////////////////////////////////////////////////////////
24
25 #include <Riostream.h>
26 #include <TClonesArray.h>
27 #include <TCanvas.h>
28 #include <TList.h>
29 #include <TH1F.h>
30 #include <TH2F.h>
31 #include <TDatabasePDG.h>
32
33 #include <AliAnalysisDataSlot.h>
34 #include <AliAnalysisDataContainer.h>
35 #include "AliAnalysisManager.h"
36 #include "AliESDtrack.h"
37 #include "AliVertexerTracks.h"
38 #include "AliPID.h"
39 #include "AliTPCPIDResponse.h"
40 #include "AliAODHandler.h"
41 #include "AliAODEvent.h"
42 #include "AliAODVertex.h"
43 #include "AliAODTrack.h"
44 #include "AliAODMCParticle.h"
45 #include "AliAODMCHeader.h"
46 #include "AliAODRecoDecayHF2Prong.h"
47 #include "AliAODRecoCascadeHF.h"
48 #include "AliAnalysisVertexingHF.h"
49 #include "AliAnalysisTaskSE.h"
50 #include "AliCounterCollection.h"
51 #include "AliRDHFCuts.h"
52 #include "AliRDHFCutsDplustoKpipi.h"
53 #include "AliRDHFCutsD0toKpipipi.h"
54 #include "AliRDHFCutsDstoKKpi.h"
55 #include "AliRDHFCutsDStartoKpipi.h"
56 #include "AliRDHFCutsD0toKpi.h"
57 #include "AliRDHFCutsLctopKpi.h"
58
59 #include "AliAnalysisTaskSEHFQA.h"
60
61 ClassImp(AliAnalysisTaskSEHFQA)
62
63 //____________________________________________________________________________
64
65 AliAnalysisTaskSEHFQA::AliAnalysisTaskSEHFQA():AliAnalysisTaskSE(),
66   fNEntries(0x0),
67   fOutputPID(0x0),
68   fOutputTrack(0x0),
69   fOutputCounters(0x0),
70   fOutputCheckCentrality(0x0),
71   fOutputEvSelection(0x0),
72   fDecayChannel(AliAnalysisTaskSEHFQA::kD0toKpi),
73   fCuts(0x0),
74   fEstimator(AliRDHFCuts::kCentTRK),
75   fReadMC(kFALSE),
76   fSimpleMode(kFALSE),
77   fOnOff()
78 {
79   //default constructor
80   fOnOff[0]=kTRUE;
81   fOnOff[1]=kTRUE;
82   fOnOff[2]=kTRUE;
83   fOnOff[3]=kTRUE;
84 }
85
86 //____________________________________________________________________________
87 AliAnalysisTaskSEHFQA::AliAnalysisTaskSEHFQA(const char *name, AliAnalysisTaskSEHFQA::DecChannel ch,AliRDHFCuts* cuts):
88   AliAnalysisTaskSE(name),
89   fNEntries(0x0),
90   fOutputPID(0x0),
91   fOutputTrack(0x0),
92   fOutputCounters(0x0),
93   fOutputCheckCentrality(0x0),
94   fOutputEvSelection(0x0),
95   fDecayChannel(ch),
96   fCuts(0x0),
97   fEstimator(AliRDHFCuts::kCentTRK),
98   fReadMC(kFALSE),
99   fSimpleMode(kFALSE),
100   fOnOff()
101 {
102   //constructor
103
104   //SetCutObject(cuts);
105   fCuts=cuts;
106
107   fOnOff[0]=kTRUE;
108   fOnOff[1]=kTRUE;
109   fOnOff[2]=kTRUE;
110   fOnOff[3]=kTRUE;
111
112   // Output slot #1 writes into a TH1F container (number of events)
113   DefineOutput(1,TH1F::Class());  //My private output
114   // Output slot #2 writes into a TList container (PID)
115   if (fOnOff[1]) DefineOutput(2,TList::Class());  //My private output
116   // Output slot #3 writes into a TList container (Tracks)
117   if (fOnOff[0]) DefineOutput(3,TList::Class());  //My private output
118   // Output slot #4 writes into a AliRDHFCuts container (cuts)
119   switch(fDecayChannel){
120   case 0:
121     DefineOutput(4,AliRDHFCutsDplustoKpipi::Class());  //My private output
122     break;
123   case 1:
124     DefineOutput(4,AliRDHFCutsD0toKpi::Class());  //My private output
125     break;
126   case 2:
127     DefineOutput(4,AliRDHFCutsDStartoKpipi::Class());  //My private output
128     break;
129   case 3:
130     DefineOutput(4,AliRDHFCutsDstoKKpi::Class());  //My private output
131     break;
132   case 4:
133     DefineOutput(4,AliRDHFCutsD0toKpipipi::Class());  //My private output
134     break;
135   case 5:
136     DefineOutput(4,AliRDHFCutsLctopKpi::Class());  //My private output
137     break;
138   }
139   if (fOnOff[2]) {
140     // Output slot #5 writes into a TList container (AliCounterCollection)
141     DefineOutput(5,TList::Class());  //My private output
142     // Output slot #6 writes into a TList container (TH1F)
143     DefineOutput(6,TList::Class());  //My private output
144   }
145
146   if(fOnOff[3]) DefineOutput(7,TList::Class());  //My private output
147
148 }
149
150 //___________________________________________________________________________
151 AliAnalysisTaskSEHFQA::~AliAnalysisTaskSEHFQA()
152 {
153   //destructor
154   delete fNEntries;
155
156   delete fOutputPID;
157
158   delete fOutputTrack;
159
160   delete fOutputCounters;
161
162   delete fOutputCheckCentrality;
163
164   delete fOutputEvSelection;
165
166 }
167
168 //___________________________________________________________________________
169 void AliAnalysisTaskSEHFQA::Init(){
170
171   //initialization
172   if(fDebug > 1) printf("AnalysisTaskSEHFQA::Init() \n");
173
174   switch(fDecayChannel){
175   case 0:
176     {
177       AliRDHFCutsDplustoKpipi* copycut=new AliRDHFCutsDplustoKpipi(*(static_cast<AliRDHFCutsDplustoKpipi*>(fCuts)));
178       // Post the data
179       PostData(4,copycut);
180     }
181     break;
182   case 1:
183     {
184       AliRDHFCutsD0toKpi* copycut=new AliRDHFCutsD0toKpi(*(static_cast<AliRDHFCutsD0toKpi*>(fCuts)));
185       // Post the data
186       PostData(4,copycut);
187     }
188     break;
189   case 2:
190     {
191       AliRDHFCutsDStartoKpipi* copycut=new AliRDHFCutsDStartoKpipi(*(static_cast<AliRDHFCutsDStartoKpipi*>(fCuts)));
192       // Post the data
193       PostData(4,copycut);
194     }
195     break;
196   case 3:
197     {
198       AliRDHFCutsDstoKKpi* copycut=new AliRDHFCutsDstoKKpi(*(static_cast<AliRDHFCutsDstoKKpi*>(fCuts)));
199       // Post the data
200       PostData(4,copycut);
201     }
202     break;
203   case 4:
204     {
205       AliRDHFCutsD0toKpipipi* copycut=new AliRDHFCutsD0toKpipipi(*(static_cast<AliRDHFCutsD0toKpipipi*>(fCuts)));
206       // Post the data
207       PostData(4,copycut);
208     }
209     break;
210   case 5:
211     {
212       AliRDHFCutsLctopKpi* copycut=new AliRDHFCutsLctopKpi(*(static_cast<AliRDHFCutsLctopKpi*>(fCuts)));
213       // Post the data
214       PostData(4,copycut);
215     }
216     break;
217
218   default:
219     return;
220   }
221
222
223
224 }
225
226 //___________________________________________________________________________
227 void AliAnalysisTaskSEHFQA::UserCreateOutputObjects()
228 {
229
230   //create the output container
231   if(fDebug > 1) printf("AnalysisTaskSEHFQA::UserCreateOutputObjects() \n");
232
233   //count events
234
235   fNEntries=new TH1F(GetOutputSlot(1)->GetContainer()->GetName(), "Counts the number of events", 10,-0.5,9.5);
236   fNEntries->GetXaxis()->SetBinLabel(1,"nEventsAnal");
237   fNEntries->GetXaxis()->SetBinLabel(2,"Pile-up Rej");
238   fNEntries->GetXaxis()->SetBinLabel(3,"No VertexingHF");
239   fNEntries->GetXaxis()->SetBinLabel(4,"nCandidates(AnCuts)");
240   fNEntries->GetXaxis()->SetBinLabel(5,"EventsWithGoodVtx");
241   //fNEntries->GetXaxis()->SetBinLabel(6,"N. of 0SMH");
242   fNEntries->GetXaxis()->SetBinLabel(6,"N. of CSH1");
243   if(fReadMC){
244     fNEntries->GetXaxis()->SetBinLabel(7,"MC Cand from c");
245     fNEntries->GetXaxis()->SetBinLabel(8,"MC Cand from b");
246     fNEntries->GetXaxis()->SetBinLabel(9,"N fake Trks");
247     fNEntries->GetXaxis()->SetBinLabel(10,"N true Trks");
248   } else{
249     fNEntries->GetXaxis()->SetBinLabel(7,"N candidates");
250   }
251
252   fNEntries->GetXaxis()->SetNdivisions(1,kFALSE);
253
254   //PID
255   if(fOnOff[1]){
256     fOutputPID=new TList();
257     fOutputPID->SetOwner();
258     fOutputPID->SetName(GetOutputSlot(2)->GetContainer()->GetName());
259
260     //TOF pid
261     TString hname="hTOFsig";
262     TH1F* hTOFsig=new TH1F(hname.Data(),"Distribution of TOF signal;TOF time [ps];Entries", 100, -2.e3,40.e3);
263
264     hname="hTOFtime";
265     TH1F* hTOFtime=new TH1F(hname.Data(),"Distribution of TOF time Kaon;TOF time(Kaon) [ps];Entries", 1000, 0.,50000.);
266
267     hname="hTOFtimeKaonHyptime";
268     TH2F* hTOFtimeKaonHyptime=new TH2F(hname.Data(),"TOFtime - timeHypothesisForKaon;p[GeV/c];TOFtime - timeHypothesisForKaon [ps]",200,0.,4.,1000,-20000.,20000.);
269
270     hname="hTOFtimeKaonHyptimeAC";
271     TH2F* hTOFtimeKaonHyptimeAC=new TH2F(hname.Data(),"TOFtime - timeHypothesisForKaon;p[GeV/c];TOFtime - timeHypothesisForKaon [ps]",200,0.,4.,1000,-20000.,20000.);
272
273     hname="hTOFsigmaKSigPid";
274     TH2F* hTOFsigmaKSigPid=new TH2F(hname.Data(),"(TOFsignal-timeK)/tofSigPid;p[GeV/c];(TOFsignal-timeK)/tofSigPid",200,0.,4.,100,-5,5);
275
276     hname="hTOFsigmaPionSigPid";
277     TH2F* hTOFsigmaPionSigPid=new TH2F(hname.Data(),"(TOFsignal-time#pi)/tofSigPid;p[GeV/c];(TOFsignal-time#pi)/tofSigPid",200,0.,4.,100,-5,5);
278
279     hname="hTOFsigmaProtonSigPid";
280     TH2F* hTOFsigmaProtonSigPid=new TH2F(hname.Data(),"(TOFsignal-timep)/tofSigPid;p[GeV/c];(TOFsignal-time p)/tofSigPid",200,0.,4.,100,-5,5);
281
282     hname="hTOFsigPid3sigPion";
283     TH1F* hTOFsigPid3sigPion=new TH1F(hname.Data(),"TOF PID resolution (#pi) [ps]",500,0.,1000.);
284
285     hname="hTOFsigPid3sigKaon";
286     TH1F* hTOFsigPid3sigKaon=new TH1F(hname.Data(),"TOF PID resolution (K) [ps]",500,0.,1000.);
287
288     hname="hTOFsigPid3sigProton";
289     TH1F* hTOFsigPid3sigProton=new TH1F(hname.Data(),"TOF PID resolution (p) [ps]",500,0.,1000.);
290
291
292     //TPC pid
293     hname="hTPCsig";
294     TH1F* hTPCsig=new TH1F(hname.Data(),"Distribution of TPC signal;TPC sig;Entries", 100, 35.,100.);
295
296     hname="hTPCsigvsp";
297     TH2F* hTPCsigvsp=new TH2F(hname.Data(),"TPCsig vs p;TPC p[GeV/c];TPCsig",200,0.,4.,1000,35.,100.);
298  
299     hname="hTPCsigvspAC";
300     TH2F* hTPCsigvspAC=new TH2F(hname.Data(),"TPCsig vs p;TPCp[GeV/c];TPCsig",200,0.,4.,1000,35.,100.);
301
302     hname="hTPCsigmaK";
303     TH2F* hTPCsigmaK=new TH2F(hname.Data(),"TPC Sigma for K as a function of momentum;p[GeV/c];Sigma Kaon",200,0.,4.,200,-5,5);
304
305     hname="hTPCsigmaPion";
306     TH2F* hTPCsigmaPion=new TH2F(hname.Data(),"TPC Sigma for #pi as a function of momentum;p[GeV/c];Sigma #pi",200,0.,4.,200,-5,5);
307
308     hname="hTPCsigmaProton";
309     TH2F* hTPCsigmaProton=new TH2F(hname.Data(),"TPC Sigma for proton as a function of momentum;p[GeV/c];Sigma Proton",200,0.,4.,200,-5,5);
310
311     fOutputPID->Add(hTOFsig);
312     fOutputPID->Add(hTPCsig);
313     fOutputPID->Add(hTOFtime);
314     fOutputPID->Add(hTOFtimeKaonHyptime);
315     fOutputPID->Add(hTOFtimeKaonHyptimeAC);
316     fOutputPID->Add(hTOFsigmaKSigPid);
317     fOutputPID->Add(hTOFsigmaPionSigPid);
318     fOutputPID->Add(hTOFsigmaProtonSigPid);
319     fOutputPID->Add(hTOFsigPid3sigPion);
320     fOutputPID->Add(hTOFsigPid3sigKaon);
321     fOutputPID->Add(hTOFsigPid3sigProton);
322     fOutputPID->Add(hTPCsigvsp);
323     fOutputPID->Add(hTPCsigvspAC);
324     fOutputPID->Add(hTPCsigmaK);
325     fOutputPID->Add(hTPCsigmaPion);
326     fOutputPID->Add(hTPCsigmaProton);
327   }
328
329   //quality of the tracks
330   if(fOnOff[0]){
331     fOutputTrack=new TList();
332     fOutputTrack->SetOwner();
333     fOutputTrack->SetName(GetOutputSlot(3)->GetContainer()->GetName());
334
335     TString hname="hnClsITS";
336     TH1F* hnClsITS=new TH1F(hname.Data(),"Distribution of number of ITS clusters;nITScls;Entries",7,-0.5,6.5);
337
338     hname="hnClsITS-SA";
339     TH1F* hnClsITSSA=new TH1F(hname.Data(),"Distribution of number of ITS clusters(ITS-SA);nITScls;Entries",7,-0.5,6.5);
340
341     hname="hnClsSPD";
342     TH1F* hnClsSPD=new TH1F(hname.Data(),"Distribution of number of SPD clusters;nSPDcls;Entries",3,-0.5,2.5);
343
344     hname="hptGoodTr";
345     TH1F* hptGoodTr=new TH1F(hname.Data(),"Pt distribution of 'good' tracks;p_{t}[GeV];Entries/0.05 GeV/c",400,0.,20.);
346     hptGoodTr->SetTitleOffset(1.3,"Y");
347
348     hname="hdistrGoodTr";
349     TH1F* hdistrGoodTr=new TH1F(hname.Data(),"Distribution of number of 'good' tracks per event;no.good-tracks/ev;Entries",4000,-0.5,3999.5);
350     hdistrGoodTr->SetTitleOffset(1.3,"Y");
351
352     hname="hd0";
353     TH1F* hd0=new TH1F(hname.Data(),"Impact parameter distribution of 'good' tracks;d_{0}[cm];Entries/10^{3} cm",200,-0.1,0.1);
354
355     fOutputTrack->Add(hnClsITS);
356     fOutputTrack->Add(hnClsITSSA);
357     fOutputTrack->Add(hnClsSPD);
358     fOutputTrack->Add(hptGoodTr);
359     fOutputTrack->Add(hdistrGoodTr);
360     fOutputTrack->Add(hd0);
361
362     if(fReadMC){
363       hname="hdistrFakeTr";
364       TH1F* hdistrFakeTr=new TH1F(hname.Data(),"Distribution of number of fake tracks per event;no.fake-tracks/ev;Entries",4000,-0.5,3999.5);
365       hdistrGoodTr->SetTitleOffset(1.3,"Y");
366
367       hname="hd0f";
368       TH1F* hd0f=new TH1F(hname.Data(),"Impact parameter distribution of fake tracks;d_{0}[cm];Entries/10^{3} cm",200,-0.1,0.1);
369
370       hname="hptFakeTr";
371       TH1F* hptFakeTr=new TH1F(hname.Data(),"Pt distribution of fake tracks;p_{t}[GeV];Entries/0.05 GeV/c",400,0.,20.);
372       hptFakeTr->SetTitleOffset(1.3,"Y");
373
374       fOutputTrack->Add(hptFakeTr);
375       fOutputTrack->Add(hdistrFakeTr);
376       fOutputTrack->Add(hd0f);
377    
378     }
379   }
380
381   
382   if(fOnOff[2] && fCuts->GetUseCentrality()){
383
384     //Centrality (Counters)
385     fOutputCounters=new TList();
386     fOutputCounters->SetOwner();
387     fOutputCounters->SetName(GetOutputSlot(5)->GetContainer()->GetName());
388
389     AliCounterCollection *stdEstimator=new AliCounterCollection("stdEstimator");
390     stdEstimator->AddRubric("run",500000);
391     stdEstimator->AddRubric("centralityclass","-10_0/0_10/10_20/20_30/30_40/40_50/50_60/60_70/70_80/80_90/90_100/-990_-980");
392     stdEstimator->Init();
393     AliCounterCollection *secondEstimator=new AliCounterCollection("secondEstimator");
394     secondEstimator->AddRubric("run",500000);
395     secondEstimator->AddRubric("centralityclass","-10_0/0_10/10_20/20_30/30_40/40_50/50_60/60_70/70_80/80_90/90_100/-990_-980");
396     secondEstimator->Init();
397
398     fOutputCounters->Add(stdEstimator);
399     fOutputCounters->Add(secondEstimator);
400
401     //Centrality (Checks)
402     fOutputCheckCentrality=new TList();
403     fOutputCheckCentrality->SetOwner();
404     fOutputCheckCentrality->SetName(GetOutputSlot(6)->GetContainer()->GetName());
405
406     TString hname="hNtrackletsIn";
407     TH1F* hNtrackletsIn=new TH1F(hname.Data(),"Number of tracklets in Centrality range;ntracklets;Entries",5000,-0.5,4999.5);
408
409     hname="hMultIn";
410     TH1F* hMultIn=new TH1F(hname.Data(),"Multiplicity;multiplicity in Centrality range;Entries",10000,-0.5,9999.5);
411
412     hname="hNtrackletsOut";
413     TH1F* hNtrackletsOut=new TH1F(hname.Data(),"Number of tracklets out of Centrality range;ntracklets;Entries",5000,-0.5,4999.5);
414
415     hname="hMultOut";
416     TH1F* hMultOut=new TH1F(hname.Data(),"Multiplicity out of Centrality range;multiplicity;Entries",10000,-0.5,9999.5);
417
418     hname="hMultvsPercentile";
419     TH2F* hMultvsPercentile=new TH2F(hname.Data(),"Multiplicity vs Percentile;multiplicity;percentile",10000,-0.5,9999.5,12,-10.,110);
420
421     fOutputCheckCentrality->Add(hNtrackletsIn);
422     fOutputCheckCentrality->Add(hNtrackletsOut);
423     fOutputCheckCentrality->Add(hMultIn);
424     fOutputCheckCentrality->Add(hMultOut);
425     fOutputCheckCentrality->Add(hMultvsPercentile);
426
427     PostData(6,fOutputCheckCentrality);
428   
429   } else{
430     if(fOnOff[0]){
431       TString hname="hNtracklets";
432       TH1F* hNtracklets=new TH1F(hname.Data(),"Number of tracklets;ntracklets;Entries",5000,-0.5,4999.5);
433
434       hname="hMult";
435       TH1F* hMult=new TH1F(hname.Data(),"Multiplicity;multiplicity;Entries",10000,-0.5,9999.5);
436       fOutputTrack->Add(hNtracklets);
437       fOutputTrack->Add(hMult);
438     }
439   }
440
441   //event selection (z vertex for the moment)
442   if(fOnOff[3]){
443     fOutputEvSelection=new TList();
444     fOutputEvSelection->SetOwner();
445     fOutputEvSelection->SetName(GetOutputSlot(7)->GetContainer()->GetName());
446     AliCounterCollection *evselection=new AliCounterCollection("evselection");
447     evselection->AddRubric("run",500000);
448     evselection->AddRubric("evnonsel","zvtx");
449     evselection->Init();
450
451     TH1F* hzvtx=new TH1F("hzvtx", "Distribution of z_{VTX};z_{VTX} [cm];Entries",100,-20,20);
452
453     fOutputEvSelection->Add(evselection);
454     fOutputEvSelection->Add(hzvtx);
455   }
456
457   // Post the data
458   PostData(1,fNEntries);
459   if(fOnOff[1]) PostData(2,fOutputPID);
460   if(fOnOff[0]) PostData(3,fOutputTrack);
461   PostData(4,fCuts);
462   if(fOnOff[2]) PostData(5,fOutputCounters);
463   if(fOnOff[3]) PostData(7,fOutputEvSelection);
464
465   if(!fOnOff[0] && !fOnOff[1] && !fOnOff[2]) AliError("Nothing will be filled!");
466 }
467
468 //___________________________________________________________________________
469 void AliAnalysisTaskSEHFQA::UserExec(Option_t */*option*/)
470 {
471   // Execute analysis for current event
472
473   AliAODEvent *aod = dynamic_cast<AliAODEvent*> (InputEvent());
474   if(fDebug>2) printf("Analysing decay %d\n",fDecayChannel);
475   // Post the data already here
476   PostData(1,fNEntries);
477   if(fOnOff[1]) PostData(2,fOutputPID);
478   if(fOnOff[0]) PostData(3,fOutputTrack);
479   PostData(4,fCuts);
480   if(fOnOff[2]) {
481     PostData(5,fOutputCounters);
482     if(fCuts->GetUseCentrality()) PostData(6,fOutputCheckCentrality);
483   }
484
485   TClonesArray *arrayProng =0;
486   Int_t pdg=0;
487   Int_t *pdgdaughters=0x0;
488
489   if(!aod && AODEvent() && IsStandardAOD()) { 
490     // In case there is an AOD handler writing a standard AOD, use the AOD 
491     // event in memory rather than the input (ESD) event.    
492     aod = dynamic_cast<AliAODEvent*> (AODEvent());
493     // in this case the braches in the deltaAOD (AliAOD.VertexingHF.root)
494     // have to taken from the AOD event hold by the AliAODExtension
495     AliAODHandler* aodHandler = (AliAODHandler*) 
496       ((AliAnalysisManager::GetAnalysisManager())->GetOutputEventHandler());
497     if(aodHandler->GetExtensions()) {
498       
499       AliAODExtension *ext = (AliAODExtension*)aodHandler->GetExtensions()->FindObject("AliAOD.VertexingHF.root");
500       AliAODEvent *aodFromExt = ext->GetAOD();
501    
502    
503       
504       switch(fDecayChannel){
505       case 0:
506         arrayProng=(TClonesArray*)aodFromExt->GetList()->FindObject("Charm3Prong");
507         pdg=411;
508         if(fReadMC){
509           pdgdaughters =new Int_t[3];
510           pdgdaughters[0]=211;//pi
511           pdgdaughters[1]=321;//K
512           pdgdaughters[2]=211;//pi
513         }
514         break; 
515       case 1:
516         arrayProng=(TClonesArray*)aodFromExt->GetList()->FindObject("D0toKpi");
517         pdg=421;
518         if(fReadMC){
519           pdgdaughters =new Int_t[2];
520           pdgdaughters[0]=211;//pi 
521           pdgdaughters[1]=321;//K
522         }
523         break; 
524       case 2:
525         arrayProng=(TClonesArray*)aodFromExt->GetList()->FindObject("Dstar");
526         pdg=413;
527         if(fReadMC){
528           pdgdaughters =new Int_t[3];
529           pdgdaughters[1]=211;//pi
530           pdgdaughters[0]=321;//K
531           pdgdaughters[2]=211;//pi (soft?)
532         }
533         break; 
534       case 3:
535         arrayProng=(TClonesArray*)aodFromExt->GetList()->FindObject("Charm3Prong");
536         pdg=431;
537         if(fReadMC){
538           pdgdaughters =new Int_t[3];
539           pdgdaughters[0]=321;//K
540           pdgdaughters[1]=321;//K
541           pdgdaughters[2]=211;//pi
542         }
543         break; 
544       case 4:
545         arrayProng=(TClonesArray*)aodFromExt->GetList()->FindObject("Charm4Prong");
546         pdg=421;
547         if(fReadMC){
548           pdgdaughters =new Int_t[4];
549           pdgdaughters[0]=321;
550           pdgdaughters[1]=211;
551           pdgdaughters[2]=211;
552           pdgdaughters[3]=211;
553         }
554         break; 
555       case 5:
556         arrayProng=(TClonesArray*)aodFromExt->GetList()->FindObject("Charm3Prong");
557         pdg=4122;
558         if(fReadMC){
559           pdgdaughters =new Int_t[3];
560           pdgdaughters[0]=2212;//p
561           pdgdaughters[1]=321;//K
562           pdgdaughters[2]=211;//pi
563         }
564         break; 
565       }
566     }
567   } else if(aod) {
568     switch(fDecayChannel){
569     case 0:
570       arrayProng=(TClonesArray*)aod->GetList()->FindObject("Charm3Prong");
571       pdg=411;
572       if(fReadMC){
573         pdgdaughters =new Int_t[3];
574         pdgdaughters[0]=211;//pi
575         pdgdaughters[1]=321;//K
576         pdgdaughters[2]=211;//pi
577       }
578       break; 
579     case 1:
580       arrayProng=(TClonesArray*)aod->GetList()->FindObject("D0toKpi");
581       pdg=421;
582       if(fReadMC){
583         pdgdaughters =new Int_t[2];
584         pdgdaughters[0]=211;//pi 
585         pdgdaughters[1]=321;//K
586       }
587       break; 
588     case 2:
589       arrayProng=(TClonesArray*)aod->GetList()->FindObject("Dstar");
590       pdg=413;
591       if(fReadMC){
592         pdgdaughters =new Int_t[3];
593         pdgdaughters[1]=211;//pi
594         pdgdaughters[0]=321;//K
595         pdgdaughters[2]=211;//pi (soft?)
596       }
597       break; 
598     case 3:
599       arrayProng=(TClonesArray*)aod->GetList()->FindObject("Charm3Prong");
600       pdg=431;
601       if(fReadMC){
602         pdgdaughters =new Int_t[3];
603         pdgdaughters[0]=321;//K
604         pdgdaughters[1]=321;//K
605         pdgdaughters[2]=211;//pi
606       }
607       break; 
608     case 4:
609       arrayProng=(TClonesArray*)aod->GetList()->FindObject("Charm4Prong");
610       pdg=421;
611       if(fReadMC){
612         pdgdaughters =new Int_t[4];
613         pdgdaughters[0]=321;
614         pdgdaughters[1]=211;
615         pdgdaughters[2]=211;
616         pdgdaughters[3]=211;
617       }
618       break; 
619     case 5:
620       arrayProng=(TClonesArray*)aod->GetList()->FindObject("Charm3Prong");
621       pdg=4122;
622       if(fReadMC){
623         pdgdaughters =new Int_t[3];
624         pdgdaughters[0]=2212;//p
625         pdgdaughters[1]=321;//K
626         pdgdaughters[2]=211;//pi
627       }
628       break; 
629     }
630   }
631   Bool_t isSimpleMode=fSimpleMode;
632   if(!arrayProng) {
633     AliInfo("Branch not found! The output will contain only trak related histograms\n");
634     isSimpleMode=kTRUE;
635     fNEntries->Fill(2);
636   }
637   
638   TClonesArray *mcArray = 0;
639   AliAODMCHeader *mcHeader = 0;
640
641   //check if MC
642   if(fReadMC) {
643     // load MC particles
644     mcArray = (TClonesArray*)aod->GetList()->FindObject(AliAODMCParticle::StdBranchName());
645     if(!mcArray) {
646       printf("AliAnalysisTaskSEHFQA::UserExec: MC particles branch not found!\n");
647       delete [] pdgdaughters;
648       return;
649     }
650     
651     // load MC header
652     mcHeader = (AliAODMCHeader*)aod->GetList()->FindObject(AliAODMCHeader::StdBranchName());
653     if(!mcHeader) {
654       printf("AliAnalysisTaskSEHFQA::UserExec: MC header branch not found!\n");
655       delete [] pdgdaughters;
656       return;
657     }
658   }
659   if(!aod) {delete [] pdgdaughters;return;}
660   // fix for temporary bug in ESDfilter 
661   // the AODs with null vertex pointer didn't pass the PhysSel
662   if(!aod->GetPrimaryVertex() || TMath::Abs(aod->GetMagneticField())<0.001) return;
663
664   // count event
665   fNEntries->Fill(0); 
666
667   //count events with good vertex
668   // AOD primary vertex
669   AliAODVertex *vtx1 = (AliAODVertex*)aod->GetPrimaryVertex();
670   TString primTitle = vtx1->GetTitle();
671   if(primTitle.Contains("VertexerTracks") && vtx1->GetNContributors()>0) fNEntries->Fill(4);
672
673   // trigger class for PbPb C0SMH-B-NOPF-ALLNOTRD, C0SMH-B-NOPF-ALL
674   //TString trigclass=aod->GetFiredTriggerClasses();
675   //if(trigclass.Contains("C0SMH-B-NOPF-ALLNOTRD") || trigclass.Contains("C0SMH-B-NOPF-ALL")) fNEntries->Fill(5); //tmp
676
677   Int_t runNumber = aod->GetRunNumber();
678
679   Bool_t evSelbyCentrality=kTRUE,evSelected=kTRUE,evSelByVertex=kTRUE,evselByPileup=kFALSE;
680   //select event
681   if(!fCuts->IsEventSelected(aod)) {
682     evSelected=kFALSE;
683     if(fCuts->GetWhyRejection()==1) {fNEntries->Fill(1); evselByPileup=kTRUE;}// rejected for pileup
684     if(fCuts->GetWhyRejection()==2 || fCuts->GetWhyRejection()==3) evSelbyCentrality=kFALSE; //rejected by centrality
685     if(fCuts->GetWhyRejection()==4) evSelByVertex=kFALSE; //rejected by vertex
686     if(fCuts->GetWhyRejection()==5) fNEntries->Fill(5);//tmp
687     if(fCuts->GetWhyRejection()==6 && fOnOff[3]) ((AliCounterCollection*)fOutputEvSelection->FindObject("evselection"))->Count("evnonsel:zvtx/Run:%d",runNumber);
688   }
689
690   if(evSelected || (!evSelected && !evSelbyCentrality && evSelByVertex && !evselByPileup)){ //events selected or not selected because of vtx or pileup
691     if(fOnOff[2] && fCuts->GetUseCentrality()){
692
693       Float_t stdCentf=fCuts->GetCentrality(aod);
694       Int_t stdCent = (Int_t)(stdCentf+0.5);
695       Float_t secondCentf =fCuts->GetCentrality(aod,fEstimator);
696       Int_t secondCent = (Int_t)(secondCentf+0.5);
697       Int_t mincent=stdCent-stdCent%10;
698       if(stdCentf==-1) {
699         mincent=-10; 
700         stdCent=-1;
701       }
702       ((AliCounterCollection*)fOutputCounters->FindObject("stdEstimator"))->Count(Form("centralityclass:%d_%d/Run:%d",mincent,mincent+10,runNumber));
703
704       mincent=secondCent-secondCent%10;
705       if(secondCentf==-1) {
706         mincent=-10;
707         secondCent=-1;
708       }
709       ((AliCounterCollection*)fOutputCounters->FindObject("secondEstimator"))->Count(Form("centralityclass:%d_%d/Run:%d",mincent,mincent+10,runNumber));
710
711       if(stdCent<fCuts->GetMinCentrality() || stdCent>fCuts->GetMaxCentrality()){
712         ((TH1F*)fOutputCheckCentrality->FindObject("hNtrackletsOut"))->Fill(aod->GetTracklets()->GetNumberOfTracklets());
713         ((TH1F*)fOutputCheckCentrality->FindObject("hMultOut"))->Fill(aod->GetHeader()->GetRefMultiplicity());
714       }else{
715         ((TH1F*)fOutputCheckCentrality->FindObject("hNtrackletsIn"))->Fill(aod->GetTracklets()->GetNumberOfTracklets());
716         ((TH1F*)fOutputCheckCentrality->FindObject("hMultIn"))->Fill(aod->GetHeader()->GetRefMultiplicity());
717       }
718       ((TH2F*)fOutputCheckCentrality->FindObject("hMultvsPercentile"))->Fill(aod->GetHeader()->GetRefMultiplicity(),stdCentf);
719
720       PostData(6,fOutputCheckCentrality);
721
722     } else{
723       if(fOnOff[0]){
724         ((TH1F*)fOutputTrack->FindObject("hNtracklets"))->Fill(aod->GetTracklets()->GetNumberOfTracklets());
725         ((TH1F*)fOutputTrack->FindObject("hMult"))->Fill(aod->GetHeader()->GetRefMultiplicity());
726       }
727     }
728   }
729
730   if(fOnOff[3]){
731     const AliVVertex *vertex = aod->GetPrimaryVertex();
732     Double_t zvtx=vertex->GetZ();
733     if(evSelected || (!evSelected && zvtx > 10.))
734     ((TH1F*)fOutputEvSelection->FindObject("hzvtx"))->Fill(zvtx);
735   }
736
737   if(!evSelected) {
738     delete [] pdgdaughters;
739     return; //discard all events not selected (vtx and/or centrality)
740   }
741
742
743   Int_t ntracks=0;
744   Int_t isGoodTrack=0, isFakeTrack=0;
745
746   if(aod) ntracks=aod->GetNTracks();
747
748   if(fOnOff[0] || fOnOff[1]){
749     //loop on tracks in the event
750     for (Int_t k=0;k<ntracks;k++){
751       AliAODTrack* track=aod->GetTrack(k);
752       AliAODPidHF* pidHF=fCuts->GetPidHF();
753       AliAODPid *pid = track->GetDetPid();
754
755
756       if(fOnOff[1]){
757         if(!pid)  {if (fDebug>1)cout<<"No AliAODPid found"<<endl; continue;}
758         Double_t times[AliPID::kSPECIES];
759         pid->GetIntegratedTimes(times);
760     
761         Double_t tofRes[AliPID::kSPECIES];
762         pid->GetTOFpidResolution(tofRes);
763
764         //check TOF
765         if(pidHF && pidHF->CheckStatus(track,"TOF")){
766           ((TH1F*)fOutputPID->FindObject("hTOFtime"))->Fill(times[AliPID::kProton]);
767           ((TH2F*)fOutputPID->FindObject("hTOFtimeKaonHyptime"))->Fill(track->P(),pid->GetTOFsignal()-times[3]); //3 is kaon
768           ((TH1F*)fOutputPID->FindObject("hTOFsig"))->Fill(pid->GetTOFsignal());
769           if (pid->GetTOFsignal()< 0) ((TH1F*)fOutputPID->FindObject("hTOFsig"))->Fill(-1);
770
771           
772           // test TOF sigma PID
773           if (tofRes[2] != 0.) {   // protection against 'old' AODs...
774             ((TH2F*)fOutputPID->FindObject("hTOFsigmaKSigPid"))->Fill(track->P(),(pid->GetTOFsignal()-times[AliPID::kKaon])/tofRes[3]);
775             ((TH2F*)fOutputPID->FindObject("hTOFsigmaPionSigPid"))->Fill(track->P(),(pid->GetTOFsignal()-times[AliPID::kPion])/tofRes[2]);
776             ((TH2F*)fOutputPID->FindObject("hTOFsigmaProtonSigPid"))->Fill(track->P(),(pid->GetTOFsignal()-times[AliPID::kProton])/tofRes[4]);
777             for (Int_t iS=2; iS<5; iS++){ //we plot TOF Pid resolution for 3-sigma identified particles
778               if ( (TMath::Abs(times[iS]-pid->GetTOFsignal())/tofRes[iS])<3.){
779                 switch (iS) {
780                 case AliPID::kPion:
781                   ((TH1F*)fOutputPID->FindObject("hTOFsigPid3sigPion"))->Fill(tofRes[iS]);
782                   break;
783                 case AliPID::kKaon:
784                   ((TH1F*)fOutputPID->FindObject("hTOFsigPid3sigKaon"))->Fill(tofRes[iS]);
785                   break;
786                 case AliPID::kProton:
787                   ((TH1F*)fOutputPID->FindObject("hTOFsigPid3sigProton"))->Fill(tofRes[iS]);
788                   break;
789                 default:
790                   break;
791                 }
792               }
793             }
794           }
795         }//if TOF status
796
797         if(pidHF && pidHF->CheckStatus(track,"TPC")){ 
798           Double_t alephParameters[5];
799           /*
800           //this is recommended for LHC10d
801           alephParameters[0] = 1.34490e+00/50.;
802           alephParameters[1] =  2.69455e+01;
803           alephParameters[2] =  TMath::Exp(-2.97552e+01);
804           alephParameters[3] = 2.35339e+00;
805           alephParameters[4] = 5.98079e+00;
806           */
807       
808           //this is recommended for PbPb (LHC10h)
809           alephParameters[0] = 1.25202/50.; 
810           alephParameters[1] = 2.74992e+01; 
811           alephParameters[2] = TMath::Exp(-3.31517e+01); 
812           alephParameters[3] = 2.46246; 
813           alephParameters[4] = 6.78938;
814       
815           /*
816           //this is recommended for LHC10bc
817           alephParameters[0] = 0.0283086/0.97;
818           alephParameters[1] = 2.63394e+01;
819           alephParameters[2] = 5.04114e-11;
820           alephParameters[3] = 2.12543e+00;
821           alephParameters[4] = 4.88663e+00;
822           */
823
824           AliTPCPIDResponse* tpcres=new AliTPCPIDResponse();
825           tpcres->SetBetheBlochParameters(alephParameters[0],alephParameters[1],alephParameters[2],alephParameters[3],alephParameters[4]);
826           Double_t TPCp=pid->GetTPCmomentum();
827           Double_t TPCsignal=pid->GetTPCsignal();
828           ((TH1F*)fOutputPID->FindObject("hTPCsig"))->Fill(TPCsignal);
829           ((TH1F*)fOutputPID->FindObject("hTPCsigvsp"))->Fill(TPCp,TPCsignal);
830           //if (pidHF->IsKaonRaw(track, "TOF"))
831           ((TH2F*)fOutputPID->FindObject("hTPCsigmaK"))->Fill(TPCp,tpcres->GetNumberOfSigmas(TPCp,TPCsignal,track->GetTPCNcls(),AliPID::kKaon));
832           //if (pidHF->IsPionRaw(track, "TOF"))
833           ((TH2F*)fOutputPID->FindObject("hTPCsigmaPion"))->Fill(TPCp,tpcres->GetNumberOfSigmas(TPCp,TPCsignal,track->GetTPCNcls(),AliPID::kPion));
834           //if (pidHF->IsProtonRaw(track,"TOF"))
835           ((TH2F*)fOutputPID->FindObject("hTPCsigmaProton"))->Fill(TPCp,tpcres->GetNumberOfSigmas(TPCp,TPCsignal,track->GetTPCNcls(),AliPID::kProton));
836           delete tpcres;
837         }//if TPC status
838       } //end PID histograms
839
840       Int_t nclsTot=0,nclsSPD=0;
841
842       //check clusters of the tracks
843       if(fOnOff[0]){
844
845         for(Int_t l=0;l<6;l++) {
846           if(TESTBIT(track->GetITSClusterMap(),l)) {
847             nclsTot++; if(l<2) nclsSPD++;
848           }
849         }
850         ((TH1F*)fOutputTrack->FindObject("hnClsITS"))->Fill(nclsTot);
851         ((TH1F*)fOutputTrack->FindObject("hnClsSPD"))->Fill(nclsSPD);
852
853         if(!(track->GetStatus()&AliESDtrack::kTPCin) && track->GetStatus()&AliESDtrack::kITSrefit && !(track->GetStatus()&AliESDtrack::kITSpureSA)){//tracks retrieved in the ITS and not reconstructed in the TPC
854           ((TH1F*)fOutputTrack->FindObject("hnClsITS-SA"))->Fill(nclsTot);
855         }
856         Int_t label=0;
857         if(fReadMC){
858           label=track->GetLabel();
859           if (label<0)fNEntries->Fill(8);
860           else fNEntries->Fill(9); 
861         }
862
863         if(isSimpleMode){
864
865           if (track->Pt()>0.3 &&
866               track->GetStatus()&AliESDtrack::kTPCrefit &&
867               track->GetStatus()&AliESDtrack::kITSrefit &&
868               /*nclsTot>3 &&*/
869               nclsSPD>0) {//count good tracks
870
871             
872             if(fReadMC && label<0) {
873               ((TH1F*)fOutputTrack->FindObject("hptFakeTr"))->Fill(track->Pt());
874               isFakeTrack++;    
875             } else {
876               ((TH1F*)fOutputTrack->FindObject("hptGoodTr"))->Fill(track->Pt());
877               isGoodTrack++;
878             }
879           }
880         } //simple mode: no IsSelected on tracks: use "manual" cuts   
881       } //fill track histos
882     } //end loop on tracks
883
884       //fill once per event
885     if(fOnOff[0]){
886       if (fReadMC) ((TH1F*)fOutputTrack->FindObject("hdistrFakeTr"))->Fill(isFakeTrack);
887       ((TH1F*)fOutputTrack->FindObject("hdistrGoodTr"))->Fill(isGoodTrack);
888     }
889
890     if(!isSimpleMode){
891       // loop over candidates
892       Int_t nCand = arrayProng->GetEntriesFast();
893       Int_t ndaugh=3;
894       if(fDecayChannel==AliAnalysisTaskSEHFQA::kD0toKpi) ndaugh=2;
895       if(fDecayChannel==AliAnalysisTaskSEHFQA::kD0toKpipipi) ndaugh=4;
896
897       for (Int_t iCand = 0; iCand < nCand; iCand++) {
898         AliAODRecoDecayHF *d = (AliAODRecoDecayHF*)arrayProng->UncheckedAt(iCand);
899         if(d->GetSelectionMap()) {
900           if(fDecayChannel==AliAnalysisTaskSEHFQA::kD0toKpi && !d->HasSelectionBit(AliRDHFCuts::kD0toKpiCuts)) continue; //skip the D0 from Dstar
901           if(fDecayChannel==AliAnalysisTaskSEHFQA::kDplustoKpipi && !d->HasSelectionBit(AliRDHFCuts::kDplusCuts)) continue; //skip the 3 prong !D+
902         }
903
904         if(fReadMC){ 
905           Int_t labD = d->MatchToMC(pdg,mcArray,ndaugh,pdgdaughters);
906           if(labD>=0){
907             AliAODMCParticle *partD = (AliAODMCParticle*)mcArray->At(labD);
908             Int_t label=partD->GetMother();
909             AliAODMCParticle *mot = (AliAODMCParticle*)mcArray->At(label);
910             while(label>=0){//get first mother
911               mot = (AliAODMCParticle*)mcArray->At(label);
912               label=mot->GetMother();
913             }
914             Int_t pdgMotCode = mot->GetPdgCode();
915         
916             if(TMath::Abs(pdgMotCode)==4) fNEntries->Fill(6); //from primary charm
917             if(TMath::Abs(pdgMotCode)==5) fNEntries->Fill(7); //from beauty
918
919           }
920         }//end MC
921         else fNEntries->Fill(6); //count the candidates (data)
922
923         for(Int_t id=0;id<ndaugh;id++){
924
925           //other histograms to be filled when the cut object is given
926           AliAODTrack* track=(AliAODTrack*)d->GetDaughter(id);
927
928           //track quality
929
930           if (fCuts->IsInFiducialAcceptance(d->Pt(),d->Y(pdg)) && fCuts->IsSelected(d,AliRDHFCuts::kTracks,aod)) {
931             
932             Int_t label=0;
933             if(fReadMC)label=track->GetLabel();
934             if(fOnOff[0]){
935               
936               if(fReadMC && label<0) {
937                 isFakeTrack++;
938                 ((TH1F*)fOutputTrack->FindObject("hptFakeTr"))->Fill(track->Pt());
939            
940                 ((TH1F*)fOutputTrack->FindObject("hd0f"))->Fill(d->Getd0Prong(id));
941               } else {
942                 isGoodTrack++;
943                 ((TH1F*)fOutputTrack->FindObject("hptGoodTr"))->Fill(track->Pt());
944             
945                 ((TH1F*)fOutputTrack->FindObject("hd0"))->Fill(d->Getd0Prong(id));
946               }
947             }
948             if (fCuts->IsSelected(d,AliRDHFCuts::kAll,aod) && fOnOff[1]){
949           
950               AliAODPid *pid = track->GetDetPid();
951               AliAODPidHF* pidHF=fCuts->GetPidHF();
952               Double_t times[5];
953               pid->GetIntegratedTimes(times);
954               if(pidHF && pidHF->CheckStatus(track,"TOF")) ((TH2F*)fOutputPID->FindObject("hTOFtimeKaonHyptimeAC"))->Fill(track->P(),pid->GetTOFsignal()-times[AliPID::kKaon]);
955               if(pidHF && pidHF->CheckStatus(track,"TPC")) ((TH2F*)fOutputPID->FindObject("hTPCsigvspAC"))->Fill(pid->GetTPCmomentum(),pid->GetTPCsignal());
956
957               fNEntries->Fill(3);
958             } //end analysis cuts
959           } //end acceptance and track cuts
960         } //end loop on tracks in the candidate
961       } //end loop on candidates
962       if(fOnOff[0]){
963         if(fReadMC) ((TH1F*)fOutputTrack->FindObject("hdistrFakeTr"))->Fill(isFakeTrack);
964         ((TH1F*)fOutputTrack->FindObject("hdistrGoodTr"))->Fill(isGoodTrack);
965       }
966     }
967   } //end if on pid or track histograms
968
969   delete [] pdgdaughters;
970   PostData(1,fNEntries);
971   if(fOnOff[1]) PostData(2,fOutputPID);
972   if(fOnOff[0]) PostData(3,fOutputTrack);
973   PostData(4,fCuts);
974   if(fOnOff[2]) PostData(5,fOutputCounters);
975   //Post data 6 done in case of centrality on   
976
977 }
978
979 //____________________________________________________________________________
980 void AliAnalysisTaskSEHFQA::Terminate(Option_t */*option*/){
981   //terminate analysis
982
983   fNEntries = dynamic_cast<TH1F*>(GetOutputData(1));
984   if(!fNEntries){
985     printf("ERROR: %s not available\n",GetOutputSlot(1)->GetContainer()->GetName());
986     return;
987   }
988
989   fOutputPID = dynamic_cast<TList*> (GetOutputData(2));
990   if (!fOutputPID && fOnOff[1]) {     
991     printf("ERROR: %s not available\n",GetOutputSlot(2)->GetContainer()->GetName());
992     return;
993   }
994
995   fOutputTrack = dynamic_cast<TList*> (GetOutputData(3));
996   if (!fOutputTrack && fOnOff[0]) {     
997     printf("ERROR: %s not available\n",GetOutputSlot(3)->GetContainer()->GetName());
998     return;
999   }
1000
1001 }
1002