Coverity
[u/mrichter/AliRoot.git] / PWG3 / vertexingHF / AliRDHFCutsD0toKpipipi.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-2010, 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 //
18 // Class for cuts on AOD reconstructed D0->Kpipipi
19 //
20 // Author: r.romita@gsi.de, andrea.dainese@pd.infn.it,
21 //         fabio.colamaria@ba.infn.it
22 /////////////////////////////////////////////////////////////
23
24 #include <TDatabasePDG.h>
25 #include <Riostream.h>
26
27 #include "AliRDHFCutsD0toKpipipi.h"
28 #include "AliAODRecoDecayHF4Prong.h"
29 #include "AliAODTrack.h"
30 #include "AliESDtrack.h"
31 #include "AliAODPidHF.h"
32
33 ClassImp(AliRDHFCutsD0toKpipipi)
34
35 //--------------------------------------------------------------------------
36 AliRDHFCutsD0toKpipipi::AliRDHFCutsD0toKpipipi(const char* name) : 
37 AliRDHFCuts(name)
38 {
39   //
40   // Default Constructor
41   //
42   Int_t nvars=9;
43   SetNVars(nvars);
44   TString varNames[9]={"inv. mass [GeV]",   
45                        "dca [cm]",
46                        "Dist 2-trk Vtx to PrimVtx [cm]",
47                        "Dist 3-trk Vtx to PrimVtx [cm]",
48                        "Dist 4-trk Vtx to PrimVtx [cm]",
49                        "cosThetaPoint",
50                        "pt [GeV/c]",
51                        "rho mass [GeV]",
52                        "PID cut"};
53   Bool_t isUpperCut[9]={kTRUE,
54                         kTRUE,
55                         kFALSE,
56                         kFALSE,
57                         kFALSE,
58                         kFALSE,
59                         kFALSE,
60                         kTRUE,
61                         kFALSE};
62   SetVarNames(nvars,varNames,isUpperCut);
63   Bool_t forOpt[9]={kFALSE,
64                     kTRUE,
65                     kTRUE,
66                     kTRUE,
67                     kTRUE,
68                     kTRUE,
69                     kFALSE,
70                     kFALSE,
71                     kFALSE};
72   SetVarsForOpt(5,forOpt);
73   Float_t limits[2]={0,999999999.};
74   SetPtBins(2,limits);
75 }
76 //--------------------------------------------------------------------------
77 AliRDHFCutsD0toKpipipi::AliRDHFCutsD0toKpipipi(const AliRDHFCutsD0toKpipipi &source) :
78   AliRDHFCuts(source)
79 {
80   //
81   // Copy constructor
82   //
83
84 }
85 //--------------------------------------------------------------------------
86 AliRDHFCutsD0toKpipipi &AliRDHFCutsD0toKpipipi::operator=(const AliRDHFCutsD0toKpipipi &source)
87 {
88   //
89   // assignment operator
90   //
91   if(&source == this) return *this;
92
93   AliRDHFCuts::operator=(source);
94
95   return *this;
96 }
97
98
99 //---------------------------------------------------------------------------
100 void AliRDHFCutsD0toKpipipi::GetCutVarsForOpt(AliAODRecoDecayHF *d,Float_t *vars,Int_t nvars,Int_t *pdgdaughters,AliAODEvent* aod) {
101   // 
102   // Fills in vars the values of the variables 
103   //
104
105   if(nvars!=fnVarsForOpt) {
106     printf("AliRDHFCutsD0toKpipipi::GetCutsVarsForOpt: wrong number of variables\n");
107     return;
108   }
109
110   AliAODRecoDecayHF4Prong *dd = (AliAODRecoDecayHF4Prong*)d;
111
112   //recalculate vertex w/o daughters
113   Bool_t cleanvtx=kFALSE;
114   AliAODVertex *origownvtx=0x0;
115   if(fRemoveDaughtersFromPrimary) {
116     if(dd->GetOwnPrimaryVtx()) origownvtx=new AliAODVertex(*dd->GetOwnPrimaryVtx());
117     cleanvtx=kTRUE;
118     if(!RecalcOwnPrimaryVtx(dd,aod)) {
119       CleanOwnPrimaryVtx(dd,aod,origownvtx);
120       cleanvtx=kFALSE;
121     }
122   }
123
124   Int_t iter=-1;
125
126   if(fVarsForOpt[0]) {
127     iter++;
128     Double_t mD0[2],mD0bar[2];
129     if(TMath::Abs(pdgdaughters[1])==321 || TMath::Abs(pdgdaughters[3])==321) {
130       dd->InvMassD0(mD0);
131       if(TMath::Abs(pdgdaughters[1])==321) {
132        vars[iter]=mD0[0];
133       }else{
134        vars[iter]=mD0[1];
135       }
136     } else {
137       dd->InvMassD0bar(mD0bar);
138       if(TMath::Abs(pdgdaughters[0])==321) {
139        vars[iter]=mD0bar[0];
140       }else{
141        vars[iter]=mD0bar[1];
142       }
143    }
144   }
145
146   if(fVarsForOpt[1]){
147     iter++;
148     vars[iter]=dd->GetDCA();
149   }
150
151   if(fVarsForOpt[2]){
152     iter++;
153     vars[iter]=dd->GetDist12toPrim();
154   }
155   if(fVarsForOpt[3]){
156     iter++;
157     vars[iter]=dd->GetDist3toPrim();
158   }
159   if(fVarsForOpt[4]){
160     iter++;
161     vars[iter]=dd->GetDist4toPrim();
162   }
163   if(fVarsForOpt[5]){
164     iter++;
165     vars[iter]=dd->CosPointingAngle();
166   }
167   if(fVarsForOpt[6]){
168     iter++;
169     vars[iter]=dd->Pt();
170   }
171   if(fVarsForOpt[7]){
172     iter++;
173     vars[iter]=999999999.;
174     printf("ERROR: optmization for rho mass cut not implemented\n");
175   }
176   if(fVarsForOpt[8]){
177     iter++;
178     vars[iter]=999999999.;
179     printf("ERROR: optmization for PID cut not implemented\n");
180   }
181   
182     if(cleanvtx)CleanOwnPrimaryVtx(dd,aod,origownvtx);
183   return;
184 }
185 //---------------------------------------------------------------------------
186 Int_t AliRDHFCutsD0toKpipipi::IsSelected(TObject* obj,Int_t selectionLevel) {
187   //
188   // Apply selection
189   //
190
191   if(!fCutsRD){
192     cout<<"Cut matrix not inizialized. Exit..."<<endl;
193     return 0;
194   }
195   //PrintAll();
196   AliAODRecoDecayHF4Prong* d=(AliAODRecoDecayHF4Prong*)obj;
197
198   if(!d){
199     cout<<"AliAODRecoDecayHF4Prong null"<<endl;
200     return 0;
201   }
202
203   Double_t ptD=d->Pt();
204   if(ptD<fMinPtCand) return 0;
205   if(ptD>fMaxPtCand) return 0;
206
207   if(d->HasBadDaughters()) return 0;  
208
209   // selection on daughter tracks 
210   if(selectionLevel==AliRDHFCuts::kAll || 
211      selectionLevel==AliRDHFCuts::kTracks) {
212     if(!AreDaughtersSelected(d)) return 0;
213   }
214
215
216   Int_t returnvalue=1;
217
218   // selection on candidate
219   if(selectionLevel==AliRDHFCuts::kAll || 
220      selectionLevel==AliRDHFCuts::kCandidate) {
221
222     Int_t ptbin=PtBin(d->Pt());
223   
224     Int_t okD0=1,okD0bar=1;       
225     Double_t mD0[2],mD0bar[2];
226     Double_t mD0PDG = TDatabasePDG::Instance()->GetParticle(421)->Mass();
227
228     d->InvMassD0(mD0);
229     if(TMath::Abs(mD0[0]-mD0PDG) > fCutsRD[GetGlobalIndex(0,ptbin)] &&
230        TMath::Abs(mD0[1]-mD0PDG) > fCutsRD[GetGlobalIndex(0,ptbin)]) okD0 = 0;
231     d->InvMassD0bar(mD0bar);
232     if(TMath::Abs(mD0bar[0]-mD0PDG) > fCutsRD[GetGlobalIndex(0,ptbin)] &&
233        TMath::Abs(mD0bar[1]-mD0PDG) > fCutsRD[GetGlobalIndex(0,ptbin)]) okD0bar = 0;
234     if(!okD0 && !okD0bar) return 0;
235     
236     if(d->GetDCA() > fCutsRD[GetGlobalIndex(1,ptbin)]
237        || d->GetDCA(3) > fCutsRD[GetGlobalIndex(1,ptbin)]
238        || d->GetDCA(2) > fCutsRD[GetGlobalIndex(1,ptbin)]
239        || d->GetDCA(5) > fCutsRD[GetGlobalIndex(1,ptbin)]) return 0;
240     if(d->GetDist12toPrim() < fCutsRD[GetGlobalIndex(2,ptbin)]) return 0;
241     if(d->GetDist3toPrim() < fCutsRD[GetGlobalIndex(3,ptbin)]) return 0;
242     if(d->GetDist4toPrim() < fCutsRD[GetGlobalIndex(4,ptbin)]) return 0;
243     if(d->CosPointingAngle() < fCutsRD[GetGlobalIndex(5,ptbin)]) return 0;
244     if(d->Pt() < fCutsRD[GetGlobalIndex(6,ptbin)]) return 0;
245     if(!d->CutRhoMass(mD0,mD0bar,fCutsRD[GetGlobalIndex(0,ptbin)],fCutsRD[GetGlobalIndex(7,ptbin)])) return 0;
246
247     if (okD0) returnvalue=1; //cuts passed as D0
248     if (okD0bar) returnvalue=2; //cuts passed as D0bar
249     if (okD0 && okD0bar) returnvalue=3; //cuts passed as D0 and D0bar
250   }
251
252   // selection on PID (from AliAODPidHF)
253   if(selectionLevel==AliRDHFCuts::kAll || 
254      selectionLevel==AliRDHFCuts::kPID) {
255
256     Int_t selD01 = D01Selected(d,AliRDHFCuts::kCandidate);
257     Int_t selD02 = D02Selected(d,AliRDHFCuts::kCandidate);  
258     Int_t selD0bar1 = D0bar1Selected(d,AliRDHFCuts::kCandidate);
259     Int_t selD0bar2 = D0bar2Selected(d,AliRDHFCuts::kCandidate);
260
261     Int_t d01PID = 0, d02PID = 0, d0bar1PID = 0, d0bar2PID = 0;
262     returnvalue = IsSelectedFromPID(d, &d01PID, &d02PID, &d0bar1PID, &d0bar2PID);  //This returnvalue is dummy! Now it's modified as it must be!
263
264 returnvalue = 0;
265
266     if((selD01 == 1 && d01PID == 1)||(selD02 == 1 && d02PID == 1)||(selD0bar1 == 1 && d0bar1PID == 1)||(selD0bar2 == 1 && d0bar2PID == 1)) returnvalue = 1;
267   }
268
269   return returnvalue;
270 }
271
272 //---------------------------------------------------------------------------
273 Int_t AliRDHFCutsD0toKpipipi::IsSelectedFromPID(AliAODRecoDecayHF4Prong *d, Int_t *hyp1, Int_t *hyp2, Int_t *hyp3, Int_t *hyp4) {
274   //
275   // Apply selection (using AliAODPidHF methods)
276   // Mass hypothesis true if each particle is at least compatible with specie of hypothesis
277   // 
278
279   Int_t output=0;
280
281   Int_t matchK[4], matchPi[4];
282   Double_t ptlimit[2] = {0.6,0.8};
283   AliAODTrack* trk[4];
284   trk[0] = (AliAODTrack*)d->GetDaughter(0);
285   trk[1] = (AliAODTrack*)d->GetDaughter(1);
286   trk[2] = (AliAODTrack*)d->GetDaughter(2);
287   trk[3] = (AliAODTrack*)d->GetDaughter(3);
288 //  if(!trk[0] || !trk[1] || !trk[2] || !trk[3]) {          //REMOVED (needs also AliAODEvent to be passed, here and in IsSelected method)
289 //    trk[0]=aodIn->GetTrack(trkIDtoEntry[d->GetProngID(0)]);
290 //    trk[1]=aodIn->GetTrack(trkIDtoEntry[d->GetProngID(1)]);
291 //    trk[2]=aodIn->GetTrack(trkIDtoEntry[d->GetProngID(2)]);
292 //    trk[3]=aodIn->GetTrack(trkIDtoEntry[d->GetProngID(3)]);}
293
294   AliAODPidHF* PidObj = new AliAODPidHF();
295   PidObj->SetAsym(kTRUE);
296   PidObj->SetPLimit(ptlimit);
297   PidObj->SetSigma(0,2.);  //TPC sigma, in three pT ranges
298   PidObj->SetSigma(1,1.);
299   PidObj->SetSigma(2,0.);  
300   PidObj->SetSigma(3,2.);  //TOF sigma, whole pT range
301   PidObj->SetTPC(kTRUE);
302   PidObj->SetTOF(kTRUE);
303    
304   for(Int_t ii=0; ii<4; ii++) {
305     PidObj->SetSigma(0,2.);
306     matchK[ii] = PidObj->MatchTPCTOF(trk[ii],1,3,kTRUE); //arguments: track, mode, particle#, compatibility allowed
307     PidObj->SetSigma(0,2.);
308     matchPi[ii] = PidObj->MatchTPCTOF(trk[ii],1,2,kTRUE); 
309   }
310
311   //Rho invariant mass under various hypotheses (to be matched with PID infos in order to accet the candidate)
312   Int_t d01rho03 = 0, d01rho23 = 0, d02rho01 = 0, d02rho12 = 0, d0bar1rho12 = 0, d0bar1rho23 = 0, d0bar2rho01 = 0, d0bar2rho03 = 0;
313   if(TMath::Abs(0.775 - d->InvMassRho(0,3))<0.1)  {d01rho03 = 1; d0bar2rho03 = 1;}
314   if(TMath::Abs(0.775 - d->InvMassRho(2,3))<0.1)  {d01rho23 = 1; d0bar1rho23 = 1;}
315   if(TMath::Abs(0.775 - d->InvMassRho(0,1))<0.1)  {d02rho01 = 1; d0bar2rho01 = 1;}
316   if(TMath::Abs(0.775 - d->InvMassRho(1,2))<0.1)  {d02rho12 = 1; d0bar1rho12 = 1;}
317   Int_t d01rho = 0, d02rho = 0, d0bar1rho = 0, d0bar2rho = 0;
318   if(d01rho03==1||d01rho23==1) d01rho = 1;
319   if(d02rho01==1||d02rho12==1) d02rho = 1;
320   if(d0bar1rho12==1||d0bar1rho23==1) d0bar1rho = 1;
321   if(d0bar2rho01==1||d0bar2rho03==1) d0bar2rho = 1;
322
323   //This way because there could be multiple hypotheses accepted
324   if(d01rho==1 && (matchK[1]>=0 && matchPi[0]>=0 && matchPi[2]>=0 && matchPi[3]>=0)) {*hyp1 = 1; output = 1;} //d01 hyp
325   if(d02rho==1 && (matchK[3]>=0 && matchPi[0]>=0 && matchPi[1]>=0 && matchPi[2]>=0)) {*hyp2 = 1; output = 1;} //d02 hyp
326   if(d0bar1rho==1 && (matchK[0]>=0 && matchPi[1]>=0 && matchPi[2]>=0 && matchPi[3]>=0)) {*hyp3 = 1; output = 1;} //d0bar1 hyp
327   if(d0bar2rho==1 && (matchK[2]>=0 && matchPi[0]>=0 && matchPi[1]>=0 && matchPi[3]>=0)) {*hyp4 = 1; output = 1;} //d0bar2 hyp
328
329   return output;
330 }
331 //---------------------------------------------------------------------------
332 Int_t AliRDHFCutsD0toKpipipi::D01Selected(TObject* obj,Int_t selectionLevel) {
333   //
334   // Apply selection
335   //
336
337   if(!fCutsRD){
338     cout<<"Cut matrix not inizialized. Exit..."<<endl;
339     return 0;
340   }
341   //PrintAll();
342   AliAODRecoDecayHF4Prong* d=(AliAODRecoDecayHF4Prong*)obj;
343
344   if(!d){
345     cout<<"AliAODRecoDecayHF4Prong null"<<endl;
346     return 0;
347   }
348
349   Int_t returnvalue=0;
350
351   // selection on candidate
352   if(selectionLevel==AliRDHFCuts::kAll || 
353      selectionLevel==AliRDHFCuts::kCandidate) {
354
355     Int_t ptbin=PtBin(d->Pt());
356     
357     Double_t mD0[2];
358     Double_t mD0PDG = TDatabasePDG::Instance()->GetParticle(421)->Mass();
359
360     d->InvMassD0(mD0);
361     if(TMath::Abs(mD0[0]-mD0PDG) < fCutsRD[GetGlobalIndex(0,ptbin)]) returnvalue = 1;
362   }
363
364   return returnvalue;
365 }
366 //---------------------------------------------------------------------------
367 Int_t AliRDHFCutsD0toKpipipi::D02Selected(TObject* obj,Int_t selectionLevel) {
368   //
369   // Apply selection
370   //
371
372   if(!fCutsRD){
373     cout<<"Cut matrix not inizialized. Exit..."<<endl;
374     return 0;
375   }
376   //PrintAll();
377   AliAODRecoDecayHF4Prong* d=(AliAODRecoDecayHF4Prong*)obj;
378
379   if(!d){
380     cout<<"AliAODRecoDecayHF4Prong null"<<endl;
381     return 0;
382   }
383
384   Int_t returnvalue=0;
385
386   // selection on candidate
387   if(selectionLevel==AliRDHFCuts::kAll || 
388      selectionLevel==AliRDHFCuts::kCandidate) {
389
390     Int_t ptbin=PtBin(d->Pt());
391       
392     Double_t mD0[2];
393     Double_t mD0PDG = TDatabasePDG::Instance()->GetParticle(421)->Mass();
394
395     d->InvMassD0(mD0);
396     if(TMath::Abs(mD0[1]-mD0PDG) < fCutsRD[GetGlobalIndex(0,ptbin)]) returnvalue = 1;
397   }
398
399   return returnvalue;
400 }//---------------------------------------------------------------------------
401 Int_t AliRDHFCutsD0toKpipipi::D0bar1Selected(TObject* obj,Int_t selectionLevel) {
402   //
403   // Apply selection
404   //
405
406   if(!fCutsRD){
407     cout<<"Cut matrix not inizialized. Exit..."<<endl;
408     return 0;
409   }
410   //PrintAll();
411   AliAODRecoDecayHF4Prong* d=(AliAODRecoDecayHF4Prong*)obj;
412
413   if(!d){
414     cout<<"AliAODRecoDecayHF4Prong null"<<endl;
415     return 0;
416   }
417
418   Int_t returnvalue=0;
419
420   // selection on candidate
421   if(selectionLevel==AliRDHFCuts::kAll || 
422      selectionLevel==AliRDHFCuts::kCandidate) {
423
424     Int_t ptbin=PtBin(d->Pt());
425  
426     Double_t mD0bar[2];
427     Double_t mD0PDG = TDatabasePDG::Instance()->GetParticle(421)->Mass();
428
429     d->InvMassD0bar(mD0bar);
430     if(TMath::Abs(mD0bar[0]-mD0PDG) < fCutsRD[GetGlobalIndex(0,ptbin)]) returnvalue = 1;
431   }
432
433   return returnvalue;
434 }
435 //---------------------------------------------------------------------------
436 Int_t AliRDHFCutsD0toKpipipi::D0bar2Selected(TObject* obj,Int_t selectionLevel) {
437   //
438   // Apply selection
439   //
440
441   if(!fCutsRD){
442     cout<<"Cut matrix not inizialized. Exit..."<<endl;
443     return 0;
444   }
445   //PrintAll();
446   AliAODRecoDecayHF4Prong* d=(AliAODRecoDecayHF4Prong*)obj;
447
448   if(!d){
449     cout<<"AliAODRecoDecayHF4Prong null"<<endl;
450     return 0;
451   }
452
453   Int_t returnvalue=0;
454
455   // selection on candidate
456   if(selectionLevel==AliRDHFCuts::kAll || 
457      selectionLevel==AliRDHFCuts::kCandidate) {
458
459     Int_t ptbin=PtBin(d->Pt());
460     
461     Double_t mD0bar[2];
462     Double_t mD0PDG = TDatabasePDG::Instance()->GetParticle(421)->Mass();
463
464     d->InvMassD0bar(mD0bar);
465     if(TMath::Abs(mD0bar[1]-mD0PDG) < fCutsRD[GetGlobalIndex(0,ptbin)]) returnvalue = 1;
466   }
467
468   return returnvalue;
469 }
470 //---------------------------------------------------------------------------
471 Bool_t AliRDHFCutsD0toKpipipi::IsInFiducialAcceptance(Double_t pt, Double_t y) const
472 {
473   //
474   // Checking if D0 is in fiducial acceptance region 
475   //
476
477   if(pt > 5.) {
478     // applying cut for pt > 5 GeV
479     AliDebug(4,Form("pt of D0 = %f (> 5), cutting at |y| < 0.8\n",pt)); 
480     if (TMath::Abs(y) > 0.8){
481       return kFALSE;
482     }
483   } else {
484     // appliying smooth cut for pt < 5 GeV
485     Double_t maxFiducialY = -0.2/15*pt*pt+1.9/15*pt+0.5; 
486     Double_t minFiducialY = 0.2/15*pt*pt-1.9/15*pt-0.5;         
487     AliDebug(4,Form("pt of D0 = %f (< 5), cutting  according to the fiducial zone [%f, %f]\n",pt,minFiducialY,maxFiducialY)); 
488     if (y < minFiducialY || y > maxFiducialY){
489       return kFALSE;
490     }
491   }
492
493   return kTRUE;
494 }
495