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