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