Implemented possibility to compute efficiencies for D mesons with or without a fake...
[u/mrichter/AliRoot.git] / PWG3 / vertexingHF / AliCFVertexingHFCascade.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-2011, 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 //-----------------------------------------------------------------------
17 // Class for HF corrections as a function of many variables and steps
18 // For D* and other cascades
19 // 
20 // Author : A.Grelli a.grelli@uu.nl  UTECHT
21 //-----------------------------------------------------------------------
22
23 #include "AliAODRecoDecayHF2Prong.h"
24 #include "AliAODMCParticle.h"
25 #include "AliAODEvent.h"
26 #include "TClonesArray.h"
27 #include "AliCFVertexingHF.h"
28 #include "AliESDtrack.h"
29 #include "TDatabasePDG.h"
30 #include "AliAODRecoCascadeHF.h"
31 #include "AliCFVertexingHFCascade.h"
32 #include "AliCFContainer.h"
33
34 ClassImp(AliCFVertexingHFCascade)
35
36
37 //_________________________________________
38   AliCFVertexingHFCascade::AliCFVertexingHFCascade(TClonesArray *mcArray, UShort_t originDselection):
39   AliCFVertexingHF(mcArray, originDselection)
40 {       
41   // standard constructor
42
43   SetNProngs(3);
44   fPtAccCut=new Float_t[fProngs];
45   fEtaAccCut=new Float_t[fProngs];
46   // element 0 in the cut arrays corresponds to the soft pion!!!!!!!! Careful when setting the values...
47   fPtAccCut[0]=0.;
48   fEtaAccCut[0]=0.;
49   for(Int_t iP=1; iP<fProngs; iP++){
50           fPtAccCut[iP]=0.1;
51           fEtaAccCut[iP]=0.9;
52   }
53
54 }
55
56
57 //_____________________________________
58 AliCFVertexingHFCascade& AliCFVertexingHFCascade::operator=(const AliCFVertexingHFCascade& c)
59 {
60   // operator =
61  
62   if  (this != &c) {
63
64     AliCFVertexingHF::operator=(c);
65    
66   }
67   return *this;
68 }
69
70 //__________________________________________
71 Bool_t AliCFVertexingHFCascade::SetRecoCandidateParam(AliAODRecoDecayHF *recoCand)
72 {
73   // set the AliAODRecoDecay candidate
74   
75   Bool_t bSignAssoc = kFALSE;
76  
77   fRecoCandidate = recoCand;
78   AliAODRecoCascadeHF* dstarD0pi = (AliAODRecoCascadeHF*)recoCand;
79
80   if (!fRecoCandidate) {
81     AliError("fRecoCandidate not found, problem in assignement\n");
82     return bSignAssoc;
83   }
84   
85   if ( fRecoCandidate->GetPrimaryVtx()) AliDebug(3,"fReco Candidate has a pointer to PrimVtx\n");
86   //if (recoCand->GetPrimaryVtx()) printf("Reco Cand has a pointer to PrimVtx\n");
87   
88   //Int_t pdgCand = 413;
89
90   Int_t pdgDgDStartoD0pi[2]={421,211};
91   Int_t pdgDgD0toKpi[2]={321,211};
92
93   Int_t nentries = fmcArray->GetEntriesFast();
94
95   AliDebug(3,Form("nentries = %d\n", nentries));
96  
97   Int_t mcLabel =  dstarD0pi->MatchToMC(413,421,pdgDgDStartoD0pi,pdgDgD0toKpi,fmcArray); 
98   
99   if (mcLabel == -1) return bSignAssoc;
100
101   if (fRecoCandidate->NumberOfFakeDaughters()>0){
102           fFake = 0;    // fake candidate
103           if (fFakeSelection==1) return bSignAssoc;
104   }
105   if (fRecoCandidate->NumberOfFakeDaughters()==0){
106           fFake = 2;    // non-fake candidate
107           if (fFakeSelection==2) return bSignAssoc;
108   }
109   
110   SetMCLabel(mcLabel);
111   fmcPartCandidate = dynamic_cast<AliAODMCParticle*>(fmcArray->At(fmcLabel)); 
112
113   if (!fmcPartCandidate){
114     AliDebug(3,"No part candidate");
115     return bSignAssoc;
116   }
117  
118   bSignAssoc = kTRUE;
119   return bSignAssoc;
120 }
121
122 //______________________________________________
123 Bool_t AliCFVertexingHFCascade::GetGeneratedValuesFromMCParticle(Double_t* vectorMC) 
124 {
125   // 
126   // collecting all the necessary info (pt, y, cosThetaStar, ptPi, ptKa, cT) from MC particle
127   //
128         
129         Bool_t bGenValues = kFALSE;
130         
131         
132         Int_t daughter0ds = fmcPartCandidate->GetDaughter(0);
133         Int_t daughter1ds = fmcPartCandidate->GetDaughter(1);
134
135         //the D0
136         AliAODMCParticle* mcPartDaughterD0 = dynamic_cast<AliAODMCParticle*>(fmcArray->At(daughter0ds));
137         AliAODMCParticle* mcPartDaughterPis = dynamic_cast<AliAODMCParticle*>(fmcArray->At(daughter1ds));
138
139         Double_t vtx1[3] = {0,0,0};   // primary vertex         
140         Double_t vtx2daughter0[3] = {0,0,0};   // secondary vertex from daughter 0
141         Double_t vtx2daughter1[3] = {0,0,0};   // secondary vertex from daughter 1
142         fmcPartCandidate->XvYvZv(vtx1);  // cm
143
144         //D0 daughters
145         Int_t daughter0 = mcPartDaughterD0->GetDaughter(0);
146         Int_t daughter1 = mcPartDaughterD0->GetDaughter(1);
147
148         AliAODMCParticle* mcPartDaughter0 = dynamic_cast<AliAODMCParticle*>(fmcArray->At(daughter0)); //D0
149         AliAODMCParticle* mcPartDaughter1 = dynamic_cast<AliAODMCParticle*>(fmcArray->At(daughter1)); //pis
150
151         // getting vertex from daughters
152         mcPartDaughter0->XvYvZv(vtx2daughter0);  // cm
153         mcPartDaughter1->XvYvZv(vtx2daughter1);  //cm
154         if (TMath::Abs(vtx2daughter0[0] - vtx2daughter1[0])>1E-5 || TMath::Abs(vtx2daughter0[1]- vtx2daughter1[1])>1E-5 || TMath::Abs(vtx2daughter0[2] - vtx2daughter1[2])>1E-5) {
155           AliError("Daughters have different secondary vertex, skipping the track");
156           return bGenValues;
157         }
158         
159         Int_t nprongs = 2;
160         Short_t charge = 0;
161         // always instantiate the AliAODRecoDecay with the positive daughter first, the negative second
162         AliAODMCParticle* positiveDaugh = mcPartDaughter0;
163         AliAODMCParticle* negativeDaugh = mcPartDaughter1;
164         if (mcPartDaughter0->GetPdgCode()<0 && mcPartDaughter1->GetPdgCode()>0){
165                 // inverting in case the positive daughter is the second one
166                 positiveDaugh = mcPartDaughter1;
167                 negativeDaugh = mcPartDaughter0;
168         }
169         // getting the momentum from the daughters
170         Double_t px[2] = {positiveDaugh->Px(), negativeDaugh->Px()};            
171         Double_t py[2] = {positiveDaugh->Py(), negativeDaugh->Py()};            
172         Double_t pz[2] = {positiveDaugh->Pz(), negativeDaugh->Pz()};
173         
174         Double_t d0[2] = {0.,0.};               
175         
176         AliAODRecoDecayHF* decay = new AliAODRecoDecayHF(vtx1,vtx2daughter0,nprongs,charge,px,py,pz,d0);
177         
178         Double_t cosThetaStar = 0.;
179         Double_t cosThetaStarD0 = 0.;
180         Double_t cosThetaStarD0bar = 0.;
181         cosThetaStarD0 = decay->CosThetaStar(1,421,211,321);
182         cosThetaStarD0bar = decay->CosThetaStar(0,421,321,211);
183         if (mcPartDaughterD0->GetPdgCode() == 421){  // D0
184           AliDebug(3, Form("D0, with pdgprong0 = %d, pdgprong1 = %d",mcPartDaughter0->GetPdgCode(),mcPartDaughter1->GetPdgCode()));
185           cosThetaStar = cosThetaStarD0;
186         }
187         else if (mcPartDaughterD0->GetPdgCode() == -421){  // D0bar{
188           AliDebug(3, Form("D0bar, with pdgprong0 = %d, pdgprong1 = %d",mcPartDaughter0->GetPdgCode(),mcPartDaughter1->GetPdgCode()));
189           cosThetaStar = cosThetaStarD0bar;
190         }
191         else{
192           AliWarning("There are problems!! particle was expected to be either a D0 or a D0bar, check...");
193           return vectorMC;
194         }
195         if (cosThetaStar < -1 || cosThetaStar > 1) {
196           AliWarning("Invalid value for cosine Theta star, returning");
197           return bGenValues;
198         }
199         
200         Double_t vectorD0[2] ={0.,0.};
201
202         // evaluate the correct cascade
203         if (!EvaluateIfD0toKpi(mcPartDaughterD0,vectorD0)) {
204           AliDebug(2, "Error! the D0 MC doesn't have correct daughters!!");
205           return bGenValues;  
206         }       
207
208         //ct
209         Double_t cT = decay->Ct(421);
210         // get the pT of the daughters
211         Double_t pTD0 = 0.;
212         Double_t pTpi = 0.;
213         
214         if (TMath::Abs(fmcPartCandidate->GetPdgCode()) == 413) {
215                 pTD0 = mcPartDaughterD0->Pt();
216                 pTpi = mcPartDaughterPis->Pt();
217         }
218
219         
220         vectorMC[0] = fmcPartCandidate->Pt();
221         vectorMC[1] = fmcPartCandidate->Y() ;
222         vectorMC[2] = cosThetaStar ;
223         vectorMC[3] = vectorD0[0]; 
224         vectorMC[4] = vectorD0[1];
225         vectorMC[5] = cT*1.E4 ;  // in micron
226         vectorMC[6] = 0.;   // dummy value, meaningless in MC
227         vectorMC[7] = 0.;   // dummy value, meaningless in MC, in micron
228         vectorMC[8] = 0.;   // dummy value, meaningless in MC, in micron
229         vectorMC[9] = -100000.; // dummy value, meaningless in MC, in micron^2
230         vectorMC[10] = 1.01;    // dummy value, meaningless in MC
231         vectorMC[11] = fmcPartCandidate->Phi(); 
232         vectorMC[12] = fzMCVertex;    // z of reconstructed of primary vertex
233         vectorMC[13] = fCentValue; // reconstructed centrality
234         vectorMC[14] = 1.;           // always filling with 1 at MC level 
235
236         delete decay;
237         bGenValues = kTRUE;
238         return bGenValues;
239 }
240 //____________________________________________
241 Bool_t AliCFVertexingHFCascade::GetRecoValuesFromCandidate(Double_t *vectorReco) const
242
243   // read the variables for the container
244
245   Bool_t bFillRecoValues=kFALSE;
246   
247   //Get the D* and the D0 from D*
248   AliAODRecoCascadeHF* dstarD0pi = (AliAODRecoCascadeHF*)fRecoCandidate;
249   AliAODRecoDecayHF2Prong* d0toKpi = (AliAODRecoDecayHF2Prong*)dstarD0pi->Get2Prong();
250   
251
252   if (dstarD0pi->GetPrimaryVtx())printf("dstarD0pi has primary vtx\n");
253   if (fRecoCandidate->GetPrimaryVtx())printf("fRecoCandidateDstar has primary vtx\n");
254
255   Double_t pt =  dstarD0pi->Pt();
256   Double_t rapidity =  dstarD0pi->YDstar();
257   Double_t invMass=0.;
258   Double_t cosThetaStar = 9999.;
259   Double_t pTpi = 0.;
260   Double_t pTK = 0.;
261   Double_t dca = d0toKpi->GetDCA();
262   Double_t d0pi = 0.;
263   Double_t d0K = 0.;
264   Double_t d0xd0 = d0toKpi->Prodd0d0();
265   Double_t cosPointingAngle = d0toKpi->CosPointingAngle();
266   Double_t phi = dstarD0pi->Phi();
267
268   Int_t pdgCode = fmcPartCandidate->GetPdgCode();
269  
270   if (pdgCode > 0){
271     cosThetaStar = d0toKpi->CosThetaStarD0();
272     pTpi = d0toKpi->PtProng(0);
273     pTK = d0toKpi->PtProng(1);
274     d0pi = d0toKpi->Getd0Prong(0);
275     d0K = d0toKpi->Getd0Prong(1);
276     invMass=d0toKpi->InvMassD0();
277   }
278   else {
279     cosThetaStar = d0toKpi->CosThetaStarD0bar();
280     pTpi = d0toKpi->PtProng(1);
281     pTK = d0toKpi->PtProng(0);
282     d0pi = d0toKpi->Getd0Prong(1);
283     d0K = d0toKpi->Getd0Prong(0);
284     invMass= d0toKpi->InvMassD0bar();
285   }
286   
287   Double_t cT = d0toKpi->CtD0();
288   
289   vectorReco[0] = pt;
290   vectorReco[1] = rapidity;
291   vectorReco[2] = cosThetaStar;
292   vectorReco[3] = pTpi;
293   vectorReco[4] = pTK;
294   vectorReco[5] = cT*1.E4;  // in micron
295   vectorReco[6] = dca*1.E4;  // in micron
296   vectorReco[7] = d0pi*1.E4;  // in micron
297   vectorReco[8] = d0K*1.E4;  // in micron
298   vectorReco[9] = d0xd0*1.E8;  // in micron^2
299   vectorReco[10] = cosPointingAngle;  // in micron
300   vectorReco[11] = phi;  
301   vectorReco[12] = fzPrimVertex;    // z of reconstructed of primary vertex
302   vectorReco[13] = fCentValue;
303   vectorReco[14] = fFake;      // whether the reconstructed candidate was a fake (fFake = 0) or not (fFake = 2) 
304
305   bFillRecoValues = kTRUE;
306
307   return bFillRecoValues;
308 }
309
310
311 //_____________________________________________________________
312 Bool_t AliCFVertexingHFCascade::CheckMCChannelDecay() const
313
314   // check the required decay channel
315
316   Bool_t checkCD = kFALSE;
317
318   
319   Int_t daughter0 = fmcPartCandidate->GetDaughter(0);
320   Int_t daughter1 = fmcPartCandidate->GetDaughter(1);
321   AliAODMCParticle* mcPartDaughter0 = dynamic_cast<AliAODMCParticle*>(fmcArray->At(daughter0));
322   AliAODMCParticle* mcPartDaughter1 = dynamic_cast<AliAODMCParticle*>(fmcArray->At(daughter1));
323
324   if (!mcPartDaughter0 || !mcPartDaughter1) {
325     AliDebug (2,"Problems in the MC Daughters\n");
326     return checkCD;
327   }
328
329   if (!(TMath::Abs(mcPartDaughter0->GetPdgCode())==421 &&
330         TMath::Abs(mcPartDaughter1->GetPdgCode())==211) && 
331       !(TMath::Abs(mcPartDaughter0->GetPdgCode())==211 &&
332         TMath::Abs(mcPartDaughter1->GetPdgCode())==421)) {
333     AliDebug(2, "The D0 MC doesn't come from a Kpi decay, skipping!!");
334     return checkCD;  
335   }
336   
337   Double_t vectorD0[2] ={0.,0.};
338
339   // D* is a cascade ...evaluate the correct cascade
340   if (!EvaluateIfD0toKpi(mcPartDaughter0,vectorD0)) {
341     AliDebug(2, "Error! the D0 MC doesn't have correct daughters!!");
342     return checkCD;  
343   }
344    
345   checkCD = kTRUE;
346   return checkCD;
347   
348 }
349
350 //__________________________________________
351 Bool_t AliCFVertexingHFCascade::EvaluateIfD0toKpi(AliAODMCParticle* neutralDaugh, Double_t* vectorD0)const
352 {  
353   //
354   // chack wether D0 is decaing into kpi
355   //
356   
357   Bool_t isHadronic = kFALSE;
358   
359   Int_t daughterD00 = neutralDaugh->GetDaughter(0);
360   Int_t daughterD01 = neutralDaugh->GetDaughter(1);
361   
362   AliDebug(2, Form("daughter0 = %d and daughter1 = %d",daughterD00,daughterD01));
363   if (daughterD00 == 0 || daughterD01 == 0) {
364     AliDebug(2, "Error! the D0 MC doesn't have correct daughters!!");
365     return isHadronic;  
366   }
367   
368   if (TMath::Abs(daughterD01 - daughterD00) != 1) { // should be everytime true - see PDGdatabooklet
369     AliDebug(2, "The D0 MC doesn't come from a 2-prong decay, skipping!!");
370     return isHadronic;  
371   }
372   
373   AliAODMCParticle* mcPartDaughterD00 = dynamic_cast<AliAODMCParticle*>(fmcArray->At(daughterD00));
374   AliAODMCParticle* mcPartDaughterD01 = dynamic_cast<AliAODMCParticle*>(fmcArray->At(daughterD01));
375   if (!mcPartDaughterD00 || !mcPartDaughterD01) {
376     AliWarning("D0 MC analysis: At least one Daughter Particle not found in tree, skipping"); 
377     return isHadronic;  
378   }
379   
380   if (!(TMath::Abs(mcPartDaughterD00->GetPdgCode())==321 &&
381         TMath::Abs(mcPartDaughterD01->GetPdgCode())==211) && 
382       !(TMath::Abs(mcPartDaughterD00->GetPdgCode())==211 &&
383         TMath::Abs(mcPartDaughterD01->GetPdgCode())==321)) {
384     AliDebug(2, "The D0 MC doesn't come from a Kpi decay, skipping!!");
385     return isHadronic;  
386   }
387   
388   Double_t pTD0pi = 0;
389   Double_t pTD0K = 0;
390   
391   
392   if (TMath::Abs(mcPartDaughterD00->GetPdgCode()) == 211) {
393     pTD0pi = mcPartDaughterD00->Pt();
394     pTD0K = mcPartDaughterD01->Pt();
395   }
396   else {
397     pTD0pi = mcPartDaughterD01->Pt();
398     pTD0K  = mcPartDaughterD00->Pt();
399   }
400   
401   isHadronic = kTRUE;
402   
403   vectorD0[0] = pTD0pi;
404   vectorD0[1] = pTD0K;
405  
406   return isHadronic;
407
408 }
409
410 //___________________________________________________________
411
412 void AliCFVertexingHFCascade::SetPtAccCut(Float_t* ptAccCut)
413 {
414         //
415         // setting the pt cut to be used in the Acceptance steps (MC+Reco)
416         //
417
418         AliInfo("The 3rd element of the pt cut array will correspond to the cut applied to the soft pion - please check that it is correct");
419         if (fProngs>0){
420                 for (Int_t iP=0; iP<fProngs; iP++){
421                         fPtAccCut[iP]=ptAccCut[iP];
422                 }
423         }
424         return;
425 }               
426
427
428
429 //___________________________________________________________
430
431 void AliCFVertexingHFCascade::SetEtaAccCut(Float_t* etaAccCut)
432 {
433         //
434         // setting the eta cut to be used in the Acceptance steps (MC+Reco)
435         //
436
437         AliInfo("The 3rd element of the eta cut array will correspond to the cut applied to the soft pion - please check that it is correct");
438         if (fProngs>0){
439                 for (Int_t iP=0; iP<fProngs; iP++){
440                         fEtaAccCut[iP]=etaAccCut[iP];
441                 }
442         }
443         return;
444 }       
445 //___________________________________________________________
446
447 void AliCFVertexingHFCascade::SetAccCut(Float_t* ptAccCut, Float_t* etaAccCut)
448 {
449         //
450         // setting the pt and eta cut to be used in the Acceptance steps (MC+Reco)
451         //
452
453         AliInfo("The 3rd element of the pt and cut array will correspond to the cut applied to the soft pion - please check that they are correct");
454         if (fProngs>0){
455                 for (Int_t iP=0; iP<fProngs; iP++){
456                         fPtAccCut[iP]=ptAccCut[iP];
457                         fEtaAccCut[iP]=etaAccCut[iP];
458                 }
459         }
460         return;
461 }               
462
463 //___________________________________________________________
464
465 void AliCFVertexingHFCascade::SetAccCut()
466 {
467         //
468         // setting the pt and eta cut to be used in the Acceptance steps (MC+Reco)
469         //
470   
471   AliAODMCParticle* mcPartDaughter = dynamic_cast<AliAODMCParticle*>(fmcArray->At(fLabelArray[2]));  // should be the soft pion...  
472   if(!mcPartDaughter) return;
473   Int_t mother =  mcPartDaughter->GetMother();
474   AliAODMCParticle* mcMother = dynamic_cast<AliAODMCParticle*>(fmcArray->At(mother)); 
475   if(!mcMother) return;
476
477   if (TMath::Abs(mcPartDaughter->GetPdgCode())!= 211 || TMath::Abs(mcMother->GetPdgCode())!=413){
478     AliFatal("Apparently the soft pion is not in the third position, causing a crash!!");
479   }              
480   if (fProngs>0){
481     for (Int_t iP=0; iP<fProngs-1; iP++){
482       fPtAccCut[iP]=0.1;
483       fEtaAccCut[iP]=0.9;
484     }
485     fPtAccCut[2]=0.06;  // soft pion
486     fEtaAccCut[2]=0.9;  // soft pion
487   }
488   return;
489 }               
490
491 //_____________________________________________________________
492 Double_t AliCFVertexingHFCascade::GetEtaProng(Int_t iProng) const 
493 {
494         //
495         // getting eta of the prong - overload the mother class method
496         //
497
498  if (fRecoCandidate){
499    
500    AliAODRecoCascadeHF* dstarD0pi = (AliAODRecoCascadeHF*)fRecoCandidate;
501
502    Double_t etaProng =-9999;
503     if(iProng==0) etaProng =dstarD0pi->Get2Prong()->EtaProng(0);
504     if(iProng==1) etaProng =dstarD0pi->Get2Prong()->EtaProng(1);
505     if(iProng==2) etaProng =dstarD0pi->EtaProng(1);
506     
507     return etaProng;
508     
509   }
510   return 999999;    
511 }
512 //_____________________________________________________________
513 Double_t AliCFVertexingHFCascade::GetPtProng(Int_t iProng) const 
514 {
515         //
516         // getting pt of the prong
517         //
518   
519   if (fRecoCandidate){
520
521     AliAODRecoCascadeHF* dstarD0pi = (AliAODRecoCascadeHF*)fRecoCandidate;
522     Double_t ptProng= -9999;
523     if(iProng==0) ptProng =dstarD0pi->Get2Prong()->PtProng(0);
524     if(iProng==1) ptProng =dstarD0pi->Get2Prong()->PtProng(1);
525     if(iProng==2) ptProng =dstarD0pi->PtProng(1);
526     
527     //  Double_t ptProng = fRecoCandidate->PtProng(iProng);  
528     return ptProng;
529     
530   }
531   return 999999;  
532   
533 }