b1bb03352256e205d1a66d7072fcb75dee8ed23e
[u/mrichter/AliRoot.git] / PWG3 / vertexingHF / AliAnalysisTaskSED0Mass.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-2009, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /////////////////////////////////////////////////////////////
17 //
18 // AliAnalysisTaskSE for D0 candidates invariant mass histogram
19 // and comparison with the MC truth and cut variables distributions.
20 //
21 // Authors: A.Dainese, andrea.dainese@lnl.infn.it
22 // Chiara Bianchin, chiara.bianchin@pd.infn.it (invariant mass)
23 // Carmelo Di Giglio, carmelo.digiglio@ba.infn.it (like sign)
24 /////////////////////////////////////////////////////////////
25
26 #include <Riostream.h>
27 #include <TClonesArray.h>
28 #include <TCanvas.h>
29 #include <TNtuple.h>
30 #include <TList.h>
31 #include <TH1F.h>
32 #include <TH2F.h>
33 #include <TDatabasePDG.h>
34
35 #include <AliAnalysisDataSlot.h>
36 #include <AliAnalysisDataContainer.h>
37 #include "AliAnalysisManager.h"
38 #include "AliESDtrack.h"
39 #include "AliVertexerTracks.h"
40 #include "AliAODHandler.h"
41 #include "AliAODEvent.h"
42 #include "AliAODVertex.h"
43 #include "AliAODTrack.h"
44 #include "AliAODMCHeader.h"
45 #include "AliAODMCParticle.h"
46 #include "AliAODRecoDecayHF2Prong.h"
47 #include "AliAODRecoCascadeHF.h"
48 #include "AliAnalysisVertexingHF.h"
49 #include "AliAnalysisTaskSE.h"
50 #include "AliAnalysisTaskSED0Mass.h"
51
52
53 ClassImp(AliAnalysisTaskSED0Mass)
54
55
56 //________________________________________________________________________
57 AliAnalysisTaskSED0Mass::AliAnalysisTaskSED0Mass():
58 AliAnalysisTaskSE(),
59 fOutputMass(0),
60 fDistr(0),
61 fNentries(0), 
62 fChecks(0),
63 fCuts(0),
64 fArray(0),
65 fReadMC(0),
66 fCutOnDistr(0),
67 fNPtBins(1),
68 fTotPosPairs(0),
69 fTotNegPairs(0),
70 fLsNormalization(1.)
71
72
73 {
74   // Default constructor
75 }
76
77 //________________________________________________________________________
78 AliAnalysisTaskSED0Mass::AliAnalysisTaskSED0Mass(const char *name,AliRDHFCutsD0toKpi* cuts):
79 AliAnalysisTaskSE(name),
80 fOutputMass(0), 
81 fDistr(0),
82 fNentries(0),
83 fChecks(0),
84 fCuts(0),
85 fArray(0),
86 fReadMC(0),
87 fCutOnDistr(0),
88 fNPtBins(1),
89 fTotPosPairs(0),
90 fTotNegPairs(0),
91 fLsNormalization(1.)
92
93
94 {
95   // Default constructor
96
97   fNPtBins=cuts->GetNPtBins();
98   fTotPosPairs=new Int_t[fNPtBins];
99   fTotNegPairs=new Int_t[fNPtBins];
100   for(Int_t i=0;i<fNPtBins;i++) {fTotPosPairs[i]=0; fTotNegPairs[i]=0;}
101     
102   fCuts=cuts;
103
104   // Output slot #1 writes into a TList container (mass with cuts)
105   DefineOutput(1,TList::Class());  //My private output
106   // Output slot #2 writes into a TList container (distributions)
107   DefineOutput(2,TList::Class());  //My private output
108   // Output slot #3 writes into a TH1F container (number of events)
109   DefineOutput(3,TH1F::Class());  //My private output
110   // Output slot #4 writes into a TList container (quality check)
111   DefineOutput(4,TList::Class());  //My private output
112   // Output slot #5 writes into a TList container (cuts)
113   DefineOutput(5,AliRDHFCutsD0toKpi::Class());  //My private output
114
115 }
116
117 //________________________________________________________________________
118 AliAnalysisTaskSED0Mass::~AliAnalysisTaskSED0Mass()
119 {
120    if (fOutputMass) {
121     delete fOutputMass;
122     fOutputMass = 0;
123   }
124   if (fDistr) {
125     delete fDistr;
126     fDistr = 0;
127   }
128   if (fChecks) {
129     delete fChecks;
130     fChecks = 0;
131   }
132   if (fCuts) {
133     delete fCuts;
134     fCuts = 0;
135   }
136   if (fNentries){
137     delete fNentries;
138     fNentries = 0;
139   }
140  
141  
142 }  
143
144 //________________________________________________________________________
145 void AliAnalysisTaskSED0Mass::Init()
146 {
147   // Initialization
148
149   if(fDebug > 1) printf("AnalysisTaskSED0Mass::Init() \n");
150
151   
152   AliRDHFCutsD0toKpi* copyfCuts=new AliRDHFCutsD0toKpi(*fCuts);
153   const char* nameoutput=GetOutputSlot(5)->GetContainer()->GetName();
154   copyfCuts->SetName(nameoutput);
155   // Post the data
156   PostData(5,copyfCuts);
157
158
159   return;
160 }
161
162 //________________________________________________________________________
163 void AliAnalysisTaskSED0Mass::UserCreateOutputObjects()
164 {
165
166   // Create the output container
167   //
168   if(fDebug > 1) printf("AnalysisTaskSED0Mass::UserCreateOutputObjects() \n");
169
170   // Several histograms are more conveniently managed in a TList
171   fOutputMass = new TList();
172   fOutputMass->SetOwner();
173   fOutputMass->SetName("listMass");
174
175   fDistr = new TList();
176   fDistr->SetOwner();
177   fDistr->SetName("distributionslist");
178
179   fChecks = new TList();
180   fChecks->SetOwner();
181   fChecks->SetName("checkHistograms");
182
183   TString nameMass=" ",nameSgn27=" ",nameSgn=" ", nameBkg=" ", nameRfl=" ",nameMassNocutsS =" ",nameMassNocutsB =" ", namedistr=" ";
184
185   for(Int_t i=0;i<fCuts->GetNPtBins();i++){
186
187     nameMass="histMass_";
188     nameMass+=i;
189     nameSgn27="histSgn27_";
190     nameSgn27+=i;
191     nameSgn="histSgn_";
192     nameSgn+=i;
193     nameBkg="histBkg_";
194     nameBkg+=i;
195     nameRfl="histRfl_";
196     nameRfl+=i;
197     nameMassNocutsS="hMassS_";
198     nameMassNocutsS+=i;
199     nameMassNocutsB="hMassB_";
200     nameMassNocutsB+=i;
201
202     //histograms of cut variable distributions
203
204     //  pT
205     namedistr="hptpiS_";
206     namedistr+=i;
207     TH1F *hptpiS = new TH1F(namedistr.Data(), "P_{T} distribution (pions);p_{T} [GeV/c]",200,0.,8.);
208
209     namedistr="hptKS_";
210     namedistr+=i;
211     TH1F *hptKS = new TH1F(namedistr.Data(), "P_{T} distribution (kaons);p_{T} [GeV/c]",200,0.,8.);
212
213     namedistr="hptB_";
214     namedistr+=i;
215     TH1F *hptB = new TH1F(namedistr.Data(), "P_{T} distribution;p_{T} [GeV/c]",200,0.,8.);
216
217     //  pT no mass cut
218     namedistr="hptpiSnoMcut_";
219     namedistr+=i;
220     TH1F *hptpiSnoMcut = new TH1F(namedistr.Data(), "P_{T} distribution (pions);p_{T} [GeV/c]",200,0.,8.);
221
222     namedistr="hptKSnoMcut_";
223     namedistr+=i;
224     TH1F *hptKSnoMcut = new TH1F(namedistr.Data(), "P_{T} distribution (kaons);p_{T} [GeV/c]",200,0.,8.);
225
226     namedistr="hptB1prongnoMcut_";
227     namedistr+=i;
228     TH1F *hptB1pnoMcut = new TH1F(namedistr.Data(), "P_{T} distribution;p_{T} [GeV/c]",200,0.,8.);
229
230     namedistr="hptB2prongsnoMcut_";
231     namedistr+=i;
232     TH1F *hptB2pnoMcut = new TH1F(namedistr.Data(), "P_{T} distribution;p_{T} [GeV/c]",200,0.,8.);
233
234     
235     //  dca
236     namedistr="hdcaS_";
237     namedistr+=i;
238     TH1F *hdcaS = new TH1F(namedistr.Data(), "DCA distribution;dca [cm]",200,0.,0.1);
239     namedistr="hdcaB_";
240     namedistr+=i;
241     TH1F *hdcaB = new TH1F(namedistr.Data(), "DCA distribution;dca [cm]",200,0.,0.1);
242
243     //  costhetastar
244     namedistr="hcosthetastarS_";
245     namedistr+=i;
246     TH1F *hcosthetastarS = new TH1F(namedistr.Data(), "cos#theta* distribution;cos#theta*",200,-1.,1.);
247     namedistr="hcosthetastarB_";
248     namedistr+=i;
249     TH1F *hcosthetastarB = new TH1F(namedistr.Data(), "cos#theta* distribution;cos#theta*",200,-1.,1.);
250
251     // impact parameter
252     namedistr="hd0piS_";
253     namedistr+=i;
254     TH1F *hd0piS = new TH1F(namedistr.Data(), "Impact parameter distribution (pions);d0(#pi) [cm]",200,-0.1,0.1);
255
256     namedistr="hd0KS_";
257     namedistr+=i;
258     TH1F *hd0KS = new TH1F(namedistr.Data(), "Impact parameter distribution (kaons);d0(K) [cm]",200,-0.1,0.1);
259
260     namedistr="hd0B_";
261     namedistr+=i;
262     TH1F *hd0B = new TH1F(namedistr.Data(), "Impact parameter distribution (both);d0 [cm]",200,-0.1,0.1);
263
264     namedistr="hd0p0B_";
265     namedistr+=i;
266     TH1F *hd0p0B = new TH1F(namedistr.Data(), "Impact parameter distribution (prong +);d0 [cm]",200,-0.1,0.1);
267
268     namedistr="hd0p1B_";
269     namedistr+=i;
270     TH1F *hd0p1B = new TH1F(namedistr.Data(), "Impact parameter distribution (prong -);d0 [cm]",200,-0.1,0.1);
271
272     namedistr="hd0vpiS_";
273     namedistr+=i;
274     TH1F *hd0vpiS = new TH1F(namedistr.Data(), "Impact parameter distribution (pions)(vtx w/o these tracks);d0(#pi) [cm]",200,-0.1,0.1);
275
276     namedistr="hd0vKS_";
277     namedistr+=i;
278     TH1F *hd0vKS = new TH1F(namedistr.Data(), "Impact parameter distribution (kaons) (vtx w/o these tracks);d0(K) [cm]",200,-0.1,0.1);
279     namedistr="hd0vB_";
280     namedistr+=i;
281     TH1F *hd0vB = new TH1F(namedistr.Data(), "Impact parameter distribution (vtx w/o these tracks);d0 [cm]",200,-0.1,0.1);
282
283     namedistr="hd0vp0B_";
284     namedistr+=i;
285     TH1F *hd0vp0B = new TH1F(namedistr.Data(), "Impact parameter distribution (prong + ** vtx w/o these tracks);d0 [cm]",200,-0.1,0.1);
286
287     namedistr="hd0vp1B_";
288     namedistr+=i;
289     TH1F *hd0vp1B = new TH1F(namedistr.Data(), "Impact parameter distribution (prong - ** vtx w/o these tracks);d0 [cm]",200,-0.1,0.1);
290
291     namedistr="hd0d0S_";
292     namedistr+=i;
293     TH1F *hd0d0S = new TH1F(namedistr.Data(), "d_{0}#timesd_{0} distribution;d_{0}#timesd_{0} [cm^{2}]",200,-0.001,0.001);
294     namedistr="hd0d0B_";
295     namedistr+=i;
296     TH1F *hd0d0B = new TH1F(namedistr.Data(), "d_{0}#timesd_{0} distribution;d_{0}#timesd_{0} [cm^{2}]",200,-0.001,0.001);
297
298      namedistr="hd0d0vS_";
299     namedistr+=i;
300     TH1F *hd0d0vS = new TH1F(namedistr.Data(), "d_{0}#timesd_{0} distribution (vtx w/o these tracks);d_{0}#timesd_{0} [cm^{2}]",200,-0.001,0.001);
301     namedistr="hd0d0vB_";
302     namedistr+=i;
303     TH1F *hd0d0vB = new TH1F(namedistr.Data(), "d_{0}#timesd_{0} distribution (vtx w/o these tracks);d_{0}#timesd_{0} [cm^{2}]",200,-0.001,0.001);
304
305     //decay lenght
306     namedistr="hdeclS_";
307     namedistr+=i;
308     TH1F *hdeclengthS = new TH1F(namedistr.Data(), "Decay Length distribution;Decay Length [cm]",200,0,0.6);
309
310     namedistr="hdeclB_";
311     namedistr+=i;
312     TH1F *hdeclengthB = new TH1F(namedistr.Data(), "Decay Length distribution;Decay Length [cm]",200,0,0.6);
313
314     namedistr="hnormdeclS_";
315     namedistr+=i;
316     TH1F *hnormdeclengthS = new TH1F(namedistr.Data(), "Normalized Decay Length distribution;Decay Length/Err ",200,0,10.);
317
318     namedistr="hnormdeclB_";
319     namedistr+=i;
320     TH1F *hnormdeclengthB = new TH1F(namedistr.Data(), "Normalized Decay Length distribution;Decay Length/Err ",200,0,10.);
321
322     namedistr="hdeclvS_";
323     namedistr+=i;
324     TH1F *hdeclengthvS = new TH1F(namedistr.Data(), "Decay Length distribution (vtx w/o tracks);Decay Length [cm]",200,0,0.6);
325
326     namedistr="hdeclvB_";
327     namedistr+=i;
328     TH1F *hdeclengthvB = new TH1F(namedistr.Data(), "Decay Length distribution (vtx w/o tracks);Decay Length [cm]",200,0,0.6);
329
330     namedistr="hnormdeclvS_";
331     namedistr+=i;
332     TH1F *hnormdeclengthvS = new TH1F(namedistr.Data(), "Normalized Decay Length distribution (vtx w/o tracks);Decay Length/Err ",200,0,10.);
333
334     namedistr="hnormdeclvB_";
335     namedistr+=i;
336     TH1F *hnormdeclengthvB = new TH1F(namedistr.Data(), "Normalized Decay Length distribution (vtx w/o tracks);Decay Length/Err ",200,0,10.);
337
338    //  costhetapoint
339     namedistr="hcosthetapointS_";
340     namedistr+=i;
341     TH1F *hcosthetapointS = new TH1F(namedistr.Data(), "cos#theta_{Point} distribution;cos#theta_{Point}",200,0,1.);
342     namedistr="hcosthetapointB_";
343     namedistr+=i;
344     TH1F *hcosthetapointB = new TH1F(namedistr.Data(), "cos#theta_{Point} distribution;cos#theta_{Point}",200,0,1.);
345
346    //  costhetapoint vs d0 or d0d0
347     namedistr="hcosthpointd0S_";
348     namedistr+=i;
349     TH2F *hcosthpointd0S= new TH2F(namedistr.Data(),"Correlation cos#theta_{Point}-d_{0};cos#theta_{Point};d_{0} [cm^{2}]",200,0,1.,200,-0.001,0.001);
350
351     namedistr="hcosthpointd0B_";
352     namedistr+=i;
353     TH2F *hcosthpointd0B= new TH2F(namedistr.Data(),"Correlation cos#theta_{Point}-d_{0};cos#theta_{Point};d_{0} [cm^{2}]",200,0,1.,200,-0.001,0.001);
354
355     namedistr="hcosthpointd0d0S_";
356     namedistr+=i;
357     TH2F *hcosthpointd0d0S= new TH2F(namedistr.Data(),"Correlation cos#theta_{Point}-d_{0}#timesd_{0};cos#theta_{Point};d_{0}#timesd_{0} [cm^{2}]",200,0,1.,200,-0.001,0.001);
358     namedistr="hcosthpointd0d0B_";
359     namedistr+=i;
360     TH2F *hcosthpointd0d0B= new TH2F(namedistr.Data(),"Correlation cos#theta_{Point}-d_{0}#timesd_{0};cos#theta_{Point};d_{0}#timesd_{0} [cm^{2}]",200,0,1.,200,-0.001,0.001);
361
362     fDistr->Add(hptpiS);
363     fDistr->Add(hptKS);
364     fDistr->Add(hptB);
365
366     fDistr->Add(hptpiSnoMcut);
367     fDistr->Add(hptKSnoMcut);
368     fDistr->Add(hptB1pnoMcut);
369     fDistr->Add(hptB2pnoMcut);
370
371     fDistr->Add(hdcaS);
372     fDistr->Add(hdcaB);
373
374     fDistr->Add(hd0piS);
375     fDistr->Add(hd0KS);
376     fDistr->Add(hd0B);
377     fDistr->Add(hd0p0B);
378     fDistr->Add(hd0p1B);
379
380     fDistr->Add(hd0vpiS);
381     fDistr->Add(hd0vKS);
382     fDistr->Add(hd0vB);
383     fDistr->Add(hd0vp0B);
384     fDistr->Add(hd0vp1B);
385
386     fDistr->Add(hd0d0S);
387     fDistr->Add(hd0d0B);
388
389     fDistr->Add(hd0d0vS);
390     fDistr->Add(hd0d0vB);
391
392     fDistr->Add(hcosthetastarS);
393     fDistr->Add(hcosthetastarB);
394
395     fDistr->Add(hcosthetapointS);
396     fDistr->Add(hcosthetapointB);
397
398     fDistr->Add(hdeclengthS);
399     fDistr->Add(hdeclengthB);
400
401     fDistr->Add(hnormdeclengthS);
402     fDistr->Add(hnormdeclengthB);
403
404     fDistr->Add(hdeclengthvS);
405     fDistr->Add(hdeclengthvB);
406
407     fDistr->Add(hnormdeclengthvS);
408     fDistr->Add(hnormdeclengthvB);
409
410     fDistr->Add(hcosthpointd0S);
411     fDistr->Add(hcosthpointd0B);
412
413     fDistr->Add(hcosthpointd0d0S);
414     fDistr->Add(hcosthpointd0d0B);
415
416
417     //histograms of invariant mass distributions
418
419     TH1F* tmpMt = new TH1F(nameMass.Data(),"D^{0} invariant mass; M [GeV]; Entries",200,1.765,1.965);
420     TH1F *tmpMl=(TH1F*)tmpMt->Clone();
421     tmpMt->Sumw2();
422     tmpMl->Sumw2();
423
424     //to compare with AliAnalysisTaskCharmFraction
425     TH1F* tmpS27t = new TH1F(nameSgn27.Data(),"D^{0} invariant mass in M(D^{0}) +/- 27 MeV - MC; M [GeV]; Entries",200,1.765,1.965);
426     TH1F *tmpS27l=(TH1F*)tmpS27t->Clone();
427     tmpS27t->Sumw2();
428     tmpS27l->Sumw2();
429  
430     //distribution w/o cuts
431     //   TH1F* tmpMS = new TH1F(nameMassNocutsS.Data(),"D^{0} invariant mass; M [GeV]; Entries",300,0.7,3.);
432     TH1F* tmpMS = new TH1F(nameMassNocutsS.Data(),"D^{0} invariant mass; M [GeV]; Entries",300,1.56484,2.16484); //range (MD0-300MeV, mD0 + 300MeV)
433     TH1F *tmpMB=(TH1F*)tmpMS->Clone();
434     tmpMB->SetName(nameMassNocutsB.Data());
435     tmpMS->Sumw2();
436     tmpMB->Sumw2();
437
438     //MC signal and background
439     TH1F* tmpSt = new TH1F(nameSgn.Data(), "D^{0} invariant mass - MC; M [GeV]; Entries",200,1.765,1.965);
440     TH1F *tmpSl=(TH1F*)tmpSt->Clone();
441     tmpSt->Sumw2();
442     tmpSl->Sumw2();
443
444     TH1F* tmpBt = new TH1F(nameBkg.Data(), "Background invariant mass - MC; M [GeV]; Entries",200,1.765,1.965);
445     TH1F *tmpBl=(TH1F*)tmpBt->Clone();
446     tmpBt->Sumw2();
447     tmpBl->Sumw2();
448
449     //Reflection: histo filled with D0Mass which pass the cut (also) as D0bar and with D0bar which pass (also) the cut as D0
450     TH1F* tmpRt = new TH1F(nameRfl.Data(), "Reflected signal invariant mass - MC; M [GeV]; Entries",200,1.765,1.965);
451     TH1F *tmpRl=(TH1F*)tmpRt->Clone();
452     tmpRt->Sumw2();
453     tmpRl->Sumw2();
454   //  printf("Created histograms %s\t%s\t%s\t%s\n",tmpM->GetName(),tmpS->GetName(),tmpB->GetName(),tmpR->GetName());
455
456     fOutputMass->Add(tmpMt);
457     fOutputMass->Add(tmpSt);
458     fOutputMass->Add(tmpS27t);
459     fOutputMass->Add(tmpBt);
460     fOutputMass->Add(tmpRt);
461
462     fDistr->Add(tmpMS);
463     fDistr->Add(tmpMB);
464
465
466   }
467
468   //histograms for vertex checking and TOF checking
469   TString checkname="hptGoodTr";
470
471   TH1F* hptGoodTr=new TH1F(checkname.Data(),"Pt distribution of 'good' tracks;p_{t}[GeV];Number",200,0.,8.);
472   hptGoodTr->SetTitleOffset(1.3,"Y");
473   checkname="hdistrGoodTr";
474
475   TH1F* hdistrGoodTr=new TH1F(checkname.Data(),"Distribution of number of good tracks per event;no.good-tracks/ev;Entries",31,0,31);
476   hdistrGoodTr->SetTitleOffset(1.3,"Y");
477
478   checkname="hTOFsig";
479   TH1F* hTOFsig=new TH1F(checkname.Data(),"Distribution of TOF signal;TOF time [ps];Entries", 100, -2.e3,40.e3);
480
481   checkname="hTOFtimeKaonHyptime";
482   TH2F* hTOFtimeKaonHyptime=new TH2F(checkname.Data(),"TOFtime - timeHypothesisForKaon;p_{t}[GeV/c];TOFtime - timeHypothesisForKaon [ps]",200,0.,4.,1000,-20000.,20000.);
483  
484  checkname="hTOFtime";
485   TH1F* hTOFtime=new TH1F(checkname.Data(),"Distribution of TOF time Kaon;TOF time(Kaon) [ps];Entries", 1000, 0.,50000.);
486   
487
488   fChecks->Add(hptGoodTr);
489   fChecks->Add(hdistrGoodTr);
490   fChecks->Add(hTOFsig);
491   fChecks->Add(hTOFtimeKaonHyptime);
492   fChecks->Add(hTOFtime);
493
494   const char* nameoutput=GetOutputSlot(3)->GetContainer()->GetName();
495
496   fNentries=new TH1F(nameoutput, "Integral(1,2) = number of AODs *** Integral(2,3) = number of candidates selected with cuts *** Integral(3,4) = number of D0 selected with cuts *** Integral(4,5) = events with good vertex ***  Integral(5,6) = pt out of bounds", 8,0.,8.);
497
498   fNentries->GetXaxis()->SetBinLabel(1,"nEventsAnal");
499   fNentries->GetXaxis()->SetBinLabel(2,"nCandSel(Cuts)");
500   fNentries->GetXaxis()->SetBinLabel(3,"nD0Selected");
501   fNentries->GetXaxis()->SetBinLabel(4,"nEventsGoodVtx");
502   fNentries->GetXaxis()->SetBinLabel(5,"nEventsGoodVtx+>2tracks");
503   fNentries->GetXaxis()->SetBinLabel(6,"ptbin = -1");
504   fNentries->GetXaxis()->SetBinLabel(7,"no daughter");
505   fNentries->GetXaxis()->SetBinLabel(8,"nCandSel(Tr)");
506
507   fNentries->GetXaxis()->SetNdivisions(1,kFALSE);
508
509  
510   // Post the data
511   PostData(1,fOutputMass);
512   PostData(2,fDistr);
513   PostData(3,fNentries);
514   PostData(4,fChecks);
515   
516   return;
517 }
518
519 //________________________________________________________________________
520 void AliAnalysisTaskSED0Mass::UserExec(Option_t */*option*/)
521 {
522   // Execute analysis for current event:
523   // heavy flavor candidates association to MC truth
524   //cout<<"I'm in UserExec"<<endl;
525
526
527     //cuts order
528     //       printf("    |M-MD0| [GeV]    < %f\n",fD0toKpiCuts[0]);
529     //     printf("    dca    [cm]  < %f\n",fD0toKpiCuts[1]);
530     //     printf("    cosThetaStar     < %f\n",fD0toKpiCuts[2]);
531     //     printf("    pTK     [GeV/c]    > %f\n",fD0toKpiCuts[3]);
532     //     printf("    pTpi    [GeV/c]    > %f\n",fD0toKpiCuts[4]);
533     //     printf("    |d0K|  [cm]  < %f\n",fD0toKpiCuts[5]);
534     //     printf("    |d0pi| [cm]  < %f\n",fD0toKpiCuts[6]);
535     //     printf("    d0d0  [cm^2] < %f\n",fD0toKpiCuts[7]);
536     //     printf("    cosThetaPoint    > %f\n",fD0toKpiCuts[8]);
537   
538
539   AliAODEvent *aod = dynamic_cast<AliAODEvent*> (InputEvent());
540
541   TString bname;
542   if(fArray==0){ //D0 candidates
543     // load D0->Kpi candidates
544     //cout<<"D0 candidates"<<endl;
545     bname="D0toKpi";
546   } else { //LikeSign candidates
547     // load like sign candidates
548     //cout<<"LS candidates"<<endl;
549     bname="LikeSign2Prong";
550   }
551
552   TClonesArray *inputArray=0;
553  
554   if(!aod && AODEvent() && IsStandardAOD()) {
555     // In case there is an AOD handler writing a standard AOD, use the AOD 
556     // event in memory rather than the input (ESD) event.    
557     aod = dynamic_cast<AliAODEvent*> (AODEvent());
558     // in this case the braches in the deltaAOD (AliAOD.VertexingHF.root)
559     // have to taken from the AOD event hold by the AliAODExtension
560     AliAODHandler* aodHandler = (AliAODHandler*) 
561       ((AliAnalysisManager::GetAnalysisManager())->GetOutputEventHandler());
562
563     if(aodHandler->GetExtensions()) {
564       AliAODExtension *ext = (AliAODExtension*)aodHandler->GetExtensions()->FindObject("AliAOD.VertexingHF.root");
565       AliAODEvent* aodFromExt = ext->GetAOD();
566       inputArray=(TClonesArray*)aodFromExt->GetList()->FindObject(bname.Data());
567     }
568   } else {
569     inputArray=(TClonesArray*)aod->GetList()->FindObject(bname.Data());
570   }
571
572
573   if(!inputArray) {
574     printf("AliAnalysisTaskSED0Mass::UserExec: input branch not found!\n");
575     return;
576   }
577   
578   
579   TClonesArray *mcArray = 0;
580   AliAODMCHeader *mcHeader = 0;
581
582   if(fReadMC) {
583     // load MC particles
584     mcArray = (TClonesArray*)aod->GetList()->FindObject(AliAODMCParticle::StdBranchName());
585     if(!mcArray) {
586       printf("AliAnalysisTaskSED0Mass::UserExec: MC particles branch not found!\n");
587       return;
588     }
589     
590     // load MC header
591     mcHeader = (AliAODMCHeader*)aod->GetList()->FindObject(AliAODMCHeader::StdBranchName());
592     if(!mcHeader) {
593       printf("AliAnalysisTaskSED0Mass::UserExec: MC header branch not found!\n");
594       return;
595     }
596   }
597   
598   //printf("VERTEX Z %f %f\n",vtx1->GetZ(),mcHeader->GetVtxZ());
599   
600   //histogram filled with 1 for every AOD
601   fNentries->Fill(0);
602     
603
604   
605   // AOD primary vertex
606   AliAODVertex *vtx1 = (AliAODVertex*)aod->GetPrimaryVertex();
607
608   Bool_t isGoodVtx=kFALSE;
609
610    //vtx1->Print();
611   TString primTitle = vtx1->GetTitle();
612   if(primTitle.Contains("VertexerTracks") && vtx1->GetNContributors()>0) {
613     isGoodVtx=kTRUE;
614     fNentries->Fill(3);
615   }
616
617   //cout<<"Start checks"<<endl;
618   Int_t ntracks=0,isGoodTrack=0;
619
620   if(aod) ntracks=aod->GetNTracks();
621
622   //cout<<"ntracks = "<<ntracks<<endl;
623  
624   //loop on tracks in the event
625   for (Int_t k=0;k<ntracks;k++){
626     AliAODTrack* track=aod->GetTrack(k);
627
628     //check TOF
629
630     if(!(track->GetStatus()&AliESDtrack::kTPCrefit &&
631          track->GetStatus()&AliESDtrack::kITSrefit && 
632          track->GetTPCNcls() >=70 &&
633          track->GetStatus()&AliESDtrack::kTOFpid && 
634          track->GetStatus()&AliESDtrack::kTOFout &&
635          track->GetStatus()&AliESDtrack::kTIME)) continue;
636     AliAODPid *pid = track->GetDetPid();
637     if(!pid)  {if (fDebug>1)cout<<"No AliAODPid found"<<endl; continue;}
638
639     Double_t times[5];
640     pid->GetIntegratedTimes(times);
641
642     ((TH1F*)fChecks->FindObject("hTOFtime"))->Fill(times[3]);
643     ((TH1F*)fChecks->FindObject("hTOFtimeKaonHyptime"))->Fill(track->P(),pid->GetTOFsignal()-times[3]); //3 is kaon
644
645     ((TH1F*)fChecks->FindObject("hTOFsig"))->Fill(pid->GetTOFsignal());
646     if (pid->GetTOFsignal()< 0) ((TH1F*)fChecks->FindObject("hTOFsig"))->Fill(-1);
647
648
649     //check clusters of the tracks
650     Int_t nclsTot=0,nclsSPD=0;
651     
652     for(Int_t l=0;l<6;l++) {
653       if(TESTBIT(track->GetITSClusterMap(),l)) {
654         nclsTot++; if(l<2) nclsSPD++;
655       }
656     }
657     
658     if (track->Pt()>0.3 &&
659         track->GetStatus()&AliESDtrack::kTPCrefit &&
660         track->GetStatus()&AliESDtrack::kITSrefit &&
661         nclsTot>3 &&
662         nclsSPD>0) {//fill hist good tracks
663      
664       ((TH1F*)fChecks->FindObject("hptGoodTr"))->Fill(track->Pt());
665       isGoodTrack++;
666     }
667     //cout<<"isGoodTrack = "<<isGoodTrack<<endl;
668     ((TH1F*)fChecks->FindObject("hdistrGoodTr"))->Fill(isGoodTrack);
669   }
670   //number of events with good vertex and at least 2 good tracks
671   if (isGoodTrack>=2 && isGoodVtx) fNentries->Fill(4);
672
673
674   // loop over candidates
675   Int_t nInD0toKpi = inputArray->GetEntriesFast();
676   if(fDebug>2) printf("Number of D0->Kpi: %d\n",nInD0toKpi);
677
678   for (Int_t iD0toKpi = 0; iD0toKpi < nInD0toKpi; iD0toKpi++) {
679     //Int_t nPosPairs=0, nNegPairs=0;
680     //cout<<"inside the loop"<<endl;
681     AliAODRecoDecayHF2Prong *d = (AliAODRecoDecayHF2Prong*)inputArray->UncheckedAt(iD0toKpi);
682
683     //check daughters
684     if(!(d->GetDaughter(0) || d->GetDaughter(1))) {
685       AliDebug(1,"at least one daughter not found!");
686       fNentries->Fill(6);
687       return;
688     }
689
690     Bool_t unsetvtx=kFALSE;
691     if(!d->GetOwnPrimaryVtx()) {
692       d->SetOwnPrimaryVtx(vtx1); // needed to compute all variables
693       unsetvtx=kTRUE;
694     }
695   
696     
697     //check reco daughter in acceptance
698     /*
699     Double_t eta0=d->EtaProng(0);
700     Double_t eta1=d->EtaProng(1);
701     */
702     if ( fCuts->IsInFiducialAcceptance(d->Pt(),d->Y(421)) ) {
703       //if( TMath::Abs(eta0)<0.9 && TMath::Abs(eta1)<0.9 ){
704        //apply cuts on tracks
705       Int_t isSelected = fCuts->IsSelected(d,AliRDHFCuts::kTracks);
706       if(((AliAODTrack*)d->GetDaughter(0))->GetTPCNcls() < 70 || ((AliAODTrack*)d->GetDaughter(1))->GetTPCNcls() < 70) isSelected=kFALSE;
707       if (!isSelected) return;
708       fNentries->Fill(7);       
709       if(fDebug>2) cout<<"tracks selected"<<endl;
710
711       FillVarHists(aod,d,mcArray,fCuts,fDistr);
712       FillMassHists(d,mcArray,fCuts,fOutputMass);
713     }
714   
715     if(unsetvtx) d->UnsetOwnPrimaryVtx();
716   } //end for prongs
717
718
719   // Post the data
720   PostData(1,fOutputMass);
721   PostData(2,fDistr);
722   PostData(3,fNentries);
723   PostData(4,fChecks);
724
725   return;
726 }
727
728 //____________________________________________________________________________
729 void AliAnalysisTaskSED0Mass::FillVarHists(AliAODEvent* aod,AliAODRecoDecayHF2Prong *part, TClonesArray *arrMC, AliRDHFCutsD0toKpi *cuts, TList *listout){
730   //
731   // function used in UserExec to fill variable histograms:
732   //
733
734   //add distr here
735   UInt_t pdgs[2];
736     
737   Double_t mPDG=TDatabasePDG::Instance()->GetParticle(421)->Mass();
738   pdgs[0]=211;
739   pdgs[1]=321;
740   Double_t minvD0 = part->InvMassD0();
741   pdgs[1]=211;
742   pdgs[0]=321;
743   Double_t minvD0bar = part->InvMassD0bar();
744   //cout<<"inside mass cut"<<endl;
745
746   Int_t pdgDgD0toKpi[2]={321,211};
747   Int_t lab=-9999;
748   if(fReadMC) lab=part->MatchToMC(421,arrMC,2,pdgDgD0toKpi); //return MC particle label if the array corresponds to a D0, -1 if not (cf. AliAODRecoDecay.cxx)
749   //Double_t pt = d->Pt(); //mother pt
750   Bool_t isSelected=kFALSE;
751
752
753   if(fCutOnDistr){
754     isSelected = cuts->IsSelected(part,AliRDHFCuts::kCandidate);
755     if (!isSelected){
756       //cout<<"Not Selected"<<endl;
757       return;
758     }
759   }
760
761   Double_t invmasscut=0.03;
762
763   TString fillthispi="",fillthisK="",fillthis="";
764
765   Int_t ptbin=cuts->PtBin(part->Pt());
766   if(ptbin==-1) {fNentries->Fill(5); return;} //out of bounds
767
768   //recalculate vertex
769   AliAODVertex *vtxProp=0x0;
770   vtxProp=GetPrimaryVtxSkipped(aod,part);
771   Double_t dz1[2],dz2[2],covar1[3],covar2[3];//,d0xd0proper,errd0xd0proper;
772   dz1[0]=-99; dz2[0]=-99;
773   if(vtxProp) {
774     part->SetOwnPrimaryVtx(vtxProp);
775     //Bool_t unsetvtx=kTRUE;
776     //Calculate d0 for daughter tracks with Vtx Skipped
777     AliESDtrack *esdtrack1=new AliESDtrack((AliVTrack*)part->GetDaughter(0));
778     AliESDtrack *esdtrack2=new AliESDtrack((AliVTrack*)part->GetDaughter(1));
779     esdtrack1->PropagateToDCA(vtxProp,aod->GetMagneticField(),1.,dz1,covar1);
780     esdtrack2->PropagateToDCA(vtxProp,aod->GetMagneticField(),1.,dz2,covar2);
781     delete esdtrack1;
782     delete esdtrack2;
783   }
784
785   Double_t d0[2]={dz1[0],dz2[0]};
786
787   if(!fCutOnDistr || (fCutOnDistr && isSelected)){ //if no cuts or cuts passed 
788     //printf("\nif no cuts or cuts passed\n");
789     if(lab>=0 && fReadMC){ //signal
790
791       //check pdg of the prongs
792       AliAODTrack *prong0=(AliAODTrack*)part->GetDaughter(0);
793       AliAODTrack *prong1=(AliAODTrack*)part->GetDaughter(1);
794       if(!prong0 || !prong1) {
795         return;
796       }
797       Int_t labprong[2];
798       labprong[0]=prong0->GetLabel();
799       labprong[1]=prong1->GetLabel();
800       AliAODMCParticle *mcprong=0;
801       Int_t pdgProng[2]={0,0};
802       
803       for (Int_t iprong=0;iprong<2;iprong++){
804         if(labprong[iprong]>=0)  mcprong= (AliAODMCParticle*)arrMC->At(labprong[iprong]);
805         pdgProng[iprong]=mcprong->GetPdgCode();
806       }
807
808       //no mass cut ditributions: ptbis
809         
810       fillthispi="hptpiSnoMcut_";
811       fillthispi+=ptbin;
812
813       fillthisK="hptKSnoMcut_";
814       fillthisK+=ptbin;
815
816       if (TMath::Abs(pdgProng[0]) == 211 && TMath::Abs(pdgProng[1]) == 321){
817         ((TH1F*)listout->FindObject(fillthispi))->Fill(part->PtProng(0));
818         ((TH1F*)listout->FindObject(fillthisK))->Fill(part->PtProng(1));
819       }else {
820         if (TMath::Abs(pdgProng[0]) == 321 && TMath::Abs(pdgProng[1]) == 211){
821           ((TH1F*)listout->FindObject(fillthisK))->Fill(part->PtProng(0));
822           ((TH1F*)listout->FindObject(fillthispi))->Fill(part->PtProng(1));
823         }
824       }
825       
826       //no mass cut ditributions: mass
827       fillthis="hMassS_";
828       fillthis+=ptbin;
829       
830       if (((AliAODMCParticle*)arrMC->At(lab))->GetPdgCode() == 421){//D0
831         ((TH1F*)listout->FindObject(fillthis))->Fill(minvD0);
832       }
833       else { //D0bar
834         ((TH1F*)listout->FindObject(fillthis))->Fill(minvD0bar);
835       }
836
837       //apply cut on invariant mass on the pair
838       if(TMath::Abs(minvD0-mPDG)<invmasscut || TMath::Abs(minvD0bar-mPDG)<invmasscut){
839         
840         if(fArray==1) cout<<"LS signal: ERROR"<<endl;
841         for (Int_t iprong=0; iprong<2; iprong++){
842           AliAODTrack *prong=(AliAODTrack*)part->GetDaughter(iprong);
843           labprong[iprong]=prong->GetLabel();
844           
845           //cout<<"prong name = "<<prong->GetName()<<" label = "<<prong->GetLabel()<<endl;
846           if(labprong[iprong]>=0)  mcprong= (AliAODMCParticle*)arrMC->At(labprong[iprong]);
847           Int_t pdgprong=mcprong->GetPdgCode();
848           
849           if(TMath::Abs(pdgprong)==211) {
850             //cout<<"pi"<<endl;
851             
852             fillthispi="hptpiS_";
853             fillthispi+=ptbin;
854             ((TH1F*)listout->FindObject(fillthispi))->Fill(part->PtProng(iprong));
855             fillthispi="hd0piS_";
856             fillthispi+=ptbin;
857             ((TH1F*)listout->FindObject(fillthispi))->Fill(part->Getd0Prong(iprong));
858             if(d0[iprong] != -99) {
859
860               fillthispi="hd0vpiS_";
861               fillthispi+=ptbin;
862               ((TH1F*)listout->FindObject(fillthispi))->Fill(d0[iprong]);
863             }
864
865           }
866           
867           if(TMath::Abs(pdgprong)==321) {
868             //cout<<"kappa"<<endl;
869             
870             fillthisK="hptKS_";
871             fillthisK+=ptbin;
872             ((TH1F*)listout->FindObject(fillthisK))->Fill(part->PtProng(iprong));
873             fillthisK="hd0KS_";
874             fillthisK+=ptbin;
875             ((TH1F*)listout->FindObject(fillthisK))->Fill(part->Getd0Prong(iprong));
876             if (d0[iprong] != -99){
877               fillthisK="hd0vKS_";
878               fillthisK+=ptbin;
879               ((TH1F*)listout->FindObject(fillthisK))->Fill(d0[iprong]);
880             }
881           }
882
883           fillthis="hcosthpointd0S_";
884           fillthis+=ptbin;        
885           ((TH1F*)listout->FindObject(fillthis))->Fill(part->CosPointingAngle(),part->Getd0Prong(iprong));
886
887         } //end loop on prongs
888
889         fillthis="hdcaS_";
890         fillthis+=ptbin;          
891         ((TH1F*)listout->FindObject(fillthis))->Fill(part->GetDCA());
892
893         fillthis="hcosthetapointS_";
894         fillthis+=ptbin;          
895         ((TH1F*)listout->FindObject(fillthis))->Fill(part->CosPointingAngle());
896
897         fillthis="hcosthpointd0d0S_";
898         fillthis+=ptbin;          
899         ((TH2F*)listout->FindObject(fillthis))->Fill(part->CosPointingAngle(),part->Prodd0d0());
900           
901         fillthis="hcosthetastarS_";
902         fillthis+=ptbin;
903         if (((AliAODMCParticle*)arrMC->At(lab))->GetPdgCode() == 421)((TH1F*)listout->FindObject(fillthis))->Fill(part->CosThetaStarD0());
904         else ((TH1F*)listout->FindObject(fillthis))->Fill(part->CosThetaStarD0bar());
905
906         fillthis="hd0d0S_";
907         fillthis+=ptbin;
908         ((TH1F*)listout->FindObject(fillthis))->Fill(part->Prodd0d0());
909
910         if(d0[0] != -99 && d0[1] != -99 ){
911           fillthis="hd0d0vS_";
912           fillthis+=ptbin;
913           ((TH1F*)listout->FindObject(fillthis))->Fill(d0[0]*d0[1]);
914         }
915
916         fillthis="hdeclS_";
917         fillthis+=ptbin;
918         ((TH1F*)listout->FindObject(fillthis))->Fill(part->DecayLength());
919
920         fillthis="hnormdeclS_";
921         fillthis+=ptbin;
922         ((TH1F*)listout->FindObject(fillthis))->Fill(part->NormalizedDecayLength());
923
924         if(vtxProp) {
925           fillthis="hdeclvS_";
926           fillthis+=ptbin;
927           ((TH1F*)listout->FindObject(fillthis))->Fill(part->AliAODRecoDecay::DecayLength(vtxProp));
928
929           fillthis="hnormdeclvS_";
930           fillthis+=ptbin;
931           ((TH1F*)listout->FindObject(fillthis))->Fill(part->AliAODRecoDecay::NormalizedDecayLength(vtxProp));
932         }
933      } //end mass cut
934     
935     } else{ //Background or LS
936       //cout<<"is background"<<endl;
937      
938       //no mass cut distributions: mass, ptbis
939       fillthis="hMassB_";
940       fillthis+=ptbin;
941       
942       if (!fCutOnDistr || (fCutOnDistr && (isSelected==1 || isSelected==3))) ((TH1F*)listout->FindObject(fillthis))->Fill(minvD0);
943       if (!fCutOnDistr || (fCutOnDistr && isSelected>1)) ((TH1F*)listout->FindObject(fillthis))->Fill(minvD0bar);
944
945       fillthis="hptB1prongnoMcut_";
946       fillthis+=ptbin;
947       
948       ((TH1F*)listout->FindObject(fillthis))->Fill(part->PtProng(0));
949       
950       fillthis="hptB2prongsnoMcut_";
951       fillthis+=ptbin;
952       ((TH1F*)listout->FindObject(fillthis))->Fill(part->PtProng(0));
953       ((TH1F*)listout->FindObject(fillthis))->Fill(part->PtProng(1));
954
955       //apply cut on invariant mass on the pair
956       if(TMath::Abs(minvD0-mPDG)<invmasscut || TMath::Abs(minvD0bar-mPDG)<invmasscut){
957
958
959         AliAODTrack *prong=(AliAODTrack*)part->GetDaughter(0);
960         if(!prong) {
961           if(fDebug>2) cout<<"No daughter found";
962           return;
963         }
964         else{
965           if(prong->Charge()==1) {fTotPosPairs[ptbin]++;} else {fTotNegPairs[ptbin]++;}
966         }
967         
968         //normalise pt distr to half afterwards
969         fillthis="hptB_";
970         fillthis+=ptbin;
971         ((TH1F*)listout->FindObject(fillthis))->Fill(part->PtProng(0));
972         ((TH1F*)listout->FindObject(fillthis))->Fill(part->PtProng(1));
973
974         fillthis="hd0p0B_";
975         fillthis+=ptbin;
976         ((TH1F*)listout->FindObject(fillthis))->Fill(part->Getd0Prong(0));
977         fillthis="hd0p1B_";
978         fillthis+=ptbin;
979         ((TH1F*)listout->FindObject(fillthis))->Fill(part->Getd0Prong(1));
980         fillthis="hd0B_";
981         fillthis+=ptbin;
982         ((TH1F*)listout->FindObject(fillthis))->Fill(part->Getd0Prong(0));
983         ((TH1F*)listout->FindObject(fillthis))->Fill(part->Getd0Prong(1));
984
985         fillthis="hd0vp0B_";
986         fillthis+=ptbin;
987         ((TH1F*)listout->FindObject(fillthis))->Fill(d0[0]);
988         fillthis="hd0vp1B_";
989         fillthis+=ptbin;
990         ((TH1F*)listout->FindObject(fillthis))->Fill(d0[1]);
991
992         fillthis="hd0vB_";
993         fillthis+=ptbin;
994         ((TH1F*)listout->FindObject(fillthis))->Fill(d0[0]);
995         ((TH1F*)listout->FindObject(fillthis))->Fill(d0[1]);
996   
997
998         fillthis="hcosthpointd0B_";
999         fillthis+=ptbin;          
1000         ((TH1F*)listout->FindObject(fillthis))->Fill(part->CosPointingAngle(),part->Getd0Prong(0));
1001         ((TH1F*)listout->FindObject(fillthis))->Fill(part->CosPointingAngle(),part->Getd0Prong(1));
1002
1003
1004         fillthis="hdcaB_";
1005         fillthis+=ptbin;
1006         ((TH1F*)listout->FindObject(fillthis))->Fill(part->GetDCA());
1007
1008         fillthis="hcosthetastarB_";
1009         fillthis+=ptbin;
1010         if (!fCutOnDistr || (fCutOnDistr && (isSelected==1 || isSelected==3)))((TH1F*)listout->FindObject(fillthis))->Fill(part->CosThetaStarD0());
1011         if (!fCutOnDistr || (fCutOnDistr && isSelected>1))((TH1F*)listout->FindObject(fillthis))->Fill(part->CosThetaStarD0bar());      
1012
1013         fillthis="hd0d0B_";
1014         fillthis+=ptbin;
1015         ((TH1F*)listout->FindObject(fillthis))->Fill(part->Prodd0d0());
1016
1017         if(d0[0] != -99 && d0[1] != -99 ){
1018           fillthis="hd0d0vB_";
1019           fillthis+=ptbin;
1020           ((TH1F*)listout->FindObject(fillthis))->Fill(d0[0]*d0[1]);
1021         }
1022
1023         fillthis="hcosthetapointB_";
1024         fillthis+=ptbin;
1025         ((TH1F*)listout->FindObject(fillthis))->Fill(part->CosPointingAngle());
1026
1027         fillthis="hcosthpointd0d0B_";
1028         fillthis+=ptbin;
1029         ((TH2F*)listout->FindObject(fillthis))->Fill(part->CosPointingAngle(),part->Prodd0d0());
1030
1031         fillthis="hdeclB_";
1032         fillthis+=ptbin;
1033         ((TH1F*)listout->FindObject(fillthis))->Fill(part->DecayLength());
1034
1035         fillthis="hnormdeclB_";
1036         fillthis+=ptbin;
1037         ((TH1F*)listout->FindObject(fillthis))->Fill(part->NormalizedDecayLength());
1038
1039         if(vtxProp) {
1040           fillthis="hdeclvB_";
1041           fillthis+=ptbin;
1042           ((TH1F*)listout->FindObject(fillthis))->Fill(part->AliAODRecoDecay::DecayLength(vtxProp));
1043
1044           fillthis="hnormdeclvB_";
1045           fillthis+=ptbin;
1046           ((TH1F*)listout->FindObject(fillthis))->Fill(part->AliAODRecoDecay::NormalizedDecayLength(vtxProp));
1047         }
1048      }//mass cut        
1049     }//else (background)
1050   }
1051   return;
1052 }
1053 //____________________________________________________________________________
1054
1055 void AliAnalysisTaskSED0Mass::FillMassHists(AliAODRecoDecayHF2Prong *part, TClonesArray *arrMC, AliRDHFCutsD0toKpi* cuts, TList *listout){
1056   //
1057   // function used in UserExec to fill mass histograms:
1058   //
1059
1060
1061   Double_t mPDG=TDatabasePDG::Instance()->GetParticle(421)->Mass();
1062
1063   Int_t isSelected=cuts->IsSelected(part,AliRDHFCuts::kCandidate); //selected
1064   //cout<<"is selected = "<<isSelected<<endl;
1065
1066   //cout<<"check cuts = "<<endl;
1067   //cuts->PrintAll();
1068   if (!isSelected){
1069     //cout<<"Not Selected"<<endl;
1070     return;
1071   }
1072
1073   if(fDebug>2)  cout<<"Candidate selected"<<endl;
1074
1075   Double_t invmassD0 = part->InvMassD0(), invmassD0bar = part->InvMassD0bar();
1076   //printf("SELECTED\n");
1077   Int_t ptbin=cuts->PtBin(part->Pt());
1078
1079   AliAODTrack *prong=(AliAODTrack*)part->GetDaughter(0);
1080   if(!prong) {
1081     AliDebug(2,"No daughter found");
1082     return;
1083   }
1084   else{
1085     if(prong->Charge()==1) {fTotPosPairs[ptbin]++;} else {fTotNegPairs[ptbin]++;}
1086   }
1087
1088   TString fillthis="";
1089   Int_t pdgDgD0toKpi[2]={321,211};
1090   Int_t labD0=-1;
1091   if (fReadMC) labD0 = part->MatchToMC(421,arrMC,2,pdgDgD0toKpi); //return MC particle label if the array corresponds to a D0, -1 if not (cf. AliAODRecoDecay.cxx)
1092
1093   //count candidates selected by cuts
1094   fNentries->Fill(1);
1095   //count true D0 selected by cuts
1096   if (fReadMC && labD0>=0) fNentries->Fill(2);
1097   //PostData(3,fNentries);
1098
1099   if (isSelected==1 || isSelected==3) { //D0
1100     fillthis="histMass_";
1101     fillthis+=ptbin;
1102     //cout<<"Filling "<<fillthis<<endl;
1103
1104     //printf("Fill mass with D0");
1105     ((TH1F*)(listout->FindObject(fillthis)))->Fill(invmassD0);
1106  
1107     if(labD0>=0) {
1108       if(fArray==1) cout<<"LS signal ERROR"<<endl;
1109
1110       AliAODMCParticle *partD0 = (AliAODMCParticle*)arrMC->At(labD0);
1111       Int_t pdgD0 = partD0->GetPdgCode();
1112       //cout<<"pdg = "<<pdgD0<<endl;
1113       if (pdgD0==421){ //D0
1114         //cout<<"Fill S with D0"<<endl;
1115         fillthis="histSgn_";
1116         fillthis+=ptbin;
1117         ((TH1F*)(listout->FindObject(fillthis)))->Fill(invmassD0);
1118         if(TMath::Abs(invmassD0 - mPDG) < 0.027){
1119           fillthis="histSgn27_";
1120           fillthis+=ptbin;
1121           ((TH1F*)(listout->FindObject(fillthis)))->Fill(invmassD0);
1122         }
1123       } else{ //it was a D0bar
1124         fillthis="histRfl_";
1125         fillthis+=ptbin;
1126         ((TH1F*)(listout->FindObject(fillthis)))->Fill(invmassD0);
1127       }
1128     } else {//background
1129       fillthis="histBkg_";
1130       fillthis+=ptbin;
1131       ((TH1F*)(listout->FindObject(fillthis)))->Fill(invmassD0);
1132     }
1133       
1134   }
1135   if (isSelected>1) { //D0bar
1136     fillthis="histMass_";
1137     fillthis+=ptbin;
1138     //printf("Fill mass with D0bar");
1139     ((TH1F*)listout->FindObject(fillthis))->Fill(invmassD0bar);
1140  
1141     if(labD0>=0) {
1142       if(fArray==1) cout<<"LS signal ERROR"<<endl;
1143       AliAODMCParticle *partD0 = (AliAODMCParticle*)arrMC->At(labD0);
1144       Int_t pdgD0 = partD0->GetPdgCode();
1145       //cout<<" pdg = "<<pdgD0<<endl;
1146       if (pdgD0==-421){ //D0bar
1147         fillthis="histSgn_";
1148         fillthis+=ptbin;
1149         ((TH1F*)(listout->FindObject(fillthis)))->Fill(invmassD0bar);
1150         if (TMath::Abs(invmassD0bar - mPDG) < 0.027){
1151           fillthis="histSgn27_";
1152           fillthis+=ptbin;
1153           ((TH1F*)(listout->FindObject(fillthis)))->Fill(invmassD0bar);
1154         }
1155
1156           
1157       } else{
1158         fillthis="histRfl_";
1159         fillthis+=ptbin;
1160         ((TH1F*)(listout->FindObject(fillthis)))->Fill(invmassD0bar);
1161       }
1162     } else {//background or LS
1163       fillthis="histBkg_";
1164       fillthis+=ptbin;
1165       ((TH1F*)(listout->FindObject(fillthis)))->Fill(invmassD0bar);
1166     }
1167   }
1168
1169   return;
1170 }
1171
1172 //__________________________________________________________________________
1173 AliAODVertex* AliAnalysisTaskSED0Mass::GetPrimaryVtxSkipped(AliAODEvent *aodev,AliAODRecoDecayHF2Prong *d){
1174   //Calculate the primary vertex w/o the daughter tracks of the candidate
1175   
1176   AliESDVertex *vertexESD=0x0;
1177   AliAODVertex *vertexAOD=0x0;
1178   AliVertexerTracks *vertexer = new AliVertexerTracks(aodev->GetMagneticField());
1179   
1180   Int_t skipped[2];
1181   Int_t nTrksToSkip=2;
1182   AliAODTrack *dgTrack = (AliAODTrack*)d->GetDaughter(0);
1183   if(!dgTrack){
1184     AliDebug(2,"no daughter found!");
1185     return 0x0;
1186   }
1187   skipped[0]=dgTrack->GetID();
1188   dgTrack = (AliAODTrack*)d->GetDaughter(1);
1189   if(!dgTrack){
1190     AliDebug(2,"no daughter found!");
1191     return 0x0;
1192   }
1193   skipped[1]=dgTrack->GetID();
1194
1195  
1196   //
1197   vertexer->SetSkipTracks(nTrksToSkip,skipped);
1198   vertexer->SetMinClusters(4);  
1199   vertexESD = (AliESDVertex*)vertexer->FindPrimaryVertex(aodev); 
1200   if(!vertexESD) return vertexAOD;
1201   if(vertexESD->GetNContributors()<=0) { 
1202     AliDebug(2,"vertexing failed"); 
1203     delete vertexESD; vertexESD=NULL;
1204     return vertexAOD;
1205   }
1206   
1207   delete vertexer; vertexer=NULL;
1208   
1209   
1210   // convert to AliAODVertex
1211   Double_t pos[3],cov[6],chi2perNDF;
1212   vertexESD->GetXYZ(pos); // position
1213   vertexESD->GetCovMatrix(cov); //covariance matrix
1214   chi2perNDF = vertexESD->GetChi2toNDF();
1215   delete vertexESD; vertexESD=NULL;
1216   
1217   vertexAOD = new AliAODVertex(pos,cov,chi2perNDF);
1218   return vertexAOD;
1219   
1220 }
1221
1222
1223 //________________________________________________________________________
1224 void AliAnalysisTaskSED0Mass::Terminate(Option_t */*option*/)
1225 {
1226   // Terminate analysis
1227   //
1228   if(fDebug > 1) printf("AnalysisTaskSED0Mass: Terminate() \n");
1229
1230
1231   fOutputMass = dynamic_cast<TList*> (GetOutputData(1));
1232   if (!fOutputMass) {     
1233     printf("ERROR: fOutputMass not available\n");
1234     return;
1235   }
1236   fDistr = dynamic_cast<TList*> (GetOutputData(2));
1237   if (!fDistr) {
1238     printf("ERROR: fDistr not available\n");
1239     return;
1240   }
1241  
1242   fNentries = dynamic_cast<TH1F*>(GetOutputData(3));
1243   
1244   if(!fNentries){
1245     printf("ERROR: fNEntries not available\n");
1246     return;
1247   }
1248
1249   fChecks = dynamic_cast<TList*> (GetOutputData(4));
1250   if (!fChecks) {
1251     printf("ERROR: fChecks not available\n");
1252     return;
1253   }
1254   
1255  
1256   for(Int_t ipt=0;ipt<5;ipt++){ //change 5 in GetNPtBins when sure it is written and check
1257
1258
1259     if(fArray==1){ 
1260       fLsNormalization = 2.*TMath::Sqrt(fTotPosPairs[ipt]*fTotNegPairs[ipt]); 
1261
1262
1263       if(fLsNormalization>1e-6) {
1264         
1265         TString massName="histMass_";
1266         massName+=ipt;
1267         ((TH1F*)fOutputMass->FindObject(massName))->Scale((1/fLsNormalization)*((TH1F*)fOutputMass->FindObject(massName))->GetEntries());
1268
1269       }
1270     
1271
1272       fLsNormalization = 2.*TMath::Sqrt(fTotPosPairs[4]*fTotNegPairs[4]);
1273
1274       if(fLsNormalization>1e-6) {
1275
1276         TString nameDistr="hptB_";
1277         nameDistr+=ipt;
1278         ((TH1F*)fDistr->FindObject(nameDistr))->Scale((1/fLsNormalization)*((TH1F*)fDistr->FindObject(nameDistr))->GetEntries());
1279         nameDistr="hdcaB_";
1280         nameDistr+=ipt;
1281         ((TH1F*)fDistr->FindObject(nameDistr))->Scale((1/fLsNormalization)*((TH1F*)fDistr->FindObject(nameDistr))->GetEntries());
1282         nameDistr="hcosthetastarB_";
1283         nameDistr+=ipt;
1284         ((TH1F*)fDistr->FindObject(nameDistr))->Scale((1/fLsNormalization)*((TH1F*)fDistr->FindObject(nameDistr))->GetEntries());
1285         nameDistr="hd0B_";
1286         nameDistr+=ipt;
1287         ((TH1F*)fDistr->FindObject(nameDistr))->Scale((1/fLsNormalization)*((TH1F*)fDistr->FindObject(nameDistr))->GetEntries());
1288         nameDistr="hd0d0B_";
1289         nameDistr+=ipt;
1290         ((TH1F*)fDistr->FindObject(nameDistr))->Scale((1/fLsNormalization)*((TH1F*)fDistr->FindObject(nameDistr))->GetEntries());
1291         nameDistr="hcosthetapointB_";
1292         nameDistr+=ipt;
1293         ((TH1F*)fDistr->FindObject(nameDistr))->Scale((1/fLsNormalization)*((TH1F*)fDistr->FindObject(nameDistr))->GetEntries());
1294         nameDistr="hcosthpointd0d0B_";
1295         nameDistr+=ipt;
1296         ((TH2F*)fDistr->FindObject(nameDistr))->Scale((1/fLsNormalization)*((TH2F*)fDistr->FindObject(nameDistr))->GetEntries());
1297
1298       }
1299     }
1300   }
1301   TString cvname,cstname;
1302
1303   if (fArray==0){
1304     cvname="D0invmass";
1305     cstname="cstat0";
1306   } else {
1307     cvname="LSinvmass";
1308     cstname="cstat1";
1309   }
1310
1311   TCanvas *cMass=new TCanvas(cvname,cvname);
1312   cMass->cd();
1313   ((TH1F*)fOutputMass->FindObject("histMass_3"))->Draw();
1314
1315   TCanvas* cStat=new TCanvas(cstname,Form("Stat%s",fArray ? "LS" : "D0"));
1316   cStat->cd();
1317   cStat->SetGridy();
1318   fNentries->Draw("htext0");
1319
1320   // TCanvas *ccheck=new TCanvas(Form("cc%d",fArray),Form("cc%d",fArray));
1321   // ccheck->cd();
1322
1323   return;
1324 }
1325