Correct implementation of averaging (nTrigSubBin/nTrigAllBins)
[u/mrichter/AliRoot.git] / PWGPP / pid / doeffPi.C
CommitLineData
a8ad4709 1TObject* fContPid1;
2TObject* fContPid2;
3const Int_t nBinPid = 14; // pt,eta, ptPip, ptPin, PPip, PPin, TOF3sigmaPip, TOF3sigmaPin, isPhiTrue, nsigmaPip, nsigmaPin
4// 0.985 < mass < 1.045 (60) and 0 < centrality < 100 (10)
5Int_t binPid[nBinPid] = {1/*ptPhi*/,8/*EtaPi*/,20/*pt+*/,20/*pt-*/,5/*P+*/,1/*P-*/,2/*TOFmatch+*/,2/*TOFmatch-*/,2/*istrue*/,4/*Nsigma+*/,4/*Nsigma-*/,1/*DeltaPhi+*/,1/*DeltaPhi-*/,1/*Psi*/};
6Float_t xmin[nBinPid] = {1,-0.8,0.3,0.3,0,0,-0.5,-0.5,-0.5,0,0,-TMath::Pi(),-TMath::Pi(),-TMath::Pi()/2};
7Float_t xmax[nBinPid] = {5,0.8,4.3,4.3,1,1,1.5,1.5,1.5,7.5,7.5,TMath::Pi(),TMath::Pi(),TMath::Pi()/2};
8
9TF1 *fsign;
10TF1 *fall;
11TF1 *fback;
12
13Int_t ifunc=0;
14
15Float_t fitmin = 0.3;
16Float_t fitmax = 0.7;
17
18Int_t cmin = 1;
19Int_t cmax = 8;
20
21Float_t weightS = -1.;
22
23Int_t rebinsize = 4;
24
25Int_t parplotted = 2;
26
27Bool_t isMC = kFALSE; // don't change this (is set automatically)
28Bool_t selectTrue = kTRUE; // put it to true to remove background (only for MC)
29Bool_t keepTrue = kFALSE; // put it to false to fit only background (only for MC)
30
31Bool_t kGoodMatch = kFALSE; // to check good matching
32
33Bool_t kSigma2vs3 = kFALSE; // to check good matching
34
35Bool_t require5sigma = kFALSE; // don't touch this flag
36
37Bool_t bayesVsigma = kFALSE; // only to do checks
38
39Bool_t kTOFmatch = kFALSE; // for combined PID requires TOF matching
40
e34b28fe 41Bool_t kOverAll = kTRUE;
42Bool_t kOverAllTOFmatch = kFALSE;
43Bool_t kOverAll2Sigma = kTRUE;
44
a8ad4709 45
46Bool_t kLoaded=kFALSE;
47LoadLib(){
48 weightS = -1.;
49
50 if(! kLoaded){
51 gSystem->Load("libVMC.so");
52 gSystem->Load("libPhysics.so");
53 gSystem->Load("libTree.so");
54 gSystem->Load("libMinuit.so");
55 gSystem->Load("libSTEERBase.so");
56 gSystem->Load("libANALYSIS.so");
57 gSystem->Load("libAOD.so");
58 gSystem->Load("libESD.so");
59 gSystem->Load("libANALYSIS.so");
60 gSystem->Load("libANALYSISalice.so");
61 gSystem->Load("libCORRFW.so");
62 gSystem->Load("libNetx.so");
63 gSystem->Load("libPWGPPpid.so");
64
65 TFile *f = new TFile("AnalysisResults.root");
66 f->ls();
67 TList *l = (TList *) f->Get("contK0sBayes1");
68 l->ls();
69 fContPid1 = (AliPIDperfContainer *) l->At(0);
70 fContPid2 = (AliPIDperfContainer *) l->At(1);
71 }
72 kLoaded = kTRUE;
73
74 // check if MC
75 Float_t x[] = {xmin[0]+0.001,xmin[1]+0.001,xmin[2]+0.001,xmin[3]+0.001,xmin[4]+0.001,xmin[5]+0.001,xmin[6]+0.001,xmin[7]+0.001,1/*trueMC*/,xmin[9],xmin[10]};
76 Float_t x2[] = {xmax[0],xmax[1],xmax[2],xmax[3],xmax[4],xmax[5],xmax[6],xmax[7],xmax[8],xmax[9],xmax[10]};
77
78 AliPIDperfContainer *tmp = fContPid1;
79 TH1D *h = tmp->GetQA(0, x, x2)->ProjectionX("checkMC");
80
81 if(h->GetEntries()) isMC = kTRUE;
82 else isMC=kFALSE;
83
84 if(!isMC){
85 selectTrue = kFALSE;
86 keepTrue = kTRUE;
87 }
88 else{
89 printf("MC truth found!!!!!!\nIt is MC!!!!!!");
90 }
91
92 fsign = new TF1("fsign","[0]*TMath::Voigt(x-[1],[3],[2])",fitmin,fitmax);
93 fback = new TF1("fback","pol1",fitmin,fitmax);
94 fall = new TF1("fall","[0]*TMath::Voigt(x-[1],[3],[2]) + pol1(4)",fitmin,fitmax);
95
96 fsign->SetLineColor(2);
97 fback->SetLineColor(4);
98
99 if(kSigma2vs3){
100 kGoodMatch=kFALSE;
101 kOverAll = 0;
102 }
103
104 if(bayesVsigma){
105 kOverAll = 0;
106 kGoodMatch=kFALSE;
107 kSigma2vs3=kFALSE;
108 kTOFmatch=kTRUE;
109 weightS = -0.7;
110 }
111}
112
113doeffPi(Int_t pos=1,Float_t prob=0.1,Float_t etaminkp=-0.8,Float_t etamaxkp=0.8){
114 LoadLib();
115
116 Int_t nptbin = binPid[2];
117 Float_t minptbin = xmin[2];
118 Float_t maxptbin = xmax[2];
119
120 if(pos == 0){
121 nptbin = binPid[3];
122 minptbin = xmin[3];
123 maxptbin = xmax[3];
124 }
125
126 if(prob > 0.1999){
127 kGoodMatch = kFALSE;
128 kSigma2vs3 = kFALSE;
129 if(! kOverAll) require5sigma = kTRUE;
130 if(!isMC) weightS = -0.95;
131 }
132
133 TCanvas *c = new TCanvas();
134 c->Divide((nptbin+1)/2,2);
135 TH2F *hh.*hh2;
136 TH1D *h,*h2;
137 char name[100];
138 Float_t b[50][3];
139
140 Double_t xx[50],yy[50];
141 Double_t exx[50],eyy[50];
142
143 for(Int_t i=0;i < nptbin;i++){
144 c->cd(i+1)->SetLogy();
145 Float_t ptmin = minptbin+(maxptbin-minptbin)/nptbin*(i);
146 Float_t ptmax = minptbin+(maxptbin-minptbin)/nptbin*(i+1);
147
148 xx[i] = (ptmin+ptmax)/2;
149 exx[i] = (-ptmin+ptmax)/2;
150
151 Float_t pp=0.1;
152 if(prob < 0.2) pp = 0.;
153 if(pos) hh=GetHistoPip(ptmin,ptmax,pp,0.0,etaminkp,etamaxkp);
154 else hh=GetHistoPin(ptmin,ptmax,pp,0.0);
155 sprintf(name,"TOF matched: %f < p_{T} < %f GeV/#it{c}",ptmin,ptmax);
156 hh->SetTitle(name);
157 sprintf(name,"hNoPid%i",i);
158
159 pp=prob;
160 if(prob < 0.2) pp = 0.1;
161 if(pos) hh2=GetHistoPip(ptmin,ptmax,pp,0.0,etaminkp,etamaxkp);
162 else hh2=GetHistoPin(ptmin,ptmax,pp,0.0);
163 AddHisto(hh,hh2,weightS);
164
165 h = hh->ProjectionX(name,cmin,cmax);
166 h->RebinX(rebinsize);
167 h->Draw("ERR");
168 h->SetMarkerStyle(24);
169 b[i][0]=-1;
170 Int_t ntrial = 0;
171 Float_t chi2 = 10000;
172 while(ntrial < 10 && (chi2 > 20 + 1000*selectTrue)){
173 fit(h,b[i],"WW","",xx[i]);
174 c1->Update();
175// getchar();
176 fit(h,b[i],"","",xx[i]);
177 ntrial++;
178 chi2 = b[i][2];
179 printf("chi2 = %f\n",chi2);
180 c1->Update();
181// getchar();
182
183 }
184
185 yy[i] = fall->GetParameter(parplotted);
186 eyy[i] = fall->GetParError(parplotted);
187 }
188
189 TGraphErrors *gpar = new TGraphErrors(nptbin,xx,yy,exx,eyy);
190 c->cd(8);
191 gpar->Draw("AP");
192 gpar->SetMarkerStyle(20);
193
194 TCanvas *c2 = new TCanvas();
195 c2->Divide((nptbin+1)/2,2);
196 Float_t b2[50][3];
197
198 for(Int_t i=0;i < nptbin;i++){
199 c2->cd(i+1);
200 Float_t ptmin = minptbin+(maxptbin-minptbin)/nptbin*(i);
201 Float_t ptmax = minptbin+(maxptbin-minptbin)/nptbin*(i+1);
202
203 Float_t pp=prob;
204 if(prob < 0.2) pp = 0.1;
205 if(pos) hh=GetHistoPip(ptmin,ptmax,pp,0.0,etaminkp,etamaxkp);
206 else hh=GetHistoPin(ptmin,ptmax,pp,0.0);
207 sprintf(name,"P_{TOF} > 0.8: %f < p_{T} < %f GeV/#it{c}",ptmin,ptmax);
208 hh->SetTitle(name);
209 sprintf(name,"hPid60_%i",i);
210 h = hh->ProjectionX(name,cmin,cmax);
211 h->RebinX(rebinsize);
212 h->Draw("ERR");
213 h->SetMarkerStyle(24);
214 b2[i][0]=-1;
215 Int_t ntrial = 0;
216 Float_t chi2 = 10000;
217 while(ntrial < 40 && (chi2 > 20 + 1000*selectTrue)){
218 fit(h,b2[i],"WW","");
219 fit(h,b2[i],"","");
220 ntrial++;
221 chi2 = b2[i][2];
222 printf("chi2 = %f\n",chi2);
223 }
224 yy[i] = fall->GetParameter(parplotted);
225 eyy[i] = fall->GetParError(parplotted);
226
227 }
228
229 TGraphErrors *gpar2 = new TGraphErrors(nptbin,xx,yy,exx,eyy);
230 c2->cd(8);
231 gpar2->Draw("AP");
232 gpar2->SetMarkerStyle(20);
233
234 Double_t xpt[50],expt[50],eff[50],efferr[50];
235 for(Int_t i=0;i<nptbin;i++){
236 printf("%f +/- %f - %f +/- %f\n",b[i][0],b[i][1],b2[i][0],b2[i][1]);
237
238 Float_t ptmin = minptbin+(maxptbin-minptbin)/nptbin*(i);
239 Float_t ptmax = minptbin+(maxptbin-minptbin)/nptbin*(i+1);
240
241 xpt[i] = (ptmin+ptmax)/2;
242 expt[i] = (-ptmin+ptmax)/2;
243 eff[i] = b2[i][0]/(b[i][0]-b2[i][0]*weightS);
244
245 b[i][0] = b[i][0]-b2[i][0]*weightS;
246
247 efferr[i] = TMath::Sqrt(b[i][1]*b[i][1]/b[i][0]/b[i][0] + b2[i][1]*b2[i][1]/b2[i][0]/b2[i][0])*(b2[i][0]+b2[i][1])*(1+weightS*(b2[i][0]-b2[i][1])/b[i][0])/b[i][0];//*(1-eff[i]);//der2*der2*(b[i][1]*b[i][1] - b2[i][1]*b2[i][1]));
248
249 if(TMath::Abs(efferr[i]) > 1)efferr[i]=1;
250 }
251 new TCanvas();
252 TGraphErrors *geff = new TGraphErrors(nptbin,xpt,eff,expt,efferr);
253 geff->Draw("AP");
254
255 char flag[100];
256 sprintf(flag,"");
257
258 if(isMC){
259 if(selectTrue) sprintf(flag,"true");
260 else if(!keepTrue) sprintf(flag,"back");
261 }
262
263 char flag2[100];
264 sprintf(flag2,"");
265
266 char etarange[100];
267 sprintf(etarange,"_%.1f-%.1f_",etaminkp,etamaxkp);
268
269 if(kGoodMatch)
270 sprintf(flag2,"GM");
271
272 if(bayesVsigma)
273 sprintf(flag2,"BayesVsSigma");
274
275 if(kSigma2vs3)
276 sprintf(flag2,"Sigma2vs3");
277
278 if(kOverAll)
279 sprintf(flag2,"OverAll");
e34b28fe 280 if(kOverAllTOFmatch)
281 sprintf(flag2,"OverAllTOF");
282 if(kOverAll2Sigma)
283 sprintf(flag2,"OverAll2sigma");
a8ad4709 284
285 if(pos){
286 if(prob >=0.2) sprintf(name,"pionPos%sP%iEff%i_%i%s%s.root",etarange,Int_t(prob*100),(cmin-1)*10,cmax*10,flag,flag2);
287 else sprintf(name,"pionPos%sMatchEff%i_%i%s%s.root",etarange,(cmin-1)*10,cmax*10,flag,flag2);
288 }
289 else{
290 if(prob >=0.2) sprintf(name,"pionNeg%sP%iEff%i_%i%s%s.root",etarange,Int_t(prob*100),(cmin-1)*10,cmax*10,flag,flag2);
291 else sprintf(name,"pionNeg%sMatchEff%i_%i%s%s.root",etarange,(cmin-1)*10,cmax*10,flag,flag2);
292 }
293
294 TFile *fout = new TFile(name,"RECREATE");
295 geff->Write();
296 fout->Close();
297
298
299 TF1 *ff = new TF1("ff","[0] - [1]*TMath::Exp([2]*x)",0,3);
300 ff->SetParameter(0,0.67);
301 ff->SetParameter(1,1.14383e+00);
302 ff->SetParameter(2,-2.29910);
303 ff->SetLineColor(4);
304 ff->SetLineColor(2);
305 ff->Draw("SAME");
306
307 TF1 *ff2 = new TF1("ff2","[0] - [1]*TMath::Exp([2]*x)",0,3);
308 ff2->SetParameter(0,0.67);
309 ff2->SetParameter(1,9.23126e-01);
310 ff2->SetParameter(2,-1.851);
311 ff2->SetLineColor(4);
312 ff2->Draw("SAME");
313}
314
315TH2F *GetHistoPip(Float_t pt=1,Float_t ptM=1.1,Float_t pMinkp=0,Float_t pMinkn=0.,Float_t etaminkp=-0.8,Float_t etamaxkp=0.8){
316
317 Float_t x[] = {xmin[0]+0.001,etaminkp+0.001,pt+0.001,xmin[3]+0.001,pMinkp+0.001,pMinkn+0.001,(pMinkp>0.09)+0.001,kTOFmatch+0.001,selectTrue,xmin[9],xmin[10],xmin[11],xmin[12],xmin[13]};
318 Float_t x2[] = {xmax[0],etamaxkp-0.001,ptM-0.001,xmax[3],xmax[4],xmax[5],xmax[6],xmax[7],keepTrue,xmax[9],xmax[10],xmax[11],xmax[12],xmax[13]};
319
e34b28fe 320 if(kOverAll){
321 x[6] = 0.0001;
322 x2[9] = 5.9;
323 if(pMinkp > 0.19) x2[9] = 4.9;
324 }
325
326 if(kOverAllTOFmatch && pMinkp > 0.19){
327 x[6] = 1.0001;
328 x2[9] = 4.9;
329 }
330
331 if(kOverAll2Sigma && pMinkp > 0.09){
332 x2[9] = 4.9;
333 x[6] = 1.0001;
334 }
335
a8ad4709 336 if(kGoodMatch){
337 x[6] = 1.0001;
338 if(pMinkp > 0)
339 x2[9] = 4.9;
340
341 }
342
343 if(kTOFmatch){
344 x[6] = 1.0001;
345 }
346
347 if(kSigma2vs3){
348 x[6] = 1.0001;
349 x2[9] = 3;
350 if(pMinkp > 0)
351 x2[9] = 2;
352 }
353
354 if(bayesVsigma){
355 if(pMinkp > 0){
356 x[4] = 0.2001;
357 x2[9] = 5;
358 }
359 else{
360 x2[9] = 3;
361 }
362
363
364 }
365
366 if(require5sigma) x2[9] = 4.9;
367
368 AliPIDperfContainer *tmp = fContPid1;
369
370 TH2F *h = tmp->GetQA(0, x, x2);
371
372 h->GetXaxis()->SetTitle("M_{K^{0}_{s}} (GeV/#it{c}^{2})");
373 h->GetYaxis()->SetTitle("centrality [%]");
374
375 return h;
376}
377
378TH2F *GetHistoPin(Float_t pt=1,Float_t ptM=1.1,Float_t pMinkn=0,Float_t pMinkp=0.,Float_t etaminkp=-0.8,Float_t etamaxkp=0.8){
379
380 Float_t x[] = {xmin[0]+0.001,etaminkp+0.001,xmin[2]+0.001,pt+0.001,pMinkp+0.001,pMinkn+0.001,kTOFmatch+0.001,(pMinkn>0.09)+0.001,selectTrue,xmin[9],xmin[10],xmin[11],xmin[12],xmin[13]};
381 Float_t x2[] = {xmax[0],etamaxkp-0.001,xmax[2],ptM-0.001,xmax[4],xmax[5],xmax[6],xmax[7],keepTrue,xmax[9],xmax[10],xmax[11],xmax[12],xmax[13]};
382
e34b28fe 383 if(kOverAll){
384 x[7] = 0.0001;
385 x2[10] = 5.9;
386 if(pMinkn > 0.19) x2[10] = 4.9;
387 }
388
389 if(kOverAllTOFmatch && pMinkn > 0.19){
390 x[7] = 1.0001;
391 x2[10] = 4.9;
392 }
393
394 if(kOverAll2Sigma && pMinkn > 0.09){
395 x2[10] = 2;
396 x[7] = 1.0001;
397 }
398
a8ad4709 399 if(kGoodMatch){
400 x[7] = 1.0001;
401 if(pMinkn > 0)
402 x2[10] = 4.9;
403
404 }
405
406 if(kTOFmatch){
407 x[7] = 1.0001;
408 }
409
410 if(kSigma2vs3){
411 x[7] = 1.0001;
412 x2[10] = 3;
413 if(pMinkn > 0)
414 x2[10] = 2;
415 }
416
417 if(bayesVsigma){
418 if(pMinkn > 0){
419 x[5] = 0.2001;
420 x2[10] = 5;
421 }
422 else{
423 x2[10] = 3;
424 }
425 }
426
427 if(require5sigma) x2[10] = 4.9;
428
429 AliPIDperfContainer *tmp = fContPid2;
430
431 TH2F *h = tmp->GetQA(0, x, x2);
432
433 h->GetXaxis()->SetTitle("M_{K^{0}_{s}} (GeV/#it{c}^{2})");
434 h->GetYaxis()->SetTitle("centrality [%]");
435
436 return h;
437}
438
439
440fit(TH1D *h,Float_t *a=NULL,char *opt="",char *opt2="",Float_t pt=1.5){
441 if(h->Integral(1,h->GetNbinsX()) < 1){
442 if(a){
443 a[0]=0.001;
444 a[1]=1;
445 }
446 return;
447 }
448
449
450 fall->SetParameter(0,100);
451 fall->SetParameter(1,0.4971);
452 fall->SetParameter(2,0.00006);
453 fall->SetParameter(3,0.0035);
454
455 fall->SetParLimits(0,0.00001,10000);
456 fall->SetParLimits(1,0.4965,0.4985);
457 fall->SetParLimits(2,0.00005,0.001);
458 fall->SetParLimits(3,0.002,0.01);
459
460 fall->ReleaseParameter(4);
461 fall->ReleaseParameter(5);
462
463 if(selectTrue){
464 fall->FixParameter(4,0);
465 fall->FixParameter(5,0);
466 }
467
468 char name[100];
469 TF1 *ftmp=fall;
470
471 TF1 *ftmp2=new TF1(*fsign);
472 sprintf(name,"fsign%i",ifunc);
473 ftmp2->SetName(name);
474
475 TF1 *ftmp3=new TF1(*fback);
476 sprintf(name,"ftmp3%i",ifunc);
477 ftmp3->SetName(name);
478
479 ifunc++;
480
481 h->Fit(ftmp,opt,opt2,fitmin,fitmax);
482 h->Draw("ERR");
483
484 ftmp2->SetParameter(0,ftmp->GetParameter(0));
485 ftmp2->SetParameter(1,ftmp->GetParameter(1));
486 ftmp2->SetParameter(2,ftmp->GetParameter(2));
487 ftmp2->SetParameter(3,ftmp->GetParameter(3));
488 ftmp2->Draw("SAME");
489 ftmp3->SetParameter(0,ftmp->GetParameter(4));
490 ftmp3->SetParameter(1,ftmp->GetParameter(5));
491 ftmp3->Draw("SAME");
492
493 Float_t mean = ftmp->GetParameter(1);
494 Float_t sigma = 0.0044;
495
496 Float_t signI = ftmp2->Integral(mean-10*sigma,mean+10*sigma)/h->GetBinWidth(1);
497 Float_t backI = ftmp3->Integral(mean-3*sigma,mean+3*sigma)/h->GetBinWidth(1);
498
499 Float_t errI = TMath::Sqrt(ftmp->GetParError(0)*ftmp->GetParError(0)/(0.001+ftmp->GetParameter(0))/(0.001+ftmp->GetParameter(0)));
500
501 printf("signal(5 sigma) = %f +/- %f(fit) +/- %f(stat)\n",signI,errI*signI,TMath::Sqrt(signI));
502 printf("backgr(3sigma) = %f\n",backI);
503 printf("significance(3 sigma) = %f\n",signI/sqrt(signI+backI));
504
505 if(a){
506 a[0]=signI;
507 a[1]=signI*errI*signI*errI + signI;
508 a[1] = TMath::Sqrt(a[1]);
509 if(ftmp->GetNDF()) a[2] = ftmp->GetChisquare()/ftmp->GetNDF();
510
511
512 if(selectTrue){
513 a[0] = h->Integral(1,h->GetNbinsX());
514 a[1] = TMath::Sqrt(a[0]);
515 }
516 }
517}
518
519AddHisto(TH2F *h1,TH2F *h2,Float_t w){
520 Int_t nbinx = h1->GetNbinsX();
521 Int_t nbiny = h1->GetNbinsY();
522
523 for(Int_t i=1;i<=nbinx;i++){
524 for(Int_t j=1;j<=nbiny;j++){
525 Float_t val = h1->GetBinContent(i,j) + h2->GetBinContent(i,j)*w;
526 Float_t err = TMath::Min(TMath::Sqrt(val),val);
527 h1->SetBinContent(i,j,val);
528 h1->SetBinError(i,j,err);
529 }
530 }
531}