Class Reference for E1039 Core & Analysis Software
UtilHist.cc
Go to the documentation of this file.
1 #include <iomanip>
2 #include <TH1D.h>
3 #include <TH2D.h>
4 #include "UtilHist.h"
5 using namespace std;
6 
7 bool UtilHist::FindFilledRange(TH1* h1, int& bin_lo, int& bin_hi)
8 {
9  bin_lo = bin_hi = 0;
10  if (h1->Integral() == 0) return false;
11  int nn = h1->GetNbinsX();
12  bin_lo = 1;
13  while (h1->GetBinContent(bin_lo) == 0) bin_lo++;
14  bin_hi = nn;
15  while (h1->GetBinContent(bin_hi) == 0) bin_hi--;
16  return true;
17 }
18 
19 bool UtilHist::AutoSetRange(TH1* h1, const int margin_lo, const int margin_hi)
20 {
21  int nn = h1->GetNbinsX();
22  int bin_lo, bin_hi;
23  if (! FindFilledRange(h1, bin_lo, bin_hi)) return false;
24  bin_lo -= margin_lo;
25  bin_hi += margin_hi;
26  if (bin_lo < 1) bin_lo = 1;
27  if (bin_hi > nn) bin_hi = nn;
28  h1->GetXaxis()->SetRange(bin_lo, bin_hi);
29  return true;
30 }
31 
32 bool UtilHist::AutoSetRangeX(TH2* h2, const int margin_lo, const int margin_hi)
33 {
34  TH1* h1 = h2->ProjectionX("h1_auto_set_range_x");
35  int nn = h1->GetNbinsX();
36  int bin_lo, bin_hi;
37  if (! FindFilledRange(h1, bin_lo, bin_hi)) return false;
38  delete h1;
39  bin_lo -= margin_lo;
40  bin_hi += margin_hi;
41  if (bin_lo < 1) bin_lo = 1;
42  if (bin_hi > nn) bin_hi = nn;
43  h2->GetXaxis()->SetRange(bin_lo, bin_hi);
44  return true;
45 }
46 
47 bool UtilHist::AutoSetRangeY(TH2* h2, const int margin_lo, const int margin_hi)
48 {
49  TH1* h1 = h2->ProjectionY("h1_auto_set_range_y");
50  int nn = h1->GetNbinsX();
51  int bin_lo, bin_hi;
52  if (! FindFilledRange(h1, bin_lo, bin_hi)) return false;
53  delete h1;
54  bin_lo -= margin_lo;
55  bin_hi += margin_hi;
56  if (bin_lo < 1) bin_lo = 1;
57  if (bin_hi > nn) bin_hi = nn;
58  h2->GetYaxis()->SetRange(bin_lo, bin_hi);
59  return true;
60 }
void FindFilledRange(TH1 *h1, int &bin_lo, int &bin_hi)
Find the lowest and highest bins ("bin_lo" and "bin_hi") out of non-empty bins of "h1".
Definition: UtilHist.cc:7
void AutoSetRangeY(TH2 *h2, const int margin_lo=5, const int margin_hi=5)
Definition: UtilHist.cc:43
void AutoSetRange(TH1 *h1, const int margin_lo=5, const int margin_hi=5)
Adjust the axis range via "h1->GetXaxis()->SetRange(bin_lo, bin_hi)" to zoom up non-empty bins.
Definition: UtilHist.cc:17
void AutoSetRangeX(TH2 *h2, const int margin_lo=5, const int margin_hi=5)
Definition: UtilHist.cc:29