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