Changed AddTasks
[u/mrichter/AliRoot.git] / PWGGA / GammaConv / AliAnalysisTaskGammaConvV1.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: Martin Wilde, Daniel Lohner, Friederike Bock                   *
5  * Version 1.0                                                            *
6  *                                                                        *
7  * based on: on older version (see aliroot up to v5-04-42-AN)             *
8  *           AliAnalysisTaskGammaConversion.cxx                           *
9  *           Authors: Kathrin Koch, Kenneth Aamodt, Ana Marin             *
10  *                                                                        *
11  * Permission to use, copy, modify and distribute this software and its   *
12  * documentation strictly for non-commercial purposes is hereby granted   *
13  * without fee, provided that the above copyright notice appears in all   *
14  * copies and that both the copyright notice and this permission notice   *
15  * appear in the supporting documentation. The authors make no claims     *
16  * about the suitability of this software for any purpose. It is          *
17  * provided "as is" without express or implied warranty.                  *
18  **************************************************************************/
19
20 ////////////////////////////////////////////////
21 //---------------------------------------------
22
23 // Class used to do analysis on conversion pairs
24 //---------------------------------------------
25 ///////////////////////////////////////////////
26 #include "TChain.h"
27 #include "TTree.h"
28 #include "TBranch.h"
29 #include "TFile.h"
30 #include "TH1F.h"
31 #include "TH2F.h"
32 #include "TH3F.h"
33 #include "THnSparse.h"
34 #include "TCanvas.h"
35 #include "TNtuple.h"
36 #include "AliAnalysisTask.h"
37 #include "AliAnalysisManager.h"
38 #include "AliESDEvent.h"
39 #include "AliESDInputHandler.h"
40 #include "AliMCEventHandler.h"
41 #include "AliMCEvent.h"
42 #include "AliMCParticle.h"
43 #include "AliCentrality.h"
44 #include "AliESDVZERO.h"
45 #include "AliESDpid.h"
46 #include "AliAnalysisTaskGammaConvV1.h"
47 #include "AliVParticle.h"
48 #include "AliESDtrack.h"
49 #include "AliESDtrackCuts.h"
50 #include "AliKFVertex.h"
51 #include "AliV0ReaderV1.h"
52 #include "AliGenCocktailEventHeader.h"
53 #include "AliConversionAODBGHandlerRP.h"
54 #include "AliAODMCParticle.h"
55 #include "AliAODMCHeader.h"
56 #include "AliEventplane.h"
57
58 ClassImp(AliAnalysisTaskGammaConvV1)
59
60 //________________________________________________________________________
61 AliAnalysisTaskGammaConvV1::AliAnalysisTaskGammaConvV1(): AliAnalysisTaskSE(),
62    fV0Reader(NULL),
63    fBGHandler(NULL),
64    fBGHandlerRP(NULL),
65    fInputEvent(NULL),
66    fMCEvent(NULL),
67    fMCStack(NULL),
68    fCutFolder(NULL),
69    fESDList(NULL),
70    fBackList(NULL),
71    fMotherList(NULL),
72    fPhotonDCAList(NULL),
73    fMesonDCAList(NULL),        
74    fTrueList(NULL),
75    fMCList(NULL),
76    fHeaderNameList(NULL),
77    fOutputContainer(0),
78    fReaderGammas(NULL),
79    fGammaCandidates(NULL),
80    fCutArray(NULL),
81    fConversionCuts(NULL),
82    fMesonCutArray(NULL),
83    fMesonCuts(NULL),
84    hESDConvGammaPt(NULL),
85    hESDConvGammaR(NULL),
86    hESDConvGammaEta(NULL),
87    tESDConvGammaPtDcazCat(NULL),
88    fPtGamma(0),
89    fDCAzPhoton(0),
90    fRConvPhoton(0),
91    fEtaPhoton(0),
92    iCatPhoton(0),
93    iPhotonMCInfo(0),
94    hESDMotherInvMassPt(NULL),
95    sESDMotherInvMassPtZM(NULL),
96    hESDMotherBackInvMassPt(NULL),
97    sESDMotherBackInvMassPtZM(NULL),
98    hESDMotherInvMassEalpha(NULL),
99    hESDMotherPi0PtY(NULL),
100    hESDMotherEtaPtY(NULL),
101    hESDMotherPi0PtAlpha(NULL),
102    hESDMotherEtaPtAlpha(NULL),
103    hESDMotherPi0PtOpenAngle(NULL),
104    hESDMotherEtaPtOpenAngle(NULL),
105    hMCHeaders(NULL),
106    hMCAllGammaPt(NULL),
107    hMCDecayGammaPi0Pt(NULL),
108    hMCDecayGammaRhoPt(NULL),
109    hMCDecayGammaEtaPt(NULL),
110    hMCDecayGammaOmegaPt(NULL),
111    hMCDecayGammaEtapPt(NULL),
112    hMCDecayGammaPhiPt(NULL),
113    hMCDecayGammaSigmaPt(NULL),
114    hMCConvGammaPt(NULL),
115    hMCConvGammaR(NULL),
116    hMCConvGammaEta(NULL),
117    hMCPi0Pt(NULL),
118    hMCPi0WOWeightPt(NULL),
119    hMCEtaPt(NULL),
120    hMCEtaWOWeightPt(NULL),
121    hMCPi0InAccPt(NULL),
122    hMCEtaInAccPt(NULL),
123    hMCPi0PtY(NULL),
124    hMCEtaPtY(NULL),
125    hMCK0sPt(NULL),
126    hMCK0sWOWeightPt(NULL),
127    hMCK0sPtY(NULL),
128    hESDTrueMotherInvMassPt(NULL),
129    hESDTruePrimaryMotherInvMassPt(NULL),
130    hESDTruePrimaryMotherW0WeightingInvMassPt(NULL),
131    pESDTruePrimaryMotherWeightsInvMassPt(NULL),
132    hESDTruePrimaryPi0MCPtResolPt(NULL),
133    hESDTruePrimaryEtaMCPtResolPt(NULL),
134    hESDTrueSecondaryMotherInvMassPt(NULL),
135    hESDTrueSecondaryMotherFromK0sInvMassPt(NULL),
136    hESDTrueK0sWithPi0DaughterMCPt(NULL),
137    hESDTrueSecondaryMotherFromEtaInvMassPt(NULL),
138    hESDTrueEtaWithPi0DaughterMCPt(NULL),
139    hESDTrueBckGGInvMassPt(NULL),
140    hESDTrueBckContInvMassPt(NULL),
141    hESDTruePi0PtY(NULL),
142    hESDTrueEtaPtY(NULL),
143    hESDTruePi0PtAlpha(NULL),
144    hESDTrueEtaPtAlpha(NULL),
145    hESDTruePi0PtOpenAngle(NULL),
146    hESDTrueEtaPtOpenAngle(NULL),
147    hESDTrueMotherDalitzInvMassPt(NULL),
148    hESDTrueConvGammaPt(NULL),
149    hESDCombinatorialPt(NULL),
150    hESDTruePrimaryConvGammaPt(NULL),
151    hESDTruePrimaryConvGammaESDPtMCPt(NULL),
152    hESDTrueSecondaryConvGammaPt(NULL),
153    hESDTrueSecondaryConvGammaFromXFromK0sPt(NULL),
154    hESDTrueSecondaryConvGammaFromXFromLambdaPt(NULL),
155    hESDTrueDalitzPsiPairDeltaPhi(NULL),
156    hESDTrueGammaPsiPairDeltaPhi(NULL),
157    hNEvents(NULL),
158    hNGoodESDTracks(NULL),
159    hNGammaCandidates(NULL),
160    hNV0Tracks(NULL),
161    hEtaShift(NULL),
162    tESDMesonsInvMassPtDcazMinDcazMaxFlag(NULL),
163    fInvMass(0),
164    fPt(0),
165    fDCAzGammaMin(0),
166    fDCAzGammaMax(0),
167    iFlag(0),
168    iMesonMCInfo(0),
169    fEventPlaneAngle(-100),
170    fRandom(0),
171    fnGammaCandidates(0),
172    fUnsmearedPx(NULL),
173    fUnsmearedPy(NULL),
174    fUnsmearedPz(NULL),
175    fUnsmearedE(NULL),
176    fMCStackPos(NULL),
177    fMCStackNeg(NULL),
178    fESDArrayPos(NULL),
179    fESDArrayNeg(NULL),
180    fnCuts(0),
181    fiCut(0),
182    fMoveParticleAccordingToVertex(kTRUE),
183    fIsHeavyIon(0),
184    fDoMesonAnalysis(kTRUE),
185    fDoMesonQA(0),
186    fDoPhotonQA(0),
187    fIsFromMBHeader(kTRUE),
188    fIsMC(kFALSE)
189 {
190
191 }
192
193 //________________________________________________________________________
194 AliAnalysisTaskGammaConvV1::AliAnalysisTaskGammaConvV1(const char *name):
195    AliAnalysisTaskSE(name),
196    fV0Reader(NULL),
197    fBGHandler(NULL),
198    fBGHandlerRP(NULL),
199    fInputEvent(NULL),
200    fMCEvent(NULL),
201    fMCStack(NULL),
202    fCutFolder(NULL),
203    fESDList(NULL),
204    fBackList(NULL),
205    fMotherList(NULL),
206    fPhotonDCAList(NULL),
207    fMesonDCAList(NULL),
208    fTrueList(NULL),
209    fMCList(NULL),
210    fHeaderNameList(NULL),
211    fOutputContainer(0),
212    fReaderGammas(NULL),
213    fGammaCandidates(NULL),
214    fCutArray(NULL),
215    fConversionCuts(NULL),
216    fMesonCutArray(NULL),
217    fMesonCuts(NULL),
218    hESDConvGammaPt(NULL),
219    hESDConvGammaR(NULL),
220    hESDConvGammaEta(NULL),
221    tESDConvGammaPtDcazCat(NULL),
222    fPtGamma(0),
223    fDCAzPhoton(0),
224    fRConvPhoton(0),
225    fEtaPhoton(0),
226    iCatPhoton(0),
227    iPhotonMCInfo(0),
228    hESDMotherInvMassPt(NULL),
229    sESDMotherInvMassPtZM(NULL),
230    hESDMotherBackInvMassPt(NULL),
231    sESDMotherBackInvMassPtZM(NULL),
232    hESDMotherInvMassEalpha(NULL),
233    hESDMotherPi0PtY(NULL),
234    hESDMotherEtaPtY(NULL),
235    hESDMotherPi0PtAlpha(NULL),
236    hESDMotherEtaPtAlpha(NULL),
237    hESDMotherPi0PtOpenAngle(NULL),
238    hESDMotherEtaPtOpenAngle(NULL),
239    hMCHeaders(NULL),
240    hMCAllGammaPt(NULL),
241    hMCDecayGammaPi0Pt(NULL),
242    hMCDecayGammaRhoPt(NULL),
243    hMCDecayGammaEtaPt(NULL),
244    hMCDecayGammaOmegaPt(NULL),
245    hMCDecayGammaEtapPt(NULL),
246    hMCDecayGammaPhiPt(NULL),
247    hMCDecayGammaSigmaPt(NULL),
248    hMCConvGammaPt(NULL),
249    hMCConvGammaR(NULL),
250    hMCConvGammaEta(NULL),
251    hMCPi0Pt(NULL),
252    hMCPi0WOWeightPt(NULL),
253    hMCEtaPt(NULL),
254    hMCEtaWOWeightPt(NULL),
255    hMCPi0InAccPt(NULL),
256    hMCEtaInAccPt(NULL),
257    hMCPi0PtY(NULL),
258    hMCEtaPtY(NULL),
259    hMCK0sPt(NULL),
260    hMCK0sWOWeightPt(NULL),
261    hMCK0sPtY(NULL),
262    hESDTrueMotherInvMassPt(NULL),
263    hESDTruePrimaryMotherInvMassPt(NULL),
264    hESDTruePrimaryMotherW0WeightingInvMassPt(NULL),
265    pESDTruePrimaryMotherWeightsInvMassPt(NULL),
266    hESDTruePrimaryPi0MCPtResolPt(NULL),
267    hESDTruePrimaryEtaMCPtResolPt(NULL),
268    hESDTrueSecondaryMotherInvMassPt(NULL),
269    hESDTrueSecondaryMotherFromK0sInvMassPt(NULL),
270    hESDTrueK0sWithPi0DaughterMCPt(NULL),
271    hESDTrueSecondaryMotherFromEtaInvMassPt(NULL),
272    hESDTrueEtaWithPi0DaughterMCPt(NULL),
273    hESDTrueBckGGInvMassPt(NULL),
274    hESDTrueBckContInvMassPt(NULL),
275    hESDTruePi0PtY(NULL),
276    hESDTrueEtaPtY(NULL),
277    hESDTruePi0PtAlpha(NULL),
278    hESDTrueEtaPtAlpha(NULL),
279    hESDTruePi0PtOpenAngle(NULL),
280    hESDTrueEtaPtOpenAngle(NULL),
281    hESDTrueMotherDalitzInvMassPt(NULL),
282    hESDTrueConvGammaPt(NULL),
283    hESDCombinatorialPt(NULL),
284    hESDTruePrimaryConvGammaPt(NULL),
285    hESDTruePrimaryConvGammaESDPtMCPt(NULL),
286    hESDTrueSecondaryConvGammaPt(NULL),
287    hESDTrueSecondaryConvGammaFromXFromK0sPt(NULL),
288    hESDTrueSecondaryConvGammaFromXFromLambdaPt(NULL),
289    hESDTrueDalitzPsiPairDeltaPhi(NULL),
290    hESDTrueGammaPsiPairDeltaPhi(NULL),
291    hNEvents(NULL),
292    hNGoodESDTracks(NULL),
293    hNGammaCandidates(NULL),
294    hNV0Tracks(NULL),
295    hEtaShift(NULL),
296    tESDMesonsInvMassPtDcazMinDcazMaxFlag(NULL),
297    fInvMass(0),
298    fPt(0),
299    fDCAzGammaMin(0),
300    fDCAzGammaMax(0),
301    iFlag(0),
302    iMesonMCInfo(0),
303    fEventPlaneAngle(-100),
304    fRandom(0),
305    fnGammaCandidates(0),
306    fUnsmearedPx(NULL),
307    fUnsmearedPy(NULL),
308    fUnsmearedPz(NULL),
309    fUnsmearedE(NULL),
310    fMCStackPos(NULL),
311    fMCStackNeg(NULL),
312    fESDArrayPos(NULL),
313    fESDArrayNeg(NULL),
314    fnCuts(0),
315    fiCut(0),
316    fMoveParticleAccordingToVertex(kTRUE),
317    fIsHeavyIon(0),
318    fDoMesonAnalysis(kTRUE),
319    fDoMesonQA(0),
320    fDoPhotonQA(0),
321    fIsFromMBHeader(kTRUE),
322    fIsMC(kFALSE)
323 {
324    // Define output slots here
325    DefineOutput(1, TList::Class());
326 }
327
328 AliAnalysisTaskGammaConvV1::~AliAnalysisTaskGammaConvV1()
329 {
330    if(fGammaCandidates){
331       delete fGammaCandidates;
332       fGammaCandidates = 0x0;
333    }
334    if(fBGHandler){
335       delete[] fBGHandler;
336       fBGHandler = 0x0;
337    }
338    if(fBGHandlerRP){
339       delete[] fBGHandlerRP;
340       fBGHandlerRP = 0x0;
341    }
342 }
343 //___________________________________________________________
344 void AliAnalysisTaskGammaConvV1::InitBack(){
345
346    const Int_t nDim = 4;
347    Int_t nBins[nDim] = {800,250,7,4};
348    Double_t xMin[nDim] = {0,0, 0,0};
349    Double_t xMax[nDim] = {0.8,25,7,4};
350    
351    sESDMotherInvMassPtZM = new THnSparseF*[fnCuts];
352    sESDMotherBackInvMassPtZM = new THnSparseF*[fnCuts];
353
354    fBGHandler = new AliGammaConversionAODBGHandler*[fnCuts];
355    fBGHandlerRP = new AliConversionAODBGHandlerRP*[fnCuts];
356    for(Int_t iCut = 0; iCut<fnCuts;iCut++){
357       if (((AliConversionMesonCuts*)fMesonCutArray->At(iCut))->DoBGCalculation()){
358          TString cutstring = ((AliConversionCuts*)fCutArray->At(iCut))->GetCutNumber();
359          TString cutstringMeson = ((AliConversionMesonCuts*)fMesonCutArray->At(iCut))->GetCutNumber();
360          
361          Int_t collisionSystem = atoi((TString)(((AliConversionCuts*)fCutArray->At(iCut))->GetCutNumber())(0,1));
362          Int_t centMin = atoi((TString)(((AliConversionCuts*)fCutArray->At(iCut))->GetCutNumber())(1,1));
363          Int_t centMax = atoi((TString)(((AliConversionCuts*)fCutArray->At(iCut))->GetCutNumber())(2,1));
364          
365          if(collisionSystem == 1 || collisionSystem == 2 ||
366             collisionSystem == 5 || collisionSystem == 8 ||
367             collisionSystem == 9){
368             centMin = centMin*10;
369             centMax = centMax*10; 
370             if(centMax ==0 && centMax!=centMin) centMax=100;
371          }
372          else if(collisionSystem == 3 || collisionSystem == 6){
373             centMin = centMin*5;
374             centMax = centMax*5;
375          }
376          else if(collisionSystem == 4 || collisionSystem == 7){
377             centMin = ((centMin*5)+45);
378             centMax = ((centMax*5)+45);
379          }
380          
381          fBackList[iCut] = new TList();
382          fBackList[iCut]->SetName(Form("%s_%s Back histograms",cutstring.Data(),cutstringMeson.Data()));
383          fBackList[iCut]->SetOwner(kTRUE);
384          fCutFolder[iCut]->Add(fBackList[iCut]);
385
386          sESDMotherBackInvMassPtZM[iCut] = new THnSparseF("Back_Back_InvMass_Pt_z_m","Back_Back_InvMass_Pt_z_m",nDim,nBins,xMin,xMax);
387          fBackList[iCut]->Add(sESDMotherBackInvMassPtZM[iCut]);
388
389          fMotherList[iCut] = new TList();
390          fMotherList[iCut]->SetName(Form("%s_%s Mother histograms",cutstring.Data(),cutstringMeson.Data()));
391          fMotherList[iCut]->SetOwner(kTRUE);
392          fCutFolder[iCut]->Add(fMotherList[iCut]);
393
394          sESDMotherInvMassPtZM[iCut] = new THnSparseF("Back_Mother_InvMass_Pt_z_m","Back_Mother_InvMass_Pt_z_m",nDim,nBins,xMin,xMax);
395          fMotherList[iCut]->Add(sESDMotherInvMassPtZM[iCut]);
396
397          if(((AliConversionMesonCuts*)fMesonCutArray->At(iCut))->BackgroundHandlerType() == 0){
398             fBGHandler[iCut] = new AliGammaConversionAODBGHandler(
399                                                                   collisionSystem,centMin,centMax,
400                                                                   ((AliConversionMesonCuts*)fMesonCutArray->At(iCut))->GetNumberOfBGEvents(),
401                                                                   ((AliConversionMesonCuts*)fMesonCutArray->At(iCut))->UseTrackMultiplicity());
402             fBGHandlerRP[iCut] = NULL;
403          }
404          else{
405             fBGHandlerRP[iCut] = new AliConversionAODBGHandlerRP(
406                                                                  ((AliConversionCuts*)fCutArray->At(fiCut))->IsHeavyIon(),
407                                                                  ((AliConversionMesonCuts*)fMesonCutArray->At(fiCut))->UseTrackMultiplicity(),
408                                                                  ((AliConversionMesonCuts*)fMesonCutArray->At(iCut))->GetNumberOfBGEvents());
409             fBGHandler[iCut] = NULL;
410          }
411       }
412    }
413 }
414 //________________________________________________________________________
415 void AliAnalysisTaskGammaConvV1::UserCreateOutputObjects()
416 {
417
418    // Create histograms
419    if(fOutputContainer != NULL){
420       delete fOutputContainer;
421       fOutputContainer = NULL;
422    }
423    if(fOutputContainer == NULL){
424       fOutputContainer = new TList();
425       fOutputContainer->SetOwner(kTRUE);
426    }
427
428    // Array of current cut's gammas
429    fGammaCandidates = new TList();
430
431    fCutFolder = new TList*[fnCuts];
432    fESDList = new TList*[fnCuts];
433    fBackList = new TList*[fnCuts];
434    fMotherList = new TList*[fnCuts];
435    hNEvents = new TH1I*[fnCuts];
436    hNGoodESDTracks = new TH1I*[fnCuts];
437    hNGammaCandidates = new TH1I*[fnCuts];
438    hNV0Tracks = new TH1I*[fnCuts];
439    hEtaShift = new TProfile*[fnCuts];
440    hESDConvGammaPt = new TH1F*[fnCuts];
441
442    if (fDoPhotonQA == 2){
443       fPhotonDCAList = new TList*[fnCuts];
444       tESDConvGammaPtDcazCat = new TTree*[fnCuts];
445    }
446    if (fDoPhotonQA > 0){
447       hESDConvGammaR = new TH1F*[fnCuts];
448       hESDConvGammaEta = new TH1F*[fnCuts];
449    } 
450    
451    if(fDoMesonAnalysis){
452       hESDMotherInvMassPt = new TH2F*[fnCuts];
453       hESDMotherBackInvMassPt = new TH2F*[fnCuts];
454       hESDMotherInvMassEalpha = new TH2F*[fnCuts];
455       if (fDoMesonQA == 2){
456          fMesonDCAList = new TList*[fnCuts];
457          tESDMesonsInvMassPtDcazMinDcazMaxFlag = new TTree*[fnCuts];
458       }
459       if (fDoMesonQA > 0){
460          hESDMotherPi0PtY =  new TH2F*[fnCuts];
461          hESDMotherEtaPtY =  new TH2F*[fnCuts];
462          hESDMotherPi0PtAlpha =  new TH2F*[fnCuts];
463          hESDMotherEtaPtAlpha =  new TH2F*[fnCuts];
464          hESDMotherPi0PtOpenAngle =  new TH2F*[fnCuts];
465          hESDMotherEtaPtOpenAngle =  new TH2F*[fnCuts];
466       }   
467    }
468
469    for(Int_t iCut = 0; iCut<fnCuts;iCut++){
470
471       TString cutstring = ((AliConversionCuts*)fCutArray->At(iCut))->GetCutNumber();
472       TString cutstringMeson = "NoMesonCut";
473       if(fDoMesonAnalysis)cutstringMeson = ((AliConversionMesonCuts*)fMesonCutArray->At(iCut))->GetCutNumber();
474
475       fCutFolder[iCut] = new TList();
476       fCutFolder[iCut]->SetName(Form("Cut Number %s_%s",cutstring.Data(),cutstringMeson.Data()));
477       fCutFolder[iCut]->SetOwner(kTRUE);
478       fOutputContainer->Add(fCutFolder[iCut]);
479       fESDList[iCut] = new TList();
480       fESDList[iCut]->SetName(Form("%s_%s ESD histograms",cutstring.Data(),cutstringMeson.Data()));
481       fESDList[iCut]->SetOwner(kTRUE);
482       fCutFolder[iCut]->Add(fESDList[iCut]);
483
484       hNEvents[iCut] = new TH1I("NEvents","NEvents",9,-0.5,8.5);
485       hNEvents[iCut]->GetXaxis()->SetBinLabel(1,"Accepted");
486       hNEvents[iCut]->GetXaxis()->SetBinLabel(2,"Centrality");
487       hNEvents[iCut]->GetXaxis()->SetBinLabel(3,"Missing MC");
488       if (((AliConversionCuts*)fCutArray->At(iCut))->IsSpecialTrigger() == 4 ){
489          TString TriggerNames = "Not Trigger: ";
490          TriggerNames = TriggerNames+ ( (AliConversionCuts*)fCutArray->At(iCut))->GetSpecialTriggerName();
491          hNEvents[iCut]->GetXaxis()->SetBinLabel(4,TriggerNames.Data());
492       } else {
493          hNEvents[iCut]->GetXaxis()->SetBinLabel(4,"Trigger");
494       }
495       hNEvents[iCut]->GetXaxis()->SetBinLabel(5,"Vertex Z");
496       hNEvents[iCut]->GetXaxis()->SetBinLabel(6,"Cont. Vertex");
497       hNEvents[iCut]->GetXaxis()->SetBinLabel(7,"Pile-Up");
498       hNEvents[iCut]->GetXaxis()->SetBinLabel(8,"no SDD");
499       hNEvents[iCut]->GetXaxis()->SetBinLabel(9,"no V0AND");
500       fESDList[iCut]->Add(hNEvents[iCut]);
501       
502       if(fIsHeavyIon == 1) hNGoodESDTracks[iCut] = new TH1I("GoodESDTracks","GoodESDTracks",4000,0,4000);
503       else if(fIsHeavyIon == 2) hNGoodESDTracks[iCut] = new TH1I("GoodESDTracks","GoodESDTracks",400,0,400);
504       else hNGoodESDTracks[iCut] = new TH1I("GoodESDTracks","GoodESDTracks",200,0,200);
505       fESDList[iCut]->Add(hNGoodESDTracks[iCut]);
506       if(fIsHeavyIon == 1) hNGammaCandidates[iCut] = new TH1I("GammaCandidates","GammaCandidates",100,0,100);
507       else if(fIsHeavyIon == 2) hNGammaCandidates[iCut] = new TH1I("GammaCandidates","GammaCandidates",50,0,50);
508       else hNGammaCandidates[iCut] = new TH1I("GammaCandidates","GammaCandidates",50,0,50);
509       fESDList[iCut]->Add(hNGammaCandidates[iCut]);
510       if(fIsHeavyIon == 1) hNV0Tracks[iCut] = new TH1I("V0 Multiplicity","V0 Multiplicity",30000,0,30000);
511       else if(fIsHeavyIon == 2) hNV0Tracks[iCut] = new TH1I("V0 Multiplicity","V0 Multiplicity",2500,0,2500);
512       else hNV0Tracks[iCut] = new TH1I("V0 Multiplicity","V0 Multiplicity",1500,0,1500);
513       fESDList[iCut]->Add(hNV0Tracks[iCut]);
514       hEtaShift[iCut] = new TProfile("Eta Shift","Eta Shift",1, -0.5,0.5);
515       fESDList[iCut]->Add(hEtaShift[iCut]);
516       hESDConvGammaPt[iCut] = new TH1F("ESD_ConvGamma_Pt","ESD_ConvGamma_Pt",250,0,25);
517       fESDList[iCut]->Add(hESDConvGammaPt[iCut]);
518
519       if (fDoPhotonQA == 2){
520          fPhotonDCAList[iCut] = new TList();
521          fPhotonDCAList[iCut]->SetName(Form("%s_%s Photon DCA tree",cutstring.Data(),cutstringMeson.Data()));
522          fPhotonDCAList[iCut]->SetOwner(kTRUE);
523          fCutFolder[iCut]->Add(fPhotonDCAList[iCut]);
524             
525         tESDConvGammaPtDcazCat[iCut] = new TTree("ESD_ConvGamma_Pt_Dcaz_R_Eta","ESD_ConvGamma_Pt_Dcaz_R_Eta_Cat");   
526          tESDConvGammaPtDcazCat[iCut]->Branch("Pt",&fPtGamma,"fPtGamma/F");
527          tESDConvGammaPtDcazCat[iCut]->Branch("DcaZPhoton",&fDCAzPhoton,"fDCAzPhoton/F");
528 //          tESDConvGammaPtDcazCat[iCut]->Branch("R",&fRConvPhoton,"fRConvPhoton/F");
529 //          tESDConvGammaPtDcazCat[iCut]->Branch("Eta",&fEtaPhoton,"fEtaPhoton/F");
530          
531          tESDConvGammaPtDcazCat[iCut]->Branch("cat",&iCatPhoton,"iCatPhoton/b");
532          if(fIsMC){
533             tESDConvGammaPtDcazCat[iCut]->Branch("photonMCInfo",&iPhotonMCInfo,"iPhotonMCInfo/b");
534          }
535          fPhotonDCAList[iCut]->Add(tESDConvGammaPtDcazCat[iCut]);
536       }
537
538       if (fDoPhotonQA > 0){
539          hESDConvGammaR[iCut] = new TH1F("ESD_ConvGamma_R","ESD_ConvGamma_R",800,0,200);
540          fESDList[iCut]->Add(hESDConvGammaR[iCut]);
541          hESDConvGammaEta[iCut] = new TH1F("ESD_ConvGamma_Eta","ESD_ConvGamma_Eta",2000,-2,2);
542          fESDList[iCut]->Add(hESDConvGammaEta[iCut]);
543       }
544       
545       if(fDoMesonAnalysis){
546          hESDMotherInvMassPt[iCut] = new TH2F("ESD_Mother_InvMass_Pt","ESD_Mother_InvMass_Pt",800,0,0.8,250,0,25);
547          fESDList[iCut]->Add(hESDMotherInvMassPt[iCut]);
548          hESDMotherBackInvMassPt[iCut] = new TH2F("ESD_Background_InvMass_Pt","ESD_Background_InvMass_Pt",800,0,0.8,250,0,25);
549          fESDList[iCut]->Add(hESDMotherBackInvMassPt[iCut]);
550          hESDMotherInvMassEalpha[iCut] = new TH2F("ESD_Mother_InvMass_vs_E_alpha","ESD_Mother_InvMass_vs_E_alpha",800,0,0.8,250,0,25);
551          fESDList[iCut]->Add(hESDMotherInvMassEalpha[iCut]);
552          if (fDoMesonQA == 2){    
553             fMesonDCAList[iCut] = new TList();
554             fMesonDCAList[iCut]->SetName(Form("%s_%s Meson DCA tree",cutstring.Data(),cutstringMeson.Data()));
555             fMesonDCAList[iCut]->SetOwner(kTRUE);
556             fCutFolder[iCut]->Add(fMesonDCAList[iCut]);
557             
558             tESDMesonsInvMassPtDcazMinDcazMaxFlag[iCut] = new TTree("ESD_Mesons_InvMass_Pt_DcazMin_DcazMax_Flag","ESD_Mesons_InvMass_Pt_DcazMin_DcazMax_Flag");   
559             tESDMesonsInvMassPtDcazMinDcazMaxFlag[iCut]->Branch("InvMass",&fInvMass,"fInvMass/F");
560             tESDMesonsInvMassPtDcazMinDcazMaxFlag[iCut]->Branch("Pt",&fPt,"fPt/F");
561             tESDMesonsInvMassPtDcazMinDcazMaxFlag[iCut]->Branch("DcaZMin",&fDCAzGammaMin,"fDCAzGammaMin/F");
562             tESDMesonsInvMassPtDcazMinDcazMaxFlag[iCut]->Branch("DcaZMax",&fDCAzGammaMax,"fDCAzGammaMax/F");
563             tESDMesonsInvMassPtDcazMinDcazMaxFlag[iCut]->Branch("kind",&iFlag,"iFlag/b");
564             if(fIsMC){
565                tESDMesonsInvMassPtDcazMinDcazMaxFlag[iCut]->Branch("mesonMCInfo",&iMesonMCInfo,"iMesonMCInfo/b");
566             }
567             fMesonDCAList[iCut]->Add(tESDMesonsInvMassPtDcazMinDcazMaxFlag[iCut]);
568               
569          }
570          if (fDoMesonQA > 0 ){
571             hESDMotherPi0PtY[iCut] = new TH2F("ESD_MotherPi0_Pt_Y","ESD_MotherPi0_Pt_Y",150,0.03,15.,150,-1.5,1.5);            
572             SetLogBinningXTH2(hESDMotherPi0PtY[iCut]);
573             fESDList[iCut]->Add(hESDMotherPi0PtY[iCut]);
574             hESDMotherEtaPtY[iCut] = new TH2F("ESD_MotherEta_Pt_Y","ESD_MotherEta_Pt_Y",150,0.03,15.,150,-1.5,1.5);
575             SetLogBinningXTH2(hESDMotherEtaPtY[iCut]);
576             fESDList[iCut]->Add(hESDMotherEtaPtY[iCut]);
577             hESDMotherPi0PtAlpha[iCut] = new TH2F("ESD_MotherPi0_Pt_Alpha","ESD_MotherPi0_Pt_Alpha",150,0.03,15.,100,0,1);            
578             SetLogBinningXTH2(hESDMotherPi0PtAlpha[iCut]);
579             fESDList[iCut]->Add(hESDMotherPi0PtAlpha[iCut]);
580             hESDMotherEtaPtAlpha[iCut] = new TH2F("ESD_MotherEta_Pt_Alpha","ESD_MotherEta_Pt_Alpha",150,0.03,15.,100,0,1);
581             SetLogBinningXTH2(hESDMotherEtaPtAlpha[iCut]);
582             fESDList[iCut]->Add(hESDMotherEtaPtAlpha[iCut]);
583             hESDMotherPi0PtOpenAngle[iCut] = new TH2F("ESD_MotherPi0_Pt_OpenAngle","ESD_MotherPi0_Pt_OpenAngle",150,0.03,15.,200,0,2*TMath::Pi());            
584             SetLogBinningXTH2(hESDMotherPi0PtOpenAngle[iCut]);
585             fESDList[iCut]->Add(hESDMotherPi0PtOpenAngle[iCut]);
586             hESDMotherEtaPtOpenAngle[iCut] = new TH2F("ESD_MotherEta_Pt_OpenAngle","ESD_MotherEta_Pt_OpenAngle",150,0.03,15.,200,0,2*TMath::Pi());            
587             SetLogBinningXTH2(hESDMotherEtaPtOpenAngle[iCut]);
588             fESDList[iCut]->Add(hESDMotherEtaPtOpenAngle[iCut]);
589          }
590          
591             
592       }
593
594
595    }
596    if(fDoMesonAnalysis){
597       InitBack(); // Init Background Handler
598    }
599
600    if(fIsMC){
601       // MC Histogramms
602       fMCList = new TList*[fnCuts];
603       // True Histogramms
604       fTrueList = new TList*[fnCuts];
605       // Selected Header List
606       fHeaderNameList = new TList*[fnCuts];
607       hMCHeaders = new TH1I*[fnCuts];
608       hMCAllGammaPt = new TH1F*[fnCuts];
609       hMCDecayGammaPi0Pt = new TH1F*[fnCuts];
610       hMCDecayGammaRhoPt = new TH1F*[fnCuts];
611       hMCDecayGammaEtaPt = new TH1F*[fnCuts];
612       hMCDecayGammaOmegaPt = new TH1F*[fnCuts];
613       hMCDecayGammaEtapPt = new TH1F*[fnCuts];
614       hMCDecayGammaPhiPt = new TH1F*[fnCuts];
615       hMCDecayGammaSigmaPt = new TH1F*[fnCuts];
616       hMCConvGammaPt = new TH1F*[fnCuts];
617       hESDTrueConvGammaPt = new TH1F*[fnCuts];
618
619       hESDCombinatorialPt = new TH2F*[fnCuts];
620       hESDTruePrimaryConvGammaPt = new TH1F*[fnCuts];
621       hESDTruePrimaryConvGammaESDPtMCPt = new TH2F*[fnCuts];
622       hESDTrueSecondaryConvGammaPt = new TH1F*[fnCuts];
623       hESDTrueSecondaryConvGammaFromXFromK0sPt = new TH1F*[fnCuts];
624       hESDTrueSecondaryConvGammaFromXFromLambdaPt = new TH1F*[fnCuts];
625
626       hESDTrueDalitzPsiPairDeltaPhi= new TH2F*[fnCuts];
627       hESDTrueGammaPsiPairDeltaPhi= new TH2F*[fnCuts];
628
629       if (fDoPhotonQA > 0){
630          hMCConvGammaR = new TH1F*[fnCuts];
631          hMCConvGammaEta = new TH1F*[fnCuts];
632       }
633
634       if(fDoMesonAnalysis){
635          hMCPi0Pt = new TH1F*[fnCuts];
636          hMCPi0WOWeightPt = new TH1F*[fnCuts];
637          hMCEtaPt = new TH1F*[fnCuts];
638          hMCEtaWOWeightPt = new TH1F*[fnCuts];
639          hMCPi0InAccPt = new TH1F*[fnCuts];
640          hMCEtaInAccPt = new TH1F*[fnCuts];
641
642          hESDTrueMotherInvMassPt = new TH2F*[fnCuts];
643          hESDTruePrimaryMotherInvMassPt = new TH2F*[fnCuts];
644          hESDTruePrimaryMotherW0WeightingInvMassPt = new TH2F*[fnCuts];
645          pESDTruePrimaryMotherWeightsInvMassPt = new TProfile2D*[fnCuts];
646          hESDTrueSecondaryMotherInvMassPt = new TH2F*[fnCuts];
647          hESDTrueSecondaryMotherFromK0sInvMassPt = new TH2F*[fnCuts];
648          hESDTrueSecondaryMotherFromEtaInvMassPt = new TH2F*[fnCuts];
649          hESDTrueMotherDalitzInvMassPt = new TH2F*[fnCuts];
650          if (fDoMesonQA > 0){
651             hMCPi0PtY = new TH2F*[fnCuts];
652             hMCEtaPtY = new TH2F*[fnCuts];
653             hMCK0sPt = new TH1F*[fnCuts];
654             hMCK0sWOWeightPt = new TH1F*[fnCuts];
655             hMCK0sPtY = new TH2F*[fnCuts];
656             hESDTruePrimaryPi0MCPtResolPt = new TH2F*[fnCuts];
657             hESDTruePrimaryEtaMCPtResolPt = new TH2F*[fnCuts];
658             hESDTrueK0sWithPi0DaughterMCPt = new TH1F*[fnCuts];
659             hESDTrueEtaWithPi0DaughterMCPt = new TH1F*[fnCuts];
660             hESDTrueBckGGInvMassPt = new TH2F*[fnCuts];
661             hESDTrueBckContInvMassPt = new TH2F*[fnCuts];
662             hESDTruePi0PtY = new TH2F*[fnCuts];
663             hESDTrueEtaPtY = new TH2F*[fnCuts];
664             hESDTruePi0PtAlpha = new TH2F*[fnCuts];
665             hESDTrueEtaPtAlpha = new TH2F*[fnCuts];
666             hESDTruePi0PtOpenAngle = new TH2F*[fnCuts];
667             hESDTrueEtaPtOpenAngle = new TH2F*[fnCuts];
668          }
669       }
670
671       for(Int_t iCut = 0; iCut<fnCuts;iCut++){
672          TString cutstring = ((AliConversionCuts*)fCutArray->At(iCut))->GetCutNumber();
673          TString cutstringMeson = "NoMesonCut";
674          if(fDoMesonAnalysis)cutstringMeson = ((AliConversionMesonCuts*)fMesonCutArray->At(iCut))->GetCutNumber();
675
676          fMCList[iCut] = new TList();
677          fMCList[iCut]->SetName(Form("%s_%s MC histograms",cutstring.Data(),cutstringMeson.Data()));
678          fMCList[iCut]->SetOwner(kTRUE);
679          fCutFolder[iCut]->Add(fMCList[iCut]);
680          hMCHeaders[iCut] = new TH1I("MC_Headers","MC_Headers",20,0,20);
681          fMCList[iCut]->Add(hMCHeaders[iCut]);
682          hMCAllGammaPt[iCut] = new TH1F("MC_AllGamma_Pt","MC_AllGamma_Pt",250,0,25);
683          fMCList[iCut]->Add(hMCAllGammaPt[iCut]);
684          hMCDecayGammaPi0Pt[iCut] = new TH1F("MC_DecayGammaPi0_Pt","MC_DecayGammaPi0_Pt",250,0,25);
685          fMCList[iCut]->Add(hMCDecayGammaPi0Pt[iCut]);
686          hMCDecayGammaRhoPt[iCut] = new TH1F("MC_DecayGammaRho_Pt","MC_DecayGammaRho_Pt",250,0,25);
687          fMCList[iCut]->Add(hMCDecayGammaRhoPt[iCut]);
688          hMCDecayGammaEtaPt[iCut] = new TH1F("MC_DecayGammaEta_Pt","MC_DecayGammaEta_Pt",250,0,25);
689          fMCList[iCut]->Add(hMCDecayGammaEtaPt[iCut]);
690          hMCDecayGammaOmegaPt[iCut] = new TH1F("MC_DecayGammaOmega_Pt","MC_DecayGammaOmmega_Pt",250,0,25);
691          fMCList[iCut]->Add(hMCDecayGammaOmegaPt[iCut]);
692          hMCDecayGammaEtapPt[iCut] = new TH1F("MC_DecayGammaEtap_Pt","MC_DecayGammaEtap_Pt",250,0,25);
693          fMCList[iCut]->Add(hMCDecayGammaEtapPt[iCut]);
694          hMCDecayGammaPhiPt[iCut] = new TH1F("MC_DecayGammaPhi_Pt","MC_DecayGammaPhi_Pt",250,0,25);
695          fMCList[iCut]->Add(hMCDecayGammaPhiPt[iCut]);
696          hMCDecayGammaSigmaPt[iCut] = new TH1F("MC_DecayGammaSigma_Pt","MC_DecayGammaSigma_Pt",250,0,25);
697          fMCList[iCut]->Add(hMCDecayGammaSigmaPt[iCut]);
698          hMCConvGammaPt[iCut] = new TH1F("MC_ConvGamma_Pt","MC_ConvGamma_Pt",250,0,25);
699          fMCList[iCut]->Add(hMCConvGammaPt[iCut]);
700       
701          if (fDoPhotonQA > 0){
702             hMCConvGammaR[iCut] = new TH1F("MC_ConvGamma_R","MC_ConvGamma_R",800,0,200);
703             fMCList[iCut]->Add(hMCConvGammaR[iCut]);
704             hMCConvGammaEta[iCut] = new TH1F("MC_ConvGamma_Eta","MC_ConvGamma_Eta",100,-4,4);
705             fMCList[iCut]->Add(hMCConvGammaEta[iCut]);
706          }
707
708          if(fDoMesonAnalysis){
709             hMCPi0Pt[iCut] = new TH1F("MC_Pi0_Pt","MC_Pi0_Pt",250,0,25);
710             hMCPi0Pt[iCut]->Sumw2();
711             fMCList[iCut]->Add(hMCPi0Pt[iCut]);
712             hMCPi0WOWeightPt[iCut] = new TH1F("MC_Pi0_WOWeights_Pt","MC_Pi0_WOWeights_Pt",250,0,25);
713             hMCPi0WOWeightPt[iCut]->Sumw2();
714             fMCList[iCut]->Add(hMCPi0WOWeightPt[iCut]);
715             
716             hMCEtaPt[iCut] = new TH1F("MC_Eta_Pt","MC_Eta_Pt",250,0,25);
717             hMCEtaPt[iCut]->Sumw2();
718             fMCList[iCut]->Add(hMCEtaPt[iCut]);
719             hMCEtaWOWeightPt[iCut] = new TH1F("MC_Eta_WOWeights_Pt","MC_Eta_WOWeights_Pt",250,0,25);
720             hMCEtaWOWeightPt[iCut]->Sumw2();
721             fMCList[iCut]->Add(hMCEtaWOWeightPt[iCut]);
722             hMCPi0InAccPt[iCut] = new TH1F("MC_Pi0InAcc_Pt","MC_Pi0InAcc_Pt",250,0,25);
723             hMCPi0InAccPt[iCut]->Sumw2();
724             fMCList[iCut]->Add(hMCPi0InAccPt[iCut]);
725             hMCEtaInAccPt[iCut] = new TH1F("MC_EtaInAcc_Pt","MC_EtaInAcc_Pt",250,0,25);
726             hMCEtaInAccPt[iCut]->Sumw2();
727             fMCList[iCut]->Add(hMCEtaInAccPt[iCut]);
728             if (fDoMesonQA > 0){
729                hMCPi0PtY[iCut] = new TH2F("MC_Pi0_Pt_Y","MC_Pi0_Pt_Y",150,0.03,15.,150,-1.5,1.5);
730                hMCPi0PtY[iCut]->Sumw2();
731                SetLogBinningXTH2(hMCPi0PtY[iCut]);
732                fMCList[iCut]->Add(hMCPi0PtY[iCut]);
733                hMCEtaPtY[iCut] = new TH2F("MC_Eta_Pt_Y","MC_Eta_Pt_Y",150,0.03,15.,150,-1.5,1.5);
734                hMCEtaPtY[iCut]->Sumw2();
735                SetLogBinningXTH2(hMCEtaPtY[iCut]);
736                fMCList[iCut]->Add(hMCEtaPtY[iCut]);
737                hMCK0sPt[iCut] = new TH1F("MC_K0s_Pt","MC_K0s_Pt",150,0,15);
738                hMCK0sPt[iCut]->Sumw2();
739                fMCList[iCut]->Add(hMCK0sPt[iCut]);
740                hMCK0sWOWeightPt[iCut] = new TH1F("MC_K0s_WOWeights_Pt","MC_K0s_WOWeights_Pt",150,0,15);
741                hMCK0sWOWeightPt[iCut]->Sumw2();
742                fMCList[iCut]->Add(hMCK0sWOWeightPt[iCut]);
743                hMCK0sPtY[iCut] = new TH2F("MC_K0s_Pt_Y","MC_K0s_Pt_Y",150,0.03,15.,150,-1.5,1.5);
744                hMCK0sPtY[iCut]->Sumw2();
745                SetLogBinningXTH2(hMCK0sPtY[iCut]);
746                fMCList[iCut]->Add(hMCK0sPtY[iCut]);
747                
748             }
749
750          }
751          fTrueList[iCut] = new TList();
752          fTrueList[iCut]->SetName(Form("%s_%s True histograms",cutstring.Data(),cutstringMeson.Data()));
753          fTrueList[iCut]->SetOwner(kTRUE);
754          fCutFolder[iCut]->Add(fTrueList[iCut]);
755
756          hESDTrueConvGammaPt[iCut] = new TH1F("ESD_TrueConvGamma_Pt","ESD_TrueConvGamma_Pt",250,0,25);
757          fTrueList[iCut]->Add(hESDTrueConvGammaPt[iCut]);
758
759          hESDCombinatorialPt[iCut] = new TH2F("ESD_TrueCombinatorial_Pt","ESD_TrueCombinatorial_Pt",250,0,25,16,-0.5,15.5);
760          hESDCombinatorialPt[iCut]->GetYaxis()->SetBinLabel( 1,"Elec+Elec");
761          hESDCombinatorialPt[iCut]->GetYaxis()->SetBinLabel( 2,"Elec+Pion");
762          hESDCombinatorialPt[iCut]->GetYaxis()->SetBinLabel( 3,"Elec+Kaon");
763          hESDCombinatorialPt[iCut]->GetYaxis()->SetBinLabel( 4,"Elec+Proton");
764          hESDCombinatorialPt[iCut]->GetYaxis()->SetBinLabel( 5,"Elec+Muon");
765          hESDCombinatorialPt[iCut]->GetYaxis()->SetBinLabel( 6,"Pion+Pion");
766          hESDCombinatorialPt[iCut]->GetYaxis()->SetBinLabel( 7,"Pion+Kaon");
767          hESDCombinatorialPt[iCut]->GetYaxis()->SetBinLabel( 8,"Pion+Proton");
768          hESDCombinatorialPt[iCut]->GetYaxis()->SetBinLabel( 9,"Pion+Muon");
769          hESDCombinatorialPt[iCut]->GetYaxis()->SetBinLabel(10,"Kaon+Kaon");
770          hESDCombinatorialPt[iCut]->GetYaxis()->SetBinLabel(11,"Kaon+Proton");
771          hESDCombinatorialPt[iCut]->GetYaxis()->SetBinLabel(12,"Kaon+Muon");
772          hESDCombinatorialPt[iCut]->GetYaxis()->SetBinLabel(13,"Proton+Proton");
773          hESDCombinatorialPt[iCut]->GetYaxis()->SetBinLabel(14,"Proton+Muon");
774          hESDCombinatorialPt[iCut]->GetYaxis()->SetBinLabel(15,"Muon+Muon");
775          hESDCombinatorialPt[iCut]->GetYaxis()->SetBinLabel(16,"Rest");
776          fTrueList[iCut]->Add(hESDCombinatorialPt[iCut]);
777          hESDTruePrimaryConvGammaPt[iCut] = new TH1F("ESD_TruePrimaryConvGamma_Pt","ESD_TruePrimaryConvGamma_Pt",250,0,25);
778          fTrueList[iCut]->Add(hESDTruePrimaryConvGammaPt[iCut]);
779          hESDTrueSecondaryConvGammaPt[iCut] = new TH1F("ESD_TrueSecondaryConvGamma_Pt","ESD_TrueSecondaryConvGamma_Pt",250,0,25);
780          fTrueList[iCut]->Add(hESDTrueSecondaryConvGammaPt[iCut]);
781
782          hESDTrueSecondaryConvGammaFromXFromK0sPt[iCut]
783             = new TH1F("ESD_TrueSecondaryConvGammaFromXFromK0s_Pt", "ESD_TrueSecondaryConvGammaFromXFromK0s_Pt",250,0,25);
784          fTrueList[iCut]->Add(hESDTrueSecondaryConvGammaFromXFromK0sPt[iCut]);
785          hESDTrueSecondaryConvGammaFromXFromLambdaPt[iCut]
786             = new TH1F("ESD_TrueSecondaryConvGammaFromXFromLambda_Pt", "ESD_TrueSecondaryConvGammaFromXFromLambda_Pt",250,0,25);
787          fTrueList[iCut]->Add(hESDTrueSecondaryConvGammaFromXFromLambdaPt[iCut]);
788
789          hESDTrueDalitzPsiPairDeltaPhi[iCut]
790             = new TH2F("ESD_TrueDalitzPsiPairDeltaPhi_Pt", "ESD_TrueDalitzPsiPairDeltaPhi_Pt",100,-0.5,2,100,-0.5,0.5);
791          fTrueList[iCut]->Add(hESDTrueDalitzPsiPairDeltaPhi[iCut]);
792          
793          hESDTrueGammaPsiPairDeltaPhi[iCut]
794             = new TH2F("ESD_TrueGammaPsiPairDeltaPhi_Pt", "ESD_TrueGammaPsiPairDeltaPhi_Pt",100,-0.5,2,100,-0.5,0.5);
795          fTrueList[iCut]->Add(hESDTrueGammaPsiPairDeltaPhi[iCut]);
796
797          
798          hESDTruePrimaryConvGammaESDPtMCPt[iCut] = new TH2F("ESD_TruePrimaryConvGammaESD_PtMCPt", "ESD_TruePrimaryConvGammaESD_PtMCPt",250,0,25,250,0,25);
799          fTrueList[iCut]->Add(hESDTruePrimaryConvGammaESDPtMCPt[iCut]);
800          
801          if(fDoMesonAnalysis){
802             hESDTrueMotherInvMassPt[iCut] = new TH2F("ESD_TrueMother_InvMass_Pt","ESD_TrueMother_InvMass_Pt",800,0,0.8,250,0,25);
803             fTrueList[iCut]->Add(hESDTrueMotherInvMassPt[iCut]);
804             hESDTruePrimaryMotherInvMassPt[iCut]
805                = new TH2F("ESD_TruePrimaryMother_InvMass_Pt", "ESD_TruePrimaryMother_InvMass_Pt", 800,0,0.8,250,0,25);
806             hESDTruePrimaryMotherInvMassPt[iCut]->Sumw2();
807             fTrueList[iCut]->Add(hESDTruePrimaryMotherInvMassPt[iCut]);
808             hESDTruePrimaryMotherW0WeightingInvMassPt[iCut]
809                = new TH2F("ESD_TruePrimaryMotherW0Weights_InvMass_Pt", "ESD_TruePrimaryMotherW0Weights_InvMass_Pt", 800,0,0.8,250,0,25);
810             hESDTruePrimaryMotherW0WeightingInvMassPt[iCut]->Sumw2();
811             fTrueList[iCut]->Add(hESDTruePrimaryMotherW0WeightingInvMassPt[iCut]);
812             pESDTruePrimaryMotherWeightsInvMassPt[iCut]
813                = new TProfile2D("ESD_TruePrimaryMotherWeights_InvMass_Pt", "ESD_TruePrimaryMotherWeights_InvMass_Pt", 800,0,0.8,250,0,25);
814             pESDTruePrimaryMotherWeightsInvMassPt[iCut]->Sumw2();
815             fTrueList[iCut]->Add(pESDTruePrimaryMotherWeightsInvMassPt[iCut]);
816             hESDTrueSecondaryMotherInvMassPt[iCut]
817                = new TH2F("ESD_TrueSecondaryMother_InvMass_Pt", "ESD_TrueSecondaryMother_InvMass_Pt", 800,0,0.8,250,0,25);
818             hESDTrueSecondaryMotherInvMassPt[iCut]->Sumw2();
819             fTrueList[iCut]->Add(hESDTrueSecondaryMotherInvMassPt[iCut]);
820             hESDTrueSecondaryMotherFromK0sInvMassPt[iCut]
821                = new TH2F("ESD_TrueSecondaryMotherFromK0s_InvMass_Pt","ESD_TrueSecondaryMotherFromK0s_InvMass_Pt",800,0,0.8,250,0,25);
822             hESDTrueSecondaryMotherFromK0sInvMassPt[iCut]->Sumw2();
823             fTrueList[iCut]->Add(hESDTrueSecondaryMotherFromK0sInvMassPt[iCut]);
824             hESDTrueSecondaryMotherFromEtaInvMassPt[iCut]
825                = new TH2F("ESD_TrueSecondaryMotherFromEta_InvMass_Pt","ESD_TrueSecondaryMotherFromEta_InvMass_Pt",800,0,0.8,250,0,25);
826             fTrueList[iCut]->Add(hESDTrueSecondaryMotherFromEtaInvMassPt[iCut]);
827             hESDTrueMotherDalitzInvMassPt[iCut] = new TH2F("ESD_TrueDalitz_InvMass_Pt","ESD_TrueDalitz_InvMass_Pt",800,0,0.8,250,0,25);
828             fTrueList[iCut]->Add(hESDTrueMotherDalitzInvMassPt[iCut]);         
829             if (fDoMesonQA > 0){
830                hESDTruePrimaryPi0MCPtResolPt[iCut] = new TH2F("ESD_TruePrimaryPi0_MCPt_ResolPt","ESD_TruePrimaryPi0_ResolPt_MCPt",500,0.03,25,1000,-1.,1.);
831                hESDTruePrimaryPi0MCPtResolPt[iCut]->Sumw2();
832                SetLogBinningXTH2(hESDTruePrimaryPi0MCPtResolPt[iCut]);
833                fTrueList[iCut]->Add(hESDTruePrimaryPi0MCPtResolPt[iCut]);
834                hESDTruePrimaryEtaMCPtResolPt[iCut]  = new TH2F("ESD_TruePrimaryEta_MCPt_ResolPt","ESD_TruePrimaryEta_ResolPt_MCPt",500,0.03,25,1000,-1.,1.);
835                hESDTruePrimaryEtaMCPtResolPt[iCut]->Sumw2();
836                SetLogBinningXTH2(hESDTruePrimaryEtaMCPtResolPt[iCut]);
837                fTrueList[iCut]->Add(hESDTruePrimaryEtaMCPtResolPt[iCut]);
838                hESDTrueBckGGInvMassPt[iCut] = new TH2F("ESD_TrueBckGG_InvMass_Pt","ESD_TrueBckGG_InvMass_Pt",800,0,0.8,250,0,25);
839                fTrueList[iCut]->Add(hESDTrueBckGGInvMassPt[iCut]);
840                hESDTrueBckContInvMassPt[iCut] = new TH2F("ESD_TrueBckCont_InvMass_Pt","ESD_TrueBckCont_InvMass_Pt",800,0,0.8,250,0,25);
841                fTrueList[iCut]->Add(hESDTrueBckContInvMassPt[iCut]);
842                hESDTrueK0sWithPi0DaughterMCPt[iCut] = new TH1F("ESD_TrueK0sWithPi0Daughter_MCPt","ESD_TrueK0sWithPi0Daughter_MCPt",250,0,25);
843                fTrueList[iCut]->Add(hESDTrueK0sWithPi0DaughterMCPt[iCut]);
844                hESDTrueEtaWithPi0DaughterMCPt[iCut] = new TH1F("ESD_TrueEtaWithPi0Daughter_MCPt","ESD_TrueEtaWithPi0Daughter_MCPt",250,0,25);
845                fTrueList[iCut]->Add(hESDTrueEtaWithPi0DaughterMCPt[iCut]);
846                hESDTruePi0PtY[iCut] = new TH2F("ESD_TruePi0_Pt_Y","ESD_TruePi0_Pt_Y",150,0.03,15.,150,-1.5,1.5);
847                SetLogBinningXTH2(hESDTruePi0PtY[iCut]);
848                fTrueList[iCut]->Add(hESDTruePi0PtY[iCut]);
849                hESDTrueEtaPtY[iCut] = new TH2F("ESD_TrueEta_Pt_Y","ESD_TrueEta_Pt_Y",150,0.03,15.,150,-1.5,1.5);
850                SetLogBinningXTH2(hESDTrueEtaPtY[iCut]);
851                fTrueList[iCut]->Add(hESDTrueEtaPtY[iCut]);
852                hESDTruePi0PtAlpha[iCut] = new TH2F("ESD_TruePi0_Pt_Alpha","ESD_TruePi0_Pt_Alpha",150,0.03,15.,100,0,1);
853                SetLogBinningXTH2(hESDTruePi0PtAlpha[iCut]);
854                fTrueList[iCut]->Add(hESDTruePi0PtAlpha[iCut]);
855                hESDTrueEtaPtAlpha[iCut] = new TH2F("ESD_TrueEta_Pt_Alpha","ESD_TrueEta_Pt_Alpha",150,0.03,15.,100,0,1);
856                SetLogBinningXTH2(hESDTrueEtaPtAlpha[iCut]);
857                fTrueList[iCut]->Add(hESDTrueEtaPtAlpha[iCut]);
858                
859                hESDTruePi0PtOpenAngle[iCut] = new TH2F("ESD_TruePi0_Pt_OpenAngle","ESD_TruePi0_Pt_OpenAngle",150,0.03,15.,200,0,2*TMath::Pi());            
860                SetLogBinningXTH2(hESDTruePi0PtOpenAngle[iCut]);
861                fTrueList[iCut]->Add(hESDTruePi0PtOpenAngle[iCut]);
862                hESDTrueEtaPtOpenAngle[iCut] = new TH2F("ESD_TrueEta_Pt_OpenAngle","ESD_TrueEta_Pt_OpenAngle",150,0.03,15.,200,0,2*TMath::Pi());            
863                SetLogBinningXTH2(hESDTrueEtaPtOpenAngle[iCut]);
864                fTrueList[iCut]->Add(hESDTrueEtaPtOpenAngle[iCut]);
865                
866             }
867          }
868       }
869    }
870
871    fV0Reader=(AliV0ReaderV1*)AliAnalysisManager::GetAnalysisManager()->GetTask("V0ReaderV1");
872    if(!fV0Reader){printf("Error: No V0 Reader");return;} // GetV0Reader
873
874    if(fV0Reader)
875       if((AliConversionCuts*)fV0Reader->GetConversionCuts())
876          if(((AliConversionCuts*)fV0Reader->GetConversionCuts())->GetCutHistograms())
877             fOutputContainer->Add(((AliConversionCuts*)fV0Reader->GetConversionCuts())->GetCutHistograms());
878
879    for(Int_t iCut = 0; iCut<fnCuts;iCut++){
880       if(!((AliConversionCuts*)fCutArray->At(iCut))) continue;
881       if(((AliConversionCuts*)fCutArray->At(iCut))->GetCutHistograms()){
882          fCutFolder[iCut]->Add(((AliConversionCuts*)fCutArray->At(iCut))->GetCutHistograms());
883       }
884       if(fDoMesonAnalysis){
885          if(!((AliConversionMesonCuts*)fMesonCutArray->At(iCut))) continue;
886          if(((AliConversionMesonCuts*)fMesonCutArray->At(iCut))->GetCutHistograms()){
887             fCutFolder[iCut]->Add(((AliConversionMesonCuts*)fMesonCutArray->At(iCut))->GetCutHistograms());
888          }
889       }
890    }
891    PostData(1, fOutputContainer);
892 }
893 //_____________________________________________________________________________
894 Bool_t AliAnalysisTaskGammaConvV1::Notify()
895 {
896    for(Int_t iCut = 0; iCut<fnCuts;iCut++){
897       if(!((AliConversionCuts*)fCutArray->At(iCut))->GetDoEtaShift()){
898          hEtaShift[iCut]->Fill(0.,(((AliConversionCuts*)fCutArray->At(iCut))->GetEtaShift()));
899          continue; // No Eta Shift requested, continue
900       }
901       if(((AliConversionCuts*)fCutArray->At(iCut))->GetEtaShift() == 0.0){ // Eta Shift requested but not set, get shift automatically
902          ((AliConversionCuts*)fCutArray->At(iCut))->GetCorrectEtaShiftFromPeriod(fV0Reader->GetPeriodName());
903           hEtaShift[iCut]->Fill(0.,(((AliConversionCuts*)fCutArray->At(iCut))->GetEtaShift()));
904          ((AliConversionCuts*)fCutArray->At(iCut))->DoEtaShift(kFALSE); // Eta Shift Set, make sure that it is called only once   
905          continue;
906       }
907       else{
908          printf(" Gamma Conversion Task %s :: Eta Shift Manually Set to %f \n\n",
909                 (((AliConversionCuts*)fCutArray->At(iCut))->GetCutNumber()).Data(),((AliConversionCuts*)fCutArray->At(iCut))->GetEtaShift());
910           hEtaShift[iCut]->Fill(0.,(((AliConversionCuts*)fCutArray->At(iCut))->GetEtaShift()));
911          ((AliConversionCuts*)fCutArray->At(iCut))->DoEtaShift(kFALSE); // Eta Shift Set, make sure that it is called only once   
912       }
913    }
914    
915    return kTRUE;
916 }
917 //_____________________________________________________________________________
918 void AliAnalysisTaskGammaConvV1::UserExec(Option_t *)
919 {
920    //
921    // Called for each event
922    //
923    Int_t eventQuality = ((AliConversionCuts*)fV0Reader->GetConversionCuts())->GetEventQuality();
924    if(eventQuality == 2 || eventQuality == 3){// Event Not Accepted due to MC event missing or wrong trigger for V0ReaderV1
925       for(Int_t iCut = 0; iCut<fnCuts; iCut++){
926          hNEvents[iCut]->Fill(eventQuality);
927       }
928       return;
929    }
930
931    if(fIsMC) fMCEvent = MCEvent();
932    if(fMCEvent == NULL) fIsMC = kFALSE;
933    
934    fInputEvent = InputEvent();
935
936    if(fIsMC && fInputEvent->IsA()==AliESDEvent::Class()){
937       fMCStack = fMCEvent->Stack();
938       if(fMCStack == NULL) fIsMC = kFALSE;
939    }
940
941    fReaderGammas = fV0Reader->GetReconstructedGammas(); // Gammas from default Cut
942    
943    // ------------------- BeginEvent ----------------------------
944
945    AliEventplane *EventPlane = fInputEvent->GetEventplane();
946    if(fIsHeavyIon ==1)fEventPlaneAngle = EventPlane->GetEventplane("V0",fInputEvent,2);
947    else fEventPlaneAngle=0.0;
948    
949    if(fIsMC && fInputEvent->IsA()==AliAODEvent::Class() && !(fV0Reader->AreAODsRelabeled())){
950       RelabelAODPhotonCandidates(kTRUE);    // In case of AODMC relabeling MC
951       fV0Reader->RelabelAODs(kTRUE);
952    }
953    for(Int_t iCut = 0; iCut<fnCuts; iCut++){
954       fiCut = iCut;
955       Int_t eventNotAccepted =
956          ((AliConversionCuts*)fCutArray->At(iCut))
957          ->IsEventAcceptedByConversionCut(fV0Reader->GetConversionCuts(),fInputEvent,fMCEvent,fIsHeavyIon);
958       if(eventNotAccepted){
959          // cout << "event rejected due to wrong trigger: " <<eventNotAccepted << endl;
960          hNEvents[iCut]->Fill(eventNotAccepted); // Check Centrality, PileUp, SDD and V0AND --> Not Accepted => eventQuality = 1
961          continue;
962       }
963
964       if(eventQuality != 0){// Event Not Accepted
965          // cout << "event rejected due to: " <<eventQuality << endl;
966          hNEvents[iCut]->Fill(eventQuality);
967          continue;
968       }
969
970       hNEvents[iCut]->Fill(eventQuality); // Should be 0 here
971       hNGoodESDTracks[iCut]->Fill(fV0Reader->GetNumberOfPrimaryTracks());
972       if(((AliConversionCuts*)fCutArray->At(iCut))->IsHeavyIon() == 2)  hNV0Tracks[iCut]->Fill(fInputEvent->GetVZEROData()->GetMTotV0A());
973       else hNV0Tracks[iCut]->Fill(fInputEvent->GetVZEROData()->GetMTotV0A()+fInputEvent->GetVZEROData()->GetMTotV0C());
974
975       if(fIsMC){
976          // Process MC Particle
977          if(((AliConversionCuts*)fCutArray->At(iCut))->GetSignalRejection() != 0){
978             if(fInputEvent->IsA()==AliESDEvent::Class()){
979                ((AliConversionCuts*)fCutArray->At(iCut))->GetNotRejectedParticles(((AliConversionCuts*)fCutArray->At(iCut))->GetSignalRejection(),
980                                                                                   ((AliConversionCuts*)fCutArray->At(iCut))->GetAcceptedHeader(),
981                                                                                   fMCEvent);
982             }
983             else if(fInputEvent->IsA()==AliAODEvent::Class()){
984                ((AliConversionCuts*)fCutArray->At(iCut))->GetNotRejectedParticles(((AliConversionCuts*)fCutArray->At(iCut))->GetSignalRejection(),
985                                                                                   ((AliConversionCuts*)fCutArray->At(iCut))->GetAcceptedHeader(),
986                                                                                   fInputEvent);
987             }
988
989             if(((AliConversionCuts*)fCutArray->At(iCut))->GetAcceptedHeader()){
990                for(Int_t i = 0;i<(((AliConversionCuts*)fCutArray->At(iCut))->GetAcceptedHeader())->GetEntries();i++){
991                   TString nameBin= hMCHeaders[iCut]->GetXaxis()->GetBinLabel(i+1);
992                   if (nameBin.CompareTo("")== 0){
993                      TString nameHeader = ((TObjString*)((TList*)((AliConversionCuts*)fCutArray->At(iCut))
994                                                          ->GetAcceptedHeader())->At(i))->GetString();
995                      hMCHeaders[iCut]->GetXaxis()->SetBinLabel(i+1,nameHeader.Data());
996                   }
997                }
998             }
999          }
1000       }
1001       if(fIsMC){
1002          if(fInputEvent->IsA()==AliESDEvent::Class())
1003             ProcessMCParticles();
1004          if(fInputEvent->IsA()==AliAODEvent::Class())
1005             ProcessAODMCParticles();
1006       }
1007
1008       ProcessPhotonCandidates(); // Process this cuts gammas
1009
1010       hNGammaCandidates[iCut]->Fill(fGammaCandidates->GetEntries());
1011       if(fDoMesonAnalysis){ // Meson Analysis
1012          if(((AliConversionMesonCuts*)fMesonCutArray->At(iCut))->UseMCPSmearing() && fIsMC){
1013             fUnsmearedPx = new Double_t[fGammaCandidates->GetEntries()]; // Store unsmeared Momenta
1014             fUnsmearedPy = new Double_t[fGammaCandidates->GetEntries()];
1015             fUnsmearedPz = new Double_t[fGammaCandidates->GetEntries()];
1016             fUnsmearedE =  new Double_t[fGammaCandidates->GetEntries()];
1017
1018             for(Int_t gamma=0;gamma<fGammaCandidates->GetEntries();gamma++){ // Smear the AODPhotons in MC
1019                fUnsmearedPx[gamma] = ((AliAODConversionPhoton*)fGammaCandidates->At(gamma))->Px();
1020                fUnsmearedPy[gamma] = ((AliAODConversionPhoton*)fGammaCandidates->At(gamma))->Py();
1021                fUnsmearedPz[gamma] = ((AliAODConversionPhoton*)fGammaCandidates->At(gamma))->Pz();
1022                fUnsmearedE[gamma] =  ((AliAODConversionPhoton*)fGammaCandidates->At(gamma))->E();
1023                ((AliConversionMesonCuts*)fMesonCutArray->At(iCut))->SmearParticle(dynamic_cast<AliAODConversionPhoton*>(fGammaCandidates->At(gamma)));
1024             }
1025          }
1026
1027          CalculatePi0Candidates(); // Combine Gammas
1028          if(((AliConversionMesonCuts*)fMesonCutArray->At(iCut))->DoBGCalculation()){
1029             if(((AliConversionMesonCuts*)fMesonCutArray->At(iCut))->BackgroundHandlerType() == 0){
1030                CalculateBackground(); // Combinatorial Background
1031                UpdateEventByEventData(); // Store Event for mixed Events
1032             }
1033             else{
1034                CalculateBackgroundRP(); // Combinatorial Background
1035                fBGHandlerRP[iCut]->AddEvent(fGammaCandidates,fInputEvent); // Store Event for mixed Events
1036             }
1037          }
1038          if(((AliConversionMesonCuts*)fMesonCutArray->At(iCut))->UseMCPSmearing() && fIsMC){
1039             for(Int_t gamma=0;gamma<fGammaCandidates->GetEntries();gamma++){ // Smear the AODPhotons in MC
1040                ((AliAODConversionPhoton*)fGammaCandidates->At(gamma))->SetPx(fUnsmearedPx[gamma]); // Reset Unsmeared Momenta
1041                ((AliAODConversionPhoton*)fGammaCandidates->At(gamma))->SetPy(fUnsmearedPy[gamma]);
1042                ((AliAODConversionPhoton*)fGammaCandidates->At(gamma))->SetPz(fUnsmearedPz[gamma]);
1043                ((AliAODConversionPhoton*)fGammaCandidates->At(gamma))->SetE(fUnsmearedE[gamma]);
1044             }
1045             delete[] fUnsmearedPx; fUnsmearedPx = 0x0;
1046             delete[] fUnsmearedPy; fUnsmearedPy = 0x0;
1047             delete[] fUnsmearedPz; fUnsmearedPz = 0x0;
1048             delete[] fUnsmearedE;  fUnsmearedE  = 0x0;
1049          }
1050       }
1051       fGammaCandidates->Clear(); // delete this cuts good gammas
1052    }
1053
1054    if(fIsMC && fInputEvent->IsA()==AliAODEvent::Class() && !(fV0Reader->AreAODsRelabeled())){
1055       RelabelAODPhotonCandidates(kFALSE); // Back to ESDMC Label
1056       fV0Reader->RelabelAODs(kFALSE);
1057    }
1058
1059    PostData(1, fOutputContainer);
1060 }
1061 //________________________________________________________________________
1062 void AliAnalysisTaskGammaConvV1::ProcessPhotonCandidates()
1063 {
1064    Int_t nV0 = 0;
1065    TList *GammaCandidatesStepOne = new TList();
1066    TList *GammaCandidatesStepTwo = new TList();
1067    // Loop over Photon Candidates allocated by ReaderV1
1068    for(Int_t i = 0; i < fReaderGammas->GetEntriesFast(); i++){
1069       AliAODConversionPhoton* PhotonCandidate = (AliAODConversionPhoton*) fReaderGammas->At(i);
1070       if(!PhotonCandidate) continue;
1071       fIsFromMBHeader = kTRUE;
1072       if(fIsMC && ((AliConversionCuts*)fCutArray->At(fiCut))->GetSignalRejection() != 0){
1073          Int_t isPosFromMBHeader
1074             = ((AliConversionCuts*)fCutArray->At(fiCut))->IsParticleFromBGEvent(PhotonCandidate->GetMCLabelPositive(), fMCStack, fInputEvent);
1075          if(isPosFromMBHeader == 0 && ((AliConversionCuts*)fCutArray->At(fiCut))->GetSignalRejection() != 3) continue;
1076          Int_t isNegFromMBHeader
1077             = ((AliConversionCuts*)fCutArray->At(fiCut))->IsParticleFromBGEvent(PhotonCandidate->GetMCLabelNegative(), fMCStack, fInputEvent);
1078          if(isNegFromMBHeader == 0 && ((AliConversionCuts*)fCutArray->At(fiCut))->GetSignalRejection() != 3) continue;
1079
1080          if( (isNegFromMBHeader+isPosFromMBHeader) != 4) fIsFromMBHeader = kFALSE;
1081       }
1082
1083       if(!((AliConversionCuts*)fCutArray->At(fiCut))->PhotonIsSelected(PhotonCandidate,fInputEvent)) continue;
1084       if(!((AliConversionCuts*)fCutArray->At(fiCut))->InPlaneOutOfPlaneCut(PhotonCandidate->GetPhotonPhi(),fEventPlaneAngle)) continue;
1085       if(!((AliConversionCuts*)fCutArray->At(fiCut))->UseElecSharingCut() &&
1086          !((AliConversionCuts*)fCutArray->At(fiCut))->UseToCloseV0sCut()){
1087          fGammaCandidates->Add(PhotonCandidate); // if no second loop is required add to events good gammas
1088
1089          if(fIsFromMBHeader){
1090             hESDConvGammaPt[fiCut]->Fill(PhotonCandidate->Pt());
1091             if (fDoPhotonQA > 0){
1092                hESDConvGammaR[fiCut]->Fill(PhotonCandidate->GetConversionRadius());
1093                hESDConvGammaEta[fiCut]->Fill(PhotonCandidate->Eta());
1094             }   
1095          }
1096          if(fIsMC){
1097             if(fInputEvent->IsA()==AliESDEvent::Class())
1098                ProcessTruePhotonCandidates(PhotonCandidate);
1099             if(fInputEvent->IsA()==AliAODEvent::Class())
1100                ProcessTruePhotonCandidatesAOD(PhotonCandidate);
1101          }
1102          if (fIsFromMBHeader && fDoPhotonQA == 2){
1103             if (fIsHeavyIon == 1 && PhotonCandidate->Pt() > 0.399 && PhotonCandidate->Pt() < 12.){
1104                fPtGamma = PhotonCandidate->Pt();
1105                fDCAzPhoton = PhotonCandidate->GetDCAzToPrimVtx();
1106                fRConvPhoton = PhotonCandidate->GetConversionRadius();
1107                fEtaPhoton = PhotonCandidate->GetPhotonEta();
1108                iCatPhoton = PhotonCandidate->GetPhotonQuality();
1109                tESDConvGammaPtDcazCat[fiCut]->Fill();
1110             } else if ( PhotonCandidate->Pt() > 0.299 && PhotonCandidate->Pt() < 16.){
1111                fPtGamma = PhotonCandidate->Pt();
1112                fDCAzPhoton = PhotonCandidate->GetDCAzToPrimVtx();
1113                fRConvPhoton = PhotonCandidate->GetConversionRadius();
1114                fEtaPhoton = PhotonCandidate->GetPhotonEta();
1115                iCatPhoton = PhotonCandidate->GetPhotonQuality();
1116                tESDConvGammaPtDcazCat[fiCut]->Fill();
1117             }   
1118          }   
1119       } else if(((AliConversionCuts*)fCutArray->At(fiCut))->UseElecSharingCut()){ // if Shared Electron cut is enabled, Fill array, add to step one
1120          ((AliConversionCuts*)fCutArray->At(fiCut))->FillElectonLabelArray(PhotonCandidate,nV0);
1121          nV0++;
1122          GammaCandidatesStepOne->Add(PhotonCandidate);
1123       } else if(!((AliConversionCuts*)fCutArray->At(fiCut))->UseElecSharingCut() &&
1124               ((AliConversionCuts*)fCutArray->At(fiCut))->UseToCloseV0sCut()){ // shared electron is disabled, step one not needed -> step two
1125          GammaCandidatesStepTwo->Add(PhotonCandidate);
1126       }
1127    }
1128    if(((AliConversionCuts*)fCutArray->At(fiCut))->UseElecSharingCut()){
1129       for(Int_t i = 0;i<GammaCandidatesStepOne->GetEntries();i++){
1130          AliAODConversionPhoton *PhotonCandidate= (AliAODConversionPhoton*) GammaCandidatesStepOne->At(i);
1131          if(!PhotonCandidate) continue;
1132          fIsFromMBHeader = kTRUE;
1133          if(fMCStack && ((AliConversionCuts*)fCutArray->At(fiCut))->GetSignalRejection() != 0){
1134             Int_t isPosFromMBHeader
1135                = ((AliConversionCuts*)fCutArray->At(fiCut))->IsParticleFromBGEvent(PhotonCandidate->GetMCLabelPositive(), fMCStack, fInputEvent);
1136             Int_t isNegFromMBHeader
1137                = ((AliConversionCuts*)fCutArray->At(fiCut))->IsParticleFromBGEvent(PhotonCandidate->GetMCLabelNegative(), fMCStack, fInputEvent);
1138             if( (isNegFromMBHeader+isPosFromMBHeader) != 4) fIsFromMBHeader = kFALSE;
1139          }
1140          if(!((AliConversionCuts*)fCutArray->At(fiCut))->RejectSharedElectronV0s(PhotonCandidate,i,GammaCandidatesStepOne->GetEntries())) continue;
1141          if(!((AliConversionCuts*)fCutArray->At(fiCut))->UseToCloseV0sCut()){ // To Colse v0s cut diabled, step two not needed
1142             fGammaCandidates->Add(PhotonCandidate);
1143             if(fIsFromMBHeader){
1144                hESDConvGammaPt[fiCut]->Fill(PhotonCandidate->Pt());
1145                if (fDoPhotonQA > 0){
1146                   hESDConvGammaR[fiCut]->Fill(PhotonCandidate->GetConversionRadius());
1147                   hESDConvGammaEta[fiCut]->Fill(PhotonCandidate->Eta());
1148                }
1149             }
1150          }
1151          if(fIsMC){
1152             if(fInputEvent->IsA()==AliESDEvent::Class())
1153                ProcessTruePhotonCandidates(PhotonCandidate);
1154             if(fInputEvent->IsA()==AliAODEvent::Class())
1155                ProcessTruePhotonCandidatesAOD(PhotonCandidate);
1156          } else GammaCandidatesStepTwo->Add(PhotonCandidate); // Close v0s cut enabled -> add to list two
1157          
1158            if (fIsFromMBHeader && fDoPhotonQA == 2){
1159             if (fIsHeavyIon ==1 && PhotonCandidate->Pt() > 0.399 && PhotonCandidate->Pt() < 12.){
1160                fPtGamma = PhotonCandidate->Pt();
1161                fDCAzPhoton = PhotonCandidate->GetDCAzToPrimVtx();
1162                fRConvPhoton = PhotonCandidate->GetConversionRadius();
1163                fEtaPhoton = PhotonCandidate->GetPhotonEta();
1164                iCatPhoton = PhotonCandidate->GetPhotonQuality();
1165                tESDConvGammaPtDcazCat[fiCut]->Fill();
1166             } else if ( PhotonCandidate->Pt() > 0.299 && PhotonCandidate->Pt() < 16.){
1167                fPtGamma = PhotonCandidate->Pt();
1168                fDCAzPhoton = PhotonCandidate->GetDCAzToPrimVtx();
1169                fRConvPhoton = PhotonCandidate->GetConversionRadius();
1170                fEtaPhoton = PhotonCandidate->GetPhotonEta();
1171                iCatPhoton = PhotonCandidate->GetPhotonQuality();
1172                tESDConvGammaPtDcazCat[fiCut]->Fill();
1173             }
1174          }
1175       }
1176    }
1177    if(((AliConversionCuts*)fCutArray->At(fiCut))->UseToCloseV0sCut()){
1178       for(Int_t i = 0;i<GammaCandidatesStepTwo->GetEntries();i++){
1179          AliAODConversionPhoton* PhotonCandidate = (AliAODConversionPhoton*) GammaCandidatesStepTwo->At(i);
1180          if(!PhotonCandidate) continue;
1181          fIsFromMBHeader = kTRUE;
1182          if(fMCStack && ((AliConversionCuts*)fCutArray->At(fiCut))->GetSignalRejection() != 0){
1183             Int_t isPosFromMBHeader
1184                = ((AliConversionCuts*)fCutArray->At(fiCut))->IsParticleFromBGEvent(PhotonCandidate->GetMCLabelPositive(), fMCStack, fInputEvent);
1185             Int_t isNegFromMBHeader
1186                = ((AliConversionCuts*)fCutArray->At(fiCut))->IsParticleFromBGEvent(PhotonCandidate->GetMCLabelNegative(), fMCStack, fInputEvent);
1187             if( (isNegFromMBHeader+isPosFromMBHeader) != 4) fIsFromMBHeader = kFALSE;
1188          }
1189          if(!((AliConversionCuts*)fCutArray->At(fiCut))->RejectToCloseV0s(PhotonCandidate,GammaCandidatesStepTwo,i)) continue;
1190          fGammaCandidates->Add(PhotonCandidate); // Add gamma to current cut TList
1191          if(fIsFromMBHeader){
1192             hESDConvGammaPt[fiCut]->Fill(PhotonCandidate->Pt());
1193             if (fDoPhotonQA > 0){
1194                hESDConvGammaR[fiCut]->Fill(PhotonCandidate->GetConversionRadius());
1195                hESDConvGammaEta[fiCut]->Fill(PhotonCandidate->Eta());
1196             }
1197          }
1198          if(fIsMC){
1199             if(fInputEvent->IsA()==AliESDEvent::Class())
1200                ProcessTruePhotonCandidates(PhotonCandidate);
1201             if(fInputEvent->IsA()==AliAODEvent::Class())
1202                ProcessTruePhotonCandidatesAOD(PhotonCandidate);
1203          }
1204          if (fIsFromMBHeader){
1205            if (fIsHeavyIon == 1 && PhotonCandidate->Pt() > 0.399 && PhotonCandidate->Pt() < 12.){
1206                fPtGamma = PhotonCandidate->Pt();
1207                fDCAzPhoton = PhotonCandidate->GetDCAzToPrimVtx();
1208                fRConvPhoton = PhotonCandidate->GetConversionRadius();
1209                fEtaPhoton = PhotonCandidate->GetPhotonEta();
1210                iCatPhoton = PhotonCandidate->GetPhotonQuality();
1211                tESDConvGammaPtDcazCat[fiCut]->Fill();
1212             } else if ( PhotonCandidate->Pt() > 0.299 && PhotonCandidate->Pt() < 16.){
1213                fPtGamma = PhotonCandidate->Pt();
1214                fDCAzPhoton = PhotonCandidate->GetDCAzToPrimVtx();
1215                fRConvPhoton = PhotonCandidate->GetConversionRadius();
1216                fEtaPhoton = PhotonCandidate->GetPhotonEta();
1217                iCatPhoton = PhotonCandidate->GetPhotonQuality();
1218                tESDConvGammaPtDcazCat[fiCut]->Fill();
1219             }
1220          }
1221       }
1222    }
1223
1224    delete GammaCandidatesStepOne;
1225    GammaCandidatesStepOne = 0x0;
1226    delete GammaCandidatesStepTwo;
1227    GammaCandidatesStepTwo = 0x0;
1228
1229 }
1230 //________________________________________________________________________
1231 void AliAnalysisTaskGammaConvV1::ProcessTruePhotonCandidatesAOD(AliAODConversionPhoton *TruePhotonCandidate)
1232 {
1233
1234    Double_t magField = fInputEvent->GetMagneticField();
1235    if( magField  < 0.0 ){
1236       magField =  1.0;
1237    }
1238    else {
1239       magField =  -1.0;
1240    }
1241
1242    TClonesArray *AODMCTrackArray = dynamic_cast<TClonesArray*>(fInputEvent->FindListObject(AliAODMCParticle::StdBranchName()));
1243    AliAODMCParticle *posDaughter = (AliAODMCParticle*) AODMCTrackArray->At(TruePhotonCandidate->GetMCLabelPositive());
1244    AliAODMCParticle *negDaughter = (AliAODMCParticle*) AODMCTrackArray->At(TruePhotonCandidate->GetMCLabelNegative());
1245    iPhotonMCInfo = 0;
1246    
1247    if(posDaughter == NULL || negDaughter == NULL) return; // One particle does not exist
1248    Int_t pdgCode[2] = {abs(posDaughter->GetPdgCode()),abs(negDaughter->GetPdgCode())};
1249
1250    if(posDaughter->GetMother() != negDaughter->GetMother()){
1251       FillPhotonCombinatorialBackgroundHist(TruePhotonCandidate, pdgCode);
1252       iPhotonMCInfo = 1;
1253       return;
1254    }
1255    else if(posDaughter->GetMother() == -1){
1256       FillPhotonCombinatorialBackgroundHist(TruePhotonCandidate, pdgCode);
1257       iPhotonMCInfo = 1;
1258       return;
1259    }
1260
1261    if(pdgCode[0]!=11 || pdgCode[1]!=11){
1262       iPhotonMCInfo = 1;
1263       return; //One Particle is not a electron
1264    }
1265    if(posDaughter->GetPdgCode()==negDaughter->GetPdgCode()){
1266       iPhotonMCInfo = 1; 
1267       return; // Same Charge
1268    }
1269    
1270    AliAODMCParticle *Photon = (AliAODMCParticle*) AODMCTrackArray->At(posDaughter->GetMother());
1271    AliVTrack * electronCandidate = ((AliConversionCuts*)fCutArray->At(fiCut))->GetTrack(fInputEvent,TruePhotonCandidate->GetTrackLabelNegative() );
1272    AliVTrack * positronCandidate = ((AliConversionCuts*)fCutArray->At(fiCut))->GetTrack(fInputEvent,TruePhotonCandidate->GetTrackLabelPositive() );
1273    Double_t deltaPhi = magField * TVector2::Phi_mpi_pi( electronCandidate->Phi()-positronCandidate->Phi());
1274
1275    if(Photon->GetPdgCode() != 22){
1276       hESDTrueDalitzPsiPairDeltaPhi[fiCut]->Fill(deltaPhi,TruePhotonCandidate->GetPsiPair());
1277       iPhotonMCInfo = 1;
1278       return; // Mother is no Photon
1279    }
1280    
1281    if(((posDaughter->GetMCProcessCode())) != 5 || ((negDaughter->GetMCProcessCode())) != 5){
1282       iPhotonMCInfo = 1;
1283       return;// check if the daughters come from a conversion 
1284    }   
1285    // STILL A BUG IN ALIROOT >>8 HAS TPO BE REMOVED AFTER FIX
1286    
1287    
1288    
1289    // True Photon
1290    if(fIsFromMBHeader)hESDTrueConvGammaPt[fiCut]->Fill(TruePhotonCandidate->Pt());
1291    hESDTrueGammaPsiPairDeltaPhi[fiCut]->Fill(deltaPhi,TruePhotonCandidate->GetPsiPair());  
1292    if(Photon->IsPrimary()){ 
1293       // Count just primary MC Gammas as true --> For Ratio esdtruegamma / mcconvgamma
1294       if(fIsFromMBHeader){
1295          iPhotonMCInfo = 6;
1296          hESDTruePrimaryConvGammaPt[fiCut]->Fill(TruePhotonCandidate->Pt());
1297          hESDTruePrimaryConvGammaESDPtMCPt[fiCut]->Fill(TruePhotonCandidate->Pt(),Photon->Pt()); // Allways Filled
1298       }
1299       // (Not Filled for i6, Extra Signal Gamma (parambox) are secondary)
1300    }
1301    else{
1302       if(fIsFromMBHeader){
1303          hESDTrueSecondaryConvGammaPt[fiCut]->Fill(TruePhotonCandidate->Pt());
1304          iPhotonMCInfo = 2;
1305          if(((AliAODMCParticle*)AODMCTrackArray->At(Photon->GetMother()))->GetMother() > -1 &&
1306             ((AliAODMCParticle*)AODMCTrackArray->At(((AliAODMCParticle*)AODMCTrackArray->At(Photon->GetMother()))->GetMother()))->GetPdgCode() == 3122){
1307             iPhotonMCInfo = 5;
1308             hESDTrueSecondaryConvGammaFromXFromLambdaPt[fiCut]->Fill(TruePhotonCandidate->Pt());
1309          }
1310          if(((AliAODMCParticle*)AODMCTrackArray->At(Photon->GetMother()))->GetMother() > -1 &&
1311             ((AliAODMCParticle*)AODMCTrackArray->At(((AliAODMCParticle*)AODMCTrackArray->At(Photon->GetMother()))->GetMother()))->GetPdgCode() == 310){
1312             iPhotonMCInfo = 4;
1313             hESDTrueSecondaryConvGammaFromXFromK0sPt[fiCut]->Fill(TruePhotonCandidate->Pt());
1314          }
1315          if(((AliAODMCParticle*)AODMCTrackArray->At(Photon->GetMother()))->GetMother() > -1 &&
1316             ((AliAODMCParticle*)AODMCTrackArray->At(((AliAODMCParticle*)AODMCTrackArray->At(Photon->GetMother()))->GetMother()))->GetPdgCode() == 221){
1317             iPhotonMCInfo = 3;
1318          }
1319       }
1320    }
1321    
1322 }
1323 //________________________________________________________________________
1324 void AliAnalysisTaskGammaConvV1::ProcessTruePhotonCandidates(AliAODConversionPhoton *TruePhotonCandidate)
1325 {
1326    
1327   Double_t magField = fInputEvent->GetMagneticField();
1328    if( magField  < 0.0 ){
1329       magField =  1.0;
1330    }
1331    else {
1332       magField =  -1.0;
1333    }
1334
1335    // Process True Photons
1336    TParticle *posDaughter = TruePhotonCandidate->GetPositiveMCDaughter(fMCStack);
1337    TParticle *negDaughter = TruePhotonCandidate->GetNegativeMCDaughter(fMCStack);
1338
1339    iPhotonMCInfo = 0;
1340    
1341    if(posDaughter == NULL || negDaughter == NULL) return; // One particle does not exist
1342    Int_t pdgCode[2] = {abs(posDaughter->GetPdgCode()),abs(negDaughter->GetPdgCode())};
1343    iPhotonMCInfo = 1;
1344    if(posDaughter->GetMother(0) != negDaughter->GetMother(0)){
1345       FillPhotonCombinatorialBackgroundHist(TruePhotonCandidate, pdgCode);
1346       return;
1347    }
1348    else if(posDaughter->GetMother(0) == -1){
1349       FillPhotonCombinatorialBackgroundHist(TruePhotonCandidate, pdgCode);
1350       return;
1351    }
1352
1353    if(pdgCode[0]!=11 || pdgCode[1]!=11) return; //One Particle is not a electron
1354    
1355    if(posDaughter->GetPdgCode()==negDaughter->GetPdgCode()) return; // Same Charge
1356
1357    TParticle *Photon = TruePhotonCandidate->GetMCParticle(fMCStack);
1358    AliVTrack * electronCandidate = ((AliConversionCuts*)fCutArray->At(fiCut))->GetTrack(fInputEvent,TruePhotonCandidate->GetTrackLabelNegative() );
1359    AliVTrack * positronCandidate = ((AliConversionCuts*)fCutArray->At(fiCut))->GetTrack(fInputEvent,TruePhotonCandidate->GetTrackLabelPositive() );
1360    Double_t deltaPhi = magField * TVector2::Phi_mpi_pi( electronCandidate->Phi()-positronCandidate->Phi());
1361    
1362    if(Photon->GetPdgCode() != 22){
1363       hESDTrueDalitzPsiPairDeltaPhi[fiCut]->Fill(deltaPhi,TruePhotonCandidate->GetPsiPair());
1364       return; // Mother is no Photon
1365    }
1366    
1367    if(posDaughter->GetUniqueID() != 5 || negDaughter->GetUniqueID() !=5) return;// check if the daughters come from a conversion
1368
1369    
1370    
1371    // True Photon
1372    if(fIsFromMBHeader){
1373       hESDTrueConvGammaPt[fiCut]->Fill(TruePhotonCandidate->Pt());
1374    }
1375    hESDTrueGammaPsiPairDeltaPhi[fiCut]->Fill(deltaPhi,TruePhotonCandidate->GetPsiPair());  
1376    if(posDaughter->GetMother(0) <= fMCStack->GetNprimary()){
1377       // Count just primary MC Gammas as true --> For Ratio esdtruegamma / mcconvgamma
1378       if(fIsFromMBHeader){
1379          iPhotonMCInfo = 6;
1380          hESDTruePrimaryConvGammaPt[fiCut]->Fill(TruePhotonCandidate->Pt());  
1381          hESDTruePrimaryConvGammaESDPtMCPt[fiCut]->Fill(TruePhotonCandidate->Pt(),Photon->Pt()); // Allways Filled
1382
1383       }
1384       // (Not Filled for i6, Extra Signal Gamma (parambox) are secondary)
1385    }
1386    else{
1387       if(fIsFromMBHeader){
1388          iPhotonMCInfo = 2;
1389          hESDTrueSecondaryConvGammaPt[fiCut]->Fill(TruePhotonCandidate->Pt());
1390          if(fMCStack->Particle(Photon->GetMother(0))->GetMother(0) > -1 &&
1391             fMCStack->Particle(fMCStack->Particle(Photon->GetMother(0))->GetMother(0))->GetPdgCode() == 3122){
1392             hESDTrueSecondaryConvGammaFromXFromLambdaPt[fiCut]->Fill(TruePhotonCandidate->Pt());
1393             iPhotonMCInfo = 5;
1394          }
1395          if(fMCStack->Particle(Photon->GetMother(0))->GetMother(0) > -1 &&
1396             fMCStack->Particle(fMCStack->Particle(Photon->GetMother(0))->GetMother(0))->GetPdgCode() == 310){
1397             hESDTrueSecondaryConvGammaFromXFromK0sPt[fiCut]->Fill(TruePhotonCandidate->Pt());
1398             iPhotonMCInfo = 4;
1399          }
1400          if(fMCStack->Particle(Photon->GetMother(0))->GetMother(0) > -1 &&
1401             fMCStack->Particle(fMCStack->Particle(Photon->GetMother(0))->GetMother(0))->GetPdgCode() == 221){
1402             iPhotonMCInfo = 3;
1403          }
1404       }
1405    }
1406 }
1407 //________________________________________________________________________
1408 void AliAnalysisTaskGammaConvV1::ProcessAODMCParticles()
1409 {
1410
1411    TClonesArray *AODMCTrackArray = dynamic_cast<TClonesArray*>(fInputEvent->FindListObject(AliAODMCParticle::StdBranchName()));
1412
1413    // Loop over all primary MC particle
1414    for(Int_t i = 0; i < AODMCTrackArray->GetEntriesFast(); i++) {
1415
1416       AliAODMCParticle* particle = static_cast<AliAODMCParticle*>(AODMCTrackArray->At(i));
1417       if (!particle) continue;
1418       if (!particle->IsPrimary()) continue;
1419
1420       Int_t isMCFromMBHeader = -1;
1421       if(((AliConversionCuts*)fCutArray->At(fiCut))->GetSignalRejection() != 0){
1422          isMCFromMBHeader
1423             = ((AliConversionCuts*)fCutArray->At(fiCut))->IsParticleFromBGEvent(i, fMCStack, fInputEvent);
1424          if(isMCFromMBHeader == 0 && ((AliConversionCuts*)fCutArray->At(fiCut))->GetSignalRejection() != 3) continue;
1425       }
1426       
1427       if(!((AliConversionCuts*)fCutArray->At(fiCut))->InPlaneOutOfPlaneCut(particle->Phi(),fEventPlaneAngle,kFALSE)) continue;
1428       if(((AliConversionCuts*)fCutArray->At(fiCut))->PhotonIsSelectedAODMC(particle,AODMCTrackArray,kFALSE)){
1429          hMCAllGammaPt[fiCut]->Fill(particle->Pt()); // All MC Gamma
1430          if(particle->GetMother() >-1){ // Meson Decay Gamma
1431             switch((static_cast<AliAODMCParticle*>(AODMCTrackArray->At(particle->GetMother())))->GetPdgCode()){
1432             case 111: // Pi0
1433                hMCDecayGammaPi0Pt[fiCut]->Fill(particle->Pt());
1434                break;
1435             case 113: // Rho0
1436                hMCDecayGammaRhoPt[fiCut]->Fill(particle->Pt());
1437                break;
1438             case 221: // Eta
1439                hMCDecayGammaEtaPt[fiCut]->Fill(particle->Pt());
1440                break;
1441             case 223: // Omega
1442                hMCDecayGammaOmegaPt[fiCut]->Fill(particle->Pt());
1443                break;
1444             case 331: // Eta'
1445                hMCDecayGammaEtapPt[fiCut]->Fill(particle->Pt());
1446                break;
1447             case 333: // Phi
1448                hMCDecayGammaPhiPt[fiCut]->Fill(particle->Pt());
1449                break;
1450             case 3212: // Sigma
1451                hMCDecayGammaSigmaPt[fiCut]->Fill(particle->Pt());
1452                break;
1453             }
1454          }
1455       }
1456       if(((AliConversionCuts*)fCutArray->At(fiCut))->PhotonIsSelectedAODMC(particle,AODMCTrackArray,kTRUE)){
1457          Double_t rConv = 0;
1458          for(Int_t daughterIndex=particle->GetDaughter(0);daughterIndex<=particle->GetDaughter(1);daughterIndex++){
1459             AliAODMCParticle *tmpDaughter = static_cast<AliAODMCParticle*>(AODMCTrackArray->At(daughterIndex));
1460             if(!tmpDaughter) continue;
1461             if(abs(tmpDaughter->GetPdgCode()) == 11){
1462                rConv = sqrt( (tmpDaughter->Xv()*tmpDaughter->Xv()) + (tmpDaughter->Yv()*tmpDaughter->Yv()) );
1463             }
1464          }
1465          hMCConvGammaPt[fiCut]->Fill(particle->Pt());
1466          if (fDoPhotonQA > 0){
1467             hMCConvGammaR[fiCut]->Fill(rConv);
1468             hMCConvGammaEta[fiCut]->Fill(particle->Eta());
1469          }
1470       }
1471       // Converted MC Gamma
1472       if(fDoMesonAnalysis){
1473          if(particle->GetPdgCode() == 310 && fDoMesonQA > 0){
1474             Double_t mesonY = 10.;
1475             if(particle->E() - particle->Pz() == 0 || particle->E() + particle->Pz() == 0){
1476                mesonY=10.-((AliConversionCuts*)fCutArray->At(fiCut))->GetEtaShift();
1477             } else{
1478                mesonY = 0.5*(TMath::Log((particle->E()+particle->Pz()) / (particle->E()-particle->Pz())))
1479                   -((AliConversionCuts*)fCutArray->At(fiCut))->GetEtaShift();
1480             }
1481             Float_t weightedK0s= 1;
1482             if(((AliConversionCuts*)fCutArray->At(fiCut))->IsParticleFromBGEvent(i, fMCStack, fInputEvent)){
1483                if (particle->Pt()>0.005){
1484                   weightedK0s= ((AliConversionCuts*)fCutArray->At(fiCut))->GetWeightForMeson(fV0Reader->GetPeriodName(),i, 0x0, fInputEvent);
1485                   //cout << "MC input \t"<<i << "\t" <<  particle->Pt()<<"\t"<<weighted << endl;
1486                }
1487             }
1488             hMCK0sPt[fiCut]->Fill(particle->Pt(),weightedK0s);
1489             hMCK0sWOWeightPt[fiCut]->Fill(particle->Pt());
1490             hMCK0sPtY[fiCut]->Fill(particle->Pt(),mesonY,weightedK0s);
1491          }         
1492          if(((AliConversionMesonCuts*)fMesonCutArray->At(fiCut))
1493             ->MesonIsSelectedAODMC(particle,AODMCTrackArray,((AliConversionCuts*)fCutArray->At(fiCut))->GetEtaShift())){
1494             AliAODMCParticle* daughter0 = static_cast<AliAODMCParticle*>(AODMCTrackArray->At(particle->GetDaughter(0)));
1495             AliAODMCParticle* daughter1 = static_cast<AliAODMCParticle*>(AODMCTrackArray->At(particle->GetDaughter(1)));
1496             Float_t weighted= 1;
1497             if(((AliConversionCuts*)fCutArray->At(fiCut))->IsParticleFromBGEvent(i, fMCStack, fInputEvent)){
1498                if (particle->Pt()>0.005){
1499                   weighted= ((AliConversionCuts*)fCutArray->At(fiCut))->GetWeightForMeson(fV0Reader->GetPeriodName(),i, 0x0, fInputEvent);
1500 //                   if(particle->GetPdgCode() == 221){
1501 //                      cout << "MC input \t"<<i << "\t" <<  particle->Pt()<<"\t"<<weighted << endl;
1502 //                   }
1503                }
1504             }
1505             Double_t mesonY = 10.;
1506             if(particle->E() - particle->Pz() == 0 || particle->E() + particle->Pz() == 0){
1507                mesonY=10.-((AliConversionCuts*)fCutArray->At(fiCut))->GetEtaShift();
1508             } else{
1509                mesonY = 0.5*(TMath::Log((particle->E()+particle->Pz()) / (particle->E()-particle->Pz())))
1510                   -((AliConversionCuts*)fCutArray->At(fiCut))->GetEtaShift();
1511             }
1512
1513             if(particle->GetPdgCode() == 111){
1514                hMCPi0Pt[fiCut]->Fill(particle->Pt(),weighted); // All MC Pi0
1515                hMCPi0WOWeightPt[fiCut]->Fill(particle->Pt());
1516                if (fDoMesonQA > 0) hMCPi0PtY[fiCut]->Fill(particle->Pt(),mesonY,weighted); // All MC Pi0
1517             } else if(particle->GetPdgCode() == 221){
1518                hMCEtaPt[fiCut]->Fill(particle->Pt(),weighted); // All MC Eta
1519                hMCEtaWOWeightPt[fiCut]->Fill(particle->Pt());
1520                if (fDoMesonQA > 0) hMCEtaPtY[fiCut]->Fill(particle->Pt(),mesonY,weighted); // All MC Pi0
1521             }
1522             
1523             // Check the acceptance for both gammas
1524             if(((AliConversionCuts*)fCutArray->At(fiCut))->PhotonIsSelectedAODMC(daughter0,AODMCTrackArray,kFALSE) &&
1525                ((AliConversionCuts*)fCutArray->At(fiCut))->PhotonIsSelectedAODMC(daughter1,AODMCTrackArray,kFALSE)  &&
1526                ((AliConversionCuts*)fCutArray->At(fiCut))->InPlaneOutOfPlaneCut(daughter0->Phi(),fEventPlaneAngle,kFALSE) &&
1527                ((AliConversionCuts*)fCutArray->At(fiCut))->InPlaneOutOfPlaneCut(daughter1->Phi(),fEventPlaneAngle,kFALSE)){
1528
1529                if(particle->GetPdgCode() == 111){
1530                   hMCPi0InAccPt[fiCut]->Fill(particle->Pt(),weighted); // MC Pi0 with gamma in acc
1531                } else if(particle->GetPdgCode() == 221){
1532                   hMCEtaInAccPt[fiCut]->Fill(particle->Pt(),weighted); // MC Eta with gamma in acc
1533                }
1534             }
1535          }
1536       }
1537    }
1538 }
1539 //________________________________________________________________________
1540 void AliAnalysisTaskGammaConvV1::ProcessMCParticles()
1541 {
1542    // Loop over all primary MC particle
1543    for(Int_t i = 0; i < fMCStack->GetNprimary(); i++) {
1544       TParticle* particle = (TParticle *)fMCStack->Particle(i);
1545       if (!particle) continue;
1546
1547       Int_t isMCFromMBHeader = -1;
1548       if(((AliConversionCuts*)fCutArray->At(fiCut))->GetSignalRejection() != 0){
1549          isMCFromMBHeader
1550             = ((AliConversionCuts*)fCutArray->At(fiCut))->IsParticleFromBGEvent(i, fMCStack, fInputEvent);
1551          if(isMCFromMBHeader == 0 && ((AliConversionCuts*)fCutArray->At(fiCut))->GetSignalRejection() != 3) continue;
1552       }
1553
1554       if(!((AliConversionCuts*)fCutArray->At(fiCut))->InPlaneOutOfPlaneCut(particle->Phi(),fEventPlaneAngle,kFALSE)) continue;
1555       if(((AliConversionCuts*)fCutArray->At(fiCut))->PhotonIsSelectedMC(particle,fMCStack,kFALSE)){
1556          hMCAllGammaPt[fiCut]->Fill(particle->Pt()); // All MC Gamma
1557          if(particle->GetMother(0) >-1){ // Meson Decay Gamma
1558             switch(fMCStack->Particle(particle->GetMother(0))->GetPdgCode()){
1559             case 111: // Pi0
1560                hMCDecayGammaPi0Pt[fiCut]->Fill(particle->Pt());
1561                break;
1562             case 113: // Rho0
1563                hMCDecayGammaRhoPt[fiCut]->Fill(particle->Pt());
1564                break;
1565             case 221: // Eta
1566                hMCDecayGammaEtaPt[fiCut]->Fill(particle->Pt());
1567                break;
1568             case 223: // Omega
1569                hMCDecayGammaOmegaPt[fiCut]->Fill(particle->Pt());
1570                break;
1571             case 331: // Eta'
1572                hMCDecayGammaEtapPt[fiCut]->Fill(particle->Pt());
1573                break;
1574             case 333: // Phi
1575                hMCDecayGammaPhiPt[fiCut]->Fill(particle->Pt());
1576                break;
1577             case 3212: // Sigma
1578                hMCDecayGammaSigmaPt[fiCut]->Fill(particle->Pt());
1579                break;
1580             }
1581          }
1582       }
1583       if(((AliConversionCuts*)fCutArray->At(fiCut))->PhotonIsSelectedMC(particle,fMCStack,kTRUE)){
1584          hMCConvGammaPt[fiCut]->Fill(particle->Pt());
1585          if (fDoPhotonQA > 0){
1586             hMCConvGammaR[fiCut]->Fill(((TParticle*)fMCStack->Particle(particle->GetFirstDaughter()))->R());
1587             hMCConvGammaEta[fiCut]->Fill(particle->Eta());
1588          }
1589       } // Converted MC Gamma
1590       if(fDoMesonAnalysis){
1591          if(particle->GetPdgCode() == 310 && fDoMesonQA > 0){
1592             Double_t mesonY = 10.;
1593             if(particle->Energy() - particle->Pz() == 0 || particle->Energy() + particle->Pz() == 0){
1594                mesonY=10.-((AliConversionCuts*)fCutArray->At(fiCut))->GetEtaShift();
1595             } else{
1596                mesonY = 0.5*(TMath::Log((particle->Energy()+particle->Pz()) / (particle->Energy()-particle->Pz())))
1597                   -((AliConversionCuts*)fCutArray->At(fiCut))->GetEtaShift();
1598             }
1599             Float_t weightedK0s= 1;
1600             if(((AliConversionCuts*)fCutArray->At(fiCut))->IsParticleFromBGEvent(i, fMCStack, fInputEvent)){
1601                if (particle->Pt()>0.005){
1602                   weightedK0s= ((AliConversionCuts*)fCutArray->At(fiCut))->GetWeightForMeson(fV0Reader->GetPeriodName(),i, fMCStack, fInputEvent);
1603                   //cout << "MC input \t"<<i << "\t" <<  particle->Pt()<<"\t"<<weighted << endl;
1604                }
1605             }
1606             hMCK0sPt[fiCut]->Fill(particle->Pt(),weightedK0s);
1607             hMCK0sWOWeightPt[fiCut]->Fill(particle->Pt());
1608             hMCK0sPtY[fiCut]->Fill(particle->Pt(),mesonY,weightedK0s);
1609          }
1610          if(((AliConversionMesonCuts*)fMesonCutArray->At(fiCut))
1611             ->MesonIsSelectedMC(particle,fMCStack,((AliConversionCuts*)fCutArray->At(fiCut))->GetEtaShift())){
1612             TParticle* daughter0 = (TParticle*)fMCStack->Particle(particle->GetFirstDaughter());
1613             TParticle* daughter1 = (TParticle*)fMCStack->Particle(particle->GetLastDaughter());
1614
1615             Float_t weighted= 1;
1616             if(((AliConversionCuts*)fCutArray->At(fiCut))->IsParticleFromBGEvent(i, fMCStack, fInputEvent)){
1617                if (particle->Pt()>0.005){
1618                   weighted= ((AliConversionCuts*)fCutArray->At(fiCut))->GetWeightForMeson(fV0Reader->GetPeriodName(),i, fMCStack, fInputEvent);
1619 //                   if(particle->GetPdgCode() == 221){
1620 //                      cout << "MC input \t"<<i << "\t" <<  particle->Pt()<<"\t"<<weighted << endl;
1621 //                   }
1622                }
1623             }
1624             Double_t mesonY = 10.;
1625             if(particle->Energy() - particle->Pz() == 0 || particle->Energy() + particle->Pz() == 0){
1626                mesonY=10.-((AliConversionCuts*)fCutArray->At(fiCut))->GetEtaShift();
1627             } else{
1628                mesonY = 0.5*(TMath::Log((particle->Energy()+particle->Pz()) / (particle->Energy()-particle->Pz())))
1629                   -((AliConversionCuts*)fCutArray->At(fiCut))->GetEtaShift();
1630             }
1631
1632             if(particle->GetPdgCode() == 111){
1633                hMCPi0Pt[fiCut]->Fill(particle->Pt(),weighted); // All MC Pi0
1634                hMCPi0WOWeightPt[fiCut]->Fill(particle->Pt());
1635                if (fDoMesonQA > 0) hMCPi0PtY[fiCut]->Fill(particle->Pt(),mesonY,weighted); // All MC Pi0
1636             } else if(particle->GetPdgCode() == 221){
1637                hMCEtaPt[fiCut]->Fill(particle->Pt(),weighted); // All MC Eta
1638                hMCEtaWOWeightPt[fiCut]->Fill(particle->Pt());
1639                if (fDoMesonQA > 0) hMCEtaPtY[fiCut]->Fill(particle->Pt(),mesonY,weighted); // All MC Pi0
1640             } 
1641
1642             // Check the acceptance for both gammas
1643             if(((AliConversionCuts*)fCutArray->At(fiCut))->PhotonIsSelectedMC(daughter0,fMCStack,kFALSE) &&
1644                ((AliConversionCuts*)fCutArray->At(fiCut))->PhotonIsSelectedMC(daughter1,fMCStack,kFALSE)  &&
1645                ((AliConversionCuts*)fCutArray->At(fiCut))->InPlaneOutOfPlaneCut(daughter0->Phi(),fEventPlaneAngle,kFALSE) &&
1646                ((AliConversionCuts*)fCutArray->At(fiCut))->InPlaneOutOfPlaneCut(daughter1->Phi(),fEventPlaneAngle,kFALSE)){
1647
1648                if(particle->GetPdgCode() == 111){
1649                   hMCPi0InAccPt[fiCut]->Fill(particle->Pt(),weighted); // MC Pi0 with gamma in acc
1650                } else if(particle->GetPdgCode() == 221){
1651                   hMCEtaInAccPt[fiCut]->Fill(particle->Pt(),weighted); // MC Eta with gamma in acc
1652                }
1653             }
1654          }
1655       }
1656    }
1657 }
1658 //________________________________________________________________________
1659 void AliAnalysisTaskGammaConvV1::CalculatePi0Candidates(){
1660
1661    // Conversion Gammas
1662    if(fGammaCandidates->GetEntries()>1){
1663       for(Int_t firstGammaIndex=0;firstGammaIndex<fGammaCandidates->GetEntries()-1;firstGammaIndex++){
1664          AliAODConversionPhoton *gamma0=dynamic_cast<AliAODConversionPhoton*>(fGammaCandidates->At(firstGammaIndex));
1665          if (gamma0==NULL) continue;
1666          for(Int_t secondGammaIndex=firstGammaIndex+1;secondGammaIndex<fGammaCandidates->GetEntries();secondGammaIndex++){
1667             AliAODConversionPhoton *gamma1=dynamic_cast<AliAODConversionPhoton*>(fGammaCandidates->At(secondGammaIndex));
1668             //Check for same Electron ID
1669             if (gamma1==NULL) continue;
1670             if(gamma0->GetTrackLabelPositive() == gamma1->GetTrackLabelPositive() ||
1671                gamma0->GetTrackLabelNegative() == gamma1->GetTrackLabelNegative() ||
1672                gamma0->GetTrackLabelNegative() == gamma1->GetTrackLabelPositive() ||
1673                gamma0->GetTrackLabelPositive() == gamma1->GetTrackLabelNegative() ) continue;
1674
1675             AliAODConversionMother *pi0cand = new AliAODConversionMother(gamma0,gamma1);
1676             pi0cand->SetLabels(firstGammaIndex,secondGammaIndex);
1677
1678             pi0cand->CalculateDistanceOfClossetApproachToPrimVtx(fInputEvent->GetPrimaryVertex());
1679             if((((AliConversionMesonCuts*)fMesonCutArray->At(fiCut))
1680                 ->MesonIsSelected(pi0cand,kTRUE,((AliConversionCuts*)fCutArray->At(fiCut))->GetEtaShift()))){
1681                hESDMotherInvMassPt[fiCut]->Fill(pi0cand->M(),pi0cand->Pt());
1682                if(pi0cand->GetAlpha()<0.1)
1683                   hESDMotherInvMassEalpha[fiCut]->Fill(pi0cand->M(),pi0cand->E());
1684                
1685                if (fDoMesonQA > 0){
1686                   if ( pi0cand->M() > 0.05 && pi0cand->M() < 0.17){
1687                      hESDMotherPi0PtY[fiCut]->Fill(pi0cand->Pt(),pi0cand->Rapidity()-((AliConversionCuts*)fCutArray->At(fiCut))->GetEtaShift());  
1688                      hESDMotherPi0PtAlpha[fiCut]->Fill(pi0cand->Pt(),pi0cand->GetAlpha());  
1689                      hESDMotherPi0PtOpenAngle[fiCut]->Fill(pi0cand->Pt(),pi0cand->GetOpeningAngle()); 
1690                      
1691                   } 
1692                   if ( pi0cand->M() > 0.45 && pi0cand->M() < 0.65){
1693                      hESDMotherEtaPtY[fiCut]->Fill(pi0cand->Pt(),pi0cand->Rapidity()-((AliConversionCuts*)fCutArray->At(fiCut))->GetEtaShift());  
1694                      hESDMotherEtaPtAlpha[fiCut]->Fill(pi0cand->Pt(),pi0cand->GetAlpha());  
1695                      hESDMotherEtaPtOpenAngle[fiCut]->Fill(pi0cand->Pt(),pi0cand->GetOpeningAngle());       
1696                   }
1697                }   
1698                if(((AliConversionMesonCuts*)fMesonCutArray->At(fiCut))->DoBGCalculation()){
1699                   Int_t zbin = 0;
1700                   Int_t mbin = 0;
1701
1702                   if(((AliConversionMesonCuts*)fMesonCutArray->At(fiCut))->BackgroundHandlerType() == 0){
1703                      zbin = fBGHandler[fiCut]->GetZBinIndex(fInputEvent->GetPrimaryVertex()->GetZ());
1704                      if(((AliConversionMesonCuts*)fMesonCutArray->At(fiCut))->UseTrackMultiplicity()){
1705                         mbin = fBGHandler[fiCut]->GetMultiplicityBinIndex(fV0Reader->GetNumberOfPrimaryTracks());
1706                      } else {
1707                         mbin = fBGHandler[fiCut]->GetMultiplicityBinIndex(fGammaCandidates->GetEntries());
1708                      }
1709                   }
1710                   else{
1711                      zbin = fBGHandlerRP[fiCut]->GetZBinIndex(fInputEvent->GetPrimaryVertex()->GetZ());
1712                      if(((AliConversionMesonCuts*)fMesonCutArray->At(fiCut))->UseTrackMultiplicity()){
1713                         mbin = fBGHandlerRP[fiCut]->GetMultiplicityBinIndex(fV0Reader->GetNumberOfPrimaryTracks());
1714                      } else {
1715                         mbin = fBGHandlerRP[fiCut]->GetMultiplicityBinIndex(fGammaCandidates->GetEntries());
1716                      }
1717                   }
1718                   Double_t sparesFill[4] = {pi0cand->M(),pi0cand->Pt(),(Double_t)zbin,(Double_t)mbin};
1719                   sESDMotherInvMassPtZM[fiCut]->Fill(sparesFill,1);
1720                }
1721                
1722
1723                if(fIsMC){
1724                   if(fInputEvent->IsA()==AliESDEvent::Class())
1725                      ProcessTrueMesonCandidates(pi0cand,gamma0,gamma1);
1726                   if(fInputEvent->IsA()==AliAODEvent::Class())
1727                      ProcessTrueMesonCandidatesAOD(pi0cand,gamma0,gamma1);
1728                }
1729                if (fDoMesonQA == 2){
1730                   fInvMass = pi0cand->M();
1731                   fPt  = pi0cand->Pt();
1732                   if (abs(gamma0->GetDCAzToPrimVtx()) < abs(gamma1->GetDCAzToPrimVtx())){
1733                      fDCAzGammaMin = gamma0->GetDCAzToPrimVtx();
1734                      fDCAzGammaMax = gamma1->GetDCAzToPrimVtx();
1735                   } else {
1736                      fDCAzGammaMin = gamma1->GetDCAzToPrimVtx();
1737                      fDCAzGammaMax = gamma0->GetDCAzToPrimVtx();
1738                   }
1739                   iFlag = pi0cand->GetMesonQuality();
1740 //                   cout << "gamma 0: " << gamma0->GetV0Index()<< "\t" << gamma0->GetPx() << "\t" << gamma0->GetPy() << "\t" <<  gamma0->GetPz() << "\t" << endl; 
1741 //                   cout << "gamma 1: " << gamma1->GetV0Index()<< "\t"<< gamma1->GetPx() << "\t" << gamma1->GetPy() << "\t" <<  gamma1->GetPz() << "\t" << endl; 
1742 //                    cout << "pi0: "<<fInvMass << "\t" << fPt <<"\t" << fDCAzGammaMin << "\t" << fDCAzGammaMax << "\t" << (Int_t)iFlag << "\t" << (Int_t)iMesonMCInfo <<endl;
1743                   if (fIsHeavyIon == 1 && fPt > 0.399 && fPt < 20. ) {
1744                      if (fInvMass > 0.08 && fInvMass < 0.2) tESDMesonsInvMassPtDcazMinDcazMaxFlag[fiCut]->Fill();
1745                      if ((fInvMass > 0.45 && fInvMass < 0.6) &&  (fPt > 0.999 && fPt < 20.) )tESDMesonsInvMassPtDcazMinDcazMaxFlag[fiCut]->Fill();
1746                   } else if (fPt > 0.299 && fPt < 20. )  {
1747                      if ( (fInvMass > 0.08 && fInvMass < 0.2) || (fInvMass > 0.45 && fInvMass < 0.6)) tESDMesonsInvMassPtDcazMinDcazMaxFlag[fiCut]->Fill();
1748                   }   
1749                }
1750             }
1751             delete pi0cand;
1752             pi0cand=0x0;
1753          }
1754       }
1755    }
1756 }
1757 //______________________________________________________________________
1758 void AliAnalysisTaskGammaConvV1::ProcessTrueMesonCandidates(AliAODConversionMother *Pi0Candidate, AliAODConversionPhoton *TrueGammaCandidate0, AliAODConversionPhoton *TrueGammaCandidate1)
1759 {
1760    // Process True Mesons
1761    AliStack *MCStack = fMCEvent->Stack();
1762    iMesonMCInfo = 0;
1763    if(TrueGammaCandidate0->GetV0Index()<fInputEvent->GetNumberOfV0s()){
1764       Bool_t isTruePi0 = kFALSE;
1765       Bool_t isTrueEta = kFALSE;
1766       Bool_t isTruePi0Dalitz = kFALSE;
1767       Bool_t isTrueEtaDalitz = kFALSE;
1768       Bool_t gamma0DalitzCand = kFALSE;
1769       Bool_t gamma1DalitzCand = kFALSE;
1770       Int_t gamma0MCLabel = TrueGammaCandidate0->GetMCParticleLabel(MCStack);
1771       Int_t gamma0MotherLabel = -1;
1772       if(gamma0MCLabel != -1){ // Gamma is Combinatorial; MC Particles don't belong to the same Mother
1773          // Daughters Gamma 0
1774          TParticle * negativeMC = (TParticle*)TrueGammaCandidate0->GetNegativeMCDaughter(MCStack);
1775          TParticle * positiveMC = (TParticle*)TrueGammaCandidate0->GetPositiveMCDaughter(MCStack);
1776          TParticle * gammaMC0 = (TParticle*)MCStack->Particle(gamma0MCLabel);
1777          if(abs(negativeMC->GetPdgCode())==11 && abs(positiveMC->GetPdgCode())==11){  // Electrons ...
1778             if(negativeMC->GetUniqueID() == 5 && positiveMC->GetUniqueID() ==5){ // ... From Conversion ...
1779                if(gammaMC0->GetPdgCode() == 22){ // ... with Gamma Mother
1780                   gamma0MotherLabel=gammaMC0->GetFirstMother();
1781                }
1782             }
1783             if(gammaMC0->GetPdgCode() ==111){ // Dalitz candidate
1784                gamma0DalitzCand = kTRUE;
1785                gamma0MotherLabel=-111;
1786             }
1787             if(gammaMC0->GetPdgCode() ==221){ // Dalitz candidate
1788                gamma0DalitzCand = kTRUE;
1789                gamma0MotherLabel=-221;
1790             }
1791          }
1792       }
1793       if(TrueGammaCandidate1->GetV0Index()<fInputEvent->GetNumberOfV0s()){
1794          Int_t gamma1MCLabel = TrueGammaCandidate1->GetMCParticleLabel(MCStack);
1795          Int_t gamma1MotherLabel = -1;
1796          if(gamma1MCLabel != -1){ // Gamma is Combinatorial; MC Particles don't belong to the same Mother
1797             // Daughters Gamma 1
1798             TParticle * negativeMC = (TParticle*)TrueGammaCandidate1->GetNegativeMCDaughter(MCStack);
1799             TParticle * positiveMC = (TParticle*)TrueGammaCandidate1->GetPositiveMCDaughter(MCStack);
1800             TParticle * gammaMC1 = (TParticle*)MCStack->Particle(gamma1MCLabel);
1801             if(abs(negativeMC->GetPdgCode())==11 && abs(positiveMC->GetPdgCode())==11){  // Electrons ...
1802                if(negativeMC->GetUniqueID() == 5 && positiveMC->GetUniqueID() ==5){ // ... From Conversion ...
1803                   if(gammaMC1->GetPdgCode() == 22){ // ... with Gamma Mother
1804                      gamma1MotherLabel=gammaMC1->GetFirstMother();
1805                   }
1806                }
1807                if(gammaMC1->GetPdgCode() ==111 ){ // Dalitz candidate
1808                  gamma1DalitzCand = kTRUE;
1809                  gamma1MotherLabel=-111;
1810                }
1811                if(gammaMC1->GetPdgCode() ==221){ // Dalitz candidate
1812                  gamma1DalitzCand = kTRUE;
1813                  gamma1MotherLabel=-221;
1814                }
1815             }
1816          }
1817          if(gamma0MotherLabel>=0 && gamma0MotherLabel==gamma1MotherLabel){
1818             if(((TParticle*)MCStack->Particle(gamma1MotherLabel))->GetPdgCode() == 111){
1819                isTruePi0=kTRUE;
1820             }
1821             if(((TParticle*)MCStack->Particle(gamma1MotherLabel))->GetPdgCode() == 221){
1822                isTrueEta=kTRUE;
1823             }
1824          }
1825          
1826          //Identify Dalitz candidate
1827          if (gamma1DalitzCand || gamma0DalitzCand){
1828             if (gamma0DalitzCand && gamma0MCLabel >=0 && gamma0MCLabel==gamma1MotherLabel){
1829                if (gamma0MotherLabel == -111) isTruePi0Dalitz = kTRUE;
1830                if (gamma0MotherLabel == -221) isTrueEtaDalitz = kTRUE;   
1831             }   
1832             if (gamma1DalitzCand && gamma1MCLabel >=0 && gamma1MCLabel==gamma0MotherLabel){
1833                if (gamma1MotherLabel == -111) isTruePi0Dalitz = kTRUE;
1834                if (gamma1MotherLabel == -221) isTrueEtaDalitz = kTRUE;   
1835             }
1836          }
1837          
1838          
1839          if(isTruePi0 || isTrueEta){// True Pion or Eta
1840             hESDTrueMotherInvMassPt[fiCut]->Fill(Pi0Candidate->M(),Pi0Candidate->Pt()); 
1841             if (fDoMesonQA > 0){
1842                if (isTruePi0){
1843                   if ( Pi0Candidate->M() > 0.05 && Pi0Candidate->M() < 0.17){
1844                      hESDTruePi0PtY[fiCut]->Fill(Pi0Candidate->Pt(),Pi0Candidate->Rapidity()-((AliConversionCuts*)fCutArray->At(fiCut))->GetEtaShift()); 
1845                      hESDTruePi0PtAlpha[fiCut]->Fill(Pi0Candidate->Pt(),Pi0Candidate->GetAlpha()); 
1846                      hESDTruePi0PtOpenAngle[fiCut]->Fill(Pi0Candidate->Pt(),Pi0Candidate->GetOpeningAngle()); 
1847                   }
1848                } else if (isTrueEta){   
1849                   if ( Pi0Candidate->M() > 0.45 && Pi0Candidate->M() < 0.65){
1850                      hESDTrueEtaPtY[fiCut]->Fill(Pi0Candidate->Pt(),Pi0Candidate->Rapidity()-((AliConversionCuts*)fCutArray->At(fiCut))->GetEtaShift()); 
1851                      hESDTrueEtaPtAlpha[fiCut]->Fill(Pi0Candidate->Pt(),Pi0Candidate->GetAlpha()); 
1852                      hESDTrueEtaPtOpenAngle[fiCut]->Fill(Pi0Candidate->Pt(),Pi0Candidate->GetOpeningAngle()); 
1853                   }
1854                }
1855             }   
1856             if(gamma0MotherLabel >= MCStack->GetNprimary()){ // Secondary Meson
1857                Int_t secMotherLabel = ((TParticle*)MCStack->Particle(gamma1MotherLabel))->GetMother(0);
1858                Float_t weightedSec= 1;
1859                if(((AliConversionCuts*)fCutArray->At(fiCut))->IsParticleFromBGEvent(secMotherLabel, fMCStack, fInputEvent) && MCStack->Particle(secMotherLabel)->GetPdgCode()==310){
1860                   weightedSec= ((AliConversionCuts*)fCutArray->At(fiCut))->GetWeightForMeson(fV0Reader->GetPeriodName(),secMotherLabel, fMCStack, fInputEvent)/2.; //invariant mass is additive thus the weight for the daughters has to be devide by two for the K0s at a certain pt
1861                   //cout << "MC input \t"<<i << "\t" <<  particle->Pt()<<"\t"<<weighted << endl;
1862                }
1863                hESDTrueSecondaryMotherInvMassPt[fiCut]->Fill(Pi0Candidate->M(),Pi0Candidate->Pt(),weightedSec);
1864                iMesonMCInfo = 2;
1865                if (secMotherLabel >-1){
1866                   if(MCStack->Particle(secMotherLabel)->GetPdgCode()==310){
1867                      iMesonMCInfo = 4;
1868                      hESDTrueSecondaryMotherFromK0sInvMassPt[fiCut]->Fill(Pi0Candidate->M(),Pi0Candidate->Pt(),weightedSec);
1869                      if (fDoMesonQA > 0)hESDTrueK0sWithPi0DaughterMCPt[fiCut]
1870                                        ->Fill(MCStack->Particle(secMotherLabel)->Pt());
1871                   }
1872                   if(MCStack->Particle(secMotherLabel)->GetPdgCode()==221){
1873                      iMesonMCInfo = 3;
1874                      hESDTrueSecondaryMotherFromEtaInvMassPt[fiCut]->Fill(Pi0Candidate->M(),Pi0Candidate->Pt(),weightedSec);
1875                      if (fDoMesonQA > 0)hESDTrueEtaWithPi0DaughterMCPt[fiCut]
1876                                        ->Fill(MCStack->Particle(secMotherLabel)->Pt());
1877                   }
1878                }
1879             }else{ // Only primary pi0 for efficiency calculation
1880                iMesonMCInfo = 6;
1881                Float_t weighted= 1;
1882                if(((AliConversionCuts*)fCutArray->At(fiCut))->IsParticleFromBGEvent(gamma1MotherLabel, fMCStack, fInputEvent)){
1883                   if (((TParticle*)MCStack->Particle(gamma1MotherLabel))->Pt()>0.005){
1884                      weighted= ((AliConversionCuts*)fCutArray->At(fiCut))->GetWeightForMeson(fV0Reader->GetPeriodName(),gamma1MotherLabel, fMCStack, fInputEvent);
1885 //                      cout << "rec \t " <<gamma1MotherLabel << "\t" <<  weighted << endl;
1886                   }
1887                }
1888                hESDTruePrimaryMotherInvMassPt[fiCut]->Fill(Pi0Candidate->M(),Pi0Candidate->Pt(),weighted);
1889                hESDTruePrimaryMotherW0WeightingInvMassPt[fiCut]->Fill(Pi0Candidate->M(),Pi0Candidate->Pt());
1890                pESDTruePrimaryMotherWeightsInvMassPt[fiCut]->Fill(Pi0Candidate->M(),Pi0Candidate->Pt(),weighted);
1891               
1892                
1893                if (fDoMesonQA > 0){
1894                   if(isTruePi0){ // Only primary pi0 for resolution
1895                      hESDTruePrimaryPi0MCPtResolPt[fiCut]->Fill(((TParticle*)MCStack->Particle(gamma1MotherLabel))->Pt(),(Pi0Candidate->Pt()-((TParticle*)MCStack->Particle(gamma1MotherLabel))->Pt())/((TParticle*)MCStack->Particle(gamma1MotherLabel))->Pt(),weighted);
1896                   }
1897                   if (isTrueEta){ // Only primary eta for resolution
1898                      hESDTruePrimaryEtaMCPtResolPt[fiCut]->Fill(((TParticle*)MCStack->Particle(gamma1MotherLabel))->Pt(),(Pi0Candidate->Pt()-((TParticle*)MCStack->Particle(gamma1MotherLabel))->Pt())/((TParticle*)MCStack->Particle(gamma1MotherLabel))->Pt(),weighted);
1899                   }
1900                }
1901             }
1902          } else if(!isTruePi0 && !isTrueEta){ // Background
1903             if (fDoMesonQA > 0){
1904                if(gamma0MotherLabel>-1 && gamma1MotherLabel>-1){ // Both Tracks are Photons and have a mother but not Pi0 or Eta
1905                   hESDTrueBckGGInvMassPt[fiCut]->Fill(Pi0Candidate->M(),Pi0Candidate->Pt());
1906                   iMesonMCInfo = 1;
1907                } else { // No photon or without mother
1908                   hESDTrueBckContInvMassPt[fiCut]->Fill(Pi0Candidate->M(),Pi0Candidate->Pt());
1909                }
1910             }
1911             if( isTruePi0Dalitz || isTrueEtaDalitz ){
1912                // Dalitz
1913                iMesonMCInfo = 5;
1914                hESDTrueMotherDalitzInvMassPt[fiCut]->Fill(Pi0Candidate->M(),Pi0Candidate->Pt());
1915             } else if (gamma0DalitzCand || gamma1DalitzCand){
1916                if (fDoMesonQA > 0)hESDTrueBckContInvMassPt[fiCut]->Fill(Pi0Candidate->M(),Pi0Candidate->Pt());
1917             }   
1918          }
1919       }
1920    }
1921 }
1922 //______________________________________________________________________
1923 void AliAnalysisTaskGammaConvV1::ProcessTrueMesonCandidatesAOD(AliAODConversionMother *Pi0Candidate, AliAODConversionPhoton *TrueGammaCandidate0, AliAODConversionPhoton *TrueGammaCandidate1)
1924 {
1925
1926    // Process True Mesons
1927    TClonesArray *AODMCTrackArray = dynamic_cast<TClonesArray*>(fInputEvent->FindListObject(AliAODMCParticle::StdBranchName()));
1928    Bool_t isTruePi0 = kFALSE;
1929    Bool_t isTrueEta = kFALSE;
1930    Bool_t isTruePi0Dalitz = kFALSE;
1931    Bool_t isTrueEtaDalitz = kFALSE;
1932    Bool_t gamma0DalitzCand = kFALSE;
1933    Bool_t gamma1DalitzCand = kFALSE;
1934    
1935    AliAODMCParticle *positiveMC = static_cast<AliAODMCParticle*>(AODMCTrackArray->At(TrueGammaCandidate0->GetMCLabelPositive()));
1936    AliAODMCParticle *negativeMC = static_cast<AliAODMCParticle*>(AODMCTrackArray->At(TrueGammaCandidate0->GetMCLabelNegative()));
1937
1938    iMesonMCInfo = 0;
1939    Int_t gamma0MCLabel = -1;
1940    Int_t gamma0MotherLabel = -1;
1941    if(!positiveMC||!negativeMC)
1942       return;
1943    
1944    if(positiveMC->GetMother()>-1&&(negativeMC->GetMother() == positiveMC->GetMother())){
1945       gamma0MCLabel = positiveMC->GetMother();
1946    }
1947
1948    if(gamma0MCLabel != -1){ // Gamma is Combinatorial; MC Particles don't belong to the same Mother
1949       // Daughters Gamma 0
1950       AliAODMCParticle * gammaMC0 = static_cast<AliAODMCParticle*>(AODMCTrackArray->At(gamma0MCLabel));
1951       if(abs(negativeMC->GetPdgCode())==11 && abs(positiveMC->GetPdgCode())==11){  // Electrons ...
1952          if(((positiveMC->GetMCProcessCode())) == 5 && ((negativeMC->GetMCProcessCode())) == 5){ // ... From Conversion ...     
1953             if(gammaMC0->GetPdgCode() == 22){ // ... with Gamma Mother
1954                gamma0MotherLabel=gammaMC0->GetMother();
1955             }
1956          }
1957          if(gammaMC0->GetPdgCode() ==111){ // Dalitz candidate
1958             gamma0DalitzCand = kTRUE;
1959             gamma0MotherLabel=-111;
1960          }
1961          if(gammaMC0->GetPdgCode() ==221){ // Dalitz candidate
1962             gamma0DalitzCand = kTRUE;
1963             gamma0MotherLabel=-221;
1964          }
1965       }
1966    }
1967    positiveMC = static_cast<AliAODMCParticle*>(AODMCTrackArray->At(TrueGammaCandidate1->GetMCLabelPositive()));
1968    negativeMC = static_cast<AliAODMCParticle*>(AODMCTrackArray->At(TrueGammaCandidate1->GetMCLabelNegative()));
1969    
1970    Int_t gamma1MCLabel = -1;
1971    Int_t gamma1MotherLabel = -1;
1972    if(!positiveMC||!negativeMC)
1973       return;
1974    
1975    if(positiveMC->GetMother()>-1&&(negativeMC->GetMother() == positiveMC->GetMother())){
1976       gamma1MCLabel = positiveMC->GetMother();
1977    }
1978    if(gamma1MCLabel != -1){ // Gamma is Combinatorial; MC Particles don't belong to the same Mother
1979       // Daughters Gamma 1
1980       AliAODMCParticle * gammaMC1 = static_cast<AliAODMCParticle*>(AODMCTrackArray->At(gamma1MCLabel));
1981       if(abs(negativeMC->GetPdgCode())==11 && abs(positiveMC->GetPdgCode())==11){  // Electrons ...
1982          if(((positiveMC->GetMCProcessCode())) == 5 && ((negativeMC->GetMCProcessCode())) == 5){ // ... From Conversion ...     
1983             if(gammaMC1->GetPdgCode() == 22){ // ... with Gamma Mother
1984                gamma1MotherLabel=gammaMC1->GetMother();
1985             }
1986          }
1987          if(gammaMC1->GetPdgCode() ==111 ){ // Dalitz candidate
1988                  gamma1DalitzCand = kTRUE;
1989                  gamma1MotherLabel=-111;
1990          }
1991          if(gammaMC1->GetPdgCode() ==221){ // Dalitz candidate
1992             gamma1DalitzCand = kTRUE;
1993             gamma1MotherLabel=-221;
1994          }
1995       }
1996    }
1997    if(gamma0MotherLabel>=0 && gamma0MotherLabel==gamma1MotherLabel){
1998       if(static_cast<AliAODMCParticle*>(AODMCTrackArray->At(gamma1MotherLabel))->GetPdgCode() == 111){
1999          isTruePi0=kTRUE;
2000       }
2001       if(static_cast<AliAODMCParticle*>(AODMCTrackArray->At(gamma1MotherLabel))->GetPdgCode() == 221){
2002          isTrueEta=kTRUE;
2003       }
2004    }
2005    
2006    //Identify Dalitz candidate
2007    if (gamma1DalitzCand || gamma0DalitzCand){
2008       if (gamma0DalitzCand && gamma0MCLabel >=0 && gamma0MCLabel==gamma1MotherLabel){
2009          if (gamma0MotherLabel == -111) isTruePi0Dalitz = kTRUE;
2010          if (gamma0MotherLabel == -221) isTrueEtaDalitz = kTRUE;   
2011       }   
2012       if (gamma1DalitzCand && gamma1MCLabel >=0 && gamma1MCLabel==gamma0MotherLabel){
2013          if (gamma1MotherLabel == -111) isTruePi0Dalitz = kTRUE;
2014          if (gamma1MotherLabel == -221) isTrueEtaDalitz = kTRUE;   
2015       }
2016    }
2017             
2018    if(isTruePi0 || isTrueEta){// True Pion or Eta
2019       hESDTrueMotherInvMassPt[fiCut]->Fill(Pi0Candidate->M(),Pi0Candidate->Pt());
2020       if (fDoMesonQA > 0){
2021          if (isTruePi0){
2022             if ( Pi0Candidate->M() > 0.05 && Pi0Candidate->M() < 0.17){
2023                hESDTruePi0PtY[fiCut]->Fill(Pi0Candidate->Pt(),Pi0Candidate->Rapidity()-((AliConversionCuts*)fCutArray->At(fiCut))->GetEtaShift());
2024                hESDTruePi0PtAlpha[fiCut]->Fill(Pi0Candidate->Pt(),Pi0Candidate->GetAlpha()); 
2025                hESDTruePi0PtOpenAngle[fiCut]->Fill(Pi0Candidate->Pt(),Pi0Candidate->GetOpeningAngle()); 
2026             }
2027          } else if (isTrueEta){   
2028             if ( Pi0Candidate->M() > 0.45 && Pi0Candidate->M() < 0.65){
2029                hESDTrueEtaPtY[fiCut]->Fill(Pi0Candidate->Pt(),Pi0Candidate->Rapidity()-((AliConversionCuts*)fCutArray->At(fiCut))->GetEtaShift()); 
2030                hESDTrueEtaPtAlpha[fiCut]->Fill(Pi0Candidate->Pt(),Pi0Candidate->GetAlpha()); 
2031                hESDTrueEtaPtOpenAngle[fiCut]->Fill(Pi0Candidate->Pt(),Pi0Candidate->GetOpeningAngle()); 
2032             }
2033          }
2034       }
2035       if(!(static_cast<AliAODMCParticle*>(AODMCTrackArray->At(gamma0MotherLabel))->IsPrimary())){ // Secondary Meson
2036          Int_t secMotherLabel = static_cast<AliAODMCParticle*>(AODMCTrackArray->At(gamma1MotherLabel))->GetMother();
2037          Float_t weightedSec= 1;
2038          if(((AliConversionCuts*)fCutArray->At(fiCut))->IsParticleFromBGEvent(secMotherLabel, 0x0, fInputEvent) && static_cast<AliAODMCParticle*>(AODMCTrackArray->At(secMotherLabel))->GetPdgCode()==310){
2039             weightedSec= ((AliConversionCuts*)fCutArray->At(fiCut))->GetWeightForMeson(fV0Reader->GetPeriodName(),secMotherLabel, 0x0, fInputEvent)/2.; //invariant mass is additive thus the weight for the daughters has to be devide by two for the K0s at a certain pt
2040             //cout << "MC input \t"<<i << "\t" <<  particle->Pt()<<"\t"<<weighted << endl;
2041          }
2042          hESDTrueSecondaryMotherInvMassPt[fiCut]->Fill(Pi0Candidate->M(),Pi0Candidate->Pt(),weightedSec);
2043          iMesonMCInfo = 2;   
2044          if (secMotherLabel >-1){
2045             if(static_cast<AliAODMCParticle*>(AODMCTrackArray->At(secMotherLabel))->GetPdgCode()==310){
2046                iMesonMCInfo = 4;
2047                hESDTrueSecondaryMotherFromK0sInvMassPt[fiCut]->Fill(Pi0Candidate->M(),Pi0Candidate->Pt(),weightedSec);
2048                if (fDoMesonQA > 0)hESDTrueK0sWithPi0DaughterMCPt[fiCut]
2049                                  ->Fill(static_cast<AliAODMCParticle*>(AODMCTrackArray->At(secMotherLabel))->Pt());
2050             }
2051             if(static_cast<AliAODMCParticle*>(AODMCTrackArray->At(secMotherLabel))->GetPdgCode()==221){
2052                iMesonMCInfo = 3;
2053                hESDTrueSecondaryMotherFromEtaInvMassPt[fiCut]->Fill(Pi0Candidate->M(),Pi0Candidate->Pt(),weightedSec);
2054                if (fDoMesonQA > 0)hESDTrueEtaWithPi0DaughterMCPt[fiCut]
2055                                  ->Fill(static_cast<AliAODMCParticle*>(AODMCTrackArray->At(secMotherLabel))->Pt());
2056             }
2057          }
2058       }else{ // Only primary pi0 for efficiency calculation
2059          Float_t weighted= 1;
2060          iMesonMCInfo = 6;
2061          if(((AliConversionCuts*)fCutArray->At(fiCut))->IsParticleFromBGEvent(gamma1MotherLabel, 0x0, fInputEvent)){
2062             if (static_cast<AliAODMCParticle*>(AODMCTrackArray->At(gamma1MotherLabel))->Pt()>0.005){
2063                weighted= ((AliConversionCuts*)fCutArray->At(fiCut))->GetWeightForMeson(fV0Reader->GetPeriodName(),gamma1MotherLabel, 0x0, fInputEvent);
2064                //                      cout << "rec \t " <<gamma1MotherLabel << "\t" <<  weighted << endl;
2065             }
2066          }
2067          hESDTruePrimaryMotherInvMassPt[fiCut]->Fill(Pi0Candidate->M(),Pi0Candidate->Pt(),weighted);
2068          hESDTruePrimaryMotherW0WeightingInvMassPt[fiCut]->Fill(Pi0Candidate->M(),Pi0Candidate->Pt());
2069          pESDTruePrimaryMotherWeightsInvMassPt[fiCut]->Fill(Pi0Candidate->M(),Pi0Candidate->Pt(),weighted);
2070               
2071          if (fDoMesonQA > 0){
2072             if(isTruePi0){ // Only primary pi0 for resolution
2073                hESDTruePrimaryPi0MCPtResolPt[fiCut]->Fill(static_cast<AliAODMCParticle*>(AODMCTrackArray->At(gamma1MotherLabel))->Pt(),
2074                                                           (Pi0Candidate->Pt()-static_cast<AliAODMCParticle*>(AODMCTrackArray->At(gamma1MotherLabel))->Pt())/static_cast<AliAODMCParticle*>(AODMCTrackArray->At(gamma1MotherLabel))->Pt(),weighted);
2075                
2076             }
2077             if (isTrueEta){ // Only primary eta for resolution
2078                hESDTruePrimaryEtaMCPtResolPt[fiCut]->Fill(static_cast<AliAODMCParticle*>(AODMCTrackArray->At(gamma1MotherLabel))->Pt(),
2079                                                           (Pi0Candidate->Pt()-static_cast<AliAODMCParticle*>(AODMCTrackArray->At(gamma1MotherLabel))->Pt())/static_cast<AliAODMCParticle*>(AODMCTrackArray->At(gamma1MotherLabel))->Pt(),weighted);
2080             }
2081          }
2082       }
2083    } else if(!isTruePi0 && !isTrueEta) { // Background
2084       if (fDoMesonQA > 0){
2085          if(gamma0MotherLabel>-1 && gamma1MotherLabel>-1){ // Both Tracks are Photons and have a mother but not Pi0 or Eta
2086             hESDTrueBckGGInvMassPt[fiCut]->Fill(Pi0Candidate->M(),Pi0Candidate->Pt());
2087             iMesonMCInfo = 1;
2088          } else { // No photon or without mother
2089             hESDTrueBckContInvMassPt[fiCut]->Fill(Pi0Candidate->M(),Pi0Candidate->Pt());
2090          }
2091       }
2092       if( isTruePi0Dalitz || isTrueEtaDalitz ){
2093          // Dalitz
2094          iMesonMCInfo = 5;
2095          hESDTrueMotherDalitzInvMassPt[fiCut]->Fill(Pi0Candidate->M(),Pi0Candidate->Pt());
2096       } else if (gamma0DalitzCand || gamma1DalitzCand){
2097          if (fDoMesonQA > 0)hESDTrueBckContInvMassPt[fiCut]->Fill(Pi0Candidate->M(),Pi0Candidate->Pt());
2098       }
2099    }
2100 }
2101 //________________________________________________________________________
2102 void AliAnalysisTaskGammaConvV1::CalculateBackground(){
2103
2104    Int_t zbin= fBGHandler[fiCut]->GetZBinIndex(fInputEvent->GetPrimaryVertex()->GetZ());
2105    Int_t mbin = 0;
2106
2107    if(((AliConversionMesonCuts*)fMesonCutArray->At(fiCut))->UseTrackMultiplicity()){
2108       mbin = fBGHandler[fiCut]->GetMultiplicityBinIndex(fV0Reader->GetNumberOfPrimaryTracks());
2109    } else {
2110       mbin = fBGHandler[fiCut]->GetMultiplicityBinIndex(fGammaCandidates->GetEntries());
2111    }
2112
2113    if(((AliConversionMesonCuts*)fMesonCutArray->At(fiCut))->UseRotationMethod()){
2114
2115       for(Int_t iCurrent=0;iCurrent<fGammaCandidates->GetEntries();iCurrent++){
2116          AliAODConversionPhoton currentEventGoodV0 = *(AliAODConversionPhoton*)(fGammaCandidates->At(iCurrent));
2117          for(Int_t iCurrent2=iCurrent+1;iCurrent2<fGammaCandidates->GetEntries();iCurrent2++){
2118             for(Int_t nRandom=0;nRandom<((AliConversionMesonCuts*)fMesonCutArray->At(fiCut))->GetNumberOfBGEvents();nRandom++){
2119                AliAODConversionPhoton currentEventGoodV02 = *(AliAODConversionPhoton*)(fGammaCandidates->At(iCurrent2));
2120
2121                if(((AliConversionMesonCuts*)fMesonCutArray->At(fiCut))->DoBGProbability()){
2122                   AliAODConversionMother *backgroundCandidateProb = new AliAODConversionMother(&currentEventGoodV0,&currentEventGoodV02);
2123                   Double_t massBGprob = backgroundCandidateProb->M();
2124                   if(massBGprob>0.1 && massBGprob<0.14){
2125                      if(fRandom.Rndm()>fBGHandler[fiCut]->GetBGProb(zbin,mbin)){
2126                         delete backgroundCandidateProb;
2127                         continue;
2128                      }
2129                   }
2130                   delete backgroundCandidateProb;
2131                   backgroundCandidateProb = 0x0;
2132                }
2133
2134                RotateParticle(&currentEventGoodV02);
2135                AliAODConversionMother *backgroundCandidate = new AliAODConversionMother(&currentEventGoodV0,&currentEventGoodV02);
2136                backgroundCandidate->CalculateDistanceOfClossetApproachToPrimVtx(fInputEvent->GetPrimaryVertex());
2137                if((((AliConversionMesonCuts*)fMesonCutArray->At(fiCut))
2138                    ->MesonIsSelected(backgroundCandidate,kFALSE,((AliConversionCuts*)fCutArray->At(fiCut))->GetEtaShift()))){
2139                   hESDMotherBackInvMassPt[fiCut]->Fill(backgroundCandidate->M(),backgroundCandidate->Pt());
2140                   Double_t sparesFill[4] = {backgroundCandidate->M(),backgroundCandidate->Pt(),(Double_t)zbin,(Double_t)mbin};
2141                   sESDMotherBackInvMassPtZM[fiCut]->Fill(sparesFill,1);
2142                }
2143                delete backgroundCandidate;
2144                backgroundCandidate = 0x0;
2145             }
2146          }
2147       }
2148    }else{
2149       AliGammaConversionAODBGHandler::GammaConversionVertex *bgEventVertex = NULL;
2150
2151       if(((AliConversionMesonCuts*)fMesonCutArray->At(fiCut))->UseTrackMultiplicity()){
2152          for(Int_t nEventsInBG=0;nEventsInBG<fBGHandler[fiCut]->GetNBGEvents();nEventsInBG++){
2153             AliGammaConversionAODVector *previousEventV0s = fBGHandler[fiCut]->GetBGGoodV0s(zbin,mbin,nEventsInBG);
2154             if(fMoveParticleAccordingToVertex == kTRUE){
2155                bgEventVertex = fBGHandler[fiCut]->GetBGEventVertex(zbin,mbin,nEventsInBG);
2156             }
2157
2158             for(Int_t iCurrent=0;iCurrent<fGammaCandidates->GetEntries();iCurrent++){
2159                AliAODConversionPhoton currentEventGoodV0 = *(AliAODConversionPhoton*)(fGammaCandidates->At(iCurrent));
2160                for(UInt_t iPrevious=0;iPrevious<previousEventV0s->size();iPrevious++){
2161                   AliAODConversionPhoton previousGoodV0 = (AliAODConversionPhoton)(*(previousEventV0s->at(iPrevious)));
2162                   if(fMoveParticleAccordingToVertex == kTRUE){
2163                      MoveParticleAccordingToVertex(&previousGoodV0,bgEventVertex);
2164                   }
2165                   if(((AliConversionCuts*)fCutArray->At(fiCut))->GetInPlaneOutOfPlaneCut() != 0){
2166                      RotateParticleAccordingToEP(&previousGoodV0,bgEventVertex->fEP,fEventPlaneAngle);
2167                   }
2168
2169                   AliAODConversionMother *backgroundCandidate = new AliAODConversionMother(&currentEventGoodV0,&previousGoodV0);
2170                   backgroundCandidate->CalculateDistanceOfClossetApproachToPrimVtx(fInputEvent->GetPrimaryVertex());
2171                   if((((AliConversionMesonCuts*)fMesonCutArray->At(fiCut))
2172                       ->MesonIsSelected(backgroundCandidate,kFALSE,((AliConversionCuts*)fCutArray->At(fiCut))->GetEtaShift()))){
2173                      hESDMotherBackInvMassPt[fiCut]->Fill(backgroundCandidate->M(),backgroundCandidate->Pt());
2174                      Double_t sparesFill[4] = {backgroundCandidate->M(),backgroundCandidate->Pt(),(Double_t)zbin,(Double_t)mbin};
2175                      sESDMotherBackInvMassPtZM[fiCut]->Fill(sparesFill,1);
2176                   }
2177                   delete backgroundCandidate;
2178                   backgroundCandidate = 0x0;
2179                }
2180             }
2181          }
2182       }
2183       else{
2184          for(Int_t nEventsInBG=0;nEventsInBG <fBGHandler[fiCut]->GetNBGEvents();nEventsInBG++){
2185             AliGammaConversionAODVector *previousEventV0s = fBGHandler[fiCut]->GetBGGoodV0s(zbin,mbin,nEventsInBG);
2186             if(previousEventV0s){
2187                if(fMoveParticleAccordingToVertex == kTRUE){
2188                   bgEventVertex = fBGHandler[fiCut]->GetBGEventVertex(zbin,mbin,nEventsInBG);
2189                }
2190                for(Int_t iCurrent=0;iCurrent<fGammaCandidates->GetEntries();iCurrent++){
2191                   AliAODConversionPhoton currentEventGoodV0 = *(AliAODConversionPhoton*)(fGammaCandidates->At(iCurrent));
2192                   for(UInt_t iPrevious=0;iPrevious<previousEventV0s->size();iPrevious++){
2193
2194                      AliAODConversionPhoton previousGoodV0 = (AliAODConversionPhoton)(*(previousEventV0s->at(iPrevious)));
2195
2196                      if(fMoveParticleAccordingToVertex == kTRUE){
2197                         MoveParticleAccordingToVertex(&previousGoodV0,bgEventVertex);
2198                      }
2199                      if(((AliConversionCuts*)fCutArray->At(fiCut))->GetInPlaneOutOfPlaneCut() != 0){
2200                         RotateParticleAccordingToEP(&previousGoodV0,bgEventVertex->fEP,fEventPlaneAngle);
2201                      }
2202
2203
2204                      AliAODConversionMother *backgroundCandidate = new AliAODConversionMother(&currentEventGoodV0,&previousGoodV0);
2205                      backgroundCandidate->CalculateDistanceOfClossetApproachToPrimVtx(fInputEvent->GetPrimaryVertex());
2206                      if((((AliConversionMesonCuts*)fMesonCutArray->At(fiCut))
2207                          ->MesonIsSelected(backgroundCandidate,kFALSE,((AliConversionCuts*)fCutArray->At(fiCut))->GetEtaShift()))){
2208                         hESDMotherBackInvMassPt[fiCut]->Fill(backgroundCandidate->M(),backgroundCandidate->Pt());
2209                         Double_t sparesFill[4] = {backgroundCandidate->M(),backgroundCandidate->Pt(),(Double_t)zbin,(Double_t)mbin};
2210                         sESDMotherBackInvMassPtZM[fiCut]->Fill(sparesFill,1);
2211                      }
2212                      delete backgroundCandidate;
2213                      backgroundCandidate = 0x0;
2214                   }
2215                }
2216             }
2217          }
2218       }
2219    }
2220 }
2221 //________________________________________________________________________
2222 void AliAnalysisTaskGammaConvV1::CalculateBackgroundRP(){
2223
2224    Int_t zbin= fBGHandlerRP[fiCut]->GetZBinIndex(fInputEvent->GetPrimaryVertex()->GetZ());
2225    Int_t mbin = 0;
2226    if(((AliConversionMesonCuts*)fMesonCutArray->At(fiCut))->UseTrackMultiplicity()){
2227       mbin = fBGHandlerRP[fiCut]->GetMultiplicityBinIndex(fV0Reader->GetNumberOfPrimaryTracks());
2228    } else {
2229       mbin = fBGHandlerRP[fiCut]->GetMultiplicityBinIndex(fGammaCandidates->GetEntries());
2230    }
2231
2232
2233    //Rotation Method
2234    if(((AliConversionMesonCuts*)fMesonCutArray->At(fiCut))->UseRotationMethod()){
2235       // Correct for the number of rotations
2236       // BG is for rotation the same, except for factor NRotations
2237       Double_t weight=1./Double_t(((AliConversionMesonCuts*)fMesonCutArray->At(fiCut))->GetNumberOfBGEvents());
2238
2239       for(Int_t firstGammaIndex=0;firstGammaIndex<fGammaCandidates->GetEntries();firstGammaIndex++){
2240
2241          AliAODConversionPhoton *gamma0=dynamic_cast<AliAODConversionPhoton*>(fGammaCandidates->At(firstGammaIndex));
2242          if (gamma0==NULL) continue;
2243          for(Int_t secondGammaIndex=firstGammaIndex+1;secondGammaIndex<fGammaCandidates->GetEntries();secondGammaIndex++){
2244             AliAODConversionPhoton *gamma1=dynamic_cast<AliAODConversionPhoton*>(fGammaCandidates->At(secondGammaIndex));
2245             if (gamma1 == NULL) continue;
2246             if(!((AliConversionCuts*)fCutArray->At(fiCut))->PhotonIsSelected(gamma1,fInputEvent))continue;
2247             for(Int_t nRandom=0;nRandom<((AliConversionMesonCuts*)fMesonCutArray->At(fiCut))->GetNumberOfBGEvents();nRandom++){
2248
2249                RotateParticle(gamma1);
2250
2251                AliAODConversionMother backgroundCandidate(gamma0,gamma1);
2252                backgroundCandidate.CalculateDistanceOfClossetApproachToPrimVtx(fInputEvent->GetPrimaryVertex());
2253                if(((AliConversionMesonCuts*)fMesonCutArray->At(fiCut))
2254                   ->MesonIsSelected(&backgroundCandidate,kFALSE,((AliConversionCuts*)fCutArray->At(fiCut))->GetEtaShift())){
2255                   hESDMotherBackInvMassPt[fiCut]->Fill(backgroundCandidate.M(),backgroundCandidate.Pt());
2256                   Double_t sparesFill[4] = {backgroundCandidate.M(),backgroundCandidate.Pt(),(Double_t)zbin,(Double_t)mbin};
2257                   sESDMotherBackInvMassPtZM[fiCut]->Fill(sparesFill,weight);
2258                }
2259             }
2260          }
2261       }
2262    }
2263    else{
2264       // Do Event Mixing
2265       for(Int_t nEventsInBG=0;nEventsInBG <fBGHandlerRP[fiCut]->GetNBGEvents(fGammaCandidates,fInputEvent);nEventsInBG++){
2266
2267          AliGammaConversionPhotonVector *previousEventGammas = fBGHandlerRP[fiCut]->GetBGGoodGammas(fGammaCandidates,fInputEvent,nEventsInBG);
2268
2269          if(previousEventGammas){
2270             // test weighted background
2271             Double_t weight=1.0;
2272             // Correct for the number of eventmixing:
2273             // N gammas -> (N-1) + (N-2) +(N-3) ...+ (N-(N-1))  using sum formula sum(i)=N*(N-1)/2  -> N*(N-1)/2
2274             // real combinations (since you cannot combine a photon with its own)
2275             // but BG leads to N_{a}*N_{b} combinations
2276             weight*=0.5*(Double_t(fGammaCandidates->GetEntries()-1))/Double_t(previousEventGammas->size());
2277
2278             for(Int_t iCurrent=0;iCurrent<fGammaCandidates->GetEntries();iCurrent++){
2279
2280                AliAODConversionPhoton *gamma0 = (AliAODConversionPhoton*)(fGammaCandidates->At(iCurrent));
2281
2282                for(UInt_t iPrevious=0;iPrevious<previousEventGammas->size();iPrevious++){
2283
2284                   AliAODConversionPhoton *gamma1 = (AliAODConversionPhoton*)(previousEventGammas->at(iPrevious));
2285
2286                   AliAODConversionMother backgroundCandidate(gamma0,gamma1);
2287                   backgroundCandidate.CalculateDistanceOfClossetApproachToPrimVtx(fInputEvent->GetPrimaryVertex());
2288                   if(((AliConversionMesonCuts*)fMesonCutArray->At(fiCut))
2289                      ->MesonIsSelected(&backgroundCandidate,kFALSE,((AliConversionCuts*)fCutArray->At(fiCut))->GetEtaShift())){
2290                      hESDMotherBackInvMassPt[fiCut]->Fill(backgroundCandidate.M(),backgroundCandidate.Pt());
2291                      Double_t sparesFill[4] = {backgroundCandidate.M(),backgroundCandidate.Pt(),(Double_t)zbin,(Double_t)mbin};
2292                      sESDMotherBackInvMassPtZM[fiCut]->Fill(sparesFill,weight);
2293                   }
2294                }
2295             }
2296          }
2297       }
2298    }
2299 }
2300 //________________________________________________________________________
2301 void AliAnalysisTaskGammaConvV1::RotateParticle(AliAODConversionPhoton *gamma){
2302    Int_t fNDegreesPMBackground= ((AliConversionMesonCuts*)fMesonCutArray->At(fiCut))->NDegreesRotation();
2303    Double_t nRadiansPM = fNDegreesPMBackground*TMath::Pi()/180;
2304    Double_t rotationValue = fRandom.Rndm()*2*nRadiansPM + TMath::Pi()-nRadiansPM;
2305    gamma->RotateZ(rotationValue);
2306 }
2307
2308 //________________________________________________________________________
2309 void AliAnalysisTaskGammaConvV1::RotateParticleAccordingToEP(AliAODConversionPhoton *gamma, Double_t previousEventEP, Double_t thisEventEP){
2310
2311    previousEventEP=previousEventEP+TMath::Pi();
2312    thisEventEP=thisEventEP+TMath::Pi();
2313    Double_t rotationValue= thisEventEP-previousEventEP;
2314    gamma->RotateZ(rotationValue);
2315 }
2316
2317 //________________________________________________________________________
2318 void AliAnalysisTaskGammaConvV1::MoveParticleAccordingToVertex(AliAODConversionPhoton* particle,const AliGammaConversionAODBGHandler::GammaConversionVertex *vertex){
2319    //see header file for documentation
2320
2321    Double_t dx = vertex->fX - fInputEvent->GetPrimaryVertex()->GetX();
2322    Double_t dy = vertex->fY - fInputEvent->GetPrimaryVertex()->GetY();
2323    Double_t dz = vertex->fZ - fInputEvent->GetPrimaryVertex()->GetZ();
2324
2325    Double_t movedPlace[3] = {particle->GetConversionX() - dx,particle->GetConversionY() - dy,particle->GetConversionZ() - dz};
2326    particle->SetConversionPoint(movedPlace);
2327 }
2328 //________________________________________________________________________
2329 void AliAnalysisTaskGammaConvV1::UpdateEventByEventData(){
2330    //see header file for documentation
2331    if(fGammaCandidates->GetEntries() >0 ){
2332       if(((AliConversionMesonCuts*)fMesonCutArray->At(fiCut))->UseTrackMultiplicity()){
2333          fBGHandler[fiCut]->AddEvent(fGammaCandidates,fInputEvent->GetPrimaryVertex()->GetX(),fInputEvent->GetPrimaryVertex()->GetY(),fInputEvent->GetPrimaryVertex()->GetZ(),fV0Reader->GetNumberOfPrimaryTracks(),fEventPlaneAngle);
2334       }
2335       else{ // means we use #V0s for multiplicity
2336          fBGHandler[fiCut]->AddEvent(fGammaCandidates,fInputEvent->GetPrimaryVertex()->GetX(),fInputEvent->GetPrimaryVertex()->GetY(),fInputEvent->GetPrimaryVertex()->GetZ(),fGammaCandidates->GetEntries(),fEventPlaneAngle);
2337       }
2338    }
2339 }
2340
2341
2342 //________________________________________________________________________
2343 void AliAnalysisTaskGammaConvV1::FillPhotonCombinatorialBackgroundHist(AliAODConversionPhoton *TruePhotonCandidate, Int_t pdgCode[])
2344 {
2345    // Combinatorial Bck = 0 ee, 1 ep,i 2 ek, 3 ep, 4 emu, 5 pipi, 6 pik, 7 pip, 8 pimu, 9 kk, 10 kp, 11 kmu, 12 pp, 13 pmu, 14 mumu, 15 Rest
2346    if(pdgCode[0]==11   && pdgCode[1]==11){if(fIsFromMBHeader)hESDCombinatorialPt[fiCut]->Fill(TruePhotonCandidate->Pt(),0);}
2347    else if( (pdgCode[0]==11   && pdgCode[1]==211) || (pdgCode[0]==211  && pdgCode[1]==11) )
2348       {if(fIsFromMBHeader)hESDCombinatorialPt[fiCut]->Fill(TruePhotonCandidate->Pt(),1);}
2349    else if( (pdgCode[0]==11   && pdgCode[1]==321) || (pdgCode[0]==321  && pdgCode[1]==11) )
2350       {if(fIsFromMBHeader)hESDCombinatorialPt[fiCut]->Fill(TruePhotonCandidate->Pt(),2);}
2351    else if( (pdgCode[0]==11   && pdgCode[1]==2212) || (pdgCode[0]==2212 && pdgCode[1]==11) )
2352       {if(fIsFromMBHeader)hESDCombinatorialPt[fiCut]->Fill(TruePhotonCandidate->Pt(),3);}
2353    else if( (pdgCode[0]==11   && pdgCode[1]==13) || (pdgCode[0]==13   && pdgCode[1]==11) )
2354       {if(fIsFromMBHeader)hESDCombinatorialPt[fiCut]->Fill(TruePhotonCandidate->Pt(),4);}
2355    else if(  pdgCode[0]==211  && pdgCode[1]==211 ){if(fIsFromMBHeader)hESDCombinatorialPt[fiCut]->Fill(TruePhotonCandidate->Pt(),5);}
2356    else if( (pdgCode[0]==211  && pdgCode[1]==321) || (pdgCode[0]==321  && pdgCode[1]==211) )
2357       {if(fIsFromMBHeader)hESDCombinatorialPt[fiCut]->Fill(TruePhotonCandidate->Pt(),6);}
2358    else if( (pdgCode[0]==211  && pdgCode[1]==2212) || (pdgCode[0]==2212 && pdgCode[1]==211) )
2359       {if(fIsFromMBHeader)hESDCombinatorialPt[fiCut]->Fill(TruePhotonCandidate->Pt(),7);}
2360    else if( (pdgCode[0]==211  && pdgCode[1]==13) || (pdgCode[0]==13   && pdgCode[1]==211) )
2361       {if(fIsFromMBHeader)hESDCombinatorialPt[fiCut]->Fill(TruePhotonCandidate->Pt(),8);}
2362    else if(  pdgCode[0]==321  && pdgCode[1]==321 ){if(fIsFromMBHeader)hESDCombinatorialPt[fiCut]->Fill(TruePhotonCandidate->Pt(),9);}
2363    else if( (pdgCode[0]==321  && pdgCode[1]==2212) || (pdgCode[0]==2212 && pdgCode[1]==321) )
2364       {if(fIsFromMBHeader)hESDCombinatorialPt[fiCut]->Fill(TruePhotonCandidate->Pt(),10);}
2365    else if( (pdgCode[0]==321  && pdgCode[1]==13) || (pdgCode[0]==13   && pdgCode[1]==321) )
2366       {if(fIsFromMBHeader)hESDCombinatorialPt[fiCut]->Fill(TruePhotonCandidate->Pt(),11);}
2367    else if(  pdgCode[0]==2212   && pdgCode[1]==2212  ){if(fIsFromMBHeader)hESDCombinatorialPt[fiCut]->Fill(TruePhotonCandidate->Pt(),12);}
2368    else if( (pdgCode[0]==2212  && pdgCode[1]==13) || (pdgCode[0]==13   && pdgCode[1]==2212) )
2369       {if(fIsFromMBHeader)hESDCombinatorialPt[fiCut]->Fill(TruePhotonCandidate->Pt(),13);}
2370    else if(  pdgCode[0]==13   && pdgCode[1]==13  ){if(fIsFromMBHeader)hESDCombinatorialPt[fiCut]->Fill(TruePhotonCandidate->Pt(),14);}
2371    else {if(fIsFromMBHeader)hESDCombinatorialPt[fiCut]->Fill(TruePhotonCandidate->Pt(),15);}
2372 }
2373 //________________________________________________________________________
2374 void AliAnalysisTaskGammaConvV1::RelabelAODPhotonCandidates(Bool_t mode){
2375
2376    // Relabeling For AOD Event
2377    // ESDiD -> AODiD
2378    // MCLabel -> AODMCLabel
2379    
2380    if(mode){
2381       fMCStackPos = new Int_t[fReaderGammas->GetEntries()];
2382       fMCStackNeg = new Int_t[fReaderGammas->GetEntries()];
2383       fESDArrayPos = new Int_t[fReaderGammas->GetEntries()];
2384       fESDArrayNeg = new Int_t[fReaderGammas->GetEntries()];
2385    }
2386    
2387    for(Int_t iGamma = 0;iGamma<fReaderGammas->GetEntries();iGamma++){
2388       AliAODConversionPhoton* PhotonCandidate = (AliAODConversionPhoton*) fReaderGammas->At(iGamma);
2389       if(!PhotonCandidate) continue;
2390       if(!mode){// Back to ESD Labels
2391          PhotonCandidate->SetMCLabelPositive(fMCStackPos[iGamma]);
2392          PhotonCandidate->SetMCLabelNegative(fMCStackNeg[iGamma]);
2393          PhotonCandidate->SetLabelPositive(fESDArrayPos[iGamma]);
2394          PhotonCandidate->SetLabelNegative(fESDArrayNeg[iGamma]);
2395          continue;
2396       }
2397       fMCStackPos[iGamma] =  PhotonCandidate->GetMCLabelPositive();
2398       fMCStackNeg[iGamma] =  PhotonCandidate->GetMCLabelNegative();
2399       fESDArrayPos[iGamma] = PhotonCandidate->GetTrackLabelPositive();
2400       fESDArrayNeg[iGamma] = PhotonCandidate->GetTrackLabelNegative();
2401
2402       Bool_t AODLabelPos = kFALSE;
2403       Bool_t AODLabelNeg = kFALSE;
2404
2405       for(Int_t i = 0; i<fInputEvent->GetNumberOfTracks();i++){
2406          AliAODTrack *tempDaughter = static_cast<AliAODTrack*>(fInputEvent->GetTrack(i));
2407          if(!AODLabelPos){
2408             if( tempDaughter->GetID() == PhotonCandidate->GetTrackLabelPositive() ){
2409                PhotonCandidate->SetMCLabelPositive(abs(tempDaughter->GetLabel()));
2410                PhotonCandidate->SetLabelPositive(i);
2411                AODLabelPos = kTRUE;
2412             }
2413          }
2414          if(!AODLabelNeg){
2415             if( tempDaughter->GetID() == PhotonCandidate->GetTrackLabelNegative()){
2416                PhotonCandidate->SetMCLabelNegative(abs(tempDaughter->GetLabel()));
2417                PhotonCandidate->SetLabelNegative(i);
2418                AODLabelNeg = kTRUE;
2419             }
2420          }
2421          if(AODLabelNeg && AODLabelPos){
2422             break;
2423          }
2424       }
2425       if(!AODLabelPos || !AODLabelNeg){
2426          cout<<"WARNING!!! AOD TRACKS NOT FOUND FOR"<<endl;
2427       }
2428    }
2429    
2430    
2431    if(!mode){
2432       delete[] fMCStackPos;
2433       delete[] fMCStackNeg;
2434       delete[] fESDArrayPos;
2435       delete[] fESDArrayNeg;
2436    }
2437 }
2438
2439 void AliAnalysisTaskGammaConvV1::SetLogBinningXTH2(TH2* histoRebin){
2440    TAxis *axisafter = histoRebin->GetXaxis(); 
2441    Int_t bins = axisafter->GetNbins();
2442    Double_t from = axisafter->GetXmin();
2443    Double_t to = axisafter->GetXmax();
2444    Double_t *newbins = new Double_t[bins+1];
2445    newbins[0] = from;
2446    Double_t factor = TMath::Power(to/from, 1./bins);
2447    for(Int_t i=1; i<=bins; ++i) newbins[i] = factor * newbins[i-1];
2448    axisafter->Set(bins, newbins);
2449    delete [] newbins;
2450
2451 }
2452
2453 //________________________________________________________________________
2454 void AliAnalysisTaskGammaConvV1::Terminate(const Option_t *)
2455 {
2456
2457    //fOutputContainer->Print(); // Will crash on GRID
2458 }