00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "BCH2D.h"
00011 #include "BCMath.h"
00012
00013 #include <TCanvas.h>
00014 #include <TLine.h>
00015 #include <TMarker.h>
00016 #include <TLegend.h>
00017
00018
00019
00020 BCH2D::BCH2D()
00021 {
00022
00023 fHistogram = 0;
00024 fIntegratedHistogram = 0;
00025
00026 fModeFlag = 0;
00027 }
00028
00029
00030
00031 BCH2D::~BCH2D()
00032 {
00033
00034 if (fHistogram)
00035 delete fHistogram;
00036
00037 if (fIntegratedHistogram)
00038 delete fIntegratedHistogram;
00039 }
00040
00041
00042
00043 void BCH2D::GetMode(double& mode)
00044 {
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 }
00057
00058
00059
00060 void BCH2D::Print(const char * filename, int options, int ww, int wh)
00061 {
00062
00063 TCanvas * c;
00064 if(ww >0 && wh > 0)
00065 c = new TCanvas("c","c",ww,wh);
00066 else
00067 c = new TCanvas("c","c",700,700);
00068 c -> cd();
00069
00070
00071 this->Draw(options);
00072
00073 gPad->RedrawAxis();
00074
00075
00076 c -> Print(filename);
00077
00078 }
00079
00080
00081
00082 void BCH2D::Draw(int options)
00083 {
00084
00085 fHistogram -> SetLineColor(kBlack);
00086
00087
00088 fHistogram->Scale(1./fHistogram->Integral("width"));
00089
00090 double modex,modey;
00091
00092
00093 if(fModeFlag)
00094 {
00095 modex = fMode[0];
00096 modey = fMode[1];
00097 }
00098 else
00099 {
00100 int maximumbin = fHistogram -> GetMaximumBin();
00101
00102 int binx = maximumbin % (fHistogram -> GetNbinsX() + 2);
00103 int biny = maximumbin / (fHistogram -> GetNbinsX() + 2);
00104
00105 modex = fHistogram -> GetXaxis() -> GetBinCenter(binx);
00106 modey = fHistogram -> GetYaxis() -> GetBinCenter(biny);
00107 }
00108
00109
00110 fHistogram->Scale(1./fHistogram->Integral("width"));
00111
00112
00113 if (options == 0)
00114 fHistogram -> Draw("CONT0");
00115
00116 else if (options == 1)
00117 {
00118 fHistogram -> Draw("CONT3");
00119
00120
00121 this -> CalculateIntegratedHistogram();
00122
00123 double levels[4];
00124 levels[0] = 0.;
00125 levels[1] = this -> GetLevel(1.0 - 0.6827);
00126 levels[2] = this -> GetLevel(1.0 - 0.9545);
00127 levels[3] = this -> GetLevel(1.0 - 0.9973);
00128
00129 fHistogram -> SetContour(4, levels);
00130
00131
00132 TMarker* marker = new TMarker(modex, modey, 24);
00133 marker -> Draw();
00134
00135 TLegend* legend = new TLegend(0.65, 0.80, 0.95, 0.95);
00136 legend -> SetBorderSize(0);
00137 legend -> SetFillColor(kWhite);
00138 legend -> AddEntry(fHistogram, "68% prob. region", "L");
00139 legend -> AddEntry(marker, "Best fit", "P");
00140 legend -> Draw();
00141
00142 }
00143 else if (options == 2)
00144 {
00145
00146 fHistogram -> Draw("CONT3");
00147
00148
00149 this -> CalculateIntegratedHistogram();
00150
00151 double levels[2];
00152 double level32 = this -> GetLevel(0.32);
00153 levels[0] = 0.;
00154 levels[1] = level32;
00155
00156 fHistogram -> SetContour(2, levels);
00157
00158
00159 TMarker* marker = new TMarker(modex, modey, 24);
00160 marker -> Draw();
00161
00162 TLegend* legend = new TLegend(0.65, 0.80, 0.95, 0.95);
00163 legend -> SetBorderSize(0);
00164 legend -> SetFillColor(kWhite);
00165 legend -> AddEntry(fHistogram, "68% prob. region", "L");
00166 legend -> AddEntry(marker, "Best fit", "P");
00167 legend -> Draw();
00168
00169 }
00170 else if (options == 3)
00171 {
00172
00173 fHistogram -> Draw("CONT3");
00174
00175
00176 this -> CalculateIntegratedHistogram();
00177
00178 double levels[2];
00179 double level10 = this -> GetLevel(0.10);
00180 levels[0] = 0.;
00181 levels[1] = level10;
00182
00183 fHistogram -> SetContour(2, levels);
00184
00185 TLegend* legend = new TLegend(0.65, 0.80, 0.95, 0.95);
00186 legend -> SetBorderSize(0);
00187 legend -> SetFillColor(kWhite);
00188 legend -> AddEntry(fHistogram, "90% prob. region", "L");
00189 legend -> Draw();
00190
00191 }
00192 else if (options == 4)
00193 {
00194
00195 fHistogram -> Draw("CONT3");
00196
00197
00198 this -> CalculateIntegratedHistogram();
00199
00200 double levels[2];
00201 double level5 = this -> GetLevel(0.05);
00202 levels[0] = 0.;
00203 levels[1] = level5;
00204
00205 fHistogram -> SetContour(2, levels);
00206
00207 TLegend* legend = new TLegend(0.65, 0.80, 0.95, 0.95);
00208 legend -> SetBorderSize(0);
00209 legend -> SetFillColor(kWhite);
00210 legend -> AddEntry(fHistogram, "95% prob. region", "L");
00211 legend -> Draw();
00212
00213 }
00214 else if (options == 5)
00215
00216 fHistogram -> Draw("COL");
00217
00218 else if (options == 52 || options == 521)
00219 {
00220
00221
00222 int nx = fHistogram -> GetNbinsX();
00223 int ny = fHistogram -> GetNbinsY();
00224 double xmin = fHistogram->GetXaxis()->GetXmin();
00225 double xmax = fHistogram->GetXaxis()->GetXmax();
00226 TH2D * h = new TH2D("htemp52",fHistogram->GetTitle(),
00227 nx,xmin,xmax,
00228 ny,fHistogram->GetYaxis()->GetXmin(),fHistogram->GetYaxis()->GetXmax());
00229 h->SetXTitle(fHistogram->GetXaxis()->GetTitle());
00230 h->SetYTitle(fHistogram->GetYaxis()->GetTitle());
00231
00232
00233
00234 for(int i=1;i<=nx;i++)
00235 for(int j=1;j<=ny;j++)
00236 {
00237 double val = fHistogram -> GetBinContent(i,j);
00238
00239 if(options == 521)
00240 {
00241
00242
00243
00244 val = log10(val);
00245 }
00246 h -> SetBinContent(i,j,val);
00247 }
00248
00249
00250 h->SetStats(0);
00251 h->Draw("col");
00252
00253
00254 fHistogram -> Draw("cont3 same");
00255 fHistogram -> SetLineWidth(2);
00256
00257
00258 this -> CalculateIntegratedHistogram();
00259
00260 double levels[] = { this -> GetLevel(0.32) };
00261 fHistogram -> SetContour(1, levels);
00262
00263
00264 TMarker * marker0 = new TMarker(modex, modey, 8);
00265 marker0 -> SetMarkerColor(0);
00266 marker0 -> SetMarkerSize(.7);
00267 marker0 -> Draw();
00268 TMarker * marker1 = new TMarker(modex, modey, 4);
00269 marker1 -> SetMarkerColor(1);
00270 marker1 -> SetMarkerSize(.7);
00271 marker1 -> Draw();
00272
00273
00274
00275
00276 }
00277
00278 else if (options == 53 || options == 531)
00279 {
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313 gPad -> SetLogz();
00314 fHistogram -> Draw("colz");
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327 TMarker * marker0 = new TMarker(modex, modey, 8);
00328 marker0 -> SetMarkerColor(0);
00329 marker0 -> SetMarkerSize(.7);
00330 marker0 -> Draw();
00331 TMarker * marker1 = new TMarker(modex, modey, 4);
00332 marker1 -> SetMarkerColor(1);
00333 marker1 -> SetMarkerSize(.7);
00334 marker1 -> Draw();
00335
00336
00337
00338
00339 }
00340
00341 }
00342
00343
00344
00345 void BCH2D::CalculateIntegratedHistogram()
00346 {
00347
00348 int nz = 100;
00349
00350 double zmax = fHistogram -> GetMaximum();
00351 double dz = zmax / double(nz);
00352
00353 double nx = fHistogram -> GetNbinsX();
00354 double ny = fHistogram -> GetNbinsY();
00355
00356
00357
00358 if (fIntegratedHistogram)
00359 delete fIntegratedHistogram;
00360
00361 fIntegratedHistogram = new TH1D(Form("%s_int_prob",fHistogram->GetName()), "", nz, 0.0, zmax);
00362 fIntegratedHistogram -> SetXTitle("z");
00363 fIntegratedHistogram -> SetYTitle("Integrated probability");
00364 fIntegratedHistogram -> SetStats(kFALSE);
00365
00366
00367
00368 for (int ix = 1; ix <= nx; ix++)
00369 for (int iy = 1; iy <= ny; iy++)
00370 {
00371 int binmin = BCMath::Nint(fHistogram -> GetBinContent(ix, iy) / dz);
00372 for (int i = binmin; i <= nz; i++)
00373 fIntegratedHistogram -> SetBinContent(i,
00374 fIntegratedHistogram -> GetBinContent(i) +
00375 fHistogram -> GetBinContent(ix, iy));
00376 }
00377
00378 }
00379
00380
00381
00382 double BCH2D::GetLevel(double p)
00383 {
00384
00385
00386
00387 double level = 0.0;
00388
00389 int nquantiles = 1;
00390 double quantiles[1];
00391 double probsum[1];
00392
00393 probsum[0] = p;
00394
00395 fIntegratedHistogram -> GetQuantiles(nquantiles, quantiles, probsum);
00396
00397 level = quantiles[0];
00398
00399 return level;
00400 }
00401
00402
00403
00404 TGraph ** BCH2D::GetBandGraphs(TH2D * h, int &n)
00405 {
00406 n=0;
00407
00408 int nbands=0;
00409 TH2D * hcopy = (TH2D*)h->Clone(Form("%s_copy",h->GetName()));
00410
00411 std::vector <int> nint=GetNIntervalsY(hcopy,nbands);
00412
00413 if(nbands>2)
00414 {
00415
00416
00417 return 0;
00418 }
00419 else if(nbands==0)
00420 {
00421
00422 return 0;
00423 }
00424
00425 TGraph ** gxx = new TGraph*[nbands];
00426
00427 TH2D * h0 = (TH2D*)h->Clone();
00428
00429 if (nbands>0)
00430 gxx[0] = GetLowestBandGraph(h0,nint);
00431
00432 if (nbands==2)
00433 {
00434 gxx[1] = GetLowestBandGraph(h0);
00435 n=2;
00436 }
00437 else
00438 n=1;
00439
00440 return gxx;
00441 }
00442
00443
00444
00445 std::vector <int> BCH2D::GetNIntervalsY(TH2D * h, int &nfoundmax)
00446 {
00447
00448 std::vector <int> nint;
00449
00450 int nx = h -> GetNbinsX();
00451 int ny = h -> GetNbinsY();
00452
00453 nfoundmax=0;
00454
00455
00456 for (int ix=1; ix<=nx; ix++)
00457 {
00458 int nfound=0;
00459
00460
00461
00462 for (int iy=1; iy<=ny; iy++)
00463 if(h->GetBinContent(ix,iy)>0.)
00464 {
00465 while(h->GetBinContent(ix,++iy)>0.)
00466 ;
00467 nfound++;
00468 }
00469
00470
00471 if(nfound>nfoundmax)
00472 nfoundmax=nfound;
00473
00474 nint.push_back(nfound);
00475 }
00476
00477 return nint;
00478
00479 }
00480
00481
00482
00483 TGraph * BCH2D::GetLowestBandGraph(TH2D * h)
00484 {
00485 int n;
00486 return GetLowestBandGraph(h,GetNIntervalsY(h,n));
00487 }
00488
00489
00490
00491 TGraph * BCH2D::GetLowestBandGraph(TH2D * h, std::vector<int> nint)
00492 {
00493 int nx = h -> GetNbinsX() - 1;
00494 int ny = h -> GetNbinsY();
00495
00496 TGraph * g = new TGraph(2*nx);
00497
00498 for (int ix=1; ix<=nx; ix++)
00499 {
00500
00501 double x;
00502 if(ix==1)
00503 x = h->GetXaxis()->GetBinLowEdge(1);
00504 else if(ix==nx)
00505 x = h->GetXaxis()->GetBinLowEdge(nx+1);
00506 else
00507 x = h->GetXaxis()->GetBinCenter(ix);
00508
00509 for(int iy=1; iy<=ny; iy++)
00510 if(h->GetBinContent(ix,iy)>0.)
00511 {
00512
00513 g->SetPoint(ix-1, x, h->GetYaxis()->GetBinLowEdge(iy));
00514
00515
00516 if(nint[ix-1]==2)
00517 h->SetBinContent(ix,iy,0.);
00518
00519 while(h->GetBinContent(ix,++iy)>0.)
00520 if(nint[ix-1]==2)
00521 h->SetBinContent(ix,iy,0.);
00522
00523
00524 g->SetPoint(2*nx-ix, x, h->GetYaxis()->GetBinLowEdge(iy));
00525
00526 break;
00527 }
00528 }
00529
00530 return g;
00531 }
00532
00533
00534
00535 std::vector <double> BCH2D::GetLevelBoundary(TH2D * h, double level)
00536 {
00537 std::vector <double> b;
00538
00539 int nx = h -> GetNbinsX();
00540
00541 b.assign(nx - 1, 0.0);
00542
00543
00544 for (int ix = 1; ix < nx; ix++)
00545 {
00546 TH1D * h1 = h -> ProjectionY("temphist", ix, ix);
00547
00548 int nprobSum = 1;
00549 double q[1];
00550 double probSum[] = { level };
00551
00552 h1 -> GetQuantiles(nprobSum, q, probSum);
00553
00554 b[ix-1] = q[0];
00555 }
00556
00557 return b;
00558 }
00559
00560
00561
00562 TGraph * BCH2D::GetBandGraph(TH2D * h , double l1, double l2)
00563 {
00564
00565 int nx = h -> GetNbinsX() - 1;
00566
00567 TGraph * g = new TGraph(2*nx);
00568
00569
00570
00571
00572 std::vector <double> ymin = GetLevelBoundary(h,l1);
00573 std::vector <double> ymax = GetLevelBoundary(h,l2);
00574
00575 for (int i = 0; i < nx; i++)
00576 {
00577 g -> SetPoint(i, h -> GetXaxis() -> GetBinCenter(i+1), ymin[i]);
00578 g -> SetPoint(nx+i, h -> GetXaxis() -> GetBinCenter(nx-i), ymax[nx-i-1]);
00579 }
00580
00581 return g;
00582 }
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594