BAT  0.9.4
The Bayesian analysis toolkit
 All Classes Namespaces Functions Variables Enumerations
BCH2D.cxx
1 /*
2  * Copyright (C) 2007-2014, the BAT core developer team
3  * All rights reserved.
4  *
5  * For the licensing terms see doc/COPYING.
6  * For documentation see http://mpp.mpg.de/bat
7  */
8 
9 // ---------------------------------------------------------
10 
11 #include "BCH2D.h"
12 #include "BCH1D.h"
13 
14 #include "BCLog.h"
15 #include "BCMath.h"
16 
17 #include <TArrow.h>
18 #include <TCanvas.h>
19 #include <TH1D.h>
20 #include <TH2D.h>
21 #include <TGraph.h>
22 #include <TLegend.h>
23 #include <TLegendEntry.h>
24 #include <TMarker.h>
25 #include <TObject.h>
26 #include <TROOT.h>
27 #include <TString.h>
28 #include <TStyle.h>
29 #include <TLine.h>
30 
31 #include <math.h>
32 
33 unsigned int BCH2D::fHCounter=0;
34 
35 // ---------------------------------------------------------
36 BCH2D::BCH2D(TH2D * h)
37  : fHistogram(0)
38  , fIntegratedHistogram(0)
39  , fModeFlag(0)
40 {
41  if (h)
42  SetHistogram(h);
43 }
44 
45 // ---------------------------------------------------------
47 {
48  if (fIntegratedHistogram)
49  delete fIntegratedHistogram;
50 
51  for (unsigned i = 0; i < fROOTObjects.size(); ++i)
52  delete fROOTObjects[i];
53 }
54 
55 // ---------------------------------------------------------
56 void BCH2D::SetColorScheme(int scheme)
57 {
58  fColors.clear();
59 
60  // color numbering
61  // 0,1,2 : intervals
62  // 3 : quantile lines
63  // 4 : mean, mode, median
64 
65  if (scheme == 0) {
66  fColors.push_back(18);
67  fColors.push_back(16);
68  fColors.push_back(14);
69  fColors.push_back(kBlack);
70  fColors.push_back(kBlack);
71  }
72  else if (scheme == 1) {
73  fColors.push_back(kGreen);
74  fColors.push_back(kYellow);
75  fColors.push_back(kRed);
76  fColors.push_back(kBlack);
77  fColors.push_back(kBlack);
78  }
79  else if (scheme == 2) {
80  fColors.push_back(kBlue+4);
81  fColors.push_back(kBlue+2);
82  fColors.push_back(kBlue);
83  fColors.push_back(kOrange);
84  fColors.push_back(kOrange);
85  }
86  else if (scheme == 3) {
87  fColors.push_back(kRed+4);
88  fColors.push_back(kRed+2);
89  fColors.push_back(kRed);
90  fColors.push_back(kGreen);
91  fColors.push_back(kGreen);
92  }
93  else {
94  SetColorScheme(1);
95  }
96 }
97 
98 // ---------------------------------------------------------
99 void BCH2D::SetHistogram(TH2D * hist)
100 {
101  fHistogram = hist;
102  if (fHistogram and fHistogram->Integral() > 0)
103  fHistogram->Scale(1.0/fHistogram->Integral("width"));
104 }
105 
106 // ---------------------------------------------------------
107 void BCH2D::Print(const char * filename, std::string options, std::vector<double> intervals, int ww, int wh)
108 {
109  // option flags
110  bool flag_logz = false;
111  bool flag_rescale = false;
112 
113  // check content of options string
114  if (options.find("logz") < options.size()) {
115  flag_logz = true;
116  }
117 
118  if (options.find("R") < options.size()) {
119  flag_rescale = true;
120  }
121 
122  // create temporary canvas
123  TCanvas * c;
124  unsigned int cindex = getNextIndex();
125  if(ww > 0 && wh > 0)
126  c = new TCanvas(TString::Format("c_bch2d_%d",cindex), TString::Format("c_bch2d_%d",cindex), ww, wh);
127  else
128  c = new TCanvas(TString::Format("c_bch2d_%d",cindex));
129 
130  // add c to list of objects
131  fROOTObjects.push_back(c);
132 
133  // set log axis
134  if (flag_logz) {
135  c->SetLogz();
136  }
137 
138  // draw histogram
139  Draw(options, intervals);
140 
141  if (flag_rescale) {
142  double top = gPad->GetTopMargin();
143  double bottom = gPad->GetBottomMargin();
144  double left = gPad->GetLeftMargin();
145  double right = gPad->GetRightMargin();
146 
147  double dx = 1.-right - left;
148  double dy = 1.-top-bottom;
149  double ratio = dy/dx;
150  double ynew = c->GetWindowWidth()/ratio;
151  c->SetCanvasSize(c->GetWindowWidth(), (int) ynew);
152  gPad->RedrawAxis();
153 
154  c->Modified();
155  c->Update();
156  }
157 
158  // print to file
159  c->Print(filename);
160 }
161 
162 // ---------------------------------------------------------
163 void BCH2D::Print(const char* filename, std::string options, double interval, int ww, int wh)
164 {
165  std::vector<double> tempvec;
166  tempvec.push_back(interval);
167  Print(filename, options, tempvec, ww, wh);
168 }
169 
170 // ---------------------------------------------------------
171 void BCH2D::Draw(std::string options, std::vector<double> intervals)
172 {
173  // option flags
174  bool flag_legend = true;
175  bool flag_mode_global = false;
176  bool flag_mode_local = false;
177  bool flag_mean = false;
178  bool flag_smooth1 = false;
179  bool flag_smooth3 = false;
180  bool flag_smooth5 = false;
181  bool flag_smooth10 = false;
182  bool flag_profilex = false;
183  bool flag_profiley = false;
184 
185  // band type
186  int bandtype = 0;
187 
188  // number of bands
189  int nbands = 1; // number of shaded bands
190  intervals.push_back(0.6827);
191 
192  // define draw options called in TH1D::Draw(...)
193  std::string draw_options = "COLZ";
194 
195  // check content of options string
196  if (options.find("smooth1") < options.size()) {
197  flag_smooth1 = true;
198  }
199 
200  if (options.find("smooth3") < options.size()) {
201  flag_smooth3 = true;
202  }
203 
204  if (options.find("smooth5") < options.size()) {
205  flag_smooth5 = true;
206  }
207 
208  if (options.find("smooth10") < options.size()) {
209  flag_smooth10 = true;
210  }
211 
212  if (options.find("nL") < options.size()) {
213  flag_legend = false;
214  }
215 
216  if (options.find("BTf") < options.size()) {
217  bandtype = 0;
218  }
219  else if (options.find("BTc") < options.size()) {
220  bandtype = 1;
221  }
222  else {
223  bandtype = 0;
224  }
225 
226  if (options.find("profilex") < options.size()) {
227  flag_profilex = true;
228  }
229 
230  if (options.find("profiley") < options.size()) {
231  flag_profiley = true;
232  }
233 
234  if (options.find("gmode") < options.size()) {
235  if (fModeFlag)
236  flag_mode_global = true;
237  }
238 
239  if (options.find("lmode") < options.size()) {
240  flag_mode_local = true;
241  }
242 
243  if (options.find("mean") < options.size()) {
244  flag_mean = true;
245  }
246 
247  if (options.find("B1") < options.size()) {
248  nbands = 1;
249  if (intervals.size() != 1) {
250  intervals.clear();
251  intervals.push_back(0.6827);
252  }
253  }
254 
255  if (options.find("B2") < options.size()) {
256  nbands = 2;
257  if (intervals.size() != 2) {
258  intervals.clear();
259  intervals.push_back(0.6827);
260  intervals.push_back(0.9545);
261  }
262  }
263 
264  if (options.find("B3") < options.size()) {
265  nbands = 3;
266  if (intervals.size() != 3) {
267  intervals.clear();
268  intervals.push_back(0.6827);
269  intervals.push_back(0.9545);
270  intervals.push_back(0.9973);
271  }
272  }
273 
274  if (options.find("CS0") < options.size()) {
275  SetColorScheme(0);
276  }
277  else if (options.find("CS1") < options.size()) {
278  SetColorScheme(1);
279  }
280  else if (options.find("CS2") < options.size()) {
281  SetColorScheme(2);
282  }
283  else if (options.find("CS3") < options.size()) {
284  SetColorScheme(3);
285  }
286  else {
287  SetColorScheme(1);
288  }
289 
290  // prepare size of histogram
291  double xmin = fHistogram->GetXaxis()->GetXmin();
292  double xmax = fHistogram->GetXaxis()->GetXmax();
293  double ymin = fHistogram->GetYaxis()->GetXmin();
294  double ymaxhist = fHistogram->GetYaxis()->GetXmax();
295  double ymax = ymaxhist;
296 
297  // prepare legend
298  TLegend* legend = new TLegend();
299  legend->SetBorderSize(0);
300  legend->SetFillColor(kWhite);
301  legend->SetTextAlign(12);
302  legend->SetTextFont(62);
303  legend->SetTextSize(0.03);
304 
305  // add legend to list of objects
306  fROOTObjects.push_back(legend);
307 
308  // copy histograms for bands
309  TH2D* hist_band = new TH2D(*fHistogram);
310 
311  // add hist_band to list of ROOT objects
312  fROOTObjects.push_back(hist_band);
313 
314  // calculate integrated histogram
316 
317  for (int ix = 1; ix <= fHistogram->GetNbinsX(); ++ix) {
318  for (int iy = 1; iy <= fHistogram->GetNbinsY(); ++iy) {
319  double p = fHistogram->GetBinContent(ix, iy);
320  hist_band->SetBinContent(ix, iy, p);
321  }
322  }
323 
324  // define levels and colors
325  std::vector<double> levels(nbands+2);
326  levels[0] = 0.;
327 
328  std::vector<int> colors(nbands+1);
329  colors[0] = kWhite;
330 
331  for (int i = 1; i <= nbands; ++i) {
332  levels[i] = GetLevel((1.-intervals[nbands-i]));
333  colors[i] = GetColor(nbands-i);
334  }
335  levels[nbands+1] = fIntegratedHistogram->GetXaxis()->GetXmax();
336 
337  // set contour
338  hist_band->SetContour(nbands+2, &levels[0]);
339 
340  // set colors
341  gStyle->SetPalette(nbands+1, &colors[0]);
342 
343  // add hist_band to legend
344  for (int i = 0; i < nbands; ++i) {
345  if (bandtype == 0) {
346  TLegendEntry* le = legend->AddEntry((TObject*)0, Form("smallest %.1f%% interval(s)", intervals[nbands-1-i]*100), "F");
347  le->SetFillColor(GetColor(nbands-1-i));
348  le->SetFillStyle(1001);
349  le->SetLineColor(GetColor(nbands-1-i));
350  le->SetTextAlign(12);
351  le->SetTextFont(62);
352  le->SetTextSize(0.03);
353  }
354  else if (bandtype == 1) {
355  TLegendEntry* le = legend->AddEntry((TObject*)0, Form("smallest %.1f%% interval(s)", intervals[nbands-1-i]*100), "F");
356  fROOTObjects.push_back(le);
357  le->SetLineColor(GetColor(nbands-1-i));
358  le->SetTextAlign(12);
359  le->SetTextFont(62);
360  le->SetTextSize(0.03);
361  }
362  }
363 
364  // mean, mode, median
365  TMarker* marker_mode_global = new TMarker(fMode[0], fMode[1], 24);
366  marker_mode_global->SetMarkerColor(GetColor(4));
367  marker_mode_global->SetMarkerSize(1.5);
368 
369  int binx, biny, binz;
370  fHistogram->GetBinXYZ( fHistogram->GetMaximumBin(), binx, biny, binz);
371  TMarker* marker_mode_local = new TMarker(fHistogram->GetXaxis()->GetBinCenter(binx), fHistogram->GetYaxis()->GetBinCenter(biny), 25);
372  marker_mode_local->SetMarkerColor(GetColor(4));
373  marker_mode_local->SetMarkerSize(1.5);
374 
375  double xmean = fHistogram->GetMean(1);
376  double ymean = fHistogram->GetMean(2);
377  double xrms = fHistogram->GetRMS(1);
378  double yrms = fHistogram->GetRMS(2);
379 
380  TMarker* marker_mean = new TMarker(xmean, ymean, 32);
381  marker_mean->SetMarkerColor(GetColor(4));
382  marker_mean->SetMarkerSize(1.5);
383 
384  // standard deviation
385  TArrow* arrow_std1 = new TArrow(xmean-xrms, ymean,
386  xmean+xrms, ymean,
387  0.02, "<|>");
388  arrow_std1->SetLineColor(GetColor(4));
389  arrow_std1->SetFillColor(GetColor(4));
390 
391  TArrow* arrow_std2 = new TArrow(xmean, ymean-yrms,
392  xmean, ymean+yrms,
393  0.02, "<|>");
394  arrow_std2->SetLineColor(GetColor(4));
395  arrow_std2->SetFillColor(GetColor(4));
396 
397  // add marker_mean and arrow_std to list of ROOT objects
398  fROOTObjects.push_back(marker_mean);
399  fROOTObjects.push_back(marker_mode_global);
400  fROOTObjects.push_back(marker_mode_local);
401  fROOTObjects.push_back(arrow_std1);
402  fROOTObjects.push_back(arrow_std2);
403 
404  if (flag_mode_global) {
405  TLegendEntry* le = legend->AddEntry(marker_mode_global, "global mode", "P");
406  le->SetMarkerStyle(24);
407  le->SetMarkerSize(1.5);
408  le->SetMarkerColor(GetColor(4));
409  }
410 
411  if (flag_mode_local) {
412  TLegendEntry* le = legend->AddEntry(marker_mode_local, "local mode", "P");
413  le->SetMarkerStyle(25);
414  le->SetMarkerSize(1.5);
415  le->SetMarkerColor(GetColor(4));
416  }
417 
418  if (flag_mean) {
419  TLegendEntry* le = legend->AddEntry(arrow_std1, "mean and standard deviation", "PL");
420  le->SetLineColor(GetColor(4));
421  le->SetMarkerStyle(32);
422  le->SetMarkerSize(1.5);
423  le->SetMarkerColor(GetColor(4));
424  }
425 
426  TGraph* graph_profilex = 0;
427  TGraph* graph_profiley = 0;
428 
429  if (flag_profilex) {
430  TLegendEntry* le = legend->AddEntry(graph_profilex, "profile x", "L");
431  le->SetLineStyle(2);
432  }
433 
434  if (flag_profiley) {
435  TLegendEntry* le = legend->AddEntry(graph_profiley, "profile y", "L");
436  le->SetLineStyle(3);
437  }
438 
439  // calculate legend height in NDC coordinates
440  double height = 0.03*legend->GetNRows();
441 
442  // make room for legend
443  if (flag_legend)
444  ymax+=(ymax-ymin)*(0.1+height);
445 
446  double deltax = 0.0015*(xmax - xmin);
447  double deltay = 0.0015*(ymaxhist - ymin);
448  TH2D* hist_axes = new TH2D("", "", 1, xmin-deltax, xmax+deltax, 1, ymin-deltay, ymaxhist+deltay);
449  hist_axes->SetXTitle(fHistogram->GetXaxis()->GetTitle());
450  hist_axes->SetYTitle(fHistogram->GetYaxis()->GetTitle());
451  hist_axes->SetLineWidth(fHistogram->GetLineWidth());
452  hist_axes->SetStats(kFALSE);
453  fROOTObjects.push_back(hist_axes);
454 
455  // draw axes
456  hist_axes->Draw("COL");
457 
458  // smooth
459  if (flag_smooth1) {
460  fHistogram->Smooth(1);
461  fHistogram->Scale(1.0/fHistogram->Integral("width"));
462  }
463  if (flag_smooth3) {
464  fHistogram->Smooth(3);
465  fHistogram->Scale(1.0/fHistogram->Integral("width"));
466  }
467  if (flag_smooth5) {
468  fHistogram->Smooth(5);
469  fHistogram->Scale(1.0/fHistogram->Integral("width"));
470  }
471  if (flag_smooth10) {
472  fHistogram->Smooth(10);
473  fHistogram->Scale(1.0/fHistogram->Integral("width"));
474  }
475 
476  // draw histogram
477  if (bandtype == 0)
478  hist_band->Draw("COL SAME");
479  else if (bandtype == 1)
480  hist_band->Draw("CONT1 SAME");
481 
482  // draw profiles
483  if (flag_profilex) {
484  graph_profilex = DrawProfileX("mode", "dashed");
485 
486  // add graph to list of objects
487  fROOTObjects.push_back(graph_profilex);
488  }
489 
490  if (flag_profiley) {
491  graph_profiley = DrawProfileY("mode", "dotted");
492 
493  // add graph to list of objects
494  fROOTObjects.push_back(graph_profiley);
495  }
496 
497  // mean, mode, median
498  if (flag_mode_global) {
499  marker_mode_global->Draw();
500  }
501 
502  if (flag_mode_local) {
503  marker_mode_local->Draw();
504  }
505 
506  if (flag_mean) {
507  arrow_std1->Draw();
508  arrow_std2->Draw();
509  marker_mean->Draw();
510  }
511 
512  if (flag_legend)
513  gPad->SetTopMargin(0.02);
514 
515  double xlegend1 = gPad->GetLeftMargin();
516  double xlegend2 = 1.0-gPad->GetRightMargin();
517  double ylegend1 = 1.-gPad->GetTopMargin()-height;
518  double ylegend2 = 1.-gPad->GetTopMargin();
519 
520  // place legend on top of histogram
521  legend->SetX1NDC(xlegend1);
522  legend->SetX2NDC(xlegend2);
523  legend->SetY1NDC(ylegend1);
524  legend->SetY2NDC(ylegend2);
525 
526  // draw legend
527  if (flag_legend) {
528  legend->Draw();
529  }
530 
531  // draw axes again
532  hist_axes->Draw("SAME");
533 
534  // rescale
535  gPad->SetTopMargin(1.-ylegend1+0.01);
536 
537  gPad->RedrawAxis();
538 
539  return;
540 }
541 
542 // ---------------------------------------------------------
543 void BCH2D::Draw(std::string options, double interval)
544 {
545  std::vector<double> tempvec;
546  tempvec.push_back(interval);
547  Draw(options, tempvec);
548 }
549 
550 // ---------------------------------------------------------
551 void BCH2D::PrintIntegratedHistogram(const char* filename)
552 {
553  TCanvas c;
554  c.Flush();
555 
556  c.cd();
557  c.SetLogy();
559  fIntegratedHistogram->Draw();
560  c.Print(filename);
561 }
562 
563 // ---------------------------------------------------------
565 {
566  int nz = 100;
567 
568  double zmin = fHistogram->GetMinimum();
569  double zmax = fHistogram->GetMaximum();
570  double dz = (zmax-zmin);
571 
572  double nx = fHistogram->GetNbinsX();
573  double ny = fHistogram->GetNbinsY();
574 
575  // create histogram
576  if (fIntegratedHistogram)
577  delete fIntegratedHistogram;
578 
579  fIntegratedHistogram = new TH1D(
580  TString::Format("%s_int_prob_%d",fHistogram->GetName(),BCLog::GetHIndex()), "", nz, zmin, zmax+0.05*dz);
581  fIntegratedHistogram->SetXTitle("z");
582  fIntegratedHistogram->SetYTitle("Integrated probability");
583  fIntegratedHistogram->SetStats(kFALSE);
584 
585  // loop over histogram
586  for (int ix = 1; ix <= nx; ix++) {
587  for (int iy = 1; iy <= ny; iy++) {
588  double p = fHistogram->GetBinContent(ix, iy);
589  fIntegratedHistogram->Fill(p, p);
590  }
591  }
592 
593  // normalize histogram
594  fIntegratedHistogram->Scale(1.0/fIntegratedHistogram->GetEntries());
595 
596 }
597 
598 // ---------------------------------------------------------
599 double BCH2D::GetLevel(double p)
600 {
601  double quantiles[1];
602  double probsum[1];
603  probsum[0] = p;
604 
605  fIntegratedHistogram->GetQuantiles( 1, quantiles, probsum);
606 
607  return quantiles[0];
608 }
609 
610 // ---------------------------------------------------------
611 double BCH2D::GetArea(double p)
612 {
613  // copy histograms for bands
614  TH2D hist_temp(*fHistogram);
615 
616  // calculate total number of bins
617  int nbins = hist_temp.GetNbinsX() * hist_temp.GetNbinsY();
618 
619  // area
620  double area = 0;
621 
622  // integrated probability
623  double intp = 0;
624 
625  // a counter
626  int counter = 0;
627 
628  // loop over bins
629  while ( (intp < p) && (counter < nbins) ) {
630 
631  // find maximum bin
632  int binx;
633  int biny;
634  int binz;
635  hist_temp.GetBinXYZ(hist_temp.GetMaximumBin(), binx, biny, binz);
636 
637  // increase probability
638  double dp = hist_temp.GetBinContent(binx, biny);
639  intp += dp * hist_temp.GetXaxis()->GetBinWidth(binx) * hist_temp.GetYaxis()->GetBinWidth(biny);
640 
641  // reset maximum bin
642  hist_temp.SetBinContent(binx, biny, 0.);
643 
644  // increase area
645  area += hist_temp.GetXaxis()->GetBinWidth(binx) * hist_temp.GetYaxis()->GetBinWidth(biny);
646 
647  // increase counter
648  counter++;
649  }
650 
651  // return area
652  return area;
653 }
654 
655 // ---------------------------------------------------------
656 std::vector<int> BCH2D::GetNIntervalsY(TH2D * h, int &nfoundmax)
657 {
658  std::vector<int> nint;
659 
660  int nx = h->GetNbinsX();
661  int ny = h->GetNbinsY();
662 
663  nfoundmax=0;
664 
665  // loop over histogram bins in x
666  for (int ix=1; ix<=nx; ix++)
667  {
668  int nfound=0;
669 
670  // loop over histogram bins in y
671  // count nonzero intervals in y
672  for (int iy=1; iy<=ny; iy++)
673  if(h->GetBinContent(ix,iy)>0.)
674  {
675  while(h->GetBinContent(ix,++iy)>0.)
676  ;
677  nfound++;
678  }
679 
680  // store maximum number of nonzero intervals for the histogram
681  if(nfound>nfoundmax)
682  nfoundmax=nfound;
683 
684  nint.push_back(nfound);
685  }
686 
687  return nint;
688 }
689 
690 // ---------------------------------------------------------
691 TGraph* BCH2D::CalculateProfileGraph(int axis, std::string options)
692 {
693  // option flags
694  bool flag_mode = true;
695  bool flag_mean = false;
696  bool flag_median = false;
697 
698  // check content of options string
699  if (options.find("mode") < options.size()) {
700  flag_mode = true;
701  }
702 
703  if (options.find("mean") < options.size()) {
704  flag_mean = true;
705  flag_mode = false;
706  flag_median = false;
707  }
708 
709  if (options.find("median") < options.size()) {
710  flag_median = true;
711  flag_mode = false;
712  flag_mean = false;
713  }
714 
715  // define profile graph
716  TGraph* graph_profile = new TGraph();
717 
718  // define limits of running
719  int nx = fHistogram->GetNbinsX();
720  int ny = fHistogram->GetNbinsY();
721 
722  double xmin = fHistogram->GetXaxis()->GetBinLowEdge(1);
723  double xmax = fHistogram->GetXaxis()->GetBinLowEdge(nx+1);
724 
725  double ymin = fHistogram->GetYaxis()->GetBinLowEdge(1);
726  double ymax = fHistogram->GetYaxis()->GetBinLowEdge(nx+1);
727 
728  int nbins_outer = 0;
729  int nbins_inner = 0;
730  double axis_min = 0;
731  double axis_max = 0;
732 
733  if (axis==0) {
734  nbins_outer = nx;
735  nbins_inner = ny;
736  axis_min = ymin;
737  axis_max = ymax;
738  }
739  else {
740  nbins_outer = ny;
741  nbins_inner = nx;
742  axis_min = xmin;
743  axis_max = xmax;
744  }
745 
746  // loop over outer axis of choice
747  for (int ibin_outer = 1; ibin_outer <= nbins_outer ; ibin_outer++) {
748 
749  // copy slice at a fixed value into a 1D histogram
750  TH1D* hist_temp = new TH1D("", "", nbins_inner, axis_min, axis_max);
751 
752  for (int ibin_inner = 1; ibin_inner <= nbins_inner; ibin_inner++) {
753  int ix = 0;
754  int iy = 0;
755  if (axis == 0) {
756  ix = ibin_outer;
757  iy = ibin_inner;
758  }
759  else {
760  ix = ibin_inner;
761  iy = ibin_outer;
762  }
763  double content = fHistogram->GetBinContent(ix, iy);
764  hist_temp->SetBinContent(ibin_inner, content);
765  }
766  // normalize to unity
767  hist_temp->Scale(1.0/hist_temp->Integral());
768 
769  // create BAT histogram
770  BCH1D* bchist_temp = new BCH1D(hist_temp);
771 
772  // calculate (x,y) of new point
773  double x = fHistogram->GetXaxis()->GetBinCenter(ibin_outer);
774  double y = fHistogram->GetYaxis()->GetBinCenter(ibin_outer);
775 
776  double temp = 0;
777 
778  if (flag_mode)
779  temp = bchist_temp->GetMode();
780  else if (flag_mean)
781  temp = bchist_temp->GetMean();
782  else if (flag_median)
783  temp = bchist_temp->GetMedian();
784 
785  if (axis == 0)
786  y = temp;
787  else
788  x = temp;
789 
790  // add new point to graph
791  graph_profile->SetPoint(ibin_outer-1, x, y);
792 
793  // clean up
794  delete bchist_temp;
795  }
796 
797  // return the graph
798  return graph_profile;
799 }
800 
801 // ---------------------------------------------------------
802 TGraph* BCH2D::DrawProfile(int axis, std::string options, std::string drawoptions)
803 {
804  // option flags
805  bool flag_black = false;
806  bool flag_red = false;
807  bool flag_solid = false;
808  bool flag_dashed = false;
809  bool flag_dotted = false;
810 
811  // check content of options string
812  if (drawoptions.find("black") < options.size()) {
813  flag_black = true;
814  }
815 
816  else if (drawoptions.find("red") < options.size()) {
817  flag_red = true;
818  }
819 
820  else {
821  flag_black = true;
822  }
823 
824  if (drawoptions.find("solid") < options.size()) {
825  flag_solid = true;
826  }
827 
828  else if (drawoptions.find("dashed") < options.size()) {
829  flag_dashed = true;
830  }
831 
832  else if (drawoptions.find("dotted") < options.size()) {
833  flag_dotted = true;
834  }
835 
836  else {
837  flag_solid = true;
838  }
839 
840  // get the profile graph
841  TGraph* graph_profile = CalculateProfileGraph(axis, options);
842 
843  // change drawing options
844  if (flag_black)
845  graph_profile->SetLineColor(kBlack);
846 
847  if (flag_red)
848  graph_profile->SetLineColor(kRed);
849 
850  if (flag_solid)
851  graph_profile->SetLineStyle(1);
852 
853  if (flag_dashed)
854  graph_profile->SetLineStyle(2);
855 
856  if (flag_dotted)
857  graph_profile->SetLineStyle(3);
858 
859  // draw
860  graph_profile->Draw("L");
861 
862  // return graph
863  return graph_profile;
864 }
865 
866 // ---------------------------------------------------------
867 /*
868 TGraph ** BCH2D::GetBandGraphs(TH2D * h, int &n)
869 {
870  n=0;
871 
872  int nbands=0;
873  TH2D * hcopy = (TH2D*)h->Clone(TString::Format("%s_copy_%d",h->GetName(),BCLog::GetHIndex()));
874 
875  std::vector<int> nint=GetNIntervalsY(hcopy,nbands);
876 
877  if(nbands>2)
878  {
879  BCLog::OutError(
880  Form("BCH2D::GetBandGraphs : Detected %d bands. Maximum allowed is 2 (sorry).",nbands));
881  return 0;
882  }
883  else if(nbands==0)
884  {
885  BCLog::OutError("BCH2D::GetBandGraphs : No bands detected.");
886  return 0;
887  }
888 
889  TGraph ** gxx = new TGraph*[nbands];
890 
891  TH2D * h0 = static_cast<TH2D *>(h->Clone());
892 
893  if (nbands>0)
894  gxx[0] = GetLowestBandGraph(h0,nint);
895 
896  if (nbands==2)
897  {
898  gxx[1] = GetLowestBandGraph(h0);
899  n=2;
900  }
901  else
902  n=1;
903 
904  fROOTObjects.push_back(h0);
905 
906  return gxx;
907 }
908 */
909 
910 // ---------------------------------------------------------
911 
912 /*
913 TGraph * BCH2D::GetLowestBandGraph(TH2D * h)
914 {
915  int n;
916  return GetLowestBandGraph(h,GetNIntervalsY(h,n));
917 }
918 
919 // ---------------------------------------------------------
920 
921 TGraph * BCH2D::GetLowestBandGraph(TH2D * h, std::vector<int> nint)
922 {
923  int nx = h->GetNbinsX() - 1;
924  int ny = h->GetNbinsY();
925 
926  TGraph * g = new TGraph(2*nx);
927 
928  for (int ix=1; ix<=nx; ix++)
929  {
930  // get x for the bin
931  double x;
932  if(ix==1)
933  x = h->GetXaxis()->GetBinLowEdge(1);
934  else if(ix==nx)
935  x = h->GetXaxis()->GetBinLowEdge(nx+1);
936  else
937  x = h->GetXaxis()->GetBinCenter(ix);
938 
939  for(int iy=1; iy<=ny; iy++)
940  if(h->GetBinContent(ix,iy)>0.)
941  {
942  // get low edge of the first not empty bin in y
943  g->SetPoint(ix-1, x, h->GetYaxis()->GetBinLowEdge(iy));
944 
945  // delete content of all subsequent not empty bins
946  if(nint[ix-1]==2)
947  h->SetBinContent(ix,iy,0.);
948 
949  while(h->GetBinContent(ix,++iy)>0.)
950  if(nint[ix-1]==2)
951  h->SetBinContent(ix,iy,0.);
952 
953  // get low edge of the first empty bin in y
954  g->SetPoint(2*nx-ix, x, h->GetYaxis()->GetBinLowEdge(iy));
955 
956  break;
957  }
958  }
959 
960  return g;
961 }
962 
963 // ---------------------------------------------------------
964 
965 std::vector<double> BCH2D::GetLevelBoundary(double level)
966 {
967  return GetLevelBoundary(fHistogram, level);
968 }
969 
970 // ---------------------------------------------------------
971 
972 std::vector<double> BCH2D::GetLevelBoundary(TH2D * h, double level)
973 {
974  std::vector<double> b;
975 
976  int nx = h->GetNbinsX();
977 
978  b.assign(nx - 1, 0.0);
979 
980  // loop over x and y bins.
981  for (int ix = 1; ix < nx; ix++)
982  {
983  TH1D * h1 = h->ProjectionY(TString::Format("temphist_%d",BCLog::GetHIndex()), ix, ix);
984 
985  int nprobSum = 1;
986  double q[1];
987  double probSum[] = { level };
988 
989  h1->GetQuantiles(nprobSum, q, probSum);
990 
991  b[ix-1] = q[0];
992  }
993 
994  return b;
995 }
996 
997 // ---------------------------------------------------------
998 
999 TGraph * BCH2D::GetBandGraph(double l1, double l2)
1000 {
1001  return GetBandGraph(fHistogram , l1, l2);
1002 }
1003 
1004 // ---------------------------------------------------------
1005 
1006 TGraph * BCH2D::GetBandGraph(TH2D * h , double l1, double l2)
1007 {
1008  // define new graph
1009  int nx = h->GetNbinsX() - 1;
1010 
1011  TGraph * g = new TGraph(2*nx);
1012 
1013  // get error bands
1014  std::vector<double> ymin = GetLevelBoundary(h,l1);
1015  std::vector<double> ymax = GetLevelBoundary(h,l2);
1016 
1017  for (int i = 0; i < nx; i++)
1018  {
1019  g->SetPoint(i, h->GetXaxis()->GetBinCenter(i+1), ymin[i]);
1020  g->SetPoint(nx+i, h->GetXaxis()->GetBinCenter(nx-i), ymax[nx-i-1]);
1021  }
1022 
1023  return g;
1024 }
1025 
1026 */
void Draw(std::string options="BTfB3CS1meangmodelmode", std::vector< double > intervals=std::vector< double >(0))
Definition: BCH2D.cxx:171
TGraph * DrawProfileX(std::string options, std::string drawoptions)
Definition: BCH2D.h:218
~BCH2D()
Definition: BCH2D.cxx:46
double GetArea(double p)
Definition: BCH2D.cxx:611
double GetLevel(double p)
Definition: BCH2D.cxx:599
double GetMean()
Definition: BCH1D.h:58
void Print(const char *filename, std::string options="BTfB3CS1meangmode", std::vector< double > intervals=std::vector< double >(0), int ww=0, int wh=0)
Definition: BCH2D.cxx:107
static int GetHIndex()
Definition: BCLog.h:164
void SetHistogram(TH2D *hist)
Definition: BCH2D.cxx:99
double GetMedian()
Definition: BCH1D.h:67
std::vector< int > GetNIntervalsY(TH2D *h, int &nfoundmax)
Definition: BCH2D.cxx:656
double GetMode()
Definition: BCH1D.cxx:52
int GetColor(int index)
Definition: BCH2D.h:67
void PrintIntegratedHistogram(const char *filename)
Definition: BCH2D.cxx:551
A class for handling 1D distributions.
Definition: BCH1D.h:31
void CalculateIntegratedHistogram()
Definition: BCH2D.cxx:564
BCH2D(TH2D *h=0)
Definition: BCH2D.cxx:36
TGraph * DrawProfile(int axis, std::string options, std::string drawoptions="blacksolid")
Definition: BCH2D.cxx:802
TGraph * CalculateProfileGraph(int axis, std::string options="mode")
Definition: BCH2D.cxx:691
void SetColorScheme(int scheme)
Definition: BCH2D.cxx:56
TGraph * DrawProfileY(std::string options, std::string drawoptions)
Definition: BCH2D.h:226