Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TH1.cxx
Go to the documentation of this file.
1// @(#)root/hist:$Id$
2// Author: Rene Brun 26/12/94
3
4/*************************************************************************
5 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#include <array>
13#include <cctype>
14#include <climits>
15#include <cmath>
16#include <cstdio>
17#include <cstdlib>
18#include <cstring>
19#include <iostream>
20#include <memory>
21#include <sstream>
22#include <fstream>
23#include <limits>
24#include <iomanip>
25
26#include "TROOT.h"
27#include "TBuffer.h"
28#include "TEnv.h"
29#include "TClass.h"
30#include "TMath.h"
31#include "THashList.h"
32#include "TH1.h"
33#include "TH2.h"
34#include "TH3.h"
35#include "TF2.h"
36#include "TF3.h"
37#include "TPluginManager.h"
38#include "TVirtualPad.h"
39#include "TRandom.h"
40#include "TVirtualFitter.h"
41#include "THLimitsFinder.h"
42#include "TProfile.h"
43#include "TStyle.h"
44#include "TVectorF.h"
45#include "TVectorD.h"
46#include "TBrowser.h"
47#include "TError.h"
48#include "TVirtualHistPainter.h"
49#include "TVirtualFFT.h"
50#include "TVirtualPaveStats.h"
51
52#include "HFitInterface.h"
53#include "Fit/DataRange.h"
54#include "Fit/BinData.h"
55#include "Math/GoFTest.h"
58
59#include "TH1Merger.h"
60
61/** \addtogroup Histograms
62@{
63\class TH1C
64\brief 1-D histogram with a byte per channel (see TH1 documentation)
65\class TH1S
66\brief 1-D histogram with a short per channel (see TH1 documentation)
67\class TH1I
68\brief 1-D histogram with an int per channel (see TH1 documentation)
69\class TH1L
70\brief 1-D histogram with a long64 per channel (see TH1 documentation)
71\class TH1F
72\brief 1-D histogram with a float per channel (see TH1 documentation)
73\class TH1D
74\brief 1-D histogram with a double per channel (see TH1 documentation)
75@}
76*/
77
78/** \class TH1
79 \ingroup Histograms
80TH1 is the base class of all histogram classes in %ROOT.
81
82It provides the common interface for operations such as binning, filling, drawing, which
83will be detailed below.
84
85-# [Creating histograms](\ref creating-histograms)
86 - [Labelling axes](\ref labelling-axis)
87-# [Binning](\ref binning)
88 - [Fix or variable bin size](\ref fix-var)
89 - [Convention for numbering bins](\ref convention)
90 - [Alphanumeric Bin Labels](\ref alpha)
91 - [Histograms with automatic bins](\ref auto-bin)
92 - [Rebinning](\ref rebinning)
93-# [Filling histograms](\ref filling-histograms)
94 - [Associated errors](\ref associated-errors)
95 - [Associated functions](\ref associated-functions)
96 - [Projections of histograms](\ref prof-hist)
97 - [Random Numbers and histograms](\ref random-numbers)
98 - [Making a copy of a histogram](\ref making-a-copy)
99 - [Normalizing histograms](\ref normalizing)
100-# [Drawing histograms](\ref drawing-histograms)
101 - [Setting Drawing histogram contour levels (2-D hists only)](\ref cont-level)
102 - [Setting histogram graphics attributes](\ref graph-att)
103 - [Customising how axes are drawn](\ref axis-drawing)
104-# [Fitting histograms](\ref fitting-histograms)
105-# [Saving/reading histograms to/from a ROOT file](\ref saving-histograms)
106-# [Operations on histograms](\ref operations-on-histograms)
107-# [Miscellaneous operations](\ref misc)
108
109ROOT supports the following histogram types:
110
111 - 1-D histograms:
112 - TH1C : histograms with one byte per channel. Maximum bin content = 127
113 - TH1S : histograms with one short per channel. Maximum bin content = 32767
114 - TH1I : histograms with one int per channel. Maximum bin content = INT_MAX (\ref intmax "*")
115 - TH1L : histograms with one long64 per channel. Maximum bin content = LLONG_MAX (\ref llongmax "**")
116 - TH1F : histograms with one float per channel. Maximum precision 7 digits, maximum integer bin content = +/-16777216 (\ref floatmax "***")
117 - TH1D : histograms with one double per channel. Maximum precision 14 digits, maximum integer bin content = +/-9007199254740992 (\ref doublemax "****")
118 - 2-D histograms:
119 - TH2C : histograms with one byte per channel. Maximum bin content = 127
120 - TH2S : histograms with one short per channel. Maximum bin content = 32767
121 - TH2I : histograms with one int per channel. Maximum bin content = INT_MAX (\ref intmax "*")
122 - TH2L : histograms with one long64 per channel. Maximum bin content = LLONG_MAX (\ref llongmax "**")
123 - TH2F : histograms with one float per channel. Maximum precision 7 digits, maximum integer bin content = +/-16777216 (\ref floatmax "***")
124 - TH2D : histograms with one double per channel. Maximum precision 14 digits, maximum integer bin content = +/-9007199254740992 (\ref doublemax "****")
125 - 3-D histograms:
126 - TH3C : histograms with one byte per channel. Maximum bin content = 127
127 - TH3S : histograms with one short per channel. Maximum bin content = 32767
128 - TH3I : histograms with one int per channel. Maximum bin content = INT_MAX (\ref intmax "*")
129 - TH3L : histograms with one long64 per channel. Maximum bin content = LLONG_MAX (\ref llongmax "**")
130 - TH3F : histograms with one float per channel. Maximum precision 7 digits, maximum integer bin content = +/-16777216 (\ref floatmax "***")
131 - TH3D : histograms with one double per channel. Maximum precision 14 digits, maximum integer bin content = +/-9007199254740992 (\ref doublemax "****")
132 - Profile histograms: See classes TProfile, TProfile2D and TProfile3D.
133 Profile histograms are used to display the mean value of Y and its standard deviation
134 for each bin in X. Profile histograms are in many cases an elegant
135 replacement of two-dimensional histograms : the inter-relation of two
136 measured quantities X and Y can always be visualized by a two-dimensional
137 histogram or scatter-plot; If Y is an unknown (but single-valued)
138 approximate function of X, this function is displayed by a profile
139 histogram with much better precision than by a scatter-plot.
140
141<sup>
142\anchor intmax (*) INT_MAX = 2147483647 is the [maximum value for a variable of type int.](https://docs.microsoft.com/en-us/cpp/c-language/cpp-integer-limits)<br>
143\anchor llongmax (**) LLONG_MAX = 9223372036854775807 is the [maximum value for a variable of type long64.](https://docs.microsoft.com/en-us/cpp/c-language/cpp-integer-limits)<br>
144\anchor floatmax (***) 2^24 = 16777216 is the [maximum integer that can be properly represented by a float32 with 23-bit mantissa.](https://stackoverflow.com/a/3793950/7471760)<br>
145\anchor doublemax (****) 2^53 = 9007199254740992 is the [maximum integer that can be properly represented by a double64 with 52-bit mantissa.](https://stackoverflow.com/a/3793950/7471760)
146</sup>
147
148The inheritance hierarchy looks as follows:
149
150\image html classTH1__inherit__graph_org.svg width=100%
151
152\anchor creating-histograms
153## Creating histograms
154
155Histograms are created by invoking one of the constructors, e.g.
156~~~ {.cpp}
157 TH1F *h1 = new TH1F("h1", "h1 title", 100, 0, 4.4);
158 TH2F *h2 = new TH2F("h2", "h2 title", 40, 0, 4, 30, -3, 3);
159~~~
160Histograms may also be created by:
161
162 - calling the Clone() function, see below
163 - making a projection from a 2-D or 3-D histogram, see below
164 - reading a histogram from a file
165
166 When a histogram is created, a reference to it is automatically added
167 to the list of in-memory objects for the current file or directory.
168 Then the pointer to this histogram in the current directory can be found
169 by its name, doing:
170~~~ {.cpp}
171 TH1F *h1 = (TH1F*)gDirectory->FindObject(name);
172~~~
173
174 This default behaviour can be changed by:
175~~~ {.cpp}
176 h->SetDirectory(nullptr); // for the current histogram h
177 TH1::AddDirectory(kFALSE); // sets a global switch disabling the referencing
178~~~
179 When the histogram is deleted, the reference to it is removed from
180 the list of objects in memory.
181 When a file is closed, all histograms in memory associated with this file
182 are automatically deleted.
183
184\anchor labelling-axis
185### Labelling axes
186
187 Axis titles can be specified in the title argument of the constructor.
188 They must be separated by ";":
189~~~ {.cpp}
190 TH1F* h=new TH1F("h", "Histogram title;X Axis;Y Axis", 100, 0, 1);
191~~~
192 The histogram title and the axis titles can be any TLatex string, and
193 are persisted if a histogram is written to a file.
194
195 Any title can be omitted:
196~~~ {.cpp}
197 TH1F* h=new TH1F("h", "Histogram title;;Y Axis", 100, 0, 1);
198 TH1F* h=new TH1F("h", ";;Y Axis", 100, 0, 1);
199~~~
200 The method SetTitle() has the same syntax:
201~~~ {.cpp}
202 h->SetTitle("Histogram title;Another X title Axis");
203~~~
204Alternatively, the title of each axis can be set directly:
205~~~ {.cpp}
206 h->GetXaxis()->SetTitle("X axis title");
207 h->GetYaxis()->SetTitle("Y axis title");
208~~~
209For bin labels see \ref binning.
210
211\anchor binning
212## Binning
213
214\anchor fix-var
215### Fix or variable bin size
216
217 All histogram types support either fix or variable bin sizes.
218 2-D histograms may have fix size bins along X and variable size bins
219 along Y or vice-versa. The functions to fill, manipulate, draw or access
220 histograms are identical in both cases.
221
222 Each histogram always contains 3 axis objects of type TAxis: fXaxis, fYaxis and fZaxis.
223 To access the axis parameters, use:
224~~~ {.cpp}
225 TAxis *xaxis = h->GetXaxis(); etc.
226 Double_t binCenter = xaxis->GetBinCenter(bin), etc.
227~~~
228 See class TAxis for a description of all the access functions.
229 The axis range is always stored internally in double precision.
230
231\anchor convention
232### Convention for numbering bins
233
234 For all histogram types: nbins, xlow, xup
235~~~ {.cpp}
236 bin = 0; underflow bin
237 bin = 1; first bin with low-edge xlow INCLUDED
238 bin = nbins; last bin with upper-edge xup EXCLUDED
239 bin = nbins+1; overflow bin
240~~~
241 In case of 2-D or 3-D histograms, a "global bin" number is defined.
242 For example, assuming a 3-D histogram with (binx, biny, binz), the function
243~~~ {.cpp}
244 Int_t gbin = h->GetBin(binx, biny, binz);
245~~~
246 returns a global/linearized gbin number. This global gbin is useful
247 to access the bin content/error information independently of the dimension.
248 Note that to access the information other than bin content and errors
249 one should use the TAxis object directly with e.g.:
250~~~ {.cpp}
251 Double_t xcenter = h3->GetZaxis()->GetBinCenter(27);
252~~~
253 returns the center along z of bin number 27 (not the global bin)
254 in the 3-D histogram h3.
255
256\anchor alpha
257### Alphanumeric Bin Labels
258
259 By default, a histogram axis is drawn with its numeric bin labels.
260 One can specify alphanumeric labels instead with:
261
262 - call TAxis::SetBinLabel(bin, label);
263 This can always be done before or after filling.
264 When the histogram is drawn, bin labels will be automatically drawn.
265 See examples labels1.C and labels2.C
266 - call to a Fill function with one of the arguments being a string, e.g.
267~~~ {.cpp}
268 hist1->Fill(somename, weight);
269 hist2->Fill(x, somename, weight);
270 hist2->Fill(somename, y, weight);
271 hist2->Fill(somenamex, somenamey, weight);
272~~~
273 See examples hlabels1.C and hlabels2.C
274 - via TTree::Draw. see for example cernstaff.C
275~~~ {.cpp}
276 tree.Draw("Nation::Division");
277~~~
278 where "Nation" and "Division" are two branches of a Tree.
279
280When using the options 2 or 3 above, the labels are automatically
281 added to the list (THashList) of labels for a given axis.
282 By default, an axis is drawn with the order of bins corresponding
283 to the filling sequence. It is possible to reorder the axis
284
285 - alphabetically
286 - by increasing or decreasing values
287
288 The reordering can be triggered via the TAxis context menu by selecting
289 the menu item "LabelsOption" or by calling directly
290 TH1::LabelsOption(option, axis) where
291
292 - axis may be "X", "Y" or "Z"
293 - option may be:
294 - "a" sort by alphabetic order
295 - ">" sort by decreasing values
296 - "<" sort by increasing values
297 - "h" draw labels horizontal
298 - "v" draw labels vertical
299 - "u" draw labels up (end of label right adjusted)
300 - "d" draw labels down (start of label left adjusted)
301
302 When using the option 2 above, new labels are added by doubling the current
303 number of bins in case one label does not exist yet.
304 When the Filling is terminated, it is possible to trim the number
305 of bins to match the number of active labels by calling
306~~~ {.cpp}
307 TH1::LabelsDeflate(axis) with axis = "X", "Y" or "Z"
308~~~
309 This operation is automatic when using TTree::Draw.
310 Once bin labels have been created, they become persistent if the histogram
311 is written to a file or when generating the C++ code via SavePrimitive.
312
313\anchor auto-bin
314### Histograms with automatic bins
315
316 When a histogram is created with an axis lower limit greater or equal
317 to its upper limit, the SetBuffer is automatically called with an
318 argument fBufferSize equal to fgBufferSize (default value=1000).
319 fgBufferSize may be reset via the static function TH1::SetDefaultBufferSize.
320 The axis limits will be automatically computed when the buffer will
321 be full or when the function BufferEmpty is called.
322
323\anchor rebinning
324### Rebinning
325
326 At any time, a histogram can be rebinned via TH1::Rebin. This function
327 returns a new histogram with the rebinned contents.
328 If bin errors were stored, they are recomputed during the rebinning.
329
330
331\anchor filling-histograms
332## Filling histograms
333
334 A histogram is typically filled with statements like:
335~~~ {.cpp}
336 h1->Fill(x);
337 h1->Fill(x, w); //fill with weight
338 h2->Fill(x, y)
339 h2->Fill(x, y, w)
340 h3->Fill(x, y, z)
341 h3->Fill(x, y, z, w)
342~~~
343 or via one of the Fill functions accepting names described above.
344 The Fill functions compute the bin number corresponding to the given
345 x, y or z argument and increment this bin by the given weight.
346 The Fill functions return the bin number for 1-D histograms or global
347 bin number for 2-D and 3-D histograms.
348 If TH1::Sumw2 has been called before filling, the sum of squares of
349 weights is also stored.
350 One can also increment directly a bin number via TH1::AddBinContent
351 or replace the existing content via TH1::SetBinContent. Passing an
352 out-of-range bin to TH1::AddBinContent leads to undefined behavior.
353 To access the bin content of a given bin, do:
354~~~ {.cpp}
355 Double_t binContent = h->GetBinContent(bin);
356~~~
357
358 By default, the bin number is computed using the current axis ranges.
359 If the automatic binning option has been set via
360~~~ {.cpp}
361 h->SetCanExtend(TH1::kAllAxes);
362~~~
363 then, the Fill Function will automatically extend the axis range to
364 accomodate the new value specified in the Fill argument. The method
365 used is to double the bin size until the new value fits in the range,
366 merging bins two by two. This automatic binning options is extensively
367 used by the TTree::Draw function when histogramming Tree variables
368 with an unknown range.
369 This automatic binning option is supported for 1-D, 2-D and 3-D histograms.
370
371 During filling, some statistics parameters are incremented to compute
372 the mean value and Root Mean Square with the maximum precision.
373
374 In case of histograms of type TH1C, TH1S, TH2C, TH2S, TH3C, TH3S
375 a check is made that the bin contents do not exceed the maximum positive
376 capacity (127 or 32767). Histograms of all types may have positive
377 or/and negative bin contents.
378
379\anchor associated-errors
380### Associated errors
381 By default, for each bin, the sum of weights is computed at fill time.
382 One can also call TH1::Sumw2 to force the storage and computation
383 of the sum of the square of weights per bin.
384 If Sumw2 has been called, the error per bin is computed as the
385 sqrt(sum of squares of weights), otherwise the error is set equal
386 to the sqrt(bin content).
387 To return the error for a given bin number, do:
388~~~ {.cpp}
389 Double_t error = h->GetBinError(bin);
390~~~
391
392\anchor associated-functions
393### Associated functions
394 One or more objects (typically a TF1*) can be added to the list
395 of functions (fFunctions) associated to each histogram.
396 When TH1::Fit is invoked, the fitted function is added to this list.
397 Given a histogram (or TGraph) `h`, one can retrieve an associated function
398 with:
399~~~ {.cpp}
400 TF1 *myfunc = h->GetFunction("myfunc");
401~~~
402
403
404\anchor operations-on-histograms
405## Operations on histograms
406
407 Many types of operations are supported on histograms or between histograms
408
409 - Addition of a histogram to the current histogram.
410 - Additions of two histograms with coefficients and storage into the current
411 histogram.
412 - Multiplications and Divisions are supported in the same way as additions.
413 - The Add, Divide and Multiply functions also exist to add, divide or multiply
414 a histogram by a function.
415
416 If a histogram has associated error bars (TH1::Sumw2 has been called),
417 the resulting error bars are also computed assuming independent histograms.
418 In case of divisions, Binomial errors are also supported.
419 One can mark a histogram to be an "average" histogram by setting its bit kIsAverage via
420 myhist.SetBit(TH1::kIsAverage);
421 When adding (see TH1::Add) average histograms, the histograms are averaged and not summed.
422
423
424\anchor prof-hist
425### Projections of histograms
426
427 One can:
428
429 - make a 1-D projection of a 2-D histogram or Profile
430 see functions TH2::ProjectionX,Y, TH2::ProfileX,Y, TProfile::ProjectionX
431 - make a 1-D, 2-D or profile out of a 3-D histogram
432 see functions TH3::ProjectionZ, TH3::Project3D.
433
434 One can fit these projections via:
435~~~ {.cpp}
436 TH2::FitSlicesX,Y, TH3::FitSlicesZ.
437~~~
438
439\anchor random-numbers
440### Random Numbers and histograms
441
442 TH1::FillRandom can be used to randomly fill a histogram using
443 the contents of an existing TF1 function or another
444 TH1 histogram (for all dimensions).
445 For example, the following two statements create and fill a histogram
446 10000 times with a default gaussian distribution of mean 0 and sigma 1:
447~~~ {.cpp}
448 TH1F h1("h1", "histo from a gaussian", 100, -3, 3);
449 h1.FillRandom("gaus", 10000);
450~~~
451 TH1::GetRandom can be used to return a random number distributed
452 according to the contents of a histogram.
453
454\anchor making-a-copy
455### Making a copy of a histogram
456 Like for any other ROOT object derived from TObject, one can use
457 the Clone() function. This makes an identical copy of the original
458 histogram including all associated errors and functions, e.g.:
459~~~ {.cpp}
460 TH1F *hnew = (TH1F*)h->Clone("hnew");
461~~~
462
463\anchor normalizing
464### Normalizing histograms
465
466 One can scale a histogram such that the bins integral is equal to
467 the normalization parameter via TH1::Scale(Double_t norm), where norm
468 is the desired normalization divided by the integral of the histogram.
471\anchor drawing-histograms
472## Drawing histograms
473
474 Histograms are drawn via the THistPainter class. Each histogram has
475 a pointer to its own painter (to be usable in a multithreaded program).
476 Many drawing options are supported.
477 See THistPainter::Paint() for more details.
478
479 The same histogram can be drawn with different options in different pads.
480 When a histogram drawn in a pad is deleted, the histogram is
481 automatically removed from the pad or pads where it was drawn.
482 If a histogram is drawn in a pad, then filled again, the new status
483 of the histogram will be automatically shown in the pad next time
484 the pad is updated. One does not need to redraw the histogram.
485 To draw the current version of a histogram in a pad, one can use
486~~~ {.cpp}
487 h->DrawCopy();
488~~~
489 This makes a clone (see Clone below) of the histogram. Once the clone
490 is drawn, the original histogram may be modified or deleted without
491 affecting the aspect of the clone.
492
493 One can use TH1::SetMaximum() and TH1::SetMinimum() to force a particular
494 value for the maximum or the minimum scale on the plot. (For 1-D
495 histograms this means the y-axis, while for 2-D histograms these
496 functions affect the z-axis).
497
498 TH1::UseCurrentStyle() can be used to change all histogram graphics
499 attributes to correspond to the current selected style.
500 This function must be called for each histogram.
501 In case one reads and draws many histograms from a file, one can force
502 the histograms to inherit automatically the current graphics style
503 by calling before gROOT->ForceStyle().
504
505\anchor cont-level
506### Setting Drawing histogram contour levels (2-D hists only)
507
508 By default contours are automatically generated at equidistant
509 intervals. A default value of 20 levels is used. This can be modified
510 via TH1::SetContour() or TH1::SetContourLevel().
511 the contours level info is used by the drawing options "cont", "surf",
512 and "lego".
513
514\anchor graph-att
515### Setting histogram graphics attributes
517 The histogram classes inherit from the attribute classes:
518 TAttLine, TAttFill, and TAttMarker.
519 See the member functions of these classes for the list of options.
520
521\anchor axis-drawing
522### Customizing how axes are drawn
523
524 Use the functions of TAxis, such as
525~~~ {.cpp}
526 histogram.GetXaxis()->SetTicks("+");
527 histogram.GetYaxis()->SetRangeUser(1., 5.);
528~~~
529
530\anchor fitting-histograms
531## Fitting histograms
532
533 Histograms (1-D, 2-D, 3-D and Profiles) can be fitted with a user
534 specified function or a pre-defined function via TH1::Fit.
535 See TH1::Fit(TF1*, Option_t *, Option_t *, Double_t, Double_t) for the fitting documentation and the possible [fitting options](\ref HFitOpt)
536
537 The FitPanel can also be used for fitting an histogram. See the [FitPanel documentation](https://root.cern/manual/fitting/#using-the-fit-panel).
538
539\anchor saving-histograms
540## Saving/reading histograms to/from a ROOT file
541
542 The following statements create a ROOT file and store a histogram
543 on the file. Because TH1 derives from TNamed, the key identifier on
544 the file is the histogram name:
545~~~ {.cpp}
546 TFile f("histos.root", "new");
547 TH1F h1("hgaus", "histo from a gaussian", 100, -3, 3);
548 h1.FillRandom("gaus", 10000);
549 h1->Write();
550~~~
551 To read this histogram in another Root session, do:
552~~~ {.cpp}
553 TFile f("histos.root");
554 TH1F *h = (TH1F*)f.Get("hgaus");
555~~~
556 One can save all histograms in memory to the file by:
557~~~ {.cpp}
558 file->Write();
559~~~
560
561
562\anchor misc
563## Miscellaneous operations
564
565~~~ {.cpp}
566 TH1::KolmogorovTest(): statistical test of compatibility in shape
567 between two histograms
568 TH1::Smooth() smooths the bin contents of a 1-d histogram
569 TH1::Integral() returns the integral of bin contents in a given bin range
570 TH1::GetMean(int axis) returns the mean value along axis
571 TH1::GetStdDev(int axis) returns the sigma distribution along axis
572 TH1::GetEntries() returns the number of entries
573 TH1::Reset() resets the bin contents and errors of a histogram
574~~~
575 IMPORTANT NOTE: The returned values for GetMean and GetStdDev depend on how the
576 histogram statistics are calculated. By default, if no range has been set, the
577 returned values are the (unbinned) ones calculated at fill time. If a range has been
578 set, however, the values are calculated using the bins in range; THIS IS TRUE EVEN
579 IF THE RANGE INCLUDES ALL BINS--use TAxis::SetRange(0, 0) to unset the range.
580 To ensure that the returned values are always those of the binned data stored in the
581 histogram, call TH1::ResetStats. See TH1::GetStats.
582*/
583
584TF1 *gF1=nullptr; //left for back compatibility (use TVirtualFitter::GetUserFunc instead)
585
590
591extern void H1InitGaus();
592extern void H1InitExpo();
593extern void H1InitPolynom();
594extern void H1LeastSquareFit(Int_t n, Int_t m, Double_t *a);
597
598
599////////////////////////////////////////////////////////////////////////////////
600/// Histogram default constructor.
601
603{
604 fDirectory = nullptr;
605 fFunctions = new TList;
606 fNcells = 0;
607 fIntegral = nullptr;
608 fPainter = nullptr;
609 fEntries = 0;
610 fNormFactor = 0;
612 fMaximum = -1111;
613 fMinimum = -1111;
614 fBufferSize = 0;
615 fBuffer = nullptr;
618 fXaxis.SetName("xaxis");
619 fYaxis.SetName("yaxis");
620 fZaxis.SetName("zaxis");
621 fXaxis.SetParent(this);
622 fYaxis.SetParent(this);
623 fZaxis.SetParent(this);
625}
626
627////////////////////////////////////////////////////////////////////////////////
628/// Histogram default destructor.
629
631{
633 return;
634 }
635 delete[] fIntegral;
636 fIntegral = nullptr;
637 delete[] fBuffer;
638 fBuffer = nullptr;
639 if (fFunctions) {
641
643 TObject* obj = nullptr;
644 //special logic to support the case where the same object is
645 //added multiple times in fFunctions.
646 //This case happens when the same object is added with different
647 //drawing modes
648 //In the loop below we must be careful with objects (eg TCutG) that may
649 // have been added to the list of functions of several histograms
650 //and may have been already deleted.
651 while ((obj = fFunctions->First())) {
652 while(fFunctions->Remove(obj)) { }
654 break;
655 }
656 delete obj;
657 obj = nullptr;
658 }
659 delete fFunctions;
660 fFunctions = nullptr;
661 }
662 if (fDirectory) {
663 fDirectory->Remove(this);
664 fDirectory = nullptr;
665 }
666 delete fPainter;
667 fPainter = nullptr;
668}
669
670////////////////////////////////////////////////////////////////////////////////
671/// Constructor for fix bin size histograms.
672/// Creates the main histogram structure.
673///
674/// \param[in] name name of histogram (avoid blanks)
675/// \param[in] title histogram title.
676/// If title is of the form `stringt;stringx;stringy;stringz`,
677/// the histogram title is set to `stringt`,
678/// the x axis title to `stringx`, the y axis title to `stringy`, etc.
679/// \param[in] nbins number of bins
680/// \param[in] xlow low edge of first bin
681/// \param[in] xup upper edge of last bin (not included in last bin)
682
683
684TH1::TH1(const char *name,const char *title,Int_t nbins,Double_t xlow,Double_t xup)
685 :TNamed(name,title)
686{
687 Build();
688 if (nbins <= 0) {Warning("TH1","nbins is <=0 - set to nbins = 1"); nbins = 1; }
689 fXaxis.Set(nbins,xlow,xup);
690 fNcells = fXaxis.GetNbins()+2;
691}
692
693////////////////////////////////////////////////////////////////////////////////
694/// Constructor for variable bin size histograms using an input array of type float.
695/// Creates the main histogram structure.
696///
697/// \param[in] name name of histogram (avoid blanks)
698/// \param[in] title histogram title.
699/// If title is of the form `stringt;stringx;stringy;stringz`
700/// the histogram title is set to `stringt`,
701/// the x axis title to `stringx`, the y axis title to `stringy`, etc.
702/// \param[in] nbins number of bins
703/// \param[in] xbins array of low-edges for each bin.
704/// This is an array of type float and size nbins+1
705
706TH1::TH1(const char *name,const char *title,Int_t nbins,const Float_t *xbins)
707 :TNamed(name,title)
708{
709 Build();
710 if (nbins <= 0) {Warning("TH1","nbins is <=0 - set to nbins = 1"); nbins = 1; }
711 if (xbins) fXaxis.Set(nbins,xbins);
712 else fXaxis.Set(nbins,0,1);
713 fNcells = fXaxis.GetNbins()+2;
714}
715
716////////////////////////////////////////////////////////////////////////////////
717/// Constructor for variable bin size histograms using an input array of type double.
718///
719/// \param[in] name name of histogram (avoid blanks)
720/// \param[in] title histogram title.
721/// If title is of the form `stringt;stringx;stringy;stringz`
722/// the histogram title is set to `stringt`,
723/// the x axis title to `stringx`, the y axis title to `stringy`, etc.
724/// \param[in] nbins number of bins
725/// \param[in] xbins array of low-edges for each bin.
726/// This is an array of type double and size nbins+1
727
728TH1::TH1(const char *name,const char *title,Int_t nbins,const Double_t *xbins)
729 :TNamed(name,title)
730{
731 Build();
732 if (nbins <= 0) {Warning("TH1","nbins is <=0 - set to nbins = 1"); nbins = 1; }
733 if (xbins) fXaxis.Set(nbins,xbins);
734 else fXaxis.Set(nbins,0,1);
735 fNcells = fXaxis.GetNbins()+2;
736}
737
738////////////////////////////////////////////////////////////////////////////////
739/// Static function: cannot be inlined on Windows/NT.
740
745
746////////////////////////////////////////////////////////////////////////////////
747/// Browse the Histogram object.
748
750{
751 Draw(b ? b->GetDrawOption() : "");
752 gPad->Update();
753}
754
755////////////////////////////////////////////////////////////////////////////////
756/// Creates histogram basic data structure.
757
759{
760 fDirectory = nullptr;
761 fPainter = nullptr;
762 fIntegral = nullptr;
763 fEntries = 0;
764 fNormFactor = 0;
766 fMaximum = -1111;
767 fMinimum = -1111;
768 fBufferSize = 0;
769 fBuffer = nullptr;
772 fXaxis.SetName("xaxis");
773 fYaxis.SetName("yaxis");
774 fZaxis.SetName("zaxis");
775 fYaxis.Set(1,0.,1.);
776 fZaxis.Set(1,0.,1.);
777 fXaxis.SetParent(this);
778 fYaxis.SetParent(this);
779 fZaxis.SetParent(this);
780
782
783 fFunctions = new TList;
784
786
789 if (fDirectory) {
791 fDirectory->Append(this,kTRUE);
792 }
793 }
794}
795
796////////////////////////////////////////////////////////////////////////////////
797/// Performs the operation: `this = this + c1*f1`
798/// if errors are defined (see TH1::Sumw2), errors are also recalculated.
799///
800/// By default, the function is computed at the centre of the bin.
801/// if option "I" is specified (1-d histogram only), the integral of the
802/// function in each bin is used instead of the value of the function at
803/// the centre of the bin.
804///
805/// Only bins inside the function range are recomputed.
806///
807/// IMPORTANT NOTE: If you intend to use the errors of this histogram later
808/// you should call Sumw2 before making this operation.
809/// This is particularly important if you fit the histogram after TH1::Add
810///
811/// The function return kFALSE if the Add operation failed
812
814{
815 if (!f1) {
816 Error("Add","Attempt to add a non-existing function");
817 return kFALSE;
818 }
819
820 TString opt = option;
821 opt.ToLower();
822 Bool_t integral = kFALSE;
823 if (opt.Contains("i") && fDimension == 1) integral = kTRUE;
824
825 Int_t ncellsx = GetNbinsX() + 2; // cells = normal bins + underflow bin + overflow bin
826 Int_t ncellsy = GetNbinsY() + 2;
827 Int_t ncellsz = GetNbinsZ() + 2;
828 if (fDimension < 2) ncellsy = 1;
829 if (fDimension < 3) ncellsz = 1;
830
831 // delete buffer if it is there since it will become invalid
832 if (fBuffer) BufferEmpty(1);
833
834 // - Add statistics
835 Double_t s1[10];
836 for (Int_t i = 0; i < 10; ++i) s1[i] = 0;
837 PutStats(s1);
838 SetMinimum();
839 SetMaximum();
840
841 // - Loop on bins (including underflows/overflows)
842 Int_t bin, binx, biny, binz;
843 Double_t cu=0;
844 Double_t xx[3];
845 Double_t *params = nullptr;
846 f1->InitArgs(xx,params);
847 for (binz = 0; binz < ncellsz; ++binz) {
849 for (biny = 0; biny < ncellsy; ++biny) {
851 for (binx = 0; binx < ncellsx; ++binx) {
853 if (!f1->IsInside(xx)) continue;
855 bin = binx + ncellsx * (biny + ncellsy * binz);
856 if (integral) {
858 } else {
859 cu = c1*f1->EvalPar(xx);
860 }
861 if (TF1::RejectedPoint()) continue;
862 AddBinContent(bin,cu);
863 }
864 }
865 }
866
867 return kTRUE;
868}
869
870int TH1::LoggedInconsistency(const char *name, const TH1 *h1, const TH1 *h2, bool useMerge) const
871{
872 const auto inconsistency = CheckConsistency(h1, h2);
873
875 if (useMerge)
876 Info(name, "Histograms have different dimensions - trying to use TH1::Merge");
877 else {
878 Error(name, "Histograms have different dimensions");
879 }
881 if (useMerge)
882 Info(name, "Histograms have different number of bins - trying to use TH1::Merge");
883 else {
884 Error(name, "Histograms have different number of bins");
885 }
886 } else if (inconsistency & kDifferentAxisLimits) {
887 if (useMerge)
888 Info(name, "Histograms have different axis limits - trying to use TH1::Merge");
889 else
890 Warning(name, "Histograms have different axis limits");
891 } else if (inconsistency & kDifferentBinLimits) {
892 if (useMerge)
893 Info(name, "Histograms have different bin limits - trying to use TH1::Merge");
894 else
895 Warning(name, "Histograms have different bin limits");
896 } else if (inconsistency & kDifferentLabels) {
897 // in case of different labels -
898 if (useMerge)
899 Info(name, "Histograms have different labels - trying to use TH1::Merge");
900 else
901 Info(name, "Histograms have different labels");
902 }
903
904 return inconsistency;
905}
906
907////////////////////////////////////////////////////////////////////////////////
908/// Performs the operation: `this = this + c1*h1`
909/// If errors are defined (see TH1::Sumw2), errors are also recalculated.
910///
911/// Note that if h1 has Sumw2 set, Sumw2 is automatically called for this
912/// if not already set.
913///
914/// Note also that adding histogram with labels is not supported, histogram will be
915/// added merging them by bin number independently of the labels.
916/// For adding histogram with labels one should use TH1::Merge
917///
918/// SPECIAL CASE (Average/Efficiency histograms)
919/// For histograms representing averages or efficiencies, one should compute the average
920/// of the two histograms and not the sum. One can mark a histogram to be an average
921/// histogram by setting its bit kIsAverage with
922/// myhist.SetBit(TH1::kIsAverage);
923/// Note that the two histograms must have their kIsAverage bit set
924///
925/// IMPORTANT NOTE1: If you intend to use the errors of this histogram later
926/// you should call Sumw2 before making this operation.
927/// This is particularly important if you fit the histogram after TH1::Add
928///
929/// IMPORTANT NOTE2: if h1 has a normalisation factor, the normalisation factor
930/// is used , ie this = this + c1*factor*h1
931/// Use the other TH1::Add function if you do not want this feature
932///
933/// IMPORTANT NOTE3: You should be careful about the statistics of the
934/// returned histogram, whose statistics may be binned or unbinned,
935/// depending on whether c1 is negative, whether TAxis::kAxisRange is true,
936/// and whether TH1::ResetStats has been called on either this or h1.
937/// See TH1::GetStats.
938///
939/// The function return kFALSE if the Add operation failed
940
942{
943 if (!h1) {
944 Error("Add","Attempt to add a non-existing histogram");
945 return kFALSE;
946 }
947
948 // delete buffer if it is there since it will become invalid
949 if (fBuffer) BufferEmpty(1);
950
951 bool useMerge = false;
952 const bool considerMerge = (c1 == 1. && !this->TestBit(kIsAverage) && !h1->TestBit(kIsAverage) );
953 const auto inconsistency = LoggedInconsistency("Add", this, h1, considerMerge);
954 // If there is a bad inconsistency and we can't even consider merging, just give up
956 return false;
957 }
958 // If there is an inconsistency, we try to use merging
961 }
962
963 if (useMerge) {
964 TList l;
965 l.Add(const_cast<TH1*>(h1));
966 auto iret = Merge(&l);
967 return (iret >= 0);
968 }
969
970 // Create Sumw2 if h1 has Sumw2 set
971 if (fSumw2.fN == 0 && h1->GetSumw2N() != 0) Sumw2();
972
973 // - Add statistics
974 Double_t entries = TMath::Abs( GetEntries() + c1 * h1->GetEntries() );
975
976 // statistics can be preserved only in case of positive coefficients
977 // otherwise with negative c1 (histogram subtraction) one risks to get negative variances
978 Bool_t resetStats = (c1 < 0);
979 Double_t s1[kNstat] = {0};
980 Double_t s2[kNstat] = {0};
981 if (!resetStats) {
982 // need to initialize to zero s1 and s2 since
983 // GetStats fills only used elements depending on dimension and type
984 GetStats(s1);
985 h1->GetStats(s2);
986 }
987
988 SetMinimum();
989 SetMaximum();
990
991 // - Loop on bins (including underflows/overflows)
992 Double_t factor = 1;
993 if (h1->GetNormFactor() != 0) factor = h1->GetNormFactor()/h1->GetSumOfWeights();
994 Double_t c1sq = c1 * c1;
995 Double_t factsq = factor * factor;
996
997 for (Int_t bin = 0; bin < fNcells; ++bin) {
998 //special case where histograms have the kIsAverage bit set
999 if (this->TestBit(kIsAverage) && h1->TestBit(kIsAverage)) {
1001 Double_t y2 = this->RetrieveBinContent(bin);
1004 Double_t w1 = 1., w2 = 1.;
1005
1006 // consider all special cases when bin errors are zero
1007 // see http://root-forum.cern.ch/viewtopic.php?f=3&t=13299
1008 if (e1sq) w1 = 1. / e1sq;
1009 else if (h1->fSumw2.fN) {
1010 w1 = 1.E200; // use an arbitrary huge value
1011 if (y1 == 0) {
1012 // use an estimated error from the global histogram scale
1013 double sf = (s2[0] != 0) ? s2[1]/s2[0] : 1;
1014 w1 = 1./(sf*sf);
1015 }
1016 }
1017 if (e2sq) w2 = 1. / e2sq;
1018 else if (fSumw2.fN) {
1019 w2 = 1.E200; // use an arbitrary huge value
1020 if (y2 == 0) {
1021 // use an estimated error from the global histogram scale
1022 double sf = (s1[0] != 0) ? s1[1]/s1[0] : 1;
1023 w2 = 1./(sf*sf);
1024 }
1025 }
1026
1027 double y = (w1*y1 + w2*y2)/(w1 + w2);
1028 UpdateBinContent(bin, y);
1029 if (fSumw2.fN) {
1030 double err2 = 1./(w1 + w2);
1031 if (err2 < 1.E-200) err2 = 0; // to remove arbitrary value when e1=0 AND e2=0
1032 fSumw2.fArray[bin] = err2;
1033 }
1034 } else { // normal case of addition between histograms
1035 AddBinContent(bin, c1 * factor * h1->RetrieveBinContent(bin));
1036 if (fSumw2.fN) fSumw2.fArray[bin] += c1sq * factsq * h1->GetBinErrorSqUnchecked(bin);
1037 }
1038 }
1039
1040 // update statistics (do here to avoid changes by SetBinContent)
1041 if (resetStats) {
1042 // statistics need to be reset in case coefficient are negative
1043 ResetStats();
1044 }
1045 else {
1046 for (Int_t i=0;i<kNstat;i++) {
1047 if (i == 1) s1[i] += c1*c1*s2[i];
1048 else s1[i] += c1*s2[i];
1049 }
1050 PutStats(s1);
1051 SetEntries(entries);
1052 }
1053 return kTRUE;
1054}
1055
1056////////////////////////////////////////////////////////////////////////////////
1057/// Replace contents of this histogram by the addition of h1 and h2.
1058///
1059/// `this = c1*h1 + c2*h2`
1060/// if errors are defined (see TH1::Sumw2), errors are also recalculated
1061///
1062/// Note that if h1 or h2 have Sumw2 set, Sumw2 is automatically called for this
1063/// if not already set.
1064///
1065/// Note also that adding histogram with labels is not supported, histogram will be
1066/// added merging them by bin number independently of the labels.
1067/// For adding histogram ith labels one should use TH1::Merge
1068///
1069/// SPECIAL CASE (Average/Efficiency histograms)
1070/// For histograms representing averages or efficiencies, one should compute the average
1071/// of the two histograms and not the sum. One can mark a histogram to be an average
1072/// histogram by setting its bit kIsAverage with
1073/// myhist.SetBit(TH1::kIsAverage);
1074/// Note that the two histograms must have their kIsAverage bit set
1075///
1076/// IMPORTANT NOTE: If you intend to use the errors of this histogram later
1077/// you should call Sumw2 before making this operation.
1078/// This is particularly important if you fit the histogram after TH1::Add
1079///
1080/// IMPORTANT NOTE2: You should be careful about the statistics of the
1081/// returned histogram, whose statistics may be binned or unbinned,
1082/// depending on whether c1 is negative, whether TAxis::kAxisRange is true,
1083/// and whether TH1::ResetStats has been called on either this or h1.
1084/// See TH1::GetStats.
1085///
1086/// ANOTHER SPECIAL CASE : h1 = h2 and c2 < 0
1087/// do a scaling this = c1 * h1 / (bin Volume)
1088///
1089/// The function returns kFALSE if the Add operation failed
1090
1092{
1093
1094 if (!h1 || !h2) {
1095 Error("Add","Attempt to add a non-existing histogram");
1096 return kFALSE;
1097 }
1098
1099 // delete buffer if it is there since it will become invalid
1100 if (fBuffer) BufferEmpty(1);
1101
1103 if (h1 == h2 && c2 < 0) {c2 = 0; normWidth = kTRUE;}
1104
1105 if (h1 != h2) {
1106 bool useMerge = false;
1107 const bool considerMerge = (c1 == 1. && c2 == 1. && !this->TestBit(kIsAverage) && !h1->TestBit(kIsAverage) );
1108
1109 // We can combine inconsistencies like this, since they are ordered and a
1110 // higher inconsistency is worse
1111 auto const inconsistency = std::max(LoggedInconsistency("Add", this, h1, considerMerge),
1112 LoggedInconsistency("Add", h1, h2, considerMerge));
1113
1114 // If there is a bad inconsistency and we can't even consider merging, just give up
1116 return false;
1117 }
1118 // If there is an inconsistency, we try to use merging
1121 }
1122
1123 if (useMerge) {
1124 TList l;
1125 // why TList takes non-const pointers ????
1126 l.Add(const_cast<TH1*>(h1));
1127 l.Add(const_cast<TH1*>(h2));
1128 Reset("ICE");
1129 auto iret = Merge(&l);
1130 return (iret >= 0);
1131 }
1132 }
1133
1134 // Create Sumw2 if h1 or h2 have Sumw2 set
1135 if (fSumw2.fN == 0 && (h1->GetSumw2N() != 0 || h2->GetSumw2N() != 0)) Sumw2();
1136
1137 // - Add statistics
1138 Double_t nEntries = TMath::Abs( c1*h1->GetEntries() + c2*h2->GetEntries() );
1139
1140 // TODO remove
1141 // statistics can be preserved only in case of positive coefficients
1142 // otherwise with negative c1 (histogram subtraction) one risks to get negative variances
1143 // also in case of scaling with the width we cannot preserve the statistics
1144 Double_t s1[kNstat] = {0};
1145 Double_t s2[kNstat] = {0};
1147
1148
1149 Bool_t resetStats = (c1*c2 < 0) || normWidth;
1150 if (!resetStats) {
1151 // need to initialize to zero s1 and s2 since
1152 // GetStats fills only used elements depending on dimension and type
1153 h1->GetStats(s1);
1154 h2->GetStats(s2);
1155 for (Int_t i=0;i<kNstat;i++) {
1156 if (i == 1) s3[i] = c1*c1*s1[i] + c2*c2*s2[i];
1157 //else s3[i] = TMath::Abs(c1)*s1[i] + TMath::Abs(c2)*s2[i];
1158 else s3[i] = c1*s1[i] + c2*s2[i];
1159 }
1160 }
1161
1162 SetMinimum();
1163 SetMaximum();
1164
1165 if (normWidth) { // DEPRECATED CASE: belongs to fitting / drawing modules
1166
1167 Int_t nbinsx = GetNbinsX() + 2; // normal bins + underflow, overflow
1168 Int_t nbinsy = GetNbinsY() + 2;
1169 Int_t nbinsz = GetNbinsZ() + 2;
1170
1171 if (fDimension < 2) nbinsy = 1;
1172 if (fDimension < 3) nbinsz = 1;
1173
1174 Int_t bin, binx, biny, binz;
1175 for (binz = 0; binz < nbinsz; ++binz) {
1177 for (biny = 0; biny < nbinsy; ++biny) {
1179 for (binx = 0; binx < nbinsx; ++binx) {
1181 bin = GetBin(binx, biny, binz);
1182 Double_t w = wx*wy*wz;
1183 UpdateBinContent(bin, c1 * h1->RetrieveBinContent(bin) / w);
1184 if (fSumw2.fN) {
1185 Double_t e1 = h1->GetBinError(bin)/w;
1186 fSumw2.fArray[bin] = c1*c1*e1*e1;
1187 }
1188 }
1189 }
1190 }
1191 } else if (h1->TestBit(kIsAverage) && h2->TestBit(kIsAverage)) {
1192 for (Int_t i = 0; i < fNcells; ++i) { // loop on cells (bins including underflow / overflow)
1193 // special case where histograms have the kIsAverage bit set
1195 Double_t y2 = h2->RetrieveBinContent(i);
1197 Double_t e2sq = h2->GetBinErrorSqUnchecked(i);
1198 Double_t w1 = 1., w2 = 1.;
1199
1200 // consider all special cases when bin errors are zero
1201 // see http://root-forum.cern.ch/viewtopic.php?f=3&t=13299
1202 if (e1sq) w1 = 1./ e1sq;
1203 else if (h1->fSumw2.fN) {
1204 w1 = 1.E200; // use an arbitrary huge value
1205 if (y1 == 0 ) { // use an estimated error from the global histogram scale
1206 double sf = (s1[0] != 0) ? s1[1]/s1[0] : 1;
1207 w1 = 1./(sf*sf);
1208 }
1209 }
1210 if (e2sq) w2 = 1./ e2sq;
1211 else if (h2->fSumw2.fN) {
1212 w2 = 1.E200; // use an arbitrary huge value
1213 if (y2 == 0) { // use an estimated error from the global histogram scale
1214 double sf = (s2[0] != 0) ? s2[1]/s2[0] : 1;
1215 w2 = 1./(sf*sf);
1216 }
1217 }
1218
1219 double y = (w1*y1 + w2*y2)/(w1 + w2);
1220 UpdateBinContent(i, y);
1221 if (fSumw2.fN) {
1222 double err2 = 1./(w1 + w2);
1223 if (err2 < 1.E-200) err2 = 0; // to remove arbitrary value when e1=0 AND e2=0
1224 fSumw2.fArray[i] = err2;
1225 }
1226 }
1227 } else { // case of simple histogram addition
1228 Double_t c1sq = c1 * c1;
1229 Double_t c2sq = c2 * c2;
1230 for (Int_t i = 0; i < fNcells; ++i) { // Loop on cells (bins including underflows/overflows)
1231 UpdateBinContent(i, c1 * h1->RetrieveBinContent(i) + c2 * h2->RetrieveBinContent(i));
1232 if (fSumw2.fN) {
1233 fSumw2.fArray[i] = c1sq * h1->GetBinErrorSqUnchecked(i) + c2sq * h2->GetBinErrorSqUnchecked(i);
1234 }
1235 }
1236 }
1237
1238 if (resetStats) {
1239 // statistics need to be reset in case coefficient are negative
1240 ResetStats();
1241 }
1242 else {
1243 // update statistics (do here to avoid changes by SetBinContent) FIXME remove???
1244 PutStats(s3);
1246 }
1247
1248 return kTRUE;
1249}
1250
1251////////////////////////////////////////////////////////////////////////////////
1252/// Sets the flag controlling the automatic add of histograms in memory
1253///
1254/// By default (fAddDirectory = kTRUE), histograms are automatically added
1255/// to the list of objects in memory.
1256/// Note that one histogram can be removed from its support directory
1257/// by calling h->SetDirectory(nullptr) or h->SetDirectory(dir) to add it
1258/// to the list of objects in the directory dir.
1259///
1260/// NOTE that this is a static function. To call it, use;
1261/// TH1::AddDirectory
1262
1264{
1265 fgAddDirectory = add;
1266}
1267
1268////////////////////////////////////////////////////////////////////////////////
1269/// Auxiliary function to get the power of 2 next (larger) or previous (smaller)
1270/// a given x
1271///
1272/// next = kTRUE : next larger
1273/// next = kFALSE : previous smaller
1274///
1275/// Used by the autobin power of 2 algorithm
1276
1278{
1279 Int_t nn;
1280 Double_t f2 = std::frexp(x, &nn);
1281 return ((next && x > 0.) || (!next && x <= 0.)) ? std::ldexp(std::copysign(1., f2), nn)
1282 : std::ldexp(std::copysign(1., f2), --nn);
1283}
1284
1285////////////////////////////////////////////////////////////////////////////////
1286/// Auxiliary function to get the next power of 2 integer value larger then n
1287///
1288/// Used by the autobin power of 2 algorithm
1289
1291{
1292 Int_t nn;
1293 Double_t f2 = std::frexp(n, &nn);
1294 if (TMath::Abs(f2 - .5) > 0.001)
1295 return (Int_t)std::ldexp(1., nn);
1296 return n;
1297}
1298
1299////////////////////////////////////////////////////////////////////////////////
1300/// Buffer-based estimate of the histogram range using the power of 2 algorithm.
1301///
1302/// Used by the autobin power of 2 algorithm.
1303///
1304/// Works on arguments (min and max from fBuffer) and internal inputs: fXmin,
1305/// fXmax, NBinsX (from fXaxis), ...
1306/// Result save internally in fXaxis.
1307///
1308/// Overloaded by TH2 and TH3.
1309///
1310/// Return -1 if internal inputs are inconsistent, 0 otherwise.
1311
1313{
1314 // We need meaningful raw limits
1315 if (xmi >= xma)
1316 return -1;
1317
1318 THLimitsFinder::GetLimitsFinder()->FindGoodLimits(this, xmi, xma);
1321
1322 // Now adjust
1323 if (TMath::Abs(xhma) > TMath::Abs(xhmi)) {
1324 // Start from the upper limit
1327 } else {
1328 // Start from the lower limit
1331 }
1332
1333 // Round the bins to the next power of 2; take into account the possible inflation
1334 // of the range
1335 Double_t rr = (xhma - xhmi) / (xma - xmi);
1337
1338 // Adjust using the same bin width and offsets
1339 Double_t bw = (xhma - xhmi) / nb;
1340 // Bins to left free on each side
1341 Double_t autoside = gEnv->GetValue("Hist.Binning.Auto.Side", 0.05);
1342 Int_t nbside = (Int_t)(nb * autoside);
1343
1344 // Side up
1345 Int_t nbup = (xhma - xma) / bw;
1346 if (nbup % 2 != 0)
1347 nbup++; // Must be even
1348 if (nbup != nbside) {
1349 // Accounts also for both case: larger or smaller
1350 xhma -= bw * (nbup - nbside);
1351 nb -= (nbup - nbside);
1352 }
1353
1354 // Side low
1355 Int_t nblw = (xmi - xhmi) / bw;
1356 if (nblw % 2 != 0)
1357 nblw++; // Must be even
1358 if (nblw != nbside) {
1359 // Accounts also for both case: larger or smaller
1360 xhmi += bw * (nblw - nbside);
1361 nb -= (nblw - nbside);
1362 }
1363
1364 // Set everything and project
1365 SetBins(nb, xhmi, xhma);
1366
1367 // Done
1368 return 0;
1369}
1370
1371/// Fill histogram with all entries in the buffer.
1372///
1373/// - action = -1 histogram is reset and refilled from the buffer (called by THistPainter::Paint)
1374/// - action = 0 histogram is reset and filled from the buffer. When the histogram is filled from the
1375/// buffer the value fBuffer[0] is set to a negative number (= - number of entries)
1376/// When calling with action == 0 the histogram is NOT refilled when fBuffer[0] is < 0
1377/// While when calling with action = -1 the histogram is reset and ALWAYS refilled independently if
1378/// the histogram was filled before. This is needed when drawing the histogram
1379/// - action = 1 histogram is filled and buffer is deleted
1380/// The buffer is automatically deleted when filling the histogram and the entries is
1381/// larger than the buffer size
1382
1384{
1385 // do we need to compute the bin size?
1386 if (!fBuffer) return 0;
1388
1389 // nbentries correspond to the number of entries of histogram
1390
1391 if (nbentries == 0) {
1392 // if action is 1 we delete the buffer
1393 // this will avoid infinite recursion
1394 if (action > 0) {
1395 delete [] fBuffer;
1396 fBuffer = nullptr;
1397 fBufferSize = 0;
1398 }
1399 return 0;
1400 }
1401 if (nbentries < 0 && action == 0) return 0; // case histogram has been already filled from the buffer
1402
1403 Double_t *buffer = fBuffer;
1404 if (nbentries < 0) {
1406 // a reset might call BufferEmpty() giving an infinite recursion
1407 // Protect it by setting fBuffer = nullptr
1408 fBuffer = nullptr;
1409 //do not reset the list of functions
1410 Reset("ICES");
1411 fBuffer = buffer;
1412 }
1413 if (CanExtendAllAxes() || (fXaxis.GetXmax() <= fXaxis.GetXmin())) {
1414 //find min, max of entries in buffer
1417 for (Int_t i=0;i<nbentries;i++) {
1418 Double_t x = fBuffer[2*i+2];
1419 // skip infinity or NaN values
1420 if (!std::isfinite(x)) continue;
1421 if (x < xmin) xmin = x;
1422 if (x > xmax) xmax = x;
1423 }
1424 if (fXaxis.GetXmax() <= fXaxis.GetXmin()) {
1425 Int_t rc = -1;
1427 if ((rc = AutoP2FindLimits(xmin, xmax)) < 0)
1428 Warning("BufferEmpty",
1429 "inconsistency found by power-of-2 autobin algorithm: fallback to standard method");
1430 }
1431 if (rc < 0)
1432 THLimitsFinder::GetLimitsFinder()->FindGoodLimits(this, xmin, xmax);
1433 } else {
1434 fBuffer = nullptr;
1437 if (xmax >= fXaxis.GetXmax()) ExtendAxis(xmax, &fXaxis);
1438 fBuffer = buffer;
1439 fBufferSize = keep;
1440 }
1441 }
1442
1443 // call DoFillN which will not put entries in the buffer as FillN does
1444 // set fBuffer to zero to avoid re-emptying the buffer from functions called
1445 // by DoFillN (e.g Sumw2)
1446 buffer = fBuffer; fBuffer = nullptr;
1447 DoFillN(nbentries,&buffer[2],&buffer[1],2);
1448 fBuffer = buffer;
1449
1450 // if action == 1 - delete the buffer
1451 if (action > 0) {
1452 delete [] fBuffer;
1453 fBuffer = nullptr;
1454 fBufferSize = 0;
1455 } else {
1456 // if number of entries is consistent with buffer - set it negative to avoid
1457 // refilling the histogram every time BufferEmpty(0) is called
1458 // In case it is not consistent, by setting fBuffer[0]=0 is like resetting the buffer
1459 // (it will not be used anymore the next time BufferEmpty is called)
1460 if (nbentries == (Int_t)fEntries)
1461 fBuffer[0] = -nbentries;
1462 else
1463 fBuffer[0] = 0;
1464 }
1465 return nbentries;
1466}
1467
1468////////////////////////////////////////////////////////////////////////////////
1469/// accumulate arguments in buffer. When buffer is full, empty the buffer
1470///
1471/// - `fBuffer[0]` = number of entries in buffer
1472/// - `fBuffer[1]` = w of first entry
1473/// - `fBuffer[2]` = x of first entry
1474
1476{
1477 if (!fBuffer) return -2;
1479
1480
1481 if (nbentries < 0) {
1482 // reset nbentries to a positive value so next time BufferEmpty() is called
1483 // the histogram will be refilled
1485 fBuffer[0] = nbentries;
1486 if (fEntries > 0) {
1487 // set fBuffer to zero to avoid calling BufferEmpty in Reset
1488 Double_t *buffer = fBuffer; fBuffer=nullptr;
1489 Reset("ICES"); // do not reset list of functions
1490 fBuffer = buffer;
1491 }
1492 }
1493 if (2*nbentries+2 >= fBufferSize) {
1494 BufferEmpty(1);
1495 if (!fBuffer)
1496 // to avoid infinite recursion Fill->BufferFill->Fill
1497 return Fill(x,w);
1498 // this cannot happen
1499 R__ASSERT(0);
1500 }
1501 fBuffer[2*nbentries+1] = w;
1502 fBuffer[2*nbentries+2] = x;
1503 fBuffer[0] += 1;
1504 return -2;
1505}
1506
1507////////////////////////////////////////////////////////////////////////////////
1508/// Check bin limits.
1509
1510bool TH1::CheckBinLimits(const TAxis* a1, const TAxis * a2)
1511{
1512 const TArrayD * h1Array = a1->GetXbins();
1513 const TArrayD * h2Array = a2->GetXbins();
1514 Int_t fN = h1Array->fN;
1515 if ( fN != 0 ) {
1516 if ( h2Array->fN != fN ) {
1517 return false;
1518 }
1519 else {
1520 for ( int i = 0; i < fN; ++i ) {
1521 // for i==fN (nbin+1) a->GetBinWidth() returns last bin width
1522 // we do not need to exclude that case
1523 double binWidth = a1->GetBinWidth(i);
1524 if ( ! TMath::AreEqualAbs( h1Array->GetAt(i), h2Array->GetAt(i), binWidth*1E-10 ) ) {
1525 return false;
1526 }
1527 }
1528 }
1529 }
1530
1531 return true;
1532}
1533
1534////////////////////////////////////////////////////////////////////////////////
1535/// Check that axis have same labels.
1536
1537bool TH1::CheckBinLabels(const TAxis* a1, const TAxis * a2)
1538{
1539 THashList *l1 = a1->GetLabels();
1540 THashList *l2 = a2->GetLabels();
1541
1542 if (!l1 && !l2 )
1543 return true;
1544 if (!l1 || !l2 ) {
1545 return false;
1546 }
1547 // check now labels sizes are the same
1548 if (l1->GetSize() != l2->GetSize() ) {
1549 return false;
1550 }
1551 for (int i = 1; i <= a1->GetNbins(); ++i) {
1552 TString label1 = a1->GetBinLabel(i);
1553 TString label2 = a2->GetBinLabel(i);
1554 if (label1 != label2) {
1555 return false;
1556 }
1557 }
1558
1559 return true;
1560}
1561
1562////////////////////////////////////////////////////////////////////////////////
1563/// Check that the axis limits of the histograms are the same.
1564/// If a first and last bin is passed the axis is compared between the given range
1565
1566bool TH1::CheckAxisLimits(const TAxis *a1, const TAxis *a2 )
1567{
1568 double firstBin = a1->GetBinWidth(1);
1569 double lastBin = a1->GetBinWidth( a1->GetNbins() );
1570 if ( ! TMath::AreEqualAbs(a1->GetXmin(), a2->GetXmin(), firstBin* 1.E-10) ||
1571 ! TMath::AreEqualAbs(a1->GetXmax(), a2->GetXmax(), lastBin*1.E-10) ) {
1572 return false;
1573 }
1574 return true;
1575}
1576
1577////////////////////////////////////////////////////////////////////////////////
1578/// Check that the axis are the same
1579
1580bool TH1::CheckEqualAxes(const TAxis *a1, const TAxis *a2 )
1581{
1582 if (a1->GetNbins() != a2->GetNbins() ) {
1583 ::Info("CheckEqualAxes","Axes have different number of bins : nbin1 = %d nbin2 = %d",a1->GetNbins(),a2->GetNbins() );
1584 return false;
1585 }
1586 if(!CheckAxisLimits(a1,a2)) {
1587 ::Info("CheckEqualAxes","Axes have different limits");
1588 return false;
1589 }
1590 if(!CheckBinLimits(a1,a2)) {
1591 ::Info("CheckEqualAxes","Axes have different bin limits");
1592 return false;
1593 }
1594
1595 // check labels
1596 if(!CheckBinLabels(a1,a2)) {
1597 ::Info("CheckEqualAxes","Axes have different labels");
1598 return false;
1599 }
1600
1601 return true;
1602}
1603
1604////////////////////////////////////////////////////////////////////////////////
1605/// Check that two sub axis are the same.
1606/// The limits are defined by first bin and last bin
1607/// N.B. no check is done in this case for variable bins
1608
1610{
1611 // By default is assumed that no bins are given for the second axis
1613 Double_t xmin1 = a1->GetBinLowEdge(firstBin1);
1614 Double_t xmax1 = a1->GetBinUpEdge(lastBin1);
1615
1616 Int_t nbins2 = a2->GetNbins();
1617 Double_t xmin2 = a2->GetXmin();
1618 Double_t xmax2 = a2->GetXmax();
1619
1620 if (firstBin2 < lastBin2) {
1621 // in this case assume no bins are given for the second axis
1623 xmin2 = a1->GetBinLowEdge(firstBin1);
1624 xmax2 = a1->GetBinUpEdge(lastBin1);
1625 }
1626
1627 if (nbins1 != nbins2 ) {
1628 ::Info("CheckConsistentSubAxes","Axes have different number of bins");
1629 return false;
1630 }
1631
1632 Double_t firstBin = a1->GetBinWidth(firstBin1);
1633 Double_t lastBin = a1->GetBinWidth(lastBin1);
1634 if ( ! TMath::AreEqualAbs(xmin1,xmin2,1.E-10 * firstBin) ||
1635 ! TMath::AreEqualAbs(xmax1,xmax2,1.E-10 * lastBin) ) {
1636 ::Info("CheckConsistentSubAxes","Axes have different limits");
1637 return false;
1638 }
1639
1640 return true;
1641}
1642
1643////////////////////////////////////////////////////////////////////////////////
1644/// Check histogram compatibility.
1645/// The returned integer is part of EInconsistencyBits
1646/// The value 0 means that the histograms are compatible
1647
1649{
1650 if (h1 == h2) return kFullyConsistent;
1651
1652 if (h1->GetDimension() != h2->GetDimension() ) {
1653 return kDifferentDimensions;
1654 }
1655 Int_t dim = h1->GetDimension();
1656
1657 // returns kTRUE if number of bins and bin limits are identical
1658 Int_t nbinsx = h1->GetNbinsX();
1659 Int_t nbinsy = h1->GetNbinsY();
1660 Int_t nbinsz = h1->GetNbinsZ();
1661
1662 // Check whether the histograms have the same number of bins.
1663 if (nbinsx != h2->GetNbinsX() ||
1664 (dim > 1 && nbinsy != h2->GetNbinsY()) ||
1665 (dim > 2 && nbinsz != h2->GetNbinsZ()) ) {
1667 }
1668
1669 bool ret = true;
1670
1671 // check axis limits
1672 ret &= CheckAxisLimits(h1->GetXaxis(), h2->GetXaxis());
1673 if (dim > 1) ret &= CheckAxisLimits(h1->GetYaxis(), h2->GetYaxis());
1674 if (dim > 2) ret &= CheckAxisLimits(h1->GetZaxis(), h2->GetZaxis());
1675 if (!ret) return kDifferentAxisLimits;
1676
1677 // check bin limits
1678 ret &= CheckBinLimits(h1->GetXaxis(), h2->GetXaxis());
1679 if (dim > 1) ret &= CheckBinLimits(h1->GetYaxis(), h2->GetYaxis());
1680 if (dim > 2) ret &= CheckBinLimits(h1->GetZaxis(), h2->GetZaxis());
1681 if (!ret) return kDifferentBinLimits;
1682
1683 // check labels if histograms are both not empty
1684 if ( !h1->IsEmpty() && !h2->IsEmpty() ) {
1685 ret &= CheckBinLabels(h1->GetXaxis(), h2->GetXaxis());
1686 if (dim > 1) ret &= CheckBinLabels(h1->GetYaxis(), h2->GetYaxis());
1687 if (dim > 2) ret &= CheckBinLabels(h1->GetZaxis(), h2->GetZaxis());
1688 if (!ret) return kDifferentLabels;
1689 }
1690
1691 return kFullyConsistent;
1692}
1693
1694////////////////////////////////////////////////////////////////////////////////
1695/// \f$ \chi^{2} \f$ test for comparing weighted and unweighted histograms.
1696///
1697/// Compares the histograms' adjusted (normalized) residuals.
1698/// Function: Returns p-value. Other return values are specified by the 3rd parameter
1699///
1700/// \param[in] h2 the second histogram
1701/// \param[in] option
1702/// - "UU" = experiment experiment comparison (unweighted-unweighted)
1703/// - "UW" = experiment MC comparison (unweighted-weighted). Note that
1704/// the first histogram should be unweighted
1705/// - "WW" = MC MC comparison (weighted-weighted)
1706/// - "NORM" = to be used when one or both of the histograms is scaled
1707/// but the histogram originally was unweighted
1708/// - by default underflows and overflows are not included:
1709/// * "OF" = overflows included
1710/// * "UF" = underflows included
1711/// - "P" = print chi2, ndf, p_value, igood
1712/// - "CHI2" = returns chi2 instead of p-value
1713/// - "CHI2/NDF" = returns \f$ \chi^{2} \f$/ndf
1714/// \param[in] res not empty - computes normalized residuals and returns them in this array
1715///
1716/// The current implementation is based on the papers \f$ \chi^{2} \f$ test for comparison
1717/// of weighted and unweighted histograms" in Proceedings of PHYSTAT05 and
1718/// "Comparison weighted and unweighted histograms", arXiv:physics/0605123
1719/// by N.Gagunashvili. This function has been implemented by Daniel Haertl in August 2006.
1720///
1721/// #### Introduction:
1722///
1723/// A frequently used technique in data analysis is the comparison of
1724/// histograms. First suggested by Pearson [1] the \f$ \chi^{2} \f$ test of
1725/// homogeneity is used widely for comparing usual (unweighted) histograms.
1726/// This paper describes the implementation modified \f$ \chi^{2} \f$ tests
1727/// for comparison of weighted and unweighted histograms and two weighted
1728/// histograms [2] as well as usual Pearson's \f$ \chi^{2} \f$ test for
1729/// comparison two usual (unweighted) histograms.
1730///
1731/// #### Overview:
1732///
1733/// Comparison of two histograms expect hypotheses that two histograms
1734/// represent identical distributions. To make a decision p-value should
1735/// be calculated. The hypotheses of identity is rejected if the p-value is
1736/// lower then some significance level. Traditionally significance levels
1737/// 0.1, 0.05 and 0.01 are used. The comparison procedure should include an
1738/// analysis of the residuals which is often helpful in identifying the
1739/// bins of histograms responsible for a significant overall \f$ \chi^{2} \f$ value.
1740/// Residuals are the difference between bin contents and expected bin
1741/// contents. Most convenient for analysis are the normalized residuals. If
1742/// hypotheses of identity are valid then normalized residuals are
1743/// approximately independent and identically distributed random variables
1744/// having N(0,1) distribution. Analysis of residuals expect test of above
1745/// mentioned properties of residuals. Notice that indirectly the analysis
1746/// of residuals increase the power of \f$ \chi^{2} \f$ test.
1747///
1748/// #### Methods of comparison:
1749///
1750/// \f$ \chi^{2} \f$ test for comparison two (unweighted) histograms:
1751/// Let us consider two histograms with the same binning and the number
1752/// of bins equal to r. Let us denote the number of events in the ith bin
1753/// in the first histogram as ni and as mi in the second one. The total
1754/// number of events in the first histogram is equal to:
1755/// \f[
1756/// N = \sum_{i=1}^{r} n_{i}
1757/// \f]
1758/// and
1759/// \f[
1760/// M = \sum_{i=1}^{r} m_{i}
1761/// \f]
1762/// in the second histogram. The hypothesis of identity (homogeneity) [3]
1763/// is that the two histograms represent random values with identical
1764/// distributions. It is equivalent that there exist r constants p1,...,pr,
1765/// such that
1766/// \f[
1767///\sum_{i=1}^{r} p_{i}=1
1768/// \f]
1769/// and the probability of belonging to the ith bin for some measured value
1770/// in both experiments is equal to pi. The number of events in the ith
1771/// bin is a random variable with a distribution approximated by a Poisson
1772/// probability distribution
1773/// \f[
1774///\frac{e^{-Np_{i}}(Np_{i})^{n_{i}}}{n_{i}!}
1775/// \f]
1776///for the first histogram and with distribution
1777/// \f[
1778///\frac{e^{-Mp_{i}}(Mp_{i})^{m_{i}}}{m_{i}!}
1779/// \f]
1780/// for the second histogram. If the hypothesis of homogeneity is valid,
1781/// then the maximum likelihood estimator of pi, i=1,...,r, is
1782/// \f[
1783///\hat{p}_{i}= \frac{n_{i}+m_{i}}{N+M}
1784/// \f]
1785/// and then
1786/// \f[
1787/// X^{2} = \sum_{i=1}^{r}\frac{(n_{i}-N\hat{p}_{i})^{2}}{N\hat{p}_{i}} + \sum_{i=1}^{r}\frac{(m_{i}-M\hat{p}_{i})^{2}}{M\hat{p}_{i}} =\frac{1}{MN} \sum_{i=1}^{r}\frac{(Mn_{i}-Nm_{i})^{2}}{n_{i}+m_{i}}
1788/// \f]
1789/// has approximately a \f$ \chi^{2}_{(r-1)} \f$ distribution [3].
1790/// The comparison procedure can include an analysis of the residuals which
1791/// is often helpful in identifying the bins of histograms responsible for
1792/// a significant overall \f$ \chi^{2} \f$ value. Most convenient for
1793/// analysis are the adjusted (normalized) residuals [4]
1794/// \f[
1795/// r_{i} = \frac{n_{i}-N\hat{p}_{i}}{\sqrt{N\hat{p}_{i}}\sqrt{(1-N/(N+M))(1-(n_{i}+m_{i})/(N+M))}}
1796/// \f]
1797/// If hypotheses of homogeneity are valid then residuals ri are
1798/// approximately independent and identically distributed random variables
1799/// having N(0,1) distribution. The application of the \f$ \chi^{2} \f$ test has
1800/// restrictions related to the value of the expected frequencies Npi,
1801/// Mpi, i=1,...,r. A conservative rule formulated in [5] is that all the
1802/// expectations must be 1 or greater for both histograms. In practical
1803/// cases when expected frequencies are not known the estimated expected
1804/// frequencies \f$ M\hat{p}_{i}, N\hat{p}_{i}, i=1,...,r \f$ can be used.
1805///
1806/// #### Unweighted and weighted histograms comparison:
1807///
1808/// A simple modification of the ideas described above can be used for the
1809/// comparison of the usual (unweighted) and weighted histograms. Let us
1810/// denote the number of events in the ith bin in the unweighted
1811/// histogram as ni and the common weight of events in the ith bin of the
1812/// weighted histogram as wi. The total number of events in the
1813/// unweighted histogram is equal to
1814///\f[
1815/// N = \sum_{i=1}^{r} n_{i}
1816///\f]
1817/// and the total weight of events in the weighted histogram is equal to
1818///\f[
1819/// W = \sum_{i=1}^{r} w_{i}
1820///\f]
1821/// Let us formulate the hypothesis of identity of an unweighted histogram
1822/// to a weighted histogram so that there exist r constants p1,...,pr, such
1823/// that
1824///\f[
1825/// \sum_{i=1}^{r} p_{i} = 1
1826///\f]
1827/// for the unweighted histogram. The weight wi is a random variable with a
1828/// distribution approximated by the normal probability distribution
1829/// \f$ N(Wp_{i},\sigma_{i}^{2}) \f$ where \f$ \sigma_{i}^{2} \f$ is the variance of the weight wi.
1830/// If we replace the variance \f$ \sigma_{i}^{2} \f$
1831/// with estimate \f$ s_{i}^{2} \f$ (sum of squares of weights of
1832/// events in the ith bin) and the hypothesis of identity is valid, then the
1833/// maximum likelihood estimator of pi,i=1,...,r, is
1834///\f[
1835/// \hat{p}_{i} = \frac{Ww_{i}-Ns_{i}^{2}+\sqrt{(Ww_{i}-Ns_{i}^{2})^{2}+4W^{2}s_{i}^{2}n_{i}}}{2W^{2}}
1836///\f]
1837/// We may then use the test statistic
1838///\f[
1839/// X^{2} = \sum_{i=1}^{r} \frac{(n_{i}-N\hat{p}_{i})^{2}}{N\hat{p}_{i}} + \sum_{i=1}^{r} \frac{(w_{i}-W\hat{p}_{i})^{2}}{s_{i}^{2}}
1840///\f]
1841/// and it has approximately a \f$ \sigma^{2}_{(r-1)} \f$ distribution [2]. This test, as well
1842/// as the original one [3], has a restriction on the expected frequencies. The
1843/// expected frequencies recommended for the weighted histogram is more than 25.
1844/// The value of the minimal expected frequency can be decreased down to 10 for
1845/// the case when the weights of the events are close to constant. In the case
1846/// of a weighted histogram if the number of events is unknown, then we can
1847/// apply this recommendation for the equivalent number of events as
1848///\f[
1849/// n_{i}^{equiv} = \frac{ w_{i}^{2} }{ s_{i}^{2} }
1850///\f]
1851/// The minimal expected frequency for an unweighted histogram must be 1. Notice
1852/// that any usual (unweighted) histogram can be considered as a weighted
1853/// histogram with events that have constant weights equal to 1.
1854/// The variance \f$ z_{i}^{2} \f$ of the difference between the weight wi
1855/// and the estimated expectation value of the weight is approximately equal to:
1856///\f[
1857/// z_{i}^{2} = Var(w_{i}-W\hat{p}_{i}) = N\hat{p}_{i}(1-N\hat{p}_{i})\left(\frac{Ws_{i}^{2}}{\sqrt{(Ns_{i}^{2}-w_{i}W)^{2}+4W^{2}s_{i}^{2}n_{i}}}\right)^{2}+\frac{s_{i}^{2}}{4}\left(1+\frac{Ns_{i}^{2}-w_{i}W}{\sqrt{(Ns_{i}^{2}-w_{i}W)^{2}+4W^{2}s_{i}^{2}n_{i}}}\right)^{2}
1858///\f]
1859/// The residuals
1860///\f[
1861/// r_{i} = \frac{w_{i}-W\hat{p}_{i}}{z_{i}}
1862///\f]
1863/// have approximately a normal distribution with mean equal to 0 and standard
1864/// deviation equal to 1.
1865///
1866/// #### Two weighted histograms comparison:
1867///
1868/// Let us denote the common weight of events of the ith bin in the first
1869/// histogram as w1i and as w2i in the second one. The total weight of events
1870/// in the first histogram is equal to
1871///\f[
1872/// W_{1} = \sum_{i=1}^{r} w_{1i}
1873///\f]
1874/// and
1875///\f[
1876/// W_{2} = \sum_{i=1}^{r} w_{2i}
1877///\f]
1878/// in the second histogram. Let us formulate the hypothesis of identity of
1879/// weighted histograms so that there exist r constants p1,...,pr, such that
1880///\f[
1881/// \sum_{i=1}^{r} p_{i} = 1
1882///\f]
1883/// and also expectation value of weight w1i equal to W1pi and expectation value
1884/// of weight w2i equal to W2pi. Weights in both the histograms are random
1885/// variables with distributions which can be approximated by a normal
1886/// probability distribution \f$ N(W_{1}p_{i},\sigma_{1i}^{2}) \f$ for the first histogram
1887/// and by a distribution \f$ N(W_{2}p_{i},\sigma_{2i}^{2}) \f$ for the second.
1888/// Here \f$ \sigma_{1i}^{2} \f$ and \f$ \sigma_{2i}^{2} \f$ are the variances
1889/// of w1i and w2i with estimators \f$ s_{1i}^{2} \f$ and \f$ s_{2i}^{2} \f$ respectively.
1890/// If the hypothesis of identity is valid, then the maximum likelihood and
1891/// Least Square Method estimator of pi,i=1,...,r, is
1892///\f[
1893/// \hat{p}_{i} = \frac{w_{1i}W_{1}/s_{1i}^{2}+w_{2i}W_{2} /s_{2i}^{2}}{W_{1}^{2}/s_{1i}^{2}+W_{2}^{2}/s_{2i}^{2}}
1894///\f]
1895/// We may then use the test statistic
1896///\f[
1897/// X^{2} = \sum_{i=1}^{r} \frac{(w_{1i}-W_{1}\hat{p}_{i})^{2}}{s_{1i}^{2}} + \sum_{i=1}^{r} \frac{(w_{2i}-W_{2}\hat{p}_{i})^{2}}{s_{2i}^{2}} = \sum_{i=1}^{r} \frac{(W_{1}w_{2i}-W_{2}w_{1i})^{2}}{W_{1}^{2}s_{2i}^{2}+W_{2}^{2}s_{1i}^{2}}
1898///\f]
1899/// and it has approximately a \f$ \chi^{2}_{(r-1)} \f$ distribution [2].
1900/// The normalized or studentised residuals [6]
1901///\f[
1902/// r_{i} = \frac{w_{1i}-W_{1}\hat{p}_{i}}{s_{1i}\sqrt{1 - \frac{1}{(1+W_{2}^{2}s_{1i}^{2}/W_{1}^{2}s_{2i}^{2})}}}
1903///\f]
1904/// have approximately a normal distribution with mean equal to 0 and standard
1905/// deviation 1. A recommended minimal expected frequency is equal to 10 for
1906/// the proposed test.
1907///
1908/// #### Numerical examples:
1909///
1910/// The method described herein is now illustrated with an example.
1911/// We take a distribution
1912///\f[
1913/// \phi(x) = \frac{2}{(x-10)^{2}+1} + \frac{1}{(x-14)^{2}+1} (1)
1914///\f]
1915/// defined on the interval [4,16]. Events distributed according to the formula
1916/// (1) are simulated to create the unweighted histogram. Uniformly distributed
1917/// events are simulated for the weighted histogram with weights calculated by
1918/// formula (1). Each histogram has the same number of bins: 20. Fig.1 shows
1919/// the result of comparison of the unweighted histogram with 200 events
1920/// (minimal expected frequency equal to one) and the weighted histogram with
1921/// 500 events (minimal expected frequency equal to 25)
1922/// Begin_Macro
1923/// ../../../tutorials/math/chi2test.C
1924/// End_Macro
1925/// Fig 1. An example of comparison of the unweighted histogram with 200 events
1926/// and the weighted histogram with 500 events:
1927/// 1. unweighted histogram;
1928/// 2. weighted histogram;
1929/// 3. normalized residuals plot;
1930/// 4. normal Q-Q plot of residuals.
1931///
1932/// The value of the test statistic \f$ \chi^{2} \f$ is equal to
1933/// 21.09 with p-value equal to 0.33, therefore the hypothesis of identity of
1934/// the two histograms can be accepted for 0.05 significant level. The behavior
1935/// of the normalized residuals plot (see Fig. 1c) and the normal Q-Q plot
1936/// (see Fig. 1d) of residuals are regular and we cannot identify the outliers
1937/// or bins with a big influence on \f$ \chi^{2} \f$.
1938///
1939/// The second example presents the same two histograms but 17 events was added
1940/// to content of bin number 15 in unweighted histogram. Fig.2 shows the result
1941/// of comparison of the unweighted histogram with 217 events (minimal expected
1942/// frequency equal to one) and the weighted histogram with 500 events (minimal
1943/// expected frequency equal to 25)
1944/// Begin_Macro
1945/// ../../../tutorials/math/chi2test.C(17)
1946/// End_Macro
1947/// Fig 2. An example of comparison of the unweighted histogram with 217 events
1948/// and the weighted histogram with 500 events:
1949/// 1. unweighted histogram;
1950/// 2. weighted histogram;
1951/// 3. normalized residuals plot;
1952/// 4. normal Q-Q plot of residuals.
1953///
1954/// The value of the test statistic \f$ \chi^{2} \f$ is equal to
1955/// 32.33 with p-value equal to 0.029, therefore the hypothesis of identity of
1956/// the two histograms is rejected for 0.05 significant level. The behavior of
1957/// the normalized residuals plot (see Fig. 2c) and the normal Q-Q plot (see
1958/// Fig. 2d) of residuals are not regular and we can identify the outlier or
1959/// bin with a big influence on \f$ \chi^{2} \f$.
1960///
1961/// #### References:
1962///
1963/// - [1] Pearson, K., 1904. On the Theory of Contingency and Its Relation to
1964/// Association and Normal Correlation. Drapers' Co. Memoirs, Biometric
1965/// Series No. 1, London.
1966/// - [2] Gagunashvili, N., 2006. \f$ \sigma^{2} \f$ test for comparison
1967/// of weighted and unweighted histograms. Statistical Problems in Particle
1968/// Physics, Astrophysics and Cosmology, Proceedings of PHYSTAT05,
1969/// Oxford, UK, 12-15 September 2005, Imperial College Press, London, 43-44.
1970/// Gagunashvili,N., Comparison of weighted and unweighted histograms,
1971/// arXiv:physics/0605123, 2006.
1972/// - [3] Cramer, H., 1946. Mathematical methods of statistics.
1973/// Princeton University Press, Princeton.
1974/// - [4] Haberman, S.J., 1973. The analysis of residuals in cross-classified tables.
1975/// Biometrics 29, 205-220.
1976/// - [5] Lewontin, R.C. and Felsenstein, J., 1965. The robustness of homogeneity
1977/// test in 2xN tables. Biometrics 21, 19-33.
1978/// - [6] Seber, G.A.F., Lee, A.J., 2003, Linear Regression Analysis.
1979/// John Wiley & Sons Inc., New York.
1980
1981Double_t TH1::Chi2Test(const TH1* h2, Option_t *option, Double_t *res) const
1982{
1983 Double_t chi2 = 0;
1984 Int_t ndf = 0, igood = 0;
1985
1986 TString opt = option;
1987 opt.ToUpper();
1988
1990
1991 if(opt.Contains("P")) {
1992 printf("Chi2 = %f, Prob = %g, NDF = %d, igood = %d\n", chi2,prob,ndf,igood);
1993 }
1994 if(opt.Contains("CHI2/NDF")) {
1995 if (ndf == 0) return 0;
1996 return chi2/ndf;
1997 }
1998 if(opt.Contains("CHI2")) {
1999 return chi2;
2000 }
2001
2002 return prob;
2003}
2004
2005////////////////////////////////////////////////////////////////////////////////
2006/// The computation routine of the Chisquare test. For the method description,
2007/// see Chi2Test() function.
2008///
2009/// \return p-value
2010/// \param[in] h2 the second histogram
2011/// \param[in] option
2012/// - "UU" = experiment experiment comparison (unweighted-unweighted)
2013/// - "UW" = experiment MC comparison (unweighted-weighted). Note that the first
2014/// histogram should be unweighted
2015/// - "WW" = MC MC comparison (weighted-weighted)
2016/// - "NORM" = if one or both histograms is scaled
2017/// - "OF" = overflows included
2018/// - "UF" = underflows included
2019/// by default underflows and overflows are not included
2020/// \param[out] igood test output
2021/// - igood=0 - no problems
2022/// - For unweighted unweighted comparison
2023/// - igood=1'There is a bin in the 1st histogram with less than 1 event'
2024/// - igood=2'There is a bin in the 2nd histogram with less than 1 event'
2025/// - igood=3'when the conditions for igood=1 and igood=2 are satisfied'
2026/// - For unweighted weighted comparison
2027/// - igood=1'There is a bin in the 1st histogram with less then 1 event'
2028/// - igood=2'There is a bin in the 2nd histogram with less then 10 effective number of events'
2029/// - igood=3'when the conditions for igood=1 and igood=2 are satisfied'
2030/// - For weighted weighted comparison
2031/// - igood=1'There is a bin in the 1st histogram with less then 10 effective
2032/// number of events'
2033/// - igood=2'There is a bin in the 2nd histogram with less then 10 effective
2034/// number of events'
2035/// - igood=3'when the conditions for igood=1 and igood=2 are satisfied'
2036/// \param[out] chi2 chisquare of the test
2037/// \param[out] ndf number of degrees of freedom (important, when both histograms have the same empty bins)
2038/// \param[out] res normalized residuals for further analysis
2039
2041{
2042
2046
2047 Double_t sum1 = 0.0, sumw1 = 0.0;
2048 Double_t sum2 = 0.0, sumw2 = 0.0;
2049
2050 chi2 = 0.0;
2051 ndf = 0;
2052
2053 TString opt = option;
2054 opt.ToUpper();
2055
2056 if (fBuffer) const_cast<TH1*>(this)->BufferEmpty();
2057
2058 const TAxis *xaxis1 = GetXaxis();
2059 const TAxis *xaxis2 = h2->GetXaxis();
2060 const TAxis *yaxis1 = GetYaxis();
2061 const TAxis *yaxis2 = h2->GetYaxis();
2062 const TAxis *zaxis1 = GetZaxis();
2063 const TAxis *zaxis2 = h2->GetZaxis();
2064
2065 Int_t nbinx1 = xaxis1->GetNbins();
2066 Int_t nbinx2 = xaxis2->GetNbins();
2067 Int_t nbiny1 = yaxis1->GetNbins();
2068 Int_t nbiny2 = yaxis2->GetNbins();
2069 Int_t nbinz1 = zaxis1->GetNbins();
2070 Int_t nbinz2 = zaxis2->GetNbins();
2071
2072 //check dimensions
2073 if (this->GetDimension() != h2->GetDimension() ){
2074 Error("Chi2TestX","Histograms have different dimensions.");
2075 return 0.0;
2076 }
2077
2078 //check number of channels
2079 if (nbinx1 != nbinx2) {
2080 Error("Chi2TestX","different number of x channels");
2081 }
2082 if (nbiny1 != nbiny2) {
2083 Error("Chi2TestX","different number of y channels");
2084 }
2085 if (nbinz1 != nbinz2) {
2086 Error("Chi2TestX","different number of z channels");
2087 }
2088
2089 //check for ranges
2090 i_start = j_start = k_start = 1;
2091 i_end = nbinx1;
2092 j_end = nbiny1;
2093 k_end = nbinz1;
2094
2095 if (xaxis1->TestBit(TAxis::kAxisRange)) {
2096 i_start = xaxis1->GetFirst();
2097 i_end = xaxis1->GetLast();
2098 }
2099 if (yaxis1->TestBit(TAxis::kAxisRange)) {
2100 j_start = yaxis1->GetFirst();
2101 j_end = yaxis1->GetLast();
2102 }
2103 if (zaxis1->TestBit(TAxis::kAxisRange)) {
2104 k_start = zaxis1->GetFirst();
2105 k_end = zaxis1->GetLast();
2106 }
2107
2108
2109 if (opt.Contains("OF")) {
2110 if (GetDimension() == 3) k_end = ++nbinz1;
2111 if (GetDimension() >= 2) j_end = ++nbiny1;
2112 if (GetDimension() >= 1) i_end = ++nbinx1;
2113 }
2114
2115 if (opt.Contains("UF")) {
2116 if (GetDimension() == 3) k_start = 0;
2117 if (GetDimension() >= 2) j_start = 0;
2118 if (GetDimension() >= 1) i_start = 0;
2119 }
2120
2121 ndf = (i_end - i_start + 1) * (j_end - j_start + 1) * (k_end - k_start + 1) - 1;
2122
2123 Bool_t comparisonUU = opt.Contains("UU");
2124 Bool_t comparisonUW = opt.Contains("UW");
2125 Bool_t comparisonWW = opt.Contains("WW");
2126 Bool_t scaledHistogram = opt.Contains("NORM");
2127
2128 if (scaledHistogram && !comparisonUU) {
2129 Info("Chi2TestX", "NORM option should be used together with UU option. It is ignored");
2130 }
2131
2132 // look at histo global bin content and effective entries
2133 Stat_t s[kNstat];
2134 GetStats(s);// s[1] sum of squares of weights, s[0] sum of weights
2135 Double_t sumBinContent1 = s[0];
2136 Double_t effEntries1 = (s[1] ? s[0] * s[0] / s[1] : 0.0);
2137
2138 h2->GetStats(s);// s[1] sum of squares of weights, s[0] sum of weights
2139 Double_t sumBinContent2 = s[0];
2140 Double_t effEntries2 = (s[1] ? s[0] * s[0] / s[1] : 0.0);
2141
2142 if (!comparisonUU && !comparisonUW && !comparisonWW ) {
2143 // deduce automatically from type of histogram
2146 else comparisonUW = true;
2147 }
2148 else comparisonWW = true;
2149 }
2150 // check unweighted histogram
2151 if (comparisonUW) {
2153 Warning("Chi2TestX","First histogram is not unweighted and option UW has been requested");
2154 }
2155 }
2156 if ( (!scaledHistogram && comparisonUU) ) {
2158 Warning("Chi2TestX","Both histograms are not unweighted and option UU has been requested");
2159 }
2160 }
2161
2162
2163 //get number of events in histogram
2165 for (Int_t i = i_start; i <= i_end; ++i) {
2166 for (Int_t j = j_start; j <= j_end; ++j) {
2167 for (Int_t k = k_start; k <= k_end; ++k) {
2168
2169 Int_t bin = GetBin(i, j, k);
2170
2172 Double_t cnt2 = h2->RetrieveBinContent(bin);
2174 Double_t e2sq = h2->GetBinErrorSqUnchecked(bin);
2175
2176 if (e1sq > 0.0) cnt1 = TMath::Floor(cnt1 * cnt1 / e1sq + 0.5); // avoid rounding errors
2177 else cnt1 = 0.0;
2178
2179 if (e2sq > 0.0) cnt2 = TMath::Floor(cnt2 * cnt2 / e2sq + 0.5); // avoid rounding errors
2180 else cnt2 = 0.0;
2181
2182 // sum contents
2183 sum1 += cnt1;
2184 sum2 += cnt2;
2185 sumw1 += e1sq;
2186 sumw2 += e2sq;
2187 }
2188 }
2189 }
2190 if (sumw1 <= 0.0 || sumw2 <= 0.0) {
2191 Error("Chi2TestX", "Cannot use option NORM when one histogram has all zero errors");
2192 return 0.0;
2193 }
2194
2195 } else {
2196 for (Int_t i = i_start; i <= i_end; ++i) {
2197 for (Int_t j = j_start; j <= j_end; ++j) {
2198 for (Int_t k = k_start; k <= k_end; ++k) {
2199
2200 Int_t bin = GetBin(i, j, k);
2201
2202 sum1 += RetrieveBinContent(bin);
2203 sum2 += h2->RetrieveBinContent(bin);
2204
2206 if ( comparisonUW || comparisonWW ) sumw2 += h2->GetBinErrorSqUnchecked(bin);
2207 }
2208 }
2209 }
2210 }
2211 //checks that the histograms are not empty
2212 if (sum1 == 0.0 || sum2 == 0.0) {
2213 Error("Chi2TestX","one histogram is empty");
2214 return 0.0;
2215 }
2216
2217 if ( comparisonWW && ( sumw1 <= 0.0 && sumw2 <= 0.0 ) ){
2218 Error("Chi2TestX","Hist1 and Hist2 have both all zero errors\n");
2219 return 0.0;
2220 }
2221
2222 //THE TEST
2223 Int_t m = 0, n = 0;
2224
2225 //Experiment - experiment comparison
2226 if (comparisonUU) {
2227 Double_t sum = sum1 + sum2;
2228 for (Int_t i = i_start; i <= i_end; ++i) {
2229 for (Int_t j = j_start; j <= j_end; ++j) {
2230 for (Int_t k = k_start; k <= k_end; ++k) {
2231
2232 Int_t bin = GetBin(i, j, k);
2233
2235 Double_t cnt2 = h2->RetrieveBinContent(bin);
2236
2237 if (scaledHistogram) {
2238 // scale bin value to effective bin entries
2240 Double_t e2sq = h2->GetBinErrorSqUnchecked(bin);
2241
2242 if (e1sq > 0) cnt1 = TMath::Floor(cnt1 * cnt1 / e1sq + 0.5); // avoid rounding errors
2243 else cnt1 = 0;
2244
2245 if (e2sq > 0) cnt2 = TMath::Floor(cnt2 * cnt2 / e2sq + 0.5); // avoid rounding errors
2246 else cnt2 = 0;
2247 }
2248
2249 if (Int_t(cnt1) == 0 && Int_t(cnt2) == 0) --ndf; // no data means one degree of freedom less
2250 else {
2251
2254 //Double_t nexp2 = binsum*sum2/sum;
2255
2256 if (res) res[i - i_start] = (cnt1 - nexp1) / TMath::Sqrt(nexp1);
2257
2258 if (cnt1 < 1) ++m;
2259 if (cnt2 < 1) ++n;
2260
2261 //Habermann correction for residuals
2262 Double_t correc = (1. - sum1 / sum) * (1. - cntsum / sum);
2263 if (res) res[i - i_start] /= TMath::Sqrt(correc);
2264
2265 Double_t delta = sum2 * cnt1 - sum1 * cnt2;
2266 chi2 += delta * delta / cntsum;
2267 }
2268 }
2269 }
2270 }
2271 chi2 /= sum1 * sum2;
2272
2273 // flag error only when of the two histogram is zero
2274 if (m) {
2275 igood += 1;
2276 Info("Chi2TestX","There is a bin in h1 with less than 1 event.\n");
2277 }
2278 if (n) {
2279 igood += 2;
2280 Info("Chi2TestX","There is a bin in h2 with less than 1 event.\n");
2281 }
2282
2284 return prob;
2285
2286 }
2287
2288 // unweighted - weighted comparison
2289 // case of error = 0 and content not zero is treated without problems by excluding second chi2 sum
2290 // and can be considered as a data-theory comparison
2291 if ( comparisonUW ) {
2292 for (Int_t i = i_start; i <= i_end; ++i) {
2293 for (Int_t j = j_start; j <= j_end; ++j) {
2294 for (Int_t k = k_start; k <= k_end; ++k) {
2295
2296 Int_t bin = GetBin(i, j, k);
2297
2299 Double_t cnt2 = h2->RetrieveBinContent(bin);
2300 Double_t e2sq = h2->GetBinErrorSqUnchecked(bin);
2301
2302 // case both histogram have zero bin contents
2303 if (cnt1 * cnt1 == 0 && cnt2 * cnt2 == 0) {
2304 --ndf; //no data means one degree of freedom less
2305 continue;
2306 }
2307
2308 // case weighted histogram has zero bin content and error
2309 if (cnt2 * cnt2 == 0 && e2sq == 0) {
2310 if (sumw2 > 0) {
2311 // use as approximated error as 1 scaled by a scaling ratio
2312 // estimated from the total sum weight and sum weight squared
2313 e2sq = sumw2 / sum2;
2314 }
2315 else {
2316 // return error because infinite discrepancy here:
2317 // bin1 != 0 and bin2 =0 in a histogram with all errors zero
2318 Error("Chi2TestX","Hist2 has in bin (%d,%d,%d) zero content and zero errors\n", i, j, k);
2319 chi2 = 0; return 0;
2320 }
2321 }
2322
2323 if (cnt1 < 1) m++;
2324 if (e2sq > 0 && cnt2 * cnt2 / e2sq < 10) n++;
2325
2326 Double_t var1 = sum2 * cnt2 - sum1 * e2sq;
2327 Double_t var2 = var1 * var1 + 4. * sum2 * sum2 * cnt1 * e2sq;
2328
2329 // if cnt1 is zero and cnt2 = 1 and sum1 = sum2 var1 = 0 && var2 == 0
2330 // approximate by incrementing cnt1
2331 // LM (this need to be fixed for numerical errors)
2332 while (var1 * var1 + cnt1 == 0 || var1 + var2 == 0) {
2333 sum1++;
2334 cnt1++;
2335 var1 = sum2 * cnt2 - sum1 * e2sq;
2336 var2 = var1 * var1 + 4. * sum2 * sum2 * cnt1 * e2sq;
2337 }
2339
2340 while (var1 + var2 == 0) {
2341 sum1++;
2342 cnt1++;
2343 var1 = sum2 * cnt2 - sum1 * e2sq;
2344 var2 = var1 * var1 + 4. * sum2 * sum2 * cnt1 * e2sq;
2345 while (var1 * var1 + cnt1 == 0 || var1 + var2 == 0) {
2346 sum1++;
2347 cnt1++;
2348 var1 = sum2 * cnt2 - sum1 * e2sq;
2349 var2 = var1 * var1 + 4. * sum2 * sum2 * cnt1 * e2sq;
2350 }
2352 }
2353
2354 Double_t probb = (var1 + var2) / (2. * sum2 * sum2);
2355
2358
2361
2362 chi2 += delta1 * delta1 / nexp1;
2363
2364 if (e2sq > 0) {
2365 chi2 += delta2 * delta2 / e2sq;
2366 }
2367
2368 if (res) {
2369 if (e2sq > 0) {
2370 Double_t temp1 = sum2 * e2sq / var2;
2371 Double_t temp2 = 1.0 + (sum1 * e2sq - sum2 * cnt2) / var2;
2372 temp2 = temp1 * temp1 * sum1 * probb * (1.0 - probb) + temp2 * temp2 * e2sq / 4.0;
2373 // invert sign here
2374 res[i - i_start] = - delta2 / TMath::Sqrt(temp2);
2375 }
2376 else
2377 res[i - i_start] = delta1 / TMath::Sqrt(nexp1);
2378 }
2379 }
2380 }
2381 }
2382
2383 if (m) {
2384 igood += 1;
2385 Info("Chi2TestX","There is a bin in h1 with less than 1 event.\n");
2386 }
2387 if (n) {
2388 igood += 2;
2389 Info("Chi2TestX","There is a bin in h2 with less than 10 effective events.\n");
2390 }
2391
2393
2394 return prob;
2395 }
2396
2397 // weighted - weighted comparison
2398 if (comparisonWW) {
2399 for (Int_t i = i_start; i <= i_end; ++i) {
2400 for (Int_t j = j_start; j <= j_end; ++j) {
2401 for (Int_t k = k_start; k <= k_end; ++k) {
2402
2403 Int_t bin = GetBin(i, j, k);
2405 Double_t cnt2 = h2->RetrieveBinContent(bin);
2407 Double_t e2sq = h2->GetBinErrorSqUnchecked(bin);
2408
2409 // case both histogram have zero bin contents
2410 // (use square of content to avoid numerical errors)
2411 if (cnt1 * cnt1 == 0 && cnt2 * cnt2 == 0) {
2412 --ndf; //no data means one degree of freedom less
2413 continue;
2414 }
2415
2416 if (e1sq == 0 && e2sq == 0) {
2417 // cannot treat case of booth histogram have zero zero errors
2418 Error("Chi2TestX","h1 and h2 both have bin %d,%d,%d with all zero errors\n", i,j,k);
2419 chi2 = 0; return 0;
2420 }
2421
2422 Double_t sigma = sum1 * sum1 * e2sq + sum2 * sum2 * e1sq;
2423 Double_t delta = sum2 * cnt1 - sum1 * cnt2;
2424 chi2 += delta * delta / sigma;
2425
2426 if (res) {
2427 Double_t temp = cnt1 * sum1 * e2sq + cnt2 * sum2 * e1sq;
2428 Double_t probb = temp / sigma;
2429 Double_t z = 0;
2430 if (e1sq > e2sq) {
2431 Double_t d1 = cnt1 - sum1 * probb;
2432 Double_t s1 = e1sq * ( 1. - e2sq * sum1 * sum1 / sigma );
2433 z = d1 / TMath::Sqrt(s1);
2434 }
2435 else {
2436 Double_t d2 = cnt2 - sum2 * probb;
2437 Double_t s2 = e2sq * ( 1. - e1sq * sum2 * sum2 / sigma );
2438 z = -d2 / TMath::Sqrt(s2);
2439 }
2440 res[i - i_start] = z;
2441 }
2442
2443 if (e1sq > 0 && cnt1 * cnt1 / e1sq < 10) m++;
2444 if (e2sq > 0 && cnt2 * cnt2 / e2sq < 10) n++;
2445 }
2446 }
2447 }
2448 if (m) {
2449 igood += 1;
2450 Info("Chi2TestX","There is a bin in h1 with less than 10 effective events.\n");
2451 }
2452 if (n) {
2453 igood += 2;
2454 Info("Chi2TestX","There is a bin in h2 with less than 10 effective events.\n");
2455 }
2457 return prob;
2458 }
2459 return 0;
2460}
2461////////////////////////////////////////////////////////////////////////////////
2462/// Compute and return the chisquare of this histogram with respect to a function
2463/// The chisquare is computed by weighting each histogram point by the bin error
2464/// By default the full range of the histogram is used, unless TAxis::SetRange or TAxis::SetRangeUser was called before.
2465/// Use option "R" for restricting the chisquare calculation to the given range of the function
2466/// Use option "L" for using the chisquare based on the poisson likelihood (Baker-Cousins Chisquare)
2467/// Use option "P" for using the Pearson chisquare based on the expected bin errors
2468
2470{
2471 if (!func) {
2472 Error("Chisquare","Function pointer is Null - return -1");
2473 return -1;
2474 }
2475
2476 TString opt(option); opt.ToUpper();
2477 bool useRange = opt.Contains("R");
2478 ROOT::Fit::EChisquareType type = ROOT::Fit::EChisquareType::kNeyman; // default chi2 with observed error
2481
2482 return ROOT::Fit::Chisquare(*this, *func, useRange, type);
2483}
2484
2485////////////////////////////////////////////////////////////////////////////////
2486/// Remove all the content from the underflow and overflow bins, without changing the number of entries
2487/// After calling this method, every undeflow and overflow bins will have content 0.0
2488/// The Sumw2 is also cleared, since there is no more content in the bins
2489
2491{
2492 for (Int_t bin = 0; bin < fNcells; ++bin)
2493 if (IsBinUnderflow(bin) || IsBinOverflow(bin)) {
2494 UpdateBinContent(bin, 0.0);
2495 if (fSumw2.fN) fSumw2.fArray[bin] = 0.0;
2496 }
2497}
2498
2499////////////////////////////////////////////////////////////////////////////////
2500/// Compute integral (normalized cumulative sum of bins) w/o under/overflows
2501/// The result is stored in fIntegral and used by the GetRandom functions.
2502/// This function is automatically called by GetRandom when the fIntegral
2503/// array does not exist or when the number of entries in the histogram
2504/// has changed since the previous call to GetRandom.
2505/// The resulting integral is normalized to 1.
2506/// If the routine is called with the onlyPositive flag set an error will
2507/// be produced in case of negative bin content and a NaN value returned
2508/// \return 1 if success, 0 if integral is zero, NAN if onlyPositive-test fails
2509
2511{
2512 if (fBuffer) BufferEmpty();
2513
2514 // delete previously computed integral (if any)
2515 if (fIntegral) delete [] fIntegral;
2516
2517 // - Allocate space to store the integral and compute integral
2521 Int_t nbins = nbinsx * nbinsy * nbinsz;
2522
2523 fIntegral = new Double_t[nbins + 2];
2524 Int_t ibin = 0; fIntegral[ibin] = 0;
2525
2526 for (Int_t binz=1; binz <= nbinsz; ++binz) {
2527 for (Int_t biny=1; biny <= nbinsy; ++biny) {
2528 for (Int_t binx=1; binx <= nbinsx; ++binx) {
2529 ++ibin;
2531 if (onlyPositive && y < 0) {
2532 Error("ComputeIntegral","Bin content is negative - return a NaN value");
2533 fIntegral[nbins] = TMath::QuietNaN();
2534 break;
2535 }
2536 fIntegral[ibin] = fIntegral[ibin - 1] + y;
2537 }
2538 }
2539 }
2540
2541 // - Normalize integral to 1
2542 if (fIntegral[nbins] == 0 ) {
2543 Error("ComputeIntegral", "Integral = 0, no hits in histogram bins (excluding over/underflow).");
2544 return 0;
2545 }
2546 for (Int_t bin=1; bin <= nbins; ++bin) fIntegral[bin] /= fIntegral[nbins];
2547 fIntegral[nbins+1] = fEntries;
2548 return fIntegral[nbins];
2549}
2550
2551////////////////////////////////////////////////////////////////////////////////
2552/// Return a pointer to the array of bins integral.
2553/// if the pointer fIntegral is null, TH1::ComputeIntegral is called
2554/// The array dimension is the number of bins in the histograms
2555/// including underflow and overflow (fNCells)
2556/// the last value integral[fNCells] is set to the number of entries of
2557/// the histogram
2558
2560{
2561 if (!fIntegral) ComputeIntegral();
2562 return fIntegral;
2563}
2564
2565////////////////////////////////////////////////////////////////////////////////
2566/// Return a pointer to a histogram containing the cumulative content.
2567/// The cumulative can be computed both in the forward (default) or backward
2568/// direction; the name of the new histogram is constructed from
2569/// the name of this histogram with the suffix "suffix" appended provided
2570/// by the user. If not provided a default suffix="_cumulative" is used.
2571///
2572/// The cumulative distribution is formed by filling each bin of the
2573/// resulting histogram with the sum of that bin and all previous
2574/// (forward == kTRUE) or following (forward = kFALSE) bins.
2575///
2576/// Note: while cumulative distributions make sense in one dimension, you
2577/// may not be getting what you expect in more than 1D because the concept
2578/// of a cumulative distribution is much trickier to define; make sure you
2579/// understand the order of summation before you use this method with
2580/// histograms of dimension >= 2.
2581///
2582/// Note 2: By default the cumulative is computed from bin 1 to Nbins
2583/// If an axis range is set, values between the minimum and maximum of the range
2584/// are set.
2585/// Setting an axis range can also be used for including underflow and overflow in
2586/// the cumulative (e.g. by setting h->GetXaxis()->SetRange(0, h->GetNbinsX()+1); )
2588
2589TH1 *TH1::GetCumulative(Bool_t forward, const char* suffix) const
2590{
2591 const Int_t firstX = fXaxis.GetFirst();
2592 const Int_t lastX = fXaxis.GetLast();
2593 const Int_t firstY = (fDimension > 1) ? fYaxis.GetFirst() : 1;
2594 const Int_t lastY = (fDimension > 1) ? fYaxis.GetLast() : 1;
2595 const Int_t firstZ = (fDimension > 1) ? fZaxis.GetFirst() : 1;
2596 const Int_t lastZ = (fDimension > 1) ? fZaxis.GetLast() : 1;
2597
2599 hintegrated->Reset();
2600 Double_t sum = 0.;
2601 Double_t esum = 0;
2602 if (forward) { // Forward computation
2603 for (Int_t binz = firstZ; binz <= lastZ; ++binz) {
2604 for (Int_t biny = firstY; biny <= lastY; ++biny) {
2605 for (Int_t binx = firstX; binx <= lastX; ++binx) {
2606 const Int_t bin = hintegrated->GetBin(binx, biny, binz);
2607 sum += RetrieveBinContent(bin);
2608 hintegrated->AddBinContent(bin, sum);
2609 if (fSumw2.fN) {
2611 hintegrated->fSumw2.fArray[bin] = esum;
2612 }
2613 }
2614 }
2615 }
2616 } else { // Backward computation
2617 for (Int_t binz = lastZ; binz >= firstZ; --binz) {
2618 for (Int_t biny = lastY; biny >= firstY; --biny) {
2619 for (Int_t binx = lastX; binx >= firstX; --binx) {
2620 const Int_t bin = hintegrated->GetBin(binx, biny, binz);
2621 sum += RetrieveBinContent(bin);
2622 hintegrated->AddBinContent(bin, sum);
2623 if (fSumw2.fN) {
2625 hintegrated->fSumw2.fArray[bin] = esum;
2626 }
2627 }
2628 }
2629 }
2630 }
2631 return hintegrated;
2632}
2633
2634////////////////////////////////////////////////////////////////////////////////
2635/// Copy this histogram structure to newth1.
2636///
2637/// Note that this function does not copy the list of associated functions.
2638/// Use TObject::Clone to make a full copy of a histogram.
2639///
2640/// Note also that the histogram it will be created in gDirectory (if AddDirectoryStatus()=true)
2641/// or will not be added to any directory if AddDirectoryStatus()=false
2642/// independently of the current directory stored in the original histogram
2643
2644void TH1::Copy(TObject &obj) const
2645{
2646 if (((TH1&)obj).fDirectory) {
2647 // We are likely to change the hash value of this object
2648 // with TNamed::Copy, to keep things correct, we need to
2649 // clean up its existing entries.
2650 ((TH1&)obj).fDirectory->Remove(&obj);
2651 ((TH1&)obj).fDirectory = nullptr;
2652 }
2653 TNamed::Copy(obj);
2654 ((TH1&)obj).fDimension = fDimension;
2655 ((TH1&)obj).fNormFactor= fNormFactor;
2656 ((TH1&)obj).fNcells = fNcells;
2657 ((TH1&)obj).fBarOffset = fBarOffset;
2658 ((TH1&)obj).fBarWidth = fBarWidth;
2659 ((TH1&)obj).fOption = fOption;
2660 ((TH1&)obj).fBinStatErrOpt = fBinStatErrOpt;
2661 ((TH1&)obj).fBufferSize= fBufferSize;
2662 // copy the Buffer
2663 // delete first a previously existing buffer
2664 if (((TH1&)obj).fBuffer != nullptr) {
2665 delete [] ((TH1&)obj).fBuffer;
2666 ((TH1&)obj).fBuffer = nullptr;
2667 }
2668 if (fBuffer) {
2669 Double_t *buf = new Double_t[fBufferSize];
2670 for (Int_t i=0;i<fBufferSize;i++) buf[i] = fBuffer[i];
2671 // obj.fBuffer has been deleted before
2672 ((TH1&)obj).fBuffer = buf;
2673 }
2674
2675 // copy bin contents (this should be done by the derived classes, since TH1 does not store the bin content)
2676 // Do this in case derived from TArray
2677 TArray* a = dynamic_cast<TArray*>(&obj);
2678 if (a) {
2679 a->Set(fNcells);
2680 for (Int_t i = 0; i < fNcells; i++)
2682 }
2683
2684 ((TH1&)obj).fEntries = fEntries;
2685
2686 // which will call BufferEmpty(0) and set fBuffer[0] to a Maybe one should call
2687 // assignment operator on the TArrayD
2688
2689 ((TH1&)obj).fTsumw = fTsumw;
2690 ((TH1&)obj).fTsumw2 = fTsumw2;
2691 ((TH1&)obj).fTsumwx = fTsumwx;
2692 ((TH1&)obj).fTsumwx2 = fTsumwx2;
2693 ((TH1&)obj).fMaximum = fMaximum;
2694 ((TH1&)obj).fMinimum = fMinimum;
2695
2696 TAttLine::Copy(((TH1&)obj));
2697 TAttFill::Copy(((TH1&)obj));
2698 TAttMarker::Copy(((TH1&)obj));
2699 fXaxis.Copy(((TH1&)obj).fXaxis);
2700 fYaxis.Copy(((TH1&)obj).fYaxis);
2701 fZaxis.Copy(((TH1&)obj).fZaxis);
2702 ((TH1&)obj).fXaxis.SetParent(&obj);
2703 ((TH1&)obj).fYaxis.SetParent(&obj);
2704 ((TH1&)obj).fZaxis.SetParent(&obj);
2705 fContour.Copy(((TH1&)obj).fContour);
2706 fSumw2.Copy(((TH1&)obj).fSumw2);
2707 // fFunctions->Copy(((TH1&)obj).fFunctions);
2708 // when copying an histogram if the AddDirectoryStatus() is true it
2709 // will be added to gDirectory independently of the fDirectory stored.
2710 // and if the AddDirectoryStatus() is false it will not be added to
2711 // any directory (fDirectory = nullptr)
2712 if (fgAddDirectory && gDirectory) {
2713 gDirectory->Append(&obj);
2714 ((TH1&)obj).fFunctions->UseRWLock();
2715 ((TH1&)obj).fDirectory = gDirectory;
2716 } else
2717 ((TH1&)obj).fDirectory = nullptr;
2718
2719}
2720
2721////////////////////////////////////////////////////////////////////////////////
2722/// Make a complete copy of the underlying object. If 'newname' is set,
2723/// the copy's name will be set to that name.
2724
2725TObject* TH1::Clone(const char* newname) const
2726{
2727 TH1* obj = (TH1*)IsA()->GetNew()(nullptr);
2728 Copy(*obj);
2729
2730 // Now handle the parts that Copy doesn't do
2731 if(fFunctions) {
2732 // The Copy above might have published 'obj' to the ListOfCleanups.
2733 // Clone can call RecursiveRemove, for example via TCheckHashRecursiveRemoveConsistency
2734 // when dictionary information is initialized, so we need to
2735 // keep obj->fFunction valid during its execution and
2736 // protect the update with the write lock.
2737
2738 // Reset stats parent - else cloning the stats will clone this histogram, too.
2739 auto oldstats = dynamic_cast<TVirtualPaveStats*>(fFunctions->FindObject("stats"));
2740 TObject *oldparent = nullptr;
2741 if (oldstats) {
2742 oldparent = oldstats->GetParent();
2743 oldstats->SetParent(nullptr);
2744 }
2745
2746 auto newlist = (TList*)fFunctions->Clone();
2747
2748 if (oldstats)
2749 oldstats->SetParent(oldparent);
2750 auto newstats = dynamic_cast<TVirtualPaveStats*>(obj->fFunctions->FindObject("stats"));
2751 if (newstats)
2752 newstats->SetParent(obj);
2753
2754 auto oldlist = obj->fFunctions;
2755 {
2757 obj->fFunctions = newlist;
2758 }
2759 delete oldlist;
2760 }
2761 if(newname && strlen(newname) ) {
2762 obj->SetName(newname);
2763 }
2764 return obj;
2765}
2766
2767////////////////////////////////////////////////////////////////////////////////
2768/// Perform the automatic addition of the histogram to the given directory
2769///
2770/// Note this function is called in place when the semantic requires
2771/// this object to be added to a directory (I.e. when being read from
2772/// a TKey or being Cloned)
2773
2775{
2777 if (addStatus) {
2778 SetDirectory(dir);
2779 if (dir) {
2781 }
2782 }
2783}
2784
2785////////////////////////////////////////////////////////////////////////////////
2786/// Compute distance from point px,py to a line.
2787///
2788/// Compute the closest distance of approach from point px,py to elements
2789/// of a histogram.
2790/// The distance is computed in pixels units.
2791///
2792/// #### Algorithm:
2793/// Currently, this simple model computes the distance from the mouse
2794/// to the histogram contour only.
2795
2797{
2798 if (!fPainter) return 9999;
2799 return fPainter->DistancetoPrimitive(px,py);
2800}
2801
2802////////////////////////////////////////////////////////////////////////////////
2803/// Performs the operation: `this = this/(c1*f1)`
2804/// if errors are defined (see TH1::Sumw2), errors are also recalculated.
2805///
2806/// Only bins inside the function range are recomputed.
2807/// IMPORTANT NOTE: If you intend to use the errors of this histogram later
2808/// you should call Sumw2 before making this operation.
2809/// This is particularly important if you fit the histogram after TH1::Divide
2810///
2811/// The function return kFALSE if the divide operation failed
2812
2814{
2815 if (!f1) {
2816 Error("Divide","Attempt to divide by a non-existing function");
2817 return kFALSE;
2818 }
2819
2820 // delete buffer if it is there since it will become invalid
2821 if (fBuffer) BufferEmpty(1);
2822
2823 Int_t nx = GetNbinsX() + 2; // normal bins + uf / of
2824 Int_t ny = GetNbinsY() + 2;
2825 Int_t nz = GetNbinsZ() + 2;
2826 if (fDimension < 2) ny = 1;
2827 if (fDimension < 3) nz = 1;
2828
2829
2830 SetMinimum();
2831 SetMaximum();
2832
2833 // - Loop on bins (including underflows/overflows)
2834 Int_t bin, binx, biny, binz;
2835 Double_t cu, w;
2836 Double_t xx[3];
2837 Double_t *params = nullptr;
2838 f1->InitArgs(xx,params);
2839 for (binz = 0; binz < nz; ++binz) {
2840 xx[2] = fZaxis.GetBinCenter(binz);
2841 for (biny = 0; biny < ny; ++biny) {
2842 xx[1] = fYaxis.GetBinCenter(biny);
2843 for (binx = 0; binx < nx; ++binx) {
2844 xx[0] = fXaxis.GetBinCenter(binx);
2845 if (!f1->IsInside(xx)) continue;
2847 bin = binx + nx * (biny + ny * binz);
2848 cu = c1 * f1->EvalPar(xx);
2849 if (TF1::RejectedPoint()) continue;
2850 if (cu) w = RetrieveBinContent(bin) / cu;
2851 else w = 0;
2852 UpdateBinContent(bin, w);
2853 if (fSumw2.fN) {
2854 if (cu != 0) fSumw2.fArray[bin] = GetBinErrorSqUnchecked(bin) / (cu * cu);
2855 else fSumw2.fArray[bin] = 0;
2856 }
2857 }
2858 }
2859 }
2860 ResetStats();
2861 return kTRUE;
2862}
2863
2864////////////////////////////////////////////////////////////////////////////////
2865/// Divide this histogram by h1.
2866///
2867/// `this = this/h1`
2868/// if errors are defined (see TH1::Sumw2), errors are also recalculated.
2869/// Note that if h1 has Sumw2 set, Sumw2 is automatically called for this
2870/// if not already set.
2871/// The resulting errors are calculated assuming uncorrelated histograms.
2872/// See the other TH1::Divide that gives the possibility to optionally
2873/// compute binomial errors.
2874///
2875/// IMPORTANT NOTE: If you intend to use the errors of this histogram later
2876/// you should call Sumw2 before making this operation.
2877/// This is particularly important if you fit the histogram after TH1::Scale
2878///
2879/// The function return kFALSE if the divide operation failed
2880
2881Bool_t TH1::Divide(const TH1 *h1)
2882{
2883 if (!h1) {
2884 Error("Divide", "Input histogram passed does not exist (NULL).");
2885 return kFALSE;
2886 }
2887
2888 // delete buffer if it is there since it will become invalid
2889 if (fBuffer) BufferEmpty(1);
2890
2891 if (LoggedInconsistency("Divide", this, h1) >= kDifferentNumberOfBins) {
2892 return false;
2893 }
2894
2895 // Create Sumw2 if h1 has Sumw2 set
2896 if (fSumw2.fN == 0 && h1->GetSumw2N() != 0) Sumw2();
2897
2898 // - Loop on bins (including underflows/overflows)
2899 for (Int_t i = 0; i < fNcells; ++i) {
2902 if (c1) UpdateBinContent(i, c0 / c1);
2903 else UpdateBinContent(i, 0);
2904
2905 if(fSumw2.fN) {
2906 if (c1 == 0) { fSumw2.fArray[i] = 0; continue; }
2907 Double_t c1sq = c1 * c1;
2908 fSumw2.fArray[i] = (GetBinErrorSqUnchecked(i) * c1sq + h1->GetBinErrorSqUnchecked(i) * c0 * c0) / (c1sq * c1sq);
2909 }
2910 }
2911 ResetStats();
2912 return kTRUE;
2913}
2914
2915////////////////////////////////////////////////////////////////////////////////
2916/// Replace contents of this histogram by the division of h1 by h2.
2917///
2918/// `this = c1*h1/(c2*h2)`
2919///
2920/// If errors are defined (see TH1::Sumw2), errors are also recalculated
2921/// Note that if h1 or h2 have Sumw2 set, Sumw2 is automatically called for this
2922/// if not already set.
2923/// The resulting errors are calculated assuming uncorrelated histograms.
2924/// However, if option ="B" is specified, Binomial errors are computed.
2925/// In this case c1 and c2 do not make real sense and they are ignored.
2926///
2927/// IMPORTANT NOTE: If you intend to use the errors of this histogram later
2928/// you should call Sumw2 before making this operation.
2929/// This is particularly important if you fit the histogram after TH1::Divide
2930///
2931/// Please note also that in the binomial case errors are calculated using standard
2932/// binomial statistics, which means when b1 = b2, the error is zero.
2933/// If you prefer to have efficiency errors not going to zero when the efficiency is 1, you must
2934/// use the function TGraphAsymmErrors::BayesDivide, which will return an asymmetric and non-zero lower
2935/// error for the case b1=b2.
2936///
2937/// The function return kFALSE if the divide operation failed
2938
2940{
2941
2942 TString opt = option;
2943 opt.ToLower();
2944 Bool_t binomial = kFALSE;
2945 if (opt.Contains("b")) binomial = kTRUE;
2946 if (!h1 || !h2) {
2947 Error("Divide", "At least one of the input histograms passed does not exist (NULL).");
2948 return kFALSE;
2949 }
2950
2951 // delete buffer if it is there since it will become invalid
2952 if (fBuffer) BufferEmpty(1);
2953
2954 if (LoggedInconsistency("Divide", this, h1) >= kDifferentNumberOfBins ||
2955 LoggedInconsistency("Divide", h1, h2) >= kDifferentNumberOfBins) {
2956 return false;
2957 }
2958
2959 if (!c2) {
2960 Error("Divide","Coefficient of dividing histogram cannot be zero");
2961 return kFALSE;
2962 }
2963
2964 // Create Sumw2 if h1 or h2 have Sumw2 set, or if binomial errors are explicitly requested
2965 if (fSumw2.fN == 0 && (h1->GetSumw2N() != 0 || h2->GetSumw2N() != 0 || binomial)) Sumw2();
2966
2967 SetMinimum();
2968 SetMaximum();
2969
2970 // - Loop on bins (including underflows/overflows)
2971 for (Int_t i = 0; i < fNcells; ++i) {
2973 Double_t b2 = h2->RetrieveBinContent(i);
2974 if (b2) UpdateBinContent(i, c1 * b1 / (c2 * b2));
2975 else UpdateBinContent(i, 0);
2976
2977 if (fSumw2.fN) {
2978 if (b2 == 0) { fSumw2.fArray[i] = 0; continue; }
2979 Double_t b1sq = b1 * b1; Double_t b2sq = b2 * b2;
2980 Double_t c1sq = c1 * c1; Double_t c2sq = c2 * c2;
2982 Double_t e2sq = h2->GetBinErrorSqUnchecked(i);
2983 if (binomial) {
2984 if (b1 != b2) {
2985 // in the case of binomial statistics c1 and c2 must be 1 otherwise it does not make sense
2986 // c1 and c2 are ignored
2987 //fSumw2.fArray[bin] = TMath::Abs(w*(1-w)/(c2*b2));//this is the formula in Hbook/Hoper1
2988 //fSumw2.fArray[bin] = TMath::Abs(w*(1-w)/b2); // old formula from G. Flucke
2989 // formula which works also for weighted histogram (see http://root-forum.cern.ch/viewtopic.php?t=3753 )
2990 fSumw2.fArray[i] = TMath::Abs( ( (1. - 2.* b1 / b2) * e1sq + b1sq * e2sq / b2sq ) / b2sq );
2991 } else {
2992 //in case b1=b2 error is zero
2993 //use TGraphAsymmErrors::BayesDivide for getting the asymmetric error not equal to zero
2994 fSumw2.fArray[i] = 0;
2995 }
2996 } else {
2997 fSumw2.fArray[i] = c1sq * c2sq * (e1sq * b2sq + e2sq * b1sq) / (c2sq * c2sq * b2sq * b2sq);
2998 }
2999 }
3000 }
3001 ResetStats();
3002 if (binomial)
3003 // in case of binomial division use denominator for number of entries
3004 SetEntries ( h2->GetEntries() );
3005
3006 return kTRUE;
3007}
3008
3009////////////////////////////////////////////////////////////////////////////////
3010/// Draw this histogram with options.
3011///
3012/// Histograms are drawn via the THistPainter class. Each histogram has
3013/// a pointer to its own painter (to be usable in a multithreaded program).
3014/// The same histogram can be drawn with different options in different pads.
3015/// When a histogram drawn in a pad is deleted, the histogram is
3016/// automatically removed from the pad or pads where it was drawn.
3017/// If a histogram is drawn in a pad, then filled again, the new status
3018/// of the histogram will be automatically shown in the pad next time
3019/// the pad is updated. One does not need to redraw the histogram.
3020/// To draw the current version of a histogram in a pad, one can use
3021/// `h->DrawCopy();`
3022/// This makes a clone of the histogram. Once the clone is drawn, the original
3023/// histogram may be modified or deleted without affecting the aspect of the
3024/// clone.
3025/// By default, TH1::Draw clears the current pad.
3026///
3027/// One can use TH1::SetMaximum and TH1::SetMinimum to force a particular
3028/// value for the maximum or the minimum scale on the plot.
3029///
3030/// TH1::UseCurrentStyle can be used to change all histogram graphics
3031/// attributes to correspond to the current selected style.
3032/// This function must be called for each histogram.
3033/// In case one reads and draws many histograms from a file, one can force
3034/// the histograms to inherit automatically the current graphics style
3035/// by calling before gROOT->ForceStyle();
3036///
3037/// See the THistPainter class for a description of all the drawing options.
3038
3040{
3041 TString opt1 = option; opt1.ToLower();
3043 Int_t index = opt1.Index("same");
3044
3045 // Check if the string "same" is part of a TCutg name.
3046 if (index>=0) {
3047 Int_t indb = opt1.Index("[");
3048 if (indb>=0) {
3049 Int_t indk = opt1.Index("]");
3050 if (index>indb && index<indk) index = -1;
3051 }
3052 }
3053
3054 // If there is no pad or an empty pad the "same" option is ignored.
3055 if (gPad) {
3056 if (!gPad->IsEditable()) gROOT->MakeDefCanvas();
3057 if (index>=0) {
3058 if (gPad->GetX1() == 0 && gPad->GetX2() == 1 &&
3059 gPad->GetY1() == 0 && gPad->GetY2() == 1 &&
3060 gPad->GetListOfPrimitives()->GetSize()==0) opt2.Remove(index,4);
3061 } else {
3062 //the following statement is necessary in case one attempts to draw
3063 //a temporary histogram already in the current pad
3064 if (TestBit(kCanDelete)) gPad->Remove(this);
3065 gPad->Clear();
3066 }
3067 gPad->IncrementPaletteColor(1, opt1);
3068 } else {
3069 if (index>=0) opt2.Remove(index,4);
3070 }
3071
3072 AppendPad(opt2.Data());
3073}
3074
3075////////////////////////////////////////////////////////////////////////////////
3076/// Copy this histogram and Draw in the current pad.
3077///
3078/// Once the histogram is drawn into the pad, any further modification
3079/// using graphics input will be made on the copy of the histogram,
3080/// and not to the original object.
3081/// By default a postfix "_copy" is added to the histogram name. Pass an empty postfix in case
3082/// you want to draw a histogram with the same name
3083///
3084/// See Draw for the list of options
3085
3086TH1 *TH1::DrawCopy(Option_t *option, const char * name_postfix) const
3087{
3088 TString opt = option;
3089 opt.ToLower();
3090 if (gPad && !opt.Contains("same")) gPad->Clear();
3092 if (name_postfix) newName.Form("%s%s", GetName(), name_postfix);
3093 TH1 *newth1 = (TH1 *)Clone(newName.Data());
3094 newth1->SetDirectory(nullptr);
3095 newth1->SetBit(kCanDelete);
3096 if (gPad) gPad->IncrementPaletteColor(1, opt);
3097
3098 newth1->AppendPad(option);
3099 return newth1;
3100}
3101
3102////////////////////////////////////////////////////////////////////////////////
3103/// Draw a normalized copy of this histogram.
3104///
3105/// A clone of this histogram is normalized to norm and drawn with option.
3106/// A pointer to the normalized histogram is returned.
3107/// The contents of the histogram copy are scaled such that the new
3108/// sum of weights (excluding under and overflow) is equal to norm.
3109/// Note that the returned normalized histogram is not added to the list
3110/// of histograms in the current directory in memory.
3111/// It is the user's responsibility to delete this histogram.
3112/// The kCanDelete bit is set for the returned object. If a pad containing
3113/// this copy is cleared, the histogram will be automatically deleted.
3114///
3115/// See Draw for the list of options
3116
3118{
3120 if (sum == 0) {
3121 Error("DrawNormalized","Sum of weights is null. Cannot normalize histogram: %s",GetName());
3122 return nullptr;
3123 }
3126 TH1 *h = (TH1*)Clone();
3128 // in case of drawing with error options - scale correctly the error
3129 TString opt(option); opt.ToUpper();
3130 if (fSumw2.fN == 0) {
3131 h->Sumw2();
3132 // do not use in this case the "Error option " for drawing which is enabled by default since the normalized histogram has now errors
3133 if (opt.IsNull() || opt == "SAME") opt += "HIST";
3134 }
3135 h->Scale(norm/sum);
3136 if (TMath::Abs(fMaximum+1111) > 1e-3) h->SetMaximum(fMaximum*norm/sum);
3137 if (TMath::Abs(fMinimum+1111) > 1e-3) h->SetMinimum(fMinimum*norm/sum);
3138 h->Draw(opt);
3140 return h;
3141}
3142
3143////////////////////////////////////////////////////////////////////////////////
3144/// Display a panel with all histogram drawing options.
3145///
3146/// See class TDrawPanelHist for example
3147
3148void TH1::DrawPanel()
3149{
3150 if (!fPainter) {Draw(); if (gPad) gPad->Update();}
3151 if (fPainter) fPainter->DrawPanel();
3152}
3153
3154////////////////////////////////////////////////////////////////////////////////
3155/// Evaluate function f1 at the center of bins of this histogram.
3156///
3157/// - If option "R" is specified, the function is evaluated only
3158/// for the bins included in the function range.
3159/// - If option "A" is specified, the value of the function is added to the
3160/// existing bin contents
3161/// - If option "S" is specified, the value of the function is used to
3162/// generate a value, distributed according to the Poisson
3163/// distribution, with f1 as the mean.
3164
3166{
3167 Double_t x[3];
3168 Int_t range, stat, add;
3169 if (!f1) return;
3170
3171 TString opt = option;
3172 opt.ToLower();
3173 if (opt.Contains("a")) add = 1;
3174 else add = 0;
3175 if (opt.Contains("s")) stat = 1;
3176 else stat = 0;
3177 if (opt.Contains("r")) range = 1;
3178 else range = 0;
3179
3180 // delete buffer if it is there since it will become invalid
3181 if (fBuffer) BufferEmpty(1);
3182
3186 if (!add) Reset();
3187
3188 for (Int_t binz = 1; binz <= nbinsz; ++binz) {
3189 x[2] = fZaxis.GetBinCenter(binz);
3190 for (Int_t biny = 1; biny <= nbinsy; ++biny) {
3191 x[1] = fYaxis.GetBinCenter(biny);
3192 for (Int_t binx = 1; binx <= nbinsx; ++binx) {
3193 Int_t bin = GetBin(binx,biny,binz);
3194 x[0] = fXaxis.GetBinCenter(binx);
3195 if (range && !f1->IsInside(x)) continue;
3196 Double_t fu = f1->Eval(x[0], x[1], x[2]);
3197 if (stat) fu = gRandom->PoissonD(fu);
3198 AddBinContent(bin, fu);
3199 if (fSumw2.fN) fSumw2.fArray[bin] += TMath::Abs(fu);
3200 }
3201 }
3202 }
3203}
3204
3205////////////////////////////////////////////////////////////////////////////////
3206/// Execute action corresponding to one event.
3207///
3208/// This member function is called when a histogram is clicked with the locator
3209///
3210/// If Left button clicked on the bin top value, then the content of this bin
3211/// is modified according to the new position of the mouse when it is released.
3212
3213void TH1::ExecuteEvent(Int_t event, Int_t px, Int_t py)
3214{
3215 if (fPainter) fPainter->ExecuteEvent(event, px, py);
3216}
3217
3218////////////////////////////////////////////////////////////////////////////////
3219/// This function allows to do discrete Fourier transforms of TH1 and TH2.
3220/// Available transform types and flags are described below.
3221///
3222/// To extract more information about the transform, use the function
3223/// TVirtualFFT::GetCurrentTransform() to get a pointer to the current
3224/// transform object.
3225///
3226/// \param[out] h_output histogram for the output. If a null pointer is passed, a new histogram is created
3227/// and returned, otherwise, the provided histogram is used and should be big enough
3228/// \param[in] option option parameters consists of 3 parts:
3229/// - option on what to return
3230/// - "RE" - returns a histogram of the real part of the output
3231/// - "IM" - returns a histogram of the imaginary part of the output
3232/// - "MAG"- returns a histogram of the magnitude of the output
3233/// - "PH" - returns a histogram of the phase of the output
3234/// - option of transform type
3235/// - "R2C" - real to complex transforms - default
3236/// - "R2HC" - real to halfcomplex (special format of storing output data,
3237/// results the same as for R2C)
3238/// - "DHT" - discrete Hartley transform
3239/// real to real transforms (sine and cosine):
3240/// - "R2R_0", "R2R_1", "R2R_2", "R2R_3" - discrete cosine transforms of types I-IV
3241/// - "R2R_4", "R2R_5", "R2R_6", "R2R_7" - discrete sine transforms of types I-IV
3242/// To specify the type of each dimension of a 2-dimensional real to real
3243/// transform, use options of form "R2R_XX", for example, "R2R_02" for a transform,
3244/// which is of type "R2R_0" in 1st dimension and "R2R_2" in the 2nd.
3245/// - option of transform flag
3246/// - "ES" (from "estimate") - no time in preparing the transform, but probably sub-optimal
3247/// performance
3248/// - "M" (from "measure") - some time spend in finding the optimal way to do the transform
3249/// - "P" (from "patient") - more time spend in finding the optimal way to do the transform
3250/// - "EX" (from "exhaustive") - the most optimal way is found
3251/// This option should be chosen depending on how many transforms of the same size and
3252/// type are going to be done. Planning is only done once, for the first transform of this
3253/// size and type. Default is "ES".
3254///
3255/// Examples of valid options: "Mag R2C M" "Re R2R_11" "Im R2C ES" "PH R2HC EX"
3256
3258{
3259
3260 Int_t ndim[3];
3261 ndim[0] = this->GetNbinsX();
3262 ndim[1] = this->GetNbinsY();
3263 ndim[2] = this->GetNbinsZ();
3264
3266 TString opt = option;
3267 opt.ToUpper();
3268 if (!opt.Contains("2R")){
3269 if (!opt.Contains("2C") && !opt.Contains("2HC") && !opt.Contains("DHT")) {
3270 //no type specified, "R2C" by default
3271 opt.Append("R2C");
3272 }
3273 fft = TVirtualFFT::FFT(this->GetDimension(), ndim, opt.Data());
3274 }
3275 else {
3276 //find the kind of transform
3277 Int_t ind = opt.Index("R2R", 3);
3278 Int_t *kind = new Int_t[2];
3279 char t;
3280 t = opt[ind+4];
3281 kind[0] = atoi(&t);
3282 if (h_output->GetDimension()>1) {
3283 t = opt[ind+5];
3284 kind[1] = atoi(&t);
3285 }
3286 fft = TVirtualFFT::SineCosine(this->GetDimension(), ndim, kind, option);
3287 delete [] kind;
3288 }
3289
3290 if (!fft) return nullptr;
3291 Int_t in=0;
3292 for (Int_t binx = 1; binx<=ndim[0]; binx++) {
3293 for (Int_t biny=1; biny<=ndim[1]; biny++) {
3294 for (Int_t binz=1; binz<=ndim[2]; binz++) {
3295 fft->SetPoint(in, this->GetBinContent(binx, biny, binz));
3296 in++;
3297 }
3298 }
3299 }
3300 fft->Transform();
3302 return h_output;
3303}
3304
3305////////////////////////////////////////////////////////////////////////////////
3306/// Increment bin with abscissa X by 1.
3307///
3308/// if x is less than the low-edge of the first bin, the Underflow bin is incremented
3309/// if x is equal to or greater than the upper edge of last bin, the Overflow bin is incremented
3310///
3311/// If the storage of the sum of squares of weights has been triggered,
3312/// via the function Sumw2, then the sum of the squares of weights is incremented
3313/// by 1 in the bin corresponding to x.
3314///
3315/// The function returns the corresponding bin number which has its content incremented by 1
3316
3318{
3319 if (fBuffer) return BufferFill(x,1);
3320
3321 Int_t bin;
3322 fEntries++;
3323 bin =fXaxis.FindBin(x);
3324 if (bin <0) return -1;
3325 AddBinContent(bin);
3326 if (fSumw2.fN) ++fSumw2.fArray[bin];
3327 if (bin == 0 || bin > fXaxis.GetNbins()) {
3328 if (!GetStatOverflowsBehaviour()) return -1;
3329 }
3330 ++fTsumw;
3331 ++fTsumw2;
3332 fTsumwx += x;
3333 fTsumwx2 += x*x;
3334 return bin;
3335}
3336
3337////////////////////////////////////////////////////////////////////////////////
3338/// Increment bin with abscissa X with a weight w.
3339///
3340/// if x is less than the low-edge of the first bin, the Underflow bin is incremented
3341/// if x is equal to or greater than the upper edge of last bin, the Overflow bin is incremented
3342///
3343/// If the weight is not equal to 1, the storage of the sum of squares of
3344/// weights is automatically triggered and the sum of the squares of weights is incremented
3345/// by \f$ w^2 \f$ in the bin corresponding to x.
3346///
3347/// The function returns the corresponding bin number which has its content incremented by w
3348
3350{
3351
3352 if (fBuffer) return BufferFill(x,w);
3353
3354 Int_t bin;
3355 fEntries++;
3356 bin =fXaxis.FindBin(x);
3357 if (bin <0) return -1;
3358 if (!fSumw2.fN && w != 1.0 && !TestBit(TH1::kIsNotW) ) Sumw2(); // must be called before AddBinContent
3359 if (fSumw2.fN) fSumw2.fArray[bin] += w*w;
3360 AddBinContent(bin, w);
3361 if (bin == 0 || bin > fXaxis.GetNbins()) {
3362 if (!GetStatOverflowsBehaviour()) return -1;
3363 }
3364 Double_t z= w;
3365 fTsumw += z;
3366 fTsumw2 += z*z;
3367 fTsumwx += z*x;
3368 fTsumwx2 += z*x*x;
3369 return bin;
3370}
3371
3372////////////////////////////////////////////////////////////////////////////////
3373/// Increment bin with namex with a weight w
3374///
3375/// if x is less than the low-edge of the first bin, the Underflow bin is incremented
3376/// if x is equal to or greater than the upper edge of last bin, the Overflow bin is incremented
3377///
3378/// If the weight is not equal to 1, the storage of the sum of squares of
3379/// weights is automatically triggered and the sum of the squares of weights is incremented
3380/// by \f$ w^2 \f$ in the bin corresponding to x.
3381///
3382/// The function returns the corresponding bin number which has its content
3383/// incremented by w.
3384
3385Int_t TH1::Fill(const char *namex, Double_t w)
3386{
3387 Int_t bin;
3388 fEntries++;
3389 bin =fXaxis.FindBin(namex);
3390 if (bin <0) return -1;
3391 if (!fSumw2.fN && w != 1.0 && !TestBit(TH1::kIsNotW)) Sumw2();
3392 if (fSumw2.fN) fSumw2.fArray[bin] += w*w;
3393 AddBinContent(bin, w);
3394 if (bin == 0 || bin > fXaxis.GetNbins()) return -1;
3395 Double_t z= w;
3396 fTsumw += z;
3397 fTsumw2 += z*z;
3398 // this make sense if the histogram is not expanding (the x axis cannot be extended)
3399 if (!fXaxis.CanExtend() || !fXaxis.IsAlphanumeric()) {
3401 fTsumwx += z*x;
3402 fTsumwx2 += z*x*x;
3403 }
3404 return bin;
3405}
3406
3407////////////////////////////////////////////////////////////////////////////////
3408/// Fill this histogram with an array x and weights w.
3409///
3410/// \param[in] ntimes number of entries in arrays x and w (array size must be ntimes*stride)
3411/// \param[in] x array of values to be histogrammed
3412/// \param[in] w array of weighs
3413/// \param[in] stride step size through arrays x and w
3414///
3415/// If the weight is not equal to 1, the storage of the sum of squares of
3416/// weights is automatically triggered and the sum of the squares of weights is incremented
3417/// by \f$ w^2 \f$ in the bin corresponding to x.
3418/// if w is NULL each entry is assumed a weight=1
3419
3420void TH1::FillN(Int_t ntimes, const Double_t *x, const Double_t *w, Int_t stride)
3421{
3422 //If a buffer is activated, fill buffer
3423 if (fBuffer) {
3424 ntimes *= stride;
3425 Int_t i = 0;
3426 for (i=0;i<ntimes;i+=stride) {
3427 if (!fBuffer) break; // buffer can be deleted in BufferFill when is empty
3428 if (w) BufferFill(x[i],w[i]);
3429 else BufferFill(x[i], 1.);
3430 }
3431 // fill the remaining entries if the buffer has been deleted
3432 if (i < ntimes && !fBuffer) {
3433 auto weights = w ? &w[i] : nullptr;
3434 DoFillN((ntimes-i)/stride,&x[i],weights,stride);
3435 }
3436 return;
3437 }
3438 // call internal method
3439 DoFillN(ntimes, x, w, stride);
3440}
3441
3442////////////////////////////////////////////////////////////////////////////////
3443/// Internal method to fill histogram content from a vector
3444/// called directly by TH1::BufferEmpty
3445
3446void TH1::DoFillN(Int_t ntimes, const Double_t *x, const Double_t *w, Int_t stride)
3447{
3448 Int_t bin,i;
3449
3450 fEntries += ntimes;
3451 Double_t ww = 1;
3452 Int_t nbins = fXaxis.GetNbins();
3453 ntimes *= stride;
3454 for (i=0;i<ntimes;i+=stride) {
3455 bin =fXaxis.FindBin(x[i]);
3456 if (bin <0) continue;
3457 if (w) ww = w[i];
3458 if (!fSumw2.fN && ww != 1.0 && !TestBit(TH1::kIsNotW)) Sumw2();
3459 if (fSumw2.fN) fSumw2.fArray[bin] += ww*ww;
3460 AddBinContent(bin, ww);
3461 if (bin == 0 || bin > nbins) {
3462 if (!GetStatOverflowsBehaviour()) continue;
3463 }
3464 Double_t z= ww;
3465 fTsumw += z;
3466 fTsumw2 += z*z;
3467 fTsumwx += z*x[i];
3468 fTsumwx2 += z*x[i]*x[i];
3469 }
3470}
3471
3472////////////////////////////////////////////////////////////////////////////////
3473/// Fill histogram following distribution in function fname.
3474///
3475/// @param fname : Function name used for filling the histogram
3476/// @param ntimes : number of times the histogram is filled
3477/// @param rng : (optional) Random number generator used to sample
3478///
3479///
3480/// The distribution contained in the function fname (TF1) is integrated
3481/// over the channel contents for the bin range of this histogram.
3482/// It is normalized to 1.
3483///
3484/// Getting one random number implies:
3485/// - Generating a random number between 0 and 1 (say r1)
3486/// - Look in which bin in the normalized integral r1 corresponds to
3487/// - Fill histogram channel
3488/// ntimes random numbers are generated
3489///
3490/// One can also call TF1::GetRandom to get a random variate from a function.
3491
3492void TH1::FillRandom(const char *fname, Int_t ntimes, TRandom * rng)
3493{
3494 // - Search for fname in the list of ROOT defined functions
3495 TF1 *f1 = (TF1*)gROOT->GetFunction(fname);
3496 if (!f1) { Error("FillRandom", "Unknown function: %s",fname); return; }
3497
3500
3502{
3503 Int_t bin, binx, ibin, loop;
3504 Double_t r1, x;
3505
3506 // - Allocate temporary space to store the integral and compute integral
3507
3508 TAxis * xAxis = &fXaxis;
3509
3510 // in case axis of histogram is not defined use the function axis
3511 if (fXaxis.GetXmax() <= fXaxis.GetXmin()) {
3513 f1->GetRange(xmin,xmax);
3514 Info("FillRandom","Using function axis and range [%g,%g]",xmin, xmax);
3515 xAxis = f1->GetHistogram()->GetXaxis();
3516 }
3517
3518 Int_t first = xAxis->GetFirst();
3519 Int_t last = xAxis->GetLast();
3520 Int_t nbinsx = last-first+1;
3521
3522 Double_t *integral = new Double_t[nbinsx+1];
3523 integral[0] = 0;
3524 for (binx=1;binx<=nbinsx;binx++) {
3525 Double_t fint = f1->Integral(xAxis->GetBinLowEdge(binx+first-1),xAxis->GetBinUpEdge(binx+first-1), 0.);
3526 integral[binx] = integral[binx-1] + fint;
3527 }
3528
3529 // - Normalize integral to 1
3530 if (integral[nbinsx] == 0 ) {
3531 delete [] integral;
3532 Error("FillRandom", "Integral = zero"); return;
3533 }
3534 for (bin=1;bin<=nbinsx;bin++) integral[bin] /= integral[nbinsx];
3535
3536 // --------------Start main loop ntimes
3537 for (loop=0;loop<ntimes;loop++) {
3538 r1 = (rng) ? rng->Rndm() : gRandom->Rndm();
3539 ibin = TMath::BinarySearch(nbinsx,&integral[0],r1);
3540 //binx = 1 + ibin;
3541 //x = xAxis->GetBinCenter(binx); //this is not OK when SetBuffer is used
3542 x = xAxis->GetBinLowEdge(ibin+first)
3543 +xAxis->GetBinWidth(ibin+first)*(r1-integral[ibin])/(integral[ibin+1] - integral[ibin]);
3544 Fill(x);
3545 }
3546 delete [] integral;
3547}
3548
3549////////////////////////////////////////////////////////////////////////////////
3550/// Fill histogram following distribution in histogram h.
3551///
3552/// @param h : Histogram pointer used for sampling random number
3553/// @param ntimes : number of times the histogram is filled
3554/// @param rng : (optional) Random number generator used for sampling
3555///
3556/// The distribution contained in the histogram h (TH1) is integrated
3557/// over the channel contents for the bin range of this histogram.
3558/// It is normalized to 1.
3559///
3560/// Getting one random number implies:
3561/// - Generating a random number between 0 and 1 (say r1)
3562/// - Look in which bin in the normalized integral r1 corresponds to
3563/// - Fill histogram channel ntimes random numbers are generated
3564///
3565/// SPECIAL CASE when the target histogram has the same binning as the source.
3566/// in this case we simply use a poisson distribution where
3567/// the mean value per bin = bincontent/integral.
3568
3570{
3571 if (!h) { Error("FillRandom", "Null histogram"); return; }
3572 if (fDimension != h->GetDimension()) {
3573 Error("FillRandom", "Histograms with different dimensions"); return;
3574 }
3575 if (std::isnan(h->ComputeIntegral(true))) {
3576 Error("FillRandom", "Histograms contains negative bins, does not represent probabilities");
3577 return;
3578 }
3579
3580 //in case the target histogram has the same binning and ntimes much greater
3581 //than the number of bins we can use a fast method
3582 Int_t first = fXaxis.GetFirst();
3583 Int_t last = fXaxis.GetLast();
3584 Int_t nbins = last-first+1;
3585 if (ntimes > 10*nbins) {
3586 auto inconsistency = CheckConsistency(this,h);
3587 if (inconsistency != kFullyConsistent) return; // do nothing
3588 Double_t sumw = h->Integral(first,last);
3589 if (sumw == 0) return;
3590 Double_t sumgen = 0;
3591 for (Int_t bin=first;bin<=last;bin++) {
3592 Double_t mean = h->RetrieveBinContent(bin)*ntimes/sumw;
3593 Double_t cont = (rng) ? rng->Poisson(mean) : gRandom->Poisson(mean);
3594 sumgen += cont;
3595 AddBinContent(bin,cont);
3596 if (fSumw2.fN) fSumw2.fArray[bin] += cont;
3597 }
3598
3599 // fix for the fluctuations in the total number n
3600 // since we use Poisson instead of multinomial
3601 // add a correction to have ntimes as generated entries
3602 Int_t i;
3603 if (sumgen < ntimes) {
3604 // add missing entries
3605 for (i = Int_t(sumgen+0.5); i < ntimes; ++i)
3606 {
3607 Double_t x = h->GetRandom();
3608 Fill(x);
3609 }
3610 }
3611 else if (sumgen > ntimes) {
3612 // remove extra entries
3613 i = Int_t(sumgen+0.5);
3614 while( i > ntimes) {
3615 Double_t x = h->GetRandom(rng);
3618 // skip in case bin is empty
3619 if (y > 0) {
3620 SetBinContent(ibin, y-1.);
3621 i--;
3622 }
3623 }
3624 }
3625
3626 ResetStats();
3627 return;
3628 }
3629 // case of different axis and not too large ntimes
3630
3631 if (h->ComputeIntegral() ==0) return;
3632 Int_t loop;
3633 Double_t x;
3634 for (loop=0;loop<ntimes;loop++) {
3635 x = h->GetRandom();
3636 Fill(x);
3637 }
3638}
3639
3640////////////////////////////////////////////////////////////////////////////////
3641/// Return Global bin number corresponding to x,y,z
3642///
3643/// 2-D and 3-D histograms are represented with a one dimensional
3644/// structure. This has the advantage that all existing functions, such as
3645/// GetBinContent, GetBinError, GetBinFunction work for all dimensions.
3646/// This function tries to extend the axis if the given point belongs to an
3647/// under-/overflow bin AND if CanExtendAllAxes() is true.
3648///
3649/// See also TH1::GetBin, TAxis::FindBin and TAxis::FindFixBin
3650
3652{
3653 if (GetDimension() < 2) {
3654 return fXaxis.FindBin(x);
3655 }
3656 if (GetDimension() < 3) {
3657 Int_t nx = fXaxis.GetNbins()+2;
3660 return binx + nx*biny;
3661 }
3662 if (GetDimension() < 4) {
3663 Int_t nx = fXaxis.GetNbins()+2;
3664 Int_t ny = fYaxis.GetNbins()+2;
3667 Int_t binz = fZaxis.FindBin(z);
3668 return binx + nx*(biny +ny*binz);
3669 }
3670 return -1;
3671}
3672
3673////////////////////////////////////////////////////////////////////////////////
3674/// Return Global bin number corresponding to x,y,z.
3675///
3676/// 2-D and 3-D histograms are represented with a one dimensional
3677/// structure. This has the advantage that all existing functions, such as
3678/// GetBinContent, GetBinError, GetBinFunction work for all dimensions.
3679/// This function DOES NOT try to extend the axis if the given point belongs
3680/// to an under-/overflow bin.
3681///
3682/// See also TH1::GetBin, TAxis::FindBin and TAxis::FindFixBin
3683
3685{
3686 if (GetDimension() < 2) {
3687 return fXaxis.FindFixBin(x);
3688 }
3689 if (GetDimension() < 3) {
3690 Int_t nx = fXaxis.GetNbins()+2;
3693 return binx + nx*biny;
3694 }
3695 if (GetDimension() < 4) {
3696 Int_t nx = fXaxis.GetNbins()+2;
3697 Int_t ny = fYaxis.GetNbins()+2;
3701 return binx + nx*(biny +ny*binz);
3702 }
3703 return -1;
3704}
3705
3706////////////////////////////////////////////////////////////////////////////////
3707/// Find first bin with content > threshold for axis (1=x, 2=y, 3=z)
3708/// if no bins with content > threshold is found the function returns -1.
3709/// The search will occur between the specified first and last bin. Specifying
3710/// the value of the last bin to search to less than zero will search until the
3711/// last defined bin.
3712
3714{
3715 if (fBuffer) ((TH1*)this)->BufferEmpty();
3716
3717 if (axis < 1 || (axis > 1 && GetDimension() == 1 ) ||
3718 ( axis > 2 && GetDimension() == 2 ) || ( axis > 3 && GetDimension() > 3 ) ) {
3719 Warning("FindFirstBinAbove","Invalid axis number : %d, axis x assumed\n",axis);
3720 axis = 1;
3721 }
3722 if (firstBin < 1) {
3723 firstBin = 1;
3724 }
3726 Int_t nbinsy = (GetDimension() > 1 ) ? fYaxis.GetNbins() : 1;
3727 Int_t nbinsz = (GetDimension() > 2 ) ? fZaxis.GetNbins() : 1;
3728
3729 if (axis == 1) {
3732 }
3733 for (Int_t binx = firstBin; binx <= lastBin; binx++) {
3734 for (Int_t biny = 1; biny <= nbinsy; biny++) {
3735 for (Int_t binz = 1; binz <= nbinsz; binz++) {
3737 }
3738 }
3739 }
3740 }
3741 else if (axis == 2) {
3744 }
3745 for (Int_t biny = firstBin; biny <= lastBin; biny++) {
3746 for (Int_t binx = 1; binx <= nbinsx; binx++) {
3747 for (Int_t binz = 1; binz <= nbinsz; binz++) {
3749 }
3750 }
3751 }
3752 }
3753 else if (axis == 3) {
3756 }
3757 for (Int_t binz = firstBin; binz <= lastBin; binz++) {
3758 for (Int_t binx = 1; binx <= nbinsx; binx++) {
3759 for (Int_t biny = 1; biny <= nbinsy; biny++) {
3761 }
3762 }
3763 }
3764 }
3765
3766 return -1;
3767}
3768
3769////////////////////////////////////////////////////////////////////////////////
3770/// Find last bin with content > threshold for axis (1=x, 2=y, 3=z)
3771/// if no bins with content > threshold is found the function returns -1.
3772/// The search will occur between the specified first and last bin. Specifying
3773/// the value of the last bin to search to less than zero will search until the
3774/// last defined bin.
3775
3777{
3778 if (fBuffer) ((TH1*)this)->BufferEmpty();
3779
3780
3781 if (axis < 1 || ( axis > 1 && GetDimension() == 1 ) ||
3782 ( axis > 2 && GetDimension() == 2 ) || ( axis > 3 && GetDimension() > 3) ) {
3783 Warning("FindFirstBinAbove","Invalid axis number : %d, axis x assumed\n",axis);
3784 axis = 1;
3785 }
3786 if (firstBin < 1) {
3787 firstBin = 1;
3788 }
3790 Int_t nbinsy = (GetDimension() > 1 ) ? fYaxis.GetNbins() : 1;
3791 Int_t nbinsz = (GetDimension() > 2 ) ? fZaxis.GetNbins() : 1;
3792
3793 if (axis == 1) {
3796 }
3797 for (Int_t binx = lastBin; binx >= firstBin; binx--) {
3798 for (Int_t biny = 1; biny <= nbinsy; biny++) {
3799 for (Int_t binz = 1; binz <= nbinsz; binz++) {
3801 }
3802 }
3803 }
3804 }
3805 else if (axis == 2) {
3808 }
3809 for (Int_t biny = lastBin; biny >= firstBin; biny--) {
3810 for (Int_t binx = 1; binx <= nbinsx; binx++) {
3811 for (Int_t binz = 1; binz <= nbinsz; binz++) {
3813 }
3814 }
3815 }
3816 }
3817 else if (axis == 3) {
3820 }
3821 for (Int_t binz = lastBin; binz >= firstBin; binz--) {
3822 for (Int_t binx = 1; binx <= nbinsx; binx++) {
3823 for (Int_t biny = 1; biny <= nbinsy; biny++) {
3825 }
3826 }
3827 }
3828 }
3829
3830 return -1;
3831}
3832
3833////////////////////////////////////////////////////////////////////////////////
3834/// Search object named name in the list of functions.
3835
3836TObject *TH1::FindObject(const char *name) const
3837{
3838 if (fFunctions) return fFunctions->FindObject(name);
3839 return nullptr;
3840}
3841
3842////////////////////////////////////////////////////////////////////////////////
3843/// Search object obj in the list of functions.
3844
3845TObject *TH1::FindObject(const TObject *obj) const
3846{
3847 if (fFunctions) return fFunctions->FindObject(obj);
3848 return nullptr;
3849}
3850
3851////////////////////////////////////////////////////////////////////////////////
3852/// Fit histogram with function fname.
3853///
3854///
3855/// fname is the name of a function available in the global ROOT list of functions
3856/// `gROOT->GetListOfFunctions`
3857/// The list include any TF1 object created by the user plus some pre-defined functions
3858/// which are automatically created by ROOT the first time a pre-defined function is requested from `gROOT`
3859/// (i.e. when calling `gROOT->GetFunction(const char *name)`).
3860/// These pre-defined functions are:
3861/// - `gaus, gausn` where gausn is the normalized Gaussian
3862/// - `landau, landaun`
3863/// - `expo`
3864/// - `pol1,...9, chebyshev1,...9`.
3865///
3866/// For printing the list of all available functions do:
3867///
3868/// TF1::InitStandardFunctions(); // not needed if `gROOT->GetFunction` is called before
3869/// gROOT->GetListOfFunctions()->ls()
3870///
3871/// `fname` can also be a formula that is accepted by the linear fitter containing the special operator `++`,
3872/// representing linear components separated by `++` sign, for example `x++sin(x)` for fitting `[0]*x+[1]*sin(x)`
3873///
3874/// This function finds a pointer to the TF1 object with name `fname` and calls TH1::Fit(TF1 *, Option_t *, Option_t *,
3875/// Double_t, Double_t). See there for the fitting options and the details about fitting histograms
3876
3878{
3879 char *linear;
3880 linear= (char*)strstr(fname, "++");
3881 Int_t ndim=GetDimension();
3882 if (linear){
3883 if (ndim<2){
3885 return Fit(&f1,option,goption,xxmin,xxmax);
3886 }
3887 else if (ndim<3){
3888 TF2 f2(fname, fname);
3889 return Fit(&f2,option,goption,xxmin,xxmax);
3890 }
3891 else{
3892 TF3 f3(fname, fname);
3893 return Fit(&f3,option,goption,xxmin,xxmax);
3894 }
3895 }
3896 else{
3897 TF1 * f1 = (TF1*)gROOT->GetFunction(fname);
3898 if (!f1) { Printf("Unknown function: %s",fname); return -1; }
3899 return Fit(f1,option,goption,xxmin,xxmax);
3900 }
3901}
3902
3903////////////////////////////////////////////////////////////////////////////////
3904/// Fit histogram with the function pointer f1.
3905///
3906/// \param[in] f1 pointer to the function object
3907/// \param[in] option string defining the fit options (see table below).
3908/// \param[in] goption specify a list of graphics options. See TH1::Draw for a complete list of these options.
3909/// \param[in] xxmin lower fitting range
3910/// \param[in] xxmax upper fitting range
3911/// \return A smart pointer to the TFitResult class
3912///
3913/// \anchor HFitOpt
3914/// ### Histogram Fitting Options
3915///
3916/// Here is the full list of fit options that can be given in the parameter `option`.
3917/// Several options can be used together by concatanating the strings without the need of any delimiters.
3918///
3919/// option | description
3920/// -------|------------
3921/// "L" | Uses a log likelihood method (default is chi-square method). To be used when the histogram represents counts.
3922/// "WL" | Weighted log likelihood method. To be used when the histogram has been filled with weights different than 1. This is needed for getting correct parameter uncertainties for weighted fits.
3923/// "P" | Uses Pearson chi-square method. Uses expected errors instead of the observed one (default case). The expected error is instead estimated from the square-root of the bin function value.
3924/// "MULTI" | Uses Loglikelihood method based on multi-nomial distribution. In this case the function must be normalized and one fits only the function shape.
3925/// "W" | Fit using the chi-square method and ignoring the bin uncertainties and skip empty bins.
3926/// "WW" | Fit using the chi-square method and ignoring the bin uncertainties and include the empty bins.
3927/// "I" | Uses the integral of function in the bin instead of the default bin center value.
3928/// "F" | Uses the default minimizer (e.g. Minuit) when fitting a linear function (e.g. polN) instead of the linear fitter.
3929/// "U" | Uses a user specified objective function (e.g. user providedlikelihood function) defined using `TVirtualFitter::SetFCN`
3930/// "E" | Performs a better parameter errors estimation using the Minos technique for all fit parameters.
3931/// "M" | Uses the IMPROVE algorithm (available only in TMinuit). This algorithm attempts improve the found local minimum by searching for a better one.
3932/// "S" | The full result of the fit is returned in the `TFitResultPtr`. This is needed to get the covariance matrix of the fit. See `TFitResult` and the base class `ROOT::Math::FitResult`.
3933/// "Q" | Quiet mode (minimum printing)
3934/// "V" | Verbose mode (default is between Q and V)
3935/// "+" | Adds this new fitted function to the list of fitted functions. By default, the previous function is deleted and only the last one is kept.
3936/// "N" | Does not store the graphics function, does not draw the histogram with the function after fitting.
3937/// "0" | Does not draw the histogram and the fitted function after fitting, but in contrast to option "N", it stores the fitted function in the histogram list of functions.
3938/// "R" | Fit using a fitting range specified in the function range with `TF1::SetRange`.
3939/// "B" | Use this option when you want to fix or set limits on one or more parameters and the fitting function is a predefined one (e.g gaus, expo,..), otherwise in case of pre-defined functions, some default initial values and limits will be used.
3940/// "C" | In case of linear fitting, do no calculate the chisquare (saves CPU time).
3941/// "G" | Uses the gradient implemented in `TF1::GradientPar` for the minimization. This allows to use Automatic Differentiation when it is supported by the provided TF1 function.
3942/// "WIDTH" | Scales the histogran bin content by the bin width (useful for variable bins histograms)
3943/// "SERIAL" | Runs in serial mode. By default if ROOT is built with MT support and MT is enables, the fit is perfomed in multi-thread - "E" Perform better Errors estimation using Minos technique
3944/// "MULTITHREAD" | Forces usage of multi-thread execution whenever possible
3945///
3946/// The default fitting of an histogram (when no option is given) is perfomed as following:
3947/// - a chi-square fit (see below Chi-square Fits) computed using the bin histogram errors and excluding bins with zero errors (empty bins);
3948/// - the full range of the histogram is used, unless TAxis::SetRange or TAxis::SetRangeUser was called before;
3949/// - the default Minimizer with its default configuration is used (see below Minimizer Configuration) except for linear function;
3950/// - for linear functions (`polN`, `chenbyshev` or formula expressions combined using operator `++`) a linear minimization is used.
3951/// - only the status of the fit is returned;
3952/// - the fit is performed in Multithread whenever is enabled in ROOT;
3953/// - only the last fitted function is saved in the histogram;
3954/// - the histogram is drawn after fitting overalyed with the resulting fitting function
3955///
3956/// \anchor HFitMinimizer
3957/// ### Minimizer Configuration
3958///
3959/// The Fit is perfomed using the default Minimizer, defined in the `ROOT::Math::MinimizerOptions` class.
3960/// It is possible to change the default minimizer and its configuration parameters by calling these static functions before fitting (before calling `TH1::Fit`):
3961/// - `ROOT::Math::MinimizerOptions::SetDefaultMinimizer(minimizerName, minimizerAgorithm)` for changing the minmizer and/or the corresponding algorithm.
3962/// For example `ROOT::Math::MinimizerOptions::SetDefaultMinimizer("GSLMultiMin","BFGS");` will set the usage of the BFGS algorithm of the GSL multi-dimensional minimization
3963/// The current defaults are ("Minuit","Migrad").
3964/// See the documentation of the `ROOT::Math::MinimizerOptions` for the available minimizers in ROOT and their corresponding algorithms.
3965/// - `ROOT::Math::MinimizerOptions::SetDefaultTolerance` for setting a different tolerance value for the minimization.
3966/// - `ROOT::Math::MinimizerOptions::SetDefaultMaxFunctionCalls` for setting the maximum number of function calls.
3967/// - `ROOT::Math::MinimizerOptions::SetDefaultPrintLevel` for changing the minimizer print level from level=0 (minimal printing) to level=3 maximum printing
3968///
3969/// Other options are possible depending on the Minimizer used, see the corresponding documentation.
3970/// The default minimizer can be also set in the resource file in etc/system.rootrc. For example
3971///
3972/// ~~~ {.cpp}
3973/// Root.Fitter: Minuit2
3974/// ~~~
3975///
3976/// \anchor HFitChi2
3977/// ### Chi-square Fits
3978///
3979/// By default a chi-square (least-square) fit is performed on the histogram. The so-called modified least-square method
3980/// is used where the residual for each bin is computed using as error the observed value (the bin error) returned by `TH1::GetBinError`
3981///
3982/// \f[
3983/// Chi2 = \sum_{i}{ \left(\frac{y(i) - f(x(i) | p )}{e(i)} \right)^2 }
3984/// \f]
3985///
3986/// where `y(i)` is the bin content for each bin `i`, `x(i)` is the bin center and `e(i)` is the bin error (`sqrt(y(i)` for
3987/// an un-weighted histogram). Bins with zero errors are excluded from the fit. See also later the note on the treatment
3988/// of empty bins. When using option "I" the residual is computed not using the function value at the bin center, `f(x(i)|p)`,
3989/// but the integral of the function in the bin, Integral{ f(x|p)dx }, divided by the bin volume.
3990/// When using option `P` (Pearson chi2), the expected error computed as `e(i) = sqrt(f(x(i)|p))` is used.
3991/// In this case empty bins are considered in the fit.
3992/// Both chi-square methods should not be used when the bin content represent counts, especially in case of low bin statistics,
3993/// because they could return a biased result.
3994///
3995/// \anchor HFitNLL
3996/// ### Likelihood Fits
3997///
3998/// When using option "L" a likelihood fit is used instead of the default chi-square fit.
3999/// The likelihood is built assuming a Poisson probability density function for each bin.
4000/// The negative log-likelihood to be minimized is
4001///
4002/// \f[
4003/// NLL = - \sum_{i}{ \log {\mathrm P} ( y(i) | f(x(i) | p ) ) }
4004/// \f]
4005/// where `P(y|f)` is the Poisson distribution of observing a count `y(i)` in the bin when the expected count is `f(x(i)|p)`.
4006/// The exact likelihood used is the Poisson likelihood described in this paper:
4007/// S. Baker and R. D. Cousins, “Clarification of the use of chi-square and likelihood functions in fits to histograms,”
4008/// Nucl. Instrum. Meth. 221 (1984) 437.
4009///
4010/// \f[
4011/// NLL = \sum_{i}{( f(x(i) | p ) + y(i)\log(y(i)/ f(x(i) | p )) - y(i)) }
4012/// \f]
4013/// By using this formulation, `2*NLL` can be interpreted as the chi-square resulting from the fit.
4014///
4015/// This method should be always used when the bin content represents counts (i.e. errors are sqrt(N) ).
4016/// The likelihood method has the advantage of treating correctly bins with low statistics. In case of high
4017/// statistics/bin the distribution of the bin content becomes a normal distribution and the likelihood and the chi2 fit
4018/// give the same result.
4019///
4020/// The likelihood method, although a bit slower, it is therefore the recommended method,
4021/// when the histogram represent counts (Poisson statistics), where the chi-square methods may
4022/// give incorrect results, especially in case of low statistics.
4023/// In case of a weighted histogram, it is possible to perform also a likelihood fit by using the
4024/// option "WL". Note a weighted histogram is a histogram which has been filled with weights and it
4025/// has the information on the sum of the weight square for each bin ( TH1::Sumw2() has been called).
4026/// The bin error for a weighted histogram is the square root of the sum of the weight square.
4027///
4028/// \anchor HFitRes
4029/// ### Fit Result
4030///
4031/// The function returns a TFitResultPtr which can hold a pointer to a TFitResult object.
4032/// By default the TFitResultPtr contains only the status of the fit which is return by an
4033/// automatic conversion of the TFitResultPtr to an integer. One can write in this case directly:
4034///
4035/// ~~~ {.cpp}
4036/// Int_t fitStatus = h->Fit(myFunc);
4037/// ~~~
4038///
4039/// If the option "S" is instead used, TFitResultPtr behaves as a smart
4040/// pointer to the TFitResult object. This is useful for retrieving the full result information from the fit, such as the covariance matrix,
4041/// as shown in this example code:
4042///
4043/// ~~~ {.cpp}
4044/// TFitResultPtr r = h->Fit(myFunc,"S");
4045/// TMatrixDSym cov = r->GetCovarianceMatrix(); // to access the covariance matrix
4046/// Double_t chi2 = r->Chi2(); // to retrieve the fit chi2
4047/// Double_t par0 = r->Parameter(0); // retrieve the value for the parameter 0
4048/// Double_t err0 = r->ParError(0); // retrieve the error for the parameter 0
4049/// r->Print("V"); // print full information of fit including covariance matrix
4050/// r->Write(); // store the result in a file
4051/// ~~~
4052///
4053/// The fit parameters, error and chi-square (but not covariance matrix) can be retrieved also
4054/// directly from the fitted function that is passed to this call.
4055/// Given a pointer to an associated fitted function `myfunc`, one can retrieve the function/fit
4056/// parameters with calls such as:
4057///
4058/// ~~~ {.cpp}
4059/// Double_t chi2 = myfunc->GetChisquare();
4060/// Double_t par0 = myfunc->GetParameter(0); //value of 1st parameter
4061/// Double_t err0 = myfunc->GetParError(0); //error on first parameter
4062/// ~~~
4063///
4064/// ##### Associated functions
4065///
4066/// One or more objects (typically a TF1*) can be added to the list
4067/// of functions (fFunctions) associated to each histogram.
4068/// When TH1::Fit is invoked, the fitted function is added to the histogram list of functions (fFunctions).
4069/// If the histogram is made persistent, the list of associated functions is also persistent.
4070/// Given a histogram h, one can retrieve an associated function with:
4071///
4072/// ~~~ {.cpp}
4073/// TF1 *myfunc = h->GetFunction("myfunc");
4074/// ~~~
4075/// or by quering directly the list obtained by calling `TH1::GetListOfFunctions`.
4076///
4077/// \anchor HFitStatus
4078/// ### Fit status
4079///
4080/// The status of the fit is obtained converting the TFitResultPtr to an integer
4081/// independently if the fit option "S" is used or not:
4082///
4083/// ~~~ {.cpp}
4084/// TFitResultPtr r = h->Fit(myFunc,opt);
4085/// Int_t fitStatus = r;
4086/// ~~~
4087///
4088/// - `status = 0` : the fit has been performed successfully (i.e no error occurred).
4089/// - `status < 0` : there is an error not connected with the minimization procedure, for example when a wrong function is used.
4090/// - `status > 0` : return status from Minimizer, depends on used Minimizer. For example for TMinuit and Minuit2 we have:
4091/// - `status = migradStatus + 10*minosStatus + 100*hesseStatus + 1000*improveStatus`.
4092/// TMinuit returns 0 (for migrad, minos, hesse or improve) in case of success and 4 in case of error (see the documentation of TMinuit::mnexcm). For example, for an error
4093/// only in Minos but not in Migrad a fitStatus of 40 will be returned.
4094/// Minuit2 returns 0 in case of success and different values in migrad,minos or
4095/// hesse depending on the error. See in this case the documentation of
4096/// Minuit2Minimizer::Minimize for the migrad return status, Minuit2Minimizer::GetMinosError for the
4097/// minos return status and Minuit2Minimizer::Hesse for the hesse return status.
4098/// If other minimizers are used see their specific documentation for the status code returned.
4099/// For example in the case of Fumili, see TFumili::Minimize.
4100///
4101/// \anchor HFitRange
4102/// ### Fitting in a range
4103///
4104/// In order to fit in a sub-range of the histogram you have two options:
4105/// - pass to this function the lower (`xxmin`) and upper (`xxmax`) values for the fitting range;
4106/// - define a specific range in the fitted function and use the fitting option "R".
4107/// For example, if your histogram has a defined range between -4 and 4 and you want to fit a gaussian
4108/// only in the interval 1 to 3, you can do:
4109///
4110/// ~~~ {.cpp}
4111/// TF1 *f1 = new TF1("f1", "gaus", 1, 3);
4112/// histo->Fit("f1", "R");
4113/// ~~~
4114///
4115/// The fitting range is also limited by the histogram range defined using TAxis::SetRange
4116/// or TAxis::SetRangeUser. Therefore the fitting range is the smallest range between the
4117/// histogram one and the one defined by one of the two previous options described above.
4118///
4119/// \anchor HFitInitial
4120/// ### Setting initial conditions
4121///
4122/// Parameters must be initialized before invoking the Fit function.
4123/// The setting of the parameter initial values is automatic for the
4124/// predefined functions such as poln, expo, gaus, landau. One can however disable
4125/// this automatic computation by using the option "B".
4126/// Note that if a predefined function is defined with an argument,
4127/// eg, gaus(0), expo(1), you must specify the initial values for
4128/// the parameters.
4129/// You can specify boundary limits for some or all parameters via
4130///
4131/// ~~~ {.cpp}
4132/// f1->SetParLimits(p_number, parmin, parmax);
4133/// ~~~
4134///
4135/// if `parmin >= parmax`, the parameter is fixed
4136/// Note that you are not forced to fix the limits for all parameters.
4137/// For example, if you fit a function with 6 parameters, you can do:
4138///
4139/// ~~~ {.cpp}
4140/// func->SetParameters(0, 3.1, 1.e-6, -8, 0, 100);
4141/// func->SetParLimits(3, -10, -4);
4142/// func->FixParameter(4, 0);
4143/// func->SetParLimits(5, 1, 1);
4144/// ~~~
4145///
4146/// With this setup, parameters 0->2 can vary freely
4147/// Parameter 3 has boundaries [-10,-4] with initial value -8
4148/// Parameter 4 is fixed to 0
4149/// Parameter 5 is fixed to 100.
4150/// When the lower limit and upper limit are equal, the parameter is fixed.
4151/// However to fix a parameter to 0, one must call the FixParameter function.
4152///
4153/// \anchor HFitStatBox
4154/// ### Fit Statistics Box
4155///
4156/// The statistics box can display the result of the fit.
4157/// You can change the statistics box to display the fit parameters with
4158/// the TStyle::SetOptFit(mode) method. This mode has four digits.
4159/// mode = pcev (default = 0111)
4160///
4161/// v = 1; print name/values of parameters
4162/// e = 1; print errors (if e=1, v must be 1)
4163/// c = 1; print Chisquare/Number of degrees of freedom
4164/// p = 1; print Probability
4165///
4166/// For example: gStyle->SetOptFit(1011);
4167/// prints the fit probability, parameter names/values, and errors.
4168/// You can change the position of the statistics box with these lines
4169/// (where g is a pointer to the TGraph):
4170///
4171/// TPaveStats *st = (TPaveStats*)g->GetListOfFunctions()->FindObject("stats");
4172/// st->SetX1NDC(newx1); //new x start position
4173/// st->SetX2NDC(newx2); //new x end position
4174///
4175/// \anchor HFitExtra
4176/// ### Additional Notes on Fitting
4177///
4178/// #### Fitting a histogram of dimension N with a function of dimension N-1
4179///
4180/// It is possible to fit a TH2 with a TF1 or a TH3 with a TF2.
4181/// In this case the chi-square is computed from the squared error distance between the function values and the bin centers weighted by the bin content.
4182/// For correct error scaling, the obtained parameter error are corrected as in the case when the
4183/// option "W" is used.
4184///
4185/// #### User defined objective functions
4186///
4187/// By default when fitting a chi square function is used for fitting. When option "L" is used
4188/// a Poisson likelihood function is used. Using option "MULTI" a multinomial likelihood fit is used.
4189/// Thes functions are defined in the header Fit/Chi2Func.h or Fit/PoissonLikelihoodFCN and they
4190/// are implemented using the routines FitUtil::EvaluateChi2 or FitUtil::EvaluatePoissonLogL in
4191/// the file math/mathcore/src/FitUtil.cxx.
4192/// It is possible to specify a user defined fitting function, using option "U" and
4193/// calling the following functions:
4194///
4195/// ~~~ {.cpp}
4196/// TVirtualFitter::Fitter(myhist)->SetFCN(MyFittingFunction);
4197/// ~~~
4198///
4199/// where MyFittingFunction is of type:
4200///
4201/// ~~~ {.cpp}
4202/// extern void MyFittingFunction(Int_t &npar, Double_t *gin, Double_t &f, Double_t *u, Int_t flag);
4203/// ~~~
4204///
4205/// #### Note on treatment of empty bins
4206///
4207/// Empty bins, which have the content equal to zero AND error equal to zero,
4208/// are excluded by default from the chi-square fit, but they are considered in the likelihood fit.
4209/// since they affect the likelihood if the function value in these bins is not negligible.
4210/// Note that if the histogram is having bins with zero content and non zero-errors they are considered as
4211/// any other bins in the fit. Instead bins with zero error and non-zero content are by default excluded in the chi-squared fit.
4212/// In general, one should not fit a histogram with non-empty bins and zero errors.
4213///
4214/// If the bin errors are not known, one should use the fit option "W", which gives a weight=1 for each bin (it is an unweighted least-square
4215/// fit). When using option "WW" the empty bins will be also considered in the chi-square fit with an error of 1.
4216/// Note that in this fitting case (option "W" or "WW") the resulting fitted parameter errors
4217/// are corrected by the obtained chi2 value using this scaling expression:
4218/// `errorp *= sqrt(chisquare/(ndf-1))` as it is done when fitting a TGraph with
4219/// no point errors.
4220///
4221/// #### Excluding points
4222///
4223/// You can use TF1::RejectPoint inside your fitting function to exclude some points
4224/// within a certain range from the fit. See the tutorial `fit/fitExclude.C`.
4225///
4226///
4227/// #### Warning when using the option "0"
4228///
4229/// When selecting the option "0", the fitted function is added to
4230/// the list of functions of the histogram, but it is not drawn when the histogram is drawn.
4231/// You can undo this behaviour resetting its corresponding bit in the TF1 object as following:
4232///
4233/// ~~~ {.cpp}
4234/// h.Fit("myFunction", "0"); // fit, store function but do not draw
4235/// h.Draw(); // function is not drawn
4236/// h.GetFunction("myFunction")->ResetBit(TF1::kNotDraw);
4237/// h.Draw(); // function is visible again
4238/// ~~~
4240
4242{
4243 // implementation of Fit method is in file hist/src/HFitImpl.cxx
4246
4247 // create range and minimizer options with default values
4250
4251 // need to empty the buffer before
4252 // (t.b.d. do a ML unbinned fit with buffer data)
4253 if (fBuffer) BufferEmpty();
4254
4256}
4257
4258////////////////////////////////////////////////////////////////////////////////
4259/// Display a panel with all histogram fit options.
4260///
4261/// See class TFitPanel for example
4262
4263void TH1::FitPanel()
4264{
4265 if (!gPad)
4266 gROOT->MakeDefCanvas();
4267
4268 if (!gPad) {
4269 Error("FitPanel", "Unable to create a default canvas");
4270 return;
4271 }
4272
4273
4274 // use plugin manager to create instance of TFitEditor
4275 TPluginHandler *handler = gROOT->GetPluginManager()->FindHandler("TFitEditor");
4276 if (handler && handler->LoadPlugin() != -1) {
4277 if (handler->ExecPlugin(2, gPad, this) == 0)
4278 Error("FitPanel", "Unable to create the FitPanel");
4279 }
4280 else
4281 Error("FitPanel", "Unable to find the FitPanel plug-in");
4282}
4283
4284////////////////////////////////////////////////////////////////////////////////
4285/// Return a histogram containing the asymmetry of this histogram with h2,
4286/// where the asymmetry is defined as:
4287///
4288/// ~~~ {.cpp}
4289/// Asymmetry = (h1 - h2)/(h1 + h2) where h1 = this
4290/// ~~~
4291///
4292/// works for 1D, 2D, etc. histograms
4293/// c2 is an optional argument that gives a relative weight between the two
4294/// histograms, and dc2 is the error on this weight. This is useful, for example,
4295/// when forming an asymmetry between two histograms from 2 different data sets that
4296/// need to be normalized to each other in some way. The function calculates
4297/// the errors assuming Poisson statistics on h1 and h2 (that is, dh = sqrt(h)).
4298///
4299/// example: assuming 'h1' and 'h2' are already filled
4300///
4301/// ~~~ {.cpp}
4302/// h3 = h1->GetAsymmetry(h2)
4303/// ~~~
4304///
4305/// then 'h3' is created and filled with the asymmetry between 'h1' and 'h2';
4306/// h1 and h2 are left intact.
4307///
4308/// Note that it is the user's responsibility to manage the created histogram.
4309/// The name of the returned histogram will be `Asymmetry_nameOfh1-nameOfh2`
4310///
4311/// code proposed by Jason Seely (seely@mit.edu) and adapted by R.Brun
4312///
4313/// clone the histograms so top and bottom will have the
4314/// correct dimensions:
4315/// Sumw2 just makes sure the errors will be computed properly
4316/// when we form sums and ratios below.
4317
4319{
4320 TH1 *h1 = this;
4321 TString name = TString::Format("Asymmetry_%s-%s",h1->GetName(),h2->GetName() );
4322 TH1 *asym = (TH1*)Clone(name);
4323
4324 // set also the title
4325 TString title = TString::Format("(%s - %s)/(%s+%s)",h1->GetName(),h2->GetName(),h1->GetName(),h2->GetName() );
4326 asym->SetTitle(title);
4327
4328 asym->Sumw2();
4331 TH1 *top = (TH1*)asym->Clone();
4332 TH1 *bottom = (TH1*)asym->Clone();
4334
4335 // form the top and bottom of the asymmetry, and then divide:
4336 top->Add(h1,h2,1,-c2);
4337 bottom->Add(h1,h2,1,c2);
4338 asym->Divide(top,bottom);
4339
4340 Int_t xmax = asym->GetNbinsX();
4341 Int_t ymax = asym->GetNbinsY();
4342 Int_t zmax = asym->GetNbinsZ();
4343
4344 if (h1->fBuffer) h1->BufferEmpty(1);
4345 if (h2->fBuffer) h2->BufferEmpty(1);
4346 if (bottom->fBuffer) bottom->BufferEmpty(1);
4347
4348 // now loop over bins to calculate the correct errors
4349 // the reason this error calculation looks complex is because of c2
4350 for(Int_t i=1; i<= xmax; i++){
4351 for(Int_t j=1; j<= ymax; j++){
4352 for(Int_t k=1; k<= zmax; k++){
4353 Int_t bin = GetBin(i, j, k);
4354 // here some bin contents are written into variables to make the error
4355 // calculation a little more legible:
4357 Double_t b = h2->RetrieveBinContent(bin);
4358 Double_t bot = bottom->RetrieveBinContent(bin);
4359
4360 // make sure there are some events, if not, then the errors are set = 0
4361 // automatically.
4362 //if(bot < 1){} was changed to the next line from recommendation of Jason Seely (28 Nov 2005)
4363 if(bot < 1e-6){}
4364 else{
4365 // computation of errors by Christos Leonidopoulos
4367 Double_t dbsq = h2->GetBinErrorSqUnchecked(bin);
4368 Double_t error = 2*TMath::Sqrt(a*a*c2*c2*dbsq + c2*c2*b*b*dasq+a*a*b*b*dc2*dc2)/(bot*bot);
4369 asym->SetBinError(i,j,k,error);
4370 }
4371 }
4372 }
4373 }
4374 delete top;
4375 delete bottom;
4376
4377 return asym;
4378}
4379
4380////////////////////////////////////////////////////////////////////////////////
4381/// Static function
4382/// return the default buffer size for automatic histograms
4383/// the parameter fgBufferSize may be changed via SetDefaultBufferSize
4384
4386{
4387 return fgBufferSize;
4388}
4389
4390////////////////////////////////////////////////////////////////////////////////
4391/// Return kTRUE if TH1::Sumw2 must be called when creating new histograms.
4392/// see TH1::SetDefaultSumw2.
4393
4395{
4396 return fgDefaultSumw2;
4397}
4398
4399////////////////////////////////////////////////////////////////////////////////
4400/// Return the current number of entries.
4401
4403{
4404 if (fBuffer) {
4405 Int_t nentries = (Int_t) fBuffer[0];
4406 if (nentries > 0) return nentries;
4407 }
4408
4409 return fEntries;
4410}
4411
4412////////////////////////////////////////////////////////////////////////////////
4413/// Number of effective entries of the histogram.
4414///
4415/// \f[
4416/// neff = \frac{(\sum Weights )^2}{(\sum Weight^2 )}
4417/// \f]
4418///
4419/// In case of an unweighted histogram this number is equivalent to the
4420/// number of entries of the histogram.
4421/// For a weighted histogram, this number corresponds to the hypothetical number of unweighted entries
4422/// a histogram would need to have the same statistical power as this weighted histogram.
4423/// Note: The underflow/overflow are included if one has set the TH1::StatOverFlows flag
4424/// and if the statistics has been computed at filling time.
4425/// If a range is set in the histogram the number is computed from the given range.
4426
4428{
4429 Stat_t s[kNstat];
4430 this->GetStats(s);// s[1] sum of squares of weights, s[0] sum of weights
4431 return (s[1] ? s[0]*s[0]/s[1] : TMath::Abs(s[0]) );
4432}
4433
4434////////////////////////////////////////////////////////////////////////////////
4435/// Shortcut to set the three histogram colors with a single call.
4436///
4437/// By default: linecolor = markercolor = fillcolor = -1
4438/// If a color is < 0 this method does not change the corresponding color if positive or null it set the color.
4439///
4440/// For instance:
4441/// ~~~ {.cpp}
4442/// h->SetColors(kRed, kRed);
4443/// ~~~
4444/// will set the line color and the marker color to red.
4445
4447{
4448 if (linecolor >= 0)
4450 if (markercolor >= 0)
4452 if (fillcolor >= 0)
4454}
4455
4456
4457////////////////////////////////////////////////////////////////////////////////
4458/// Set highlight (enable/disable) mode for the histogram
4459/// by default highlight mode is disable
4460
4461void TH1::SetHighlight(Bool_t set)
4462{
4463 if (IsHighlight() == set)
4464 return;
4465 if (fDimension > 2) {
4466 Info("SetHighlight", "Supported only 1-D or 2-D histograms");
4467 return;
4468 }
4469
4470 SetBit(kIsHighlight, set);
4471
4472 if (fPainter)
4474}
4475
4476////////////////////////////////////////////////////////////////////////////////
4477/// Redefines TObject::GetObjectInfo.
4478/// Displays the histogram info (bin number, contents, integral up to bin
4479/// corresponding to cursor position px,py
4480
4481char *TH1::GetObjectInfo(Int_t px, Int_t py) const
4482{
4483 return ((TH1*)this)->GetPainter()->GetObjectInfo(px,py);
4484}
4485
4486////////////////////////////////////////////////////////////////////////////////
4487/// Return pointer to painter.
4488/// If painter does not exist, it is created
4489
4491{
4492 if (!fPainter) {
4493 TString opt = option;
4494 opt.ToLower();
4495 if (opt.Contains("gl") || gStyle->GetCanvasPreferGL()) {
4496 //try to create TGLHistPainter
4497 TPluginHandler *handler = gROOT->GetPluginManager()->FindHandler("TGLHistPainter");
4498
4499 if (handler && handler->LoadPlugin() != -1)
4500 fPainter = reinterpret_cast<TVirtualHistPainter *>(handler->ExecPlugin(1, this));
4501 }
4502 }
4503
4505
4506 return fPainter;
4507}
4508
4509////////////////////////////////////////////////////////////////////////////////
4510/// Compute Quantiles for this histogram.
4511/// A quantile x_p := Q(p) is defined as the value x_p such that the cumulative
4512/// probability distribution Function F of variable X yields:
4513///
4514/// ~~~ {.cpp}
4515/// F(x_p) = Pr(X <= x_p) = p with 0 <= p <= 1.
4516/// x_p = Q(p) = F_inv(p)
4517/// ~~~
4518///
4519/// For instance the median x_0.5 of a distribution is defined as that value
4520/// of the random variable X for which the distribution function equals 0.5:
4521///
4522/// ~~~ {.cpp}
4523/// F(x_0.5) = Probability(X < x_0.5) = 0.5
4524/// x_0.5 = Q(0.5)
4525/// ~~~
4526///
4527/// \author Eddy Offermann
4528/// code from Eddy Offermann, Renaissance
4529///
4530/// \param[in] n maximum size of the arrays xp and p (if given)
4531/// \param[out] xp array to be filled with nq quantiles evaluated at (p). Memory has to be preallocated by caller.
4532/// - If `p == nullptr`, the quantiles are computed at the (first `n`) probabilities p given by the CDF of the histogram;
4533/// `n` must thus be smaller or equal Nbins+1, otherwise the extra values of `xp` will not be filled and `nq` will be smaller than `n`.
4534/// If all bins have non-zero entries, the quantiles happen to be the bin centres.
4535/// Empty bins will, however, be skipped in the quantiles.
4536/// If the CDF is e.g. [0., 0., 0.1, ...], the quantiles would be, [3., 3., 3., ...], with the third bin starting
4537/// at 3.
4538/// \param[in] p array of cumulative probabilities where quantiles should be evaluated.
4539/// - if `p == nullptr`, the CDF of the histogram will be used to compute the quantiles, and will
4540/// have a size of n.
4541/// - Otherwise, it is assumed to contain at least n values.
4542/// \return number of quantiles computed
4543/// \note Unlike in TF1::GetQuantiles, `p` is here an optional argument
4544///
4545/// Note that the Integral of the histogram is automatically recomputed
4546/// if the number of entries is different of the number of entries when
4547/// the integral was computed last time. In case you do not use the Fill
4548/// functions to fill your histogram, but SetBinContent, you must call
4549/// TH1::ComputeIntegral before calling this function.
4550///
4551/// Getting quantiles xp from two histograms and storing results in a TGraph,
4552/// a so-called QQ-plot
4553///
4554/// ~~~ {.cpp}
4555/// TGraph *gr = new TGraph(nprob);
4556/// h1->GetQuantiles(nprob,gr->GetX());
4557/// h2->GetQuantiles(nprob,gr->GetY());
4558/// gr->Draw("alp");
4559/// ~~~
4560///
4561/// Example:
4562///
4563/// ~~~ {.cpp}
4564/// void quantiles() {
4565/// // demo for quantiles
4566/// const Int_t nq = 20;
4567/// TH1F *h = new TH1F("h","demo quantiles",100,-3,3);
4568/// h->FillRandom("gaus",5000);
4569/// h->GetXaxis()->SetTitle("x");
4570/// h->GetYaxis()->SetTitle("Counts");
4571///
4572/// Double_t p[nq]; // probabilities where to evaluate the quantiles in [0,1]
4573/// Double_t xp[nq]; // array of positions X to store the resulting quantiles
4574/// for (Int_t i=0;i<nq;i++) p[i] = Float_t(i+1)/nq;
4575/// h->GetQuantiles(nq,xp,p);
4576///
4577/// //show the original histogram in the top pad
4578/// TCanvas *c1 = new TCanvas("c1","demo quantiles",10,10,700,900);
4579/// c1->Divide(1,2);
4580/// c1->cd(1);
4581/// h->Draw();
4582///
4583/// // show the quantiles in the bottom pad
4584/// c1->cd(2);
4585/// gPad->SetGrid();
4586/// TGraph *gr = new TGraph(nq,p,xp);
4587/// gr->SetMarkerStyle(21);
4588/// gr->GetXaxis()->SetTitle("p");
4589/// gr->GetYaxis()->SetTitle("x");
4590/// gr->Draw("alp");
4591/// }
4592/// ~~~
4593
4595{
4596 if (GetDimension() > 1) {
4597 Error("GetQuantiles","Only available for 1-d histograms");
4598 return 0;
4599 }
4600
4601 const Int_t nbins = GetXaxis()->GetNbins();
4602 if (!fIntegral) ComputeIntegral();
4603 if (fIntegral[nbins+1] != fEntries) ComputeIntegral();
4604
4605 Int_t i, ibin;
4606 Int_t nq = n;
4607 std::unique_ptr<Double_t[]> localProb;
4608 if (p == nullptr) {
4609 nq = nbins+1;
4610 localProb.reset(new Double_t[nq]);
4611 localProb[0] = 0;
4612 for (i=1;i<nq;i++) {
4613 localProb[i] = fIntegral[i] / fIntegral[nbins];
4614 }
4615 }
4616 Double_t const *const prob = p ? p : localProb.get();
4617
4618 for (i = 0; i < nq; i++) {
4620 if (fIntegral[ibin] == prob[i]) {
4621 if (prob[i] == 0.) {
4622 for (; ibin+1 <= nbins && fIntegral[ibin+1] == 0.; ++ibin) {
4623
4624 }
4625 xp[i] = fXaxis.GetBinUpEdge(ibin);
4626 }
4627 else if (prob[i] == 1.) {
4628 xp[i] = fXaxis.GetBinUpEdge(ibin);
4629 }
4630 else {
4631 // Find equal integral in later bins (ie their entries are zero)
4632 Double_t width = 0;
4633 for (Int_t j = ibin+1; j <= nbins; ++j) {
4634 if (prob[i] == fIntegral[j]) {
4636 }
4637 else
4638 break;
4639 }
4641 }
4642 }
4643 else {
4644 xp[i] = GetBinLowEdge(ibin+1);
4646 if (dint > 0) xp[i] += GetBinWidth(ibin+1)*(prob[i]-fIntegral[ibin])/dint;
4647 }
4648 }
4649
4650 return nq;
4651}
4652
4653////////////////////////////////////////////////////////////////////////////////
4659 return 1;
4660}
4661
4662////////////////////////////////////////////////////////////////////////////////
4663/// Compute Initial values of parameters for a gaussian.
4664
4665void H1InitGaus()
4666{
4667 Double_t allcha, sumx, sumx2, x, val, stddev, mean;
4668 Int_t bin;
4669 const Double_t sqrtpi = 2.506628;
4670
4671 // - Compute mean value and StdDev of the histogram in the given range
4673 TH1 *curHist = (TH1*)hFitter->GetObjectFit();
4674 Int_t hxfirst = hFitter->GetXfirst();
4675 Int_t hxlast = hFitter->GetXlast();
4676 Double_t valmax = curHist->GetBinContent(hxfirst);
4677 Double_t binwidx = curHist->GetBinWidth(hxfirst);
4678 allcha = sumx = sumx2 = 0;
4679 for (bin=hxfirst;bin<=hxlast;bin++) {
4680 x = curHist->GetBinCenter(bin);
4681 val = TMath::Abs(curHist->GetBinContent(bin));
4682 if (val > valmax) valmax = val;
4683 sumx += val*x;
4684 sumx2 += val*x*x;
4685 allcha += val;
4686 }
4687 if (allcha == 0) return;
4688 mean = sumx/allcha;
4689 stddev = sumx2/allcha - mean*mean;
4690 if (stddev > 0) stddev = TMath::Sqrt(stddev);
4691 else stddev = 0;
4692 if (stddev == 0) stddev = binwidx*(hxlast-hxfirst+1)/4;
4693 //if the distribution is really gaussian, the best approximation
4694 //is binwidx*allcha/(sqrtpi*stddev)
4695 //However, in case of non-gaussian tails, this underestimates
4696 //the normalisation constant. In this case the maximum value
4697 //is a better approximation.
4698 //We take the average of both quantities
4700
4701 //In case the mean value is outside the histo limits and
4702 //the StdDev is bigger than the range, we take
4703 // mean = center of bins
4704 // stddev = half range
4705 Double_t xmin = curHist->GetXaxis()->GetXmin();
4706 Double_t xmax = curHist->GetXaxis()->GetXmax();
4707 if ((mean < xmin || mean > xmax) && stddev > (xmax-xmin)) {
4708 mean = 0.5*(xmax+xmin);
4709 stddev = 0.5*(xmax-xmin);
4710 }
4711 TF1 *f1 = (TF1*)hFitter->GetUserFunc();
4713 f1->SetParameter(1,mean);
4715 f1->SetParLimits(2,0,10*stddev);
4716}
4717
4718////////////////////////////////////////////////////////////////////////////////
4719/// Compute Initial values of parameters for an exponential.
4720
4721void H1InitExpo()
4722{
4724 Int_t ifail;
4726 Int_t hxfirst = hFitter->GetXfirst();
4727 Int_t hxlast = hFitter->GetXlast();
4728 Int_t nchanx = hxlast - hxfirst + 1;
4729
4731
4732 TF1 *f1 = (TF1*)hFitter->GetUserFunc();
4734 f1->SetParameter(1,slope);
4735
4736}
4737
4738////////////////////////////////////////////////////////////////////////////////
4739/// Compute Initial values of parameters for a polynom.
4740
4741void H1InitPolynom()
4742{
4743 Double_t fitpar[25];
4744
4746 TF1 *f1 = (TF1*)hFitter->GetUserFunc();
4747 Int_t hxfirst = hFitter->GetXfirst();
4748 Int_t hxlast = hFitter->GetXlast();
4749 Int_t nchanx = hxlast - hxfirst + 1;
4750 Int_t npar = f1->GetNpar();
4751
4752 if (nchanx <=1 || npar == 1) {
4753 TH1 *curHist = (TH1*)hFitter->GetObjectFit();
4754 fitpar[0] = curHist->GetSumOfWeights()/Double_t(nchanx);
4755 } else {
4757 }
4758 for (Int_t i=0;i<npar;i++) f1->SetParameter(i, fitpar[i]);
4759}
4760
4761////////////////////////////////////////////////////////////////////////////////
4762/// Least squares lpolynomial fitting without weights.
4763///
4764/// \param[in] n number of points to fit
4765/// \param[in] m number of parameters
4766/// \param[in] a array of parameters
4767///
4768/// based on CERNLIB routine LSQ: Translated to C++ by Rene Brun
4769/// (E.Keil. revised by B.Schorr, 23.10.1981.)
4770
4772{
4773 const Double_t zero = 0.;
4774 const Double_t one = 1.;
4775 const Int_t idim = 20;
4776
4777 Double_t b[400] /* was [20][20] */;
4778 Int_t i, k, l, ifail;
4780 Double_t da[20], xk, yk;
4781
4782 if (m <= 2) {
4783 H1LeastSquareLinearFit(n, a[0], a[1], ifail);
4784 return;
4785 }
4786 if (m > idim || m > n) return;
4787 b[0] = Double_t(n);
4788 da[0] = zero;
4789 for (l = 2; l <= m; ++l) {
4790 b[l-1] = zero;
4791 b[m + l*20 - 21] = zero;
4792 da[l-1] = zero;
4793 }
4795 TH1 *curHist = (TH1*)hFitter->GetObjectFit();
4796 Int_t hxfirst = hFitter->GetXfirst();
4797 Int_t hxlast = hFitter->GetXlast();
4798 for (k = hxfirst; k <= hxlast; ++k) {
4799 xk = curHist->GetBinCenter(k);
4800 yk = curHist->GetBinContent(k);
4801 power = one;
4802 da[0] += yk;
4803 for (l = 2; l <= m; ++l) {
4804 power *= xk;
4805 b[l-1] += power;
4806 da[l-1] += power*yk;
4807 }
4808 for (l = 2; l <= m; ++l) {
4809 power *= xk;
4810 b[m + l*20 - 21] += power;
4811 }
4812 }
4813 for (i = 3; i <= m; ++i) {
4814 for (k = i; k <= m; ++k) {
4815 b[k - 1 + (i-1)*20 - 21] = b[k + (i-2)*20 - 21];
4816 }
4817 }
4819
4820 for (i=0; i<m; ++i) a[i] = da[i];
4821
4822}
4823
4824////////////////////////////////////////////////////////////////////////////////
4825/// Least square linear fit without weights.
4826///
4827/// extracted from CERNLIB LLSQ: Translated to C++ by Rene Brun
4828/// (added to LSQ by B. Schorr, 15.02.1982.)
4829
4831{
4833 Int_t i, n;
4835 Double_t fn, xk, yk;
4836 Double_t det;
4837
4838 n = TMath::Abs(ndata);
4839 ifail = -2;
4840 xbar = ybar = x2bar = xybar = 0;
4842 TH1 *curHist = (TH1*)hFitter->GetObjectFit();
4843 Int_t hxfirst = hFitter->GetXfirst();
4844 Int_t hxlast = hFitter->GetXlast();
4845 for (i = hxfirst; i <= hxlast; ++i) {
4846 xk = curHist->GetBinCenter(i);
4847 yk = curHist->GetBinContent(i);
4848 if (ndata < 0) {
4849 if (yk <= 0) yk = 1e-9;
4850 yk = TMath::Log(yk);
4851 }
4852 xbar += xk;
4853 ybar += yk;
4854 x2bar += xk*xk;
4855 xybar += xk*yk;
4856 }
4857 fn = Double_t(n);
4858 det = fn*x2bar - xbar*xbar;
4859 ifail = -1;
4860 if (det <= 0) {
4861 a0 = ybar/fn;
4862 a1 = 0;
4863 return;
4864 }
4865 ifail = 0;
4866 a0 = (x2bar*ybar - xbar*xybar) / det;
4867 a1 = (fn*xybar - xbar*ybar) / det;
4868
4869}
4870
4871////////////////////////////////////////////////////////////////////////////////
4872/// Extracted from CERN Program library routine DSEQN.
4873///
4874/// Translated to C++ by Rene Brun
4875
4877{
4879 Int_t nmjp1, i, j, l;
4880 Int_t im1, jp1, nm1, nmi;
4881 Double_t s1, s21, s22;
4882 const Double_t one = 1.;
4883
4884 /* Parameter adjustments */
4885 b_dim1 = idim;
4886 b_offset = b_dim1 + 1;
4887 b -= b_offset;
4888 a_dim1 = idim;
4889 a_offset = a_dim1 + 1;
4890 a -= a_offset;
4891
4892 if (idim < n) return;
4893
4894 ifail = 0;
4895 for (j = 1; j <= n; ++j) {
4896 if (a[j + j*a_dim1] <= 0) { ifail = -1; return; }
4897 a[j + j*a_dim1] = one / a[j + j*a_dim1];
4898 if (j == n) continue;
4899 jp1 = j + 1;
4900 for (l = jp1; l <= n; ++l) {
4901 a[j + l*a_dim1] = a[j + j*a_dim1] * a[l + j*a_dim1];
4902 s1 = -a[l + (j+1)*a_dim1];
4903 for (i = 1; i <= j; ++i) { s1 = a[l + i*a_dim1] * a[i + (j+1)*a_dim1] + s1; }
4904 a[l + (j+1)*a_dim1] = -s1;
4905 }
4906 }
4907 if (k <= 0) return;
4908
4909 for (l = 1; l <= k; ++l) {
4910 b[l*b_dim1 + 1] = a[a_dim1 + 1]*b[l*b_dim1 + 1];
4911 }
4912 if (n == 1) return;
4913 for (l = 1; l <= k; ++l) {
4914 for (i = 2; i <= n; ++i) {
4915 im1 = i - 1;
4916 s21 = -b[i + l*b_dim1];
4917 for (j = 1; j <= im1; ++j) {
4918 s21 = a[i + j*a_dim1]*b[j + l*b_dim1] + s21;
4919 }
4920 b[i + l*b_dim1] = -a[i + i*a_dim1]*s21;
4921 }
4922 nm1 = n - 1;
4923 for (i = 1; i <= nm1; ++i) {
4924 nmi = n - i;
4925 s22 = -b[nmi + l*b_dim1];
4926 for (j = 1; j <= i; ++j) {
4927 nmjp1 = n - j + 1;
4928 s22 = a[nmi + nmjp1*a_dim1]*b[nmjp1 + l*b_dim1] + s22;
4929 }
4930 b[nmi + l*b_dim1] = -s22;
4931 }
4932 }
4933}
4934
4935////////////////////////////////////////////////////////////////////////////////
4936/// Return Global bin number corresponding to binx,y,z.
4937///
4938/// 2-D and 3-D histograms are represented with a one dimensional
4939/// structure.
4940/// This has the advantage that all existing functions, such as
4941/// GetBinContent, GetBinError, GetBinFunction work for all dimensions.
4942///
4943/// In case of a TH1x, returns binx directly.
4944/// see TH1::GetBinXYZ for the inverse transformation.
4945///
4946/// Convention for numbering bins
4947///
4948/// For all histogram types: nbins, xlow, xup
4949///
4950/// - bin = 0; underflow bin
4951/// - bin = 1; first bin with low-edge xlow INCLUDED
4952/// - bin = nbins; last bin with upper-edge xup EXCLUDED
4953/// - bin = nbins+1; overflow bin
4954///
4955/// In case of 2-D or 3-D histograms, a "global bin" number is defined.
4956/// For example, assuming a 3-D histogram with binx,biny,binz, the function
4957///
4958/// ~~~ {.cpp}
4959/// Int_t bin = h->GetBin(binx,biny,binz);
4960/// ~~~
4961///
4962/// returns a global/linearized bin number. This global bin is useful
4963/// to access the bin information independently of the dimension.
4964
4966{
4967 Int_t ofx = fXaxis.GetNbins() + 1; // overflow bin
4968 if (binx < 0) binx = 0;
4969 if (binx > ofx) binx = ofx;
4970
4971 return binx;
4972}
4973
4974////////////////////////////////////////////////////////////////////////////////
4975/// Return binx, biny, binz corresponding to the global bin number globalbin
4976/// see TH1::GetBin function above
4977
4979{
4980 Int_t nx = fXaxis.GetNbins()+2;
4981 Int_t ny = fYaxis.GetNbins()+2;
4982
4983 if (GetDimension() == 1) {
4984 binx = binglobal%nx;
4985 biny = 0;
4986 binz = 0;
4987 return;
4988 }
4989 if (GetDimension() == 2) {
4990 binx = binglobal%nx;
4991 biny = ((binglobal-binx)/nx)%ny;
4992 binz = 0;
4993 return;
4994 }
4995 if (GetDimension() == 3) {
4996 binx = binglobal%nx;
4997 biny = ((binglobal-binx)/nx)%ny;
4998 binz = ((binglobal-binx)/nx -biny)/ny;
4999 }
5000}
5001
5002////////////////////////////////////////////////////////////////////////////////
5003/// Return a random number distributed according the histogram bin contents.
5004/// This function checks if the bins integral exists. If not, the integral
5005/// is evaluated, normalized to one.
5006///
5007/// @param rng (optional) Random number generator pointer used (default is gRandom)
5008///
5009/// The integral is automatically recomputed if the number of entries
5010/// is not the same then when the integral was computed.
5011/// NB Only valid for 1-d histograms. Use GetRandom2 or 3 otherwise.
5012/// If the histogram has a bin with negative content a NaN is returned
5013
5015{
5016 if (fDimension > 1) {
5017 Error("GetRandom","Function only valid for 1-d histograms");
5018 return 0;
5019 }
5021 Double_t integral = 0;
5022 // compute integral checking that all bins have positive content (see ROOT-5894)
5023 if (fIntegral) {
5024 if (fIntegral[nbinsx+1] != fEntries) integral = ((TH1*)this)->ComputeIntegral(true);
5025 else integral = fIntegral[nbinsx];
5026 } else {
5027 integral = ((TH1*)this)->ComputeIntegral(true);
5028 }
5029 if (integral == 0) return 0;
5030 // return a NaN in case some bins have negative content
5031 if (integral == TMath::QuietNaN() ) return TMath::QuietNaN();
5032
5033 Double_t r1 = (rng) ? rng->Rndm() : gRandom->Rndm();
5036 if (r1 > fIntegral[ibin]) x +=
5038 return x;
5039}
5040
5041////////////////////////////////////////////////////////////////////////////////
5042/// Return content of bin number bin.
5043///
5044/// Implemented in TH1C,S,F,D
5045///
5046/// Convention for numbering bins
5047///
5048/// For all histogram types: nbins, xlow, xup
5049///
5050/// - bin = 0; underflow bin
5051/// - bin = 1; first bin with low-edge xlow INCLUDED
5052/// - bin = nbins; last bin with upper-edge xup EXCLUDED
5053/// - bin = nbins+1; overflow bin
5054///
5055/// In case of 2-D or 3-D histograms, a "global bin" number is defined.
5056/// For example, assuming a 3-D histogram with binx,biny,binz, the function
5057///
5058/// ~~~ {.cpp}
5059/// Int_t bin = h->GetBin(binx,biny,binz);
5060/// ~~~
5061///
5062/// returns a global/linearized bin number. This global bin is useful
5063/// to access the bin information independently of the dimension.
5064
5066{
5067 if (fBuffer) const_cast<TH1*>(this)->BufferEmpty();
5068 if (bin < 0) bin = 0;
5069 if (bin >= fNcells) bin = fNcells-1;
5070
5071 return RetrieveBinContent(bin);
5072}
5073
5074////////////////////////////////////////////////////////////////////////////////
5075/// Compute first binx in the range [firstx,lastx] for which
5076/// diff = abs(bin_content-c) <= maxdiff
5077///
5078/// In case several bins in the specified range with diff=0 are found
5079/// the first bin found is returned in binx.
5080/// In case several bins in the specified range satisfy diff <=maxdiff
5081/// the bin with the smallest difference is returned in binx.
5082/// In all cases the function returns the smallest difference.
5083///
5084/// NOTE1: if firstx <= 0, firstx is set to bin 1
5085/// if (lastx < firstx then firstx is set to the number of bins
5086/// ie if firstx=0 and lastx=0 (default) the search is on all bins.
5087///
5088/// NOTE2: if maxdiff=0 (default), the first bin with content=c is returned.
5089
5091{
5092 if (fDimension > 1) {
5093 binx = 0;
5094 Error("GetBinWithContent","function is only valid for 1-D histograms");
5095 return 0;
5096 }
5097
5098 if (fBuffer) ((TH1*)this)->BufferEmpty();
5099
5100 if (firstx <= 0) firstx = 1;
5101 if (lastx < firstx) lastx = fXaxis.GetNbins();
5102 Int_t binminx = 0;
5103 Double_t diff, curmax = 1.e240;
5104 for (Int_t i=firstx;i<=lastx;i++) {
5106 if (diff <= 0) {binx = i; return diff;}
5107 if (diff < curmax && diff <= maxdiff) {curmax = diff, binminx=i;}
5108 }
5109 binx = binminx;
5110 return curmax;
5111}
5112
5113////////////////////////////////////////////////////////////////////////////////
5114/// Given a point x, approximates the value via linear interpolation
5115/// based on the two nearest bin centers
5116///
5117/// Andy Mastbaum 10/21/08
5118
5120{
5121 if (fBuffer) ((TH1*)this)->BufferEmpty();
5122
5124 Double_t x0,x1,y0,y1;
5125
5126 if(x<=GetBinCenter(1)) {
5127 return RetrieveBinContent(1);
5128 } else if(x>=GetBinCenter(GetNbinsX())) {
5129 return RetrieveBinContent(GetNbinsX());
5130 } else {
5131 if(x<=GetBinCenter(xbin)) {
5133 x0 = GetBinCenter(xbin-1);
5135 x1 = GetBinCenter(xbin);
5136 } else {
5138 x0 = GetBinCenter(xbin);
5140 x1 = GetBinCenter(xbin+1);
5141 }
5142 return y0 + (x-x0)*((y1-y0)/(x1-x0));
5143 }
5144}
5145
5146////////////////////////////////////////////////////////////////////////////////
5147/// 2d Interpolation. Not yet implemented.
5148
5150{
5151 Error("Interpolate","This function must be called with 1 argument for a TH1");
5152 return 0;
5153}
5154
5155////////////////////////////////////////////////////////////////////////////////
5156/// 3d Interpolation. Not yet implemented.
5157
5159{
5160 Error("Interpolate","This function must be called with 1 argument for a TH1");
5161 return 0;
5162}
5163
5164///////////////////////////////////////////////////////////////////////////////
5165/// Check if a histogram is empty
5166/// (this is a protected method used mainly by TH1Merger )
5167
5168Bool_t TH1::IsEmpty() const
5169{
5170 // if fTsumw or fentries are not zero histogram is not empty
5171 // need to use GetEntries() instead of fEntries in case of bugger histograms
5172 // so we will flash the buffer
5173 if (fTsumw != 0) return kFALSE;
5174 if (GetEntries() != 0) return kFALSE;
5175 // case fTSumw == 0 amd entries are also zero
5176 // this should not really happening, but if one sets content by hand
5177 // it can happen. a call to ResetStats() should be done in such cases
5178 double sumw = 0;
5179 for (int i = 0; i< GetNcells(); ++i) sumw += RetrieveBinContent(i);
5180 return (sumw != 0) ? kFALSE : kTRUE;
5181}
5182
5183////////////////////////////////////////////////////////////////////////////////
5184/// Return true if the bin is overflow.
5185
5187{
5188 Int_t binx, biny, binz;
5189 GetBinXYZ(bin, binx, biny, binz);
5190
5191 if (iaxis == 0) {
5192 if ( fDimension == 1 )
5193 return binx >= GetNbinsX() + 1;
5194 if ( fDimension == 2 )
5195 return (binx >= GetNbinsX() + 1) ||
5196 (biny >= GetNbinsY() + 1);
5197 if ( fDimension == 3 )
5198 return (binx >= GetNbinsX() + 1) ||
5199 (biny >= GetNbinsY() + 1) ||
5200 (binz >= GetNbinsZ() + 1);
5201 return kFALSE;
5202 }
5203 if (iaxis == 1)
5204 return binx >= GetNbinsX() + 1;
5205 if (iaxis == 2)
5206 return biny >= GetNbinsY() + 1;
5207 if (iaxis == 3)
5208 return binz >= GetNbinsZ() + 1;
5209
5210 Error("IsBinOverflow","Invalid axis value");
5211 return kFALSE;
5212}
5213
5214////////////////////////////////////////////////////////////////////////////////
5215/// Return true if the bin is underflow.
5216/// If iaxis = 0 make OR with all axes otherwise check only for the given axis
5217
5219{
5220 Int_t binx, biny, binz;
5221 GetBinXYZ(bin, binx, biny, binz);
5222
5223 if (iaxis == 0) {
5224 if ( fDimension == 1 )
5225 return (binx <= 0);
5226 else if ( fDimension == 2 )
5227 return (binx <= 0 || biny <= 0);
5228 else if ( fDimension == 3 )
5229 return (binx <= 0 || biny <= 0 || binz <= 0);
5230 else
5231 return kFALSE;
5232 }
5233 if (iaxis == 1)
5234 return (binx <= 0);
5235 if (iaxis == 2)
5236 return (biny <= 0);
5237 if (iaxis == 3)
5238 return (binz <= 0);
5239
5240 Error("IsBinUnderflow","Invalid axis value");
5241 return kFALSE;
5242}
5243
5244////////////////////////////////////////////////////////////////////////////////
5245/// Reduce the number of bins for the axis passed in the option to the number of bins having a label.
5246/// The method will remove only the extra bins existing after the last "labeled" bin.
5247/// Note that if there are "un-labeled" bins present between "labeled" bins they will not be removed
5248
5250{
5252 TAxis *axis = nullptr;
5253 if (iaxis == 1) axis = GetXaxis();
5254 if (iaxis == 2) axis = GetYaxis();
5255 if (iaxis == 3) axis = GetZaxis();
5256 if (!axis) {
5257 Error("LabelsDeflate","Invalid axis option %s",ax);
5258 return;
5259 }
5260 if (!axis->GetLabels()) return;
5261
5262 // find bin with last labels
5263 // bin number is object ID in list of labels
5264 // therefore max bin number is number of bins of the deflated histograms
5265 TIter next(axis->GetLabels());
5266 TObject *obj;
5267 Int_t nbins = 0;
5268 while ((obj = next())) {
5269 Int_t ibin = obj->GetUniqueID();
5270 if (ibin > nbins) nbins = ibin;
5271 }
5272 if (nbins < 1) nbins = 1;
5273
5274 // Do nothing in case it was the last bin
5275 if (nbins==axis->GetNbins()) return;
5276
5277 TH1 *hold = (TH1*)IsA()->New();
5278 R__ASSERT(hold);
5279 hold->SetDirectory(nullptr);
5280 Copy(*hold);
5281
5282 Bool_t timedisp = axis->GetTimeDisplay();
5283 Double_t xmin = axis->GetXmin();
5284 Double_t xmax = axis->GetBinUpEdge(nbins);
5285 if (xmax <= xmin) xmax = xmin +nbins;
5286 axis->SetRange(0,0);
5287 axis->Set(nbins,xmin,xmax);
5288 SetBinsLength(-1); // reset the number of cells
5290 if (errors) fSumw2.Set(fNcells);
5291 axis->SetTimeDisplay(timedisp);
5292 // reset histogram content
5293 Reset("ICE");
5294
5295 //now loop on all bins and refill
5296 // NOTE that if the bins without labels have content
5297 // it will be put in the underflow/overflow.
5298 // For this reason we use AddBinContent method
5300 Int_t bin,binx,biny,binz;
5301 for (bin=0; bin < hold->fNcells; ++bin) {
5302 hold->GetBinXYZ(bin,binx,biny,binz);
5304 Double_t cu = hold->RetrieveBinContent(bin);
5306 if (errors) {
5307 fSumw2.fArray[ibin] += hold->fSumw2.fArray[bin];
5308 }
5309 }
5311 delete hold;
5312}
5313
5314////////////////////////////////////////////////////////////////////////////////
5315/// Double the number of bins for axis.
5316/// Refill histogram.
5317/// This function is called by TAxis::FindBin(const char *label)
5318
5320{
5322 TAxis *axis = nullptr;
5323 if (iaxis == 1) axis = GetXaxis();
5324 if (iaxis == 2) axis = GetYaxis();
5325 if (iaxis == 3) axis = GetZaxis();
5326 if (!axis) return;
5327
5328 TH1 *hold = (TH1*)IsA()->New();
5329 hold->SetDirectory(nullptr);
5330 Copy(*hold);
5331 hold->ResetBit(kMustCleanup);
5332
5333 Bool_t timedisp = axis->GetTimeDisplay();
5334 Int_t nbins = axis->GetNbins();
5335 Double_t xmin = axis->GetXmin();
5336 Double_t xmax = axis->GetXmax();
5337 xmax = xmin + 2*(xmax-xmin);
5338 axis->SetRange(0,0);
5339 // double the bins and recompute ncells
5340 axis->Set(2*nbins,xmin,xmax);
5341 SetBinsLength(-1);
5343 if (errors) fSumw2.Set(fNcells);
5344 axis->SetTimeDisplay(timedisp);
5345
5346 Reset("ICE"); // reset content and error
5347
5348 //now loop on all bins and refill
5350 Int_t bin,ibin,binx,biny,binz;
5351 for (ibin =0; ibin < hold->fNcells; ibin++) {
5352 // get the binx,y,z values . The x-y-z (axis) bin values will stay the same between new-old after the expanding
5353 hold->GetBinXYZ(ibin,binx,biny,binz);
5354 bin = GetBin(binx,biny,binz);
5355
5356 // underflow and overflow will be cleaned up because their meaning has been altered
5357 if (hold->IsBinUnderflow(ibin,iaxis) || hold->IsBinOverflow(ibin,iaxis)) {
5358 continue;
5359 }
5360 else {
5361 AddBinContent(bin, hold->RetrieveBinContent(ibin));
5362 if (errors) fSumw2.fArray[bin] += hold->fSumw2.fArray[ibin];
5363 }
5364 }
5366 delete hold;
5367}
5368
5369////////////////////////////////////////////////////////////////////////////////
5370/// Sort bins with labels or set option(s) to draw axis with labels
5371/// \param[in] option
5372/// - "a" sort by alphabetic order
5373/// - ">" sort by decreasing values
5374/// - "<" sort by increasing values
5375/// - "h" draw labels horizontal
5376/// - "v" draw labels vertical
5377/// - "u" draw labels up (end of label right adjusted)
5378/// - "d" draw labels down (start of label left adjusted)
5379///
5380/// In case not all bins have labels sorting will work only in the case
5381/// the first `n` consecutive bins have all labels and sorting will be performed on
5382/// those label bins.
5383///
5384/// \param[in] ax axis
5385
5387{
5389 TAxis *axis = nullptr;
5390 if (iaxis == 1)
5391 axis = GetXaxis();
5392 if (iaxis == 2)
5393 axis = GetYaxis();
5394 if (iaxis == 3)
5395 axis = GetZaxis();
5396 if (!axis)
5397 return;
5398 THashList *labels = axis->GetLabels();
5399 if (!labels) {
5400 Warning("LabelsOption", "Axis %s has no labels!",axis->GetName());
5401 return;
5402 }
5403 TString opt = option;
5404 opt.ToLower();
5405 Int_t iopt = -1;
5406 if (opt.Contains("h")) {
5411 iopt = 0;
5412 }
5413 if (opt.Contains("v")) {
5418 iopt = 1;
5419 }
5420 if (opt.Contains("u")) {
5421 axis->SetBit(TAxis::kLabelsUp);
5425 iopt = 2;
5426 }
5427 if (opt.Contains("d")) {
5432 iopt = 3;
5433 }
5434 Int_t sort = -1;
5435 if (opt.Contains("a"))
5436 sort = 0;
5437 if (opt.Contains(">"))
5438 sort = 1;
5439 if (opt.Contains("<"))
5440 sort = 2;
5441 if (sort < 0) {
5442 if (iopt < 0)
5443 Error("LabelsOption", "%s is an invalid label placement option!",opt.Data());
5444 return;
5445 }
5446
5447 // Code works only if first n bins have labels if we uncomment following line
5448 // but we don't want to support this special case
5449 // Int_t n = TMath::Min(axis->GetNbins(), labels->GetSize());
5450
5451 // support only cases where each bin has a labels (should be when axis is alphanumeric)
5452 Int_t n = labels->GetSize();
5453 if (n != axis->GetNbins()) {
5454 // check if labels are all consecutive and starts from the first bin
5455 // in that case the current code will work fine
5456 Int_t firstLabelBin = axis->GetNbins()+1;
5457 Int_t lastLabelBin = -1;
5458 for (Int_t i = 0; i < n; ++i) {
5459 Int_t bin = labels->At(i)->GetUniqueID();
5460 if (bin < firstLabelBin) firstLabelBin = bin;
5461 if (bin > lastLabelBin) lastLabelBin = bin;
5462 }
5463 if (firstLabelBin != 1 || lastLabelBin-firstLabelBin +1 != n) {
5464 Error("LabelsOption", "%s of Histogram %s contains bins without labels. Sorting will not work correctly - return",
5465 axis->GetName(), GetName());
5466 return;
5467 }
5468 // case where label bins are consecutive starting from first bin will work
5469 // calling before a TH1::LabelsDeflate() will avoid this error message
5470 Warning("LabelsOption", "axis %s of Histogram %s has extra following bins without labels. Sorting will work only for first label bins",
5471 axis->GetName(), GetName());
5472 }
5473 std::vector<Int_t> a(n);
5474 std::vector<Int_t> b(n);
5475
5476
5477 Int_t i, j, k;
5478 std::vector<Double_t> cont;
5479 std::vector<Double_t> errors2;
5480 THashList *labold = new THashList(labels->GetSize(), 1);
5481 TIter nextold(labels);
5482 TObject *obj = nullptr;
5483 labold->AddAll(labels);
5484 labels->Clear();
5485
5486 // delete buffer if it is there since bins will be reordered.
5487 if (fBuffer)
5488 BufferEmpty(1);
5489
5490 if (sort > 0) {
5491 //---sort by values of bins
5492 if (GetDimension() == 1) {
5493 cont.resize(n);
5494 if (fSumw2.fN)
5495 errors2.resize(n);
5496 for (i = 0; i < n; i++) {
5497 cont[i] = RetrieveBinContent(i + 1);
5498 if (!errors2.empty())
5499 errors2[i] = GetBinErrorSqUnchecked(i + 1);
5500 b[i] = labold->At(i)->GetUniqueID(); // this is the bin corresponding to the label
5501 a[i] = i;
5502 }
5503 if (sort == 1)
5504 TMath::Sort(n, cont.data(), a.data(), kTRUE); // sort by decreasing values
5505 else
5506 TMath::Sort(n, cont.data(), a.data(), kFALSE); // sort by increasing values
5507 for (i = 0; i < n; i++) {
5508 // use UpdateBinCOntent to not screw up histogram entries
5509 UpdateBinContent(i + 1, cont[b[a[i]] - 1]); // b[a[i]] returns bin number. .we need to subtract 1
5510 if (gDebug)
5511 Info("LabelsOption","setting bin %d value %f from bin %d label %s at pos %d ",
5512 i+1,cont[b[a[i]] - 1],b[a[i]],labold->At(a[i])->GetName(),a[i]);
5513 if (!errors2.empty())
5514 fSumw2.fArray[i + 1] = errors2[b[a[i]] - 1];
5515 }
5516 for (i = 0; i < n; i++) {
5517 obj = labold->At(a[i]);
5518 labels->Add(obj);
5519 obj->SetUniqueID(i + 1);
5520 }
5521 } else if (GetDimension() == 2) {
5522 std::vector<Double_t> pcont(n + 2);
5523 Int_t nx = fXaxis.GetNbins() + 2;
5524 Int_t ny = fYaxis.GetNbins() + 2;
5525 cont.resize((nx + 2) * (ny + 2));
5526 if (fSumw2.fN)
5527 errors2.resize((nx + 2) * (ny + 2));
5528 for (i = 0; i < nx; i++) {
5529 for (j = 0; j < ny; j++) {
5530 Int_t bin = GetBin(i,j);
5531 cont[i + nx * j] = RetrieveBinContent(bin);
5532 if (!errors2.empty())
5533 errors2[i + nx * j] = GetBinErrorSqUnchecked(bin);
5534 if (axis == GetXaxis())
5535 k = i - 1;
5536 else
5537 k = j - 1;
5538 if (k >= 0 && k < n) { // we consider underflow/overflows in y for ordering the bins
5539 pcont[k] += cont[i + nx * j];
5540 a[k] = k;
5541 }
5542 }
5543 }
5544 if (sort == 1)
5545 TMath::Sort(n, pcont.data(), a.data(), kTRUE); // sort by decreasing values
5546 else
5547 TMath::Sort(n, pcont.data(), a.data(), kFALSE); // sort by increasing values
5548 for (i = 0; i < n; i++) {
5549 // iterate on old label list to find corresponding bin match
5550 TIter next(labold);
5551 UInt_t bin = a[i] + 1;
5552 while ((obj = next())) {
5553 if (obj->GetUniqueID() == (UInt_t)bin)
5554 break;
5555 else
5556 obj = nullptr;
5557 }
5558 if (!obj) {
5559 // this should not really happen
5560 R__ASSERT("LabelsOption - No corresponding bin found when ordering labels");
5561 return;
5562 }
5563
5564 labels->Add(obj);
5565 if (gDebug)
5566 std::cout << " set label " << obj->GetName() << " to bin " << i + 1 << " from order " << a[i] << " bin "
5567 << b[a[i]] << "content " << pcont[a[i]] << std::endl;
5568 }
5569 // need to set here new ordered labels - otherwise loop before does not work since labold and labels list
5570 // contain same objects
5571 for (i = 0; i < n; i++) {
5572 labels->At(i)->SetUniqueID(i + 1);
5573 }
5574 // set now the bin contents
5575 if (axis == GetXaxis()) {
5576 for (i = 0; i < n; i++) {
5577 Int_t ix = a[i] + 1;
5578 for (j = 0; j < ny; j++) {
5579 Int_t bin = GetBin(i + 1, j);
5580 UpdateBinContent(bin, cont[ix + nx * j]);
5581 if (!errors2.empty())
5582 fSumw2.fArray[bin] = errors2[ix + nx * j];
5583 }
5584 }
5585 } else {
5586 // using y axis
5587 for (i = 0; i < nx; i++) {
5588 for (j = 0; j < n; j++) {
5589 Int_t iy = a[j] + 1;
5590 Int_t bin = GetBin(i, j + 1);
5591 UpdateBinContent(bin, cont[i + nx * iy]);
5592 if (!errors2.empty())
5593 fSumw2.fArray[bin] = errors2[i + nx * iy];
5594 }
5595 }
5596 }
5597 } else {
5598 // sorting histograms: 3D case
5599 std::vector<Double_t> pcont(n + 2);
5600 Int_t nx = fXaxis.GetNbins() + 2;
5601 Int_t ny = fYaxis.GetNbins() + 2;
5602 Int_t nz = fZaxis.GetNbins() + 2;
5603 Int_t l = 0;
5604 cont.resize((nx + 2) * (ny + 2) * (nz + 2));
5605 if (fSumw2.fN)
5606 errors2.resize((nx + 2) * (ny + 2) * (nz + 2));
5607 for (i = 0; i < nx; i++) {
5608 for (j = 0; j < ny; j++) {
5609 for (k = 0; k < nz; k++) {
5610 Int_t bin = GetBin(i,j,k);
5612 if (axis == GetXaxis())
5613 l = i - 1;
5614 else if (axis == GetYaxis())
5615 l = j - 1;
5616 else
5617 l = k - 1;
5618 if (l >= 0 && l < n) { // we consider underflow/overflows in y for ordering the bins
5619 pcont[l] += c;
5620 a[l] = l;
5621 }
5622 cont[i + nx * (j + ny * k)] = c;
5623 if (!errors2.empty())
5624 errors2[i + nx * (j + ny * k)] = GetBinErrorSqUnchecked(bin);
5625 }
5626 }
5627 }
5628 if (sort == 1)
5629 TMath::Sort(n, pcont.data(), a.data(), kTRUE); // sort by decreasing values
5630 else
5631 TMath::Sort(n, pcont.data(), a.data(), kFALSE); // sort by increasing values
5632 for (i = 0; i < n; i++) {
5633 // iterate on the old label list to find corresponding bin match
5634 TIter next(labold);
5635 UInt_t bin = a[i] + 1;
5636 obj = nullptr;
5637 while ((obj = next())) {
5638 if (obj->GetUniqueID() == (UInt_t)bin) {
5639 break;
5640 }
5641 else
5642 obj = nullptr;
5643 }
5644 if (!obj) {
5645 R__ASSERT("LabelsOption - No corresponding bin found when ordering labels");
5646 return;
5647 }
5648 labels->Add(obj);
5649 if (gDebug)
5650 std::cout << " set label " << obj->GetName() << " to bin " << i + 1 << " from bin " << a[i] << "content "
5651 << pcont[a[i]] << std::endl;
5652 }
5653
5654 // need to set here new ordered labels - otherwise loop before does not work since labold and llabels list
5655 // contain same objects
5656 for (i = 0; i < n; i++) {
5657 labels->At(i)->SetUniqueID(i + 1);
5658 }
5659 // set now the bin contents
5660 if (axis == GetXaxis()) {
5661 for (i = 0; i < n; i++) {
5662 Int_t ix = a[i] + 1;
5663 for (j = 0; j < ny; j++) {
5664 for (k = 0; k < nz; k++) {
5665 Int_t bin = GetBin(i + 1, j, k);
5666 UpdateBinContent(bin, cont[ix + nx * (j + ny * k)]);
5667 if (!errors2.empty())
5668 fSumw2.fArray[bin] = errors2[ix + nx * (j + ny * k)];
5669 }
5670 }
5671 }
5672 } else if (axis == GetYaxis()) {
5673 // using y axis
5674 for (i = 0; i < nx; i++) {
5675 for (j = 0; j < n; j++) {
5676 Int_t iy = a[j] + 1;
5677 for (k = 0; k < nz; k++) {
5678 Int_t bin = GetBin(i, j + 1, k);
5679 UpdateBinContent(bin, cont[i + nx * (iy + ny * k)]);
5680 if (!errors2.empty())
5681 fSumw2.fArray[bin] = errors2[i + nx * (iy + ny * k)];
5682 }
5683 }
5684 }
5685 } else {
5686 // using z axis
5687 for (i = 0; i < nx; i++) {
5688 for (j = 0; j < ny; j++) {
5689 for (k = 0; k < n; k++) {
5690 Int_t iz = a[k] + 1;
5691 Int_t bin = GetBin(i, j , k +1);
5692 UpdateBinContent(bin, cont[i + nx * (j + ny * iz)]);
5693 if (!errors2.empty())
5694 fSumw2.fArray[bin] = errors2[i + nx * (j + ny * iz)];
5695 }
5696 }
5697 }
5698 }
5699 }
5700 } else {
5701 //---alphabetic sort
5702 // sort labels using vector of strings and TMath::Sort
5703 // I need to array because labels order in list is not necessary that of the bins
5704 std::vector<std::string> vecLabels(n);
5705 for (i = 0; i < n; i++) {
5706 vecLabels[i] = labold->At(i)->GetName();
5707 b[i] = labold->At(i)->GetUniqueID(); // this is the bin corresponding to the label
5708 a[i] = i;
5709 }
5710 // sort in ascending order for strings
5711 TMath::Sort(n, vecLabels.data(), a.data(), kFALSE);
5712 // set the new labels
5713 for (i = 0; i < n; i++) {
5714 TObject *labelObj = labold->At(a[i]);
5715 labels->Add(labold->At(a[i]));
5716 // set the corresponding bin. NB bin starts from 1
5717 labelObj->SetUniqueID(i + 1);
5718 if (gDebug)
5719 std::cout << "bin " << i + 1 << " setting new labels for axis " << labold->At(a[i])->GetName() << " from "
5720 << b[a[i]] << std::endl;
5721 }
5722
5723 if (GetDimension() == 1) {
5724 cont.resize(n + 2);
5725 if (fSumw2.fN)
5726 errors2.resize(n + 2);
5727 for (i = 0; i < n; i++) {
5728 cont[i] = RetrieveBinContent(b[a[i]]);
5729 if (!errors2.empty())
5731 }
5732 for (i = 0; i < n; i++) {
5733 UpdateBinContent(i + 1, cont[i]);
5734 if (!errors2.empty())
5735 fSumw2.fArray[i+1] = errors2[i];
5736 }
5737 } else if (GetDimension() == 2) {
5738 Int_t nx = fXaxis.GetNbins() + 2;
5739 Int_t ny = fYaxis.GetNbins() + 2;
5740 cont.resize(nx * ny);
5741 if (fSumw2.fN)
5742 errors2.resize(nx * ny);
5743 // copy old bin contents and then set to new ordered bins
5744 // N.B. bin in histograms starts from 1, but in y we consider under/overflows
5745 for (i = 0; i < nx; i++) {
5746 for (j = 0; j < ny; j++) { // ny is nbins+2
5747 Int_t bin = GetBin(i, j);
5748 cont[i + nx * j] = RetrieveBinContent(bin);
5749 if (!errors2.empty())
5750 errors2[i + nx * j] = GetBinErrorSqUnchecked(bin);
5751 }
5752 }
5753 if (axis == GetXaxis()) {
5754 for (i = 0; i < n; i++) {
5755 for (j = 0; j < ny; j++) {
5756 Int_t bin = GetBin(i + 1 , j);
5757 UpdateBinContent(bin, cont[b[a[i]] + nx * j]);
5758 if (!errors2.empty())
5759 fSumw2.fArray[bin] = errors2[b[a[i]] + nx * j];
5760 }
5761 }
5762 } else {
5763 for (i = 0; i < nx; i++) {
5764 for (j = 0; j < n; j++) {
5765 Int_t bin = GetBin(i, j + 1);
5766 UpdateBinContent(bin, cont[i + nx * b[a[j]]]);
5767 if (!errors2.empty())
5768 fSumw2.fArray[bin] = errors2[i + nx * b[a[j]]];
5769 }
5770 }
5771 }
5772 } else {
5773 // case of 3D (needs to be tested)
5774 Int_t nx = fXaxis.GetNbins() + 2;
5775 Int_t ny = fYaxis.GetNbins() + 2;
5776 Int_t nz = fZaxis.GetNbins() + 2;
5777 cont.resize(nx * ny * nz);
5778 if (fSumw2.fN)
5779 errors2.resize(nx * ny * nz);
5780 for (i = 0; i < nx; i++) {
5781 for (j = 0; j < ny; j++) {
5782 for (k = 0; k < nz; k++) {
5783 Int_t bin = GetBin(i, j, k);
5784 cont[i + nx * (j + ny * k)] = RetrieveBinContent(bin);
5785 if (!errors2.empty())
5786 errors2[i + nx * (j + ny * k)] = GetBinErrorSqUnchecked(bin);
5787 }
5788 }
5789 }
5790 if (axis == GetXaxis()) {
5791 // labels on x axis
5792 for (i = 0; i < n; i++) { // for x we loop only on bins with the labels
5793 for (j = 0; j < ny; j++) {
5794 for (k = 0; k < nz; k++) {
5795 Int_t bin = GetBin(i + 1, j, k);
5796 UpdateBinContent(bin, cont[b[a[i]] + nx * (j + ny * k)]);
5797 if (!errors2.empty())
5798 fSumw2.fArray[bin] = errors2[b[a[i]] + nx * (j + ny * k)];
5799 }
5800 }
5801 }
5802 } else if (axis == GetYaxis()) {
5803 // labels on y axis
5804 for (i = 0; i < nx; i++) {
5805 for (j = 0; j < n; j++) {
5806 for (k = 0; k < nz; k++) {
5807 Int_t bin = GetBin(i, j+1, k);
5808 UpdateBinContent(bin, cont[i + nx * (b[a[j]] + ny * k)]);
5809 if (!errors2.empty())
5810 fSumw2.fArray[bin] = errors2[i + nx * (b[a[j]] + ny * k)];
5811 }
5812 }
5813 }
5814 } else {
5815 // labels on z axis
5816 for (i = 0; i < nx; i++) {
5817 for (j = 0; j < ny; j++) {
5818 for (k = 0; k < n; k++) {
5819 Int_t bin = GetBin(i, j, k+1);
5820 UpdateBinContent(bin, cont[i + nx * (j + ny * b[a[k]])]);
5821 if (!errors2.empty())
5822 fSumw2.fArray[bin] = errors2[i + nx * (j + ny * b[a[k]])];
5823 }
5824 }
5825 }
5826 }
5827 }
5828 }
5829 // need to set to zero the statistics if axis has been sorted
5830 // see for example TH3::PutStats for definition of s vector
5831 bool labelsAreSorted = kFALSE;
5832 for (i = 0; i < n; ++i) {
5833 if (a[i] != i) {
5835 break;
5836 }
5837 }
5838 if (labelsAreSorted) {
5839 double s[TH1::kNstat];
5840 GetStats(s);
5841 if (iaxis == 1) {
5842 s[2] = 0; // fTsumwx
5843 s[3] = 0; // fTsumwx2
5844 s[6] = 0; // fTsumwxy
5845 s[9] = 0; // fTsumwxz
5846 } else if (iaxis == 2) {
5847 s[4] = 0; // fTsumwy
5848 s[5] = 0; // fTsumwy2
5849 s[6] = 0; // fTsumwxy
5850 s[10] = 0; // fTsumwyz
5851 } else if (iaxis == 3) {
5852 s[7] = 0; // fTsumwz
5853 s[8] = 0; // fTsumwz2
5854 s[9] = 0; // fTsumwxz
5855 s[10] = 0; // fTsumwyz
5856 }
5857 PutStats(s);
5858 }
5859 delete labold;
5860}
5861
5862////////////////////////////////////////////////////////////////////////////////
5863/// Test if two double are almost equal.
5864
5865static inline Bool_t AlmostEqual(Double_t a, Double_t b, Double_t epsilon = 0.00000001)
5866{
5867 return TMath::Abs(a - b) < epsilon;
5868}
5869
5870////////////////////////////////////////////////////////////////////////////////
5871/// Test if a double is almost an integer.
5872
5873static inline Bool_t AlmostInteger(Double_t a, Double_t epsilon = 0.00000001)
5874{
5875 return AlmostEqual(a - TMath::Floor(a), 0, epsilon) ||
5876 AlmostEqual(a - TMath::Floor(a), 1, epsilon);
5877}
5878
5879////////////////////////////////////////////////////////////////////////////////
5880/// Test if the binning is equidistant.
5881
5882static inline bool IsEquidistantBinning(const TAxis& axis)
5883{
5884 // check if axis bin are equals
5885 if (!axis.GetXbins()->fN) return true; //
5886 // not able to check if there is only one axis entry
5887 bool isEquidistant = true;
5888 const Double_t firstBinWidth = axis.GetBinWidth(1);
5889 for (int i = 1; i < axis.GetNbins(); ++i) {
5890 const Double_t binWidth = axis.GetBinWidth(i);
5891 const bool match = TMath::AreEqualRel(firstBinWidth, binWidth, 1.E-10);
5892 isEquidistant &= match;
5893 if (!match)
5894 break;
5895 }
5896 return isEquidistant;
5897}
5898
5899////////////////////////////////////////////////////////////////////////////////
5900/// Same limits and bins.
5901
5903 return axis1.GetNbins() == axis2.GetNbins() &&
5904 TMath::AreEqualAbs(axis1.GetXmin(), axis2.GetXmin(), axis1.GetBinWidth(axis1.GetNbins()) * 1.E-10) &&
5905 TMath::AreEqualAbs(axis1.GetXmax(), axis2.GetXmax(), axis1.GetBinWidth(axis1.GetNbins()) * 1.E-10);
5906}
5907
5908////////////////////////////////////////////////////////////////////////////////
5909/// Finds new limits for the axis for the Merge function.
5910/// returns false if the limits are incompatible
5911
5913{
5915 return kTRUE;
5916
5918 return kFALSE; // not equidistant user binning not supported
5919
5920 Double_t width1 = destAxis.GetBinWidth(0);
5921 Double_t width2 = anAxis.GetBinWidth(0);
5922 if (width1 == 0 || width2 == 0)
5923 return kFALSE; // no binning not supported
5924
5925 Double_t xmin = TMath::Min(destAxis.GetXmin(), anAxis.GetXmin());
5926 Double_t xmax = TMath::Max(destAxis.GetXmax(), anAxis.GetXmax());
5928
5929 // check the bin size
5931 return kFALSE;
5932
5933 // std::cout << "Find new limit using given axis " << anAxis.GetXmin() << " , " << anAxis.GetXmax() << " bin width " << width2 << std::endl;
5934 // std::cout << " and destination axis " << destAxis.GetXmin() << " , " << destAxis.GetXmax() << " bin width " << width1 << std::endl;
5935
5936
5937 // check the limits
5938 Double_t delta;
5939 delta = (destAxis.GetXmin() - xmin)/width1;
5940 if (!AlmostInteger(delta))
5941 xmin -= (TMath::Ceil(delta) - delta)*width1;
5942
5943 delta = (anAxis.GetXmin() - xmin)/width2;
5944 if (!AlmostInteger(delta))
5945 xmin -= (TMath::Ceil(delta) - delta)*width2;
5946
5947
5948 delta = (destAxis.GetXmin() - xmin)/width1;
5949 if (!AlmostInteger(delta))
5950 return kFALSE;
5951
5952
5953 delta = (xmax - destAxis.GetXmax())/width1;
5954 if (!AlmostInteger(delta))
5955 xmax += (TMath::Ceil(delta) - delta)*width1;
5956
5957
5958 delta = (xmax - anAxis.GetXmax())/width2;
5959 if (!AlmostInteger(delta))
5960 xmax += (TMath::Ceil(delta) - delta)*width2;
5961
5962
5963 delta = (xmax - destAxis.GetXmax())/width1;
5964 if (!AlmostInteger(delta))
5965 return kFALSE;
5966#ifdef DEBUG
5967 if (!AlmostInteger((xmax - xmin) / width)) { // unnecessary check
5968 printf("TH1::RecomputeAxisLimits - Impossible\n");
5969 return kFALSE;
5970 }
5971#endif
5972
5973
5975
5976 //std::cout << "New re-computed axis : [ " << xmin << " , " << xmax << " ] width = " << width << " nbins " << destAxis.GetNbins() << std::endl;
5977
5978 return kTRUE;
5979}
5980
5981////////////////////////////////////////////////////////////////////////////////
5982/// Add all histograms in the collection to this histogram.
5983/// This function computes the min/max for the x axis,
5984/// compute a new number of bins, if necessary,
5985/// add bin contents, errors and statistics.
5986/// If all histograms have bin labels, bins with identical labels
5987/// will be merged, no matter what their order is.
5988/// If overflows are present and limits are different the function will fail.
5989/// The function returns the total number of entries in the result histogram
5990/// if the merge is successful, -1 otherwise.
5991///
5992/// Possible option:
5993/// -NOL : the merger will ignore the labels and merge the histograms bin by bin using bin center values to match bins
5994/// -NOCHECK: the histogram will not perform a check for duplicate labels in case of axes with labels. The check
5995/// (enabled by default) slows down the merging
5996///
5997/// IMPORTANT remark. The axis x may have different number
5998/// of bins and different limits, BUT the largest bin width must be
5999/// a multiple of the smallest bin width and the upper limit must also
6000/// be a multiple of the bin width.
6001/// Example:
6002///
6003/// ~~~ {.cpp}
6004/// void atest() {
6005/// TH1F *h1 = new TH1F("h1","h1",110,-110,0);
6006/// TH1F *h2 = new TH1F("h2","h2",220,0,110);
6007/// TH1F *h3 = new TH1F("h3","h3",330,-55,55);
6008/// TRandom r;
6009/// for (Int_t i=0;i<10000;i++) {
6010/// h1->Fill(r.Gaus(-55,10));
6011/// h2->Fill(r.Gaus(55,10));
6012/// h3->Fill(r.Gaus(0,10));
6013/// }
6014///
6015/// TList *list = new TList;
6016/// list->Add(h1);
6017/// list->Add(h2);
6018/// list->Add(h3);
6019/// TH1F *h = (TH1F*)h1->Clone("h");
6020/// h->Reset();
6021/// h->Merge(list);
6022/// h->Draw();
6023/// }
6024/// ~~~
6025
6027{
6028 if (!li) return 0;
6029 if (li->IsEmpty()) return (Long64_t) GetEntries();
6030
6031 // use TH1Merger class
6032 TH1Merger merger(*this,*li,opt);
6033 Bool_t ret = merger();
6034
6035 return (ret) ? GetEntries() : -1;
6036}
6037
6038
6039////////////////////////////////////////////////////////////////////////////////
6040/// Performs the operation:
6041///
6042/// `this = this*c1*f1`
6043///
6044/// If errors are defined (see TH1::Sumw2), errors are also recalculated.
6045///
6046/// Only bins inside the function range are recomputed.
6047/// IMPORTANT NOTE: If you intend to use the errors of this histogram later
6048/// you should call Sumw2 before making this operation.
6049/// This is particularly important if you fit the histogram after TH1::Multiply
6050///
6051/// The function return kFALSE if the Multiply operation failed
6052
6054{
6055 if (!f1) {
6056 Error("Multiply","Attempt to multiply by a non-existing function");
6057 return kFALSE;
6058 }
6059
6060 // delete buffer if it is there since it will become invalid
6061 if (fBuffer) BufferEmpty(1);
6062
6063 Int_t nx = GetNbinsX() + 2; // normal bins + uf / of (cells)
6064 Int_t ny = GetNbinsY() + 2;
6065 Int_t nz = GetNbinsZ() + 2;
6066 if (fDimension < 2) ny = 1;
6067 if (fDimension < 3) nz = 1;
6068
6069 // reset min-maximum
6070 SetMinimum();
6071 SetMaximum();
6072
6073 // - Loop on bins (including underflows/overflows)
6074 Double_t xx[3];
6075 Double_t *params = nullptr;
6076 f1->InitArgs(xx,params);
6077
6078 for (Int_t binz = 0; binz < nz; ++binz) {
6079 xx[2] = fZaxis.GetBinCenter(binz);
6080 for (Int_t biny = 0; biny < ny; ++biny) {
6081 xx[1] = fYaxis.GetBinCenter(biny);
6082 for (Int_t binx = 0; binx < nx; ++binx) {
6083 xx[0] = fXaxis.GetBinCenter(binx);
6084 if (!f1->IsInside(xx)) continue;
6086 Int_t bin = binx + nx * (biny + ny *binz);
6087 Double_t cu = c1*f1->EvalPar(xx);
6088 if (TF1::RejectedPoint()) continue;
6090 if (fSumw2.fN) {
6091 fSumw2.fArray[bin] = cu * cu * GetBinErrorSqUnchecked(bin);
6092 }
6093 }
6094 }
6095 }
6096 ResetStats();
6097 return kTRUE;
6098}
6099
6100////////////////////////////////////////////////////////////////////////////////
6101/// Multiply this histogram by h1.
6102///
6103/// `this = this*h1`
6104///
6105/// If errors of this are available (TH1::Sumw2), errors are recalculated.
6106/// Note that if h1 has Sumw2 set, Sumw2 is automatically called for this
6107/// if not already set.
6108///
6109/// IMPORTANT NOTE: If you intend to use the errors of this histogram later
6110/// you should call Sumw2 before making this operation.
6111/// This is particularly important if you fit the histogram after TH1::Multiply
6112///
6113/// The function return kFALSE if the Multiply operation failed
6114
6115Bool_t TH1::Multiply(const TH1 *h1)
6116{
6117 if (!h1) {
6118 Error("Multiply","Attempt to multiply by a non-existing histogram");
6119 return kFALSE;
6120 }
6121
6122 // delete buffer if it is there since it will become invalid
6123 if (fBuffer) BufferEmpty(1);
6124
6125 if (LoggedInconsistency("Multiply", this, h1) >= kDifferentNumberOfBins) {
6126 return false;
6127 }
6128
6129 // Create Sumw2 if h1 has Sumw2 set
6130 if (fSumw2.fN == 0 && h1->GetSumw2N() != 0) Sumw2();
6131
6132 // - Reset min- maximum
6133 SetMinimum();
6134 SetMaximum();
6135
6136 // - Loop on bins (including underflows/overflows)
6137 for (Int_t i = 0; i < fNcells; ++i) {
6140 UpdateBinContent(i, c0 * c1);
6141 if (fSumw2.fN) {
6143 }
6144 }
6145 ResetStats();
6146 return kTRUE;
6147}
6148
6149////////////////////////////////////////////////////////////////////////////////
6150/// Replace contents of this histogram by multiplication of h1 by h2.
6151///
6152/// `this = (c1*h1)*(c2*h2)`
6153///
6154/// If errors of this are available (TH1::Sumw2), errors are recalculated.
6155/// Note that if h1 or h2 have Sumw2 set, Sumw2 is automatically called for this
6156/// if not already set.
6157///
6158/// IMPORTANT NOTE: If you intend to use the errors of this histogram later
6159/// you should call Sumw2 before making this operation.
6160/// This is particularly important if you fit the histogram after TH1::Multiply
6161///
6162/// The function return kFALSE if the Multiply operation failed
6163
6165{
6166 TString opt = option;
6167 opt.ToLower();
6168 // Bool_t binomial = kFALSE;
6169 // if (opt.Contains("b")) binomial = kTRUE;
6170 if (!h1 || !h2) {
6171 Error("Multiply","Attempt to multiply by a non-existing histogram");
6172 return kFALSE;
6173 }
6174
6175 // delete buffer if it is there since it will become invalid
6176 if (fBuffer) BufferEmpty(1);
6177
6178 if (LoggedInconsistency("Multiply", this, h1) >= kDifferentNumberOfBins ||
6179 LoggedInconsistency("Multiply", h1, h2) >= kDifferentNumberOfBins) {
6180 return false;
6181 }
6182
6183 // Create Sumw2 if h1 or h2 have Sumw2 set
6184 if (fSumw2.fN == 0 && (h1->GetSumw2N() != 0 || h2->GetSumw2N() != 0)) Sumw2();
6185
6186 // - Reset min - maximum
6187 SetMinimum();
6188 SetMaximum();
6189
6190 // - Loop on bins (including underflows/overflows)
6191 Double_t c1sq = c1 * c1; Double_t c2sq = c2 * c2;
6192 for (Int_t i = 0; i < fNcells; ++i) {
6194 Double_t b2 = h2->RetrieveBinContent(i);
6195 UpdateBinContent(i, c1 * b1 * c2 * b2);
6196 if (fSumw2.fN) {
6197 fSumw2.fArray[i] = c1sq * c2sq * (h1->GetBinErrorSqUnchecked(i) * b2 * b2 + h2->GetBinErrorSqUnchecked(i) * b1 * b1);
6198 }
6199 }
6200 ResetStats();
6201 return kTRUE;
6202}
6203
6204////////////////////////////////////////////////////////////////////////////////
6205/// @brief Normalize a histogram to its integral or to its maximum.
6206/// @note Works for TH1, TH2, TH3, ...
6207/// @param option: normalization strategy ("", "max" or "sum")
6208/// - "": Scale to `1/(sum*bin_width)`.
6209/// - max: Scale to `1/GetMaximum()`
6210/// - sum: Scale to `1/sum`.
6211///
6212/// In case the norm is zero, it raises an error.
6213/// @sa https://root-forum.cern.ch/t/different-ways-of-normalizing-histograms/15582/
6214
6216{
6217 TString opt = option;
6218 opt.ToLower();
6219 if (!opt.IsNull() && (opt != "max") && (opt != "sum")) {
6220 Error("Normalize", "Unrecognized option %s", option);
6221 return;
6222 }
6223
6224 const Double_t norm = (opt == "max") ? GetMaximum() : Integral(opt.IsNull() ? "width" : "");
6225
6226 if (norm == 0) {
6227 Error("Normalize", "Attempt to normalize histogram with zero integral");
6228 } else {
6229 Scale(1.0 / norm, "");
6230 // An alternative could have been to call Integral("") and Scale(1/norm, "width"), but this
6231 // will lead to a different value of GetEntries.
6232 // Instead, doing simultaneously Integral("width") and Scale(1/norm, "width") leads to an error since you are
6233 // dividing twice by bin width.
6234 }
6235}
6236
6237////////////////////////////////////////////////////////////////////////////////
6238/// Control routine to paint any kind of histograms.
6239///
6240/// This function is automatically called by TCanvas::Update.
6241/// (see TH1::Draw for the list of options)
6242
6244{
6246
6247 if (fPainter) {
6248 if (option && strlen(option) > 0)
6250 else
6252 }
6253}
6254
6255////////////////////////////////////////////////////////////////////////////////
6256/// Rebin this histogram
6257///
6258/// #### case 1 xbins=0
6259///
6260/// If newname is blank (default), the current histogram is modified and
6261/// a pointer to it is returned.
6262///
6263/// If newname is not blank, the current histogram is not modified, and a
6264/// new histogram is returned which is a Clone of the current histogram
6265/// with its name set to newname.
6266///
6267/// The parameter ngroup indicates how many bins of this have to be merged
6268/// into one bin of the result.
6269///
6270/// If the original histogram has errors stored (via Sumw2), the resulting
6271/// histograms has new errors correctly calculated.
6272///
6273/// examples: if h1 is an existing TH1F histogram with 100 bins
6274///
6275/// ~~~ {.cpp}
6276/// h1->Rebin(); //merges two bins in one in h1: previous contents of h1 are lost
6277/// h1->Rebin(5); //merges five bins in one in h1
6278/// TH1F *hnew = dynamic_cast<TH1F*>(h1->Rebin(5,"hnew")); // creates a new histogram hnew
6279/// // merging 5 bins of h1 in one bin
6280/// ~~~
6281///
6282/// NOTE: If ngroup is not an exact divider of the number of bins,
6283/// the top limit of the rebinned histogram is reduced
6284/// to the upper edge of the last bin that can make a complete
6285/// group. The remaining bins are added to the overflow bin.
6286/// Statistics will be recomputed from the new bin contents.
6287///
6288/// #### case 2 xbins!=0
6289///
6290/// A new histogram is created (you should specify newname).
6291/// The parameter ngroup is the number of variable size bins in the created histogram.
6292/// The array xbins must contain ngroup+1 elements that represent the low-edges
6293/// of the bins.
6294/// If the original histogram has errors stored (via Sumw2), the resulting
6295/// histograms has new errors correctly calculated.
6296///
6297/// NOTE: The bin edges specified in xbins should correspond to bin edges
6298/// in the original histogram. If a bin edge in the new histogram is
6299/// in the middle of a bin in the original histogram, all entries in
6300/// the split bin in the original histogram will be transfered to the
6301/// lower of the two possible bins in the new histogram. This is
6302/// probably not what you want. A warning message is emitted in this
6303/// case
6304///
6305/// examples: if h1 is an existing TH1F histogram with 100 bins
6306///
6307/// ~~~ {.cpp}
6308/// Double_t xbins[25] = {...} array of low-edges (xbins[25] is the upper edge of last bin
6309/// h1->Rebin(24,"hnew",xbins); //creates a new variable bin size histogram hnew
6310/// ~~~
6311
6312TH1 *TH1::Rebin(Int_t ngroup, const char*newname, const Double_t *xbins)
6313{
6314 Int_t nbins = fXaxis.GetNbins();
6317 if ((ngroup <= 0) || (ngroup > nbins)) {
6318 Error("Rebin", "Illegal value of ngroup=%d",ngroup);
6319 return nullptr;
6320 }
6321
6322 if (fDimension > 1 || InheritsFrom(TProfile::Class())) {
6323 Error("Rebin", "Operation valid on 1-D histograms only");
6324 return nullptr;
6325 }
6326 if (!newname && xbins) {
6327 Error("Rebin","if xbins is specified, newname must be given");
6328 return nullptr;
6329 }
6330
6331 Int_t newbins = nbins/ngroup;
6332 if (!xbins) {
6333 Int_t nbg = nbins/ngroup;
6334 if (nbg*ngroup != nbins) {
6335 Warning("Rebin", "ngroup=%d is not an exact divider of nbins=%d.",ngroup,nbins);
6336 }
6337 }
6338 else {
6339 // in the case that xbins is given (rebinning in variable bins), ngroup is
6340 // the new number of bins and number of grouped bins is not constant.
6341 // when looping for setting the contents for the new histogram we
6342 // need to loop on all bins of original histogram. Then set ngroup=nbins
6343 newbins = ngroup;
6344 ngroup = nbins;
6345 }
6346
6347 // Save old bin contents into a new array
6348 Double_t entries = fEntries;
6349 Double_t *oldBins = new Double_t[nbins+2];
6350 Int_t bin, i;
6351 for (bin=0;bin<nbins+2;bin++) oldBins[bin] = RetrieveBinContent(bin);
6352 Double_t *oldErrors = nullptr;
6353 if (fSumw2.fN != 0) {
6354 oldErrors = new Double_t[nbins+2];
6355 for (bin=0;bin<nbins+2;bin++) oldErrors[bin] = GetBinError(bin);
6356 }
6357 // rebin will not include underflow/overflow if new axis range is larger than old axis range
6358 if (xbins) {
6359 if (xbins[0] < fXaxis.GetXmin() && oldBins[0] != 0 )
6360 Warning("Rebin","underflow entries will not be used when rebinning");
6361 if (xbins[newbins] > fXaxis.GetXmax() && oldBins[nbins+1] != 0 )
6362 Warning("Rebin","overflow entries will not be used when rebinning");
6363 }
6364
6365
6366 // create a clone of the old histogram if newname is specified
6367 TH1 *hnew = this;
6368 if ((newname && strlen(newname) > 0) || xbins) {
6369 hnew = (TH1*)Clone(newname);
6370 }
6371
6372 //reset can extend bit to avoid an axis extension in SetBinContent
6373 UInt_t oldExtendBitMask = hnew->SetCanExtend(kNoAxis);
6374
6375 // save original statistics
6376 Double_t stat[kNstat];
6377 GetStats(stat);
6378 bool resetStat = false;
6379 // change axis specs and rebuild bin contents array::RebinAx
6380 if(!xbins && (newbins*ngroup != nbins)) {
6382 resetStat = true; //stats must be reset because top bins will be moved to overflow bin
6383 }
6384 // save the TAttAxis members (reset by SetBins)
6396
6397 if(!xbins && (fXaxis.GetXbins()->GetSize() > 0)){ // variable bin sizes
6398 Double_t *bins = new Double_t[newbins+1];
6399 for(i = 0; i <= newbins; ++i) bins[i] = fXaxis.GetBinLowEdge(1+i*ngroup);
6400 hnew->SetBins(newbins,bins); //this also changes errors array (if any)
6401 delete [] bins;
6402 } else if (xbins) {
6403 hnew->SetBins(newbins,xbins);
6404 } else {
6405 hnew->SetBins(newbins,xmin,xmax);
6406 }
6407
6408 // Restore axis attributes
6420
6421 // copy merged bin contents (ignore under/overflows)
6422 // Start merging only once the new lowest edge is reached
6423 Int_t startbin = 1;
6424 const Double_t newxmin = hnew->GetXaxis()->GetBinLowEdge(1);
6425 while( fXaxis.GetBinCenter(startbin) < newxmin && startbin <= nbins ) {
6426 startbin++;
6427 }
6430 for (bin = 1;bin<=newbins;bin++) {
6431 binContent = 0;
6432 binError = 0;
6433 Int_t imax = ngroup;
6434 Double_t xbinmax = hnew->GetXaxis()->GetBinUpEdge(bin);
6435 // check bin edges for the cases when we provide an array of bins
6436 // be careful in case bins can have zero width
6438 hnew->GetXaxis()->GetBinLowEdge(bin),
6439 TMath::Max(1.E-8 * fXaxis.GetBinWidth(oldbin), 1.E-16 )) )
6440 {
6441 Warning("Rebin","Bin edge %d of rebinned histogram does not match any bin edges of the old histogram. Result can be inconsistent",bin);
6442 }
6443 for (i=0;i<ngroup;i++) {
6444 if( (oldbin+i > nbins) ||
6445 ( hnew != this && (fXaxis.GetBinCenter(oldbin+i) > xbinmax)) ) {
6446 imax = i;
6447 break;
6448 }
6451 }
6452 hnew->SetBinContent(bin,binContent);
6453 if (oldErrors) hnew->SetBinError(bin,TMath::Sqrt(binError));
6454 oldbin += imax;
6455 }
6456
6457 // sum underflow and overflow contents until startbin
6458 binContent = 0;
6459 binError = 0;
6460 for (i = 0; i < startbin; ++i) {
6461 binContent += oldBins[i];
6462 if (oldErrors) binError += oldErrors[i]*oldErrors[i];
6463 }
6464 hnew->SetBinContent(0,binContent);
6465 if (oldErrors) hnew->SetBinError(0,TMath::Sqrt(binError));
6466 // sum overflow
6467 binContent = 0;
6468 binError = 0;
6469 for (i = oldbin; i <= nbins+1; ++i) {
6470 binContent += oldBins[i];
6471 if (oldErrors) binError += oldErrors[i]*oldErrors[i];
6472 }
6473 hnew->SetBinContent(newbins+1,binContent);
6474 if (oldErrors) hnew->SetBinError(newbins+1,TMath::Sqrt(binError));
6475
6476 hnew->SetCanExtend(oldExtendBitMask); // restore previous state
6477
6478 // restore statistics and entries modified by SetBinContent
6479 hnew->SetEntries(entries);
6480 if (!resetStat) hnew->PutStats(stat);
6481 delete [] oldBins;
6482 if (oldErrors) delete [] oldErrors;
6483 return hnew;
6484}
6485
6486////////////////////////////////////////////////////////////////////////////////
6487/// finds new limits for the axis so that *point* is within the range and
6488/// the limits are compatible with the previous ones (see TH1::Merge).
6489/// new limits are put into *newMin* and *newMax* variables.
6490/// axis - axis whose limits are to be recomputed
6491/// point - point that should fit within the new axis limits
6492/// newMin - new minimum will be stored here
6493/// newMax - new maximum will be stored here.
6494/// false if failed (e.g. if the initial axis limits are wrong
6495/// or the new range is more than \f$ 2^{64} \f$ times the old one).
6496
6498{
6499 Double_t xmin = axis->GetXmin();
6500 Double_t xmax = axis->GetXmax();
6501 if (xmin >= xmax) return kFALSE;
6503
6504 //recompute new axis limits by doubling the current range
6505 Int_t ntimes = 0;
6506 while (point < xmin) {
6507 if (ntimes++ > 64)
6508 return kFALSE;
6509 xmin = xmin - range;
6510 range *= 2;
6511 }
6512 while (point >= xmax) {
6513 if (ntimes++ > 64)
6514 return kFALSE;
6515 xmax = xmax + range;
6516 range *= 2;
6517 }
6518 newMin = xmin;
6519 newMax = xmax;
6520 // Info("FindNewAxisLimits", "OldAxis: (%lf, %lf), new: (%lf, %lf), point: %lf",
6521 // axis->GetXmin(), axis->GetXmax(), xmin, xmax, point);
6522
6523 return kTRUE;
6524}
6525
6526////////////////////////////////////////////////////////////////////////////////
6527/// Histogram is resized along axis such that x is in the axis range.
6528/// The new axis limits are recomputed by doubling iteratively
6529/// the current axis range until the specified value x is within the limits.
6530/// The algorithm makes a copy of the histogram, then loops on all bins
6531/// of the old histogram to fill the extended histogram.
6532/// Takes into account errors (Sumw2) if any.
6533/// The algorithm works for 1-d, 2-D and 3-D histograms.
6534/// The axis must be extendable before invoking this function.
6535/// Ex:
6536///
6537/// ~~~ {.cpp}
6538/// h->GetXaxis()->SetCanExtend(kTRUE);
6539/// ~~~
6540
6541void TH1::ExtendAxis(Double_t x, TAxis *axis)
6542{
6543 if (!axis->CanExtend()) return;
6544 if (TMath::IsNaN(x)) { // x may be a NaN
6546 return;
6547 }
6548
6549 if (axis->GetXmin() >= axis->GetXmax()) return;
6550 if (axis->GetNbins() <= 0) return;
6551
6553 if (!FindNewAxisLimits(axis, x, xmin, xmax))
6554 return;
6555
6556 //save a copy of this histogram
6557 TH1 *hold = (TH1*)IsA()->New();
6558 hold->SetDirectory(nullptr);
6559 Copy(*hold);
6560 //set new axis limits
6561 axis->SetLimits(xmin,xmax);
6562
6563
6564 //now loop on all bins and refill
6566
6567 Reset("ICE"); //reset only Integral, contents and Errors
6568
6569 int iaxis = 0;
6570 if (axis == &fXaxis) iaxis = 1;
6571 if (axis == &fYaxis) iaxis = 2;
6572 if (axis == &fZaxis) iaxis = 3;
6573 bool firstw = kTRUE;
6574 Int_t binx,biny, binz = 0;
6575 Int_t ix = 0,iy = 0,iz = 0;
6576 Double_t bx,by,bz;
6577 Int_t ncells = hold->GetNcells();
6578 for (Int_t bin = 0; bin < ncells; ++bin) {
6579 hold->GetBinXYZ(bin,binx,biny,binz);
6580 bx = hold->GetXaxis()->GetBinCenter(binx);
6581 ix = fXaxis.FindFixBin(bx);
6582 if (fDimension > 1) {
6583 by = hold->GetYaxis()->GetBinCenter(biny);
6584 iy = fYaxis.FindFixBin(by);
6585 if (fDimension > 2) {
6586 bz = hold->GetZaxis()->GetBinCenter(binz);
6587 iz = fZaxis.FindFixBin(bz);
6588 }
6589 }
6590 // exclude underflow/overflow
6591 double content = hold->RetrieveBinContent(bin);
6592 if (content == 0) continue;
6593 if (IsBinUnderflow(bin,iaxis) || IsBinOverflow(bin,iaxis) ) {
6594 if (firstw) {
6595 Warning("ExtendAxis","Histogram %s has underflow or overflow in the axis that is extendable"
6596 " their content will be lost",GetName() );
6597 firstw= kFALSE;
6598 }
6599 continue;
6600 }
6601 Int_t ibin= GetBin(ix,iy,iz);
6603 if (errors) {
6604 fSumw2.fArray[ibin] += hold->GetBinErrorSqUnchecked(bin);
6605 }
6606 }
6607 delete hold;
6608}
6609
6610////////////////////////////////////////////////////////////////////////////////
6611/// Recursively remove object from the list of functions
6612
6614{
6615 // Rely on TROOT::RecursiveRemove to take the readlock.
6616
6617 if (fFunctions) {
6619 }
6620}
6621
6622////////////////////////////////////////////////////////////////////////////////
6623/// Multiply this histogram by a constant c1.
6624///
6625/// `this = c1*this`
6626///
6627/// Note that both contents and errors (if any) are scaled.
6628/// This function uses the services of TH1::Add
6629///
6630/// IMPORTANT NOTE: Sumw2() is called automatically when scaling.
6631/// If you are not interested in the histogram statistics you can call
6632/// Sumw2(kFALSE) or use the option "nosw2"
6633///
6634/// One can scale a histogram such that the bins integral is equal to
6635/// the normalization parameter via TH1::Scale(Double_t norm), where norm
6636/// is the desired normalization divided by the integral of the histogram.
6637///
6638/// If option contains "width" the bin contents and errors are divided
6639/// by the bin width.
6640
6642{
6643
6644 TString opt = option; opt.ToLower();
6645 // store bin errors when scaling since cannot anymore be computed as sqrt(N)
6646 if (!opt.Contains("nosw2") && GetSumw2N() == 0) Sumw2();
6647 if (opt.Contains("width")) Add(this, this, c1, -1);
6648 else {
6649 if (fBuffer) BufferEmpty(1);
6650 for(Int_t i = 0; i < fNcells; ++i) UpdateBinContent(i, c1 * RetrieveBinContent(i));
6651 if (fSumw2.fN) for(Int_t i = 0; i < fNcells; ++i) fSumw2.fArray[i] *= (c1 * c1); // update errors
6652 // update global histograms statistics
6653 Double_t s[kNstat] = {0};
6654 GetStats(s);
6655 for (Int_t i=0 ; i < kNstat; i++) {
6656 if (i == 1) s[i] = c1*c1*s[i];
6657 else s[i] = c1*s[i];
6658 }
6659 PutStats(s);
6660 SetMinimum(); SetMaximum(); // minimum and maximum value will be recalculated the next time
6661 }
6662
6663 // if contours set, must also scale contours
6665 if (ncontours == 0) return;
6667 for (Int_t i = 0; i < ncontours; ++i) levels[i] *= c1;
6668}
6669
6670////////////////////////////////////////////////////////////////////////////////
6671/// Returns true if all axes are extendable.
6672
6674{
6676 if (GetDimension() > 1) canExtend &= fYaxis.CanExtend();
6677 if (GetDimension() > 2) canExtend &= fZaxis.CanExtend();
6678
6679 return canExtend;
6680}
6681
6682////////////////////////////////////////////////////////////////////////////////
6683/// Make the histogram axes extendable / not extendable according to the bit mask
6684/// returns the previous bit mask specifying which axes are extendable
6685
6687{
6689
6693
6694 if (GetDimension() > 1) {
6698 }
6699
6700 if (GetDimension() > 2) {
6704 }
6705
6706 return oldExtendBitMask;
6707}
6708
6709///////////////////////////////////////////////////////////////////////////////
6710/// Internal function used in TH1::Fill to see which axis is full alphanumeric,
6711/// i.e. can be extended and is alphanumeric
6713{
6717 bitMask |= kYaxis;
6719 bitMask |= kZaxis;
6720
6721 return bitMask;
6722}
6723
6724////////////////////////////////////////////////////////////////////////////////
6725/// Static function to set the default buffer size for automatic histograms.
6726/// When a histogram is created with one of its axis lower limit greater
6727/// or equal to its upper limit, the function SetBuffer is automatically
6728/// called with the default buffer size.
6729
6731{
6732 fgBufferSize = bufsize > 0 ? bufsize : 0;
6733}
6734
6735////////////////////////////////////////////////////////////////////////////////
6736/// When this static function is called with `sumw2=kTRUE`, all new
6737/// histograms will automatically activate the storage
6738/// of the sum of squares of errors, ie TH1::Sumw2 is automatically called.
6739
6741{
6743}
6744
6745////////////////////////////////////////////////////////////////////////////////
6746/// Change/set the title.
6747///
6748/// If title is in the form `stringt;stringx;stringy;stringz`
6749/// the histogram title is set to `stringt`, the x axis title to `stringx`,
6750/// the y axis title to `stringy`, and the z axis title to `stringz`.
6751///
6752/// To insert the character `;` in one of the titles, one should use `#;`
6753/// or `#semicolon`.
6754
6755void TH1::SetTitle(const char *title)
6756{
6757 fTitle = title;
6758 fTitle.ReplaceAll("#;",2,"#semicolon",10);
6759
6760 // Decode fTitle. It may contain X, Y and Z titles
6762 Int_t isc = str1.Index(";");
6763 Int_t lns = str1.Length();
6764
6765 if (isc >=0 ) {
6766 fTitle = str1(0,isc);
6767 str1 = str1(isc+1, lns);
6768 isc = str1.Index(";");
6769 if (isc >=0 ) {
6770 str2 = str1(0,isc);
6771 str2.ReplaceAll("#semicolon",10,";",1);
6772 fXaxis.SetTitle(str2.Data());
6773 lns = str1.Length();
6774 str1 = str1(isc+1, lns);
6775 isc = str1.Index(";");
6776 if (isc >=0 ) {
6777 str2 = str1(0,isc);
6778 str2.ReplaceAll("#semicolon",10,";",1);
6779 fYaxis.SetTitle(str2.Data());
6780 lns = str1.Length();
6781 str1 = str1(isc+1, lns);
6782 str1.ReplaceAll("#semicolon",10,";",1);
6783 fZaxis.SetTitle(str1.Data());
6784 } else {
6785 str1.ReplaceAll("#semicolon",10,";",1);
6786 fYaxis.SetTitle(str1.Data());
6787 }
6788 } else {
6789 str1.ReplaceAll("#semicolon",10,";",1);
6790 fXaxis.SetTitle(str1.Data());
6791 }
6792 }
6793
6794 fTitle.ReplaceAll("#semicolon",10,";",1);
6795
6796 if (gPad && TestBit(kMustCleanup)) gPad->Modified();
6797}
6798
6799////////////////////////////////////////////////////////////////////////////////
6800/// Smooth array xx, translation of Hbook routine `hsmoof.F`.
6801/// Based on algorithm 353QH twice presented by J. Friedman
6802/// in [Proc. of the 1974 CERN School of Computing, Norway, 11-24 August, 1974](https://cds.cern.ch/record/186223).
6803/// See also Section 4.2 in [J. Friedman, Data Analysis Techniques for High Energy Physics](https://www.slac.stanford.edu/pubs/slacreports/reports16/slac-r-176.pdf).
6804
6806{
6807 if (nn < 3 ) {
6808 ::Error("SmoothArray","Need at least 3 points for smoothing: n = %d",nn);
6809 return;
6810 }
6811
6812 Int_t ii;
6813 std::array<double, 3> hh{};
6814
6815 std::vector<double> yy(nn);
6816 std::vector<double> zz(nn);
6817 std::vector<double> rr(nn);
6818
6819 for (Int_t pass=0;pass<ntimes;pass++) {
6820 // first copy original data into temp array
6821 std::copy(xx, xx+nn, zz.begin() );
6822
6823 for (int noent = 0; noent < 2; ++noent) { // run algorithm two times
6824
6825 // do 353 i.e. running median 3, 5, and 3 in a single loop
6826 for (int kk = 0; kk < 3; kk++) {
6827 std::copy(zz.begin(), zz.end(), yy.begin());
6828 int medianType = (kk != 1) ? 3 : 5;
6829 int ifirst = (kk != 1 ) ? 1 : 2;
6830 int ilast = (kk != 1 ) ? nn-1 : nn -2;
6831 //nn2 = nn - ik - 1;
6832 // do all elements beside the first and last point for median 3
6833 // and first two and last 2 for median 5
6834 for ( ii = ifirst; ii < ilast; ii++) {
6835 zz[ii] = TMath::Median(medianType, yy.data() + ii - ifirst);
6836 }
6837
6838 if (kk == 0) { // first median 3
6839 // first point
6840 hh[0] = zz[1];
6841 hh[1] = zz[0];
6842 hh[2] = 3*zz[1] - 2*zz[2];
6843 zz[0] = TMath::Median(3, hh.data());
6844 // last point
6845 hh[0] = zz[nn - 2];
6846 hh[1] = zz[nn - 1];
6847 hh[2] = 3*zz[nn - 2] - 2*zz[nn - 3];
6848 zz[nn - 1] = TMath::Median(3, hh.data());
6849 }
6850
6851 if (kk == 1) { // median 5
6852 // second point with window length 3
6853 zz[1] = TMath::Median(3, yy.data());
6854 // second-to-last point with window length 3
6855 zz[nn - 2] = TMath::Median(3, yy.data() + nn - 3);
6856 }
6857
6858 // In the third iteration (kk == 2), the first and last point stay
6859 // the same (see paper linked in the documentation).
6860 }
6861
6862 std::copy ( zz.begin(), zz.end(), yy.begin() );
6863
6864 // quadratic interpolation for flat segments
6865 for (ii = 2; ii < (nn - 2); ii++) {
6866 if (zz[ii - 1] != zz[ii]) continue;
6867 if (zz[ii] != zz[ii + 1]) continue;
6868 const double tmp0 = zz[ii - 2] - zz[ii];
6869 const double tmp1 = zz[ii + 2] - zz[ii];
6870 if (tmp0 * tmp1 <= 0) continue;
6871 int jk = 1;
6872 if ( std::abs(tmp1) > std::abs(tmp0) ) jk = -1;
6873 yy[ii] = -0.5*zz[ii - 2*jk] + zz[ii]/0.75 + zz[ii + 2*jk] /6.;
6874 yy[ii + jk] = 0.5*(zz[ii + 2*jk] - zz[ii - 2*jk]) + zz[ii];
6875 }
6876
6877 // running means
6878 //std::copy(zz.begin(), zz.end(), yy.begin());
6879 for (ii = 1; ii < nn - 1; ii++) {
6880 zz[ii] = 0.25*yy[ii - 1] + 0.5*yy[ii] + 0.25*yy[ii + 1];
6881 }
6882 zz[0] = yy[0];
6883 zz[nn - 1] = yy[nn - 1];
6884
6885 if (noent == 0) {
6886
6887 // save computed values
6888 std::copy(zz.begin(), zz.end(), rr.begin());
6889
6890 // COMPUTE residuals
6891 for (ii = 0; ii < nn; ii++) {
6892 zz[ii] = xx[ii] - zz[ii];
6893 }
6894 }
6895
6896 } // end loop on noent
6897
6898
6899 double xmin = TMath::MinElement(nn,xx);
6900 for (ii = 0; ii < nn; ii++) {
6901 if (xmin < 0) xx[ii] = rr[ii] + zz[ii];
6902 // make smoothing defined positive - not better using 0 ?
6903 else xx[ii] = std::max((rr[ii] + zz[ii]),0.0 );
6904 }
6905 }
6906}
6907
6908////////////////////////////////////////////////////////////////////////////////
6909/// Smooth bin contents of this histogram.
6910/// if option contains "R" smoothing is applied only to the bins
6911/// defined in the X axis range (default is to smooth all bins)
6912/// Bin contents are replaced by their smooth values.
6913/// Errors (if any) are not modified.
6914/// the smoothing procedure is repeated ntimes (default=1)
6915
6917{
6918 if (fDimension != 1) {
6919 Error("Smooth","Smooth only supported for 1-d histograms");
6920 return;
6921 }
6922 Int_t nbins = fXaxis.GetNbins();
6923 if (nbins < 3) {
6924 Error("Smooth","Smooth only supported for histograms with >= 3 bins. Nbins = %d",nbins);
6925 return;
6926 }
6927
6928 // delete buffer if it is there since it will become invalid
6929 if (fBuffer) BufferEmpty(1);
6930
6931 Int_t firstbin = 1, lastbin = nbins;
6932 TString opt = option;
6933 opt.ToLower();
6934 if (opt.Contains("r")) {
6937 }
6938 nbins = lastbin - firstbin + 1;
6939 Double_t *xx = new Double_t[nbins];
6941 Int_t i;
6942 for (i=0;i<nbins;i++) {
6944 }
6945
6946 TH1::SmoothArray(nbins,xx,ntimes);
6947
6948 for (i=0;i<nbins;i++) {
6950 }
6951 fEntries = nent;
6952 delete [] xx;
6953
6954 if (gPad) gPad->Modified();
6955}
6956
6957////////////////////////////////////////////////////////////////////////////////
6958/// if flag=kTRUE, underflows and overflows are used by the Fill functions
6959/// in the computation of statistics (mean value, StdDev).
6960/// By default, underflows or overflows are not used.
6961
6963{
6965}
6966
6967////////////////////////////////////////////////////////////////////////////////
6968/// Stream a class object.
6969
6970void TH1::Streamer(TBuffer &b)
6971{
6972 if (b.IsReading()) {
6973 UInt_t R__s, R__c;
6974 Version_t R__v = b.ReadVersion(&R__s, &R__c);
6975 if (fDirectory) fDirectory->Remove(this);
6976 fDirectory = nullptr;
6977 if (R__v > 2) {
6978 b.ReadClassBuffer(TH1::Class(), this, R__v, R__s, R__c);
6979
6981 fXaxis.SetParent(this);
6982 fYaxis.SetParent(this);
6983 fZaxis.SetParent(this);
6984 TIter next(fFunctions);
6985 TObject *obj;
6986 while ((obj=next())) {
6987 if (obj->InheritsFrom(TF1::Class())) ((TF1*)obj)->SetParent(this);
6988 }
6989 return;
6990 }
6991 //process old versions before automatic schema evolution
6996 b >> fNcells;
6997 fXaxis.Streamer(b);
6998 fYaxis.Streamer(b);
6999 fZaxis.Streamer(b);
7000 fXaxis.SetParent(this);
7001 fYaxis.SetParent(this);
7002 fZaxis.SetParent(this);
7003 b >> fBarOffset;
7004 b >> fBarWidth;
7005 b >> fEntries;
7006 b >> fTsumw;
7007 b >> fTsumw2;
7008 b >> fTsumwx;
7009 b >> fTsumwx2;
7010 if (R__v < 2) {
7012 Float_t *contour=nullptr;
7013 b >> maximum; fMaximum = maximum;
7014 b >> minimum; fMinimum = minimum;
7015 b >> norm; fNormFactor = norm;
7016 Int_t n = b.ReadArray(contour);
7017 fContour.Set(n);
7018 for (Int_t i=0;i<n;i++) fContour.fArray[i] = contour[i];
7019 delete [] contour;
7020 } else {
7021 b >> fMaximum;
7022 b >> fMinimum;
7023 b >> fNormFactor;
7025 }
7026 fSumw2.Streamer(b);
7028 fFunctions->Delete();
7030 b.CheckByteCount(R__s, R__c, TH1::IsA());
7031
7032 } else {
7033 b.WriteClassBuffer(TH1::Class(),this);
7034 }
7035}
7036
7037////////////////////////////////////////////////////////////////////////////////
7038/// Print some global quantities for this histogram.
7039/// \param[in] option
7040/// - "base" is given, number of bins and ranges are also printed
7041/// - "range" is given, bin contents and errors are also printed
7042/// for all bins in the current range (default 1-->nbins)
7043/// - "all" is given, bin contents and errors are also printed
7044/// for all bins including under and overflows.
7045
7046void TH1::Print(Option_t *option) const
7047{
7048 if (fBuffer) const_cast<TH1*>(this)->BufferEmpty();
7049 printf( "TH1.Print Name = %s, Entries= %d, Total sum= %g\n",GetName(),Int_t(fEntries),GetSumOfWeights());
7050 TString opt = option;
7051 opt.ToLower();
7052 Int_t all;
7053 if (opt.Contains("all")) all = 0;
7054 else if (opt.Contains("range")) all = 1;
7055 else if (opt.Contains("base")) all = 2;
7056 else return;
7057
7058 Int_t bin, binx, biny, binz;
7060 if (all == 0) {
7061 lastx = fXaxis.GetNbins()+1;
7062 if (fDimension > 1) lasty = fYaxis.GetNbins()+1;
7063 if (fDimension > 2) lastz = fZaxis.GetNbins()+1;
7064 } else {
7066 if (fDimension > 1) {firsty = fYaxis.GetFirst(); lasty = fYaxis.GetLast();}
7067 if (fDimension > 2) {firstz = fZaxis.GetFirst(); lastz = fZaxis.GetLast();}
7068 }
7069
7070 if (all== 2) {
7071 printf(" Title = %s\n", GetTitle());
7072 printf(" NbinsX= %d, xmin= %g, xmax=%g", fXaxis.GetNbins(), fXaxis.GetXmin(), fXaxis.GetXmax());
7073 if( fDimension > 1) printf(", NbinsY= %d, ymin= %g, ymax=%g", fYaxis.GetNbins(), fYaxis.GetXmin(), fYaxis.GetXmax());
7074 if( fDimension > 2) printf(", NbinsZ= %d, zmin= %g, zmax=%g", fZaxis.GetNbins(), fZaxis.GetXmin(), fZaxis.GetXmax());
7075 printf("\n");
7076 return;
7077 }
7078
7079 Double_t w,e;
7080 Double_t x,y,z;
7081 if (fDimension == 1) {
7082 for (binx=firstx;binx<=lastx;binx++) {
7085 e = GetBinError(binx);
7086 if(fSumw2.fN) printf(" fSumw[%d]=%g, x=%g, error=%g\n",binx,w,x,e);
7087 else printf(" fSumw[%d]=%g, x=%g\n",binx,w,x);
7088 }
7089 }
7090 if (fDimension == 2) {
7091 for (biny=firsty;biny<=lasty;biny++) {
7093 for (binx=firstx;binx<=lastx;binx++) {
7094 bin = GetBin(binx,biny);
7096 w = RetrieveBinContent(bin);
7097 e = GetBinError(bin);
7098 if(fSumw2.fN) printf(" fSumw[%d][%d]=%g, x=%g, y=%g, error=%g\n",binx,biny,w,x,y,e);
7099 else printf(" fSumw[%d][%d]=%g, x=%g, y=%g\n",binx,biny,w,x,y);
7100 }
7101 }
7102 }
7103 if (fDimension == 3) {
7104 for (binz=firstz;binz<=lastz;binz++) {
7106 for (biny=firsty;biny<=lasty;biny++) {
7108 for (binx=firstx;binx<=lastx;binx++) {
7109 bin = GetBin(binx,biny,binz);
7111 w = RetrieveBinContent(bin);
7112 e = GetBinError(bin);
7113 if(fSumw2.fN) printf(" fSumw[%d][%d][%d]=%g, x=%g, y=%g, z=%g, error=%g\n",binx,biny,binz,w,x,y,z,e);
7114 else printf(" fSumw[%d][%d][%d]=%g, x=%g, y=%g, z=%g\n",binx,biny,binz,w,x,y,z);
7115 }
7116 }
7117 }
7118 }
7119}
7120
7121////////////////////////////////////////////////////////////////////////////////
7122/// Using the current bin info, recompute the arrays for contents and errors
7123
7124void TH1::Rebuild(Option_t *)
7125{
7126 SetBinsLength();
7127 if (fSumw2.fN) {
7129 }
7130}
7131
7132////////////////////////////////////////////////////////////////////////////////
7133/// Reset this histogram: contents, errors, etc.
7134/// \param[in] option
7135/// - if "ICE" is specified, resets only Integral, Contents and Errors.
7136/// - if "ICES" is specified, resets only Integral, Contents, Errors and Statistics
7137/// This option is used
7138/// - if "M" is specified, resets also Minimum and Maximum
7139
7141{
7142 // The option "ICE" is used when extending the histogram (in ExtendAxis, LabelInflate, etc..)
7143 // The option "ICES is used in combination with the buffer (see BufferEmpty and BufferFill)
7144
7145 TString opt = option;
7146 opt.ToUpper();
7147 fSumw2.Reset();
7148 if (fIntegral) {
7149 delete [] fIntegral;
7150 fIntegral = nullptr;
7151 }
7152
7153 if (opt.Contains("M")) {
7154 SetMinimum();
7155 SetMaximum();
7156 }
7157
7158 if (opt.Contains("ICE") && !opt.Contains("S")) return;
7159
7160 // Setting fBuffer[0] = 0 is like resetting the buffer but not deleting it
7161 // But what is the sense of calling BufferEmpty() ? For making the axes ?
7162 // BufferEmpty will update contents that later will be
7163 // reset in calling TH1D::Reset. For this we need to reset the stats afterwards
7164 // It may be needed for computing the axis limits....
7165 if (fBuffer) {BufferEmpty(); fBuffer[0] = 0;}
7166
7167 // need to reset also the statistics
7168 // (needs to be done after calling BufferEmpty() )
7169 fTsumw = 0;
7170 fTsumw2 = 0;
7171 fTsumwx = 0;
7172 fTsumwx2 = 0;
7173 fEntries = 0;
7174
7175 if (opt == "ICES") return;
7176
7177
7178 TObject *stats = fFunctions->FindObject("stats");
7180 //special logic to support the case where the same object is
7181 //added multiple times in fFunctions.
7182 //This case happens when the same object is added with different
7183 //drawing modes
7184 TObject *obj;
7185 while ((obj = fFunctions->First())) {
7186 while(fFunctions->Remove(obj)) { }
7187 delete obj;
7188 }
7189 if(stats) fFunctions->Add(stats);
7190 fContour.Set(0);
7191}
7192
7193////////////////////////////////////////////////////////////////////////////////
7194/// Save the histogram as .csv, .tsv or .txt. In case of any other extension, fall
7195/// back to TObject::SaveAs, which saves as a .C macro (but with the file name
7196/// extension specified by the user)
7197///
7198/// The Under/Overflow bins are also exported (as first and last lines)
7199/// The fist 2 columns are the lower and upper edges of the bins
7200/// Column 3 contains the bin contents
7201/// The last column contains the error in y. If errors are not present, the column
7202/// is left empty
7203///
7204/// The result can be immediately imported into Excel, gnuplot, Python or whatever,
7205/// without the needing to install pyroot, etc.
7206///
7207/// \param filename the name of the file where to store the histogram
7208/// \param option some tuning options
7209///
7210/// The file extension defines the delimiter used:
7211/// - `.csv` : comma
7212/// - `.tsv` : tab
7213/// - `.txt` : space
7214///
7215/// If option = "title" a title line is generated. If the y-axis has a title,
7216/// this title is displayed as column 3 name, otherwise, it shows "BinContent"
7217
7218void TH1::SaveAs(const char *filename, Option_t *option) const
7219{
7220 char del = '\0';
7221 TString ext = "";
7223 TString opt = option;
7224
7225 if (filename) {
7226 if (fname.EndsWith(".csv")) {
7227 del = ',';
7228 ext = "csv";
7229 } else if (fname.EndsWith(".tsv")) {
7230 del = '\t';
7231 ext = "tsv";
7232 } else if (fname.EndsWith(".txt")) {
7233 del = ' ';
7234 ext = "txt";
7235 }
7236 }
7237 if (!del) {
7239 return;
7240 }
7241 std::ofstream out;
7242 out.open(filename, std::ios::out);
7243 if (!out.good()) {
7244 Error("SaveAs", "cannot open file: %s", filename);
7245 return;
7246 }
7247 if (opt.Contains("title")) {
7248 if (std::strcmp(GetYaxis()->GetTitle(), "") == 0) {
7249 out << "# " << "BinLowEdge" << del << "BinUpEdge" << del
7250 << "BinContent"
7251 << del << "ey" << std::endl;
7252 } else {
7253 out << "# " << "BinLowEdge" << del << "BinUpEdge" << del << GetYaxis()->GetTitle() << del << "ey" << std::endl;
7254 }
7255 }
7256 if (fSumw2.fN) {
7257 for (Int_t i = 0; i < fNcells; ++i) { // loop on cells (bins including underflow / overflow)
7258 out << GetXaxis()->GetBinLowEdge(i) << del << GetXaxis()->GetBinUpEdge(i) << del << GetBinContent(i) << del
7259 << GetBinError(i) << std::endl;
7260 }
7261 } else {
7262 for (Int_t i = 0; i < fNcells; ++i) { // loop on cells (bins including underflow / overflow)
7263 out << GetXaxis()->GetBinLowEdge(i) << del << GetXaxis()->GetBinUpEdge(i) << del << GetBinContent(i) << del
7264 << std::endl;
7265 }
7266 }
7267 out.close();
7268 Info("SaveAs", "%s file: %s has been generated", ext.Data(), filename);
7269}
7270
7271////////////////////////////////////////////////////////////////////////////////
7272/// Provide variable name for histogram for saving as primitive
7273/// Histogram pointer has by default the histogram name with an incremental suffix.
7274/// If the histogram belongs to a graph or a stack the suffix is not added because
7275/// the graph and stack objects are not aware of this new name. Same thing if
7276/// the histogram is drawn with the option COLZ because the TPaletteAxis drawn
7277/// when this option is selected, does not know this new name either.
7278
7280{
7281 thread_local Int_t storeNumber = 0;
7282
7283 TString opt = option;
7284 opt.ToLower();
7285 TString histName = GetName();
7286 // for TProfile and TH2Poly also fDirectory should be tested
7287 if (!histName.Contains("Graph") && !histName.Contains("_stack_") && !opt.Contains("colz") &&
7288 (!testfdir || !fDirectory)) {
7289 storeNumber++;
7290 histName += "__";
7291 histName += storeNumber;
7292 }
7293 if (histName.IsNull())
7294 histName = "unnamed";
7295 return gInterpreter->MapCppName(histName);
7296}
7297
7298////////////////////////////////////////////////////////////////////////////////
7299/// Save primitive as a C++ statement(s) on output stream out
7300
7301void TH1::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
7302{
7303 // empty the buffer before if it exists
7304 if (fBuffer)
7305 BufferEmpty();
7306
7308
7311 SetName(hname);
7312
7313 out <<" \n";
7314
7315 // Check if the histogram has equidistant X bins or not. If not, we
7316 // create an array holding the bins.
7317 if (GetXaxis()->GetXbins()->fN && GetXaxis()->GetXbins()->fArray)
7318 sxaxis = SavePrimitiveVector(out, hname + "_x", GetXaxis()->GetXbins()->fN, GetXaxis()->GetXbins()->fArray);
7319 // If the histogram is 2 or 3 dimensional, check if the histogram
7320 // has equidistant Y bins or not. If not, we create an array
7321 // holding the bins.
7322 if (fDimension > 1 && GetYaxis()->GetXbins()->fN && GetYaxis()->GetXbins()->fArray)
7323 syaxis = SavePrimitiveVector(out, hname + "_y", GetYaxis()->GetXbins()->fN, GetYaxis()->GetXbins()->fArray);
7324 // IF the histogram is 3 dimensional, check if the histogram
7325 // has equidistant Z bins or not. If not, we create an array
7326 // holding the bins.
7327 if (fDimension > 2 && GetZaxis()->GetXbins()->fN && GetZaxis()->GetXbins()->fArray)
7328 szaxis = SavePrimitiveVector(out, hname + "_z", GetZaxis()->GetXbins()->fN, GetZaxis()->GetXbins()->fArray);
7329
7330 const auto old_precision{out.precision()};
7331 constexpr auto max_precision{std::numeric_limits<double>::digits10 + 1};
7332 out << std::setprecision(max_precision);
7333
7334 out << " " << ClassName() << " *" << hname << " = new " << ClassName() << "(\"" << hname << "\", \""
7335 << TString(GetTitle()).ReplaceSpecialCppChars() << "\", " << GetXaxis()->GetNbins();
7336 if (!sxaxis.IsNull())
7337 out << ", " << sxaxis << ".data()";
7338 else
7339 out << ", " << GetXaxis()->GetXmin() << ", " << GetXaxis()->GetXmax();
7340 if (fDimension > 1) {
7341 out << ", " << GetYaxis()->GetNbins();
7342 if (!syaxis.IsNull())
7343 out << ", " << syaxis << ".data()";
7344 else
7345 out << ", " << GetYaxis()->GetXmin() << ", " << GetYaxis()->GetXmax();
7346 }
7347 if (fDimension > 2) {
7348 out << ", " << GetZaxis()->GetNbins();
7349 if (!szaxis.IsNull())
7350 out << ", " << szaxis << ".data()";
7351 else
7352 out << ", " << GetZaxis()->GetXmin() << ", " << GetZaxis()->GetXmax();
7353 }
7354 out << ");\n";
7355
7357 Int_t numbins = 0, numerrors = 0;
7358
7359 std::vector<Double_t> content(fNcells), errors(save_errors ? fNcells : 0);
7360 for (Int_t bin = 0; bin < fNcells; bin++) {
7361 content[bin] = RetrieveBinContent(bin);
7362 if (content[bin])
7363 numbins++;
7364 if (save_errors) {
7365 errors[bin] = GetBinError(bin);
7366 if (errors[bin])
7367 numerrors++;
7368 }
7369 }
7370
7371 if ((numbins < 100) && (numerrors < 100)) {
7372 // in case of few non-empty bins store them as before
7373 for (Int_t bin = 0; bin < fNcells; bin++) {
7374 if (content[bin])
7375 out << " " << hname << "->SetBinContent(" << bin << "," << content[bin] << ");\n";
7376 }
7377 if (save_errors)
7378 for (Int_t bin = 0; bin < fNcells; bin++) {
7379 if (errors[bin])
7380 out << " " << hname << "->SetBinError(" << bin << "," << errors[bin] << ");\n";
7381 }
7382 } else {
7383 if (numbins > 0) {
7385 out << " for (Int_t bin = 0; bin < " << fNcells << "; bin++)\n";
7386 out << " if (" << vectname << "[bin])\n";
7387 out << " " << hname << "->SetBinContent(bin, " << vectname << "[bin]);\n";
7388 }
7389 if (numerrors > 0) {
7391 out << " for (Int_t bin = 0; bin < " << fNcells << "; bin++)\n";
7392 out << " if (" << vectname << "[bin])\n";
7393 out << " " << hname << "->SetBinError(bin, " << vectname << "[bin]);\n";
7394 }
7395 }
7396
7398 out << std::setprecision(old_precision);
7399 SetName(savedName.Data());
7400}
7401
7402////////////////////////////////////////////////////////////////////////////////
7403/// Helper function for the SavePrimitive functions from TH1
7404/// or classes derived from TH1, eg TProfile, TProfile2D.
7405
7406void TH1::SavePrimitiveHelp(std::ostream &out, const char *hname, Option_t *option /*= ""*/)
7407{
7408 if (TMath::Abs(GetBarOffset()) > 1e-5)
7409 out << " " << hname << "->SetBarOffset(" << GetBarOffset() << ");\n";
7410 if (TMath::Abs(GetBarWidth() - 1) > 1e-5)
7411 out << " " << hname << "->SetBarWidth(" << GetBarWidth() << ");\n";
7412 if (fMinimum != -1111)
7413 out << " " << hname << "->SetMinimum(" << fMinimum << ");\n";
7414 if (fMaximum != -1111)
7415 out << " " << hname << "->SetMaximum(" << fMaximum << ");\n";
7416 if (fNormFactor != 0)
7417 out << " " << hname << "->SetNormFactor(" << fNormFactor << ");\n";
7418 if (fEntries != 0)
7419 out << " " << hname << "->SetEntries(" << fEntries << ");\n";
7420 if (!fDirectory)
7421 out << " " << hname << "->SetDirectory(nullptr);\n";
7422 if (TestBit(kNoStats))
7423 out << " " << hname << "->SetStats(0);\n";
7424 if (fOption.Length() != 0)
7425 out << " " << hname << "->SetOption(\n" << TString(fOption).ReplaceSpecialCppChars() << "\");\n";
7426
7427 // save contour levels
7429 if (ncontours > 0) {
7431 if (TestBit(kUserContour)) {
7432 std::vector<Double_t> levels(ncontours);
7433 for (Int_t bin = 0; bin < ncontours; bin++)
7434 levels[bin] = GetContourLevel(bin);
7436 }
7437 out << " " << hname << "->SetContour(" << ncontours;
7438 if (!vectname.IsNull())
7439 out << ", " << vectname << ".data()";
7440 out << ");\n";
7441 }
7442
7444
7445 // save attributes
7446 SaveFillAttributes(out, hname, 0, 1001);
7447 SaveLineAttributes(out, hname, 1, 1, 1);
7448 SaveMarkerAttributes(out, hname, 1, 1, 1);
7449 fXaxis.SaveAttributes(out, hname, "->GetXaxis()");
7450 fYaxis.SaveAttributes(out, hname, "->GetYaxis()");
7451 fZaxis.SaveAttributes(out, hname, "->GetZaxis()");
7452
7454}
7455
7456////////////////////////////////////////////////////////////////////////////////
7457/// Save list of functions
7458/// Also can be used by TGraph classes
7459
7460void TH1::SavePrimitiveFunctions(std::ostream &out, const char *varname, TList *lst)
7461{
7462 thread_local Int_t funcNumber = 0;
7463
7464 TObjLink *lnk = lst ? lst->FirstLink() : nullptr;
7465 while (lnk) {
7466 auto obj = lnk->GetObject();
7467 obj->SavePrimitive(out, TString::Format("nodraw #%d\n", ++funcNumber).Data());
7468 TString objvarname = obj->GetName();
7470 if (obj->InheritsFrom(TF1::Class())) {
7472 objvarname = gInterpreter->MapCppName(objvarname);
7473 out << " " << objvarname << "->SetParent(" << varname << ");\n";
7474 } else if (obj->InheritsFrom("TPaveStats")) {
7475 objvarname = "ptstats";
7476 withopt = kFALSE; // pave stats preserve own draw options
7477 out << " " << objvarname << "->SetParent(" << varname << ");\n";
7478 } else if (obj->InheritsFrom("TPolyMarker")) {
7479 objvarname = "pmarker";
7480 }
7481
7482 out << " " << varname << "->GetListOfFunctions()->Add(" << objvarname;
7483 if (withopt)
7484 out << ",\"" << TString(lnk->GetOption()).ReplaceSpecialCppChars() << "\"";
7485 out << ");\n";
7486
7487 lnk = lnk->Next();
7488 }
7489}
7490
7491////////////////////////////////////////////////////////////////////////////////
7532 }
7533}
7534
7535////////////////////////////////////////////////////////////////////////////////
7536/// For axis = 1,2 or 3 returns the mean value of the histogram along
7537/// X,Y or Z axis.
7538///
7539/// For axis = 11, 12, 13 returns the standard error of the mean value
7540/// of the histogram along X, Y or Z axis
7541///
7542/// Note that the mean value/StdDev is computed using the bins in the currently
7543/// defined range (see TAxis::SetRange). By default the range includes
7544/// all bins from 1 to nbins included, excluding underflows and overflows.
7545/// To force the underflows and overflows in the computation, one must
7546/// call the static function TH1::StatOverflows(kTRUE) before filling
7547/// the histogram.
7548///
7549/// IMPORTANT NOTE: The returned value depends on how the histogram statistics
7550/// are calculated. By default, if no range has been set, the returned mean is
7551/// the (unbinned) one calculated at fill time. If a range has been set, however,
7552/// the mean is calculated using the bins in range, as described above; THIS
7553/// IS TRUE EVEN IF THE RANGE INCLUDES ALL BINS--use TAxis::SetRange(0, 0) to unset
7554/// the range. To ensure that the returned mean (and all other statistics) is
7555/// always that of the binned data stored in the histogram, call TH1::ResetStats.
7556/// See TH1::GetStats.
7557///
7558/// Return mean value of this histogram along the X axis.
7559
7560Double_t TH1::GetMean(Int_t axis) const
7561{
7562 if (axis<1 || (axis>3 && axis<11) || axis>13) return 0;
7564 for (Int_t i=4;i<kNstat;i++) stats[i] = 0;
7565 GetStats(stats);
7566 if (stats[0] == 0) return 0;
7567 if (axis<4){
7568 Int_t ax[3] = {2,4,7};
7569 return stats[ax[axis-1]]/stats[0];
7570 } else {
7571 // mean error = StdDev / sqrt( Neff )
7572 Double_t stddev = GetStdDev(axis-10);
7574 return ( neff > 0 ? stddev/TMath::Sqrt(neff) : 0. );
7575 }
7576}
7577
7578////////////////////////////////////////////////////////////////////////////////
7579/// Return standard error of mean of this histogram along the X axis.
7580///
7581/// Note that the mean value/StdDev is computed using the bins in the currently
7582/// defined range (see TAxis::SetRange). By default the range includes
7583/// all bins from 1 to nbins included, excluding underflows and overflows.
7584/// To force the underflows and overflows in the computation, one must
7585/// call the static function TH1::StatOverflows(kTRUE) before filling
7586/// the histogram.
7587///
7588/// Also note, that although the definition of standard error doesn't include the
7589/// assumption of normality, many uses of this feature implicitly assume it.
7590///
7591/// IMPORTANT NOTE: The returned value depends on how the histogram statistics
7592/// are calculated. By default, if no range has been set, the returned value is
7593/// the (unbinned) one calculated at fill time. If a range has been set, however,
7594/// the value is calculated using the bins in range, as described above; THIS
7595/// IS TRUE EVEN IF THE RANGE INCLUDES ALL BINS--use TAxis::SetRange(0, 0) to unset
7596/// the range. To ensure that the returned value (and all other statistics) is
7597/// always that of the binned data stored in the histogram, call TH1::ResetStats.
7598/// See TH1::GetStats.
7599
7601{
7602 return GetMean(axis+10);
7603}
7604
7605////////////////////////////////////////////////////////////////////////////////
7606/// Returns the Standard Deviation (Sigma).
7607/// The Sigma estimate is computed as
7608/// \f[
7609/// \sqrt{\frac{1}{N}(\sum(x_i-x_{mean})^2)}
7610/// \f]
7611/// For axis = 1,2 or 3 returns the Sigma value of the histogram along
7612/// X, Y or Z axis
7613/// For axis = 11, 12 or 13 returns the error of StdDev estimation along
7614/// X, Y or Z axis for Normal distribution
7615///
7616/// Note that the mean value/sigma is computed using the bins in the currently
7617/// defined range (see TAxis::SetRange). By default the range includes
7618/// all bins from 1 to nbins included, excluding underflows and overflows.
7619/// To force the underflows and overflows in the computation, one must
7620/// call the static function TH1::StatOverflows(kTRUE) before filling
7621/// the histogram.
7622///
7623/// IMPORTANT NOTE: The returned value depends on how the histogram statistics
7624/// are calculated. By default, if no range has been set, the returned standard
7625/// deviation is the (unbinned) one calculated at fill time. If a range has been
7626/// set, however, the standard deviation is calculated using the bins in range,
7627/// as described above; THIS IS TRUE EVEN IF THE RANGE INCLUDES ALL BINS--use
7628/// TAxis::SetRange(0, 0) to unset the range. To ensure that the returned standard
7629/// deviation (and all other statistics) is always that of the binned data stored
7630/// in the histogram, call TH1::ResetStats. See TH1::GetStats.
7631
7632Double_t TH1::GetStdDev(Int_t axis) const
7633{
7634 if (axis<1 || (axis>3 && axis<11) || axis>13) return 0;
7635
7637 for (Int_t i=4;i<kNstat;i++) stats[i] = 0;
7638 GetStats(stats);
7639 if (stats[0] == 0) return 0;
7640 Int_t ax[3] = {2,4,7};
7641 Int_t axm = ax[axis%10 - 1];
7642 x = stats[axm]/stats[0];
7643 // for negative stddev (e.g. when having negative weights) - return stdev=0
7644 stddev2 = TMath::Max( stats[axm+1]/stats[0] -x*x, 0.0 );
7645 if (axis<10)
7646 return TMath::Sqrt(stddev2);
7647 else {
7648 // The right formula for StdDev error depends on 4th momentum (see Kendall-Stuart Vol 1 pag 243)
7649 // formula valid for only gaussian distribution ( 4-th momentum = 3 * sigma^4 )
7651 return ( neff > 0 ? TMath::Sqrt(stddev2/(2*neff) ) : 0. );
7652 }
7653}
7654
7655////////////////////////////////////////////////////////////////////////////////
7656/// Return error of standard deviation estimation for Normal distribution
7657///
7658/// Note that the mean value/StdDev is computed using the bins in the currently
7659/// defined range (see TAxis::SetRange). By default the range includes
7660/// all bins from 1 to nbins included, excluding underflows and overflows.
7661/// To force the underflows and overflows in the computation, one must
7662/// call the static function TH1::StatOverflows(kTRUE) before filling
7663/// the histogram.
7664///
7665/// Value returned is standard deviation of sample standard deviation.
7666/// Note that it is an approximated value which is valid only in the case that the
7667/// original data distribution is Normal. The correct one would require
7668/// the 4-th momentum value, which cannot be accurately estimated from a histogram since
7669/// the x-information for all entries is not kept.
7670///
7671/// IMPORTANT NOTE: The returned value depends on how the histogram statistics
7672/// are calculated. By default, if no range has been set, the returned value is
7673/// the (unbinned) one calculated at fill time. If a range has been set, however,
7674/// the value is calculated using the bins in range, as described above; THIS
7675/// IS TRUE EVEN IF THE RANGE INCLUDES ALL BINS--use TAxis::SetRange(0, 0) to unset
7676/// the range. To ensure that the returned value (and all other statistics) is
7677/// always that of the binned data stored in the histogram, call TH1::ResetStats.
7678/// See TH1::GetStats.
7679
7681{
7682 return GetStdDev(axis+10);
7683}
7684
7685////////////////////////////////////////////////////////////////////////////////
7686/// - For axis = 1, 2 or 3 returns skewness of the histogram along x, y or z axis.
7687/// - For axis = 11, 12 or 13 returns the approximate standard error of skewness
7688/// of the histogram along x, y or z axis
7689///
7690///Note, that since third and fourth moment are not calculated
7691///at the fill time, skewness and its standard error are computed bin by bin
7692///
7693/// IMPORTANT NOTE: The returned value depends on how the histogram statistics
7694/// are calculated. See TH1::GetMean and TH1::GetStdDev.
7695
7697{
7698
7699 if (axis > 0 && axis <= 3){
7700
7701 Double_t mean = GetMean(axis);
7702 Double_t stddev = GetStdDev(axis);
7704
7711 // include underflow/overflow if TH1::StatOverflows(kTRUE) in case no range is set on the axis
7714 if (firstBinX == 1) firstBinX = 0;
7715 if (lastBinX == fXaxis.GetNbins() ) lastBinX += 1;
7716 }
7718 if (firstBinY == 1) firstBinY = 0;
7719 if (lastBinY == fYaxis.GetNbins() ) lastBinY += 1;
7720 }
7722 if (firstBinZ == 1) firstBinZ = 0;
7723 if (lastBinZ == fZaxis.GetNbins() ) lastBinZ += 1;
7724 }
7725 }
7726
7727 Double_t x = 0;
7728 Double_t sum=0;
7729 Double_t np=0;
7730 for (Int_t binx = firstBinX; binx <= lastBinX; binx++) {
7731 for (Int_t biny = firstBinY; biny <= lastBinY; biny++) {
7732 for (Int_t binz = firstBinZ; binz <= lastBinZ; binz++) {
7733 if (axis==1 ) x = fXaxis.GetBinCenter(binx);
7734 else if (axis==2 ) x = fYaxis.GetBinCenter(biny);
7735 else if (axis==3 ) x = fZaxis.GetBinCenter(binz);
7737 np+=w;
7738 sum+=w*(x-mean)*(x-mean)*(x-mean);
7739 }
7740 }
7741 }
7742 sum/=np*stddev3;
7743 return sum;
7744 }
7745 else if (axis > 10 && axis <= 13) {
7746 //compute standard error of skewness
7747 // assume parent normal distribution use formula from Kendall-Stuart, Vol 1 pag 243, second edition
7749 return ( neff > 0 ? TMath::Sqrt(6./neff ) : 0. );
7750 }
7751 else {
7752 Error("GetSkewness", "illegal value of parameter");
7753 return 0;
7754 }
7755}
7756
7757////////////////////////////////////////////////////////////////////////////////
7758/// - For axis =1, 2 or 3 returns kurtosis of the histogram along x, y or z axis.
7759/// Kurtosis(gaussian(0, 1)) = 0.
7760/// - For axis =11, 12 or 13 returns the approximate standard error of kurtosis
7761/// of the histogram along x, y or z axis
7762////
7763/// Note, that since third and fourth moment are not calculated
7764/// at the fill time, kurtosis and its standard error are computed bin by bin
7765///
7766/// IMPORTANT NOTE: The returned value depends on how the histogram statistics
7767/// are calculated. See TH1::GetMean and TH1::GetStdDev.
7768
7770{
7771 if (axis > 0 && axis <= 3){
7772
7773 Double_t mean = GetMean(axis);
7774 Double_t stddev = GetStdDev(axis);
7776
7783 // include underflow/overflow if TH1::StatOverflows(kTRUE) in case no range is set on the axis
7786 if (firstBinX == 1) firstBinX = 0;
7787 if (lastBinX == fXaxis.GetNbins() ) lastBinX += 1;
7788 }
7790 if (firstBinY == 1) firstBinY = 0;
7791 if (lastBinY == fYaxis.GetNbins() ) lastBinY += 1;
7792 }
7794 if (firstBinZ == 1) firstBinZ = 0;
7795 if (lastBinZ == fZaxis.GetNbins() ) lastBinZ += 1;
7796 }
7797 }
7798
7799 Double_t x = 0;
7800 Double_t sum=0;
7801 Double_t np=0;
7802 for (Int_t binx = firstBinX; binx <= lastBinX; binx++) {
7803 for (Int_t biny = firstBinY; biny <= lastBinY; biny++) {
7804 for (Int_t binz = firstBinZ; binz <= lastBinZ; binz++) {
7805 if (axis==1 ) x = fXaxis.GetBinCenter(binx);
7806 else if (axis==2 ) x = fYaxis.GetBinCenter(biny);
7807 else if (axis==3 ) x = fZaxis.GetBinCenter(binz);
7809 np+=w;
7810 sum+=w*(x-mean)*(x-mean)*(x-mean)*(x-mean);
7811 }
7812 }
7813 }
7814 sum/=(np*stddev4);
7815 return sum-3;
7816
7817 } else if (axis > 10 && axis <= 13) {
7818 //compute standard error of skewness
7819 // assume parent normal distribution use formula from Kendall-Stuart, Vol 1 pag 243, second edition
7821 return ( neff > 0 ? TMath::Sqrt(24./neff ) : 0. );
7822 }
7823 else {
7824 Error("GetKurtosis", "illegal value of parameter");
7825 return 0;
7826 }
7827}
7828
7829////////////////////////////////////////////////////////////////////////////////
7830/// fill the array stats from the contents of this histogram
7831/// The array stats must be correctly dimensioned in the calling program.
7832///
7833/// ~~~ {.cpp}
7834/// stats[0] = sumw
7835/// stats[1] = sumw2
7836/// stats[2] = sumwx
7837/// stats[3] = sumwx2
7838/// ~~~
7839///
7840/// If no axis-subrange is specified (via TAxis::SetRange), the array stats
7841/// is simply a copy of the statistics quantities computed at filling time.
7842/// If a sub-range is specified, the function recomputes these quantities
7843/// from the bin contents in the current axis range.
7844///
7845/// IMPORTANT NOTE: This means that the returned statistics are context-dependent.
7846/// If TAxis::kAxisRange, the returned statistics are dependent on the binning;
7847/// otherwise, they are a copy of the histogram statistics computed at fill time,
7848/// which are unbinned by default (calling TH1::ResetStats forces them to use
7849/// binned statistics). You can reset TAxis::kAxisRange using TAxis::SetRange(0, 0).
7850///
7851/// Note that the mean value/StdDev is computed using the bins in the currently
7852/// defined range (see TAxis::SetRange). By default the range includes
7853/// all bins from 1 to nbins included, excluding underflows and overflows.
7854/// To force the underflows and overflows in the computation, one must
7855/// call the static function TH1::StatOverflows(kTRUE) before filling
7856/// the histogram.
7857
7858void TH1::GetStats(Double_t *stats) const
7859{
7860 if (fBuffer) ((TH1*)this)->BufferEmpty();
7861
7862 // Loop on bins (possibly including underflows/overflows)
7863 Int_t bin, binx;
7864 Double_t w,err;
7865 Double_t x;
7866 // identify the case of labels with extension of axis range
7867 // in this case the statistics in x does not make any sense
7868 Bool_t labelHist = ((const_cast<TAxis&>(fXaxis)).GetLabels() && fXaxis.CanExtend() );
7869 // fTsumw == 0 && fEntries > 0 is a special case when uses SetBinContent or calls ResetStats before
7870 if ( (fTsumw == 0 && fEntries > 0) || fXaxis.TestBit(TAxis::kAxisRange) ) {
7871 for (bin=0;bin<4;bin++) stats[bin] = 0;
7872
7875 // include underflow/overflow if TH1::StatOverflows(kTRUE) in case no range is set on the axis
7877 if (firstBinX == 1) firstBinX = 0;
7878 if (lastBinX == fXaxis.GetNbins() ) lastBinX += 1;
7879 }
7880 for (binx = firstBinX; binx <= lastBinX; binx++) {
7882 //w = TMath::Abs(RetrieveBinContent(binx));
7883 // not sure what to do here if w < 0
7885 err = TMath::Abs(GetBinError(binx));
7886 stats[0] += w;
7887 stats[1] += err*err;
7888 // statistics in x makes sense only for not labels histograms
7889 if (!labelHist) {
7890 stats[2] += w*x;
7891 stats[3] += w*x*x;
7892 }
7893 }
7894 // if (stats[0] < 0) {
7895 // // in case total is negative do something ??
7896 // stats[0] = 0;
7897 // }
7898 } else {
7899 stats[0] = fTsumw;
7900 stats[1] = fTsumw2;
7901 stats[2] = fTsumwx;
7902 stats[3] = fTsumwx2;
7903 }
7904}
7905
7906////////////////////////////////////////////////////////////////////////////////
7907/// Replace current statistics with the values in array stats
7908
7910{
7911 fTsumw = stats[0];
7912 fTsumw2 = stats[1];
7913 fTsumwx = stats[2];
7914 fTsumwx2 = stats[3];
7915}
7916
7917////////////////////////////////////////////////////////////////////////////////
7918/// Reset the statistics including the number of entries
7919/// and replace with values calculated from bin content
7920///
7921/// The number of entries is set to the total bin content or (in case of weighted histogram)
7922/// to number of effective entries
7923///
7924/// \note By default, before calling this function, statistics are those
7925/// computed at fill time, which are unbinned. See TH1::GetStats.
7926
7927void TH1::ResetStats()
7928{
7929 Double_t stats[kNstat] = {0};
7930 fTsumw = 0;
7931 fEntries = 1; // to force re-calculation of the statistics in TH1::GetStats
7932 GetStats(stats);
7933 PutStats(stats);
7935 // use effective entries for weighted histograms: (sum_w) ^2 / sum_w2
7936 if (fSumw2.fN > 0 && fTsumw > 0 && stats[1] > 0 ) fEntries = stats[0]*stats[0]/ stats[1];
7937}
7938
7939////////////////////////////////////////////////////////////////////////////////
7940/// Return the sum of all weights
7941/// \param includeOverflow true to include under/overflows bins, false to exclude those.
7942/// \note Different from TH1::GetSumOfWeights, that always excludes those
7943
7945{
7946 if (fBuffer) const_cast<TH1*>(this)->BufferEmpty();
7947
7948 const Int_t start = (includeOverflow ? 0 : 1);
7949 const Int_t lastX = fXaxis.GetNbins() + (includeOverflow ? 1 : 0);
7950 const Int_t lastY = fYaxis.GetNbins() + (includeOverflow ? 1 : 0);
7951 const Int_t lastZ = fZaxis.GetNbins() + (includeOverflow ? 1 : 0);
7952 Double_t sum =0;
7953 for(auto binz = start; binz <= lastZ; binz++) {
7954 for(auto biny = start; biny <= lastY; biny++) {
7955 for(auto binx = start; binx <= lastX; binx++) {
7956 const auto bin = GetBin(binx, biny, binz);
7957 sum += RetrieveBinContent(bin);
7958 }
7959 }
7960 }
7961 return sum;
7962}
7963
7964////////////////////////////////////////////////////////////////////////////////
7965///Return integral of bin contents. Only bins in the bins range are considered.
7966///
7967/// By default the integral is computed as the sum of bin contents in the range.
7968/// if option "width" is specified, the integral is the sum of
7969/// the bin contents multiplied by the bin width in x.
7970
7972{
7974}
7975
7976////////////////////////////////////////////////////////////////////////////////
7977/// Return integral of bin contents in range [binx1,binx2].
7978///
7979/// By default the integral is computed as the sum of bin contents in the range.
7980/// if option "width" is specified, the integral is the sum of
7981/// the bin contents multiplied by the bin width in x.
7982
7984{
7985 double err = 0;
7986 return DoIntegral(binx1,binx2,0,-1,0,-1,err,option);
7987}
7988
7989////////////////////////////////////////////////////////////////////////////////
7990/// Return integral of bin contents in range [binx1,binx2] and its error.
7991///
7992/// By default the integral is computed as the sum of bin contents in the range.
7993/// if option "width" is specified, the integral is the sum of
7994/// the bin contents multiplied by the bin width in x.
7995/// the error is computed using error propagation from the bin errors assuming that
7996/// all the bins are uncorrelated
7997
7999{
8000 return DoIntegral(binx1,binx2,0,-1,0,-1,error,option,kTRUE);
8001}
8002
8003////////////////////////////////////////////////////////////////////////////////
8004/// Internal function compute integral and optionally the error between the limits
8005/// specified by the bin number values working for all histograms (1D, 2D and 3D)
8006
8008 Option_t *option, Bool_t doError) const
8009{
8010 if (fBuffer) ((TH1*)this)->BufferEmpty();
8011
8012 Int_t nx = GetNbinsX() + 2;
8013 if (binx1 < 0) binx1 = 0;
8014 if (binx2 >= nx || binx2 < binx1) binx2 = nx - 1;
8015
8016 if (GetDimension() > 1) {
8017 Int_t ny = GetNbinsY() + 2;
8018 if (biny1 < 0) biny1 = 0;
8019 if (biny2 >= ny || biny2 < biny1) biny2 = ny - 1;
8020 } else {
8021 biny1 = 0; biny2 = 0;
8022 }
8023
8024 if (GetDimension() > 2) {
8025 Int_t nz = GetNbinsZ() + 2;
8026 if (binz1 < 0) binz1 = 0;
8027 if (binz2 >= nz || binz2 < binz1) binz2 = nz - 1;
8028 } else {
8029 binz1 = 0; binz2 = 0;
8030 }
8031
8032 // - Loop on bins in specified range
8033 TString opt = option;
8034 opt.ToLower();
8036 if (opt.Contains("width")) width = kTRUE;
8037
8038
8039 Double_t dx = 1., dy = .1, dz =.1;
8040 Double_t integral = 0;
8041 Double_t igerr2 = 0;
8042 for (Int_t binx = binx1; binx <= binx2; ++binx) {
8043 if (width) dx = fXaxis.GetBinWidth(binx);
8044 for (Int_t biny = biny1; biny <= biny2; ++biny) {
8045 if (width) dy = fYaxis.GetBinWidth(biny);
8046 for (Int_t binz = binz1; binz <= binz2; ++binz) {
8047 Int_t bin = GetBin(binx, biny, binz);
8048 Double_t dv = 0.0;
8049 if (width) {
8051 dv = dx * dy * dz;
8052 integral += RetrieveBinContent(bin) * dv;
8053 } else {
8054 integral += RetrieveBinContent(bin);
8055 }
8056 if (doError) {
8057 if (width) igerr2 += GetBinErrorSqUnchecked(bin) * dv * dv;
8058 else igerr2 += GetBinErrorSqUnchecked(bin);
8059 }
8060 }
8061 }
8062 }
8063
8064 if (doError) error = TMath::Sqrt(igerr2);
8065 return integral;
8066}
8067
8068////////////////////////////////////////////////////////////////////////////////
8069/// Statistical test of compatibility in shape between
8070/// this histogram and h2, using the Anderson-Darling 2 sample test.
8071///
8072/// The AD 2 sample test formula are derived from the paper
8073/// F.W Scholz, M.A. Stephens "k-Sample Anderson-Darling Test".
8074///
8075/// The test is implemented in root in the ROOT::Math::GoFTest class
8076/// It is the same formula ( (6) in the paper), and also shown in
8077/// [this preprint](http://arxiv.org/pdf/0804.0380v1.pdf)
8078///
8079/// Binned data are considered as un-binned data
8080/// with identical observation happening in the bin center.
8081///
8082/// \param[in] h2 Pointer to 1D histogram
8083/// \param[in] option is a character string to specify options
8084/// - "D" Put out a line of "Debug" printout
8085/// - "T" Return the normalized A-D test statistic
8086///
8087/// - Note1: Underflow and overflow are not considered in the test
8088/// - Note2: The test works only for un-weighted histogram (i.e. representing counts)
8089/// - Note3: The histograms are not required to have the same X axis
8090/// - Note4: The test works only for 1-dimensional histograms
8091
8093{
8094 Double_t advalue = 0;
8096
8097 TString opt = option;
8098 opt.ToUpper();
8099 if (opt.Contains("D") ) {
8100 printf(" AndersonDarlingTest Prob = %g, AD TestStatistic = %g\n",pvalue,advalue);
8101 }
8102 if (opt.Contains("T") ) return advalue;
8103
8104 return pvalue;
8105}
8106
8107////////////////////////////////////////////////////////////////////////////////
8108/// Same function as above but returning also the test statistic value
8109
8111{
8112 if (GetDimension() != 1 || h2->GetDimension() != 1) {
8113 Error("AndersonDarlingTest","Histograms must be 1-D");
8114 return -1;
8115 }
8116
8117 // empty the buffer. Probably we could add as an unbinned test
8118 if (fBuffer) ((TH1*)this)->BufferEmpty();
8119
8120 // use the BinData class
8123
8124 ROOT::Fit::FillData(data1, this, nullptr);
8125 ROOT::Fit::FillData(data2, h2, nullptr);
8126
8127 double pvalue;
8129
8130 return pvalue;
8131}
8132
8133////////////////////////////////////////////////////////////////////////////////
8134/// Statistical test of compatibility in shape between
8135/// this histogram and h2, using Kolmogorov test.
8136/// Note that the KolmogorovTest (KS) test should in theory be used only for unbinned data
8137/// and not for binned data as in the case of the histogram (see NOTE 3 below).
8138/// So, before using this method blindly, read the NOTE 3.
8139///
8140/// Default: Ignore under- and overflow bins in comparison
8141///
8142/// \param[in] h2 histogram
8143/// \param[in] option is a character string to specify options
8144/// - "U" include Underflows in test (also for 2-dim)
8145/// - "O" include Overflows (also valid for 2-dim)
8146/// - "N" include comparison of normalizations
8147/// - "D" Put out a line of "Debug" printout
8148/// - "M" Return the Maximum Kolmogorov distance instead of prob
8149/// - "X" Run the pseudo experiments post-processor with the following procedure:
8150/// make pseudoexperiments based on random values from the parent distribution,
8151/// compare the KS distance of the pseudoexperiment to the parent
8152/// distribution, and count all the KS values above the value
8153/// obtained from the original data to Monte Carlo distribution.
8154/// The number of pseudo-experiments nEXPT is by default 1000, and
8155/// it can be changed by specifying the option as "X=number",
8156/// for example "X=10000" for 10000 toys.
8157/// The function returns the probability.
8158/// (thanks to Ben Kilminster to submit this procedure). Note that
8159/// this option "X" is much slower.
8160///
8161/// The returned function value is the probability of test
8162/// (much less than one means NOT compatible)
8163///
8164/// Code adapted by Rene Brun from original HBOOK routine HDIFF
8165///
8166/// NOTE1
8167/// A good description of the Kolmogorov test can be seen at:
8168/// http://www.itl.nist.gov/div898/handbook/eda/section3/eda35g.htm
8169///
8170/// NOTE2
8171/// see also alternative function TH1::Chi2Test
8172/// The Kolmogorov test is assumed to give better results than Chi2Test
8173/// in case of histograms with low statistics.
8174///
8175/// NOTE3 (Jan Conrad, Fred James)
8176/// "The returned value PROB is calculated such that it will be
8177/// uniformly distributed between zero and one for compatible histograms,
8178/// provided the data are not binned (or the number of bins is very large
8179/// compared with the number of events). Users who have access to unbinned
8180/// data and wish exact confidence levels should therefore not put their data
8181/// into histograms, but should call directly TMath::KolmogorovTest. On
8182/// the other hand, since TH1 is a convenient way of collecting data and
8183/// saving space, this function has been provided. However, the values of
8184/// PROB for binned data will be shifted slightly higher than expected,
8185/// depending on the effects of the binning. For example, when comparing two
8186/// uniform distributions of 500 events in 100 bins, the values of PROB,
8187/// instead of being exactly uniformly distributed between zero and one, have
8188/// a mean value of about 0.56. We can apply a useful
8189/// rule: As long as the bin width is small compared with any significant
8190/// physical effect (for example the experimental resolution) then the binning
8191/// cannot have an important effect. Therefore, we believe that for all
8192/// practical purposes, the probability value PROB is calculated correctly
8193/// provided the user is aware that:
8194///
8195/// 1. The value of PROB should not be expected to have exactly the correct
8196/// distribution for binned data.
8197/// 2. The user is responsible for seeing to it that the bin widths are
8198/// small compared with any physical phenomena of interest.
8199/// 3. The effect of binning (if any) is always to make the value of PROB
8200/// slightly too big. That is, setting an acceptance criterion of (PROB>0.05
8201/// will assure that at most 5% of truly compatible histograms are rejected,
8202/// and usually somewhat less."
8203///
8204/// Note also that for GoF test of unbinned data ROOT provides also the class
8205/// ROOT::Math::GoFTest. The class has also method for doing one sample tests
8206/// (i.e. comparing the data with a given distribution).
8207
8209{
8210 TString opt = option;
8211 opt.ToUpper();
8212
8213 Double_t prob = 0;
8214 TH1 *h1 = (TH1*)this;
8215 if (h2 == nullptr) return 0;
8216 const TAxis *axis1 = h1->GetXaxis();
8217 const TAxis *axis2 = h2->GetXaxis();
8218 Int_t ncx1 = axis1->GetNbins();
8219 Int_t ncx2 = axis2->GetNbins();
8220
8221 // Check consistency of dimensions
8222 if (h1->GetDimension() != 1 || h2->GetDimension() != 1) {
8223 Error("KolmogorovTest","Histograms must be 1-D\n");
8224 return 0;
8225 }
8226
8227 // Check consistency in number of channels
8228 if (ncx1 != ncx2) {
8229 Error("KolmogorovTest","Histograms have different number of bins, %d and %d\n",ncx1,ncx2);
8230 return 0;
8231 }
8232
8233 // empty the buffer. Probably we could add as an unbinned test
8234 if (fBuffer) ((TH1*)this)->BufferEmpty();
8235
8236 // Check consistency in bin edges
8237 for(Int_t i = 1; i <= axis1->GetNbins() + 1; ++i) {
8238 if(!TMath::AreEqualRel(axis1->GetBinLowEdge(i), axis2->GetBinLowEdge(i), 1.E-15)) {
8239 Error("KolmogorovTest","Histograms are not consistent: they have different bin edges");
8240 return 0;
8241 }
8242 }
8243
8246 Double_t sum1 = 0, sum2 = 0;
8247 Double_t ew1, ew2, w1 = 0, w2 = 0;
8248 Int_t bin;
8249 Int_t ifirst = 1;
8250 Int_t ilast = ncx1;
8251 // integral of all bins (use underflow/overflow if option)
8252 if (opt.Contains("U")) ifirst = 0;
8253 if (opt.Contains("O")) ilast = ncx1 +1;
8254 for (bin = ifirst; bin <= ilast; bin++) {
8255 sum1 += h1->RetrieveBinContent(bin);
8256 sum2 += h2->RetrieveBinContent(bin);
8257 ew1 = h1->GetBinError(bin);
8258 ew2 = h2->GetBinError(bin);
8259 w1 += ew1*ew1;
8260 w2 += ew2*ew2;
8261 }
8262 if (sum1 == 0) {
8263 Error("KolmogorovTest","Histogram1 %s integral is zero\n",h1->GetName());
8264 return 0;
8265 }
8266 if (sum2 == 0) {
8267 Error("KolmogorovTest","Histogram2 %s integral is zero\n",h2->GetName());
8268 return 0;
8269 }
8270
8271 // calculate the effective entries.
8272 // the case when errors are zero (w1 == 0 or w2 ==0) are equivalent to
8273 // compare to a function. In that case the rescaling is done only on sqrt(esum2) or sqrt(esum1)
8274 Double_t esum1 = 0, esum2 = 0;
8275 if (w1 > 0)
8276 esum1 = sum1 * sum1 / w1;
8277 else
8278 afunc1 = kTRUE; // use later for calculating z
8279
8280 if (w2 > 0)
8281 esum2 = sum2 * sum2 / w2;
8282 else
8283 afunc2 = kTRUE; // use later for calculating z
8284
8285 if (afunc2 && afunc1) {
8286 Error("KolmogorovTest","Errors are zero for both histograms\n");
8287 return 0;
8288 }
8289
8290
8291 Double_t s1 = 1/sum1;
8292 Double_t s2 = 1/sum2;
8293
8294 // Find largest difference for Kolmogorov Test
8295 Double_t dfmax =0, rsum1 = 0, rsum2 = 0;
8296
8297 for (bin=ifirst;bin<=ilast;bin++) {
8298 rsum1 += s1*h1->RetrieveBinContent(bin);
8299 rsum2 += s2*h2->RetrieveBinContent(bin);
8301 }
8302
8303 // Get Kolmogorov probability
8304 Double_t z, prb1=0, prb2=0, prb3=0;
8305
8306 // case h1 is exact (has zero errors)
8307 if (afunc1)
8308 z = dfmax*TMath::Sqrt(esum2);
8309 // case h2 has zero errors
8310 else if (afunc2)
8311 z = dfmax*TMath::Sqrt(esum1);
8312 else
8313 // for comparison between two data sets
8315
8317
8318 // option N to combine normalization makes sense if both afunc1 and afunc2 are false
8319 if (opt.Contains("N") && !(afunc1 || afunc2 ) ) {
8320 // Combine probabilities for shape and normalization,
8321 prb1 = prob;
8324 prb2 = TMath::Prob(chi2,1);
8325 // see Eadie et al., section 11.6.2
8326 if (prob > 0 && prb2 > 0) prob *= prb2*(1-TMath::Log(prob*prb2));
8327 else prob = 0;
8328 }
8329 // X option. Run Pseudo-experiments to determine NULL distribution of the
8330 // KS distance. We can find the probability from the number of pseudo-experiment that have a
8331 // KS distance larger than the one opbserved in the data.
8332 // We use the histogram with the largest statistics as a parent distribution for the NULL.
8333 // Note if one histogram has zero errors is considered as a function. In that case we use it
8334 // as parent distribution for the toys.
8335 //
8336 Int_t nEXPT = 1000;
8337 if (opt.Contains("X")) {
8338 // get number of pseudo-experiment of specified
8339 if (opt.Contains("X=")) {
8340 int numpos = opt.Index("X=") + 2; // 2 is length of X=
8341 int numlen = 0;
8342 int len = opt.Length();
8343 while( (numpos+numlen<len) && isdigit(opt[numpos+numlen]) )
8344 numlen++;
8345 TString snum = opt(numpos,numlen);
8346 int num = atoi(snum.Data());
8347 if (num <= 0)
8348 Warning("KolmogorovTest","invalid number of toys given: %d - use 1000",num);
8349 else
8350 nEXPT = num;
8351 }
8352
8354 TH1D hparent;
8355 // we cannot have afunc1 and func2 both True
8356 if (afunc1 || esum1 > esum2 ) h1->Copy(hparent);
8357 else h2->Copy(hparent);
8358
8359 // copy h1Expt from h1 and h2. It is just needed to get the correct binning
8360
8361
8362 if (hparent.GetMinimum() < 0.0) {
8363 // we need to create a new histogram
8364 // With negative bins we can't draw random samples in a meaningful way.
8365 Warning("KolmogorovTest", "Detected bins with negative weights, these have been ignored and output might be "
8366 "skewed. Reduce number of bins for histogram?");
8367 while (hparent.GetMinimum() < 0.0) {
8368 Int_t idx = hparent.GetMinimumBin();
8369 hparent.SetBinContent(idx, 0.0);
8370 }
8371 }
8372
8373 // make nEXPT experiments (this should be a parameter)
8374 prb3 = 0;
8375 TH1D h1Expt;
8376 h1->Copy(h1Expt);
8377 TH1D h2Expt;
8378 h1->Copy(h2Expt);
8379 // loop on pseudoexperients and generate the two histograms h1Expt and h2Expt according to the
8380 // parent distribution. In case the parent distribution is not an histogram but a function randomize only one
8381 // histogram
8382 for (Int_t i=0; i < nEXPT; i++) {
8383 if (!afunc1) {
8384 h1Expt.Reset();
8385 h1Expt.FillRandom(&hparent, (Int_t)esum1);
8386 }
8387 if (!afunc2) {
8388 h2Expt.Reset();
8389 h2Expt.FillRandom(&hparent, (Int_t)esum2);
8390 }
8391 // note we cannot have both afunc1 and afunc2 to be true
8392 if (afunc1)
8393 dSEXPT = hparent.KolmogorovTest(&h2Expt,"M");
8394 else if (afunc2)
8395 dSEXPT = hparent.KolmogorovTest(&h1Expt,"M");
8396 else
8397 dSEXPT = h1Expt.KolmogorovTest(&h2Expt,"M");
8398 // count number of cases toy KS distance (TS) is larger than oberved one
8399 if (dSEXPT>dfmax) prb3 += 1.0;
8400 }
8401 // compute p-value
8402 prb3 /= (Double_t)nEXPT;
8403 }
8404
8405
8406 // debug printout
8407 if (opt.Contains("D")) {
8408 printf(" Kolmo Prob h1 = %s, sum bin content =%g effective entries =%g\n",h1->GetName(),sum1,esum1);
8409 printf(" Kolmo Prob h2 = %s, sum bin content =%g effective entries =%g\n",h2->GetName(),sum2,esum2);
8410 printf(" Kolmo Prob = %g, Max Dist = %g\n",prob,dfmax);
8411 if (opt.Contains("N"))
8412 printf(" Kolmo Prob = %f for shape alone, =%f for normalisation alone\n",prb1,prb2);
8413 if (opt.Contains("X"))
8414 printf(" Kolmo Prob = %f with %d pseudo-experiments\n",prb3,nEXPT);
8415 }
8416 // This numerical error condition should never occur:
8417 if (TMath::Abs(rsum1-1) > 0.002) Warning("KolmogorovTest","Numerical problems with h1=%s\n",h1->GetName());
8418 if (TMath::Abs(rsum2-1) > 0.002) Warning("KolmogorovTest","Numerical problems with h2=%s\n",h2->GetName());
8419
8420 if(opt.Contains("M")) return dfmax;
8421 else if(opt.Contains("X")) return prb3;
8422 else return prob;
8423}
8424
8425////////////////////////////////////////////////////////////////////////////////
8426/// Replace bin contents by the contents of array content
8427
8428void TH1::SetContent(const Double_t *content)
8429{
8430 fEntries = fNcells;
8431 fTsumw = 0;
8432 for (Int_t i = 0; i < fNcells; ++i) UpdateBinContent(i, content[i]);
8433}
8434
8435////////////////////////////////////////////////////////////////////////////////
8436/// Return contour values into array levels if pointer levels is non zero.
8437///
8438/// The function returns the number of contour levels.
8439/// see GetContourLevel to return one contour only
8440
8442{
8444 if (levels) {
8445 if (nlevels == 0) {
8446 nlevels = 20;
8448 } else {
8450 }
8451 for (Int_t level=0; level<nlevels; level++) levels[level] = fContour.fArray[level];
8452 }
8453 return nlevels;
8454}
8455
8456////////////////////////////////////////////////////////////////////////////////
8457/// Return value of contour number level.
8458/// Use GetContour to return the array of all contour levels
8459
8461{
8462 return (level >= 0 && level < fContour.fN) ? fContour.fArray[level] : 0.0;
8463}
8464
8465////////////////////////////////////////////////////////////////////////////////
8466/// Return the value of contour number "level" in Pad coordinates.
8467/// ie: if the Pad is in log scale along Z it returns le log of the contour level
8468/// value. See GetContour to return the array of all contour levels
8469
8471{
8472 if (level <0 || level >= fContour.fN) return 0;
8473 Double_t zlevel = fContour.fArray[level];
8474
8475 // In case of user defined contours and Pad in log scale along Z,
8476 // fContour.fArray doesn't contain the log of the contour whereas it does
8477 // in case of equidistant contours.
8478 if (gPad && gPad->GetLogz() && TestBit(kUserContour)) {
8479 if (zlevel <= 0) return 0;
8481 }
8482 return zlevel;
8483}
8484
8485////////////////////////////////////////////////////////////////////////////////
8486/// Set the maximum number of entries to be kept in the buffer.
8487
8488void TH1::SetBuffer(Int_t bufsize, Option_t * /*option*/)
8489{
8490 if (fBuffer) {
8491 BufferEmpty();
8492 delete [] fBuffer;
8493 fBuffer = nullptr;
8494 }
8495 if (bufsize <= 0) {
8496 fBufferSize = 0;
8497 return;
8498 }
8499 if (bufsize < 100) bufsize = 100;
8500 fBufferSize = 1 + bufsize*(fDimension+1);
8502 memset(fBuffer, 0, sizeof(Double_t)*fBufferSize);
8503}
8504
8505////////////////////////////////////////////////////////////////////////////////
8506/// Set the number and values of contour levels.
8507///
8508/// By default the number of contour levels is set to 20. The contours values
8509/// in the array "levels" should be specified in increasing order.
8510///
8511/// if argument levels = 0 or missing, equidistant contours are computed
8512
8514{
8515 Int_t level;
8517 if (nlevels <=0 ) {
8518 fContour.Set(0);
8519 return;
8520 }
8522
8523 // - Contour levels are specified
8524 if (levels) {
8526 for (level=0; level<nlevels; level++) fContour.fArray[level] = levels[level];
8527 } else {
8528 // - contour levels are computed automatically as equidistant contours
8529 Double_t zmin = GetMinimum();
8530 Double_t zmax = GetMaximum();
8531 if ((zmin == zmax) && (zmin != 0)) {
8532 zmax += 0.01*TMath::Abs(zmax);
8533 zmin -= 0.01*TMath::Abs(zmin);
8534 }
8535 Double_t dz = (zmax-zmin)/Double_t(nlevels);
8536 if (gPad && gPad->GetLogz()) {
8537 if (zmax <= 0) return;
8538 if (zmin <= 0) zmin = 0.001*zmax;
8539 zmin = TMath::Log10(zmin);
8540 zmax = TMath::Log10(zmax);
8541 dz = (zmax-zmin)/Double_t(nlevels);
8542 }
8543 for (level=0; level<nlevels; level++) {
8544 fContour.fArray[level] = zmin + dz*Double_t(level);
8545 }
8546 }
8547}
8548
8549////////////////////////////////////////////////////////////////////////////////
8550/// Set value for one contour level.
8551
8553{
8554 if (level < 0 || level >= fContour.fN) return;
8556 fContour.fArray[level] = value;
8557}
8558
8559////////////////////////////////////////////////////////////////////////////////
8560/// Return maximum value smaller than maxval of bins in the range,
8561/// unless the value has been overridden by TH1::SetMaximum,
8562/// in which case it returns that value. This happens, for example,
8563/// when the histogram is drawn and the y or z axis limits are changed
8564///
8565/// To get the maximum value of bins in the histogram regardless of
8566/// whether the value has been overridden (using TH1::SetMaximum), use
8567///
8568/// ~~~ {.cpp}
8569/// h->GetBinContent(h->GetMaximumBin())
8570/// ~~~
8571///
8572/// TH1::GetMaximumBin can be used to get the location of the maximum
8573/// value.
8574
8576{
8577 if (fMaximum != -1111) return fMaximum;
8578
8579 // empty the buffer
8580 if (fBuffer) ((TH1*)this)->BufferEmpty();
8581
8582 Int_t bin, binx, biny, binz;
8583 Int_t xfirst = fXaxis.GetFirst();
8584 Int_t xlast = fXaxis.GetLast();
8585 Int_t yfirst = fYaxis.GetFirst();
8586 Int_t ylast = fYaxis.GetLast();
8587 Int_t zfirst = fZaxis.GetFirst();
8588 Int_t zlast = fZaxis.GetLast();
8590 for (binz=zfirst;binz<=zlast;binz++) {
8591 for (biny=yfirst;biny<=ylast;biny++) {
8592 for (binx=xfirst;binx<=xlast;binx++) {
8593 bin = GetBin(binx,biny,binz);
8595 if (value > maximum && value < maxval) maximum = value;
8596 }
8597 }
8598 }
8599 return maximum;
8600}
8601
8602////////////////////////////////////////////////////////////////////////////////
8603/// Return location of bin with maximum value in the range.
8604///
8605/// TH1::GetMaximum can be used to get the maximum value.
8606
8608{
8611}
8612
8613////////////////////////////////////////////////////////////////////////////////
8614/// Return location of bin with maximum value in the range.
8615
8617{
8618 // empty the buffer
8619 if (fBuffer) ((TH1*)this)->BufferEmpty();
8620
8621 Int_t bin, binx, biny, binz;
8622 Int_t locm;
8623 Int_t xfirst = fXaxis.GetFirst();
8624 Int_t xlast = fXaxis.GetLast();
8625 Int_t yfirst = fYaxis.GetFirst();
8626 Int_t ylast = fYaxis.GetLast();
8627 Int_t zfirst = fZaxis.GetFirst();
8628 Int_t zlast = fZaxis.GetLast();
8630 locm = locmax = locmay = locmaz = 0;
8631 for (binz=zfirst;binz<=zlast;binz++) {
8632 for (biny=yfirst;biny<=ylast;biny++) {
8633 for (binx=xfirst;binx<=xlast;binx++) {
8634 bin = GetBin(binx,biny,binz);
8636 if (value > maximum) {
8637 maximum = value;
8638 locm = bin;
8639 locmax = binx;
8640 locmay = biny;
8641 locmaz = binz;
8642 }
8643 }
8644 }
8645 }
8646 return locm;
8647}
8648
8649////////////////////////////////////////////////////////////////////////////////
8650/// Return minimum value larger than minval of bins in the range,
8651/// unless the value has been overridden by TH1::SetMinimum,
8652/// in which case it returns that value. This happens, for example,
8653/// when the histogram is drawn and the y or z axis limits are changed
8654///
8655/// To get the minimum value of bins in the histogram regardless of
8656/// whether the value has been overridden (using TH1::SetMinimum), use
8657///
8658/// ~~~ {.cpp}
8659/// h->GetBinContent(h->GetMinimumBin())
8660/// ~~~
8661///
8662/// TH1::GetMinimumBin can be used to get the location of the
8663/// minimum value.
8664
8666{
8667 if (fMinimum != -1111) return fMinimum;
8668
8669 // empty the buffer
8670 if (fBuffer) ((TH1*)this)->BufferEmpty();
8671
8672 Int_t bin, binx, biny, binz;
8673 Int_t xfirst = fXaxis.GetFirst();
8674 Int_t xlast = fXaxis.GetLast();
8675 Int_t yfirst = fYaxis.GetFirst();
8676 Int_t ylast = fYaxis.GetLast();
8677 Int_t zfirst = fZaxis.GetFirst();
8678 Int_t zlast = fZaxis.GetLast();
8680 for (binz=zfirst;binz<=zlast;binz++) {
8681 for (biny=yfirst;biny<=ylast;biny++) {
8682 for (binx=xfirst;binx<=xlast;binx++) {
8683 bin = GetBin(binx,biny,binz);
8686 }
8687 }
8688 }
8689 return minimum;
8690}
8691
8692////////////////////////////////////////////////////////////////////////////////
8693/// Return location of bin with minimum value in the range.
8694
8696{
8699}
8700
8701////////////////////////////////////////////////////////////////////////////////
8702/// Return location of bin with minimum value in the range.
8703
8705{
8706 // empty the buffer
8707 if (fBuffer) ((TH1*)this)->BufferEmpty();
8708
8709 Int_t bin, binx, biny, binz;
8710 Int_t locm;
8711 Int_t xfirst = fXaxis.GetFirst();
8712 Int_t xlast = fXaxis.GetLast();
8713 Int_t yfirst = fYaxis.GetFirst();
8714 Int_t ylast = fYaxis.GetLast();
8715 Int_t zfirst = fZaxis.GetFirst();
8716 Int_t zlast = fZaxis.GetLast();
8718 locm = locmix = locmiy = locmiz = 0;
8719 for (binz=zfirst;binz<=zlast;binz++) {
8720 for (biny=yfirst;biny<=ylast;biny++) {
8721 for (binx=xfirst;binx<=xlast;binx++) {
8722 bin = GetBin(binx,biny,binz);
8724 if (value < minimum) {
8725 minimum = value;
8726 locm = bin;
8727 locmix = binx;
8728 locmiy = biny;
8729 locmiz = binz;
8730 }
8731 }
8732 }
8733 }
8734 return locm;
8735}
8736
8737///////////////////////////////////////////////////////////////////////////////
8738/// Retrieve the minimum and maximum values in the histogram
8739///
8740/// This will not return a cached value and will always search the
8741/// histogram for the min and max values. The user can condition whether
8742/// or not to call this with the GetMinimumStored() and GetMaximumStored()
8743/// methods. If the cache is empty, then the value will be -1111. Users
8744/// can then use the SetMinimum() or SetMaximum() methods to cache the results.
8745/// For example, the following recipe will make efficient use of this method
8746/// and the cached minimum and maximum values.
8747//
8748/// \code{.cpp}
8749/// Double_t currentMin = pHist->GetMinimumStored();
8750/// Double_t currentMax = pHist->GetMaximumStored();
8751/// if ((currentMin == -1111) || (currentMax == -1111)) {
8752/// pHist->GetMinimumAndMaximum(currentMin, currentMax);
8753/// pHist->SetMinimum(currentMin);
8754/// pHist->SetMaximum(currentMax);
8755/// }
8756/// \endcode
8757///
8758/// \param min reference to variable that will hold found minimum value
8759/// \param max reference to variable that will hold found maximum value
8760
8761void TH1::GetMinimumAndMaximum(Double_t& min, Double_t& max) const
8762{
8763 // empty the buffer
8764 if (fBuffer) ((TH1*)this)->BufferEmpty();
8765
8766 Int_t bin, binx, biny, binz;
8767 Int_t xfirst = fXaxis.GetFirst();
8768 Int_t xlast = fXaxis.GetLast();
8769 Int_t yfirst = fYaxis.GetFirst();
8770 Int_t ylast = fYaxis.GetLast();
8771 Int_t zfirst = fZaxis.GetFirst();
8772 Int_t zlast = fZaxis.GetLast();
8773 min=TMath::Infinity();
8774 max=-TMath::Infinity();
8776 for (binz=zfirst;binz<=zlast;binz++) {
8777 for (biny=yfirst;biny<=ylast;biny++) {
8778 for (binx=xfirst;binx<=xlast;binx++) {
8779 bin = GetBin(binx,biny,binz);
8781 if (value < min) min = value;
8782 if (value > max) max = value;
8783 }
8784 }
8785 }
8786}
8787
8788////////////////////////////////////////////////////////////////////////////////
8789/// Redefine x axis parameters.
8790///
8791/// The X axis parameters are modified.
8792/// The bins content array is resized
8793/// if errors (Sumw2) the errors array is resized
8794/// The previous bin contents are lost
8795/// To change only the axis limits, see TAxis::SetRange
8796
8798{
8799 if (GetDimension() != 1) {
8800 Error("SetBins","Operation only valid for 1-d histograms");
8801 return;
8802 }
8803 fXaxis.SetRange(0,0);
8805 fYaxis.Set(1,0,1);
8806 fZaxis.Set(1,0,1);
8807 fNcells = nx+2;
8809 if (fSumw2.fN) {
8811 }
8812}
8813
8814////////////////////////////////////////////////////////////////////////////////
8815/// Redefine x axis parameters with variable bin sizes.
8816///
8817/// The X axis parameters are modified.
8818/// The bins content array is resized
8819/// if errors (Sumw2) the errors array is resized
8820/// The previous bin contents are lost
8821/// To change only the axis limits, see TAxis::SetRange
8822/// xBins is supposed to be of length nx+1
8823
8824void TH1::SetBins(Int_t nx, const Double_t *xBins)
8825{
8826 if (GetDimension() != 1) {
8827 Error("SetBins","Operation only valid for 1-d histograms");
8828 return;
8829 }
8830 fXaxis.SetRange(0,0);
8831 fXaxis.Set(nx,xBins);
8832 fYaxis.Set(1,0,1);
8833 fZaxis.Set(1,0,1);
8834 fNcells = nx+2;
8836 if (fSumw2.fN) {
8838 }
8839}
8840
8841////////////////////////////////////////////////////////////////////////////////
8842/// Redefine x and y axis parameters.
8843///
8844/// The X and Y axis parameters are modified.
8845/// The bins content array is resized
8846/// if errors (Sumw2) the errors array is resized
8847/// The previous bin contents are lost
8848/// To change only the axis limits, see TAxis::SetRange
8849
8851{
8852 if (GetDimension() != 2) {
8853 Error("SetBins","Operation only valid for 2-D histograms");
8854 return;
8855 }
8856 fXaxis.SetRange(0,0);
8857 fYaxis.SetRange(0,0);
8860 fZaxis.Set(1,0,1);
8861 fNcells = (nx+2)*(ny+2);
8863 if (fSumw2.fN) {
8865 }
8866}
8867
8868////////////////////////////////////////////////////////////////////////////////
8869/// Redefine x and y axis parameters with variable bin sizes.
8870///
8871/// The X and Y axis parameters are modified.
8872/// The bins content array is resized
8873/// if errors (Sumw2) the errors array is resized
8874/// The previous bin contents are lost
8875/// To change only the axis limits, see TAxis::SetRange
8876/// xBins is supposed to be of length nx+1, yBins is supposed to be of length ny+1
8877
8878void TH1::SetBins(Int_t nx, const Double_t *xBins, Int_t ny, const Double_t *yBins)
8879{
8880 if (GetDimension() != 2) {
8881 Error("SetBins","Operation only valid for 2-D histograms");
8882 return;
8883 }
8884 fXaxis.SetRange(0,0);
8885 fYaxis.SetRange(0,0);
8886 fXaxis.Set(nx,xBins);
8887 fYaxis.Set(ny,yBins);
8888 fZaxis.Set(1,0,1);
8889 fNcells = (nx+2)*(ny+2);
8891 if (fSumw2.fN) {
8893 }
8894}
8895
8896////////////////////////////////////////////////////////////////////////////////
8897/// Redefine x, y and z axis parameters.
8898///
8899/// The X, Y and Z axis parameters are modified.
8900/// The bins content array is resized
8901/// if errors (Sumw2) the errors array is resized
8902/// The previous bin contents are lost
8903/// To change only the axis limits, see TAxis::SetRange
8904
8906{
8907 if (GetDimension() != 3) {
8908 Error("SetBins","Operation only valid for 3-D histograms");
8909 return;
8910 }
8911 fXaxis.SetRange(0,0);
8912 fYaxis.SetRange(0,0);
8913 fZaxis.SetRange(0,0);
8916 fZaxis.Set(nz,zmin,zmax);
8917 fNcells = (nx+2)*(ny+2)*(nz+2);
8919 if (fSumw2.fN) {
8921 }
8922}
8923
8924////////////////////////////////////////////////////////////////////////////////
8925/// Redefine x, y and z axis parameters with variable bin sizes.
8926///
8927/// The X, Y and Z axis parameters are modified.
8928/// The bins content array is resized
8929/// if errors (Sumw2) the errors array is resized
8930/// The previous bin contents are lost
8931/// To change only the axis limits, see TAxis::SetRange
8932/// xBins is supposed to be of length nx+1, yBins is supposed to be of length ny+1,
8933/// zBins is supposed to be of length nz+1
8934
8935void TH1::SetBins(Int_t nx, const Double_t *xBins, Int_t ny, const Double_t *yBins, Int_t nz, const Double_t *zBins)
8936{
8937 if (GetDimension() != 3) {
8938 Error("SetBins","Operation only valid for 3-D histograms");
8939 return;
8940 }
8941 fXaxis.SetRange(0,0);
8942 fYaxis.SetRange(0,0);
8943 fZaxis.SetRange(0,0);
8944 fXaxis.Set(nx,xBins);
8945 fYaxis.Set(ny,yBins);
8946 fZaxis.Set(nz,zBins);
8947 fNcells = (nx+2)*(ny+2)*(nz+2);
8949 if (fSumw2.fN) {
8951 }
8952}
8953
8954////////////////////////////////////////////////////////////////////////////////
8955/// By default, when a histogram is created, it is added to the list
8956/// of histogram objects in the current directory in memory.
8957/// Remove reference to this histogram from current directory and add
8958/// reference to new directory dir. dir can be 0 in which case the
8959/// histogram does not belong to any directory.
8960///
8961/// Note that the directory is not a real property of the histogram and
8962/// it will not be copied when the histogram is copied or cloned.
8963/// If the user wants to have the copied (cloned) histogram in the same
8964/// directory, he needs to set again the directory using SetDirectory to the
8965/// copied histograms
8966
8968{
8969 if (fDirectory == dir) return;
8970 if (fDirectory) fDirectory->Remove(this);
8971 fDirectory = dir;
8972 if (fDirectory) {
8974 fDirectory->Append(this);
8975 }
8976}
8977
8978////////////////////////////////////////////////////////////////////////////////
8979/// Replace bin errors by values in array error.
8980
8981void TH1::SetError(const Double_t *error)
8982{
8983 for (Int_t i = 0; i < fNcells; ++i) SetBinError(i, error[i]);
8984}
8985
8986////////////////////////////////////////////////////////////////////////////////
8987/// Change the name of this histogram
8989
8990void TH1::SetName(const char *name)
8991{
8992 // Histograms are named objects in a THashList.
8993 // We must update the hashlist if we change the name
8994 // We protect this operation
8996 if (fDirectory) fDirectory->Remove(this);
8997 fName = name;
8998 if (fDirectory) fDirectory->Append(this);
8999}
9000
9001////////////////////////////////////////////////////////////////////////////////
9002/// Change the name and title of this histogram
9003
9004void TH1::SetNameTitle(const char *name, const char *title)
9005{
9006 // Histograms are named objects in a THashList.
9007 // We must update the hashlist if we change the name
9008 SetName(name);
9009 SetTitle(title);
9010}
9011
9012////////////////////////////////////////////////////////////////////////////////
9013/// Set statistics option on/off.
9014///
9015/// By default, the statistics box is drawn.
9016/// The paint options can be selected via gStyle->SetOptStat.
9017/// This function sets/resets the kNoStats bit in the histogram object.
9018/// It has priority over the Style option.
9019
9021{
9023 if (!stats) {
9025 //remove the "stats" object from the list of functions
9026 if (fFunctions) {
9027 TObject *obj = fFunctions->FindObject("stats");
9028 if (obj) {
9029 fFunctions->Remove(obj);
9030 delete obj;
9031 }
9032 }
9033 }
9034}
9035
9036////////////////////////////////////////////////////////////////////////////////
9037/// Create structure to store sum of squares of weights.
9038///
9039/// if histogram is already filled, the sum of squares of weights
9040/// is filled with the existing bin contents
9041///
9042/// The error per bin will be computed as sqrt(sum of squares of weight)
9043/// for each bin.
9044///
9045/// This function is automatically called when the histogram is created
9046/// if the static function TH1::SetDefaultSumw2 has been called before.
9047/// If flag = false the structure containing the sum of the square of weights
9048/// is rest and it will be empty, but it is not deleted (i.e. GetSumw2()->fN = 0)
9049
9051{
9052 if (!flag) {
9053 // clear the array if existing - do nothing otherwise
9054 if (fSumw2.fN > 0 ) fSumw2.Set(0);
9055 return;
9056 }
9057
9058 if (fSumw2.fN == fNcells) {
9059 if (!fgDefaultSumw2 )
9060 Warning("Sumw2","Sum of squares of weights structure already created");
9061 return;
9062 }
9063
9065
9066 if (fEntries > 0)
9067 for (Int_t i = 0; i < fNcells; ++i)
9069}
9070
9071////////////////////////////////////////////////////////////////////////////////
9072/// Return pointer to function with name.
9073///
9074///
9075/// Functions such as TH1::Fit store the fitted function in the list of
9076/// functions of this histogram.
9077
9078TF1 *TH1::GetFunction(const char *name) const
9079{
9080 return (TF1*)fFunctions->FindObject(name);
9081}
9082
9083////////////////////////////////////////////////////////////////////////////////
9084/// Return value of error associated to bin number bin.
9085///
9086/// if the sum of squares of weights has been defined (via Sumw2),
9087/// this function returns the sqrt(sum of w2).
9088/// otherwise it returns the sqrt(contents) for this bin.
9089
9091{
9092 if (bin < 0) bin = 0;
9093 if (bin >= fNcells) bin = fNcells-1;
9094 if (fBuffer) ((TH1*)this)->BufferEmpty();
9095 if (fSumw2.fN) return TMath::Sqrt(fSumw2.fArray[bin]);
9096
9098}
9099
9100////////////////////////////////////////////////////////////////////////////////
9101/// Return lower error associated to bin number bin.
9102///
9103/// The error will depend on the statistic option used will return
9104/// the binContent - lower interval value
9105
9107{
9108 if (fBinStatErrOpt == kNormal) return GetBinError(bin);
9109 // in case of weighted histogram check if it is really weighted
9110 if (fSumw2.fN && fTsumw != fTsumw2) return GetBinError(bin);
9111
9112 if (bin < 0) bin = 0;
9113 if (bin >= fNcells) bin = fNcells-1;
9114 if (fBuffer) ((TH1*)this)->BufferEmpty();
9115
9116 Double_t alpha = 1.- 0.682689492;
9117 if (fBinStatErrOpt == kPoisson2) alpha = 0.05;
9118
9120 Int_t n = int(c);
9121 if (n < 0) {
9122 Warning("GetBinErrorLow","Histogram has negative bin content-force usage to normal errors");
9123 ((TH1*)this)->fBinStatErrOpt = kNormal;
9124 return GetBinError(bin);
9125 }
9126
9127 if (n == 0) return 0;
9128 return c - ROOT::Math::gamma_quantile( alpha/2, n, 1.);
9129}
9130
9131////////////////////////////////////////////////////////////////////////////////
9132/// Return upper error associated to bin number bin.
9133///
9134/// The error will depend on the statistic option used will return
9135/// the binContent - upper interval value
9136
9138{
9139 if (fBinStatErrOpt == kNormal) return GetBinError(bin);
9140 // in case of weighted histogram check if it is really weighted
9141 if (fSumw2.fN && fTsumw != fTsumw2) return GetBinError(bin);
9142 if (bin < 0) bin = 0;
9143 if (bin >= fNcells) bin = fNcells-1;
9144 if (fBuffer) ((TH1*)this)->BufferEmpty();
9145
9146 Double_t alpha = 1.- 0.682689492;
9147 if (fBinStatErrOpt == kPoisson2) alpha = 0.05;
9148
9150 Int_t n = int(c);
9151 if (n < 0) {
9152 Warning("GetBinErrorUp","Histogram has negative bin content-force usage to normal errors");
9153 ((TH1*)this)->fBinStatErrOpt = kNormal;
9154 return GetBinError(bin);
9155 }
9156
9157 // for N==0 return an upper limit at 0.68 or (1-alpha)/2 ?
9158 // decide to return always (1-alpha)/2 upper interval
9159 //if (n == 0) return ROOT::Math::gamma_quantile_c(alpha,n+1,1);
9160 return ROOT::Math::gamma_quantile_c( alpha/2, n+1, 1) - c;
9161}
9162
9163//L.M. These following getters are useless and should be probably deprecated
9164////////////////////////////////////////////////////////////////////////////////
9165/// Return bin center for 1D histogram.
9166/// Better to use h1.GetXaxis()->GetBinCenter(bin)
9167
9169{
9170 if (fDimension == 1) return fXaxis.GetBinCenter(bin);
9171 Error("GetBinCenter","Invalid method for a %d-d histogram - return a NaN",fDimension);
9172 return TMath::QuietNaN();
9173}
9174
9175////////////////////////////////////////////////////////////////////////////////
9176/// Return bin lower edge for 1D histogram.
9177/// Better to use h1.GetXaxis()->GetBinLowEdge(bin)
9178
9180{
9181 if (fDimension == 1) return fXaxis.GetBinLowEdge(bin);
9182 Error("GetBinLowEdge","Invalid method for a %d-d histogram - return a NaN",fDimension);
9183 return TMath::QuietNaN();
9184}
9185
9186////////////////////////////////////////////////////////////////////////////////
9187/// Return bin width for 1D histogram.
9188/// Better to use h1.GetXaxis()->GetBinWidth(bin)
9189
9191{
9192 if (fDimension == 1) return fXaxis.GetBinWidth(bin);
9193 Error("GetBinWidth","Invalid method for a %d-d histogram - return a NaN",fDimension);
9194 return TMath::QuietNaN();
9195}
9196
9197////////////////////////////////////////////////////////////////////////////////
9198/// Fill array with center of bins for 1D histogram
9199/// Better to use h1.GetXaxis()->GetCenter(center)
9200
9201void TH1::GetCenter(Double_t *center) const
9202{
9203 if (fDimension == 1) {
9204 fXaxis.GetCenter(center);
9205 return;
9206 }
9207 Error("GetCenter","Invalid method for a %d-d histogram ",fDimension);
9208}
9209
9210////////////////////////////////////////////////////////////////////////////////
9211/// Fill array with low edge of bins for 1D histogram
9212/// Better to use h1.GetXaxis()->GetLowEdge(edge)
9213
9214void TH1::GetLowEdge(Double_t *edge) const
9215{
9216 if (fDimension == 1) {
9218 return;
9219 }
9220 Error("GetLowEdge","Invalid method for a %d-d histogram ",fDimension);
9221}
9222
9223////////////////////////////////////////////////////////////////////////////////
9224/// Set the bin Error
9225/// Note that this resets the bin eror option to be of Normal Type and for the
9226/// non-empty bin the bin error is set by default to the square root of their content.
9227/// Note that in case the user sets after calling SetBinError explicitly a new bin content (e.g. using SetBinContent)
9228/// he needs then to provide also the corresponding bin error (using SetBinError) since the bin error
9229/// will not be recalculated after setting the content and a default error = 0 will be used for those bins.
9230///
9231/// See convention for numbering bins in TH1::GetBin
9232
9233void TH1::SetBinError(Int_t bin, Double_t error)
9234{
9235 if (bin < 0 || bin>= fNcells) return;
9236 if (!fSumw2.fN) Sumw2();
9237 fSumw2.fArray[bin] = error * error;
9238 // reset the bin error option
9240}
9241
9242////////////////////////////////////////////////////////////////////////////////
9243/// Set bin content
9244/// see convention for numbering bins in TH1::GetBin
9245/// In case the bin number is greater than the number of bins and
9246/// the timedisplay option is set or CanExtendAllAxes(),
9247/// the number of bins is automatically doubled to accommodate the new bin
9248
9250{
9251 fEntries++;
9252 fTsumw = 0;
9253 if (bin < 0) return;
9254 if (bin >= fNcells-1) {
9256 while (bin >= fNcells-1) LabelsInflate();
9257 } else {
9258 if (bin == fNcells-1) UpdateBinContent(bin, content);
9259 return;
9260 }
9261 }
9263}
9264
9265////////////////////////////////////////////////////////////////////////////////
9266/// See convention for numbering bins in TH1::GetBin
9267
9269{
9270 if (binx < 0 || binx > fXaxis.GetNbins() + 1) return;
9271 if (biny < 0 || biny > fYaxis.GetNbins() + 1) return;
9272 SetBinError(GetBin(binx, biny), error);
9273}
9274
9275////////////////////////////////////////////////////////////////////////////////
9276/// See convention for numbering bins in TH1::GetBin
9277
9279{
9280 if (binx < 0 || binx > fXaxis.GetNbins() + 1) return;
9281 if (biny < 0 || biny > fYaxis.GetNbins() + 1) return;
9282 if (binz < 0 || binz > fZaxis.GetNbins() + 1) return;
9283 SetBinError(GetBin(binx, biny, binz), error);
9284}
9285
9286////////////////////////////////////////////////////////////////////////////////
9287/// This function calculates the background spectrum in this histogram.
9288/// The background is returned as a histogram.
9289///
9290/// \param[in] niter number of iterations (default value = 2)
9291/// Increasing niter make the result smoother and lower.
9292/// \param[in] option may contain one of the following options
9293/// - to set the direction parameter
9294/// "BackDecreasingWindow". By default the direction is BackIncreasingWindow
9295/// - filterOrder-order of clipping filter (default "BackOrder2")
9296/// possible values= "BackOrder4" "BackOrder6" "BackOrder8"
9297/// - "nosmoothing" - if selected, the background is not smoothed
9298/// By default the background is smoothed.
9299/// - smoothWindow - width of smoothing window, (default is "BackSmoothing3")
9300/// possible values= "BackSmoothing5" "BackSmoothing7" "BackSmoothing9"
9301/// "BackSmoothing11" "BackSmoothing13" "BackSmoothing15"
9302/// - "nocompton" - if selected the estimation of Compton edge
9303/// will be not be included (by default the compton estimation is set)
9304/// - "same" if this option is specified, the resulting background
9305/// histogram is superimposed on the picture in the current pad.
9306/// This option is given by default.
9307///
9308/// NOTE that the background is only evaluated in the current range of this histogram.
9309/// i.e., if this has a bin range (set via h->GetXaxis()->SetRange(binmin, binmax),
9310/// the returned histogram will be created with the same number of bins
9311/// as this input histogram, but only bins from binmin to binmax will be filled
9312/// with the estimated background.
9313
9315{
9316 return (TH1*)gROOT->ProcessLineFast(TString::Format("TSpectrum::StaticBackground((TH1*)0x%zx,%d,\"%s\")",
9317 (size_t)this, niter, option).Data());
9318}
9319
9320////////////////////////////////////////////////////////////////////////////////
9321/// Interface to TSpectrum::Search.
9322/// The function finds peaks in this histogram where the width is > sigma
9323/// and the peak maximum greater than threshold*maximum bin content of this.
9324/// For more details see TSpectrum::Search.
9325/// Note the difference in the default value for option compared to TSpectrum::Search
9326/// option="" by default (instead of "goff").
9327
9329{
9330 return (Int_t)gROOT->ProcessLineFast(TString::Format("TSpectrum::StaticSearch((TH1*)0x%zx,%g,\"%s\",%g)",
9331 (size_t)this, sigma, option, threshold).Data());
9332}
9333
9334////////////////////////////////////////////////////////////////////////////////
9335/// For a given transform (first parameter), fills the histogram (second parameter)
9336/// with the transform output data, specified in the third parameter
9337/// If the 2nd parameter h_output is empty, a new histogram (TH1D or TH2D) is created
9338/// and the user is responsible for deleting it.
9339///
9340/// Available options:
9341/// - "RE" - real part of the output
9342/// - "IM" - imaginary part of the output
9343/// - "MAG" - magnitude of the output
9344/// - "PH" - phase of the output
9345
9347{
9348 if (!fft || !fft->GetN() ) {
9349 ::Error("TransformHisto","Invalid FFT transform class");
9350 return nullptr;
9351 }
9352
9353 if (fft->GetNdim()>2){
9354 ::Error("TransformHisto","Only 1d and 2D transform are supported");
9355 return nullptr;
9356 }
9357 Int_t binx,biny;
9358 TString opt = option;
9359 opt.ToUpper();
9360 Int_t *n = fft->GetN();
9361 TH1 *hout=nullptr;
9362 if (h_output) {
9363 hout = h_output;
9364 }
9365 else {
9366 TString name = TString::Format("out_%s", opt.Data());
9367 if (fft->GetNdim()==1)
9368 hout = new TH1D(name, name,n[0], 0, n[0]);
9369 else if (fft->GetNdim()==2)
9370 hout = new TH2D(name, name, n[0], 0, n[0], n[1], 0, n[1]);
9371 }
9372 R__ASSERT(hout != nullptr);
9373 TString type=fft->GetType();
9374 Int_t ind[2];
9375 if (opt.Contains("RE")){
9376 if (type.Contains("2C") || type.Contains("2HC")) {
9377 Double_t re, im;
9378 for (binx = 1; binx<=hout->GetNbinsX(); binx++) {
9379 for (biny=1; biny<=hout->GetNbinsY(); biny++) {
9380 ind[0] = binx-1; ind[1] = biny-1;
9381 fft->GetPointComplex(ind, re, im);
9382 hout->SetBinContent(binx, biny, re);
9383 }
9384 }
9385 } else {
9386 for (binx = 1; binx<=hout->GetNbinsX(); binx++) {
9387 for (biny=1; biny<=hout->GetNbinsY(); biny++) {
9388 ind[0] = binx-1; ind[1] = biny-1;
9389 hout->SetBinContent(binx, biny, fft->GetPointReal(ind));
9390 }
9391 }
9392 }
9393 }
9394 if (opt.Contains("IM")) {
9395 if (type.Contains("2C") || type.Contains("2HC")) {
9396 Double_t re, im;
9397 for (binx = 1; binx<=hout->GetNbinsX(); binx++) {
9398 for (biny=1; biny<=hout->GetNbinsY(); biny++) {
9399 ind[0] = binx-1; ind[1] = biny-1;
9400 fft->GetPointComplex(ind, re, im);
9401 hout->SetBinContent(binx, biny, im);
9402 }
9403 }
9404 } else {
9405 ::Error("TransformHisto","No complex numbers in the output");
9406 return nullptr;
9407 }
9408 }
9409 if (opt.Contains("MA")) {
9410 if (type.Contains("2C") || type.Contains("2HC")) {
9411 Double_t re, im;
9412 for (binx = 1; binx<=hout->GetNbinsX(); binx++) {
9413 for (biny=1; biny<=hout->GetNbinsY(); biny++) {
9414 ind[0] = binx-1; ind[1] = biny-1;
9415 fft->GetPointComplex(ind, re, im);
9416 hout->SetBinContent(binx, biny, TMath::Sqrt(re*re + im*im));
9417 }
9418 }
9419 } else {
9420 for (binx = 1; binx<=hout->GetNbinsX(); binx++) {
9421 for (biny=1; biny<=hout->GetNbinsY(); biny++) {
9422 ind[0] = binx-1; ind[1] = biny-1;
9423 hout->SetBinContent(binx, biny, TMath::Abs(fft->GetPointReal(ind)));
9424 }
9425 }
9426 }
9427 }
9428 if (opt.Contains("PH")) {
9429 if (type.Contains("2C") || type.Contains("2HC")){
9430 Double_t re, im, ph;
9431 for (binx = 1; binx<=hout->GetNbinsX(); binx++){
9432 for (biny=1; biny<=hout->GetNbinsY(); biny++){
9433 ind[0] = binx-1; ind[1] = biny-1;
9434 fft->GetPointComplex(ind, re, im);
9435 if (TMath::Abs(re) > 1e-13){
9436 ph = TMath::ATan(im/re);
9437 //find the correct quadrant
9438 if (re<0 && im<0)
9439 ph -= TMath::Pi();
9440 if (re<0 && im>=0)
9441 ph += TMath::Pi();
9442 } else {
9443 if (TMath::Abs(im) < 1e-13)
9444 ph = 0;
9445 else if (im>0)
9446 ph = TMath::Pi()*0.5;
9447 else
9448 ph = -TMath::Pi()*0.5;
9449 }
9450 hout->SetBinContent(binx, biny, ph);
9451 }
9452 }
9453 } else {
9454 printf("Pure real output, no phase");
9455 return nullptr;
9456 }
9457 }
9458
9459 return hout;
9460}
9461
9462////////////////////////////////////////////////////////////////////////////////
9463/// Print value overload
9464
9465std::string cling::printValue(TH1 *val) {
9466 std::ostringstream strm;
9467 strm << cling::printValue((TObject*)val) << " NbinsX: " << val->GetNbinsX();
9468 return strm.str();
9469}
9470
9471//______________________________________________________________________________
9472// TH1C methods
9473// TH1C : histograms with one byte per channel. Maximum bin content = 127
9474//______________________________________________________________________________
9475
9476
9477////////////////////////////////////////////////////////////////////////////////
9478/// Constructor.
9479
9480TH1C::TH1C()
9481{
9482 fDimension = 1;
9483 SetBinsLength(3);
9484 if (fgDefaultSumw2) Sumw2();
9485}
9486
9487////////////////////////////////////////////////////////////////////////////////
9488/// Create a 1-Dim histogram with fix bins of type char (one byte per channel)
9489/// (see TH1::TH1 for explanation of parameters)
9490
9491TH1C::TH1C(const char *name,const char *title,Int_t nbins,Double_t xlow,Double_t xup)
9492: TH1(name,title,nbins,xlow,xup)
9493{
9494 fDimension = 1;
9496
9497 if (xlow >= xup) SetBuffer(fgBufferSize);
9498 if (fgDefaultSumw2) Sumw2();
9499}
9500
9501////////////////////////////////////////////////////////////////////////////////
9502/// Create a 1-Dim histogram with variable bins of type char (one byte per channel)
9503/// (see TH1::TH1 for explanation of parameters)
9504
9505TH1C::TH1C(const char *name,const char *title,Int_t nbins,const Float_t *xbins)
9506: TH1(name,title,nbins,xbins)
9507{
9508 fDimension = 1;
9510 if (fgDefaultSumw2) Sumw2();
9511}
9512
9513////////////////////////////////////////////////////////////////////////////////
9514/// Create a 1-Dim histogram with variable bins of type char (one byte per channel)
9515/// (see TH1::TH1 for explanation of parameters)
9516
9517TH1C::TH1C(const char *name,const char *title,Int_t nbins,const Double_t *xbins)
9518: TH1(name,title,nbins,xbins)
9519{
9520 fDimension = 1;
9522 if (fgDefaultSumw2) Sumw2();
9523}
9524
9525////////////////////////////////////////////////////////////////////////////////
9526/// Destructor.
9527
9529{
9530}
9531
9532////////////////////////////////////////////////////////////////////////////////
9533/// Copy constructor.
9534/// The list of functions is not copied. (Use Clone() if needed)
9535
9536TH1C::TH1C(const TH1C &h1c) : TH1(), TArrayC()
9537{
9538 h1c.TH1C::Copy(*this);
9539}
9540
9541////////////////////////////////////////////////////////////////////////////////
9542/// Increment bin content by 1.
9543/// Passing an out-of-range bin leads to undefined behavior
9544
9545void TH1C::AddBinContent(Int_t bin)
9546{
9547 if (fArray[bin] < 127) fArray[bin]++;
9548}
9549
9550////////////////////////////////////////////////////////////////////////////////
9551/// Increment bin content by w.
9552/// \warning The value of w is cast to `Int_t` before being added.
9553/// Passing an out-of-range bin leads to undefined behavior
9554
9556{
9557 Int_t newval = fArray[bin] + Int_t(w);
9558 if (newval > -128 && newval < 128) {fArray[bin] = Char_t(newval); return;}
9559 if (newval < -127) fArray[bin] = -127;
9560 if (newval > 127) fArray[bin] = 127;
9561}
9562
9563////////////////////////////////////////////////////////////////////////////////
9564/// Copy this to newth1
9565
9566void TH1C::Copy(TObject &newth1) const
9567{
9569}
9570
9571////////////////////////////////////////////////////////////////////////////////
9572/// Reset.
9573
9575{
9578}
9579
9580////////////////////////////////////////////////////////////////////////////////
9581/// Set total number of bins including under/overflow
9582/// Reallocate bin contents array
9583
9585{
9586 if (n < 0) n = fXaxis.GetNbins() + 2;
9587 fNcells = n;
9588 TArrayC::Set(n);
9589}
9590
9591////////////////////////////////////////////////////////////////////////////////
9592/// Operator =
9593
9594TH1C& TH1C::operator=(const TH1C &h1)
9595{
9596 if (this != &h1)
9597 h1.TH1C::Copy(*this);
9598 return *this;
9599}
9600
9601////////////////////////////////////////////////////////////////////////////////
9602/// Operator *
9603
9605{
9606 TH1C hnew = h1;
9607 hnew.Scale(c1);
9608 hnew.SetDirectory(nullptr);
9609 return hnew;
9610}
9611
9612////////////////////////////////////////////////////////////////////////////////
9613/// Operator +
9614
9615TH1C operator+(const TH1C &h1, const TH1C &h2)
9616{
9617 TH1C hnew = h1;
9618 hnew.Add(&h2,1);
9619 hnew.SetDirectory(nullptr);
9620 return hnew;
9621}
9622
9623////////////////////////////////////////////////////////////////////////////////
9624/// Operator -
9625
9626TH1C operator-(const TH1C &h1, const TH1C &h2)
9627{
9628 TH1C hnew = h1;
9629 hnew.Add(&h2,-1);
9630 hnew.SetDirectory(nullptr);
9631 return hnew;
9632}
9633
9634////////////////////////////////////////////////////////////////////////////////
9635/// Operator *
9636
9637TH1C operator*(const TH1C &h1, const TH1C &h2)
9638{
9639 TH1C hnew = h1;
9640 hnew.Multiply(&h2);
9641 hnew.SetDirectory(nullptr);
9642 return hnew;
9643}
9644
9645////////////////////////////////////////////////////////////////////////////////
9646/// Operator /
9647
9648TH1C operator/(const TH1C &h1, const TH1C &h2)
9649{
9650 TH1C hnew = h1;
9651 hnew.Divide(&h2);
9652 hnew.SetDirectory(nullptr);
9653 return hnew;
9654}
9655
9656//______________________________________________________________________________
9657// TH1S methods
9658// TH1S : histograms with one short per channel. Maximum bin content = 32767
9659//______________________________________________________________________________
9660
9661
9662////////////////////////////////////////////////////////////////////////////////
9663/// Constructor.
9664
9665TH1S::TH1S()
9666{
9667 fDimension = 1;
9668 SetBinsLength(3);
9669 if (fgDefaultSumw2) Sumw2();
9670}
9671
9672////////////////////////////////////////////////////////////////////////////////
9673/// Create a 1-Dim histogram with fix bins of type short
9674/// (see TH1::TH1 for explanation of parameters)
9675
9676TH1S::TH1S(const char *name,const char *title,Int_t nbins,Double_t xlow,Double_t xup)
9677: TH1(name,title,nbins,xlow,xup)
9678{
9679 fDimension = 1;
9681
9682 if (xlow >= xup) SetBuffer(fgBufferSize);
9683 if (fgDefaultSumw2) Sumw2();
9684}
9685
9686////////////////////////////////////////////////////////////////////////////////
9687/// Create a 1-Dim histogram with variable bins of type short
9688/// (see TH1::TH1 for explanation of parameters)
9689
9690TH1S::TH1S(const char *name,const char *title,Int_t nbins,const Float_t *xbins)
9691: TH1(name,title,nbins,xbins)
9692{
9693 fDimension = 1;
9695 if (fgDefaultSumw2) Sumw2();
9696}
9697
9698////////////////////////////////////////////////////////////////////////////////
9699/// Create a 1-Dim histogram with variable bins of type short
9700/// (see TH1::TH1 for explanation of parameters)
9701
9702TH1S::TH1S(const char *name,const char *title,Int_t nbins,const Double_t *xbins)
9703: TH1(name,title,nbins,xbins)
9704{
9705 fDimension = 1;
9707 if (fgDefaultSumw2) Sumw2();
9708}
9709
9710////////////////////////////////////////////////////////////////////////////////
9711/// Destructor.
9712
9714{
9715}
9716
9717////////////////////////////////////////////////////////////////////////////////
9718/// Copy constructor.
9719/// The list of functions is not copied. (Use Clone() if needed)
9720
9721TH1S::TH1S(const TH1S &h1s) : TH1(), TArrayS()
9722{
9723 h1s.TH1S::Copy(*this);
9724}
9725
9726////////////////////////////////////////////////////////////////////////////////
9727/// Increment bin content by 1.
9728/// Passing an out-of-range bin leads to undefined behavior
9729
9730void TH1S::AddBinContent(Int_t bin)
9731{
9732 if (fArray[bin] < 32767) fArray[bin]++;
9733}
9734
9735////////////////////////////////////////////////////////////////////////////////
9736/// Increment bin content by w.
9737/// \warning The value of w is cast to `Int_t` before being added.
9738/// Passing an out-of-range bin leads to undefined behavior
9739
9741{
9742 Int_t newval = fArray[bin] + Int_t(w);
9743 if (newval > -32768 && newval < 32768) {fArray[bin] = Short_t(newval); return;}
9744 if (newval < -32767) fArray[bin] = -32767;
9745 if (newval > 32767) fArray[bin] = 32767;
9746}
9747
9748////////////////////////////////////////////////////////////////////////////////
9749/// Copy this to newth1
9750
9751void TH1S::Copy(TObject &newth1) const
9752{
9754}
9755
9756////////////////////////////////////////////////////////////////////////////////
9757/// Reset.
9758
9760{
9763}
9764
9765////////////////////////////////////////////////////////////////////////////////
9766/// Set total number of bins including under/overflow
9767/// Reallocate bin contents array
9768
9770{
9771 if (n < 0) n = fXaxis.GetNbins() + 2;
9772 fNcells = n;
9773 TArrayS::Set(n);
9774}
9775
9776////////////////////////////////////////////////////////////////////////////////
9777/// Operator =
9778
9779TH1S& TH1S::operator=(const TH1S &h1)
9780{
9781 if (this != &h1)
9782 h1.TH1S::Copy(*this);
9783 return *this;
9784}
9785
9786////////////////////////////////////////////////////////////////////////////////
9787/// Operator *
9788
9790{
9791 TH1S hnew = h1;
9792 hnew.Scale(c1);
9793 hnew.SetDirectory(nullptr);
9794 return hnew;
9795}
9796
9797////////////////////////////////////////////////////////////////////////////////
9798/// Operator +
9799
9800TH1S operator+(const TH1S &h1, const TH1S &h2)
9801{
9802 TH1S hnew = h1;
9803 hnew.Add(&h2,1);
9804 hnew.SetDirectory(nullptr);
9805 return hnew;
9806}
9807
9808////////////////////////////////////////////////////////////////////////////////
9809/// Operator -
9810
9811TH1S operator-(const TH1S &h1, const TH1S &h2)
9812{
9813 TH1S hnew = h1;
9814 hnew.Add(&h2,-1);
9815 hnew.SetDirectory(nullptr);
9816 return hnew;
9817}
9818
9819////////////////////////////////////////////////////////////////////////////////
9820/// Operator *
9821
9822TH1S operator*(const TH1S &h1, const TH1S &h2)
9823{
9824 TH1S hnew = h1;
9825 hnew.Multiply(&h2);
9826 hnew.SetDirectory(nullptr);
9827 return hnew;
9828}
9829
9830////////////////////////////////////////////////////////////////////////////////
9831/// Operator /
9832
9833TH1S operator/(const TH1S &h1, const TH1S &h2)
9834{
9835 TH1S hnew = h1;
9836 hnew.Divide(&h2);
9837 hnew.SetDirectory(nullptr);
9838 return hnew;
9839}
9840
9841//______________________________________________________________________________
9842// TH1I methods
9843// TH1I : histograms with one int per channel. Maximum bin content = 2147483647
9844// 2147483647 = INT_MAX
9845//______________________________________________________________________________
9846
9847
9848////////////////////////////////////////////////////////////////////////////////
9849/// Constructor.
9850
9851TH1I::TH1I()
9852{
9853 fDimension = 1;
9854 SetBinsLength(3);
9855 if (fgDefaultSumw2) Sumw2();
9856}
9857
9858////////////////////////////////////////////////////////////////////////////////
9859/// Create a 1-Dim histogram with fix bins of type integer
9860/// (see TH1::TH1 for explanation of parameters)
9861
9862TH1I::TH1I(const char *name,const char *title,Int_t nbins,Double_t xlow,Double_t xup)
9863: TH1(name,title,nbins,xlow,xup)
9864{
9865 fDimension = 1;
9867
9868 if (xlow >= xup) SetBuffer(fgBufferSize);
9869 if (fgDefaultSumw2) Sumw2();
9870}
9871
9872////////////////////////////////////////////////////////////////////////////////
9873/// Create a 1-Dim histogram with variable bins of type integer
9874/// (see TH1::TH1 for explanation of parameters)
9875
9876TH1I::TH1I(const char *name,const char *title,Int_t nbins,const Float_t *xbins)
9877: TH1(name,title,nbins,xbins)
9878{
9879 fDimension = 1;
9881 if (fgDefaultSumw2) Sumw2();
9882}
9883
9884////////////////////////////////////////////////////////////////////////////////
9885/// Create a 1-Dim histogram with variable bins of type integer
9886/// (see TH1::TH1 for explanation of parameters)
9887
9888TH1I::TH1I(const char *name,const char *title,Int_t nbins,const Double_t *xbins)
9889: TH1(name,title,nbins,xbins)
9890{
9891 fDimension = 1;
9893 if (fgDefaultSumw2) Sumw2();
9894}
9895
9896////////////////////////////////////////////////////////////////////////////////
9897/// Destructor.
9898
9900{
9901}
9902
9903////////////////////////////////////////////////////////////////////////////////
9904/// Copy constructor.
9905/// The list of functions is not copied. (Use Clone() if needed)
9906
9907TH1I::TH1I(const TH1I &h1i) : TH1(), TArrayI()
9908{
9909 h1i.TH1I::Copy(*this);
9910}
9911
9912////////////////////////////////////////////////////////////////////////////////
9913/// Increment bin content by 1.
9914/// Passing an out-of-range bin leads to undefined behavior
9915
9916void TH1I::AddBinContent(Int_t bin)
9917{
9918 if (fArray[bin] < INT_MAX) fArray[bin]++;
9919}
9920
9921////////////////////////////////////////////////////////////////////////////////
9922/// Increment bin content by w
9923/// \warning The value of w is cast to `Long64_t` before being added.
9924/// Passing an out-of-range bin leads to undefined behavior
9925
9927{
9928 Long64_t newval = fArray[bin] + Long64_t(w);
9929 if (newval > -INT_MAX && newval < INT_MAX) {fArray[bin] = Int_t(newval); return;}
9930 if (newval < -INT_MAX) fArray[bin] = -INT_MAX;
9931 if (newval > INT_MAX) fArray[bin] = INT_MAX;
9932}
9933
9934////////////////////////////////////////////////////////////////////////////////
9935/// Copy this to newth1
9936
9937void TH1I::Copy(TObject &newth1) const
9938{
9940}
9941
9942////////////////////////////////////////////////////////////////////////////////
9943/// Reset.
9944
9946{
9949}
9950
9951////////////////////////////////////////////////////////////////////////////////
9952/// Set total number of bins including under/overflow
9953/// Reallocate bin contents array
9954
9956{
9957 if (n < 0) n = fXaxis.GetNbins() + 2;
9958 fNcells = n;
9959 TArrayI::Set(n);
9960}
9961
9962////////////////////////////////////////////////////////////////////////////////
9963/// Operator =
9964
9965TH1I& TH1I::operator=(const TH1I &h1)
9966{
9967 if (this != &h1)
9968 h1.TH1I::Copy(*this);
9969 return *this;
9970}
9971
9972
9973////////////////////////////////////////////////////////////////////////////////
9974/// Operator *
9975
9977{
9978 TH1I hnew = h1;
9979 hnew.Scale(c1);
9980 hnew.SetDirectory(nullptr);
9981 return hnew;
9982}
9983
9984////////////////////////////////////////////////////////////////////////////////
9985/// Operator +
9986
9987TH1I operator+(const TH1I &h1, const TH1I &h2)
9988{
9989 TH1I hnew = h1;
9990 hnew.Add(&h2,1);
9991 hnew.SetDirectory(nullptr);
9992 return hnew;
9993}
9994
9995////////////////////////////////////////////////////////////////////////////////
9996/// Operator -
9997
9998TH1I operator-(const TH1I &h1, const TH1I &h2)
9999{
10000 TH1I hnew = h1;
10001 hnew.Add(&h2,-1);
10002 hnew.SetDirectory(nullptr);
10003 return hnew;
10004}
10005
10006////////////////////////////////////////////////////////////////////////////////
10007/// Operator *
10008
10009TH1I operator*(const TH1I &h1, const TH1I &h2)
10010{
10011 TH1I hnew = h1;
10012 hnew.Multiply(&h2);
10013 hnew.SetDirectory(nullptr);
10014 return hnew;
10015}
10016
10017////////////////////////////////////////////////////////////////////////////////
10018/// Operator /
10019
10020TH1I operator/(const TH1I &h1, const TH1I &h2)
10021{
10022 TH1I hnew = h1;
10023 hnew.Divide(&h2);
10024 hnew.SetDirectory(nullptr);
10025 return hnew;
10026}
10027
10028//______________________________________________________________________________
10029// TH1L methods
10030// TH1L : histograms with one long64 per channel. Maximum bin content = 9223372036854775807
10031// 9223372036854775807 = LLONG_MAX
10032//______________________________________________________________________________
10033
10034
10035////////////////////////////////////////////////////////////////////////////////
10036/// Constructor.
10037
10038TH1L::TH1L()
10039{
10040 fDimension = 1;
10041 SetBinsLength(3);
10042 if (fgDefaultSumw2) Sumw2();
10043}
10044
10045////////////////////////////////////////////////////////////////////////////////
10046/// Create a 1-Dim histogram with fix bins of type long64
10047/// (see TH1::TH1 for explanation of parameters)
10048
10049TH1L::TH1L(const char *name,const char *title,Int_t nbins,Double_t xlow,Double_t xup)
10050: TH1(name,title,nbins,xlow,xup)
10051{
10052 fDimension = 1;
10054
10055 if (xlow >= xup) SetBuffer(fgBufferSize);
10056 if (fgDefaultSumw2) Sumw2();
10057}
10058
10059////////////////////////////////////////////////////////////////////////////////
10060/// Create a 1-Dim histogram with variable bins of type long64
10061/// (see TH1::TH1 for explanation of parameters)
10062
10063TH1L::TH1L(const char *name,const char *title,Int_t nbins,const Float_t *xbins)
10064: TH1(name,title,nbins,xbins)
10065{
10066 fDimension = 1;
10068 if (fgDefaultSumw2) Sumw2();
10069}
10070
10071////////////////////////////////////////////////////////////////////////////////
10072/// Create a 1-Dim histogram with variable bins of type long64
10073/// (see TH1::TH1 for explanation of parameters)
10074
10075TH1L::TH1L(const char *name,const char *title,Int_t nbins,const Double_t *xbins)
10076: TH1(name,title,nbins,xbins)
10077{
10078 fDimension = 1;
10080 if (fgDefaultSumw2) Sumw2();
10081}
10082
10083////////////////////////////////////////////////////////////////////////////////
10084/// Destructor.
10085
10087{
10088}
10089
10090////////////////////////////////////////////////////////////////////////////////
10091/// Copy constructor.
10092/// The list of functions is not copied. (Use Clone() if needed)
10093
10094TH1L::TH1L(const TH1L &h1l) : TH1(), TArrayL64()
10095{
10096 h1l.TH1L::Copy(*this);
10097}
10098
10099////////////////////////////////////////////////////////////////////////////////
10100/// Increment bin content by 1.
10101/// Passing an out-of-range bin leads to undefined behavior
10102
10103void TH1L::AddBinContent(Int_t bin)
10104{
10105 if (fArray[bin] < LLONG_MAX) fArray[bin]++;
10106}
10107
10108////////////////////////////////////////////////////////////////////////////////
10109/// Increment bin content by w.
10110/// \warning The value of w is cast to `Long64_t` before being added.
10111/// Passing an out-of-range bin leads to undefined behavior
10112
10114{
10115 Long64_t newval = fArray[bin] + Long64_t(w);
10116 if (newval > -LLONG_MAX && newval < LLONG_MAX) {fArray[bin] = newval; return;}
10117 if (newval < -LLONG_MAX) fArray[bin] = -LLONG_MAX;
10118 if (newval > LLONG_MAX) fArray[bin] = LLONG_MAX;
10119}
10120
10121////////////////////////////////////////////////////////////////////////////////
10122/// Copy this to newth1
10123
10124void TH1L::Copy(TObject &newth1) const
10125{
10127}
10128
10129////////////////////////////////////////////////////////////////////////////////
10130/// Reset.
10131
10133{
10136}
10137
10138////////////////////////////////////////////////////////////////////////////////
10139/// Set total number of bins including under/overflow
10140/// Reallocate bin contents array
10141
10143{
10144 if (n < 0) n = fXaxis.GetNbins() + 2;
10145 fNcells = n;
10147}
10148
10149////////////////////////////////////////////////////////////////////////////////
10150/// Operator =
10151
10152TH1L& TH1L::operator=(const TH1L &h1)
10153{
10154 if (this != &h1)
10155 h1.TH1L::Copy(*this);
10156 return *this;
10157}
10158
10159
10160////////////////////////////////////////////////////////////////////////////////
10161/// Operator *
10162
10164{
10165 TH1L hnew = h1;
10166 hnew.Scale(c1);
10167 hnew.SetDirectory(nullptr);
10168 return hnew;
10169}
10170
10171////////////////////////////////////////////////////////////////////////////////
10172/// Operator +
10173
10174TH1L operator+(const TH1L &h1, const TH1L &h2)
10175{
10176 TH1L hnew = h1;
10177 hnew.Add(&h2,1);
10178 hnew.SetDirectory(nullptr);
10179 return hnew;
10180}
10181
10182////////////////////////////////////////////////////////////////////////////////
10183/// Operator -
10184
10185TH1L operator-(const TH1L &h1, const TH1L &h2)
10186{
10187 TH1L hnew = h1;
10188 hnew.Add(&h2,-1);
10189 hnew.SetDirectory(nullptr);
10190 return hnew;
10191}
10192
10193////////////////////////////////////////////////////////////////////////////////
10194/// Operator *
10195
10196TH1L operator*(const TH1L &h1, const TH1L &h2)
10197{
10198 TH1L hnew = h1;
10199 hnew.Multiply(&h2);
10200 hnew.SetDirectory(nullptr);
10201 return hnew;
10202}
10203
10204////////////////////////////////////////////////////////////////////////////////
10205/// Operator /
10206
10207TH1L operator/(const TH1L &h1, const TH1L &h2)
10208{
10209 TH1L hnew = h1;
10210 hnew.Divide(&h2);
10211 hnew.SetDirectory(nullptr);
10212 return hnew;
10213}
10214
10215//______________________________________________________________________________
10216// TH1F methods
10217// TH1F : histograms with one float per channel. Maximum precision 7 digits, maximum integer bin content = +/-16777216
10218//______________________________________________________________________________
10219
10220
10221////////////////////////////////////////////////////////////////////////////////
10222/// Constructor.
10223
10224TH1F::TH1F()
10225{
10226 fDimension = 1;
10227 SetBinsLength(3);
10228 if (fgDefaultSumw2) Sumw2();
10229}
10230
10231////////////////////////////////////////////////////////////////////////////////
10232/// Create a 1-Dim histogram with fix bins of type float
10233/// (see TH1::TH1 for explanation of parameters)
10234
10235TH1F::TH1F(const char *name,const char *title,Int_t nbins,Double_t xlow,Double_t xup)
10236: TH1(name,title,nbins,xlow,xup)
10237{
10238 fDimension = 1;
10240
10241 if (xlow >= xup) SetBuffer(fgBufferSize);
10242 if (fgDefaultSumw2) Sumw2();
10243}
10244
10245////////////////////////////////////////////////////////////////////////////////
10246/// Create a 1-Dim histogram with variable bins of type float
10247/// (see TH1::TH1 for explanation of parameters)
10248
10249TH1F::TH1F(const char *name,const char *title,Int_t nbins,const Float_t *xbins)
10250: TH1(name,title,nbins,xbins)
10251{
10252 fDimension = 1;
10254 if (fgDefaultSumw2) Sumw2();
10255}
10256
10257////////////////////////////////////////////////////////////////////////////////
10258/// Create a 1-Dim histogram with variable bins of type float
10259/// (see TH1::TH1 for explanation of parameters)
10260
10261TH1F::TH1F(const char *name,const char *title,Int_t nbins,const Double_t *xbins)
10262: TH1(name,title,nbins,xbins)
10263{
10264 fDimension = 1;
10266 if (fgDefaultSumw2) Sumw2();
10267}
10268
10269////////////////////////////////////////////////////////////////////////////////
10270/// Create a histogram from a TVectorF
10271/// by default the histogram name is "TVectorF" and title = ""
10272
10273TH1F::TH1F(const TVectorF &v)
10274: TH1("TVectorF","",v.GetNrows(),0,v.GetNrows())
10275{
10277 fDimension = 1;
10278 Int_t ivlow = v.GetLwb();
10279 for (Int_t i=0;i<fNcells-2;i++) {
10280 SetBinContent(i+1,v(i+ivlow));
10281 }
10283 if (fgDefaultSumw2) Sumw2();
10284}
10285
10286////////////////////////////////////////////////////////////////////////////////
10287/// Copy Constructor.
10288/// The list of functions is not copied. (Use Clone() if needed)
10289
10290TH1F::TH1F(const TH1F &h1f) : TH1(), TArrayF()
10291{
10292 h1f.TH1F::Copy(*this);
10293}
10294
10295////////////////////////////////////////////////////////////////////////////////
10296/// Destructor.
10297
10299{
10300}
10301
10302////////////////////////////////////////////////////////////////////////////////
10303/// Copy this to newth1.
10304
10305void TH1F::Copy(TObject &newth1) const
10306{
10308}
10309
10310////////////////////////////////////////////////////////////////////////////////
10311/// Reset.
10312
10314{
10317}
10318
10319////////////////////////////////////////////////////////////////////////////////
10320/// Set total number of bins including under/overflow
10321/// Reallocate bin contents array
10322
10324{
10325 if (n < 0) n = fXaxis.GetNbins() + 2;
10326 fNcells = n;
10327 TArrayF::Set(n);
10328}
10329
10330////////////////////////////////////////////////////////////////////////////////
10331/// Operator =
10332
10334{
10335 if (this != &h1f)
10336 h1f.TH1F::Copy(*this);
10337 return *this;
10338}
10339
10340////////////////////////////////////////////////////////////////////////////////
10341/// Operator *
10342
10344{
10345 TH1F hnew = h1;
10346 hnew.Scale(c1);
10347 hnew.SetDirectory(nullptr);
10348 return hnew;
10349}
10350
10351////////////////////////////////////////////////////////////////////////////////
10352/// Operator +
10353
10354TH1F operator+(const TH1F &h1, const TH1F &h2)
10355{
10356 TH1F hnew = h1;
10357 hnew.Add(&h2,1);
10358 hnew.SetDirectory(nullptr);
10359 return hnew;
10360}
10361
10362////////////////////////////////////////////////////////////////////////////////
10363/// Operator -
10364
10365TH1F operator-(const TH1F &h1, const TH1F &h2)
10366{
10367 TH1F hnew = h1;
10368 hnew.Add(&h2,-1);
10369 hnew.SetDirectory(nullptr);
10370 return hnew;
10371}
10372
10373////////////////////////////////////////////////////////////////////////////////
10374/// Operator *
10375
10376TH1F operator*(const TH1F &h1, const TH1F &h2)
10377{
10378 TH1F hnew = h1;
10379 hnew.Multiply(&h2);
10380 hnew.SetDirectory(nullptr);
10381 return hnew;
10382}
10383
10384////////////////////////////////////////////////////////////////////////////////
10385/// Operator /
10386
10387TH1F operator/(const TH1F &h1, const TH1F &h2)
10388{
10389 TH1F hnew = h1;
10390 hnew.Divide(&h2);
10391 hnew.SetDirectory(nullptr);
10392 return hnew;
10393}
10394
10395//______________________________________________________________________________
10396// TH1D methods
10397// TH1D : histograms with one double per channel. Maximum precision 14 digits, maximum integer bin content = +/-9007199254740992
10398//______________________________________________________________________________
10399
10400
10401////////////////////////////////////////////////////////////////////////////////
10402/// Constructor.
10403
10404TH1D::TH1D()
10405{
10406 fDimension = 1;
10407 SetBinsLength(3);
10408 if (fgDefaultSumw2) Sumw2();
10409}
10410
10411////////////////////////////////////////////////////////////////////////////////
10412/// Create a 1-Dim histogram with fix bins of type double
10413/// (see TH1::TH1 for explanation of parameters)
10414
10415TH1D::TH1D(const char *name,const char *title,Int_t nbins,Double_t xlow,Double_t xup)
10416: TH1(name,title,nbins,xlow,xup)
10417{
10418 fDimension = 1;
10420
10421 if (xlow >= xup) SetBuffer(fgBufferSize);
10422 if (fgDefaultSumw2) Sumw2();
10423}
10424
10425////////////////////////////////////////////////////////////////////////////////
10426/// Create a 1-Dim histogram with variable bins of type double
10427/// (see TH1::TH1 for explanation of parameters)
10428
10429TH1D::TH1D(const char *name,const char *title,Int_t nbins,const Float_t *xbins)
10430: TH1(name,title,nbins,xbins)
10431{
10432 fDimension = 1;
10434 if (fgDefaultSumw2) Sumw2();
10435}
10436
10437////////////////////////////////////////////////////////////////////////////////
10438/// Create a 1-Dim histogram with variable bins of type double
10439/// (see TH1::TH1 for explanation of parameters)
10440
10441TH1D::TH1D(const char *name,const char *title,Int_t nbins,const Double_t *xbins)
10442: TH1(name,title,nbins,xbins)
10443{
10444 fDimension = 1;
10446 if (fgDefaultSumw2) Sumw2();
10447}
10448
10449////////////////////////////////////////////////////////////////////////////////
10450/// Create a histogram from a TVectorD
10451/// by default the histogram name is "TVectorD" and title = ""
10452
10453TH1D::TH1D(const TVectorD &v)
10454: TH1("TVectorD","",v.GetNrows(),0,v.GetNrows())
10455{
10457 fDimension = 1;
10458 Int_t ivlow = v.GetLwb();
10459 for (Int_t i=0;i<fNcells-2;i++) {
10460 SetBinContent(i+1,v(i+ivlow));
10461 }
10463 if (fgDefaultSumw2) Sumw2();
10464}
10465
10466////////////////////////////////////////////////////////////////////////////////
10467/// Destructor.
10468
10470{
10471}
10472
10473////////////////////////////////////////////////////////////////////////////////
10474/// Constructor.
10475
10476TH1D::TH1D(const TH1D &h1d) : TH1(), TArrayD()
10477{
10478 // intentially call virtual method to warn if TProfile is copying
10479 h1d.Copy(*this);
10480}
10481
10482////////////////////////////////////////////////////////////////////////////////
10483/// Copy this to newth1
10484
10485void TH1D::Copy(TObject &newth1) const
10486{
10488}
10489
10490////////////////////////////////////////////////////////////////////////////////
10491/// Reset.
10492
10494{
10497}
10498
10499////////////////////////////////////////////////////////////////////////////////
10500/// Set total number of bins including under/overflow
10501/// Reallocate bin contents array
10502
10504{
10505 if (n < 0) n = fXaxis.GetNbins() + 2;
10506 fNcells = n;
10507 TArrayD::Set(n);
10508}
10509
10510////////////////////////////////////////////////////////////////////////////////
10511/// Operator =
10512
10514{
10515 // intentially call virtual method to warn if TProfile is copying
10516 if (this != &h1d)
10517 h1d.Copy(*this);
10518 return *this;
10519}
10520
10521////////////////////////////////////////////////////////////////////////////////
10522/// Operator *
10523
10525{
10526 TH1D hnew = h1;
10527 hnew.Scale(c1);
10528 hnew.SetDirectory(nullptr);
10529 return hnew;
10530}
10531
10532////////////////////////////////////////////////////////////////////////////////
10533/// Operator +
10534
10535TH1D operator+(const TH1D &h1, const TH1D &h2)
10536{
10537 TH1D hnew = h1;
10538 hnew.Add(&h2,1);
10539 hnew.SetDirectory(nullptr);
10540 return hnew;
10541}
10542
10543////////////////////////////////////////////////////////////////////////////////
10544/// Operator -
10545
10546TH1D operator-(const TH1D &h1, const TH1D &h2)
10547{
10548 TH1D hnew = h1;
10549 hnew.Add(&h2,-1);
10550 hnew.SetDirectory(nullptr);
10551 return hnew;
10552}
10553
10554////////////////////////////////////////////////////////////////////////////////
10555/// Operator *
10556
10557TH1D operator*(const TH1D &h1, const TH1D &h2)
10558{
10559 TH1D hnew = h1;
10560 hnew.Multiply(&h2);
10561 hnew.SetDirectory(nullptr);
10562 return hnew;
10563}
10564
10565////////////////////////////////////////////////////////////////////////////////
10566/// Operator /
10567
10568TH1D operator/(const TH1D &h1, const TH1D &h2)
10569{
10570 TH1D hnew = h1;
10571 hnew.Divide(&h2);
10572 hnew.SetDirectory(nullptr);
10573 return hnew;
10574}
10575
10576////////////////////////////////////////////////////////////////////////////////
10577///return pointer to histogram with name
10578///hid if id >=0
10579///h_id if id <0
10580
10581TH1 *R__H(Int_t hid)
10582{
10583 TString hname;
10584 if(hid >= 0) hname.Form("h%d",hid);
10585 else hname.Form("h_%d",hid);
10586 return (TH1*)gDirectory->Get(hname);
10587}
10588
10589////////////////////////////////////////////////////////////////////////////////
10590///return pointer to histogram with name hname
10591
10592TH1 *R__H(const char * hname)
10593{
10594 return (TH1*)gDirectory->Get(hname);
10595}
10596
10597
10598/// \fn void TH1::SetBarOffset(Float_t offset)
10599/// Set the bar offset as fraction of the bin width for drawing mode "B".
10600/// This shifts bars to the right on the x axis, and helps to draw bars next to each other.
10601/// \see THistPainter, SetBarWidth()
10602
10603/// \fn void TH1::SetBarWidth(Float_t width)
10604/// Set the width of bars as fraction of the bin width for drawing mode "B".
10605/// This allows for making bars narrower than the bin width. With SetBarOffset(), this helps to draw multiple bars next to each other.
10606/// \see THistPainter, SetBarOffset()
#define b(i)
Definition RSha256.hxx:100
#define c(i)
Definition RSha256.hxx:101
#define a(i)
Definition RSha256.hxx:99
#define s1(x)
Definition RSha256.hxx:91
#define h(i)
Definition RSha256.hxx:106
#define e(i)
Definition RSha256.hxx:103
short Style_t
Style number (short)
Definition RtypesCore.h:96
bool Bool_t
Boolean (0=false, 1=true) (bool)
Definition RtypesCore.h:77
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:59
short Color_t
Color number (short)
Definition RtypesCore.h:99
short Version_t
Class version identifier (short)
Definition RtypesCore.h:79
char Char_t
Character 1 byte (char)
Definition RtypesCore.h:51
float Float_t
Float 4 bytes (float)
Definition RtypesCore.h:71
short Short_t
Signed Short integer 2 bytes (short)
Definition RtypesCore.h:53
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
double Double_t
Double 8 bytes.
Definition RtypesCore.h:73
long long Long64_t
Portable signed long integer 8 bytes.
Definition RtypesCore.h:83
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
const char Option_t
Option string (const char)
Definition RtypesCore.h:80
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#define gDirectory
Definition TDirectory.h:385
R__EXTERN TEnv * gEnv
Definition TEnv.h:170
#define R__ASSERT(e)
Checks condition e and reports a fatal error if it's false.
Definition TError.h:125
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t option
Option_t Option_t SetLineWidth
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char filename
Option_t Option_t SetFillStyle
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t del
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t np
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
Option_t Option_t SetLineColor
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t UChar_t len
Option_t Option_t TPoint TPoint const char x1
Option_t Option_t TPoint TPoint const char y2
Option_t Option_t SetFillColor
Option_t Option_t SetMarkerStyle
Option_t Option_t width
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
Option_t Option_t TPoint TPoint const char y1
char name[80]
Definition TGX11.cxx:110
static bool IsEquidistantBinning(const TAxis &axis)
Test if the binning is equidistant.
Definition TH1.cxx:5880
void H1LeastSquareLinearFit(Int_t ndata, Double_t &a0, Double_t &a1, Int_t &ifail)
Least square linear fit without weights.
Definition TH1.cxx:4828
void H1InitGaus()
Compute Initial values of parameters for a gaussian.
Definition TH1.cxx:4663
void H1InitExpo()
Compute Initial values of parameters for an exponential.
Definition TH1.cxx:4719
TH1C operator+(const TH1C &h1, const TH1C &h2)
Operator +.
Definition TH1.cxx:9613
TH1C operator-(const TH1C &h1, const TH1C &h2)
Operator -.
Definition TH1.cxx:9624
TH1C operator/(const TH1C &h1, const TH1C &h2)
Operator /.
Definition TH1.cxx:9646
void H1LeastSquareSeqnd(Int_t n, Double_t *a, Int_t idim, Int_t &ifail, Int_t k, Double_t *b)
Extracted from CERN Program library routine DSEQN.
Definition TH1.cxx:4874
static Bool_t AlmostEqual(Double_t a, Double_t b, Double_t epsilon=0.00000001)
Test if two double are almost equal.
Definition TH1.cxx:5863
static Bool_t AlmostInteger(Double_t a, Double_t epsilon=0.00000001)
Test if a double is almost an integer.
Definition TH1.cxx:5871
TF1 * gF1
Definition TH1.cxx:584
TH1 * R__H(Int_t hid)
return pointer to histogram with name hid if id >=0 h_id if id <0
Definition TH1.cxx:10579
TH1C operator*(Double_t c1, const TH1C &h1)
Operator *.
Definition TH1.cxx:9602
void H1LeastSquareFit(Int_t n, Int_t m, Double_t *a)
Least squares lpolynomial fitting without weights.
Definition TH1.cxx:4769
void H1InitPolynom()
Compute Initial values of parameters for a polynom.
Definition TH1.cxx:4739
float xmin
int nentries
float ymin
float xmax
float ymax
#define gInterpreter
Int_t gDebug
Global variable setting the debug level. Set to 0 to disable, increase it in steps of 1 to increase t...
Definition TROOT.cxx:627
R__EXTERN TVirtualMutex * gROOTMutex
Definition TROOT.h:63
#define gROOT
Definition TROOT.h:411
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
void Printf(const char *fmt,...)
Formats a string in a circular formatting buffer and prints the string.
Definition TString.cxx:2509
R__EXTERN TStyle * gStyle
Definition TStyle.h:442
#define R__LOCKGUARD(mutex)
#define gPad
#define R__WRITE_LOCKGUARD(mutex)
Class describing the binned data sets : vectors of x coordinates, y values and optionally error on y ...
Definition BinData.h:52
class describing the range in the coordinates it supports multiple range in a coordinate.
Definition DataRange.h:35
void AndersonDarling2SamplesTest(Double_t &pvalue, Double_t &testStat) const
Performs the Anderson-Darling 2-Sample Test.
Definition GoFTest.cxx:646
const_iterator begin() const
const_iterator end() const
Array of chars or bytes (8 bits per element).
Definition TArrayC.h:27
Char_t * fArray
Definition TArrayC.h:30
void Reset(Char_t val=0)
Definition TArrayC.h:47
void Set(Int_t n) override
Set size of this array to n chars.
Definition TArrayC.cxx:104
Array of doubles (64 bits per element).
Definition TArrayD.h:27
Double_t * fArray
Definition TArrayD.h:30
void Streamer(TBuffer &) override
Stream a TArrayD object.
Definition TArrayD.cxx:148
void Copy(TArrayD &array) const
Definition TArrayD.h:42
void Set(Int_t n) override
Set size of this array to n doubles.
Definition TArrayD.cxx:105
const Double_t * GetArray() const
Definition TArrayD.h:43
void Reset()
Definition TArrayD.h:47
Array of floats (32 bits per element).
Definition TArrayF.h:27
void Reset()
Definition TArrayF.h:47
void Set(Int_t n) override
Set size of this array to n floats.
Definition TArrayF.cxx:104
Array of integers (32 bits per element).
Definition TArrayI.h:27
Int_t * fArray
Definition TArrayI.h:30
void Set(Int_t n) override
Set size of this array to n ints.
Definition TArrayI.cxx:104
void Reset()
Definition TArrayI.h:47
Array of long64s (64 bits per element).
Definition TArrayL64.h:27
Long64_t * fArray
Definition TArrayL64.h:30
void Set(Int_t n) override
Set size of this array to n long64s.
void Reset()
Definition TArrayL64.h:47
Array of shorts (16 bits per element).
Definition TArrayS.h:27
void Set(Int_t n) override
Set size of this array to n shorts.
Definition TArrayS.cxx:104
void Reset()
Definition TArrayS.h:47
Short_t * fArray
Definition TArrayS.h:30
Abstract array base class.
Definition TArray.h:31
Int_t fN
Definition TArray.h:38
virtual void Set(Int_t n)=0
virtual Color_t GetTitleColor() const
Definition TAttAxis.h:47
virtual Color_t GetLabelColor() const
Definition TAttAxis.h:39
virtual Int_t GetNdivisions() const
Definition TAttAxis.h:37
virtual Color_t GetAxisColor() const
Definition TAttAxis.h:38
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition TAttAxis.cxx:279
virtual Style_t GetTitleFont() const
Definition TAttAxis.h:48
virtual Float_t GetLabelOffset() const
Definition TAttAxis.h:41
virtual void SetAxisColor(Color_t color=1, Float_t alpha=1.)
Set color of the line axis and tick marks.
Definition TAttAxis.cxx:141
virtual void SetLabelSize(Float_t size=0.04)
Set size of axis labels.
Definition TAttAxis.cxx:184
virtual Style_t GetLabelFont() const
Definition TAttAxis.h:40
virtual void SetTitleFont(Style_t font=62)
Set the title font.
Definition TAttAxis.cxx:308
virtual void SetLabelOffset(Float_t offset=0.005)
Set distance between the axis and the labels.
Definition TAttAxis.cxx:172
virtual void SetLabelFont(Style_t font=62)
Set labels' font.
Definition TAttAxis.cxx:161
virtual void SetTitleSize(Float_t size=0.04)
Set size of axis title.
Definition TAttAxis.cxx:290
virtual void SetTitleColor(Color_t color=1)
Set color of axis title.
Definition TAttAxis.cxx:299
virtual Float_t GetTitleSize() const
Definition TAttAxis.h:45
virtual Float_t GetLabelSize() const
Definition TAttAxis.h:42
virtual Float_t GetTickLength() const
Definition TAttAxis.h:46
virtual void ResetAttAxis(Option_t *option="")
Reset axis attributes.
Definition TAttAxis.cxx:78
virtual Float_t GetTitleOffset() const
Definition TAttAxis.h:44
virtual void SetTickLength(Float_t length=0.03)
Set tick mark length.
Definition TAttAxis.cxx:265
virtual void SetNdivisions(Int_t n=510, Bool_t optim=kTRUE)
Set the number of divisions for this axis.
Definition TAttAxis.cxx:214
virtual void SetLabelColor(Color_t color=1, Float_t alpha=1.)
Set color of labels.
Definition TAttAxis.cxx:151
virtual void Streamer(TBuffer &)
virtual Color_t GetFillColor() const
Return the fill area color.
Definition TAttFill.h:31
void Copy(TAttFill &attfill) const
Copy this fill attributes to a new TAttFill.
Definition TAttFill.cxx:206
virtual Style_t GetFillStyle() const
Return the fill area style.
Definition TAttFill.h:32
virtual void SaveFillAttributes(std::ostream &out, const char *name, Int_t coldef=1, Int_t stydef=1001)
Save fill attributes as C++ statement(s) on output stream out.
Definition TAttFill.cxx:238
virtual void Streamer(TBuffer &)
virtual Color_t GetLineColor() const
Return the line color.
Definition TAttLine.h:35
virtual void SetLineStyle(Style_t lstyle)
Set the line style.
Definition TAttLine.h:44
virtual Width_t GetLineWidth() const
Return the line width.
Definition TAttLine.h:37
virtual Style_t GetLineStyle() const
Return the line style.
Definition TAttLine.h:36
void Copy(TAttLine &attline) const
Copy this line attributes to a new TAttLine.
Definition TAttLine.cxx:176
virtual void SaveLineAttributes(std::ostream &out, const char *name, Int_t coldef=1, Int_t stydef=1, Int_t widdef=1)
Save line attributes as C++ statement(s) on output stream out.
Definition TAttLine.cxx:274
virtual void SaveMarkerAttributes(std::ostream &out, const char *name, Int_t coldef=1, Int_t stydef=1, Int_t sizdef=1)
Save line attributes as C++ statement(s) on output stream out.
virtual Style_t GetMarkerStyle() const
Return the marker style.
Definition TAttMarker.h:33
virtual void SetMarkerColor(Color_t mcolor=1)
Set the marker color.
Definition TAttMarker.h:39
virtual Color_t GetMarkerColor() const
Return the marker color.
Definition TAttMarker.h:32
virtual Size_t GetMarkerSize() const
Return the marker size.
Definition TAttMarker.h:34
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition TAttMarker.h:41
void Copy(TAttMarker &attmarker) const
Copy this marker attributes to a new TAttMarker.
virtual void Streamer(TBuffer &)
virtual void SetMarkerSize(Size_t msize=1)
Set the marker size.
Definition TAttMarker.h:46
Class to manage histogram axis.
Definition TAxis.h:32
virtual void GetCenter(Double_t *center) const
Return an array with the center of all bins.
Definition TAxis.cxx:557
virtual Bool_t GetTimeDisplay() const
Definition TAxis.h:133
Bool_t IsAlphanumeric() const
Definition TAxis.h:90
const char * GetTitle() const override
Returns title of object.
Definition TAxis.h:137
virtual Double_t GetBinCenter(Int_t bin) const
Return center of bin.
Definition TAxis.cxx:481
Bool_t CanExtend() const
Definition TAxis.h:88
virtual void SetParent(TObject *obj)
Definition TAxis.h:169
const TArrayD * GetXbins() const
Definition TAxis.h:138
void SetCanExtend(Bool_t canExtend)
Definition TAxis.h:92
void Copy(TObject &axis) const override
Copy axis structure to another axis.
Definition TAxis.cxx:210
Double_t GetXmax() const
Definition TAxis.h:142
@ kLabelsUp
Definition TAxis.h:75
@ kLabelsDown
Definition TAxis.h:74
@ kLabelsHori
Definition TAxis.h:72
@ kAxisRange
Definition TAxis.h:66
@ kLabelsVert
Definition TAxis.h:73
virtual Int_t FindBin(Double_t x)
Find bin number corresponding to abscissa x.
Definition TAxis.cxx:292
virtual Double_t GetBinLowEdge(Int_t bin) const
Return low edge of bin.
Definition TAxis.cxx:521
virtual void SetTimeDisplay(Int_t value)
Definition TAxis.h:173
virtual void Set(Int_t nbins, Double_t xmin, Double_t xmax)
Initialize axis with fix bins.
Definition TAxis.cxx:783
virtual Int_t FindFixBin(Double_t x) const
Find bin number corresponding to abscissa x
Definition TAxis.cxx:421
void SaveAttributes(std::ostream &out, const char *name, const char *subname) override
Save axis attributes as C++ statement(s) on output stream out.
Definition TAxis.cxx:714
Int_t GetLast() const
Return last bin on the axis i.e.
Definition TAxis.cxx:472
virtual void SetLimits(Double_t xmin, Double_t xmax)
Definition TAxis.h:166
Double_t GetXmin() const
Definition TAxis.h:141
void Streamer(TBuffer &) override
Stream an object of class TAxis.
Definition TAxis.cxx:1208
Int_t GetNbins() const
Definition TAxis.h:127
virtual void GetLowEdge(Double_t *edge) const
Return an array with the low edge of all bins.
Definition TAxis.cxx:566
virtual void SetRange(Int_t first=0, Int_t last=0)
Set the viewing range for the axis using bin numbers.
Definition TAxis.cxx:1045
virtual Double_t GetBinWidth(Int_t bin) const
Return bin width.
Definition TAxis.cxx:545
virtual Double_t GetBinUpEdge(Int_t bin) const
Return up edge of bin.
Definition TAxis.cxx:531
Int_t GetFirst() const
Return first bin on the axis i.e.
Definition TAxis.cxx:461
THashList * GetLabels() const
Definition TAxis.h:123
Using a TBrowser one can browse all ROOT objects.
Definition TBrowser.h:37
Buffer base class used for serializing objects.
Definition TBuffer.h:43
void * New(ENewType defConstructor=kClassNew, Bool_t quiet=kFALSE) const
Return a pointer to a newly allocated object of this class.
Definition TClass.cxx:5017
ROOT::NewFunc_t GetNew() const
Return the wrapper around new ThisClass().
Definition TClass.cxx:7552
Collection abstract base class.
Definition TCollection.h:65
virtual bool UseRWLock(Bool_t enable=true)
Set this collection to use a RW lock upon access, making it thread safe.
TObject * Clone(const char *newname="") const override
Make a clone of an collection using the Streamer facility.
virtual Int_t GetSize() const
Return the capacity of the collection, i.e.
Describe directory structure in memory.
Definition TDirectory.h:45
virtual void Append(TObject *obj, Bool_t replace=kFALSE)
Append object to this directory.
virtual TObject * Remove(TObject *)
Remove an object from the in-memory list.
virtual Int_t GetValue(const char *name, Int_t dflt) const
Returns the integer value for a resource.
Definition TEnv.cxx:490
1-Dim function class
Definition TF1.h:182
static void RejectPoint(Bool_t reject=kTRUE)
Static function to set the global flag to reject points the fgRejectPoint global flag is tested by al...
Definition TF1.cxx:3699
virtual TH1 * GetHistogram() const
Return a pointer to the histogram used to visualise the function Note that this histogram is managed ...
Definition TF1.cxx:1611
static TClass * Class()
virtual Int_t GetNpar() const
Definition TF1.h:461
virtual Double_t Integral(Double_t a, Double_t b, Double_t epsrel=1.e-12)
IntegralOneDim or analytical integral.
Definition TF1.cxx:2556
virtual void InitArgs(const Double_t *x, const Double_t *params)
Initialize parameters addresses.
Definition TF1.cxx:2507
virtual void GetRange(Double_t *xmin, Double_t *xmax) const
Return range of a generic N-D function.
Definition TF1.cxx:2306
virtual Double_t EvalPar(const Double_t *x, const Double_t *params=nullptr)
Evaluate function with given coordinates and parameters.
Definition TF1.cxx:1475
virtual void SetParLimits(Int_t ipar, Double_t parmin, Double_t parmax)
Set lower and upper limits for parameter ipar.
Definition TF1.cxx:3538
static Bool_t RejectedPoint()
See TF1::RejectPoint above.
Definition TF1.cxx:3708
virtual Double_t Eval(Double_t x, Double_t y=0, Double_t z=0, Double_t t=0) const
Evaluate this function.
Definition TF1.cxx:1446
virtual void SetParameter(Int_t param, Double_t value)
Definition TF1.h:623
virtual Bool_t IsInside(const Double_t *x) const
return kTRUE if the point is inside the function range
Definition TF1.h:582
A 2-Dim function with parameters.
Definition TF2.h:29
A 3-Dim function with parameters.
Definition TF3.h:28
Provides an indirection to the TFitResult class and with a semantics identical to a TFitResult pointe...
1-D histogram with a byte per channel (see TH1 documentation)
Definition TH1.h:715
~TH1C() override
Destructor.
Definition TH1.cxx:9526
void SetBinsLength(Int_t n=-1) override
Set total number of bins including under/overflow Reallocate bin contents array.
Definition TH1.cxx:9582
TH1C & operator=(const TH1C &h1)
Operator =.
Definition TH1.cxx:9592
TH1C()
Constructor.
Definition TH1.cxx:9478
void Copy(TObject &hnew) const override
Copy this to newth1.
Definition TH1.cxx:9564
void AddBinContent(Int_t bin) override
Increment bin content by 1.
Definition TH1.cxx:9543
void Reset(Option_t *option="") override
Reset.
Definition TH1.cxx:9572
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:927
~TH1D() override
Destructor.
Definition TH1.cxx:10467
void SetBinsLength(Int_t n=-1) override
Set total number of bins including under/overflow Reallocate bin contents array.
Definition TH1.cxx:10501
void Copy(TObject &hnew) const override
Copy this to newth1.
Definition TH1.cxx:10483
TH1D()
Constructor.
Definition TH1.cxx:10402
TH1D & operator=(const TH1D &h1)
Operator =.
Definition TH1.cxx:10511
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:879
Double_t RetrieveBinContent(Int_t bin) const override
Raw retrieval of bin content on internal data structure see convention for numbering bins in TH1::Get...
Definition TH1.h:913
TH1F & operator=(const TH1F &h1)
Operator =.
Definition TH1.cxx:10331
void Copy(TObject &hnew) const override
Copy this to newth1.
Definition TH1.cxx:10303
void SetBinsLength(Int_t n=-1) override
Set total number of bins including under/overflow Reallocate bin contents array.
Definition TH1.cxx:10321
~TH1F() override
Destructor.
Definition TH1.cxx:10296
TH1F()
Constructor.
Definition TH1.cxx:10222
1-D histogram with an int per channel (see TH1 documentation)
Definition TH1.h:797
void SetBinsLength(Int_t n=-1) override
Set total number of bins including under/overflow Reallocate bin contents array.
Definition TH1.cxx:9953
void AddBinContent(Int_t bin) override
Increment bin content by 1.
Definition TH1.cxx:9914
~TH1I() override
Destructor.
Definition TH1.cxx:9897
TH1I()
Constructor.
Definition TH1.cxx:9849
void Copy(TObject &hnew) const override
Copy this to newth1.
Definition TH1.cxx:9935
TH1I & operator=(const TH1I &h1)
Operator =.
Definition TH1.cxx:9963
1-D histogram with a long64 per channel (see TH1 documentation)
Definition TH1.h:838
TH1L & operator=(const TH1L &h1)
Operator =.
Definition TH1.cxx:10150
void AddBinContent(Int_t bin) override
Increment bin content by 1.
Definition TH1.cxx:10101
void SetBinsLength(Int_t n=-1) override
Set total number of bins including under/overflow Reallocate bin contents array.
Definition TH1.cxx:10140
~TH1L() override
Destructor.
Definition TH1.cxx:10084
TH1L()
Constructor.
Definition TH1.cxx:10036
void Copy(TObject &hnew) const override
Copy this to newth1.
Definition TH1.cxx:10122
1-D histogram with a short per channel (see TH1 documentation)
Definition TH1.h:756
TH1S & operator=(const TH1S &h1)
Operator =.
Definition TH1.cxx:9777
void Copy(TObject &hnew) const override
Copy this to newth1.
Definition TH1.cxx:9749
TH1S()
Constructor.
Definition TH1.cxx:9663
void SetBinsLength(Int_t n=-1) override
Set total number of bins including under/overflow Reallocate bin contents array.
Definition TH1.cxx:9767
~TH1S() override
Destructor.
Definition TH1.cxx:9711
void AddBinContent(Int_t bin) override
Increment bin content by 1.
Definition TH1.cxx:9728
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:109
~TH1() override
Histogram default destructor.
Definition TH1.cxx:630
virtual void SetError(const Double_t *error)
Replace bin errors by values in array error.
Definition TH1.cxx:8979
virtual void SetDirectory(TDirectory *dir)
By default, when a histogram is created, it is added to the list of histogram objects in the current ...
Definition TH1.cxx:8965
virtual void FitPanel()
Display a panel with all histogram fit options.
Definition TH1.cxx:4261
Double_t * fBuffer
[fBufferSize] entry buffer
Definition TH1.h:169
virtual Int_t AutoP2FindLimits(Double_t min, Double_t max)
Buffer-based estimate of the histogram range using the power of 2 algorithm.
Definition TH1.cxx:1312
virtual Double_t GetEffectiveEntries() const
Number of effective entries of the histogram.
Definition TH1.cxx:4425
char * GetObjectInfo(Int_t px, Int_t py) const override
Redefines TObject::GetObjectInfo.
Definition TH1.cxx:4479
virtual void Smooth(Int_t ntimes=1, Option_t *option="")
Smooth bin contents of this histogram.
Definition TH1.cxx:6914
virtual Double_t GetBinCenter(Int_t bin) const
Return bin center for 1D histogram.
Definition TH1.cxx:9166
virtual void Rebuild(Option_t *option="")
Using the current bin info, recompute the arrays for contents and errors.
Definition TH1.cxx:7122
virtual void SetBarOffset(Float_t offset=0.25)
Set the bar offset as fraction of the bin width for drawing mode "B".
Definition TH1.h:613
static Bool_t fgStatOverflows
! Flag to use under/overflows in statistics
Definition TH1.h:178
virtual Int_t FindLastBinAbove(Double_t threshold=0, Int_t axis=1, Int_t firstBin=1, Int_t lastBin=-1) const
Find last bin with content > threshold for axis (1=x, 2=y, 3=z) if no bins with content > threshold i...
Definition TH1.cxx:3774
TAxis * GetZaxis()
Definition TH1.h:574
Int_t DistancetoPrimitive(Int_t px, Int_t py) override
Compute distance from point px,py to a line.
Definition TH1.cxx:2794
virtual Bool_t Multiply(TF1 *f1, Double_t c1=1)
Performs the operation:
Definition TH1.cxx:6051
@ kXaxis
Definition TH1.h:123
@ kNoAxis
NOTE: Must always be 0 !!!
Definition TH1.h:122
@ kZaxis
Definition TH1.h:125
@ kYaxis
Definition TH1.h:124
Int_t fNcells
Number of bins(1D), cells (2D) +U/Overflows.
Definition TH1.h:150
virtual void GetStats(Double_t *stats) const
fill the array stats from the contents of this histogram The array stats must be correctly dimensione...
Definition TH1.cxx:7856
virtual void Normalize(Option_t *option="")
Normalize a histogram to its integral or to its maximum.
Definition TH1.cxx:6213
void Copy(TObject &hnew) const override
Copy this histogram structure to newth1.
Definition TH1.cxx:2642
void SetTitle(const char *title) override
Change/set the title.
Definition TH1.cxx:6753
Double_t fTsumw
Total Sum of weights.
Definition TH1.h:157
virtual Float_t GetBarWidth() const
Definition TH1.h:502
Double_t fTsumw2
Total Sum of squares of weights.
Definition TH1.h:158
static void StatOverflows(Bool_t flag=kTRUE)
if flag=kTRUE, underflows and overflows are used by the Fill functions in the computation of statisti...
Definition TH1.cxx:6960
virtual Float_t GetBarOffset() const
Definition TH1.h:501
TList * fFunctions
->Pointer to list of functions (fits and user)
Definition TH1.h:167
static Bool_t fgAddDirectory
! Flag to add histograms to the directory
Definition TH1.h:177
static TClass * Class()
static Int_t GetDefaultBufferSize()
Static function return the default buffer size for automatic histograms the parameter fgBufferSize ma...
Definition TH1.cxx:4383
virtual Double_t DoIntegral(Int_t ix1, Int_t ix2, Int_t iy1, Int_t iy2, Int_t iz1, Int_t iz2, Double_t &err, Option_t *opt, Bool_t doerr=kFALSE) const
Internal function compute integral and optionally the error between the limits specified by the bin n...
Definition TH1.cxx:8005
Double_t fTsumwx2
Total Sum of weight*X*X.
Definition TH1.h:160
virtual Double_t GetStdDev(Int_t axis=1) const
Returns the Standard Deviation (Sigma).
Definition TH1.cxx:7630
TH1()
Histogram default constructor.
Definition TH1.cxx:602
static TH1 * TransformHisto(TVirtualFFT *fft, TH1 *h_output, Option_t *option)
For a given transform (first parameter), fills the histogram (second parameter) with the transform ou...
Definition TH1.cxx:9344
void UseCurrentStyle() override
Copy current attributes from/to current style.
Definition TH1.cxx:7492
virtual void LabelsOption(Option_t *option="h", Option_t *axis="X")
Sort bins with labels or set option(s) to draw axis with labels.
Definition TH1.cxx:5384
virtual Int_t GetNbinsY() const
Definition TH1.h:543
Short_t fBarOffset
(1000*offset) for bar charts or legos
Definition TH1.h:154
virtual Double_t Chi2TestX(const TH1 *h2, Double_t &chi2, Int_t &ndf, Int_t &igood, Option_t *option="UU", Double_t *res=nullptr) const
The computation routine of the Chisquare test.
Definition TH1.cxx:2038
static bool CheckBinLimits(const TAxis *a1, const TAxis *a2)
Check bin limits.
Definition TH1.cxx:1510
virtual Double_t GetBinError(Int_t bin) const
Return value of error associated to bin number bin.
Definition TH1.cxx:9088
static Int_t FitOptionsMake(Option_t *option, Foption_t &Foption)
Decode string choptin and fill fitOption structure.
Definition TH1.cxx:4654
virtual Int_t GetNbinsZ() const
Definition TH1.h:544
virtual Double_t GetNormFactor() const
Definition TH1.h:546
virtual Double_t GetMean(Int_t axis=1) const
For axis = 1,2 or 3 returns the mean value of the histogram along X,Y or Z axis.
Definition TH1.cxx:7558
virtual Double_t GetSkewness(Int_t axis=1) const
Definition TH1.cxx:7694
virtual void ClearUnderflowAndOverflow()
Remove all the content from the underflow and overflow bins, without changing the number of entries A...
Definition TH1.cxx:2488
virtual void FillRandom(TF1 *f1, Int_t ntimes=5000, TRandom *rng=nullptr)
Definition TH1.cxx:3499
virtual Double_t GetContourLevelPad(Int_t level) const
Return the value of contour number "level" in Pad coordinates.
Definition TH1.cxx:8468
virtual TH1 * DrawNormalized(Option_t *option="", Double_t norm=1) const
Draw a normalized copy of this histogram.
Definition TH1.cxx:3115
@ kNeutral
Adapt to the global flag.
Definition TH1.h:133
virtual Int_t GetDimension() const
Definition TH1.h:528
void Streamer(TBuffer &) override
Stream a class object.
Definition TH1.cxx:6968
static void AddDirectory(Bool_t add=kTRUE)
Sets the flag controlling the automatic add of histograms in memory.
Definition TH1.cxx:1263
Double_t GetSumOfAllWeights(const bool includeOverflow) const
Return the sum of all weights.
Definition TH1.cxx:7942
@ kIsAverage
Bin contents are average (used by Add)
Definition TH1.h:410
@ kUserContour
User specified contour levels.
Definition TH1.h:405
@ kNoStats
Don't draw stats box.
Definition TH1.h:404
@ kAutoBinPTwo
different than 1.
Definition TH1.h:413
@ kIsNotW
Histogram is forced to be not weighted even when the histogram is filled with weighted.
Definition TH1.h:411
@ kIsHighlight
bit set if histo is highlight
Definition TH1.h:414
virtual void SetContourLevel(Int_t level, Double_t value)
Set value for one contour level.
Definition TH1.cxx:8550
virtual Bool_t CanExtendAllAxes() const
Returns true if all axes are extendable.
Definition TH1.cxx:6671
TDirectory * fDirectory
! Pointer to directory holding this histogram
Definition TH1.h:170
virtual void Reset(Option_t *option="")
Reset this histogram: contents, errors, etc.
Definition TH1.cxx:7138
void SetNameTitle(const char *name, const char *title) override
Change the name and title of this histogram.
Definition TH1.cxx:9002
TAxis * GetXaxis()
Definition TH1.h:572
virtual void GetBinXYZ(Int_t binglobal, Int_t &binx, Int_t &biny, Int_t &binz) const
Return binx, biny, binz corresponding to the global bin number globalbin see TH1::GetBin function abo...
Definition TH1.cxx:4976
TH1 * GetCumulative(Bool_t forward=kTRUE, const char *suffix="_cumulative") const
Return a pointer to a histogram containing the cumulative content.
Definition TH1.cxx:2587
static Double_t AutoP2GetPower2(Double_t x, Bool_t next=kTRUE)
Auxiliary function to get the power of 2 next (larger) or previous (smaller) a given x.
Definition TH1.cxx:1277
virtual Int_t GetNcells() const
Definition TH1.h:545
virtual Int_t ShowPeaks(Double_t sigma=2, Option_t *option="", Double_t threshold=0.05)
Interface to TSpectrum::Search.
Definition TH1.cxx:9326
static Bool_t RecomputeAxisLimits(TAxis &destAxis, const TAxis &anAxis)
Finds new limits for the axis for the Merge function.
Definition TH1.cxx:5910
virtual Double_t GetSumOfWeights() const
Return the sum of weights across all bins excluding under/overflows.
Definition TH1.h:560
virtual void PutStats(Double_t *stats)
Replace current statistics with the values in array stats.
Definition TH1.cxx:7907
TVirtualHistPainter * GetPainter(Option_t *option="")
Return pointer to painter.
Definition TH1.cxx:4488
TObject * FindObject(const char *name) const override
Search object named name in the list of functions.
Definition TH1.cxx:3834
void Print(Option_t *option="") const override
Print some global quantities for this histogram.
Definition TH1.cxx:7044
static Bool_t GetDefaultSumw2()
Return kTRUE if TH1::Sumw2 must be called when creating new histograms.
Definition TH1.cxx:4392
virtual Int_t FindFirstBinAbove(Double_t threshold=0, Int_t axis=1, Int_t firstBin=1, Int_t lastBin=-1) const
Find first bin with content > threshold for axis (1=x, 2=y, 3=z) if no bins with content > threshold ...
Definition TH1.cxx:3711
virtual TFitResultPtr Fit(const char *formula, Option_t *option="", Option_t *goption="", Double_t xmin=0, Double_t xmax=0)
Fit histogram with function fname.
Definition TH1.cxx:3875
virtual Int_t GetBin(Int_t binx, Int_t biny=0, Int_t binz=0) const
Return Global bin number corresponding to binx,y,z.
Definition TH1.cxx:4963
virtual Double_t GetMaximum(Double_t maxval=FLT_MAX) const
Return maximum value smaller than maxval of bins in the range, unless the value has been overridden b...
Definition TH1.cxx:8573
virtual Int_t GetNbinsX() const
Definition TH1.h:542
virtual void SetMaximum(Double_t maximum=-1111)
Definition TH1.h:653
virtual TH1 * FFT(TH1 *h_output, Option_t *option)
This function allows to do discrete Fourier transforms of TH1 and TH2.
Definition TH1.cxx:3255
virtual void LabelsInflate(Option_t *axis="X")
Double the number of bins for axis.
Definition TH1.cxx:5317
virtual TH1 * ShowBackground(Int_t niter=20, Option_t *option="same")
This function calculates the background spectrum in this histogram.
Definition TH1.cxx:9312
static Bool_t SameLimitsAndNBins(const TAxis &axis1, const TAxis &axis2)
Same limits and bins.
Definition TH1.cxx:5900
virtual Bool_t Add(TF1 *h1, Double_t c1=1, Option_t *option="")
Performs the operation: this = this + c1*f1 if errors are defined (see TH1::Sumw2),...
Definition TH1.cxx:813
Double_t fMaximum
Maximum value for plotting.
Definition TH1.h:161
Int_t fBufferSize
fBuffer size
Definition TH1.h:168
TString ProvideSaveName(Option_t *option, Bool_t testfdir=kFALSE)
Provide variable name for histogram for saving as primitive Histogram pointer has by default the hist...
Definition TH1.cxx:7277
virtual Double_t IntegralAndError(Int_t binx1, Int_t binx2, Double_t &err, Option_t *option="") const
Return integral of bin contents in range [binx1,binx2] and its error.
Definition TH1.cxx:7996
Int_t fDimension
! Histogram dimension (1, 2 or 3 dim)
Definition TH1.h:171
virtual void SetBinError(Int_t bin, Double_t error)
Set the bin Error Note that this resets the bin eror option to be of Normal Type and for the non-empt...
Definition TH1.cxx:9231
EBinErrorOpt fBinStatErrOpt
Option for bin statistical errors.
Definition TH1.h:174
static Int_t fgBufferSize
! Default buffer size for automatic histograms
Definition TH1.h:176
virtual void SetBinsLength(Int_t=-1)
Definition TH1.h:629
Double_t fNormFactor
Normalization factor.
Definition TH1.h:163
@ kFullyConsistent
Definition TH1.h:139
@ kDifferentNumberOfBins
Definition TH1.h:143
@ kDifferentDimensions
Definition TH1.h:144
@ kDifferentBinLimits
Definition TH1.h:141
@ kDifferentAxisLimits
Definition TH1.h:142
@ kDifferentLabels
Definition TH1.h:140
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition TH1.cxx:3315
TAxis * GetYaxis()
Definition TH1.h:573
TArrayD fContour
Array to display contour levels.
Definition TH1.h:164
virtual Double_t GetBinErrorLow(Int_t bin) const
Return lower error associated to bin number bin.
Definition TH1.cxx:9104
void Browse(TBrowser *b) override
Browse the Histogram object.
Definition TH1.cxx:749
virtual void SetContent(const Double_t *content)
Replace bin contents by the contents of array content.
Definition TH1.cxx:8426
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3037
virtual void SavePrimitiveHelp(std::ostream &out, const char *hname, Option_t *option="")
Helper function for the SavePrimitive functions from TH1 or classes derived from TH1,...
Definition TH1.cxx:7404
Short_t fBarWidth
(1000*width) for bar charts or legos
Definition TH1.h:155
virtual Double_t GetBinErrorSqUnchecked(Int_t bin) const
Definition TH1.h:706
Int_t AxisChoice(Option_t *axis) const
Choose an axis according to "axis".
Definition Haxis.cxx:14
virtual void SetMinimum(Double_t minimum=-1111)
Definition TH1.h:654
Bool_t IsBinUnderflow(Int_t bin, Int_t axis=0) const
Return true if the bin is underflow.
Definition TH1.cxx:5216
void SavePrimitive(std::ostream &out, Option_t *option="") override
Save primitive as a C++ statement(s) on output stream out.
Definition TH1.cxx:7299
static bool CheckBinLabels(const TAxis *a1, const TAxis *a2)
Check that axis have same labels.
Definition TH1.cxx:1537
virtual Double_t Interpolate(Double_t x) const
Given a point x, approximates the value via linear interpolation based on the two nearest bin centers...
Definition TH1.cxx:5117
static void SetDefaultSumw2(Bool_t sumw2=kTRUE)
When this static function is called with sumw2=kTRUE, all new histograms will automatically activate ...
Definition TH1.cxx:6738
virtual void SetBuffer(Int_t bufsize, Option_t *option="")
Set the maximum number of entries to be kept in the buffer.
Definition TH1.cxx:8486
Bool_t IsBinOverflow(Int_t bin, Int_t axis=0) const
Return true if the bin is overflow.
Definition TH1.cxx:5184
UInt_t GetAxisLabelStatus() const
Internal function used in TH1::Fill to see which axis is full alphanumeric, i.e.
Definition TH1.cxx:6710
Double_t * fIntegral
! Integral of bins used by GetRandom
Definition TH1.h:172
Double_t fMinimum
Minimum value for plotting.
Definition TH1.h:162
virtual Double_t Integral(Option_t *option="") const
Return integral of bin contents.
Definition TH1.cxx:7969
static void SetDefaultBufferSize(Int_t bufsize=1000)
Static function to set the default buffer size for automatic histograms.
Definition TH1.cxx:6728
virtual void SetBinContent(Int_t bin, Double_t content)
Set bin content see convention for numbering bins in TH1::GetBin In case the bin number is greater th...
Definition TH1.cxx:9247
virtual void DirectoryAutoAdd(TDirectory *)
Perform the automatic addition of the histogram to the given directory.
Definition TH1.cxx:2772
virtual void GetLowEdge(Double_t *edge) const
Fill array with low edge of bins for 1D histogram Better to use h1.GetXaxis()->GetLowEdge(edge)
Definition TH1.cxx:9212
virtual Double_t GetBinLowEdge(Int_t bin) const
Return bin lower edge for 1D histogram.
Definition TH1.cxx:9177
void Build()
Creates histogram basic data structure.
Definition TH1.cxx:758
virtual Double_t GetEntries() const
Return the current number of entries.
Definition TH1.cxx:4400
virtual Double_t RetrieveBinContent(Int_t bin) const =0
Raw retrieval of bin content on internal data structure see convention for numbering bins in TH1::Get...
virtual TF1 * GetFunction(const char *name) const
Return pointer to function with name.
Definition TH1.cxx:9076
virtual TH1 * Rebin(Int_t ngroup=2, const char *newname="", const Double_t *xbins=nullptr)
Rebin this histogram.
Definition TH1.cxx:6310
virtual Int_t BufferFill(Double_t x, Double_t w)
accumulate arguments in buffer.
Definition TH1.cxx:1475
virtual Double_t GetBinWithContent(Double_t c, Int_t &binx, Int_t firstx=0, Int_t lastx=0, Double_t maxdiff=0) const
Compute first binx in the range [firstx,lastx] for which diff = abs(bin_content-c) <= maxdiff.
Definition TH1.cxx:5088
virtual UInt_t SetCanExtend(UInt_t extendBitMask)
Make the histogram axes extendable / not extendable according to the bit mask returns the previous bi...
Definition TH1.cxx:6684
TList * GetListOfFunctions() const
Definition TH1.h:489
void SetName(const char *name) override
Change the name of this histogram.
Definition TH1.cxx:8988
virtual TH1 * DrawCopy(Option_t *option="", const char *name_postfix="_copy") const
Copy this histogram and Draw in the current pad.
Definition TH1.cxx:3084
Bool_t IsEmpty() const
Check if a histogram is empty (this is a protected method used mainly by TH1Merger )
Definition TH1.cxx:5166
virtual Double_t GetMeanError(Int_t axis=1) const
Return standard error of mean of this histogram along the X axis.
Definition TH1.cxx:7598
void Paint(Option_t *option="") override
Control routine to paint any kind of histograms.
Definition TH1.cxx:6241
virtual Double_t AndersonDarlingTest(const TH1 *h2, Option_t *option="") const
Statistical test of compatibility in shape between this histogram and h2, using the Anderson-Darling ...
Definition TH1.cxx:8090
virtual void ResetStats()
Reset the statistics including the number of entries and replace with values calculated from bin cont...
Definition TH1.cxx:7925
virtual void SetBinErrorOption(EBinErrorOpt type)
Definition TH1.h:630
virtual void DrawPanel()
Display a panel with all histogram drawing options.
Definition TH1.cxx:3146
virtual Double_t GetRandom(TRandom *rng=nullptr) const
Return a random number distributed according the histogram bin contents.
Definition TH1.cxx:5012
virtual Double_t Chisquare(TF1 *f1, Option_t *option="") const
Compute and return the chisquare of this histogram with respect to a function The chisquare is comput...
Definition TH1.cxx:2467
virtual Double_t Chi2Test(const TH1 *h2, Option_t *option="UU", Double_t *res=nullptr) const
test for comparing weighted and unweighted histograms.
Definition TH1.cxx:1979
virtual void DoFillN(Int_t ntimes, const Double_t *x, const Double_t *w, Int_t stride=1)
Internal method to fill histogram content from a vector called directly by TH1::BufferEmpty.
Definition TH1.cxx:3444
virtual void GetMinimumAndMaximum(Double_t &min, Double_t &max) const
Retrieve the minimum and maximum values in the histogram.
Definition TH1.cxx:8759
@ kNstat
Size of statistics data (up to TProfile3D)
Definition TH1.h:423
virtual Int_t GetMaximumBin() const
Return location of bin with maximum value in the range.
Definition TH1.cxx:8605
static Int_t AutoP2GetBins(Int_t n)
Auxiliary function to get the next power of 2 integer value larger then n.
Definition TH1.cxx:1290
Double_t fEntries
Number of entries.
Definition TH1.h:156
virtual Long64_t Merge(TCollection *list)
Definition TH1.h:593
virtual void SetColors(Color_t linecolor=-1, Color_t markercolor=-1, Color_t fillcolor=-1)
Shortcut to set the three histogram colors with a single call.
Definition TH1.cxx:4444
void ExecuteEvent(Int_t event, Int_t px, Int_t py) override
Execute action corresponding to one event.
Definition TH1.cxx:3211
virtual Double_t * GetIntegral()
Return a pointer to the array of bins integral.
Definition TH1.cxx:2557
TAxis fZaxis
Z axis descriptor.
Definition TH1.h:153
EStatOverflows fStatOverflows
Per object flag to use under/overflows in statistics.
Definition TH1.h:175
TClass * IsA() const override
Definition TH1.h:694
virtual void FillN(Int_t ntimes, const Double_t *x, const Double_t *w, Int_t stride=1)
Fill this histogram with an array x and weights w.
Definition TH1.cxx:3418
static bool CheckEqualAxes(const TAxis *a1, const TAxis *a2)
Check that the axis are the same.
Definition TH1.cxx:1580
@ kPoisson2
Errors from Poisson interval at 95% CL (~ 2 sigma)
Definition TH1.h:117
@ kNormal
Errors with Normal (Wald) approximation: errorUp=errorLow= sqrt(N)
Definition TH1.h:115
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
Definition TH1.cxx:5063
virtual Int_t GetContour(Double_t *levels=nullptr)
Return contour values into array levels if pointer levels is non zero.
Definition TH1.cxx:8439
TAxis fXaxis
X axis descriptor.
Definition TH1.h:151
virtual Bool_t IsHighlight() const
Definition TH1.h:586
virtual void ExtendAxis(Double_t x, TAxis *axis)
Histogram is resized along axis such that x is in the axis range.
Definition TH1.cxx:6539
virtual Double_t GetBinWidth(Int_t bin) const
Return bin width for 1D histogram.
Definition TH1.cxx:9188
TArrayD fSumw2
Array of sum of squares of weights.
Definition TH1.h:165
TH1 * GetAsymmetry(TH1 *h2, Double_t c2=1, Double_t dc2=0)
Return a histogram containing the asymmetry of this histogram with h2, where the asymmetry is defined...
Definition TH1.cxx:4316
virtual Double_t GetContourLevel(Int_t level) const
Return value of contour number level.
Definition TH1.cxx:8458
virtual void SetContour(Int_t nlevels, const Double_t *levels=nullptr)
Set the number and values of contour levels.
Definition TH1.cxx:8511
virtual void SetHighlight(Bool_t set=kTRUE)
Set highlight (enable/disable) mode for the histogram by default highlight mode is disable.
Definition TH1.cxx:4459
virtual Double_t GetBinErrorUp(Int_t bin) const
Return upper error associated to bin number bin.
Definition TH1.cxx:9135
virtual void Scale(Double_t c1=1, Option_t *option="")
Multiply this histogram by a constant c1.
Definition TH1.cxx:6639
virtual Int_t GetMinimumBin() const
Return location of bin with minimum value in the range.
Definition TH1.cxx:8693
virtual Int_t GetSumw2N() const
Definition TH1.h:563
virtual Int_t FindBin(Double_t x, Double_t y=0, Double_t z=0)
Return Global bin number corresponding to x,y,z.
Definition TH1.cxx:3649
Bool_t GetStatOverflowsBehaviour() const
Definition TH1.h:392
void SaveAs(const char *filename="hist", Option_t *option="") const override
Save the histogram as .csv, .tsv or .txt.
Definition TH1.cxx:7216
virtual Int_t GetQuantiles(Int_t n, Double_t *xp, const Double_t *p=nullptr)
Compute Quantiles for this histogram.
Definition TH1.cxx:4592
virtual void AddBinContent(Int_t bin)=0
Increment bin content by 1.
TObject * Clone(const char *newname="") const override
Make a complete copy of the underlying object.
Definition TH1.cxx:2723
virtual Double_t GetStdDevError(Int_t axis=1) const
Return error of standard deviation estimation for Normal distribution.
Definition TH1.cxx:7678
virtual Bool_t Divide(TF1 *f1, Double_t c1=1)
Performs the operation: this = this/(c1*f1) if errors are defined (see TH1::Sumw2),...
Definition TH1.cxx:2811
virtual Double_t GetMinimum(Double_t minval=-FLT_MAX) const
Return minimum value larger than minval of bins in the range, unless the value has been overridden by...
Definition TH1.cxx:8663
int LoggedInconsistency(const char *name, const TH1 *h1, const TH1 *h2, bool useMerge=false) const
Definition TH1.cxx:870
static bool CheckConsistentSubAxes(const TAxis *a1, Int_t firstBin1, Int_t lastBin1, const TAxis *a2, Int_t firstBin2=0, Int_t lastBin2=0)
Check that two sub axis are the same.
Definition TH1.cxx:1609
static Int_t CheckConsistency(const TH1 *h1, const TH1 *h2)
Check histogram compatibility.
Definition TH1.cxx:1648
void RecursiveRemove(TObject *obj) override
Recursively remove object from the list of functions.
Definition TH1.cxx:6611
TAxis fYaxis
Y axis descriptor.
Definition TH1.h:152
virtual Double_t KolmogorovTest(const TH1 *h2, Option_t *option="") const
Statistical test of compatibility in shape between this histogram and h2, using Kolmogorov test.
Definition TH1.cxx:8206
static void SmoothArray(Int_t NN, Double_t *XX, Int_t ntimes=1)
Smooth array xx, translation of Hbook routine hsmoof.F.
Definition TH1.cxx:6803
virtual void GetCenter(Double_t *center) const
Fill array with center of bins for 1D histogram Better to use h1.GetXaxis()->GetCenter(center)
Definition TH1.cxx:9199
TVirtualHistPainter * fPainter
! Pointer to histogram painter
Definition TH1.h:173
virtual void SetBins(Int_t nx, Double_t xmin, Double_t xmax)
Redefine x axis parameters.
Definition TH1.cxx:8795
virtual Int_t FindFixBin(Double_t x, Double_t y=0, Double_t z=0) const
Return Global bin number corresponding to x,y,z.
Definition TH1.cxx:3682
virtual void Sumw2(Bool_t flag=kTRUE)
Create structure to store sum of squares of weights.
Definition TH1.cxx:9048
virtual void SetEntries(Double_t n)
Definition TH1.h:640
virtual Bool_t FindNewAxisLimits(const TAxis *axis, const Double_t point, Double_t &newMin, Double_t &newMax)
finds new limits for the axis so that point is within the range and the limits are compatible with th...
Definition TH1.cxx:6495
static bool CheckAxisLimits(const TAxis *a1, const TAxis *a2)
Check that the axis limits of the histograms are the same.
Definition TH1.cxx:1566
static Bool_t AddDirectoryStatus()
Static function: cannot be inlined on Windows/NT.
Definition TH1.cxx:741
static Bool_t fgDefaultSumw2
! Flag to call TH1::Sumw2 automatically at histogram creation time
Definition TH1.h:179
static void SavePrimitiveFunctions(std::ostream &out, const char *varname, TList *lst)
Save list of functions Also can be used by TGraph classes.
Definition TH1.cxx:7458
virtual void UpdateBinContent(Int_t bin, Double_t content)=0
Raw update of bin content on internal data structure see convention for numbering bins in TH1::GetBin...
Double_t fTsumwx
Total Sum of weight*X.
Definition TH1.h:159
virtual void LabelsDeflate(Option_t *axis="X")
Reduce the number of bins for the axis passed in the option to the number of bins having a label.
Definition TH1.cxx:5247
virtual Double_t ComputeIntegral(Bool_t onlyPositive=false)
Compute integral (normalized cumulative sum of bins) w/o under/overflows The result is stored in fInt...
Definition TH1.cxx:2508
TString fOption
Histogram options.
Definition TH1.h:166
virtual void Eval(TF1 *f1, Option_t *option="")
Evaluate function f1 at the center of bins of this histogram.
Definition TH1.cxx:3163
virtual void SetBarWidth(Float_t width=0.5)
Set the width of bars as fraction of the bin width for drawing mode "B".
Definition TH1.h:614
virtual Int_t BufferEmpty(Int_t action=0)
Fill histogram with all entries in the buffer.
Definition TH1.cxx:1383
virtual void SetStats(Bool_t stats=kTRUE)
Set statistics option on/off.
Definition TH1.cxx:9018
virtual Double_t GetKurtosis(Int_t axis=1) const
Definition TH1.cxx:7767
2-D histogram with a double per channel (see TH1 documentation)
Definition TH2.h:356
static THLimitsFinder * GetLimitsFinder()
Return pointer to the current finder.
THashList implements a hybrid collection class consisting of a hash table and a list to store TObject...
Definition THashList.h:34
void Clear(Option_t *option="") override
Remove all objects from the list.
A doubly linked list.
Definition TList.h:38
void Streamer(TBuffer &) override
Stream all objects in the collection to or from the I/O buffer.
Definition TList.cxx:1190
TObject * FindObject(const char *name) const override
Find an object in this list using its name.
Definition TList.cxx:575
void RecursiveRemove(TObject *obj) override
Remove object from this collection and recursively remove the object from all other objects (and coll...
Definition TList.cxx:761
void Add(TObject *obj) override
Definition TList.h:81
TObject * Remove(TObject *obj) override
Remove object from the list.
Definition TList.cxx:819
TObject * First() const override
Return the first object in the list. Returns 0 when list is empty.
Definition TList.cxx:656
void Delete(Option_t *option="") override
Remove all objects from the list AND delete all heap based objects.
Definition TList.cxx:467
TObject * At(Int_t idx) const override
Returns the object at position idx. Returns 0 if idx is out of range.
Definition TList.cxx:354
The TNamed class is the base class for all named ROOT classes.
Definition TNamed.h:29
void Copy(TObject &named) const override
Copy this to obj.
Definition TNamed.cxx:93
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition TNamed.cxx:173
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
void Streamer(TBuffer &) override
Stream an object of class TObject.
const char * GetTitle() const override
Returns title of object.
Definition TNamed.h:50
TString fTitle
Definition TNamed.h:33
TString fName
Definition TNamed.h:32
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition TNamed.cxx:149
Mother of all ROOT objects.
Definition TObject.h:41
virtual const char * GetName() const
Returns name of object.
Definition TObject.cxx:457
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition TObject.h:202
virtual UInt_t GetUniqueID() const
Return the unique object id.
Definition TObject.cxx:475
static TString SavePrimitiveVector(std::ostream &out, const char *prefix, Int_t len, Double_t *arr, Bool_t empty_line=kFALSE)
Save array in the output stream "out" as vector.
Definition TObject.cxx:788
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition TObject.cxx:226
virtual void UseCurrentStyle()
Set current style settings in this object This function is called when either TCanvas::UseCurrentStyl...
Definition TObject.cxx:885
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition TObject.cxx:1057
virtual void AppendPad(Option_t *option="")
Append graphics object to current pad.
Definition TObject.cxx:203
virtual void SaveAs(const char *filename="", Option_t *option="") const
Save this object in the file specified by filename.
Definition TObject.cxx:705
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition TObject.cxx:864
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition TObject.cxx:543
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:1071
virtual void SetUniqueID(UInt_t uid)
Set the unique object id.
Definition TObject.cxx:875
static void SavePrimitiveDraw(std::ostream &out, const char *variable_name, Option_t *option=nullptr)
Save invocation of primitive Draw() method Skipped if option contains "nodraw" string.
Definition TObject.cxx:822
void ResetBit(UInt_t f)
Definition TObject.h:201
@ kCanDelete
if object in a list can be deleted
Definition TObject.h:68
@ kInvalidObject
if object ctor succeeded but object should not be used
Definition TObject.h:78
@ kMustCleanup
if object destructor must call RecursiveRemove()
Definition TObject.h:70
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition TObject.cxx:1045
Longptr_t ExecPlugin(int nargs)
Int_t LoadPlugin()
Load the plugin library for this handler.
static TClass * Class()
This is the base class for the ROOT Random number generators.
Definition TRandom.h:27
Double_t Rndm() override
Machine independent random number generator.
Definition TRandom.cxx:558
virtual Double_t PoissonD(Double_t mean)
Generates a random number according to a Poisson law.
Definition TRandom.cxx:460
virtual ULong64_t Poisson(Double_t mean)
Generates a random integer N according to a Poisson law.
Definition TRandom.cxx:403
Basic string class.
Definition TString.h:138
Ssiz_t Length() const
Definition TString.h:425
void ToLower()
Change string to lower-case.
Definition TString.cxx:1189
TString & ReplaceSpecialCppChars()
Find special characters which are typically used in printf() calls and replace them by appropriate es...
Definition TString.cxx:1121
const char * Data() const
Definition TString.h:384
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition TString.h:712
void ToUpper()
Change string to upper case.
Definition TString.cxx:1202
Bool_t IsNull() const
Definition TString.h:422
virtual void Streamer(TBuffer &)
Stream a string object.
Definition TString.cxx:1418
TString & Append(const char *cs)
Definition TString.h:580
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2384
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:640
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition TString.h:659
Int_t GetOptStat() const
Definition TStyle.h:247
void SetOptStat(Int_t stat=1)
The type of information printed in the histogram statistics box can be selected via the parameter mod...
Definition TStyle.cxx:1641
void SetHistFillColor(Color_t color=1)
Definition TStyle.h:383
Color_t GetHistLineColor() const
Definition TStyle.h:235
Bool_t IsReading() const
Definition TStyle.h:300
Float_t GetBarOffset() const
Definition TStyle.h:184
void SetHistLineStyle(Style_t styl=0)
Definition TStyle.h:386
Style_t GetHistFillStyle() const
Definition TStyle.h:236
Color_t GetHistFillColor() const
Definition TStyle.h:234
Float_t GetBarWidth() const
Definition TStyle.h:185
Bool_t GetCanvasPreferGL() const
Definition TStyle.h:189
void SetHistLineColor(Color_t color=1)
Definition TStyle.h:384
void SetBarOffset(Float_t baroff=0.5)
Definition TStyle.h:339
Style_t GetHistLineStyle() const
Definition TStyle.h:237
void SetBarWidth(Float_t barwidth=0.5)
Definition TStyle.h:340
void SetHistFillStyle(Style_t styl=0)
Definition TStyle.h:385
Width_t GetHistLineWidth() const
Definition TStyle.h:238
Int_t GetOptFit() const
Definition TStyle.h:246
void SetHistLineWidth(Width_t width=1)
Definition TStyle.h:387
TVectorT.
Definition TVectorT.h:29
TVirtualFFT is an interface class for Fast Fourier Transforms.
Definition TVirtualFFT.h:88
static TVirtualFFT * FFT(Int_t ndim, Int_t *n, Option_t *option)
Returns a pointer to the FFT of requested size and type.
static TVirtualFFT * SineCosine(Int_t ndim, Int_t *n, Int_t *r2rkind, Option_t *option)
Returns a pointer to a sine or cosine transform of requested size and kind.
Abstract Base Class for Fitting.
static TVirtualFitter * GetFitter()
static: return the current Fitter
Abstract interface to a histogram painter.
virtual void DrawPanel()=0
Int_t DistancetoPrimitive(Int_t px, Int_t py) override=0
Computes distance from point (px,py) to the object.
void ExecuteEvent(Int_t event, Int_t px, Int_t py) override=0
Execute action corresponding to an event at (px,py).
virtual void SetHighlight()=0
static TVirtualHistPainter * HistPainter(TH1 *obj)
Static function returning a pointer to the current histogram painter.
void Paint(Option_t *option="") override=0
This method must be overridden if a class wants to paint itself.
double gamma_quantile_c(double z, double alpha, double theta)
Inverse ( ) of the cumulative distribution function of the upper tail of the gamma distribution (gamm...
double gamma_quantile(double z, double alpha, double theta)
Inverse ( ) of the cumulative distribution function of the lower tail of the gamma distribution (gamm...
const Double_t sigma
Double_t y[n]
Definition legend1.C:17
return c1
Definition legend1.C:41
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
TH1F * h1
Definition legend1.C:5
TF1 * f1
Definition legend1.C:11
return c2
Definition legend2.C:14
R__ALWAYS_INLINE bool HasBeenDeleted(const TObject *obj)
Check if the TObject's memory has been deleted.
Definition TObject.h:405
TFitResultPtr FitObject(TH1 *h1, TF1 *f1, Foption_t &option, const ROOT::Math::MinimizerOptions &moption, const char *goption, ROOT::Fit::DataRange &range)
fitting function for a TH1 (called from TH1::Fit)
Definition HFitImpl.cxx:977
double Chisquare(const TH1 &h1, TF1 &f1, bool useRange, EChisquareType type)
compute the chi2 value for an histogram given a function (see TH1::Chisquare for the documentation)
void FitOptionsMake(EFitObjectType type, const char *option, Foption_t &fitOption)
Decode list of options into fitOption.
Definition HFitImpl.cxx:685
void FillData(BinData &dv, const TH1 *hist, TF1 *func=nullptr)
fill the data vector from a TH1.
R__EXTERN TVirtualRWMutex * gCoreMutex
Bool_t IsNaN(Double_t x)
Definition TMath.h:903
Int_t Nint(T x)
Round to nearest integer. Rounds half integers to the nearest even integer.
Definition TMath.h:704
Short_t Max(Short_t a, Short_t b)
Returns the largest of a and b.
Definition TMathBase.h:251
Double_t Prob(Double_t chi2, Int_t ndf)
Computation of the probability for a certain Chi-squared (chi2) and number of degrees of freedom (ndf...
Definition TMath.cxx:637
Double_t Median(Long64_t n, const T *a, const Double_t *w=nullptr, Long64_t *work=nullptr)
Same as RMS.
Definition TMath.h:1359
Double_t QuietNaN()
Returns a quiet NaN as defined by IEEE 754.
Definition TMath.h:913
Double_t Floor(Double_t x)
Rounds x downward, returning the largest integral value that is not greater than x.
Definition TMath.h:691
Double_t ATan(Double_t)
Returns the principal value of the arc tangent of x, expressed in radians.
Definition TMath.h:651
Double_t Ceil(Double_t x)
Rounds x upward, returning the smallest integral value that is not less than x.
Definition TMath.h:679
T MinElement(Long64_t n, const T *a)
Returns minimum of array a of length n.
Definition TMath.h:971
Double_t Log(Double_t x)
Returns the natural logarithm of x.
Definition TMath.h:767
Double_t Sqrt(Double_t x)
Returns the square root of x.
Definition TMath.h:673
Short_t Min(Short_t a, Short_t b)
Returns the smallest of a and b.
Definition TMathBase.h:199
constexpr Double_t Pi()
Definition TMath.h:40
Bool_t AreEqualRel(Double_t af, Double_t bf, Double_t relPrec)
Comparing floating points.
Definition TMath.h:429
Bool_t AreEqualAbs(Double_t af, Double_t bf, Double_t epsilon)
Comparing floating points.
Definition TMath.h:421
Double_t KolmogorovProb(Double_t z)
Calculates the Kolmogorov distribution function,.
Definition TMath.cxx:679
void Sort(Index n, const Element *a, Index *index, Bool_t down=kTRUE)
Sort the n elements of the array a of generic templated type Element.
Definition TMathBase.h:432
Long64_t BinarySearch(Long64_t n, const T *array, T value)
Binary search in an array of n values to locate value.
Definition TMathBase.h:348
Double_t Log10(Double_t x)
Returns the common (base-10) logarithm of x.
Definition TMath.h:773
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
Definition TMathBase.h:124
Double_t Infinity()
Returns an infinity as defined by the IEEE standard.
Definition TMath.h:928
th1 Draw()
TMarker m
Definition textangle.C:8
TLine l
Definition textangle.C:4
static uint64_t sum(uint64_t i)
Definition Factory.cxx:2339