Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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.
469
470
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
516
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
599
600////////////////////////////////////////////////////////////////////////////////
601/// Histogram default constructor.
602
604{
605 fDirectory = nullptr;
606 fFunctions = new TList;
607 fNcells = 0;
608 fIntegral = nullptr;
609 fPainter = nullptr;
610 fEntries = 0;
611 fNormFactor = 0;
613 fMaximum = -1111;
614 fMinimum = -1111;
615 fBufferSize = 0;
616 fBuffer = nullptr;
619 fXaxis.SetName("xaxis");
620 fYaxis.SetName("yaxis");
621 fZaxis.SetName("zaxis");
622 fXaxis.SetParent(this);
623 fYaxis.SetParent(this);
624 fZaxis.SetParent(this);
626}
627
628////////////////////////////////////////////////////////////////////////////////
629/// Histogram default destructor.
630
632{
634 return;
635 }
636 delete[] fIntegral;
637 fIntegral = nullptr;
638 delete[] fBuffer;
639 fBuffer = nullptr;
640 if (fFunctions) {
642
644 TObject* obj = nullptr;
645 //special logic to support the case where the same object is
646 //added multiple times in fFunctions.
647 //This case happens when the same object is added with different
648 //drawing modes
649 //In the loop below we must be careful with objects (eg TCutG) that may
650 // have been added to the list of functions of several histograms
651 //and may have been already deleted.
652 while ((obj = fFunctions->First())) {
653 while(fFunctions->Remove(obj)) { }
655 break;
656 }
657 delete obj;
658 obj = nullptr;
659 }
660 delete fFunctions;
661 fFunctions = nullptr;
662 }
663 if (fDirectory) {
664 fDirectory->Remove(this);
665 fDirectory = nullptr;
666 }
667 delete fPainter;
668 fPainter = nullptr;
669}
670
671////////////////////////////////////////////////////////////////////////////////
672/// Constructor for fix bin size histograms.
673/// Creates the main histogram structure.
674///
675/// \param[in] name name of histogram (avoid blanks)
676/// \param[in] title histogram title.
677/// If title is of the form `stringt;stringx;stringy;stringz`,
678/// the histogram title is set to `stringt`,
679/// the x axis title to `stringx`, the y axis title to `stringy`, etc.
680/// \param[in] nbins number of bins
681/// \param[in] xlow low edge of first bin
682/// \param[in] xup upper edge of last bin (not included in last bin)
683
684
685TH1::TH1(const char *name,const char *title,Int_t nbins,Double_t xlow,Double_t xup)
686 :TNamed(name,title)
687{
688 Build();
689 if (nbins <= 0) {Warning("TH1","nbins is <=0 - set to nbins = 1"); nbins = 1; }
690 fXaxis.Set(nbins,xlow,xup);
691 fNcells = fXaxis.GetNbins()+2;
692}
693
694////////////////////////////////////////////////////////////////////////////////
695/// Constructor for variable bin size histograms using an input array of type float.
696/// Creates the main histogram structure.
697///
698/// \param[in] name name of histogram (avoid blanks)
699/// \param[in] title histogram title.
700/// If title is of the form `stringt;stringx;stringy;stringz`
701/// the histogram title is set to `stringt`,
702/// the x axis title to `stringx`, the y axis title to `stringy`, etc.
703/// \param[in] nbins number of bins
704/// \param[in] xbins array of low-edges for each bin.
705/// This is an array of type float and size nbins+1
706
707TH1::TH1(const char *name,const char *title,Int_t nbins,const Float_t *xbins)
708 :TNamed(name,title)
709{
710 Build();
711 if (nbins <= 0) {Warning("TH1","nbins is <=0 - set to nbins = 1"); nbins = 1; }
712 if (xbins) fXaxis.Set(nbins,xbins);
713 else fXaxis.Set(nbins,0,1);
714 fNcells = fXaxis.GetNbins()+2;
715}
716
717////////////////////////////////////////////////////////////////////////////////
718/// Constructor for variable bin size histograms using an input array of type double.
719///
720/// \param[in] name name of histogram (avoid blanks)
721/// \param[in] title histogram title.
722/// If title is of the form `stringt;stringx;stringy;stringz`
723/// the histogram title is set to `stringt`,
724/// the x axis title to `stringx`, the y axis title to `stringy`, etc.
725/// \param[in] nbins number of bins
726/// \param[in] xbins array of low-edges for each bin.
727/// This is an array of type double and size nbins+1
728
729TH1::TH1(const char *name,const char *title,Int_t nbins,const Double_t *xbins)
730 :TNamed(name,title)
731{
732 Build();
733 if (nbins <= 0) {Warning("TH1","nbins is <=0 - set to nbins = 1"); nbins = 1; }
734 if (xbins) fXaxis.Set(nbins,xbins);
735 else fXaxis.Set(nbins,0,1);
736 fNcells = fXaxis.GetNbins()+2;
737}
738
739////////////////////////////////////////////////////////////////////////////////
740/// Static function: cannot be inlined on Windows/NT.
741
746
747////////////////////////////////////////////////////////////////////////////////
748/// Browse the Histogram object.
749
751{
752 Draw(b ? b->GetDrawOption() : "");
753 gPad->Update();
754}
755
756////////////////////////////////////////////////////////////////////////////////
757/// Creates histogram basic data structure.
758
760{
761 fDirectory = nullptr;
762 fPainter = nullptr;
763 fIntegral = nullptr;
764 fEntries = 0;
765 fNormFactor = 0;
767 fMaximum = -1111;
768 fMinimum = -1111;
769 fBufferSize = 0;
770 fBuffer = nullptr;
773 fXaxis.SetName("xaxis");
774 fYaxis.SetName("yaxis");
775 fZaxis.SetName("zaxis");
776 fYaxis.Set(1,0.,1.);
777 fZaxis.Set(1,0.,1.);
778 fXaxis.SetParent(this);
779 fYaxis.SetParent(this);
780 fZaxis.SetParent(this);
781
783
784 fFunctions = new TList;
785
787
790 if (fDirectory) {
792 fDirectory->Append(this,kTRUE);
793 }
794 }
795}
796
797////////////////////////////////////////////////////////////////////////////////
798/// Performs the operation: `this = this + c1*f1`
799/// if errors are defined (see TH1::Sumw2), errors are also recalculated.
800///
801/// By default, the function is computed at the centre of the bin.
802/// if option "I" is specified (1-d histogram only), the integral of the
803/// function in each bin is used instead of the value of the function at
804/// the centre of the bin.
805///
806/// Only bins inside the function range are recomputed.
807///
808/// IMPORTANT NOTE: If you intend to use the errors of this histogram later
809/// you should call Sumw2 before making this operation.
810/// This is particularly important if you fit the histogram after TH1::Add
811///
812/// The function return kFALSE if the Add operation failed
813
815{
816 if (!f1) {
817 Error("Add","Attempt to add a non-existing function");
818 return kFALSE;
819 }
820
821 TString opt = option;
822 opt.ToLower();
823 Bool_t integral = kFALSE;
824 if (opt.Contains("i") && fDimension == 1) integral = kTRUE;
825
826 Int_t ncellsx = GetNbinsX() + 2; // cells = normal bins + underflow bin + overflow bin
827 Int_t ncellsy = GetNbinsY() + 2;
828 Int_t ncellsz = GetNbinsZ() + 2;
829 if (fDimension < 2) ncellsy = 1;
830 if (fDimension < 3) ncellsz = 1;
831
832 // delete buffer if it is there since it will become invalid
833 if (fBuffer) BufferEmpty(1);
834
835 // - Add statistics
836 Double_t s1[10];
837 for (Int_t i = 0; i < 10; ++i) s1[i] = 0;
838 PutStats(s1);
839 SetMinimum();
840 SetMaximum();
841
842 // - Loop on bins (including underflows/overflows)
843 Int_t bin, binx, biny, binz;
844 Double_t cu=0;
845 Double_t xx[3];
846 Double_t *params = nullptr;
847 f1->InitArgs(xx,params);
848 for (binz = 0; binz < ncellsz; ++binz) {
850 for (biny = 0; biny < ncellsy; ++biny) {
852 for (binx = 0; binx < ncellsx; ++binx) {
854 if (!f1->IsInside(xx)) continue;
856 bin = binx + ncellsx * (biny + ncellsy * binz);
857 if (integral) {
859 } else {
860 cu = c1*f1->EvalPar(xx);
861 }
862 if (TF1::RejectedPoint()) continue;
863 AddBinContent(bin,cu);
864 }
865 }
866 }
867
868 return kTRUE;
869}
870
871int TH1::LoggedInconsistency(const char *name, const TH1 *h1, const TH1 *h2, bool useMerge) const
872{
873 const auto inconsistency = CheckConsistency(h1, h2);
874
876 if (useMerge)
877 Info(name, "Histograms have different dimensions - trying to use TH1::Merge");
878 else {
879 Error(name, "Histograms have different dimensions");
880 }
882 if (useMerge)
883 Info(name, "Histograms have different number of bins - trying to use TH1::Merge");
884 else {
885 Error(name, "Histograms have different number of bins");
886 }
887 } else if (inconsistency & kDifferentAxisLimits) {
888 if (useMerge)
889 Info(name, "Histograms have different axis limits - trying to use TH1::Merge");
890 else
891 Warning(name, "Histograms have different axis limits");
892 } else if (inconsistency & kDifferentBinLimits) {
893 if (useMerge)
894 Info(name, "Histograms have different bin limits - trying to use TH1::Merge");
895 else
896 Warning(name, "Histograms have different bin limits");
897 } else if (inconsistency & kDifferentLabels) {
898 // in case of different labels -
899 if (useMerge)
900 Info(name, "Histograms have different labels - trying to use TH1::Merge");
901 else
902 Info(name, "Histograms have different labels");
903 }
904
905 return inconsistency;
906}
907
908////////////////////////////////////////////////////////////////////////////////
909/// Performs the operation: `this = this + c1*h1`
910/// If errors are defined (see TH1::Sumw2), errors are also recalculated.
911///
912/// Note that if h1 has Sumw2 set, Sumw2 is automatically called for this
913/// if not already set.
914///
915/// Note also that adding histogram with labels is not supported, histogram will be
916/// added merging them by bin number independently of the labels.
917/// For adding histogram with labels one should use TH1::Merge
918///
919/// SPECIAL CASE (Average/Efficiency histograms)
920/// For histograms representing averages or efficiencies, one should compute the average
921/// of the two histograms and not the sum. One can mark a histogram to be an average
922/// histogram by setting its bit kIsAverage with
923/// myhist.SetBit(TH1::kIsAverage);
924/// Note that the two histograms must have their kIsAverage bit set
925///
926/// IMPORTANT NOTE1: If you intend to use the errors of this histogram later
927/// you should call Sumw2 before making this operation.
928/// This is particularly important if you fit the histogram after TH1::Add
929///
930/// IMPORTANT NOTE2: if h1 has a normalisation factor, the normalisation factor
931/// is used , ie this = this + c1*factor*h1
932/// Use the other TH1::Add function if you do not want this feature
933///
934/// IMPORTANT NOTE3: You should be careful about the statistics of the
935/// returned histogram, whose statistics may be binned or unbinned,
936/// depending on whether c1 is negative, whether TAxis::kAxisRange is true,
937/// and whether TH1::ResetStats has been called on either this or h1.
938/// See TH1::GetStats.
939///
940/// The function return kFALSE if the Add operation failed
941
943{
944 if (!h1) {
945 Error("Add","Attempt to add a non-existing histogram");
946 return kFALSE;
947 }
948
949 // delete buffer if it is there since it will become invalid
950 if (fBuffer) BufferEmpty(1);
951
952 bool useMerge = false;
953 const bool considerMerge = (c1 == 1. && !this->TestBit(kIsAverage) && !h1->TestBit(kIsAverage) );
954 const auto inconsistency = LoggedInconsistency("Add", this, h1, considerMerge);
955 // If there is a bad inconsistency and we can't even consider merging, just give up
957 return false;
958 }
959 // If there is an inconsistency, we try to use merging
962 }
963
964 if (useMerge) {
965 TList l;
966 l.Add(const_cast<TH1*>(h1));
967 auto iret = Merge(&l);
968 return (iret >= 0);
969 }
970
971 // Create Sumw2 if h1 has Sumw2 set
972 if (fSumw2.fN == 0 && h1->GetSumw2N() != 0) Sumw2();
973
974 // - Add statistics
975 Double_t entries = TMath::Abs( GetEntries() + c1 * h1->GetEntries() );
976
977 // statistics can be preserved only in case of positive coefficients
978 // otherwise with negative c1 (histogram subtraction) one risks to get negative variances
979 Bool_t resetStats = (c1 < 0);
980 Double_t s1[kNstat] = {0};
981 Double_t s2[kNstat] = {0};
982 if (!resetStats) {
983 // need to initialize to zero s1 and s2 since
984 // GetStats fills only used elements depending on dimension and type
985 GetStats(s1);
986 h1->GetStats(s2);
987 }
988
989 SetMinimum();
990 SetMaximum();
991
992 // - Loop on bins (including underflows/overflows)
993 Double_t factor = 1;
994 if (h1->GetNormFactor() != 0) factor = h1->GetNormFactor()/h1->GetSumOfWeights();
995 Double_t c1sq = c1 * c1;
996 Double_t factsq = factor * factor;
997
998 for (Int_t bin = 0; bin < fNcells; ++bin) {
999 //special case where histograms have the kIsAverage bit set
1000 if (this->TestBit(kIsAverage) && h1->TestBit(kIsAverage)) {
1002 Double_t y2 = this->RetrieveBinContent(bin);
1005 Double_t w1 = 1., w2 = 1.;
1006
1007 // consider all special cases when bin errors are zero
1008 // see http://root-forum.cern.ch/viewtopic.php?f=3&t=13299
1009 if (e1sq) w1 = 1. / e1sq;
1010 else if (h1->fSumw2.fN) {
1011 w1 = 1.E200; // use an arbitrary huge value
1012 if (y1 == 0) {
1013 // use an estimated error from the global histogram scale
1014 double sf = (s2[0] != 0) ? s2[1]/s2[0] : 1;
1015 w1 = 1./(sf*sf);
1016 }
1017 }
1018 if (e2sq) w2 = 1. / e2sq;
1019 else if (fSumw2.fN) {
1020 w2 = 1.E200; // use an arbitrary huge value
1021 if (y2 == 0) {
1022 // use an estimated error from the global histogram scale
1023 double sf = (s1[0] != 0) ? s1[1]/s1[0] : 1;
1024 w2 = 1./(sf*sf);
1025 }
1026 }
1027
1028 double y = (w1*y1 + w2*y2)/(w1 + w2);
1029 UpdateBinContent(bin, y);
1030 if (fSumw2.fN) {
1031 double err2 = 1./(w1 + w2);
1032 if (err2 < 1.E-200) err2 = 0; // to remove arbitrary value when e1=0 AND e2=0
1033 fSumw2.fArray[bin] = err2;
1034 }
1035 } else { // normal case of addition between histograms
1036 AddBinContent(bin, c1 * factor * h1->RetrieveBinContent(bin));
1037 if (fSumw2.fN) fSumw2.fArray[bin] += c1sq * factsq * h1->GetBinErrorSqUnchecked(bin);
1038 }
1039 }
1040
1041 // update statistics (do here to avoid changes by SetBinContent)
1042 if (resetStats) {
1043 // statistics need to be reset in case coefficient are negative
1044 ResetStats();
1045 }
1046 else {
1047 for (Int_t i=0;i<kNstat;i++) {
1048 if (i == 1) s1[i] += c1*c1*s2[i];
1049 else s1[i] += c1*s2[i];
1050 }
1051 PutStats(s1);
1052 SetEntries(entries);
1053 }
1054 return kTRUE;
1055}
1056
1057////////////////////////////////////////////////////////////////////////////////
1058/// Replace contents of this histogram by the addition of h1 and h2.
1059///
1060/// `this = c1*h1 + c2*h2`
1061/// if errors are defined (see TH1::Sumw2), errors are also recalculated
1062///
1063/// Note that if h1 or h2 have Sumw2 set, Sumw2 is automatically called for this
1064/// if not already set.
1065///
1066/// Note also that adding histogram with labels is not supported, histogram will be
1067/// added merging them by bin number independently of the labels.
1068/// For adding histogram ith labels one should use TH1::Merge
1069///
1070/// SPECIAL CASE (Average/Efficiency histograms)
1071/// For histograms representing averages or efficiencies, one should compute the average
1072/// of the two histograms and not the sum. One can mark a histogram to be an average
1073/// histogram by setting its bit kIsAverage with
1074/// myhist.SetBit(TH1::kIsAverage);
1075/// Note that the two histograms must have their kIsAverage bit set
1076///
1077/// IMPORTANT NOTE: If you intend to use the errors of this histogram later
1078/// you should call Sumw2 before making this operation.
1079/// This is particularly important if you fit the histogram after TH1::Add
1080///
1081/// IMPORTANT NOTE2: You should be careful about the statistics of the
1082/// returned histogram, whose statistics may be binned or unbinned,
1083/// depending on whether c1 is negative, whether TAxis::kAxisRange is true,
1084/// and whether TH1::ResetStats has been called on either this or h1.
1085/// See TH1::GetStats.
1086///
1087/// ANOTHER SPECIAL CASE : h1 = h2 and c2 < 0
1088/// do a scaling this = c1 * h1 / (bin Volume)
1089///
1090/// The function returns kFALSE if the Add operation failed
1091
1093{
1094
1095 if (!h1 || !h2) {
1096 Error("Add","Attempt to add a non-existing histogram");
1097 return kFALSE;
1098 }
1099
1100 // delete buffer if it is there since it will become invalid
1101 if (fBuffer) BufferEmpty(1);
1102
1104 if (h1 == h2 && c2 < 0) {c2 = 0; normWidth = kTRUE;}
1105
1106 if (h1 != h2) {
1107 bool useMerge = false;
1108 const bool considerMerge = (c1 == 1. && c2 == 1. && !this->TestBit(kIsAverage) && !h1->TestBit(kIsAverage) );
1109
1110 // We can combine inconsistencies like this, since they are ordered and a
1111 // higher inconsistency is worse
1112 auto const inconsistency = std::max(LoggedInconsistency("Add", this, h1, considerMerge),
1113 LoggedInconsistency("Add", h1, h2, considerMerge));
1114
1115 // If there is a bad inconsistency and we can't even consider merging, just give up
1117 return false;
1118 }
1119 // If there is an inconsistency, we try to use merging
1122 }
1123
1124 if (useMerge) {
1125 TList l;
1126 // why TList takes non-const pointers ????
1127 l.Add(const_cast<TH1*>(h1));
1128 l.Add(const_cast<TH1*>(h2));
1129 Reset("ICE");
1130 auto iret = Merge(&l);
1131 return (iret >= 0);
1132 }
1133 }
1134
1135 // Create Sumw2 if h1 or h2 have Sumw2 set
1136 if (fSumw2.fN == 0 && (h1->GetSumw2N() != 0 || h2->GetSumw2N() != 0)) Sumw2();
1137
1138 // - Add statistics
1139 Double_t nEntries = TMath::Abs( c1*h1->GetEntries() + c2*h2->GetEntries() );
1140
1141 // TODO remove
1142 // statistics can be preserved only in case of positive coefficients
1143 // otherwise with negative c1 (histogram subtraction) one risks to get negative variances
1144 // also in case of scaling with the width we cannot preserve the statistics
1145 Double_t s1[kNstat] = {0};
1146 Double_t s2[kNstat] = {0};
1148
1149
1150 Bool_t resetStats = (c1*c2 < 0) || normWidth;
1151 if (!resetStats) {
1152 // need to initialize to zero s1 and s2 since
1153 // GetStats fills only used elements depending on dimension and type
1154 h1->GetStats(s1);
1155 h2->GetStats(s2);
1156 for (Int_t i=0;i<kNstat;i++) {
1157 if (i == 1) s3[i] = c1*c1*s1[i] + c2*c2*s2[i];
1158 //else s3[i] = TMath::Abs(c1)*s1[i] + TMath::Abs(c2)*s2[i];
1159 else s3[i] = c1*s1[i] + c2*s2[i];
1160 }
1161 }
1162
1163 SetMinimum();
1164 SetMaximum();
1165
1166 if (normWidth) { // DEPRECATED CASE: belongs to fitting / drawing modules
1167
1168 Int_t nbinsx = GetNbinsX() + 2; // normal bins + underflow, overflow
1169 Int_t nbinsy = GetNbinsY() + 2;
1170 Int_t nbinsz = GetNbinsZ() + 2;
1171
1172 if (fDimension < 2) nbinsy = 1;
1173 if (fDimension < 3) nbinsz = 1;
1174
1175 Int_t bin, binx, biny, binz;
1176 for (binz = 0; binz < nbinsz; ++binz) {
1178 for (biny = 0; biny < nbinsy; ++biny) {
1180 for (binx = 0; binx < nbinsx; ++binx) {
1182 bin = GetBin(binx, biny, binz);
1183 Double_t w = wx*wy*wz;
1184 UpdateBinContent(bin, c1 * h1->RetrieveBinContent(bin) / w);
1185 if (fSumw2.fN) {
1186 Double_t e1 = h1->GetBinError(bin)/w;
1187 fSumw2.fArray[bin] = c1*c1*e1*e1;
1188 }
1189 }
1190 }
1191 }
1192 } else if (h1->TestBit(kIsAverage) && h2->TestBit(kIsAverage)) {
1193 for (Int_t i = 0; i < fNcells; ++i) { // loop on cells (bins including underflow / overflow)
1194 // special case where histograms have the kIsAverage bit set
1196 Double_t y2 = h2->RetrieveBinContent(i);
1198 Double_t e2sq = h2->GetBinErrorSqUnchecked(i);
1199 Double_t w1 = 1., w2 = 1.;
1200
1201 // consider all special cases when bin errors are zero
1202 // see http://root-forum.cern.ch/viewtopic.php?f=3&t=13299
1203 if (e1sq) w1 = 1./ e1sq;
1204 else if (h1->fSumw2.fN) {
1205 w1 = 1.E200; // use an arbitrary huge value
1206 if (y1 == 0 ) { // use an estimated error from the global histogram scale
1207 double sf = (s1[0] != 0) ? s1[1]/s1[0] : 1;
1208 w1 = 1./(sf*sf);
1209 }
1210 }
1211 if (e2sq) w2 = 1./ e2sq;
1212 else if (h2->fSumw2.fN) {
1213 w2 = 1.E200; // use an arbitrary huge value
1214 if (y2 == 0) { // use an estimated error from the global histogram scale
1215 double sf = (s2[0] != 0) ? s2[1]/s2[0] : 1;
1216 w2 = 1./(sf*sf);
1217 }
1218 }
1219
1220 double y = (w1*y1 + w2*y2)/(w1 + w2);
1221 UpdateBinContent(i, y);
1222 if (fSumw2.fN) {
1223 double err2 = 1./(w1 + w2);
1224 if (err2 < 1.E-200) err2 = 0; // to remove arbitrary value when e1=0 AND e2=0
1225 fSumw2.fArray[i] = err2;
1226 }
1227 }
1228 } else { // case of simple histogram addition
1229 Double_t c1sq = c1 * c1;
1230 Double_t c2sq = c2 * c2;
1231 for (Int_t i = 0; i < fNcells; ++i) { // Loop on cells (bins including underflows/overflows)
1232 UpdateBinContent(i, c1 * h1->RetrieveBinContent(i) + c2 * h2->RetrieveBinContent(i));
1233 if (fSumw2.fN) {
1234 fSumw2.fArray[i] = c1sq * h1->GetBinErrorSqUnchecked(i) + c2sq * h2->GetBinErrorSqUnchecked(i);
1235 }
1236 }
1237 }
1238
1239 if (resetStats) {
1240 // statistics need to be reset in case coefficient are negative
1241 ResetStats();
1242 }
1243 else {
1244 // update statistics (do here to avoid changes by SetBinContent) FIXME remove???
1245 PutStats(s3);
1247 }
1248
1249 return kTRUE;
1250}
1251
1252////////////////////////////////////////////////////////////////////////////////
1253/// Sets the flag controlling the automatic add of histograms in memory
1254///
1255/// By default (fAddDirectory = kTRUE), histograms are automatically added
1256/// to the list of objects in memory.
1257/// Note that one histogram can be removed from its support directory
1258/// by calling h->SetDirectory(nullptr) or h->SetDirectory(dir) to add it
1259/// to the list of objects in the directory dir.
1260///
1261/// NOTE that this is a static function. To call it, use;
1262/// TH1::AddDirectory
1263
1265{
1266 fgAddDirectory = add;
1267}
1268
1269////////////////////////////////////////////////////////////////////////////////
1270/// Auxiliary function to get the power of 2 next (larger) or previous (smaller)
1271/// a given x
1272///
1273/// next = kTRUE : next larger
1274/// next = kFALSE : previous smaller
1275///
1276/// Used by the autobin power of 2 algorithm
1277
1279{
1280 Int_t nn;
1281 Double_t f2 = std::frexp(x, &nn);
1282 return ((next && x > 0.) || (!next && x <= 0.)) ? std::ldexp(std::copysign(1., f2), nn)
1283 : std::ldexp(std::copysign(1., f2), --nn);
1284}
1285
1286////////////////////////////////////////////////////////////////////////////////
1287/// Auxiliary function to get the next power of 2 integer value larger then n
1288///
1289/// Used by the autobin power of 2 algorithm
1290
1292{
1293 Int_t nn;
1294 Double_t f2 = std::frexp(n, &nn);
1295 if (TMath::Abs(f2 - .5) > 0.001)
1296 return (Int_t)std::ldexp(1., nn);
1297 return n;
1298}
1299
1300////////////////////////////////////////////////////////////////////////////////
1301/// Buffer-based estimate of the histogram range using the power of 2 algorithm.
1302///
1303/// Used by the autobin power of 2 algorithm.
1304///
1305/// Works on arguments (min and max from fBuffer) and internal inputs: fXmin,
1306/// fXmax, NBinsX (from fXaxis), ...
1307/// Result save internally in fXaxis.
1308///
1309/// Overloaded by TH2 and TH3.
1310///
1311/// Return -1 if internal inputs are inconsistent, 0 otherwise.
1312
1314{
1315 // We need meaningful raw limits
1316 if (xmi >= xma)
1317 return -1;
1318
1319 THLimitsFinder::GetLimitsFinder()->FindGoodLimits(this, xmi, xma);
1322
1323 // Now adjust
1324 if (TMath::Abs(xhma) > TMath::Abs(xhmi)) {
1325 // Start from the upper limit
1328 } else {
1329 // Start from the lower limit
1332 }
1333
1334 // Round the bins to the next power of 2; take into account the possible inflation
1335 // of the range
1336 Double_t rr = (xhma - xhmi) / (xma - xmi);
1338
1339 // Adjust using the same bin width and offsets
1340 Double_t bw = (xhma - xhmi) / nb;
1341 // Bins to left free on each side
1342 Double_t autoside = gEnv->GetValue("Hist.Binning.Auto.Side", 0.05);
1343 Int_t nbside = (Int_t)(nb * autoside);
1344
1345 // Side up
1346 Int_t nbup = (xhma - xma) / bw;
1347 if (nbup % 2 != 0)
1348 nbup++; // Must be even
1349 if (nbup != nbside) {
1350 // Accounts also for both case: larger or smaller
1351 xhma -= bw * (nbup - nbside);
1352 nb -= (nbup - nbside);
1353 }
1354
1355 // Side low
1356 Int_t nblw = (xmi - xhmi) / bw;
1357 if (nblw % 2 != 0)
1358 nblw++; // Must be even
1359 if (nblw != nbside) {
1360 // Accounts also for both case: larger or smaller
1361 xhmi += bw * (nblw - nbside);
1362 nb -= (nblw - nbside);
1363 }
1364
1365 // Set everything and project
1366 SetBins(nb, xhmi, xhma);
1367
1368 // Done
1369 return 0;
1370}
1371
1372/// Fill histogram with all entries in the buffer.
1373///
1374/// - action = -1 histogram is reset and refilled from the buffer (called by THistPainter::Paint)
1375/// - action = 0 histogram is reset and filled from the buffer. When the histogram is filled from the
1376/// buffer the value fBuffer[0] is set to a negative number (= - number of entries)
1377/// When calling with action == 0 the histogram is NOT refilled when fBuffer[0] is < 0
1378/// While when calling with action = -1 the histogram is reset and ALWAYS refilled independently if
1379/// the histogram was filled before. This is needed when drawing the histogram
1380/// - action = 1 histogram is filled and buffer is deleted
1381/// The buffer is automatically deleted when filling the histogram and the entries is
1382/// larger than the buffer size
1383
1385{
1386 // do we need to compute the bin size?
1387 if (!fBuffer) return 0;
1389
1390 // nbentries correspond to the number of entries of histogram
1391
1392 if (nbentries == 0) {
1393 // if action is 1 we delete the buffer
1394 // this will avoid infinite recursion
1395 if (action > 0) {
1396 delete [] fBuffer;
1397 fBuffer = nullptr;
1398 fBufferSize = 0;
1399 }
1400 return 0;
1401 }
1402 if (nbentries < 0 && action == 0) return 0; // case histogram has been already filled from the buffer
1403
1404 Double_t *buffer = fBuffer;
1405 if (nbentries < 0) {
1407 // a reset might call BufferEmpty() giving an infinite recursion
1408 // Protect it by setting fBuffer = nullptr
1409 fBuffer = nullptr;
1410 //do not reset the list of functions
1411 Reset("ICES");
1412 fBuffer = buffer;
1413 }
1414 if (CanExtendAllAxes() || (fXaxis.GetXmax() <= fXaxis.GetXmin())) {
1415 //find min, max of entries in buffer
1418 for (Int_t i=0;i<nbentries;i++) {
1419 Double_t x = fBuffer[2*i+2];
1420 // skip infinity or NaN values
1421 if (!std::isfinite(x)) continue;
1422 if (x < xmin) xmin = x;
1423 if (x > xmax) xmax = x;
1424 }
1425 if (fXaxis.GetXmax() <= fXaxis.GetXmin()) {
1426 Int_t rc = -1;
1428 if ((rc = AutoP2FindLimits(xmin, xmax)) < 0)
1429 Warning("BufferEmpty",
1430 "inconsistency found by power-of-2 autobin algorithm: fallback to standard method");
1431 }
1432 if (rc < 0)
1433 THLimitsFinder::GetLimitsFinder()->FindGoodLimits(this, xmin, xmax);
1434 } else {
1435 fBuffer = nullptr;
1438 if (xmax >= fXaxis.GetXmax()) ExtendAxis(xmax, &fXaxis);
1439 fBuffer = buffer;
1440 fBufferSize = keep;
1441 }
1442 }
1443
1444 // call DoFillN which will not put entries in the buffer as FillN does
1445 // set fBuffer to zero to avoid re-emptying the buffer from functions called
1446 // by DoFillN (e.g Sumw2)
1447 buffer = fBuffer; fBuffer = nullptr;
1448 DoFillN(nbentries,&buffer[2],&buffer[1],2);
1449 fBuffer = buffer;
1450
1451 // if action == 1 - delete the buffer
1452 if (action > 0) {
1453 delete [] fBuffer;
1454 fBuffer = nullptr;
1455 fBufferSize = 0;
1456 } else {
1457 // if number of entries is consistent with buffer - set it negative to avoid
1458 // refilling the histogram every time BufferEmpty(0) is called
1459 // In case it is not consistent, by setting fBuffer[0]=0 is like resetting the buffer
1460 // (it will not be used anymore the next time BufferEmpty is called)
1461 if (nbentries == (Int_t)fEntries)
1462 fBuffer[0] = -nbentries;
1463 else
1464 fBuffer[0] = 0;
1465 }
1466 return nbentries;
1467}
1468
1469////////////////////////////////////////////////////////////////////////////////
1470/// accumulate arguments in buffer. When buffer is full, empty the buffer
1471///
1472/// - `fBuffer[0]` = number of entries in buffer
1473/// - `fBuffer[1]` = w of first entry
1474/// - `fBuffer[2]` = x of first entry
1475
1477{
1478 if (!fBuffer) return -2;
1480
1481
1482 if (nbentries < 0) {
1483 // reset nbentries to a positive value so next time BufferEmpty() is called
1484 // the histogram will be refilled
1486 fBuffer[0] = nbentries;
1487 if (fEntries > 0) {
1488 // set fBuffer to zero to avoid calling BufferEmpty in Reset
1489 Double_t *buffer = fBuffer; fBuffer=nullptr;
1490 Reset("ICES"); // do not reset list of functions
1491 fBuffer = buffer;
1492 }
1493 }
1494 if (2*nbentries+2 >= fBufferSize) {
1495 BufferEmpty(1);
1496 if (!fBuffer)
1497 // to avoid infinite recursion Fill->BufferFill->Fill
1498 return Fill(x,w);
1499 // this cannot happen
1500 R__ASSERT(0);
1501 }
1502 fBuffer[2*nbentries+1] = w;
1503 fBuffer[2*nbentries+2] = x;
1504 fBuffer[0] += 1;
1505 return -2;
1506}
1507
1508////////////////////////////////////////////////////////////////////////////////
1509/// Check bin limits.
1510
1511bool TH1::CheckBinLimits(const TAxis* a1, const TAxis * a2)
1512{
1513 const TArrayD * h1Array = a1->GetXbins();
1514 const TArrayD * h2Array = a2->GetXbins();
1515 Int_t fN = h1Array->fN;
1516 if ( fN != 0 ) {
1517 if ( h2Array->fN != fN ) {
1518 return false;
1519 }
1520 else {
1521 for ( int i = 0; i < fN; ++i ) {
1522 // for i==fN (nbin+1) a->GetBinWidth() returns last bin width
1523 // we do not need to exclude that case
1524 double binWidth = a1->GetBinWidth(i);
1525 if ( ! TMath::AreEqualAbs( h1Array->GetAt(i), h2Array->GetAt(i), binWidth*1E-10 ) ) {
1526 return false;
1527 }
1528 }
1529 }
1530 }
1531
1532 return true;
1533}
1534
1535////////////////////////////////////////////////////////////////////////////////
1536/// Check that axis have same labels.
1537
1538bool TH1::CheckBinLabels(const TAxis* a1, const TAxis * a2)
1539{
1540 THashList *l1 = a1->GetLabels();
1541 THashList *l2 = a2->GetLabels();
1542
1543 if (!l1 && !l2 )
1544 return true;
1545 if (!l1 || !l2 ) {
1546 return false;
1547 }
1548 // check now labels sizes are the same
1549 if (l1->GetSize() != l2->GetSize() ) {
1550 return false;
1551 }
1552 for (int i = 1; i <= a1->GetNbins(); ++i) {
1553 TString label1 = a1->GetBinLabel(i);
1554 TString label2 = a2->GetBinLabel(i);
1555 if (label1 != label2) {
1556 return false;
1557 }
1558 }
1559
1560 return true;
1561}
1562
1563////////////////////////////////////////////////////////////////////////////////
1564/// Check that the axis limits of the histograms are the same.
1565/// If a first and last bin is passed the axis is compared between the given range
1566
1567bool TH1::CheckAxisLimits(const TAxis *a1, const TAxis *a2 )
1568{
1569 double firstBin = a1->GetBinWidth(1);
1570 double lastBin = a1->GetBinWidth( a1->GetNbins() );
1571 if ( ! TMath::AreEqualAbs(a1->GetXmin(), a2->GetXmin(), firstBin* 1.E-10) ||
1572 ! TMath::AreEqualAbs(a1->GetXmax(), a2->GetXmax(), lastBin*1.E-10) ) {
1573 return false;
1574 }
1575 return true;
1576}
1577
1578////////////////////////////////////////////////////////////////////////////////
1579/// Check that the axis are the same
1580
1581bool TH1::CheckEqualAxes(const TAxis *a1, const TAxis *a2 )
1582{
1583 if (a1->GetNbins() != a2->GetNbins() ) {
1584 ::Info("CheckEqualAxes","Axes have different number of bins : nbin1 = %d nbin2 = %d",a1->GetNbins(),a2->GetNbins() );
1585 return false;
1586 }
1587 if(!CheckAxisLimits(a1,a2)) {
1588 ::Info("CheckEqualAxes","Axes have different limits");
1589 return false;
1590 }
1591 if(!CheckBinLimits(a1,a2)) {
1592 ::Info("CheckEqualAxes","Axes have different bin limits");
1593 return false;
1594 }
1595
1596 // check labels
1597 if(!CheckBinLabels(a1,a2)) {
1598 ::Info("CheckEqualAxes","Axes have different labels");
1599 return false;
1600 }
1601
1602 return true;
1603}
1604
1605////////////////////////////////////////////////////////////////////////////////
1606/// Check that two sub axis are the same.
1607/// The limits are defined by first bin and last bin
1608/// N.B. no check is done in this case for variable bins
1609
1611{
1612 // By default is assumed that no bins are given for the second axis
1614 Double_t xmin1 = a1->GetBinLowEdge(firstBin1);
1615 Double_t xmax1 = a1->GetBinUpEdge(lastBin1);
1616
1617 Int_t nbins2 = a2->GetNbins();
1618 Double_t xmin2 = a2->GetXmin();
1619 Double_t xmax2 = a2->GetXmax();
1620
1621 if (firstBin2 < lastBin2) {
1622 // in this case assume no bins are given for the second axis
1624 xmin2 = a1->GetBinLowEdge(firstBin1);
1625 xmax2 = a1->GetBinUpEdge(lastBin1);
1626 }
1627
1628 if (nbins1 != nbins2 ) {
1629 ::Info("CheckConsistentSubAxes","Axes have different number of bins");
1630 return false;
1631 }
1632
1633 Double_t firstBin = a1->GetBinWidth(firstBin1);
1634 Double_t lastBin = a1->GetBinWidth(lastBin1);
1635 if ( ! TMath::AreEqualAbs(xmin1,xmin2,1.E-10 * firstBin) ||
1636 ! TMath::AreEqualAbs(xmax1,xmax2,1.E-10 * lastBin) ) {
1637 ::Info("CheckConsistentSubAxes","Axes have different limits");
1638 return false;
1639 }
1640
1641 return true;
1642}
1643
1644////////////////////////////////////////////////////////////////////////////////
1645/// Check histogram compatibility.
1646/// The returned integer is part of EInconsistencyBits
1647/// The value 0 means that the histograms are compatible
1648
1650{
1651 if (h1 == h2) return kFullyConsistent;
1652
1653 if (h1->GetDimension() != h2->GetDimension() ) {
1654 return kDifferentDimensions;
1655 }
1656 Int_t dim = h1->GetDimension();
1657
1658 // returns kTRUE if number of bins and bin limits are identical
1659 Int_t nbinsx = h1->GetNbinsX();
1660 Int_t nbinsy = h1->GetNbinsY();
1661 Int_t nbinsz = h1->GetNbinsZ();
1662
1663 // Check whether the histograms have the same number of bins.
1664 if (nbinsx != h2->GetNbinsX() ||
1665 (dim > 1 && nbinsy != h2->GetNbinsY()) ||
1666 (dim > 2 && nbinsz != h2->GetNbinsZ()) ) {
1668 }
1669
1670 bool ret = true;
1671
1672 // check axis limits
1673 ret &= CheckAxisLimits(h1->GetXaxis(), h2->GetXaxis());
1674 if (dim > 1) ret &= CheckAxisLimits(h1->GetYaxis(), h2->GetYaxis());
1675 if (dim > 2) ret &= CheckAxisLimits(h1->GetZaxis(), h2->GetZaxis());
1676 if (!ret) return kDifferentAxisLimits;
1677
1678 // check bin limits
1679 ret &= CheckBinLimits(h1->GetXaxis(), h2->GetXaxis());
1680 if (dim > 1) ret &= CheckBinLimits(h1->GetYaxis(), h2->GetYaxis());
1681 if (dim > 2) ret &= CheckBinLimits(h1->GetZaxis(), h2->GetZaxis());
1682 if (!ret) return kDifferentBinLimits;
1683
1684 // check labels if histograms are both not empty
1685 if ( !h1->IsEmpty() && !h2->IsEmpty() ) {
1686 ret &= CheckBinLabels(h1->GetXaxis(), h2->GetXaxis());
1687 if (dim > 1) ret &= CheckBinLabels(h1->GetYaxis(), h2->GetYaxis());
1688 if (dim > 2) ret &= CheckBinLabels(h1->GetZaxis(), h2->GetZaxis());
1689 if (!ret) return kDifferentLabels;
1690 }
1691
1692 return kFullyConsistent;
1693}
1694
1695////////////////////////////////////////////////////////////////////////////////
1696/// \f$ \chi^{2} \f$ test for comparing weighted and unweighted histograms.
1697///
1698/// Compares the histograms' adjusted (normalized) residuals.
1699/// Function: Returns p-value. Other return values are specified by the 3rd parameter
1700///
1701/// \param[in] h2 the second histogram
1702/// \param[in] option
1703/// - "UU" = experiment experiment comparison (unweighted-unweighted)
1704/// - "UW" = experiment MC comparison (unweighted-weighted). Note that
1705/// the first histogram should be unweighted
1706/// - "WW" = MC MC comparison (weighted-weighted)
1707/// - "NORM" = to be used when one or both of the histograms is scaled
1708/// but the histogram originally was unweighted
1709/// - by default underflows and overflows are not included:
1710/// * "OF" = overflows included
1711/// * "UF" = underflows included
1712/// - "P" = print chi2, ndf, p_value, igood
1713/// - "CHI2" = returns chi2 instead of p-value
1714/// - "CHI2/NDF" = returns \f$ \chi^{2} \f$/ndf
1715/// \param[in] res not empty - computes normalized residuals and returns them in this array
1716///
1717/// The current implementation is based on the papers \f$ \chi^{2} \f$ test for comparison
1718/// of weighted and unweighted histograms" in Proceedings of PHYSTAT05 and
1719/// "Comparison weighted and unweighted histograms", arXiv:physics/0605123
1720/// by N.Gagunashvili. This function has been implemented by Daniel Haertl in August 2006.
1721///
1722/// #### Introduction:
1723///
1724/// A frequently used technique in data analysis is the comparison of
1725/// histograms. First suggested by Pearson [1] the \f$ \chi^{2} \f$ test of
1726/// homogeneity is used widely for comparing usual (unweighted) histograms.
1727/// This paper describes the implementation modified \f$ \chi^{2} \f$ tests
1728/// for comparison of weighted and unweighted histograms and two weighted
1729/// histograms [2] as well as usual Pearson's \f$ \chi^{2} \f$ test for
1730/// comparison two usual (unweighted) histograms.
1731///
1732/// #### Overview:
1733///
1734/// Comparison of two histograms expect hypotheses that two histograms
1735/// represent identical distributions. To make a decision p-value should
1736/// be calculated. The hypotheses of identity is rejected if the p-value is
1737/// lower then some significance level. Traditionally significance levels
1738/// 0.1, 0.05 and 0.01 are used. The comparison procedure should include an
1739/// analysis of the residuals which is often helpful in identifying the
1740/// bins of histograms responsible for a significant overall \f$ \chi^{2} \f$ value.
1741/// Residuals are the difference between bin contents and expected bin
1742/// contents. Most convenient for analysis are the normalized residuals. If
1743/// hypotheses of identity are valid then normalized residuals are
1744/// approximately independent and identically distributed random variables
1745/// having N(0,1) distribution. Analysis of residuals expect test of above
1746/// mentioned properties of residuals. Notice that indirectly the analysis
1747/// of residuals increase the power of \f$ \chi^{2} \f$ test.
1748///
1749/// #### Methods of comparison:
1750///
1751/// \f$ \chi^{2} \f$ test for comparison two (unweighted) histograms:
1752/// Let us consider two histograms with the same binning and the number
1753/// of bins equal to r. Let us denote the number of events in the ith bin
1754/// in the first histogram as ni and as mi in the second one. The total
1755/// number of events in the first histogram is equal to:
1756/// \f[
1757/// N = \sum_{i=1}^{r} n_{i}
1758/// \f]
1759/// and
1760/// \f[
1761/// M = \sum_{i=1}^{r} m_{i}
1762/// \f]
1763/// in the second histogram. The hypothesis of identity (homogeneity) [3]
1764/// is that the two histograms represent random values with identical
1765/// distributions. It is equivalent that there exist r constants p1,...,pr,
1766/// such that
1767/// \f[
1768///\sum_{i=1}^{r} p_{i}=1
1769/// \f]
1770/// and the probability of belonging to the ith bin for some measured value
1771/// in both experiments is equal to pi. The number of events in the ith
1772/// bin is a random variable with a distribution approximated by a Poisson
1773/// probability distribution
1774/// \f[
1775///\frac{e^{-Np_{i}}(Np_{i})^{n_{i}}}{n_{i}!}
1776/// \f]
1777///for the first histogram and with distribution
1778/// \f[
1779///\frac{e^{-Mp_{i}}(Mp_{i})^{m_{i}}}{m_{i}!}
1780/// \f]
1781/// for the second histogram. If the hypothesis of homogeneity is valid,
1782/// then the maximum likelihood estimator of pi, i=1,...,r, is
1783/// \f[
1784///\hat{p}_{i}= \frac{n_{i}+m_{i}}{N+M}
1785/// \f]
1786/// and then
1787/// \f[
1788/// 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}}
1789/// \f]
1790/// has approximately a \f$ \chi^{2}_{(r-1)} \f$ distribution [3].
1791/// The comparison procedure can include an analysis of the residuals which
1792/// is often helpful in identifying the bins of histograms responsible for
1793/// a significant overall \f$ \chi^{2} \f$ value. Most convenient for
1794/// analysis are the adjusted (normalized) residuals [4]
1795/// \f[
1796/// 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))}}
1797/// \f]
1798/// If hypotheses of homogeneity are valid then residuals ri are
1799/// approximately independent and identically distributed random variables
1800/// having N(0,1) distribution. The application of the \f$ \chi^{2} \f$ test has
1801/// restrictions related to the value of the expected frequencies Npi,
1802/// Mpi, i=1,...,r. A conservative rule formulated in [5] is that all the
1803/// expectations must be 1 or greater for both histograms. In practical
1804/// cases when expected frequencies are not known the estimated expected
1805/// frequencies \f$ M\hat{p}_{i}, N\hat{p}_{i}, i=1,...,r \f$ can be used.
1806///
1807/// #### Unweighted and weighted histograms comparison:
1808///
1809/// A simple modification of the ideas described above can be used for the
1810/// comparison of the usual (unweighted) and weighted histograms. Let us
1811/// denote the number of events in the ith bin in the unweighted
1812/// histogram as ni and the common weight of events in the ith bin of the
1813/// weighted histogram as wi. The total number of events in the
1814/// unweighted histogram is equal to
1815///\f[
1816/// N = \sum_{i=1}^{r} n_{i}
1817///\f]
1818/// and the total weight of events in the weighted histogram is equal to
1819///\f[
1820/// W = \sum_{i=1}^{r} w_{i}
1821///\f]
1822/// Let us formulate the hypothesis of identity of an unweighted histogram
1823/// to a weighted histogram so that there exist r constants p1,...,pr, such
1824/// that
1825///\f[
1826/// \sum_{i=1}^{r} p_{i} = 1
1827///\f]
1828/// for the unweighted histogram. The weight wi is a random variable with a
1829/// distribution approximated by the normal probability distribution
1830/// \f$ N(Wp_{i},\sigma_{i}^{2}) \f$ where \f$ \sigma_{i}^{2} \f$ is the variance of the weight wi.
1831/// If we replace the variance \f$ \sigma_{i}^{2} \f$
1832/// with estimate \f$ s_{i}^{2} \f$ (sum of squares of weights of
1833/// events in the ith bin) and the hypothesis of identity is valid, then the
1834/// maximum likelihood estimator of pi,i=1,...,r, is
1835///\f[
1836/// \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}}
1837///\f]
1838/// We may then use the test statistic
1839///\f[
1840/// 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}}
1841///\f]
1842/// and it has approximately a \f$ \sigma^{2}_{(r-1)} \f$ distribution [2]. This test, as well
1843/// as the original one [3], has a restriction on the expected frequencies. The
1844/// expected frequencies recommended for the weighted histogram is more than 25.
1845/// The value of the minimal expected frequency can be decreased down to 10 for
1846/// the case when the weights of the events are close to constant. In the case
1847/// of a weighted histogram if the number of events is unknown, then we can
1848/// apply this recommendation for the equivalent number of events as
1849///\f[
1850/// n_{i}^{equiv} = \frac{ w_{i}^{2} }{ s_{i}^{2} }
1851///\f]
1852/// The minimal expected frequency for an unweighted histogram must be 1. Notice
1853/// that any usual (unweighted) histogram can be considered as a weighted
1854/// histogram with events that have constant weights equal to 1.
1855/// The variance \f$ z_{i}^{2} \f$ of the difference between the weight wi
1856/// and the estimated expectation value of the weight is approximately equal to:
1857///\f[
1858/// 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}
1859///\f]
1860/// The residuals
1861///\f[
1862/// r_{i} = \frac{w_{i}-W\hat{p}_{i}}{z_{i}}
1863///\f]
1864/// have approximately a normal distribution with mean equal to 0 and standard
1865/// deviation equal to 1.
1866///
1867/// #### Two weighted histograms comparison:
1868///
1869/// Let us denote the common weight of events of the ith bin in the first
1870/// histogram as w1i and as w2i in the second one. The total weight of events
1871/// in the first histogram is equal to
1872///\f[
1873/// W_{1} = \sum_{i=1}^{r} w_{1i}
1874///\f]
1875/// and
1876///\f[
1877/// W_{2} = \sum_{i=1}^{r} w_{2i}
1878///\f]
1879/// in the second histogram. Let us formulate the hypothesis of identity of
1880/// weighted histograms so that there exist r constants p1,...,pr, such that
1881///\f[
1882/// \sum_{i=1}^{r} p_{i} = 1
1883///\f]
1884/// and also expectation value of weight w1i equal to W1pi and expectation value
1885/// of weight w2i equal to W2pi. Weights in both the histograms are random
1886/// variables with distributions which can be approximated by a normal
1887/// probability distribution \f$ N(W_{1}p_{i},\sigma_{1i}^{2}) \f$ for the first histogram
1888/// and by a distribution \f$ N(W_{2}p_{i},\sigma_{2i}^{2}) \f$ for the second.
1889/// Here \f$ \sigma_{1i}^{2} \f$ and \f$ \sigma_{2i}^{2} \f$ are the variances
1890/// of w1i and w2i with estimators \f$ s_{1i}^{2} \f$ and \f$ s_{2i}^{2} \f$ respectively.
1891/// If the hypothesis of identity is valid, then the maximum likelihood and
1892/// Least Square Method estimator of pi,i=1,...,r, is
1893///\f[
1894/// \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}}
1895///\f]
1896/// We may then use the test statistic
1897///\f[
1898/// 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}}
1899///\f]
1900/// and it has approximately a \f$ \chi^{2}_{(r-1)} \f$ distribution [2].
1901/// The normalized or studentised residuals [6]
1902///\f[
1903/// 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})}}}
1904///\f]
1905/// have approximately a normal distribution with mean equal to 0 and standard
1906/// deviation 1. A recommended minimal expected frequency is equal to 10 for
1907/// the proposed test.
1908///
1909/// #### Numerical examples:
1910///
1911/// The method described herein is now illustrated with an example.
1912/// We take a distribution
1913///\f[
1914/// \phi(x) = \frac{2}{(x-10)^{2}+1} + \frac{1}{(x-14)^{2}+1} (1)
1915///\f]
1916/// defined on the interval [4,16]. Events distributed according to the formula
1917/// (1) are simulated to create the unweighted histogram. Uniformly distributed
1918/// events are simulated for the weighted histogram with weights calculated by
1919/// formula (1). Each histogram has the same number of bins: 20. Fig.1 shows
1920/// the result of comparison of the unweighted histogram with 200 events
1921/// (minimal expected frequency equal to one) and the weighted histogram with
1922/// 500 events (minimal expected frequency equal to 25)
1923/// Begin_Macro
1924/// ../../../tutorials/math/chi2test.C
1925/// End_Macro
1926/// Fig 1. An example of comparison of the unweighted histogram with 200 events
1927/// and the weighted histogram with 500 events:
1928/// 1. unweighted histogram;
1929/// 2. weighted histogram;
1930/// 3. normalized residuals plot;
1931/// 4. normal Q-Q plot of residuals.
1932///
1933/// The value of the test statistic \f$ \chi^{2} \f$ is equal to
1934/// 21.09 with p-value equal to 0.33, therefore the hypothesis of identity of
1935/// the two histograms can be accepted for 0.05 significant level. The behavior
1936/// of the normalized residuals plot (see Fig. 1c) and the normal Q-Q plot
1937/// (see Fig. 1d) of residuals are regular and we cannot identify the outliers
1938/// or bins with a big influence on \f$ \chi^{2} \f$.
1939///
1940/// The second example presents the same two histograms but 17 events was added
1941/// to content of bin number 15 in unweighted histogram. Fig.2 shows the result
1942/// of comparison of the unweighted histogram with 217 events (minimal expected
1943/// frequency equal to one) and the weighted histogram with 500 events (minimal
1944/// expected frequency equal to 25)
1945/// Begin_Macro
1946/// ../../../tutorials/math/chi2test.C(17)
1947/// End_Macro
1948/// Fig 2. An example of comparison of the unweighted histogram with 217 events
1949/// and the weighted histogram with 500 events:
1950/// 1. unweighted histogram;
1951/// 2. weighted histogram;
1952/// 3. normalized residuals plot;
1953/// 4. normal Q-Q plot of residuals.
1954///
1955/// The value of the test statistic \f$ \chi^{2} \f$ is equal to
1956/// 32.33 with p-value equal to 0.029, therefore the hypothesis of identity of
1957/// the two histograms is rejected for 0.05 significant level. The behavior of
1958/// the normalized residuals plot (see Fig. 2c) and the normal Q-Q plot (see
1959/// Fig. 2d) of residuals are not regular and we can identify the outlier or
1960/// bin with a big influence on \f$ \chi^{2} \f$.
1961///
1962/// #### References:
1963///
1964/// - [1] Pearson, K., 1904. On the Theory of Contingency and Its Relation to
1965/// Association and Normal Correlation. Drapers' Co. Memoirs, Biometric
1966/// Series No. 1, London.
1967/// - [2] Gagunashvili, N., 2006. \f$ \sigma^{2} \f$ test for comparison
1968/// of weighted and unweighted histograms. Statistical Problems in Particle
1969/// Physics, Astrophysics and Cosmology, Proceedings of PHYSTAT05,
1970/// Oxford, UK, 12-15 September 2005, Imperial College Press, London, 43-44.
1971/// Gagunashvili,N., Comparison of weighted and unweighted histograms,
1972/// arXiv:physics/0605123, 2006.
1973/// - [3] Cramer, H., 1946. Mathematical methods of statistics.
1974/// Princeton University Press, Princeton.
1975/// - [4] Haberman, S.J., 1973. The analysis of residuals in cross-classified tables.
1976/// Biometrics 29, 205-220.
1977/// - [5] Lewontin, R.C. and Felsenstein, J., 1965. The robustness of homogeneity
1978/// test in 2xN tables. Biometrics 21, 19-33.
1979/// - [6] Seber, G.A.F., Lee, A.J., 2003, Linear Regression Analysis.
1980/// John Wiley & Sons Inc., New York.
1981
1982Double_t TH1::Chi2Test(const TH1* h2, Option_t *option, Double_t *res) const
1983{
1984 Double_t chi2 = 0;
1985 Int_t ndf = 0, igood = 0;
1986
1987 TString opt = option;
1988 opt.ToUpper();
1989
1991
1992 if(opt.Contains("P")) {
1993 printf("Chi2 = %f, Prob = %g, NDF = %d, igood = %d\n", chi2,prob,ndf,igood);
1994 }
1995 if(opt.Contains("CHI2/NDF")) {
1996 if (ndf == 0) return 0;
1997 return chi2/ndf;
1998 }
1999 if(opt.Contains("CHI2")) {
2000 return chi2;
2001 }
2002
2003 return prob;
2004}
2005
2006////////////////////////////////////////////////////////////////////////////////
2007/// The computation routine of the Chisquare test. For the method description,
2008/// see Chi2Test() function.
2009///
2010/// \return p-value
2011/// \param[in] h2 the second histogram
2012/// \param[in] option
2013/// - "UU" = experiment experiment comparison (unweighted-unweighted)
2014/// - "UW" = experiment MC comparison (unweighted-weighted). Note that the first
2015/// histogram should be unweighted
2016/// - "WW" = MC MC comparison (weighted-weighted)
2017/// - "NORM" = if one or both histograms is scaled
2018/// - "OF" = overflows included
2019/// - "UF" = underflows included
2020/// by default underflows and overflows are not included
2021/// \param[out] igood test output
2022/// - igood=0 - no problems
2023/// - For unweighted unweighted comparison
2024/// - igood=1'There is a bin in the 1st histogram with less than 1 event'
2025/// - igood=2'There is a bin in the 2nd histogram with less than 1 event'
2026/// - igood=3'when the conditions for igood=1 and igood=2 are satisfied'
2027/// - For unweighted weighted comparison
2028/// - igood=1'There is a bin in the 1st histogram with less then 1 event'
2029/// - igood=2'There is a bin in the 2nd histogram with less then 10 effective number of events'
2030/// - igood=3'when the conditions for igood=1 and igood=2 are satisfied'
2031/// - For weighted weighted comparison
2032/// - igood=1'There is a bin in the 1st histogram with less then 10 effective
2033/// number of events'
2034/// - igood=2'There is a bin in the 2nd histogram with less then 10 effective
2035/// number of events'
2036/// - igood=3'when the conditions for igood=1 and igood=2 are satisfied'
2037/// \param[out] chi2 chisquare of the test
2038/// \param[out] ndf number of degrees of freedom (important, when both histograms have the same empty bins)
2039/// \param[out] res normalized residuals for further analysis
2040
2042{
2043
2047
2048 Double_t sum1 = 0.0, sumw1 = 0.0;
2049 Double_t sum2 = 0.0, sumw2 = 0.0;
2050
2051 chi2 = 0.0;
2052 ndf = 0;
2053
2054 TString opt = option;
2055 opt.ToUpper();
2056
2057 if (fBuffer) const_cast<TH1*>(this)->BufferEmpty();
2058
2059 const TAxis *xaxis1 = GetXaxis();
2060 const TAxis *xaxis2 = h2->GetXaxis();
2061 const TAxis *yaxis1 = GetYaxis();
2062 const TAxis *yaxis2 = h2->GetYaxis();
2063 const TAxis *zaxis1 = GetZaxis();
2064 const TAxis *zaxis2 = h2->GetZaxis();
2065
2066 Int_t nbinx1 = xaxis1->GetNbins();
2067 Int_t nbinx2 = xaxis2->GetNbins();
2068 Int_t nbiny1 = yaxis1->GetNbins();
2069 Int_t nbiny2 = yaxis2->GetNbins();
2070 Int_t nbinz1 = zaxis1->GetNbins();
2071 Int_t nbinz2 = zaxis2->GetNbins();
2072
2073 //check dimensions
2074 if (this->GetDimension() != h2->GetDimension() ){
2075 Error("Chi2TestX","Histograms have different dimensions.");
2076 return 0.0;
2077 }
2078
2079 //check number of channels
2080 if (nbinx1 != nbinx2) {
2081 Error("Chi2TestX","different number of x channels");
2082 }
2083 if (nbiny1 != nbiny2) {
2084 Error("Chi2TestX","different number of y channels");
2085 }
2086 if (nbinz1 != nbinz2) {
2087 Error("Chi2TestX","different number of z channels");
2088 }
2089
2090 //check for ranges
2091 i_start = j_start = k_start = 1;
2092 i_end = nbinx1;
2093 j_end = nbiny1;
2094 k_end = nbinz1;
2095
2096 if (xaxis1->TestBit(TAxis::kAxisRange)) {
2097 i_start = xaxis1->GetFirst();
2098 i_end = xaxis1->GetLast();
2099 }
2100 if (yaxis1->TestBit(TAxis::kAxisRange)) {
2101 j_start = yaxis1->GetFirst();
2102 j_end = yaxis1->GetLast();
2103 }
2104 if (zaxis1->TestBit(TAxis::kAxisRange)) {
2105 k_start = zaxis1->GetFirst();
2106 k_end = zaxis1->GetLast();
2107 }
2108
2109
2110 if (opt.Contains("OF")) {
2111 if (GetDimension() == 3) k_end = ++nbinz1;
2112 if (GetDimension() >= 2) j_end = ++nbiny1;
2113 if (GetDimension() >= 1) i_end = ++nbinx1;
2114 }
2115
2116 if (opt.Contains("UF")) {
2117 if (GetDimension() == 3) k_start = 0;
2118 if (GetDimension() >= 2) j_start = 0;
2119 if (GetDimension() >= 1) i_start = 0;
2120 }
2121
2122 ndf = (i_end - i_start + 1) * (j_end - j_start + 1) * (k_end - k_start + 1) - 1;
2123
2124 Bool_t comparisonUU = opt.Contains("UU");
2125 Bool_t comparisonUW = opt.Contains("UW");
2126 Bool_t comparisonWW = opt.Contains("WW");
2127 Bool_t scaledHistogram = opt.Contains("NORM");
2128
2129 if (scaledHistogram && !comparisonUU) {
2130 Info("Chi2TestX", "NORM option should be used together with UU option. It is ignored");
2131 }
2132
2133 // look at histo global bin content and effective entries
2134 Stat_t s[kNstat];
2135 GetStats(s);// s[1] sum of squares of weights, s[0] sum of weights
2136 Double_t sumBinContent1 = s[0];
2137 Double_t effEntries1 = (s[1] ? s[0] * s[0] / s[1] : 0.0);
2138
2139 h2->GetStats(s);// s[1] sum of squares of weights, s[0] sum of weights
2140 Double_t sumBinContent2 = s[0];
2141 Double_t effEntries2 = (s[1] ? s[0] * s[0] / s[1] : 0.0);
2142
2143 if (!comparisonUU && !comparisonUW && !comparisonWW ) {
2144 // deduce automatically from type of histogram
2147 else comparisonUW = true;
2148 }
2149 else comparisonWW = true;
2150 }
2151 // check unweighted histogram
2152 if (comparisonUW) {
2154 Warning("Chi2TestX","First histogram is not unweighted and option UW has been requested");
2155 }
2156 }
2157 if ( (!scaledHistogram && comparisonUU) ) {
2159 Warning("Chi2TestX","Both histograms are not unweighted and option UU has been requested");
2160 }
2161 }
2162
2163
2164 //get number of events in histogram
2166 for (Int_t i = i_start; i <= i_end; ++i) {
2167 for (Int_t j = j_start; j <= j_end; ++j) {
2168 for (Int_t k = k_start; k <= k_end; ++k) {
2169
2170 Int_t bin = GetBin(i, j, k);
2171
2173 Double_t cnt2 = h2->RetrieveBinContent(bin);
2175 Double_t e2sq = h2->GetBinErrorSqUnchecked(bin);
2176
2177 if (e1sq > 0.0) cnt1 = TMath::Floor(cnt1 * cnt1 / e1sq + 0.5); // avoid rounding errors
2178 else cnt1 = 0.0;
2179
2180 if (e2sq > 0.0) cnt2 = TMath::Floor(cnt2 * cnt2 / e2sq + 0.5); // avoid rounding errors
2181 else cnt2 = 0.0;
2182
2183 // sum contents
2184 sum1 += cnt1;
2185 sum2 += cnt2;
2186 sumw1 += e1sq;
2187 sumw2 += e2sq;
2188 }
2189 }
2190 }
2191 if (sumw1 <= 0.0 || sumw2 <= 0.0) {
2192 Error("Chi2TestX", "Cannot use option NORM when one histogram has all zero errors");
2193 return 0.0;
2194 }
2195
2196 } else {
2197 for (Int_t i = i_start; i <= i_end; ++i) {
2198 for (Int_t j = j_start; j <= j_end; ++j) {
2199 for (Int_t k = k_start; k <= k_end; ++k) {
2200
2201 Int_t bin = GetBin(i, j, k);
2202
2203 sum1 += RetrieveBinContent(bin);
2204 sum2 += h2->RetrieveBinContent(bin);
2205
2207 if ( comparisonUW || comparisonWW ) sumw2 += h2->GetBinErrorSqUnchecked(bin);
2208 }
2209 }
2210 }
2211 }
2212 //checks that the histograms are not empty
2213 if (sum1 == 0.0 || sum2 == 0.0) {
2214 Error("Chi2TestX","one histogram is empty");
2215 return 0.0;
2216 }
2217
2218 if ( comparisonWW && ( sumw1 <= 0.0 && sumw2 <= 0.0 ) ){
2219 Error("Chi2TestX","Hist1 and Hist2 have both all zero errors\n");
2220 return 0.0;
2221 }
2222
2223 //THE TEST
2224 Int_t m = 0, n = 0;
2225
2226 //Experiment - experiment comparison
2227 if (comparisonUU) {
2228 Double_t sum = sum1 + sum2;
2229 for (Int_t i = i_start; i <= i_end; ++i) {
2230 for (Int_t j = j_start; j <= j_end; ++j) {
2231 for (Int_t k = k_start; k <= k_end; ++k) {
2232
2233 Int_t bin = GetBin(i, j, k);
2234
2236 Double_t cnt2 = h2->RetrieveBinContent(bin);
2237
2238 if (scaledHistogram) {
2239 // scale bin value to effective bin entries
2241 Double_t e2sq = h2->GetBinErrorSqUnchecked(bin);
2242
2243 if (e1sq > 0) cnt1 = TMath::Floor(cnt1 * cnt1 / e1sq + 0.5); // avoid rounding errors
2244 else cnt1 = 0;
2245
2246 if (e2sq > 0) cnt2 = TMath::Floor(cnt2 * cnt2 / e2sq + 0.5); // avoid rounding errors
2247 else cnt2 = 0;
2248 }
2249
2250 if (Int_t(cnt1) == 0 && Int_t(cnt2) == 0) --ndf; // no data means one degree of freedom less
2251 else {
2252
2255 //Double_t nexp2 = binsum*sum2/sum;
2256
2257 if (res) res[i - i_start] = (cnt1 - nexp1) / TMath::Sqrt(nexp1);
2258
2259 if (cnt1 < 1) ++m;
2260 if (cnt2 < 1) ++n;
2261
2262 //Habermann correction for residuals
2263 Double_t correc = (1. - sum1 / sum) * (1. - cntsum / sum);
2264 if (res) res[i - i_start] /= TMath::Sqrt(correc);
2265
2266 Double_t delta = sum2 * cnt1 - sum1 * cnt2;
2267 chi2 += delta * delta / cntsum;
2268 }
2269 }
2270 }
2271 }
2272 chi2 /= sum1 * sum2;
2273
2274 // flag error only when of the two histogram is zero
2275 if (m) {
2276 igood += 1;
2277 Info("Chi2TestX","There is a bin in h1 with less than 1 event.\n");
2278 }
2279 if (n) {
2280 igood += 2;
2281 Info("Chi2TestX","There is a bin in h2 with less than 1 event.\n");
2282 }
2283
2285 return prob;
2286
2287 }
2288
2289 // unweighted - weighted comparison
2290 // case of error = 0 and content not zero is treated without problems by excluding second chi2 sum
2291 // and can be considered as a data-theory comparison
2292 if ( comparisonUW ) {
2293 for (Int_t i = i_start; i <= i_end; ++i) {
2294 for (Int_t j = j_start; j <= j_end; ++j) {
2295 for (Int_t k = k_start; k <= k_end; ++k) {
2296
2297 Int_t bin = GetBin(i, j, k);
2298
2300 Double_t cnt2 = h2->RetrieveBinContent(bin);
2301 Double_t e2sq = h2->GetBinErrorSqUnchecked(bin);
2302
2303 // case both histogram have zero bin contents
2304 if (cnt1 * cnt1 == 0 && cnt2 * cnt2 == 0) {
2305 --ndf; //no data means one degree of freedom less
2306 continue;
2307 }
2308
2309 // case weighted histogram has zero bin content and error
2310 if (cnt2 * cnt2 == 0 && e2sq == 0) {
2311 if (sumw2 > 0) {
2312 // use as approximated error as 1 scaled by a scaling ratio
2313 // estimated from the total sum weight and sum weight squared
2314 e2sq = sumw2 / sum2;
2315 }
2316 else {
2317 // return error because infinite discrepancy here:
2318 // bin1 != 0 and bin2 =0 in a histogram with all errors zero
2319 Error("Chi2TestX","Hist2 has in bin (%d,%d,%d) zero content and zero errors\n", i, j, k);
2320 chi2 = 0; return 0;
2321 }
2322 }
2323
2324 if (cnt1 < 1) m++;
2325 if (e2sq > 0 && cnt2 * cnt2 / e2sq < 10) n++;
2326
2327 Double_t var1 = sum2 * cnt2 - sum1 * e2sq;
2328 Double_t var2 = var1 * var1 + 4. * sum2 * sum2 * cnt1 * e2sq;
2329
2330 // if cnt1 is zero and cnt2 = 1 and sum1 = sum2 var1 = 0 && var2 == 0
2331 // approximate by incrementing cnt1
2332 // LM (this need to be fixed for numerical errors)
2333 while (var1 * var1 + cnt1 == 0 || var1 + var2 == 0) {
2334 sum1++;
2335 cnt1++;
2336 var1 = sum2 * cnt2 - sum1 * e2sq;
2337 var2 = var1 * var1 + 4. * sum2 * sum2 * cnt1 * e2sq;
2338 }
2340
2341 while (var1 + var2 == 0) {
2342 sum1++;
2343 cnt1++;
2344 var1 = sum2 * cnt2 - sum1 * e2sq;
2345 var2 = var1 * var1 + 4. * sum2 * sum2 * cnt1 * e2sq;
2346 while (var1 * var1 + cnt1 == 0 || var1 + var2 == 0) {
2347 sum1++;
2348 cnt1++;
2349 var1 = sum2 * cnt2 - sum1 * e2sq;
2350 var2 = var1 * var1 + 4. * sum2 * sum2 * cnt1 * e2sq;
2351 }
2353 }
2354
2355 Double_t probb = (var1 + var2) / (2. * sum2 * sum2);
2356
2359
2362
2363 chi2 += delta1 * delta1 / nexp1;
2364
2365 if (e2sq > 0) {
2366 chi2 += delta2 * delta2 / e2sq;
2367 }
2368
2369 if (res) {
2370 if (e2sq > 0) {
2371 Double_t temp1 = sum2 * e2sq / var2;
2372 Double_t temp2 = 1.0 + (sum1 * e2sq - sum2 * cnt2) / var2;
2373 temp2 = temp1 * temp1 * sum1 * probb * (1.0 - probb) + temp2 * temp2 * e2sq / 4.0;
2374 // invert sign here
2375 res[i - i_start] = - delta2 / TMath::Sqrt(temp2);
2376 }
2377 else
2378 res[i - i_start] = delta1 / TMath::Sqrt(nexp1);
2379 }
2380 }
2381 }
2382 }
2383
2384 if (m) {
2385 igood += 1;
2386 Info("Chi2TestX","There is a bin in h1 with less than 1 event.\n");
2387 }
2388 if (n) {
2389 igood += 2;
2390 Info("Chi2TestX","There is a bin in h2 with less than 10 effective events.\n");
2391 }
2392
2394
2395 return prob;
2396 }
2397
2398 // weighted - weighted comparison
2399 if (comparisonWW) {
2400 for (Int_t i = i_start; i <= i_end; ++i) {
2401 for (Int_t j = j_start; j <= j_end; ++j) {
2402 for (Int_t k = k_start; k <= k_end; ++k) {
2403
2404 Int_t bin = GetBin(i, j, k);
2406 Double_t cnt2 = h2->RetrieveBinContent(bin);
2408 Double_t e2sq = h2->GetBinErrorSqUnchecked(bin);
2409
2410 // case both histogram have zero bin contents
2411 // (use square of content to avoid numerical errors)
2412 if (cnt1 * cnt1 == 0 && cnt2 * cnt2 == 0) {
2413 --ndf; //no data means one degree of freedom less
2414 continue;
2415 }
2416
2417 if (e1sq == 0 && e2sq == 0) {
2418 // cannot treat case of booth histogram have zero zero errors
2419 Error("Chi2TestX","h1 and h2 both have bin %d,%d,%d with all zero errors\n", i,j,k);
2420 chi2 = 0; return 0;
2421 }
2422
2423 Double_t sigma = sum1 * sum1 * e2sq + sum2 * sum2 * e1sq;
2424 Double_t delta = sum2 * cnt1 - sum1 * cnt2;
2425 chi2 += delta * delta / sigma;
2426
2427 if (res) {
2428 Double_t temp = cnt1 * sum1 * e2sq + cnt2 * sum2 * e1sq;
2429 Double_t probb = temp / sigma;
2430 Double_t z = 0;
2431 if (e1sq > e2sq) {
2432 Double_t d1 = cnt1 - sum1 * probb;
2433 Double_t s1 = e1sq * ( 1. - e2sq * sum1 * sum1 / sigma );
2434 z = d1 / TMath::Sqrt(s1);
2435 }
2436 else {
2437 Double_t d2 = cnt2 - sum2 * probb;
2438 Double_t s2 = e2sq * ( 1. - e1sq * sum2 * sum2 / sigma );
2439 z = -d2 / TMath::Sqrt(s2);
2440 }
2441 res[i - i_start] = z;
2442 }
2443
2444 if (e1sq > 0 && cnt1 * cnt1 / e1sq < 10) m++;
2445 if (e2sq > 0 && cnt2 * cnt2 / e2sq < 10) n++;
2446 }
2447 }
2448 }
2449 if (m) {
2450 igood += 1;
2451 Info("Chi2TestX","There is a bin in h1 with less than 10 effective events.\n");
2452 }
2453 if (n) {
2454 igood += 2;
2455 Info("Chi2TestX","There is a bin in h2 with less than 10 effective events.\n");
2456 }
2458 return prob;
2459 }
2460 return 0;
2461}
2462////////////////////////////////////////////////////////////////////////////////
2463/// Compute and return the chisquare of this histogram with respect to a function
2464/// The chisquare is computed by weighting each histogram point by the bin error
2465/// By default the full range of the histogram is used, unless TAxis::SetRange or TAxis::SetRangeUser was called before.
2466/// Use option "R" for restricting the chisquare calculation to the given range of the function
2467/// Use option "L" for using the chisquare based on the poisson likelihood (Baker-Cousins Chisquare)
2468/// Use option "P" for using the Pearson chisquare based on the expected bin errors
2469
2471{
2472 if (!func) {
2473 Error("Chisquare","Function pointer is Null - return -1");
2474 return -1;
2475 }
2476
2477 TString opt(option); opt.ToUpper();
2478 bool useRange = opt.Contains("R");
2479 ROOT::Fit::EChisquareType type = ROOT::Fit::EChisquareType::kNeyman; // default chi2 with observed error
2482
2483 return ROOT::Fit::Chisquare(*this, *func, useRange, type);
2484}
2485
2486////////////////////////////////////////////////////////////////////////////////
2487/// Remove all the content from the underflow and overflow bins, without changing the number of entries
2488/// After calling this method, every undeflow and overflow bins will have content 0.0
2489/// The Sumw2 is also cleared, since there is no more content in the bins
2490
2492{
2493 for (Int_t bin = 0; bin < fNcells; ++bin)
2494 if (IsBinUnderflow(bin) || IsBinOverflow(bin)) {
2495 UpdateBinContent(bin, 0.0);
2496 if (fSumw2.fN) fSumw2.fArray[bin] = 0.0;
2497 }
2498}
2499
2500////////////////////////////////////////////////////////////////////////////////
2501/// Compute integral (normalized cumulative sum of bins) w/o under/overflows
2502/// The result is stored in fIntegral and used by the GetRandom functions.
2503/// This function is automatically called by GetRandom when the fIntegral
2504/// array does not exist or when the number of entries in the histogram
2505/// has changed since the previous call to GetRandom.
2506/// The resulting integral is normalized to 1.
2507/// If the routine is called with the onlyPositive flag set an error will
2508/// be produced in case of negative bin content and a NaN value returned
2509/// \return 1 if success, 0 if integral is zero, NAN if onlyPositive-test fails
2510
2512{
2513 if (fBuffer) BufferEmpty();
2514
2515 // delete previously computed integral (if any)
2516 if (fIntegral) delete [] fIntegral;
2517
2518 // - Allocate space to store the integral and compute integral
2522 Int_t nbins = nbinsx * nbinsy * nbinsz;
2523
2524 fIntegral = new Double_t[nbins + 2];
2525 Int_t ibin = 0; fIntegral[ibin] = 0;
2526
2527 for (Int_t binz=1; binz <= nbinsz; ++binz) {
2528 for (Int_t biny=1; biny <= nbinsy; ++biny) {
2529 for (Int_t binx=1; binx <= nbinsx; ++binx) {
2530 ++ibin;
2532 if (onlyPositive && y < 0) {
2533 Error("ComputeIntegral","Bin content is negative - return a NaN value");
2534 fIntegral[nbins] = TMath::QuietNaN();
2535 break;
2536 }
2537 fIntegral[ibin] = fIntegral[ibin - 1] + y;
2538 }
2539 }
2540 }
2541
2542 // - Normalize integral to 1
2543 if (fIntegral[nbins] == 0 ) {
2544 Error("ComputeIntegral", "Integral = 0, no hits in histogram bins (excluding over/underflow).");
2545 return 0;
2546 }
2547 for (Int_t bin=1; bin <= nbins; ++bin) fIntegral[bin] /= fIntegral[nbins];
2548 fIntegral[nbins+1] = fEntries;
2549 return fIntegral[nbins];
2550}
2551
2552////////////////////////////////////////////////////////////////////////////////
2553/// Return a pointer to the array of bins integral.
2554/// if the pointer fIntegral is null, TH1::ComputeIntegral is called
2555/// The array dimension is the number of bins in the histograms
2556/// including underflow and overflow (fNCells)
2557/// the last value integral[fNCells] is set to the number of entries of
2558/// the histogram
2559
2561{
2562 if (!fIntegral) ComputeIntegral();
2563 return fIntegral;
2564}
2565
2566////////////////////////////////////////////////////////////////////////////////
2567/// Return a pointer to a histogram containing the cumulative content.
2568/// The cumulative can be computed both in the forward (default) or backward
2569/// direction; the name of the new histogram is constructed from
2570/// the name of this histogram with the suffix "suffix" appended provided
2571/// by the user. If not provided a default suffix="_cumulative" is used.
2572///
2573/// The cumulative distribution is formed by filling each bin of the
2574/// resulting histogram with the sum of that bin and all previous
2575/// (forward == kTRUE) or following (forward = kFALSE) bins.
2576///
2577/// Note: while cumulative distributions make sense in one dimension, you
2578/// may not be getting what you expect in more than 1D because the concept
2579/// of a cumulative distribution is much trickier to define; make sure you
2580/// understand the order of summation before you use this method with
2581/// histograms of dimension >= 2.
2582///
2583/// Note 2: By default the cumulative is computed from bin 1 to Nbins
2584/// If an axis range is set, values between the minimum and maximum of the range
2585/// are set.
2586/// Setting an axis range can also be used for including underflow and overflow in
2587/// the cumulative (e.g. by setting h->GetXaxis()->SetRange(0, h->GetNbinsX()+1); )
2589
2590TH1 *TH1::GetCumulative(Bool_t forward, const char* suffix) const
2591{
2592 const Int_t firstX = fXaxis.GetFirst();
2593 const Int_t lastX = fXaxis.GetLast();
2594 const Int_t firstY = (fDimension > 1) ? fYaxis.GetFirst() : 1;
2595 const Int_t lastY = (fDimension > 1) ? fYaxis.GetLast() : 1;
2596 const Int_t firstZ = (fDimension > 1) ? fZaxis.GetFirst() : 1;
2597 const Int_t lastZ = (fDimension > 1) ? fZaxis.GetLast() : 1;
2598
2600 hintegrated->Reset();
2601 Double_t sum = 0.;
2602 Double_t esum = 0;
2603 if (forward) { // Forward computation
2604 for (Int_t binz = firstZ; binz <= lastZ; ++binz) {
2605 for (Int_t biny = firstY; biny <= lastY; ++biny) {
2606 for (Int_t binx = firstX; binx <= lastX; ++binx) {
2607 const Int_t bin = hintegrated->GetBin(binx, biny, binz);
2608 sum += RetrieveBinContent(bin);
2609 hintegrated->AddBinContent(bin, sum);
2610 if (fSumw2.fN) {
2612 hintegrated->fSumw2.fArray[bin] = esum;
2613 }
2614 }
2615 }
2616 }
2617 } else { // Backward computation
2618 for (Int_t binz = lastZ; binz >= firstZ; --binz) {
2619 for (Int_t biny = lastY; biny >= firstY; --biny) {
2620 for (Int_t binx = lastX; binx >= firstX; --binx) {
2621 const Int_t bin = hintegrated->GetBin(binx, biny, binz);
2622 sum += RetrieveBinContent(bin);
2623 hintegrated->AddBinContent(bin, sum);
2624 if (fSumw2.fN) {
2626 hintegrated->fSumw2.fArray[bin] = esum;
2627 }
2628 }
2629 }
2630 }
2631 }
2632 return hintegrated;
2633}
2634
2635////////////////////////////////////////////////////////////////////////////////
2636/// Copy this histogram structure to newth1.
2637///
2638/// Note that this function does not copy the list of associated functions.
2639/// Use TObject::Clone to make a full copy of a histogram.
2640///
2641/// Note also that the histogram it will be created in gDirectory (if AddDirectoryStatus()=true)
2642/// or will not be added to any directory if AddDirectoryStatus()=false
2643/// independently of the current directory stored in the original histogram
2644
2645void TH1::Copy(TObject &obj) const
2646{
2647 if (((TH1&)obj).fDirectory) {
2648 // We are likely to change the hash value of this object
2649 // with TNamed::Copy, to keep things correct, we need to
2650 // clean up its existing entries.
2651 ((TH1&)obj).fDirectory->Remove(&obj);
2652 ((TH1&)obj).fDirectory = nullptr;
2653 }
2654 TNamed::Copy(obj);
2655 ((TH1&)obj).fDimension = fDimension;
2656 ((TH1&)obj).fNormFactor= fNormFactor;
2657 ((TH1&)obj).fNcells = fNcells;
2658 ((TH1&)obj).fBarOffset = fBarOffset;
2659 ((TH1&)obj).fBarWidth = fBarWidth;
2660 ((TH1&)obj).fOption = fOption;
2661 ((TH1&)obj).fBinStatErrOpt = fBinStatErrOpt;
2662 ((TH1&)obj).fBufferSize= fBufferSize;
2663 // copy the Buffer
2664 // delete first a previously existing buffer
2665 if (((TH1&)obj).fBuffer != nullptr) {
2666 delete [] ((TH1&)obj).fBuffer;
2667 ((TH1&)obj).fBuffer = nullptr;
2668 }
2669 if (fBuffer) {
2670 Double_t *buf = new Double_t[fBufferSize];
2671 for (Int_t i=0;i<fBufferSize;i++) buf[i] = fBuffer[i];
2672 // obj.fBuffer has been deleted before
2673 ((TH1&)obj).fBuffer = buf;
2674 }
2675
2676 // copy bin contents (this should be done by the derived classes, since TH1 does not store the bin content)
2677 // Do this in case derived from TArray
2678 TArray* a = dynamic_cast<TArray*>(&obj);
2679 if (a) {
2680 a->Set(fNcells);
2681 for (Int_t i = 0; i < fNcells; i++)
2683 }
2684
2685 ((TH1&)obj).fEntries = fEntries;
2686
2687 // which will call BufferEmpty(0) and set fBuffer[0] to a Maybe one should call
2688 // assignment operator on the TArrayD
2689
2690 ((TH1&)obj).fTsumw = fTsumw;
2691 ((TH1&)obj).fTsumw2 = fTsumw2;
2692 ((TH1&)obj).fTsumwx = fTsumwx;
2693 ((TH1&)obj).fTsumwx2 = fTsumwx2;
2694 ((TH1&)obj).fMaximum = fMaximum;
2695 ((TH1&)obj).fMinimum = fMinimum;
2696
2697 TAttLine::Copy(((TH1&)obj));
2698 TAttFill::Copy(((TH1&)obj));
2699 TAttMarker::Copy(((TH1&)obj));
2700 fXaxis.Copy(((TH1&)obj).fXaxis);
2701 fYaxis.Copy(((TH1&)obj).fYaxis);
2702 fZaxis.Copy(((TH1&)obj).fZaxis);
2703 ((TH1&)obj).fXaxis.SetParent(&obj);
2704 ((TH1&)obj).fYaxis.SetParent(&obj);
2705 ((TH1&)obj).fZaxis.SetParent(&obj);
2706 fContour.Copy(((TH1&)obj).fContour);
2707 fSumw2.Copy(((TH1&)obj).fSumw2);
2708 // fFunctions->Copy(((TH1&)obj).fFunctions);
2709 // when copying an histogram if the AddDirectoryStatus() is true it
2710 // will be added to gDirectory independently of the fDirectory stored.
2711 // and if the AddDirectoryStatus() is false it will not be added to
2712 // any directory (fDirectory = nullptr)
2713 if (fgAddDirectory && gDirectory) {
2714 gDirectory->Append(&obj);
2715 ((TH1&)obj).fFunctions->UseRWLock();
2716 ((TH1&)obj).fDirectory = gDirectory;
2717 } else
2718 ((TH1&)obj).fDirectory = nullptr;
2719
2720}
2721
2722////////////////////////////////////////////////////////////////////////////////
2723/// Make a complete copy of the underlying object. If 'newname' is set,
2724/// the copy's name will be set to that name.
2725
2726TObject* TH1::Clone(const char* newname) const
2727{
2728 TH1* obj = (TH1*)IsA()->GetNew()(nullptr);
2729 Copy(*obj);
2730
2731 // Now handle the parts that Copy doesn't do
2732 if(fFunctions) {
2733 // The Copy above might have published 'obj' to the ListOfCleanups.
2734 // Clone can call RecursiveRemove, for example via TCheckHashRecursiveRemoveConsistency
2735 // when dictionary information is initialized, so we need to
2736 // keep obj->fFunction valid during its execution and
2737 // protect the update with the write lock.
2738
2739 // Reset stats parent - else cloning the stats will clone this histogram, too.
2740 auto oldstats = dynamic_cast<TVirtualPaveStats*>(fFunctions->FindObject("stats"));
2741 TObject *oldparent = nullptr;
2742 if (oldstats) {
2743 oldparent = oldstats->GetParent();
2744 oldstats->SetParent(nullptr);
2745 }
2746
2747 auto newlist = (TList*)fFunctions->Clone();
2748
2749 if (oldstats)
2750 oldstats->SetParent(oldparent);
2751 auto newstats = dynamic_cast<TVirtualPaveStats*>(obj->fFunctions->FindObject("stats"));
2752 if (newstats)
2753 newstats->SetParent(obj);
2754
2755 auto oldlist = obj->fFunctions;
2756 {
2758 obj->fFunctions = newlist;
2759 }
2760 delete oldlist;
2761 }
2762 if(newname && strlen(newname) ) {
2763 obj->SetName(newname);
2764 }
2765 return obj;
2766}
2767
2768////////////////////////////////////////////////////////////////////////////////
2769/// Perform the automatic addition of the histogram to the given directory
2770///
2771/// Note this function is called in place when the semantic requires
2772/// this object to be added to a directory (I.e. when being read from
2773/// a TKey or being Cloned)
2774
2776{
2778 if (addStatus) {
2779 SetDirectory(dir);
2780 if (dir) {
2782 }
2783 }
2784}
2785
2786////////////////////////////////////////////////////////////////////////////////
2787/// Compute distance from point px,py to a line.
2788///
2789/// Compute the closest distance of approach from point px,py to elements
2790/// of a histogram.
2791/// The distance is computed in pixels units.
2792///
2793/// #### Algorithm:
2794/// Currently, this simple model computes the distance from the mouse
2795/// to the histogram contour only.
2796
2798{
2799 if (!fPainter) return 9999;
2800 return fPainter->DistancetoPrimitive(px,py);
2801}
2802
2803////////////////////////////////////////////////////////////////////////////////
2804/// Performs the operation: `this = this/(c1*f1)`
2805/// if errors are defined (see TH1::Sumw2), errors are also recalculated.
2806///
2807/// Only bins inside the function range are recomputed.
2808/// IMPORTANT NOTE: If you intend to use the errors of this histogram later
2809/// you should call Sumw2 before making this operation.
2810/// This is particularly important if you fit the histogram after TH1::Divide
2811///
2812/// The function return kFALSE if the divide operation failed
2813
2815{
2816 if (!f1) {
2817 Error("Divide","Attempt to divide by a non-existing function");
2818 return kFALSE;
2819 }
2820
2821 // delete buffer if it is there since it will become invalid
2822 if (fBuffer) BufferEmpty(1);
2823
2824 Int_t nx = GetNbinsX() + 2; // normal bins + uf / of
2825 Int_t ny = GetNbinsY() + 2;
2826 Int_t nz = GetNbinsZ() + 2;
2827 if (fDimension < 2) ny = 1;
2828 if (fDimension < 3) nz = 1;
2829
2830
2831 SetMinimum();
2832 SetMaximum();
2833
2834 // - Loop on bins (including underflows/overflows)
2835 Int_t bin, binx, biny, binz;
2836 Double_t cu, w;
2837 Double_t xx[3];
2838 Double_t *params = nullptr;
2839 f1->InitArgs(xx,params);
2840 for (binz = 0; binz < nz; ++binz) {
2841 xx[2] = fZaxis.GetBinCenter(binz);
2842 for (biny = 0; biny < ny; ++biny) {
2843 xx[1] = fYaxis.GetBinCenter(biny);
2844 for (binx = 0; binx < nx; ++binx) {
2845 xx[0] = fXaxis.GetBinCenter(binx);
2846 if (!f1->IsInside(xx)) continue;
2848 bin = binx + nx * (biny + ny * binz);
2849 cu = c1 * f1->EvalPar(xx);
2850 if (TF1::RejectedPoint()) continue;
2851 if (cu) w = RetrieveBinContent(bin) / cu;
2852 else w = 0;
2853 UpdateBinContent(bin, w);
2854 if (fSumw2.fN) {
2855 if (cu != 0) fSumw2.fArray[bin] = GetBinErrorSqUnchecked(bin) / (cu * cu);
2856 else fSumw2.fArray[bin] = 0;
2857 }
2858 }
2859 }
2860 }
2861 ResetStats();
2862 return kTRUE;
2863}
2864
2865////////////////////////////////////////////////////////////////////////////////
2866/// Divide this histogram by h1.
2867///
2868/// `this = this/h1`
2869/// if errors are defined (see TH1::Sumw2), errors are also recalculated.
2870/// Note that if h1 has Sumw2 set, Sumw2 is automatically called for this
2871/// if not already set.
2872/// The resulting errors are calculated assuming uncorrelated histograms.
2873/// See the other TH1::Divide that gives the possibility to optionally
2874/// compute binomial errors.
2875///
2876/// IMPORTANT NOTE: If you intend to use the errors of this histogram later
2877/// you should call Sumw2 before making this operation.
2878/// This is particularly important if you fit the histogram after TH1::Scale
2879///
2880/// The function return kFALSE if the divide operation failed
2881
2882Bool_t TH1::Divide(const TH1 *h1)
2883{
2884 if (!h1) {
2885 Error("Divide", "Input histogram passed does not exist (NULL).");
2886 return kFALSE;
2887 }
2888
2889 // delete buffer if it is there since it will become invalid
2890 if (fBuffer) BufferEmpty(1);
2891
2892 if (LoggedInconsistency("Divide", this, h1) >= kDifferentNumberOfBins) {
2893 return false;
2894 }
2895
2896 // Create Sumw2 if h1 has Sumw2 set
2897 if (fSumw2.fN == 0 && h1->GetSumw2N() != 0) Sumw2();
2898
2899 // - Loop on bins (including underflows/overflows)
2900 for (Int_t i = 0; i < fNcells; ++i) {
2903 if (c1) UpdateBinContent(i, c0 / c1);
2904 else UpdateBinContent(i, 0);
2905
2906 if(fSumw2.fN) {
2907 if (c1 == 0) { fSumw2.fArray[i] = 0; continue; }
2908 Double_t c1sq = c1 * c1;
2909 fSumw2.fArray[i] = (GetBinErrorSqUnchecked(i) * c1sq + h1->GetBinErrorSqUnchecked(i) * c0 * c0) / (c1sq * c1sq);
2910 }
2911 }
2912 ResetStats();
2913 return kTRUE;
2914}
2915
2916////////////////////////////////////////////////////////////////////////////////
2917/// Replace contents of this histogram by the division of h1 by h2.
2918///
2919/// `this = c1*h1/(c2*h2)`
2920///
2921/// If errors are defined (see TH1::Sumw2), errors are also recalculated
2922/// Note that if h1 or h2 have Sumw2 set, Sumw2 is automatically called for this
2923/// if not already set.
2924/// The resulting errors are calculated assuming uncorrelated histograms.
2925/// However, if option ="B" is specified, Binomial errors are computed.
2926/// In this case c1 and c2 do not make real sense and they are ignored.
2927///
2928/// IMPORTANT NOTE: If you intend to use the errors of this histogram later
2929/// you should call Sumw2 before making this operation.
2930/// This is particularly important if you fit the histogram after TH1::Divide
2931///
2932/// Please note also that in the binomial case errors are calculated using standard
2933/// binomial statistics, which means when b1 = b2, the error is zero.
2934/// If you prefer to have efficiency errors not going to zero when the efficiency is 1, you must
2935/// use the function TGraphAsymmErrors::BayesDivide, which will return an asymmetric and non-zero lower
2936/// error for the case b1=b2.
2937///
2938/// The function return kFALSE if the divide operation failed
2939
2941{
2942
2943 TString opt = option;
2944 opt.ToLower();
2945 Bool_t binomial = kFALSE;
2946 if (opt.Contains("b")) binomial = kTRUE;
2947 if (!h1 || !h2) {
2948 Error("Divide", "At least one of the input histograms passed does not exist (NULL).");
2949 return kFALSE;
2950 }
2951
2952 // delete buffer if it is there since it will become invalid
2953 if (fBuffer) BufferEmpty(1);
2954
2955 if (LoggedInconsistency("Divide", this, h1) >= kDifferentNumberOfBins ||
2956 LoggedInconsistency("Divide", h1, h2) >= kDifferentNumberOfBins) {
2957 return false;
2958 }
2959
2960 if (!c2) {
2961 Error("Divide","Coefficient of dividing histogram cannot be zero");
2962 return kFALSE;
2963 }
2964
2965 // Create Sumw2 if h1 or h2 have Sumw2 set, or if binomial errors are explicitly requested
2966 if (fSumw2.fN == 0 && (h1->GetSumw2N() != 0 || h2->GetSumw2N() != 0 || binomial)) Sumw2();
2967
2968 SetMinimum();
2969 SetMaximum();
2970
2971 // - Loop on bins (including underflows/overflows)
2972 for (Int_t i = 0; i < fNcells; ++i) {
2974 Double_t b2 = h2->RetrieveBinContent(i);
2975 if (b2) UpdateBinContent(i, c1 * b1 / (c2 * b2));
2976 else UpdateBinContent(i, 0);
2977
2978 if (fSumw2.fN) {
2979 if (b2 == 0) { fSumw2.fArray[i] = 0; continue; }
2980 Double_t b1sq = b1 * b1; Double_t b2sq = b2 * b2;
2981 Double_t c1sq = c1 * c1; Double_t c2sq = c2 * c2;
2983 Double_t e2sq = h2->GetBinErrorSqUnchecked(i);
2984 if (binomial) {
2985 if (b1 != b2) {
2986 // in the case of binomial statistics c1 and c2 must be 1 otherwise it does not make sense
2987 // c1 and c2 are ignored
2988 //fSumw2.fArray[bin] = TMath::Abs(w*(1-w)/(c2*b2));//this is the formula in Hbook/Hoper1
2989 //fSumw2.fArray[bin] = TMath::Abs(w*(1-w)/b2); // old formula from G. Flucke
2990 // formula which works also for weighted histogram (see http://root-forum.cern.ch/viewtopic.php?t=3753 )
2991 fSumw2.fArray[i] = TMath::Abs( ( (1. - 2.* b1 / b2) * e1sq + b1sq * e2sq / b2sq ) / b2sq );
2992 } else {
2993 //in case b1=b2 error is zero
2994 //use TGraphAsymmErrors::BayesDivide for getting the asymmetric error not equal to zero
2995 fSumw2.fArray[i] = 0;
2996 }
2997 } else {
2998 fSumw2.fArray[i] = c1sq * c2sq * (e1sq * b2sq + e2sq * b1sq) / (c2sq * c2sq * b2sq * b2sq);
2999 }
3000 }
3001 }
3002 ResetStats();
3003 if (binomial)
3004 // in case of binomial division use denominator for number of entries
3005 SetEntries ( h2->GetEntries() );
3006
3007 return kTRUE;
3008}
3009
3010////////////////////////////////////////////////////////////////////////////////
3011/// Draw this histogram with options.
3012///
3013/// Histograms are drawn via the THistPainter class. Each histogram has
3014/// a pointer to its own painter (to be usable in a multithreaded program).
3015/// The same histogram can be drawn with different options in different pads.
3016/// When a histogram drawn in a pad is deleted, the histogram is
3017/// automatically removed from the pad or pads where it was drawn.
3018/// If a histogram is drawn in a pad, then filled again, the new status
3019/// of the histogram will be automatically shown in the pad next time
3020/// the pad is updated. One does not need to redraw the histogram.
3021/// To draw the current version of a histogram in a pad, one can use
3022/// `h->DrawCopy();`
3023/// This makes a clone of the histogram. Once the clone is drawn, the original
3024/// histogram may be modified or deleted without affecting the aspect of the
3025/// clone.
3026/// By default, TH1::Draw clears the current pad.
3027///
3028/// One can use TH1::SetMaximum and TH1::SetMinimum to force a particular
3029/// value for the maximum or the minimum scale on the plot.
3030///
3031/// TH1::UseCurrentStyle can be used to change all histogram graphics
3032/// attributes to correspond to the current selected style.
3033/// This function must be called for each histogram.
3034/// In case one reads and draws many histograms from a file, one can force
3035/// the histograms to inherit automatically the current graphics style
3036/// by calling before gROOT->ForceStyle();
3037///
3038/// See the THistPainter class for a description of all the drawing options.
3039
3041{
3042 TString opt1 = option; opt1.ToLower();
3044 Int_t index = opt1.Index("same");
3045
3046 // Check if the string "same" is part of a TCutg name.
3047 if (index>=0) {
3048 Int_t indb = opt1.Index("[");
3049 if (indb>=0) {
3050 Int_t indk = opt1.Index("]");
3051 if (index>indb && index<indk) index = -1;
3052 }
3053 }
3054
3055 // If there is no pad or an empty pad the "same" option is ignored.
3056 if (gPad) {
3057 if (!gPad->IsEditable()) gROOT->MakeDefCanvas();
3058 if (index>=0) {
3059 if (gPad->GetX1() == 0 && gPad->GetX2() == 1 &&
3060 gPad->GetY1() == 0 && gPad->GetY2() == 1 &&
3061 gPad->GetListOfPrimitives()->GetSize()==0) opt2.Remove(index,4);
3062 } else {
3063 //the following statement is necessary in case one attempts to draw
3064 //a temporary histogram already in the current pad
3065 if (TestBit(kCanDelete)) gPad->Remove(this);
3066 gPad->Clear();
3067 }
3068 gPad->IncrementPaletteColor(1, opt1);
3069 } else {
3070 if (index>=0) opt2.Remove(index,4);
3071 }
3072
3073 AppendPad(opt2.Data());
3074}
3075
3076////////////////////////////////////////////////////////////////////////////////
3077/// Copy this histogram and Draw in the current pad.
3078///
3079/// Once the histogram is drawn into the pad, any further modification
3080/// using graphics input will be made on the copy of the histogram,
3081/// and not to the original object.
3082/// By default a postfix "_copy" is added to the histogram name. Pass an empty postfix in case
3083/// you want to draw a histogram with the same name
3084///
3085/// See Draw for the list of options
3086
3087TH1 *TH1::DrawCopy(Option_t *option, const char * name_postfix) const
3088{
3089 TString opt = option;
3090 opt.ToLower();
3091 if (gPad && !opt.Contains("same")) gPad->Clear();
3093 if (name_postfix) newName.Form("%s%s", GetName(), name_postfix);
3094 TH1 *newth1 = (TH1 *)Clone(newName.Data());
3095 newth1->SetDirectory(nullptr);
3096 newth1->SetBit(kCanDelete);
3097 if (gPad) gPad->IncrementPaletteColor(1, opt);
3098
3099 newth1->AppendPad(option);
3100 return newth1;
3101}
3102
3103////////////////////////////////////////////////////////////////////////////////
3104/// Draw a normalized copy of this histogram.
3105///
3106/// A clone of this histogram is normalized to norm and drawn with option.
3107/// A pointer to the normalized histogram is returned.
3108/// The contents of the histogram copy are scaled such that the new
3109/// sum of weights (excluding under and overflow) is equal to norm.
3110/// Note that the returned normalized histogram is not added to the list
3111/// of histograms in the current directory in memory.
3112/// It is the user's responsibility to delete this histogram.
3113/// The kCanDelete bit is set for the returned object. If a pad containing
3114/// this copy is cleared, the histogram will be automatically deleted.
3115///
3116/// See Draw for the list of options
3117
3119{
3121 if (sum == 0) {
3122 Error("DrawNormalized","Sum of weights is null. Cannot normalize histogram: %s",GetName());
3123 return nullptr;
3124 }
3127 TH1 *h = (TH1*)Clone();
3129 // in case of drawing with error options - scale correctly the error
3130 TString opt(option); opt.ToUpper();
3131 if (fSumw2.fN == 0) {
3132 h->Sumw2();
3133 // do not use in this case the "Error option " for drawing which is enabled by default since the normalized histogram has now errors
3134 if (opt.IsNull() || opt == "SAME") opt += "HIST";
3135 }
3136 h->Scale(norm/sum);
3137 if (TMath::Abs(fMaximum+1111) > 1e-3) h->SetMaximum(fMaximum*norm/sum);
3138 if (TMath::Abs(fMinimum+1111) > 1e-3) h->SetMinimum(fMinimum*norm/sum);
3139 h->Draw(opt);
3141 return h;
3142}
3143
3144////////////////////////////////////////////////////////////////////////////////
3145/// Display a panel with all histogram drawing options.
3146///
3147/// See class TDrawPanelHist for example
3148
3149void TH1::DrawPanel()
3150{
3151 if (!fPainter) {Draw(); if (gPad) gPad->Update();}
3152 if (fPainter) fPainter->DrawPanel();
3153}
3154
3155////////////////////////////////////////////////////////////////////////////////
3156/// Evaluate function f1 at the center of bins of this histogram.
3157///
3158/// - If option "R" is specified, the function is evaluated only
3159/// for the bins included in the function range.
3160/// - If option "A" is specified, the value of the function is added to the
3161/// existing bin contents
3162/// - If option "S" is specified, the value of the function is used to
3163/// generate a value, distributed according to the Poisson
3164/// distribution, with f1 as the mean.
3165
3167{
3168 Double_t x[3];
3169 Int_t range, stat, add;
3170 if (!f1) return;
3171
3172 TString opt = option;
3173 opt.ToLower();
3174 if (opt.Contains("a")) add = 1;
3175 else add = 0;
3176 if (opt.Contains("s")) stat = 1;
3177 else stat = 0;
3178 if (opt.Contains("r")) range = 1;
3179 else range = 0;
3180
3181 // delete buffer if it is there since it will become invalid
3182 if (fBuffer) BufferEmpty(1);
3183
3187 if (!add) Reset();
3188
3189 for (Int_t binz = 1; binz <= nbinsz; ++binz) {
3190 x[2] = fZaxis.GetBinCenter(binz);
3191 for (Int_t biny = 1; biny <= nbinsy; ++biny) {
3192 x[1] = fYaxis.GetBinCenter(biny);
3193 for (Int_t binx = 1; binx <= nbinsx; ++binx) {
3194 Int_t bin = GetBin(binx,biny,binz);
3195 x[0] = fXaxis.GetBinCenter(binx);
3196 if (range && !f1->IsInside(x)) continue;
3197 Double_t fu = f1->Eval(x[0], x[1], x[2]);
3198 if (stat) fu = gRandom->PoissonD(fu);
3199 AddBinContent(bin, fu);
3200 if (fSumw2.fN) fSumw2.fArray[bin] += TMath::Abs(fu);
3201 }
3202 }
3203 }
3204}
3205
3206////////////////////////////////////////////////////////////////////////////////
3207/// Execute action corresponding to one event.
3208///
3209/// This member function is called when a histogram is clicked with the locator
3210///
3211/// If Left button clicked on the bin top value, then the content of this bin
3212/// is modified according to the new position of the mouse when it is released.
3213
3214void TH1::ExecuteEvent(Int_t event, Int_t px, Int_t py)
3215{
3216 if (fPainter) fPainter->ExecuteEvent(event, px, py);
3217}
3218
3219////////////////////////////////////////////////////////////////////////////////
3220/// This function allows to do discrete Fourier transforms of TH1 and TH2.
3221/// Available transform types and flags are described below.
3222///
3223/// To extract more information about the transform, use the function
3224/// TVirtualFFT::GetCurrentTransform() to get a pointer to the current
3225/// transform object.
3226///
3227/// \param[out] h_output histogram for the output. If a null pointer is passed, a new histogram is created
3228/// and returned, otherwise, the provided histogram is used and should be big enough
3229/// \param[in] option option parameters consists of 3 parts:
3230/// - option on what to return
3231/// - "RE" - returns a histogram of the real part of the output
3232/// - "IM" - returns a histogram of the imaginary part of the output
3233/// - "MAG"- returns a histogram of the magnitude of the output
3234/// - "PH" - returns a histogram of the phase of the output
3235/// - option of transform type
3236/// - "R2C" - real to complex transforms - default
3237/// - "R2HC" - real to halfcomplex (special format of storing output data,
3238/// results the same as for R2C)
3239/// - "DHT" - discrete Hartley transform
3240/// real to real transforms (sine and cosine):
3241/// - "R2R_0", "R2R_1", "R2R_2", "R2R_3" - discrete cosine transforms of types I-IV
3242/// - "R2R_4", "R2R_5", "R2R_6", "R2R_7" - discrete sine transforms of types I-IV
3243/// To specify the type of each dimension of a 2-dimensional real to real
3244/// transform, use options of form "R2R_XX", for example, "R2R_02" for a transform,
3245/// which is of type "R2R_0" in 1st dimension and "R2R_2" in the 2nd.
3246/// - option of transform flag
3247/// - "ES" (from "estimate") - no time in preparing the transform, but probably sub-optimal
3248/// performance
3249/// - "M" (from "measure") - some time spend in finding the optimal way to do the transform
3250/// - "P" (from "patient") - more time spend in finding the optimal way to do the transform
3251/// - "EX" (from "exhaustive") - the most optimal way is found
3252/// This option should be chosen depending on how many transforms of the same size and
3253/// type are going to be done. Planning is only done once, for the first transform of this
3254/// size and type. Default is "ES".
3255///
3256/// Examples of valid options: "Mag R2C M" "Re R2R_11" "Im R2C ES" "PH R2HC EX"
3257
3259{
3260
3261 Int_t ndim[3];
3262 ndim[0] = this->GetNbinsX();
3263 ndim[1] = this->GetNbinsY();
3264 ndim[2] = this->GetNbinsZ();
3265
3267 TString opt = option;
3268 opt.ToUpper();
3269 if (!opt.Contains("2R")){
3270 if (!opt.Contains("2C") && !opt.Contains("2HC") && !opt.Contains("DHT")) {
3271 //no type specified, "R2C" by default
3272 opt.Append("R2C");
3273 }
3274 fft = TVirtualFFT::FFT(this->GetDimension(), ndim, opt.Data());
3275 }
3276 else {
3277 //find the kind of transform
3278 Int_t ind = opt.Index("R2R", 3);
3279 Int_t *kind = new Int_t[2];
3280 char t;
3281 t = opt[ind+4];
3282 kind[0] = atoi(&t);
3283 if (h_output->GetDimension()>1) {
3284 t = opt[ind+5];
3285 kind[1] = atoi(&t);
3286 }
3287 fft = TVirtualFFT::SineCosine(this->GetDimension(), ndim, kind, option);
3288 delete [] kind;
3289 }
3290
3291 if (!fft) return nullptr;
3292 Int_t in=0;
3293 for (Int_t binx = 1; binx<=ndim[0]; binx++) {
3294 for (Int_t biny=1; biny<=ndim[1]; biny++) {
3295 for (Int_t binz=1; binz<=ndim[2]; binz++) {
3296 fft->SetPoint(in, this->GetBinContent(binx, biny, binz));
3297 in++;
3298 }
3299 }
3300 }
3301 fft->Transform();
3303 return h_output;
3304}
3305
3306////////////////////////////////////////////////////////////////////////////////
3307/// Increment bin with abscissa X by 1.
3308///
3309/// if x is less than the low-edge of the first bin, the Underflow bin is incremented
3310/// if x is equal to or greater than the upper edge of last bin, the Overflow bin is incremented
3311///
3312/// If the storage of the sum of squares of weights has been triggered,
3313/// via the function Sumw2, then the sum of the squares of weights is incremented
3314/// by 1 in the bin corresponding to x.
3315///
3316/// The function returns the corresponding bin number which has its content incremented by 1
3317
3319{
3320 if (fBuffer) return BufferFill(x,1);
3321
3322 Int_t bin;
3323 fEntries++;
3324 bin =fXaxis.FindBin(x);
3325 if (bin <0) return -1;
3326 AddBinContent(bin);
3327 if (fSumw2.fN) ++fSumw2.fArray[bin];
3328 if (bin == 0 || bin > fXaxis.GetNbins()) {
3329 if (!GetStatOverflowsBehaviour()) return -1;
3330 }
3331 ++fTsumw;
3332 ++fTsumw2;
3333 fTsumwx += x;
3334 fTsumwx2 += x*x;
3335 return bin;
3336}
3337
3338////////////////////////////////////////////////////////////////////////////////
3339/// Increment bin with abscissa X with a weight w.
3340///
3341/// if x is less than the low-edge of the first bin, the Underflow bin is incremented
3342/// if x is equal to or greater than the upper edge of last bin, the Overflow bin is incremented
3343///
3344/// If the weight is not equal to 1, the storage of the sum of squares of
3345/// weights is automatically triggered and the sum of the squares of weights is incremented
3346/// by \f$ w^2 \f$ in the bin corresponding to x.
3347///
3348/// The function returns the corresponding bin number which has its content incremented by w
3349
3351{
3352
3353 if (fBuffer) return BufferFill(x,w);
3354
3355 Int_t bin;
3356 fEntries++;
3357 bin =fXaxis.FindBin(x);
3358 if (bin <0) return -1;
3359 if (!fSumw2.fN && w != 1.0 && !TestBit(TH1::kIsNotW) ) Sumw2(); // must be called before AddBinContent
3360 if (fSumw2.fN) fSumw2.fArray[bin] += w*w;
3361 AddBinContent(bin, w);
3362 if (bin == 0 || bin > fXaxis.GetNbins()) {
3363 if (!GetStatOverflowsBehaviour()) return -1;
3364 }
3365 Double_t z= w;
3366 fTsumw += z;
3367 fTsumw2 += z*z;
3368 fTsumwx += z*x;
3369 fTsumwx2 += z*x*x;
3370 return bin;
3371}
3372
3373////////////////////////////////////////////////////////////////////////////////
3374/// Increment bin with namex with a weight w
3375///
3376/// if x is less than the low-edge of the first bin, the Underflow bin is incremented
3377/// if x is equal to or greater than the upper edge of last bin, the Overflow bin is incremented
3378///
3379/// If the weight is not equal to 1, the storage of the sum of squares of
3380/// weights is automatically triggered and the sum of the squares of weights is incremented
3381/// by \f$ w^2 \f$ in the bin corresponding to x.
3382///
3383/// The function returns the corresponding bin number which has its content
3384/// incremented by w.
3385
3386Int_t TH1::Fill(const char *namex, Double_t w)
3387{
3388 Int_t bin;
3389 fEntries++;
3390 bin =fXaxis.FindBin(namex);
3391 if (bin <0) return -1;
3392 if (!fSumw2.fN && w != 1.0 && !TestBit(TH1::kIsNotW)) Sumw2();
3393 if (fSumw2.fN) fSumw2.fArray[bin] += w*w;
3394 AddBinContent(bin, w);
3395 if (bin == 0 || bin > fXaxis.GetNbins()) return -1;
3396 Double_t z= w;
3397 fTsumw += z;
3398 fTsumw2 += z*z;
3399 // this make sense if the histogram is not expanding (the x axis cannot be extended)
3400 if (!fXaxis.CanExtend() || !fXaxis.IsAlphanumeric()) {
3402 fTsumwx += z*x;
3403 fTsumwx2 += z*x*x;
3404 }
3405 return bin;
3406}
3407
3408////////////////////////////////////////////////////////////////////////////////
3409/// Fill this histogram with an array x and weights w.
3410///
3411/// \param[in] ntimes number of entries in arrays x and w (array size must be ntimes*stride)
3412/// \param[in] x array of values to be histogrammed
3413/// \param[in] w array of weighs
3414/// \param[in] stride step size through arrays x and w
3415///
3416/// If the weight is not equal to 1, the storage of the sum of squares of
3417/// weights is automatically triggered and the sum of the squares of weights is incremented
3418/// by \f$ w^2 \f$ in the bin corresponding to x.
3419/// if w is NULL each entry is assumed a weight=1
3420
3421void TH1::FillN(Int_t ntimes, const Double_t *x, const Double_t *w, Int_t stride)
3422{
3423 //If a buffer is activated, fill buffer
3424 if (fBuffer) {
3425 ntimes *= stride;
3426 Int_t i = 0;
3427 for (i=0;i<ntimes;i+=stride) {
3428 if (!fBuffer) break; // buffer can be deleted in BufferFill when is empty
3429 if (w) BufferFill(x[i],w[i]);
3430 else BufferFill(x[i], 1.);
3431 }
3432 // fill the remaining entries if the buffer has been deleted
3433 if (i < ntimes && !fBuffer) {
3434 auto weights = w ? &w[i] : nullptr;
3435 DoFillN((ntimes-i)/stride,&x[i],weights,stride);
3436 }
3437 return;
3438 }
3439 // call internal method
3440 DoFillN(ntimes, x, w, stride);
3441}
3442
3443////////////////////////////////////////////////////////////////////////////////
3444/// Internal method to fill histogram content from a vector
3445/// called directly by TH1::BufferEmpty
3446
3447void TH1::DoFillN(Int_t ntimes, const Double_t *x, const Double_t *w, Int_t stride)
3448{
3449 Int_t bin,i;
3450
3451 fEntries += ntimes;
3452 Double_t ww = 1;
3453 Int_t nbins = fXaxis.GetNbins();
3454 ntimes *= stride;
3455 for (i=0;i<ntimes;i+=stride) {
3456 bin =fXaxis.FindBin(x[i]);
3457 if (bin <0) continue;
3458 if (w) ww = w[i];
3459 if (!fSumw2.fN && ww != 1.0 && !TestBit(TH1::kIsNotW)) Sumw2();
3460 if (fSumw2.fN) fSumw2.fArray[bin] += ww*ww;
3461 AddBinContent(bin, ww);
3462 if (bin == 0 || bin > nbins) {
3463 if (!GetStatOverflowsBehaviour()) continue;
3464 }
3465 Double_t z= ww;
3466 fTsumw += z;
3467 fTsumw2 += z*z;
3468 fTsumwx += z*x[i];
3469 fTsumwx2 += z*x[i]*x[i];
3470 }
3471}
3472
3473////////////////////////////////////////////////////////////////////////////////
3474/// Fill histogram following distribution in function fname.
3475///
3476/// @param fname : Function name used for filling the histogram
3477/// @param ntimes : number of times the histogram is filled
3478/// @param rng : (optional) Random number generator used to sample
3479///
3480///
3481/// The distribution contained in the function fname (TF1) is integrated
3482/// over the channel contents for the bin range of this histogram.
3483/// It is normalized to 1.
3484///
3485/// Getting one random number implies:
3486/// - Generating a random number between 0 and 1 (say r1)
3487/// - Look in which bin in the normalized integral r1 corresponds to
3488/// - Fill histogram channel
3489/// ntimes random numbers are generated
3490///
3491/// One can also call TF1::GetRandom to get a random variate from a function.
3492
3493void TH1::FillRandom(const char *fname, Int_t ntimes, TRandom * rng)
3494{
3495 // - Search for fname in the list of ROOT defined functions
3496 TF1 *f1 = (TF1*)gROOT->GetFunction(fname);
3497 if (!f1) { Error("FillRandom", "Unknown function: %s",fname); return; }
3498
3501
3503{
3504 Int_t bin, binx, ibin, loop;
3505 Double_t r1, x;
3506
3507 // - Allocate temporary space to store the integral and compute integral
3508
3509 TAxis * xAxis = &fXaxis;
3510
3511 // in case axis of histogram is not defined use the function axis
3512 if (fXaxis.GetXmax() <= fXaxis.GetXmin()) {
3514 f1->GetRange(xmin,xmax);
3515 Info("FillRandom","Using function axis and range [%g,%g]",xmin, xmax);
3516 xAxis = f1->GetHistogram()->GetXaxis();
3517 }
3518
3519 Int_t first = xAxis->GetFirst();
3520 Int_t last = xAxis->GetLast();
3521 Int_t nbinsx = last-first+1;
3522
3523 Double_t *integral = new Double_t[nbinsx+1];
3524 integral[0] = 0;
3525 for (binx=1;binx<=nbinsx;binx++) {
3526 Double_t fint = f1->Integral(xAxis->GetBinLowEdge(binx+first-1),xAxis->GetBinUpEdge(binx+first-1), 0.);
3527 integral[binx] = integral[binx-1] + fint;
3528 }
3529
3530 // - Normalize integral to 1
3531 if (integral[nbinsx] == 0 ) {
3532 delete [] integral;
3533 Error("FillRandom", "Integral = zero"); return;
3534 }
3535 for (bin=1;bin<=nbinsx;bin++) integral[bin] /= integral[nbinsx];
3536
3537 // --------------Start main loop ntimes
3538 for (loop=0;loop<ntimes;loop++) {
3539 r1 = (rng) ? rng->Rndm() : gRandom->Rndm();
3540 ibin = TMath::BinarySearch(nbinsx,&integral[0],r1);
3541 //binx = 1 + ibin;
3542 //x = xAxis->GetBinCenter(binx); //this is not OK when SetBuffer is used
3543 x = xAxis->GetBinLowEdge(ibin+first)
3544 +xAxis->GetBinWidth(ibin+first)*(r1-integral[ibin])/(integral[ibin+1] - integral[ibin]);
3545 Fill(x);
3546 }
3547 delete [] integral;
3548}
3549
3550////////////////////////////////////////////////////////////////////////////////
3551/// Fill histogram following distribution in histogram h.
3552///
3553/// @param h : Histogram pointer used for sampling random number
3554/// @param ntimes : number of times the histogram is filled
3555/// @param rng : (optional) Random number generator used for sampling
3556///
3557/// The distribution contained in the histogram h (TH1) is integrated
3558/// over the channel contents for the bin range of this histogram.
3559/// It is normalized to 1.
3560///
3561/// Getting one random number implies:
3562/// - Generating a random number between 0 and 1 (say r1)
3563/// - Look in which bin in the normalized integral r1 corresponds to
3564/// - Fill histogram channel ntimes random numbers are generated
3565///
3566/// SPECIAL CASE when the target histogram has the same binning as the source.
3567/// in this case we simply use a poisson distribution where
3568/// the mean value per bin = bincontent/integral.
3569
3571{
3572 if (!h) { Error("FillRandom", "Null histogram"); return; }
3573 if (fDimension != h->GetDimension()) {
3574 Error("FillRandom", "Histograms with different dimensions"); return;
3575 }
3576 if (std::isnan(h->ComputeIntegral(true))) {
3577 Error("FillRandom", "Histograms contains negative bins, does not represent probabilities");
3578 return;
3579 }
3580
3581 //in case the target histogram has the same binning and ntimes much greater
3582 //than the number of bins we can use a fast method
3583 Int_t first = fXaxis.GetFirst();
3584 Int_t last = fXaxis.GetLast();
3585 Int_t nbins = last-first+1;
3586 if (ntimes > 10*nbins) {
3587 auto inconsistency = CheckConsistency(this,h);
3588 if (inconsistency != kFullyConsistent) return; // do nothing
3589 Double_t sumw = h->Integral(first,last);
3590 if (sumw == 0) return;
3591 Double_t sumgen = 0;
3592 for (Int_t bin=first;bin<=last;bin++) {
3593 Double_t mean = h->RetrieveBinContent(bin)*ntimes/sumw;
3594 Double_t cont = (rng) ? rng->Poisson(mean) : gRandom->Poisson(mean);
3595 sumgen += cont;
3596 AddBinContent(bin,cont);
3597 if (fSumw2.fN) fSumw2.fArray[bin] += cont;
3598 }
3599
3600 // fix for the fluctuations in the total number n
3601 // since we use Poisson instead of multinomial
3602 // add a correction to have ntimes as generated entries
3603 Int_t i;
3604 if (sumgen < ntimes) {
3605 // add missing entries
3606 for (i = Int_t(sumgen+0.5); i < ntimes; ++i)
3607 {
3608 Double_t x = h->GetRandom();
3609 Fill(x);
3610 }
3611 }
3612 else if (sumgen > ntimes) {
3613 // remove extra entries
3614 i = Int_t(sumgen+0.5);
3615 while( i > ntimes) {
3616 Double_t x = h->GetRandom(rng);
3619 // skip in case bin is empty
3620 if (y > 0) {
3621 SetBinContent(ibin, y-1.);
3622 i--;
3623 }
3624 }
3625 }
3626
3627 ResetStats();
3628 return;
3629 }
3630 // case of different axis and not too large ntimes
3631
3632 if (h->ComputeIntegral() ==0) return;
3633 Int_t loop;
3634 Double_t x;
3635 for (loop=0;loop<ntimes;loop++) {
3636 x = h->GetRandom();
3637 Fill(x);
3638 }
3639}
3640
3641////////////////////////////////////////////////////////////////////////////////
3642/// Return Global bin number corresponding to x,y,z
3643///
3644/// 2-D and 3-D histograms are represented with a one dimensional
3645/// structure. This has the advantage that all existing functions, such as
3646/// GetBinContent, GetBinError, GetBinFunction work for all dimensions.
3647/// This function tries to extend the axis if the given point belongs to an
3648/// under-/overflow bin AND if CanExtendAllAxes() is true.
3649///
3650/// See also TH1::GetBin, TAxis::FindBin and TAxis::FindFixBin
3651
3653{
3654 if (GetDimension() < 2) {
3655 return fXaxis.FindBin(x);
3656 }
3657 if (GetDimension() < 3) {
3658 Int_t nx = fXaxis.GetNbins()+2;
3661 return binx + nx*biny;
3662 }
3663 if (GetDimension() < 4) {
3664 Int_t nx = fXaxis.GetNbins()+2;
3665 Int_t ny = fYaxis.GetNbins()+2;
3668 Int_t binz = fZaxis.FindBin(z);
3669 return binx + nx*(biny +ny*binz);
3670 }
3671 return -1;
3672}
3673
3674////////////////////////////////////////////////////////////////////////////////
3675/// Return Global bin number corresponding to x,y,z.
3676///
3677/// 2-D and 3-D histograms are represented with a one dimensional
3678/// structure. This has the advantage that all existing functions, such as
3679/// GetBinContent, GetBinError, GetBinFunction work for all dimensions.
3680/// This function DOES NOT try to extend the axis if the given point belongs
3681/// to an under-/overflow bin.
3682///
3683/// See also TH1::GetBin, TAxis::FindBin and TAxis::FindFixBin
3684
3686{
3687 if (GetDimension() < 2) {
3688 return fXaxis.FindFixBin(x);
3689 }
3690 if (GetDimension() < 3) {
3691 Int_t nx = fXaxis.GetNbins()+2;
3694 return binx + nx*biny;
3695 }
3696 if (GetDimension() < 4) {
3697 Int_t nx = fXaxis.GetNbins()+2;
3698 Int_t ny = fYaxis.GetNbins()+2;
3702 return binx + nx*(biny +ny*binz);
3703 }
3704 return -1;
3705}
3706
3707////////////////////////////////////////////////////////////////////////////////
3708/// Find first bin with content > threshold for axis (1=x, 2=y, 3=z)
3709/// if no bins with content > threshold is found the function returns -1.
3710/// The search will occur between the specified first and last bin. Specifying
3711/// the value of the last bin to search to less than zero will search until the
3712/// last defined bin.
3713
3715{
3716 if (fBuffer) ((TH1*)this)->BufferEmpty();
3717
3718 if (axis < 1 || (axis > 1 && GetDimension() == 1 ) ||
3719 ( axis > 2 && GetDimension() == 2 ) || ( axis > 3 && GetDimension() > 3 ) ) {
3720 Warning("FindFirstBinAbove","Invalid axis number : %d, axis x assumed\n",axis);
3721 axis = 1;
3722 }
3723 if (firstBin < 1) {
3724 firstBin = 1;
3725 }
3727 Int_t nbinsy = (GetDimension() > 1 ) ? fYaxis.GetNbins() : 1;
3728 Int_t nbinsz = (GetDimension() > 2 ) ? fZaxis.GetNbins() : 1;
3729
3730 if (axis == 1) {
3733 }
3734 for (Int_t binx = firstBin; binx <= lastBin; binx++) {
3735 for (Int_t biny = 1; biny <= nbinsy; biny++) {
3736 for (Int_t binz = 1; binz <= nbinsz; binz++) {
3738 }
3739 }
3740 }
3741 }
3742 else if (axis == 2) {
3745 }
3746 for (Int_t biny = firstBin; biny <= lastBin; biny++) {
3747 for (Int_t binx = 1; binx <= nbinsx; binx++) {
3748 for (Int_t binz = 1; binz <= nbinsz; binz++) {
3750 }
3751 }
3752 }
3753 }
3754 else if (axis == 3) {
3757 }
3758 for (Int_t binz = firstBin; binz <= lastBin; binz++) {
3759 for (Int_t binx = 1; binx <= nbinsx; binx++) {
3760 for (Int_t biny = 1; biny <= nbinsy; biny++) {
3762 }
3763 }
3764 }
3765 }
3766
3767 return -1;
3768}
3769
3770////////////////////////////////////////////////////////////////////////////////
3771/// Find last bin with content > threshold for axis (1=x, 2=y, 3=z)
3772/// if no bins with content > threshold is found the function returns -1.
3773/// The search will occur between the specified first and last bin. Specifying
3774/// the value of the last bin to search to less than zero will search until the
3775/// last defined bin.
3776
3778{
3779 if (fBuffer) ((TH1*)this)->BufferEmpty();
3780
3781
3782 if (axis < 1 || ( axis > 1 && GetDimension() == 1 ) ||
3783 ( axis > 2 && GetDimension() == 2 ) || ( axis > 3 && GetDimension() > 3) ) {
3784 Warning("FindFirstBinAbove","Invalid axis number : %d, axis x assumed\n",axis);
3785 axis = 1;
3786 }
3787 if (firstBin < 1) {
3788 firstBin = 1;
3789 }
3791 Int_t nbinsy = (GetDimension() > 1 ) ? fYaxis.GetNbins() : 1;
3792 Int_t nbinsz = (GetDimension() > 2 ) ? fZaxis.GetNbins() : 1;
3793
3794 if (axis == 1) {
3797 }
3798 for (Int_t binx = lastBin; binx >= firstBin; binx--) {
3799 for (Int_t biny = 1; biny <= nbinsy; biny++) {
3800 for (Int_t binz = 1; binz <= nbinsz; binz++) {
3802 }
3803 }
3804 }
3805 }
3806 else if (axis == 2) {
3809 }
3810 for (Int_t biny = lastBin; biny >= firstBin; biny--) {
3811 for (Int_t binx = 1; binx <= nbinsx; binx++) {
3812 for (Int_t binz = 1; binz <= nbinsz; binz++) {
3814 }
3815 }
3816 }
3817 }
3818 else if (axis == 3) {
3821 }
3822 for (Int_t binz = lastBin; binz >= firstBin; binz--) {
3823 for (Int_t binx = 1; binx <= nbinsx; binx++) {
3824 for (Int_t biny = 1; biny <= nbinsy; biny++) {
3826 }
3827 }
3828 }
3829 }
3830
3831 return -1;
3832}
3833
3834////////////////////////////////////////////////////////////////////////////////
3835/// Search object named name in the list of functions.
3836
3837TObject *TH1::FindObject(const char *name) const
3838{
3839 if (fFunctions) return fFunctions->FindObject(name);
3840 return nullptr;
3841}
3842
3843////////////////////////////////////////////////////////////////////////////////
3844/// Search object obj in the list of functions.
3845
3846TObject *TH1::FindObject(const TObject *obj) const
3847{
3848 if (fFunctions) return fFunctions->FindObject(obj);
3849 return nullptr;
3850}
3851
3852////////////////////////////////////////////////////////////////////////////////
3853/// Fit histogram with function fname.
3854///
3855///
3856/// fname is the name of a function available in the global ROOT list of functions
3857/// `gROOT->GetListOfFunctions`
3858/// The list include any TF1 object created by the user plus some pre-defined functions
3859/// which are automatically created by ROOT the first time a pre-defined function is requested from `gROOT`
3860/// (i.e. when calling `gROOT->GetFunction(const char *name)`).
3861/// These pre-defined functions are:
3862/// - `gaus, gausn` where gausn is the normalized Gaussian
3863/// - `landau, landaun`
3864/// - `expo`
3865/// - `pol1,...9, chebyshev1,...9`.
3866///
3867/// For printing the list of all available functions do:
3868///
3869/// TF1::InitStandardFunctions(); // not needed if `gROOT->GetFunction` is called before
3870/// gROOT->GetListOfFunctions()->ls()
3871///
3872/// `fname` can also be a formula that is accepted by the linear fitter containing the special operator `++`,
3873/// representing linear components separated by `++` sign, for example `x++sin(x)` for fitting `[0]*x+[1]*sin(x)`
3874///
3875/// This function finds a pointer to the TF1 object with name `fname` and calls TH1::Fit(TF1 *, Option_t *, Option_t *,
3876/// Double_t, Double_t). See there for the fitting options and the details about fitting histograms
3877
3879{
3880 char *linear;
3881 linear= (char*)strstr(fname, "++");
3882 Int_t ndim=GetDimension();
3883 if (linear){
3884 if (ndim<2){
3886 return Fit(&f1,option,goption,xxmin,xxmax);
3887 }
3888 else if (ndim<3){
3889 TF2 f2(fname, fname);
3890 return Fit(&f2,option,goption,xxmin,xxmax);
3891 }
3892 else{
3893 TF3 f3(fname, fname);
3894 return Fit(&f3,option,goption,xxmin,xxmax);
3895 }
3896 }
3897 else{
3898 TF1 * f1 = (TF1*)gROOT->GetFunction(fname);
3899 if (!f1) { Printf("Unknown function: %s",fname); return -1; }
3900 return Fit(f1,option,goption,xxmin,xxmax);
3901 }
3902}
3903
3904////////////////////////////////////////////////////////////////////////////////
3905/// Fit histogram with the function pointer f1.
3906///
3907/// \param[in] f1 pointer to the function object
3908/// \param[in] option string defining the fit options (see table below).
3909/// \param[in] goption specify a list of graphics options. See TH1::Draw for a complete list of these options.
3910/// \param[in] xxmin lower fitting range
3911/// \param[in] xxmax upper fitting range
3912/// \return A smart pointer to the TFitResult class
3913///
3914/// \anchor HFitOpt
3915/// ### Histogram Fitting Options
3916///
3917/// Here is the full list of fit options that can be given in the parameter `option`.
3918/// Several options can be used together by concatanating the strings without the need of any delimiters.
3919///
3920/// option | description
3921/// -------|------------
3922/// "L" | Uses a log likelihood method (default is chi-square method). To be used when the histogram represents counts.
3923/// "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.
3924/// "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.
3925/// "MULTI" | Uses Loglikelihood method based on multi-nomial distribution. In this case the function must be normalized and one fits only the function shape.
3926/// "W" | Fit using the chi-square method and ignoring the bin uncertainties and skip empty bins.
3927/// "WW" | Fit using the chi-square method and ignoring the bin uncertainties and include the empty bins.
3928/// "I" | Uses the integral of function in the bin instead of the default bin center value.
3929/// "F" | Uses the default minimizer (e.g. Minuit) when fitting a linear function (e.g. polN) instead of the linear fitter.
3930/// "U" | Uses a user specified objective function (e.g. user providedlikelihood function) defined using `TVirtualFitter::SetFCN`
3931/// "E" | Performs a better parameter errors estimation using the Minos technique for all fit parameters.
3932/// "M" | Uses the IMPROVE algorithm (available only in TMinuit). This algorithm attempts improve the found local minimum by searching for a better one.
3933/// "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`.
3934/// "Q" | Quiet mode (minimum printing)
3935/// "V" | Verbose mode (default is between Q and V)
3936/// "+" | 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.
3937/// "N" | Does not store the graphics function, does not draw the histogram with the function after fitting.
3938/// "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.
3939/// "R" | Fit using a fitting range specified in the function range with `TF1::SetRange`.
3940/// "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.
3941/// "C" | In case of linear fitting, do no calculate the chisquare (saves CPU time).
3942/// "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.
3943/// "WIDTH" | Scales the histogran bin content by the bin width (useful for variable bins histograms)
3944/// "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
3945/// "MULTITHREAD" | Forces usage of multi-thread execution whenever possible
3946///
3947/// The default fitting of an histogram (when no option is given) is perfomed as following:
3948/// - a chi-square fit (see below Chi-square Fits) computed using the bin histogram errors and excluding bins with zero errors (empty bins);
3949/// - the full range of the histogram is used, unless TAxis::SetRange or TAxis::SetRangeUser was called before;
3950/// - the default Minimizer with its default configuration is used (see below Minimizer Configuration) except for linear function;
3951/// - for linear functions (`polN`, `chenbyshev` or formula expressions combined using operator `++`) a linear minimization is used.
3952/// - only the status of the fit is returned;
3953/// - the fit is performed in Multithread whenever is enabled in ROOT;
3954/// - only the last fitted function is saved in the histogram;
3955/// - the histogram is drawn after fitting overalyed with the resulting fitting function
3956///
3957/// \anchor HFitMinimizer
3958/// ### Minimizer Configuration
3959///
3960/// The Fit is perfomed using the default Minimizer, defined in the `ROOT::Math::MinimizerOptions` class.
3961/// It is possible to change the default minimizer and its configuration parameters by calling these static functions before fitting (before calling `TH1::Fit`):
3962/// - `ROOT::Math::MinimizerOptions::SetDefaultMinimizer(minimizerName, minimizerAgorithm)` for changing the minmizer and/or the corresponding algorithm.
3963/// For example `ROOT::Math::MinimizerOptions::SetDefaultMinimizer("GSLMultiMin","BFGS");` will set the usage of the BFGS algorithm of the GSL multi-dimensional minimization
3964/// The current defaults are ("Minuit","Migrad").
3965/// See the documentation of the `ROOT::Math::MinimizerOptions` for the available minimizers in ROOT and their corresponding algorithms.
3966/// - `ROOT::Math::MinimizerOptions::SetDefaultTolerance` for setting a different tolerance value for the minimization.
3967/// - `ROOT::Math::MinimizerOptions::SetDefaultMaxFunctionCalls` for setting the maximum number of function calls.
3968/// - `ROOT::Math::MinimizerOptions::SetDefaultPrintLevel` for changing the minimizer print level from level=0 (minimal printing) to level=3 maximum printing
3969///
3970/// Other options are possible depending on the Minimizer used, see the corresponding documentation.
3971/// The default minimizer can be also set in the resource file in etc/system.rootrc. For example
3972///
3973/// ~~~ {.cpp}
3974/// Root.Fitter: Minuit2
3975/// ~~~
3976///
3977/// \anchor HFitChi2
3978/// ### Chi-square Fits
3979///
3980/// By default a chi-square (least-square) fit is performed on the histogram. The so-called modified least-square method
3981/// is used where the residual for each bin is computed using as error the observed value (the bin error) returned by `TH1::GetBinError`
3982///
3983/// \f[
3984/// Chi2 = \sum_{i}{ \left(\frac{y(i) - f(x(i) | p )}{e(i)} \right)^2 }
3985/// \f]
3986///
3987/// 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
3988/// an un-weighted histogram). Bins with zero errors are excluded from the fit. See also later the note on the treatment
3989/// of empty bins. When using option "I" the residual is computed not using the function value at the bin center, `f(x(i)|p)`,
3990/// but the integral of the function in the bin, Integral{ f(x|p)dx }, divided by the bin volume.
3991/// When using option `P` (Pearson chi2), the expected error computed as `e(i) = sqrt(f(x(i)|p))` is used.
3992/// In this case empty bins are considered in the fit.
3993/// Both chi-square methods should not be used when the bin content represent counts, especially in case of low bin statistics,
3994/// because they could return a biased result.
3995///
3996/// \anchor HFitNLL
3997/// ### Likelihood Fits
3998///
3999/// When using option "L" a likelihood fit is used instead of the default chi-square fit.
4000/// The likelihood is built assuming a Poisson probability density function for each bin.
4001/// The negative log-likelihood to be minimized is
4002///
4003/// \f[
4004/// NLL = - \sum_{i}{ \log {\mathrm P} ( y(i) | f(x(i) | p ) ) }
4005/// \f]
4006/// 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)`.
4007/// The exact likelihood used is the Poisson likelihood described in this paper:
4008/// S. Baker and R. D. Cousins, “Clarification of the use of chi-square and likelihood functions in fits to histograms,”
4009/// Nucl. Instrum. Meth. 221 (1984) 437.
4010///
4011/// \f[
4012/// NLL = \sum_{i}{( f(x(i) | p ) + y(i)\log(y(i)/ f(x(i) | p )) - y(i)) }
4013/// \f]
4014/// By using this formulation, `2*NLL` can be interpreted as the chi-square resulting from the fit.
4015///
4016/// This method should be always used when the bin content represents counts (i.e. errors are sqrt(N) ).
4017/// The likelihood method has the advantage of treating correctly bins with low statistics. In case of high
4018/// statistics/bin the distribution of the bin content becomes a normal distribution and the likelihood and the chi2 fit
4019/// give the same result.
4020///
4021/// The likelihood method, although a bit slower, it is therefore the recommended method,
4022/// when the histogram represent counts (Poisson statistics), where the chi-square methods may
4023/// give incorrect results, especially in case of low statistics.
4024/// In case of a weighted histogram, it is possible to perform also a likelihood fit by using the
4025/// option "WL". Note a weighted histogram is a histogram which has been filled with weights and it
4026/// has the information on the sum of the weight square for each bin ( TH1::Sumw2() has been called).
4027/// The bin error for a weighted histogram is the square root of the sum of the weight square.
4028///
4029/// \anchor HFitRes
4030/// ### Fit Result
4031///
4032/// The function returns a TFitResultPtr which can hold a pointer to a TFitResult object.
4033/// By default the TFitResultPtr contains only the status of the fit which is return by an
4034/// automatic conversion of the TFitResultPtr to an integer. One can write in this case directly:
4035///
4036/// ~~~ {.cpp}
4037/// Int_t fitStatus = h->Fit(myFunc);
4038/// ~~~
4039///
4040/// If the option "S" is instead used, TFitResultPtr behaves as a smart
4041/// pointer to the TFitResult object. This is useful for retrieving the full result information from the fit, such as the covariance matrix,
4042/// as shown in this example code:
4043///
4044/// ~~~ {.cpp}
4045/// TFitResultPtr r = h->Fit(myFunc,"S");
4046/// TMatrixDSym cov = r->GetCovarianceMatrix(); // to access the covariance matrix
4047/// Double_t chi2 = r->Chi2(); // to retrieve the fit chi2
4048/// Double_t par0 = r->Parameter(0); // retrieve the value for the parameter 0
4049/// Double_t err0 = r->ParError(0); // retrieve the error for the parameter 0
4050/// r->Print("V"); // print full information of fit including covariance matrix
4051/// r->Write(); // store the result in a file
4052/// ~~~
4053///
4054/// The fit parameters, error and chi-square (but not covariance matrix) can be retrieved also
4055/// directly from the fitted function that is passed to this call.
4056/// Given a pointer to an associated fitted function `myfunc`, one can retrieve the function/fit
4057/// parameters with calls such as:
4058///
4059/// ~~~ {.cpp}
4060/// Double_t chi2 = myfunc->GetChisquare();
4061/// Double_t par0 = myfunc->GetParameter(0); //value of 1st parameter
4062/// Double_t err0 = myfunc->GetParError(0); //error on first parameter
4063/// ~~~
4064///
4065/// ##### Associated functions
4066///
4067/// One or more objects (typically a TF1*) can be added to the list
4068/// of functions (fFunctions) associated to each histogram.
4069/// When TH1::Fit is invoked, the fitted function is added to the histogram list of functions (fFunctions).
4070/// If the histogram is made persistent, the list of associated functions is also persistent.
4071/// Given a histogram h, one can retrieve an associated function with:
4072///
4073/// ~~~ {.cpp}
4074/// TF1 *myfunc = h->GetFunction("myfunc");
4075/// ~~~
4076/// or by quering directly the list obtained by calling `TH1::GetListOfFunctions`.
4077///
4078/// \anchor HFitStatus
4079/// ### Fit status
4080///
4081/// The status of the fit is obtained converting the TFitResultPtr to an integer
4082/// independently if the fit option "S" is used or not:
4083///
4084/// ~~~ {.cpp}
4085/// TFitResultPtr r = h->Fit(myFunc,opt);
4086/// Int_t fitStatus = r;
4087/// ~~~
4088///
4089/// - `status = 0` : the fit has been performed successfully (i.e no error occurred).
4090/// - `status < 0` : there is an error not connected with the minimization procedure, for example when a wrong function is used.
4091/// - `status > 0` : return status from Minimizer, depends on used Minimizer. For example for TMinuit and Minuit2 we have:
4092/// - `status = migradStatus + 10*minosStatus + 100*hesseStatus + 1000*improveStatus`.
4093/// 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
4094/// only in Minos but not in Migrad a fitStatus of 40 will be returned.
4095/// Minuit2 returns 0 in case of success and different values in migrad,minos or
4096/// hesse depending on the error. See in this case the documentation of
4097/// Minuit2Minimizer::Minimize for the migrad return status, Minuit2Minimizer::GetMinosError for the
4098/// minos return status and Minuit2Minimizer::Hesse for the hesse return status.
4099/// If other minimizers are used see their specific documentation for the status code returned.
4100/// For example in the case of Fumili, see TFumili::Minimize.
4101///
4102/// \anchor HFitRange
4103/// ### Fitting in a range
4104///
4105/// In order to fit in a sub-range of the histogram you have two options:
4106/// - pass to this function the lower (`xxmin`) and upper (`xxmax`) values for the fitting range;
4107/// - define a specific range in the fitted function and use the fitting option "R".
4108/// For example, if your histogram has a defined range between -4 and 4 and you want to fit a gaussian
4109/// only in the interval 1 to 3, you can do:
4110///
4111/// ~~~ {.cpp}
4112/// TF1 *f1 = new TF1("f1", "gaus", 1, 3);
4113/// histo->Fit("f1", "R");
4114/// ~~~
4115///
4116/// The fitting range is also limited by the histogram range defined using TAxis::SetRange
4117/// or TAxis::SetRangeUser. Therefore the fitting range is the smallest range between the
4118/// histogram one and the one defined by one of the two previous options described above.
4119///
4120/// \anchor HFitInitial
4121/// ### Setting initial conditions
4122///
4123/// Parameters must be initialized before invoking the Fit function.
4124/// The setting of the parameter initial values is automatic for the
4125/// predefined functions such as poln, expo, gaus, landau. One can however disable
4126/// this automatic computation by using the option "B".
4127/// Note that if a predefined function is defined with an argument,
4128/// eg, gaus(0), expo(1), you must specify the initial values for
4129/// the parameters.
4130/// You can specify boundary limits for some or all parameters via
4131///
4132/// ~~~ {.cpp}
4133/// f1->SetParLimits(p_number, parmin, parmax);
4134/// ~~~
4135///
4136/// if `parmin >= parmax`, the parameter is fixed
4137/// Note that you are not forced to fix the limits for all parameters.
4138/// For example, if you fit a function with 6 parameters, you can do:
4139///
4140/// ~~~ {.cpp}
4141/// func->SetParameters(0, 3.1, 1.e-6, -8, 0, 100);
4142/// func->SetParLimits(3, -10, -4);
4143/// func->FixParameter(4, 0);
4144/// func->SetParLimits(5, 1, 1);
4145/// ~~~
4146///
4147/// With this setup, parameters 0->2 can vary freely
4148/// Parameter 3 has boundaries [-10,-4] with initial value -8
4149/// Parameter 4 is fixed to 0
4150/// Parameter 5 is fixed to 100.
4151/// When the lower limit and upper limit are equal, the parameter is fixed.
4152/// However to fix a parameter to 0, one must call the FixParameter function.
4153///
4154/// \anchor HFitStatBox
4155/// ### Fit Statistics Box
4156///
4157/// The statistics box can display the result of the fit.
4158/// You can change the statistics box to display the fit parameters with
4159/// the TStyle::SetOptFit(mode) method. This mode has four digits.
4160/// mode = pcev (default = 0111)
4161///
4162/// v = 1; print name/values of parameters
4163/// e = 1; print errors (if e=1, v must be 1)
4164/// c = 1; print Chisquare/Number of degrees of freedom
4165/// p = 1; print Probability
4166///
4167/// For example: gStyle->SetOptFit(1011);
4168/// prints the fit probability, parameter names/values, and errors.
4169/// You can change the position of the statistics box with these lines
4170/// (where g is a pointer to the TGraph):
4171///
4172/// TPaveStats *st = (TPaveStats*)g->GetListOfFunctions()->FindObject("stats");
4173/// st->SetX1NDC(newx1); //new x start position
4174/// st->SetX2NDC(newx2); //new x end position
4175///
4176/// \anchor HFitExtra
4177/// ### Additional Notes on Fitting
4178///
4179/// #### Fitting a histogram of dimension N with a function of dimension N-1
4180///
4181/// It is possible to fit a TH2 with a TF1 or a TH3 with a TF2.
4182/// 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.
4183/// For correct error scaling, the obtained parameter error are corrected as in the case when the
4184/// option "W" is used.
4185///
4186/// #### User defined objective functions
4187///
4188/// By default when fitting a chi square function is used for fitting. When option "L" is used
4189/// a Poisson likelihood function is used. Using option "MULTI" a multinomial likelihood fit is used.
4190/// Thes functions are defined in the header Fit/Chi2Func.h or Fit/PoissonLikelihoodFCN and they
4191/// are implemented using the routines FitUtil::EvaluateChi2 or FitUtil::EvaluatePoissonLogL in
4192/// the file math/mathcore/src/FitUtil.cxx.
4193/// It is possible to specify a user defined fitting function, using option "U" and
4194/// calling the following functions:
4195///
4196/// ~~~ {.cpp}
4197/// TVirtualFitter::Fitter(myhist)->SetFCN(MyFittingFunction);
4198/// ~~~
4199///
4200/// where MyFittingFunction is of type:
4201///
4202/// ~~~ {.cpp}
4203/// extern void MyFittingFunction(Int_t &npar, Double_t *gin, Double_t &f, Double_t *u, Int_t flag);
4204/// ~~~
4205///
4206/// #### Note on treatment of empty bins
4207///
4208/// Empty bins, which have the content equal to zero AND error equal to zero,
4209/// are excluded by default from the chi-square fit, but they are considered in the likelihood fit.
4210/// since they affect the likelihood if the function value in these bins is not negligible.
4211/// Note that if the histogram is having bins with zero content and non zero-errors they are considered as
4212/// any other bins in the fit. Instead bins with zero error and non-zero content are by default excluded in the chi-squared fit.
4213/// In general, one should not fit a histogram with non-empty bins and zero errors.
4214///
4215/// 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
4216/// fit). When using option "WW" the empty bins will be also considered in the chi-square fit with an error of 1.
4217/// Note that in this fitting case (option "W" or "WW") the resulting fitted parameter errors
4218/// are corrected by the obtained chi2 value using this scaling expression:
4219/// `errorp *= sqrt(chisquare/(ndf-1))` as it is done when fitting a TGraph with
4220/// no point errors.
4221///
4222/// #### Excluding points
4223///
4224/// You can use TF1::RejectPoint inside your fitting function to exclude some points
4225/// within a certain range from the fit. See the tutorial `fit/fitExclude.C`.
4226///
4227///
4228/// #### Warning when using the option "0"
4229///
4230/// When selecting the option "0", the fitted function is added to
4231/// the list of functions of the histogram, but it is not drawn when the histogram is drawn.
4232/// You can undo this behaviour resetting its corresponding bit in the TF1 object as following:
4233///
4234/// ~~~ {.cpp}
4235/// h.Fit("myFunction", "0"); // fit, store function but do not draw
4236/// h.Draw(); // function is not drawn
4237/// h.GetFunction("myFunction")->ResetBit(TF1::kNotDraw);
4238/// h.Draw(); // function is visible again
4239/// ~~~
4241
4243{
4244 // implementation of Fit method is in file hist/src/HFitImpl.cxx
4247
4248 // create range and minimizer options with default values
4251
4252 // need to empty the buffer before
4253 // (t.b.d. do a ML unbinned fit with buffer data)
4254 if (fBuffer) BufferEmpty();
4255
4257}
4258
4259////////////////////////////////////////////////////////////////////////////////
4260/// Display a panel with all histogram fit options.
4261///
4262/// See class TFitPanel for example
4263
4264void TH1::FitPanel()
4265{
4266 if (!gPad)
4267 gROOT->MakeDefCanvas();
4268
4269 if (!gPad) {
4270 Error("FitPanel", "Unable to create a default canvas");
4271 return;
4272 }
4273
4274
4275 // use plugin manager to create instance of TFitEditor
4276 TPluginHandler *handler = gROOT->GetPluginManager()->FindHandler("TFitEditor");
4277 if (handler && handler->LoadPlugin() != -1) {
4278 if (handler->ExecPlugin(2, gPad, this) == 0)
4279 Error("FitPanel", "Unable to create the FitPanel");
4280 }
4281 else
4282 Error("FitPanel", "Unable to find the FitPanel plug-in");
4283}
4284
4285////////////////////////////////////////////////////////////////////////////////
4286/// Return a histogram containing the asymmetry of this histogram with h2,
4287/// where the asymmetry is defined as:
4288///
4289/// ~~~ {.cpp}
4290/// Asymmetry = (h1 - h2)/(h1 + h2) where h1 = this
4291/// ~~~
4292///
4293/// works for 1D, 2D, etc. histograms
4294/// c2 is an optional argument that gives a relative weight between the two
4295/// histograms, and dc2 is the error on this weight. This is useful, for example,
4296/// when forming an asymmetry between two histograms from 2 different data sets that
4297/// need to be normalized to each other in some way. The function calculates
4298/// the errors assuming Poisson statistics on h1 and h2 (that is, dh = sqrt(h)).
4299///
4300/// example: assuming 'h1' and 'h2' are already filled
4301///
4302/// ~~~ {.cpp}
4303/// h3 = h1->GetAsymmetry(h2)
4304/// ~~~
4305///
4306/// then 'h3' is created and filled with the asymmetry between 'h1' and 'h2';
4307/// h1 and h2 are left intact.
4308///
4309/// Note that it is the user's responsibility to manage the created histogram.
4310/// The name of the returned histogram will be `Asymmetry_nameOfh1-nameOfh2`
4311///
4312/// code proposed by Jason Seely (seely@mit.edu) and adapted by R.Brun
4313///
4314/// clone the histograms so top and bottom will have the
4315/// correct dimensions:
4316/// Sumw2 just makes sure the errors will be computed properly
4317/// when we form sums and ratios below.
4318
4320{
4321 TH1 *h1 = this;
4322 TString name = TString::Format("Asymmetry_%s-%s",h1->GetName(),h2->GetName() );
4323 TH1 *asym = (TH1*)Clone(name);
4324
4325 // set also the title
4326 TString title = TString::Format("(%s - %s)/(%s+%s)",h1->GetName(),h2->GetName(),h1->GetName(),h2->GetName() );
4327 asym->SetTitle(title);
4328
4329 asym->Sumw2();
4332 TH1 *top = (TH1*)asym->Clone();
4333 TH1 *bottom = (TH1*)asym->Clone();
4335
4336 // form the top and bottom of the asymmetry, and then divide:
4337 top->Add(h1,h2,1,-c2);
4338 bottom->Add(h1,h2,1,c2);
4339 asym->Divide(top,bottom);
4340
4341 Int_t xmax = asym->GetNbinsX();
4342 Int_t ymax = asym->GetNbinsY();
4343 Int_t zmax = asym->GetNbinsZ();
4344
4345 if (h1->fBuffer) h1->BufferEmpty(1);
4346 if (h2->fBuffer) h2->BufferEmpty(1);
4347 if (bottom->fBuffer) bottom->BufferEmpty(1);
4348
4349 // now loop over bins to calculate the correct errors
4350 // the reason this error calculation looks complex is because of c2
4351 for(Int_t i=1; i<= xmax; i++){
4352 for(Int_t j=1; j<= ymax; j++){
4353 for(Int_t k=1; k<= zmax; k++){
4354 Int_t bin = GetBin(i, j, k);
4355 // here some bin contents are written into variables to make the error
4356 // calculation a little more legible:
4358 Double_t b = h2->RetrieveBinContent(bin);
4359 Double_t bot = bottom->RetrieveBinContent(bin);
4360
4361 // make sure there are some events, if not, then the errors are set = 0
4362 // automatically.
4363 //if(bot < 1){} was changed to the next line from recommendation of Jason Seely (28 Nov 2005)
4364 if(bot < 1e-6){}
4365 else{
4366 // computation of errors by Christos Leonidopoulos
4368 Double_t dbsq = h2->GetBinErrorSqUnchecked(bin);
4369 Double_t error = 2*TMath::Sqrt(a*a*c2*c2*dbsq + c2*c2*b*b*dasq+a*a*b*b*dc2*dc2)/(bot*bot);
4370 asym->SetBinError(i,j,k,error);
4371 }
4372 }
4373 }
4374 }
4375 delete top;
4376 delete bottom;
4377
4378 return asym;
4379}
4380
4381////////////////////////////////////////////////////////////////////////////////
4382/// Static function
4383/// return the default buffer size for automatic histograms
4384/// the parameter fgBufferSize may be changed via SetDefaultBufferSize
4385
4387{
4388 return fgBufferSize;
4389}
4390
4391////////////////////////////////////////////////////////////////////////////////
4392/// Return kTRUE if TH1::Sumw2 must be called when creating new histograms.
4393/// see TH1::SetDefaultSumw2.
4394
4396{
4397 return fgDefaultSumw2;
4398}
4399
4400////////////////////////////////////////////////////////////////////////////////
4401/// Return the current number of entries.
4402
4404{
4405 if (fBuffer) {
4406 Int_t nentries = (Int_t) fBuffer[0];
4407 if (nentries > 0) return nentries;
4408 }
4409
4410 return fEntries;
4411}
4412
4413////////////////////////////////////////////////////////////////////////////////
4414/// Number of effective entries of the histogram.
4415///
4416/// \f[
4417/// neff = \frac{(\sum Weights )^2}{(\sum Weight^2 )}
4418/// \f]
4419///
4420/// In case of an unweighted histogram this number is equivalent to the
4421/// number of entries of the histogram.
4422/// For a weighted histogram, this number corresponds to the hypothetical number of unweighted entries
4423/// a histogram would need to have the same statistical power as this weighted histogram.
4424/// Note: The underflow/overflow are included if one has set the TH1::StatOverFlows flag
4425/// and if the statistics has been computed at filling time.
4426/// If a range is set in the histogram the number is computed from the given range.
4427
4429{
4430 Stat_t s[kNstat];
4431 this->GetStats(s);// s[1] sum of squares of weights, s[0] sum of weights
4432 return (s[1] ? s[0]*s[0]/s[1] : TMath::Abs(s[0]) );
4433}
4434
4435////////////////////////////////////////////////////////////////////////////////
4436/// Shortcut to set the three histogram colors with a single call.
4437///
4438/// By default: linecolor = markercolor = fillcolor = -1
4439/// If a color is < 0 this method does not change the corresponding color if positive or null it set the color.
4440///
4441/// For instance:
4442/// ~~~ {.cpp}
4443/// h->SetColors(kRed, kRed);
4444/// ~~~
4445/// will set the line color and the marker color to red.
4446
4448{
4449 if (linecolor >= 0)
4451 if (markercolor >= 0)
4453 if (fillcolor >= 0)
4455}
4456
4457
4458////////////////////////////////////////////////////////////////////////////////
4459/// Set highlight (enable/disable) mode for the histogram
4460/// by default highlight mode is disable
4461
4462void TH1::SetHighlight(Bool_t set)
4463{
4464 if (IsHighlight() == set)
4465 return;
4466 if (fDimension > 2) {
4467 Info("SetHighlight", "Supported only 1-D or 2-D histograms");
4468 return;
4469 }
4470
4471 SetBit(kIsHighlight, set);
4472
4473 if (fPainter)
4475}
4476
4477////////////////////////////////////////////////////////////////////////////////
4478/// Redefines TObject::GetObjectInfo.
4479/// Displays the histogram info (bin number, contents, integral up to bin
4480/// corresponding to cursor position px,py
4481
4482char *TH1::GetObjectInfo(Int_t px, Int_t py) const
4483{
4484 return ((TH1*)this)->GetPainter()->GetObjectInfo(px,py);
4485}
4486
4487////////////////////////////////////////////////////////////////////////////////
4488/// Return pointer to painter.
4489/// If painter does not exist, it is created
4490
4492{
4493 if (!fPainter) {
4494 TString opt = option;
4495 opt.ToLower();
4496 if (opt.Contains("gl") || gStyle->GetCanvasPreferGL()) {
4497 //try to create TGLHistPainter
4498 TPluginHandler *handler = gROOT->GetPluginManager()->FindHandler("TGLHistPainter");
4499
4500 if (handler && handler->LoadPlugin() != -1)
4501 fPainter = reinterpret_cast<TVirtualHistPainter *>(handler->ExecPlugin(1, this));
4502 }
4503 }
4504
4506
4507 return fPainter;
4508}
4509
4510////////////////////////////////////////////////////////////////////////////////
4511/// Compute Quantiles for this histogram.
4512/// A quantile x_p := Q(p) is defined as the value x_p such that the cumulative
4513/// probability distribution Function F of variable X yields:
4514///
4515/// ~~~ {.cpp}
4516/// F(x_p) = Pr(X <= x_p) = p with 0 <= p <= 1.
4517/// x_p = Q(p) = F_inv(p)
4518/// ~~~
4519///
4520/// For instance the median x_0.5 of a distribution is defined as that value
4521/// of the random variable X for which the distribution function equals 0.5:
4522///
4523/// ~~~ {.cpp}
4524/// F(x_0.5) = Probability(X < x_0.5) = 0.5
4525/// x_0.5 = Q(0.5)
4526/// ~~~
4527///
4528/// \author Eddy Offermann
4529/// code from Eddy Offermann, Renaissance
4530///
4531/// \param[in] n maximum size of the arrays xp and p (if given)
4532/// \param[out] xp array to be filled with nq quantiles evaluated at (p). Memory has to be preallocated by caller.
4533/// - If `p == nullptr`, the quantiles are computed at the (first `n`) probabilities p given by the CDF of the histogram;
4534/// `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`.
4535/// If all bins have non-zero entries, the quantiles happen to be the bin centres.
4536/// Empty bins will, however, be skipped in the quantiles.
4537/// If the CDF is e.g. [0., 0., 0.1, ...], the quantiles would be, [3., 3., 3., ...], with the third bin starting
4538/// at 3.
4539/// \param[in] p array of cumulative probabilities where quantiles should be evaluated.
4540/// - if `p == nullptr`, the CDF of the histogram will be used to compute the quantiles, and will
4541/// have a size of n.
4542/// - Otherwise, it is assumed to contain at least n values.
4543/// \return number of quantiles computed
4544/// \note Unlike in TF1::GetQuantiles, `p` is here an optional argument
4545///
4546/// Note that the Integral of the histogram is automatically recomputed
4547/// if the number of entries is different of the number of entries when
4548/// the integral was computed last time. In case you do not use the Fill
4549/// functions to fill your histogram, but SetBinContent, you must call
4550/// TH1::ComputeIntegral before calling this function.
4551///
4552/// Getting quantiles xp from two histograms and storing results in a TGraph,
4553/// a so-called QQ-plot
4554///
4555/// ~~~ {.cpp}
4556/// TGraph *gr = new TGraph(nprob);
4557/// h1->GetQuantiles(nprob,gr->GetX());
4558/// h2->GetQuantiles(nprob,gr->GetY());
4559/// gr->Draw("alp");
4560/// ~~~
4561///
4562/// Example:
4563///
4564/// ~~~ {.cpp}
4565/// void quantiles() {
4566/// // demo for quantiles
4567/// const Int_t nq = 20;
4568/// TH1F *h = new TH1F("h","demo quantiles",100,-3,3);
4569/// h->FillRandom("gaus",5000);
4570/// h->GetXaxis()->SetTitle("x");
4571/// h->GetYaxis()->SetTitle("Counts");
4572///
4573/// Double_t p[nq]; // probabilities where to evaluate the quantiles in [0,1]
4574/// Double_t xp[nq]; // array of positions X to store the resulting quantiles
4575/// for (Int_t i=0;i<nq;i++) p[i] = Float_t(i+1)/nq;
4576/// h->GetQuantiles(nq,xp,p);
4577///
4578/// //show the original histogram in the top pad
4579/// TCanvas *c1 = new TCanvas("c1","demo quantiles",10,10,700,900);
4580/// c1->Divide(1,2);
4581/// c1->cd(1);
4582/// h->Draw();
4583///
4584/// // show the quantiles in the bottom pad
4585/// c1->cd(2);
4586/// gPad->SetGrid();
4587/// TGraph *gr = new TGraph(nq,p,xp);
4588/// gr->SetMarkerStyle(21);
4589/// gr->GetXaxis()->SetTitle("p");
4590/// gr->GetYaxis()->SetTitle("x");
4591/// gr->Draw("alp");
4592/// }
4593/// ~~~
4594
4596{
4597 if (GetDimension() > 1) {
4598 Error("GetQuantiles","Only available for 1-d histograms");
4599 return 0;
4600 }
4601
4602 const Int_t nbins = GetXaxis()->GetNbins();
4603 if (!fIntegral) ComputeIntegral();
4604 if (fIntegral[nbins+1] != fEntries) ComputeIntegral();
4605
4606 Int_t i, ibin;
4607 Int_t nq = n;
4608 std::unique_ptr<Double_t[]> localProb;
4609 if (p == nullptr) {
4610 nq = nbins+1;
4611 localProb.reset(new Double_t[nq]);
4612 localProb[0] = 0;
4613 for (i=1;i<nq;i++) {
4614 localProb[i] = fIntegral[i] / fIntegral[nbins];
4615 }
4616 }
4617 Double_t const *const prob = p ? p : localProb.get();
4618
4619 for (i = 0; i < nq; i++) {
4621 if (fIntegral[ibin] == prob[i]) {
4622 if (prob[i] == 0.) {
4623 for (; ibin+1 <= nbins && fIntegral[ibin+1] == 0.; ++ibin) {
4624
4625 }
4626 xp[i] = fXaxis.GetBinUpEdge(ibin);
4627 }
4628 else if (prob[i] == 1.) {
4629 xp[i] = fXaxis.GetBinUpEdge(ibin);
4630 }
4631 else {
4632 // Find equal integral in later bins (ie their entries are zero)
4633 Double_t width = 0;
4634 for (Int_t j = ibin+1; j <= nbins; ++j) {
4635 if (prob[i] == fIntegral[j]) {
4637 }
4638 else
4639 break;
4640 }
4642 }
4643 }
4644 else {
4645 xp[i] = GetBinLowEdge(ibin+1);
4647 if (dint > 0) xp[i] += GetBinWidth(ibin+1)*(prob[i]-fIntegral[ibin])/dint;
4648 }
4649 }
4650
4651 return nq;
4652}
4653
4654////////////////////////////////////////////////////////////////////////////////
4660 return 1;
4661}
4662
4663////////////////////////////////////////////////////////////////////////////////
4664/// Compute Initial values of parameters for a gaussian.
4665
4666void H1InitGaus()
4667{
4668 Double_t allcha, sumx, sumx2, x, val, stddev, mean;
4669 Int_t bin;
4670 const Double_t sqrtpi = 2.506628;
4671
4672 // - Compute mean value and StdDev of the histogram in the given range
4674 TH1 *curHist = (TH1*)hFitter->GetObjectFit();
4675 Int_t hxfirst = hFitter->GetXfirst();
4676 Int_t hxlast = hFitter->GetXlast();
4677 Double_t valmax = curHist->GetBinContent(hxfirst);
4678 Double_t binwidx = curHist->GetBinWidth(hxfirst);
4679 allcha = sumx = sumx2 = 0;
4680 for (bin=hxfirst;bin<=hxlast;bin++) {
4681 x = curHist->GetBinCenter(bin);
4682 val = TMath::Abs(curHist->GetBinContent(bin));
4683 if (val > valmax) valmax = val;
4684 sumx += val*x;
4685 sumx2 += val*x*x;
4686 allcha += val;
4687 }
4688 if (allcha == 0) return;
4689 mean = sumx/allcha;
4690 stddev = sumx2/allcha - mean*mean;
4691 if (stddev > 0) stddev = TMath::Sqrt(stddev);
4692 else stddev = 0;
4693 if (stddev == 0) stddev = binwidx*(hxlast-hxfirst+1)/4;
4694 //if the distribution is really gaussian, the best approximation
4695 //is binwidx*allcha/(sqrtpi*stddev)
4696 //However, in case of non-gaussian tails, this underestimates
4697 //the normalisation constant. In this case the maximum value
4698 //is a better approximation.
4699 //We take the average of both quantities
4701
4702 //In case the mean value is outside the histo limits and
4703 //the StdDev is bigger than the range, we take
4704 // mean = center of bins
4705 // stddev = half range
4706 Double_t xmin = curHist->GetXaxis()->GetXmin();
4707 Double_t xmax = curHist->GetXaxis()->GetXmax();
4708 if ((mean < xmin || mean > xmax) && stddev > (xmax-xmin)) {
4709 mean = 0.5*(xmax+xmin);
4710 stddev = 0.5*(xmax-xmin);
4711 }
4712 TF1 *f1 = (TF1*)hFitter->GetUserFunc();
4714 f1->SetParameter(1,mean);
4716 f1->SetParLimits(2,0,10*stddev);
4717}
4718
4719////////////////////////////////////////////////////////////////////////////////
4720/// Compute Initial values of parameters for an exponential.
4721
4722void H1InitExpo()
4723{
4725 Int_t ifail;
4727 Int_t hxfirst = hFitter->GetXfirst();
4728 Int_t hxlast = hFitter->GetXlast();
4729 Int_t nchanx = hxlast - hxfirst + 1;
4730
4732
4733 TF1 *f1 = (TF1*)hFitter->GetUserFunc();
4735 f1->SetParameter(1,slope);
4736
4737}
4738
4739////////////////////////////////////////////////////////////////////////////////
4740/// Compute Initial values of parameters for a polynom.
4741
4742void H1InitPolynom()
4743{
4744 Double_t fitpar[25];
4745
4747 TF1 *f1 = (TF1*)hFitter->GetUserFunc();
4748 Int_t hxfirst = hFitter->GetXfirst();
4749 Int_t hxlast = hFitter->GetXlast();
4750 Int_t nchanx = hxlast - hxfirst + 1;
4751 Int_t npar = f1->GetNpar();
4752
4753 if (nchanx <=1 || npar == 1) {
4754 TH1 *curHist = (TH1*)hFitter->GetObjectFit();
4755 fitpar[0] = curHist->GetSumOfWeights()/Double_t(nchanx);
4756 } else {
4758 }
4759 for (Int_t i=0;i<npar;i++) f1->SetParameter(i, fitpar[i]);
4760}
4761
4762////////////////////////////////////////////////////////////////////////////////
4763/// Least squares lpolynomial fitting without weights.
4764///
4765/// \param[in] n number of points to fit
4766/// \param[in] m number of parameters
4767/// \param[in] a array of parameters
4768///
4769/// based on CERNLIB routine LSQ: Translated to C++ by Rene Brun
4770/// (E.Keil. revised by B.Schorr, 23.10.1981.)
4771
4773{
4774 const Double_t zero = 0.;
4775 const Double_t one = 1.;
4776 const Int_t idim = 20;
4777
4778 Double_t b[400] /* was [20][20] */;
4779 Int_t i, k, l, ifail;
4781 Double_t da[20], xk, yk;
4782
4783 if (m <= 2) {
4784 H1LeastSquareLinearFit(n, a[0], a[1], ifail);
4785 return;
4786 }
4787 if (m > idim || m > n) return;
4788 b[0] = Double_t(n);
4789 da[0] = zero;
4790 for (l = 2; l <= m; ++l) {
4791 b[l-1] = zero;
4792 b[m + l*20 - 21] = zero;
4793 da[l-1] = zero;
4794 }
4796 TH1 *curHist = (TH1*)hFitter->GetObjectFit();
4797 Int_t hxfirst = hFitter->GetXfirst();
4798 Int_t hxlast = hFitter->GetXlast();
4799 for (k = hxfirst; k <= hxlast; ++k) {
4800 xk = curHist->GetBinCenter(k);
4801 yk = curHist->GetBinContent(k);
4802 power = one;
4803 da[0] += yk;
4804 for (l = 2; l <= m; ++l) {
4805 power *= xk;
4806 b[l-1] += power;
4807 da[l-1] += power*yk;
4808 }
4809 for (l = 2; l <= m; ++l) {
4810 power *= xk;
4811 b[m + l*20 - 21] += power;
4812 }
4813 }
4814 for (i = 3; i <= m; ++i) {
4815 for (k = i; k <= m; ++k) {
4816 b[k - 1 + (i-1)*20 - 21] = b[k + (i-2)*20 - 21];
4817 }
4818 }
4820
4821 for (i=0; i<m; ++i) a[i] = da[i];
4822
4823}
4824
4825////////////////////////////////////////////////////////////////////////////////
4826/// Least square linear fit without weights.
4827///
4828/// extracted from CERNLIB LLSQ: Translated to C++ by Rene Brun
4829/// (added to LSQ by B. Schorr, 15.02.1982.)
4830
4832{
4834 Int_t i, n;
4836 Double_t fn, xk, yk;
4837 Double_t det;
4838
4839 n = TMath::Abs(ndata);
4840 ifail = -2;
4841 xbar = ybar = x2bar = xybar = 0;
4843 TH1 *curHist = (TH1*)hFitter->GetObjectFit();
4844 Int_t hxfirst = hFitter->GetXfirst();
4845 Int_t hxlast = hFitter->GetXlast();
4846 for (i = hxfirst; i <= hxlast; ++i) {
4847 xk = curHist->GetBinCenter(i);
4848 yk = curHist->GetBinContent(i);
4849 if (ndata < 0) {
4850 if (yk <= 0) yk = 1e-9;
4851 yk = TMath::Log(yk);
4852 }
4853 xbar += xk;
4854 ybar += yk;
4855 x2bar += xk*xk;
4856 xybar += xk*yk;
4857 }
4858 fn = Double_t(n);
4859 det = fn*x2bar - xbar*xbar;
4860 ifail = -1;
4861 if (det <= 0) {
4862 a0 = ybar/fn;
4863 a1 = 0;
4864 return;
4865 }
4866 ifail = 0;
4867 a0 = (x2bar*ybar - xbar*xybar) / det;
4868 a1 = (fn*xybar - xbar*ybar) / det;
4869
4870}
4871
4872////////////////////////////////////////////////////////////////////////////////
4873/// Extracted from CERN Program library routine DSEQN.
4874///
4875/// Translated to C++ by Rene Brun
4876
4878{
4880 Int_t nmjp1, i, j, l;
4881 Int_t im1, jp1, nm1, nmi;
4882 Double_t s1, s21, s22;
4883 const Double_t one = 1.;
4884
4885 /* Parameter adjustments */
4886 b_dim1 = idim;
4887 b_offset = b_dim1 + 1;
4888 b -= b_offset;
4889 a_dim1 = idim;
4890 a_offset = a_dim1 + 1;
4891 a -= a_offset;
4892
4893 if (idim < n) return;
4894
4895 ifail = 0;
4896 for (j = 1; j <= n; ++j) {
4897 if (a[j + j*a_dim1] <= 0) { ifail = -1; return; }
4898 a[j + j*a_dim1] = one / a[j + j*a_dim1];
4899 if (j == n) continue;
4900 jp1 = j + 1;
4901 for (l = jp1; l <= n; ++l) {
4902 a[j + l*a_dim1] = a[j + j*a_dim1] * a[l + j*a_dim1];
4903 s1 = -a[l + (j+1)*a_dim1];
4904 for (i = 1; i <= j; ++i) { s1 = a[l + i*a_dim1] * a[i + (j+1)*a_dim1] + s1; }
4905 a[l + (j+1)*a_dim1] = -s1;
4906 }
4907 }
4908 if (k <= 0) return;
4909
4910 for (l = 1; l <= k; ++l) {
4911 b[l*b_dim1 + 1] = a[a_dim1 + 1]*b[l*b_dim1 + 1];
4912 }
4913 if (n == 1) return;
4914 for (l = 1; l <= k; ++l) {
4915 for (i = 2; i <= n; ++i) {
4916 im1 = i - 1;
4917 s21 = -b[i + l*b_dim1];
4918 for (j = 1; j <= im1; ++j) {
4919 s21 = a[i + j*a_dim1]*b[j + l*b_dim1] + s21;
4920 }
4921 b[i + l*b_dim1] = -a[i + i*a_dim1]*s21;
4922 }
4923 nm1 = n - 1;
4924 for (i = 1; i <= nm1; ++i) {
4925 nmi = n - i;
4926 s22 = -b[nmi + l*b_dim1];
4927 for (j = 1; j <= i; ++j) {
4928 nmjp1 = n - j + 1;
4929 s22 = a[nmi + nmjp1*a_dim1]*b[nmjp1 + l*b_dim1] + s22;
4930 }
4931 b[nmi + l*b_dim1] = -s22;
4932 }
4933 }
4934}
4935
4936////////////////////////////////////////////////////////////////////////////////
4937/// Return Global bin number corresponding to binx,y,z.
4938///
4939/// 2-D and 3-D histograms are represented with a one dimensional
4940/// structure.
4941/// This has the advantage that all existing functions, such as
4942/// GetBinContent, GetBinError, GetBinFunction work for all dimensions.
4943///
4944/// In case of a TH1x, returns binx directly.
4945/// see TH1::GetBinXYZ for the inverse transformation.
4946///
4947/// Convention for numbering bins
4948///
4949/// For all histogram types: nbins, xlow, xup
4950///
4951/// - bin = 0; underflow bin
4952/// - bin = 1; first bin with low-edge xlow INCLUDED
4953/// - bin = nbins; last bin with upper-edge xup EXCLUDED
4954/// - bin = nbins+1; overflow bin
4955///
4956/// In case of 2-D or 3-D histograms, a "global bin" number is defined.
4957/// For example, assuming a 3-D histogram with binx,biny,binz, the function
4958///
4959/// ~~~ {.cpp}
4960/// Int_t bin = h->GetBin(binx,biny,binz);
4961/// ~~~
4962///
4963/// returns a global/linearized bin number. This global bin is useful
4964/// to access the bin information independently of the dimension.
4965
4967{
4968 Int_t ofx = fXaxis.GetNbins() + 1; // overflow bin
4969 if (binx < 0) binx = 0;
4970 if (binx > ofx) binx = ofx;
4971
4972 return binx;
4973}
4974
4975////////////////////////////////////////////////////////////////////////////////
4976/// Return binx, biny, binz corresponding to the global bin number globalbin
4977/// see TH1::GetBin function above
4978
4980{
4981 Int_t nx = fXaxis.GetNbins()+2;
4982 Int_t ny = fYaxis.GetNbins()+2;
4983
4984 if (GetDimension() == 1) {
4985 binx = binglobal%nx;
4986 biny = 0;
4987 binz = 0;
4988 return;
4989 }
4990 if (GetDimension() == 2) {
4991 binx = binglobal%nx;
4992 biny = ((binglobal-binx)/nx)%ny;
4993 binz = 0;
4994 return;
4995 }
4996 if (GetDimension() == 3) {
4997 binx = binglobal%nx;
4998 biny = ((binglobal-binx)/nx)%ny;
4999 binz = ((binglobal-binx)/nx -biny)/ny;
5000 }
5001}
5002
5003////////////////////////////////////////////////////////////////////////////////
5004/// Return a random number distributed according the histogram bin contents.
5005/// This function checks if the bins integral exists. If not, the integral
5006/// is evaluated, normalized to one.
5007///
5008/// @param rng (optional) Random number generator pointer used (default is gRandom)
5009///
5010/// The integral is automatically recomputed if the number of entries
5011/// is not the same then when the integral was computed.
5012/// NB Only valid for 1-d histograms. Use GetRandom2 or 3 otherwise.
5013/// If the histogram has a bin with negative content a NaN is returned
5014
5016{
5017 if (fDimension > 1) {
5018 Error("GetRandom","Function only valid for 1-d histograms");
5019 return 0;
5020 }
5022 Double_t integral = 0;
5023 // compute integral checking that all bins have positive content (see ROOT-5894)
5024 if (fIntegral) {
5025 if (fIntegral[nbinsx+1] != fEntries) integral = ((TH1*)this)->ComputeIntegral(true);
5026 else integral = fIntegral[nbinsx];
5027 } else {
5028 integral = ((TH1*)this)->ComputeIntegral(true);
5029 }
5030 if (integral == 0) return 0;
5031 // return a NaN in case some bins have negative content
5032 if (integral == TMath::QuietNaN() ) return TMath::QuietNaN();
5033
5034 Double_t r1 = (rng) ? rng->Rndm() : gRandom->Rndm();
5037 if (r1 > fIntegral[ibin]) x +=
5039 return x;
5040}
5041
5042////////////////////////////////////////////////////////////////////////////////
5043/// Return content of bin number bin.
5044///
5045/// Implemented in TH1C,S,F,D
5046///
5047/// Convention for numbering bins
5048///
5049/// For all histogram types: nbins, xlow, xup
5050///
5051/// - bin = 0; underflow bin
5052/// - bin = 1; first bin with low-edge xlow INCLUDED
5053/// - bin = nbins; last bin with upper-edge xup EXCLUDED
5054/// - bin = nbins+1; overflow bin
5055///
5056/// In case of 2-D or 3-D histograms, a "global bin" number is defined.
5057/// For example, assuming a 3-D histogram with binx,biny,binz, the function
5058///
5059/// ~~~ {.cpp}
5060/// Int_t bin = h->GetBin(binx,biny,binz);
5061/// ~~~
5062///
5063/// returns a global/linearized bin number. This global bin is useful
5064/// to access the bin information independently of the dimension.
5065
5067{
5068 if (fBuffer) const_cast<TH1*>(this)->BufferEmpty();
5069 if (bin < 0) bin = 0;
5070 if (bin >= fNcells) bin = fNcells-1;
5071
5072 return RetrieveBinContent(bin);
5073}
5074
5075////////////////////////////////////////////////////////////////////////////////
5076/// Compute first binx in the range [firstx,lastx] for which
5077/// diff = abs(bin_content-c) <= maxdiff
5078///
5079/// In case several bins in the specified range with diff=0 are found
5080/// the first bin found is returned in binx.
5081/// In case several bins in the specified range satisfy diff <=maxdiff
5082/// the bin with the smallest difference is returned in binx.
5083/// In all cases the function returns the smallest difference.
5084///
5085/// NOTE1: if firstx <= 0, firstx is set to bin 1
5086/// if (lastx < firstx then firstx is set to the number of bins
5087/// ie if firstx=0 and lastx=0 (default) the search is on all bins.
5088///
5089/// NOTE2: if maxdiff=0 (default), the first bin with content=c is returned.
5090
5092{
5093 if (fDimension > 1) {
5094 binx = 0;
5095 Error("GetBinWithContent","function is only valid for 1-D histograms");
5096 return 0;
5097 }
5098
5099 if (fBuffer) ((TH1*)this)->BufferEmpty();
5100
5101 if (firstx <= 0) firstx = 1;
5102 if (lastx < firstx) lastx = fXaxis.GetNbins();
5103 Int_t binminx = 0;
5104 Double_t diff, curmax = 1.e240;
5105 for (Int_t i=firstx;i<=lastx;i++) {
5107 if (diff <= 0) {binx = i; return diff;}
5108 if (diff < curmax && diff <= maxdiff) {curmax = diff, binminx=i;}
5109 }
5110 binx = binminx;
5111 return curmax;
5112}
5113
5114////////////////////////////////////////////////////////////////////////////////
5115/// Given a point x, approximates the value via linear interpolation
5116/// based on the two nearest bin centers
5117///
5118/// Andy Mastbaum 10/21/08
5119
5121{
5122 if (fBuffer) ((TH1*)this)->BufferEmpty();
5123
5125 Double_t x0,x1,y0,y1;
5126
5127 if(x<=GetBinCenter(1)) {
5128 return RetrieveBinContent(1);
5129 } else if(x>=GetBinCenter(GetNbinsX())) {
5130 return RetrieveBinContent(GetNbinsX());
5131 } else {
5132 if(x<=GetBinCenter(xbin)) {
5134 x0 = GetBinCenter(xbin-1);
5136 x1 = GetBinCenter(xbin);
5137 } else {
5139 x0 = GetBinCenter(xbin);
5141 x1 = GetBinCenter(xbin+1);
5142 }
5143 return y0 + (x-x0)*((y1-y0)/(x1-x0));
5144 }
5145}
5146
5147////////////////////////////////////////////////////////////////////////////////
5148/// 2d Interpolation. Not yet implemented.
5149
5151{
5152 Error("Interpolate","This function must be called with 1 argument for a TH1");
5153 return 0;
5154}
5155
5156////////////////////////////////////////////////////////////////////////////////
5157/// 3d Interpolation. Not yet implemented.
5158
5160{
5161 Error("Interpolate","This function must be called with 1 argument for a TH1");
5162 return 0;
5163}
5164
5165///////////////////////////////////////////////////////////////////////////////
5166/// Check if a histogram is empty
5167/// (this is a protected method used mainly by TH1Merger )
5168
5169Bool_t TH1::IsEmpty() const
5170{
5171 // if fTsumw or fentries are not zero histogram is not empty
5172 // need to use GetEntries() instead of fEntries in case of bugger histograms
5173 // so we will flash the buffer
5174 if (fTsumw != 0) return kFALSE;
5175 if (GetEntries() != 0) return kFALSE;
5176 // case fTSumw == 0 amd entries are also zero
5177 // this should not really happening, but if one sets content by hand
5178 // it can happen. a call to ResetStats() should be done in such cases
5179 double sumw = 0;
5180 for (int i = 0; i< GetNcells(); ++i) sumw += RetrieveBinContent(i);
5181 return (sumw != 0) ? kFALSE : kTRUE;
5182}
5183
5184////////////////////////////////////////////////////////////////////////////////
5185/// Return true if the bin is overflow.
5186
5188{
5189 Int_t binx, biny, binz;
5190 GetBinXYZ(bin, binx, biny, binz);
5191
5192 if (iaxis == 0) {
5193 if ( fDimension == 1 )
5194 return binx >= GetNbinsX() + 1;
5195 if ( fDimension == 2 )
5196 return (binx >= GetNbinsX() + 1) ||
5197 (biny >= GetNbinsY() + 1);
5198 if ( fDimension == 3 )
5199 return (binx >= GetNbinsX() + 1) ||
5200 (biny >= GetNbinsY() + 1) ||
5201 (binz >= GetNbinsZ() + 1);
5202 return kFALSE;
5203 }
5204 if (iaxis == 1)
5205 return binx >= GetNbinsX() + 1;
5206 if (iaxis == 2)
5207 return biny >= GetNbinsY() + 1;
5208 if (iaxis == 3)
5209 return binz >= GetNbinsZ() + 1;
5210
5211 Error("IsBinOverflow","Invalid axis value");
5212 return kFALSE;
5213}
5214
5215////////////////////////////////////////////////////////////////////////////////
5216/// Return true if the bin is underflow.
5217/// If iaxis = 0 make OR with all axes otherwise check only for the given axis
5218
5220{
5221 Int_t binx, biny, binz;
5222 GetBinXYZ(bin, binx, biny, binz);
5223
5224 if (iaxis == 0) {
5225 if ( fDimension == 1 )
5226 return (binx <= 0);
5227 else if ( fDimension == 2 )
5228 return (binx <= 0 || biny <= 0);
5229 else if ( fDimension == 3 )
5230 return (binx <= 0 || biny <= 0 || binz <= 0);
5231 else
5232 return kFALSE;
5233 }
5234 if (iaxis == 1)
5235 return (binx <= 0);
5236 if (iaxis == 2)
5237 return (biny <= 0);
5238 if (iaxis == 3)
5239 return (binz <= 0);
5240
5241 Error("IsBinUnderflow","Invalid axis value");
5242 return kFALSE;
5243}
5244
5245////////////////////////////////////////////////////////////////////////////////
5246/// Reduce the number of bins for the axis passed in the option to the number of bins having a label.
5247/// The method will remove only the extra bins existing after the last "labeled" bin.
5248/// Note that if there are "un-labeled" bins present between "labeled" bins they will not be removed
5249
5251{
5253 TAxis *axis = nullptr;
5254 if (iaxis == 1) axis = GetXaxis();
5255 if (iaxis == 2) axis = GetYaxis();
5256 if (iaxis == 3) axis = GetZaxis();
5257 if (!axis) {
5258 Error("LabelsDeflate","Invalid axis option %s",ax);
5259 return;
5260 }
5261 if (!axis->GetLabels()) return;
5262
5263 // find bin with last labels
5264 // bin number is object ID in list of labels
5265 // therefore max bin number is number of bins of the deflated histograms
5266 TIter next(axis->GetLabels());
5267 TObject *obj;
5268 Int_t nbins = 0;
5269 while ((obj = next())) {
5270 Int_t ibin = obj->GetUniqueID();
5271 if (ibin > nbins) nbins = ibin;
5272 }
5273 if (nbins < 1) nbins = 1;
5274
5275 // Do nothing in case it was the last bin
5276 if (nbins==axis->GetNbins()) return;
5277
5278 TH1 *hold = (TH1*)IsA()->New();
5279 R__ASSERT(hold);
5280 hold->SetDirectory(nullptr);
5281 Copy(*hold);
5282
5283 Bool_t timedisp = axis->GetTimeDisplay();
5284 Double_t xmin = axis->GetXmin();
5285 Double_t xmax = axis->GetBinUpEdge(nbins);
5286 if (xmax <= xmin) xmax = xmin +nbins;
5287 axis->SetRange(0,0);
5288 axis->Set(nbins,xmin,xmax);
5289 SetBinsLength(-1); // reset the number of cells
5291 if (errors) fSumw2.Set(fNcells);
5292 axis->SetTimeDisplay(timedisp);
5293 // reset histogram content
5294 Reset("ICE");
5295
5296 //now loop on all bins and refill
5297 // NOTE that if the bins without labels have content
5298 // it will be put in the underflow/overflow.
5299 // For this reason we use AddBinContent method
5301 Int_t bin,binx,biny,binz;
5302 for (bin=0; bin < hold->fNcells; ++bin) {
5303 hold->GetBinXYZ(bin,binx,biny,binz);
5305 Double_t cu = hold->RetrieveBinContent(bin);
5307 if (errors) {
5308 fSumw2.fArray[ibin] += hold->fSumw2.fArray[bin];
5309 }
5310 }
5312 delete hold;
5313}
5314
5315////////////////////////////////////////////////////////////////////////////////
5316/// Double the number of bins for axis.
5317/// Refill histogram.
5318/// This function is called by TAxis::FindBin(const char *label)
5319
5321{
5323 TAxis *axis = nullptr;
5324 if (iaxis == 1) axis = GetXaxis();
5325 if (iaxis == 2) axis = GetYaxis();
5326 if (iaxis == 3) axis = GetZaxis();
5327 if (!axis) return;
5328
5329 TH1 *hold = (TH1*)IsA()->New();
5330 hold->SetDirectory(nullptr);
5331 Copy(*hold);
5332 hold->ResetBit(kMustCleanup);
5333
5334 Bool_t timedisp = axis->GetTimeDisplay();
5335 Int_t nbins = axis->GetNbins();
5336 Double_t xmin = axis->GetXmin();
5337 Double_t xmax = axis->GetXmax();
5338 xmax = xmin + 2*(xmax-xmin);
5339 axis->SetRange(0,0);
5340 // double the bins and recompute ncells
5341 axis->Set(2*nbins,xmin,xmax);
5342 SetBinsLength(-1);
5344 if (errors) fSumw2.Set(fNcells);
5345 axis->SetTimeDisplay(timedisp);
5346
5347 Reset("ICE"); // reset content and error
5348
5349 //now loop on all bins and refill
5351 Int_t bin,ibin,binx,biny,binz;
5352 for (ibin =0; ibin < hold->fNcells; ibin++) {
5353 // get the binx,y,z values . The x-y-z (axis) bin values will stay the same between new-old after the expanding
5354 hold->GetBinXYZ(ibin,binx,biny,binz);
5355 bin = GetBin(binx,biny,binz);
5356
5357 // underflow and overflow will be cleaned up because their meaning has been altered
5358 if (hold->IsBinUnderflow(ibin,iaxis) || hold->IsBinOverflow(ibin,iaxis)) {
5359 continue;
5360 }
5361 else {
5362 AddBinContent(bin, hold->RetrieveBinContent(ibin));
5363 if (errors) fSumw2.fArray[bin] += hold->fSumw2.fArray[ibin];
5364 }
5365 }
5367 delete hold;
5368}
5369
5370////////////////////////////////////////////////////////////////////////////////
5371/// Sort bins with labels or set option(s) to draw axis with labels
5372/// \param[in] option
5373/// - "a" sort by alphabetic order
5374/// - ">" sort by decreasing values
5375/// - "<" sort by increasing values
5376/// - "h" draw labels horizontal
5377/// - "v" draw labels vertical
5378/// - "u" draw labels up (end of label right adjusted)
5379/// - "d" draw labels down (start of label left adjusted)
5380///
5381/// In case not all bins have labels sorting will work only in the case
5382/// the first `n` consecutive bins have all labels and sorting will be performed on
5383/// those label bins.
5384///
5385/// \param[in] ax axis
5386
5388{
5390 TAxis *axis = nullptr;
5391 if (iaxis == 1)
5392 axis = GetXaxis();
5393 if (iaxis == 2)
5394 axis = GetYaxis();
5395 if (iaxis == 3)
5396 axis = GetZaxis();
5397 if (!axis)
5398 return;
5399 THashList *labels = axis->GetLabels();
5400 if (!labels) {
5401 Warning("LabelsOption", "Axis %s has no labels!",axis->GetName());
5402 return;
5403 }
5404 TString opt = option;
5405 opt.ToLower();
5406 Int_t iopt = -1;
5407 if (opt.Contains("h")) {
5412 iopt = 0;
5413 }
5414 if (opt.Contains("v")) {
5419 iopt = 1;
5420 }
5421 if (opt.Contains("u")) {
5422 axis->SetBit(TAxis::kLabelsUp);
5426 iopt = 2;
5427 }
5428 if (opt.Contains("d")) {
5433 iopt = 3;
5434 }
5435 Int_t sort = -1;
5436 if (opt.Contains("a"))
5437 sort = 0;
5438 if (opt.Contains(">"))
5439 sort = 1;
5440 if (opt.Contains("<"))
5441 sort = 2;
5442 if (sort < 0) {
5443 if (iopt < 0)
5444 Error("LabelsOption", "%s is an invalid label placement option!",opt.Data());
5445 return;
5446 }
5447
5448 // Code works only if first n bins have labels if we uncomment following line
5449 // but we don't want to support this special case
5450 // Int_t n = TMath::Min(axis->GetNbins(), labels->GetSize());
5451
5452 // support only cases where each bin has a labels (should be when axis is alphanumeric)
5453 Int_t n = labels->GetSize();
5454 if (n != axis->GetNbins()) {
5455 // check if labels are all consecutive and starts from the first bin
5456 // in that case the current code will work fine
5457 Int_t firstLabelBin = axis->GetNbins()+1;
5458 Int_t lastLabelBin = -1;
5459 for (Int_t i = 0; i < n; ++i) {
5460 Int_t bin = labels->At(i)->GetUniqueID();
5461 if (bin < firstLabelBin) firstLabelBin = bin;
5462 if (bin > lastLabelBin) lastLabelBin = bin;
5463 }
5464 if (firstLabelBin != 1 || lastLabelBin-firstLabelBin +1 != n) {
5465 Error("LabelsOption", "%s of Histogram %s contains bins without labels. Sorting will not work correctly - return",
5466 axis->GetName(), GetName());
5467 return;
5468 }
5469 // case where label bins are consecutive starting from first bin will work
5470 // calling before a TH1::LabelsDeflate() will avoid this error message
5471 Warning("LabelsOption", "axis %s of Histogram %s has extra following bins without labels. Sorting will work only for first label bins",
5472 axis->GetName(), GetName());
5473 }
5474 std::vector<Int_t> a(n);
5475 std::vector<Int_t> b(n);
5476
5477
5478 Int_t i, j, k;
5479 std::vector<Double_t> cont;
5480 std::vector<Double_t> errors2;
5481 THashList *labold = new THashList(labels->GetSize(), 1);
5482 TIter nextold(labels);
5483 TObject *obj = nullptr;
5484 labold->AddAll(labels);
5485 labels->Clear();
5486
5487 // delete buffer if it is there since bins will be reordered.
5488 if (fBuffer)
5489 BufferEmpty(1);
5490
5491 if (sort > 0) {
5492 //---sort by values of bins
5493 if (GetDimension() == 1) {
5494 cont.resize(n);
5495 if (fSumw2.fN)
5496 errors2.resize(n);
5497 for (i = 0; i < n; i++) {
5498 cont[i] = RetrieveBinContent(i + 1);
5499 if (!errors2.empty())
5500 errors2[i] = GetBinErrorSqUnchecked(i + 1);
5501 b[i] = labold->At(i)->GetUniqueID(); // this is the bin corresponding to the label
5502 a[i] = i;
5503 }
5504 if (sort == 1)
5505 TMath::Sort(n, cont.data(), a.data(), kTRUE); // sort by decreasing values
5506 else
5507 TMath::Sort(n, cont.data(), a.data(), kFALSE); // sort by increasing values
5508 for (i = 0; i < n; i++) {
5509 // use UpdateBinCOntent to not screw up histogram entries
5510 UpdateBinContent(i + 1, cont[b[a[i]] - 1]); // b[a[i]] returns bin number. .we need to subtract 1
5511 if (gDebug)
5512 Info("LabelsOption","setting bin %d value %f from bin %d label %s at pos %d ",
5513 i+1,cont[b[a[i]] - 1],b[a[i]],labold->At(a[i])->GetName(),a[i]);
5514 if (!errors2.empty())
5515 fSumw2.fArray[i + 1] = errors2[b[a[i]] - 1];
5516 }
5517 for (i = 0; i < n; i++) {
5518 obj = labold->At(a[i]);
5519 labels->Add(obj);
5520 obj->SetUniqueID(i + 1);
5521 }
5522 } else if (GetDimension() == 2) {
5523 std::vector<Double_t> pcont(n + 2);
5524 Int_t nx = fXaxis.GetNbins() + 2;
5525 Int_t ny = fYaxis.GetNbins() + 2;
5526 cont.resize((nx + 2) * (ny + 2));
5527 if (fSumw2.fN)
5528 errors2.resize((nx + 2) * (ny + 2));
5529 for (i = 0; i < nx; i++) {
5530 for (j = 0; j < ny; j++) {
5531 Int_t bin = GetBin(i,j);
5532 cont[i + nx * j] = RetrieveBinContent(bin);
5533 if (!errors2.empty())
5534 errors2[i + nx * j] = GetBinErrorSqUnchecked(bin);
5535 if (axis == GetXaxis())
5536 k = i - 1;
5537 else
5538 k = j - 1;
5539 if (k >= 0 && k < n) { // we consider underflow/overflows in y for ordering the bins
5540 pcont[k] += cont[i + nx * j];
5541 a[k] = k;
5542 }
5543 }
5544 }
5545 if (sort == 1)
5546 TMath::Sort(n, pcont.data(), a.data(), kTRUE); // sort by decreasing values
5547 else
5548 TMath::Sort(n, pcont.data(), a.data(), kFALSE); // sort by increasing values
5549 for (i = 0; i < n; i++) {
5550 // iterate on old label list to find corresponding bin match
5551 TIter next(labold);
5552 UInt_t bin = a[i] + 1;
5553 while ((obj = next())) {
5554 if (obj->GetUniqueID() == (UInt_t)bin)
5555 break;
5556 else
5557 obj = nullptr;
5558 }
5559 if (!obj) {
5560 // this should not really happen
5561 R__ASSERT("LabelsOption - No corresponding bin found when ordering labels");
5562 return;
5563 }
5564
5565 labels->Add(obj);
5566 if (gDebug)
5567 std::cout << " set label " << obj->GetName() << " to bin " << i + 1 << " from order " << a[i] << " bin "
5568 << b[a[i]] << "content " << pcont[a[i]] << std::endl;
5569 }
5570 // need to set here new ordered labels - otherwise loop before does not work since labold and labels list
5571 // contain same objects
5572 for (i = 0; i < n; i++) {
5573 labels->At(i)->SetUniqueID(i + 1);
5574 }
5575 // set now the bin contents
5576 if (axis == GetXaxis()) {
5577 for (i = 0; i < n; i++) {
5578 Int_t ix = a[i] + 1;
5579 for (j = 0; j < ny; j++) {
5580 Int_t bin = GetBin(i + 1, j);
5581 UpdateBinContent(bin, cont[ix + nx * j]);
5582 if (!errors2.empty())
5583 fSumw2.fArray[bin] = errors2[ix + nx * j];
5584 }
5585 }
5586 } else {
5587 // using y axis
5588 for (i = 0; i < nx; i++) {
5589 for (j = 0; j < n; j++) {
5590 Int_t iy = a[j] + 1;
5591 Int_t bin = GetBin(i, j + 1);
5592 UpdateBinContent(bin, cont[i + nx * iy]);
5593 if (!errors2.empty())
5594 fSumw2.fArray[bin] = errors2[i + nx * iy];
5595 }
5596 }
5597 }
5598 } else {
5599 // sorting histograms: 3D case
5600 std::vector<Double_t> pcont(n + 2);
5601 Int_t nx = fXaxis.GetNbins() + 2;
5602 Int_t ny = fYaxis.GetNbins() + 2;
5603 Int_t nz = fZaxis.GetNbins() + 2;
5604 Int_t l = 0;
5605 cont.resize((nx + 2) * (ny + 2) * (nz + 2));
5606 if (fSumw2.fN)
5607 errors2.resize((nx + 2) * (ny + 2) * (nz + 2));
5608 for (i = 0; i < nx; i++) {
5609 for (j = 0; j < ny; j++) {
5610 for (k = 0; k < nz; k++) {
5611 Int_t bin = GetBin(i,j,k);
5613 if (axis == GetXaxis())
5614 l = i - 1;
5615 else if (axis == GetYaxis())
5616 l = j - 1;
5617 else
5618 l = k - 1;
5619 if (l >= 0 && l < n) { // we consider underflow/overflows in y for ordering the bins
5620 pcont[l] += c;
5621 a[l] = l;
5622 }
5623 cont[i + nx * (j + ny * k)] = c;
5624 if (!errors2.empty())
5625 errors2[i + nx * (j + ny * k)] = GetBinErrorSqUnchecked(bin);
5626 }
5627 }
5628 }
5629 if (sort == 1)
5630 TMath::Sort(n, pcont.data(), a.data(), kTRUE); // sort by decreasing values
5631 else
5632 TMath::Sort(n, pcont.data(), a.data(), kFALSE); // sort by increasing values
5633 for (i = 0; i < n; i++) {
5634 // iterate on the old label list to find corresponding bin match
5635 TIter next(labold);
5636 UInt_t bin = a[i] + 1;
5637 obj = nullptr;
5638 while ((obj = next())) {
5639 if (obj->GetUniqueID() == (UInt_t)bin) {
5640 break;
5641 }
5642 else
5643 obj = nullptr;
5644 }
5645 if (!obj) {
5646 R__ASSERT("LabelsOption - No corresponding bin found when ordering labels");
5647 return;
5648 }
5649 labels->Add(obj);
5650 if (gDebug)
5651 std::cout << " set label " << obj->GetName() << " to bin " << i + 1 << " from bin " << a[i] << "content "
5652 << pcont[a[i]] << std::endl;
5653 }
5654
5655 // need to set here new ordered labels - otherwise loop before does not work since labold and llabels list
5656 // contain same objects
5657 for (i = 0; i < n; i++) {
5658 labels->At(i)->SetUniqueID(i + 1);
5659 }
5660 // set now the bin contents
5661 if (axis == GetXaxis()) {
5662 for (i = 0; i < n; i++) {
5663 Int_t ix = a[i] + 1;
5664 for (j = 0; j < ny; j++) {
5665 for (k = 0; k < nz; k++) {
5666 Int_t bin = GetBin(i + 1, j, k);
5667 UpdateBinContent(bin, cont[ix + nx * (j + ny * k)]);
5668 if (!errors2.empty())
5669 fSumw2.fArray[bin] = errors2[ix + nx * (j + ny * k)];
5670 }
5671 }
5672 }
5673 } else if (axis == GetYaxis()) {
5674 // using y axis
5675 for (i = 0; i < nx; i++) {
5676 for (j = 0; j < n; j++) {
5677 Int_t iy = a[j] + 1;
5678 for (k = 0; k < nz; k++) {
5679 Int_t bin = GetBin(i, j + 1, k);
5680 UpdateBinContent(bin, cont[i + nx * (iy + ny * k)]);
5681 if (!errors2.empty())
5682 fSumw2.fArray[bin] = errors2[i + nx * (iy + ny * k)];
5683 }
5684 }
5685 }
5686 } else {
5687 // using z axis
5688 for (i = 0; i < nx; i++) {
5689 for (j = 0; j < ny; j++) {
5690 for (k = 0; k < n; k++) {
5691 Int_t iz = a[k] + 1;
5692 Int_t bin = GetBin(i, j , k +1);
5693 UpdateBinContent(bin, cont[i + nx * (j + ny * iz)]);
5694 if (!errors2.empty())
5695 fSumw2.fArray[bin] = errors2[i + nx * (j + ny * iz)];
5696 }
5697 }
5698 }
5699 }
5700 }
5701 } else {
5702 //---alphabetic sort
5703 // sort labels using vector of strings and TMath::Sort
5704 // I need to array because labels order in list is not necessary that of the bins
5705 std::vector<std::string> vecLabels(n);
5706 for (i = 0; i < n; i++) {
5707 vecLabels[i] = labold->At(i)->GetName();
5708 b[i] = labold->At(i)->GetUniqueID(); // this is the bin corresponding to the label
5709 a[i] = i;
5710 }
5711 // sort in ascending order for strings
5712 TMath::Sort(n, vecLabels.data(), a.data(), kFALSE);
5713 // set the new labels
5714 for (i = 0; i < n; i++) {
5715 TObject *labelObj = labold->At(a[i]);
5716 labels->Add(labold->At(a[i]));
5717 // set the corresponding bin. NB bin starts from 1
5718 labelObj->SetUniqueID(i + 1);
5719 if (gDebug)
5720 std::cout << "bin " << i + 1 << " setting new labels for axis " << labold->At(a[i])->GetName() << " from "
5721 << b[a[i]] << std::endl;
5722 }
5723
5724 if (GetDimension() == 1) {
5725 cont.resize(n + 2);
5726 if (fSumw2.fN)
5727 errors2.resize(n + 2);
5728 for (i = 0; i < n; i++) {
5729 cont[i] = RetrieveBinContent(b[a[i]]);
5730 if (!errors2.empty())
5732 }
5733 for (i = 0; i < n; i++) {
5734 UpdateBinContent(i + 1, cont[i]);
5735 if (!errors2.empty())
5736 fSumw2.fArray[i+1] = errors2[i];
5737 }
5738 } else if (GetDimension() == 2) {
5739 Int_t nx = fXaxis.GetNbins() + 2;
5740 Int_t ny = fYaxis.GetNbins() + 2;
5741 cont.resize(nx * ny);
5742 if (fSumw2.fN)
5743 errors2.resize(nx * ny);
5744 // copy old bin contents and then set to new ordered bins
5745 // N.B. bin in histograms starts from 1, but in y we consider under/overflows
5746 for (i = 0; i < nx; i++) {
5747 for (j = 0; j < ny; j++) { // ny is nbins+2
5748 Int_t bin = GetBin(i, j);
5749 cont[i + nx * j] = RetrieveBinContent(bin);
5750 if (!errors2.empty())
5751 errors2[i + nx * j] = GetBinErrorSqUnchecked(bin);
5752 }
5753 }
5754 if (axis == GetXaxis()) {
5755 for (i = 0; i < n; i++) {
5756 for (j = 0; j < ny; j++) {
5757 Int_t bin = GetBin(i + 1 , j);
5758 UpdateBinContent(bin, cont[b[a[i]] + nx * j]);
5759 if (!errors2.empty())
5760 fSumw2.fArray[bin] = errors2[b[a[i]] + nx * j];
5761 }
5762 }
5763 } else {
5764 for (i = 0; i < nx; i++) {
5765 for (j = 0; j < n; j++) {
5766 Int_t bin = GetBin(i, j + 1);
5767 UpdateBinContent(bin, cont[i + nx * b[a[j]]]);
5768 if (!errors2.empty())
5769 fSumw2.fArray[bin] = errors2[i + nx * b[a[j]]];
5770 }
5771 }
5772 }
5773 } else {
5774 // case of 3D (needs to be tested)
5775 Int_t nx = fXaxis.GetNbins() + 2;
5776 Int_t ny = fYaxis.GetNbins() + 2;
5777 Int_t nz = fZaxis.GetNbins() + 2;
5778 cont.resize(nx * ny * nz);
5779 if (fSumw2.fN)
5780 errors2.resize(nx * ny * nz);
5781 for (i = 0; i < nx; i++) {
5782 for (j = 0; j < ny; j++) {
5783 for (k = 0; k < nz; k++) {
5784 Int_t bin = GetBin(i, j, k);
5785 cont[i + nx * (j + ny * k)] = RetrieveBinContent(bin);
5786 if (!errors2.empty())
5787 errors2[i + nx * (j + ny * k)] = GetBinErrorSqUnchecked(bin);
5788 }
5789 }
5790 }
5791 if (axis == GetXaxis()) {
5792 // labels on x axis
5793 for (i = 0; i < n; i++) { // for x we loop only on bins with the labels
5794 for (j = 0; j < ny; j++) {
5795 for (k = 0; k < nz; k++) {
5796 Int_t bin = GetBin(i + 1, j, k);
5797 UpdateBinContent(bin, cont[b[a[i]] + nx * (j + ny * k)]);
5798 if (!errors2.empty())
5799 fSumw2.fArray[bin] = errors2[b[a[i]] + nx * (j + ny * k)];
5800 }
5801 }
5802 }
5803 } else if (axis == GetYaxis()) {
5804 // labels on y axis
5805 for (i = 0; i < nx; i++) {
5806 for (j = 0; j < n; j++) {
5807 for (k = 0; k < nz; k++) {
5808 Int_t bin = GetBin(i, j+1, k);
5809 UpdateBinContent(bin, cont[i + nx * (b[a[j]] + ny * k)]);
5810 if (!errors2.empty())
5811 fSumw2.fArray[bin] = errors2[i + nx * (b[a[j]] + ny * k)];
5812 }
5813 }
5814 }
5815 } else {
5816 // labels on z axis
5817 for (i = 0; i < nx; i++) {
5818 for (j = 0; j < ny; j++) {
5819 for (k = 0; k < n; k++) {
5820 Int_t bin = GetBin(i, j, k+1);
5821 UpdateBinContent(bin, cont[i + nx * (j + ny * b[a[k]])]);
5822 if (!errors2.empty())
5823 fSumw2.fArray[bin] = errors2[i + nx * (j + ny * b[a[k]])];
5824 }
5825 }
5826 }
5827 }
5828 }
5829 }
5830 // need to set to zero the statistics if axis has been sorted
5831 // see for example TH3::PutStats for definition of s vector
5832 bool labelsAreSorted = kFALSE;
5833 for (i = 0; i < n; ++i) {
5834 if (a[i] != i) {
5836 break;
5837 }
5838 }
5839 if (labelsAreSorted) {
5840 double s[TH1::kNstat];
5841 GetStats(s);
5842 if (iaxis == 1) {
5843 s[2] = 0; // fTsumwx
5844 s[3] = 0; // fTsumwx2
5845 s[6] = 0; // fTsumwxy
5846 s[9] = 0; // fTsumwxz
5847 } else if (iaxis == 2) {
5848 s[4] = 0; // fTsumwy
5849 s[5] = 0; // fTsumwy2
5850 s[6] = 0; // fTsumwxy
5851 s[10] = 0; // fTsumwyz
5852 } else if (iaxis == 3) {
5853 s[7] = 0; // fTsumwz
5854 s[8] = 0; // fTsumwz2
5855 s[9] = 0; // fTsumwxz
5856 s[10] = 0; // fTsumwyz
5857 }
5858 PutStats(s);
5859 }
5860 delete labold;
5861}
5862
5863////////////////////////////////////////////////////////////////////////////////
5864/// Test if two double are almost equal.
5865
5866static inline Bool_t AlmostEqual(Double_t a, Double_t b, Double_t epsilon = 0.00000001)
5867{
5868 return TMath::Abs(a - b) < epsilon;
5869}
5870
5871////////////////////////////////////////////////////////////////////////////////
5872/// Test if a double is almost an integer.
5873
5874static inline Bool_t AlmostInteger(Double_t a, Double_t epsilon = 0.00000001)
5875{
5876 return AlmostEqual(a - TMath::Floor(a), 0, epsilon) ||
5877 AlmostEqual(a - TMath::Floor(a), 1, epsilon);
5878}
5879
5880////////////////////////////////////////////////////////////////////////////////
5881/// Test if the binning is equidistant.
5882
5883static inline bool IsEquidistantBinning(const TAxis& axis)
5884{
5885 // check if axis bin are equals
5886 if (!axis.GetXbins()->fN) return true; //
5887 // not able to check if there is only one axis entry
5888 bool isEquidistant = true;
5889 const Double_t firstBinWidth = axis.GetBinWidth(1);
5890 for (int i = 1; i < axis.GetNbins(); ++i) {
5891 const Double_t binWidth = axis.GetBinWidth(i);
5892 const bool match = TMath::AreEqualRel(firstBinWidth, binWidth, 1.E-10);
5893 isEquidistant &= match;
5894 if (!match)
5895 break;
5896 }
5897 return isEquidistant;
5898}
5899
5900////////////////////////////////////////////////////////////////////////////////
5901/// Same limits and bins.
5902
5904 return axis1.GetNbins() == axis2.GetNbins() &&
5905 TMath::AreEqualAbs(axis1.GetXmin(), axis2.GetXmin(), axis1.GetBinWidth(axis1.GetNbins()) * 1.E-10) &&
5906 TMath::AreEqualAbs(axis1.GetXmax(), axis2.GetXmax(), axis1.GetBinWidth(axis1.GetNbins()) * 1.E-10);
5907}
5908
5909////////////////////////////////////////////////////////////////////////////////
5910/// Finds new limits for the axis for the Merge function.
5911/// returns false if the limits are incompatible
5912
5914{
5916 return kTRUE;
5917
5919 return kFALSE; // not equidistant user binning not supported
5920
5921 Double_t width1 = destAxis.GetBinWidth(0);
5922 Double_t width2 = anAxis.GetBinWidth(0);
5923 if (width1 == 0 || width2 == 0)
5924 return kFALSE; // no binning not supported
5925
5926 Double_t xmin = TMath::Min(destAxis.GetXmin(), anAxis.GetXmin());
5927 Double_t xmax = TMath::Max(destAxis.GetXmax(), anAxis.GetXmax());
5929
5930 // check the bin size
5932 return kFALSE;
5933
5934 // std::cout << "Find new limit using given axis " << anAxis.GetXmin() << " , " << anAxis.GetXmax() << " bin width " << width2 << std::endl;
5935 // std::cout << " and destination axis " << destAxis.GetXmin() << " , " << destAxis.GetXmax() << " bin width " << width1 << std::endl;
5936
5937
5938 // check the limits
5939 Double_t delta;
5940 delta = (destAxis.GetXmin() - xmin)/width1;
5941 if (!AlmostInteger(delta))
5942 xmin -= (TMath::Ceil(delta) - delta)*width1;
5943
5944 delta = (anAxis.GetXmin() - xmin)/width2;
5945 if (!AlmostInteger(delta))
5946 xmin -= (TMath::Ceil(delta) - delta)*width2;
5947
5948
5949 delta = (destAxis.GetXmin() - xmin)/width1;
5950 if (!AlmostInteger(delta))
5951 return kFALSE;
5952
5953
5954 delta = (xmax - destAxis.GetXmax())/width1;
5955 if (!AlmostInteger(delta))
5956 xmax += (TMath::Ceil(delta) - delta)*width1;
5957
5958
5959 delta = (xmax - anAxis.GetXmax())/width2;
5960 if (!AlmostInteger(delta))
5961 xmax += (TMath::Ceil(delta) - delta)*width2;
5962
5963
5964 delta = (xmax - destAxis.GetXmax())/width1;
5965 if (!AlmostInteger(delta))
5966 return kFALSE;
5967#ifdef DEBUG
5968 if (!AlmostInteger((xmax - xmin) / width)) { // unnecessary check
5969 printf("TH1::RecomputeAxisLimits - Impossible\n");
5970 return kFALSE;
5971 }
5972#endif
5973
5974
5976
5977 //std::cout << "New re-computed axis : [ " << xmin << " , " << xmax << " ] width = " << width << " nbins " << destAxis.GetNbins() << std::endl;
5978
5979 return kTRUE;
5980}
5981
5982////////////////////////////////////////////////////////////////////////////////
5983/// Add all histograms in the collection to this histogram.
5984/// This function computes the min/max for the x axis,
5985/// compute a new number of bins, if necessary,
5986/// add bin contents, errors and statistics.
5987/// If all histograms have bin labels, bins with identical labels
5988/// will be merged, no matter what their order is.
5989/// If overflows are present and limits are different the function will fail.
5990/// The function returns the total number of entries in the result histogram
5991/// if the merge is successful, -1 otherwise.
5992///
5993/// Possible option:
5994/// -NOL : the merger will ignore the labels and merge the histograms bin by bin using bin center values to match bins
5995/// -NOCHECK: the histogram will not perform a check for duplicate labels in case of axes with labels. The check
5996/// (enabled by default) slows down the merging
5997///
5998/// IMPORTANT remark. The axis x may have different number
5999/// of bins and different limits, BUT the largest bin width must be
6000/// a multiple of the smallest bin width and the upper limit must also
6001/// be a multiple of the bin width.
6002/// Example:
6003///
6004/// ~~~ {.cpp}
6005/// void atest() {
6006/// TH1F *h1 = new TH1F("h1","h1",110,-110,0);
6007/// TH1F *h2 = new TH1F("h2","h2",220,0,110);
6008/// TH1F *h3 = new TH1F("h3","h3",330,-55,55);
6009/// TRandom r;
6010/// for (Int_t i=0;i<10000;i++) {
6011/// h1->Fill(r.Gaus(-55,10));
6012/// h2->Fill(r.Gaus(55,10));
6013/// h3->Fill(r.Gaus(0,10));
6014/// }
6015///
6016/// TList *list = new TList;
6017/// list->Add(h1);
6018/// list->Add(h2);
6019/// list->Add(h3);
6020/// TH1F *h = (TH1F*)h1->Clone("h");
6021/// h->Reset();
6022/// h->Merge(list);
6023/// h->Draw();
6024/// }
6025/// ~~~
6026
6028{
6029 if (!li) return 0;
6030 if (li->IsEmpty()) return (Long64_t) GetEntries();
6031
6032 // use TH1Merger class
6033 TH1Merger merger(*this,*li,opt);
6034 Bool_t ret = merger();
6035
6036 return (ret) ? GetEntries() : -1;
6037}
6038
6039
6040////////////////////////////////////////////////////////////////////////////////
6041/// Performs the operation:
6042///
6043/// `this = this*c1*f1`
6044///
6045/// If errors are defined (see TH1::Sumw2), errors are also recalculated.
6046///
6047/// Only bins inside the function range are recomputed.
6048/// IMPORTANT NOTE: If you intend to use the errors of this histogram later
6049/// you should call Sumw2 before making this operation.
6050/// This is particularly important if you fit the histogram after TH1::Multiply
6051///
6052/// The function return kFALSE if the Multiply operation failed
6053
6055{
6056 if (!f1) {
6057 Error("Multiply","Attempt to multiply by a non-existing function");
6058 return kFALSE;
6059 }
6060
6061 // delete buffer if it is there since it will become invalid
6062 if (fBuffer) BufferEmpty(1);
6063
6064 Int_t nx = GetNbinsX() + 2; // normal bins + uf / of (cells)
6065 Int_t ny = GetNbinsY() + 2;
6066 Int_t nz = GetNbinsZ() + 2;
6067 if (fDimension < 2) ny = 1;
6068 if (fDimension < 3) nz = 1;
6069
6070 // reset min-maximum
6071 SetMinimum();
6072 SetMaximum();
6073
6074 // - Loop on bins (including underflows/overflows)
6075 Double_t xx[3];
6076 Double_t *params = nullptr;
6077 f1->InitArgs(xx,params);
6078
6079 for (Int_t binz = 0; binz < nz; ++binz) {
6080 xx[2] = fZaxis.GetBinCenter(binz);
6081 for (Int_t biny = 0; biny < ny; ++biny) {
6082 xx[1] = fYaxis.GetBinCenter(biny);
6083 for (Int_t binx = 0; binx < nx; ++binx) {
6084 xx[0] = fXaxis.GetBinCenter(binx);
6085 if (!f1->IsInside(xx)) continue;
6087 Int_t bin = binx + nx * (biny + ny *binz);
6088 Double_t cu = c1*f1->EvalPar(xx);
6089 if (TF1::RejectedPoint()) continue;
6091 if (fSumw2.fN) {
6092 fSumw2.fArray[bin] = cu * cu * GetBinErrorSqUnchecked(bin);
6093 }
6094 }
6095 }
6096 }
6097 ResetStats();
6098 return kTRUE;
6099}
6100
6101////////////////////////////////////////////////////////////////////////////////
6102/// Multiply this histogram by h1.
6103///
6104/// `this = this*h1`
6105///
6106/// If errors of this are available (TH1::Sumw2), errors are recalculated.
6107/// Note that if h1 has Sumw2 set, Sumw2 is automatically called for this
6108/// if not already set.
6109///
6110/// IMPORTANT NOTE: If you intend to use the errors of this histogram later
6111/// you should call Sumw2 before making this operation.
6112/// This is particularly important if you fit the histogram after TH1::Multiply
6113///
6114/// The function return kFALSE if the Multiply operation failed
6115
6116Bool_t TH1::Multiply(const TH1 *h1)
6117{
6118 if (!h1) {
6119 Error("Multiply","Attempt to multiply by a non-existing histogram");
6120 return kFALSE;
6121 }
6122
6123 // delete buffer if it is there since it will become invalid
6124 if (fBuffer) BufferEmpty(1);
6125
6126 if (LoggedInconsistency("Multiply", this, h1) >= kDifferentNumberOfBins) {
6127 return false;
6128 }
6129
6130 // Create Sumw2 if h1 has Sumw2 set
6131 if (fSumw2.fN == 0 && h1->GetSumw2N() != 0) Sumw2();
6132
6133 // - Reset min- maximum
6134 SetMinimum();
6135 SetMaximum();
6136
6137 // - Loop on bins (including underflows/overflows)
6138 for (Int_t i = 0; i < fNcells; ++i) {
6141 UpdateBinContent(i, c0 * c1);
6142 if (fSumw2.fN) {
6144 }
6145 }
6146 ResetStats();
6147 return kTRUE;
6148}
6149
6150////////////////////////////////////////////////////////////////////////////////
6151/// Replace contents of this histogram by multiplication of h1 by h2.
6152///
6153/// `this = (c1*h1)*(c2*h2)`
6154///
6155/// If errors of this are available (TH1::Sumw2), errors are recalculated.
6156/// Note that if h1 or h2 have Sumw2 set, Sumw2 is automatically called for this
6157/// if not already set.
6158///
6159/// IMPORTANT NOTE: If you intend to use the errors of this histogram later
6160/// you should call Sumw2 before making this operation.
6161/// This is particularly important if you fit the histogram after TH1::Multiply
6162///
6163/// The function return kFALSE if the Multiply operation failed
6164
6166{
6167 TString opt = option;
6168 opt.ToLower();
6169 // Bool_t binomial = kFALSE;
6170 // if (opt.Contains("b")) binomial = kTRUE;
6171 if (!h1 || !h2) {
6172 Error("Multiply","Attempt to multiply by a non-existing histogram");
6173 return kFALSE;
6174 }
6175
6176 // delete buffer if it is there since it will become invalid
6177 if (fBuffer) BufferEmpty(1);
6178
6179 if (LoggedInconsistency("Multiply", this, h1) >= kDifferentNumberOfBins ||
6180 LoggedInconsistency("Multiply", h1, h2) >= kDifferentNumberOfBins) {
6181 return false;
6182 }
6183
6184 // Create Sumw2 if h1 or h2 have Sumw2 set
6185 if (fSumw2.fN == 0 && (h1->GetSumw2N() != 0 || h2->GetSumw2N() != 0)) Sumw2();
6186
6187 // - Reset min - maximum
6188 SetMinimum();
6189 SetMaximum();
6190
6191 // - Loop on bins (including underflows/overflows)
6192 Double_t c1sq = c1 * c1; Double_t c2sq = c2 * c2;
6193 for (Int_t i = 0; i < fNcells; ++i) {
6195 Double_t b2 = h2->RetrieveBinContent(i);
6196 UpdateBinContent(i, c1 * b1 * c2 * b2);
6197 if (fSumw2.fN) {
6198 fSumw2.fArray[i] = c1sq * c2sq * (h1->GetBinErrorSqUnchecked(i) * b2 * b2 + h2->GetBinErrorSqUnchecked(i) * b1 * b1);
6199 }
6200 }
6201 ResetStats();
6202 return kTRUE;
6203}
6204
6205////////////////////////////////////////////////////////////////////////////////
6206/// Control routine to paint any kind of histograms.
6207///
6208/// This function is automatically called by TCanvas::Update.
6209/// (see TH1::Draw for the list of options)
6210
6212{
6214
6215 if (fPainter) {
6216 if (option && strlen(option) > 0)
6218 else
6220 }
6221}
6222
6223////////////////////////////////////////////////////////////////////////////////
6224/// Rebin this histogram
6225///
6226/// #### case 1 xbins=0
6227///
6228/// If newname is blank (default), the current histogram is modified and
6229/// a pointer to it is returned.
6230///
6231/// If newname is not blank, the current histogram is not modified, and a
6232/// new histogram is returned which is a Clone of the current histogram
6233/// with its name set to newname.
6234///
6235/// The parameter ngroup indicates how many bins of this have to be merged
6236/// into one bin of the result.
6237///
6238/// If the original histogram has errors stored (via Sumw2), the resulting
6239/// histograms has new errors correctly calculated.
6240///
6241/// examples: if h1 is an existing TH1F histogram with 100 bins
6242///
6243/// ~~~ {.cpp}
6244/// h1->Rebin(); //merges two bins in one in h1: previous contents of h1 are lost
6245/// h1->Rebin(5); //merges five bins in one in h1
6246/// TH1F *hnew = dynamic_cast<TH1F*>(h1->Rebin(5,"hnew")); // creates a new histogram hnew
6247/// // merging 5 bins of h1 in one bin
6248/// ~~~
6249///
6250/// NOTE: If ngroup is not an exact divider of the number of bins,
6251/// the top limit of the rebinned histogram is reduced
6252/// to the upper edge of the last bin that can make a complete
6253/// group. The remaining bins are added to the overflow bin.
6254/// Statistics will be recomputed from the new bin contents.
6255///
6256/// #### case 2 xbins!=0
6257///
6258/// A new histogram is created (you should specify newname).
6259/// The parameter ngroup is the number of variable size bins in the created histogram.
6260/// The array xbins must contain ngroup+1 elements that represent the low-edges
6261/// of the bins.
6262/// If the original histogram has errors stored (via Sumw2), the resulting
6263/// histograms has new errors correctly calculated.
6264///
6265/// NOTE: The bin edges specified in xbins should correspond to bin edges
6266/// in the original histogram. If a bin edge in the new histogram is
6267/// in the middle of a bin in the original histogram, all entries in
6268/// the split bin in the original histogram will be transfered to the
6269/// lower of the two possible bins in the new histogram. This is
6270/// probably not what you want. A warning message is emitted in this
6271/// case
6272///
6273/// examples: if h1 is an existing TH1F histogram with 100 bins
6274///
6275/// ~~~ {.cpp}
6276/// Double_t xbins[25] = {...} array of low-edges (xbins[25] is the upper edge of last bin
6277/// h1->Rebin(24,"hnew",xbins); //creates a new variable bin size histogram hnew
6278/// ~~~
6279
6280TH1 *TH1::Rebin(Int_t ngroup, const char*newname, const Double_t *xbins)
6281{
6282 Int_t nbins = fXaxis.GetNbins();
6285 if ((ngroup <= 0) || (ngroup > nbins)) {
6286 Error("Rebin", "Illegal value of ngroup=%d",ngroup);
6287 return nullptr;
6288 }
6289
6290 if (fDimension > 1 || InheritsFrom(TProfile::Class())) {
6291 Error("Rebin", "Operation valid on 1-D histograms only");
6292 return nullptr;
6293 }
6294 if (!newname && xbins) {
6295 Error("Rebin","if xbins is specified, newname must be given");
6296 return nullptr;
6297 }
6298
6299 Int_t newbins = nbins/ngroup;
6300 if (!xbins) {
6301 Int_t nbg = nbins/ngroup;
6302 if (nbg*ngroup != nbins) {
6303 Warning("Rebin", "ngroup=%d is not an exact divider of nbins=%d.",ngroup,nbins);
6304 }
6305 }
6306 else {
6307 // in the case that xbins is given (rebinning in variable bins), ngroup is
6308 // the new number of bins and number of grouped bins is not constant.
6309 // when looping for setting the contents for the new histogram we
6310 // need to loop on all bins of original histogram. Then set ngroup=nbins
6311 newbins = ngroup;
6312 ngroup = nbins;
6313 }
6314
6315 // Save old bin contents into a new array
6316 Double_t entries = fEntries;
6317 Double_t *oldBins = new Double_t[nbins+2];
6318 Int_t bin, i;
6319 for (bin=0;bin<nbins+2;bin++) oldBins[bin] = RetrieveBinContent(bin);
6320 Double_t *oldErrors = nullptr;
6321 if (fSumw2.fN != 0) {
6322 oldErrors = new Double_t[nbins+2];
6323 for (bin=0;bin<nbins+2;bin++) oldErrors[bin] = GetBinError(bin);
6324 }
6325 // rebin will not include underflow/overflow if new axis range is larger than old axis range
6326 if (xbins) {
6327 if (xbins[0] < fXaxis.GetXmin() && oldBins[0] != 0 )
6328 Warning("Rebin","underflow entries will not be used when rebinning");
6329 if (xbins[newbins] > fXaxis.GetXmax() && oldBins[nbins+1] != 0 )
6330 Warning("Rebin","overflow entries will not be used when rebinning");
6331 }
6332
6333
6334 // create a clone of the old histogram if newname is specified
6335 TH1 *hnew = this;
6336 if ((newname && strlen(newname) > 0) || xbins) {
6337 hnew = (TH1*)Clone(newname);
6338 }
6339
6340 //reset can extend bit to avoid an axis extension in SetBinContent
6341 UInt_t oldExtendBitMask = hnew->SetCanExtend(kNoAxis);
6342
6343 // save original statistics
6344 Double_t stat[kNstat];
6345 GetStats(stat);
6346 bool resetStat = false;
6347 // change axis specs and rebuild bin contents array::RebinAx
6348 if(!xbins && (newbins*ngroup != nbins)) {
6350 resetStat = true; //stats must be reset because top bins will be moved to overflow bin
6351 }
6352 // save the TAttAxis members (reset by SetBins)
6364
6365 if(!xbins && (fXaxis.GetXbins()->GetSize() > 0)){ // variable bin sizes
6366 Double_t *bins = new Double_t[newbins+1];
6367 for(i = 0; i <= newbins; ++i) bins[i] = fXaxis.GetBinLowEdge(1+i*ngroup);
6368 hnew->SetBins(newbins,bins); //this also changes errors array (if any)
6369 delete [] bins;
6370 } else if (xbins) {
6371 hnew->SetBins(newbins,xbins);
6372 } else {
6373 hnew->SetBins(newbins,xmin,xmax);
6374 }
6375
6376 // Restore axis attributes
6388
6389 // copy merged bin contents (ignore under/overflows)
6390 // Start merging only once the new lowest edge is reached
6391 Int_t startbin = 1;
6392 const Double_t newxmin = hnew->GetXaxis()->GetBinLowEdge(1);
6393 while( fXaxis.GetBinCenter(startbin) < newxmin && startbin <= nbins ) {
6394 startbin++;
6395 }
6398 for (bin = 1;bin<=newbins;bin++) {
6399 binContent = 0;
6400 binError = 0;
6401 Int_t imax = ngroup;
6402 Double_t xbinmax = hnew->GetXaxis()->GetBinUpEdge(bin);
6403 // check bin edges for the cases when we provide an array of bins
6404 // be careful in case bins can have zero width
6406 hnew->GetXaxis()->GetBinLowEdge(bin),
6407 TMath::Max(1.E-8 * fXaxis.GetBinWidth(oldbin), 1.E-16 )) )
6408 {
6409 Warning("Rebin","Bin edge %d of rebinned histogram does not match any bin edges of the old histogram. Result can be inconsistent",bin);
6410 }
6411 for (i=0;i<ngroup;i++) {
6412 if( (oldbin+i > nbins) ||
6413 ( hnew != this && (fXaxis.GetBinCenter(oldbin+i) > xbinmax)) ) {
6414 imax = i;
6415 break;
6416 }
6419 }
6420 hnew->SetBinContent(bin,binContent);
6421 if (oldErrors) hnew->SetBinError(bin,TMath::Sqrt(binError));
6422 oldbin += imax;
6423 }
6424
6425 // sum underflow and overflow contents until startbin
6426 binContent = 0;
6427 binError = 0;
6428 for (i = 0; i < startbin; ++i) {
6429 binContent += oldBins[i];
6430 if (oldErrors) binError += oldErrors[i]*oldErrors[i];
6431 }
6432 hnew->SetBinContent(0,binContent);
6433 if (oldErrors) hnew->SetBinError(0,TMath::Sqrt(binError));
6434 // sum overflow
6435 binContent = 0;
6436 binError = 0;
6437 for (i = oldbin; i <= nbins+1; ++i) {
6438 binContent += oldBins[i];
6439 if (oldErrors) binError += oldErrors[i]*oldErrors[i];
6440 }
6441 hnew->SetBinContent(newbins+1,binContent);
6442 if (oldErrors) hnew->SetBinError(newbins+1,TMath::Sqrt(binError));
6443
6444 hnew->SetCanExtend(oldExtendBitMask); // restore previous state
6445
6446 // restore statistics and entries modified by SetBinContent
6447 hnew->SetEntries(entries);
6448 if (!resetStat) hnew->PutStats(stat);
6449 delete [] oldBins;
6450 if (oldErrors) delete [] oldErrors;
6451 return hnew;
6452}
6453
6454////////////////////////////////////////////////////////////////////////////////
6455/// finds new limits for the axis so that *point* is within the range and
6456/// the limits are compatible with the previous ones (see TH1::Merge).
6457/// new limits are put into *newMin* and *newMax* variables.
6458/// axis - axis whose limits are to be recomputed
6459/// point - point that should fit within the new axis limits
6460/// newMin - new minimum will be stored here
6461/// newMax - new maximum will be stored here.
6462/// false if failed (e.g. if the initial axis limits are wrong
6463/// or the new range is more than \f$ 2^{64} \f$ times the old one).
6464
6466{
6467 Double_t xmin = axis->GetXmin();
6468 Double_t xmax = axis->GetXmax();
6469 if (xmin >= xmax) return kFALSE;
6471
6472 //recompute new axis limits by doubling the current range
6473 Int_t ntimes = 0;
6474 while (point < xmin) {
6475 if (ntimes++ > 64)
6476 return kFALSE;
6477 xmin = xmin - range;
6478 range *= 2;
6479 }
6480 while (point >= xmax) {
6481 if (ntimes++ > 64)
6482 return kFALSE;
6483 xmax = xmax + range;
6484 range *= 2;
6485 }
6486 newMin = xmin;
6487 newMax = xmax;
6488 // Info("FindNewAxisLimits", "OldAxis: (%lf, %lf), new: (%lf, %lf), point: %lf",
6489 // axis->GetXmin(), axis->GetXmax(), xmin, xmax, point);
6490
6491 return kTRUE;
6492}
6493
6494////////////////////////////////////////////////////////////////////////////////
6495/// Histogram is resized along axis such that x is in the axis range.
6496/// The new axis limits are recomputed by doubling iteratively
6497/// the current axis range until the specified value x is within the limits.
6498/// The algorithm makes a copy of the histogram, then loops on all bins
6499/// of the old histogram to fill the extended histogram.
6500/// Takes into account errors (Sumw2) if any.
6501/// The algorithm works for 1-d, 2-D and 3-D histograms.
6502/// The axis must be extendable before invoking this function.
6503/// Ex:
6504///
6505/// ~~~ {.cpp}
6506/// h->GetXaxis()->SetCanExtend(kTRUE);
6507/// ~~~
6508
6509void TH1::ExtendAxis(Double_t x, TAxis *axis)
6510{
6511 if (!axis->CanExtend()) return;
6512 if (TMath::IsNaN(x)) { // x may be a NaN
6514 return;
6515 }
6516
6517 if (axis->GetXmin() >= axis->GetXmax()) return;
6518 if (axis->GetNbins() <= 0) return;
6519
6521 if (!FindNewAxisLimits(axis, x, xmin, xmax))
6522 return;
6523
6524 //save a copy of this histogram
6525 TH1 *hold = (TH1*)IsA()->New();
6526 hold->SetDirectory(nullptr);
6527 Copy(*hold);
6528 //set new axis limits
6529 axis->SetLimits(xmin,xmax);
6530
6531
6532 //now loop on all bins and refill
6534
6535 Reset("ICE"); //reset only Integral, contents and Errors
6536
6537 int iaxis = 0;
6538 if (axis == &fXaxis) iaxis = 1;
6539 if (axis == &fYaxis) iaxis = 2;
6540 if (axis == &fZaxis) iaxis = 3;
6541 bool firstw = kTRUE;
6542 Int_t binx,biny, binz = 0;
6543 Int_t ix = 0,iy = 0,iz = 0;
6544 Double_t bx,by,bz;
6545 Int_t ncells = hold->GetNcells();
6546 for (Int_t bin = 0; bin < ncells; ++bin) {
6547 hold->GetBinXYZ(bin,binx,biny,binz);
6548 bx = hold->GetXaxis()->GetBinCenter(binx);
6549 ix = fXaxis.FindFixBin(bx);
6550 if (fDimension > 1) {
6551 by = hold->GetYaxis()->GetBinCenter(biny);
6552 iy = fYaxis.FindFixBin(by);
6553 if (fDimension > 2) {
6554 bz = hold->GetZaxis()->GetBinCenter(binz);
6555 iz = fZaxis.FindFixBin(bz);
6556 }
6557 }
6558 // exclude underflow/overflow
6559 double content = hold->RetrieveBinContent(bin);
6560 if (content == 0) continue;
6561 if (IsBinUnderflow(bin,iaxis) || IsBinOverflow(bin,iaxis) ) {
6562 if (firstw) {
6563 Warning("ExtendAxis","Histogram %s has underflow or overflow in the axis that is extendable"
6564 " their content will be lost",GetName() );
6565 firstw= kFALSE;
6566 }
6567 continue;
6568 }
6569 Int_t ibin= GetBin(ix,iy,iz);
6571 if (errors) {
6572 fSumw2.fArray[ibin] += hold->GetBinErrorSqUnchecked(bin);
6573 }
6574 }
6575 delete hold;
6576}
6577
6578////////////////////////////////////////////////////////////////////////////////
6579/// Recursively remove object from the list of functions
6580
6582{
6583 // Rely on TROOT::RecursiveRemove to take the readlock.
6584
6585 if (fFunctions) {
6587 }
6588}
6589
6590////////////////////////////////////////////////////////////////////////////////
6591/// Multiply this histogram by a constant c1.
6592///
6593/// `this = c1*this`
6594///
6595/// Note that both contents and errors (if any) are scaled.
6596/// This function uses the services of TH1::Add
6597///
6598/// IMPORTANT NOTE: Sumw2() is called automatically when scaling.
6599/// If you are not interested in the histogram statistics you can call
6600/// Sumw2(kFALSE) or use the option "nosw2"
6601///
6602/// One can scale a histogram such that the bins integral is equal to
6603/// the normalization parameter via TH1::Scale(Double_t norm), where norm
6604/// is the desired normalization divided by the integral of the histogram.
6605///
6606/// If option contains "width" the bin contents and errors are divided
6607/// by the bin width.
6608
6610{
6611
6612 TString opt = option; opt.ToLower();
6613 // store bin errors when scaling since cannot anymore be computed as sqrt(N)
6614 if (!opt.Contains("nosw2") && GetSumw2N() == 0) Sumw2();
6615 if (opt.Contains("width")) Add(this, this, c1, -1);
6616 else {
6617 if (fBuffer) BufferEmpty(1);
6618 for(Int_t i = 0; i < fNcells; ++i) UpdateBinContent(i, c1 * RetrieveBinContent(i));
6619 if (fSumw2.fN) for(Int_t i = 0; i < fNcells; ++i) fSumw2.fArray[i] *= (c1 * c1); // update errors
6620 // update global histograms statistics
6621 Double_t s[kNstat] = {0};
6622 GetStats(s);
6623 for (Int_t i=0 ; i < kNstat; i++) {
6624 if (i == 1) s[i] = c1*c1*s[i];
6625 else s[i] = c1*s[i];
6626 }
6627 PutStats(s);
6628 SetMinimum(); SetMaximum(); // minimum and maximum value will be recalculated the next time
6629 }
6630
6631 // if contours set, must also scale contours
6633 if (ncontours == 0) return;
6635 for (Int_t i = 0; i < ncontours; ++i) levels[i] *= c1;
6636}
6637
6638////////////////////////////////////////////////////////////////////////////////
6639/// Returns true if all axes are extendable.
6640
6642{
6644 if (GetDimension() > 1) canExtend &= fYaxis.CanExtend();
6645 if (GetDimension() > 2) canExtend &= fZaxis.CanExtend();
6646
6647 return canExtend;
6648}
6649
6650////////////////////////////////////////////////////////////////////////////////
6651/// Make the histogram axes extendable / not extendable according to the bit mask
6652/// returns the previous bit mask specifying which axes are extendable
6653
6655{
6657
6661
6662 if (GetDimension() > 1) {
6666 }
6667
6668 if (GetDimension() > 2) {
6672 }
6673
6674 return oldExtendBitMask;
6675}
6676
6677///////////////////////////////////////////////////////////////////////////////
6678/// Internal function used in TH1::Fill to see which axis is full alphanumeric,
6679/// i.e. can be extended and is alphanumeric
6681{
6685 bitMask |= kYaxis;
6687 bitMask |= kZaxis;
6688
6689 return bitMask;
6690}
6691
6692////////////////////////////////////////////////////////////////////////////////
6693/// Static function to set the default buffer size for automatic histograms.
6694/// When a histogram is created with one of its axis lower limit greater
6695/// or equal to its upper limit, the function SetBuffer is automatically
6696/// called with the default buffer size.
6697
6699{
6701}
6702
6703////////////////////////////////////////////////////////////////////////////////
6704/// When this static function is called with `sumw2=kTRUE`, all new
6705/// histograms will automatically activate the storage
6706/// of the sum of squares of errors, ie TH1::Sumw2 is automatically called.
6707
6709{
6711}
6712
6713////////////////////////////////////////////////////////////////////////////////
6714/// Change/set the title.
6715///
6716/// If title is in the form `stringt;stringx;stringy;stringz`
6717/// the histogram title is set to `stringt`, the x axis title to `stringx`,
6718/// the y axis title to `stringy`, and the z axis title to `stringz`.
6719///
6720/// To insert the character `;` in one of the titles, one should use `#;`
6721/// or `#semicolon`.
6722
6723void TH1::SetTitle(const char *title)
6724{
6725 fTitle = title;
6726 fTitle.ReplaceAll("#;",2,"#semicolon",10);
6727
6728 // Decode fTitle. It may contain X, Y and Z titles
6730 Int_t isc = str1.Index(";");
6731 Int_t lns = str1.Length();
6732
6733 if (isc >=0 ) {
6734 fTitle = str1(0,isc);
6735 str1 = str1(isc+1, lns);
6736 isc = str1.Index(";");
6737 if (isc >=0 ) {
6738 str2 = str1(0,isc);
6739 str2.ReplaceAll("#semicolon",10,";",1);
6740 fXaxis.SetTitle(str2.Data());
6741 lns = str1.Length();
6742 str1 = str1(isc+1, lns);
6743 isc = str1.Index(";");
6744 if (isc >=0 ) {
6745 str2 = str1(0,isc);
6746 str2.ReplaceAll("#semicolon",10,";",1);
6747 fYaxis.SetTitle(str2.Data());
6748 lns = str1.Length();
6749 str1 = str1(isc+1, lns);
6750 str1.ReplaceAll("#semicolon",10,";",1);
6751 fZaxis.SetTitle(str1.Data());
6752 } else {
6753 str1.ReplaceAll("#semicolon",10,";",1);
6754 fYaxis.SetTitle(str1.Data());
6755 }
6756 } else {
6757 str1.ReplaceAll("#semicolon",10,";",1);
6758 fXaxis.SetTitle(str1.Data());
6759 }
6760 }
6761
6762 fTitle.ReplaceAll("#semicolon",10,";",1);
6763
6764 if (gPad && TestBit(kMustCleanup)) gPad->Modified();
6765}
6766
6767////////////////////////////////////////////////////////////////////////////////
6768/// Smooth array xx, translation of Hbook routine `hsmoof.F`.
6769/// Based on algorithm 353QH twice presented by J. Friedman
6770/// in [Proc. of the 1974 CERN School of Computing, Norway, 11-24 August, 1974](https://cds.cern.ch/record/186223).
6771/// 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).
6772
6774{
6775 if (nn < 3 ) {
6776 ::Error("SmoothArray","Need at least 3 points for smoothing: n = %d",nn);
6777 return;
6778 }
6779
6780 Int_t ii;
6781 std::array<double, 3> hh{};
6782
6783 std::vector<double> yy(nn);
6784 std::vector<double> zz(nn);
6785 std::vector<double> rr(nn);
6786
6787 for (Int_t pass=0;pass<ntimes;pass++) {
6788 // first copy original data into temp array
6789 std::copy(xx, xx+nn, zz.begin() );
6790
6791 for (int noent = 0; noent < 2; ++noent) { // run algorithm two times
6792
6793 // do 353 i.e. running median 3, 5, and 3 in a single loop
6794 for (int kk = 0; kk < 3; kk++) {
6795 std::copy(zz.begin(), zz.end(), yy.begin());
6796 int medianType = (kk != 1) ? 3 : 5;
6797 int ifirst = (kk != 1 ) ? 1 : 2;
6798 int ilast = (kk != 1 ) ? nn-1 : nn -2;
6799 //nn2 = nn - ik - 1;
6800 // do all elements beside the first and last point for median 3
6801 // and first two and last 2 for median 5
6802 for ( ii = ifirst; ii < ilast; ii++) {
6803 zz[ii] = TMath::Median(medianType, yy.data() + ii - ifirst);
6804 }
6805
6806 if (kk == 0) { // first median 3
6807 // first point
6808 hh[0] = zz[1];
6809 hh[1] = zz[0];
6810 hh[2] = 3*zz[1] - 2*zz[2];
6811 zz[0] = TMath::Median(3, hh.data());
6812 // last point
6813 hh[0] = zz[nn - 2];
6814 hh[1] = zz[nn - 1];
6815 hh[2] = 3*zz[nn - 2] - 2*zz[nn - 3];
6816 zz[nn - 1] = TMath::Median(3, hh.data());
6817 }
6818
6819 if (kk == 1) { // median 5
6820 // second point with window length 3
6821 zz[1] = TMath::Median(3, yy.data());
6822 // second-to-last point with window length 3
6823 zz[nn - 2] = TMath::Median(3, yy.data() + nn - 3);
6824 }
6825
6826 // In the third iteration (kk == 2), the first and last point stay
6827 // the same (see paper linked in the documentation).
6828 }
6829
6830 std::copy ( zz.begin(), zz.end(), yy.begin() );
6831
6832 // quadratic interpolation for flat segments
6833 for (ii = 2; ii < (nn - 2); ii++) {
6834 if (zz[ii - 1] != zz[ii]) continue;
6835 if (zz[ii] != zz[ii + 1]) continue;
6836 const double tmp0 = zz[ii - 2] - zz[ii];
6837 const double tmp1 = zz[ii + 2] - zz[ii];
6838 if (tmp0 * tmp1 <= 0) continue;
6839 int jk = 1;
6840 if ( std::abs(tmp1) > std::abs(tmp0) ) jk = -1;
6841 yy[ii] = -0.5*zz[ii - 2*jk] + zz[ii]/0.75 + zz[ii + 2*jk] /6.;
6842 yy[ii + jk] = 0.5*(zz[ii + 2*jk] - zz[ii - 2*jk]) + zz[ii];
6843 }
6844
6845 // running means
6846 //std::copy(zz.begin(), zz.end(), yy.begin());
6847 for (ii = 1; ii < nn - 1; ii++) {
6848 zz[ii] = 0.25*yy[ii - 1] + 0.5*yy[ii] + 0.25*yy[ii + 1];
6849 }
6850 zz[0] = yy[0];
6851 zz[nn - 1] = yy[nn - 1];
6852
6853 if (noent == 0) {
6854
6855 // save computed values
6856 std::copy(zz.begin(), zz.end(), rr.begin());
6857
6858 // COMPUTE residuals
6859 for (ii = 0; ii < nn; ii++) {
6860 zz[ii] = xx[ii] - zz[ii];
6861 }
6862 }
6863
6864 } // end loop on noent
6865
6866
6867 double xmin = TMath::MinElement(nn,xx);
6868 for (ii = 0; ii < nn; ii++) {
6869 if (xmin < 0) xx[ii] = rr[ii] + zz[ii];
6870 // make smoothing defined positive - not better using 0 ?
6871 else xx[ii] = std::max((rr[ii] + zz[ii]),0.0 );
6872 }
6873 }
6874}
6875
6876////////////////////////////////////////////////////////////////////////////////
6877/// Smooth bin contents of this histogram.
6878/// if option contains "R" smoothing is applied only to the bins
6879/// defined in the X axis range (default is to smooth all bins)
6880/// Bin contents are replaced by their smooth values.
6881/// Errors (if any) are not modified.
6882/// the smoothing procedure is repeated ntimes (default=1)
6883
6885{
6886 if (fDimension != 1) {
6887 Error("Smooth","Smooth only supported for 1-d histograms");
6888 return;
6889 }
6890 Int_t nbins = fXaxis.GetNbins();
6891 if (nbins < 3) {
6892 Error("Smooth","Smooth only supported for histograms with >= 3 bins. Nbins = %d",nbins);
6893 return;
6894 }
6895
6896 // delete buffer if it is there since it will become invalid
6897 if (fBuffer) BufferEmpty(1);
6898
6899 Int_t firstbin = 1, lastbin = nbins;
6900 TString opt = option;
6901 opt.ToLower();
6902 if (opt.Contains("r")) {
6905 }
6906 nbins = lastbin - firstbin + 1;
6907 Double_t *xx = new Double_t[nbins];
6909 Int_t i;
6910 for (i=0;i<nbins;i++) {
6912 }
6913
6914 TH1::SmoothArray(nbins,xx,ntimes);
6915
6916 for (i=0;i<nbins;i++) {
6918 }
6919 fEntries = nent;
6920 delete [] xx;
6921
6922 if (gPad) gPad->Modified();
6923}
6924
6925////////////////////////////////////////////////////////////////////////////////
6926/// if flag=kTRUE, underflows and overflows are used by the Fill functions
6927/// in the computation of statistics (mean value, StdDev).
6928/// By default, underflows or overflows are not used.
6929
6931{
6933}
6934
6935////////////////////////////////////////////////////////////////////////////////
6936/// Stream a class object.
6937
6938void TH1::Streamer(TBuffer &b)
6939{
6940 if (b.IsReading()) {
6941 UInt_t R__s, R__c;
6942 Version_t R__v = b.ReadVersion(&R__s, &R__c);
6943 if (fDirectory) fDirectory->Remove(this);
6944 fDirectory = nullptr;
6945 if (R__v > 2) {
6946 b.ReadClassBuffer(TH1::Class(), this, R__v, R__s, R__c);
6947
6949 fXaxis.SetParent(this);
6950 fYaxis.SetParent(this);
6951 fZaxis.SetParent(this);
6952 TIter next(fFunctions);
6953 TObject *obj;
6954 while ((obj=next())) {
6955 if (obj->InheritsFrom(TF1::Class())) ((TF1*)obj)->SetParent(this);
6956 }
6957 return;
6958 }
6959 //process old versions before automatic schema evolution
6964 b >> fNcells;
6965 fXaxis.Streamer(b);
6966 fYaxis.Streamer(b);
6967 fZaxis.Streamer(b);
6968 fXaxis.SetParent(this);
6969 fYaxis.SetParent(this);
6970 fZaxis.SetParent(this);
6971 b >> fBarOffset;
6972 b >> fBarWidth;
6973 b >> fEntries;
6974 b >> fTsumw;
6975 b >> fTsumw2;
6976 b >> fTsumwx;
6977 b >> fTsumwx2;
6978 if (R__v < 2) {
6980 Float_t *contour=nullptr;
6981 b >> maximum; fMaximum = maximum;
6982 b >> minimum; fMinimum = minimum;
6983 b >> norm; fNormFactor = norm;
6984 Int_t n = b.ReadArray(contour);
6985 fContour.Set(n);
6986 for (Int_t i=0;i<n;i++) fContour.fArray[i] = contour[i];
6987 delete [] contour;
6988 } else {
6989 b >> fMaximum;
6990 b >> fMinimum;
6991 b >> fNormFactor;
6993 }
6994 fSumw2.Streamer(b);
6996 fFunctions->Delete();
6998 b.CheckByteCount(R__s, R__c, TH1::IsA());
6999
7000 } else {
7001 b.WriteClassBuffer(TH1::Class(),this);
7002 }
7003}
7004
7005////////////////////////////////////////////////////////////////////////////////
7006/// Print some global quantities for this histogram.
7007/// \param[in] option
7008/// - "base" is given, number of bins and ranges are also printed
7009/// - "range" is given, bin contents and errors are also printed
7010/// for all bins in the current range (default 1-->nbins)
7011/// - "all" is given, bin contents and errors are also printed
7012/// for all bins including under and overflows.
7013
7014void TH1::Print(Option_t *option) const
7015{
7016 if (fBuffer) const_cast<TH1*>(this)->BufferEmpty();
7017 printf( "TH1.Print Name = %s, Entries= %d, Total sum= %g\n",GetName(),Int_t(fEntries),GetSumOfWeights());
7018 TString opt = option;
7019 opt.ToLower();
7020 Int_t all;
7021 if (opt.Contains("all")) all = 0;
7022 else if (opt.Contains("range")) all = 1;
7023 else if (opt.Contains("base")) all = 2;
7024 else return;
7025
7026 Int_t bin, binx, biny, binz;
7028 if (all == 0) {
7029 lastx = fXaxis.GetNbins()+1;
7030 if (fDimension > 1) lasty = fYaxis.GetNbins()+1;
7031 if (fDimension > 2) lastz = fZaxis.GetNbins()+1;
7032 } else {
7034 if (fDimension > 1) {firsty = fYaxis.GetFirst(); lasty = fYaxis.GetLast();}
7035 if (fDimension > 2) {firstz = fZaxis.GetFirst(); lastz = fZaxis.GetLast();}
7036 }
7037
7038 if (all== 2) {
7039 printf(" Title = %s\n", GetTitle());
7040 printf(" NbinsX= %d, xmin= %g, xmax=%g", fXaxis.GetNbins(), fXaxis.GetXmin(), fXaxis.GetXmax());
7041 if( fDimension > 1) printf(", NbinsY= %d, ymin= %g, ymax=%g", fYaxis.GetNbins(), fYaxis.GetXmin(), fYaxis.GetXmax());
7042 if( fDimension > 2) printf(", NbinsZ= %d, zmin= %g, zmax=%g", fZaxis.GetNbins(), fZaxis.GetXmin(), fZaxis.GetXmax());
7043 printf("\n");
7044 return;
7045 }
7046
7047 Double_t w,e;
7048 Double_t x,y,z;
7049 if (fDimension == 1) {
7050 for (binx=firstx;binx<=lastx;binx++) {
7053 e = GetBinError(binx);
7054 if(fSumw2.fN) printf(" fSumw[%d]=%g, x=%g, error=%g\n",binx,w,x,e);
7055 else printf(" fSumw[%d]=%g, x=%g\n",binx,w,x);
7056 }
7057 }
7058 if (fDimension == 2) {
7059 for (biny=firsty;biny<=lasty;biny++) {
7061 for (binx=firstx;binx<=lastx;binx++) {
7062 bin = GetBin(binx,biny);
7064 w = RetrieveBinContent(bin);
7065 e = GetBinError(bin);
7066 if(fSumw2.fN) printf(" fSumw[%d][%d]=%g, x=%g, y=%g, error=%g\n",binx,biny,w,x,y,e);
7067 else printf(" fSumw[%d][%d]=%g, x=%g, y=%g\n",binx,biny,w,x,y);
7068 }
7069 }
7070 }
7071 if (fDimension == 3) {
7072 for (binz=firstz;binz<=lastz;binz++) {
7074 for (biny=firsty;biny<=lasty;biny++) {
7076 for (binx=firstx;binx<=lastx;binx++) {
7077 bin = GetBin(binx,biny,binz);
7079 w = RetrieveBinContent(bin);
7080 e = GetBinError(bin);
7081 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);
7082 else printf(" fSumw[%d][%d][%d]=%g, x=%g, y=%g, z=%g\n",binx,biny,binz,w,x,y,z);
7083 }
7084 }
7085 }
7086 }
7087}
7088
7089////////////////////////////////////////////////////////////////////////////////
7090/// Using the current bin info, recompute the arrays for contents and errors
7091
7092void TH1::Rebuild(Option_t *)
7093{
7094 SetBinsLength();
7095 if (fSumw2.fN) {
7097 }
7098}
7099
7100////////////////////////////////////////////////////////////////////////////////
7101/// Reset this histogram: contents, errors, etc.
7102/// \param[in] option
7103/// - if "ICE" is specified, resets only Integral, Contents and Errors.
7104/// - if "ICES" is specified, resets only Integral, Contents, Errors and Statistics
7105/// This option is used
7106/// - if "M" is specified, resets also Minimum and Maximum
7107
7109{
7110 // The option "ICE" is used when extending the histogram (in ExtendAxis, LabelInflate, etc..)
7111 // The option "ICES is used in combination with the buffer (see BufferEmpty and BufferFill)
7112
7113 TString opt = option;
7114 opt.ToUpper();
7115 fSumw2.Reset();
7116 if (fIntegral) {
7117 delete [] fIntegral;
7118 fIntegral = nullptr;
7119 }
7120
7121 if (opt.Contains("M")) {
7122 SetMinimum();
7123 SetMaximum();
7124 }
7125
7126 if (opt.Contains("ICE") && !opt.Contains("S")) return;
7127
7128 // Setting fBuffer[0] = 0 is like resetting the buffer but not deleting it
7129 // But what is the sense of calling BufferEmpty() ? For making the axes ?
7130 // BufferEmpty will update contents that later will be
7131 // reset in calling TH1D::Reset. For this we need to reset the stats afterwards
7132 // It may be needed for computing the axis limits....
7133 if (fBuffer) {BufferEmpty(); fBuffer[0] = 0;}
7134
7135 // need to reset also the statistics
7136 // (needs to be done after calling BufferEmpty() )
7137 fTsumw = 0;
7138 fTsumw2 = 0;
7139 fTsumwx = 0;
7140 fTsumwx2 = 0;
7141 fEntries = 0;
7142
7143 if (opt == "ICES") return;
7144
7145
7146 TObject *stats = fFunctions->FindObject("stats");
7148 //special logic to support the case where the same object is
7149 //added multiple times in fFunctions.
7150 //This case happens when the same object is added with different
7151 //drawing modes
7152 TObject *obj;
7153 while ((obj = fFunctions->First())) {
7154 while(fFunctions->Remove(obj)) { }
7155 delete obj;
7156 }
7157 if(stats) fFunctions->Add(stats);
7158 fContour.Set(0);
7159}
7160
7161////////////////////////////////////////////////////////////////////////////////
7162/// Save the histogram as .csv, .tsv or .txt. In case of any other extension, fall
7163/// back to TObject::SaveAs, which saves as a .C macro (but with the file name
7164/// extension specified by the user)
7165///
7166/// The Under/Overflow bins are also exported (as first and last lines)
7167/// The fist 2 columns are the lower and upper edges of the bins
7168/// Column 3 contains the bin contents
7169/// The last column contains the error in y. If errors are not present, the column
7170/// is left empty
7171///
7172/// The result can be immediately imported into Excel, gnuplot, Python or whatever,
7173/// without the needing to install pyroot, etc.
7174///
7175/// \param filename the name of the file where to store the histogram
7176/// \param option some tuning options
7177///
7178/// The file extension defines the delimiter used:
7179/// - `.csv` : comma
7180/// - `.tsv` : tab
7181/// - `.txt` : space
7182///
7183/// If option = "title" a title line is generated. If the y-axis has a title,
7184/// this title is displayed as column 3 name, otherwise, it shows "BinContent"
7185
7186void TH1::SaveAs(const char *filename, Option_t *option) const
7187{
7188 char del = '\0';
7189 TString ext = "";
7191 TString opt = option;
7192
7193 if (filename) {
7194 if (fname.EndsWith(".csv")) {
7195 del = ',';
7196 ext = "csv";
7197 } else if (fname.EndsWith(".tsv")) {
7198 del = '\t';
7199 ext = "tsv";
7200 } else if (fname.EndsWith(".txt")) {
7201 del = ' ';
7202 ext = "txt";
7203 }
7204 }
7205 if (!del) {
7207 return;
7208 }
7209 std::ofstream out;
7210 out.open(filename, std::ios::out);
7211 if (!out.good()) {
7212 Error("SaveAs", "cannot open file: %s", filename);
7213 return;
7214 }
7215 if (opt.Contains("title")) {
7216 if (std::strcmp(GetYaxis()->GetTitle(), "") == 0) {
7217 out << "# " << "BinLowEdge" << del << "BinUpEdge" << del
7218 << "BinContent"
7219 << del << "ey" << std::endl;
7220 } else {
7221 out << "# " << "BinLowEdge" << del << "BinUpEdge" << del << GetYaxis()->GetTitle() << del << "ey" << std::endl;
7222 }
7223 }
7224 if (fSumw2.fN) {
7225 for (Int_t i = 0; i < fNcells; ++i) { // loop on cells (bins including underflow / overflow)
7226 out << GetXaxis()->GetBinLowEdge(i) << del << GetXaxis()->GetBinUpEdge(i) << del << GetBinContent(i) << del
7227 << GetBinError(i) << std::endl;
7228 }
7229 } else {
7230 for (Int_t i = 0; i < fNcells; ++i) { // loop on cells (bins including underflow / overflow)
7231 out << GetXaxis()->GetBinLowEdge(i) << del << GetXaxis()->GetBinUpEdge(i) << del << GetBinContent(i) << del
7232 << std::endl;
7233 }
7234 }
7235 out.close();
7236 Info("SaveAs", "%s file: %s has been generated", ext.Data(), filename);
7237}
7238
7239////////////////////////////////////////////////////////////////////////////////
7240/// Provide variable name for histogram for saving as primitive
7241/// Histogram pointer has by default the histogram name with an incremental suffix.
7242/// If the histogram belongs to a graph or a stack the suffix is not added because
7243/// the graph and stack objects are not aware of this new name. Same thing if
7244/// the histogram is drawn with the option COLZ because the TPaletteAxis drawn
7245/// when this option is selected, does not know this new name either.
7246
7248{
7249 thread_local Int_t storeNumber = 0;
7250
7251 TString opt = option;
7252 opt.ToLower();
7253 TString histName = GetName();
7254 // for TProfile and TH2Poly also fDirectory should be tested
7255 if (!histName.Contains("Graph") && !histName.Contains("_stack_") && !opt.Contains("colz") &&
7256 (!testfdir || !fDirectory)) {
7257 storeNumber++;
7258 histName += "__";
7259 histName += storeNumber;
7260 }
7261 if (histName.IsNull())
7262 histName = "unnamed";
7263 return gInterpreter->MapCppName(histName);
7264}
7265
7266////////////////////////////////////////////////////////////////////////////////
7267/// Save primitive as a C++ statement(s) on output stream out
7268
7269void TH1::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
7270{
7271 // empty the buffer before if it exists
7272 if (fBuffer)
7273 BufferEmpty();
7274
7276
7279 SetName(hname);
7280
7281 out <<" \n";
7282
7283 // Check if the histogram has equidistant X bins or not. If not, we
7284 // create an array holding the bins.
7285 if (GetXaxis()->GetXbins()->fN && GetXaxis()->GetXbins()->fArray)
7286 sxaxis = SavePrimitiveVector(out, hname + "_x", GetXaxis()->GetXbins()->fN, GetXaxis()->GetXbins()->fArray);
7287 // If the histogram is 2 or 3 dimensional, check if the histogram
7288 // has equidistant Y bins or not. If not, we create an array
7289 // holding the bins.
7290 if (fDimension > 1 && GetYaxis()->GetXbins()->fN && GetYaxis()->GetXbins()->fArray)
7291 syaxis = SavePrimitiveVector(out, hname + "_y", GetYaxis()->GetXbins()->fN, GetYaxis()->GetXbins()->fArray);
7292 // IF the histogram is 3 dimensional, check if the histogram
7293 // has equidistant Z bins or not. If not, we create an array
7294 // holding the bins.
7295 if (fDimension > 2 && GetZaxis()->GetXbins()->fN && GetZaxis()->GetXbins()->fArray)
7296 szaxis = SavePrimitiveVector(out, hname + "_z", GetZaxis()->GetXbins()->fN, GetZaxis()->GetXbins()->fArray);
7297
7298 const auto old_precision{out.precision()};
7299 constexpr auto max_precision{std::numeric_limits<double>::digits10 + 1};
7300 out << std::setprecision(max_precision);
7301
7302 out << " " << ClassName() << " *" << hname << " = new " << ClassName() << "(\"" << hname << "\", \""
7303 << TString(GetTitle()).ReplaceSpecialCppChars() << "\", " << GetXaxis()->GetNbins();
7304 if (!sxaxis.IsNull())
7305 out << ", " << sxaxis << ".data()";
7306 else
7307 out << ", " << GetXaxis()->GetXmin() << ", " << GetXaxis()->GetXmax();
7308 if (fDimension > 1) {
7309 out << ", " << GetYaxis()->GetNbins();
7310 if (!syaxis.IsNull())
7311 out << ", " << syaxis << ".data()";
7312 else
7313 out << ", " << GetYaxis()->GetXmin() << ", " << GetYaxis()->GetXmax();
7314 }
7315 if (fDimension > 2) {
7316 out << ", " << GetZaxis()->GetNbins();
7317 if (!szaxis.IsNull())
7318 out << ", " << szaxis << ".data()";
7319 else
7320 out << ", " << GetZaxis()->GetXmin() << ", " << GetZaxis()->GetXmax();
7321 }
7322 out << ");\n";
7323
7325 Int_t numbins = 0, numerrors = 0;
7326
7327 std::vector<Double_t> content(fNcells), errors(save_errors ? fNcells : 0);
7328 for (Int_t bin = 0; bin < fNcells; bin++) {
7329 content[bin] = RetrieveBinContent(bin);
7330 if (content[bin])
7331 numbins++;
7332 if (save_errors) {
7333 errors[bin] = GetBinError(bin);
7334 if (errors[bin])
7335 numerrors++;
7336 }
7337 }
7338
7339 if ((numbins < 100) && (numerrors < 100)) {
7340 // in case of few non-empty bins store them as before
7341 for (Int_t bin = 0; bin < fNcells; bin++) {
7342 if (content[bin])
7343 out << " " << hname << "->SetBinContent(" << bin << "," << content[bin] << ");\n";
7344 }
7345 if (save_errors)
7346 for (Int_t bin = 0; bin < fNcells; bin++) {
7347 if (errors[bin])
7348 out << " " << hname << "->SetBinError(" << bin << "," << errors[bin] << ");\n";
7349 }
7350 } else {
7351 if (numbins > 0) {
7353 out << " for (Int_t bin = 0; bin < " << fNcells << "; bin++)\n";
7354 out << " if (" << vectname << "[bin])\n";
7355 out << " " << hname << "->SetBinContent(bin, " << vectname << "[bin]);\n";
7356 }
7357 if (numerrors > 0) {
7359 out << " for (Int_t bin = 0; bin < " << fNcells << "; bin++)\n";
7360 out << " if (" << vectname << "[bin])\n";
7361 out << " " << hname << "->SetBinError(bin, " << vectname << "[bin]);\n";
7362 }
7363 }
7364
7366 out << std::setprecision(old_precision);
7367 SetName(savedName.Data());
7368}
7369
7370////////////////////////////////////////////////////////////////////////////////
7371/// Helper function for the SavePrimitive functions from TH1
7372/// or classes derived from TH1, eg TProfile, TProfile2D.
7373
7374void TH1::SavePrimitiveHelp(std::ostream &out, const char *hname, Option_t *option /*= ""*/)
7375{
7376 if (TMath::Abs(GetBarOffset()) > 1e-5)
7377 out << " " << hname << "->SetBarOffset(" << GetBarOffset() << ");\n";
7378 if (TMath::Abs(GetBarWidth() - 1) > 1e-5)
7379 out << " " << hname << "->SetBarWidth(" << GetBarWidth() << ");\n";
7380 if (fMinimum != -1111)
7381 out << " " << hname << "->SetMinimum(" << fMinimum << ");\n";
7382 if (fMaximum != -1111)
7383 out << " " << hname << "->SetMaximum(" << fMaximum << ");\n";
7384 if (fNormFactor != 0)
7385 out << " " << hname << "->SetNormFactor(" << fNormFactor << ");\n";
7386 if (fEntries != 0)
7387 out << " " << hname << "->SetEntries(" << fEntries << ");\n";
7388 if (!fDirectory)
7389 out << " " << hname << "->SetDirectory(nullptr);\n";
7390 if (TestBit(kNoStats))
7391 out << " " << hname << "->SetStats(0);\n";
7392 if (fOption.Length() != 0)
7393 out << " " << hname << "->SetOption(\n" << TString(fOption).ReplaceSpecialCppChars() << "\");\n";
7394
7395 // save contour levels
7397 if (ncontours > 0) {
7399 if (TestBit(kUserContour)) {
7400 std::vector<Double_t> levels(ncontours);
7401 for (Int_t bin = 0; bin < ncontours; bin++)
7402 levels[bin] = GetContourLevel(bin);
7404 }
7405 out << " " << hname << "->SetContour(" << ncontours;
7406 if (!vectname.IsNull())
7407 out << ", " << vectname << ".data()";
7408 out << ");\n";
7409 }
7410
7412
7413 // save attributes
7414 SaveFillAttributes(out, hname, 0, 1001);
7415 SaveLineAttributes(out, hname, 1, 1, 1);
7416 SaveMarkerAttributes(out, hname, 1, 1, 1);
7417 fXaxis.SaveAttributes(out, hname, "->GetXaxis()");
7418 fYaxis.SaveAttributes(out, hname, "->GetYaxis()");
7419 fZaxis.SaveAttributes(out, hname, "->GetZaxis()");
7420
7422}
7423
7424////////////////////////////////////////////////////////////////////////////////
7425/// Save list of functions
7426/// Also can be used by TGraph classes
7427
7428void TH1::SavePrimitiveFunctions(std::ostream &out, const char *varname, TList *lst)
7429{
7430 thread_local Int_t funcNumber = 0;
7431
7432 TObjLink *lnk = lst ? lst->FirstLink() : nullptr;
7433 while (lnk) {
7434 auto obj = lnk->GetObject();
7435 obj->SavePrimitive(out, TString::Format("nodraw #%d\n", ++funcNumber).Data());
7436 TString objvarname = obj->GetName();
7438 if (obj->InheritsFrom(TF1::Class())) {
7440 objvarname = gInterpreter->MapCppName(objvarname);
7441 out << " " << objvarname << "->SetParent(" << varname << ");\n";
7442 } else if (obj->InheritsFrom("TPaveStats")) {
7443 objvarname = "ptstats";
7444 withopt = kFALSE; // pave stats preserve own draw options
7445 out << " " << objvarname << "->SetParent(" << varname << ");\n";
7446 } else if (obj->InheritsFrom("TPolyMarker")) {
7447 objvarname = "pmarker";
7448 }
7449
7450 out << " " << varname << "->GetListOfFunctions()->Add(" << objvarname;
7451 if (withopt)
7452 out << ",\"" << TString(lnk->GetOption()).ReplaceSpecialCppChars() << "\"";
7453 out << ");\n";
7454
7455 lnk = lnk->Next();
7456 }
7457}
7458
7459////////////////////////////////////////////////////////////////////////////////
7500 }
7501}
7502
7503////////////////////////////////////////////////////////////////////////////////
7504/// For axis = 1,2 or 3 returns the mean value of the histogram along
7505/// X,Y or Z axis.
7506///
7507/// For axis = 11, 12, 13 returns the standard error of the mean value
7508/// of the histogram along X, Y or Z axis
7509///
7510/// Note that the mean value/StdDev is computed using the bins in the currently
7511/// defined range (see TAxis::SetRange). By default the range includes
7512/// all bins from 1 to nbins included, excluding underflows and overflows.
7513/// To force the underflows and overflows in the computation, one must
7514/// call the static function TH1::StatOverflows(kTRUE) before filling
7515/// the histogram.
7516///
7517/// IMPORTANT NOTE: The returned value depends on how the histogram statistics
7518/// are calculated. By default, if no range has been set, the returned mean is
7519/// the (unbinned) one calculated at fill time. If a range has been set, however,
7520/// the mean is calculated using the bins in range, as described above; THIS
7521/// IS TRUE EVEN IF THE RANGE INCLUDES ALL BINS--use TAxis::SetRange(0, 0) to unset
7522/// the range. To ensure that the returned mean (and all other statistics) is
7523/// always that of the binned data stored in the histogram, call TH1::ResetStats.
7524/// See TH1::GetStats.
7525///
7526/// Return mean value of this histogram along the X axis.
7527
7528Double_t TH1::GetMean(Int_t axis) const
7529{
7530 if (axis<1 || (axis>3 && axis<11) || axis>13) return 0;
7532 for (Int_t i=4;i<kNstat;i++) stats[i] = 0;
7533 GetStats(stats);
7534 if (stats[0] == 0) return 0;
7535 if (axis<4){
7536 Int_t ax[3] = {2,4,7};
7537 return stats[ax[axis-1]]/stats[0];
7538 } else {
7539 // mean error = StdDev / sqrt( Neff )
7540 Double_t stddev = GetStdDev(axis-10);
7542 return ( neff > 0 ? stddev/TMath::Sqrt(neff) : 0. );
7543 }
7544}
7545
7546////////////////////////////////////////////////////////////////////////////////
7547/// Return standard error of mean of this histogram along the X axis.
7548///
7549/// Note that the mean value/StdDev is computed using the bins in the currently
7550/// defined range (see TAxis::SetRange). By default the range includes
7551/// all bins from 1 to nbins included, excluding underflows and overflows.
7552/// To force the underflows and overflows in the computation, one must
7553/// call the static function TH1::StatOverflows(kTRUE) before filling
7554/// the histogram.
7555///
7556/// Also note, that although the definition of standard error doesn't include the
7557/// assumption of normality, many uses of this feature implicitly assume it.
7558///
7559/// IMPORTANT NOTE: The returned value depends on how the histogram statistics
7560/// are calculated. By default, if no range has been set, the returned value is
7561/// the (unbinned) one calculated at fill time. If a range has been set, however,
7562/// the value is calculated using the bins in range, as described above; THIS
7563/// IS TRUE EVEN IF THE RANGE INCLUDES ALL BINS--use TAxis::SetRange(0, 0) to unset
7564/// the range. To ensure that the returned value (and all other statistics) is
7565/// always that of the binned data stored in the histogram, call TH1::ResetStats.
7566/// See TH1::GetStats.
7567
7569{
7570 return GetMean(axis+10);
7571}
7572
7573////////////////////////////////////////////////////////////////////////////////
7574/// Returns the Standard Deviation (Sigma).
7575/// The Sigma estimate is computed as
7576/// \f[
7577/// \sqrt{\frac{1}{N}(\sum(x_i-x_{mean})^2)}
7578/// \f]
7579/// For axis = 1,2 or 3 returns the Sigma value of the histogram along
7580/// X, Y or Z axis
7581/// For axis = 11, 12 or 13 returns the error of StdDev estimation along
7582/// X, Y or Z axis for Normal distribution
7583///
7584/// Note that the mean value/sigma is computed using the bins in the currently
7585/// defined range (see TAxis::SetRange). By default the range includes
7586/// all bins from 1 to nbins included, excluding underflows and overflows.
7587/// To force the underflows and overflows in the computation, one must
7588/// call the static function TH1::StatOverflows(kTRUE) before filling
7589/// the histogram.
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 standard
7593/// deviation is the (unbinned) one calculated at fill time. If a range has been
7594/// set, however, the standard deviation is calculated using the bins in range,
7595/// as described above; THIS IS TRUE EVEN IF THE RANGE INCLUDES ALL BINS--use
7596/// TAxis::SetRange(0, 0) to unset the range. To ensure that the returned standard
7597/// deviation (and all other statistics) is always that of the binned data stored
7598/// in the histogram, call TH1::ResetStats. See TH1::GetStats.
7599
7600Double_t TH1::GetStdDev(Int_t axis) const
7601{
7602 if (axis<1 || (axis>3 && axis<11) || axis>13) return 0;
7603
7605 for (Int_t i=4;i<kNstat;i++) stats[i] = 0;
7606 GetStats(stats);
7607 if (stats[0] == 0) return 0;
7608 Int_t ax[3] = {2,4,7};
7609 Int_t axm = ax[axis%10 - 1];
7610 x = stats[axm]/stats[0];
7611 // for negative stddev (e.g. when having negative weights) - return stdev=0
7612 stddev2 = TMath::Max( stats[axm+1]/stats[0] -x*x, 0.0 );
7613 if (axis<10)
7614 return TMath::Sqrt(stddev2);
7615 else {
7616 // The right formula for StdDev error depends on 4th momentum (see Kendall-Stuart Vol 1 pag 243)
7617 // formula valid for only gaussian distribution ( 4-th momentum = 3 * sigma^4 )
7619 return ( neff > 0 ? TMath::Sqrt(stddev2/(2*neff) ) : 0. );
7620 }
7621}
7622
7623////////////////////////////////////////////////////////////////////////////////
7624/// Return error of standard deviation estimation for Normal distribution
7625///
7626/// Note that the mean value/StdDev is computed using the bins in the currently
7627/// defined range (see TAxis::SetRange). By default the range includes
7628/// all bins from 1 to nbins included, excluding underflows and overflows.
7629/// To force the underflows and overflows in the computation, one must
7630/// call the static function TH1::StatOverflows(kTRUE) before filling
7631/// the histogram.
7632///
7633/// Value returned is standard deviation of sample standard deviation.
7634/// Note that it is an approximated value which is valid only in the case that the
7635/// original data distribution is Normal. The correct one would require
7636/// the 4-th momentum value, which cannot be accurately estimated from a histogram since
7637/// the x-information for all entries is not kept.
7638///
7639/// IMPORTANT NOTE: The returned value depends on how the histogram statistics
7640/// are calculated. By default, if no range has been set, the returned value is
7641/// the (unbinned) one calculated at fill time. If a range has been set, however,
7642/// the value is calculated using the bins in range, as described above; THIS
7643/// IS TRUE EVEN IF THE RANGE INCLUDES ALL BINS--use TAxis::SetRange(0, 0) to unset
7644/// the range. To ensure that the returned value (and all other statistics) is
7645/// always that of the binned data stored in the histogram, call TH1::ResetStats.
7646/// See TH1::GetStats.
7647
7649{
7650 return GetStdDev(axis+10);
7651}
7652
7653////////////////////////////////////////////////////////////////////////////////
7654/// - For axis = 1, 2 or 3 returns skewness of the histogram along x, y or z axis.
7655/// - For axis = 11, 12 or 13 returns the approximate standard error of skewness
7656/// of the histogram along x, y or z axis
7657///
7658///Note, that since third and fourth moment are not calculated
7659///at the fill time, skewness and its standard error are computed bin by bin
7660///
7661/// IMPORTANT NOTE: The returned value depends on how the histogram statistics
7662/// are calculated. See TH1::GetMean and TH1::GetStdDev.
7663
7665{
7666
7667 if (axis > 0 && axis <= 3){
7668
7669 Double_t mean = GetMean(axis);
7670 Double_t stddev = GetStdDev(axis);
7672
7679 // include underflow/overflow if TH1::StatOverflows(kTRUE) in case no range is set on the axis
7682 if (firstBinX == 1) firstBinX = 0;
7683 if (lastBinX == fXaxis.GetNbins() ) lastBinX += 1;
7684 }
7686 if (firstBinY == 1) firstBinY = 0;
7687 if (lastBinY == fYaxis.GetNbins() ) lastBinY += 1;
7688 }
7690 if (firstBinZ == 1) firstBinZ = 0;
7691 if (lastBinZ == fZaxis.GetNbins() ) lastBinZ += 1;
7692 }
7693 }
7694
7695 Double_t x = 0;
7696 Double_t sum=0;
7697 Double_t np=0;
7698 for (Int_t binx = firstBinX; binx <= lastBinX; binx++) {
7699 for (Int_t biny = firstBinY; biny <= lastBinY; biny++) {
7700 for (Int_t binz = firstBinZ; binz <= lastBinZ; binz++) {
7701 if (axis==1 ) x = fXaxis.GetBinCenter(binx);
7702 else if (axis==2 ) x = fYaxis.GetBinCenter(biny);
7703 else if (axis==3 ) x = fZaxis.GetBinCenter(binz);
7705 np+=w;
7706 sum+=w*(x-mean)*(x-mean)*(x-mean);
7707 }
7708 }
7709 }
7710 sum/=np*stddev3;
7711 return sum;
7712 }
7713 else if (axis > 10 && axis <= 13) {
7714 //compute standard error of skewness
7715 // assume parent normal distribution use formula from Kendall-Stuart, Vol 1 pag 243, second edition
7717 return ( neff > 0 ? TMath::Sqrt(6./neff ) : 0. );
7718 }
7719 else {
7720 Error("GetSkewness", "illegal value of parameter");
7721 return 0;
7722 }
7723}
7724
7725////////////////////////////////////////////////////////////////////////////////
7726/// - For axis =1, 2 or 3 returns kurtosis of the histogram along x, y or z axis.
7727/// Kurtosis(gaussian(0, 1)) = 0.
7728/// - For axis =11, 12 or 13 returns the approximate standard error of kurtosis
7729/// of the histogram along x, y or z axis
7730////
7731/// Note, that since third and fourth moment are not calculated
7732/// at the fill time, kurtosis and its standard error are computed bin by bin
7733///
7734/// IMPORTANT NOTE: The returned value depends on how the histogram statistics
7735/// are calculated. See TH1::GetMean and TH1::GetStdDev.
7736
7738{
7739 if (axis > 0 && axis <= 3){
7740
7741 Double_t mean = GetMean(axis);
7742 Double_t stddev = GetStdDev(axis);
7744
7751 // include underflow/overflow if TH1::StatOverflows(kTRUE) in case no range is set on the axis
7754 if (firstBinX == 1) firstBinX = 0;
7755 if (lastBinX == fXaxis.GetNbins() ) lastBinX += 1;
7756 }
7758 if (firstBinY == 1) firstBinY = 0;
7759 if (lastBinY == fYaxis.GetNbins() ) lastBinY += 1;
7760 }
7762 if (firstBinZ == 1) firstBinZ = 0;
7763 if (lastBinZ == fZaxis.GetNbins() ) lastBinZ += 1;
7764 }
7765 }
7766
7767 Double_t x = 0;
7768 Double_t sum=0;
7769 Double_t np=0;
7770 for (Int_t binx = firstBinX; binx <= lastBinX; binx++) {
7771 for (Int_t biny = firstBinY; biny <= lastBinY; biny++) {
7772 for (Int_t binz = firstBinZ; binz <= lastBinZ; binz++) {
7773 if (axis==1 ) x = fXaxis.GetBinCenter(binx);
7774 else if (axis==2 ) x = fYaxis.GetBinCenter(biny);
7775 else if (axis==3 ) x = fZaxis.GetBinCenter(binz);
7777 np+=w;
7778 sum+=w*(x-mean)*(x-mean)*(x-mean)*(x-mean);
7779 }
7780 }
7781 }
7782 sum/=(np*stddev4);
7783 return sum-3;
7784
7785 } else if (axis > 10 && axis <= 13) {
7786 //compute standard error of skewness
7787 // assume parent normal distribution use formula from Kendall-Stuart, Vol 1 pag 243, second edition
7789 return ( neff > 0 ? TMath::Sqrt(24./neff ) : 0. );
7790 }
7791 else {
7792 Error("GetKurtosis", "illegal value of parameter");
7793 return 0;
7794 }
7795}
7796
7797////////////////////////////////////////////////////////////////////////////////
7798/// fill the array stats from the contents of this histogram
7799/// The array stats must be correctly dimensioned in the calling program.
7800///
7801/// ~~~ {.cpp}
7802/// stats[0] = sumw
7803/// stats[1] = sumw2
7804/// stats[2] = sumwx
7805/// stats[3] = sumwx2
7806/// ~~~
7807///
7808/// If no axis-subrange is specified (via TAxis::SetRange), the array stats
7809/// is simply a copy of the statistics quantities computed at filling time.
7810/// If a sub-range is specified, the function recomputes these quantities
7811/// from the bin contents in the current axis range.
7812///
7813/// IMPORTANT NOTE: This means that the returned statistics are context-dependent.
7814/// If TAxis::kAxisRange, the returned statistics are dependent on the binning;
7815/// otherwise, they are a copy of the histogram statistics computed at fill time,
7816/// which are unbinned by default (calling TH1::ResetStats forces them to use
7817/// binned statistics). You can reset TAxis::kAxisRange using TAxis::SetRange(0, 0).
7818///
7819/// Note that the mean value/StdDev is computed using the bins in the currently
7820/// defined range (see TAxis::SetRange). By default the range includes
7821/// all bins from 1 to nbins included, excluding underflows and overflows.
7822/// To force the underflows and overflows in the computation, one must
7823/// call the static function TH1::StatOverflows(kTRUE) before filling
7824/// the histogram.
7825
7826void TH1::GetStats(Double_t *stats) const
7827{
7828 if (fBuffer) ((TH1*)this)->BufferEmpty();
7829
7830 // Loop on bins (possibly including underflows/overflows)
7831 Int_t bin, binx;
7832 Double_t w,err;
7833 Double_t x;
7834 // identify the case of labels with extension of axis range
7835 // in this case the statistics in x does not make any sense
7836 Bool_t labelHist = ((const_cast<TAxis&>(fXaxis)).GetLabels() && fXaxis.CanExtend() );
7837 // fTsumw == 0 && fEntries > 0 is a special case when uses SetBinContent or calls ResetStats before
7838 if ( (fTsumw == 0 && fEntries > 0) || fXaxis.TestBit(TAxis::kAxisRange) ) {
7839 for (bin=0;bin<4;bin++) stats[bin] = 0;
7840
7843 // include underflow/overflow if TH1::StatOverflows(kTRUE) in case no range is set on the axis
7845 if (firstBinX == 1) firstBinX = 0;
7846 if (lastBinX == fXaxis.GetNbins() ) lastBinX += 1;
7847 }
7848 for (binx = firstBinX; binx <= lastBinX; binx++) {
7850 //w = TMath::Abs(RetrieveBinContent(binx));
7851 // not sure what to do here if w < 0
7853 err = TMath::Abs(GetBinError(binx));
7854 stats[0] += w;
7855 stats[1] += err*err;
7856 // statistics in x makes sense only for not labels histograms
7857 if (!labelHist) {
7858 stats[2] += w*x;
7859 stats[3] += w*x*x;
7860 }
7861 }
7862 // if (stats[0] < 0) {
7863 // // in case total is negative do something ??
7864 // stats[0] = 0;
7865 // }
7866 } else {
7867 stats[0] = fTsumw;
7868 stats[1] = fTsumw2;
7869 stats[2] = fTsumwx;
7870 stats[3] = fTsumwx2;
7871 }
7872}
7873
7874////////////////////////////////////////////////////////////////////////////////
7875/// Replace current statistics with the values in array stats
7876
7878{
7879 fTsumw = stats[0];
7880 fTsumw2 = stats[1];
7881 fTsumwx = stats[2];
7882 fTsumwx2 = stats[3];
7883}
7884
7885////////////////////////////////////////////////////////////////////////////////
7886/// Reset the statistics including the number of entries
7887/// and replace with values calculated from bin content
7888///
7889/// The number of entries is set to the total bin content or (in case of weighted histogram)
7890/// to number of effective entries
7891///
7892/// \note By default, before calling this function, statistics are those
7893/// computed at fill time, which are unbinned. See TH1::GetStats.
7894
7895void TH1::ResetStats()
7896{
7897 Double_t stats[kNstat] = {0};
7898 fTsumw = 0;
7899 fEntries = 1; // to force re-calculation of the statistics in TH1::GetStats
7900 GetStats(stats);
7901 PutStats(stats);
7903 // use effective entries for weighted histograms: (sum_w) ^2 / sum_w2
7904 if (fSumw2.fN > 0 && fTsumw > 0 && stats[1] > 0 ) fEntries = stats[0]*stats[0]/ stats[1];
7905}
7906
7907////////////////////////////////////////////////////////////////////////////////
7908/// Return the sum of all weights
7909/// \param includeOverflow true to include under/overflows bins, false to exclude those.
7910/// \note Different from TH1::GetSumOfWeights, that always excludes those
7911
7913{
7914 if (fBuffer) const_cast<TH1*>(this)->BufferEmpty();
7915
7916 const Int_t start = (includeOverflow ? 0 : 1);
7917 const Int_t lastX = fXaxis.GetNbins() + (includeOverflow ? 1 : 0);
7918 const Int_t lastY = fYaxis.GetNbins() + (includeOverflow ? 1 : 0);
7919 const Int_t lastZ = fZaxis.GetNbins() + (includeOverflow ? 1 : 0);
7920 Double_t sum =0;
7921 for(auto binz = start; binz <= lastZ; binz++) {
7922 for(auto biny = start; biny <= lastY; biny++) {
7923 for(auto binx = start; binx <= lastX; binx++) {
7924 const auto bin = GetBin(binx, biny, binz);
7925 sum += RetrieveBinContent(bin);
7926 }
7927 }
7928 }
7929 return sum;
7930}
7931
7932////////////////////////////////////////////////////////////////////////////////
7933///Return integral of bin contents. Only bins in the bins range are considered.
7934///
7935/// By default the integral is computed as the sum of bin contents in the range.
7936/// if option "width" is specified, the integral is the sum of
7937/// the bin contents multiplied by the bin width in x.
7938
7940{
7942}
7943
7944////////////////////////////////////////////////////////////////////////////////
7945/// Return integral of bin contents in range [binx1,binx2].
7946///
7947/// By default the integral is computed as the sum of bin contents in the range.
7948/// if option "width" is specified, the integral is the sum of
7949/// the bin contents multiplied by the bin width in x.
7950
7952{
7953 double err = 0;
7954 return DoIntegral(binx1,binx2,0,-1,0,-1,err,option);
7955}
7956
7957////////////////////////////////////////////////////////////////////////////////
7958/// Return integral of bin contents in range [binx1,binx2] and its error.
7959///
7960/// By default the integral is computed as the sum of bin contents in the range.
7961/// if option "width" is specified, the integral is the sum of
7962/// the bin contents multiplied by the bin width in x.
7963/// the error is computed using error propagation from the bin errors assuming that
7964/// all the bins are uncorrelated
7965
7967{
7968 return DoIntegral(binx1,binx2,0,-1,0,-1,error,option,kTRUE);
7969}
7970
7971////////////////////////////////////////////////////////////////////////////////
7972/// Internal function compute integral and optionally the error between the limits
7973/// specified by the bin number values working for all histograms (1D, 2D and 3D)
7974
7976 Option_t *option, Bool_t doError) const
7977{
7978 if (fBuffer) ((TH1*)this)->BufferEmpty();
7979
7980 Int_t nx = GetNbinsX() + 2;
7981 if (binx1 < 0) binx1 = 0;
7982 if (binx2 >= nx || binx2 < binx1) binx2 = nx - 1;
7983
7984 if (GetDimension() > 1) {
7985 Int_t ny = GetNbinsY() + 2;
7986 if (biny1 < 0) biny1 = 0;
7987 if (biny2 >= ny || biny2 < biny1) biny2 = ny - 1;
7988 } else {
7989 biny1 = 0; biny2 = 0;
7990 }
7991
7992 if (GetDimension() > 2) {
7993 Int_t nz = GetNbinsZ() + 2;
7994 if (binz1 < 0) binz1 = 0;
7995 if (binz2 >= nz || binz2 < binz1) binz2 = nz - 1;
7996 } else {
7997 binz1 = 0; binz2 = 0;
7998 }
7999
8000 // - Loop on bins in specified range
8001 TString opt = option;
8002 opt.ToLower();
8004 if (opt.Contains("width")) width = kTRUE;
8005
8006
8007 Double_t dx = 1., dy = .1, dz =.1;
8008 Double_t integral = 0;
8009 Double_t igerr2 = 0;
8010 for (Int_t binx = binx1; binx <= binx2; ++binx) {
8011 if (width) dx = fXaxis.GetBinWidth(binx);
8012 for (Int_t biny = biny1; biny <= biny2; ++biny) {
8013 if (width) dy = fYaxis.GetBinWidth(biny);
8014 for (Int_t binz = binz1; binz <= binz2; ++binz) {
8015 Int_t bin = GetBin(binx, biny, binz);
8016 Double_t dv = 0.0;
8017 if (width) {
8019 dv = dx * dy * dz;
8020 integral += RetrieveBinContent(bin) * dv;
8021 } else {
8022 integral += RetrieveBinContent(bin);
8023 }
8024 if (doError) {
8025 if (width) igerr2 += GetBinErrorSqUnchecked(bin) * dv * dv;
8026 else igerr2 += GetBinErrorSqUnchecked(bin);
8027 }
8028 }
8029 }
8030 }
8031
8032 if (doError) error = TMath::Sqrt(igerr2);
8033 return integral;
8034}
8035
8036////////////////////////////////////////////////////////////////////////////////
8037/// Statistical test of compatibility in shape between
8038/// this histogram and h2, using the Anderson-Darling 2 sample test.
8039///
8040/// The AD 2 sample test formula are derived from the paper
8041/// F.W Scholz, M.A. Stephens "k-Sample Anderson-Darling Test".
8042///
8043/// The test is implemented in root in the ROOT::Math::GoFTest class
8044/// It is the same formula ( (6) in the paper), and also shown in
8045/// [this preprint](http://arxiv.org/pdf/0804.0380v1.pdf)
8046///
8047/// Binned data are considered as un-binned data
8048/// with identical observation happening in the bin center.
8049///
8050/// \param[in] h2 Pointer to 1D histogram
8051/// \param[in] option is a character string to specify options
8052/// - "D" Put out a line of "Debug" printout
8053/// - "T" Return the normalized A-D test statistic
8054///
8055/// - Note1: Underflow and overflow are not considered in the test
8056/// - Note2: The test works only for un-weighted histogram (i.e. representing counts)
8057/// - Note3: The histograms are not required to have the same X axis
8058/// - Note4: The test works only for 1-dimensional histograms
8059
8061{
8062 Double_t advalue = 0;
8064
8065 TString opt = option;
8066 opt.ToUpper();
8067 if (opt.Contains("D") ) {
8068 printf(" AndersonDarlingTest Prob = %g, AD TestStatistic = %g\n",pvalue,advalue);
8069 }
8070 if (opt.Contains("T") ) return advalue;
8071
8072 return pvalue;
8073}
8074
8075////////////////////////////////////////////////////////////////////////////////
8076/// Same function as above but returning also the test statistic value
8077
8079{
8080 if (GetDimension() != 1 || h2->GetDimension() != 1) {
8081 Error("AndersonDarlingTest","Histograms must be 1-D");
8082 return -1;
8083 }
8084
8085 // empty the buffer. Probably we could add as an unbinned test
8086 if (fBuffer) ((TH1*)this)->BufferEmpty();
8087
8088 // use the BinData class
8091
8092 ROOT::Fit::FillData(data1, this, nullptr);
8093 ROOT::Fit::FillData(data2, h2, nullptr);
8094
8095 double pvalue;
8097
8098 return pvalue;
8099}
8100
8101////////////////////////////////////////////////////////////////////////////////
8102/// Statistical test of compatibility in shape between
8103/// this histogram and h2, using Kolmogorov test.
8104/// Note that the KolmogorovTest (KS) test should in theory be used only for unbinned data
8105/// and not for binned data as in the case of the histogram (see NOTE 3 below).
8106/// So, before using this method blindly, read the NOTE 3.
8107///
8108/// Default: Ignore under- and overflow bins in comparison
8109///
8110/// \param[in] h2 histogram
8111/// \param[in] option is a character string to specify options
8112/// - "U" include Underflows in test (also for 2-dim)
8113/// - "O" include Overflows (also valid for 2-dim)
8114/// - "N" include comparison of normalizations
8115/// - "D" Put out a line of "Debug" printout
8116/// - "M" Return the Maximum Kolmogorov distance instead of prob
8117/// - "X" Run the pseudo experiments post-processor with the following procedure:
8118/// make pseudoexperiments based on random values from the parent distribution,
8119/// compare the KS distance of the pseudoexperiment to the parent
8120/// distribution, and count all the KS values above the value
8121/// obtained from the original data to Monte Carlo distribution.
8122/// The number of pseudo-experiments nEXPT is by default 1000, and
8123/// it can be changed by specifying the option as "X=number",
8124/// for example "X=10000" for 10000 toys.
8125/// The function returns the probability.
8126/// (thanks to Ben Kilminster to submit this procedure). Note that
8127/// this option "X" is much slower.
8128///
8129/// The returned function value is the probability of test
8130/// (much less than one means NOT compatible)
8131///
8132/// Code adapted by Rene Brun from original HBOOK routine HDIFF
8133///
8134/// NOTE1
8135/// A good description of the Kolmogorov test can be seen at:
8136/// http://www.itl.nist.gov/div898/handbook/eda/section3/eda35g.htm
8137///
8138/// NOTE2
8139/// see also alternative function TH1::Chi2Test
8140/// The Kolmogorov test is assumed to give better results than Chi2Test
8141/// in case of histograms with low statistics.
8142///
8143/// NOTE3 (Jan Conrad, Fred James)
8144/// "The returned value PROB is calculated such that it will be
8145/// uniformly distributed between zero and one for compatible histograms,
8146/// provided the data are not binned (or the number of bins is very large
8147/// compared with the number of events). Users who have access to unbinned
8148/// data and wish exact confidence levels should therefore not put their data
8149/// into histograms, but should call directly TMath::KolmogorovTest. On
8150/// the other hand, since TH1 is a convenient way of collecting data and
8151/// saving space, this function has been provided. However, the values of
8152/// PROB for binned data will be shifted slightly higher than expected,
8153/// depending on the effects of the binning. For example, when comparing two
8154/// uniform distributions of 500 events in 100 bins, the values of PROB,
8155/// instead of being exactly uniformly distributed between zero and one, have
8156/// a mean value of about 0.56. We can apply a useful
8157/// rule: As long as the bin width is small compared with any significant
8158/// physical effect (for example the experimental resolution) then the binning
8159/// cannot have an important effect. Therefore, we believe that for all
8160/// practical purposes, the probability value PROB is calculated correctly
8161/// provided the user is aware that:
8162///
8163/// 1. The value of PROB should not be expected to have exactly the correct
8164/// distribution for binned data.
8165/// 2. The user is responsible for seeing to it that the bin widths are
8166/// small compared with any physical phenomena of interest.
8167/// 3. The effect of binning (if any) is always to make the value of PROB
8168/// slightly too big. That is, setting an acceptance criterion of (PROB>0.05
8169/// will assure that at most 5% of truly compatible histograms are rejected,
8170/// and usually somewhat less."
8171///
8172/// Note also that for GoF test of unbinned data ROOT provides also the class
8173/// ROOT::Math::GoFTest. The class has also method for doing one sample tests
8174/// (i.e. comparing the data with a given distribution).
8175
8177{
8178 TString opt = option;
8179 opt.ToUpper();
8180
8181 Double_t prob = 0;
8182 TH1 *h1 = (TH1*)this;
8183 if (h2 == nullptr) return 0;
8184 const TAxis *axis1 = h1->GetXaxis();
8185 const TAxis *axis2 = h2->GetXaxis();
8186 Int_t ncx1 = axis1->GetNbins();
8187 Int_t ncx2 = axis2->GetNbins();
8188
8189 // Check consistency of dimensions
8190 if (h1->GetDimension() != 1 || h2->GetDimension() != 1) {
8191 Error("KolmogorovTest","Histograms must be 1-D\n");
8192 return 0;
8193 }
8194
8195 // Check consistency in number of channels
8196 if (ncx1 != ncx2) {
8197 Error("KolmogorovTest","Histograms have different number of bins, %d and %d\n",ncx1,ncx2);
8198 return 0;
8199 }
8200
8201 // empty the buffer. Probably we could add as an unbinned test
8202 if (fBuffer) ((TH1*)this)->BufferEmpty();
8203
8204 // Check consistency in bin edges
8205 for(Int_t i = 1; i <= axis1->GetNbins() + 1; ++i) {
8206 if(!TMath::AreEqualRel(axis1->GetBinLowEdge(i), axis2->GetBinLowEdge(i), 1.E-15)) {
8207 Error("KolmogorovTest","Histograms are not consistent: they have different bin edges");
8208 return 0;
8209 }
8210 }
8211
8214 Double_t sum1 = 0, sum2 = 0;
8215 Double_t ew1, ew2, w1 = 0, w2 = 0;
8216 Int_t bin;
8217 Int_t ifirst = 1;
8218 Int_t ilast = ncx1;
8219 // integral of all bins (use underflow/overflow if option)
8220 if (opt.Contains("U")) ifirst = 0;
8221 if (opt.Contains("O")) ilast = ncx1 +1;
8222 for (bin = ifirst; bin <= ilast; bin++) {
8223 sum1 += h1->RetrieveBinContent(bin);
8224 sum2 += h2->RetrieveBinContent(bin);
8225 ew1 = h1->GetBinError(bin);
8226 ew2 = h2->GetBinError(bin);
8227 w1 += ew1*ew1;
8228 w2 += ew2*ew2;
8229 }
8230 if (sum1 == 0) {
8231 Error("KolmogorovTest","Histogram1 %s integral is zero\n",h1->GetName());
8232 return 0;
8233 }
8234 if (sum2 == 0) {
8235 Error("KolmogorovTest","Histogram2 %s integral is zero\n",h2->GetName());
8236 return 0;
8237 }
8238
8239 // calculate the effective entries.
8240 // the case when errors are zero (w1 == 0 or w2 ==0) are equivalent to
8241 // compare to a function. In that case the rescaling is done only on sqrt(esum2) or sqrt(esum1)
8242 Double_t esum1 = 0, esum2 = 0;
8243 if (w1 > 0)
8244 esum1 = sum1 * sum1 / w1;
8245 else
8246 afunc1 = kTRUE; // use later for calculating z
8247
8248 if (w2 > 0)
8249 esum2 = sum2 * sum2 / w2;
8250 else
8251 afunc2 = kTRUE; // use later for calculating z
8252
8253 if (afunc2 && afunc1) {
8254 Error("KolmogorovTest","Errors are zero for both histograms\n");
8255 return 0;
8256 }
8257
8258
8259 Double_t s1 = 1/sum1;
8260 Double_t s2 = 1/sum2;
8261
8262 // Find largest difference for Kolmogorov Test
8263 Double_t dfmax =0, rsum1 = 0, rsum2 = 0;
8264
8265 for (bin=ifirst;bin<=ilast;bin++) {
8266 rsum1 += s1*h1->RetrieveBinContent(bin);
8267 rsum2 += s2*h2->RetrieveBinContent(bin);
8269 }
8270
8271 // Get Kolmogorov probability
8272 Double_t z, prb1=0, prb2=0, prb3=0;
8273
8274 // case h1 is exact (has zero errors)
8275 if (afunc1)
8276 z = dfmax*TMath::Sqrt(esum2);
8277 // case h2 has zero errors
8278 else if (afunc2)
8279 z = dfmax*TMath::Sqrt(esum1);
8280 else
8281 // for comparison between two data sets
8283
8285
8286 // option N to combine normalization makes sense if both afunc1 and afunc2 are false
8287 if (opt.Contains("N") && !(afunc1 || afunc2 ) ) {
8288 // Combine probabilities for shape and normalization,
8289 prb1 = prob;
8292 prb2 = TMath::Prob(chi2,1);
8293 // see Eadie et al., section 11.6.2
8294 if (prob > 0 && prb2 > 0) prob *= prb2*(1-TMath::Log(prob*prb2));
8295 else prob = 0;
8296 }
8297 // X option. Run Pseudo-experiments to determine NULL distribution of the
8298 // KS distance. We can find the probability from the number of pseudo-experiment that have a
8299 // KS distance larger than the one opbserved in the data.
8300 // We use the histogram with the largest statistics as a parent distribution for the NULL.
8301 // Note if one histogram has zero errors is considered as a function. In that case we use it
8302 // as parent distribution for the toys.
8303 //
8304 Int_t nEXPT = 1000;
8305 if (opt.Contains("X")) {
8306 // get number of pseudo-experiment of specified
8307 if (opt.Contains("X=")) {
8308 int numpos = opt.Index("X=") + 2; // 2 is length of X=
8309 int numlen = 0;
8310 int len = opt.Length();
8311 while( (numpos+numlen<len) && isdigit(opt[numpos+numlen]) )
8312 numlen++;
8313 TString snum = opt(numpos,numlen);
8314 int num = atoi(snum.Data());
8315 if (num <= 0)
8316 Warning("KolmogorovTest","invalid number of toys given: %d - use 1000",num);
8317 else
8318 nEXPT = num;
8319 }
8320
8322 TH1D hparent;
8323 // we cannot have afunc1 and func2 both True
8324 if (afunc1 || esum1 > esum2 ) h1->Copy(hparent);
8325 else h2->Copy(hparent);
8326
8327 // copy h1Expt from h1 and h2. It is just needed to get the correct binning
8328
8329
8330 if (hparent.GetMinimum() < 0.0) {
8331 // we need to create a new histogram
8332 // With negative bins we can't draw random samples in a meaningful way.
8333 Warning("KolmogorovTest", "Detected bins with negative weights, these have been ignored and output might be "
8334 "skewed. Reduce number of bins for histogram?");
8335 while (hparent.GetMinimum() < 0.0) {
8336 Int_t idx = hparent.GetMinimumBin();
8337 hparent.SetBinContent(idx, 0.0);
8338 }
8339 }
8340
8341 // make nEXPT experiments (this should be a parameter)
8342 prb3 = 0;
8343 TH1D h1Expt;
8344 h1->Copy(h1Expt);
8345 TH1D h2Expt;
8346 h1->Copy(h2Expt);
8347 // loop on pseudoexperients and generate the two histograms h1Expt and h2Expt according to the
8348 // parent distribution. In case the parent distribution is not an histogram but a function randomize only one
8349 // histogram
8350 for (Int_t i=0; i < nEXPT; i++) {
8351 if (!afunc1) {
8352 h1Expt.Reset();
8353 h1Expt.FillRandom(&hparent, (Int_t)esum1);
8354 }
8355 if (!afunc2) {
8356 h2Expt.Reset();
8357 h2Expt.FillRandom(&hparent, (Int_t)esum2);
8358 }
8359 // note we cannot have both afunc1 and afunc2 to be true
8360 if (afunc1)
8361 dSEXPT = hparent.KolmogorovTest(&h2Expt,"M");
8362 else if (afunc2)
8363 dSEXPT = hparent.KolmogorovTest(&h1Expt,"M");
8364 else
8365 dSEXPT = h1Expt.KolmogorovTest(&h2Expt,"M");
8366 // count number of cases toy KS distance (TS) is larger than oberved one
8367 if (dSEXPT>dfmax) prb3 += 1.0;
8368 }
8369 // compute p-value
8370 prb3 /= (Double_t)nEXPT;
8371 }
8372
8373
8374 // debug printout
8375 if (opt.Contains("D")) {
8376 printf(" Kolmo Prob h1 = %s, sum bin content =%g effective entries =%g\n",h1->GetName(),sum1,esum1);
8377 printf(" Kolmo Prob h2 = %s, sum bin content =%g effective entries =%g\n",h2->GetName(),sum2,esum2);
8378 printf(" Kolmo Prob = %g, Max Dist = %g\n",prob,dfmax);
8379 if (opt.Contains("N"))
8380 printf(" Kolmo Prob = %f for shape alone, =%f for normalisation alone\n",prb1,prb2);
8381 if (opt.Contains("X"))
8382 printf(" Kolmo Prob = %f with %d pseudo-experiments\n",prb3,nEXPT);
8383 }
8384 // This numerical error condition should never occur:
8385 if (TMath::Abs(rsum1-1) > 0.002) Warning("KolmogorovTest","Numerical problems with h1=%s\n",h1->GetName());
8386 if (TMath::Abs(rsum2-1) > 0.002) Warning("KolmogorovTest","Numerical problems with h2=%s\n",h2->GetName());
8387
8388 if(opt.Contains("M")) return dfmax;
8389 else if(opt.Contains("X")) return prb3;
8390 else return prob;
8391}
8392
8393////////////////////////////////////////////////////////////////////////////////
8394/// Replace bin contents by the contents of array content
8395
8396void TH1::SetContent(const Double_t *content)
8397{
8398 fEntries = fNcells;
8399 fTsumw = 0;
8400 for (Int_t i = 0; i < fNcells; ++i) UpdateBinContent(i, content[i]);
8401}
8402
8403////////////////////////////////////////////////////////////////////////////////
8404/// Return contour values into array levels if pointer levels is non zero.
8405///
8406/// The function returns the number of contour levels.
8407/// see GetContourLevel to return one contour only
8408
8410{
8412 if (levels) {
8413 if (nlevels == 0) {
8414 nlevels = 20;
8416 } else {
8418 }
8419 for (Int_t level=0; level<nlevels; level++) levels[level] = fContour.fArray[level];
8420 }
8421 return nlevels;
8422}
8423
8424////////////////////////////////////////////////////////////////////////////////
8425/// Return value of contour number level.
8426/// Use GetContour to return the array of all contour levels
8427
8429{
8430 return (level >= 0 && level < fContour.fN) ? fContour.fArray[level] : 0.0;
8431}
8432
8433////////////////////////////////////////////////////////////////////////////////
8434/// Return the value of contour number "level" in Pad coordinates.
8435/// ie: if the Pad is in log scale along Z it returns le log of the contour level
8436/// value. See GetContour to return the array of all contour levels
8437
8439{
8440 if (level <0 || level >= fContour.fN) return 0;
8441 Double_t zlevel = fContour.fArray[level];
8442
8443 // In case of user defined contours and Pad in log scale along Z,
8444 // fContour.fArray doesn't contain the log of the contour whereas it does
8445 // in case of equidistant contours.
8446 if (gPad && gPad->GetLogz() && TestBit(kUserContour)) {
8447 if (zlevel <= 0) return 0;
8449 }
8450 return zlevel;
8451}
8452
8453////////////////////////////////////////////////////////////////////////////////
8454/// Set the maximum number of entries to be kept in the buffer.
8455
8456void TH1::SetBuffer(Int_t buffersize, Option_t * /*option*/)
8457{
8458 if (fBuffer) {
8459 BufferEmpty();
8460 delete [] fBuffer;
8461 fBuffer = nullptr;
8462 }
8463 if (buffersize <= 0) {
8464 fBufferSize = 0;
8465 return;
8466 }
8467 if (buffersize < 100) buffersize = 100;
8470 memset(fBuffer, 0, sizeof(Double_t)*fBufferSize);
8471}
8472
8473////////////////////////////////////////////////////////////////////////////////
8474/// Set the number and values of contour levels.
8475///
8476/// By default the number of contour levels is set to 20. The contours values
8477/// in the array "levels" should be specified in increasing order.
8478///
8479/// if argument levels = 0 or missing, equidistant contours are computed
8480
8482{
8483 Int_t level;
8485 if (nlevels <=0 ) {
8486 fContour.Set(0);
8487 return;
8488 }
8490
8491 // - Contour levels are specified
8492 if (levels) {
8494 for (level=0; level<nlevels; level++) fContour.fArray[level] = levels[level];
8495 } else {
8496 // - contour levels are computed automatically as equidistant contours
8497 Double_t zmin = GetMinimum();
8498 Double_t zmax = GetMaximum();
8499 if ((zmin == zmax) && (zmin != 0)) {
8500 zmax += 0.01*TMath::Abs(zmax);
8501 zmin -= 0.01*TMath::Abs(zmin);
8502 }
8503 Double_t dz = (zmax-zmin)/Double_t(nlevels);
8504 if (gPad && gPad->GetLogz()) {
8505 if (zmax <= 0) return;
8506 if (zmin <= 0) zmin = 0.001*zmax;
8507 zmin = TMath::Log10(zmin);
8508 zmax = TMath::Log10(zmax);
8509 dz = (zmax-zmin)/Double_t(nlevels);
8510 }
8511 for (level=0; level<nlevels; level++) {
8512 fContour.fArray[level] = zmin + dz*Double_t(level);
8513 }
8514 }
8515}
8516
8517////////////////////////////////////////////////////////////////////////////////
8518/// Set value for one contour level.
8519
8521{
8522 if (level < 0 || level >= fContour.fN) return;
8524 fContour.fArray[level] = value;
8525}
8526
8527////////////////////////////////////////////////////////////////////////////////
8528/// Return maximum value smaller than maxval of bins in the range,
8529/// unless the value has been overridden by TH1::SetMaximum,
8530/// in which case it returns that value. This happens, for example,
8531/// when the histogram is drawn and the y or z axis limits are changed
8532///
8533/// To get the maximum value of bins in the histogram regardless of
8534/// whether the value has been overridden (using TH1::SetMaximum), use
8535///
8536/// ~~~ {.cpp}
8537/// h->GetBinContent(h->GetMaximumBin())
8538/// ~~~
8539///
8540/// TH1::GetMaximumBin can be used to get the location of the maximum
8541/// value.
8542
8544{
8545 if (fMaximum != -1111) return fMaximum;
8546
8547 // empty the buffer
8548 if (fBuffer) ((TH1*)this)->BufferEmpty();
8549
8550 Int_t bin, binx, biny, binz;
8551 Int_t xfirst = fXaxis.GetFirst();
8552 Int_t xlast = fXaxis.GetLast();
8553 Int_t yfirst = fYaxis.GetFirst();
8554 Int_t ylast = fYaxis.GetLast();
8555 Int_t zfirst = fZaxis.GetFirst();
8556 Int_t zlast = fZaxis.GetLast();
8558 for (binz=zfirst;binz<=zlast;binz++) {
8559 for (biny=yfirst;biny<=ylast;biny++) {
8560 for (binx=xfirst;binx<=xlast;binx++) {
8561 bin = GetBin(binx,biny,binz);
8563 if (value > maximum && value < maxval) maximum = value;
8564 }
8565 }
8566 }
8567 return maximum;
8568}
8569
8570////////////////////////////////////////////////////////////////////////////////
8571/// Return location of bin with maximum value in the range.
8572///
8573/// TH1::GetMaximum can be used to get the maximum value.
8574
8576{
8579}
8580
8581////////////////////////////////////////////////////////////////////////////////
8582/// Return location of bin with maximum value in the range.
8583
8585{
8586 // empty the buffer
8587 if (fBuffer) ((TH1*)this)->BufferEmpty();
8588
8589 Int_t bin, binx, biny, binz;
8590 Int_t locm;
8591 Int_t xfirst = fXaxis.GetFirst();
8592 Int_t xlast = fXaxis.GetLast();
8593 Int_t yfirst = fYaxis.GetFirst();
8594 Int_t ylast = fYaxis.GetLast();
8595 Int_t zfirst = fZaxis.GetFirst();
8596 Int_t zlast = fZaxis.GetLast();
8598 locm = locmax = locmay = locmaz = 0;
8599 for (binz=zfirst;binz<=zlast;binz++) {
8600 for (biny=yfirst;biny<=ylast;biny++) {
8601 for (binx=xfirst;binx<=xlast;binx++) {
8602 bin = GetBin(binx,biny,binz);
8604 if (value > maximum) {
8605 maximum = value;
8606 locm = bin;
8607 locmax = binx;
8608 locmay = biny;
8609 locmaz = binz;
8610 }
8611 }
8612 }
8613 }
8614 return locm;
8615}
8616
8617////////////////////////////////////////////////////////////////////////////////
8618/// Return minimum value larger than minval of bins in the range,
8619/// unless the value has been overridden by TH1::SetMinimum,
8620/// in which case it returns that value. This happens, for example,
8621/// when the histogram is drawn and the y or z axis limits are changed
8622///
8623/// To get the minimum value of bins in the histogram regardless of
8624/// whether the value has been overridden (using TH1::SetMinimum), use
8625///
8626/// ~~~ {.cpp}
8627/// h->GetBinContent(h->GetMinimumBin())
8628/// ~~~
8629///
8630/// TH1::GetMinimumBin can be used to get the location of the
8631/// minimum value.
8632
8634{
8635 if (fMinimum != -1111) return fMinimum;
8636
8637 // empty the buffer
8638 if (fBuffer) ((TH1*)this)->BufferEmpty();
8639
8640 Int_t bin, binx, biny, binz;
8641 Int_t xfirst = fXaxis.GetFirst();
8642 Int_t xlast = fXaxis.GetLast();
8643 Int_t yfirst = fYaxis.GetFirst();
8644 Int_t ylast = fYaxis.GetLast();
8645 Int_t zfirst = fZaxis.GetFirst();
8646 Int_t zlast = fZaxis.GetLast();
8648 for (binz=zfirst;binz<=zlast;binz++) {
8649 for (biny=yfirst;biny<=ylast;biny++) {
8650 for (binx=xfirst;binx<=xlast;binx++) {
8651 bin = GetBin(binx,biny,binz);
8654 }
8655 }
8656 }
8657 return minimum;
8658}
8659
8660////////////////////////////////////////////////////////////////////////////////
8661/// Return location of bin with minimum value in the range.
8662
8664{
8667}
8668
8669////////////////////////////////////////////////////////////////////////////////
8670/// Return location of bin with minimum value in the range.
8671
8673{
8674 // empty the buffer
8675 if (fBuffer) ((TH1*)this)->BufferEmpty();
8676
8677 Int_t bin, binx, biny, binz;
8678 Int_t locm;
8679 Int_t xfirst = fXaxis.GetFirst();
8680 Int_t xlast = fXaxis.GetLast();
8681 Int_t yfirst = fYaxis.GetFirst();
8682 Int_t ylast = fYaxis.GetLast();
8683 Int_t zfirst = fZaxis.GetFirst();
8684 Int_t zlast = fZaxis.GetLast();
8686 locm = locmix = locmiy = locmiz = 0;
8687 for (binz=zfirst;binz<=zlast;binz++) {
8688 for (biny=yfirst;biny<=ylast;biny++) {
8689 for (binx=xfirst;binx<=xlast;binx++) {
8690 bin = GetBin(binx,biny,binz);
8692 if (value < minimum) {
8693 minimum = value;
8694 locm = bin;
8695 locmix = binx;
8696 locmiy = biny;
8697 locmiz = binz;
8698 }
8699 }
8700 }
8701 }
8702 return locm;
8703}
8704
8705///////////////////////////////////////////////////////////////////////////////
8706/// Retrieve the minimum and maximum values in the histogram
8707///
8708/// This will not return a cached value and will always search the
8709/// histogram for the min and max values. The user can condition whether
8710/// or not to call this with the GetMinimumStored() and GetMaximumStored()
8711/// methods. If the cache is empty, then the value will be -1111. Users
8712/// can then use the SetMinimum() or SetMaximum() methods to cache the results.
8713/// For example, the following recipe will make efficient use of this method
8714/// and the cached minimum and maximum values.
8715//
8716/// \code{.cpp}
8717/// Double_t currentMin = pHist->GetMinimumStored();
8718/// Double_t currentMax = pHist->GetMaximumStored();
8719/// if ((currentMin == -1111) || (currentMax == -1111)) {
8720/// pHist->GetMinimumAndMaximum(currentMin, currentMax);
8721/// pHist->SetMinimum(currentMin);
8722/// pHist->SetMaximum(currentMax);
8723/// }
8724/// \endcode
8725///
8726/// \param min reference to variable that will hold found minimum value
8727/// \param max reference to variable that will hold found maximum value
8728
8729void TH1::GetMinimumAndMaximum(Double_t& min, Double_t& max) const
8730{
8731 // empty the buffer
8732 if (fBuffer) ((TH1*)this)->BufferEmpty();
8733
8734 Int_t bin, binx, biny, binz;
8735 Int_t xfirst = fXaxis.GetFirst();
8736 Int_t xlast = fXaxis.GetLast();
8737 Int_t yfirst = fYaxis.GetFirst();
8738 Int_t ylast = fYaxis.GetLast();
8739 Int_t zfirst = fZaxis.GetFirst();
8740 Int_t zlast = fZaxis.GetLast();
8741 min=TMath::Infinity();
8742 max=-TMath::Infinity();
8744 for (binz=zfirst;binz<=zlast;binz++) {
8745 for (biny=yfirst;biny<=ylast;biny++) {
8746 for (binx=xfirst;binx<=xlast;binx++) {
8747 bin = GetBin(binx,biny,binz);
8749 if (value < min) min = value;
8750 if (value > max) max = value;
8751 }
8752 }
8753 }
8754}
8755
8756////////////////////////////////////////////////////////////////////////////////
8757/// Redefine x axis parameters.
8758///
8759/// The X axis parameters are modified.
8760/// The bins content array is resized
8761/// if errors (Sumw2) the errors array is resized
8762/// The previous bin contents are lost
8763/// To change only the axis limits, see TAxis::SetRange
8764
8766{
8767 if (GetDimension() != 1) {
8768 Error("SetBins","Operation only valid for 1-d histograms");
8769 return;
8770 }
8771 fXaxis.SetRange(0,0);
8773 fYaxis.Set(1,0,1);
8774 fZaxis.Set(1,0,1);
8775 fNcells = nx+2;
8777 if (fSumw2.fN) {
8779 }
8780}
8781
8782////////////////////////////////////////////////////////////////////////////////
8783/// Redefine x axis parameters with variable bin sizes.
8784///
8785/// The X axis parameters are modified.
8786/// The bins content array is resized
8787/// if errors (Sumw2) the errors array is resized
8788/// The previous bin contents are lost
8789/// To change only the axis limits, see TAxis::SetRange
8790/// xBins is supposed to be of length nx+1
8791
8792void TH1::SetBins(Int_t nx, const Double_t *xBins)
8793{
8794 if (GetDimension() != 1) {
8795 Error("SetBins","Operation only valid for 1-d histograms");
8796 return;
8797 }
8798 fXaxis.SetRange(0,0);
8799 fXaxis.Set(nx,xBins);
8800 fYaxis.Set(1,0,1);
8801 fZaxis.Set(1,0,1);
8802 fNcells = nx+2;
8804 if (fSumw2.fN) {
8806 }
8807}
8808
8809////////////////////////////////////////////////////////////////////////////////
8810/// Redefine x and y axis parameters.
8811///
8812/// The X and Y axis parameters are modified.
8813/// The bins content array is resized
8814/// if errors (Sumw2) the errors array is resized
8815/// The previous bin contents are lost
8816/// To change only the axis limits, see TAxis::SetRange
8817
8819{
8820 if (GetDimension() != 2) {
8821 Error("SetBins","Operation only valid for 2-D histograms");
8822 return;
8823 }
8824 fXaxis.SetRange(0,0);
8825 fYaxis.SetRange(0,0);
8828 fZaxis.Set(1,0,1);
8829 fNcells = (nx+2)*(ny+2);
8831 if (fSumw2.fN) {
8833 }
8834}
8835
8836////////////////////////////////////////////////////////////////////////////////
8837/// Redefine x and y axis parameters with variable bin sizes.
8838///
8839/// The X and Y axis parameters are modified.
8840/// The bins content array is resized
8841/// if errors (Sumw2) the errors array is resized
8842/// The previous bin contents are lost
8843/// To change only the axis limits, see TAxis::SetRange
8844/// xBins is supposed to be of length nx+1, yBins is supposed to be of length ny+1
8845
8846void TH1::SetBins(Int_t nx, const Double_t *xBins, Int_t ny, const Double_t *yBins)
8847{
8848 if (GetDimension() != 2) {
8849 Error("SetBins","Operation only valid for 2-D histograms");
8850 return;
8851 }
8852 fXaxis.SetRange(0,0);
8853 fYaxis.SetRange(0,0);
8854 fXaxis.Set(nx,xBins);
8855 fYaxis.Set(ny,yBins);
8856 fZaxis.Set(1,0,1);
8857 fNcells = (nx+2)*(ny+2);
8859 if (fSumw2.fN) {
8861 }
8862}
8863
8864////////////////////////////////////////////////////////////////////////////////
8865/// Redefine x, y and z axis parameters.
8866///
8867/// The X, Y and Z axis parameters are modified.
8868/// The bins content array is resized
8869/// if errors (Sumw2) the errors array is resized
8870/// The previous bin contents are lost
8871/// To change only the axis limits, see TAxis::SetRange
8872
8874{
8875 if (GetDimension() != 3) {
8876 Error("SetBins","Operation only valid for 3-D histograms");
8877 return;
8878 }
8879 fXaxis.SetRange(0,0);
8880 fYaxis.SetRange(0,0);
8881 fZaxis.SetRange(0,0);
8884 fZaxis.Set(nz,zmin,zmax);
8885 fNcells = (nx+2)*(ny+2)*(nz+2);
8887 if (fSumw2.fN) {
8889 }
8890}
8891
8892////////////////////////////////////////////////////////////////////////////////
8893/// Redefine x, y and z axis parameters with variable bin sizes.
8894///
8895/// The X, Y and Z axis parameters are modified.
8896/// The bins content array is resized
8897/// if errors (Sumw2) the errors array is resized
8898/// The previous bin contents are lost
8899/// To change only the axis limits, see TAxis::SetRange
8900/// xBins is supposed to be of length nx+1, yBins is supposed to be of length ny+1,
8901/// zBins is supposed to be of length nz+1
8902
8903void TH1::SetBins(Int_t nx, const Double_t *xBins, Int_t ny, const Double_t *yBins, Int_t nz, const Double_t *zBins)
8904{
8905 if (GetDimension() != 3) {
8906 Error("SetBins","Operation only valid for 3-D histograms");
8907 return;
8908 }
8909 fXaxis.SetRange(0,0);
8910 fYaxis.SetRange(0,0);
8911 fZaxis.SetRange(0,0);
8912 fXaxis.Set(nx,xBins);
8913 fYaxis.Set(ny,yBins);
8914 fZaxis.Set(nz,zBins);
8915 fNcells = (nx+2)*(ny+2)*(nz+2);
8917 if (fSumw2.fN) {
8919 }
8920}
8921
8922////////////////////////////////////////////////////////////////////////////////
8923/// By default, when a histogram is created, it is added to the list
8924/// of histogram objects in the current directory in memory.
8925/// Remove reference to this histogram from current directory and add
8926/// reference to new directory dir. dir can be 0 in which case the
8927/// histogram does not belong to any directory.
8928///
8929/// Note that the directory is not a real property of the histogram and
8930/// it will not be copied when the histogram is copied or cloned.
8931/// If the user wants to have the copied (cloned) histogram in the same
8932/// directory, he needs to set again the directory using SetDirectory to the
8933/// copied histograms
8934
8936{
8937 if (fDirectory == dir) return;
8938 if (fDirectory) fDirectory->Remove(this);
8939 fDirectory = dir;
8940 if (fDirectory) {
8942 fDirectory->Append(this);
8943 }
8944}
8945
8946////////////////////////////////////////////////////////////////////////////////
8947/// Replace bin errors by values in array error.
8948
8949void TH1::SetError(const Double_t *error)
8950{
8951 for (Int_t i = 0; i < fNcells; ++i) SetBinError(i, error[i]);
8952}
8953
8954////////////////////////////////////////////////////////////////////////////////
8955/// Change the name of this histogram
8957
8958void TH1::SetName(const char *name)
8959{
8960 // Histograms are named objects in a THashList.
8961 // We must update the hashlist if we change the name
8962 // We protect this operation
8964 if (fDirectory) fDirectory->Remove(this);
8965 fName = name;
8966 if (fDirectory) fDirectory->Append(this);
8967}
8968
8969////////////////////////////////////////////////////////////////////////////////
8970/// Change the name and title of this histogram
8971
8972void TH1::SetNameTitle(const char *name, const char *title)
8973{
8974 // Histograms are named objects in a THashList.
8975 // We must update the hashlist if we change the name
8976 SetName(name);
8977 SetTitle(title);
8978}
8979
8980////////////////////////////////////////////////////////////////////////////////
8981/// Set statistics option on/off.
8982///
8983/// By default, the statistics box is drawn.
8984/// The paint options can be selected via gStyle->SetOptStat.
8985/// This function sets/resets the kNoStats bit in the histogram object.
8986/// It has priority over the Style option.
8987
8989{
8991 if (!stats) {
8993 //remove the "stats" object from the list of functions
8994 if (fFunctions) {
8995 TObject *obj = fFunctions->FindObject("stats");
8996 if (obj) {
8997 fFunctions->Remove(obj);
8998 delete obj;
8999 }
9000 }
9001 }
9002}
9003
9004////////////////////////////////////////////////////////////////////////////////
9005/// Create structure to store sum of squares of weights.
9006///
9007/// if histogram is already filled, the sum of squares of weights
9008/// is filled with the existing bin contents
9009///
9010/// The error per bin will be computed as sqrt(sum of squares of weight)
9011/// for each bin.
9012///
9013/// This function is automatically called when the histogram is created
9014/// if the static function TH1::SetDefaultSumw2 has been called before.
9015/// If flag = false the structure containing the sum of the square of weights
9016/// is rest and it will be empty, but it is not deleted (i.e. GetSumw2()->fN = 0)
9017
9019{
9020 if (!flag) {
9021 // clear the array if existing - do nothing otherwise
9022 if (fSumw2.fN > 0 ) fSumw2.Set(0);
9023 return;
9024 }
9025
9026 if (fSumw2.fN == fNcells) {
9027 if (!fgDefaultSumw2 )
9028 Warning("Sumw2","Sum of squares of weights structure already created");
9029 return;
9030 }
9031
9033
9034 // empty the buffer
9035 if (fBuffer) BufferEmpty();
9036
9037 if (fEntries > 0)
9038 for (Int_t i = 0; i < fNcells; ++i)
9040}
9041
9042////////////////////////////////////////////////////////////////////////////////
9043/// Return pointer to function with name.
9044///
9045///
9046/// Functions such as TH1::Fit store the fitted function in the list of
9047/// functions of this histogram.
9048
9049TF1 *TH1::GetFunction(const char *name) const
9050{
9051 return (TF1*)fFunctions->FindObject(name);
9052}
9053
9054////////////////////////////////////////////////////////////////////////////////
9055/// Return value of error associated to bin number bin.
9056///
9057/// if the sum of squares of weights has been defined (via Sumw2),
9058/// this function returns the sqrt(sum of w2).
9059/// otherwise it returns the sqrt(contents) for this bin.
9060
9062{
9063 if (bin < 0) bin = 0;
9064 if (bin >= fNcells) bin = fNcells-1;
9065 if (fBuffer) ((TH1*)this)->BufferEmpty();
9066 if (fSumw2.fN) return TMath::Sqrt(fSumw2.fArray[bin]);
9067
9069}
9070
9071////////////////////////////////////////////////////////////////////////////////
9072/// Return lower error associated to bin number bin.
9073///
9074/// The error will depend on the statistic option used will return
9075/// the binContent - lower interval value
9076
9078{
9079 if (fBinStatErrOpt == kNormal) return GetBinError(bin);
9080 // in case of weighted histogram check if it is really weighted
9081 if (fSumw2.fN && fTsumw != fTsumw2) return GetBinError(bin);
9082
9083 if (bin < 0) bin = 0;
9084 if (bin >= fNcells) bin = fNcells-1;
9085 if (fBuffer) ((TH1*)this)->BufferEmpty();
9086
9087 Double_t alpha = 1.- 0.682689492;
9088 if (fBinStatErrOpt == kPoisson2) alpha = 0.05;
9089
9091 Int_t n = int(c);
9092 if (n < 0) {
9093 Warning("GetBinErrorLow","Histogram has negative bin content-force usage to normal errors");
9094 ((TH1*)this)->fBinStatErrOpt = kNormal;
9095 return GetBinError(bin);
9096 }
9097
9098 if (n == 0) return 0;
9099 return c - ROOT::Math::gamma_quantile( alpha/2, n, 1.);
9100}
9101
9102////////////////////////////////////////////////////////////////////////////////
9103/// Return upper error associated to bin number bin.
9104///
9105/// The error will depend on the statistic option used will return
9106/// the binContent - upper interval value
9107
9109{
9110 if (fBinStatErrOpt == kNormal) return GetBinError(bin);
9111 // in case of weighted histogram check if it is really weighted
9112 if (fSumw2.fN && fTsumw != fTsumw2) return GetBinError(bin);
9113 if (bin < 0) bin = 0;
9114 if (bin >= fNcells) bin = fNcells-1;
9115 if (fBuffer) ((TH1*)this)->BufferEmpty();
9116
9117 Double_t alpha = 1.- 0.682689492;
9118 if (fBinStatErrOpt == kPoisson2) alpha = 0.05;
9119
9121 Int_t n = int(c);
9122 if (n < 0) {
9123 Warning("GetBinErrorUp","Histogram has negative bin content-force usage to normal errors");
9124 ((TH1*)this)->fBinStatErrOpt = kNormal;
9125 return GetBinError(bin);
9126 }
9127
9128 // for N==0 return an upper limit at 0.68 or (1-alpha)/2 ?
9129 // decide to return always (1-alpha)/2 upper interval
9130 //if (n == 0) return ROOT::Math::gamma_quantile_c(alpha,n+1,1);
9131 return ROOT::Math::gamma_quantile_c( alpha/2, n+1, 1) - c;
9132}
9133
9134//L.M. These following getters are useless and should be probably deprecated
9135////////////////////////////////////////////////////////////////////////////////
9136/// Return bin center for 1D histogram.
9137/// Better to use h1.GetXaxis()->GetBinCenter(bin)
9138
9140{
9141 if (fDimension == 1) return fXaxis.GetBinCenter(bin);
9142 Error("GetBinCenter","Invalid method for a %d-d histogram - return a NaN",fDimension);
9143 return TMath::QuietNaN();
9144}
9145
9146////////////////////////////////////////////////////////////////////////////////
9147/// Return bin lower edge for 1D histogram.
9148/// Better to use h1.GetXaxis()->GetBinLowEdge(bin)
9149
9151{
9152 if (fDimension == 1) return fXaxis.GetBinLowEdge(bin);
9153 Error("GetBinLowEdge","Invalid method for a %d-d histogram - return a NaN",fDimension);
9154 return TMath::QuietNaN();
9155}
9156
9157////////////////////////////////////////////////////////////////////////////////
9158/// Return bin width for 1D histogram.
9159/// Better to use h1.GetXaxis()->GetBinWidth(bin)
9160
9162{
9163 if (fDimension == 1) return fXaxis.GetBinWidth(bin);
9164 Error("GetBinWidth","Invalid method for a %d-d histogram - return a NaN",fDimension);
9165 return TMath::QuietNaN();
9166}
9167
9168////////////////////////////////////////////////////////////////////////////////
9169/// Fill array with center of bins for 1D histogram
9170/// Better to use h1.GetXaxis()->GetCenter(center)
9171
9172void TH1::GetCenter(Double_t *center) const
9173{
9174 if (fDimension == 1) {
9175 fXaxis.GetCenter(center);
9176 return;
9177 }
9178 Error("GetCenter","Invalid method for a %d-d histogram ",fDimension);
9179}
9180
9181////////////////////////////////////////////////////////////////////////////////
9182/// Fill array with low edge of bins for 1D histogram
9183/// Better to use h1.GetXaxis()->GetLowEdge(edge)
9184
9185void TH1::GetLowEdge(Double_t *edge) const
9186{
9187 if (fDimension == 1) {
9189 return;
9190 }
9191 Error("GetLowEdge","Invalid method for a %d-d histogram ",fDimension);
9192}
9193
9194////////////////////////////////////////////////////////////////////////////////
9195/// Set the bin Error
9196/// Note that this resets the bin eror option to be of Normal Type and for the
9197/// non-empty bin the bin error is set by default to the square root of their content.
9198/// Note that in case the user sets after calling SetBinError explicitly a new bin content (e.g. using SetBinContent)
9199/// he needs then to provide also the corresponding bin error (using SetBinError) since the bin error
9200/// will not be recalculated after setting the content and a default error = 0 will be used for those bins.
9201///
9202/// See convention for numbering bins in TH1::GetBin
9203
9204void TH1::SetBinError(Int_t bin, Double_t error)
9205{
9206 if (bin < 0 || bin>= fNcells) return;
9207 if (!fSumw2.fN) Sumw2();
9208 fSumw2.fArray[bin] = error * error;
9209 // reset the bin error option
9211}
9212
9213////////////////////////////////////////////////////////////////////////////////
9214/// Set bin content
9215/// see convention for numbering bins in TH1::GetBin
9216/// In case the bin number is greater than the number of bins and
9217/// the timedisplay option is set or CanExtendAllAxes(),
9218/// the number of bins is automatically doubled to accommodate the new bin
9219
9221{
9222 fEntries++;
9223 fTsumw = 0;
9224 if (bin < 0) return;
9225 if (bin >= fNcells-1) {
9227 while (bin >= fNcells-1) LabelsInflate();
9228 } else {
9229 if (bin == fNcells-1) UpdateBinContent(bin, content);
9230 return;
9231 }
9232 }
9234}
9235
9236////////////////////////////////////////////////////////////////////////////////
9237/// See convention for numbering bins in TH1::GetBin
9238
9240{
9241 if (binx < 0 || binx > fXaxis.GetNbins() + 1) return;
9242 if (biny < 0 || biny > fYaxis.GetNbins() + 1) return;
9243 SetBinError(GetBin(binx, biny), error);
9244}
9245
9246////////////////////////////////////////////////////////////////////////////////
9247/// See convention for numbering bins in TH1::GetBin
9248
9250{
9251 if (binx < 0 || binx > fXaxis.GetNbins() + 1) return;
9252 if (biny < 0 || biny > fYaxis.GetNbins() + 1) return;
9253 if (binz < 0 || binz > fZaxis.GetNbins() + 1) return;
9254 SetBinError(GetBin(binx, biny, binz), error);
9255}
9256
9257////////////////////////////////////////////////////////////////////////////////
9258/// This function calculates the background spectrum in this histogram.
9259/// The background is returned as a histogram.
9260///
9261/// \param[in] niter number of iterations (default value = 2)
9262/// Increasing niter make the result smoother and lower.
9263/// \param[in] option may contain one of the following options
9264/// - to set the direction parameter
9265/// "BackDecreasingWindow". By default the direction is BackIncreasingWindow
9266/// - filterOrder-order of clipping filter (default "BackOrder2")
9267/// possible values= "BackOrder4" "BackOrder6" "BackOrder8"
9268/// - "nosmoothing" - if selected, the background is not smoothed
9269/// By default the background is smoothed.
9270/// - smoothWindow - width of smoothing window, (default is "BackSmoothing3")
9271/// possible values= "BackSmoothing5" "BackSmoothing7" "BackSmoothing9"
9272/// "BackSmoothing11" "BackSmoothing13" "BackSmoothing15"
9273/// - "nocompton" - if selected the estimation of Compton edge
9274/// will be not be included (by default the compton estimation is set)
9275/// - "same" if this option is specified, the resulting background
9276/// histogram is superimposed on the picture in the current pad.
9277/// This option is given by default.
9278///
9279/// NOTE that the background is only evaluated in the current range of this histogram.
9280/// i.e., if this has a bin range (set via h->GetXaxis()->SetRange(binmin, binmax),
9281/// the returned histogram will be created with the same number of bins
9282/// as this input histogram, but only bins from binmin to binmax will be filled
9283/// with the estimated background.
9284
9286{
9287 return (TH1*)gROOT->ProcessLineFast(TString::Format("TSpectrum::StaticBackground((TH1*)0x%zx,%d,\"%s\")",
9288 (size_t)this, niter, option).Data());
9289}
9290
9291////////////////////////////////////////////////////////////////////////////////
9292/// Interface to TSpectrum::Search.
9293/// The function finds peaks in this histogram where the width is > sigma
9294/// and the peak maximum greater than threshold*maximum bin content of this.
9295/// For more details see TSpectrum::Search.
9296/// Note the difference in the default value for option compared to TSpectrum::Search
9297/// option="" by default (instead of "goff").
9298
9300{
9301 return (Int_t)gROOT->ProcessLineFast(TString::Format("TSpectrum::StaticSearch((TH1*)0x%zx,%g,\"%s\",%g)",
9302 (size_t)this, sigma, option, threshold).Data());
9303}
9304
9305////////////////////////////////////////////////////////////////////////////////
9306/// For a given transform (first parameter), fills the histogram (second parameter)
9307/// with the transform output data, specified in the third parameter
9308/// If the 2nd parameter h_output is empty, a new histogram (TH1D or TH2D) is created
9309/// and the user is responsible for deleting it.
9310///
9311/// Available options:
9312/// - "RE" - real part of the output
9313/// - "IM" - imaginary part of the output
9314/// - "MAG" - magnitude of the output
9315/// - "PH" - phase of the output
9316
9318{
9319 if (!fft || !fft->GetN() ) {
9320 ::Error("TransformHisto","Invalid FFT transform class");
9321 return nullptr;
9322 }
9323
9324 if (fft->GetNdim()>2){
9325 ::Error("TransformHisto","Only 1d and 2D transform are supported");
9326 return nullptr;
9327 }
9328 Int_t binx,biny;
9329 TString opt = option;
9330 opt.ToUpper();
9331 Int_t *n = fft->GetN();
9332 TH1 *hout=nullptr;
9333 if (h_output) {
9334 hout = h_output;
9335 }
9336 else {
9337 TString name = TString::Format("out_%s", opt.Data());
9338 if (fft->GetNdim()==1)
9339 hout = new TH1D(name, name,n[0], 0, n[0]);
9340 else if (fft->GetNdim()==2)
9341 hout = new TH2D(name, name, n[0], 0, n[0], n[1], 0, n[1]);
9342 }
9343 R__ASSERT(hout != nullptr);
9344 TString type=fft->GetType();
9345 Int_t ind[2];
9346 if (opt.Contains("RE")){
9347 if (type.Contains("2C") || type.Contains("2HC")) {
9348 Double_t re, im;
9349 for (binx = 1; binx<=hout->GetNbinsX(); binx++) {
9350 for (biny=1; biny<=hout->GetNbinsY(); biny++) {
9351 ind[0] = binx-1; ind[1] = biny-1;
9352 fft->GetPointComplex(ind, re, im);
9353 hout->SetBinContent(binx, biny, re);
9354 }
9355 }
9356 } else {
9357 for (binx = 1; binx<=hout->GetNbinsX(); binx++) {
9358 for (biny=1; biny<=hout->GetNbinsY(); biny++) {
9359 ind[0] = binx-1; ind[1] = biny-1;
9360 hout->SetBinContent(binx, biny, fft->GetPointReal(ind));
9361 }
9362 }
9363 }
9364 }
9365 if (opt.Contains("IM")) {
9366 if (type.Contains("2C") || type.Contains("2HC")) {
9367 Double_t re, im;
9368 for (binx = 1; binx<=hout->GetNbinsX(); binx++) {
9369 for (biny=1; biny<=hout->GetNbinsY(); biny++) {
9370 ind[0] = binx-1; ind[1] = biny-1;
9371 fft->GetPointComplex(ind, re, im);
9372 hout->SetBinContent(binx, biny, im);
9373 }
9374 }
9375 } else {
9376 ::Error("TransformHisto","No complex numbers in the output");
9377 return nullptr;
9378 }
9379 }
9380 if (opt.Contains("MA")) {
9381 if (type.Contains("2C") || type.Contains("2HC")) {
9382 Double_t re, im;
9383 for (binx = 1; binx<=hout->GetNbinsX(); binx++) {
9384 for (biny=1; biny<=hout->GetNbinsY(); biny++) {
9385 ind[0] = binx-1; ind[1] = biny-1;
9386 fft->GetPointComplex(ind, re, im);
9387 hout->SetBinContent(binx, biny, TMath::Sqrt(re*re + im*im));
9388 }
9389 }
9390 } else {
9391 for (binx = 1; binx<=hout->GetNbinsX(); binx++) {
9392 for (biny=1; biny<=hout->GetNbinsY(); biny++) {
9393 ind[0] = binx-1; ind[1] = biny-1;
9394 hout->SetBinContent(binx, biny, TMath::Abs(fft->GetPointReal(ind)));
9395 }
9396 }
9397 }
9398 }
9399 if (opt.Contains("PH")) {
9400 if (type.Contains("2C") || type.Contains("2HC")){
9401 Double_t re, im, ph;
9402 for (binx = 1; binx<=hout->GetNbinsX(); binx++){
9403 for (biny=1; biny<=hout->GetNbinsY(); biny++){
9404 ind[0] = binx-1; ind[1] = biny-1;
9405 fft->GetPointComplex(ind, re, im);
9406 if (TMath::Abs(re) > 1e-13){
9407 ph = TMath::ATan(im/re);
9408 //find the correct quadrant
9409 if (re<0 && im<0)
9410 ph -= TMath::Pi();
9411 if (re<0 && im>=0)
9412 ph += TMath::Pi();
9413 } else {
9414 if (TMath::Abs(im) < 1e-13)
9415 ph = 0;
9416 else if (im>0)
9417 ph = TMath::Pi()*0.5;
9418 else
9419 ph = -TMath::Pi()*0.5;
9420 }
9421 hout->SetBinContent(binx, biny, ph);
9422 }
9423 }
9424 } else {
9425 printf("Pure real output, no phase");
9426 return nullptr;
9427 }
9428 }
9429
9430 return hout;
9431}
9432
9433////////////////////////////////////////////////////////////////////////////////
9434/// Print value overload
9435
9436std::string cling::printValue(TH1 *val) {
9437 std::ostringstream strm;
9438 strm << cling::printValue((TObject*)val) << " NbinsX: " << val->GetNbinsX();
9439 return strm.str();
9440}
9441
9442//______________________________________________________________________________
9443// TH1C methods
9444// TH1C : histograms with one byte per channel. Maximum bin content = 127
9445//______________________________________________________________________________
9446
9447ClassImp(TH1C);
9448
9449////////////////////////////////////////////////////////////////////////////////
9450/// Constructor.
9451
9452TH1C::TH1C()
9453{
9454 fDimension = 1;
9455 SetBinsLength(3);
9456 if (fgDefaultSumw2) Sumw2();
9457}
9458
9459////////////////////////////////////////////////////////////////////////////////
9460/// Create a 1-Dim histogram with fix bins of type char (one byte per channel)
9461/// (see TH1::TH1 for explanation of parameters)
9462
9463TH1C::TH1C(const char *name,const char *title,Int_t nbins,Double_t xlow,Double_t xup)
9464: TH1(name,title,nbins,xlow,xup)
9465{
9466 fDimension = 1;
9468
9469 if (xlow >= xup) SetBuffer(fgBufferSize);
9470 if (fgDefaultSumw2) Sumw2();
9471}
9472
9473////////////////////////////////////////////////////////////////////////////////
9474/// Create a 1-Dim histogram with variable bins of type char (one byte per channel)
9475/// (see TH1::TH1 for explanation of parameters)
9476
9477TH1C::TH1C(const char *name,const char *title,Int_t nbins,const Float_t *xbins)
9478: TH1(name,title,nbins,xbins)
9479{
9480 fDimension = 1;
9482 if (fgDefaultSumw2) Sumw2();
9483}
9484
9485////////////////////////////////////////////////////////////////////////////////
9486/// Create a 1-Dim histogram with variable bins of type char (one byte per channel)
9487/// (see TH1::TH1 for explanation of parameters)
9488
9489TH1C::TH1C(const char *name,const char *title,Int_t nbins,const Double_t *xbins)
9490: TH1(name,title,nbins,xbins)
9491{
9492 fDimension = 1;
9494 if (fgDefaultSumw2) Sumw2();
9495}
9496
9497////////////////////////////////////////////////////////////////////////////////
9498/// Destructor.
9499
9501{
9502}
9503
9504////////////////////////////////////////////////////////////////////////////////
9505/// Copy constructor.
9506/// The list of functions is not copied. (Use Clone() if needed)
9507
9508TH1C::TH1C(const TH1C &h1c) : TH1(), TArrayC()
9509{
9510 h1c.TH1C::Copy(*this);
9511}
9512
9513////////////////////////////////////////////////////////////////////////////////
9514/// Increment bin content by 1.
9515/// Passing an out-of-range bin leads to undefined behavior
9516
9517void TH1C::AddBinContent(Int_t bin)
9518{
9519 if (fArray[bin] < 127) fArray[bin]++;
9520}
9521
9522////////////////////////////////////////////////////////////////////////////////
9523/// Increment bin content by w.
9524/// \warning The value of w is cast to `Int_t` before being added.
9525/// Passing an out-of-range bin leads to undefined behavior
9526
9528{
9529 Int_t newval = fArray[bin] + Int_t(w);
9530 if (newval > -128 && newval < 128) {fArray[bin] = Char_t(newval); return;}
9531 if (newval < -127) fArray[bin] = -127;
9532 if (newval > 127) fArray[bin] = 127;
9533}
9534
9535////////////////////////////////////////////////////////////////////////////////
9536/// Copy this to newth1
9537
9538void TH1C::Copy(TObject &newth1) const
9539{
9541}
9542
9543////////////////////////////////////////////////////////////////////////////////
9544/// Reset.
9545
9547{
9550}
9551
9552////////////////////////////////////////////////////////////////////////////////
9553/// Set total number of bins including under/overflow
9554/// Reallocate bin contents array
9555
9557{
9558 if (n < 0) n = fXaxis.GetNbins() + 2;
9559 fNcells = n;
9560 TArrayC::Set(n);
9561}
9562
9563////////////////////////////////////////////////////////////////////////////////
9564/// Operator =
9565
9566TH1C& TH1C::operator=(const TH1C &h1)
9567{
9568 if (this != &h1)
9569 h1.TH1C::Copy(*this);
9570 return *this;
9571}
9572
9573////////////////////////////////////////////////////////////////////////////////
9574/// Operator *
9575
9577{
9578 TH1C hnew = h1;
9579 hnew.Scale(c1);
9580 hnew.SetDirectory(nullptr);
9581 return hnew;
9582}
9583
9584////////////////////////////////////////////////////////////////////////////////
9585/// Operator +
9586
9587TH1C operator+(const TH1C &h1, const TH1C &h2)
9588{
9589 TH1C hnew = h1;
9590 hnew.Add(&h2,1);
9591 hnew.SetDirectory(nullptr);
9592 return hnew;
9593}
9594
9595////////////////////////////////////////////////////////////////////////////////
9596/// Operator -
9597
9598TH1C operator-(const TH1C &h1, const TH1C &h2)
9599{
9600 TH1C hnew = h1;
9601 hnew.Add(&h2,-1);
9602 hnew.SetDirectory(nullptr);
9603 return hnew;
9604}
9605
9606////////////////////////////////////////////////////////////////////////////////
9607/// Operator *
9608
9609TH1C operator*(const TH1C &h1, const TH1C &h2)
9610{
9611 TH1C hnew = h1;
9612 hnew.Multiply(&h2);
9613 hnew.SetDirectory(nullptr);
9614 return hnew;
9615}
9616
9617////////////////////////////////////////////////////////////////////////////////
9618/// Operator /
9619
9620TH1C operator/(const TH1C &h1, const TH1C &h2)
9621{
9622 TH1C hnew = h1;
9623 hnew.Divide(&h2);
9624 hnew.SetDirectory(nullptr);
9625 return hnew;
9626}
9627
9628//______________________________________________________________________________
9629// TH1S methods
9630// TH1S : histograms with one short per channel. Maximum bin content = 32767
9631//______________________________________________________________________________
9632
9633ClassImp(TH1S);
9634
9635////////////////////////////////////////////////////////////////////////////////
9636/// Constructor.
9637
9638TH1S::TH1S()
9639{
9640 fDimension = 1;
9641 SetBinsLength(3);
9642 if (fgDefaultSumw2) Sumw2();
9643}
9644
9645////////////////////////////////////////////////////////////////////////////////
9646/// Create a 1-Dim histogram with fix bins of type short
9647/// (see TH1::TH1 for explanation of parameters)
9648
9649TH1S::TH1S(const char *name,const char *title,Int_t nbins,Double_t xlow,Double_t xup)
9650: TH1(name,title,nbins,xlow,xup)
9651{
9652 fDimension = 1;
9654
9655 if (xlow >= xup) SetBuffer(fgBufferSize);
9656 if (fgDefaultSumw2) Sumw2();
9657}
9658
9659////////////////////////////////////////////////////////////////////////////////
9660/// Create a 1-Dim histogram with variable bins of type short
9661/// (see TH1::TH1 for explanation of parameters)
9662
9663TH1S::TH1S(const char *name,const char *title,Int_t nbins,const Float_t *xbins)
9664: TH1(name,title,nbins,xbins)
9665{
9666 fDimension = 1;
9668 if (fgDefaultSumw2) Sumw2();
9669}
9670
9671////////////////////////////////////////////////////////////////////////////////
9672/// Create a 1-Dim histogram with variable bins of type short
9673/// (see TH1::TH1 for explanation of parameters)
9674
9675TH1S::TH1S(const char *name,const char *title,Int_t nbins,const Double_t *xbins)
9676: TH1(name,title,nbins,xbins)
9677{
9678 fDimension = 1;
9680 if (fgDefaultSumw2) Sumw2();
9681}
9682
9683////////////////////////////////////////////////////////////////////////////////
9684/// Destructor.
9685
9687{
9688}
9689
9690////////////////////////////////////////////////////////////////////////////////
9691/// Copy constructor.
9692/// The list of functions is not copied. (Use Clone() if needed)
9693
9694TH1S::TH1S(const TH1S &h1s) : TH1(), TArrayS()
9695{
9696 h1s.TH1S::Copy(*this);
9697}
9698
9699////////////////////////////////////////////////////////////////////////////////
9700/// Increment bin content by 1.
9701/// Passing an out-of-range bin leads to undefined behavior
9702
9703void TH1S::AddBinContent(Int_t bin)
9704{
9705 if (fArray[bin] < 32767) fArray[bin]++;
9706}
9707
9708////////////////////////////////////////////////////////////////////////////////
9709/// Increment bin content by w.
9710/// \warning The value of w is cast to `Int_t` before being added.
9711/// Passing an out-of-range bin leads to undefined behavior
9712
9714{
9715 Int_t newval = fArray[bin] + Int_t(w);
9716 if (newval > -32768 && newval < 32768) {fArray[bin] = Short_t(newval); return;}
9717 if (newval < -32767) fArray[bin] = -32767;
9718 if (newval > 32767) fArray[bin] = 32767;
9719}
9720
9721////////////////////////////////////////////////////////////////////////////////
9722/// Copy this to newth1
9723
9724void TH1S::Copy(TObject &newth1) const
9725{
9727}
9728
9729////////////////////////////////////////////////////////////////////////////////
9730/// Reset.
9731
9733{
9736}
9737
9738////////////////////////////////////////////////////////////////////////////////
9739/// Set total number of bins including under/overflow
9740/// Reallocate bin contents array
9741
9743{
9744 if (n < 0) n = fXaxis.GetNbins() + 2;
9745 fNcells = n;
9746 TArrayS::Set(n);
9747}
9748
9749////////////////////////////////////////////////////////////////////////////////
9750/// Operator =
9751
9752TH1S& TH1S::operator=(const TH1S &h1)
9753{
9754 if (this != &h1)
9755 h1.TH1S::Copy(*this);
9756 return *this;
9757}
9758
9759////////////////////////////////////////////////////////////////////////////////
9760/// Operator *
9761
9763{
9764 TH1S hnew = h1;
9765 hnew.Scale(c1);
9766 hnew.SetDirectory(nullptr);
9767 return hnew;
9768}
9769
9770////////////////////////////////////////////////////////////////////////////////
9771/// Operator +
9772
9773TH1S operator+(const TH1S &h1, const TH1S &h2)
9774{
9775 TH1S hnew = h1;
9776 hnew.Add(&h2,1);
9777 hnew.SetDirectory(nullptr);
9778 return hnew;
9779}
9780
9781////////////////////////////////////////////////////////////////////////////////
9782/// Operator -
9783
9784TH1S operator-(const TH1S &h1, const TH1S &h2)
9785{
9786 TH1S hnew = h1;
9787 hnew.Add(&h2,-1);
9788 hnew.SetDirectory(nullptr);
9789 return hnew;
9790}
9791
9792////////////////////////////////////////////////////////////////////////////////
9793/// Operator *
9794
9795TH1S operator*(const TH1S &h1, const TH1S &h2)
9796{
9797 TH1S hnew = h1;
9798 hnew.Multiply(&h2);
9799 hnew.SetDirectory(nullptr);
9800 return hnew;
9801}
9802
9803////////////////////////////////////////////////////////////////////////////////
9804/// Operator /
9805
9806TH1S operator/(const TH1S &h1, const TH1S &h2)
9807{
9808 TH1S hnew = h1;
9809 hnew.Divide(&h2);
9810 hnew.SetDirectory(nullptr);
9811 return hnew;
9812}
9813
9814//______________________________________________________________________________
9815// TH1I methods
9816// TH1I : histograms with one int per channel. Maximum bin content = 2147483647
9817// 2147483647 = INT_MAX
9818//______________________________________________________________________________
9819
9820ClassImp(TH1I);
9821
9822////////////////////////////////////////////////////////////////////////////////
9823/// Constructor.
9824
9825TH1I::TH1I()
9826{
9827 fDimension = 1;
9828 SetBinsLength(3);
9829 if (fgDefaultSumw2) Sumw2();
9830}
9831
9832////////////////////////////////////////////////////////////////////////////////
9833/// Create a 1-Dim histogram with fix bins of type integer
9834/// (see TH1::TH1 for explanation of parameters)
9835
9836TH1I::TH1I(const char *name,const char *title,Int_t nbins,Double_t xlow,Double_t xup)
9837: TH1(name,title,nbins,xlow,xup)
9838{
9839 fDimension = 1;
9841
9842 if (xlow >= xup) SetBuffer(fgBufferSize);
9843 if (fgDefaultSumw2) Sumw2();
9844}
9845
9846////////////////////////////////////////////////////////////////////////////////
9847/// Create a 1-Dim histogram with variable bins of type integer
9848/// (see TH1::TH1 for explanation of parameters)
9849
9850TH1I::TH1I(const char *name,const char *title,Int_t nbins,const Float_t *xbins)
9851: TH1(name,title,nbins,xbins)
9852{
9853 fDimension = 1;
9855 if (fgDefaultSumw2) Sumw2();
9856}
9857
9858////////////////////////////////////////////////////////////////////////////////
9859/// Create a 1-Dim histogram with variable bins of type integer
9860/// (see TH1::TH1 for explanation of parameters)
9861
9862TH1I::TH1I(const char *name,const char *title,Int_t nbins,const Double_t *xbins)
9863: TH1(name,title,nbins,xbins)
9864{
9865 fDimension = 1;
9867 if (fgDefaultSumw2) Sumw2();
9868}
9869
9870////////////////////////////////////////////////////////////////////////////////
9871/// Destructor.
9872
9874{
9875}
9876
9877////////////////////////////////////////////////////////////////////////////////
9878/// Copy constructor.
9879/// The list of functions is not copied. (Use Clone() if needed)
9880
9881TH1I::TH1I(const TH1I &h1i) : TH1(), TArrayI()
9882{
9883 h1i.TH1I::Copy(*this);
9884}
9885
9886////////////////////////////////////////////////////////////////////////////////
9887/// Increment bin content by 1.
9888/// Passing an out-of-range bin leads to undefined behavior
9889
9890void TH1I::AddBinContent(Int_t bin)
9891{
9892 if (fArray[bin] < INT_MAX) fArray[bin]++;
9893}
9894
9895////////////////////////////////////////////////////////////////////////////////
9896/// Increment bin content by w
9897/// \warning The value of w is cast to `Long64_t` before being added.
9898/// Passing an out-of-range bin leads to undefined behavior
9899
9901{
9902 Long64_t newval = fArray[bin] + Long64_t(w);
9903 if (newval > -INT_MAX && newval < INT_MAX) {fArray[bin] = Int_t(newval); return;}
9904 if (newval < -INT_MAX) fArray[bin] = -INT_MAX;
9905 if (newval > INT_MAX) fArray[bin] = INT_MAX;
9906}
9907
9908////////////////////////////////////////////////////////////////////////////////
9909/// Copy this to newth1
9910
9911void TH1I::Copy(TObject &newth1) const
9912{
9914}
9915
9916////////////////////////////////////////////////////////////////////////////////
9917/// Reset.
9918
9920{
9923}
9924
9925////////////////////////////////////////////////////////////////////////////////
9926/// Set total number of bins including under/overflow
9927/// Reallocate bin contents array
9928
9930{
9931 if (n < 0) n = fXaxis.GetNbins() + 2;
9932 fNcells = n;
9933 TArrayI::Set(n);
9934}
9935
9936////////////////////////////////////////////////////////////////////////////////
9937/// Operator =
9938
9939TH1I& TH1I::operator=(const TH1I &h1)
9940{
9941 if (this != &h1)
9942 h1.TH1I::Copy(*this);
9943 return *this;
9944}
9945
9946
9947////////////////////////////////////////////////////////////////////////////////
9948/// Operator *
9949
9951{
9952 TH1I hnew = h1;
9953 hnew.Scale(c1);
9954 hnew.SetDirectory(nullptr);
9955 return hnew;
9956}
9957
9958////////////////////////////////////////////////////////////////////////////////
9959/// Operator +
9960
9961TH1I operator+(const TH1I &h1, const TH1I &h2)
9962{
9963 TH1I hnew = h1;
9964 hnew.Add(&h2,1);
9965 hnew.SetDirectory(nullptr);
9966 return hnew;
9967}
9968
9969////////////////////////////////////////////////////////////////////////////////
9970/// Operator -
9971
9972TH1I operator-(const TH1I &h1, const TH1I &h2)
9973{
9974 TH1I hnew = h1;
9975 hnew.Add(&h2,-1);
9976 hnew.SetDirectory(nullptr);
9977 return hnew;
9978}
9979
9980////////////////////////////////////////////////////////////////////////////////
9981/// Operator *
9982
9983TH1I operator*(const TH1I &h1, const TH1I &h2)
9984{
9985 TH1I hnew = h1;
9986 hnew.Multiply(&h2);
9987 hnew.SetDirectory(nullptr);
9988 return hnew;
9989}
9990
9991////////////////////////////////////////////////////////////////////////////////
9992/// Operator /
9993
9994TH1I operator/(const TH1I &h1, const TH1I &h2)
9995{
9996 TH1I hnew = h1;
9997 hnew.Divide(&h2);
9998 hnew.SetDirectory(nullptr);
9999 return hnew;
10000}
10001
10002//______________________________________________________________________________
10003// TH1L methods
10004// TH1L : histograms with one long64 per channel. Maximum bin content = 9223372036854775807
10005// 9223372036854775807 = LLONG_MAX
10006//______________________________________________________________________________
10007
10008ClassImp(TH1L);
10009
10010////////////////////////////////////////////////////////////////////////////////
10011/// Constructor.
10012
10013TH1L::TH1L()
10014{
10015 fDimension = 1;
10016 SetBinsLength(3);
10017 if (fgDefaultSumw2) Sumw2();
10018}
10019
10020////////////////////////////////////////////////////////////////////////////////
10021/// Create a 1-Dim histogram with fix bins of type long64
10022/// (see TH1::TH1 for explanation of parameters)
10023
10024TH1L::TH1L(const char *name,const char *title,Int_t nbins,Double_t xlow,Double_t xup)
10025: TH1(name,title,nbins,xlow,xup)
10026{
10027 fDimension = 1;
10029
10030 if (xlow >= xup) SetBuffer(fgBufferSize);
10031 if (fgDefaultSumw2) Sumw2();
10032}
10033
10034////////////////////////////////////////////////////////////////////////////////
10035/// Create a 1-Dim histogram with variable bins of type long64
10036/// (see TH1::TH1 for explanation of parameters)
10037
10038TH1L::TH1L(const char *name,const char *title,Int_t nbins,const Float_t *xbins)
10039: TH1(name,title,nbins,xbins)
10040{
10041 fDimension = 1;
10043 if (fgDefaultSumw2) Sumw2();
10044}
10045
10046////////////////////////////////////////////////////////////////////////////////
10047/// Create a 1-Dim histogram with variable bins of type long64
10048/// (see TH1::TH1 for explanation of parameters)
10049
10050TH1L::TH1L(const char *name,const char *title,Int_t nbins,const Double_t *xbins)
10051: TH1(name,title,nbins,xbins)
10052{
10053 fDimension = 1;
10055 if (fgDefaultSumw2) Sumw2();
10056}
10057
10058////////////////////////////////////////////////////////////////////////////////
10059/// Destructor.
10060
10062{
10063}
10064
10065////////////////////////////////////////////////////////////////////////////////
10066/// Copy constructor.
10067/// The list of functions is not copied. (Use Clone() if needed)
10068
10069TH1L::TH1L(const TH1L &h1l) : TH1(), TArrayL64()
10070{
10071 h1l.TH1L::Copy(*this);
10072}
10073
10074////////////////////////////////////////////////////////////////////////////////
10075/// Increment bin content by 1.
10076/// Passing an out-of-range bin leads to undefined behavior
10077
10078void TH1L::AddBinContent(Int_t bin)
10079{
10080 if (fArray[bin] < LLONG_MAX) fArray[bin]++;
10081}
10082
10083////////////////////////////////////////////////////////////////////////////////
10084/// Increment bin content by w.
10085/// \warning The value of w is cast to `Long64_t` before being added.
10086/// Passing an out-of-range bin leads to undefined behavior
10087
10089{
10090 Long64_t newval = fArray[bin] + Long64_t(w);
10091 if (newval > -LLONG_MAX && newval < LLONG_MAX) {fArray[bin] = newval; return;}
10092 if (newval < -LLONG_MAX) fArray[bin] = -LLONG_MAX;
10093 if (newval > LLONG_MAX) fArray[bin] = LLONG_MAX;
10094}
10095
10096////////////////////////////////////////////////////////////////////////////////
10097/// Copy this to newth1
10098
10099void TH1L::Copy(TObject &newth1) const
10100{
10102}
10103
10104////////////////////////////////////////////////////////////////////////////////
10105/// Reset.
10106
10108{
10111}
10112
10113////////////////////////////////////////////////////////////////////////////////
10114/// Set total number of bins including under/overflow
10115/// Reallocate bin contents array
10116
10118{
10119 if (n < 0) n = fXaxis.GetNbins() + 2;
10120 fNcells = n;
10122}
10123
10124////////////////////////////////////////////////////////////////////////////////
10125/// Operator =
10126
10127TH1L& TH1L::operator=(const TH1L &h1)
10128{
10129 if (this != &h1)
10130 h1.TH1L::Copy(*this);
10131 return *this;
10132}
10133
10134
10135////////////////////////////////////////////////////////////////////////////////
10136/// Operator *
10137
10139{
10140 TH1L hnew = h1;
10141 hnew.Scale(c1);
10142 hnew.SetDirectory(nullptr);
10143 return hnew;
10144}
10145
10146////////////////////////////////////////////////////////////////////////////////
10147/// Operator +
10148
10149TH1L operator+(const TH1L &h1, const TH1L &h2)
10150{
10151 TH1L hnew = h1;
10152 hnew.Add(&h2,1);
10153 hnew.SetDirectory(nullptr);
10154 return hnew;
10155}
10156
10157////////////////////////////////////////////////////////////////////////////////
10158/// Operator -
10159
10160TH1L operator-(const TH1L &h1, const TH1L &h2)
10161{
10162 TH1L hnew = h1;
10163 hnew.Add(&h2,-1);
10164 hnew.SetDirectory(nullptr);
10165 return hnew;
10166}
10167
10168////////////////////////////////////////////////////////////////////////////////
10169/// Operator *
10170
10171TH1L operator*(const TH1L &h1, const TH1L &h2)
10172{
10173 TH1L hnew = h1;
10174 hnew.Multiply(&h2);
10175 hnew.SetDirectory(nullptr);
10176 return hnew;
10177}
10178
10179////////////////////////////////////////////////////////////////////////////////
10180/// Operator /
10181
10182TH1L operator/(const TH1L &h1, const TH1L &h2)
10183{
10184 TH1L hnew = h1;
10185 hnew.Divide(&h2);
10186 hnew.SetDirectory(nullptr);
10187 return hnew;
10188}
10189
10190//______________________________________________________________________________
10191// TH1F methods
10192// TH1F : histograms with one float per channel. Maximum precision 7 digits, maximum integer bin content = +/-16777216
10193//______________________________________________________________________________
10194
10195ClassImp(TH1F);
10196
10197////////////////////////////////////////////////////////////////////////////////
10198/// Constructor.
10199
10200TH1F::TH1F()
10201{
10202 fDimension = 1;
10203 SetBinsLength(3);
10204 if (fgDefaultSumw2) Sumw2();
10205}
10206
10207////////////////////////////////////////////////////////////////////////////////
10208/// Create a 1-Dim histogram with fix bins of type float
10209/// (see TH1::TH1 for explanation of parameters)
10210
10211TH1F::TH1F(const char *name,const char *title,Int_t nbins,Double_t xlow,Double_t xup)
10212: TH1(name,title,nbins,xlow,xup)
10213{
10214 fDimension = 1;
10216
10217 if (xlow >= xup) SetBuffer(fgBufferSize);
10218 if (fgDefaultSumw2) Sumw2();
10219}
10220
10221////////////////////////////////////////////////////////////////////////////////
10222/// Create a 1-Dim histogram with variable bins of type float
10223/// (see TH1::TH1 for explanation of parameters)
10224
10225TH1F::TH1F(const char *name,const char *title,Int_t nbins,const Float_t *xbins)
10226: TH1(name,title,nbins,xbins)
10227{
10228 fDimension = 1;
10230 if (fgDefaultSumw2) Sumw2();
10231}
10232
10233////////////////////////////////////////////////////////////////////////////////
10234/// Create a 1-Dim histogram with variable bins of type float
10235/// (see TH1::TH1 for explanation of parameters)
10236
10237TH1F::TH1F(const char *name,const char *title,Int_t nbins,const Double_t *xbins)
10238: TH1(name,title,nbins,xbins)
10239{
10240 fDimension = 1;
10242 if (fgDefaultSumw2) Sumw2();
10243}
10244
10245////////////////////////////////////////////////////////////////////////////////
10246/// Create a histogram from a TVectorF
10247/// by default the histogram name is "TVectorF" and title = ""
10248
10249TH1F::TH1F(const TVectorF &v)
10250: TH1("TVectorF","",v.GetNrows(),0,v.GetNrows())
10251{
10253 fDimension = 1;
10254 Int_t ivlow = v.GetLwb();
10255 for (Int_t i=0;i<fNcells-2;i++) {
10256 SetBinContent(i+1,v(i+ivlow));
10257 }
10259 if (fgDefaultSumw2) Sumw2();
10260}
10261
10262////////////////////////////////////////////////////////////////////////////////
10263/// Copy Constructor.
10264/// The list of functions is not copied. (Use Clone() if needed)
10265
10266TH1F::TH1F(const TH1F &h1f) : TH1(), TArrayF()
10267{
10268 h1f.TH1F::Copy(*this);
10269}
10270
10271////////////////////////////////////////////////////////////////////////////////
10272/// Destructor.
10273
10275{
10276}
10277
10278////////////////////////////////////////////////////////////////////////////////
10279/// Copy this to newth1.
10280
10281void TH1F::Copy(TObject &newth1) const
10282{
10284}
10285
10286////////////////////////////////////////////////////////////////////////////////
10287/// Reset.
10288
10290{
10293}
10294
10295////////////////////////////////////////////////////////////////////////////////
10296/// Set total number of bins including under/overflow
10297/// Reallocate bin contents array
10298
10300{
10301 if (n < 0) n = fXaxis.GetNbins() + 2;
10302 fNcells = n;
10303 TArrayF::Set(n);
10304}
10305
10306////////////////////////////////////////////////////////////////////////////////
10307/// Operator =
10308
10310{
10311 if (this != &h1f)
10312 h1f.TH1F::Copy(*this);
10313 return *this;
10314}
10315
10316////////////////////////////////////////////////////////////////////////////////
10317/// Operator *
10318
10320{
10321 TH1F hnew = h1;
10322 hnew.Scale(c1);
10323 hnew.SetDirectory(nullptr);
10324 return hnew;
10325}
10326
10327////////////////////////////////////////////////////////////////////////////////
10328/// Operator +
10329
10330TH1F operator+(const TH1F &h1, const TH1F &h2)
10331{
10332 TH1F hnew = h1;
10333 hnew.Add(&h2,1);
10334 hnew.SetDirectory(nullptr);
10335 return hnew;
10336}
10337
10338////////////////////////////////////////////////////////////////////////////////
10339/// Operator -
10340
10341TH1F operator-(const TH1F &h1, const TH1F &h2)
10342{
10343 TH1F hnew = h1;
10344 hnew.Add(&h2,-1);
10345 hnew.SetDirectory(nullptr);
10346 return hnew;
10347}
10348
10349////////////////////////////////////////////////////////////////////////////////
10350/// Operator *
10351
10352TH1F operator*(const TH1F &h1, const TH1F &h2)
10353{
10354 TH1F hnew = h1;
10355 hnew.Multiply(&h2);
10356 hnew.SetDirectory(nullptr);
10357 return hnew;
10358}
10359
10360////////////////////////////////////////////////////////////////////////////////
10361/// Operator /
10362
10363TH1F operator/(const TH1F &h1, const TH1F &h2)
10364{
10365 TH1F hnew = h1;
10366 hnew.Divide(&h2);
10367 hnew.SetDirectory(nullptr);
10368 return hnew;
10369}
10370
10371//______________________________________________________________________________
10372// TH1D methods
10373// TH1D : histograms with one double per channel. Maximum precision 14 digits, maximum integer bin content = +/-9007199254740992
10374//______________________________________________________________________________
10375
10376ClassImp(TH1D);
10377
10378////////////////////////////////////////////////////////////////////////////////
10379/// Constructor.
10380
10381TH1D::TH1D()
10382{
10383 fDimension = 1;
10384 SetBinsLength(3);
10385 if (fgDefaultSumw2) Sumw2();
10386}
10387
10388////////////////////////////////////////////////////////////////////////////////
10389/// Create a 1-Dim histogram with fix bins of type double
10390/// (see TH1::TH1 for explanation of parameters)
10391
10392TH1D::TH1D(const char *name,const char *title,Int_t nbins,Double_t xlow,Double_t xup)
10393: TH1(name,title,nbins,xlow,xup)
10394{
10395 fDimension = 1;
10397
10398 if (xlow >= xup) SetBuffer(fgBufferSize);
10399 if (fgDefaultSumw2) Sumw2();
10400}
10401
10402////////////////////////////////////////////////////////////////////////////////
10403/// Create a 1-Dim histogram with variable bins of type double
10404/// (see TH1::TH1 for explanation of parameters)
10405
10406TH1D::TH1D(const char *name,const char *title,Int_t nbins,const Float_t *xbins)
10407: TH1(name,title,nbins,xbins)
10408{
10409 fDimension = 1;
10411 if (fgDefaultSumw2) Sumw2();
10412}
10413
10414////////////////////////////////////////////////////////////////////////////////
10415/// Create a 1-Dim histogram with variable bins of type double
10416/// (see TH1::TH1 for explanation of parameters)
10417
10418TH1D::TH1D(const char *name,const char *title,Int_t nbins,const Double_t *xbins)
10419: TH1(name,title,nbins,xbins)
10420{
10421 fDimension = 1;
10423 if (fgDefaultSumw2) Sumw2();
10424}
10425
10426////////////////////////////////////////////////////////////////////////////////
10427/// Create a histogram from a TVectorD
10428/// by default the histogram name is "TVectorD" and title = ""
10429
10430TH1D::TH1D(const TVectorD &v)
10431: TH1("TVectorD","",v.GetNrows(),0,v.GetNrows())
10432{
10434 fDimension = 1;
10435 Int_t ivlow = v.GetLwb();
10436 for (Int_t i=0;i<fNcells-2;i++) {
10437 SetBinContent(i+1,v(i+ivlow));
10438 }
10440 if (fgDefaultSumw2) Sumw2();
10441}
10442
10443////////////////////////////////////////////////////////////////////////////////
10444/// Destructor.
10445
10447{
10448}
10449
10450////////////////////////////////////////////////////////////////////////////////
10451/// Constructor.
10452
10453TH1D::TH1D(const TH1D &h1d) : TH1(), TArrayD()
10454{
10455 // intentially call virtual method to warn if TProfile is copying
10456 h1d.Copy(*this);
10457}
10458
10459////////////////////////////////////////////////////////////////////////////////
10460/// Copy this to newth1
10461
10462void TH1D::Copy(TObject &newth1) const
10463{
10465}
10466
10467////////////////////////////////////////////////////////////////////////////////
10468/// Reset.
10469
10471{
10474}
10475
10476////////////////////////////////////////////////////////////////////////////////
10477/// Set total number of bins including under/overflow
10478/// Reallocate bin contents array
10479
10481{
10482 if (n < 0) n = fXaxis.GetNbins() + 2;
10483 fNcells = n;
10484 TArrayD::Set(n);
10485}
10486
10487////////////////////////////////////////////////////////////////////////////////
10488/// Operator =
10489
10491{
10492 // intentially call virtual method to warn if TProfile is copying
10493 if (this != &h1d)
10494 h1d.Copy(*this);
10495 return *this;
10496}
10497
10498////////////////////////////////////////////////////////////////////////////////
10499/// Operator *
10500
10502{
10503 TH1D hnew = h1;
10504 hnew.Scale(c1);
10505 hnew.SetDirectory(nullptr);
10506 return hnew;
10507}
10508
10509////////////////////////////////////////////////////////////////////////////////
10510/// Operator +
10511
10512TH1D operator+(const TH1D &h1, const TH1D &h2)
10513{
10514 TH1D hnew = h1;
10515 hnew.Add(&h2,1);
10516 hnew.SetDirectory(nullptr);
10517 return hnew;
10518}
10519
10520////////////////////////////////////////////////////////////////////////////////
10521/// Operator -
10522
10523TH1D operator-(const TH1D &h1, const TH1D &h2)
10524{
10525 TH1D hnew = h1;
10526 hnew.Add(&h2,-1);
10527 hnew.SetDirectory(nullptr);
10528 return hnew;
10529}
10530
10531////////////////////////////////////////////////////////////////////////////////
10532/// Operator *
10533
10534TH1D operator*(const TH1D &h1, const TH1D &h2)
10535{
10536 TH1D hnew = h1;
10537 hnew.Multiply(&h2);
10538 hnew.SetDirectory(nullptr);
10539 return hnew;
10540}
10541
10542////////////////////////////////////////////////////////////////////////////////
10543/// Operator /
10544
10545TH1D operator/(const TH1D &h1, const TH1D &h2)
10546{
10547 TH1D hnew = h1;
10548 hnew.Divide(&h2);
10549 hnew.SetDirectory(nullptr);
10550 return hnew;
10551}
10552
10553////////////////////////////////////////////////////////////////////////////////
10554///return pointer to histogram with name
10555///hid if id >=0
10556///h_id if id <0
10557
10558TH1 *R__H(Int_t hid)
10559{
10560 TString hname;
10561 if(hid >= 0) hname.Form("h%d",hid);
10562 else hname.Form("h_%d",hid);
10563 return (TH1*)gDirectory->Get(hname);
10564}
10565
10566////////////////////////////////////////////////////////////////////////////////
10567///return pointer to histogram with name hname
10568
10569TH1 *R__H(const char * hname)
10570{
10571 return (TH1*)gDirectory->Get(hname);
10572}
10573
10574
10575/// \fn void TH1::SetBarOffset(Float_t offset)
10576/// Set the bar offset as fraction of the bin width for drawing mode "B".
10577/// This shifts bars to the right on the x axis, and helps to draw bars next to each other.
10578/// \see THistPainter, SetBarWidth()
10579
10580/// \fn void TH1::SetBarWidth(Float_t width)
10581/// Set the width of bars as fraction of the bin width for drawing mode "B".
10582/// This allows for making bars narrower than the bin width. With SetBarOffset(), this helps to draw multiple bars next to each other.
10583/// \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
Definition RtypesCore.h:82
bool Bool_t
Definition RtypesCore.h:63
int Int_t
Definition RtypesCore.h:45
short Color_t
Definition RtypesCore.h:85
short Version_t
Definition RtypesCore.h:65
char Char_t
Definition RtypesCore.h:37
float Float_t
Definition RtypesCore.h:57
short Short_t
Definition RtypesCore.h:39
constexpr Bool_t kFALSE
Definition RtypesCore.h:94
double Double_t
Definition RtypesCore.h:59
long long Long64_t
Definition RtypesCore.h:69
constexpr Bool_t kTRUE
Definition RtypesCore.h:93
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:374
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#define gDirectory
Definition TDirectory.h:384
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:5881
void H1LeastSquareLinearFit(Int_t ndata, Double_t &a0, Double_t &a1, Int_t &ifail)
Least square linear fit without weights.
Definition TH1.cxx:4829
void H1InitGaus()
Compute Initial values of parameters for a gaussian.
Definition TH1.cxx:4664
void H1InitExpo()
Compute Initial values of parameters for an exponential.
Definition TH1.cxx:4720
TH1C operator+(const TH1C &h1, const TH1C &h2)
Operator +.
Definition TH1.cxx:9585
TH1C operator-(const TH1C &h1, const TH1C &h2)
Operator -.
Definition TH1.cxx:9596
TH1C operator/(const TH1C &h1, const TH1C &h2)
Operator /.
Definition TH1.cxx:9618
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:4875
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:5864
static Bool_t AlmostInteger(Double_t a, Double_t epsilon=0.00000001)
Test if a double is almost an integer.
Definition TH1.cxx:5872
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:10556
TH1C operator*(Double_t c1, const TH1C &h1)
Operator *.
Definition TH1.cxx:9574
void H1LeastSquareFit(Int_t n, Int_t m, Double_t *a)
Least squares lpolynomial fitting without weights.
Definition TH1.cxx:4770
void H1InitPolynom()
Compute Initial values of parameters for a polynom.
Definition TH1.cxx:4740
float xmin
int nentries
float ymin
float xmax
float ymax
#define gInterpreter
Int_t gDebug
Definition TROOT.cxx:597
R__EXTERN TVirtualMutex * gROOTMutex
Definition TROOT.h:63
#define gROOT
Definition TROOT.h:406
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:2503
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:105
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:149
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:106
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:105
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:105
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:105
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:280
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:142
virtual void SetLabelSize(Float_t size=0.04)
Set size of axis labels.
Definition TAttAxis.cxx:185
virtual Style_t GetLabelFont() const
Definition TAttAxis.h:40
virtual void SetTitleFont(Style_t font=62)
Set the title font.
Definition TAttAxis.cxx:309
virtual void SetLabelOffset(Float_t offset=0.005)
Set distance between the axis and the labels.
Definition TAttAxis.cxx:173
virtual void SetLabelFont(Style_t font=62)
Set labels' font.
Definition TAttAxis.cxx:162
virtual void SetTitleSize(Float_t size=0.04)
Set size of axis title.
Definition TAttAxis.cxx:291
virtual void SetTitleColor(Color_t color=1)
Set color of axis title.
Definition TAttAxis.cxx:300
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:79
virtual Float_t GetTitleOffset() const
Definition TAttAxis.h:44
virtual void SetTickLength(Float_t length=0.03)
Set tick mark length.
Definition TAttAxis.cxx:266
virtual void SetNdivisions(Int_t n=510, Bool_t optim=kTRUE)
Set the number of divisions for this axis.
Definition TAttAxis.cxx:215
virtual void SetLabelColor(Color_t color=1, Float_t alpha=1.)
Set color of labels.
Definition TAttAxis.cxx:152
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:207
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:239
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:177
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:275
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:558
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:482
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:211
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:296
virtual Double_t GetBinLowEdge(Int_t bin) const
Return low edge of bin.
Definition TAxis.cxx:522
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:784
virtual Int_t FindFixBin(Double_t x) const
Find bin number corresponding to abscissa x
Definition TAxis.cxx:423
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:715
Int_t GetLast() const
Return last bin on the axis i.e.
Definition TAxis.cxx:473
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:1209
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:567
virtual void SetRange(Int_t first=0, Int_t last=0)
Set the viewing range for the axis using bin numbers.
Definition TAxis.cxx:1046
virtual Double_t GetBinWidth(Int_t bin) const
Return bin width.
Definition TAxis.cxx:546
virtual Double_t GetBinUpEdge(Int_t bin) const
Return up edge of bin.
Definition TAxis.cxx:532
Int_t GetFirst() const
Return first bin on the axis i.e.
Definition TAxis.cxx:462
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:5104
ROOT::NewFunc_t GetNew() const
Return the wrapper around new ThisClass().
Definition TClass.cxx:7619
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:491
1-Dim function class
Definition TF1.h:234
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:3694
virtual TH1 * GetHistogram() const
Return a pointer to the histogram used to visualise the function Note that this histogram is managed ...
Definition TF1.cxx:1606
static TClass * Class()
virtual Int_t GetNpar() const
Definition TF1.h:513
virtual Double_t Integral(Double_t a, Double_t b, Double_t epsrel=1.e-12)
IntegralOneDim or analytical integral.
Definition TF1.cxx:2551
virtual void InitArgs(const Double_t *x, const Double_t *params)
Initialize parameters addresses.
Definition TF1.cxx:2502
virtual void GetRange(Double_t *xmin, Double_t *xmax) const
Return range of a generic N-D function.
Definition TF1.cxx:2301
virtual Double_t EvalPar(const Double_t *x, const Double_t *params=nullptr)
Evaluate function with given coordinates and parameters.
Definition TF1.cxx:1470
virtual void SetParLimits(Int_t ipar, Double_t parmin, Double_t parmax)
Set lower and upper limits for parameter ipar.
Definition TF1.cxx:3533
static Bool_t RejectedPoint()
See TF1::RejectPoint above.
Definition TF1.cxx:3703
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:1441
virtual void SetParameter(Int_t param, Double_t value)
Definition TF1.h:675
virtual Bool_t IsInside(const Double_t *x) const
return kTRUE if the point is inside the function range
Definition TF1.h:634
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:486
~TH1C() override
Destructor.
Definition TH1.cxx:9498
void SetBinsLength(Int_t n=-1) override
Set total number of bins including under/overflow Reallocate bin contents array.
Definition TH1.cxx:9554
TH1C & operator=(const TH1C &h1)
Operator =.
Definition TH1.cxx:9564
TH1C()
Constructor.
Definition TH1.cxx:9450
void Copy(TObject &hnew) const override
Copy this to newth1.
Definition TH1.cxx:9536
void AddBinContent(Int_t bin) override
Increment bin content by 1.
Definition TH1.cxx:9515
void Reset(Option_t *option="") override
Reset.
Definition TH1.cxx:9544
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:698
~TH1D() override
Destructor.
Definition TH1.cxx:10444
void SetBinsLength(Int_t n=-1) override
Set total number of bins including under/overflow Reallocate bin contents array.
Definition TH1.cxx:10478
void Copy(TObject &hnew) const override
Copy this to newth1.
Definition TH1.cxx:10460
TH1D()
Constructor.
Definition TH1.cxx:10379
TH1D & operator=(const TH1D &h1)
Operator =.
Definition TH1.cxx:10488
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:650
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:684
TH1F & operator=(const TH1F &h1)
Operator =.
Definition TH1.cxx:10307
void Copy(TObject &hnew) const override
Copy this to newth1.
Definition TH1.cxx:10279
void SetBinsLength(Int_t n=-1) override
Set total number of bins including under/overflow Reallocate bin contents array.
Definition TH1.cxx:10297
~TH1F() override
Destructor.
Definition TH1.cxx:10272
TH1F()
Constructor.
Definition TH1.cxx:10198
1-D histogram with an int per channel (see TH1 documentation)
Definition TH1.h:568
void SetBinsLength(Int_t n=-1) override
Set total number of bins including under/overflow Reallocate bin contents array.
Definition TH1.cxx:9927
void AddBinContent(Int_t bin) override
Increment bin content by 1.
Definition TH1.cxx:9888
~TH1I() override
Destructor.
Definition TH1.cxx:9871
TH1I()
Constructor.
Definition TH1.cxx:9823
void Copy(TObject &hnew) const override
Copy this to newth1.
Definition TH1.cxx:9909
TH1I & operator=(const TH1I &h1)
Operator =.
Definition TH1.cxx:9937
1-D histogram with a long64 per channel (see TH1 documentation)
Definition TH1.h:609
TH1L & operator=(const TH1L &h1)
Operator =.
Definition TH1.cxx:10125
void AddBinContent(Int_t bin) override
Increment bin content by 1.
Definition TH1.cxx:10076
void SetBinsLength(Int_t n=-1) override
Set total number of bins including under/overflow Reallocate bin contents array.
Definition TH1.cxx:10115
~TH1L() override
Destructor.
Definition TH1.cxx:10059
TH1L()
Constructor.
Definition TH1.cxx:10011
void Copy(TObject &hnew) const override
Copy this to newth1.
Definition TH1.cxx:10097
1-D histogram with a short per channel (see TH1 documentation)
Definition TH1.h:527
TH1S & operator=(const TH1S &h1)
Operator =.
Definition TH1.cxx:9750
void Copy(TObject &hnew) const override
Copy this to newth1.
Definition TH1.cxx:9722
TH1S()
Constructor.
Definition TH1.cxx:9636
void SetBinsLength(Int_t n=-1) override
Set total number of bins including under/overflow Reallocate bin contents array.
Definition TH1.cxx:9740
~TH1S() override
Destructor.
Definition TH1.cxx:9684
void AddBinContent(Int_t bin) override
Increment bin content by 1.
Definition TH1.cxx:9701
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:59
~TH1() override
Histogram default destructor.
Definition TH1.cxx:631
virtual void SetError(const Double_t *error)
Replace bin errors by values in array error.
Definition TH1.cxx:8947
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:8933
virtual void FitPanel()
Display a panel with all histogram fit options.
Definition TH1.cxx:4262
Double_t * fBuffer
[fBufferSize] entry buffer
Definition TH1.h:119
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:1313
virtual Double_t GetEffectiveEntries() const
Number of effective entries of the histogram.
Definition TH1.cxx:4426
char * GetObjectInfo(Int_t px, Int_t py) const override
Redefines TObject::GetObjectInfo.
Definition TH1.cxx:4480
virtual void Smooth(Int_t ntimes=1, Option_t *option="")
Smooth bin contents of this histogram.
Definition TH1.cxx:6882
virtual Double_t GetBinCenter(Int_t bin) const
Return bin center for 1D histogram.
Definition TH1.cxx:9137
virtual void Rebuild(Option_t *option="")
Using the current bin info, recompute the arrays for contents and errors.
Definition TH1.cxx:7090
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:384
static Bool_t fgStatOverflows
! Flag to use under/overflows in statistics
Definition TH1.h:128
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:3775
TAxis * GetZaxis()
Definition TH1.h:346
Int_t DistancetoPrimitive(Int_t px, Int_t py) override
Compute distance from point px,py to a line.
Definition TH1.cxx:2795
virtual Bool_t Multiply(TF1 *f1, Double_t c1=1)
Performs the operation:
Definition TH1.cxx:6052
@ kXaxis
Definition TH1.h:73
@ kNoAxis
NOTE: Must always be 0 !!!
Definition TH1.h:72
@ kZaxis
Definition TH1.h:75
@ kYaxis
Definition TH1.h:74
Int_t fNcells
Number of bins(1D), cells (2D) +U/Overflows.
Definition TH1.h:100
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:7824
void Copy(TObject &hnew) const override
Copy this histogram structure to newth1.
Definition TH1.cxx:2643
void SetTitle(const char *title) override
Change/set the title.
Definition TH1.cxx:6721
Double_t fTsumw
Total Sum of weights.
Definition TH1.h:107
virtual Float_t GetBarWidth() const
Definition TH1.h:274
Double_t fTsumw2
Total Sum of squares of weights.
Definition TH1.h:108
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:6928
virtual Float_t GetBarOffset() const
Definition TH1.h:273
TList * fFunctions
->Pointer to list of functions (fits and user)
Definition TH1.h:117
static Bool_t fgAddDirectory
! Flag to add histograms to the directory
Definition TH1.h:127
static TClass * Class()
static Int_t GetDefaultBufferSize()
Static function return the default buffer size for automatic histograms the parameter fgBufferSize ma...
Definition TH1.cxx:4384
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:7973
Double_t fTsumwx2
Total Sum of weight*X*X.
Definition TH1.h:110
virtual Double_t GetStdDev(Int_t axis=1) const
Returns the Standard Deviation (Sigma).
Definition TH1.cxx:7598
TH1()
Histogram default constructor.
Definition TH1.cxx:603
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:9315
void UseCurrentStyle() override
Copy current attributes from/to current style.
Definition TH1.cxx:7460
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:5385
virtual Int_t GetNbinsY() const
Definition TH1.h:315
Short_t fBarOffset
(1000*offset) for bar charts or legos
Definition TH1.h:104
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:2039
static bool CheckBinLimits(const TAxis *a1, const TAxis *a2)
Check bin limits.
Definition TH1.cxx:1511
virtual Double_t GetBinError(Int_t bin) const
Return value of error associated to bin number bin.
Definition TH1.cxx:9059
static Int_t FitOptionsMake(Option_t *option, Foption_t &Foption)
Decode string choptin and fill fitOption structure.
Definition TH1.cxx:4655
virtual Int_t GetNbinsZ() const
Definition TH1.h:316
virtual Double_t GetNormFactor() const
Definition TH1.h:318
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:7526
virtual Double_t GetSkewness(Int_t axis=1) const
Definition TH1.cxx:7662
virtual void ClearUnderflowAndOverflow()
Remove all the content from the underflow and overflow bins, without changing the number of entries A...
Definition TH1.cxx:2489
virtual void FillRandom(TF1 *f1, Int_t ntimes=5000, TRandom *rng=nullptr)
Definition TH1.cxx:3500
virtual Double_t GetContourLevelPad(Int_t level) const
Return the value of contour number "level" in Pad coordinates.
Definition TH1.cxx:8436
virtual TH1 * DrawNormalized(Option_t *option="", Double_t norm=1) const
Draw a normalized copy of this histogram.
Definition TH1.cxx:3116
@ kNeutral
Adapt to the global flag.
Definition TH1.h:83
virtual Int_t GetDimension() const
Definition TH1.h:300
void Streamer(TBuffer &) override
Stream a class object.
Definition TH1.cxx:6936
static void AddDirectory(Bool_t add=kTRUE)
Sets the flag controlling the automatic add of histograms in memory.
Definition TH1.cxx:1264
Double_t GetSumOfAllWeights(const bool includeOverflow) const
Return the sum of all weights.
Definition TH1.cxx:7910
@ kIsAverage
Bin contents are average (used by Add)
Definition TH1.h:182
@ kUserContour
User specified contour levels.
Definition TH1.h:177
@ kNoStats
Don't draw stats box.
Definition TH1.h:176
@ kAutoBinPTwo
different than 1.
Definition TH1.h:185
@ kIsNotW
Histogram is forced to be not weighted even when the histogram is filled with weighted.
Definition TH1.h:183
@ kIsHighlight
bit set if histo is highlight
Definition TH1.h:186
virtual void SetContourLevel(Int_t level, Double_t value)
Set value for one contour level.
Definition TH1.cxx:8518
virtual Bool_t CanExtendAllAxes() const
Returns true if all axes are extendable.
Definition TH1.cxx:6639
TDirectory * fDirectory
! Pointer to directory holding this histogram
Definition TH1.h:120
virtual void Reset(Option_t *option="")
Reset this histogram: contents, errors, etc.
Definition TH1.cxx:7106
void SetNameTitle(const char *name, const char *title) override
Change the name and title of this histogram.
Definition TH1.cxx:8970
TAxis * GetXaxis()
Definition TH1.h:344
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:4977
TH1 * GetCumulative(Bool_t forward=kTRUE, const char *suffix="_cumulative") const
Return a pointer to a histogram containing the cumulative content.
Definition TH1.cxx:2588
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:1278
virtual Int_t GetNcells() const
Definition TH1.h:317
virtual Int_t ShowPeaks(Double_t sigma=2, Option_t *option="", Double_t threshold=0.05)
Interface to TSpectrum::Search.
Definition TH1.cxx:9297
static Bool_t RecomputeAxisLimits(TAxis &destAxis, const TAxis &anAxis)
Finds new limits for the axis for the Merge function.
Definition TH1.cxx:5911
virtual Double_t GetSumOfWeights() const
Return the sum of weights across all bins excluding under/overflows.
Definition TH1.h:332
virtual void PutStats(Double_t *stats)
Replace current statistics with the values in array stats.
Definition TH1.cxx:7875
TVirtualHistPainter * GetPainter(Option_t *option="")
Return pointer to painter.
Definition TH1.cxx:4489
TObject * FindObject(const char *name) const override
Search object named name in the list of functions.
Definition TH1.cxx:3835
void Print(Option_t *option="") const override
Print some global quantities for this histogram.
Definition TH1.cxx:7012
static Bool_t GetDefaultSumw2()
Return kTRUE if TH1::Sumw2 must be called when creating new histograms.
Definition TH1.cxx:4393
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:3712
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:3876
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:4964
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:8541
virtual Int_t GetNbinsX() const
Definition TH1.h:314
virtual void SetMaximum(Double_t maximum=-1111)
Definition TH1.h:424
virtual TH1 * FFT(TH1 *h_output, Option_t *option)
This function allows to do discrete Fourier transforms of TH1 and TH2.
Definition TH1.cxx:3256
virtual void LabelsInflate(Option_t *axis="X")
Double the number of bins for axis.
Definition TH1.cxx:5318
virtual TH1 * ShowBackground(Int_t niter=20, Option_t *option="same")
This function calculates the background spectrum in this histogram.
Definition TH1.cxx:9283
static Bool_t SameLimitsAndNBins(const TAxis &axis1, const TAxis &axis2)
Same limits and bins.
Definition TH1.cxx:5901
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:814
Double_t fMaximum
Maximum value for plotting.
Definition TH1.h:111
Int_t fBufferSize
fBuffer size
Definition TH1.h:118
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:7245
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:7964
Int_t fDimension
! Histogram dimension (1, 2 or 3 dim)
Definition TH1.h:121
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:9202
EBinErrorOpt fBinStatErrOpt
Option for bin statistical errors.
Definition TH1.h:124
static Int_t fgBufferSize
! Default buffer size for automatic histograms
Definition TH1.h:126
virtual void SetBinsLength(Int_t=-1)
Definition TH1.h:400
Double_t fNormFactor
Normalization factor.
Definition TH1.h:113
@ kFullyConsistent
Definition TH1.h:89
@ kDifferentNumberOfBins
Definition TH1.h:93
@ kDifferentDimensions
Definition TH1.h:94
@ kDifferentBinLimits
Definition TH1.h:91
@ kDifferentAxisLimits
Definition TH1.h:92
@ kDifferentLabels
Definition TH1.h:90
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition TH1.cxx:3316
TAxis * GetYaxis()
Definition TH1.h:345
TArrayD fContour
Array to display contour levels.
Definition TH1.h:114
virtual Double_t GetBinErrorLow(Int_t bin) const
Return lower error associated to bin number bin.
Definition TH1.cxx:9075
void Browse(TBrowser *b) override
Browse the Histogram object.
Definition TH1.cxx:750
virtual void SetContent(const Double_t *content)
Replace bin contents by the contents of array content.
Definition TH1.cxx:8394
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3038
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:7372
Short_t fBarWidth
(1000*width) for bar charts or legos
Definition TH1.h:105
virtual Double_t GetBinErrorSqUnchecked(Int_t bin) const
Definition TH1.h:477
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:425
Bool_t IsBinUnderflow(Int_t bin, Int_t axis=0) const
Return true if the bin is underflow.
Definition TH1.cxx:5217
void SavePrimitive(std::ostream &out, Option_t *option="") override
Save primitive as a C++ statement(s) on output stream out.
Definition TH1.cxx:7267
static bool CheckBinLabels(const TAxis *a1, const TAxis *a2)
Check that axis have same labels.
Definition TH1.cxx:1538
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:5118
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:6706
Bool_t IsBinOverflow(Int_t bin, Int_t axis=0) const
Return true if the bin is overflow.
Definition TH1.cxx:5185
UInt_t GetAxisLabelStatus() const
Internal function used in TH1::Fill to see which axis is full alphanumeric, i.e.
Definition TH1.cxx:6678
Double_t * fIntegral
! Integral of bins used by GetRandom
Definition TH1.h:122
Double_t fMinimum
Minimum value for plotting.
Definition TH1.h:112
virtual Double_t Integral(Option_t *option="") const
Return integral of bin contents.
Definition TH1.cxx:7937
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:9218
virtual void DirectoryAutoAdd(TDirectory *)
Perform the automatic addition of the histogram to the given directory.
Definition TH1.cxx:2773
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:9183
virtual Double_t GetBinLowEdge(Int_t bin) const
Return bin lower edge for 1D histogram.
Definition TH1.cxx:9148
void Build()
Creates histogram basic data structure.
Definition TH1.cxx:759
virtual Double_t GetEntries() const
Return the current number of entries.
Definition TH1.cxx:4401
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:9047
virtual TH1 * Rebin(Int_t ngroup=2, const char *newname="", const Double_t *xbins=nullptr)
Rebin this histogram.
Definition TH1.cxx:6278
virtual Int_t BufferFill(Double_t x, Double_t w)
accumulate arguments in buffer.
Definition TH1.cxx:1476
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:5089
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:6652
TList * GetListOfFunctions() const
Definition TH1.h:261
void SetName(const char *name) override
Change the name of this histogram.
Definition TH1.cxx:8956
virtual TH1 * DrawCopy(Option_t *option="", const char *name_postfix="_copy") const
Copy this histogram and Draw in the current pad.
Definition TH1.cxx:3085
Bool_t IsEmpty() const
Check if a histogram is empty (this is a protected method used mainly by TH1Merger )
Definition TH1.cxx:5167
virtual Double_t GetMeanError(Int_t axis=1) const
Return standard error of mean of this histogram along the X axis.
Definition TH1.cxx:7566
void Paint(Option_t *option="") override
Control routine to paint any kind of histograms.
Definition TH1.cxx:6209
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:8058
virtual void ResetStats()
Reset the statistics including the number of entries and replace with values calculated from bin cont...
Definition TH1.cxx:7893
static void SetDefaultBufferSize(Int_t buffersize=1000)
Static function to set the default buffer size for automatic histograms.
Definition TH1.cxx:6696
virtual void SetBinErrorOption(EBinErrorOpt type)
Definition TH1.h:401
virtual void SetBuffer(Int_t buffersize, Option_t *option="")
Set the maximum number of entries to be kept in the buffer.
Definition TH1.cxx:8454
virtual void DrawPanel()
Display a panel with all histogram drawing options.
Definition TH1.cxx:3147
virtual Double_t GetRandom(TRandom *rng=nullptr) const
Return a random number distributed according the histogram bin contents.
Definition TH1.cxx:5013
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:2468
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:1980
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:3445
virtual void GetMinimumAndMaximum(Double_t &min, Double_t &max) const
Retrieve the minimum and maximum values in the histogram.
Definition TH1.cxx:8727
@ kNstat
Size of statistics data (up to TProfile3D)
Definition TH1.h:195
virtual Int_t GetMaximumBin() const
Return location of bin with maximum value in the range.
Definition TH1.cxx:8573
static Int_t AutoP2GetBins(Int_t n)
Auxiliary function to get the next power of 2 integer value larger then n.
Definition TH1.cxx:1291
Double_t fEntries
Number of entries.
Definition TH1.h:106
virtual Long64_t Merge(TCollection *list)
Definition TH1.h:365
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:4445
void ExecuteEvent(Int_t event, Int_t px, Int_t py) override
Execute action corresponding to one event.
Definition TH1.cxx:3212
virtual Double_t * GetIntegral()
Return a pointer to the array of bins integral.
Definition TH1.cxx:2558
TAxis fZaxis
Z axis descriptor.
Definition TH1.h:103
EStatOverflows fStatOverflows
Per object flag to use under/overflows in statistics.
Definition TH1.h:125
TClass * IsA() const override
Definition TH1.h:465
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:3419
static bool CheckEqualAxes(const TAxis *a1, const TAxis *a2)
Check that the axis are the same.
Definition TH1.cxx:1581
@ kPoisson2
Errors from Poisson interval at 95% CL (~ 2 sigma)
Definition TH1.h:67
@ kNormal
Errors with Normal (Wald) approximation: errorUp=errorLow= sqrt(N)
Definition TH1.h:65
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
Definition TH1.cxx:5064
virtual Int_t GetContour(Double_t *levels=nullptr)
Return contour values into array levels if pointer levels is non zero.
Definition TH1.cxx:8407
TAxis fXaxis
X axis descriptor.
Definition TH1.h:101
virtual Bool_t IsHighlight() const
Definition TH1.h:358
virtual void ExtendAxis(Double_t x, TAxis *axis)
Histogram is resized along axis such that x is in the axis range.
Definition TH1.cxx:6507
virtual Double_t GetBinWidth(Int_t bin) const
Return bin width for 1D histogram.
Definition TH1.cxx:9159
TArrayD fSumw2
Array of sum of squares of weights.
Definition TH1.h:115
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:4317
virtual Double_t GetContourLevel(Int_t level) const
Return value of contour number level.
Definition TH1.cxx:8426
virtual void SetContour(Int_t nlevels, const Double_t *levels=nullptr)
Set the number and values of contour levels.
Definition TH1.cxx:8479
virtual void SetHighlight(Bool_t set=kTRUE)
Set highlight (enable/disable) mode for the histogram by default highlight mode is disable.
Definition TH1.cxx:4460
virtual Double_t GetBinErrorUp(Int_t bin) const
Return upper error associated to bin number bin.
Definition TH1.cxx:9106
virtual void Scale(Double_t c1=1, Option_t *option="")
Multiply this histogram by a constant c1.
Definition TH1.cxx:6607
virtual Int_t GetMinimumBin() const
Return location of bin with minimum value in the range.
Definition TH1.cxx:8661
virtual Int_t GetSumw2N() const
Definition TH1.h:335
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:3650
Bool_t GetStatOverflowsBehaviour() const
Definition TH1.h:164
void SaveAs(const char *filename="hist", Option_t *option="") const override
Save the histogram as .csv, .tsv or .txt.
Definition TH1.cxx:7184
virtual Int_t GetQuantiles(Int_t n, Double_t *xp, const Double_t *p=nullptr)
Compute Quantiles for this histogram.
Definition TH1.cxx:4593
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:2724
virtual Double_t GetStdDevError(Int_t axis=1) const
Return error of standard deviation estimation for Normal distribution.
Definition TH1.cxx:7646
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:2812
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:8631
int LoggedInconsistency(const char *name, const TH1 *h1, const TH1 *h2, bool useMerge=false) const
Definition TH1.cxx:871
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:1610
static Int_t CheckConsistency(const TH1 *h1, const TH1 *h2)
Check histogram compatibility.
Definition TH1.cxx:1649
void RecursiveRemove(TObject *obj) override
Recursively remove object from the list of functions.
Definition TH1.cxx:6579
TAxis fYaxis
Y axis descriptor.
Definition TH1.h:102
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:8174
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:6771
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:9170
TVirtualHistPainter * fPainter
! Pointer to histogram painter
Definition TH1.h:123
virtual void SetBins(Int_t nx, Double_t xmin, Double_t xmax)
Redefine x axis parameters.
Definition TH1.cxx:8763
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:3683
virtual void Sumw2(Bool_t flag=kTRUE)
Create structure to store sum of squares of weights.
Definition TH1.cxx:9016
virtual void SetEntries(Double_t n)
Definition TH1.h:411
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:6463
static bool CheckAxisLimits(const TAxis *a1, const TAxis *a2)
Check that the axis limits of the histograms are the same.
Definition TH1.cxx:1567
static Bool_t AddDirectoryStatus()
Static function: cannot be inlined on Windows/NT.
Definition TH1.cxx:742
static Bool_t fgDefaultSumw2
! Flag to call TH1::Sumw2 automatically at histogram creation time
Definition TH1.h:129
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:7426
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:109
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:5248
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:2509
TString fOption
Histogram options.
Definition TH1.h:116
virtual void Eval(TF1 *f1, Option_t *option="")
Evaluate function f1 at the center of bins of this histogram.
Definition TH1.cxx:3164
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:385
virtual Int_t BufferEmpty(Int_t action=0)
Fill histogram with all entries in the buffer.
Definition TH1.cxx:1384
virtual void SetStats(Bool_t stats=kTRUE)
Set statistics option on/off.
Definition TH1.cxx:8986
virtual Double_t GetKurtosis(Int_t axis=1) const
Definition TH1.cxx:7735
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:1192
TObject * FindObject(const char *name) const override
Find an object in this list using its name.
Definition TList.cxx:576
void RecursiveRemove(TObject *obj) override
Remove object from this collection and recursively remove the object from all other objects (and coll...
Definition TList.cxx:762
void Add(TObject *obj) override
Definition TList.h:81
TObject * Remove(TObject *obj) override
Remove object from the list.
Definition TList.cxx:820
TObject * First() const override
Return the first object in the list. Returns 0 when list is empty.
Definition TList.cxx:657
void Delete(Option_t *option="") override
Remove all objects from the list AND delete all heap based objects.
Definition TList.cxx:468
TObject * At(Int_t idx) const override
Returns the object at position idx. Returns 0 if idx is out of range.
Definition TList.cxx:355
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:94
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition TNamed.cxx:174
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:150
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:205
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:204
@ 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
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:559
virtual Double_t PoissonD(Double_t mean)
Generates a random number according to a Poisson law.
Definition TRandom.cxx:461
virtual ULong64_t Poisson(Double_t mean)
Generates a random integer N according to a Poisson law.
Definition TRandom.cxx:404
Basic string class.
Definition TString.h:139
Ssiz_t Length() const
Definition TString.h:417
void ToLower()
Change string to lower-case.
Definition TString.cxx:1182
TString & ReplaceSpecialCppChars()
Find special characters which are typically used in printf() calls and replace them by appropriate es...
Definition TString.cxx:1114
const char * Data() const
Definition TString.h:376
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition TString.h:704
void ToUpper()
Change string to upper case.
Definition TString.cxx:1195
Bool_t IsNull() const
Definition TString.h:414
virtual void Streamer(TBuffer &)
Stream a string object.
Definition TString.cxx:1412
TString & Append(const char *cs)
Definition TString.h:572
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:2378
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:632
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition TString.h:651
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:1642
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
std::ostream & Info()
Definition hadd.cxx:171
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:408
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:896
Int_t Nint(T x)
Round to nearest integer. Rounds half integers to the nearest even integer.
Definition TMath.h:697
Short_t Max(Short_t a, Short_t b)
Returns the largest of a and b.
Definition TMathBase.h:250
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:1352
Double_t QuietNaN()
Returns a quiet NaN as defined by IEEE 754.
Definition TMath.h:906
Double_t Floor(Double_t x)
Rounds x downward, returning the largest integral value that is not greater than x.
Definition TMath.h:684
Double_t ATan(Double_t)
Returns the principal value of the arc tangent of x, expressed in radians.
Definition TMath.h:644
Double_t Ceil(Double_t x)
Rounds x upward, returning the smallest integral value that is not less than x.
Definition TMath.h:672
T MinElement(Long64_t n, const T *a)
Returns minimum of array a of length n.
Definition TMath.h:964
Double_t Log(Double_t x)
Returns the natural logarithm of x.
Definition TMath.h:760
Double_t Sqrt(Double_t x)
Returns the square root of x.
Definition TMath.h:666
Short_t Min(Short_t a, Short_t b)
Returns the smallest of a and b.
Definition TMathBase.h:198
constexpr Double_t Pi()
Definition TMath.h:37
Bool_t AreEqualRel(Double_t af, Double_t bf, Double_t relPrec)
Comparing floating points.
Definition TMath.h:426
Bool_t AreEqualAbs(Double_t af, Double_t bf, Double_t epsilon)
Comparing floating points.
Definition TMath.h:418
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:431
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:347
Double_t Log10(Double_t x)
Returns the common (base-10) logarithm of x.
Definition TMath.h:766
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
Definition TMathBase.h:123
Double_t Infinity()
Returns an infinity as defined by the IEEE standard.
Definition TMath.h:921
th1 Draw()
TMarker m
Definition textangle.C:8
TLine l
Definition textangle.C:4
static uint64_t sum(uint64_t i)
Definition Factory.cxx:2345