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