]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGCF/FEMTOSCOPY/Chaoticity/AliFourPion.cxx
fix MbinMixing bug
[u/mrichter/AliRoot.git] / PWGCF / FEMTOSCOPY / Chaoticity / AliFourPion.cxx
1 #include <iostream>
2 #include <math.h>
3 #include "TChain.h"
4 #include "TFile.h"
5 #include "TKey.h"
6 #include "TObject.h"
7 #include "TObjString.h"
8 #include "TList.h"
9 #include "TTree.h"
10 #include "TH1F.h"
11 #include "TH1D.h"
12 #include "TH2D.h"
13 #include "TH3D.h"
14 #include "TProfile.h"
15 #include "TProfile2D.h"
16 #include "TCanvas.h"
17 #include "TRandom3.h"
18 #include "TF1.h"
19
20 #include "AliAnalysisTask.h"
21 #include "AliAnalysisManager.h"
22
23
24 #include "AliESDEvent.h"
25 #include "AliESDInputHandler.h"
26 #include "AliESDtrackCuts.h"
27
28 #include "AliAODEvent.h"
29 #include "AliAODInputHandler.h"
30 #include "AliAODMCParticle.h"
31 #include "AliAnalysisUtils.h"
32
33 #include "AliFourPion.h"
34
35 #define PI 3.1415927
36 #define G_Coeff 0.006399 // 2*pi*alpha*M_pion
37 #define FmToGeV 0.19733 // conversion of Fm to GeV
38 #define kappa3 0.15 // kappa3 Edgeworth coefficient (non-Gaussian features of C2)
39 #define kappa4 0.32 // kappa4 Edgeworth coefficient (non-Gaussian features of C2)
40 #define kappa3Fit 0.1 // kappa3 for c4QS fit
41 #define kappa4Fit 0.5 // kappa4 for c4QS fit
42
43 // Author: Dhevan Gangadharan
44
45 ClassImp(AliFourPion)
46
47 //________________________________________________________________________
48 AliFourPion::AliFourPion():
49 AliAnalysisTaskSE(),
50   fname(0),
51   fAOD(0x0), 
52   fOutputList(0x0),
53   fPIDResponse(0x0),
54   fEC(0x0),
55   fEvt(0x0),
56   fTempStruct(0x0),
57   fRandomNumber(0x0),
58   fLEGO(kTRUE),
59   fMCcase(kFALSE),
60   fAODcase(kTRUE),
61   fPbPbcase(kTRUE),
62   fGenerateSignal(kFALSE),
63   fGeneratorOnly(kFALSE),
64   fTabulatePairs(kFALSE),
65   fLinearInterpolation(kTRUE),
66   fMixedChargeCut(kFALSE),
67   fRMax(11),
68   ffcSq(0.7),
69   ffcSqMRC(0.6),
70   fFilterBit(7),
71   fMaxChi2NDF(10),
72   fMinTPCncls(0),
73   fBfield(0),
74   fMbin(0),
75   fFSIindex(0),
76   fEDbin(0),
77   fMbins(fCentBins),
78   fMultLimit(0),
79   fCentBinLowLimit(0),
80   fCentBinHighLimit(1),
81   fEventCounter(0),
82   fEventsToMix(0),
83   fZvertexBins(0),
84   fMultLimits(),
85   fMinPt(0.16),
86   fMaxPt(1.0),
87   fQcut(0),
88   fQLowerCut(0),
89   fNormQcutLow(0),
90   fNormQcutHigh(0),
91   fKupperBound(0),
92   fQupperBoundQ2(0),
93   fQupperBoundQ3(0),
94   fQupperBoundQ4(0),
95   fQbinsQ2(1),
96   fQbinsQ3(1),
97   fQbinsQ4(1),
98   fQupperBoundWeights(0),
99   fKstepT(),
100   fKstepY(),
101   fKmeanT(),
102   fKmeanY(),
103   fKmiddleT(),
104   fKmiddleY(),
105   fQstep(0),
106   fQstepWeights(0),
107   fQmean(),
108   fDampStart(0),
109   fDampStep(0),
110   fTPCTOFboundry(0),
111   fTOFboundry(0),
112   fSigmaCutTPC(2.0),
113   fSigmaCutTOF(2.0),
114   fMinSepPairEta(0.03),
115   fMinSepPairPhi(0.04),
116   fShareQuality(0),
117   fShareFraction(0),
118   fTrueMassP(0), 
119   fTrueMassPi(0), 
120   fTrueMassK(0), 
121   fTrueMassKs(0), 
122   fTrueMassLam(0),
123   fKtIndexL(0),
124   fKtIndexH(0),
125   fQoIndexL(0),
126   fQoIndexH(0),
127   fQsIndexL(0),
128   fQsIndexH(0),
129   fQlIndexL(0),
130   fQlIndexH(0),
131   fDummyB(0),
132   fKT3transition(0.3),
133   fKT4transition(0.3),
134   farrP1(),
135   farrP2(),
136   fDefaultsCharSwitch(),
137   fLowQPairSwitch_E0E0(),
138   fLowQPairSwitch_E0E1(),
139   fLowQPairSwitch_E0E2(),
140   fLowQPairSwitch_E0E3(),
141   fLowQPairSwitch_E1E1(),
142   fLowQPairSwitch_E1E2(),
143   fLowQPairSwitch_E1E3(),
144   fLowQPairSwitch_E2E3(),
145   fNormQPairSwitch_E0E0(),
146   fNormQPairSwitch_E0E1(),
147   fNormQPairSwitch_E0E2(),
148   fNormQPairSwitch_E0E3(),
149   fNormQPairSwitch_E1E1(),
150   fNormQPairSwitch_E1E2(),
151   fNormQPairSwitch_E1E3(),
152   fNormQPairSwitch_E2E3(),
153   fMomResC2SC(0x0),
154   fMomResC2MC(0x0),
155   fWeightmuonCorrection(0x0)
156 {
157   // Default constructor
158   for(Int_t mb=0; mb<fMbins; mb++){
159     for(Int_t edB=0; edB<fEDbins; edB++){
160       for(Int_t c1=0; c1<2; c1++){
161         for(Int_t c2=0; c2<2; c2++){
162           for(Int_t term=0; term<2; term++){
163             
164             Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fTerms2=0x0;
165             
166             Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fIdeal = 0x0;
167             Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fSmeared = 0x0;
168             Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].OSL_ktbin[0].fTerms2OSL = 0x0;
169             Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].OSL_ktbin[0].fTerms2OSLQW = 0x0;
170             Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].OSL_ktbin[1].fTerms2OSL = 0x0;
171             Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].OSL_ktbin[1].fTerms2OSLQW = 0x0;
172             
173           }// term_2
174           
175           
176           for(Int_t c3=0; c3<2; c3++){
177             for(Int_t term=0; term<5; term++){
178               
179               Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fNorm3 = 0x0;
180               Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fTerms3 = 0x0;
181               Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fKfactor = 0x0;
182               Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fTwoPartNorm = 0x0;
183                       
184             }// term_3
185
186             for(Int_t c4=0; c4<2; c4++){
187               for(Int_t term=0; term<13; term++){
188                 
189                 Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fNorm4 = 0x0;
190                 Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fTerms4 = 0x0;
191                 Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fKfactor = 0x0;
192                 Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fTwoPartNorm = 0x0;
193                 
194               }// term_4
195
196             }// c4
197           }//c3
198         }//c2
199       }//c1
200       for(Int_t tKbin=0; tKbin<fKbinsT; tKbin++){
201         for(Int_t yKbin=0; yKbin<fKbinsY; yKbin++){
202           KT[tKbin].KY[yKbin].MB[mb].EDB[edB].TwoPT[0].fTerms2ThreeD = 0x0;
203           KT[tKbin].KY[yKbin].MB[mb].EDB[edB].TwoPT[1].fTerms2ThreeD = 0x0;
204         }
205       }
206       
207     }// ED
208   }// Mbin
209   
210   // Initialize FSI histograms
211   for(Int_t i=0; i<12; i++){
212     fFSIss[i]=0x0; 
213     fFSIos[i]=0x0;
214   }
215
216
217   // Initialize fNormWeight and fNormWeightErr to 0
218   for(Int_t i=0; i<3; i++){// Kt iterator
219     for(Int_t j=0; j<10; j++){// Mbin iterator
220       fNormWeight[i][j]=0x0;
221     }
222   }
223   
224
225 }
226 //________________________________________________________________________
227 AliFourPion::AliFourPion(const Char_t *name) 
228 : AliAnalysisTaskSE(name), 
229   fname(name),
230   fAOD(0x0), 
231   fOutputList(0x0),
232   fPIDResponse(0x0),
233   fEC(0x0),
234   fEvt(0x0),
235   fTempStruct(0x0),
236   fRandomNumber(0x0),
237   fLEGO(kTRUE),
238   fMCcase(kFALSE),
239   fAODcase(kTRUE),
240   fPbPbcase(kTRUE),
241   fGenerateSignal(kFALSE),
242   fGeneratorOnly(kFALSE),
243   fTabulatePairs(kFALSE),
244   fLinearInterpolation(kTRUE),
245   fMixedChargeCut(kFALSE),
246   fRMax(11),
247   ffcSq(0.7),
248   ffcSqMRC(0.6),
249   fFilterBit(7),
250   fMaxChi2NDF(10),
251   fMinTPCncls(0),
252   fBfield(0),
253   fMbin(0),
254   fFSIindex(0),
255   fEDbin(0),
256   fMbins(fCentBins),
257   fMultLimit(0),
258   fCentBinLowLimit(0),
259   fCentBinHighLimit(1),
260   fEventCounter(0),
261   fEventsToMix(0),
262   fZvertexBins(0),
263   fMultLimits(),
264   fMinPt(0.16),
265   fMaxPt(1.0),
266   fQcut(0),
267   fQLowerCut(0),
268   fNormQcutLow(0),
269   fNormQcutHigh(0),
270   fKupperBound(0),
271   fQupperBoundQ2(0),
272   fQupperBoundQ3(0),
273   fQupperBoundQ4(0),
274   fQbinsQ2(1),
275   fQbinsQ3(1),
276   fQbinsQ4(1),
277   fQupperBoundWeights(0),
278   fKstepT(),
279   fKstepY(),
280   fKmeanT(),
281   fKmeanY(),
282   fKmiddleT(),
283   fKmiddleY(),
284   fQstep(0),
285   fQstepWeights(0),
286   fQmean(),
287   fDampStart(0),
288   fDampStep(0),
289   fTPCTOFboundry(0),
290   fTOFboundry(0),
291   fSigmaCutTPC(2.0),
292   fSigmaCutTOF(2.0),
293   fMinSepPairEta(0.03),
294   fMinSepPairPhi(0.04),
295   fShareQuality(0),
296   fShareFraction(0),
297   fTrueMassP(0), 
298   fTrueMassPi(0), 
299   fTrueMassK(0), 
300   fTrueMassKs(0), 
301   fTrueMassLam(0),
302   fKtIndexL(0),
303   fKtIndexH(0),
304   fQoIndexL(0),
305   fQoIndexH(0),
306   fQsIndexL(0),
307   fQsIndexH(0),
308   fQlIndexL(0),
309   fQlIndexH(0),
310   fDummyB(0),
311   fKT3transition(0.3),
312   fKT4transition(0.3),
313   farrP1(),
314   farrP2(),
315   fDefaultsCharSwitch(),
316   fLowQPairSwitch_E0E0(),
317   fLowQPairSwitch_E0E1(),
318   fLowQPairSwitch_E0E2(),
319   fLowQPairSwitch_E0E3(),
320   fLowQPairSwitch_E1E1(),
321   fLowQPairSwitch_E1E2(),
322   fLowQPairSwitch_E1E3(),
323   fLowQPairSwitch_E2E3(),
324   fNormQPairSwitch_E0E0(),
325   fNormQPairSwitch_E0E1(),
326   fNormQPairSwitch_E0E2(),
327   fNormQPairSwitch_E0E3(),
328   fNormQPairSwitch_E1E1(),
329   fNormQPairSwitch_E1E2(),
330   fNormQPairSwitch_E1E3(),
331   fNormQPairSwitch_E2E3(),
332   fMomResC2SC(0x0),
333   fMomResC2MC(0x0),
334   fWeightmuonCorrection(0x0)
335 {
336   // Main constructor
337   fAODcase=kTRUE;
338   
339   
340
341   for(Int_t mb=0; mb<fMbins; mb++){
342     for(Int_t edB=0; edB<fEDbins; edB++){
343       for(Int_t c1=0; c1<2; c1++){
344         for(Int_t c2=0; c2<2; c2++){
345           for(Int_t term=0; term<2; term++){
346             
347             Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fTerms2=0x0;
348             
349             Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fIdeal = 0x0;
350             Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fSmeared = 0x0;
351             Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].OSL_ktbin[0].fTerms2OSL = 0x0;
352             Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].OSL_ktbin[0].fTerms2OSLQW = 0x0;
353             Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].OSL_ktbin[1].fTerms2OSL = 0x0;
354             Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].OSL_ktbin[1].fTerms2OSLQW = 0x0;
355             
356           }// term_2
357           
358           for(Int_t c3=0; c3<2; c3++){
359             for(Int_t term=0; term<5; term++){
360               
361               Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fNorm3 = 0x0;
362               Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fTerms3 = 0x0;
363               Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fKfactor = 0x0;
364               Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fTwoPartNorm = 0x0;
365               
366             }// term_3
367
368             for(Int_t c4=0; c4<2; c4++){
369               for(Int_t term=0; term<13; term++){
370                 
371                 Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fNorm4 = 0x0;
372                 Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fTerms4 = 0x0;
373                 Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fKfactor = 0x0;
374                 Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fTwoPartNorm = 0x0;
375                 
376               }// term_4
377             }// c4
378           }//c3
379         }//c2
380       }//c1
381       
382       for(Int_t tKbin=0; tKbin<fKbinsT; tKbin++){
383         for(Int_t yKbin=0; yKbin<fKbinsY; yKbin++){
384           KT[tKbin].KY[yKbin].MB[mb].EDB[edB].TwoPT[0].fTerms2ThreeD = 0x0;
385           KT[tKbin].KY[yKbin].MB[mb].EDB[edB].TwoPT[1].fTerms2ThreeD = 0x0;
386         }
387       }
388       
389     }// ED
390   }// Mbin
391   
392   // Initialize FSI histograms
393   for(Int_t i=0; i<12; i++){
394     fFSIss[i]=0x0; 
395     fFSIos[i]=0x0;
396   }
397   
398   // Initialize fNormWeight and fNormWeightErr to 0
399   for(Int_t i=0; i<3; i++){// Kt iterator
400     for(Int_t j=0; j<10; j++){// Mbin iterator
401       fNormWeight[i][j]=0x0;
402     }
403   }
404
405
406   DefineOutput(1, TList::Class());
407 }
408 //________________________________________________________________________
409 AliFourPion::AliFourPion(const AliFourPion &obj) 
410   : AliAnalysisTaskSE(obj.fname),
411     fname(obj.fname),
412     fAOD(obj.fAOD), 
413     //fESD(obj.fESD), 
414     fOutputList(obj.fOutputList),
415     fPIDResponse(obj.fPIDResponse),
416     fEC(obj.fEC),
417     fEvt(obj.fEvt),
418     fTempStruct(obj.fTempStruct),
419     fRandomNumber(obj.fRandomNumber),
420     fLEGO(obj.fLEGO),
421     fMCcase(obj.fMCcase),
422     fAODcase(obj.fAODcase),
423     fPbPbcase(obj.fPbPbcase),
424     fGenerateSignal(obj.fGenerateSignal),
425     fGeneratorOnly(obj.fGeneratorOnly),
426     fTabulatePairs(obj.fTabulatePairs),
427     fLinearInterpolation(obj.fLinearInterpolation),
428     fMixedChargeCut(obj.fMixedChargeCut),
429     fRMax(obj.fRMax),
430     ffcSq(obj.ffcSq),
431     ffcSqMRC(obj.ffcSqMRC),
432     fFilterBit(obj.fFilterBit),
433     fMaxChi2NDF(obj.fMaxChi2NDF),
434     fMinTPCncls(obj.fMinTPCncls),
435     fBfield(obj.fBfield),
436     fMbin(obj.fMbin),
437     fFSIindex(obj.fFSIindex),
438     fEDbin(obj.fEDbin),
439     fMbins(obj.fMbins),
440     fMultLimit(obj.fMultLimit),
441     fCentBinLowLimit(obj.fCentBinLowLimit),
442     fCentBinHighLimit(obj.fCentBinHighLimit),
443     fEventCounter(obj.fEventCounter),
444     fEventsToMix(obj.fEventsToMix),
445     fZvertexBins(obj.fZvertexBins),
446     fMultLimits(),
447     fMinPt(obj.fMinPt),
448     fMaxPt(obj.fMaxPt),
449     fQcut(obj.fQcut),
450     fQLowerCut(obj.fQLowerCut),
451     fNormQcutLow(0),
452     fNormQcutHigh(0),
453     fKupperBound(obj.fKupperBound),
454     fQupperBoundQ2(obj.fQupperBoundQ2),
455     fQupperBoundQ3(obj.fQupperBoundQ3),
456     fQupperBoundQ4(obj.fQupperBoundQ4),
457     fQbinsQ2(obj.fQbinsQ2),
458     fQbinsQ3(obj.fQbinsQ3),
459     fQbinsQ4(obj.fQbinsQ4),
460     fQupperBoundWeights(obj.fQupperBoundWeights),
461     fKstepT(),
462     fKstepY(),
463     fKmeanT(),
464     fKmeanY(),
465     fKmiddleT(),
466     fKmiddleY(),
467     fQstep(obj.fQstep),
468     fQstepWeights(obj.fQstepWeights),
469     fQmean(),
470     fDampStart(obj.fDampStart),
471     fDampStep(obj.fDampStep),
472     fTPCTOFboundry(obj.fTPCTOFboundry),
473     fTOFboundry(obj.fTOFboundry),
474     fSigmaCutTPC(obj.fSigmaCutTPC),
475     fSigmaCutTOF(obj.fSigmaCutTOF),
476     fMinSepPairEta(obj.fMinSepPairEta),
477     fMinSepPairPhi(obj.fMinSepPairPhi),
478     fShareQuality(obj.fShareQuality),
479     fShareFraction(obj.fShareFraction),
480     fTrueMassP(obj.fTrueMassP), 
481     fTrueMassPi(obj.fTrueMassPi), 
482     fTrueMassK(obj.fTrueMassK), 
483     fTrueMassKs(obj.fTrueMassKs), 
484     fTrueMassLam(obj.fTrueMassLam),
485     fKtIndexL(obj.fKtIndexL),
486     fKtIndexH(obj.fKtIndexH),
487     fQoIndexL(obj.fQoIndexL),
488     fQoIndexH(obj.fQoIndexH),
489     fQsIndexL(obj.fQsIndexL),
490     fQsIndexH(obj.fQsIndexH),
491     fQlIndexL(obj.fQlIndexL),
492     fQlIndexH(obj.fQlIndexH),
493     fDummyB(obj.fDummyB),
494     fKT3transition(obj.fKT3transition),
495     fKT4transition(obj.fKT4transition),
496     farrP1(),
497     farrP2(),
498     fDefaultsCharSwitch(),
499     fLowQPairSwitch_E0E0(),
500     fLowQPairSwitch_E0E1(),
501     fLowQPairSwitch_E0E2(),
502     fLowQPairSwitch_E0E3(),
503     fLowQPairSwitch_E1E1(),
504     fLowQPairSwitch_E1E2(),
505     fLowQPairSwitch_E1E3(),
506     fLowQPairSwitch_E2E3(),
507     fNormQPairSwitch_E0E0(),
508     fNormQPairSwitch_E0E1(),
509     fNormQPairSwitch_E0E2(),
510     fNormQPairSwitch_E0E3(),
511     fNormQPairSwitch_E1E1(),
512     fNormQPairSwitch_E1E2(),
513     fNormQPairSwitch_E1E3(),
514     fNormQPairSwitch_E2E3(),
515     fMomResC2SC(obj.fMomResC2SC),
516     fMomResC2MC(obj.fMomResC2MC),
517     fWeightmuonCorrection(obj.fWeightmuonCorrection)
518 {
519   // Copy Constructor
520   
521   for(Int_t i=0; i<12; i++){
522     fFSIss[i]=obj.fFSIss[i]; 
523     fFSIos[i]=obj.fFSIos[i];
524   }
525   
526   // Initialize fNormWeight and fNormWeightErr to 0
527   for(Int_t i=0; i<3; i++){// Kt iterator
528     for(Int_t j=0; j<10; j++){// Mbin iterator
529       fNormWeight[i][j]=0x0;
530     }
531   }
532   
533
534 }
535 //________________________________________________________________________
536 AliFourPion &AliFourPion::operator=(const AliFourPion &obj) 
537 {
538   // Assignment operator  
539   if (this == &obj)
540     return *this;
541
542   fname = obj.fname;
543   fAOD = obj.fAOD; 
544   fOutputList = obj.fOutputList;
545   fPIDResponse = obj.fPIDResponse;
546   fEC = obj.fEC;
547   fEvt = obj.fEvt;
548   fTempStruct = obj.fTempStruct;
549   fRandomNumber = obj.fRandomNumber;
550   fLEGO = fLEGO;
551   fMCcase = obj.fMCcase;
552   fAODcase = obj.fAODcase;
553   fPbPbcase = obj.fPbPbcase; 
554   fGenerateSignal = obj.fGenerateSignal;
555   fGeneratorOnly = obj.fGeneratorOnly;
556   fTabulatePairs = obj.fTabulatePairs;
557   fLinearInterpolation = obj.fLinearInterpolation;
558   fMixedChargeCut = obj.fMixedChargeCut;
559   fRMax = obj.fRMax;
560   ffcSq = obj.ffcSq;
561   ffcSqMRC = obj.ffcSqMRC;
562   fFilterBit = obj.fFilterBit;
563   fMaxChi2NDF = obj.fMaxChi2NDF;
564   fMinTPCncls = obj.fMinTPCncls;
565   fBfield = obj.fBfield;
566   fMbin = obj.fMbin;
567   fFSIindex = obj.fFSIindex;
568   fEDbin = obj.fEDbin;
569   fMbins = obj.fMbins;
570   fMultLimit = obj.fMultLimit;
571   fCentBinLowLimit = obj.fCentBinLowLimit;
572   fCentBinHighLimit = obj.fCentBinHighLimit;
573   fEventCounter = obj.fEventCounter;
574   fEventsToMix = obj.fEventsToMix;
575   fZvertexBins = obj.fZvertexBins;
576   fMinPt = obj.fMinPt;
577   fMaxPt = obj.fMaxPt;
578   fQcut = obj.fQcut;
579   fQLowerCut = obj.fQLowerCut;
580   fKupperBound = obj.fKupperBound;
581   fQupperBoundQ2 = obj.fQupperBoundQ2;
582   fQupperBoundQ3 = obj.fQupperBoundQ3;
583   fQupperBoundQ4 = obj.fQupperBoundQ4;
584   fQbinsQ2 = obj.fQbinsQ2;
585   fQbinsQ3 = obj.fQbinsQ3;
586   fQbinsQ4 = obj.fQbinsQ4;
587   fQupperBoundWeights = obj.fQupperBoundWeights;
588   fQstep = obj.fQstep;
589   fQstepWeights = obj.fQstepWeights;
590   fDampStart = obj.fDampStart;
591   fDampStep = obj.fDampStep;
592   fTPCTOFboundry = obj.fTPCTOFboundry;
593   fTOFboundry = obj.fTOFboundry;
594   fSigmaCutTPC = obj.fSigmaCutTPC;
595   fSigmaCutTOF = obj.fSigmaCutTOF;
596   fMinSepPairEta = obj.fMinSepPairEta;
597   fMinSepPairPhi = obj.fMinSepPairPhi;
598   fShareQuality = obj.fShareQuality;
599   fShareFraction = obj.fShareFraction;
600   fTrueMassP = obj.fTrueMassP; 
601   fTrueMassPi = obj.fTrueMassPi; 
602   fTrueMassK = obj.fTrueMassK; 
603   fTrueMassKs = obj.fTrueMassKs; 
604   fTrueMassLam = obj.fTrueMassLam;
605   fKtIndexL = obj.fKtIndexL;
606   fKtIndexH = obj.fKtIndexH;
607   fQoIndexL = obj.fQoIndexL;
608   fQoIndexH = obj.fQoIndexH;
609   fQsIndexL = obj.fQsIndexL;
610   fQsIndexH = obj.fQsIndexH;
611   fQlIndexL = obj.fQlIndexL;
612   fQlIndexH = obj.fQlIndexH;
613   fDummyB = obj.fDummyB;
614   fKT3transition = obj.fKT3transition;
615   fKT4transition = obj.fKT4transition;
616   fMomResC2SC = obj.fMomResC2SC;
617   fMomResC2MC = obj.fMomResC2MC;
618   fWeightmuonCorrection = obj.fWeightmuonCorrection;
619   
620   for(Int_t i=0; i<12; i++){
621     fFSIss[i]=obj.fFSIss[i]; 
622     fFSIos[i]=obj.fFSIos[i];
623   }
624   for(Int_t i=0; i<3; i++){// Kt iterator
625     for(Int_t j=0; j<10; j++){// Mbin iterator
626       fNormWeight[i][j]=obj.fNormWeight[i][j];
627     }
628   }
629   
630   return (*this);
631 }
632 //________________________________________________________________________
633 AliFourPion::~AliFourPion()
634 {
635   // Destructor
636   if(fAOD) delete fAOD; 
637   //if(fESD) delete fESD; 
638   if(fOutputList) delete fOutputList;
639   if(fPIDResponse) delete fPIDResponse;
640   if(fEC) delete fEC;
641   if(fEvt) delete fEvt;
642   if(fTempStruct) delete [] fTempStruct;
643   if(fRandomNumber) delete fRandomNumber;
644   if(fMomResC2SC) delete fMomResC2SC;
645   if(fMomResC2MC) delete fMomResC2MC;
646   if(fWeightmuonCorrection) delete fWeightmuonCorrection;
647
648   for(Int_t j=0; j<kMultLimitPbPb; j++){
649     if(fLowQPairSwitch_E0E0[j]) delete [] fLowQPairSwitch_E0E0[j];
650     if(fLowQPairSwitch_E0E1[j]) delete [] fLowQPairSwitch_E0E1[j];
651     if(fLowQPairSwitch_E0E2[j]) delete [] fLowQPairSwitch_E0E2[j];
652     if(fLowQPairSwitch_E0E3[j]) delete [] fLowQPairSwitch_E0E3[j];
653     if(fLowQPairSwitch_E1E1[j]) delete [] fLowQPairSwitch_E1E1[j];
654     if(fLowQPairSwitch_E1E2[j]) delete [] fLowQPairSwitch_E1E2[j];
655     if(fLowQPairSwitch_E1E3[j]) delete [] fLowQPairSwitch_E1E3[j];
656     if(fLowQPairSwitch_E2E3[j]) delete [] fLowQPairSwitch_E2E3[j];
657     //
658     if(fNormQPairSwitch_E0E0[j]) delete [] fNormQPairSwitch_E0E0[j];
659     if(fNormQPairSwitch_E0E1[j]) delete [] fNormQPairSwitch_E0E1[j];
660     if(fNormQPairSwitch_E0E2[j]) delete [] fNormQPairSwitch_E0E2[j];
661     if(fNormQPairSwitch_E0E3[j]) delete [] fNormQPairSwitch_E0E3[j];
662     if(fNormQPairSwitch_E1E1[j]) delete [] fNormQPairSwitch_E1E1[j];
663     if(fNormQPairSwitch_E1E2[j]) delete [] fNormQPairSwitch_E1E2[j];
664     if(fNormQPairSwitch_E1E3[j]) delete [] fNormQPairSwitch_E1E3[j];
665     if(fNormQPairSwitch_E2E3[j]) delete [] fNormQPairSwitch_E2E3[j];
666   }
667   
668   //
669   for(Int_t mb=0; mb<fMbins; mb++){
670     for(Int_t edB=0; edB<fEDbins; edB++){
671       for(Int_t c1=0; c1<2; c1++){
672         for(Int_t c2=0; c2<2; c2++){
673           for(Int_t term=0; term<2; term++){
674             
675             if(Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fTerms2) delete Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fTerms2;
676             
677             if(Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fIdeal) delete Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fIdeal;
678             if(Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fSmeared) delete Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fSmeared;
679             if(Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].OSL_ktbin[0].fTerms2OSL) delete Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].OSL_ktbin[0].fTerms2OSL;
680             if(Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].OSL_ktbin[0].fTerms2OSLQW) delete Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].OSL_ktbin[0].fTerms2OSLQW;
681             if(Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].OSL_ktbin[1].fTerms2OSL) delete Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].OSL_ktbin[1].fTerms2OSL;
682             if(Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].OSL_ktbin[1].fTerms2OSLQW) delete Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].OSL_ktbin[1].fTerms2OSLQW;
683             //
684             if(Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fMCqinv) delete Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fMCqinv;
685             if(Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fMCqinvQW) delete Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fMCqinvQW;
686           }// term_2
687           
688           for(Int_t c3=0; c3<2; c3++){
689             for(Int_t term=0; term<5; term++){
690                 
691               if(Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fNorm3) delete Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fNorm3;
692               if(Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fTerms3) delete Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fTerms3;
693               if(Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fKfactor) delete Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fKfactor;
694               if(Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fKfactorWeighted) delete Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fKfactorWeighted;
695               //
696               if(Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fTwoPartNorm) delete Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fTwoPartNorm;
697                 
698             }// term_3
699
700             for(Int_t c4=0; c4<2; c4++){
701               for(Int_t term=0; term<13; term++){
702                 
703                 if(Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fNorm4) delete Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fNorm4;
704                 if(Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fTerms4) delete Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fTerms4;
705                 if(Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fKfactor) delete Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fKfactor;
706                 if(Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fKfactorWeighted) delete Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fKfactorWeighted;
707                 //
708                 if(Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fTwoPartNorm) delete Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fTwoPartNorm;
709               }// term_4
710
711             }//c4
712           }//c3
713         }//c2
714       }//c1
715       for(Int_t tKbin=0; tKbin<fKbinsT; tKbin++){
716         for(Int_t yKbin=0; yKbin<fKbinsY; yKbin++){
717           if(KT[tKbin].KY[yKbin].MB[mb].EDB[edB].TwoPT[0].fTerms2ThreeD) delete KT[tKbin].KY[yKbin].MB[mb].EDB[edB].TwoPT[0].fTerms2ThreeD;
718           if(KT[tKbin].KY[yKbin].MB[mb].EDB[edB].TwoPT[1].fTerms2ThreeD) delete KT[tKbin].KY[yKbin].MB[mb].EDB[edB].TwoPT[1].fTerms2ThreeD;
719         }
720       }
721       
722     }// ED
723   }// Mbin
724   
725    
726   for(Int_t i=0; i<12; i++){
727     if(fFSIss[i]) delete fFSIss[i]; 
728     if(fFSIos[i]) delete fFSIos[i];
729   }
730   for(Int_t i=0; i<3; i++){// Kt iterator
731     for(Int_t j=0; j<10; j++){// Mbin iterator
732       if(fNormWeight[i][j]) delete fNormWeight[i][j];
733     }
734   }
735   
736 }
737 //________________________________________________________________________
738 void AliFourPion::ParInit()
739 {
740   cout<<"AliFourPion MyInit() call"<<endl;
741   cout<<"lego:"<<fLEGO<<"  MCcase:"<<fMCcase<<"  PbPbcase:"<<fPbPbcase<<"  TabulatePairs:"<<fTabulatePairs<<"  GenSignal:"<<fGenerateSignal<<"  CentLow:"<<fCentBinLowLimit<<"  CentHigh:"<<fCentBinHighLimit<<"  RMax:"<<fRMax<<"  fc^2:"<<ffcSq<<"  FB:"<<fFilterBit<<"  MaxChi2/NDF:"<<fMaxChi2NDF<<"  MinTPCncls:"<<fMinTPCncls<<"  MinPairSepEta:"<<fMinSepPairEta<<"  MinPairSepPhi:"<<fMinSepPairPhi<<"  NsigTPC:"<<fSigmaCutTPC<<"  NsigTOF:"<<fSigmaCutTOF<<endl;
742
743   fRandomNumber = new TRandom3();
744   fRandomNumber->SetSeed(0);
745     
746   //
747   fEventCounter=0;
748   fEventsToMix=3;
749   fZvertexBins=2;//2
750   
751   fTPCTOFboundry = 0.6;// TPC pid used below this momentum, TOF above but below TOF_boundry
752   fTOFboundry = 2.1;// TOF pid used below this momentum
753   
754   ////////////////////////////////////////////////
755   // PadRow Pair Cuts
756   fShareQuality = .5;// max
757   fShareFraction = .05;// max
758   ////////////////////////////////////////////////
759   
760   
761   fMultLimits[0]=0, fMultLimits[1]=2, fMultLimits[2]=4, fMultLimits[3]=6, fMultLimits[4]=8, fMultLimits[5]=10;
762   fMultLimits[6]=12, fMultLimits[7]=14, fMultLimits[8]=16, fMultLimits[9]=18, fMultLimits[10]=20, fMultLimits[11]=150;
763   
764     
765   
766   if(fPbPbcase) {// PbPb
767     fMultLimit=kMultLimitPbPb;
768     fMbins=fCentBins;
769     fQcut=0.1;
770     fNormQcutLow = 0.15;// 0.15
771     fNormQcutHigh = 0.2;// 0.175
772   }else {// pp
773     fMultLimit=kMultLimitpp; 
774     fMbins=kMultBinspp; 
775     fQcut=0.6;
776     fNormQcutLow = 1.0;
777     fNormQcutHigh = 1.5;
778   }
779   
780   fQLowerCut = 0.005;// was 0.005
781   fKupperBound = 1.0;
782   //
783   fKstepY[0] = 1.6;
784   fKmeanY[0] = 0;// central y
785   fKmiddleY[0] = 0;
786
787   // 4x1 (Kt: 0-0.25, 0.25-0.35, 0.35-0.45, 0.45-1.0)
788   if(fKbinsT==4){
789     fKstepT[0] = 0.25; fKstepT[1] = 0.1; fKstepT[2] = 0.1; fKstepT[3] = 0.55;
790     fKmeanT[0] = 0.212; fKmeanT[1] = 0.299; fKmeanT[2] = 0.398; fKmeanT[3] = 0.576;
791     fKmiddleT[0] = 0.125; fKmiddleT[1] = 0.3; fKmiddleT[2] = 0.4; fKmiddleT[3] = 0.725;
792   }
793   // 3x1 (Kt: 0-0.3, 0.3-0.45, 0.45-1.0)
794   if(fKbinsT==3){
795     fKstepT[0] = 0.3; fKstepT[1] = 0.15; fKstepT[2] = 0.55;
796     fKmeanT[0] = 0.240; fKmeanT[1] = 0.369; fKmeanT[2] = 0.576;
797     fKmiddleT[0] = 0.15; fKmiddleT[1] = 0.375; fKmiddleT[2] = 0.725;
798   }
799   // 2x1 (Kt: 0-0.35, 0.35-1.0)
800   if(fKbinsT==2){
801     fKstepT[0] = 0.35; fKstepT[1] = 0.65;
802     fKmeanT[0] = 0.264; fKmeanT[1] = 0.500;
803     fKmiddleT[0] = 0.175; fKmiddleT[1] = 0.675;
804   }
805  
806   //
807   fQupperBoundWeights = 0.2;
808   fQupperBoundQ2 = 2.0;
809   fQupperBoundQ3 = 0.6;
810   fQupperBoundQ4 = 0.6;
811   fQbinsQ2 = fQupperBoundQ2/0.005;
812   fQbinsQ3 = fQupperBoundQ3/0.005;
813   fQbinsQ4 = fQupperBoundQ4/0.005;
814   fQstepWeights = fQupperBoundWeights/Float_t(kQbinsWeights);
815   for(Int_t i=0; i<kQbinsWeights; i++) {fQmean[i]=(i+0.5)*fQstepWeights;}
816   //
817   fDampStart = 0.5;// was 0.3, then 0.5
818   fDampStep = 0.02;
819   
820   //
821   
822   fEC = new AliFourPionEventCollection **[fZvertexBins];
823   for(UShort_t i=0; i<fZvertexBins; i++){
824     
825     fEC[i] = new AliFourPionEventCollection *[fCentBinsMixing];
826
827     for(UShort_t j=0; j<fCentBinsMixing; j++){
828       
829       fEC[i][j] = new AliFourPionEventCollection(fEventsToMix+1, fMultLimit, kMCarrayLimit, fMCcase);
830     }
831   }
832   
833   for(Int_t i=0; i<kMultLimitPbPb; i++) fDefaultsCharSwitch[i]='0';
834   for(Int_t i=0; i<kMultLimitPbPb; i++) {
835     fLowQPairSwitch_E0E0[i] = new TArrayC(kMultLimitPbPb,fDefaultsCharSwitch);
836     fLowQPairSwitch_E0E1[i] = new TArrayC(kMultLimitPbPb,fDefaultsCharSwitch);
837     fLowQPairSwitch_E0E2[i] = new TArrayC(kMultLimitPbPb,fDefaultsCharSwitch);
838     fLowQPairSwitch_E0E3[i] = new TArrayC(kMultLimitPbPb,fDefaultsCharSwitch);
839     fLowQPairSwitch_E1E1[i] = new TArrayC(kMultLimitPbPb,fDefaultsCharSwitch);
840     fLowQPairSwitch_E1E2[i] = new TArrayC(kMultLimitPbPb,fDefaultsCharSwitch);
841     fLowQPairSwitch_E1E3[i] = new TArrayC(kMultLimitPbPb,fDefaultsCharSwitch);
842     fLowQPairSwitch_E2E3[i] = new TArrayC(kMultLimitPbPb,fDefaultsCharSwitch);
843     //
844     fNormQPairSwitch_E0E0[i] = new TArrayC(kMultLimitPbPb,fDefaultsCharSwitch);
845     fNormQPairSwitch_E0E1[i] = new TArrayC(kMultLimitPbPb,fDefaultsCharSwitch);
846     fNormQPairSwitch_E0E2[i] = new TArrayC(kMultLimitPbPb,fDefaultsCharSwitch);
847     fNormQPairSwitch_E0E3[i] = new TArrayC(kMultLimitPbPb,fDefaultsCharSwitch);
848     fNormQPairSwitch_E1E1[i] = new TArrayC(kMultLimitPbPb,fDefaultsCharSwitch);
849     fNormQPairSwitch_E1E2[i] = new TArrayC(kMultLimitPbPb,fDefaultsCharSwitch);
850     fNormQPairSwitch_E1E3[i] = new TArrayC(kMultLimitPbPb,fDefaultsCharSwitch);
851     fNormQPairSwitch_E2E3[i] = new TArrayC(kMultLimitPbPb,fDefaultsCharSwitch);
852   }
853   
854   fTempStruct = new AliFourPionTrackStruct[fMultLimit];
855   
856   
857   fTrueMassP=0.93827, fTrueMassPi=0.13957, fTrueMassK=0.493677, fTrueMassKs=0.497614, fTrueMassLam=1.11568;
858
859   
860
861   // Set weights, Coulomb corrections, and Momentum resolution corrections manually if not on LEGO
862   if(!fLEGO) {
863     SetFSICorrelations(fLEGO);// Read in 2-particle and 3-particle FSI correlations
864     if(!fTabulatePairs) SetWeightArrays(fLEGO);// Set Weight Array
865     if(!fMCcase && !fTabulatePairs) SetMomResCorrections(fLEGO);// Read Momentum resolution file
866     if(!fMCcase && !fTabulatePairs) SetMuonCorrections(fLEGO);// Read Muon corrections
867   }
868   
869   /////////////////////////////////////////////
870   /////////////////////////////////////////////
871   
872 }
873 //________________________________________________________________________
874 void AliFourPion::UserCreateOutputObjects()
875 {
876   // Create histograms
877   // Called once
878   
879   ParInit();// Initialize my settings
880
881
882   fOutputList = new TList();
883   fOutputList->SetOwner();
884   
885   TH3F *fVertexDist = new TH3F("fVertexDist","Vertex Distribution",20,-1,1, 20,-1,1, 600,-30,30);
886   fVertexDist->GetXaxis()->SetTitle("X Vertex (cm)");
887   fVertexDist->GetYaxis()->SetTitle("Y Vertex (cm)");
888   fVertexDist->GetZaxis()->SetTitle("Z Vertex (cm)");
889   fOutputList->Add(fVertexDist);
890   
891   
892   TH2F *fDCAxyDistPlus = new TH2F("fDCAxyDistPlus","DCA distribution",300,0,3., 50,0,5);
893   fOutputList->Add(fDCAxyDistPlus);
894   TH2F *fDCAzDistPlus = new TH2F("fDCAzDistPlus","DCA distribution",300,0,3., 50,0,5);
895   fOutputList->Add(fDCAzDistPlus);
896   TH2F *fDCAxyDistMinus = new TH2F("fDCAxyDistMinus","DCA distribution",300,0,3., 50,0,5);
897   fOutputList->Add(fDCAxyDistMinus);
898   TH2F *fDCAzDistMinus = new TH2F("fDCAzDistMinus","DCA distribution",300,0,3., 50,0,5);
899   fOutputList->Add(fDCAzDistMinus);
900   
901   
902   TH1F *fEvents1 = new TH1F("fEvents1","Events vs. fMbin",fMbins,.5,fMbins+.5);
903   fOutputList->Add(fEvents1);
904   TH1F *fEvents2 = new TH1F("fEvents2","Events vs. fMbin",fMbins,.5,fMbins+.5);
905   fOutputList->Add(fEvents2);
906   
907   TH1F *fMultDist0 = new TH1F("fMultDist0","Multiplicity Distribution",fMultLimit,-.5,fMultLimit-.5);
908   fMultDist0->GetXaxis()->SetTitle("Multiplicity");
909   fOutputList->Add(fMultDist0);
910
911   TH1F *fMultDist1 = new TH1F("fMultDist1","Multiplicity Distribution",fMultLimit,-.5,fMultLimit-.5);
912   fMultDist1->GetXaxis()->SetTitle("Multiplicity");
913   fOutputList->Add(fMultDist1);
914   
915   TH1F *fMultDist2 = new TH1F("fMultDist2","Multiplicity Distribution",fMultLimit,-.5,fMultLimit-.5);
916   fMultDist2->GetXaxis()->SetTitle("Multiplicity");
917   fOutputList->Add(fMultDist2);
918   
919   TH1F *fMultDist3 = new TH1F("fMultDist3","Multiplicity Distribution",fMultLimit,-.5,fMultLimit-.5);
920   fMultDist3->GetXaxis()->SetTitle("Multiplicity");
921   fOutputList->Add(fMultDist3);
922   
923   TH3F *fPtEtaDist = new TH3F("fPtEtaDist","fPtEtaDist",2,-1.1,1.1, 300,0,3., 28,-1.4,1.4);
924   fOutputList->Add(fPtEtaDist);
925
926   TH3F *fPhiPtDist = new TH3F("fPhiPtDist","fPhiPtDist",2,-1.1,1.1, 120,0,2*PI, 300,0,3.);
927   fOutputList->Add(fPhiPtDist);
928   
929   TH3F *fTOFResponse = new TH3F("fTOFResponse","TOF relative time",20,0,100, 200,0,2, 4000,-20000,20000);
930   fOutputList->Add(fTOFResponse);
931   TH3F *fTPCResponse = new TH3F("fTPCResponse","TPCsignal",20,0,100, 200,0,2, 1000,0,1000);
932   fOutputList->Add(fTPCResponse);
933  
934   TH1F *fRejectedPairs = new TH1F("fRejectedPairs","",400,0,2);
935   fOutputList->Add(fRejectedPairs);
936   TH1F *fRejectedPairsWeighting = new TH1F("fAcceptedPairsWeighting","",400,0,2);
937   fOutputList->Add(fRejectedPairsWeighting);
938   TH1F *fTotalPairsWeighting = new TH1F("fTotalPairsWeighting","",400,0,2);
939   fOutputList->Add(fTotalPairsWeighting);
940   //
941   TH1F *fRejectedPairsMC = new TH1F("fRejectedPairsMC","",400,0,2);
942   fOutputList->Add(fRejectedPairsMC);
943   TH1F *fRejectedPairsWeightingMC = new TH1F("fAcceptedPairsWeightingMC","",400,0,2);
944   fOutputList->Add(fRejectedPairsWeightingMC);
945   TH1F *fTotalPairsWeightingMC = new TH1F("fTotalPairsWeightingMC","",400,0,2);
946   fOutputList->Add(fTotalPairsWeightingMC);
947   
948   TH1I *fRejectedEvents = new TH1I("fRejectedEvents","",fMbins,0.5,fMbins+.5);
949   fOutputList->Add(fRejectedEvents);
950     
951   TH3F *fPairsDetaDPhiNum = new TH3F("fPairsDetaDPhiNum","",10,-.5,9.5, 200,-0.2,0.2, 600,-0.3,0.3);
952   if(fMCcase) fOutputList->Add(fPairsDetaDPhiNum);
953   TH3F *fPairsDetaDPhiDen = new TH3F("fPairsDetaDPhiDen","",10,-.5,9.5, 200,-0.2,0.2, 600,-0.3,0.3);
954   if(fMCcase) fOutputList->Add(fPairsDetaDPhiDen);
955   TH3F *fPairsShareFracDPhiNum = new TH3F("fPairsShareFracDPhiNum","",10,-.5,9.5, 159,0,1, 600,-0.3,0.3);
956   if(fMCcase) fOutputList->Add(fPairsShareFracDPhiNum);
957   TH3F *fPairsShareFracDPhiDen = new TH3F("fPairsShareFracDPhiDen","",10,-.5,9.5, 159,0,1, 600,-0.3,0.3);
958   if(fMCcase) fOutputList->Add(fPairsShareFracDPhiDen);
959   TH3D* fPairsPadRowNum = new TH3D("fPairsPadRowNum","", 20,0,1, 159,0,1, 40,0,0.2);
960   if(fMCcase) fOutputList->Add(fPairsPadRowNum);
961   TH3D* fPairsPadRowDen = new TH3D("fPairsPadRowDen","", 20,0,1, 159,0,1, 40,0,0.2);
962   if(fMCcase) fOutputList->Add(fPairsPadRowDen);
963
964
965
966   TH2D *fResonanceOSPairs = new TH2D("fResonanceOSPairs","",fMbins,.5,fMbins+.5, 1000,0,2);
967   if(fMCcase) fOutputList->Add(fResonanceOSPairs);
968   TH2D *fAllOSPairs = new TH2D("fAllOSPairs","",fMbins,.5,fMbins+.5, 1000,0,2);
969   if(fMCcase) fOutputList->Add(fAllOSPairs);
970   
971   TH3D *fPrimarySCPionPairs = new TH3D("fPrimarySCPionPairs","",fMbins,.5,fMbins+.5, 20,0,1, 20,0,0.2);
972   if(fMCcase) fOutputList->Add(fPrimarySCPionPairs);
973   TH3D *fAllSCPionPairs = new TH3D("fAllSCPionPairs","",fMbins,.5,fMbins+.5, 20,0,1, 20,0,0.2);
974   if(fMCcase) fOutputList->Add(fAllSCPionPairs);
975   TH3D *fPrimaryMCPionPairs = new TH3D("fPrimaryMCPionPairs","",fMbins,.5,fMbins+.5, 20,0,1, 20,0,0.2);
976   if(fMCcase) fOutputList->Add(fPrimaryMCPionPairs);
977   TH3D *fAllMCPionPairs = new TH3D("fAllMCPionPairs","",fMbins,.5,fMbins+.5, 20,0,1, 20,0,0.2);
978   if(fMCcase) fOutputList->Add(fAllMCPionPairs);
979
980   //
981   TH1D *fMuonParents = new TH1D("fMuonParents","",500,0.5,500.5);
982   if(fMCcase) fOutputList->Add(fMuonParents);
983   TH1D *fSecondaryMuonParents = new TH1D("fSecondaryMuonParents","",500,0.5,500.5);
984   if(fMCcase) fOutputList->Add(fSecondaryMuonParents);
985   TH3D *fMuonPionDeltaQinv = new TH3D("fMuonPionDeltaQinv","",2,-0.5,1.5, 20,0,1, 100,-0.2,0.2);
986   if(fMCcase) fOutputList->Add(fMuonPionDeltaQinv);
987   TH1D *fPionCandidates = new TH1D("fPionCandidates","",500,0.5,500.5);
988   if(fMCcase) fOutputList->Add(fPionCandidates);
989   //  
990   
991
992   TProfile *fAvgMult = new TProfile("fAvgMult","",fMbins,.5,fMbins+.5, 0,1500,"");
993   fOutputList->Add(fAvgMult);
994
995   TH2D *fTrackChi2NDF = new TH2D("fTrackChi2NDF","",20,0,100, 100,0,10);
996   fOutputList->Add(fTrackChi2NDF);
997   TH2D *fTrackTPCncls = new TH2D("fTrackTPCncls","",20,0,100, 110,50,160);
998   fOutputList->Add(fTrackTPCncls);
999
1000
1001   TH1D *fTPNRejects3pion1 = new TH1D("fTPNRejects3pion1","",fQbinsQ3,0,fQupperBoundQ3);
1002   fOutputList->Add(fTPNRejects3pion1);
1003   TH1D *fTPNRejects3pion2 = new TH1D("fTPNRejects3pion2","",fQbinsQ3,0,fQupperBoundQ3);
1004   fOutputList->Add(fTPNRejects3pion2);
1005   TH1D *fTPNRejects4pion1 = new TH1D("fTPNRejects4pion1","",fQbinsQ4,0,fQupperBoundQ4);
1006   fOutputList->Add(fTPNRejects4pion1);
1007
1008   TH3D *fKT3DistTerm1 = new TH3D("fKT3DistTerm1","",fMbins,.5,fMbins+.5, 20,0,1, 20,0,0.2);
1009   TH3D *fKT3DistTerm5 = new TH3D("fKT3DistTerm5","",fMbins,.5,fMbins+.5, 20,0,1, 20,0,0.2);
1010   fOutputList->Add(fKT3DistTerm1);
1011   fOutputList->Add(fKT3DistTerm5);
1012   TH3D *fKT4DistTerm1 = new TH3D("fKT4DistTerm1","",fMbins,.5,fMbins+.5, 20,0,1, 20,0,0.2);
1013   TH3D *fKT4DistTerm13 = new TH3D("fKT4DistTerm13","",fMbins,.5,fMbins+.5, 20,0,1, 20,0,0.2);
1014   fOutputList->Add(fKT4DistTerm1);
1015   fOutputList->Add(fKT4DistTerm13);
1016
1017
1018   TProfile2D *fKT3AvgpT = new TProfile2D("fKT3AvgpT","",fMbins,.5,fMbins+.5, 2,-0.5,1.5, 0.,1.0,"");
1019   fOutputList->Add(fKT3AvgpT);
1020   TProfile2D *fKT4AvgpT = new TProfile2D("fKT4AvgpT","",fMbins,.5,fMbins+.5, 2,-0.5,1.5, 0.,1.0,"");
1021   fOutputList->Add(fKT4AvgpT);
1022   TH3D* fQ3AvgpT = new TH3D("fQ3AvgpT","", 2,-0.5,1.5, fQbinsQ3,0,fQupperBoundQ3, 180,0.1,1.0);
1023   fOutputList->Add(fQ3AvgpT);
1024   TH3D* fQ4AvgpT = new TH3D("fQ4AvgpT","", 2,-0.5,1.5, fQbinsQ4,0,fQupperBoundQ4, 180,0.1,1.0);
1025   fOutputList->Add(fQ4AvgpT);
1026
1027
1028   TH1D *fMCWeight3DTerm1SC = new TH1D("fMCWeight3DTerm1SC","", 20,0,0.2);
1029   TH1D *fMCWeight3DTerm1SCden = new TH1D("fMCWeight3DTerm1SCden","", 20,0,0.2);
1030   TH1D *fMCWeight3DTerm2SC = new TH1D("fMCWeight3DTerm2SC","", 20,0,0.2);
1031   TH1D *fMCWeight3DTerm2SCden = new TH1D("fMCWeight3DTerm2SCden","", 20,0,0.2);
1032   TH1D *fMCWeight3DTerm1MC = new TH1D("fMCWeight3DTerm1MC","", 20,0,0.2);
1033   TH1D *fMCWeight3DTerm1MCden = new TH1D("fMCWeight3DTerm1MCden","", 20,0,0.2);
1034   TH1D *fMCWeight3DTerm2MC = new TH1D("fMCWeight3DTerm2MC","", 20,0,0.2);
1035   TH1D *fMCWeight3DTerm2MCden = new TH1D("fMCWeight3DTerm2MCden","", 20,0,0.2);
1036   TH1D *fMCWeight3DTerm3MC = new TH1D("fMCWeight3DTerm3MC","", 20,0,0.2);
1037   TH1D *fMCWeight3DTerm3MCden = new TH1D("fMCWeight3DTerm3MCden","", 20,0,0.2);
1038   TH1D *fMCWeight3DTerm4MC = new TH1D("fMCWeight3DTerm4MC","", 20,0,0.2);
1039   TH1D *fMCWeight3DTerm4MCden = new TH1D("fMCWeight3DTerm4MCden","", 20,0,0.2);
1040   fOutputList->Add(fMCWeight3DTerm1SC);
1041   fOutputList->Add(fMCWeight3DTerm1SCden);
1042   fOutputList->Add(fMCWeight3DTerm2SC);
1043   fOutputList->Add(fMCWeight3DTerm2SCden);
1044   fOutputList->Add(fMCWeight3DTerm1MC);
1045   fOutputList->Add(fMCWeight3DTerm1MCden);
1046   fOutputList->Add(fMCWeight3DTerm2MC);
1047   fOutputList->Add(fMCWeight3DTerm2MCden);
1048   fOutputList->Add(fMCWeight3DTerm3MC);
1049   fOutputList->Add(fMCWeight3DTerm3MCden);
1050   fOutputList->Add(fMCWeight3DTerm4MC);
1051   fOutputList->Add(fMCWeight3DTerm4MCden);
1052
1053
1054       
1055   for(Int_t mb=0; mb<fMbins; mb++){
1056     if((mb < fCentBinLowLimit) || (mb > fCentBinHighLimit)) continue;
1057     
1058     for(Int_t edB=0; edB<fEDbins; edB++){
1059       for(Int_t c1=0; c1<2; c1++){
1060         for(Int_t c2=0; c2<2; c2++){
1061           for(Int_t term=0; term<2; term++){
1062             
1063             TString *nameEx2 = new TString("TwoParticle_Charge1_");
1064             *nameEx2 += c1;
1065             nameEx2->Append("_Charge2_");
1066             *nameEx2 += c2;
1067             nameEx2->Append("_M_");
1068             *nameEx2 += mb;
1069             nameEx2->Append("_ED_");
1070             *nameEx2 += edB;
1071             nameEx2->Append("_Term_");
1072             *nameEx2 += term+1;
1073             
1074             if( (c1+c2)==1 ) {if(c1!=0) continue;}// skip degenerate histogram
1075             
1076             
1077             Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fTerms2 = new TH2D(nameEx2->Data(),"Two Particle Distribution",20,0,1, fQbinsQ2,0,fQupperBoundQ2);
1078             fOutputList->Add(Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fTerms2);
1079             TString *nameEx2QW=new TString(nameEx2->Data());
1080             nameEx2QW->Append("_QW");
1081             Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fTerms2QW = new TH2D(nameEx2QW->Data(),"Two Particle Distribution",20,0,1, fQbinsQ2,0,fQupperBoundQ2);
1082             fOutputList->Add(Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fTerms2QW);
1083             TString *nameAvgP=new TString(nameEx2->Data());
1084             nameAvgP->Append("_AvgP");
1085             Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fAvgP = new TProfile2D(nameAvgP->Data(),"",10,0,1, fQbinsQ2,0,fQupperBoundQ2, 0.,1.0,"");
1086             fOutputList->Add(Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fAvgP);
1087             
1088             TString *nameUnitMult=new TString(nameEx2->Data());
1089             nameUnitMult->Append("_UnitMult");
1090             Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fUnitMultBin = new TH2D(nameUnitMult->Data(),"Two Particle Distribution",21,0.5,21.5, fQbinsQ2,0,fQupperBoundQ2);
1091             fOutputList->Add(Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fUnitMultBin);
1092             
1093             if(fMCcase){
1094               // Momentum resolution histos
1095               TString *nameIdeal = new TString(nameEx2->Data());
1096               nameIdeal->Append("_Ideal");
1097               Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fIdeal = new TH2D(nameIdeal->Data(),"Two Particle Distribution",11,0.5,11.5, fQbinsQ2,0,fQupperBoundQ2);
1098               if(mb==0) fOutputList->Add(Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fIdeal);
1099               TString *nameSmeared = new TString(nameEx2->Data());
1100               nameSmeared->Append("_Smeared");
1101               Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fSmeared = new TH2D(nameSmeared->Data(),"Two Particle Distribution",11,0.5,11.5, fQbinsQ2,0,fQupperBoundQ2);
1102               if(mb==0) fOutputList->Add(Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fSmeared);
1103               //
1104               // Muon correction histos
1105               TString *nameMuonIdeal=new TString(nameEx2->Data());
1106               nameMuonIdeal->Append("_MuonIdeal");
1107               Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fMuonIdeal = new TH2D(nameMuonIdeal->Data(),"", 11,0.5,11.5, fQbinsQ2,0,fQupperBoundQ2);
1108               if(mb==0 && edB==0) fOutputList->Add(Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fMuonIdeal);
1109               TString *nameMuonSmeared=new TString(nameEx2->Data());
1110               nameMuonSmeared->Append("_MuonSmeared");
1111               Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fMuonSmeared = new TH2D(nameMuonSmeared->Data(),"", 11,0.5,11.5, fQbinsQ2,0,fQupperBoundQ2);
1112               if(mb==0 && edB==0) fOutputList->Add(Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fMuonSmeared);
1113               //
1114               TString *nameMuonPionK2=new TString(nameEx2->Data());
1115               nameMuonPionK2->Append("_MuonPionK2");
1116               Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fMuonPionK2 = new TH2D(nameMuonPionK2->Data(),"", 11,0.5,11.5, fQbinsQ2,0,fQupperBoundQ2);
1117               if(mb==0 && edB==0) fOutputList->Add(Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fMuonPionK2);
1118               //
1119               TString *namePionPionK2=new TString(nameEx2->Data());
1120               namePionPionK2->Append("_PionPionK2");
1121               Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fPionPionK2 = new TH2D(namePionPionK2->Data(),"", 11,0.5,11.5, fQbinsQ2,0,fQupperBoundQ2);
1122               if(mb==0 && edB==0) fOutputList->Add(Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fPionPionK2);
1123               //
1124               //
1125               TString *nameEx2MC=new TString(nameEx2->Data());
1126               nameEx2MC->Append("_MCqinv");
1127               Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fMCqinv = new TH1D(nameEx2MC->Data(),"", fQbinsQ2,0,fQupperBoundQ2);
1128               fOutputList->Add(Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fMCqinv);
1129               TString *nameEx2MCQW=new TString(nameEx2->Data());
1130               nameEx2MCQW->Append("_MCqinvQW");
1131               Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fMCqinvQW = new TH1D(nameEx2MCQW->Data(),"", fQbinsQ2,0,fQupperBoundQ2);
1132               fOutputList->Add(Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fMCqinvQW);
1133               //
1134               TString *nameEx2PIDpurityDen=new TString(nameEx2->Data());
1135               nameEx2PIDpurityDen->Append("_PIDpurityDen");
1136               Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fPIDpurityDen = new TH2D(nameEx2PIDpurityDen->Data(),"Two Particle Distribution",20,0,1, fQbinsQ2,0,fQupperBoundQ2);
1137               fOutputList->Add(Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fPIDpurityDen);
1138               TString *nameEx2PIDpurityNum=new TString(nameEx2->Data());
1139               nameEx2PIDpurityNum->Append("_PIDpurityNum");
1140               Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fPIDpurityNum = new TH3D(nameEx2PIDpurityNum->Data(),"Two Particle Distribution",16,0.5,16.5, 20,0,1, fQbinsQ2,0,fQupperBoundQ2);
1141               fOutputList->Add(Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].fPIDpurityNum);
1142             }
1143             TString *nameEx2OSLB1 = new TString(nameEx2->Data()); 
1144             nameEx2OSLB1->Append("_osl_b1");
1145             Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].OSL_ktbin[0].fTerms2OSL = new TH3D(nameEx2OSLB1->Data(),"Two Particle Distribution",kQbinsWeights,0,fQupperBoundWeights, kQbinsWeights,0,fQupperBoundWeights, kQbinsWeights,0,fQupperBoundWeights);
1146             fOutputList->Add(Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].OSL_ktbin[0].fTerms2OSL);
1147             nameEx2OSLB1->Append("_QW");
1148             Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].OSL_ktbin[0].fTerms2OSLQW = new TH3D(nameEx2OSLB1->Data(),"Two Particle Distribution",kQbinsWeights,0,fQupperBoundWeights, kQbinsWeights,0,fQupperBoundWeights, kQbinsWeights,0,fQupperBoundWeights);
1149             fOutputList->Add(Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].OSL_ktbin[0].fTerms2OSLQW);
1150             //
1151             TString *nameEx2OSLB2 = new TString(nameEx2->Data()); 
1152             nameEx2OSLB2->Append("_osl_b2");
1153             Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].OSL_ktbin[1].fTerms2OSL = new TH3D(nameEx2OSLB2->Data(),"Two Particle Distribution",kQbinsWeights,0,fQupperBoundWeights, kQbinsWeights,0,fQupperBoundWeights, kQbinsWeights,0,fQupperBoundWeights);
1154             fOutputList->Add(Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].OSL_ktbin[1].fTerms2OSL);
1155             nameEx2OSLB2->Append("_QW");
1156             Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].OSL_ktbin[1].fTerms2OSLQW = new TH3D(nameEx2OSLB2->Data(),"Two Particle Distribution",kQbinsWeights,0,fQupperBoundWeights, kQbinsWeights,0,fQupperBoundWeights, kQbinsWeights,0,fQupperBoundWeights);
1157             fOutputList->Add(Charge1[c1].Charge2[c2].MB[mb].EDB[edB].TwoPT[term].OSL_ktbin[1].fTerms2OSLQW);
1158             
1159           }// term_2
1160           
1161           
1162           
1163           // skip 3-particle if Tabulate6DPairs is true
1164           if(fTabulatePairs) continue;
1165           
1166           for(Int_t c3=0; c3<2; c3++){
1167             for(Int_t term=0; term<5; term++){
1168               
1169               TString *namePC3 = new TString("ThreeParticle_Charge1_");
1170               *namePC3 += c1;
1171               namePC3->Append("_Charge2_");
1172               *namePC3 += c2;
1173               namePC3->Append("_Charge3_");
1174               *namePC3 += c3;
1175               namePC3->Append("_M_");
1176               *namePC3 += mb;
1177               namePC3->Append("_ED_");
1178               *namePC3 += edB;
1179               namePC3->Append("_Term_");
1180               *namePC3 += term+1;
1181               
1182               ///////////////////////////////////////
1183               // skip degenerate histograms
1184               if( (c1+c2+c3)==1) {if(c3!=1) continue;}
1185               if( (c1+c2+c3)==2) {if(c1!=0) continue;}
1186               /////////////////////////////////////////
1187               
1188               
1189               TString *nameNorm=new TString(namePC3->Data());
1190               nameNorm->Append("_Norm");
1191               Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fNorm3 = new TH1D(nameNorm->Data(),"Norm",1,-0.5,0.5);
1192               fOutputList->Add(Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fNorm3);
1193               //
1194               
1195               TString *name1DQ=new TString(namePC3->Data());
1196               name1DQ->Append("_1D");
1197               Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fTerms3 = new TH1D(name1DQ->Data(),"", fQbinsQ3,0,fQupperBoundQ3);
1198               fOutputList->Add(Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fTerms3);
1199               //
1200               TString *nameKfactor=new TString(namePC3->Data());
1201               nameKfactor->Append("_Kfactor");
1202               Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fKfactor = new TProfile(nameKfactor->Data(),"", fQbinsQ3,0,fQupperBoundQ3, 0,100, "");
1203               fOutputList->Add(Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fKfactor);
1204               //
1205               TString *nameKfactorW=new TString(namePC3->Data());
1206               nameKfactorW->Append("_KfactorWeighted");
1207               Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fKfactorWeighted = new TProfile(nameKfactorW->Data(),"", fQbinsQ3,0,fQupperBoundQ3, 0,100, "");
1208               fOutputList->Add(Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fKfactorWeighted);
1209               //
1210               TString *nameMeanQinv=new TString(namePC3->Data());
1211               nameMeanQinv->Append("_MeanQinv");
1212               Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fMeanQinv = new TProfile(nameMeanQinv->Data(),"", fQbinsQ3,0,fQupperBoundQ3, 0,.2, "");
1213               fOutputList->Add(Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fMeanQinv);
1214               
1215               if(fMCcase==kTRUE){
1216                 // Momentum resolution correction histos
1217                 TString *nameMomResIdeal=new TString(namePC3->Data());
1218                 nameMomResIdeal->Append("_Ideal");
1219                 Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fIdeal = new TH2D(nameMomResIdeal->Data(),"", 11,0.5,11.5, fQbinsQ3,0,fQupperBoundQ3);
1220                 if(mb==0) fOutputList->Add(Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fIdeal);
1221                 TString *nameMomResSmeared=new TString(namePC3->Data());
1222                 nameMomResSmeared->Append("_Smeared");
1223                 Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fSmeared = new TH2D(nameMomResSmeared->Data(),"", 11,0.5,11.5, fQbinsQ3,0,fQupperBoundQ3);
1224                 if(mb==0) fOutputList->Add(Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fSmeared);
1225                 // Muon correction histos
1226                 TString *nameMuonIdeal=new TString(namePC3->Data());
1227                 nameMuonIdeal->Append("_MuonIdeal");
1228                 Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fMuonIdeal = new TH3D(nameMuonIdeal->Data(),"", 2,0.5,2.5, 11,0.5,11.5, fQbinsQ3,0,fQupperBoundQ3);
1229                 if(mb==0 && edB==0 && term<4) fOutputList->Add(Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fMuonIdeal);
1230                 TString *nameMuonSmeared=new TString(namePC3->Data());
1231                 nameMuonSmeared->Append("_MuonSmeared");
1232                 Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fMuonSmeared = new TH3D(nameMuonSmeared->Data(),"", 2,0.5,2.5, 11,0.5,11.5, fQbinsQ3,0,fQupperBoundQ3);
1233                 if(mb==0 && edB==0 && term<4) fOutputList->Add(Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fMuonSmeared);
1234                 //
1235                 TString *nameMuonPionK3=new TString(namePC3->Data());
1236                 nameMuonPionK3->Append("_MuonPionK3");
1237                 Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fMuonPionK3 = new TH3D(nameMuonPionK3->Data(),"", 2,0.5,2.5, 11,0.5,11.5, fQbinsQ3,0,fQupperBoundQ3);
1238                 if(mb==0 && edB==0 && term<4) fOutputList->Add(Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fMuonPionK3);
1239                 //
1240                 TString *namePionPionK3=new TString(namePC3->Data());
1241                 namePionPionK3->Append("_PionPionK3");
1242                 Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fPionPionK3 = new TH3D(namePionPionK3->Data(),"", 2,0.5,2.5, 11,0.5,11.5, fQbinsQ3,0,fQupperBoundQ3);
1243                 if(mb==0 && edB==0 && term<4) fOutputList->Add(Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fPionPionK3);
1244                 
1245               }// MCcase
1246               //
1247               if(c1==c2 && c1==c3 && term==4 ){
1248                 TString *nameTwoPartNorm=new TString(namePC3->Data());
1249                 nameTwoPartNorm->Append("_TwoPartNorm");
1250                 Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fTwoPartNorm = new TH2D(nameTwoPartNorm->Data(),"", kDENtypes,0.5,kDENtypes+0.5, fQbinsQ3,0,fQupperBoundQ3);
1251                 fOutputList->Add(Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fTwoPartNorm);
1252                 //
1253                 TString *nameTwoPartNegNorm=new TString(namePC3->Data());
1254                 nameTwoPartNegNorm->Append("_TwoPartNegNorm");
1255                 Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fTwoPartNegNorm = new TH2D(nameTwoPartNegNorm->Data(),"", kDENtypes,0.5,kDENtypes+0.5, fQbinsQ3,0,fQupperBoundQ3);
1256                 fOutputList->Add(Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fTwoPartNegNorm);
1257                 //
1258                 TString *nameTwoPartNormErr=new TString(namePC3->Data());
1259                 nameTwoPartNormErr->Append("_TwoPartNormErr");
1260                 Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fTwoPartNormErr = new TH2D(nameTwoPartNormErr->Data(),"", kDENtypes,0.5,kDENtypes+0.5, fQbinsQ3,0,fQupperBoundQ3);
1261                 fOutputList->Add(Charge1[c1].Charge2[c2].Charge3[c3].MB[mb].EDB[edB].ThreePT[term].fTwoPartNormErr);
1262               }// term=4
1263               
1264             }// term_3
1265             
1266             for(Int_t c4=0; c4<2; c4++){
1267               for(Int_t term=0; term<13; term++){
1268                 
1269                 TString *namePC4 = new TString("FourParticle_Charge1_");
1270                 *namePC4 += c1;
1271                 namePC4->Append("_Charge2_");
1272                 *namePC4 += c2;
1273                 namePC4->Append("_Charge3_");
1274                 *namePC4 += c3;
1275                 namePC4->Append("_Charge4_");
1276                 *namePC4 += c4;
1277                 namePC4->Append("_M_");
1278                 *namePC4 += mb;
1279                 namePC4->Append("_ED_");
1280                 *namePC4 += edB;
1281                 namePC4->Append("_Term_");
1282                 *namePC4 += term+1;
1283                 
1284                 ///////////////////////////////////////
1285                 // skip degenerate histograms
1286                 if( (c1+c2+c3+c4)==1) {if(c4!=1) continue;}
1287                 if( (c1+c2+c3+c4)==2) {if(c3+c4!=2) continue;}
1288                 if( (c1+c2+c3+c4)==3) {if(c1!=0) continue;}
1289                 /////////////////////////////////////////
1290                 
1291                 TString *nameNorm=new TString(namePC4->Data());
1292                 nameNorm->Append("_Norm");
1293                 Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fNorm4 = new TH1D(nameNorm->Data(),"Norm",1,-0.5,0.5);
1294                 fOutputList->Add(Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fNorm4);
1295                 //
1296                 TString *name1DQ=new TString(namePC4->Data());
1297                 name1DQ->Append("_1D");
1298                 Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fTerms4 = new TH1D(name1DQ->Data(),"", fQbinsQ4,0,fQupperBoundQ4);
1299                 fOutputList->Add(Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fTerms4);
1300                 //
1301                 TString *nameKfactor=new TString(namePC4->Data());
1302                 nameKfactor->Append("_Kfactor");
1303                 Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fKfactor = new TProfile(nameKfactor->Data(),"", fQbinsQ4,0,fQupperBoundQ4, 0,100, "");
1304                 fOutputList->Add(Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fKfactor);
1305                 //
1306                 TString *nameKfactorW=new TString(namePC4->Data());
1307                 nameKfactorW->Append("_KfactorWeighted");
1308                 Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fKfactorWeighted = new TProfile(nameKfactorW->Data(),"", fQbinsQ4,0,fQupperBoundQ4, 0,100, "");
1309                 fOutputList->Add(Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fKfactorWeighted);
1310                 //
1311                 if(c1==c2 && c1==c3 && c1==c4 && term==12 ){
1312                   TString *nameTwoPartNorm=new TString(namePC4->Data());
1313                   nameTwoPartNorm->Append("_TwoPartNorm");
1314                   Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fTwoPartNorm = new TH2D(nameTwoPartNorm->Data(),"", kDENtypes,0.5,kDENtypes+0.5, fQbinsQ4,0,fQupperBoundQ4);
1315                   fOutputList->Add(Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fTwoPartNorm);
1316                   //
1317                   TString *nameTwoPartNegNorm=new TString(namePC4->Data());
1318                   nameTwoPartNegNorm->Append("_TwoPartNegNorm");
1319                   Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fTwoPartNegNorm = new TH2D(nameTwoPartNegNorm->Data(),"", kDENtypes,0.5,kDENtypes+0.5, fQbinsQ4,0,fQupperBoundQ4);
1320                   fOutputList->Add(Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fTwoPartNegNorm);
1321                   //
1322                   TString *nameTwoPartNormErr=new TString(namePC4->Data());
1323                   nameTwoPartNormErr->Append("_TwoPartNormErr");
1324                   Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fTwoPartNormErr = new TH2D(nameTwoPartNormErr->Data(),"", kDENtypes,0.5,kDENtypes+0.5, fQbinsQ4,0,fQupperBoundQ4);
1325                   fOutputList->Add(Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fTwoPartNormErr);
1326                 }
1327                 
1328                 if(fMCcase==kTRUE){
1329                   // Momentum resolution correction histos
1330                   TString *nameMomResIdeal=new TString(namePC4->Data());
1331                   nameMomResIdeal->Append("_Ideal");
1332                   Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fIdeal = new TH2D(nameMomResIdeal->Data(),"", 11,0.5,11.5, fQbinsQ4,0,fQupperBoundQ4);
1333                   if(mb==0) fOutputList->Add(Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fIdeal);
1334                   TString *nameMomResSmeared=new TString(namePC4->Data());
1335                   nameMomResSmeared->Append("_Smeared");
1336                   Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fSmeared = new TH2D(nameMomResSmeared->Data(),"", 11,0.5,11.5, fQbinsQ4,0,fQupperBoundQ4);
1337                   if(mb==0) fOutputList->Add(Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fSmeared);
1338                   // Muon correction histos
1339                   TString *nameMuonIdeal=new TString(namePC4->Data());
1340                   nameMuonIdeal->Append("_MuonIdeal");
1341                   Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fMuonIdeal = new TH3D(nameMuonIdeal->Data(),"", 2,0.5,2.5, 11,0.5,11.5, fQbinsQ4,0,fQupperBoundQ4);
1342                   if(mb==0 && edB==0 && term<12) fOutputList->Add(Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fMuonIdeal);
1343                   TString *nameMuonSmeared=new TString(namePC4->Data());
1344                   nameMuonSmeared->Append("_MuonSmeared");
1345                   Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fMuonSmeared = new TH3D(nameMuonSmeared->Data(),"", 2,0.5,2.5, 11,0.5,11.5, fQbinsQ4,0,fQupperBoundQ4);
1346                   if(mb==0 && edB==0 && term<12) fOutputList->Add(Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fMuonSmeared);
1347                   //
1348                   TString *nameMuonPionK4=new TString(namePC4->Data());
1349                   nameMuonPionK4->Append("_MuonPionK4");
1350                   Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fMuonPionK4 = new TH3D(nameMuonPionK4->Data(),"", 2,0.5,2.5, 11,0.5,11.5, fQbinsQ4,0,fQupperBoundQ4);
1351                   if(mb==0 && edB==0 && term<12) fOutputList->Add(Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fMuonPionK4);
1352                   //
1353                   TString *namePionPionK4=new TString(namePC4->Data());
1354                   namePionPionK4->Append("_PionPionK4");
1355                   Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fPionPionK4 = new TH3D(namePionPionK4->Data(),"", 2,0.5,2.5, 11,0.5,11.5, fQbinsQ4,0,fQupperBoundQ4);
1356                   if(mb==0 && edB==0 && term<12) fOutputList->Add(Charge1[c1].Charge2[c2].Charge3[c3].Charge4[c4].MB[mb].EDB[edB].FourPT[term].fPionPionK4);
1357                   
1358                 }// MCcase
1359                 
1360                 
1361               }
1362             }
1363             
1364           }//c3
1365         }//c2
1366       }//c1
1367     }// ED
1368   }// mbin
1369
1370
1371   
1372   if(fTabulatePairs){
1373     
1374     for(Int_t tKbin=0; tKbin<fKbinsT; tKbin++){
1375       for(Int_t yKbin=0; yKbin<fKbinsY; yKbin++){
1376         for(Int_t mb=0; mb<fMbins; mb++){
1377           for(Int_t edB=0; edB<fEDbins; edB++){
1378             
1379             TString *nameNum = new TString("TPN_num_Kt_");
1380             *nameNum += tKbin;
1381             nameNum->Append("_Ky_");
1382             *nameNum += yKbin;
1383             nameNum->Append("_M_");
1384             *nameNum += mb;
1385             nameNum->Append("_ED_");
1386             *nameNum += edB;
1387             
1388             TString *nameDen = new TString("TPN_den_Kt_");
1389             *nameDen += tKbin;
1390             nameDen->Append("_Ky_");
1391             *nameDen += yKbin;
1392             nameDen->Append("_M_");
1393             *nameDen += mb;
1394             nameDen->Append("_ED_");
1395             *nameDen += edB;
1396             
1397             if(edB==0){
1398               KT[tKbin].KY[yKbin].MB[mb].EDB[edB].TwoPT[0].fTerms2ThreeD = new TH3D(nameNum->Data(),"", kQbinsWeights,0,fQupperBoundWeights, kQbinsWeights,0,fQupperBoundWeights, kQbinsWeights,0,fQupperBoundWeights);
1399               fOutputList->Add(KT[tKbin].KY[yKbin].MB[mb].EDB[edB].TwoPT[0].fTerms2ThreeD);
1400               
1401               KT[tKbin].KY[yKbin].MB[mb].EDB[edB].TwoPT[1].fTerms2ThreeD = new TH3D(nameDen->Data(),"", kQbinsWeights,0,fQupperBoundWeights, kQbinsWeights,0,fQupperBoundWeights, kQbinsWeights,0,fQupperBoundWeights);
1402               fOutputList->Add(KT[tKbin].KY[yKbin].MB[mb].EDB[edB].TwoPT[1].fTerms2ThreeD);
1403             }   
1404           
1405           }
1406         }
1407       }
1408     }
1409     
1410   }
1411   
1412   
1413   TProfile *fQsmearMean = new TProfile("fQsmearMean","",2,0.5,2.5, -0.2,0.2,"");
1414   fOutputList->Add(fQsmearMean);
1415   TProfile *fQsmearSq = new TProfile("fQsmearSq","",2,0.5,2.5, -2,2,"");
1416   fOutputList->Add(fQsmearSq);
1417   TH2D *fQ2Res = new TH2D("fQ2Res","",20,0,1, 200,-.2,.2);
1418   fOutputList->Add(fQ2Res);
1419   TH2D *fQ3Res = new TH2D("fQ3Res","",20,0,1, 200,-.3,.3);
1420   fOutputList->Add(fQ3Res);
1421   TH2D *fQ4Res = new TH2D("fQ4Res","",20,0,1, 200,-.4,.4);
1422   fOutputList->Add(fQ4Res);
1423   
1424   TH2D *DistQinv4pion = new TH2D("DistQinv4pion","",6,0.5,6.5, 20,0,0.1);
1425   fOutputList->Add(DistQinv4pion);
1426   TH2D *DistQinvMC4pion = new TH2D("DistQinvMC4pion","",6,0.5,6.5, 20,0,0.1);
1427   if(fMCcase) fOutputList->Add(DistQinvMC4pion);
1428
1429   TH2D *fAvgQ12VersusQ3 = new TH2D("fAvgQ12VersusQ3","",10,0,0.1, 20,0,0.1);
1430   fOutputList->Add(fAvgQ12VersusQ3);
1431   TH2D *fAvgQ13VersusQ3 = new TH2D("fAvgQ13VersusQ3","",10,0,0.1, 20,0,0.1);
1432   fOutputList->Add(fAvgQ13VersusQ3);
1433   TH2D *fAvgQ23VersusQ3 = new TH2D("fAvgQ23VersusQ3","",10,0,0.1, 20,0,0.1);
1434   fOutputList->Add(fAvgQ23VersusQ3);
1435
1436   TH1D *fDistPionParents4 = new TH1D("fDistPionParents4","",4,0.5,4.5);
1437   fOutputList->Add(fDistPionParents4);
1438
1439   TH2D *fDistTPCNclsFindable = new TH2D("fDistTPCNclsFindable","", 100,0,0.5, 201,-0.5,200.5);
1440   fDistTPCNclsFindable->GetXaxis()->SetTitle("pT (GeV/c)"); fDistTPCNclsFindable->GetYaxis()->SetTitle("Ncls Findable");
1441   fOutputList->Add(fDistTPCNclsFindable);
1442   TProfile *fProfileTPCNclsFindable = new TProfile("fProfileTPCNclsFindable","",100,0,0.5, 0,200, "");
1443   fProfileTPCNclsFindable->GetXaxis()->SetTitle("pT (GeV/c)"); fProfileTPCNclsFindable->GetYaxis()->SetTitle("<Ncls Findable>");
1444   fOutputList->Add(fProfileTPCNclsFindable);
1445   //
1446   TH2D *fDistTPCNclsCrossed = new TH2D("fDistTPCNclsCrossed","",100,0,0.5, 201,-0.5,200.5);
1447   fDistTPCNclsCrossed->GetXaxis()->SetTitle("pT (GeV/c)"); fDistTPCNclsCrossed->GetYaxis()->SetTitle("Ncls Crossed");
1448   fOutputList->Add(fDistTPCNclsCrossed);
1449   TProfile *fProfileTPCNclsCrossed = new TProfile("fProfileTPCNclsCrossed","",100,0,0.5, 0,200, "");
1450   fProfileTPCNclsCrossed->GetXaxis()->SetTitle("pT (GeV/c)"); fProfileTPCNclsCrossed->GetYaxis()->SetTitle("<Ncls Crossed>");
1451   fOutputList->Add(fProfileTPCNclsCrossed);
1452   //
1453   TH2D *fDistTPCNclsFindableRatio = new TH2D("fDistTPCNclsFindableRatio","",100,0,0.5, 100,0,1);
1454   fDistTPCNclsFindableRatio->GetXaxis()->SetTitle("pT (GeV/c)"); fDistTPCNclsFindableRatio->GetYaxis()->SetTitle("Ncls / Ncls Findable");
1455   fOutputList->Add(fDistTPCNclsFindableRatio);
1456   TProfile *fProfileTPCNclsFindableRatio = new TProfile("fProfileTPCNclsFindableRatio","",100,0,0.5, 0,1, "");
1457   fProfileTPCNclsFindableRatio->GetXaxis()->SetTitle("pT (GeV/c)"); fProfileTPCNclsFindableRatio->GetYaxis()->SetTitle("<Ncls / Ncls Findable>");
1458   fOutputList->Add(fProfileTPCNclsFindableRatio);
1459   //
1460   TH2D *fDistTPCNclsCrossedRatio = new TH2D("fDistTPCNclsCrossedRatio","",100,0,0.5, 100,0,1);
1461   fDistTPCNclsCrossedRatio->GetXaxis()->SetTitle("pT (GeV/c)"); fDistTPCNclsCrossedRatio->GetYaxis()->SetTitle("Ncls / Ncls Crossed");
1462   fOutputList->Add(fDistTPCNclsCrossedRatio);
1463   TProfile *fProfileTPCNclsCrossedRatio = new TProfile("fProfileTPCNclsCrossedRatio","",100,0,0.5, 0,1, "");
1464   fProfileTPCNclsCrossedRatio->GetXaxis()->SetTitle("pT (GeV/c)"); fProfileTPCNclsCrossedRatio->GetYaxis()->SetTitle("<Ncls / Ncls Crossed>");
1465   fOutputList->Add(fProfileTPCNclsCrossedRatio);
1466
1467   TH2D *fc4QSFitNum = new TH2D("fc4QSFitNum","",7,0.5,7.5, fQbinsQ4,0,fQupperBoundQ4);
1468   fOutputList->Add(fc4QSFitNum);
1469   TH2D *fc4QSFitDen = new TH2D("fc4QSFitDen","",7,0.5,7.5, fQbinsQ4,0,fQupperBoundQ4);
1470   fOutputList->Add(fc4QSFitDen);
1471   
1472   ////////////////////////////////////
1473   ///////////////////////////////////  
1474   
1475   PostData(1, fOutputList);
1476 }
1477
1478 //________________________________________________________________________
1479 void AliFourPion::UserExec(Option_t *) 
1480 {
1481   // Main loop
1482   // Called for each event
1483   //cout<<"===========  Event # "<<fEventCounter+1<<"  ==========="<<endl;
1484   fEventCounter++;
1485   
1486   if(!fAODcase) {cout<<"ESDs not supported"<<endl; return;}
1487   
1488   fAOD = dynamic_cast<AliAODEvent*> (InputEvent());
1489   if (!fAOD) {Printf("ERROR: fAOD not available"); return;}
1490   
1491   
1492   // Trigger Cut
1493   if(fAOD->GetRunNumber() >= 136851 && fAOD->GetRunNumber() <= 139517){// 10h data
1494   Bool_t isSelected1 = (((AliInputEventHandler*)(AliAnalysisManager::GetAnalysisManager()->GetInputEventHandler()))->IsEventSelected() & AliVEvent::kMB);
1495   if(!isSelected1 && !fMCcase) {return;}
1496   }else if(fAOD->GetRunNumber() >= 167693 && fAOD->GetRunNumber() <= 170593){// 11h data
1497     Bool_t isSelected1 = (((AliInputEventHandler*)(AliAnalysisManager::GetAnalysisManager()->GetInputEventHandler()))->IsEventSelected() & AliVEvent::kCentral);
1498     Bool_t isSelected2 = (((AliInputEventHandler*)(AliAnalysisManager::GetAnalysisManager()->GetInputEventHandler()))->IsEventSelected() & AliVEvent::kSemiCentral);
1499     if(!isSelected1 && !isSelected2 && !fMCcase) {return;}
1500   }else {return;}
1501
1502   ///////////////////////////////////////////////////////////
1503   const AliAODVertex *primaryVertexAOD;
1504   AliCentrality *centrality;// for AODs and ESDs
1505
1506  
1507   AliAnalysisManager *man=AliAnalysisManager::GetAnalysisManager();
1508   AliInputEventHandler* inputHandler = (AliInputEventHandler*) (man->GetInputEventHandler());
1509   fPIDResponse = inputHandler->GetPIDResponse();
1510
1511   
1512   TClonesArray *mcArray = 0x0;
1513   if(fMCcase){
1514     if(fAODcase){ 
1515       mcArray = (TClonesArray*)fAOD->FindListObject(AliAODMCParticle::StdBranchName());
1516       if(!mcArray || mcArray->GetEntriesFast() >= kMCarrayLimit){
1517         cout<<"No MC particle branch found or Array too large!!"<<endl;
1518         return;
1519       }
1520     }
1521   }
1522   
1523
1524   UInt_t status=0;
1525   Int_t positiveTracks=0, negativeTracks=0;
1526   Int_t myTracks=0, pionCount=0, kaonCount=0, protonCount=0;
1527    
1528   Double_t vertex[3]={0};
1529   Int_t zbin=0;
1530   Double_t zstep=2*10/Double_t(fZvertexBins), zstart=-10.;
1531   /////////////////////////////////////////////////
1532
1533   
1534   Float_t centralityPercentile=0;
1535   Float_t cStep=5.0, cStepMixing=1.0, cStart=0;
1536   Int_t MbinMixing=0;
1537  
1538   if(fAODcase){// AOD case
1539     
1540     if(fPbPbcase){
1541       centrality = fAOD->GetCentrality();
1542       centralityPercentile = centrality->GetCentralityPercentile("V0M");
1543       if(centralityPercentile == 0) {cout<<"Centrality = 0, skipping event"<<endl; return;}
1544       if((centralityPercentile < 5*fCentBinLowLimit) || (centralityPercentile>= 5*(fCentBinHighLimit+1))) {/*cout<<"Centrality out of Range.  Skipping Event"<<endl;*/ return;}
1545       cout<<"Centrality % = "<<centralityPercentile<<endl;
1546     }
1547     
1548     
1549     ((TH1F*)fOutputList->FindObject("fMultDist0"))->Fill(fAOD->GetNumberOfTracks());
1550
1551     // Pile-up rejection
1552     AliAnalysisUtils *AnaUtil=new AliAnalysisUtils();
1553     if(!fPbPbcase) AnaUtil->SetUseMVPlpSelection(kTRUE);// use Multi-Vertex tool for pp and pPb
1554     else AnaUtil->SetUseMVPlpSelection(kFALSE);
1555     Bool_t pileUpCase=AnaUtil->IsPileUpEvent(fAOD); 
1556     if(pileUpCase) return;
1557     
1558     ////////////////////////////////
1559     // Vertexing
1560     ((TH1F*)fOutputList->FindObject("fMultDist1"))->Fill(fAOD->GetNumberOfTracks());
1561     primaryVertexAOD = fAOD->GetPrimaryVertex();
1562     vertex[0]=primaryVertexAOD->GetX(); vertex[1]=primaryVertexAOD->GetY(); vertex[2]=primaryVertexAOD->GetZ();
1563     
1564     if(fabs(vertex[2]) > 10) {cout<<"Zvertex Out of Range. Skip Event"<<endl; return;} // Z-Vertex Cut 
1565     ((TH3F*)fOutputList->FindObject("fVertexDist"))->Fill(vertex[0], vertex[1], vertex[2]);
1566     
1567     if(!fMCcase && primaryVertexAOD->GetNContributors() < 1) {cout<<"Bad Vertex. Skip Event"<<endl; return;}
1568    
1569     ((TH1F*)fOutputList->FindObject("fMultDist2"))->Fill(fAOD->GetNumberOfTracks());
1570  
1571     fBfield = fAOD->GetMagneticField();
1572     
1573     for(Int_t i=0; i<fZvertexBins; i++){
1574       if( (vertex[2] >= zstart+i*zstep) && (vertex[2] < zstart+(i+1)*zstep) ){
1575         zbin=i;
1576         break;
1577       }
1578     }
1579
1580    
1581        
1582     /////////////////////////////
1583     // Create Shuffled index list
1584     Int_t randomIndex[fAOD->GetNumberOfTracks()];
1585     for (Int_t i = 0; i < fAOD->GetNumberOfTracks(); i++) randomIndex[i]=i;
1586     Shuffle(randomIndex,0,fAOD->GetNumberOfTracks()-1);
1587     /////////////////////////////
1588   
1589    
1590     // Track loop
1591     for (Int_t i = 0; i < fAOD->GetNumberOfTracks(); i++) {
1592       AliAODTrack* aodtrack = fAOD->GetTrack(randomIndex[i]);
1593       if (!aodtrack) continue;
1594       if(myTracks >= fMultLimit) {cout<<"More tracks than Track Limit"<<endl; return;}
1595     
1596       status=aodtrack->GetStatus();
1597       
1598       if(!aodtrack->TestFilterBit(BIT(fFilterBit))) continue;// AOD filterBit cut
1599       ((TH2D*)fOutputList->FindObject("fTrackChi2NDF"))->Fill(centralityPercentile, aodtrack->Chi2perNDF());
1600       ((TH2D*)fOutputList->FindObject("fTrackTPCncls"))->Fill(centralityPercentile, aodtrack->GetTPCncls());
1601       if(aodtrack->GetTPCNcls() < fMinTPCncls) continue;// TPC nCluster cut
1602       if(aodtrack->Chi2perNDF() > fMaxChi2NDF) continue;
1603
1604       if(fFilterBit != 7){
1605         Bool_t goodTrackOtherFB = kFALSE;
1606         for (Int_t j = 0; j < fAOD->GetNumberOfTracks(); j++) {
1607           AliAODTrack* aodtrack2 = fAOD->GetTrack(randomIndex[j]);
1608           if(!aodtrack2) continue;
1609           if(!aodtrack2->TestFilterBit(BIT(fFilterBit))) continue;
1610           
1611           if(-(aodtrack->GetID()+1)==aodtrack2->GetID()) {goodTrackOtherFB=kTRUE; break;}
1612           
1613         }
1614         if(!goodTrackOtherFB) continue;
1615       }
1616       
1617       
1618       if(aodtrack->Pt() < 0.16) continue;
1619       if(fabs(aodtrack->Eta()) > 0.8) continue;
1620      
1621       
1622       Bool_t goodMomentum = aodtrack->GetPxPyPz( fTempStruct[myTracks].fP);
1623       if(!goodMomentum) continue; 
1624       aodtrack->GetXYZ( fTempStruct[myTracks].fX);
1625       
1626          
1627       Double_t dca2[2]={0};
1628       dca2[0] = sqrt( pow(fTempStruct[myTracks].fX[0] - vertex[0],2) + pow(fTempStruct[myTracks].fX[1] - vertex[1],2));
1629       dca2[1] = sqrt( pow(fTempStruct[myTracks].fX[2] - vertex[2],2));
1630       Double_t dca3d = sqrt( pow(dca2[0],2) + pow(dca2[1],2));
1631              
1632       fTempStruct[myTracks].fStatus = status;
1633       fTempStruct[myTracks].fFiltermap = aodtrack->GetFilterMap();
1634       fTempStruct[myTracks].fId = aodtrack->GetID();
1635       //
1636       fTempStruct[myTracks].fLabel = aodtrack->GetLabel();
1637       fTempStruct[myTracks].fPhi = atan2(fTempStruct[myTracks].fP[1], fTempStruct[myTracks].fP[0]);
1638       if(fTempStruct[myTracks].fPhi < 0) fTempStruct[myTracks].fPhi += 2*PI;
1639       fTempStruct[myTracks].fPt = sqrt(pow(fTempStruct[myTracks].fP[0],2) + pow(fTempStruct[myTracks].fP[1],2));
1640       fTempStruct[myTracks].fMom = sqrt( pow(fTempStruct[myTracks].fPt,2) + pow(fTempStruct[myTracks].fP[2],2) );
1641       fTempStruct[myTracks].fEta = aodtrack->Eta();
1642       fTempStruct[myTracks].fCharge = aodtrack->Charge();
1643       fTempStruct[myTracks].fDCAXY = dca2[0];
1644       fTempStruct[myTracks].fDCAZ = dca2[1];
1645       fTempStruct[myTracks].fDCA = dca3d;
1646       fTempStruct[myTracks].fClusterMap = aodtrack->GetTPCClusterMap();
1647       fTempStruct[myTracks].fSharedMap = aodtrack->GetTPCSharedMap();
1648       
1649     
1650       
1651       if(fTempStruct[myTracks].fMom > 0.9999) continue;// upper P bound
1652             
1653      
1654      
1655       // PID section
1656       fTempStruct[myTracks].fElectron = kFALSE;
1657       fTempStruct[myTracks].fPion = kFALSE;
1658       fTempStruct[myTracks].fKaon = kFALSE;
1659       fTempStruct[myTracks].fProton = kFALSE;
1660       
1661       Float_t nSigmaTPC[5];
1662       Float_t nSigmaTOF[5];
1663       nSigmaTPC[0]=10; nSigmaTPC[1]=10; nSigmaTPC[2]=10; nSigmaTPC[3]=10; nSigmaTPC[4]=10;
1664       nSigmaTOF[0]=10; nSigmaTOF[1]=10; nSigmaTOF[2]=10; nSigmaTOF[3]=10; nSigmaTOF[4]=10;
1665       fTempStruct[myTracks].fTOFhit = kFALSE;// default
1666       Float_t signalTPC=0, signalTOF=0;
1667       Double_t integratedTimesTOF[10]={0};
1668
1669     
1670       Bool_t DoPIDWorkAround=kTRUE;
1671       //if(fFilterBit == 7) DoPIDWorkAround=kTRUE;
1672       if(fMCcase && !fPbPbcase) DoPIDWorkAround=kFALSE;
1673       if(DoPIDWorkAround==kFALSE && fabs(fPIDResponse->NumberOfSigmasTPC(aodtrack,AliPID::kPion)) < 900) {
1674         nSigmaTPC[0]=fPIDResponse->NumberOfSigmasTPC(aodtrack,AliPID::kElectron);
1675         nSigmaTPC[1]=fPIDResponse->NumberOfSigmasTPC(aodtrack,AliPID::kMuon);
1676         nSigmaTPC[2]=fPIDResponse->NumberOfSigmasTPC(aodtrack,AliPID::kPion);
1677         nSigmaTPC[3]=fPIDResponse->NumberOfSigmasTPC(aodtrack,AliPID::kKaon);
1678         nSigmaTPC[4]=fPIDResponse->NumberOfSigmasTPC(aodtrack,AliPID::kProton);
1679         //
1680         nSigmaTOF[0]=fPIDResponse->NumberOfSigmasTOF(aodtrack,AliPID::kElectron);
1681         nSigmaTOF[1]=fPIDResponse->NumberOfSigmasTOF(aodtrack,AliPID::kMuon);
1682         nSigmaTOF[2]=fPIDResponse->NumberOfSigmasTOF(aodtrack,AliPID::kPion);
1683         nSigmaTOF[3]=fPIDResponse->NumberOfSigmasTOF(aodtrack,AliPID::kKaon);
1684         nSigmaTOF[4]=fPIDResponse->NumberOfSigmasTOF(aodtrack,AliPID::kProton);
1685         signalTPC = aodtrack->GetTPCsignal();
1686         if( (status&AliESDtrack::kTOFpid)!=0 && (status&AliESDtrack::kTIME)!=0 && (status&AliESDtrack::kTOFout)!=0 && (status&AliESDtrack::kTOFmismatch)<=0){// good tof hit
1687           fTempStruct[myTracks].fTOFhit = kTRUE;
1688           signalTOF = aodtrack->GetTOFsignal();
1689           aodtrack->GetIntegratedTimes(integratedTimesTOF);
1690         }else fTempStruct[myTracks].fTOFhit = kFALSE;
1691         
1692       }else {// FilterBit 7 PID workaround
1693         
1694         for(Int_t j = 0; j < fAOD->GetNumberOfTracks(); j++) {
1695           AliAODTrack* aodTrack2 = fAOD->GetTrack(j);
1696           if (!aodTrack2) continue;
1697           if(aodtrack->GetID() != (-aodTrack2->GetID() - 1)) continue;// (-aodTrack2->GetID() - 1)
1698           
1699           UInt_t status2=aodTrack2->GetStatus();
1700           
1701           nSigmaTPC[0]=fPIDResponse->NumberOfSigmasTPC(aodTrack2,AliPID::kElectron);
1702           nSigmaTPC[1]=fPIDResponse->NumberOfSigmasTPC(aodTrack2,AliPID::kMuon);
1703           nSigmaTPC[2]=fPIDResponse->NumberOfSigmasTPC(aodTrack2,AliPID::kPion);
1704           nSigmaTPC[3]=fPIDResponse->NumberOfSigmasTPC(aodTrack2,AliPID::kKaon);
1705           nSigmaTPC[4]=fPIDResponse->NumberOfSigmasTPC(aodTrack2,AliPID::kProton);
1706           //
1707           nSigmaTOF[0]=fPIDResponse->NumberOfSigmasTOF(aodTrack2,AliPID::kElectron);
1708           nSigmaTOF[1]=fPIDResponse->NumberOfSigmasTOF(aodTrack2,AliPID::kMuon);
1709           nSigmaTOF[2]=fPIDResponse->NumberOfSigmasTOF(aodTrack2,AliPID::kPion);
1710           nSigmaTOF[3]=fPIDResponse->NumberOfSigmasTOF(aodTrack2,AliPID::kKaon);
1711           nSigmaTOF[4]=fPIDResponse->NumberOfSigmasTOF(aodTrack2,AliPID::kProton);
1712           signalTPC = aodTrack2->GetTPCsignal();
1713           
1714           if( (status2&AliESDtrack::kTOFpid)!=0 && (status2&AliESDtrack::kTIME)!=0 && (status2&AliESDtrack::kTOFout)!=0 && (status2&AliESDtrack::kTOFmismatch)<=0){// good tof hit
1715             fTempStruct[myTracks].fTOFhit = kTRUE;
1716             signalTOF = aodTrack2->GetTOFsignal();
1717             aodTrack2->GetIntegratedTimes(integratedTimesTOF);
1718           }else fTempStruct[myTracks].fTOFhit = kFALSE;
1719           
1720           //if(aodTrack2->Pt()<0.2) cout<<aodTrack2->GetTPCNclsF()<<"  "<<aodTrack2->GetTPCNCrossedRows()<<"  "<<aodTrack2->GetTPCNcls()<<"  "<<aodTrack2->GetTPCFoundFraction()<<endl;
1721
1722
1723         }// aodTrack2
1724       }// FilterBit 7 PID workaround
1725      
1726      
1727       ///////////////////
1728       ((TH3F*)fOutputList->FindObject("fTPCResponse"))->Fill(centralityPercentile, fTempStruct[myTracks].fMom, signalTPC);
1729       if(fTempStruct[myTracks].fTOFhit) {
1730         ((TH3F*)fOutputList->FindObject("fTOFResponse"))->Fill(centralityPercentile, fTempStruct[myTracks].fMom, signalTOF - integratedTimesTOF[3]);
1731       }
1732       ///////////////////
1733       
1734       // Use TOF if good hit and above threshold
1735       if(fTempStruct[myTracks].fTOFhit && fTempStruct[myTracks].fMom > fTPCTOFboundry){
1736         if(fabs(nSigmaTOF[0])<fSigmaCutTOF) fTempStruct[myTracks].fElectron = kTRUE;// Electron candidate
1737         if(fabs(nSigmaTOF[2])<fSigmaCutTOF) fTempStruct[myTracks].fPion = kTRUE;// Pion candidate
1738         if(fabs(nSigmaTOF[3])<fSigmaCutTOF) fTempStruct[myTracks].fKaon = kTRUE;// Kaon candidate
1739         if(fabs(nSigmaTOF[4])<fSigmaCutTOF) fTempStruct[myTracks].fProton = kTRUE;// Proton candidate
1740       }else {// TPC info instead
1741         if(fabs(nSigmaTPC[0])<fSigmaCutTPC) fTempStruct[myTracks].fElectron = kTRUE;// Electron candidate
1742         if(fabs(nSigmaTPC[2])<fSigmaCutTPC) fTempStruct[myTracks].fPion = kTRUE;// Pion candidate
1743         if(fabs(nSigmaTPC[3])<fSigmaCutTPC) fTempStruct[myTracks].fKaon = kTRUE;// Kaon candidate
1744         if(fabs(nSigmaTPC[4])<fSigmaCutTPC) fTempStruct[myTracks].fProton = kTRUE;// Proton candidate
1745       }
1746       
1747     
1748       // Ensure there is only 1 candidate per track
1749       if(fTempStruct[myTracks].fElectron && fTempStruct[myTracks].fMom < 0.45) continue;// Remove electron band
1750       if(!fTempStruct[myTracks].fPion && !fTempStruct[myTracks].fKaon && !fTempStruct[myTracks].fProton) continue;
1751       if(!fTempStruct[myTracks].fPion) continue;// only pions
1752       if(fTempStruct[myTracks].fPion && fTempStruct[myTracks].fKaon) continue;
1753       if(fTempStruct[myTracks].fPion && fTempStruct[myTracks].fProton) continue;
1754       if(fTempStruct[myTracks].fKaon && fTempStruct[myTracks].fProton) continue;
1755       //if(fTempStruct[myTracks].fPion && fTempStruct[myTracks].fKaon && fTempStruct[myTracks].fProton) continue;// superfluous
1756       ////////////////////////
1757       //if(fTempStruct[myTracks].fProton && fTempStruct[myTracks].fMom < 0.25) continue;//extra cut for protons// superfluous
1758
1759
1760    
1761       if(fTempStruct[myTracks].fCharge==+1) {
1762         ((TH2F*)fOutputList->FindObject("fDCAxyDistPlus"))->Fill(fTempStruct[myTracks].fPt, dca2[0]);
1763         ((TH2F*)fOutputList->FindObject("fDCAzDistPlus"))->Fill(fTempStruct[myTracks].fPt, dca2[1]);
1764       }else {
1765         ((TH2F*)fOutputList->FindObject("fDCAxyDistMinus"))->Fill(fTempStruct[myTracks].fPt, dca2[0]);
1766         ((TH2F*)fOutputList->FindObject("fDCAzDistMinus"))->Fill(fTempStruct[myTracks].fPt, dca2[1]);
1767       }
1768      
1769       ((TH3F*)fOutputList->FindObject("fPhiPtDist"))->Fill(aodtrack->Charge(), aodtrack->Phi(), aodtrack->Pt());
1770       ((TH3F*)fOutputList->FindObject("fPtEtaDist"))->Fill(aodtrack->Charge(), aodtrack->Pt(), aodtrack->Eta());
1771       
1772       ((TH2D*)fOutputList->FindObject("fDistTPCNclsFindable"))->Fill(aodtrack->Pt(), aodtrack->GetTPCNclsF());
1773       ((TProfile*)fOutputList->FindObject("fProfileTPCNclsFindable"))->Fill(aodtrack->Pt(), aodtrack->GetTPCNclsF());
1774       //
1775       ((TH2D*)fOutputList->FindObject("fDistTPCNclsCrossed"))->Fill(aodtrack->Pt(), aodtrack->GetTPCNCrossedRows());
1776       ((TProfile*)fOutputList->FindObject("fProfileTPCNclsCrossed"))->Fill(aodtrack->Pt(), aodtrack->GetTPCNCrossedRows());
1777       //
1778       if(aodtrack->GetTPCNclsF() > 0){
1779         ((TH2D*)fOutputList->FindObject("fDistTPCNclsFindableRatio"))->Fill(aodtrack->Pt(), double(aodtrack->GetTPCNcls())/double(aodtrack->GetTPCNclsF()));
1780         ((TProfile*)fOutputList->FindObject("fProfileTPCNclsFindableRatio"))->Fill(aodtrack->Pt(), double(aodtrack->GetTPCNcls())/double(aodtrack->GetTPCNclsF()));
1781       }      
1782       // 
1783       ((TH2D*)fOutputList->FindObject("fDistTPCNclsCrossedRatio"))->Fill(aodtrack->Pt(), aodtrack->GetTPCFoundFraction());
1784       ((TProfile*)fOutputList->FindObject("fProfileTPCNclsCrossedRatio"))->Fill(aodtrack->Pt(), aodtrack->GetTPCFoundFraction());
1785       
1786
1787       if(fTempStruct[myTracks].fPion) {// pions
1788         fTempStruct[myTracks].fEaccepted = sqrt(pow(fTempStruct[myTracks].fMom,2) + pow(fTrueMassPi,2)); 
1789         fTempStruct[myTracks].fKey = 1;
1790       }else if(fTempStruct[myTracks].fKaon){// kaons
1791         fTempStruct[myTracks].fEaccepted = sqrt(pow(fTempStruct[myTracks].fMom,2) + pow(fTrueMassK,2));;
1792         fTempStruct[myTracks].fKey = 10;
1793       }else{// protons
1794         fTempStruct[myTracks].fEaccepted = sqrt(pow(fTempStruct[myTracks].fMom,2) + pow(fTrueMassP,2));;
1795         fTempStruct[myTracks].fKey = 100;
1796       }
1797       
1798            
1799
1800       if(aodtrack->Charge() > 0) positiveTracks++;
1801       else negativeTracks++;
1802       
1803       if(fTempStruct[myTracks].fPion) pionCount++;
1804       if(fTempStruct[myTracks].fKaon) kaonCount++;
1805       if(fTempStruct[myTracks].fProton) protonCount++;
1806
1807       myTracks++;
1808       
1809       if(fMCcase){// muon mothers
1810         AliAODMCParticle *tempMCTrack=(AliAODMCParticle*)mcArray->At(abs(aodtrack->GetLabel()));
1811         if(abs(tempMCTrack->GetPdgCode())==13 && tempMCTrack->GetMother()>0){// muons
1812           AliAODMCParticle *parent=(AliAODMCParticle*)mcArray->At(tempMCTrack->GetMother());
1813           if(parent->IsPhysicalPrimary()){
1814             ((TH1D*)fOutputList->FindObject("fMuonParents"))->Fill(abs(parent->GetPdgCode()));
1815           }else ((TH1D*)fOutputList->FindObject("fSecondaryMuonParents"))->Fill(abs(parent->GetPdgCode()));
1816         }
1817         ((TH1D*)fOutputList->FindObject("fPionCandidates"))->Fill(abs(tempMCTrack->GetPdgCode()));
1818       }
1819     }
1820     //cout<<"kinkcount = "<<kinkcount<<"   pionkinks = "<<pionkinks<<"   primarypionkinks = "<<primarypionkinks<<endl;
1821   }else {// ESD tracks
1822     cout<<"ESDs not supported currently"<<endl;
1823     return;
1824   }
1825   
1826   // Generator info only
1827   if(fMCcase && fGeneratorOnly){
1828     myTracks=0; pionCount=0; kaonCount=0; protonCount=0;// reset track counters
1829     for(Int_t mctrackN=0; mctrackN<mcArray->GetEntriesFast(); mctrackN++){
1830       if(myTracks >= fMultLimit) {cout<<"More tracks than Track Limit"<<endl; return;}
1831       if(myTracks >= 1300) continue;// additional cut to limit high mult events which exceed pair # limits
1832       
1833       AliAODMCParticle *mcParticle = (AliAODMCParticle*)mcArray->At(mctrackN);
1834       if(!mcParticle) continue;
1835       if(fabs(mcParticle->Eta())>0.8) continue;
1836       if(mcParticle->Charge()!=-3 && mcParticle->Charge()!=+3) continue;// x3 by convention
1837       if(mcParticle->Pt() < 0.16 || mcParticle->Pt() > 1.0) continue;
1838       if(!mcParticle->IsPrimary()) continue;
1839       if(!mcParticle->IsPhysicalPrimary()) continue;
1840       if(abs(mcParticle->GetPdgCode())!=211) continue;
1841       
1842       fTempStruct[myTracks].fP[0] = mcParticle->Px();
1843       fTempStruct[myTracks].fP[1] = mcParticle->Py();
1844       fTempStruct[myTracks].fP[2] = mcParticle->Pz();
1845       fTempStruct[myTracks].fX[0] = 0.; fTempStruct[myTracks].fX[1] = 0.; fTempStruct[myTracks].fX[2] = 0.;
1846       
1847       fTempStruct[myTracks].fId = myTracks;// use my track counter 
1848       fTempStruct[myTracks].fLabel = mctrackN;
1849       fTempStruct[myTracks].fPhi = atan2(fTempStruct[myTracks].fP[1], fTempStruct[myTracks].fP[0]);
1850       if(fTempStruct[myTracks].fPhi < 0) fTempStruct[myTracks].fPhi += 2*PI;
1851       fTempStruct[myTracks].fPt = sqrt(pow(fTempStruct[myTracks].fP[0],2) + pow(fTempStruct[myTracks].fP[1],2));
1852       fTempStruct[myTracks].fMom = sqrt( pow(fTempStruct[myTracks].fPt,2) + pow(fTempStruct[myTracks].fP[2],2) );
1853       fTempStruct[myTracks].fEta = mcParticle->Eta();
1854       fTempStruct[myTracks].fCharge = int(mcParticle->Charge()/3.);
1855       fTempStruct[myTracks].fDCAXY = 0.;
1856       fTempStruct[myTracks].fDCAZ = 0.;
1857       fTempStruct[myTracks].fDCA = 0.;
1858       fTempStruct[myTracks].fPion = kTRUE;
1859       fTempStruct[myTracks].fEaccepted = sqrt(pow(fTempStruct[myTracks].fMom,2) + pow(fTrueMassPi,2)); 
1860       fTempStruct[myTracks].fKey = 1;
1861       
1862       myTracks++;
1863       pionCount++;
1864     }
1865   }
1866   
1867   if(myTracks >= 1) {
1868     ((TH1F*)fOutputList->FindObject("fMultDist3"))->Fill(myTracks);
1869   }
1870  
1871  
1872   //cout<<"There are "<<myTracks<<"  myTracks"<<endl;
1873   //cout<<"pionCount = "<<pionCount<<"   kaonCount = "<<kaonCount<<"   protonCount = "<<protonCount<<endl;
1874   //return;
1875
1876   /////////////////////////////////////////
1877   // Pion Multiplicity Cut (To ensure all Correlation orders are present in each event)
1878   if(myTracks < 4) {cout<<"Less than 4 tracks. Skipping Event."<<endl; return;}
1879   /////////////////////////////////////////
1880  
1881
1882   ////////////////////////////////
1883   ///////////////////////////////
1884   // Mbin determination
1885   //
1886   // Mbin set to Pion Count Only for pp!!!!!!!
1887   fMbin=-1;
1888   if(!fPbPbcase){
1889     for(Int_t i=0; i<kMultBinspp; i++){
1890       if( ( pionCount > fMultLimits[i]) && ( pionCount <= fMultLimits[i+1]) ) { fMbin=i; break;}
1891       // Mbin 0 has 1 pion
1892     }
1893   }else{
1894     for(Int_t i=0; i<fCentBins; i++){// correlation analysis M bin
1895       if( (centralityPercentile >= cStart+i*cStep) && (centralityPercentile < cStart+(i+1)*cStep) ){
1896         fMbin=i;// 0 = most central
1897         break;
1898       }
1899     }
1900     for(Int_t i=0; i<fCentBinsMixing; i++){// event-mixing M bin
1901       if( (centralityPercentile >= cStart+i*cStepMixing) && (centralityPercentile < cStart+(i+1)*cStepMixing) ){
1902         MbinMixing=i;// 0 = most central
1903         break;
1904       }
1905     }
1906   }
1907
1908   if(fMbin==-1) {cout<<"Bad Mbin+++++++++++++++++++++++++++++++++++++++++++++++++++"<<endl; return;}
1909   
1910   ///////////////////
1911   // can only be called after fMbin has been set
1912   // Radius parameter only matters for Monte-Carlo data
1913   SetFSIindex(fRMax);
1914   ///////////////////  
1915   
1916   Int_t rBinForTPNMomRes = 10;
1917   if(fMbin==0) {rBinForTPNMomRes=10;}// 10 fm with EW (fRMax should be 11 for normal running)
1918   else if(fMbin==1) {rBinForTPNMomRes=9;}
1919   else if(fMbin<=3) {rBinForTPNMomRes=8;}
1920   else if(fMbin<=5) {rBinForTPNMomRes=7;}
1921   else {rBinForTPNMomRes=6;}
1922
1923   //////////////////////////////////////////////////
1924   fEDbin=0;// Extra Dimension bin (Kt, (Kt-Psi),....)
1925   //////////////////////////////////////////////////
1926   
1927
1928   
1929   ((TH1F*)fOutputList->FindObject("fEvents1"))->Fill(fMbin+1);
1930   ((TProfile*)fOutputList->FindObject("fAvgMult"))->Fill(fMbin+1., pionCount);
1931
1932   ////////////////////////////////////
1933   // Add event to buffer if > 0 tracks
1934   if(myTracks > 0){
1935     fEC[zbin][MbinMixing]->FIFOShift();
1936     (fEvt) = fEC[zbin][MbinMixing]->fEvtStr;
1937     (fEvt)->fNtracks = myTracks;
1938     (fEvt)->fFillStatus = 1;
1939     for(Int_t i=0; i<myTracks; i++) (fEvt)->fTracks[i] = fTempStruct[i];
1940     if(fMCcase){
1941       (fEvt)->fMCarraySize = mcArray->GetEntriesFast();
1942       for(Int_t i=0; i<mcArray->GetEntriesFast(); i++) {
1943         AliAODMCParticle *tempMCTrack = (AliAODMCParticle*)mcArray->At(i);
1944         (fEvt)->fMCtracks[i].fPx = tempMCTrack->Px();
1945         (fEvt)->fMCtracks[i].fPy = tempMCTrack->Py();
1946         (fEvt)->fMCtracks[i].fPz = tempMCTrack->Pz();
1947         (fEvt)->fMCtracks[i].fPtot = sqrt(pow(tempMCTrack->Px(),2)+pow(tempMCTrack->Py(),2)+pow(tempMCTrack->Pz(),2));
1948         (fEvt)->fMCtracks[i].fPdgCode = tempMCTrack->GetPdgCode();
1949         (fEvt)->fMCtracks[i].fMotherLabel = tempMCTrack->GetMother();
1950       } 
1951     }
1952   }
1953     
1954   
1955   
1956   Float_t qinv12=0, qinv13=0, qinv14=0, qinv23=0, qinv24=0, qinv34=0;
1957   Float_t qout=0, qside=0, qlong=0;
1958   Float_t kT12=0;
1959   Float_t q3=0, q3MC=0;
1960   Float_t q4=0, q4MC=0;
1961   Int_t ch1=0, ch2=0, ch3=0, ch4=0;
1962   Int_t bin1=0, bin2=0, bin3=0, bin4=0;
1963   Float_t pVect1[4]={0}; 
1964   Float_t pVect2[4]={0};
1965   Float_t pVect3[4]={0};
1966   Float_t pVect4[4]={0};
1967   Float_t pVect1MC[4]={0}; 
1968   Float_t pVect2MC[4]={0};
1969   Float_t pVect3MC[4]={0};
1970   Float_t pVect4MC[4]={0};
1971   Float_t Pparent1[4]={0};
1972   Float_t Pparent2[4]={0};
1973   Float_t Pparent3[4]={0};
1974   Float_t Pparent4[4]={0};
1975   Float_t weight12=0, weight13=0, weight14=0, weight23=0, weight24=0, weight34=0;
1976   Float_t weight12Err=0, weight13Err=0, weight14Err=0, weight23Err=0, weight24Err=0, weight34Err=0;
1977   Float_t weight12CC[3]={0};
1978   Float_t weight13CC[3]={0};
1979   Float_t weight14CC[3]={0};
1980   Float_t weight23CC[3]={0};
1981   Float_t weight24CC[3]={0};
1982   Float_t weight34CC[3]={0};
1983   //Float_t weight12CC_e=0, weight13CC_e=0, weight14CC_e=0, weight23CC_e=0, weight24CC_e=0, weight34CC_e=0;
1984   Float_t weightTotal=0;//, weightTotalErr=0;
1985   Float_t qinv12MC=0, qinv13MC=0, qinv14MC=0, qinv23MC=0, qinv24MC=0, qinv34MC=0; 
1986   Float_t parentQinv12=0, parentQinv13=0, parentQinv14=0, parentQinv23=0, parentQinv24=0, parentQinv34=0;
1987   Float_t parentQ3=0;
1988   Float_t FSICorr12=0, FSICorr13=0, FSICorr14=0, FSICorr23=0, FSICorr24=0, FSICorr34=0;
1989   Bool_t pionParent1=kFALSE, pionParent2=kFALSE, pionParent3=kFALSE, pionParent4=kFALSE;
1990   Bool_t FilledMCpair12=kFALSE, FilledMCtriplet123=kFALSE;
1991   Bool_t Positive1stTripletWeights=kTRUE, Positive2ndTripletWeights=kTRUE;
1992   Float_t T12=0, T13=0, T14=0, T23=0, T24=0, T34=0;
1993   Int_t momBin12=1, momBin13=1, momBin14=1, momBin23=1, momBin24=1, momBin34=1;
1994   Float_t MomResCorr12=1.0, MomResCorr13=1.0, MomResCorr14=1.0, MomResCorr23=1.0, MomResCorr24=1.0, MomResCorr34=1.0;
1995   //
1996   AliAODMCParticle *mcParticle1=0x0;
1997   AliAODMCParticle *mcParticle2=0x0;
1998   
1999
2000   ////////////////////
2001   //Int_t PairCount[7]={0};
2002   //Int_t NormPairCount[7]={0};
2003   Int_t KT3index=0, KT4index=0;
2004
2005   // reset to defaults
2006   for(Int_t i=0; i<kMultLimitPbPb; i++) {
2007     fLowQPairSwitch_E0E0[i]->Set(kMultLimitPbPb,fDefaultsCharSwitch);
2008     fLowQPairSwitch_E0E1[i]->Set(kMultLimitPbPb,fDefaultsCharSwitch);
2009     fLowQPairSwitch_E0E2[i]->Set(kMultLimitPbPb,fDefaultsCharSwitch);
2010     fLowQPairSwitch_E0E3[i]->Set(kMultLimitPbPb,fDefaultsCharSwitch);
2011     fLowQPairSwitch_E1E1[i]->Set(kMultLimitPbPb,fDefaultsCharSwitch);
2012     fLowQPairSwitch_E1E2[i]->Set(kMultLimitPbPb,fDefaultsCharSwitch);
2013     fLowQPairSwitch_E1E3[i]->Set(kMultLimitPbPb,fDefaultsCharSwitch);
2014     fLowQPairSwitch_E2E3[i]->Set(kMultLimitPbPb,fDefaultsCharSwitch);
2015     //
2016     fNormQPairSwitch_E0E0[i]->Set(kMultLimitPbPb,fDefaultsCharSwitch);
2017     fNormQPairSwitch_E0E1[i]->Set(kMultLimitPbPb,fDefaultsCharSwitch);
2018     fNormQPairSwitch_E0E2[i]->Set(kMultLimitPbPb,fDefaultsCharSwitch);
2019     fNormQPairSwitch_E0E3[i]->Set(kMultLimitPbPb,fDefaultsCharSwitch);
2020     fNormQPairSwitch_E1E1[i]->Set(kMultLimitPbPb,fDefaultsCharSwitch);
2021     fNormQPairSwitch_E1E2[i]->Set(kMultLimitPbPb,fDefaultsCharSwitch);
2022     fNormQPairSwitch_E1E3[i]->Set(kMultLimitPbPb,fDefaultsCharSwitch);
2023     fNormQPairSwitch_E2E3[i]->Set(kMultLimitPbPb,fDefaultsCharSwitch);
2024   }
2025  
2026   
2027   //////////////////////////////////////////
2028   // make low-q pair storage and normalization-pair storage
2029   // 
2030   for(Int_t en1=0; en1<=2; en1++){// 1st event number (en1=0 is the same event as current event)
2031     for(Int_t en2=en1; en2<=3; en2++){// 2nd event number (en2=0 is the same event as current event)
2032       if(en1>1 && en1==en2) continue;
2033       
2034       for (Int_t i=0; i<(fEvt+en1)->fNtracks; i++) {// 1st particle
2035         for (Int_t j=i+1; j<(fEvt+en2)->fNtracks; j++) {// 2nd particle
2036           
2037           
2038           pVect1[0]=(fEvt+en1)->fTracks[i].fEaccepted; pVect2[0]=(fEvt+en2)->fTracks[j].fEaccepted;
2039           pVect1[1]=(fEvt+en1)->fTracks[i].fP[0];      pVect2[1]=(fEvt+en2)->fTracks[j].fP[0];
2040           pVect1[2]=(fEvt+en1)->fTracks[i].fP[1];      pVect2[2]=(fEvt+en2)->fTracks[j].fP[1];
2041           pVect1[3]=(fEvt+en1)->fTracks[i].fP[2];      pVect2[3]=(fEvt+en2)->fTracks[j].fP[2];
2042           ch1 = Int_t(((fEvt+en1)->fTracks[i].fCharge + 1)/2.);
2043           ch2 = Int_t(((fEvt+en2)->fTracks[j].fCharge + 1)/2.);
2044           
2045           qinv12 = GetQinv(pVect1, pVect2);
2046           kT12 = sqrt(pow(pVect1[1]+pVect2[1],2) + pow(pVect1[2]+pVect2[2],2))/2.;
2047           SetFillBins2(ch1, ch2, bin1, bin2);
2048           
2049           if(qinv12 < fQLowerCut) continue;// remove unwanted low-q pairs (also a type of track splitting/merging cut)
2050           if(ch1 == ch2 && !fGeneratorOnly){
2051             Int_t tempChGroup[2]={0,0};
2052             if(en1==0 && en2==1) ((TH1F*)fOutputList->FindObject("fTotalPairsWeighting"))->Fill(qinv12, MCWeight(tempChGroup, 10, ffcSqMRC, qinv12, 0.));
2053             if(!AcceptPair((fEvt+en1)->fTracks[i], (fEvt+en2)->fTracks[j])) {
2054               if(en1==0 && en2==0) ((TH1F*)fOutputList->FindObject("fRejectedPairs"))->Fill(qinv12);
2055               continue;
2056             }
2057             if(en1==0 && en2==1) ((TH1F*)fOutputList->FindObject("fAcceptedPairsWeighting"))->Fill(qinv12, MCWeight(tempChGroup, 10, ffcSqMRC, qinv12, 0.));
2058           }
2059           if(fMixedChargeCut && ch1 != ch2 && !fGeneratorOnly && !fMCcase){// remove +- low-q pairs to keep balance between ++ and +- contributions to multi-particle Q3,Q4 projections
2060             Int_t tempChGroup[2]={0,1};
2061             if(en1==0 && en2==1) ((TH1F*)fOutputList->FindObject("fTotalPairsWeightingMC"))->Fill(qinv12, MCWeight(tempChGroup, 10, ffcSqMRC, qinv12, 0.));
2062             if(!AcceptPairPM((fEvt+en1)->fTracks[i], (fEvt+en2)->fTracks[j])) {
2063               if(en1==0 && en2==0) ((TH1F*)fOutputList->FindObject("fRejectedPairsMC"))->Fill(qinv12);
2064               continue;
2065             }
2066             if(en1==0 && en2==1) ((TH1F*)fOutputList->FindObject("fAcceptedPairsWeightingMC"))->Fill(qinv12, MCWeight(tempChGroup, 10, ffcSqMRC, qinv12, 0.));
2067           }
2068           
2069           GetQosl(pVect1, pVect2, qout, qside, qlong);
2070           if( (en1+en2==0)) {
2071             if(!fGenerateSignal) Charge1[bin1].Charge2[bin2].MB[fMbin].EDB[0].TwoPT[0].fTerms2->Fill(kT12, qinv12);
2072             Charge1[bin1].Charge2[bin2].MB[fMbin].EDB[0].TwoPT[0].fTerms2QW->Fill(kT12, qinv12, qinv12);
2073             // osl frame
2074             if((kT12 > 0.2) && (kT12 < 0.3)){
2075               Charge1[bin1].Charge2[bin2].MB[fMbin].EDB[0].TwoPT[en2].OSL_ktbin[0].fTerms2OSL->Fill(fabs(qout), fabs(qside), fabs(qlong));
2076               Charge1[bin1].Charge2[bin2].MB[fMbin].EDB[0].TwoPT[en2].OSL_ktbin[0].fTerms2OSLQW->Fill(fabs(qout), fabs(qside), fabs(qlong), qinv12);
2077             }
2078             if((kT12 > 0.6) && (kT12 < 0.7)){  
2079               Charge1[bin1].Charge2[bin2].MB[fMbin].EDB[0].TwoPT[en2].OSL_ktbin[1].fTerms2OSL->Fill(fabs(qout), fabs(qside), fabs(qlong));
2080               Charge1[bin1].Charge2[bin2].MB[fMbin].EDB[0].TwoPT[en2].OSL_ktbin[1].fTerms2OSLQW->Fill(fabs(qout), fabs(qside), fabs(qlong), qinv12);
2081             }
2082             // unit mult bins
2083             if( (fEvt+en1)->fNtracks%100==0){
2084               Int_t kTindex=0;
2085               if(kT12>0.3) kTindex=1;
2086               Int_t UnitMultBin = int((fEvt+en1)->fNtracks / 100.) + 1;
2087               Charge1[bin1].Charge2[bin2].MB[fMbin].EDB[kTindex].TwoPT[0].fUnitMultBin->Fill(UnitMultBin, qinv12);
2088             }
2089             
2090           }
2091           if( (en1+en2==1)) {
2092             if(!fGenerateSignal) Charge1[bin1].Charge2[bin2].MB[fMbin].EDB[0].TwoPT[1].fTerms2->Fill(kT12, qinv12);
2093             Charge1[bin1].Charge2[bin2].MB[fMbin].EDB[0].TwoPT[1].fTerms2QW->Fill(kT12, qinv12, qinv12);
2094             // osl frame
2095             if((kT12 > 0.2) && (kT12 < 0.3)){  
2096               Charge1[bin1].Charge2[bin2].MB[fMbin].EDB[fEDbin].TwoPT[en2].OSL_ktbin[0].fTerms2OSL->Fill(fabs(qout), fabs(qside), fabs(qlong));
2097               Charge1[bin1].Charge2[bin2].MB[fMbin].EDB[fEDbin].TwoPT[en2].OSL_ktbin[0].fTerms2OSLQW->Fill(fabs(qout), fabs(qside), fabs(qlong), qinv12);
2098             }
2099             if((kT12 > 0.6) && (kT12 < 0.7)){  
2100               Charge1[bin1].Charge2[bin2].MB[fMbin].EDB[fEDbin].TwoPT[en2].OSL_ktbin[1].fTerms2OSL->Fill(fabs(qout), fabs(qside), fabs(qlong));
2101               Charge1[bin1].Charge2[bin2].MB[fMbin].EDB[fEDbin].TwoPT[en2].OSL_ktbin[1].fTerms2OSLQW->Fill(fabs(qout), fabs(qside), fabs(qlong), qinv12);
2102             }
2103             // unit mult bins
2104             if( (fEvt+en1)->fNtracks%100==0){
2105               Int_t kTindex=0;
2106               if(kT12>0.3) kTindex=1;
2107               Int_t UnitMultBin = int((fEvt+en1)->fNtracks / 100.) + 1;
2108               Charge1[bin1].Charge2[bin2].MB[fMbin].EDB[kTindex].TwoPT[1].fUnitMultBin->Fill(UnitMultBin, qinv12);
2109             }
2110           }
2111           //////////////////////////////////////////
2112           if(fTabulatePairs && en1==0 && en2<=1 && bin1==bin2){
2113             Float_t kY = 0;
2114             Int_t kTbin=-1, kYbin=-1;
2115             Bool_t PairToReject=kFALSE;
2116             if((fEvt+en1)->fTracks[i].fPt < fMinPt || (fEvt+en1)->fTracks[i].fPt > fMaxPt) PairToReject=kTRUE;
2117             if((fEvt+en2)->fTracks[j].fPt < fMinPt || (fEvt+en2)->fTracks[j].fPt > fMaxPt) PairToReject=kTRUE;
2118             if(!PairToReject){
2119               for(Int_t kIt=0; kIt<fKbinsT; kIt++) {if(kT12 < (fKmiddleT[kIt] + fKstepT[kIt]/2.)) {kTbin = kIt; break;}} 
2120               for(Int_t kIt=0; kIt<fKbinsY; kIt++) {if(kY < (fKmiddleY[kIt] + fKstepY[kIt]/2.)) {kYbin = kIt; break;}}
2121               if((kTbin<0) || (kYbin<0)) {cout<<"problem!!!!!!!!!!!!!!!!!!!!!!!!!!!!"<<endl; continue;}
2122               if((kTbin>=fKbinsT) || (kYbin>=fKbinsY)) {cout<<"problem!!!!!!!!!!!!!!!!!!!!!!!!!!!!"<<endl; continue;}
2123               if(fGenerateSignal && en2==0) {
2124                 Int_t chGroup2[2]={ch1,ch2};
2125                 Float_t WInput = MCWeight(chGroup2, fRMax, ffcSqMRC, qinv12, kT12);
2126               KT[kTbin].KY[kYbin].MB[fMbin].EDB[0].TwoPT[en2].fTerms2ThreeD->Fill(fabs(qout), fabs(qside), fabs(qlong), WInput);
2127               }else KT[kTbin].KY[kYbin].MB[fMbin].EDB[0].TwoPT[en2].fTerms2ThreeD->Fill(fabs(qout), fabs(qside), fabs(qlong));
2128             }
2129           }
2130           
2131           //////////////////////////////////////////////////////////////////////////////
2132          
2133           if(qinv12 <= fQcut) {
2134             if(en1==0 && en2==0) {fLowQPairSwitch_E0E0[i]->AddAt('1',j);}
2135             if(en1==0 && en2==1) {fLowQPairSwitch_E0E1[i]->AddAt('1',j);}
2136             if(en1==0 && en2==2) {fLowQPairSwitch_E0E2[i]->AddAt('1',j);}
2137             if(en1==0 && en2==3) {fLowQPairSwitch_E0E3[i]->AddAt('1',j);}
2138             if(en1==1 && en2==1) {fLowQPairSwitch_E1E1[i]->AddAt('1',j);}
2139             if(en1==1 && en2==2) {fLowQPairSwitch_E1E2[i]->AddAt('1',j);}
2140             if(en1==1 && en2==3) {fLowQPairSwitch_E1E3[i]->AddAt('1',j);}
2141             if(en1==2 && en2==3) {fLowQPairSwitch_E2E3[i]->AddAt('1',j);}
2142           }
2143           if((qinv12 >= fNormQcutLow) && (qinv12 < fNormQcutHigh)) {
2144             if(en1==0 && en2==0) {fNormQPairSwitch_E0E0[i]->AddAt('1',j);}
2145             if(en1==0 && en2==1) {fNormQPairSwitch_E0E1[i]->AddAt('1',j);}
2146             if(en1==0 && en2==2) {fNormQPairSwitch_E0E2[i]->AddAt('1',j);}
2147             if(en1==0 && en2==3) {fNormQPairSwitch_E0E3[i]->AddAt('1',j);}
2148             if(en1==1 && en2==1) {fNormQPairSwitch_E1E1[i]->AddAt('1',j);}
2149             if(en1==1 && en2==2) {fNormQPairSwitch_E1E2[i]->AddAt('1',j);}
2150             if(en1==1 && en2==3) {fNormQPairSwitch_E1E3[i]->AddAt('1',j);}
2151             if(en1==2 && en2==3) {fNormQPairSwitch_E2E3[i]->AddAt('1',j);}
2152           }
2153           
2154         }
2155       }
2156     }
2157   }
2158     
2159   //cout<<PairCount[0]<<"  "<<PairCount[1]<<"  "<<PairCount[2]<<"  "<<PairCount[3]<<"  "<<PairCount[4]<<"  "<<PairCount[5]<<"  "<<PairCount[6]<<endl;
2160   //cout<<NormPairCount[0]<<"  "<<NormPairCount[1]<<"  "<<NormPairCount[2]<<"  "<<NormPairCount[3]<<"  "<<NormPairCount[4]<<"  "<<NormPairCount[5]<<"  "<<NormPairCount[6]<<endl;
2161   ///////////////////////////////////////////////////  
2162   // Do not use pairs from events with too many pairs
2163   
2164   ((TH1F*)fOutputList->FindObject("fEvents2"))->Fill(fMbin+1);
2165   
2166   ///////////////////////////////////////////////////
2167   
2168   
2169   if(fTabulatePairs) return;
2170
2171   /*TF1 *SCpairWeight = new TF1("SCpairWeight","[0] + [1]*x + [2]*exp(-[3]*x)",0,0.2);// same-charge pair weight for monte-carlo data without two-track cuts.
2172   SCpairWeight->FixParameter(0, 0.959);
2173   SCpairWeight->FixParameter(1, 0.278);
2174   SCpairWeight->FixParameter(2, -1.759);
2175   SCpairWeight->FixParameter(3, 115.107);*/
2176
2177   ////////////////////////////////////////////////////
2178   ////////////////////////////////////////////////////
2179   // Normalization counting of 3- and 4-particle terms
2180   for(Int_t en2=0; en2<=1; en2++){// 2nd event number (en2=0 is the same event as current event)
2181     for(Int_t en3=en2; en3<=2; en3++){// 3rd event number
2182       if(en2==0 && en3>2) continue;// not needed config
2183       if(en2==1 && en3==en2) continue;// not needed config
2184       for(Int_t en4=en3; en4<=3; en4++){// 4th event number
2185         if(en3==0 && en4>1) continue;// not needed config
2186         if(en3==1 && en4==3) continue;// not needed configs
2187         if(en3==2 && (en2+en3+en4)!=6) continue;// not needed configs
2188         
2189         for (Int_t i=0; i<myTracks; i++) {// 1st particle
2190           pVect1[1]=(fEvt)->fTracks[i].fP[0];
2191           pVect1[2]=(fEvt)->fTracks[i].fP[1];
2192           pVect1[3]=(fEvt)->fTracks[i].fP[2];
2193           ch1 = Int_t(((fEvt)->fTracks[i].fCharge + 1)/2.);
2194           
2195           for (Int_t j=i+1; j<(fEvt+en2)->fNtracks; j++) {// 2nd particle
2196             if(en2==0) {if(fNormQPairSwitch_E0E0[i]->At(j)=='0') continue;}
2197             else {if(fNormQPairSwitch_E0E1[i]->At(j)=='0') continue;}
2198             
2199             pVect2[1]=(fEvt+en2)->fTracks[j].fP[0];
2200             pVect2[2]=(fEvt+en2)->fTracks[j].fP[1];
2201             pVect2[3]=(fEvt+en2)->fTracks[j].fP[2];
2202             ch2 = Int_t(((fEvt+en2)->fTracks[j].fCharge + 1)/2.);
2203             
2204             for (Int_t k=j+1; k<(fEvt+en3)->fNtracks; k++) {// 3rd particle
2205               if(en3==0) {
2206                 if(fNormQPairSwitch_E0E0[i]->At(k)=='0') continue;
2207                 if(fNormQPairSwitch_E0E0[j]->At(k)=='0') continue;
2208               }else if(en3==1){
2209                 if(fNormQPairSwitch_E0E1[i]->At(k)=='0') continue;
2210                 if(fNormQPairSwitch_E0E1[j]->At(k)=='0') continue;
2211               }else{
2212                 if(fNormQPairSwitch_E0E2[i]->At(k)=='0') continue;
2213                 if(fNormQPairSwitch_E1E2[j]->At(k)=='0') continue;
2214               }
2215               
2216               pVect3[1]=(fEvt+en3)->fTracks[k].fP[0];
2217               pVect3[2]=(fEvt+en3)->fTracks[k].fP[1];
2218               pVect3[3]=(fEvt+en3)->fTracks[k].fP[2];
2219               ch3 = Int_t(((fEvt+en3)->fTracks[k].fCharge + 1)/2.);
2220               Bool_t fill2=kFALSE, fill3=kFALSE, fill4=kFALSE;
2221               SetFillBins3(ch1, ch2, ch3, 1, bin1, bin2, bin3, fill2, fill3, fill4);
2222               
2223               Float_t KT3 = sqrt(pow(pVect1[1]+pVect2[1]+pVect3[1],2) + pow(pVect1[2]+pVect2[2]+pVect3[2],2))/3.;
2224               if(KT3<=fKT3transition) KT3index=0;
2225               else KT3index=1;
2226               
2227               if(en2==0 && en3==0 && en4==0) Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[fMbin].EDB[KT3index].ThreePT[0].fNorm3->Fill(0);
2228               if(en2==1 && en3==2 && en4==3) Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[fMbin].EDB[KT3index].ThreePT[4].fNorm3->Fill(0);
2229               if(en2==0 && en3==1 && en4==2) {
2230                 if(fill2) Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[fMbin].EDB[KT3index].ThreePT[1].fNorm3->Fill(0);
2231                 if(fill3) Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[fMbin].EDB[KT3index].ThreePT[2].fNorm3->Fill(0);
2232                 if(fill4) Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[fMbin].EDB[KT3index].ThreePT[3].fNorm3->Fill(0);
2233               }
2234               
2235               
2236               for (Int_t l=k+1; l<(fEvt+en4)->fNtracks; l++) {// 4th particle
2237                 if(en4==0){
2238                   if(fNormQPairSwitch_E0E0[i]->At(l)=='0') continue;
2239                   if(fNormQPairSwitch_E0E0[j]->At(l)=='0') continue;
2240                   if(fNormQPairSwitch_E0E0[k]->At(l)=='0') continue;
2241                 }else if(en4==1){
2242                   if(en3==0){
2243                     if(fNormQPairSwitch_E0E1[i]->At(l)=='0') continue;
2244                     if(fNormQPairSwitch_E0E1[j]->At(l)=='0') continue;
2245                     if(fNormQPairSwitch_E0E1[k]->At(l)=='0') continue;
2246                   }else{
2247                     if(fNormQPairSwitch_E0E1[i]->At(l)=='0') continue;
2248                     if(fNormQPairSwitch_E0E1[j]->At(l)=='0') continue;
2249                     if(fNormQPairSwitch_E1E1[k]->At(l)=='0') continue;
2250                   }
2251                 }else if(en4==2){
2252                   if(fNormQPairSwitch_E0E2[i]->At(l)=='0') continue;
2253                   if(fNormQPairSwitch_E0E2[j]->At(l)=='0') continue;
2254                   if(fNormQPairSwitch_E1E2[k]->At(l)=='0') continue;
2255                 }else{
2256                   if(fNormQPairSwitch_E0E3[i]->At(l)=='0') continue;
2257                   if(fNormQPairSwitch_E1E3[j]->At(l)=='0') continue;
2258                   if(fNormQPairSwitch_E2E3[k]->At(l)=='0') continue;
2259                 }
2260                 
2261                 pVect4[1]=(fEvt+en4)->fTracks[l].fP[0];
2262                 pVect4[2]=(fEvt+en4)->fTracks[l].fP[1];
2263                 pVect4[3]=(fEvt+en4)->fTracks[l].fP[2];
2264                 ch4 = Int_t(((fEvt+en4)->fTracks[l].fCharge + 1)/2.);
2265                 Float_t KT4 = sqrt(pow(pVect1[1]+pVect2[1]+pVect3[1]+pVect4[1],2) + pow(pVect1[2]+pVect2[2]+pVect3[2]+pVect4[2],2))/4.;
2266                 if(KT4<=fKT4transition) KT4index=0;
2267                 else KT4index=1;
2268                 
2269                 Bool_t FillTerms[13]={kFALSE};
2270                 SetFillBins4(ch1, ch2, ch3, ch4, bin1, bin2, bin3, bin4, en2+en3+en4, FillTerms);
2271                 //
2272                 for(int ft=0; ft<13; ft++) {
2273                   if(FillTerms[ft]) Charge1[bin1].Charge2[bin2].Charge3[bin3].Charge4[bin4].MB[fMbin].EDB[KT4index].FourPT[ft].fNorm4->Fill(0.); 
2274                 }
2275                 
2276                 
2277               }
2278             }
2279           }
2280         }  
2281         
2282       }
2283     }
2284   }
2285     
2286
2287    
2288
2289     ///////////////////////////////////////////////////////////////////////
2290     ///////////////////////////////////////////////////////////////////////
2291     ///////////////////////////////////////////////////////////////////////
2292     //
2293     //
2294     // Start the Main Correlation Analysis
2295     //
2296     //
2297     ///////////////////////////////////////////////////////////////////////
2298   
2299
2300
2301     ////////////////////////////////////////////////////
2302     ////////////////////////////////////////////////////
2303     for(Int_t en2=0; en2<=1; en2++){// 2nd event number (en2=0 is the same event as current event)
2304       for(Int_t en3=en2; en3<=2; en3++){// 3rd event number
2305         if(en2==0 && en3>2) continue;// not needed config
2306         if(en2==1 && en3==en2) continue;// not needed config
2307         for(Int_t en4=en3; en4<=3; en4++){// 4th event number
2308           if(en3==0 && en4>1) continue;// not needed config
2309           if(en3==1 && en4==3) continue;// not needed configs
2310           if(en3==2 && (en2+en3+en4)!=6) continue;// not needed configs
2311           
2312           Int_t ENsum=en2+en3+en4;// 0 or 1 or 3 or 6
2313           
2314           /////////////////////////////////////////////////////////////
2315           for (Int_t i=0; i<myTracks; i++) {// 1st particle
2316             pVect1[0]=(fEvt)->fTracks[i].fEaccepted;
2317             pVect1[1]=(fEvt)->fTracks[i].fP[0];
2318             pVect1[2]=(fEvt)->fTracks[i].fP[1];
2319             pVect1[3]=(fEvt)->fTracks[i].fP[2];
2320             ch1 = Int_t(((fEvt)->fTracks[i].fCharge + 1)/2.);
2321             if((fEvt)->fTracks[i].fPt < fMinPt) continue; 
2322             if((fEvt)->fTracks[i].fPt > fMaxPt) continue;
2323
2324             /////////////////////////////////////////////////////////////
2325             for (Int_t j=i+1; j<(fEvt+en2)->fNtracks; j++) {// 2nd particle
2326               if(en2==0) {if(fLowQPairSwitch_E0E0[i]->At(j)=='0') continue;}
2327               else {if(fLowQPairSwitch_E0E1[i]->At(j)=='0') continue;}
2328               if((fEvt+en2)->fTracks[j].fPt < fMinPt) continue; 
2329               if((fEvt+en2)->fTracks[j].fPt > fMaxPt) continue;
2330               
2331               pVect2[0]=(fEvt+en2)->fTracks[j].fEaccepted;
2332               pVect2[1]=(fEvt+en2)->fTracks[j].fP[0];
2333               pVect2[2]=(fEvt+en2)->fTracks[j].fP[1];
2334               pVect2[3]=(fEvt+en2)->fTracks[j].fP[2];
2335               ch2 = Int_t(((fEvt+en2)->fTracks[j].fCharge + 1)/2.);
2336               qinv12 = GetQinv(pVect1, pVect2);
2337               kT12 = sqrt(pow(pVect1[1]+pVect2[1],2) + pow(pVect1[2]+pVect2[2],2))/2.;
2338               SetFillBins2(ch1, ch2, bin1, bin2);
2339               Int_t kTindex=0;
2340               if(kT12<=0.3) kTindex=0;
2341               else kTindex=1;
2342               
2343               FSICorr12 = FSICorrelation(ch1,ch2, qinv12);
2344               
2345               // two particle terms filled during tabulation of low-q pairs
2346               
2347               
2348               if(fMCcase){
2349                 FilledMCpair12=kFALSE;
2350
2351                 if(ch1==ch2 && fMbin==0 && qinv12<0.2 && ENsum!=2 && ENsum!=3 && ENsum!=6){
2352                   for(Int_t rstep=0; rstep<10; rstep++){
2353                     Float_t coeff = (rstep)*0.2*(0.18/1.2);
2354                     Float_t phi1 = (fEvt)->fTracks[i].fPhi - asin((fEvt)->fTracks[i].fCharge*(0.1*fBfield)*coeff/(fEvt)->fTracks[i].fPt);
2355                     if(phi1 > 2*PI) phi1 -= 2*PI;
2356                     if(phi1 < 0) phi1 += 2*PI;
2357                     Float_t phi2 = (fEvt+en2)->fTracks[j].fPhi - asin((fEvt+en2)->fTracks[j].fCharge*(0.1*fBfield)*coeff/(fEvt+en2)->fTracks[j].fPt);
2358                     if(phi2 > 2*PI) phi2 -= 2*PI;
2359                     if(phi2 < 0) phi2 += 2*PI;
2360                     Float_t deltaphi = phi1 - phi2;
2361                     if(deltaphi > PI) deltaphi -= PI;
2362                     if(deltaphi < -PI) deltaphi += PI;
2363                     
2364                     if(ENsum==0) ((TH3F*)fOutputList->FindObject("fPairsDetaDPhiNum"))->Fill(rstep, (fEvt)->fTracks[i].fEta-(fEvt+en2)->fTracks[j].fEta, deltaphi);
2365                     else ((TH3F*)fOutputList->FindObject("fPairsDetaDPhiDen"))->Fill(rstep, (fEvt)->fTracks[i].fEta-(fEvt+en2)->fTracks[j].fEta, deltaphi);
2366                   }
2367                   
2368                 }// pair selection
2369
2370                 // Check that label does not exceed stack size
2371                 if((fEvt)->fTracks[i].fLabel < (fEvt)->fMCarraySize && (fEvt+en2)->fTracks[j].fLabel < (fEvt+en2)->fMCarraySize){
2372                   if(ENsum==0 && abs((fEvt+en2)->fTracks[j].fLabel) == abs((fEvt)->fTracks[i].fLabel)) continue;
2373                   pVect1MC[0]=sqrt(pow((fEvt)->fMCtracks[abs((fEvt)->fTracks[i].fLabel)].fPtot,2)+pow(fTrueMassPi,2)); 
2374                   pVect2MC[0]=sqrt(pow((fEvt+en2)->fMCtracks[abs((fEvt+en2)->fTracks[j].fLabel)].fPtot,2)+pow(fTrueMassPi,2));
2375                   pVect1MC[1]=(fEvt)->fMCtracks[abs((fEvt)->fTracks[i].fLabel)].fPx; pVect2MC[1]=(fEvt+en2)->fMCtracks[abs((fEvt+en2)->fTracks[j].fLabel)].fPx;
2376                   pVect1MC[2]=(fEvt)->fMCtracks[abs((fEvt)->fTracks[i].fLabel)].fPy; pVect2MC[2]=(fEvt+en2)->fMCtracks[abs((fEvt+en2)->fTracks[j].fLabel)].fPy;
2377                   pVect1MC[3]=(fEvt)->fMCtracks[abs((fEvt)->fTracks[i].fLabel)].fPz; pVect2MC[3]=(fEvt+en2)->fMCtracks[abs((fEvt+en2)->fTracks[j].fLabel)].fPz;
2378                   qinv12MC = GetQinv(pVect1MC, pVect2MC);
2379                   Int_t chGroup2[2]={ch1,ch2};
2380
2381                   if(fGenerateSignal && (ENsum==0 || ENsum==6)){
2382                     if(ENsum==0) {
2383                       Float_t WInput = MCWeight(chGroup2, fRMax, ffcSqMRC, qinv12MC, 0.);
2384                       Charge1[bin1].Charge2[bin2].MB[fMbin].EDB[0].TwoPT[0].fTerms2->Fill(kT12, qinv12, WInput);
2385                     }else{
2386                       Charge1[bin1].Charge2[bin2].MB[fMbin].EDB[0].TwoPT[1].fTerms2->Fill(kT12, qinv12);
2387                     }             
2388                   }
2389                   
2390                   if(qinv12<0.1 && ch1==ch2 && ENsum==0) {
2391                     ((TProfile*)fOutputList->FindObject("fQsmearMean"))->Fill(1.,qinv12-qinv12MC); 
2392                     ((TProfile*)fOutputList->FindObject("fQsmearSq"))->Fill(1.,1000.*pow(qinv12-qinv12MC,2));
2393                     ((TH2D*)fOutputList->FindObject("fQ2Res"))->Fill(kT12, qinv12-qinv12MC);
2394                   }
2395                                   
2396                   // secondary contamination
2397                   if(ENsum==0){
2398                     mcParticle1 = (AliAODMCParticle*)mcArray->At(abs((fEvt)->fTracks[i].fLabel));
2399                     mcParticle2 = (AliAODMCParticle*)mcArray->At(abs((fEvt+en2)->fTracks[j].fLabel));
2400                     if(!mcParticle1 || !mcParticle2) continue;
2401                     if(abs(mcParticle1->GetPdgCode())==211 && abs(mcParticle2->GetPdgCode())==211){
2402                       if(ch1==ch2) {
2403                         ((TH3D*)fOutputList->FindObject("fAllSCPionPairs"))->Fill(fMbin+1, kT12, qinv12);
2404                         if(!mcParticle1->IsSecondaryFromWeakDecay() && !mcParticle2->IsSecondaryFromWeakDecay()) {
2405                           ((TH3D*)fOutputList->FindObject("fPrimarySCPionPairs"))->Fill(fMbin+1, kT12, qinv12);
2406                         }             
2407                       }else{
2408                         ((TH3D*)fOutputList->FindObject("fAllMCPionPairs"))->Fill(fMbin+1, kT12, qinv12);
2409                         if(!mcParticle1->IsSecondaryFromWeakDecay() && !mcParticle2->IsSecondaryFromWeakDecay()) {
2410                           ((TH3D*)fOutputList->FindObject("fPrimaryMCPionPairs"))->Fill(fMbin+1, kT12, qinv12);
2411                         }
2412                       }
2413                     }
2414                   }
2415                   
2416                   if(ENsum==6){// all mixed events
2417                   
2418                     Float_t rForQW=5.0;
2419                     if(fFSIindex<=1) rForQW=10;
2420                     else if(fFSIindex==2) rForQW=9;
2421                     else if(fFSIindex==3) rForQW=8;
2422                     else if(fFSIindex==4) rForQW=7;
2423                     else if(fFSIindex==5) rForQW=6;
2424                     else if(fFSIindex==6) rForQW=5;
2425                     else if(fFSIindex==7) rForQW=4;
2426                     else if(fFSIindex==8) rForQW=3;
2427                     else rForQW=2;
2428                     
2429                     
2430                     Charge1[bin1].Charge2[bin2].MB[fMbin].EDB[0].TwoPT[1].fMCqinv->Fill(qinv12MC, MCWeight(chGroup2, rForQW, ffcSqMRC, qinv12MC, 0.));// was 4,5
2431                     Charge1[bin1].Charge2[bin2].MB[fMbin].EDB[0].TwoPT[1].fMCqinvQW->Fill(qinv12MC, qinv12MC*MCWeight(chGroup2, rForQW, ffcSqMRC, qinv12MC, 0.));// was 4,5
2432                     // pion purity
2433                     Charge1[bin1].Charge2[bin2].MB[fMbin].EDB[0].TwoPT[1].fPIDpurityDen->Fill(kT12, qinv12);
2434                     Int_t SCNumber = 1;
2435                     Int_t PdgCodeSum = abs((fEvt)->fMCtracks[abs((fEvt)->fTracks[i].fLabel)].fPdgCode) + abs((fEvt+en2)->fMCtracks[abs((fEvt+en2)->fTracks[j].fLabel)].fPdgCode);
2436                     if(PdgCodeSum==22) SCNumber=1;// e-e
2437                     else if(PdgCodeSum==24) SCNumber=2;// e-mu
2438                     else if(PdgCodeSum==222) SCNumber=3;// e-pi
2439                     else if(PdgCodeSum==332) SCNumber=4;// e-k
2440                     else if(PdgCodeSum==2223) SCNumber=5;// e-p
2441                     else if(PdgCodeSum==26) SCNumber=6;// mu-mu
2442                     else if(PdgCodeSum==224) SCNumber=7;// mu-pi
2443                     else if(PdgCodeSum==334) SCNumber=8;// mu-k
2444                     else if(PdgCodeSum==2225) SCNumber=9;// mu-p
2445                     else if(PdgCodeSum==422) SCNumber=10;// pi-pi
2446                     else if(PdgCodeSum==532) SCNumber=11;// pi-k
2447                     else if(PdgCodeSum==2423) SCNumber=12;// pi-p
2448                     else if(PdgCodeSum==642) SCNumber=13;// k-k
2449                     else if(PdgCodeSum==2533) SCNumber=14;// k-p
2450                     else if(PdgCodeSum==4424) SCNumber=15;// p-p
2451                     else {SCNumber=16;}
2452                     
2453                     Charge1[bin1].Charge2[bin2].MB[fMbin].EDB[0].TwoPT[1].fPIDpurityNum->Fill(SCNumber, kT12, qinv12);
2454                     
2455                     ///////////////////////
2456                     // muon contamination
2457                     Pparent1[0]=pVect1MC[0]; Pparent1[1]=pVect1MC[1]; Pparent1[2]=pVect1MC[2]; Pparent1[3]=pVect1MC[3];
2458                     Pparent2[0]=pVect2MC[0]; Pparent2[1]=pVect2MC[1]; Pparent2[2]=pVect2MC[2]; Pparent2[3]=pVect2MC[3];
2459                     pionParent1=kFALSE; pionParent2=kFALSE;
2460                     FilledMCpair12=kTRUE;
2461                     //
2462                     if(abs((fEvt)->fMCtracks[abs((fEvt)->fTracks[i].fLabel)].fPdgCode)==13 || abs((fEvt+en2)->fMCtracks[abs((fEvt+en2)->fTracks[j].fLabel)].fPdgCode)==13){// muon check
2463                       Int_t MotherLabel1 = (fEvt)->fMCtracks[abs((fEvt)->fTracks[i].fLabel)].fMotherLabel;
2464                       if(abs((fEvt)->fMCtracks[MotherLabel1].fPdgCode)==211) {
2465                         pionParent1=kTRUE;
2466                         Pparent1[1] = (fEvt)->fMCtracks[MotherLabel1].fPx; Pparent1[2] = (fEvt)->fMCtracks[MotherLabel1].fPy; Pparent1[3] = (fEvt)->fMCtracks[MotherLabel1].fPz;
2467                         Pparent1[0] = sqrt(pow(Pparent1[1],2)+pow(Pparent1[2],2)+pow(Pparent1[3],2)+pow(fTrueMassPi,2));
2468                       }
2469                       // 
2470                       if(abs((fEvt+en2)->fMCtracks[abs((fEvt+en2)->fTracks[j].fLabel)].fPdgCode)==13) {
2471                         Int_t MotherLabel2 = (fEvt+en2)->fMCtracks[abs((fEvt+en2)->fTracks[j].fLabel)].fMotherLabel;
2472                         if(abs((fEvt+en2)->fMCtracks[MotherLabel2].fPdgCode)==211) {
2473                           pionParent2=kTRUE;
2474                           Pparent2[1] = (fEvt+en2)->fMCtracks[MotherLabel2].fPx; Pparent2[2] = (fEvt+en2)->fMCtracks[MotherLabel2].fPy; Pparent2[3] = (fEvt+en2)->fMCtracks[MotherLabel2].fPz;
2475                           Pparent2[0] = sqrt(pow(Pparent2[1],2)+pow(Pparent2[2],2)+pow(Pparent2[3],2)+pow(fTrueMassPi,2));
2476                         }
2477                       }
2478                       
2479                       parentQinv12 = GetQinv(Pparent1, Pparent2);
2480                       
2481                       if(pionParent1 || pionParent2){
2482                         if(parentQinv12 > 0.001 && parentQinv12 < 0.3){
2483                           Float_t muonPionK12 = FSICorrelation(ch1, ch2, qinv12MC);
2484                           Float_t pionPionK12 = FSICorrelation(ch1, ch2, parentQinv12);
2485                           for(Int_t term=1; term<=2; term++){
2486                             for(Int_t Riter=0; Riter<fRVALUES; Riter++){
2487                               Float_t Rvalue = 5+Riter;
2488                               Float_t WInput = 1.0;
2489                               if(term==1) {
2490                                 WInput = MCWeight(chGroup2, Rvalue, 1.0, parentQinv12, 0.);
2491                               }else{
2492                                 muonPionK12 = 1.0; pionPionK12=1.0;
2493                               }
2494                               
2495                               Charge1[bin1].Charge2[bin2].MB[0].EDB[0].TwoPT[term-1].fMuonSmeared->Fill(Rvalue, qinv12MC, WInput);
2496                               Charge1[bin1].Charge2[bin2].MB[0].EDB[0].TwoPT[term-1].fMuonIdeal->Fill(Rvalue, parentQinv12, WInput);
2497                               Charge1[bin1].Charge2[bin2].MB[0].EDB[0].TwoPT[term-1].fMuonPionK2->Fill(Rvalue, qinv12MC, muonPionK12);
2498                               Charge1[bin1].Charge2[bin2].MB[0].EDB[0].TwoPT[term-1].fPionPionK2->Fill(Rvalue, parentQinv12, pionPionK12);
2499                             }// Riter
2500                           }// term loop
2501                           
2502                           if(ch1==ch2) ((TH3D*)fOutputList->FindObject("fMuonPionDeltaQinv"))->Fill(0., kT12, qinv12MC-parentQinv12);
2503                           else ((TH3D*)fOutputList->FindObject("fMuonPionDeltaQinv"))->Fill(1., kT12, qinv12MC-parentQinv12);
2504                         }// parentQ check
2505                       }// pion parent check
2506                     }// muon check
2507                   
2508                     
2509                     // momentum resolution
2510                     for(Int_t Riter=0; Riter<fRVALUES; Riter++){
2511                       Float_t Rvalue = 5+Riter;
2512                       Float_t WInput = MCWeight(chGroup2, Rvalue, ffcSqMRC, qinv12MC, 0.);
2513                       Charge1[bin1].Charge2[bin2].MB[0].EDB[kTindex].TwoPT[0].fIdeal->Fill(Rvalue, qinv12MC, WInput);
2514                       Charge1[bin1].Charge2[bin2].MB[0].EDB[kTindex].TwoPT[1].fIdeal->Fill(Rvalue, qinv12MC);
2515                       Charge1[bin1].Charge2[bin2].MB[0].EDB[kTindex].TwoPT[0].fSmeared->Fill(Rvalue, qinv12, WInput);
2516                       Charge1[bin1].Charge2[bin2].MB[0].EDB[kTindex].TwoPT[1].fSmeared->Fill(Rvalue, qinv12);
2517                     }
2518                     
2519                   }// ENsum check
2520                 }// MC array check
2521               }// MC case
2522               
2523                 
2524
2525               /////////////////////////////////////////////////////////////
2526               for (Int_t k=j+1; k<(fEvt+en3)->fNtracks; k++) {// 3rd particle
2527                 if(en3==0) {
2528                   if(fLowQPairSwitch_E0E0[i]->At(k)=='0') continue;
2529                   if(fLowQPairSwitch_E0E0[j]->At(k)=='0') continue;
2530                 }else if(en3==1){
2531                   if(fLowQPairSwitch_E0E1[i]->At(k)=='0') continue;
2532                   if(fLowQPairSwitch_E0E1[j]->At(k)=='0') continue;
2533                 }else{
2534                   if(fLowQPairSwitch_E0E2[i]->At(k)=='0') continue;
2535                   if(fLowQPairSwitch_E1E2[j]->At(k)=='0') continue;
2536                 }
2537                 if((fEvt+en3)->fTracks[k].fPt < fMinPt) continue; 
2538                 if((fEvt+en3)->fTracks[k].fPt > fMaxPt) continue;
2539
2540                 pVect3[0]=(fEvt+en3)->fTracks[k].fEaccepted;
2541                 pVect3[1]=(fEvt+en3)->fTracks[k].fP[0];
2542                 pVect3[2]=(fEvt+en3)->fTracks[k].fP[1];
2543                 pVect3[3]=(fEvt+en3)->fTracks[k].fP[2];
2544                 ch3 = Int_t(((fEvt+en3)->fTracks[k].fCharge + 1)/2.);
2545                 qinv13 = GetQinv(pVect1, pVect3);
2546                 qinv23 = GetQinv(pVect2, pVect3);
2547                 q3 = sqrt(pow(qinv12,2) + pow(qinv13,2) + pow(qinv23,2));
2548                 Int_t chGroup3[3]={ch1,ch2,ch3};
2549                 Float_t QinvMCGroup3[3]={0};
2550                 Float_t kTGroup3[3]={0};
2551                 FilledMCtriplet123 = kFALSE;
2552                 if(fMCcase && fGenerateSignal){
2553                   if((fEvt+en3)->fTracks[k].fLabel == (fEvt+en2)->fTracks[j].fLabel) continue;
2554                   if((fEvt+en3)->fTracks[k].fLabel == (fEvt)->fTracks[i].fLabel) continue;
2555                   
2556                   pVect3MC[0]=sqrt(pow((fEvt+en3)->fMCtracks[abs((fEvt+en3)->fTracks[k].fLabel)].fPtot,2)+pow(fTrueMassPi,2)); 
2557                   pVect3MC[1]=(fEvt+en3)->fMCtracks[abs((fEvt+en3)->fTracks[k].fLabel)].fPx;
2558                   pVect3MC[2]=(fEvt+en3)->fMCtracks[abs((fEvt+en3)->fTracks[k].fLabel)].fPy;
2559                   pVect3MC[3]=(fEvt+en3)->fMCtracks[abs((fEvt+en3)->fTracks[k].fLabel)].fPz;
2560                   qinv13MC = GetQinv(pVect1MC, pVect3MC);
2561                   qinv23MC = GetQinv(pVect2MC, pVect3MC);
2562                   QinvMCGroup3[0] = qinv12MC; QinvMCGroup3[1] = qinv13MC; QinvMCGroup3[2] = qinv23MC;
2563                 }
2564                 
2565                 
2566                 Bool_t fill2=kFALSE, fill3=kFALSE, fill4=kFALSE;
2567                 SetFillBins3(ch1, ch2, ch3, 1, bin1, bin2, bin3, fill2, fill3, fill4);
2568                 
2569                 Float_t KT3 = sqrt(pow(pVect1[1]+pVect2[1]+pVect3[1],2) + pow(pVect1[2]+pVect2[2]+pVect3[2],2))/3.;
2570                 if(KT3<=fKT3transition) KT3index=0;
2571                 else KT3index=1;
2572                 
2573                 FSICorr13 = FSICorrelation(ch1,ch3, qinv13);
2574                 FSICorr23 = FSICorrelation(ch2,ch3, qinv23);
2575                 if(!fGenerateSignal && !fMCcase) {
2576                   momBin12 = fMomResC2SC->GetYaxis()->FindBin(qinv12);
2577                   momBin13 = fMomResC2SC->GetYaxis()->FindBin(qinv13);
2578                   momBin23 = fMomResC2SC->GetYaxis()->FindBin(qinv23);            
2579                   if(momBin12 >= 20) momBin12 = 19;
2580                   if(momBin13 >= 20) momBin13 = 19;
2581                   if(momBin23 >= 20) momBin23 = 19;
2582                   //
2583                   if(ch1==ch2) MomResCorr12 = fMomResC2SC->GetBinContent(rBinForTPNMomRes, momBin12);
2584                   else MomResCorr12 = fMomResC2MC->GetBinContent(rBinForTPNMomRes, momBin12);
2585                   if(ch1==ch3) MomResCorr13 = fMomResC2SC->GetBinContent(rBinForTPNMomRes, momBin13);
2586                   else MomResCorr13 = fMomResC2MC->GetBinContent(rBinForTPNMomRes, momBin13);
2587                   if(ch2==ch3) MomResCorr23 = fMomResC2SC->GetBinContent(rBinForTPNMomRes, momBin23);
2588                   else MomResCorr23 = fMomResC2MC->GetBinContent(rBinForTPNMomRes, momBin23);
2589                 }
2590                 if(ENsum==0) {
2591                   Float_t Winput=1.0;
2592                   if(fMCcase && fGenerateSignal) Winput = MCWeight3(1, fRMax, ffcSqMRC, chGroup3, QinvMCGroup3, kTGroup3);
2593                   Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[fMbin].EDB[KT3index].ThreePT[0].fTerms3->Fill(q3, Winput); 
2594                   Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[fMbin].EDB[KT3index].ThreePT[0].fKfactor->Fill(q3, 1/(FSICorr12*FSICorr13*FSICorr23), Winput);
2595                   Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[fMbin].EDB[KT3index].ThreePT[0].fKfactorWeighted->Fill(q3, 1/(FSICorr12*FSICorr13*FSICorr23), MomResCorr12*MomResCorr13*MomResCorr23 * Winput);
2596                   Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[fMbin].EDB[KT3index].ThreePT[0].fMeanQinv->Fill(q3, qinv12);
2597                   Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[fMbin].EDB[KT3index].ThreePT[0].fMeanQinv->Fill(q3, qinv13);
2598                   Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[fMbin].EDB[KT3index].ThreePT[0].fMeanQinv->Fill(q3, qinv23);
2599                 }
2600                 if(ENsum==6) {
2601                   Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[fMbin].EDB[KT3index].ThreePT[4].fTerms3->Fill(q3);
2602                   Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[fMbin].EDB[KT3index].ThreePT[4].fMeanQinv->Fill(q3, qinv12);
2603                   Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[fMbin].EDB[KT3index].ThreePT[4].fMeanQinv->Fill(q3, qinv13);
2604                   Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[fMbin].EDB[KT3index].ThreePT[4].fMeanQinv->Fill(q3, qinv23);
2605                 }
2606                 if(ENsum==3){
2607                   Float_t Winput=1.0;
2608                   if(fill2) {
2609                     if(fMCcase && fGenerateSignal) Winput = MCWeight3(2, fRMax, ffcSqMRC, chGroup3, QinvMCGroup3, kTGroup3);
2610                     Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[fMbin].EDB[KT3index].ThreePT[1].fTerms3->Fill(q3, Winput);
2611                     Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[fMbin].EDB[KT3index].ThreePT[1].fKfactor->Fill(q3, 1/(FSICorr12), Winput);
2612                     Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[fMbin].EDB[KT3index].ThreePT[1].fKfactorWeighted->Fill(q3, 1/(FSICorr12), MomResCorr12 * Winput);
2613                     Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[fMbin].EDB[KT3index].ThreePT[1].fMeanQinv->Fill(q3, qinv12);
2614                     Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[fMbin].EDB[KT3index].ThreePT[1].fMeanQinv->Fill(q3, qinv13);
2615                     Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[fMbin].EDB[KT3index].ThreePT[1].fMeanQinv->Fill(q3, qinv23);
2616                   }if(fill3) {
2617                     if(fMCcase && fGenerateSignal) Winput = MCWeight3(3, fRMax, ffcSqMRC, chGroup3, QinvMCGroup3, kTGroup3);
2618                     Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[fMbin].EDB[KT3index].ThreePT[2].fTerms3->Fill(q3, Winput);
2619                     Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[fMbin].EDB[KT3index].ThreePT[2].fKfactor->Fill(q3, 1/(FSICorr12), Winput);
2620                     Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[fMbin].EDB[KT3index].ThreePT[2].fKfactorWeighted->Fill(q3, 1/(FSICorr12), MomResCorr12 * Winput);
2621                     Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[fMbin].EDB[KT3index].ThreePT[2].fMeanQinv->Fill(q3, qinv12);
2622                     Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[fMbin].EDB[KT3index].ThreePT[2].fMeanQinv->Fill(q3, qinv13);
2623                     Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[fMbin].EDB[KT3index].ThreePT[2].fMeanQinv->Fill(q3, qinv23);
2624                   }if(fill4) {
2625                     if(fMCcase && fGenerateSignal) Winput = MCWeight3(4, fRMax, ffcSqMRC, chGroup3, QinvMCGroup3, kTGroup3);
2626                     Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[fMbin].EDB[KT3index].ThreePT[3].fTerms3->Fill(q3, Winput);
2627                     Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[fMbin].EDB[KT3index].ThreePT[3].fKfactor->Fill(q3, 1/(FSICorr12), Winput);
2628                     Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[fMbin].EDB[KT3index].ThreePT[3].fKfactorWeighted->Fill(q3, 1/(FSICorr12), MomResCorr12 * Winput);
2629                     Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[fMbin].EDB[KT3index].ThreePT[3].fMeanQinv->Fill(q3, qinv12);
2630                     Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[fMbin].EDB[KT3index].ThreePT[3].fMeanQinv->Fill(q3, qinv13);
2631                     Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[fMbin].EDB[KT3index].ThreePT[3].fMeanQinv->Fill(q3, qinv23);
2632                   }
2633                 }
2634                 
2635                 // r3 denominator
2636                 if(ENsum==6 && ch1==ch2 && ch1==ch3){
2637                   Positive1stTripletWeights = kTRUE;
2638                   //
2639                   GetWeight(pVect1, pVect2, weight12, weight12Err);
2640                   GetWeight(pVect1, pVect3, weight13, weight13Err);
2641                   GetWeight(pVect2, pVect3, weight23, weight23Err);
2642                   
2643                   if(sqrt(fabs(weight12*weight13*weight23)) > 1.0) {// weight should never be larger than 1
2644                     if(fMbin==0 && bin1==0) {
2645                       ((TH1D*)fOutputList->FindObject("fTPNRejects3pion1"))->Fill(q3, sqrt(fabs(weight12*weight13*weight23)));
2646                     }
2647                   }else{
2648                     
2649                     Float_t MuonCorr12=1.0, MuonCorr13=1.0, MuonCorr23=1.0;
2650                     if(!fGenerateSignal && !fMCcase) {
2651                       MuonCorr12 = fWeightmuonCorrection->GetBinContent(rBinForTPNMomRes, momBin12);
2652                       MuonCorr13 = fWeightmuonCorrection->GetBinContent(rBinForTPNMomRes, momBin13);
2653                       MuonCorr23 = fWeightmuonCorrection->GetBinContent(rBinForTPNMomRes, momBin23);
2654                     }
2655                     
2656                     // no MRC, no Muon Correction
2657                     weight12CC[0] = ((weight12+1) - ffcSq*FSICorr12 - (1-ffcSq));
2658                     weight12CC[0] /= FSICorr12*ffcSq;
2659                     weight13CC[0] = ((weight13+1) - ffcSq*FSICorr13 - (1-ffcSq));
2660                     weight13CC[0] /= FSICorr13*ffcSq;
2661                     weight23CC[0] = ((weight23+1) - ffcSq*FSICorr23 - (1-ffcSq));
2662                     weight23CC[0] /= FSICorr23*ffcSq;
2663                     if(weight12CC[0] > 0 && weight13CC[0] > 0 && weight23CC[0] > 0){
2664                       Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[fMbin].EDB[KT3index].ThreePT[4].fTwoPartNorm->Fill(1, q3, sqrt(weight12CC[0]*weight13CC[0]*weight23CC[0]));
2665                     }
2666                     // no Muon Correction
2667                     weight12CC[1] = ((weight12+1)*MomResCorr12 - ffcSq*FSICorr12 - (1-ffcSq));
2668                     weight12CC[1] /= FSICorr12*ffcSq;
2669                     weight13CC[1] = ((weight13+1)*MomResCorr13 - ffcSq*FSICorr13 - (1-ffcSq));
2670                     weight13CC[1] /= FSICorr13*ffcSq;
2671                     weight23CC[1] = ((weight23+1)*MomResCorr23 - ffcSq*FSICorr23 - (1-ffcSq));
2672                     weight23CC[1] /= FSICorr23*ffcSq;
2673                     if(weight12CC[1] > 0 && weight13CC[1] > 0 && weight23CC[1] > 0){
2674                       Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[fMbin].EDB[KT3index].ThreePT[4].fTwoPartNorm->Fill(2, q3, sqrt(weight12CC[1]*weight13CC[1]*weight23CC[1]));
2675                     }
2676                     // both Corrections
2677                     weight12CC[2] = ((weight12+1)*MomResCorr12 - ffcSq*FSICorr12 - (1-ffcSq));
2678                     weight12CC[2] /= FSICorr12*ffcSq;
2679                     weight12CC[2] *= MuonCorr12;
2680                     weight13CC[2] = ((weight13+1)*MomResCorr13 - ffcSq*FSICorr13 - (1-ffcSq));
2681                     weight13CC[2] /= FSICorr13*ffcSq;
2682                     weight13CC[2] *= MuonCorr13;
2683                     weight23CC[2] = ((weight23+1)*MomResCorr23 - ffcSq*FSICorr23 - (1-ffcSq));
2684                     weight23CC[2] /= FSICorr23*ffcSq;
2685                     weight23CC[2] *= MuonCorr23;
2686                     
2687                     if(weight12CC[2] < 0 || weight13CC[2] < 0 || weight23CC[2] < 0) {// C2^QS can never be less than unity
2688                       if(fMbin==0 && bin1==0) {
2689                         ((TH1D*)fOutputList->FindObject("fTPNRejects3pion2"))->Fill(q3, sqrt(fabs(weight12CC[2]*weight13CC[2]*weight23CC[2])));
2690                       }
2691                       if(weight12CC[2] < 0) weight12CC[2]=0;
2692                       if(weight13CC[2] < 0) weight13CC[2]=0;
2693                       if(weight23CC[2] < 0) weight23CC[2]=0;
2694                       Positive1stTripletWeights = kFALSE;
2695                     }
2696                     /////////////////////////////////////////////////////
2697                     weightTotal = sqrt(weight12CC[2]*weight13CC[2]*weight23CC[2]);
2698                     /////////////////////////////////////////////////////
2699                     if(Positive1stTripletWeights){
2700                       Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[fMbin].EDB[KT3index].ThreePT[4].fTwoPartNorm->Fill(3, q3, weightTotal);
2701                       Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[fMbin].EDB[KT3index].ThreePT[4].fTwoPartNorm->Fill(4, q3, 1);
2702                     }else{
2703                       Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[fMbin].EDB[KT3index].ThreePT[4].fTwoPartNegNorm->Fill(4, q3, 1);
2704                     }
2705                     //
2706                     // Full Weight reconstruction
2707                     
2708                     for(Int_t RcohIndex=0; RcohIndex<2; RcohIndex++){// Rcoh=0, then Rcoh=Rch
2709                       for(Int_t GIndex=0; GIndex<50; GIndex++){
2710                         Int_t FillBin = 5 + RcohIndex*50 + GIndex;
2711                         Float_t G = 0.02*GIndex;
2712                         if(RcohIndex==0){
2713                           T12 = (-2*G*(1-G) + sqrt(pow(2*G*(1-G),2) + 4*pow(1-G,2)*weight12CC[2])) / (2*pow(1-G,2));
2714                           T13 = (-2*G*(1-G) + sqrt(pow(2*G*(1-G),2) + 4*pow(1-G,2)*weight13CC[2])) / (2*pow(1-G,2));
2715                           T23 = (-2*G*(1-G) + sqrt(pow(2*G*(1-G),2) + 4*pow(1-G,2)*weight23CC[2])) / (2*pow(1-G,2));
2716                           weightTotal = 2*G*(1-G)*(T12 + T13 + T23) + pow(1-G,2)*(T12*T12 + T13*T13 + T23*T23);
2717                           weightTotal += 2*G*pow(1-G,2)*(T12*T13 + T12*T23 + T13*T23) + 2*pow(1-G,3)*T12*T13*T23;
2718                         }else{
2719                           T12 = sqrt(weight12CC[2] / (1-G*G));
2720                           T13 = sqrt(weight13CC[2] / (1-G*G));
2721                           T23 = sqrt(weight23CC[2] / (1-G*G));
2722                           weightTotal = (1-G*G)*(T12*T12 + T13*T13 + T23*T23);
2723                           weightTotal += (6*G*pow(1-G,2) + 2*pow(1-G,3)) * T12*T13*T23;
2724                         }
2725                         if(Positive1stTripletWeights){
2726                           Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[fMbin].EDB[KT3index].ThreePT[4].fTwoPartNorm->Fill(FillBin, q3, weightTotal);
2727                         }else{
2728                           Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[fMbin].EDB[KT3index].ThreePT[4].fTwoPartNegNorm->Fill(FillBin, q3, weightTotal);
2729                         }
2730                       }
2731                     }
2732                     //
2733                     /*weight12CC_e = weight12Err*MomResCorr12 / FSICorr12 / ffcSq * MuonCorr12;
2734                       weight13CC_e = weight13Err*MomResCorr13 / FSICorr13 / ffcSq * MuonCorr13;
2735                       weight23CC_e = weight23Err*MomResCorr23 / FSICorr23 / ffcSq * MuonCorr23;
2736                       if(weight12CC[2]*weight13CC[2]*weight23CC[2] > 0){
2737                       weightTotalErr = pow(2 * sqrt(3) * weight12CC_e*weight13CC[2]*weight23CC[2] / sqrt(weight12CC[2]*weight13CC[2]*weight23CC[2]),2);
2738                       }
2739                       weightTotalErr += pow(weight12CC_e,2) + pow(weight13CC_e,2) + pow(weight23CC_e,2);
2740                       Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[fMbin].EDB[KT3index].ThreePT[4].fTwoPartNormErr->Fill(4, q3, weightTotalErr);*/
2741                     
2742                   }// 1st r3 den check
2743                   
2744                 }// r3 den
2745                 
2746
2747                 if(ch1==ch2 && ch1==ch3 && ENsum==0){
2748                   ((TH3D*)fOutputList->FindObject("fKT3DistTerm1"))->Fill(fMbin+1, KT3, q3);
2749                   if(q3<0.1){
2750                     Float_t pt1=sqrt(pow(pVect1[1],2)+pow(pVect1[2],2));
2751                     Float_t pt2=sqrt(pow(pVect2[1],2)+pow(pVect2[2],2));
2752                     Float_t pt3=sqrt(pow(pVect3[1],2)+pow(pVect3[2],2));
2753                     ((TProfile2D*)fOutputList->FindObject("fKT3AvgpT"))->Fill(fMbin+1, KT3index, pt1);
2754                     ((TProfile2D*)fOutputList->FindObject("fKT3AvgpT"))->Fill(fMbin+1, KT3index, pt2);
2755                     ((TProfile2D*)fOutputList->FindObject("fKT3AvgpT"))->Fill(fMbin+1, KT3index, pt3);
2756                     if(fMbin==0){
2757                       ((TH3D*)fOutputList->FindObject("fQ3AvgpT"))->Fill(KT3index, q3, pt1);
2758                       ((TH3D*)fOutputList->FindObject("fQ3AvgpT"))->Fill(KT3index, q3, pt2);
2759                       ((TH3D*)fOutputList->FindObject("fQ3AvgpT"))->Fill(KT3index, q3, pt3);
2760                     }
2761                   }
2762                   
2763                 }
2764                 if(ch1==ch2 && ch1==ch3 && ENsum==6) ((TH3D*)fOutputList->FindObject("fKT3DistTerm5"))->Fill(fMbin+1, KT3, q3);
2765                 
2766                 
2767
2768                 
2769                 if(fMCcase && ENsum==6 && FilledMCpair12){// for momentum resolution and muon correction
2770                   if((fEvt+en3)->fTracks[k].fLabel < (fEvt+en3)->fMCarraySize){
2771                     
2772                     pVect3MC[0]=sqrt(pow((fEvt+en3)->fMCtracks[abs((fEvt+en3)->fTracks[k].fLabel)].fPtot,2)+pow(fTrueMassPi,2)); 
2773                     pVect3MC[1]=(fEvt+en3)->fMCtracks[abs((fEvt+en3)->fTracks[k].fLabel)].fPx;
2774                     pVect3MC[2]=(fEvt+en3)->fMCtracks[abs((fEvt+en3)->fTracks[k].fLabel)].fPy;
2775                     pVect3MC[3]=(fEvt+en3)->fMCtracks[abs((fEvt+en3)->fTracks[k].fLabel)].fPz;
2776                     qinv13MC = GetQinv(pVect1MC, pVect3MC);
2777                     qinv23MC = GetQinv(pVect2MC, pVect3MC);
2778                     
2779                     q3MC = sqrt(pow(qinv12MC,2)+pow(qinv13MC,2)+pow(qinv23MC,2));
2780                     if(q3<0.1 && ch1==ch2 && ch1==ch3) ((TH2D*)fOutputList->FindObject("fQ3Res"))->Fill(KT3, q3-q3MC);
2781                     
2782                     Float_t TripletWeightTTC=1.0;// same-charge weights to mimic two-track depletion of same-charge pairs
2783                     //if(ch1==ch2 && qinv12>0.006) TripletWeightTTC *= SCpairWeight->Eval(qinv12);
2784                     //if(ch1==ch3 && qinv13>0.006) TripletWeightTTC *= SCpairWeight->Eval(qinv13);
2785                     //if(ch2==ch3 && qinv23>0.006) TripletWeightTTC *= SCpairWeight->Eval(qinv23);
2786                     
2787                                     
2788                     Pparent3[0]=pVect3MC[0]; Pparent3[1]=pVect3MC[1]; Pparent3[2]=pVect3MC[2]; Pparent3[3]=pVect3MC[3];
2789                     pionParent3=kFALSE;
2790                   
2791                     if(abs((fEvt+en3)->fMCtracks[abs((fEvt+en3)->fTracks[k].fLabel)].fPdgCode)==13){// muon check
2792                       Int_t MotherLabel3 = (fEvt+en3)->fMCtracks[abs((fEvt+en3)->fTracks[k].fLabel)].fMotherLabel;
2793                       if(abs((fEvt+en3)->fMCtracks[MotherLabel3].fPdgCode)==211) {
2794                         pionParent3=kTRUE;
2795                         Pparent3[1] = (fEvt+en3)->fMCtracks[MotherLabel3].fPx; Pparent3[2] = (fEvt+en3)->fMCtracks[MotherLabel3].fPy; Pparent3[3] = (fEvt+en3)->fMCtracks[MotherLabel3].fPz;
2796                         Pparent3[0] = sqrt(pow(Pparent3[1],2)+pow(Pparent3[2],2)+pow(Pparent3[3],2)+pow(fTrueMassPi,2));
2797                       }
2798                     }
2799                     
2800                     parentQinv13 = GetQinv(Pparent1, Pparent3);
2801                     parentQinv23 = GetQinv(Pparent2, Pparent3);
2802                     parentQ3 = sqrt(pow(parentQinv12,2) + pow(parentQinv13,2) + pow(parentQinv23,2));
2803                    
2804                     if(parentQinv12 > 0.001 && parentQinv13 > 0.001 && parentQinv23 > 0.001 && parentQ3 < 0.5){
2805                       FilledMCtriplet123=kTRUE;
2806                       if(pionParent1 || pionParent2 || pionParent3) {// want at least one pion-->muon
2807                         
2808                         Float_t parentQinvGroup3[3]={parentQinv12, parentQinv13, parentQinv23};
2809                         //Float_t parentkTGroup3[3]={float(sqrt(pow(Pparent1[1]+Pparent2[1],2) + pow(Pparent1[2]+Pparent2[2],2))/2.),
2810                         //float(sqrt(pow(Pparent1[1]+Pparent3[1],2) + pow(Pparent1[2]+Pparent3[2],2))/2.),
2811                         //float(sqrt(pow(Pparent2[1]+Pparent3[1],2) + pow(Pparent2[2]+Pparent3[2],2))/2.)};
2812                         Float_t parentkTGroup3[3]={0};
2813                         
2814                         ((TH2D*)fOutputList->FindObject("fAvgQ12VersusQ3"))->Fill(parentQ3, parentQinv12);
2815                         ((TH2D*)fOutputList->FindObject("fAvgQ13VersusQ3"))->Fill(parentQ3, parentQinv13);
2816                         ((TH2D*)fOutputList->FindObject("fAvgQ23VersusQ3"))->Fill(parentQ3, parentQinv23);
2817
2818                         for(Int_t term=1; term<=4; term++){
2819                           if(term==1) {}
2820                           else if(term==2) {if(!pionParent1 && !pionParent2) continue;}
2821                           else if(term==3) {if(!pionParent1 && !pionParent3) continue;}
2822                           else {if(!pionParent2 && !pionParent3) continue;}
2823                           for(Int_t Riter=0; Riter<fRVALUES; Riter++){
2824                             Float_t Rvalue = 5+Riter;
2825                             Float_t WInput = MCWeight3(term, Rvalue, 1.0, chGroup3, parentQinvGroup3, parentkTGroup3);
2826                             Float_t WInputParentFSI = MCWeightFSI3(term, Rvalue, 1.0, chGroup3, parentQinvGroup3);
2827                             Float_t WInputFSI = MCWeightFSI3(term, Rvalue, 1.0, chGroup3, QinvMCGroup3);
2828                             Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[0].EDB[0].ThreePT[term-1].fMuonSmeared->Fill(1, Rvalue, q3MC, WInput*TripletWeightTTC);
2829                             Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[0].EDB[0].ThreePT[term-1].fMuonIdeal->Fill(1, Rvalue, parentQ3, WInput*TripletWeightTTC);
2830                             Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[0].EDB[0].ThreePT[term-1].fMuonPionK3->Fill(1, Rvalue, q3MC, WInputFSI*TripletWeightTTC);
2831                             Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[0].EDB[0].ThreePT[term-1].fPionPionK3->Fill(1, Rvalue, parentQ3, WInputParentFSI*TripletWeightTTC);
2832                             //
2833                             Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[0].EDB[0].ThreePT[term-1].fMuonSmeared->Fill(2, Rvalue, q3MC, TripletWeightTTC);
2834                             Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[0].EDB[0].ThreePT[term-1].fMuonIdeal->Fill(2, Rvalue, parentQ3, TripletWeightTTC);
2835                             Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[0].EDB[0].ThreePT[term-1].fMuonPionK3->Fill(2, Rvalue, q3MC, TripletWeightTTC);
2836                             Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[0].EDB[0].ThreePT[term-1].fPionPionK3->Fill(2, Rvalue, parentQ3, TripletWeightTTC);
2837                           }// Riter
2838                         }// term loop
2839                     
2840                       }// pion parent check
2841                     }// parentQ check (muon correction)
2842                     
2843                     // 3-pion momentum resolution
2844                     for(Int_t term=1; term<=5; term++){
2845                       for(Int_t Riter=0; Riter<fRVALUES; Riter++){
2846                         Float_t Rvalue = 5+Riter;
2847                         Float_t WInput = MCWeight3(term, Rvalue, ffcSqMRC, chGroup3, QinvMCGroup3, kTGroup3);
2848                         Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[0].EDB[KT3index].ThreePT[term-1].fIdeal->Fill(Rvalue, q3MC, WInput*TripletWeightTTC);
2849                         Charge1[bin1].Charge2[bin2].Charge3[bin3].MB[0].EDB[KT3index].ThreePT[term-1].fSmeared->Fill(Rvalue, q3, WInput*TripletWeightTTC);
2850                       }
2851                     }
2852                     
2853                   }// 3rd particle label check
2854                 }// MCcase and ENsum==6
2855                 
2856                 
2857                 
2858
2859                 /////////////////////////////////////////////////////////////
2860                 for (Int_t l=k+1; l<(fEvt+en4)->fNtracks; l++) {// 4th particle
2861                   if(en4==0){
2862                     if(fLowQPairSwitch_E0E0[i]->At(l)=='0') continue;
2863                     if(fLowQPairSwitch_E0E0[j]->At(l)=='0') continue;
2864                     if(fLowQPairSwitch_E0E0[k]->At(l)=='0') continue;
2865                   }else if(en4==1){
2866                     if(en3==0){
2867                       if(fLowQPairSwitch_E0E1[i]->At(l)=='0') continue;
2868                       if(fLowQPairSwitch_E0E1[j]->At(l)=='0') continue;
2869                       if(fLowQPairSwitch_E0E1[k]->At(l)=='0') continue;
2870                     }else{ 
2871                       if(fLowQPairSwitch_E0E1[i]->At(l)=='0') continue;
2872                       if(fLowQPairSwitch_E0E1[j]->At(l)=='0') continue;
2873                       if(fLowQPairSwitch_E1E1[k]->At(l)=='0') continue;
2874                     }
2875                   }else if(en4==2){
2876                     if(fLowQPairSwitch_E0E2[i]->At(l)=='0') continue;
2877                     if(fLowQPairSwitch_E0E2[j]->At(l)=='0') continue;
2878                     if(fLowQPairSwitch_E1E2[k]->At(l)=='0') continue;
2879                   }else{
2880                     if(fLowQPairSwitch_E0E3[i]->At(l)=='0') continue;
2881                     if(fLowQPairSwitch_E1E3[j]->At(l)=='0') continue;
2882                     if(fLowQPairSwitch_E2E3[k]->At(l)=='0') continue;
2883                   }
2884                   if((fEvt+en4)->fTracks[l].fPt < fMinPt) continue; 
2885                   if((fEvt+en4)->fTracks[l].fPt > fMaxPt) continue;
2886                   
2887                   pVect4[0]=(fEvt+en4)->fTracks[l].fEaccepted;
2888                   pVect4[1]=(fEvt+en4)->fTracks[l].fP[0];
2889                   pVect4[2]=(fEvt+en4)->fTracks[l].fP[1];
2890                   pVect4[3]=(fEvt+en4)->fTracks[l].fP[2];
2891                   ch4 = Int_t(((fEvt+en4)->fTracks[l].fCharge + 1)/2.);
2892                   qinv14 = GetQinv(pVect1, pVect4);
2893                   qinv24 = GetQinv(pVect2, pVect4);
2894                   qinv34 = GetQinv(pVect3, pVect4);
2895                   q4 = sqrt(pow(q3,2) + pow(qinv14,2) + pow(qinv24,2) + pow(qinv34,2));
2896                   Int_t chGroup4[4]={ch1,ch2,ch3,ch4};
2897                   Float_t QinvMCGroup4[6]={0};
2898                   Float_t kTGroup4[6]={0};
2899                   
2900                   if(fMCcase && fGenerateSignal){// for momentum resolution and muon correction
2901                     if((fEvt+en4)->fTracks[l].fLabel == (fEvt+en3)->fTracks[k].fLabel) continue;
2902                     if((fEvt+en4)->fTracks[l].fLabel == (fEvt+en2)->fTracks[j].fLabel) continue;
2903                     if((fEvt+en4)->fTracks[l].fLabel == (fEvt)->fTracks[i].fLabel) continue;
2904                     
2905                     pVect4MC[0]=sqrt(pow((fEvt+en4)->fMCtracks[abs((fEvt+en4)->fTracks[l].fLabel)].fPtot,2)+pow(fTrueMassPi,2)); 
2906                     pVect4MC[1]=(fEvt+en4)->fMCtracks[abs((fEvt+en4)->fTracks[l].fLabel)].fPx;
2907                     pVect4MC[2]=(fEvt+en4)->fMCtracks[abs((fEvt+en4)->fTracks[l].fLabel)].fPy;
2908                     pVect4MC[3]=(fEvt+en4)->fMCtracks[abs((fEvt+en4)->fTracks[l].fLabel)].fPz;
2909                     qinv14MC = GetQinv(pVect1MC, pVect4MC);
2910                     qinv24MC = GetQinv(pVect2MC, pVect4MC);
2911                     qinv34MC = GetQinv(pVect3MC, pVect4MC);
2912                     
2913                     QinvMCGroup4[0] = qinv12MC; QinvMCGroup4[1] = qinv13MC; QinvMCGroup4[2] = qinv14MC;
2914                     QinvMCGroup4[3] = qinv23MC; QinvMCGroup4[4] = qinv24MC; QinvMCGroup4[5] = qinv34MC;
2915                     //if(q4<0.1 && ENsum==0 && bin1==bin2 && bin1==bin3 && bin1==bin4) cout<<q4<<"  "<<fRMax<<"  "<<ffcSqMRC<<"  "<<chGroup4[0]<<"  "<<chGroup4[1]<<"  "<<chGroup4[2]<<"  "<<chGroup4[3]<<"  "<<QinvMCGroup4[0]<<"  "<<QinvMCGroup4[1]<<"  "<<QinvMCGroup4[2]<<"  "<<QinvMCGroup4[3]<<"  "<<QinvMCGroup4[4]<<"  "<<QinvMCGroup4[5]<<endl;
2916                   
2917                   }
2918                   if(ch1==ch2 && ch1==ch3 && ch1==ch4 && ENsum==6){
2919                     ((TH2D*)fOutputList->FindObject("DistQinv4pion"))->Fill(1, qinv12); ((TH2D*)fOutputList->FindObject("DistQinv4pion"))->Fill(2, qinv13); 
2920                     ((TH2D*)fOutputList->FindObject("DistQinv4pion"))->Fill(3, qinv14); ((TH2D*)fOutputList->FindObject("DistQinv4pion"))->Fill(4, qinv23); 
2921                     ((TH2D*)fOutputList->FindObject("DistQinv4pion"))->Fill(5, qinv24); ((TH2D*)fOutputList->FindObject("DistQinv4pion"))->Fill(6, qinv34);
2922                   }
2923                   
2924                   Float_t KT4 = sqrt(pow(pVect1[1]+pVect2[1]+pVect3[1]+pVect4[1],2) + pow(pVect1[2]+pVect2[2]+pVect3[2]+pVect4[2],2))/4.;
2925                   if(KT4<=fKT4transition) KT4index=0;
2926                   else KT4index=1;
2927                   
2928                   FSICorr14 = FSICorrelation(ch1,ch4, qinv14);
2929                   FSICorr24 = FSICorrelation(ch2,ch4, qinv24);
2930                   FSICorr34 = FSICorrelation(ch3,ch4, qinv34);
2931                   
2932                   if(!fGenerateSignal && !fMCcase) {
2933                     momBin14 = fMomResC2SC->GetYaxis()->FindBin(qinv14);
2934                     momBin24 = fMomResC2SC->GetYaxis()->FindBin(qinv24);
2935                     momBin34 = fMomResC2SC->GetYaxis()->FindBin(qinv34);                  
2936                     if(momBin14 >= 20) momBin14 = 19;
2937                     if(momBin24 >= 20) momBin24 = 19;
2938                     if(momBin34 >= 20) momBin34 = 19;
2939                     //
2940                     if(ch1==ch4) MomResCorr14 = fMomResC2SC->GetBinContent(rBinForTPNMomRes, momBin14);
2941                     else MomResCorr14 = fMomResC2MC->GetBinContent(rBinForTPNMomRes, momBin14);
2942                     if(ch2==ch4) MomResCorr24 = fMomResC2SC->GetBinContent(rBinForTPNMomRes, momBin24);
2943                     else MomResCorr24 = fMomResC2MC->GetBinContent(rBinForTPNMomRes, momBin24);
2944                     if(ch3==ch4) MomResCorr34 = fMomResC2SC->GetBinContent(rBinForTPNMomRes, momBin34);
2945                     else MomResCorr34 = fMomResC2MC->GetBinContent(rBinForTPNMomRes, momBin34);
2946                   }
2947                   
2948                   Bool_t FillTerms[13]={kFALSE};
2949                   SetFillBins4(ch1, ch2, ch3, ch4, bin1, bin2, bin3, bin4, ENsum, FillTerms);
2950                   //
2951                   for(int ft=0; ft<13; ft++) {
2952                     Float_t FSIfactor = 1.0;
2953                     Float_t MomResWeight = 1.0;
2954                     Float_t WInput = 1.0;
2955                     if(fMCcase && fGenerateSignal) WInput = MCWeight4(ft+1, fRMax, ffcSqMRC, chGroup4, QinvMCGroup4, kTGroup4);
2956                     if(ft==0) {
2957                       FSIfactor = 1/(FSICorr12 * FSICorr13 * FSICorr14 * FSICorr23 * FSICorr24 * FSICorr34);
2958                       MomResWeight = MomResCorr12 * MomResCorr13 * MomResCorr14 * MomResCorr23 * MomResCorr24 * MomResCorr34;
2959                     }else if(ft<=4) {
2960                       FSIfactor = 1/(FSICorr12 * FSICorr13 * FSICorr23);
2961                       MomResWeight = MomResCorr12 * MomResCorr13 * MomResCorr23;
2962                     }else if(ft<=10) {
2963                       FSIfactor = 1/(FSICorr12);
2964                       MomResWeight = MomResCorr12;
2965                     }else if(ft==11) {
2966                       FSIfactor = 1/(FSICorr12 * FSICorr34);
2967                       MomResWeight = MomResCorr12 * MomResCorr34;
2968                     }else {FSIfactor = 1.0; MomResWeight = 1.0;}
2969                     if(FillTerms[ft]) {
2970                       Charge1[bin1].Charge2[bin2].Charge3[bin3].Charge4[bin4].MB[fMbin].EDB[KT4index].FourPT[ft].fTerms4->Fill(q4, WInput);
2971                       Charge1[bin1].Charge2[bin2].Charge3[bin3].Charge4[bin4].MB[fMbin].EDB[KT4index].FourPT[ft].fKfactor->Fill(q4, FSIfactor, WInput);
2972                       Charge1[bin1].Charge2[bin2].Charge3[bin3].Charge4[bin4].MB[fMbin].EDB[KT4index].FourPT[ft].fKfactorWeighted->Fill(q4, FSIfactor, MomResWeight*WInput);
2973                     }
2974                   }
2975                   
2976                   /////////////////////////////////////////////////////////////
2977                   // r4{2}
2978                   if(ch1==ch2 && ch1==ch3 && ch1==ch4 && ENsum==6){
2979                     Positive2ndTripletWeights=kTRUE;
2980                     //
2981                     GetWeight(pVect1, pVect4, weight14, weight14Err);
2982                     GetWeight(pVect2, pVect4, weight24, weight24Err);
2983                     GetWeight(pVect3, pVect4, weight34, weight34Err);
2984                     
2985                     Float_t MuonCorr14=1.0, MuonCorr24=1.0, MuonCorr34=1.0;
2986                     if(!fGenerateSignal && !fMCcase) {
2987                       MuonCorr14 = fWeightmuonCorrection->GetBinContent(rBinForTPNMomRes, momBin14);
2988                       MuonCorr24 = fWeightmuonCorrection->GetBinContent(rBinForTPNMomRes, momBin24);
2989                       MuonCorr34 = fWeightmuonCorrection->GetBinContent(rBinForTPNMomRes, momBin34);
2990                     }
2991                     
2992                     // no MRC, no Muon Correction
2993                     weight14CC[0] = ((weight14+1) - ffcSq*FSICorr14 - (1-ffcSq));
2994                     weight14CC[0] /= FSICorr14*ffcSq;
2995                     weight24CC[0] = ((weight24+1) - ffcSq*FSICorr24 - (1-ffcSq));
2996                     weight24CC[0] /= FSICorr24*ffcSq;
2997                     weight34CC[0] = ((weight34+1) - ffcSq*FSICorr34 - (1-ffcSq));
2998                     weight34CC[0] /= FSICorr34*ffcSq;
2999                     if(weight14CC[0] > 0 && weight24CC[0] > 0 && weight34CC[0] > 0 && weight12CC[0] > 0 && weight13CC[0] > 0 && weight23CC[0] > 0){
3000                       weightTotal  = sqrt(weight12CC[0]*weight13CC[0]*weight24CC[0]*weight34CC[0]);
3001                       weightTotal += sqrt(weight12CC[0]*weight14CC[0]*weight23CC[0]*weight34CC[0]);
3002                       weightTotal += sqrt(weight13CC[0]*weight14CC[0]*weight23CC[0]*weight24CC[0]);
3003                       weightTotal /= 3.;
3004                       Charge1[bin1].Charge2[bin2].Charge3[bin3].Charge4[bin4].MB[fMbin].EDB[KT4index].FourPT[12].fTwoPartNorm->Fill(1, q4, weightTotal);
3005                     }
3006                     // no Muon Correction
3007                     weight14CC[1] = ((weight14+1)*MomResCorr14 - ffcSq*FSICorr14 - (1-ffcSq));
3008                     weight14CC[1] /= FSICorr14*ffcSq;
3009                     weight24CC[1] = ((weight24+1)*MomResCorr24 - ffcSq*FSICorr24 - (1-ffcSq));
3010                     weight24CC[1] /= FSICorr24*ffcSq;
3011                     weight34CC[1] = ((weight34+1)*MomResCorr34 - ffcSq*FSICorr34 - (1-ffcSq));
3012                     weight34CC[1] /= FSICorr34*ffcSq;
3013                     if(weight14CC[1] > 0 && weight24CC[1] > 0 && weight34CC[1] > 0 && weight12CC[1] > 0 && weight13CC[1] > 0 && weight23CC[1] > 0){
3014                       weightTotal  = sqrt(weight12CC[1]*weight13CC[1]*weight24CC[1]*weight34CC[1]);
3015                       weightTotal += sqrt(weight12CC[1]*weight14CC[1]*weight23CC[1]*weight34CC[1]);
3016                       weightTotal += sqrt(weight13CC[1]*weight14CC[1]*weight23CC[1]*weight24CC[1]);
3017                       weightTotal /= 3.;
3018                       Charge1[bin1].Charge2[bin2].Charge3[bin3].Charge4[bin4].MB[fMbin].EDB[KT4index].FourPT[12].fTwoPartNorm->Fill(2, q4, weightTotal);
3019                     }
3020                     // both corrections
3021                     weight14CC[2] = ((weight14+1)*MomResCorr14 - ffcSq*FSICorr14 - (1-ffcSq));
3022                     weight14CC[2] /= FSICorr14*ffcSq;
3023                     weight14CC[2] *= MuonCorr14;
3024                     weight24CC[2] = ((weight24+1)*MomResCorr24 - ffcSq*FSICorr24 - (1-ffcSq));
3025                     weight24CC[2] /= FSICorr24*ffcSq;
3026                     weight24CC[2] *= MuonCorr24;
3027                     weight34CC[2] = ((weight34+1)*MomResCorr34 - ffcSq*FSICorr34 - (1-ffcSq));
3028                     weight34CC[2] /= FSICorr34*ffcSq;
3029                     weight34CC[2] *= MuonCorr34;
3030  
3031                     if(weight14CC[2] < 0 || weight24CC[2] < 0 || weight34CC[2] < 0) {// C2^QS can never be less than unity
3032                       if(fMbin==0 && bin1==0 && KT4index==0) {
3033                         ((TH1D*)fOutputList->FindObject("fTPNRejects4pion1"))->Fill(q4, sqrt(fabs(weight12CC[2]*weight23CC[2]*weight34CC[2]*weight14CC[2])));
3034                       }
3035                       if(weight14CC[2] < 0) weight14CC[2]=0;
3036                       if(weight24CC[2] < 0) weight24CC[2]=0;
3037                       if(weight34CC[2] < 0) weight34CC[2]=0;
3038                       Positive2ndTripletWeights=kFALSE;
3039                     }
3040                     /////////////////////////////////////////////////////
3041                     weightTotal  = sqrt(weight12CC[2]*weight13CC[2]*weight24CC[2]*weight34CC[2]);
3042                     weightTotal += sqrt(weight12CC[2]*weight14CC[2]*weight23CC[2]*weight34CC[2]);
3043                     weightTotal += sqrt(weight13CC[2]*weight14CC[2]*weight23CC[2]*weight24CC[2]);
3044                     weightTotal /= 3.;
3045                     if(Positive1stTripletWeights && Positive2ndTripletWeights){
3046                       Charge1[bin1].Charge2[bin2].Charge3[bin3].Charge4[bin4].MB[fMbin].EDB[KT4index].FourPT[12].fTwoPartNorm->Fill(3, q4, weightTotal);
3047                       Charge1[bin1].Charge2[bin2].Charge3[bin3].Charge4[bin4].MB[fMbin].EDB[KT4index].FourPT[12].fTwoPartNorm->Fill(4, q4, 1);
3048                     }else{
3049                       Charge1[bin1].Charge2[bin2].Charge3[bin3].Charge4[bin4].MB[fMbin].EDB[KT4index].FourPT[12].fTwoPartNegNorm->Fill(4, q4, 1);
3050                     }
3051                     // Full Weight reconstruction
3052                     for(Int_t RcohIndex=0; RcohIndex<2; RcohIndex++){// Rcoh=0, then Rcoh=Rch
3053                       for(Int_t GIndex=0; GIndex<50; GIndex++){// 20 is enough
3054                         Int_t FillBin = 5 + RcohIndex*50 + GIndex;
3055                         Float_t G = 0.02*GIndex;
3056                         if(RcohIndex==0){// Rcoh=0
3057                           Float_t a = pow(1-G,2);
3058                           Float_t b = 2*G*(1-G);
3059                           T12 = (-b + sqrt(pow(b,2) + 4*a*weight12CC[2])) / (2*a);
3060                           T13 = (-b + sqrt(pow(b,2) + 4*a*weight13CC[2])) / (2*a);
3061                           T14 = (-b + sqrt(pow(b,2) + 4*a*weight14CC[2])) / (2*a);
3062                           T23 = (-b + sqrt(pow(b,2) + 4*a*weight23CC[2])) / (2*a);
3063                           T24 = (-b + sqrt(pow(b,2) + 4*a*weight24CC[2])) / (2*a);
3064                           T34 = (-b + sqrt(pow(b,2) + 4*a*weight34CC[2])) / (2*a);
3065                           weightTotal = 2*G*(1-G)*(T12 + T13 + T14 + T23 + T24 + T34) + pow(1-G,2)*(T12*T12 + T13*T13 + T14*T14 + T23*T23 + T24*T24 + T34*T34);// 2-pion
3066                           weightTotal += 2*G*pow(1-G,3)*(T12*T34*T34 + T12*T12*T34 + T13*T24*T24 + T13*T13*T24 + T14*T23*T23 + T14*T14*T23);// 2-pair
3067                           weightTotal += pow(1-G,4)*(pow(T12,2)*pow(T34,2) + pow(T13,2)*pow(T24,2) + pow(T14,2)*pow(T23,2));// 2-pair fully chaotic
3068                           weightTotal += 2*G*pow(1-G,2)*(T12*T13 + T12*T23 + T13*T23  + T12*T14 + T12*T24 + T14*T24);// 3-pion
3069                           weightTotal += 2*G*pow(1-G,2)*(T13*T14 + T13*T34 + T14*T34  + T23*T24 + T23*T34 + T24*T34);// 3-pion
3070                           weightTotal += 2*pow(1-G,3)*(T12*T13*T23 + T12*T14*T24 + T13*T14*T34 + T23*T24*T34);// 3-pion fully chaotic
3071                           weightTotal += 2*G*pow(1-G,3)*(T12*T14*T34 + T12*T14*T23 + T12*T23*T34 + T14*T23*T34);// 4-pion
3072                           weightTotal += 2*G*pow(1-G,3)*(T12*T13*T34 + T12*T34*T24 + T12*T24*T13 + T13*T24*T34);// 4-pion
3073                           weightTotal += 2*G*pow(1-G,3)*(T14*T13*T23 + T14*T13*T24 + T13*T23*T24 + T14*T24*T23);// 4-pion
3074                           weightTotal += 2*pow(1-G,4)*(T12*T13*T24*T34 + T12*T14*T23*T34 + T13*T14*T23*T24);// 4-pion fully chaotic
3075                         }else{// Rcoh=Rch
3076                           T12 = sqrt(weight12CC[2] / (1-G*G));
3077                           T13 = sqrt(weight13CC[2] / (1-G*G));
3078                           T14 = sqrt(weight14CC[2] / (1-G*G));
3079                           T23 = sqrt(weight23CC[2] / (1-G*G));
3080                           T24 = sqrt(weight24CC[2] / (1-G*G));
3081                           T34 = sqrt(weight34CC[2] / (1-G*G));
3082                           weightTotal = (1-G*G)*(T12*T12 + T13*T13 + T14*T14 + T23*T23 + T24*T24 + T34*T34);// 2-pion
3083                           weightTotal += (4*G*pow(1-G,3)+pow(1-G,4))*(pow(T12,2)*pow(T34,2) + pow(T13,2)*pow(T24,2) + pow(T14,2)*pow(T23,2));// 2-pair
3084                           weightTotal += (6*G*pow(1-G,2) + 2*pow(1-G,3))*(T12*T13*T23);// 3-pion
3085                           weightTotal += (6*G*pow(1-G,2) + 2*pow(1-G,3))*(T12*T14*T24);// 3-pion
3086                           weightTotal += (6*G*pow(1-G,2) + 2*pow(1-G,3))*(T13*T14*T34);// 3-pion
3087                           weightTotal += (6*G*pow(1-G,2) + 2*pow(1-G,3))*(T23*T24*T34);// 3-pion
3088                           weightTotal += (8*G*pow(1-G,3) + 2*pow(1-G,4))*(T12*T13*T24*T34);// 4-pion
3089                           weightTotal += (8*G*pow(1-G,3) + 2*pow(1-G,4))*(T12*T14*T23*T34);// 4-pion
3090                           weightTotal += (8*G*pow(1-G,3) + 2*pow(1-G,4))*(T13*T14*T23*T24);// 4-pion
3091                         }
3092                         if(Positive1stTripletWeights && Positive2ndTripletWeights){
3093                           Charge1[bin1].Charge2[bin2].Charge3[bin3].Charge4[bin4].MB[fMbin].EDB[KT4index].FourPT[12].fTwoPartNorm->Fill(FillBin, q4, weightTotal);
3094                         }else{
3095                           Charge1[bin1].Charge2[bin2].Charge3[bin3].Charge4[bin4].MB[fMbin].EDB[KT4index].FourPT[12].fTwoPartNegNorm->Fill(FillBin, q4, weightTotal);
3096                         }
3097                       }
3098                     }
3099                     // stat errors
3100                     /*weight14CC_e = weight14Err*MomResCorr14 / FSICorr14 / ffcSq * MuonCorr14;
3101                       weight24CC_e = weight24Err*MomResCorr24 / FSICorr24 / ffcSq * MuonCorr24;
3102                       weight34CC_e = weight34Err*MomResCorr34 / FSICorr34 / ffcSq * MuonCorr34;
3103                       if(weight12CC[2]*weight13CC[2]*weight24CC[2]*weight34CC[2] > 0){
3104                       weightTotalErr = pow( 6 * 2 * weight12CC_e*weight13CC[2]*weight24CC[2]*weight34CC[2] / sqrt(weight12CC[2]*weight13CC[2]*weight24CC[2]*weight34CC[2]),2);
3105                       }
3106                       if(weight12CC[2]*weight13CC[2]*weight23CC[2] > 0){
3107                       weightTotalErr += pow( 8 * sqrt(3) * weight12CC_e*weight13CC[2]*weight23CC[2] / sqrt(weight12CC[2]*weight13CC[2]*weight23CC[2]),2);
3108                       }
3109                       weightTotalErr += 2*(pow(weight12CC_e*weight34CC[2],2) + pow(weight13CC_e*weight24CC[2],2) + pow(weight14CC_e*weight23CC[2],2));
3110                       weightTotalErr += pow(weight12CC_e,2) + pow(weight13CC_e,2) + pow(weight14CC_e,2) + pow(weight23CC_e,2) + pow(weight24CC_e,2) + pow(weight34CC_e,2);
3111                       Charge1[bin1].Charge2[bin2].Charge3[bin3].Charge4[bin4].MB[fMbin].EDB[KT4index].FourPT[12].fTwoPartNormErr->Fill(4, q4, weightTotalErr);
3112                     */
3113                     if(fMbin==0 && KT4index==0){
3114                       for(Int_t Rindex=0; Rindex<7; Rindex++){
3115                         Float_t R = (6. + Rindex)/FmToGeV;
3116                         Float_t arg12=qinv12*R;
3117                         Float_t arg13=qinv13*R;
3118                         Float_t arg14=qinv14*R;
3119                         Float_t arg23=qinv23*R;
3120                         Float_t arg24=qinv24*R;
3121                         Float_t arg34=qinv34*R;
3122                         // Exchange Amplitudes
3123                         Float_t EA12 = exp(-pow(arg12,2)/2.)*(1 + kappa3Fit/(6.*pow(2.,1.5))*(8.*pow(arg12,3) - 12.*arg12) + kappa4Fit/(24.*pow(2.,2))*(16.*pow(arg12,4) -48.*pow(arg12,2) + 12));
3124                         Float_t EA13 = exp(-pow(arg13,2)/2.)*(1 + kappa3Fit/(6.*pow(2.,1.5))*(8.*pow(arg13,3) - 12.*arg13) + kappa4Fit/(24.*pow(2.,2))*(16.*pow(arg13,4) -48.*pow(arg13,2) + 12));
3125                         Float_t EA14 = exp(-pow(arg14,2)/2.)*(1 + kappa3Fit/(6.*pow(2.,1.5))*(8.*pow(arg14,3) - 12.*arg14) + kappa4Fit/(24.*pow(2.,2))*(16.*pow(arg14,4) -48.*pow(arg14,2) + 12));
3126                         Float_t EA23 = exp(-pow(arg23,2)/2.)*(1 + kappa3Fit/(6.*pow(2.,1.5))*(8.*pow(arg23,3) - 12.*arg23) + kappa4Fit/(24.*pow(2.,2))*(16.*pow(arg23,4) -48.*pow(arg23,2) + 12));
3127                         Float_t EA24 = exp(-pow(arg24,2)/2.)*(1 + kappa3Fit/(6.*pow(2.,1.5))*(8.*pow(arg24,3) - 12.*arg24) + kappa4Fit/(24.*pow(2.,2))*(16.*pow(arg24,4) -48.*pow(arg24,2) + 12));
3128                         Float_t EA34 = exp(-pow(arg34,2)/2.)*(1 + kappa3Fit/(6.*pow(2.,1.5))*(8.*pow(arg34,3) - 12.*arg34) + kappa4Fit/(24.*pow(2.,2))*(16.*pow(arg34,4) -48.*pow(arg34,2) + 12));
3129                         //
3130                         Float_t TotalCorrelation = 1 + 2*(EA12*EA13*EA24*EA34 + EA12*EA14*EA23*EA34 + EA13*EA14*EA23*EA24);
3131                         ((TH2D*)fOutputList->FindObject("fc4QSFitNum"))->Fill(Rindex+1, q4, TotalCorrelation);
3132                         ((TH2D*)fOutputList->FindObject("fc4QSFitDen"))->Fill(Rindex+1, q4);
3133                       }
3134                     }
3135                   }// SC and ENsum=6
3136                   /////////////////////////////////////////////////////////////
3137                   
3138                   if(ch1==ch2 && ch1==ch3 && ch1==ch4 && ENsum==0){
3139                     ((TH3D*)fOutputList->FindObject("fKT4DistTerm1"))->Fill(fMbin+1, KT4, q4);
3140                     if(q4<0.105){
3141                       Float_t pt1=sqrt(pow(pVect1[1],2)+pow(pVect1[2],2));
3142                       Float_t pt2=sqrt(pow(pVect2[1],2)+pow(pVect2[2],2));
3143                       Float_t pt3=sqrt(pow(pVect3[1],2)+pow(pVect3[2],2));
3144                       Float_t pt4=sqrt(pow(pVect4[1],2)+pow(pVect4[2],2));
3145                       ((TProfile2D*)fOutputList->FindObject("fKT4AvgpT"))->Fill(fMbin+1, KT4index, pt1);
3146                       ((TProfile2D*)fOutputList->FindObject("fKT4AvgpT"))->Fill(fMbin+1, KT4index, pt2);
3147                       ((TProfile2D*)fOutputList->FindObject("fKT4AvgpT"))->Fill(fMbin+1, KT4index, pt3);
3148                       ((TProfile2D*)fOutputList->FindObject("fKT4AvgpT"))->Fill(fMbin+1, KT4index, pt4);
3149                       if(fMbin==0){
3150                         ((TH3D*)fOutputList->FindObject("fQ4AvgpT"))->Fill(KT4index, q4, pt1);
3151                         ((TH3D*)fOutputList->FindObject("fQ4AvgpT"))->Fill(KT4index, q4, pt2);
3152                         ((TH3D*)fOutputList->FindObject("fQ4AvgpT"))->Fill(KT4index, q4, pt3);
3153                         ((TH3D*)fOutputList->FindObject("fQ4AvgpT"))->Fill(KT4index, q4, pt4);
3154                       }
3155                     }
3156                   }
3157                   if(ch1==ch2 && ch1==ch3 && ch1==ch4 && ENsum==6) ((TH3D*)fOutputList->FindObject("fKT4DistTerm13"))->Fill(fMbin+1, KT4, q4);
3158
3159
3160                   // momenumtum resolution and muon corrections
3161                   if(fMCcase && ENsum==6 && FilledMCtriplet123){// for momentum resolution and muon correction
3162                     if((fEvt+en4)->fTracks[l].fLabel < (fEvt+en4)->fMCarraySize){
3163                       
3164                       pVect4MC[0]=sqrt(pow((fEvt+en4)->fMCtracks[abs((fEvt+en4)->fTracks[l].fLabel)].fPtot,2)+pow(fTrueMassPi,2)); 
3165                       pVect4MC[1]=(fEvt+en4)->fMCtracks[abs((fEvt+en4)->fTracks[l].fLabel)].fPx;
3166                       pVect4MC[2]=(fEvt+en4)->fMCtracks[abs((fEvt+en4)->fTracks[l].fLabel)].fPy;
3167                       pVect4MC[3]=(fEvt+en4)->fMCtracks[abs((fEvt+en4)->fTracks[l].fLabel)].fPz;
3168                       qinv14MC = GetQinv(pVect1MC, pVect4MC);
3169                       qinv24MC = GetQinv(pVect2MC, pVect4MC);
3170                       qinv34MC = GetQinv(pVect3MC, pVect4MC);
3171                       
3172                       q4MC = sqrt(pow(q3MC,2) + pow(qinv14MC,2) +  pow(qinv24MC,2) +  pow(qinv34MC,2));
3173                       if(q4<0.1 && ch1==ch2 && ch1==ch3 && ch1==ch4) ((TH2D*)fOutputList->FindObject("fQ4Res"))->Fill(KT4, q4-q4MC);
3174                       if(ch1==ch2 && ch1==ch3 && ch1==ch4) {
3175                         ((TH2D*)fOutputList->FindObject("DistQinvMC4pion"))->Fill(1, qinv12MC); ((TH2D*)fOutputList->FindObject("DistQinvMC4pion"))->Fill(2, qinv13MC);
3176                         ((TH2D*)fOutputList->FindObject("DistQinvMC4pion"))->Fill(3, qinv14MC); ((TH2D*)fOutputList->FindObject("DistQinvMC4pion"))->Fill(4, qinv23MC);
3177                         ((TH2D*)fOutputList->FindObject("DistQinvMC4pion"))->Fill(5, qinv24MC); ((TH2D*)fOutputList->FindObject("DistQinvMC4pion"))->Fill(6, qinv34MC);
3178                       }
3179
3180                       Float_t QuadWeightTTC=1.0;// same-charge weights to mimic two-track depletion of same-charge pairs
3181                       //if(ch1==ch2 && qinv12>0.006) QuadWeightTTC *= SCpairWeight->Eval(qinv12);
3182                       //if(ch1==ch3 && qinv13>0.006) QuadWeightTTC *= SCpairWeight->Eval(qinv13);
3183                       //if(ch1==ch4 && qinv14>0.006) QuadWeightTTC *= SCpairWeight->Eval(qinv14);
3184                       //if(ch2==ch3 && qinv23>0.006) QuadWeightTTC *= SCpairWeight->Eval(qinv23);
3185                       //if(ch2==ch4 && qinv24>0.006) QuadWeightTTC *= SCpairWeight->Eval(qinv24);
3186                       //if(ch3==ch4 && qinv34>0.006) QuadWeightTTC *= SCpairWeight->Eval(qinv34);
3187                       
3188
3189                                       
3190                       Pparent4[0]=pVect4MC[0]; Pparent4[1]=pVect4MC[1]; Pparent4[2]=pVect4MC[2]; Pparent4[3]=pVect4MC[3];
3191                       pionParent4=kFALSE;
3192                       if(abs((fEvt+en4)->fMCtracks[abs((fEvt+en4)->fTracks[l].fLabel)].fPdgCode)==13){// muon check
3193                         Int_t MotherLabel4 = (fEvt+en4)->fMCtracks[abs((fEvt+en4)->fTracks[l].fLabel)].fMotherLabel;
3194                         if(abs((fEvt+en4)->fMCtracks[MotherLabel4].fPdgCode)==211) {
3195                           pionParent4=kTRUE;
3196                           Pparent4[1] = (fEvt+en4)->fMCtracks[MotherLabel4].fPx; Pparent4[2] = (fEvt+en4)->fMCtracks[MotherLabel4].fPy; Pparent4[3] = (fEvt+en4)->fMCtracks[MotherLabel4].fPz;
3197                           Pparent4[0] = sqrt(pow(Pparent4[1],2)+pow(Pparent4[2],2)+pow(Pparent4[3],2)+pow(fTrueMassPi,2));
3198                         }
3199                       }
3200
3201                       parentQinv14 = GetQinv(Pparent1, Pparent4);
3202                       parentQinv24 = GetQinv(Pparent2, Pparent4);
3203                       parentQinv34 = GetQinv(Pparent3, Pparent4);
3204                       Float_t parentQ4 = sqrt(pow(parentQ3,2) + pow(parentQinv14,2) + pow(parentQinv24,2) + pow(parentQinv34,2));
3205                       
3206                       if(parentQinv14 > 0.001 && parentQinv24 > 0.001 && parentQinv34 > 0.001 && parentQ4 < 0.5){
3207                         if(pionParent1 || pionParent2 || pionParent3 || pionParent4) {// want at least one pion-->muon
3208                          
3209                           if(pionParent1) ((TH1D*)fOutputList->FindObject("fDistPionParents4"))->Fill(1);
3210                           if(pionParent2) ((TH1D*)fOutputList->FindObject("fDistPionParents4"))->Fill(2);
3211                           if(pionParent3) ((TH1D*)fOutputList->FindObject("fDistPionParents4"))->Fill(3);
3212                           if(pionParent4) ((TH1D*)fOutputList->FindObject("fDistPionParents4"))->Fill(4);
3213                           Float_t parentQinvGroup4[6]={parentQinv12, parentQinv13, parentQinv14, parentQinv23, parentQinv24, parentQinv34};
3214                           Float_t parentkTGroup4[6]={0};
3215                           
3216                           for(Int_t term=1; term<=12; term++){
3217                             if(term==1) {}
3218                             else if(term==2) {if(!pionParent1 && !pionParent2 && !pionParent3) continue;}
3219                             else if(term==3) {if(!pionParent1 && !pionParent2 && !pionParent4) continue;}
3220                             else if(term==4) {if(!pionParent1 && !pionParent3 && !pionParent4) continue;}
3221                             else if(term==5) {if(!pionParent2 && !pionParent3 && !pionParent4) continue;}
3222                             else if(term==6) {if(!pionParent1 && !pionParent2) continue;}
3223                             else if(term==7) {if(!pionParent1 && !pionParent3) continue;}
3224                             else if(term==8) {if(!pionParent1 && !pionParent4) continue;}
3225                             else if(term==9) {if(!pionParent2 && !pionParent3) continue;}
3226                             else if(term==10) {if(!pionParent2 && !pionParent4) continue;}
3227                             else if(term==11) {if(!pionParent3 && !pionParent4) continue;}
3228                             else {} 
3229                             for(Int_t Riter=0; Riter<fRVALUES; Riter++){
3230                               Float_t Rvalue = 5+Riter;
3231                               Float_t WInput = MCWeight4(term, Rvalue, 1.0, chGroup4, parentQinvGroup4, parentkTGroup4);
3232                               Float_t WInputParentFSI = MCWeightFSI4(term, Rvalue, 1.0, chGroup4, parentQinvGroup4);
3233                               Float_t WInputFSI = MCWeightFSI4(term, Rvalue, 1.0, chGroup4, QinvMCGroup4);
3234                               Charge1[bin1].Charge2[bin2].Charge3[bin3].Charge4[bin4].MB[0].EDB[0].FourPT[term-1].fMuonSmeared->Fill(1, Rvalue, q4MC, WInput*QuadWeightTTC);
3235                               Charge1[bin1].Charge2[bin2].Charge3[bin3].Charge4[bin4].MB[0].EDB[0].FourPT[term-1].fMuonIdeal->Fill(1, Rvalue, parentQ4, WInput*QuadWeightTTC);
3236                               Charge1[bin1].Charge2[bin2].Charge3[bin3].Charge4[bin4].MB[0].EDB[0].FourPT[term-1].fMuonPionK4->Fill(1, Rvalue, q4MC, WInputFSI*QuadWeightTTC);
3237                               Charge1[bin1].Charge2[bin2].Charge3[bin3].Charge4[bin4].MB[0].EDB[0].FourPT[term-1].fPionPionK4->Fill(1, Rvalue, parentQ4, WInputParentFSI*QuadWeightTTC);
3238                               //
3239                               Charge1[bin1].Charge2[bin2].Charge3[bin3].Charge4[bin4].MB[0].EDB[0].FourPT[term-1].fMuonSmeared->Fill(2, Rvalue, q4MC, QuadWeightTTC);
3240                               Charge1[bin1].Charge2[bin2].Charge3[bin3].Charge4[bin4].MB[0].EDB[0].FourPT[term-1].fMuonIdeal->Fill(2, Rvalue, parentQ4, QuadWeightTTC);
3241                               Charge1[bin1].Charge2[bin2].Charge3[bin3].Charge4[bin4].MB[0].EDB[0].FourPT[term-1].fMuonPionK4->Fill(2, Rvalue, q4MC, QuadWeightTTC);
3242                               Charge1[bin1].Charge2[bin2].Charge3[bin3].Charge4[bin4].MB[0].EDB[0].FourPT[term-1].fPionPionK4->Fill(2, Rvalue, parentQ4, QuadWeightTTC);
3243                             }// Riter
3244                           }// term loop
3245                           
3246                         }// pion parent check
3247                       }// parentQ check (muon correction)
3248                       
3249                       // 4-pion momentum resolution
3250                       for(Int_t term=1; term<=13; term++){
3251                         for(Int_t Riter=0; Riter<fRVALUES; Riter++){
3252                           Float_t Rvalue = 5+Riter;
3253                           Float_t WInput = MCWeight4(term, Rvalue, ffcSqMRC, chGroup4, QinvMCGroup4, kTGroup4);
3254                           Charge1[bin1].Charge2[bin2].Charge3[bin3].Charge4[bin4].MB[0].EDB[KT4index].FourPT[term-1].fIdeal->Fill(Rvalue, q4MC, WInput*QuadWeightTTC);
3255                           Charge1[bin1].Charge2[bin2].Charge3[bin3].Charge4[bin4].MB[0].EDB[KT4index].FourPT[term-1].fSmeared->Fill(Rvalue, q4, WInput*QuadWeightTTC);
3256                         }
3257                       }
3258                       
3259                     }// label check particle 4
3260                   }// MCcase
3261                   
3262                 }// 4th particle
3263               }// 3rd particle
3264             }// 2nd particle
3265           }// 1st particle
3266           
3267         }// en4
3268       }// en3
3269     }// en2
3270     
3271     
3272
3273   
3274   
3275   
3276   
3277   // Post output data.
3278   PostData(1, fOutputList);
3279   
3280 }
3281 //________________________________________________________________________
3282 void AliFourPion::Terminate(Option_t *) 
3283 {
3284   // Called once at the end of the query
3285  
3286   cout<<"Done"<<endl;
3287
3288 }
3289 //________________________________________________________________________
3290 Bool_t AliFourPion::AcceptPair(AliFourPionTrackStruct first, AliFourPionTrackStruct second)
3291 {
3292   
3293   if(fabs(first.fEta-second.fEta) > fMinSepPairEta) return kTRUE;
3294   
3295   // propagate through B field to r=1m
3296   Float_t phi1 = first.fPhi - asin(first.fCharge*(0.1*fBfield)*0.15/first.fPt);// 0.15 for D=1m
3297   if(phi1 > 2*PI) phi1 -= 2*PI;
3298   if(phi1 < 0) phi1 += 2*PI;
3299   Float_t phi2 = second.fPhi - asin(second.fCharge*(0.1*fBfield)*0.15/second.fPt);// 0.15 for D=1m 
3300   if(phi2 > 2*PI) phi2 -= 2*PI;
3301   if(phi2 < 0) phi2 += 2*PI;
3302   
3303   Float_t deltaphi = phi1 - phi2;
3304   if(deltaphi > PI) deltaphi -= 2*PI;
3305   if(deltaphi < -PI) deltaphi += 2*PI;
3306   deltaphi = fabs(deltaphi);
3307
3308   if(deltaphi < fMinSepPairPhi) return kFALSE;// Min Separation
3309     
3310   
3311   // propagate through B field to r=1.6m
3312   phi1 = first.fPhi - asin(first.fCharge*(0.1*fBfield)*0.24/first.fPt);// mine. 0.24 for D=1.6m
3313   if(phi1 > 2*PI) phi1 -= 2*PI;
3314   if(phi1 < 0) phi1 += 2*PI;
3315   phi2 = second.fPhi - asin(second.fCharge*(0.1*fBfield)*0.24/second.fPt);// mine. 0.24 for D=1.6m 
3316   if(phi2 > 2*PI) phi2 -= 2*PI;
3317   if(phi2 < 0) phi2 += 2*PI;
3318   
3319   deltaphi = phi1 - phi2;
3320   if(deltaphi > PI) deltaphi -= 2*PI;
3321   if(deltaphi < -PI) deltaphi += 2*PI;
3322   deltaphi = fabs(deltaphi);
3323
3324   if(deltaphi < fMinSepPairPhi) return kFALSE;// Min Separation
3325   
3326   
3327    
3328   //
3329   
3330   /* Int_t ncl1 = first.fClusterMap.GetNbits();
3331   Int_t ncl2 = second.fClusterMap.GetNbits();
3332   Int_t sumCls = 0; Int_t sumSha = 0; Int_t sumQ = 0;
3333   Double_t shfrac = 0; Double_t qfactor = 0;
3334   for(Int_t imap = 0; imap < ncl1 && imap < ncl2; imap++) {
3335     if (first.fClusterMap.TestBitNumber(imap) && second.fClusterMap.TestBitNumber(imap)) {// Both clusters
3336       if (first.fSharedMap.TestBitNumber(imap) && second.fSharedMap.TestBitNumber(imap)) { // Shared
3337         sumQ++;
3338         sumCls+=2;
3339         sumSha+=2;}
3340       else {sumQ--; sumCls+=2;}
3341     }
3342     else if (first.fClusterMap.TestBitNumber(imap) || second.fClusterMap.TestBitNumber(imap)) {// Non shared
3343       sumQ++;
3344       sumCls++;}
3345   }
3346   if (sumCls>0) {
3347     qfactor = sumQ*1.0/sumCls;
3348     shfrac = sumSha*1.0/sumCls;
3349   }
3350   
3351   if(qfactor > fShareQuality || shfrac > fShareFraction) return kFALSE;
3352   */
3353   
3354   return kTRUE;
3355   
3356
3357 }
3358 //________________________________________________________________________
3359 Bool_t AliFourPion::AcceptPairPM(AliFourPionTrackStruct first, AliFourPionTrackStruct second)
3360 {// optional pair cuts for +- pairs
3361   
3362   if(fabs(first.fEta-second.fEta) > fMinSepPairEta) return kTRUE;
3363   
3364   // propagate through B field to r=1m
3365   Float_t phi1 = first.fPhi - asin(1.*(0.1*fBfield)*0.15/first.fPt);// 0.15 for D=1m
3366   if(phi1 > 2*PI) phi1 -= 2*PI;
3367   if(phi1 < 0) phi1 += 2*PI;
3368   Float_t phi2 = second.fPhi - asin(1.*(0.1*fBfield)*0.15/second.fPt);// 0.15 for D=1m 
3369   if(phi2 > 2*PI) phi2 -= 2*PI;
3370   if(phi2 < 0) phi2 += 2*PI;
3371   
3372   Float_t deltaphi = phi1 - phi2;
3373   if(deltaphi > PI) deltaphi -= 2*PI;
3374   if(deltaphi < -PI) deltaphi += 2*PI;
3375   deltaphi = fabs(deltaphi);
3376
3377   if(deltaphi < fMinSepPairPhi) return kFALSE;// Min Separation
3378     
3379   
3380   // propagate through B field to r=1.6m
3381   phi1 = first.fPhi - asin(1.*(0.1*fBfield)*0.24/first.fPt);// mine. 0.24 for D=1.6m
3382   if(phi1 > 2*PI) phi1 -= 2*PI;
3383   if(phi1 < 0) phi1 += 2*PI;
3384   phi2 = second.fPhi - asin(1.*(0.1*fBfield)*0.24/second.fPt);// mine. 0.24 for D=1.6m 
3385   if(phi2 > 2*PI) phi2 -= 2*PI;
3386   if(phi2 < 0) phi2 += 2*PI;
3387   
3388   deltaphi = phi1 - phi2;
3389   if(deltaphi > PI) deltaphi -= 2*PI;
3390   if(deltaphi < -PI) deltaphi += 2*PI;
3391   deltaphi = fabs(deltaphi);
3392
3393   if(deltaphi < fMinSepPairPhi) return kFALSE;// Min Separation
3394   
3395   return kTRUE;
3396   
3397 }
3398 //________________________________________________________________________
3399 Float_t AliFourPion::Gamov(Int_t chargeBin1, Int_t chargeBin2, Float_t qinv)
3400 {
3401   Float_t arg = G_Coeff/qinv;
3402   
3403   if(chargeBin1==chargeBin2) return (exp(arg)-1)/(arg);
3404   else {return (exp(-arg)-1)/(-arg);}
3405   
3406 }
3407 //________________________________________________________________________
3408 void AliFourPion::Shuffle(Int_t *iarr, Int_t i1, Int_t i2)
3409 {
3410   Int_t j, k;
3411   Int_t a = i2 - i1;
3412   for (Int_t i = i1; i < i2+1; i++) {
3413     j = (Int_t) (gRandom->Rndm() * a);
3414     k = iarr[j];
3415     iarr[j] = iarr[i];
3416     iarr[i] = k;
3417   }
3418 }
3419
3420
3421 //________________________________________________________________________
3422 Float_t AliFourPion::GetQinv(Float_t track1[], Float_t track2[]){
3423   
3424   Float_t qinv = sqrt( fabs(pow(track1[1]-track2[1],2) + pow(track1[2]-track2[2],2) + pow(track1[3]-track2[3],2) - pow(track1[0]-track2[0],2)) );
3425   return qinv;
3426   
3427 }
3428 //________________________________________________________________________
3429 void AliFourPion::GetQosl(Float_t track1[], Float_t track2[], Float_t& qout, Float_t& qside, Float_t& qlong){
3430  
3431   Float_t p0 = track1[0] + track2[0];
3432   Float_t px = track1[1] + track2[1];
3433   Float_t py = track1[2] + track2[2];
3434   Float_t pz = track1[3] + track2[3];
3435   
3436   Float_t mt = sqrt(p0*p0 - pz*pz);
3437   Float_t pt = sqrt(px*px + py*py);
3438   
3439   Float_t v0 = track1[0] - track2[0];
3440   Float_t vx = track1[1] - track2[1];
3441   Float_t vy = track1[2] - track2[2];
3442   Float_t vz = track1[3] - track2[3];
3443   
3444   qout = (px*vx + py*vy)/pt;
3445   qside = (px*vy - py*vx)/pt;
3446   qlong = (p0*vz - pz*v0)/mt;
3447 }
3448 //________________________________________________________________________
3449 void AliFourPion::SetWeightArrays(Bool_t legoCase, TH3F *histos[AliFourPion::fKbinsT][AliFourPion::fCentBins]){
3450
3451   if(legoCase){
3452     cout<<"LEGO call to SetWeightArrays"<<endl;
3453     
3454     for(Int_t tKbin=0; tKbin<fKbinsT; tKbin++){
3455       for(Int_t mb=0; mb<fCentBins; mb++){
3456         fNormWeight[tKbin][mb] = (TH3F*)histos[tKbin][mb]->Clone();
3457         fNormWeight[tKbin][mb]->SetDirectory(0);
3458       }
3459     }
3460     
3461   }else{
3462     
3463     TFile *wFile = new TFile("WeightFile.root","READ");
3464     if(!wFile->IsOpen()) {cout<<"No Weight File!!!!!!!!!!"<<endl; return;}
3465     else cout<<"Good Weight File Found!"<<endl;
3466     
3467     for(Int_t tKbin=0; tKbin<fKbinsT; tKbin++){
3468       for(Int_t mb=0; mb<fCentBins; mb++){
3469                     
3470         TString *name = new TString("Weight_Kt_");
3471         *name += tKbin;
3472         name->Append("_Ky_0");
3473         name->Append("_M_");
3474         *name += mb;
3475         name->Append("_ED_0");
3476         
3477         
3478         fNormWeight[tKbin][mb] = (TH3F*)wFile->Get(name->Data());
3479         fNormWeight[tKbin][mb]->SetDirectory(0);
3480         
3481         
3482       }//mb
3483     }//kt
3484     
3485     wFile->Close();
3486   }
3487   
3488   cout<<"Done reading weight file"<<endl;
3489   
3490 }
3491 //________________________________________________________________________
3492 void AliFourPion::GetWeight(Float_t track1[], Float_t track2[], Float_t& wgt, Float_t& wgtErr){
3493   
3494   Float_t kt=sqrt( pow(track1[1]+track2[1],2) + pow(track1[2]+track2[2],2))/2.;
3495   //
3496   Float_t qOut=0,qSide=0,qLong=0;
3497   GetQosl(track1, track2, qOut, qSide, qLong);
3498   qOut = fabs(qOut);
3499   qSide = fabs(qSide);
3500   qLong = fabs(qLong);
3501   Float_t wd=0, xd=0, yd=0, zd=0;
3502   //Float_t qinvtemp=GetQinv(0,track1, track2);
3503   //
3504   
3505   if(kt < fKmeanT[0]) {fKtIndexL=0; fKtIndexH=1;}
3506   else if(kt >= fKmeanT[fKbinsT-1]) {fKtIndexL=fKbinsT-2; fKtIndexH=fKbinsT-1;}
3507   else {
3508     for(Int_t i=0; i<fKbinsT-1; i++){
3509       if((kt >= fKmeanT[i]) && (kt < fKmeanT[i+1])) {fKtIndexL=i; fKtIndexH=i+1; break;}
3510     }
3511   }
3512   wd = (kt-fKmeanT[fKtIndexL])/(fKmeanT[fKtIndexH]-fKmeanT[fKtIndexL]);
3513   if(fMaxPt<=0.251) {fKtIndexL=0; fKtIndexH=0; wd=0;}
3514   if(fMinPt>0.249 && fKtIndexL==0) {fKtIndexL=1; wd=0;}
3515   //
3516   if(qOut < fQmean[0]) {fQoIndexL=0; fQoIndexH=0; xd=0;}
3517   else if(qOut >= fQmean[kQbinsWeights-1]) {fQoIndexL=kQbinsWeights-1; fQoIndexH=kQbinsWeights-1; xd=1;}
3518   else {
3519     for(Int_t i=0; i<kQbinsWeights-1; i++){
3520       if((qOut >= fQmean[i]) && (qOut < fQmean[i+1])) {fQoIndexL=i; fQoIndexH=i+1; break;}
3521       }
3522     xd = (qOut-fQmean[fQoIndexL])/(fQmean[fQoIndexH]-fQmean[fQoIndexL]);
3523   }
3524   //
3525   if(qSide < fQmean[0]) {fQsIndexL=0; fQsIndexH=0; yd=0;}
3526   else if(qSide >= fQmean[kQbinsWeights-1]) {fQsIndexL=kQbinsWeights-1; fQsIndexH=kQbinsWeights-1; yd=1;}
3527   else {
3528     for(Int_t i=0; i<kQbinsWeights-1; i++){
3529       if((qSide >= fQmean[i]) && (qSide < fQmean[i+1])) {fQsIndexL=i; fQsIndexH=i+1; break;}
3530     }
3531     yd = (qSide-fQmean[fQsIndexL])/(fQmean[fQsIndexH]-fQmean[fQsIndexL]);
3532   }
3533   //
3534   if(qLong < fQmean[0]) {fQlIndexL=0; fQlIndexH=0; zd=0;}
3535   else if(qLong >= fQmean[kQbinsWeights-1]) {fQlIndexL=kQbinsWeights-1; fQlIndexH=kQbinsWeights-1; zd=1;}
3536   else {
3537     for(Int_t i=0; i<kQbinsWeights-1; i++){
3538       if((qLong >= fQmean[i]) && (qLong < fQmean[i+1])) {fQlIndexL=i; fQlIndexH=i+1; break;}
3539     }
3540     zd = (qLong-fQmean[fQlIndexL])/(fQmean[fQlIndexH]-fQmean[fQlIndexL]);
3541   }
3542   //
3543   if(fLinearInterpolation){// Linear Interpolation of osl
3544     // w interpolation (kt)
3545     Float_t c000 = fNormWeight[fKtIndexL][fMbin]->GetBinContent(fQoIndexL+1, fQsIndexL+1, fQlIndexL+1)*(1-wd) + fNormWeight[fKtIndexH][fMbin]->GetBinContent(fQoIndexL+1, fQsIndexL+1, fQlIndexL+1)*wd;
3546     Float_t c100 = fNormWeight[fKtIndexL][fMbin]->GetBinContent(fQoIndexH+1, fQsIndexL+1, fQlIndexL+1)*(1-wd) + fNormWeight[fKtIndexH][fMbin]->GetBinContent(fQoIndexH+1, fQsIndexL+1, fQlIndexL+1)*wd;
3547     Float_t c010 = fNormWeight[fKtIndexL][fMbin]->GetBinContent(fQoIndexL+1, fQsIndexH+1, fQlIndexL+1)*(1-wd) + fNormWeight[fKtIndexH][fMbin]->GetBinContent(fQoIndexL+1, fQsIndexH+1, fQlIndexL+1)*wd;
3548     Float_t c001 = fNormWeight[fKtIndexL][fMbin]->GetBinContent(fQoIndexL+1, fQsIndexL+1, fQlIndexH+1)*(1-wd) + fNormWeight[fKtIndexH][fMbin]->GetBinContent(fQoIndexL+1, fQsIndexL+1, fQlIndexH+1)*wd;
3549     Float_t c110 = fNormWeight[fKtIndexL][fMbin]->GetBinContent(fQoIndexH+1, fQsIndexH+1, fQlIndexL+1)*(1-wd) + fNormWeight[fKtIndexH][fMbin]->GetBinContent(fQoIndexH+1, fQsIndexH+1, fQlIndexL+1)*wd;
3550     Float_t c101 = fNormWeight[fKtIndexL][fMbin]->GetBinContent(fQoIndexH+1, fQsIndexL+1, fQlIndexH+1)*(1-wd) + fNormWeight[fKtIndexH][fMbin]->GetBinContent(fQoIndexH+1, fQsIndexL+1, fQlIndexH+1)*wd;
3551     Float_t c011 = fNormWeight[fKtIndexL][fMbin]->GetBinContent(fQoIndexL+1, fQsIndexH+1, fQlIndexH+1)*(1-wd) + fNormWeight[fKtIndexH][fMbin]->GetBinContent(fQoIndexL+1, fQsIndexH+1, fQlIndexH+1)*wd;
3552     Float_t c111 = fNormWeight[fKtIndexL][fMbin]->GetBinContent(fQoIndexH+1, fQsIndexH+1, fQlIndexH+1)*(1-wd) + fNormWeight[fKtIndexH][fMbin]->GetBinContent(fQoIndexH+1, fQsIndexH+1, fQlIndexH+1)*wd;
3553     // x interpolation (qOut)
3554     Float_t c00 = c000*(1-xd) + c100*xd;
3555     Float_t c10 = c010*(1-xd) + c110*xd;
3556     Float_t c01 = c001*(1-xd) + c101*xd;
3557     Float_t c11 = c011*(1-xd) + c111*xd;
3558     // y interpolation (qSide)
3559     Float_t c0 = c00*(1-yd) + c10*yd;
3560     Float_t c1 = c01*(1-yd) + c11*yd;
3561     // z interpolation (qLong)
3562     wgt = (c0*(1-zd) + c1*zd);
3563   }else{// cubic interpolation of osl
3564     
3565     for(Int_t x=0; x<4; x++){
3566       for(Int_t y=0; y<4; y++){
3567         for(Int_t z=0; z<4; z++){
3568           Int_t binO = fQoIndexL + x;
3569           Int_t binS = fQsIndexL + y;
3570           Int_t binL = fQlIndexL + z;
3571           if(binO<=0) binO = 1;
3572           if(binS<=0) binS = 1;
3573           if(binL<=0) binL = 1;
3574           if(binO>kQbinsWeights) binO = kQbinsWeights;
3575           if(binS>kQbinsWeights) binS = kQbinsWeights;
3576           if(binL>kQbinsWeights) binL = kQbinsWeights;
3577           farrP1[x][y][z] = fNormWeight[fKtIndexL][fMbin]->GetBinContent(binO,binS,binL);
3578           farrP2[x][y][z] = fNormWeight[fKtIndexH][fMbin]->GetBinContent(binO,binS,binL);
3579         }
3580       }
3581     }
3582     Float_t coord[3]={xd, yd, zd}; 
3583     Float_t c0 = nCubicInterpolate(3, (Float_t*) farrP1, coord);
3584     Float_t c1 = nCubicInterpolate(3, (Float_t*) farrP2, coord);
3585     // kT interpolation
3586     wgt = c0*(1-wd) + c1*wd;
3587   }
3588   ////
3589   
3590   // simplified stat error 
3591   Float_t avgErr = fNormWeight[fKtIndexL][fMbin]->GetBinError(fQoIndexH+1,fQsIndexH+1,fQlIndexH+1);
3592   avgErr += fNormWeight[fKtIndexH][fMbin]->GetBinError(fQoIndexL+1,fQsIndexL+1,fQlIndexL+1);
3593   avgErr /= 2.;
3594   //
3595   wgtErr = avgErr;
3596   
3597  
3598 }
3599 //________________________________________________________________________
3600 Float_t AliFourPion::MCWeight(Int_t c[2], Float_t R, Float_t fcSq, Float_t qinv, Float_t k12){
3601   
3602   Float_t radius = R/0.19733;// convert to GeV (starts at 5 fm, was 3 fm)
3603   Float_t r12=radius*(1-k12/2.0);
3604   SetFSIindex(R);
3605   Float_t coulCorr12 = FSICorrelation(c[0], c[1], qinv);
3606   if(c[0]==c[1]){
3607     Float_t arg=qinv*r12;
3608     Float_t EW = 1 + kappa3/(6.*pow(2.,1.5))*(8.*pow(arg,3) - 12.*arg);
3609     EW += kappa4/(24.*pow(2.,2))*(16.*pow(arg,4) -48.*pow(arg,2) + 12);
3610     return ((1-fcSq) + fcSq*(1 + exp(-pow(qinv*r12,2))*pow(EW,2))*coulCorr12);
3611   }else {
3612     return ((1-fcSq) + fcSq*coulCorr12);
3613   }
3614     
3615 }
3616 //________________________________________________________________________
3617 Float_t AliFourPion::MCWeightOSL(Int_t charge1, Int_t charge2, Int_t r, Int_t dampIndex, Float_t qinv, Float_t qo, Float_t qs, Float_t ql){
3618   
3619   Float_t radiusOut = Float_t(r)/0.19733;// convert to GeV (starts at 5 fm, was 3 fm)
3620   Float_t radiusSide = radiusOut;
3621   Float_t radiusLong = radiusOut;
3622   Float_t myDamp = fDampStart + (fDampStep)*dampIndex;
3623   Float_t coulCorr12 = FSICorrelation(charge1, charge2, qinv);
3624   if(charge1==charge2){
3625     return ((1-myDamp) + myDamp*(1 + exp(-pow(qo*radiusOut,2)) * exp(-pow(qs*radiusSide,2)) * exp(-pow(ql*radiusLong,2)))*coulCorr12);
3626   }else {
3627     return ((1-myDamp) + myDamp*coulCorr12);
3628   }
3629     
3630 }
3631
3632 //________________________________________________________________________
3633 Float_t AliFourPion::MCWeight3(Int_t term, Float_t R, Float_t fcSq, Int_t c[3], Float_t qinv[3], Float_t kT[3]){
3634   // FSI + QS correlations
3635   if(term==5) return 1.0;
3636   
3637   Float_t radius=R/0.19733;
3638   Float_t r12=radius*(1-kT[0]/2.0);
3639   Float_t r13=radius*(1-kT[1]/2.0);
3640   Float_t r23=radius*(1-kT[2]/2.0);
3641  
3642   Float_t fc = sqrt(fcSq);
3643   
3644   SetFSIindex(R);
3645   Float_t Kfactor12 = FSICorrelation(c[0],c[1], qinv[0]);// K2
3646   Float_t Kfactor13 = FSICorrelation(c[0],c[2], qinv[1]);// K2
3647   Float_t Kfactor23 = FSICorrelation(c[1],c[2], qinv[2]);// K2
3648   
3649   if(c[0]==c[1] && c[0]==c[2]){// all three of the same charge
3650     Float_t arg12=qinv[0]*r12;
3651     Float_t arg13=qinv[1]*r13;
3652     Float_t arg23=qinv[2]*r23;
3653     Float_t EW12 = 1 + kappa3/(6.*pow(2.,1.5))*(8.*pow(arg12,3) - 12.*arg12);
3654     EW12 += kappa4/(24.*pow(2.,2))*(16.*pow(arg12,4) -48.*pow(arg12,2) + 12);
3655     Float_t EW13 = 1 + kappa3/(6.*pow(2.,1.5))*(8.*pow(arg13,3) - 12.*arg13);
3656     EW13 += kappa4/(24.*pow(2.,2))*(16.*pow(arg13,4) -48.*pow(arg13,2) + 12);
3657     Float_t EW23 = 1 + kappa3/(6.*pow(2.,1.5))*(8.*pow(arg23,3) - 12.*arg23);
3658     EW23 += kappa4/(24.*pow(2.,2))*(16.*pow(arg23,4) -48.*pow(arg23,2) + 12);
3659     if(term==1){
3660       Float_t C3QS = 1 + exp(-pow(qinv[0]*r12,2))*pow(EW12,2) + exp(-pow(qinv[1]*r13,2))*pow(EW13,2) + exp(-pow(qinv[2]*r23,2))*pow(EW23,2);
3661       C3QS += 2*exp(-(pow(r12,2)*pow(qinv[0],2) + pow(r13,2)*pow(qinv[1],2) + pow(r23,2)*pow(qinv[2],2))/2.)*EW12*EW13*EW23;
3662       Float_t C3 = pow(1-fc,3) + 3*fc*pow(1-fc,2);
3663       C3 += pow(fc,2)*(1-fc)*(1+exp(-pow(qinv[0]*r12,2))*pow(EW12,2))*Kfactor12;
3664       C3 += pow(fc,2)*(1-fc)*(1+exp(-pow(qinv[1]*r13,2))*pow(EW13,2))*Kfactor13;
3665       C3 += pow(fc,2)*(1-fc)*(1+exp(-pow(qinv[2]*r23,2))*pow(EW23,2))*Kfactor23;
3666       C3 += pow(fc,3)*C3QS*Kfactor12*Kfactor13*Kfactor23;
3667       return C3;
3668     }else if(term==2){
3669       return ((1-fcSq) + fcSq*(1 + exp(-pow(qinv[0]*r12,2))*pow(EW12,2))*Kfactor12);
3670     }else if(term==3){
3671       return ((1-fcSq) + fcSq*(1 + exp(-pow(qinv[1]*r13,2))*pow(EW13,2))*Kfactor13);
3672     }else if(term==4){
3673       return ((1-fcSq) + fcSq*(1 + exp(-pow(qinv[2]*r23,2))*pow(EW23,2))*Kfactor23);
3674     }else return 1.0;
3675     
3676   }else{// mixed charge case
3677     Float_t arg=qinv[0]*r12;
3678     Float_t KfactorSC = Kfactor12;
3679     Float_t KfactorMC1 = Kfactor13;
3680     Float_t KfactorMC2 = Kfactor23;
3681     if(c[0]==c[2]) {arg=qinv[1]*r13; KfactorSC = Kfactor13; KfactorMC1 = Kfactor12; KfactorMC2 = Kfactor23;} 
3682     if(c[1]==c[2]) {arg=qinv[2]*r23; KfactorSC = Kfactor23; KfactorMC1 = Kfactor12; KfactorMC2 = Kfactor13;}
3683     Float_t EW = 1 + kappa3/(6.*pow(2.,1.5))*(8.*pow(arg,3) - 12.*arg);
3684     EW += kappa4/(24.*pow(2.,2))*(16.*pow(arg,4) -48.*pow(arg,2) + 12);
3685     if(term==1){
3686       Float_t C3QS = 1 + exp(-pow(arg,2))*pow(EW,2);
3687       Float_t C3 = pow(1-fc,3) + 3*fc*pow(1-fc,2);
3688       C3 += pow(fc,2)*(1-fc)*(1+exp(-pow(arg,2))*pow(EW,2))*KfactorSC;
3689       C3 += pow(fc,2)*(1-fc)*KfactorMC1;
3690       C3 += pow(fc,2)*(1-fc)*KfactorMC2;
3691       C3 += pow(fc,3)*C3QS*KfactorSC*KfactorMC1*KfactorMC2;
3692       return C3;
3693     }else if(term==2){
3694       if( (c[0]+c[1]+c[2])==1) return ((1-fcSq) + fcSq*(1 + exp(-pow(arg,2))*pow(EW,2))*KfactorSC);
3695       else return ((1-fcSq) + fcSq*KfactorMC1);// doesn't matter MC1 or MC2
3696     }else if(term==3){
3697       return ((1-fcSq) + fcSq*KfactorMC1);// doesn't matter MC1 or MC2
3698     }else if(term==4){
3699       if( (c[0]+c[1]+c[2])==2) return ((1-fcSq) + fcSq*(1 + exp(-pow(arg,2))*pow(EW,2))*KfactorSC);
3700       else return ((1-fcSq) + fcSq*KfactorMC1);// doesn't matter MC1 or MC2
3701     }else return 1.0;
3702   }
3703   
3704 }
3705 //________________________________________________________________________
3706 Float_t AliFourPion::MCWeightFSI3(Int_t term, Float_t R, Float_t fcSq, Int_t c[3], Float_t qinv[3]){
3707   // FSI only (no QS correlations)
3708   if(term==5) return 1.0;
3709   
3710   Float_t fc = sqrt(fcSq);
3711   SetFSIindex(R);
3712   Float_t Kfactor12 = FSICorrelation(c[0],c[1], qinv[0]);// K2
3713   Float_t Kfactor13 = FSICorrelation(c[0],c[2], qinv[1]);// K2
3714   Float_t Kfactor23 = FSICorrelation(c[1],c[2], qinv[2]);// K2
3715   
3716   if(c[0]==c[1] && c[0]==c[2]){// all three of the same charge
3717     if(term==1){
3718       Float_t C3 = pow(1-fc,3) + 3*fc*pow(1-fc,2);
3719       C3 += pow(fc,2)*(1-fc)*Kfactor12;
3720       C3 += pow(fc,2)*(1-fc)*Kfactor13;
3721       C3 += pow(fc,2)*(1-fc)*Kfactor23;
3722       C3 += pow(fc,3)*Kfactor12*Kfactor13*Kfactor23;
3723       return C3;
3724     }else if(term==2){
3725       return ((1-fcSq) + fcSq*Kfactor12);
3726     }else if(term==3){
3727       return ((1-fcSq) + fcSq*Kfactor13);
3728     }else if(term==4){
3729       return ((1-fcSq) + fcSq*Kfactor23);
3730     }else return 1.0;
3731     
3732   }else{// mixed charge case
3733     Float_t KfactorSC = Kfactor12;
3734     Float_t KfactorMC1 = Kfactor13;
3735     Float_t KfactorMC2 = Kfactor23;
3736     if(c[0]==c[2]) {KfactorSC = Kfactor13; KfactorMC1 = Kfactor12; KfactorMC2 = Kfactor23;} 
3737     if(c[1]==c[2]) {KfactorSC = Kfactor23; KfactorMC1 = Kfactor12; KfactorMC2 = Kfactor13;}
3738     if(term==1){
3739       Float_t C3 = pow(1-fc,3) + 3*fc*pow(1-fc,2);
3740       C3 += pow(fc,2)*(1-fc)*KfactorSC;
3741       C3 += pow(fc,2)*(1-fc)*KfactorMC1;
3742       C3 += pow(fc,2)*(1-fc)*KfactorMC2;
3743       C3 += pow(fc,3)*KfactorSC*KfactorMC1*KfactorMC2;
3744       return C3;
3745     }else if(term==2){
3746       if( (c[0]+c[1]+c[2])==1) return ((1-fcSq) + fcSq*KfactorSC);
3747       else return ((1-fcSq) + fcSq*KfactorMC1);// doesn't matter MC1 or MC2
3748     }else if(term==3){
3749       return ((1-fcSq) + fcSq*KfactorMC1);// doesn't matter MC1 or MC2
3750     }else if(term==4){
3751       if( (c[0]+c[1]+c[2])==2) return ((1-fcSq) + fcSq*KfactorSC);
3752       else return ((1-fcSq) + fcSq*KfactorMC1);// doesn't matter MC1 or MC2
3753     }else return 1.0;
3754   }
3755   
3756 }
3757 //________________________________________________________________________
3758 Float_t AliFourPion::MCWeight4(Int_t term, Float_t R, Float_t fcSq, Int_t c[4], Float_t qinv[6], Float_t kT[6]){
3759   if(term==13) return 1.0;
3760
3761   // Charge ordering:
3762   // ----, ---+, --++, -+++, ++++
3763   //
3764   // term ordering:
3765   // Term 1: 1-2-3-4  (all same-event)
3766   // Term 2: 1-2-3 4 (particle 4 from different event)
3767   // Term 3: 1-2-4 3 (particle 3 from different event)
3768   // Term 4: 1-3-4 2 (particle 2 from different event)
3769   // Term 5: 2-3-4 1 (particle 1 from different event)
3770   // Term 6: 1-2 3 4 (particle 1 and 2 from same event)
3771   // Term 7: 1-3 2 4
3772   // Term 8: 1-4 2 3
3773   // Term 9: 2-3 1 4
3774   // Term 10: 2-4 1 3
3775   // Term 11: 3-4 1 2
3776   // Term 12: 1 2 3 4 (all from different events)
3777
3778   Float_t radius = R/0.19733;
3779   Float_t r[6]={0};
3780   r[0]=radius*(1-kT[0]/2.0);
3781   r[1]=radius*(1-kT[1]/2.0);
3782   r[2]=radius*(1-kT[2]/2.0);
3783   r[3]=radius*(1-kT[3]/2.0);
3784   r[4]=radius*(1-kT[4]/2.0);
3785   r[5]=radius*(1-kT[5]/2.0);
3786     
3787   Int_t ChargeSum=c[0]+c[1]+c[2]+c[3];
3788  
3789   Float_t fc = sqrt(fcSq);
3790   SetFSIindex(R);
3791   Float_t Kfactor12 = FSICorrelation(c[0],c[1], qinv[0]);// K2
3792   Float_t Kfactor13 = FSICorrelation(c[0],c[2], qinv[1]);// K2
3793   Float_t Kfactor14 = FSICorrelation(c[0],c[3], qinv[2]);// K2
3794   Float_t Kfactor23 = FSICorrelation(c[1],c[2], qinv[3]);// K2
3795   Float_t Kfactor24 = FSICorrelation(c[1],c[3], qinv[4]);// K2
3796   Float_t Kfactor34 = FSICorrelation(c[2],c[3], qinv[5]);// K2
3797   Float_t arg12=qinv[0]*r[0];
3798   Float_t arg13=qinv[1]*r[1];
3799   Float_t arg14=qinv[2]*r[2];
3800   Float_t arg23=qinv[3]*r[3];
3801   Float_t arg24=qinv[4]*r[4];
3802   Float_t arg34=qinv[5]*r[5];
3803   // Exchange Amplitudes
3804   Float_t EA12 = exp(-pow(arg12,2)/2.)*(1 + kappa3/(6.*pow(2.,1.5))*(8.*pow(arg12,3) - 12.*arg12) + kappa4/(24.*pow(2.,2))*(16.*pow(arg12,4) -48.*pow(arg12,2) + 12));
3805   Float_t EA13 = exp(-pow(arg13,2)/2.)*(1 + kappa3/(6.*pow(2.,1.5))*(8.*pow(arg13,3) - 12.*arg13) + kappa4/(24.*pow(2.,2))*(16.*pow(arg13,4) -48.*pow(arg13,2) + 12));
3806   Float_t EA14 = exp(-pow(arg14,2)/2.)*(1 + kappa3/(6.*pow(2.,1.5))*(8.*pow(arg14,3) - 12.*arg14) + kappa4/(24.*pow(2.,2))*(16.*pow(arg14,4) -48.*pow(arg14,2) + 12));
3807   Float_t EA23 = exp(-pow(arg23,2)/2.)*(1 + kappa3/(6.*pow(2.,1.5))*(8.*pow(arg23,3) - 12.*arg23) + kappa4/(24.*pow(2.,2))*(16.*pow(arg23,4) -48.*pow(arg23,2) + 12));
3808   Float_t EA24 = exp(-pow(arg24,2)/2.)*(1 + kappa3/(6.*pow(2.,1.5))*(8.*pow(arg24,3) - 12.*arg24) + kappa4/(24.*pow(2.,2))*(16.*pow(arg24,4) -48.*pow(arg24,2) + 12));
3809   Float_t EA34 = exp(-pow(arg34,2)/2.)*(1 + kappa3/(6.*pow(2.,1.5))*(8.*pow(arg34,3) - 12.*arg34) + kappa4/(24.*pow(2.,2))*(16.*pow(arg34,4) -48.*pow(arg34,2) + 12));
3810   
3811   if(c[0]==c[1] && c[0]==c[2] && c[0]==c[3]){// ---- and ++++ configuration
3812     
3813     if(term==1){
3814       Float_t C4QS = 1 + pow(EA12,2) + pow(EA13,2) + pow(EA14,2) + pow(EA23,2) + pow(EA24,2) + pow(EA34,2);// baseline + single pairs
3815       C4QS += pow(EA12,2) * pow(EA34,2);// 2-pairs
3816       C4QS += pow(EA13,2) * pow(EA24,2);// 2-pairs
3817       C4QS += pow(EA14,2) * pow(EA23,2);// 2-pairs
3818       C4QS += 2*EA12*EA13*EA23 + 2*EA12*EA14*EA24 + 2*EA13*EA14*EA34 + 2*EA23*EA24*EA34;// 3-particle exhange
3819       C4QS += 3*EA12*EA23*EA34*EA14 + 3*EA12*EA13*EA34*EA24;// 4-particle exchange
3820       Float_t C4 = pow(1-fc,4) + 4*fc*pow(1-fc,3);
3821       C4 += pow(fc,2)*pow(1-fc,2)*( (1 + pow(EA12,2))*Kfactor12 + (1 + pow(EA13,2))*Kfactor13 + (1 + pow(EA14,2))*Kfactor14 );
3822       C4 += pow(fc,2)*pow(1-fc,2)*( (1 + pow(EA23,2))*Kfactor23 + (1 + pow(EA24,2))*Kfactor24 + (1 + pow(EA34,2))*Kfactor34);
3823       C4 += pow(fc,3)*(1-fc)*(1 + pow(EA12,2) + pow(EA13,2) + pow(EA23,2) + 2*EA12*EA13*EA23) * Kfactor12*Kfactor13*Kfactor23;
3824       C4 += pow(fc,3)*(1-fc)*(1 + pow(EA12,2) + pow(EA14,2) + pow(EA24,2) + 2*EA12*EA14*EA24) * Kfactor12*Kfactor14*Kfactor24;
3825       C4 += pow(fc,3)*(1-fc)*(1 + pow(EA13,2) + pow(EA14,2) + pow(EA34,2) + 2*EA13*EA14*EA34) * Kfactor13*Kfactor14*Kfactor34;
3826       C4 += pow(fc,3)*(1-fc)*(1 + pow(EA23,2) + pow(EA24,2) + pow(EA34,2) + 2*EA23*EA24*EA34) * Kfactor23*Kfactor24*Kfactor34;
3827       C4 += pow(fc,4)*C4QS*Kfactor12*Kfactor13*Kfactor14*Kfactor23*Kfactor24*Kfactor34;
3828       return C4;
3829     }else if(term<=5){
3830       Float_t EA1=0, EA2=0, EA3=0, Kpair1=0, Kpair2=0, Kpair3=0;
3831       if(term==2)      {EA1=EA12; EA2=EA13; EA3=EA23; Kpair1=Kfactor12; Kpair2=Kfactor13; Kpair3=Kfactor23;}
3832       else if(term==3) {EA1=EA12; EA2=EA14; EA3=EA24; Kpair1=Kfactor12; Kpair2=Kfactor14; Kpair3=Kfactor24;}
3833       else if(term==4) {EA1=EA13; EA2=EA14; EA3=EA34; Kpair1=Kfactor13; Kpair2=Kfactor14; Kpair3=Kfactor34;}
3834       else             {EA1=EA23; EA2=EA24; EA3=EA34; Kpair1=Kfactor23; Kpair2=Kfactor24; Kpair3=Kfactor34;}
3835       Float_t C3QS = 1 + pow(EA1,2) + pow(EA2,2) + pow(EA3,2);
3836       C3QS += 2*EA1*EA2*EA3;
3837       Float_t C3 = pow(1-fc,3) + 3*fc*pow(1-fc,2);
3838       C3 += pow(fc,2)*(1-fc)*( (1+pow(EA1,2))*Kpair1 + (1+pow(EA2,2))*Kpair2 + (1+pow(EA3,2))*Kpair3 );
3839       C3 += pow(fc,3)*C3QS*Kpair1*Kpair2*Kpair3;
3840       return C3;
3841     }else if(term<=11){
3842       if(term==6)       return ((1-fcSq) + fcSq*(1 + pow(EA12,2))*Kfactor12);
3843       else if(term==7)  return ((1-fcSq) + fcSq*(1 + pow(EA13,2))*Kfactor13);
3844       else if(term==8)  return ((1-fcSq) + fcSq*(1 + pow(EA14,2))*Kfactor14);
3845       else if(term==9)  return ((1-fcSq) + fcSq*(1 + pow(EA23,2))*Kfactor23);
3846       else if(term==10) return ((1-fcSq) + fcSq*(1 + pow(EA24,2))*Kfactor24);
3847       else              return ((1-fcSq) + fcSq*(1 + pow(EA34,2))*Kfactor34);
3848     }else if(term==12){
3849       Float_t C22 = (1-fcSq) + fcSq*(1 + pow(EA12,2))*Kfactor12;
3850       C22 *= (1-fcSq) + fcSq*(1 + pow(EA34,2))*Kfactor34;
3851       return C22;
3852     }else return 1.0;
3853     
3854   }else{// mixed charge case
3855     if( ChargeSum==1 || ChargeSum==3){// ---+ and -+++ configuration
3856       Float_t EA1=0, EA2=0, EA3=0, Kpair1=0, Kpair2=0, Kpair3=0, Kpair4=0, Kpair5=0, Kpair6=0;
3857       Int_t c_OddOneOut = 1;
3858       if(ChargeSum==3) c_OddOneOut = 0;
3859       //
3860       if(c[0]==c_OddOneOut) {EA1=EA23; EA2=EA24; EA3=EA34; Kpair1=Kfactor23; Kpair2=Kfactor24; Kpair3=Kfactor34;   Kpair4=Kfactor12; Kpair5=Kfactor13; Kpair6=Kfactor14;}
3861       else if(c[1]==c_OddOneOut) {EA1=EA13; EA2=EA14; EA3=EA34; Kpair1=Kfactor13; Kpair2=Kfactor14; Kpair3=Kfactor34;   Kpair4=Kfactor12; Kpair5=Kfactor23; Kpair6=Kfactor24;}
3862       else if(c[2]==c_OddOneOut) {EA1=EA12; EA2=EA14; EA3=EA24; Kpair1=Kfactor12; Kpair2=Kfactor14; Kpair3=Kfactor24;   Kpair4=Kfactor13; Kpair5=Kfactor23; Kpair6=Kfactor34;}
3863       else {EA1=EA12; EA2=EA13; EA3=EA23; Kpair1=Kfactor12; Kpair2=Kfactor13; Kpair3=Kfactor23;   Kpair4=Kfactor14; Kpair5=Kfactor24; Kpair6=Kfactor34;}
3864       
3865       if(term==1){
3866         Float_t C3QS = 1 + pow(EA1,2) + pow(EA2,2) + pow(EA3,2) + 2*EA1*EA2*EA3;
3867         Float_t C4 = pow(1-fc,4) + 4*fc*pow(1-fc,3);
3868         C4 += pow(fc,2)*pow(1-fc,2)*( (1 + pow(EA1,2))*Kpair1 + (1 + pow(EA2,2))*Kpair2 + (1 + pow(EA3,2))*Kpair3 );
3869         C4 += pow(fc,2)*pow(1-fc,2)*( Kpair4 + Kpair5 + Kpair6 );
3870         C4 += pow(fc,3)*(1-fc)*(1 + pow(EA1,2) + pow(EA2,2) +  pow(EA3,2) + 2*EA1*EA2*EA3) * Kpair1*Kpair2*Kpair3;
3871         C4 += pow(fc,3)*(1-fc)*( (1 + pow(EA1,2))*Kpair1*Kpair4*Kpair5 + (1+pow(EA2,2))*Kpair2*Kpair4*Kpair6 + (1+pow(EA3,2))*Kpair3*Kpair5*Kpair6);// doesn't matter which MC K's
3872         C4 += pow(fc,4)*C3QS*Kfactor12*Kfactor13*Kfactor14*Kfactor23*Kfactor24*Kfactor34;
3873         return C4;
3874       }else if( (term==2 && ChargeSum==1) || (term==5 && ChargeSum==3)){
3875         Float_t C3QS = 1 + pow(EA1,2) + pow(EA2,2) + pow(EA3,2) + 2*EA1*EA2*EA3;
3876         Float_t C3 = pow(1-fc,3) + 3*fc*pow(1-fc,2);
3877         C3 += pow(fc,2)*(1-fc)*(1+pow(EA1,2))*Kpair1;
3878         C3 += pow(fc,2)*(1-fc)*(1+pow(EA2,2))*Kpair2;
3879         C3 += pow(fc,2)*(1-fc)*(1+pow(EA3,2))*Kpair3;
3880         C3 += pow(fc,3)*C3QS*Kpair1*Kpair2*Kpair3;
3881         return C3;
3882       }else if(term<=5){// one SC pair, two MC pairs
3883         Float_t C3 = pow(1-fc,3) + 3*fc*pow(1-fc,2);
3884         C3 += pow(fc,2)*(1-fc)*(1+pow(EA1,2))*Kpair1;// any SC pair will do
3885         C3 += pow(fc,2)*(1-fc)*Kpair4;// any MC pair will do
3886         C3 += pow(fc,2)*(1-fc)*Kpair5;// any MC pair will do
3887         C3 += pow(fc,3)*(1+pow(EA1,2))*Kpair1*Kpair4*Kpair5;
3888         return C3;
3889       }else if(term==6 || term==7){
3890         if(ChargeSum==1) return ((1-fcSq) + fcSq*(1 + pow(EA1,2))*Kpair1);// any SC pair will do
3891         else return ((1-fcSq) + fcSq*Kpair4);// any MC pair will do
3892       }else if(term==8){
3893         return ((1-fcSq) + fcSq*Kpair4);// any MC pair will do
3894       }else if(term==9){
3895         return ((1-fcSq) + fcSq*(1 + pow(EA1,2))*Kpair1);// any SC pair will do
3896       }else if(term==10 || term==11){
3897         if(ChargeSum==3) return ((1-fcSq) + fcSq*(1 + pow(EA1,2))*Kpair1);// any SC pair will do
3898         else return ((1-fcSq) + fcSq*Kpair4);// any MC pair will do
3899       }else return 1.0;// for 12 and 13
3900     }else{// --++ configuration
3901       Float_t EA1=0, EA2=0, Kpair1=0, Kpair2=0, Kpair3=0, Kpair4=0, Kpair5=0, Kpair6=0;
3902       if(c[0]==c[1]) {EA1=EA12; EA2=EA34; Kpair1=Kfactor12; Kpair2=Kfactor34;    Kpair3=Kfactor13; Kpair4=Kfactor14; Kpair5=Kfactor23; Kpair6=Kfactor24;}
3903       else if(c[0]==c[2]) {EA1=EA13; EA2=EA24; Kpair1=Kfactor13; Kpair2=Kfactor24;    Kpair3=Kfactor12; Kpair4=Kfactor14; Kpair5=Kfactor23; Kpair6=Kfactor34;}
3904       else {EA1=EA14; EA2=EA23; Kpair1=Kfactor14; Kpair2=Kfactor23;    Kpair3=Kfactor12; Kpair4=Kfactor13; Kpair5=Kfactor24; Kpair6=Kfactor34;}
3905       //
3906       if(term==1){
3907         Float_t C2QS = 1 + pow(EA1,2)*pow(EA2,2);
3908         Float_t C4 = pow(1-fc,4) + 4*fc*pow(1-fc,3);
3909         C4 += pow(fc,2)*pow(1-fc,2)*( (1 + pow(EA1,2))*Kpair1 + (1 + pow(EA2,2))*Kpair2 );
3910         C4 += pow(fc,2)*pow(1-fc,2)*( Kpair3 + Kpair4 + Kpair5 + Kpair6 );
3911         C4 += pow(fc,3)*(1-fc)*( (1 + pow(EA1,2))*Kpair1*Kpair3*Kpair4 + (1 + pow(EA2,2))*Kpair2*Kpair3*Kpair4);
3912         C4 += pow(fc,3)*(1-fc)*( (1 + pow(EA1,2))*Kpair1*Kpair5*Kpair6 + (1 + pow(EA2,2))*Kpair2*Kpair5*Kpair6);// doesn't matter which two MC K's used
3913         C4 += pow(fc,4)*C2QS*Kfactor12*Kfactor13*Kfactor14*Kfactor23*Kfactor24*Kfactor34;
3914         return C4;
3915       }else if(term<=5){
3916         Float_t C3 = pow(1-fc,3) + 3*fc*pow(1-fc,2);
3917         C3 += pow(fc,2)*(1-fc)*(1+pow(EA1,2))*Kpair1;// any SC pair will do
3918         C3 += pow(fc,2)*(1-fc)*Kpair4;// any MC pair will do
3919         C3 += pow(fc,2)*(1-fc)*Kpair6;// any MC pair will do
3920         C3 += pow(fc,3)*(1+pow(EA1,2))*Kpair1*Kpair4*Kpair6;
3921         return C3;
3922       }else if(term==6 || term==11){
3923         return ((1-fcSq) + fcSq*(1 + pow(EA1,2))*Kpair1);// any SC pair will do
3924       }else if(term!=12 && term !=13){
3925         return ((1-fcSq) + fcSq*Kpair3);// any MC pair will do
3926       }else if(term==12){
3927         Float_t C22 = (1-fcSq) + fcSq*(1 + pow(EA1,2))*Kpair1;
3928         C22 *= (1-fcSq) + fcSq*(1 + pow(EA2,2))*Kpair2;
3929         return C22;
3930       }else return 1.0;
3931     }
3932   }
3933   
3934 }
3935 //________________________________________________________________________
3936 Float_t AliFourPion::MCWeightFSI4(Int_t term, Float_t R, Float_t fcSq, Int_t c[4], Float_t qinv[6]){
3937   if(term==13) return 1.0;
3938     
3939   Int_t ChargeSum=c[0]+c[1]+c[2]+c[3];
3940  
3941   Float_t fc = sqrt(fcSq);
3942   SetFSIindex(R);
3943   Float_t Kfactor12 = FSICorrelation(c[0],c[1], qinv[0]);// K2
3944   Float_t Kfactor13 = FSICorrelation(c[0],c[2], qinv[1]);// K2
3945   Float_t Kfactor14 = FSICorrelation(c[0],c[3], qinv[2]);// K2
3946   Float_t Kfactor23 = FSICorrelation(c[1],c[2], qinv[3]);// K2
3947   Float_t Kfactor24 = FSICorrelation(c[1],c[3], qinv[4]);// K2
3948   Float_t Kfactor34 = FSICorrelation(c[2],c[3], qinv[5]);// K2
3949   
3950   if(c[0]==c[1] && c[0]==c[2] && c[0]==c[3]){// ---- and ++++ configuration
3951     
3952     if(term==1){
3953       Float_t C4 = pow(1-fc,4) + 4*fc*pow(1-fc,3);
3954       C4 += pow(fc,2)*pow(1-fc,2)*( Kfactor12 + Kfactor13 + Kfactor14 );
3955       C4 += pow(fc,2)*pow(1-fc,2)*( Kfactor23 + Kfactor24 + Kfactor34 );
3956       C4 += pow(fc,3)*(1-fc) * Kfactor12*Kfactor13*Kfactor23;
3957       C4 += pow(fc,3)*(1-fc) * Kfactor12*Kfactor14*Kfactor24;
3958       C4 += pow(fc,3)*(1-fc) * Kfactor13*Kfactor14*Kfactor34;
3959       C4 += pow(fc,3)*(1-fc) * Kfactor23*Kfactor24*Kfactor34;
3960       C4 += pow(fc,4) * Kfactor12*Kfactor13*Kfactor14*Kfactor23*Kfactor24*Kfactor34;
3961       return C4;
3962     }else if(term<=5){
3963       Float_t Kpair1=0, Kpair2=0, Kpair3=0;
3964       if(term==2) {Kpair1=Kfactor12; Kpair2=Kfactor13; Kpair3=Kfactor23;}
3965       else if(term==3) {Kpair1=Kfactor12; Kpair2=Kfactor14; Kpair3=Kfactor24;}
3966       else if(term==4) {Kpair1=Kfactor13; Kpair2=Kfactor14; Kpair3=Kfactor34;}
3967       else {Kpair1=Kfactor23; Kpair2=Kfactor24; Kpair3=Kfactor34;}
3968       Float_t C3 = pow(1-fc,3) + 3*fc*pow(1-fc,2);
3969       C3 += pow(fc,2)*(1-fc)*( Kpair1 + Kpair2 + Kpair3 );
3970       C3 += pow(fc,3)*Kpair1*Kpair2*Kpair3;
3971       return C3;
3972     }else if(term<=11){
3973       if(term==6) return ((1-fcSq) + fcSq*Kfactor12);
3974       else if(term==7) return ((1-fcSq) + fcSq*Kfactor13);
3975       else if(term==8) return ((1-fcSq) + fcSq*Kfactor14);
3976       else if(term==9) return ((1-fcSq) + fcSq*Kfactor23);
3977       else if(term==10) return ((1-fcSq) + fcSq*Kfactor24);
3978       else return ((1-fcSq) + fcSq*Kfactor34);
3979     }else if(term==12){
3980       Float_t C22 = (1-fcSq) + fcSq*Kfactor12;
3981       C22 *= (1-fcSq) + fcSq*Kfactor34;
3982       return C22;
3983     }else return 1.0;
3984     
3985   }else{// mixed charge case
3986     if( ChargeSum==1 || ChargeSum==3){// ---+ and -+++ configuration
3987       Float_t Kpair1=0, Kpair2=0, Kpair3=0, Kpair4=0, Kpair5=0, Kpair6=0;
3988       Int_t c_OddOneOut = 1;
3989       if(ChargeSum==3) c_OddOneOut = 0;
3990       //
3991       if(c[0]==c_OddOneOut) {Kpair1=Kfactor23; Kpair2=Kfactor24; Kpair3=Kfactor34;   Kpair4=Kfactor12; Kpair5=Kfactor13; Kpair6=Kfactor14;}
3992       else if(c[1]==c_OddOneOut) {Kpair1=Kfactor13; Kpair2=Kfactor14; Kpair3=Kfactor34;   Kpair4=Kfactor12; Kpair5=Kfactor23; Kpair6=Kfactor24;}
3993       else if(c[2]==c_OddOneOut) {Kpair1=Kfactor12; Kpair2=Kfactor14; Kpair3=Kfactor24;   Kpair4=Kfactor13; Kpair5=Kfactor23; Kpair6=Kfactor34;}
3994       else {Kpair1=Kfactor12; Kpair2=Kfactor13; Kpair3=Kfactor23;   Kpair4=Kfactor14; Kpair5=Kfactor24; Kpair6=Kfactor34;}
3995       
3996       if(term==1){
3997         Float_t C4 = pow(1-fc,4) + 4*fc*pow(1-fc,3);
3998         C4 += pow(fc,2)*pow(1-fc,2)*( Kpair1 + Kpair2 + Kpair3 );
3999         C4 += pow(fc,2)*pow(1-fc,2)*( Kpair4 + Kpair5 + Kpair6 );
4000         C4 += pow(fc,3)*(1-fc)*Kpair1*Kpair2*Kpair3;
4001         C4 += pow(fc,3)*(1-fc)*(Kpair1*Kpair4*Kpair5 + Kpair2*Kpair4*Kpair6 + Kpair3*Kpair5*Kpair6);// doesn't matter which two MC K's used
4002         C4 += pow(fc,4)*Kfactor12*Kfactor13*Kfactor14*Kfactor23*Kfactor24*Kfactor34;
4003         return C4;
4004       }else if( (term==2 && ChargeSum==1) || (term==5 && ChargeSum==3)){
4005         Float_t C3 = pow(1-fc,3) + 3*fc*pow(1-fc,2);
4006         C3 += pow(fc,2)*(1-fc)*Kpair1;
4007         C3 += pow(fc,2)*(1-fc)*Kpair2;
4008         C3 += pow(fc,2)*(1-fc)*Kpair3;
4009         C3 += pow(fc,3)*Kpair1*Kpair2*Kpair3;
4010         return C3;
4011       }else if(term<=5){// one SC pair, two MC pairs
4012         Float_t C3 = pow(1-fc,3) + 3*fc*pow(1-fc,2);
4013         C3 += pow(fc,2)*(1-fc)*Kpair1;// any SC pair will do
4014         C3 += pow(fc,2)*(1-fc)*Kpair4;// any MC pair will do
4015         C3 += pow(fc,2)*(1-fc)*Kpair5;// any MC pair will do
4016         C3 += pow(fc,3)*Kpair1*Kpair4*Kpair5;
4017         return C3;
4018       }else if(term==6 || term==7){
4019         if(ChargeSum==1) return ((1-fcSq) + fcSq*Kpair1);// any SC pair will do
4020         else return ((1-fcSq) + fcSq*Kpair4);// any MC pair will do
4021       }else if(term==8){
4022         return ((1-fcSq) + fcSq*Kpair4);// any MC pair will do
4023       }else if(term==9){
4024         return ((1-fcSq) + fcSq*Kpair1);// any SC pair will do
4025       }else if(term==10 || term==11){
4026         if(ChargeSum==3) return ((1-fcSq) + fcSq*Kpair1);// any SC pair will do
4027         else return ((1-fcSq) + fcSq*Kpair4);// any MC pair will do
4028       }else return 1.0;// 12 and 13
4029     }else{// --++ configuration
4030       Float_t Kpair1=0, Kpair2=0, Kpair3=0, Kpair4=0, Kpair5=0, Kpair6=0;
4031       if(c[0]==c[1]) {Kpair1=Kfactor12; Kpair2=Kfactor34;    Kpair3=Kfactor13; Kpair4=Kfactor14; Kpair5=Kfactor23; Kpair6=Kfactor24;}
4032       else if(c[0]==c[2]) {Kpair1=Kfactor13; Kpair2=Kfactor24;    Kpair3=Kfactor12; Kpair4=Kfactor14; Kpair5=Kfactor23; Kpair6=Kfactor34;}
4033       else {Kpair1=Kfactor14; Kpair2=Kfactor23;    Kpair3=Kfactor12; Kpair4=Kfactor13; Kpair5=Kfactor24; Kpair6=Kfactor34;}
4034       //
4035       if(term==1){
4036         Float_t C4 = pow(1-fc,4) + 4*fc*pow(1-fc,3);
4037         C4 += pow(fc,2)*pow(1-fc,2)*( Kpair1 + Kpair2 + Kpair3 + Kpair4 + Kpair5 + Kpair6);
4038         C4 += pow(fc,3)*(1-fc)*( Kpair1*Kpair3*Kpair4 + Kpair2*Kpair3*Kpair4 + Kpair1*Kpair5*Kpair6 + Kpair2*Kpair5*Kpair6);
4039         C4 += pow(fc,4)*Kfactor12*Kfactor13*Kfactor14*Kfactor23*Kfactor24*Kfactor34;
4040         return C4;
4041       }else if(term<=5){
4042         Float_t C3 = pow(1-fc,3) + 3*fc*pow(1-fc,2);
4043         C3 += pow(fc,2)*(1-fc)*Kpair1;// any SC pair will do
4044         C3 += pow(fc,2)*(1-fc)*Kpair4;// any MC pair will do
4045         C3 += pow(fc,2)*(1-fc)*Kpair6;// any MC pair will do
4046         C3 += pow(fc,3)*Kpair1*Kpair4*Kpair6;
4047         return C3;
4048       }else if(term==6 || term==11){
4049         return ((1-fcSq) + fcSq*Kpair1);// any SC pair will do
4050       }else if(term !=12 && term !=13){
4051         return ((1-fcSq) + fcSq*Kpair3);// any MC pair will do
4052       }else if(term==12){
4053         Float_t C22 = (1-fcSq) + fcSq*Kpair1;
4054         C22 *= (1-fcSq) + fcSq*Kpair2;
4055         return C22;
4056       }else return 1.0;
4057     }
4058   }
4059   
4060 }
4061 //________________________________________________________________________
4062 void AliFourPion::SetMomResCorrections(Bool_t legoCase, TH2D *temp2DSC, TH2D *temp2DMC){
4063   
4064  
4065   if(legoCase){
4066     cout<<"LEGO call to SetMomResCorrections"<<endl;
4067     fMomResC2SC = (TH2D*)temp2DSC->Clone();
4068     fMomResC2SC->SetDirectory(0);
4069     fMomResC2MC = (TH2D*)temp2DMC->Clone();
4070     fMomResC2MC->SetDirectory(0);
4071   }else {
4072     TFile *momResFile = new TFile("MomResFile.root","READ");
4073     if(!momResFile->IsOpen()) {
4074       cout<<"No momentum resolution file found"<<endl;
4075       AliFatal("No momentum resolution file found.  Kill process.");
4076     }else {cout<<"Good Momentum Resolution File Found!"<<endl;}
4077     
4078     TH2D *temp2DSC2 = (TH2D*)momResFile->Get("MRC_C2_SC");
4079     fMomResC2SC = (TH2D*)temp2DSC2->Clone();
4080     fMomResC2SC->SetDirectory(0);
4081     //
4082     TH2D *temp2DMC2 = (TH2D*)momResFile->Get("MRC_C2_MC");
4083     fMomResC2MC = (TH2D*)temp2DMC2->Clone();
4084     fMomResC2MC->SetDirectory(0);
4085     //
4086     momResFile->Close();
4087   }
4088
4089   
4090   for(Int_t bx=1; bx<=fMomResC2SC->GetNbinsX(); bx++){
4091     for(Int_t by=1; by<=fMomResC2SC->GetNbinsY(); by++){
4092       if(fMomResC2SC->GetBinContent(bx,by) > 1.5) fMomResC2SC->SetBinContent(bx,by, 1.0);// Maximum is ~1.02 
4093       if(fMomResC2SC->GetBinContent(bx,by) < 0.8) fMomResC2SC->SetBinContent(bx,by, 1.0);// Minimum is ~0.8
4094       if(fMomResC2MC->GetBinContent(bx,by) > 1.5) fMomResC2MC->SetBinContent(bx,by, 1.0);// Maximum is ~1.02 
4095       if(fMomResC2MC->GetBinContent(bx,by) < 0.8) fMomResC2MC->SetBinContent(bx,by, 1.0);// Minimum is ~0.8
4096     }
4097   }
4098   
4099   cout<<"Done reading momentum resolution file"<<endl;
4100 }
4101 //________________________________________________________________________
4102 void AliFourPion::SetFSICorrelations(Bool_t legoCase, TH1D *tempss[12], TH1D *tempos[12]){
4103   // read in 2-particle and 3-particle FSI correlations = K2 & K3
4104   // 2-particle input histo from file is binned in qinv.  3-particle in qinv of each pair
4105   if(legoCase){
4106     cout<<"LEGO call to SetFSICorrelations"<<endl;
4107     for(Int_t MB=0; MB<12; MB++) {
4108       fFSIss[MB] = (TH1D*)tempss[MB]->Clone();
4109       fFSIos[MB] = (TH1D*)tempos[MB]->Clone();
4110       //
4111       fFSIss[MB]->SetDirectory(0);
4112       fFSIos[MB]->SetDirectory(0);
4113     }
4114   }else {
4115     cout<<"non LEGO call to SetFSICorrelations"<<endl;
4116     TFile *fsifile = new TFile("KFile.root","READ");
4117     if(!fsifile->IsOpen()) {
4118       cout<<"No FSI file found"<<endl;
4119       AliFatal("No FSI file found.  Kill process.");
4120     }else {cout<<"Good FSI File Found!"<<endl;}
4121     
4122     TH1D *temphistoSS[12];
4123     TH1D *temphistoOS[12];
4124     for(Int_t MB=0; MB<12; MB++) {
4125       TString *nameK2SS = new TString("K2ss_");
4126       *nameK2SS += MB;
4127       temphistoSS[MB] = (TH1D*)fsifile->Get(nameK2SS->Data());
4128       //
4129       TString *nameK2OS = new TString("K2os_");
4130       *nameK2OS += MB;
4131       temphistoOS[MB] = (TH1D*)fsifile->Get(nameK2OS->Data());
4132       //
4133       fFSIss[MB] = (TH1D*)temphistoSS[MB]->Clone();
4134       fFSIos[MB] = (TH1D*)temphistoOS[MB]->Clone();
4135       fFSIss[MB]->SetDirectory(0);
4136       fFSIos[MB]->SetDirectory(0);
4137     }
4138     //
4139     
4140     fsifile->Close();
4141   }
4142   
4143   cout<<"Done reading FSI file"<<endl;
4144 }
4145 //________________________________________________________________________
4146 Float_t AliFourPion::FSICorrelation(Int_t charge1, Int_t charge2, Float_t qinv){
4147   // returns 2-particle Coulomb correlations = K2
4148   Int_t qbinL = fFSIss[fFSIindex]->GetXaxis()->FindBin(qinv-fFSIss[fFSIindex]->GetXaxis()->GetBinWidth(1)/2.);
4149   Int_t qbinH = qbinL+1;
4150   if(qbinL <= 0) return 1.0;
4151   if(qbinH > fFSIss[fFSIindex]->GetNbinsX()) {
4152     if(charge1!=charge2) {
4153       Float_t ScaleFac = (fFSIos[fFSIindex]->GetBinContent(fFSIos[fFSIindex]->GetNbinsX()-1) - 1);
4154       ScaleFac /= (Gamov(charge1, charge2, fFSIos[fFSIindex]->GetXaxis()->GetBinCenter(fFSIos[fFSIindex]->GetNbinsX()-1)) - 1);
4155       return ( (Gamov(charge1, charge2, qinv)-1)*ScaleFac + 1); 
4156     }else{
4157       Float_t ScaleFac = (fFSIss[fFSIindex]->GetBinContent(fFSIss[fFSIindex]->GetNbinsX()-1) - 1);
4158       ScaleFac /= (Gamov(charge1, charge2, fFSIss[fFSIindex]->GetXaxis()->GetBinCenter(fFSIss[fFSIindex]->GetNbinsX()-1)) - 1);
4159       return ( (Gamov(charge1, charge2, qinv)-1)*ScaleFac + 1);
4160     }
4161   }
4162   
4163   Float_t slope=0;
4164   if(charge1==charge2){
4165     slope = fFSIss[fFSIindex]->GetBinContent(qbinL) - fFSIss[fFSIindex]->GetBinContent(qbinH);
4166     slope /= fFSIss[fFSIindex]->GetXaxis()->GetBinCenter(qbinL) - fFSIss[fFSIindex]->GetXaxis()->GetBinCenter(qbinH);
4167     return (slope*(qinv - fFSIss[fFSIindex]->GetXaxis()->GetBinCenter(qbinL)) + fFSIss[fFSIindex]->GetBinContent(qbinL));
4168   }else {
4169     slope = fFSIos[fFSIindex]->GetBinContent(qbinL) - fFSIos[fFSIindex]->GetBinContent(qbinH);
4170     slope /= fFSIos[fFSIindex]->GetXaxis()->GetBinCenter(qbinL) - fFSIos[fFSIindex]->GetXaxis()->GetBinCenter(qbinH);
4171     return (slope*(qinv - fFSIos[fFSIindex]->GetXaxis()->GetBinCenter(qbinL)) + fFSIos[fFSIindex]->GetBinContent(qbinL));
4172   }
4173 }
4174 //________________________________________________________________________
4175 void AliFourPion::SetFillBins2(Int_t c1, Int_t c2, Int_t &b1, Int_t &b2){
4176   if((c1+c2)==1) {b1=0; b2=1;}// Re-assign to merge degenerate histos
4177   else {b1=c1; b2=c2;}
4178 }
4179 //________________________________________________________________________
4180 void AliFourPion::SetFillBins3(Int_t c1, Int_t c2, Int_t c3, Short_t part, Int_t &b1, Int_t &b2, Int_t &b3, Bool_t &fill2, Bool_t &fill3, Bool_t &fill4){
4181     
4182   // "part" specifies which pair is from the same event.  Only relevant for terms 2-4 
4183   Bool_t seSS=kFALSE;
4184   if(part==1) {// default case (irrelevant for term 1 and term 5)
4185     if(c1==c2) seSS=kTRUE;
4186   }
4187   if(part==2){
4188     if(c1==c3) seSS=kTRUE;
4189   }
4190   
4191   
4192   // fill2, fill3, fill4 are only used for Cumulant Terms 2,3,4
4193   if( (c1+c2+c3)==1) {
4194     b1=0; b2=0; b3=1;// Re-assign to merge degenerate histos
4195     //
4196     if(seSS) fill2=kTRUE;
4197     else {fill3=kTRUE; fill4=kTRUE;}
4198     //
4199   }else if( (c1+c2+c3)==2) {
4200     b1=0; b2=1; b3=1;
4201     //
4202     if(!seSS) {fill2=kTRUE; fill3=kTRUE;}
4203     else fill4=kTRUE;
4204     //
4205   }else {
4206     b1=c1; b2=c2; b3=c3;
4207     fill2=kTRUE; fill3=kTRUE; fill4=kTRUE;
4208   }
4209   
4210 }
4211 //________________________________________________________________________
4212 void AliFourPion::SetFillBins4(Int_t c1, Int_t c2, Int_t c3, Int_t c4, Int_t &b1, Int_t &b2, Int_t &b3, Int_t &b4, Int_t ENsum, Bool_t fillTerm[13]){
4213   
4214   // fill2, fill3, fill4 are only used for Cumulant Terms 2,3,4
4215   if( (c1+c2+c3+c4)==0 || (c1+c2+c3+c4)==4) {// all of the same charge: ---- or ++++
4216     
4217     b1=c1; b2=c2; b3=c3; b4=c4;
4218     if(ENsum==0) fillTerm[0]=kTRUE;
4219     else if(ENsum==1) {fillTerm[1]=kTRUE; fillTerm[2]=kTRUE; fillTerm[3]=kTRUE; fillTerm[4]=kTRUE;}
4220     else if(ENsum==2) {fillTerm[11]=kTRUE;}
4221     else if(ENsum==3) {fillTerm[5]=kTRUE; fillTerm[6]=kTRUE; fillTerm[7]=kTRUE; fillTerm[8]=kTRUE; fillTerm[9]=kTRUE; fillTerm[10]=kTRUE;}
4222     else fillTerm[12]=kTRUE;
4223   
4224   }else if( (c1+c2+c3+c4)==1) {// one positive charge: ---+
4225   
4226     b1=0; b2=0; b3=0; b4=1;// Re-assign to merge degenerate histos
4227     if(ENsum==0) fillTerm[0]=kTRUE;
4228     else if(ENsum==1){
4229       if(c4==1) fillTerm[1]=kTRUE;
4230       else {fillTerm[2]=kTRUE; fillTerm[3]=kTRUE; fillTerm[4]=kTRUE;}
4231     }else if(ENsum==2){
4232       fillTerm[11]=kTRUE;
4233     }else if(ENsum==3){
4234       if(c3==1 || c4==1) {fillTerm[5]=kTRUE; fillTerm[6]=kTRUE; fillTerm[8]=kTRUE;} 
4235       else {fillTerm[7]=kTRUE; fillTerm[9]=kTRUE; fillTerm[10]=kTRUE;}
4236     }else fillTerm[12]=kTRUE;
4237   
4238   }else if( (c1+c2+c3+c4)==2) {// two positive charges: --++
4239     
4240     b1=0; b2=0; b3=1; b4=1;// Re-assign to merge degenerate histos
4241     if(ENsum==0) fillTerm[0]=kTRUE;
4242     else if(ENsum==1){
4243       if(c4==1) {fillTerm[1]=kTRUE; fillTerm[2]=kTRUE;}
4244       else {fillTerm[3]=kTRUE; fillTerm[4]=kTRUE;}
4245     }else if(ENsum==2){
4246       if( (c1+c2)==0) fillTerm[11]=kTRUE;
4247     }else if(ENsum==3){
4248       if( (c1+c2)==0) fillTerm[5]=kTRUE;
4249       else if( (c1+c2)==1) {fillTerm[6]=kTRUE; fillTerm[7]=kTRUE; fillTerm[8]=kTRUE; fillTerm[9]=kTRUE;}
4250       else fillTerm[10]=kTRUE;
4251     }else fillTerm[12]=kTRUE;
4252
4253   }else{// three positive charges
4254     
4255     b1=0; b2=1; b3=1; b4=1;// Re-assign to merge degenerate histos
4256     if(ENsum==0) fillTerm[0]=kTRUE;
4257     else if(ENsum==1){
4258       if(c4==0) fillTerm[4]=kTRUE;
4259       else {fillTerm[1]=kTRUE; fillTerm[2]=kTRUE; fillTerm[3]=kTRUE;}
4260     }else if(ENsum==2){
4261       fillTerm[11]=kTRUE;
4262     }else if(ENsum==3){
4263       if(c3==0 || c4==0) {fillTerm[8]=kTRUE; fillTerm[9]=kTRUE; fillTerm[10]=kTRUE;}
4264       else {fillTerm[5]=kTRUE; fillTerm[6]=kTRUE; fillTerm[7]=kTRUE;}
4265     }else fillTerm[12]=kTRUE;
4266   
4267   }
4268   
4269 }
4270 //________________________________________________________________________
4271 void AliFourPion::SetFSIindex(Float_t R){
4272   if(!fMCcase){
4273     if(fPbPbcase){
4274       if(fMbin==0) fFSIindex = 0;//0-5%
4275       else if(fMbin==1) fFSIindex = 1;//5-10%
4276       else if(fMbin<=3) fFSIindex = 2;//10-20%
4277       else if(fMbin<=5) fFSIindex = 3;//20-30%
4278       else if(fMbin<=7) fFSIindex = 4;//30-40%
4279       else if(fMbin<=9) fFSIindex = 5;//40-50%
4280       else if(fMbin<=12) fFSIindex = 6;//40-50%
4281       else if(fMbin<=15) fFSIindex = 7;//40-50%
4282       else if(fMbin<=18) fFSIindex = 8;//40-50%
4283       else fFSIindex = 8;//90-100%
4284     }else fFSIindex = 9;// pp and pPb
4285   }else{// FSI binning for MC 
4286     if(R>=10.) fFSIindex = 0;
4287     else if(R>=9.) fFSIindex = 1;
4288     else if(R>=8.) fFSIindex = 2;
4289     else if(R>=7.) fFSIindex = 3;
4290     else if(R>=6.) fFSIindex = 4;
4291     else if(R>=5.) fFSIindex = 5;
4292     else if(R>=4.) fFSIindex = 6;
4293     else if(R>=3.) fFSIindex = 7;
4294     else if(R>=2.) fFSIindex = 8;
4295     else fFSIindex = 9;
4296   }
4297 }
4298 //________________________________________________________________________
4299 void AliFourPion::SetMuonCorrections(Bool_t legoCase, TH2D *tempMuon){
4300   if(legoCase){
4301     cout<<"LEGO call to SetMuonCorrections"<<endl;
4302     fWeightmuonCorrection = (TH2D*)tempMuon->Clone();
4303     fWeightmuonCorrection->SetDirectory(0);
4304   }else {
4305     cout<<"non LEGO call to SetMuonCorrections"<<endl;
4306     TFile *MuonFile=new TFile("MuonCorrection.root","READ");
4307     if(!MuonFile->IsOpen()) {
4308       cout<<"No Muon file found"<<endl;
4309       AliFatal("No Muon file found.  Kill process.");
4310     }else {cout<<"Good Muon File Found!"<<endl;}
4311     
4312     fWeightmuonCorrection = (TH2D*)MuonFile->Get("WeightmuonCorrection");
4313     fWeightmuonCorrection->SetDirectory(0);
4314     //
4315     MuonFile->Close();
4316   }
4317   cout<<"Done reading Muon file"<<endl;
4318 }
4319 //________________________________________________________________________
4320 Float_t AliFourPion::cubicInterpolate (Float_t p[4], Float_t x) {
4321   return p[1] + 0.5 * x*(p[2] - p[0] + x*(2.0*p[0] - 5.0*p[1] + 4.0*p[2] - p[3] + x*(3.0*(p[1] - p[2]) + p[3] - p[0])));// Paulinternet
4322 }
4323 //________________________________________________________________________
4324 Float_t AliFourPion::nCubicInterpolate (Int_t n, Float_t* p, Float_t coordinates[]) {
4325  
4326   if (n == 1) {
4327     return cubicInterpolate(p, *coordinates);
4328   }
4329   else {
4330     Float_t arr[4];
4331     Int_t skip = 1 << (n - 1) * 2;
4332     arr[0] = nCubicInterpolate(n - 1, p, coordinates + 1);
4333     arr[1] = nCubicInterpolate(n - 1, p + skip, coordinates + 1);
4334     arr[2] = nCubicInterpolate(n - 1, p + 2*skip, coordinates + 1);
4335     arr[3] = nCubicInterpolate(n - 1, p + 3*skip, coordinates + 1);
4336     return cubicInterpolate(arr, *coordinates);
4337   }
4338 }