]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGHF/vertexingHF/AliVertexingHFUtils.cxx
Merge branch 'master' of https://git.cern.ch/reps/AliRoot
[u/mrichter/AliRoot.git] / PWGHF / vertexingHF / AliVertexingHFUtils.cxx
1 /**************************************************************************
2  * Copyright(c) 2007-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 #include <TMath.h>
17 #include <TRandom.h>
18 #include <TProfile.h>
19 #include <TClonesArray.h>
20 #include <TH1F.h>
21 #include <TH2F.h>
22 #include <TF1.h>
23 #include <TParticle.h>
24 #include "AliStack.h"
25 #include "AliAODEvent.h"
26 #include "AliAODMCHeader.h"
27 #include "AliGenEventHeader.h"
28 #include "AliAODMCParticle.h"
29 #include "AliAODRecoDecayHF.h"
30 #include "AliVertexingHFUtils.h"
31
32 /* $Id$ */
33
34 ///////////////////////////////////////////////////////////////////
35 //                                                               //
36 // Class with functions useful for different D2H analyses        //
37 // - event plane resolution                                      //
38 // - <pt> calculation with side band subtraction                 //
39 // - tracklet multiplicity calculation                            //
40 // Origin: F.Prino, Torino, prino@to.infn.it                     //
41 //                                                               //
42 ///////////////////////////////////////////////////////////////////
43
44 ClassImp(AliVertexingHFUtils)
45
46 //______________________________________________________________________
47 AliVertexingHFUtils::AliVertexingHFUtils():TObject(),
48   fK(1),
49   fSubRes(1.),
50   fMinEtaForTracklets(-1.),
51   fMaxEtaForTracklets(1.)
52 {
53   // Default contructor
54 }
55
56
57 //______________________________________________________________________
58 AliVertexingHFUtils::AliVertexingHFUtils(Int_t k):
59   TObject(),
60   fK(k),
61   fSubRes(1.),
62   fMinEtaForTracklets(-1.),
63   fMaxEtaForTracklets(1.)
64 {
65   // Standard constructor
66 }
67
68
69 //______________________________________________________________________
70 void AliVertexingHFUtils::ComputeSignificance(Double_t signal, Double_t  errsignal, Double_t  background, Double_t  errbackground, Double_t &significance,Double_t &errsignificance){
71   // calculate significance from S, B and errors
72
73
74   Double_t errSigSq=errsignal*errsignal;
75   Double_t errBkgSq=errbackground*errbackground;
76   Double_t sigPlusBkg=signal+background;
77   if(sigPlusBkg>0. && signal>0.){
78     significance =  signal/TMath::Sqrt(signal+background);
79     errsignificance = significance*TMath::Sqrt((errSigSq+errBkgSq)/(4.*sigPlusBkg*sigPlusBkg)+(background/sigPlusBkg)*errSigSq/signal/signal);
80   }else{
81     significance=0.;
82     errsignificance=0.;
83   }
84   return;
85
86 }
87 //______________________________________________________________________
88 Double_t AliVertexingHFUtils::Pol(Double_t x, Int_t k){
89   // compute chi from polynomial approximation
90   Double_t c[5];
91   if(k==1){ 
92     c[0]=0.626657; c[1]=0.; c[2]=-0.09694; c[3]=0.02754; c[4]=-0.002283;
93   }
94   else if(k==2){
95     c[0]=0.; c[1]=0.25; c[2]=-0.011414; c[3]=-0.034726; c[4]=0.006815;
96   }
97   else return -1;
98   return c[0]*x+c[1]*x*x+c[2]*x*x*x+c[3]*x*x*x*x+c[4]*x*x*x*x*x;
99 }
100
101 //______________________________________________________________________
102 Double_t AliVertexingHFUtils:: ResolK1(Double_t x){
103   return TMath::Sqrt(TMath::Pi()/8)*x*TMath::Exp(-x*x/4)*(TMath::BesselI0(x*x/4)+TMath::BesselI1(x*x/4));
104 }
105
106
107 //______________________________________________________________________
108 Double_t AliVertexingHFUtils::FindChi(Double_t res,  Int_t k){
109   // compute chi variable (=v2*sqrt(N)) from external values
110
111   Double_t x1=0;
112   Double_t x2=15;
113   Double_t y1,y2;
114   if(k==1){
115     y1=ResolK1(x1)-res;
116     y2=ResolK1(x2)-res;
117   }
118   else if(k==2){
119     y1=Pol(x1,2)-res;
120     y2=Pol(x2,2)-res;
121   }
122   else return -1;
123
124   if(y1*y2>0) return -1;
125   if(y1==0) return y1;
126   if(y2==0) return y2;
127   Double_t xmed,ymed;
128   Int_t jiter=0;
129   while((x2-x1)>0.0001){
130     xmed=0.5*(x1+x2);
131     if(k==1){
132       y1=ResolK1(x1)-res;
133       y2=ResolK1(x2)-res;
134       ymed=ResolK1(xmed)-res;
135     }
136     else if(k==2){
137       y1=Pol(x1,2)-res;
138       y2=Pol(x2,2)-res;
139       ymed=Pol(xmed,2)-res;
140     }
141     else return -1;
142     if((y1*ymed)<0) x2=xmed;
143     if((y2*ymed)<0)x1=xmed;
144     if(ymed==0) return xmed;
145     jiter++;
146   }
147   return 0.5*(x1+x2);
148 }
149
150 //______________________________________________________________________
151 Double_t AliVertexingHFUtils::GetFullEvResol(Double_t resSub, Int_t k){
152   // computes event plane resolution starting from sub event resolution
153   Double_t chisub=FindChi(resSub,k);
154   Double_t chifull=chisub*TMath::Sqrt(2);
155   if(k==1) return ResolK1(chifull);
156   else if(k==2) return Pol(chifull,2);
157   else{
158     printf("k should be <=2\n");
159     return 1.;
160   }
161 }
162
163 //______________________________________________________________________
164 Double_t AliVertexingHFUtils::GetFullEvResol(const TH1F* hSubEvCorr, Int_t k){
165   // computes event plane resolution starting from sub event correlation histogram
166   if(!hSubEvCorr) return 1.;
167   Double_t resSub=GetSubEvResol(hSubEvCorr);
168   return GetFullEvResol(resSub,k);
169 }
170 //______________________________________________________________________
171 Double_t AliVertexingHFUtils::GetFullEvResolLowLim(const TH1F* hSubEvCorr, Int_t k){
172   // computes low limit event plane resolution starting from sub event correlation histogram
173   if(!hSubEvCorr) return 1.;
174   Double_t resSub=GetSubEvResolLowLim(hSubEvCorr);
175   printf("%f\n",resSub);
176   return GetFullEvResol(resSub,k);  
177 }
178 //______________________________________________________________________
179 Double_t AliVertexingHFUtils::GetFullEvResolHighLim(const TH1F* hSubEvCorr, Int_t k){
180   // computes high limit event plane resolution starting from sub event correlation histogram
181   if(!hSubEvCorr) return 1.;
182   Double_t resSub=GetSubEvResolHighLim(hSubEvCorr);
183   printf("%f\n",resSub);
184   return GetFullEvResol(resSub,k);  
185 }
186 //______________________________________________________________________
187 Int_t AliVertexingHFUtils::GetNumberOfTrackletsInEtaRange(AliAODEvent* ev, Double_t mineta, Double_t maxeta){
188   // counts tracklets in given eta range
189   AliAODTracklets* tracklets=ev->GetTracklets();
190   Int_t nTr=tracklets->GetNumberOfTracklets();
191   Int_t count=0;
192   for(Int_t iTr=0; iTr<nTr; iTr++){
193     Double_t theta=tracklets->GetTheta(iTr);
194     Double_t eta=-TMath::Log(TMath::Tan(theta/2.));
195     if(eta>mineta && eta<maxeta) count++;
196   }
197   return count;
198 }
199 //______________________________________________________________________
200 Int_t AliVertexingHFUtils::GetGeneratedMultiplicityInEtaRange(TClonesArray* arrayMC, Double_t mineta, Double_t maxeta){
201   // counts generated particles in fgiven eta range
202
203   Int_t nChargedMC=0;
204   for(Int_t i=0;i<arrayMC->GetEntriesFast();i++){
205     AliAODMCParticle *part=(AliAODMCParticle*)arrayMC->UncheckedAt(i);
206     Int_t charge = part->Charge();
207     Double_t eta = part->Eta();
208     if(charge!=0 && eta>mineta && eta<maxeta) nChargedMC++;
209   } 
210   return nChargedMC;
211 }
212 //______________________________________________________________________
213 Int_t AliVertexingHFUtils::GetGeneratedPrimariesInEtaRange(TClonesArray* arrayMC, Double_t mineta, Double_t maxeta){
214   // counts generated primary particles in given eta range
215
216   Int_t nChargedMC=0;
217   for(Int_t i=0;i<arrayMC->GetEntriesFast();i++){
218     AliAODMCParticle *part=(AliAODMCParticle*)arrayMC->UncheckedAt(i);
219     Int_t charge = part->Charge();
220     Double_t eta = part->Eta();
221     if(charge!=0 && eta>mineta && eta<maxeta){
222       if(part->IsPrimary())nChargedMC++;
223     } 
224   }  
225   return nChargedMC;
226 }
227 //______________________________________________________________________
228 Int_t AliVertexingHFUtils::GetGeneratedPhysicalPrimariesInEtaRange(TClonesArray* arrayMC, Double_t mineta, Double_t maxeta){
229   // counts generated primary particles in given eta range
230
231   Int_t nChargedMC=0;
232   for(Int_t i=0;i<arrayMC->GetEntriesFast();i++){
233     AliAODMCParticle *part=(AliAODMCParticle*)arrayMC->UncheckedAt(i);
234     Int_t charge = part->Charge();
235     Double_t eta = part->Eta();
236     if(charge!=0 && eta>mineta && eta<maxeta){
237       if(part->IsPhysicalPrimary())nChargedMC++;
238     } 
239   }
240   return nChargedMC;
241 }
242
243
244 //______________________________________________________________________
245 Double_t AliVertexingHFUtils::GetVZEROAEqualizedMultiplicity(AliAODEvent* ev){
246   //
247   // Method to get VZERO-A equalized multiplicty as done in AliCentralitySelectionTask
248   //  getting the equalized VZERO factors from the tender or AOD
249   // http://git.cern.ch/pubweb/AliRoot.git/blob/HEAD:/ANALYSIS/AliCentralitySelectionTask.cxx#l1345
250
251   Double_t multV0AEq=0;
252   for(Int_t iCh = 32; iCh < 64; ++iCh) {
253     Double_t mult = ev->GetVZEROEqMultiplicity(iCh);
254     multV0AEq += mult;
255   }
256   return multV0AEq;
257 }
258
259 //______________________________________________________________________
260 Double_t AliVertexingHFUtils::GetVZEROCEqualizedMultiplicity(AliAODEvent* ev){
261   // Method to get VZERO-C equalized multiplicty as done in AliCentralitySelectionTask
262   //  getting the equalized VZERO factors from the tender or AOD
263   // http://git.cern.ch/pubweb/AliRoot.git/blob/HEAD:/ANALYSIS/AliCentralitySelectionTask.cxx#l1345
264
265   Double_t multV0CEq=0;
266   for(Int_t iCh = 0; iCh < 32; ++iCh) {
267     Double_t mult = ev->GetVZEROEqMultiplicity(iCh);
268     multV0CEq += mult;
269   }
270   return multV0CEq;
271 }
272
273 //______________________________________________________________________
274 void AliVertexingHFUtils::AveragePt(Float_t& averagePt, Float_t& errorPt,Float_t ptmin,Float_t ptmax, TH2F* hMassD, Float_t massFromFit, Float_t sigmaFromFit, TF1* funcB2, Float_t sigmaRangeForSig,Float_t sigmaRangeForBkg, Float_t minMass, Float_t maxMass, Int_t rebin){
275
276   // Compute <pt> from 2D histogram M vs pt
277
278   //Make 2D histos in the desired pt range
279   Int_t start=hMassD->FindBin(ptmin);
280   Int_t end=hMassD->FindBin(ptmax)-1;
281   const Int_t nx=end-start;
282   TH2F *hMassDpt=new TH2F("hptmass","hptmass",nx,ptmin,ptmax,hMassD->GetNbinsY(),hMassD->GetYaxis()->GetBinLowEdge(1),hMassD->GetYaxis()->GetBinLowEdge(hMassD->GetNbinsY())+hMassD->GetYaxis()->GetBinWidth(hMassD->GetNbinsY()));
283   for(Int_t ix=start;ix<end;ix++){
284     for(Int_t iy=1;iy<=hMassD->GetNbinsY();iy++){
285       hMassDpt->SetBinContent(ix-start+1,iy,hMassD->GetBinContent(ix,iy));
286       hMassDpt->SetBinError(ix-start+1,iy,hMassD->GetBinError(ix,iy));
287     }
288   }
289
290   Double_t minMassSig=massFromFit-sigmaRangeForSig*sigmaFromFit;
291   Double_t maxMassSig=massFromFit+sigmaRangeForSig*sigmaFromFit;
292   Int_t minBinSig=hMassD->GetYaxis()->FindBin(minMassSig);
293   Int_t maxBinSig=hMassD->GetYaxis()->FindBin(maxMassSig);
294   Double_t minMassSigBin=hMassD->GetYaxis()->GetBinLowEdge(minBinSig);
295   Double_t maxMassSigBin=hMassD->GetYaxis()->GetBinLowEdge(maxBinSig)+hMassD->GetYaxis()->GetBinWidth(maxBinSig);
296   //  printf("Signal Fit Limits = %f %f\n",minMassSigBin,maxMassSigBin);
297
298   Double_t maxMassBkgLow=massFromFit-sigmaRangeForBkg*sigmaFromFit;
299   Int_t minBinBkgLow=TMath::Max(hMassD->GetYaxis()->FindBin(minMass),2);
300   Int_t maxBinBkgLow=hMassD->GetYaxis()->FindBin(maxMassBkgLow);
301   Double_t minMassBkgLowBin=hMassD->GetYaxis()->GetBinLowEdge(minBinBkgLow);
302   Double_t maxMassBkgLowBin=hMassD->GetYaxis()->GetBinLowEdge(maxBinBkgLow)+hMassD->GetYaxis()->GetBinWidth(maxBinBkgLow);
303   Double_t minMassBkgHi=massFromFit+sigmaRangeForBkg*sigmaFromFit;
304   Int_t minBinBkgHi=hMassD->GetYaxis()->FindBin(minMassBkgHi);
305   Int_t maxBinBkgHi=TMath::Min(hMassD->GetYaxis()->FindBin(maxMass),hMassD->GetNbinsY()-1);
306   Double_t minMassBkgHiBin=hMassD->GetYaxis()->GetBinLowEdge(minBinBkgHi);
307   Double_t maxMassBkgHiBin=hMassD->GetYaxis()->GetBinLowEdge(maxBinBkgHi)+hMassD->GetYaxis()->GetBinWidth(maxBinBkgHi);
308   //  printf("BKG Fit Limits = %f %f  && %f %f\n",minMassBkgLowBin,maxMassBkgLowBin,minMassBkgHiBin,maxMassBkgHiBin);
309
310   Double_t bkgSig=funcB2->Integral(minMassSigBin,maxMassSigBin);
311   Double_t bkgLow=funcB2->Integral(minMassBkgLowBin,maxMassBkgLowBin);
312   Double_t bkgHi=funcB2->Integral(minMassBkgHiBin,maxMassBkgHiBin);
313   //  printf("Background integrals = %f %f %f\n",bkgLow,bkgSig,bkgHi);
314
315   TH1F* hMptBkgLo=(TH1F*)hMassDpt->ProjectionX("hPtBkgLoBin",minBinBkgLow,maxBinBkgLow);
316   TH1F* hMptBkgHi=(TH1F*)hMassDpt->ProjectionX("hPtBkgHiBin",minBinBkgHi,maxBinBkgHi);
317   TH1F* hMptSigReg=(TH1F*)hMassDpt->ProjectionX("hCPtBkgSigBin",minBinSig,maxBinSig);
318
319   hMptBkgLo->Rebin(rebin);
320   hMptBkgHi->Rebin(rebin);
321   hMptSigReg->Rebin(rebin);
322
323   hMptBkgLo->Sumw2();
324   hMptBkgHi->Sumw2();
325   TH1F* hMptBkgLoScal=(TH1F*)hMptBkgLo->Clone("hPtBkgLoScalBin");
326   hMptBkgLoScal->Scale(bkgSig/bkgLow);
327   TH1F* hMptBkgHiScal=(TH1F*)hMptBkgHi->Clone("hPtBkgHiScalBin");
328   hMptBkgHiScal->Scale(bkgSig/bkgHi);
329
330   TH1F* hMptBkgAver=0x0;
331   hMptBkgAver=(TH1F*)hMptBkgLoScal->Clone("hPtBkgAverBin");
332   hMptBkgAver->Add(hMptBkgHiScal);
333   hMptBkgAver->Scale(0.5);
334   TH1F* hMptSig=(TH1F*)hMptSigReg->Clone("hCPtSigBin");
335   hMptSig->Add(hMptBkgAver,-1.);   
336  
337   averagePt = hMptSig->GetMean();
338   errorPt = hMptSig->GetMeanError();
339
340   delete hMptBkgLo;
341   delete hMptBkgHi;
342   delete hMptSigReg;
343   delete hMptBkgLoScal;
344   delete hMptBkgHiScal;
345   delete hMptBkgAver;
346   delete hMassDpt;
347   delete hMptSig;
348
349 }
350 //____________________________________________________________________________
351 Bool_t AliVertexingHFUtils::CheckT0TriggerFired(AliAODEvent* aodEv){
352   // check if T0VTX trigger was fired, based on a workaround suggested by Alla
353   const Double32_t *mean = aodEv->GetT0TOF();
354   if(mean && mean[0]<9999.) return kTRUE;
355   else return kFALSE;
356 }
357 //____________________________________________________________________________
358 Double_t AliVertexingHFUtils::GetTrueImpactParameterDzero(AliAODMCHeader *mcHeader, TClonesArray* arrayMC, AliAODMCParticle *partD) {
359   // true impact parameter calculation for Dzero
360
361   if(!partD || !arrayMC || !mcHeader) return 99999.;
362   Int_t code=partD->GetPdgCode();
363   if(TMath::Abs(code)!=421) return 99999.;
364
365   Double_t vtxTrue[3];
366   mcHeader->GetVertex(vtxTrue);
367   Double_t origD[3];
368   partD->XvYvZv(origD);
369   Short_t charge=partD->Charge();
370   Double_t pXdauTrue[2],pYdauTrue[2],pZdauTrue[2];
371   for(Int_t iDau=0; iDau<2; iDau++){
372     pXdauTrue[iDau]=0.;
373     pYdauTrue[iDau]=0.;
374     pZdauTrue[iDau]=0.;
375   }
376
377   Int_t nDau=partD->GetNDaughters();
378   Int_t labelFirstDau = partD->GetDaughter(0); 
379   if(nDau==2){
380     for(Int_t iDau=0; iDau<2; iDau++){
381       Int_t ind = labelFirstDau+iDau;
382       AliAODMCParticle* part = dynamic_cast<AliAODMCParticle*>(arrayMC->At(ind));
383       if(!part){
384         printf("Daughter particle not found in MC array");
385         return 99999.;
386       } 
387       pXdauTrue[iDau]=part->Px();
388       pYdauTrue[iDau]=part->Py();
389       pZdauTrue[iDau]=part->Pz();
390     }
391   }else{
392     return 99999.;
393   }
394
395   Double_t d0dummy[2]={0.,0.};
396   AliAODRecoDecayHF aodDvsMC(vtxTrue,origD,2,charge,pXdauTrue,pYdauTrue,pZdauTrue,d0dummy);
397   return aodDvsMC.ImpParXY();
398
399 }
400 //____________________________________________________________________________
401 Double_t AliVertexingHFUtils::GetTrueImpactParameterDplus(AliAODMCHeader *mcHeader, TClonesArray* arrayMC, AliAODMCParticle *partD) {
402   // true impact parameter calculation for Dplus
403
404   if(!partD || !arrayMC || !mcHeader) return 99999.;
405   Int_t code=partD->GetPdgCode();
406   if(TMath::Abs(code)!=411) return 99999.;
407
408   Double_t vtxTrue[3];
409   mcHeader->GetVertex(vtxTrue);
410   Double_t origD[3];
411   partD->XvYvZv(origD);
412   Short_t charge=partD->Charge();
413   Double_t pXdauTrue[3],pYdauTrue[3],pZdauTrue[3];
414   for(Int_t iDau=0; iDau<3; iDau++){
415     pXdauTrue[iDau]=0.;
416     pYdauTrue[iDau]=0.;
417     pZdauTrue[iDau]=0.;
418   }
419
420   Int_t nDau=partD->GetNDaughters();
421   Int_t labelFirstDau = partD->GetDaughter(0); 
422   if(nDau==3){
423     for(Int_t iDau=0; iDau<3; iDau++){
424       Int_t ind = labelFirstDau+iDau;
425       AliAODMCParticle* part = dynamic_cast<AliAODMCParticle*>(arrayMC->At(ind));
426       if(!part){
427         printf("Daughter particle not found in MC array");
428         return 99999.;
429       } 
430       pXdauTrue[iDau]=part->Px();
431       pYdauTrue[iDau]=part->Py();
432       pZdauTrue[iDau]=part->Pz();
433     }
434   }else if(nDau==2){
435     Int_t theDau=0;
436     for(Int_t iDau=0; iDau<2; iDau++){
437       Int_t ind = labelFirstDau+iDau;
438       AliAODMCParticle* part = dynamic_cast<AliAODMCParticle*>(arrayMC->At(ind));
439       if(!part){
440         printf("Daughter particle not found in MC array");
441         return 99999.;
442       } 
443       Int_t pdgCode=TMath::Abs(part->GetPdgCode());
444       if(pdgCode==211 || pdgCode==321){
445         pXdauTrue[theDau]=part->Px();
446         pYdauTrue[theDau]=part->Py();
447         pZdauTrue[theDau]=part->Pz();
448         ++theDau;
449       }else{
450         Int_t nDauRes=part->GetNDaughters();
451         if(nDauRes==2){
452           Int_t labelFirstDauRes = part->GetDaughter(0);        
453           for(Int_t iDauRes=0; iDauRes<2; iDauRes++){
454             Int_t indDR = labelFirstDauRes+iDauRes;
455             AliAODMCParticle* partDR = dynamic_cast<AliAODMCParticle*>(arrayMC->At(indDR));
456             if(!partDR){
457               printf("Daughter particle not found in MC array");
458               return 99999.;
459             } 
460             
461             Int_t pdgCodeDR=TMath::Abs(partDR->GetPdgCode());
462             if(pdgCodeDR==211 || pdgCodeDR==321){
463               pXdauTrue[theDau]=partDR->Px();
464               pYdauTrue[theDau]=partDR->Py();
465               pZdauTrue[theDau]=partDR->Pz();
466               ++theDau;
467             }
468           }
469         }
470       }
471     }
472     if(theDau!=3){
473       printf("Wrong number of decay prongs");
474       return 99999.;
475     }
476   }
477
478   Double_t d0dummy[3]={0.,0.,0.};
479   AliAODRecoDecayHF aodDvsMC(vtxTrue,origD,3,charge,pXdauTrue,pYdauTrue,pZdauTrue,d0dummy);
480   return aodDvsMC.ImpParXY();
481
482 }
483
484
485
486 //____________________________________________________________________________
487 Double_t AliVertexingHFUtils::GetCorrectedNtracklets(TProfile* estimatorAvg, Double_t uncorrectedNacc, Double_t vtxZ, Double_t refMult) {
488   //
489   // Correct the number of accepted tracklets based on a TProfile Hist
490   //
491   //
492
493   if(TMath::Abs(vtxZ)>10.0){
494     //    printf("ERROR: Z vertex out of range for correction of multiplicity\n");
495     return uncorrectedNacc;
496   }
497
498   if(!estimatorAvg){
499     printf("ERROR: Missing TProfile for correction of multiplicity\n");
500     return uncorrectedNacc;
501   }
502
503   Double_t localAvg = estimatorAvg->GetBinContent(estimatorAvg->FindBin(vtxZ));
504
505   Double_t deltaM = uncorrectedNacc*(refMult/localAvg - 1);
506
507   Double_t correctedNacc = uncorrectedNacc + (deltaM>0 ? 1 : -1) * gRandom->Poisson(TMath::Abs(deltaM));
508
509   return correctedNacc;
510 }
511 //______________________________________________________________________
512 TString AliVertexingHFUtils::GetGenerator(Int_t label, AliAODMCHeader* header){
513   // get the name of the generator that produced a given particle
514
515   Int_t nsumpart=0;
516   TList *lh=header->GetCocktailHeaders();
517   Int_t nh=lh->GetEntries();
518   for(Int_t i=0;i<nh;i++){
519     AliGenEventHeader* gh=(AliGenEventHeader*)lh->At(i);
520     TString genname=gh->GetName();
521     Int_t npart=gh->NProduced();
522     if(label>=nsumpart && label<(nsumpart+npart)) return genname;
523     nsumpart+=npart;
524   }
525   TString empty="";
526   return empty;
527 }
528 //_____________________________________________________________________
529 void AliVertexingHFUtils::GetTrackPrimaryGenerator(AliAODTrack *track,AliAODMCHeader *header,TClonesArray *arrayMC,TString &nameGen){
530
531   // method to check if a track comes from a given generator
532
533   Int_t lab=TMath::Abs(track->GetLabel());
534   nameGen=GetGenerator(lab,header);
535   
536   //  Int_t countControl=0;
537   
538   while(nameGen.IsWhitespace()){
539     AliAODMCParticle *mcpart= (AliAODMCParticle*)arrayMC->At(lab);
540     if(!mcpart){
541       printf("AliVertexingHFUtils::IsTrackInjected - BREAK: No valid AliAODMCParticle at label %i\n",lab);
542       break;
543     }
544     Int_t mother = mcpart->GetMother();
545     if(mother<0){
546       printf("AliVertexingHFUtils::IsTrackInjected - BREAK: Reached primary particle without valid mother\n");
547       break;
548     }
549     lab=mother;
550     nameGen=GetGenerator(mother,header);
551     // countControl++;
552     // if(countControl>=10){ // 10 = arbitrary number; protection from infinite loops
553     //   printf("AliVertexingHFUtils::IsTrackInjected - BREAK: Protection from infinite loop active\n");
554     //   break;
555     // }
556   }
557   
558   return;
559 }
560 //----------------------------------------------------------------------
561 Bool_t AliVertexingHFUtils::IsTrackInjected(AliAODTrack *track,AliAODMCHeader *header,TClonesArray *arrayMC){
562   // method to check if a track comes from the signal event or from the underlying Hijing event
563   TString nameGen;
564   GetTrackPrimaryGenerator(track,header,arrayMC,nameGen);
565   
566   if(nameGen.IsWhitespace() || nameGen.Contains("ijing")) return kFALSE;
567   
568   return kTRUE;
569 }
570 //____________________________________________________________________________
571 Bool_t AliVertexingHFUtils::IsCandidateInjected(AliAODRecoDecayHF *cand, AliAODMCHeader *header,TClonesArray *arrayMC){
572   // method to check if a D meson candidate comes from the signal event or from the underlying Hijing event
573
574   Int_t nprongs=cand->GetNProngs();
575   for(Int_t i=0;i<nprongs;i++){
576     AliAODTrack *daugh=(AliAODTrack*)cand->GetDaughter(i);
577     if(IsTrackInjected(daugh,header,arrayMC)) return kTRUE;
578   }
579   return kFALSE;
580 }
581 //____________________________________________________________________________
582 Bool_t AliVertexingHFUtils::HasCascadeCandidateAnyDaughInjected(AliAODRecoCascadeHF *cand, AliAODMCHeader *header,TClonesArray *arrayMC){
583   // method to check if a cascade candidate comes from the signal event or from the underlying Hijing event
584
585   AliAODTrack* bach = cand->GetBachelor();
586   if(IsTrackInjected(bach, header, arrayMC)) {
587     AliDebug(2, "Bachelor is injected, the whole candidate is then injected");
588     return kTRUE;
589   }
590   AliAODv0* v0 = cand->Getv0();
591   Int_t nprongs = v0->GetNProngs();
592   for(Int_t i = 0; i < nprongs; i++){
593     AliAODTrack *daugh = (AliAODTrack*)v0->GetDaughter(i);
594     if(IsTrackInjected(daugh,header,arrayMC)) {
595       AliDebug(2, Form("V0 daughter number %d is injected, the whole candidate is then injected", i));
596       return kTRUE;
597     }
598   }
599   return kFALSE;
600 }
601 //____________________________________________________________________________
602 Int_t AliVertexingHFUtils::CheckOrigin(AliStack* stack, TParticle *mcPart, Bool_t searchUpToQuark){
603   // checking whether the mother of the particles come from a charm or a bottom quark
604
605   Int_t pdgGranma = 0;
606   Int_t mother = 0;
607   mother = mcPart->GetFirstMother();
608   Int_t istep = 0;
609   Int_t abspdgGranma =0;
610   Bool_t isFromB=kFALSE;
611   Bool_t isQuarkFound=kFALSE;
612   while (mother >0 ){
613     istep++;
614     TParticle* mcGranma = stack->Particle(mother);
615     if (mcGranma){
616       pdgGranma = mcGranma->GetPdgCode();
617       abspdgGranma = TMath::Abs(pdgGranma);
618       if ((abspdgGranma > 500 && abspdgGranma < 600) || (abspdgGranma > 5000 && abspdgGranma < 6000)){
619         isFromB=kTRUE;
620       }
621       if(abspdgGranma==4 || abspdgGranma==5) isQuarkFound=kTRUE;
622       mother = mcGranma->GetFirstMother();
623     }else{
624       printf("CheckOrigin: Failed casting the mother particle!");
625       break;
626     }
627   }
628   if(searchUpToQuark && !isQuarkFound) return 0;
629   if(isFromB) return 5;
630   else return 4;
631
632 }
633 //____________________________________________________________________________
634 Int_t AliVertexingHFUtils::CheckOrigin(TClonesArray* arrayMC, AliAODMCParticle *mcPart, Bool_t searchUpToQuark){
635   // checking whether the mother of the particles come from a charm or a bottom quark
636
637   Int_t pdgGranma = 0;
638   Int_t mother = 0;
639   mother = mcPart->GetMother();
640   Int_t istep = 0;
641   Int_t abspdgGranma =0;
642   Bool_t isFromB=kFALSE;
643   Bool_t isQuarkFound=kFALSE;
644   while (mother >0 ){
645     istep++;
646     AliAODMCParticle* mcGranma = dynamic_cast<AliAODMCParticle*>(arrayMC->At(mother));
647     if (mcGranma){
648       pdgGranma = mcGranma->GetPdgCode();
649       abspdgGranma = TMath::Abs(pdgGranma);
650       if ((abspdgGranma > 500 && abspdgGranma < 600) || (abspdgGranma > 5000 && abspdgGranma < 6000)){
651         isFromB=kTRUE;
652       }
653       if(abspdgGranma==4 || abspdgGranma==5) isQuarkFound=kTRUE;
654       mother = mcGranma->GetMother();
655     }else{
656       printf("AliVertexingHFUtils::CheckOrigin: Failed casting the mother particle!");
657       break;
658     }
659   }
660   if(searchUpToQuark && !isQuarkFound) return 0;
661   if(isFromB) return 5;
662   else return 4;
663
664 }
665
666 //____________________________________________________________________________
667 Int_t AliVertexingHFUtils::CheckD0Decay(AliStack* stack, Int_t label, Int_t* arrayDauLab){
668   // Checks the D0 decay channel. Returns 1 for the D0->Kpi case, 2 for the D0->Kpipipi case, -1 in other cases
669
670   if(label<0) return -1;
671   TParticle* mcPart = stack->Particle(label);
672   Int_t pdgD=mcPart->GetPdgCode();
673   if(TMath::Abs(pdgD)!=421) return -1;
674   
675   Int_t nDau=mcPart->GetNDaughters();
676   
677   if(nDau==2){
678     Int_t daughter0 = mcPart->GetDaughter(0);
679     Int_t daughter1 = mcPart->GetDaughter(1);
680     TParticle* mcPartDaughter0 = stack->Particle(daughter0);
681     TParticle* mcPartDaughter1 = stack->Particle(daughter1);
682     if(!mcPartDaughter0 || !mcPartDaughter1) return -1;
683     arrayDauLab[0]=daughter0;
684     arrayDauLab[1]=daughter1;
685     Int_t pdgCode0=mcPartDaughter0->GetPdgCode();
686     Int_t pdgCode1=mcPartDaughter1->GetPdgCode();
687     if(!(TMath::Abs(pdgCode0)==321 && TMath::Abs(pdgCode1)==211) &&
688        !(TMath::Abs(pdgCode0)==211 && TMath::Abs(pdgCode1)==321)){
689       return -1;
690     }
691     if(TMath::Abs(pdgCode0)==321 && (pdgD*pdgCode0)>0) return -1;
692     if(TMath::Abs(pdgCode1)==321 && (pdgD*pdgCode1)>0) return -1;
693     if((pdgCode0*pdgCode1)>0) return -1;
694     Double_t sumPxDau=mcPartDaughter0->Px()+mcPartDaughter1->Px();
695     Double_t sumPyDau=mcPartDaughter0->Py()+mcPartDaughter1->Py();
696     Double_t sumPzDau=mcPartDaughter0->Pz()+mcPartDaughter1->Pz();
697     if(TMath::Abs(mcPart->Px()-sumPxDau)>0.001) return -2;
698     if(TMath::Abs(mcPart->Py()-sumPyDau)>0.001) return -2;
699     if(TMath::Abs(mcPart->Pz()-sumPzDau)>0.001) return -2;
700     return 1;
701   }
702   
703   if(nDau==3 || nDau==4){
704     Int_t nKaons=0;
705     Int_t nPions=0;
706     Double_t sumPxDau=0.;
707     Double_t sumPyDau=0.;
708     Double_t sumPzDau=0.;
709     Int_t nFoundKpi=0;
710     Int_t labelFirstDau = mcPart->GetDaughter(0);
711     for(Int_t iDau=0; iDau<nDau; iDau++){
712       Int_t indDau = labelFirstDau+iDau;
713       if(indDau<0) return -1;
714       TParticle* dau=stack->Particle(indDau);
715       if(!dau) return -1;
716       Int_t pdgdau=dau->GetPdgCode();
717       if(TMath::Abs(pdgdau)==321){
718         if(pdgD>0 && pdgdau>0) return -1;
719         if(pdgD<0 && pdgdau<0) return -1;
720         nKaons++;
721         sumPxDau+=dau->Px();
722         sumPyDau+=dau->Py();
723         sumPzDau+=dau->Pz();
724         arrayDauLab[nFoundKpi++]=indDau;
725         if(nFoundKpi>4) return -1;
726       }else if(TMath::Abs(pdgdau)==211){
727         nPions++;
728         sumPxDau+=dau->Px();
729         sumPyDau+=dau->Py();
730         sumPzDau+=dau->Pz();
731         arrayDauLab[nFoundKpi++]=indDau;
732         if(nFoundKpi>4) return -1;
733       }else if(TMath::Abs(pdgdau)==113 || TMath::Abs(pdgdau)==313){
734         Int_t nResDau=dau->GetNDaughters();
735         if(nResDau!=2) return -1;
736         Int_t indFirstResDau=dau->GetDaughter(0);
737         for(Int_t resDau=0; resDau<2; resDau++){
738           Int_t indResDau=indFirstResDau+resDau;
739           if(indResDau<0) return -1;
740           TParticle* resdau=stack->Particle(indResDau);
741           if(!resdau) return -1;
742           Int_t pdgresdau=resdau->GetPdgCode();
743           if(TMath::Abs(pdgresdau)==321){
744             if(pdgD>0 && pdgresdau>0) return -1;
745             if(pdgD<0 && pdgresdau<0) return -1;
746             nKaons++;
747             sumPxDau+=resdau->Px();
748             sumPyDau+=resdau->Py();
749             sumPzDau+=resdau->Pz();
750             arrayDauLab[nFoundKpi++]=indResDau;
751             if(nFoundKpi>4) return -1;
752           }
753           if(TMath::Abs(pdgresdau)==211){
754             nPions++;
755               sumPxDau+=resdau->Px();
756               sumPyDau+=resdau->Py();
757               sumPzDau+=resdau->Pz();
758               arrayDauLab[nFoundKpi++]=indResDau;
759               if(nFoundKpi>4) return -1;
760           }
761         }
762       }else{
763         return -1;
764       }
765     }
766     if(nPions!=3) return -1;
767     if(nKaons!=1) return -1;
768     if(TMath::Abs(mcPart->Px()-sumPxDau)>0.001) return -1;
769     if(TMath::Abs(mcPart->Py()-sumPyDau)>0.001) return -1;
770     if(TMath::Abs(mcPart->Pz()-sumPzDau)>0.001) return -1;
771     return 2;
772   }
773   return -1;
774 }
775
776 //____________________________________________________________________________
777 Int_t AliVertexingHFUtils::CheckD0Decay(TClonesArray* arrayMC, AliAODMCParticle *mcPart, Int_t* arrayDauLab){
778  // Checks the D0 decay channel. Returns 1 for the D0->Kpi case, 2 for the D0->Kpipipi case, -1 in other cases
779
780   Int_t pdgD=mcPart->GetPdgCode();
781   if(TMath::Abs(pdgD)!=421) return -1;
782
783   Int_t nDau=mcPart->GetNDaughters();
784
785   if(nDau==2){
786     Int_t daughter0 = mcPart->GetDaughter(0);
787     Int_t daughter1 = mcPart->GetDaughter(1);
788     AliAODMCParticle* mcPartDaughter0 = dynamic_cast<AliAODMCParticle*>(arrayMC->At(daughter0));
789     AliAODMCParticle* mcPartDaughter1 = dynamic_cast<AliAODMCParticle*>(arrayMC->At(daughter1));
790     if(!mcPartDaughter0 || !mcPartDaughter1) return -1;
791     arrayDauLab[0]=daughter0;
792     arrayDauLab[1]=daughter1;
793     Int_t pdgCode0=mcPartDaughter0->GetPdgCode();
794     Int_t pdgCode1=mcPartDaughter1->GetPdgCode();
795     if(!(TMath::Abs(pdgCode0)==321 && TMath::Abs(pdgCode1)==211) &&
796        !(TMath::Abs(pdgCode0)==211 && TMath::Abs(pdgCode1)==321)){
797       return -1;
798     }
799     if(TMath::Abs(pdgCode0)==321 && (pdgD*pdgCode0)>0) return -1;
800     if(TMath::Abs(pdgCode1)==321 && (pdgD*pdgCode1)>0) return -1;
801     if((pdgCode0*pdgCode1)>0) return -1;
802     Double_t sumPxDau=mcPartDaughter0->Px()+mcPartDaughter1->Px();
803     Double_t sumPyDau=mcPartDaughter0->Py()+mcPartDaughter1->Py();
804     Double_t sumPzDau=mcPartDaughter0->Pz()+mcPartDaughter1->Pz();
805     if(TMath::Abs(mcPart->Px()-sumPxDau)>0.001) return -2;
806     if(TMath::Abs(mcPart->Py()-sumPyDau)>0.001) return -2;
807     if(TMath::Abs(mcPart->Pz()-sumPzDau)>0.001) return -2;
808     return 1;
809   }
810
811   if(nDau==3 || nDau==4){
812     Int_t nKaons=0;
813     Int_t nPions=0;
814     Double_t sumPxDau=0.;
815     Double_t sumPyDau=0.;
816     Double_t sumPzDau=0.;
817     Int_t nFoundKpi=0;
818     Int_t labelFirstDau = mcPart->GetDaughter(0);
819     for(Int_t iDau=0; iDau<nDau; iDau++){
820       Int_t indDau = labelFirstDau+iDau;
821       if(indDau<0) return -1;
822       AliAODMCParticle* dau=dynamic_cast<AliAODMCParticle*>(arrayMC->At(indDau));
823       if(!dau) return -1;
824       Int_t pdgdau=dau->GetPdgCode();
825       if(TMath::Abs(pdgdau)==321){
826         if(pdgD>0 && pdgdau>0) return -1;
827         if(pdgD<0 && pdgdau<0) return -1;
828         nKaons++;
829         sumPxDau+=dau->Px();
830         sumPyDau+=dau->Py();
831         sumPzDau+=dau->Pz();
832         arrayDauLab[nFoundKpi++]=indDau;
833         if(nFoundKpi>4) return -1;
834       }else if(TMath::Abs(pdgdau)==211){
835         nPions++;
836         sumPxDau+=dau->Px();
837         sumPyDau+=dau->Py();
838         sumPzDau+=dau->Pz();
839         arrayDauLab[nFoundKpi++]=indDau;
840         if(nFoundKpi>4) return -1;
841       }else if(TMath::Abs(pdgdau)==113 || TMath::Abs(pdgdau)==313){
842         Int_t nResDau=dau->GetNDaughters();
843         if(nResDau!=2) return -1;
844         Int_t indFirstResDau=dau->GetDaughter(0);
845         for(Int_t resDau=0; resDau<2; resDau++){
846           Int_t indResDau=indFirstResDau+resDau;
847           if(indResDau<0) return -1;
848           AliAODMCParticle* resdau=dynamic_cast<AliAODMCParticle*>(arrayMC->At(indResDau));
849           if(!resdau) return -1;
850           Int_t pdgresdau=resdau->GetPdgCode();
851           if(TMath::Abs(pdgresdau)==321){
852             if(pdgD>0 && pdgresdau>0) return -1;
853             if(pdgD<0 && pdgresdau<0) return -1;
854             nKaons++;
855             sumPxDau+=resdau->Px();
856             sumPyDau+=resdau->Py();
857             sumPzDau+=resdau->Pz();
858             arrayDauLab[nFoundKpi++]=indResDau;
859             if(nFoundKpi>4) return -1;
860           }
861           if(TMath::Abs(pdgresdau)==211){
862             nPions++;
863             sumPxDau+=resdau->Px();
864             sumPyDau+=resdau->Py();
865             sumPzDau+=resdau->Pz();
866             arrayDauLab[nFoundKpi++]=indResDau;
867             if(nFoundKpi>4) return -1;
868           }
869         }
870       }else{
871         return -1;
872       }
873     }
874     if(nPions!=3) return -1;
875     if(nKaons!=1) return -1;
876     if(TMath::Abs(mcPart->Px()-sumPxDau)>0.001) return -1;
877     if(TMath::Abs(mcPart->Py()-sumPyDau)>0.001) return -1;
878     if(TMath::Abs(mcPart->Pz()-sumPzDau)>0.001) return -1;
879     return 2;
880   }
881   return -1;
882 }
883 //____________________________________________________________________________
884 Int_t AliVertexingHFUtils::CheckDplusDecay(AliStack* stack, Int_t label, Int_t* arrayDauLab){
885   // Checks the Dplus decay channel. Returns 1 for the non-resonant case, 2 for the resonant case, -1 in other cases
886
887   if(label<0) return -1;
888   TParticle* mcPart = stack->Particle(label);
889   Int_t pdgD=mcPart->GetPdgCode();
890   if(TMath::Abs(pdgD)!=411) return -1;
891
892   Int_t nDau=mcPart->GetNDaughters();
893   Int_t labelFirstDau = mcPart->GetDaughter(0);
894   Int_t nKaons=0;
895   Int_t nPions=0;
896   Double_t sumPxDau=0.;
897   Double_t sumPyDau=0.;
898   Double_t sumPzDau=0.;
899   Int_t nFoundKpi=0;
900   
901   if(nDau==3 || nDau==2){
902     for(Int_t iDau=0; iDau<nDau; iDau++){
903       Int_t indDau = labelFirstDau+iDau;
904       if(indDau<0) return -1;
905       TParticle* dau=stack->Particle(indDau);
906       if(!dau) return -1;
907       Int_t pdgdau=dau->GetPdgCode();
908       if(TMath::Abs(pdgdau)==321){
909         if(pdgD*pdgdau>0) return -1;
910         nKaons++;
911         sumPxDau+=dau->Px();
912         sumPyDau+=dau->Py();
913         sumPzDau+=dau->Pz();
914         arrayDauLab[nFoundKpi++]=indDau;
915         if(nFoundKpi>3) return -1;
916       }else if(TMath::Abs(pdgdau)==211){
917         if(pdgD*pdgdau<0) return -1;
918         nPions++;
919         sumPxDau+=dau->Px();
920         sumPyDau+=dau->Py();
921         sumPzDau+=dau->Pz();
922         arrayDauLab[nFoundKpi++]=indDau;
923         if(nFoundKpi>3) return -1;
924       }else if(TMath::Abs(pdgdau)==313){
925         Int_t nResDau=dau->GetNDaughters();
926         if(nResDau!=2) return -1;
927         Int_t indFirstResDau=dau->GetDaughter(0);
928         for(Int_t resDau=0; resDau<2; resDau++){
929           Int_t indResDau=indFirstResDau+resDau;
930           if(indResDau<0) return -1;
931           TParticle* resdau=stack->Particle(indResDau);
932           if(!resdau) return -1;
933           Int_t pdgresdau=resdau->GetPdgCode();
934           if(TMath::Abs(pdgresdau)==321){
935             if(pdgD*pdgresdau>0) return -1;
936             sumPxDau+=resdau->Px();
937             sumPyDau+=resdau->Py();
938             sumPzDau+=resdau->Pz();
939             nKaons++;
940             arrayDauLab[nFoundKpi++]=indResDau;
941             if(nFoundKpi>3) return -1;
942           }
943           if(TMath::Abs(pdgresdau)==211){
944             if(pdgD*pdgresdau<0) return -1;
945             sumPxDau+=resdau->Px();
946             sumPyDau+=resdau->Py();
947             sumPzDau+=resdau->Pz();
948             nPions++;
949             arrayDauLab[nFoundKpi++]=indResDau;
950             if(nFoundKpi>3) return -1;
951           }
952           }
953       }else{
954         return -1;
955       }
956     }
957     if(nPions!=2) return -1;
958     if(nKaons!=1) return -1;
959     if(TMath::Abs(mcPart->Px()-sumPxDau)>0.001) return -2;
960     if(TMath::Abs(mcPart->Py()-sumPyDau)>0.001) return -2;
961     if(TMath::Abs(mcPart->Pz()-sumPzDau)>0.001) return -2;
962     if(nDau==3) return 1;
963     else if(nDau==2) return 2;
964   }
965   
966   return -1;
967   
968 }
969
970 //____________________________________________________________________________
971 Int_t AliVertexingHFUtils::CheckDplusDecay(TClonesArray* arrayMC, AliAODMCParticle *mcPart, Int_t* arrayDauLab){
972   // Checks the Dplus decay channel. Returns 1 for the non-resonant case, 2 for the resonant case, -1 in other cases
973
974   Int_t pdgD=mcPart->GetPdgCode();
975   if(TMath::Abs(pdgD)!=411) return -1;
976
977   Int_t nDau=mcPart->GetNDaughters();
978   Int_t labelFirstDau = mcPart->GetDaughter(0);
979   Int_t nKaons=0;
980   Int_t nPions=0;
981   Double_t sumPxDau=0.;
982   Double_t sumPyDau=0.;
983   Double_t sumPzDau=0.;
984   Int_t nFoundKpi=0;
985
986   if(nDau==3 || nDau==2){
987     for(Int_t iDau=0; iDau<nDau; iDau++){
988       Int_t indDau = labelFirstDau+iDau;
989       if(indDau<0) return -1;
990       AliAODMCParticle* dau=dynamic_cast<AliAODMCParticle*>(arrayMC->At(indDau));
991       if(!dau) return -1;
992       Int_t pdgdau=dau->GetPdgCode();
993       if(TMath::Abs(pdgdau)==321){
994         if(pdgD*pdgdau>0) return -1;
995         nKaons++;
996         sumPxDau+=dau->Px();
997         sumPyDau+=dau->Py();
998         sumPzDau+=dau->Pz();
999         arrayDauLab[nFoundKpi++]=indDau;
1000         if(nFoundKpi>3) return -1;
1001       }else if(TMath::Abs(pdgdau)==211){
1002         if(pdgD*pdgdau<0) return -1;
1003         nPions++;
1004         sumPxDau+=dau->Px();
1005         sumPyDau+=dau->Py();
1006         sumPzDau+=dau->Pz();
1007         arrayDauLab[nFoundKpi++]=indDau;
1008         if(nFoundKpi>3) return -1;
1009       }else if(TMath::Abs(pdgdau)==313){
1010         Int_t nResDau=dau->GetNDaughters();
1011         if(nResDau!=2) return -1;
1012         Int_t indFirstResDau=dau->GetDaughter(0);
1013         for(Int_t resDau=0; resDau<2; resDau++){
1014           Int_t indResDau=indFirstResDau+resDau;
1015           if(indResDau<0) return -1;
1016           AliAODMCParticle* resdau=dynamic_cast<AliAODMCParticle*>(arrayMC->At(indResDau));
1017           if(!resdau) return -1;
1018           Int_t pdgresdau=resdau->GetPdgCode();
1019           if(TMath::Abs(pdgresdau)==321){
1020             if(pdgD*pdgresdau>0) return -1;
1021             sumPxDau+=resdau->Px();
1022             sumPyDau+=resdau->Py();
1023             sumPzDau+=resdau->Pz();
1024             nKaons++;
1025             arrayDauLab[nFoundKpi++]=indResDau;
1026             if(nFoundKpi>3) return -1;
1027           }
1028           if(TMath::Abs(pdgresdau)==211){
1029             if(pdgD*pdgresdau<0) return -1;
1030             sumPxDau+=resdau->Px();
1031             sumPyDau+=resdau->Py();
1032             sumPzDau+=resdau->Pz();
1033             nPions++;
1034             arrayDauLab[nFoundKpi++]=indResDau;
1035             if(nFoundKpi>3) return -1;
1036           }
1037         }
1038        }else{
1039         return -1;
1040       }
1041     }
1042     if(nPions!=2) return -1;
1043     if(nKaons!=1) return -1;
1044     if(TMath::Abs(mcPart->Px()-sumPxDau)>0.001) return -2;
1045     if(TMath::Abs(mcPart->Py()-sumPyDau)>0.001) return -2;
1046     if(TMath::Abs(mcPart->Pz()-sumPzDau)>0.001) return -2;
1047     if(nDau==3) return 1;
1048     else if(nDau==2) return 2;
1049   }
1050
1051   return -1;
1052
1053 }
1054 //____________________________________________________________________________
1055 Int_t AliVertexingHFUtils::CheckDsDecay(AliStack* stack, Int_t label, Int_t* arrayDauLab){
1056   // Checks the Ds decay channel. Returns 1 for Ds->phipi->KKpi, 2 for Ds->K0*K->KKpi, 3 for the non-resonant case
1057
1058   if(label<0) return -1;
1059   TParticle* mcPart = stack->Particle(label);
1060   Int_t pdgD=mcPart->GetPdgCode();
1061   if(TMath::Abs(pdgD)!=431) return -1;
1062
1063   Int_t nDau=mcPart->GetNDaughters();
1064   Int_t labelFirstDau = mcPart->GetDaughter(0);
1065   Int_t nKaons=0;
1066   Int_t nPions=0;
1067   Double_t sumPxDau=0.;
1068   Double_t sumPyDau=0.;
1069   Double_t sumPzDau=0.;
1070   Int_t nFoundKpi=0;
1071   Bool_t isPhi=kFALSE;
1072   Bool_t isk0st=kFALSE;
1073   
1074   if(nDau==3 || nDau==2){
1075     for(Int_t iDau=0; iDau<nDau; iDau++){
1076       Int_t indDau = labelFirstDau+iDau;
1077       if(indDau<0) return -1;
1078       TParticle* dau=stack->Particle(indDau);
1079       if(!dau) return -1;
1080       Int_t pdgdau=dau->GetPdgCode();
1081       if(TMath::Abs(pdgdau)==321){
1082         nKaons++;
1083         sumPxDau+=dau->Px();
1084         sumPyDau+=dau->Py();
1085         sumPzDau+=dau->Pz();
1086         arrayDauLab[nFoundKpi++]=indDau;
1087         if(nFoundKpi>3) return -1;
1088       }else if(TMath::Abs(pdgdau)==211){
1089         nPions++;
1090         sumPxDau+=dau->Px();
1091         sumPyDau+=dau->Py();
1092         sumPzDau+=dau->Pz();
1093         arrayDauLab[nFoundKpi++]=indDau;
1094         if(nFoundKpi>3) return -1;
1095       }else if(TMath::Abs(pdgdau)==313 || TMath::Abs(pdgdau)==333){
1096         if(TMath::Abs(pdgdau)==313) isk0st=kTRUE;
1097         if(TMath::Abs(pdgdau)==333) isPhi=kTRUE;
1098         Int_t nResDau=dau->GetNDaughters();
1099         if(nResDau!=2) return -1;
1100         Int_t indFirstResDau=dau->GetDaughter(0);
1101         for(Int_t resDau=0; resDau<2; resDau++){
1102           Int_t indResDau=indFirstResDau+resDau;
1103           if(indResDau<0) return -1;
1104           TParticle* resdau=stack->Particle(indResDau);
1105           if(!resdau) return -1;
1106           Int_t pdgresdau=resdau->GetPdgCode();
1107           if(TMath::Abs(pdgresdau)==321){
1108             sumPxDau+=resdau->Px();
1109             sumPyDau+=resdau->Py();
1110             sumPzDau+=resdau->Pz();
1111             nKaons++;
1112             arrayDauLab[nFoundKpi++]=indResDau;
1113             if(nFoundKpi>3) return -1;
1114           }
1115           if(TMath::Abs(pdgresdau)==211){
1116             sumPxDau+=resdau->Px();
1117             sumPyDau+=resdau->Py();
1118             sumPzDau+=resdau->Pz();
1119             nPions++;
1120             arrayDauLab[nFoundKpi++]=indResDau;
1121             if(nFoundKpi>3) return -1;
1122           }
1123           }
1124       }else{
1125         return -1;
1126       }
1127     }
1128     if(nPions!=1) return -1;
1129     if(nKaons!=2) return -1;
1130     if(TMath::Abs(mcPart->Px()-sumPxDau)>0.001) return -2;
1131     if(TMath::Abs(mcPart->Py()-sumPyDau)>0.001) return -2;
1132     if(TMath::Abs(mcPart->Pz()-sumPzDau)>0.001) return -2;
1133     if(nDau==3) return 3;
1134     else if(nDau==2){
1135       if(isk0st) return 2;
1136       if(isPhi) return 1;
1137     }
1138   }
1139   
1140   return -1;
1141   
1142 }
1143 //____________________________________________________________________________
1144 Int_t AliVertexingHFUtils::CheckDsDecay(TClonesArray* arrayMC, AliAODMCParticle *mcPart, Int_t* arrayDauLab){
1145   // Checks the Ds decay channel. Returns 1 for Ds->phipi->KKpi, 2 for Ds->K0*K->KKpi, 3 for the non-resonant case
1146
1147   Int_t pdgD=mcPart->GetPdgCode();
1148   if(TMath::Abs(pdgD)!=431) return -1;
1149
1150   Int_t nDau=mcPart->GetNDaughters();
1151   Int_t labelFirstDau = mcPart->GetDaughter(0);
1152   Int_t nKaons=0;
1153   Int_t nPions=0;
1154   Double_t sumPxDau=0.;
1155   Double_t sumPyDau=0.;
1156   Double_t sumPzDau=0.;
1157   Int_t nFoundKpi=0;
1158   Bool_t isPhi=kFALSE;
1159   Bool_t isk0st=kFALSE;
1160   
1161   if(nDau==3 || nDau==2){
1162     for(Int_t iDau=0; iDau<nDau; iDau++){
1163       Int_t indDau = labelFirstDau+iDau;
1164       if(indDau<0) return -1;
1165       AliAODMCParticle* dau=dynamic_cast<AliAODMCParticle*>(arrayMC->At(indDau));
1166       if(!dau) return -1;
1167       Int_t pdgdau=dau->GetPdgCode();
1168       if(TMath::Abs(pdgdau)==321){
1169         nKaons++;
1170         sumPxDau+=dau->Px();
1171         sumPyDau+=dau->Py();
1172         sumPzDau+=dau->Pz();
1173         arrayDauLab[nFoundKpi++]=indDau;
1174         if(nFoundKpi>3) return -1;
1175       }else if(TMath::Abs(pdgdau)==211){
1176         nPions++;
1177         sumPxDau+=dau->Px();
1178         sumPyDau+=dau->Py();
1179         sumPzDau+=dau->Pz();
1180         arrayDauLab[nFoundKpi++]=indDau;
1181         if(nFoundKpi>3) return -1;
1182       }else if(TMath::Abs(pdgdau)==313 || TMath::Abs(pdgdau)==333){
1183         if(TMath::Abs(pdgdau)==313) isk0st=kTRUE;
1184         if(TMath::Abs(pdgdau)==333) isPhi=kTRUE;
1185         Int_t nResDau=dau->GetNDaughters();
1186         if(nResDau!=2) return -1;
1187         Int_t indFirstResDau=dau->GetDaughter(0);
1188         for(Int_t resDau=0; resDau<2; resDau++){
1189           Int_t indResDau=indFirstResDau+resDau;
1190           if(indResDau<0) return -1;
1191           AliAODMCParticle* resdau=dynamic_cast<AliAODMCParticle*>(arrayMC->At(indResDau));
1192           if(!resdau) return -1;
1193           Int_t pdgresdau=resdau->GetPdgCode();
1194           if(TMath::Abs(pdgresdau)==321){
1195             sumPxDau+=resdau->Px();
1196             sumPyDau+=resdau->Py();
1197             sumPzDau+=resdau->Pz();
1198             nKaons++;
1199             arrayDauLab[nFoundKpi++]=indResDau;
1200             if(nFoundKpi>3) return -1;
1201           }
1202           if(TMath::Abs(pdgresdau)==211){
1203             sumPxDau+=resdau->Px();
1204             sumPyDau+=resdau->Py();
1205             sumPzDau+=resdau->Pz();
1206             nPions++;
1207             arrayDauLab[nFoundKpi++]=indResDau;
1208             if(nFoundKpi>3) return -1;
1209           }
1210           }
1211       }else{
1212         return -1;
1213       }
1214     }
1215     if(nPions!=1) return -1;
1216     if(nKaons!=2) return -1;
1217     if(TMath::Abs(mcPart->Px()-sumPxDau)>0.001) return -2;
1218     if(TMath::Abs(mcPart->Py()-sumPyDau)>0.001) return -2;
1219     if(TMath::Abs(mcPart->Pz()-sumPzDau)>0.001) return -2;
1220     if(nDau==3) return 3;
1221     else if(nDau==2){
1222       if(isk0st) return 2;
1223       if(isPhi) return 1;
1224     }
1225   }
1226   
1227   return -1;
1228   
1229 }
1230 //____________________________________________________________________________
1231 Int_t AliVertexingHFUtils::CheckDstarDecay(AliStack* stack, Int_t label, Int_t* arrayDauLab){
1232   // Checks the Dstar decay channel. Returns 1 for D*->D0pi->Kpipi, -1 in other cases
1233
1234   if(label<0) return -1;
1235   TParticle* mcPart = stack->Particle(label);
1236   Int_t pdgD=mcPart->GetPdgCode();
1237   if(TMath::Abs(pdgD)!=413) return -1;
1238
1239   Int_t nDau=mcPart->GetNDaughters();
1240   if(nDau!=2) return -1;
1241
1242   Int_t labelFirstDau = mcPart->GetDaughter(0);
1243   Int_t nKaons=0;
1244   Int_t nPions=0;
1245   Double_t sumPxDau=0.;
1246   Double_t sumPyDau=0.;
1247   Double_t sumPzDau=0.;
1248   Int_t nFoundKpi=0;
1249
1250   for(Int_t iDau=0; iDau<nDau; iDau++){
1251     Int_t indDau = labelFirstDau+iDau;
1252     if(indDau<0) return -1;
1253     TParticle* dau=stack->Particle(indDau);
1254     if(!dau) return -1;
1255     Int_t pdgdau=dau->GetPdgCode();
1256     if(TMath::Abs(pdgdau)==421){
1257       Int_t nResDau=dau->GetNDaughters();
1258       if(nResDau!=2) return -1;
1259       Int_t indFirstResDau=dau->GetDaughter(0);
1260       for(Int_t resDau=0; resDau<2; resDau++){
1261         Int_t indResDau=indFirstResDau+resDau;
1262         if(indResDau<0) return -1;
1263         TParticle* resdau=stack->Particle(indResDau);
1264         if(!resdau) return -1;
1265         Int_t pdgresdau=resdau->GetPdgCode();
1266         if(TMath::Abs(pdgresdau)==321){
1267           if(pdgD*pdgresdau>0) return -1;
1268           sumPxDau+=resdau->Px();
1269           sumPyDau+=resdau->Py();
1270           sumPzDau+=resdau->Pz();
1271           nKaons++;
1272           arrayDauLab[nFoundKpi++]=indResDau;
1273           if(nFoundKpi>3) return -1;
1274         }
1275         if(TMath::Abs(pdgresdau)==211){
1276           if(pdgD*pdgresdau<0) return -1;
1277           sumPxDau+=resdau->Px();
1278           sumPyDau+=resdau->Py();
1279           sumPzDau+=resdau->Pz();
1280           nPions++;
1281           arrayDauLab[nFoundKpi++]=indResDau;
1282           if(nFoundKpi>3) return -1;
1283         }
1284       }
1285     }else if(TMath::Abs(pdgdau)==211){
1286       if(pdgD*pdgdau<0) return -1;
1287       nPions++;
1288       sumPxDau+=dau->Px();
1289       sumPyDau+=dau->Py();
1290       sumPzDau+=dau->Pz();
1291       arrayDauLab[nFoundKpi++]=indDau;
1292       if(nFoundKpi>3) return -1;
1293     }
1294   }
1295
1296   if(nPions!=2) return -1;
1297   if(nKaons!=1) return -1;
1298   if(TMath::Abs(mcPart->Px()-sumPxDau)>0.001) return -2;
1299   if(TMath::Abs(mcPart->Py()-sumPyDau)>0.001) return -2;
1300   if(TMath::Abs(mcPart->Pz()-sumPzDau)>0.001) return -2;
1301   return 1;
1302   
1303 }
1304
1305 //____________________________________________________________________________
1306 Int_t AliVertexingHFUtils::CheckDstarDecay(TClonesArray* arrayMC, AliAODMCParticle *mcPart, Int_t* arrayDauLab){
1307   // Checks the Dstar decay channel. Returns 1 for D*->D0pi->Kpipi, -1 in other cases
1308
1309   Int_t pdgD=mcPart->GetPdgCode();
1310   if(TMath::Abs(pdgD)!=413) return -1;
1311
1312   Int_t nDau=mcPart->GetNDaughters();
1313   if(nDau!=2) return -1;
1314
1315   Int_t labelFirstDau = mcPart->GetDaughter(0);
1316   Int_t nKaons=0;
1317   Int_t nPions=0;
1318   Double_t sumPxDau=0.;
1319   Double_t sumPyDau=0.;
1320   Double_t sumPzDau=0.;
1321   Int_t nFoundKpi=0;
1322
1323   for(Int_t iDau=0; iDau<nDau; iDau++){
1324     Int_t indDau = labelFirstDau+iDau;
1325     if(indDau<0) return -1;
1326     AliAODMCParticle* dau=dynamic_cast<AliAODMCParticle*>(arrayMC->At(indDau));
1327     if(!dau) return -1;
1328     Int_t pdgdau=dau->GetPdgCode();
1329     if(TMath::Abs(pdgdau)==421){
1330       Int_t nResDau=dau->GetNDaughters();
1331       if(nResDau!=2) return -1;
1332       Int_t indFirstResDau=dau->GetDaughter(0);
1333       for(Int_t resDau=0; resDau<2; resDau++){
1334         Int_t indResDau=indFirstResDau+resDau;
1335         if(indResDau<0) return -1;
1336         AliAODMCParticle* resdau=dynamic_cast<AliAODMCParticle*>(arrayMC->At(indResDau)); 
1337         if(!resdau) return -1;
1338         Int_t pdgresdau=resdau->GetPdgCode();
1339         if(TMath::Abs(pdgresdau)==321){
1340           if(pdgD*pdgresdau>0) return -1;
1341           sumPxDau+=resdau->Px();
1342           sumPyDau+=resdau->Py();
1343           sumPzDau+=resdau->Pz();
1344           nKaons++;
1345           arrayDauLab[nFoundKpi++]=indResDau;
1346           if(nFoundKpi>3) return -1;
1347         }
1348         if(TMath::Abs(pdgresdau)==211){
1349           if(pdgD*pdgresdau<0) return -1;
1350           sumPxDau+=resdau->Px();
1351           sumPyDau+=resdau->Py();
1352           sumPzDau+=resdau->Pz();
1353           nPions++;
1354           arrayDauLab[nFoundKpi++]=indResDau;
1355           if(nFoundKpi>3) return -1;
1356         }
1357       }
1358     }else if(TMath::Abs(pdgdau)==211){
1359       if(pdgD*pdgdau<0) return -1;
1360       nPions++;
1361       sumPxDau+=dau->Px();
1362       sumPyDau+=dau->Py();
1363       sumPzDau+=dau->Pz();
1364       arrayDauLab[nFoundKpi++]=indDau;
1365       if(nFoundKpi>3) return -1;
1366     }
1367   }
1368
1369   if(nPions!=2) return -1;
1370   if(nKaons!=1) return -1;
1371   if(TMath::Abs(mcPart->Px()-sumPxDau)>0.001) return -2;
1372   if(TMath::Abs(mcPart->Py()-sumPyDau)>0.001) return -2;
1373   if(TMath::Abs(mcPart->Pz()-sumPzDau)>0.001) return -2;
1374   return 1;
1375   
1376 }
1377