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