Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
THLimitsFinder.cxx
Go to the documentation of this file.
1// @(#)root/hist:$Id$
2// Author: Rene Brun 14/01/2002
3/*************************************************************************
4 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
5 * All rights reserved. *
6 * *
7 * For the licensing terms see $ROOTSYS/LICENSE. *
8 * For the list of contributors see $ROOTSYS/README/CREDITS. *
9 *************************************************************************/
10
11#include "TH1.h"
12#include "TMath.h"
13#include "THLimitsFinder.h"
14
15#include <algorithm>
16#include <limits>
17
19
20
21/** \class THLimitsFinder
22 \ingroup Histograms
23Class to compute nice axis limits.
24
25This class is called by default by the histogramming system
26and also by TTree::Draw, TTreePlayer::DrawSelect. TGaxis use it also to
27optimize axis labels.
28
29A different finder may be specified via THLimitsFinder::SetFinder.
30*/
31
32////////////////////////////////////////////////////////////////////////////////
33
37
38////////////////////////////////////////////////////////////////////////////////
39
43
44
45////////////////////////////////////////////////////////////////////////////////
46/// Compute the best axis limits for the X axis.
47///
48/// If the bit kIsInteger is set, the number of channels is also recomputed.
49/// The axis parameters are replaced by the optimized parameters.
50///
51/// Example:
52/// With the input parameters xmin=-1.467 and xmax=2.344, the function
53/// will compute better limits -1.8 and 2.7 and store them in the axis.
54
56{
57 Int_t newbins;
58 TAxis *xaxis = h->GetXaxis();
59
60 if (xmin >= xmax) {
61 if (xaxis->GetLabels()) {xmin = 0; xmax = xmin +xaxis->GetNbins();}
62 else {xmin -= 1; xmax += 1;}
63 }
64
66 newbins,xmin,xmax,
68
69 h->SetBins(newbins,xmin,xmax);
70
71 return 0;
72}
73
74////////////////////////////////////////////////////////////////////////////////
75/// Compute the best axis limits for the X and Y axis
76/// if the corresponding `newbins` variable is set to 0, i.e. the default value.
77///
78/// If the bit kIsInteger is set, the number of channels is also recomputed.
79/// The axis parameters are replaced by the optimized parameters
80
82 Int_t newbinsx, Int_t newbinsy)
83{
84 TAxis *xaxis = h->GetXaxis();
85 TAxis *yaxis = h->GetYaxis();
86
87 const Bool_t xbinAuto = newbinsx == 0;
88 const Bool_t ybinAuto = newbinsy == 0;
89
90 if (xbinAuto && xmin >= xmax) {
91 if (xaxis->GetLabels()) {xmin = 0; xmax = xmin +xaxis->GetNbins();}
92 else {xmin -= 1; xmax += 1;}
93 }
94 if (ybinAuto && ymin >= ymax) {
95 if (yaxis->GetLabels()) {ymin = 0; ymax = ymin +yaxis->GetNbins();}
96 else {ymin -= 1; ymax += 1;}
97 }
98
99 if (xbinAuto)
101 newbinsx,xmin,xmax,
102 xaxis->TestBit(TAxis::kIsInteger));
103 if (ybinAuto)
105 newbinsy,ymin,ymax,
106 yaxis->TestBit(TAxis::kIsInteger));
107
108 h->SetBins(newbinsx,xmin,xmax,newbinsy,ymin,ymax);
109 return 0;
110}
111
112////////////////////////////////////////////////////////////////////////////////
113/// Compute the best axis limits and bins for the X, Y and Z axis
114/// if the corresponding `newbins` variable is set to 0, i.e. the default value.
115///
116/// If the bit kIsInteger is set, the number of channels is also recomputed.
117/// The axis parameters are replaced by the optimized parameters
118
120 Double_t zmin, Double_t zmax, Int_t newbinsx, Int_t newbinsy, Int_t newbinsz)
121{
122 TAxis *xaxis = h->GetXaxis();
123 TAxis *yaxis = h->GetYaxis();
124 TAxis *zaxis = h->GetZaxis();
125
126 const Bool_t xbinAuto = newbinsx == 0;
127 const Bool_t ybinAuto = newbinsy == 0;
128 const Bool_t zbinAuto = newbinsz == 0;
129
130 if (xbinAuto && xmin >= xmax) {
131 if (xaxis->GetLabels()) {xmin = 0; xmax = xmin +xaxis->GetNbins();}
132 else {xmin -= 1; xmax += 1;}
133 }
134 if (ybinAuto && ymin >= ymax) {
135 if (yaxis->GetLabels()) {ymin = 0; ymax = ymin +yaxis->GetNbins();}
136 else {ymin -= 1; ymax += 1;}
137 }
138 if (zbinAuto && zmin >= zmax) {
139 if (zaxis->GetLabels()) {zmin = 0; zmax = zmin +zaxis->GetNbins();}
140 else {zmin -= 1; zmax += 1;}
141 }
142
143 if (xbinAuto)
145 newbinsx,xmin,xmax,
146 xaxis->TestBit(TAxis::kIsInteger));
147 if (ybinAuto)
149 newbinsy,ymin,ymax,
150 yaxis->TestBit(TAxis::kIsInteger));
151 if (zbinAuto)
153 newbinsz,zmin,zmax,
154 zaxis->TestBit(TAxis::kIsInteger));
155
156 h->SetBins(newbinsx,xmin,xmax,newbinsy,ymin,ymax,newbinsz,zmin,zmax);
157 return 0;
158}
159
160////////////////////////////////////////////////////////////////////////////////
161/// Return pointer to the current finder.
162///
163/// Create one if none exists
164/// Use SetLimitsFinder to set a user defined finder.
165
171
172////////////////////////////////////////////////////////////////////////////////
173/// This static function can be used to specify a finder derived from THLimitsFinder.
174///
175/// The finder may redefine the functions FindGoodLimits*.
176/// Note that the redefined functions may call THLimitsFinder::FindGoodLimits*.
177
182
183////////////////////////////////////////////////////////////////////////////////
184/// Static function to compute reasonable axis limits
185///
186/// Input parameters:
187///
188/// \param[in] A1,A2 : Original axis limits
189/// \param[in] BinLow,BinHigh : Optimized axis limits. They should be initialized by the
190/// calling method for instance to 0.
191/// \param[out] nold : Original number of divisions.
192/// \param[out] nbins : Optimized number of divisions.
193/// \param[out] BinWidth : Optimized bin width. It should be initialized by the
194/// calling method for instance to 0.
195/// \param[in] option : "T" means Time axis.
196
198 Double_t &BinLow, Double_t &BinHigh,
199 Int_t &nbins, Double_t &BinWidth,
201{
202 Int_t lwid, kwid;
203 Int_t ntemp = 0;
204 Int_t jlog = 0;
205 Double_t siground = 0;
206 Double_t alb, awidth, sigfig;
207 Double_t timemulti = 1;
208 Int_t roundmode =0;
209
210 Int_t optionTime;
211 if(strchr(option,'t')) optionTime = 1; else optionTime = 0;
212
213 nbins = nold;
214
215 Double_t al = TMath::Min(A1,A2);
216 Double_t ah = TMath::Max(A1,A2);
217 if (al == ah) ah = al+1;
218 // if nold == -1 , program uses binwidth input from calling routine
219 if (nold == -1 && BinWidth > 0 ) goto L90;
220 ntemp = TMath::Max(nold,2);
221 if (ntemp < 1) ntemp = 1;
222
223L20:
224 awidth = (ah-al)/Double_t(ntemp);
225 timemulti = 1;
226 if (awidth >= std::numeric_limits<float>::max())
227 goto LOK;
228 if (awidth <= 0) goto LOK;
229
230// If time representation, bin width should be rounded to seconds
231// minutes, hours or days
232
233 if (optionTime && awidth>=60) { // if width in seconds, treat it as normal
234 // width in minutes
235 awidth /= 60; timemulti *=60;
236 roundmode = 1; // round minutes (60)
237 // width in hours ?
238 if (awidth>=60) {
239 awidth /= 60; timemulti *= 60;
240 roundmode = 2; // round hours (24)
241 // width in days ?
242 if (awidth>=24) {
243 awidth /= 24; timemulti *= 24;
244 roundmode = 3; // round days (30)
245 // width in months ?
246 if (awidth>=30.43685) { // Mean month length in 1900.
247 awidth /= 30.43685; timemulti *= 30.43685;
248 roundmode = 2; // round months (12)
249 // width in years ?
250 if (awidth>=12) {
251 awidth /= 12; timemulti *= 12;
252 roundmode = 0; // round years (10)
253 }
254 }
255 }
256 }
257 }
258// Get nominal bin width in exponential form
259
260 jlog = Int_t(TMath::Log10(awidth));
261 if (jlog <-200 || jlog > 200) {
262 BinLow = 0;
263 BinHigh = 1;
264 BinWidth = 0.01;
265 nbins = 100;
266 return;
267 }
268 if (awidth <= 1 && (!optionTime || timemulti==1) ) jlog--;
269 sigfig = awidth*TMath::Power(10,-jlog) -1e-10;
270 //in the above statement, it is important to subtract 1e-10
271 //to avoid precision problems if the tests below
272
273// Round mantissa
274
275 switch (roundmode) {
276
277// Round mantissa up to 1, 1.5, 2, 3, or 6 in case of minutes
278 case 1: // case 60
279 if (sigfig <= 1) siground = 1;
280 else if (sigfig <= 1.5 && jlog==1) siground = 1.5;
281 else if (sigfig <= 2) siground = 2;
282 else if (sigfig <= 3 && jlog ==1) siground = 3;
283 else if (sigfig <= 5 && sigfig>3 && jlog ==0) siground = 5; //added (Damir in 3.10/02)
284 else if (jlog==0) {siground = 1; jlog++;}
285 else siground = 6;
286 break;
287 case 2: // case 12 and 24
288
289// Round mantissa up to 1, 1.2, 2, 2.4, 3 or 6 in case of hours or months
290 if (sigfig <= 1 && jlog==0) siground = 1;
291 else if (sigfig <= 1.2 && jlog==1) siground = 1.2;
292 else if (sigfig <= 2 && jlog==0) siground = 2;
293 else if (sigfig <= 2.4 && jlog==1) siground = 2.4;
294 else if (sigfig <= 3) siground = 3;
295 else if (sigfig <= 6) siground = 6;
296 else if (jlog==0) siground = 12;
297 else siground = 2.4;
298 break;
299
300//- Round mantissa up to 1, 1.4, 2, or 7 in case of days (weeks)
301 case 3: // case 30
302 if (sigfig <= 1 && jlog==0) siground = 1;
303 else if (sigfig <= 1.4 && jlog==1) siground = 1.4;
304 else if (sigfig <= 3 && jlog ==1) siground = 3;
305 else siground = 7;
306 break;
307 default :
308
309// Round mantissa up to 1, 2, 2.5, 5, or 10 in case of decimal number
310 if (sigfig <= 1) siground = 1;
311 else if (sigfig <= 2) siground = 2;
312 else if (sigfig <= 5 && (!optionTime || jlog<1)) siground = 5;
313 else if (sigfig <= 6 && optionTime && jlog==1) siground = 6;
314 else {siground = 1; jlog++; }
315 break;
316 }
317
318 BinWidth = siground*TMath::Power(10,jlog);
319 if (optionTime) BinWidth *= timemulti;
320
321// Get new bounds from new width BinWidth
322
323L90:
324 alb = al/BinWidth;
325 if (TMath::Abs(alb) > 1e9) {
326 BinLow = al;
327 BinHigh = ah;
328 if (nbins > 10*nold && nbins > 10000) nbins = nold;
329 return;
330 }
331 lwid = Int_t(alb);
332 if (alb < 0) lwid--;
333 BinLow = BinWidth*Double_t(lwid);
334 alb = ah/BinWidth + 1.00001;
335 kwid = Int_t(alb);
336 if (alb < 0) kwid--;
337 BinHigh = BinWidth*Double_t(kwid);
338 nbins = kwid - lwid;
339 if (nold == -1) goto LOK;
340 if (nold <= 5) { // Request for one bin is difficult case
341 if (nold > 1 || nbins == 1)goto LOK;
342 BinWidth = BinWidth*2;
343 nbins = 1;
344 goto LOK;
345 }
346 if (2*nbins == nold && !optionTime) {ntemp++; goto L20; }
347
348LOK:
349 Double_t oldBinLow = BinLow;
350 Double_t oldBinHigh = BinHigh;
351 Int_t oldnbins = nbins;
352
353 Double_t atest = BinWidth*0.0001;
354 //if (TMath::Abs(BinLow-A1) >= atest) { BinLow += BinWidth; nbins--; } //replaced by Damir in 3.10/02
355 //if (TMath::Abs(BinHigh-A2) >= atest) { BinHigh -= BinWidth; nbins--; } //by the next two lines
356 if (al-BinLow >= atest) { BinLow += BinWidth; nbins--; }
357 if (BinHigh-ah >= atest) { BinHigh -= BinWidth; nbins--; }
358 if (!optionTime && BinLow >= BinHigh) {
359 //this case may happen when nbins <=5
360 BinLow = oldBinLow;
361 BinHigh = oldBinHigh;
362 nbins = oldnbins;
363 }
364 else if (optionTime && BinLow>=BinHigh) {
365 nbins = 2*oldnbins;
366 BinHigh = oldBinHigh;
367 BinLow = oldBinLow;
368 BinWidth = (oldBinHigh - oldBinLow)/nbins;
369 atest = BinWidth*0.0001;
370 if (al-BinLow >= atest) { BinLow += BinWidth; nbins--; }
371 if (BinHigh-ah >= atest) { BinHigh -= BinWidth; nbins--; }
372 }
373}
374
375////////////////////////////////////////////////////////////////////////////////
376/// Optimize axis limits.
377///
378/// When isInter=kTRUE, the function makes an integer binwidth
379/// and recompute the number of bins accordingly.
380
382{
383 Double_t binlow = 0,binhigh = 0,binwidth=0;
384 Int_t n=0;
385 Double_t dx = 0.1*(xmax-xmin);
386 if (isInteger) dx = 5*(xmax-xmin)/nbins;
387 Double_t umin = xmin - dx;
388 Double_t umax = xmax + dx;
389 if (umin < 0 && xmin >= 0) umin = 0;
390 if (umax > 0 && xmax <= 0) umax = 0;
391
392 THLimitsFinder::Optimize(umin,umax,nbins,binlow,binhigh,n,binwidth,"");
393
394 if (binwidth <= 0 || binwidth > 1.e+39) {
395 xmin = -1;
396 xmax = 1;
397 } else {
398 const auto delta = 0.01 * (xmax - xmin);
399 xmin = std::min(binlow, xmin - delta);
400 xmax = std::max(binhigh, xmax + delta);
401 }
402 if (isInteger) {
403 Long64_t ixmin = Long64_t(xmin);
404 Long64_t ixmax = Long64_t(xmax);
405 Double_t dxmin = Double_t(ixmin);
406 Double_t dxmax = Double_t(ixmax);
407 if (xmin < 0 && xmin != dxmin) xmin = dxmin - 1;
408 else xmin = dxmin;
409 if (xmax > 0 && xmax != dxmax) xmax = dxmax + 1;
410 else if (xmax ==0 && xmax == dxmax) xmax = 1;
411 else xmax = dxmax;
412 if (xmin >= xmax) xmax = xmin+1;
413 Long64_t bw = Long64_t((xmax-xmin)/nbins);
414 if (bw == 0) bw = 1;
415 nbins = Int_t((xmax-xmin)/bw);
416 if (xmin +nbins*bw < umax) {nbins++; xmax = xmin +nbins*bw;}
417 if (xmin > umin) {nbins++; xmin = xmax -nbins*bw;}
418 }
419 newbins = nbins;
420}
#define h(i)
Definition RSha256.hxx:106
#define e(i)
Definition RSha256.hxx:103
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:60
double Double_t
Double 8 bytes.
Definition RtypesCore.h:74
long long Long64_t
Portable signed long integer 8 bytes.
Definition RtypesCore.h:84
const char Option_t
Option string (const char)
Definition RtypesCore.h:81
Option_t Option_t option
float xmin
float ymin
float xmax
float ymax
Class to manage histogram axis.
Definition TAxis.h:32
@ kIsInteger
Definition TAxis.h:76
Int_t GetNbins() const
Definition TAxis.h:127
THashList * GetLabels() const
Definition TAxis.h:123
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:126
Class to compute nice axis limits.
static void SetLimitsFinder(THLimitsFinder *finder)
This static function can be used to specify a finder derived from THLimitsFinder.
~THLimitsFinder() override
static THLimitsFinder * GetLimitsFinder()
Return pointer to the current finder.
static void OptimizeLimits(Int_t nbins, Int_t &newbins, Double_t &xmin, Double_t &xmax, Bool_t isInteger)
Optimize axis limits.
virtual Int_t FindGoodLimitsXY(TH1 *h, Double_t xmin, Double_t xmax, Double_t ymin, Double_t ymax, Int_t newbinsx=0, Int_t newbinsy=0)
Compute the best axis limits for the X and Y axis if the corresponding newbins variable is set to 0,...
static THLimitsFinder * fgLimitsFinder
! Pointer to hist limits finder
virtual Int_t FindGoodLimits(TH1 *h, Double_t xmin, Double_t xmax)
Compute the best axis limits for the X axis.
virtual Int_t FindGoodLimitsXYZ(TH1 *h, Double_t xmin, Double_t xmax, Double_t ymin, Double_t ymax, Double_t zmin, Double_t zmax, Int_t newbinsx=0, Int_t newbinsy=0, Int_t newbinsz=0)
Compute the best axis limits and bins for the X, Y and Z axis if the corresponding newbins variable i...
static void Optimize(Double_t A1, Double_t A2, Int_t nold, Double_t &BinLow, Double_t &BinHigh, Int_t &nbins, Double_t &BWID, Option_t *option="")
Static function to compute reasonable axis limits.
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition TObject.h:204
const Int_t n
Definition legend1.C:16
Short_t Max(Short_t a, Short_t b)
Returns the largest of a and b.
Definition TMathBase.h:249
LongDouble_t Power(LongDouble_t x, LongDouble_t y)
Returns x raised to the power y.
Definition TMath.h:734
Short_t Min(Short_t a, Short_t b)
Returns the smallest of a and b.
Definition TMathBase.h:197
Double_t Log10(Double_t x)
Returns the common (base-10) logarithm of x.
Definition TMath.h:775
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
Definition TMathBase.h:122