23 #include <TLegendEntry.h>
33 unsigned int BCH2D::fHCounter=0;
38 , fIntegratedHistogram(0)
48 if (fIntegratedHistogram)
49 delete fIntegratedHistogram;
51 for (
unsigned i = 0; i < fROOTObjects.size(); ++i)
52 delete fROOTObjects[i];
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);
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);
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);
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);
102 if (fHistogram and fHistogram->Integral() > 0)
103 fHistogram->Scale(1.0/fHistogram->Integral(
"width"));
107 void BCH2D::Print(
const char * filename, std::string options, std::vector<double> intervals,
int ww,
int wh)
110 bool flag_logz =
false;
111 bool flag_rescale =
false;
114 if (options.find(
"logz") < options.size()) {
118 if (options.find(
"R") < options.size()) {
124 unsigned int cindex = getNextIndex();
126 c =
new TCanvas(TString::Format(
"c_bch2d_%d",cindex), TString::Format(
"c_bch2d_%d",cindex), ww, wh);
128 c =
new TCanvas(TString::Format(
"c_bch2d_%d",cindex));
131 fROOTObjects.push_back(c);
139 Draw(options, intervals);
142 double top = gPad->GetTopMargin();
143 double bottom = gPad->GetBottomMargin();
144 double left = gPad->GetLeftMargin();
145 double right = gPad->GetRightMargin();
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);
163 void BCH2D::Print(
const char* filename, std::string options,
double interval,
int ww,
int wh)
165 std::vector<double> tempvec;
166 tempvec.push_back(interval);
167 Print(filename, options, tempvec, ww, wh);
171 void BCH2D::Draw(std::string options, std::vector<double> intervals)
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;
190 intervals.push_back(0.6827);
193 std::string draw_options =
"COLZ";
196 if (options.find(
"smooth1") < options.size()) {
200 if (options.find(
"smooth3") < options.size()) {
204 if (options.find(
"smooth5") < options.size()) {
208 if (options.find(
"smooth10") < options.size()) {
209 flag_smooth10 =
true;
212 if (options.find(
"nL") < options.size()) {
216 if (options.find(
"BTf") < options.size()) {
219 else if (options.find(
"BTc") < options.size()) {
226 if (options.find(
"profilex") < options.size()) {
227 flag_profilex =
true;
230 if (options.find(
"profiley") < options.size()) {
231 flag_profiley =
true;
234 if (options.find(
"gmode") < options.size()) {
236 flag_mode_global =
true;
239 if (options.find(
"lmode") < options.size()) {
240 flag_mode_local =
true;
243 if (options.find(
"mean") < options.size()) {
247 if (options.find(
"B1") < options.size()) {
249 if (intervals.size() != 1) {
251 intervals.push_back(0.6827);
255 if (options.find(
"B2") < options.size()) {
257 if (intervals.size() != 2) {
259 intervals.push_back(0.6827);
260 intervals.push_back(0.9545);
264 if (options.find(
"B3") < options.size()) {
266 if (intervals.size() != 3) {
268 intervals.push_back(0.6827);
269 intervals.push_back(0.9545);
270 intervals.push_back(0.9973);
274 if (options.find(
"CS0") < options.size()) {
277 else if (options.find(
"CS1") < options.size()) {
280 else if (options.find(
"CS2") < options.size()) {
283 else if (options.find(
"CS3") < options.size()) {
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;
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);
306 fROOTObjects.push_back(legend);
309 TH2D* hist_band =
new TH2D(*fHistogram);
312 fROOTObjects.push_back(hist_band);
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);
325 std::vector<double> levels(nbands+2);
328 std::vector<int> colors(nbands+1);
331 for (
int i = 1; i <= nbands; ++i) {
332 levels[i] =
GetLevel((1.-intervals[nbands-i]));
335 levels[nbands+1] = fIntegratedHistogram->GetXaxis()->GetXmax();
338 hist_band->SetContour(nbands+2, &levels[0]);
341 gStyle->SetPalette(nbands+1, &colors[0]);
344 for (
int i = 0; i < nbands; ++i) {
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);
352 le->SetTextSize(0.03);
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);
360 le->SetTextSize(0.03);
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);
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);
375 double xmean = fHistogram->GetMean(1);
376 double ymean = fHistogram->GetMean(2);
377 double xrms = fHistogram->GetRMS(1);
378 double yrms = fHistogram->GetRMS(2);
380 TMarker* marker_mean =
new TMarker(xmean, ymean, 32);
381 marker_mean->SetMarkerColor(
GetColor(4));
382 marker_mean->SetMarkerSize(1.5);
385 TArrow* arrow_std1 =
new TArrow(xmean-xrms, ymean,
388 arrow_std1->SetLineColor(
GetColor(4));
389 arrow_std1->SetFillColor(
GetColor(4));
391 TArrow* arrow_std2 =
new TArrow(xmean, ymean-yrms,
394 arrow_std2->SetLineColor(
GetColor(4));
395 arrow_std2->SetFillColor(
GetColor(4));
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);
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);
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);
419 TLegendEntry* le = legend->AddEntry(arrow_std1,
"mean and standard deviation",
"PL");
421 le->SetMarkerStyle(32);
422 le->SetMarkerSize(1.5);
426 TGraph* graph_profilex = 0;
427 TGraph* graph_profiley = 0;
430 TLegendEntry* le = legend->AddEntry(graph_profilex,
"profile x",
"L");
435 TLegendEntry* le = legend->AddEntry(graph_profiley,
"profile y",
"L");
440 double height = 0.03*legend->GetNRows();
444 ymax+=(ymax-ymin)*(0.1+height);
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);
456 hist_axes->Draw(
"COL");
460 fHistogram->Smooth(1);
461 fHistogram->Scale(1.0/fHistogram->Integral(
"width"));
464 fHistogram->Smooth(3);
465 fHistogram->Scale(1.0/fHistogram->Integral(
"width"));
468 fHistogram->Smooth(5);
469 fHistogram->Scale(1.0/fHistogram->Integral(
"width"));
472 fHistogram->Smooth(10);
473 fHistogram->Scale(1.0/fHistogram->Integral(
"width"));
478 hist_band->Draw(
"COL SAME");
479 else if (bandtype == 1)
480 hist_band->Draw(
"CONT1 SAME");
487 fROOTObjects.push_back(graph_profilex);
494 fROOTObjects.push_back(graph_profiley);
498 if (flag_mode_global) {
499 marker_mode_global->Draw();
502 if (flag_mode_local) {
503 marker_mode_local->Draw();
513 gPad->SetTopMargin(0.02);
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();
521 legend->SetX1NDC(xlegend1);
522 legend->SetX2NDC(xlegend2);
523 legend->SetY1NDC(ylegend1);
524 legend->SetY2NDC(ylegend2);
532 hist_axes->Draw(
"SAME");
535 gPad->SetTopMargin(1.-ylegend1+0.01);
545 std::vector<double> tempvec;
546 tempvec.push_back(interval);
547 Draw(options, tempvec);
559 fIntegratedHistogram->Draw();
568 double zmin = fHistogram->GetMinimum();
569 double zmax = fHistogram->GetMaximum();
570 double dz = (zmax-zmin);
572 double nx = fHistogram->GetNbinsX();
573 double ny = fHistogram->GetNbinsY();
576 if (fIntegratedHistogram)
577 delete fIntegratedHistogram;
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);
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);
594 fIntegratedHistogram->Scale(1.0/fIntegratedHistogram->GetEntries());
605 fIntegratedHistogram->GetQuantiles( 1, quantiles, probsum);
614 TH2D hist_temp(*fHistogram);
617 int nbins = hist_temp.GetNbinsX() * hist_temp.GetNbinsY();
629 while ( (intp < p) && (counter < nbins) ) {
635 hist_temp.GetBinXYZ(hist_temp.GetMaximumBin(), binx, biny, binz);
638 double dp = hist_temp.GetBinContent(binx, biny);
639 intp += dp * hist_temp.GetXaxis()->GetBinWidth(binx) * hist_temp.GetYaxis()->GetBinWidth(biny);
642 hist_temp.SetBinContent(binx, biny, 0.);
645 area += hist_temp.GetXaxis()->GetBinWidth(binx) * hist_temp.GetYaxis()->GetBinWidth(biny);
658 std::vector<int> nint;
660 int nx = h->GetNbinsX();
661 int ny = h->GetNbinsY();
666 for (
int ix=1; ix<=nx; ix++)
672 for (
int iy=1; iy<=ny; iy++)
673 if(h->GetBinContent(ix,iy)>0.)
675 while(h->GetBinContent(ix,++iy)>0.)
684 nint.push_back(nfound);
694 bool flag_mode =
true;
695 bool flag_mean =
false;
696 bool flag_median =
false;
699 if (options.find(
"mode") < options.size()) {
703 if (options.find(
"mean") < options.size()) {
709 if (options.find(
"median") < options.size()) {
716 TGraph* graph_profile =
new TGraph();
719 int nx = fHistogram->GetNbinsX();
720 int ny = fHistogram->GetNbinsY();
722 double xmin = fHistogram->GetXaxis()->GetBinLowEdge(1);
723 double xmax = fHistogram->GetXaxis()->GetBinLowEdge(nx+1);
725 double ymin = fHistogram->GetYaxis()->GetBinLowEdge(1);
726 double ymax = fHistogram->GetYaxis()->GetBinLowEdge(nx+1);
747 for (
int ibin_outer = 1; ibin_outer <= nbins_outer ; ibin_outer++) {
750 TH1D* hist_temp =
new TH1D(
"",
"", nbins_inner, axis_min, axis_max);
752 for (
int ibin_inner = 1; ibin_inner <= nbins_inner; ibin_inner++) {
763 double content = fHistogram->GetBinContent(ix, iy);
764 hist_temp->SetBinContent(ibin_inner, content);
767 hist_temp->Scale(1.0/hist_temp->Integral());
773 double x = fHistogram->GetXaxis()->GetBinCenter(ibin_outer);
774 double y = fHistogram->GetYaxis()->GetBinCenter(ibin_outer);
782 else if (flag_median)
791 graph_profile->SetPoint(ibin_outer-1, x, y);
798 return graph_profile;
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;
812 if (drawoptions.find(
"black") < options.size()) {
816 else if (drawoptions.find(
"red") < options.size()) {
824 if (drawoptions.find(
"solid") < options.size()) {
828 else if (drawoptions.find(
"dashed") < options.size()) {
832 else if (drawoptions.find(
"dotted") < options.size()) {
845 graph_profile->SetLineColor(kBlack);
848 graph_profile->SetLineColor(kRed);
851 graph_profile->SetLineStyle(1);
854 graph_profile->SetLineStyle(2);
857 graph_profile->SetLineStyle(3);
860 graph_profile->Draw(
"L");
863 return graph_profile;
void Draw(std::string options="BTfB3CS1meangmodelmode", std::vector< double > intervals=std::vector< double >(0))
TGraph * DrawProfileX(std::string options, std::string drawoptions)
double GetLevel(double p)
void Print(const char *filename, std::string options="BTfB3CS1meangmode", std::vector< double > intervals=std::vector< double >(0), int ww=0, int wh=0)
void SetHistogram(TH2D *hist)
std::vector< int > GetNIntervalsY(TH2D *h, int &nfoundmax)
void PrintIntegratedHistogram(const char *filename)
A class for handling 1D distributions.
void CalculateIntegratedHistogram()
TGraph * DrawProfile(int axis, std::string options, std::string drawoptions="blacksolid")
TGraph * CalculateProfileGraph(int axis, std::string options="mode")
void SetColorScheme(int scheme)
TGraph * DrawProfileY(std::string options, std::string drawoptions)