FairRoot/PandaRoot
checkphsp2_2.C
Go to the documentation of this file.
1 #include "TH2F.h"
2 #include "TH1F.h"
3 #include "TChain.h"
4 #include "TCanvas.h"
5 #include "TLatex.h"
6 #include "TStyle.h"
7 #include "TDirectory.h"
8 #include "TF1.h"
9 #include "TEfficiency.h"
10 #include "TColor.h"
11 #include "TRegexp.h"
12 #include "TMath.h"
13 #include "TROOT.h"
14 
15 #include <iostream>
16 #include <vector>
17 
18 double frsig, frmean, frchi, fremp;
19 
20 // -----------------------------------------------------------------------
21 
22 double Median(const TH2F *h, const TH2F *h2)
23 {
24  int n1 = h->GetXaxis()->GetNbins();
25  int n2 = h->GetYaxis()->GetNbins();
26 
27  int nfilled=0, N = h->GetEntries();
28 
29  double x[1000];
30  double y[1000];
31 
32  for (int i=1;i<n1+1;++i)
33  for (int j=1;j<n2+1;++j)
34  {
35  double cont = h->GetBinContent(i,j);
36  double cont2 = h2->GetBinContent(i,j);
37 
38  if (cont>(0.001*N) && cont2>0.)
39  {
40  x[nfilled] = cont2/cont;
41  y[nfilled++] = 1.;
42  }
43  }
44  return TMath::Median(nfilled, &x[0], &y[0]);
45 }
46 
47 // -----------------------------------------------------------------------
49 {
50  TH1 *h = (TH1*)gDirectory->Get(name);
51  if (h) delete h;
52 }
53 
54 // -----------------------------------------------------------------------
55 
56 void palette2()
57 {
58 /* Double_t red[4] = { 0.00, 176/256., 1.00, 1.00};
59  Double_t grn[4] = { 0.00, 53/256., 168/256., 1.00};
60  Double_t blu[4] = { 0.00, 0.00, 0.00, 0.50};
61  Double_t stp[4] = { 0.00, 0.35 ,0.65, 1.00 };*/
62 // int hex[8] = {0x482223,0x64394A,0x6D5877,0x5E7E9E,0x3EA5B0,0x48C9A9,0x8DE890,0xE7FE76};
63 // int hex[8] = {0x593432,0x6A4754,0x6A6175,0x5D7E8B,0x5A9A8D,0x7AB17F,0xB4C270,0xF8CB74};
64 // Double_t blu[8], red[8], grn[8], stp[8];
65 //
66 // for (int i=0;i<8;++i)
67 // {
68 // blu[7-i] = (hex[i] & 0xff)/255.;
69 // grn[7-i] = ((hex[i]>>8) & 0xff)/255.;
70 // red[7-i] = ((hex[i]>>16) & 0xff)/255.;
71 // stp[7-i] = (double)(7-i)/7.0;
72 // // cout << red[i]<<" "<<grn[i]<<" "<<blu[i]<<" "<<stp[i]<<endl;
73 // }
74  Double_t blu[] = {1,1,0};
75  Double_t red[] = {0,1,1};
76  Double_t grn[] = {0,1,0};
77  Double_t stp[] = {0,0.5,1.0};
78 
79  TColor::CreateGradientColorTable(3,stp,red,grn,blu,100) ;
80  gStyle->SetNumberContours(100);
81 /* Double_t blu[3] = {1,0.5,0};
82  Double_t red[3] = {1,0.5,0};
83  Double_t grn[3] = {1,0.5,0};
84  Double_t stp[3] = {0,0.5,1};
85 
86  Int_t n=100;
87  TColor::CreateGradientColorTable(3,stp,red,grn,blu,n);*/
88  }
89 
90 // -----------------------------------------------------------------------
91 
92 void cleanup()
93 {
94  delete_histo("h1");
95  delete_histo("h2");
96  delete_histo("hp");
97  delete_histo("heff");
98  //delete_histo("hqa");
99 }
100 
101 // -----------------------------------------------------------------------
102 
103 void config_pad(TVirtualPad* p, double b=0.1, double r=0.12, double t=0.08, double l=0.1)
104 {
105  p->SetTopMargin(t);
106  p->SetBottomMargin(b);
107  p->SetLeftMargin(l);
108  p->SetRightMargin(r);
109 /* p->SetGridx();
110  p->SetGridy();*/
111 }
112 
113 // -----------------------------------------------------------------------
114 
115 double qadalitz(TH2F* h1, TH2F* h2, TH1F *hpull)
116 {
117  double globeff = h2->GetEntries()/h1->GetEntries();
118  hpull->Reset();
119 
120  int nx=h1->GetNbinsX();
121  int ny=h1->GetNbinsX();
122 
123  double sum=0;
124  double coverage1=0, coverage2=0;
125 
126  for (int i=1;i<=nx;++i)
127  for (int j=1;j<=ny;++j)
128  {
129  if (h1->GetBinContent(i,j)>0) coverage1+=1.;
130  if (h2->GetBinContent(i,j)>0) coverage2+=1.;
131 
132  double c1 = h1->GetBinContent(i,j);
133  double c2 = h2->GetBinContent(i,j);
134 
135  if (c1>0)
136  {
137  double eff = c2/c1;
138  double efferr = sqrt(2.)*eff/sqrt(c1);
139  if (eff>0.0001) hpull->Fill((eff-globeff)/efferr);
140  }
141  }
142 
143  TF1 f1("f1","gaus");
144  f1.SetParameters(hpull->GetMaximum(), hpull->GetMean(), hpull->GetRMS());
145  hpull->Fit("f1","");
146 
147  double mean = f1.GetParameter(1);
148  double sigma = f1.GetParameter(2);
149  double chindf = f1.GetChisquare()/f1.GetNDF();
150  if (chindf<1.) chindf=1.;
151 
152  cout <<"m="<<mean<<" sigma="<<sigma<<" chi/ndf="<<chindf<<" emtpyfrac="<<fabs(1.0 - coverage2/coverage1)<<endl;
153  return (fabs(sigma-1.) + fabs(mean) + fabs(1.0-chindf) + fabs(1.0 - coverage2/coverage1));
154 }
155 // -----------------------------------------------------------------------
156 
157 double qadalitz2(TH2F* h1, TH2F* h2, TH1F *hpull, TH2F* heff)
158 {
159  int nx=h1->GetNbinsX();
160  int ny=h1->GetNbinsX();
161 
162  double N = h1->GetEntries();
163 
164  double coverage1=0, coverage2=0;
165  for (int i=1;i<=nx;++i)
166  for (int j=1;j<=ny;++j)
167  {
168  if (h1->GetBinContent(i,j)>0.0001*N) coverage1+=1.;
169  if (h1->GetBinContent(i,j)>0.0001*N && h2->GetBinContent(i,j)>0) coverage2+=1.;
170  }
171 
172  if (coverage2==0 || h1->GetEntries()<10 || h2->GetEntries()<10) return 10;
173 
174  double globeff = Median(h1,h2);//h2->GetEntries()/h1->GetEntries()*coverage1/coverage2;
175 
176  cout <<"c1="<<coverage1<<" c2="<<coverage2<<" geff="<<globeff<<endl;
177 
178  hpull->Reset();
179 
180  TEfficiency teff(*h2, *h1);
181  teff.SetStatisticOption(TEfficiency::kFNormal);
182 
183  double perfbins=0.;
184 
185  for (int i=1;i<=nx;++i)
186  for (int j=1;j<=ny;++j)
187  {
188  double c1 = h1->GetBinContent(i,j);
189  double c2 = h2->GetBinContent(i,j);
190 
191  if (c1>0.0001*N)
192  {
193  int gbin = teff.GetGlobalBin(i,j);
194 
195  double eff = teff.GetEfficiency(gbin);
196  double efferr = teff.GetEfficiencyErrorUp(gbin);
197  if (eff>0.0001 && efferr>0)
198  {
199  hpull->Fill((eff-globeff)/efferr);
200  heff->SetBinContent(i,j,(eff-globeff)/efferr);
201  }
202  if (eff>0.99999) perfbins+=1.;
203  }
204  else
205  {
206  h1->SetBinContent(i,j,0);
207  h2->SetBinContent(i,j,0);
208  }
209  }
210 
211  if (hpull->GetEntries()<5) return 0.001;
212 
213  TF1 f1("f1","gaus");
214  f1.SetParameters(hpull->GetMaximum(), hpull->GetMean(), hpull->GetRMS());
215  f1.SetParLimits(1,-8,8);
216  f1.SetParLimits(2,0,20);
217 
218  hpull->Fit("f1","q");
219 
220  frmean = f1.GetParameter(1);
221  frsig = f1.GetParameter(2);
222  frchi = f1.GetChisquare()/f1.GetNDF();
223  fremp = fabs(1.0 - coverage2/coverage1);
224  double facperf = fabs(1.0 - perfbins/coverage1);
225 
226  if (frchi<1.) frchi=1.;
227 
228  cout <<"m="<<frmean<<" sigma="<<frsig<<" chi/ndf="<<frchi<<" emtpyfrac="<<fremp<<" not perf="<<facperf<<endl;
229  return (fabs(frsig-1.) + fabs(frmean) + fabs(1.0-frchi) + fremp)*facperf;
230 }
231 
232 // -----------------------------------------------------------------------
233 // -----------------------------------------------------------------------
234 
235 int checkphsp2_2(TString filepatt, double step=.1, TString cut="")
236 {
237  palette2();
238 
239  TCanvas *c2=new TCanvas("c2","c2",1000,10,800,600);
240  gStyle->SetOptStat(0);
241  gStyle->SetTitleH(0.07);
242 
243  if (cut=="") cut="1";
244 
245  TRegexp reg("MI[0-9][0-9][0-9]");
246  TRegexp regn("ntp[0-9]");
247 
248  TString mname=filepatt(reg);
249  mname = mname(2,3);
250 
251  TString ntp=filepatt(regn);
252 
253  TChain *n=new TChain(ntp);
254  if (filepatt.EndsWith(".root")) filepatt.ReplaceAll(".root","");
255  n->Add(filepatt+"*.root");
256 
257 
258  double minE = int(n->GetMinimum("beamm")*10+0.1)/10.;
259  double maxE = 5.5;
260  int bins = int((maxE-minE+0.05)/step)+1;
261 
262  cout <<"minE="<<minE<<" maxE="<<maxE<<" bins="<<bins<<endl;
263 
264  TH1F *hqa = new TH1F("hqa","Phsp QA for M"+mname, bins, minE-step/2., maxE+step/2.);
265  hqa->SetMarkerStyle(8);
266  hqa->SetMarkerSize(1.5);
267  hqa->SetMarkerColor(2);
268  hqa->SetStats(0);
269  hqa->SetYTitle("QA [AU]");
270  hqa->SetXTitle("E_{cm} [GeV]");
271  hqa->GetYaxis()->SetTitleOffset(1.2);
272 
273  int cnt=1;
274 
275  c2->cd();
276  TH1F *htsten = new TH1F("htsten","tst", bins, minE-step/2., maxE+step/2.);
277  n->Draw("beamm>>htsten",cut,"");
278 
279  int Npoints=0;
280  for (int j=0;j<bins;++j) if (htsten->GetBinContent(j+1)>0) Npoints++;
281 
282  int wid = 230;
283  TCanvas *c1=new TCanvas("c1","c1",10,10,wid*Npoints,wid*4);
284  c1->Divide(Npoints,4);
285 
286  c2->cd();
287 
288  for (int j=0;j<bins;++j)
289  {
290  cleanup();
291  if (htsten->GetBinContent(j+1)<1) continue;
292 
293  double ecm = hqa->GetBinCenter(j+1);
294  cout <<mname<<" Ecm:"<<ecm<<endl;
295  TString ecut = cut+TString::Format("&&abs(beamm-%f)<0.01",ecm);
296 
297  // find limits for Dalitz plot
298  int nsel = n->Draw("xdal02:xdal12>>hhh",ecut,"goff");
299  if (nsel>500000) nsel=500000;
300  TH2F *h = (TH2F*)gDirectory->Get("hhh");
301  if (h->GetEntries()==0) continue;
302 
303  double xmin=1000., ymin=1000., xmax=0., ymax=0.;
304  double *x02=n->GetV2(), *x12 = n->GetV1();
305  for (int i=0;i<nsel;++i)
306  {
307  if (x02[i]>xmax) xmax=x02[i];
308  if (x02[i]<xmin) xmin=x02[i];
309  if (x12[i]>ymax) ymax=x12[i];
310  if (x12[i]<ymin) ymin=x12[i];
311  }
312 
313  TH2F *h1 = new TH2F("h1",TString::Format("M%s @ %3.1f GeV",mname.Data(),ecm),20,xmin,xmax,20,ymin,ymax);
314  TH2F *h2 = new TH2F("h2",TString::Format("M%s @ %3.1f GeV triggered",mname.Data(),ecm),20,xmin,xmax,20,ymin,ymax);
315  TH2F *heff = new TH2F("heff",TString::Format("rel eff. M%s @ %3.1f GeV ",mname.Data(),ecm),20,xmin,xmax,20,ymin,ymax);
316  TH1F *hp = new TH1F("hp",TString::Format("M%s @ %3.1f GeV pull",mname.Data(),ecm),50,-8,8);
317 
318  h1->Sumw2();
319  h2->Sumw2();
320 
321  int N=n->GetEntries();
322  n->Project("h1","xdal02:xdal12",ecut);
323  n->Project("h2","xdal02:xdal12",ecut+"&&trig");
324  h1->SetContour(100);
325  h2->SetContour(100);
326 
327  //cout <<ecut<<" : "<<h1->GetEntries()<<" "<<h2->GetEntries()<<endl;
328  //c1->cd(1);h1->DrawCopy("colz");c1->cd(2);h2->DrawCopy("colz");c1->Update();
329  //continue;
330  double qa = qadalitz2(h1, h2, hp, heff);
331  c1->cd(cnt); config_pad(gPad); h1->DrawCopy("colz");
332  //h2->SetMaximum(h1->GetMaximum());
333  c1->cd(cnt+Npoints); config_pad(gPad); h2->DrawCopy("colz");
334  //h2->Divide(h1);
335  //h2->SetMaximum(1.0);
336  // convert in plot with sigma deviation
337  //h2->SetTitle(TString::Format("rel eff. M%s @ %3.1f GeV ",mname.Data(),ecm));
338  heff->SetMinimum(-8);
339  heff->SetMaximum(8);
340 
341  c1->cd(cnt+Npoints*2); config_pad(gPad); heff->DrawCopy("colz");
342  c1->cd(cnt+Npoints*3); gPad->SetTopMargin(0.08);//hp->Draw();
343  hp->DrawCopy();
344 
345  TLatex lat;
346  lat.DrawLatex(-7.5,hp->GetMaximum()*0.9,TString::Format("QA = %4.2f",qa));
347  cout <<"QA = "<<qa<<endl;
348  c1->Update();
349 
350  if (qa!=qa || qa>20) qa=20;
351 
352  hqa->SetBinContent(j+1,qa);
353 
354  c2->cd();
355  gPad->SetGridx();gPad->SetGridy();
356  config_pad(gPad,0.14,0.05,0.08,0.12);
357  hqa->SetMaximum(21);
358  hqa->Draw("P");
359  c2->Update();
360  cnt++;
361  }
362 
363  c1->SaveAs("QA_plots_M"+mname+".gif");
364  c2->SaveAs("QA_value_M"+mname+".gif");
365  return 0;
366 }
Double_t p
Definition: anasim.C:58
double r
Definition: RiemannTest.C:14
Int_t i
Definition: run_full.C:25
TTree * b
TF1 * f1
Definition: reco_analys2.C:50
friend F32vec4 sqrt(const F32vec4 &a)
Definition: P4_F32vec4.h:29
double qadalitz2(TH2F *h1, TH2F *h2, TH1F *hpull, TH2F *heff)
Definition: checkphsp2_2.C:157
Double_t xmax
void delete_histo(TString name)
Definition: checkphsp2_2.C:48
int n
c2
Definition: plot_dirc.C:39
Double_t sigma[nsteps]
Definition: dedx_bands.C:65
double qadalitz(TH2F *h1, TH2F *h2, TH1F *hpull)
Definition: checkphsp2_2.C:115
int checkphsp2_2(TString filepatt, double step=.1, TString cut="")
Definition: checkphsp2_2.C:235
void palette2()
Definition: checkphsp2_2.C:56
double fremp
Definition: checkphsp2_2.C:18
double cut[MAX]
Definition: autocutx.C:36
Double_t
friend F32vec4 fabs(const F32vec4 &a)
Definition: P4_F32vec4.h:47
c1
Definition: plot_dirc.C:35
TString name
double frmean
Definition: checkphsp2_2.C:18
void config_pad(TVirtualPad *p, double b=0.1, double r=0.12, double t=0.08, double l=0.1)
Definition: checkphsp2_2.C:103
double frchi
Definition: checkphsp2_2.C:18
double frsig
Definition: checkphsp2_2.C:18
Double_t x
double Median(const TH2F *h, const TH2F *h2)
Definition: checkphsp2_2.C:22
Int_t cnt
Definition: hist-t7.C:106
TTree * t
Definition: bump_analys.C:13
Double_t xmin
Double_t mean[nsteps]
Definition: dedx_bands.C:65
Double_t y
void cleanup()
Definition: checkphsp2_2.C:92