Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TH2.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 "TROOT.h"
13#include "TBuffer.h"
14#include "TClass.h"
15#include "THashList.h"
16#include "TH2.h"
17#include "TVirtualPad.h"
18#include "TF2.h"
19#include "TProfile.h"
20#include "TRandom.h"
21#include "TMatrixFBase.h"
22#include "TMatrixDBase.h"
23#include "THLimitsFinder.h"
24#include "TError.h"
25#include "TMath.h"
26#include "TObjString.h"
27#include "TObjArray.h"
28#include "TVirtualHistPainter.h"
29
30#include <cstdio>
31
32#include "Rebin2DHelpers.h"
33
34#include <vector>
35
36/** \addtogroup Histograms
37@{
38\class TH2C
39\brief 2-D histogram with a byte per channel (see TH1 documentation)
40\class TH2S
41\brief 2-D histogram with a short per channel (see TH1 documentation)
42\class TH2I
43\brief 2-D histogram with an int per channel (see TH1 documentation)
44\class TH2L
45\brief 2-D histogram with a long64 per channel (see TH1 documentation)
46\class TH2F
47\brief 2-D histogram with a float per channel (see TH1 documentation)
48\class TH2D
49\brief 2-D histogram with a double per channel (see TH1 documentation)
50@}
51*/
52
53/** \class TH2
54 Service class for 2-D histogram classes
55
56- TH2C a 2-D histogram with one byte per cell (char). Maximum bin content = 127
57- TH2S a 2-D histogram with two bytes per cell (short integer). Maximum bin content = 32767
58- TH2I a 2-D histogram with four bytes per cell (32 bit integer). Maximum bin content = INT_MAX (\ref intmax2 "*")
59- TH2L a 2-D histogram with eight bytes per cell (64 bit integer). Maximum bin content = LLONG_MAX (\ref llongmax2 "**")
60- TH2F a 2-D histogram with four bytes per cell (float). Maximum precision 7 digits, maximum integer bin content = +/-16777216 (\ref floatmax2 "***")
61- TH2D a 2-D histogram with eight bytes per cell (double). Maximum precision 14 digits, maximum integer bin content = +/-9007199254740992 (\ref doublemax2 "****")
62
63<sup>
64\anchor intmax2 (*) 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>
65\anchor llongmax2 (**) 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>
66\anchor floatmax2 (***) 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>
67\anchor doublemax2 (****) 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)
68</sup>
69
70*/
71
72
73////////////////////////////////////////////////////////////////////////////////
74/// 2-D histogram default constructor.
75
77{
78 fDimension = 2;
79 fScalefactor = 1;
81}
83////////////////////////////////////////////////////////////////////////////////
84/// Constructor for fix bin size 2-D histograms.
85/// Creates the main histogram structure.
86///
87/// \param[in] name name of histogram (avoid blanks)
88/// \param[in] title histogram title.
89/// If title is of the form `stringt;stringx;stringy;stringz`,
90/// the histogram title is set to `stringt`,
91/// the x axis title to `stringx`, the y axis title to `stringy`, etc.
92/// \param[in] nbinsx number of bins along the X axis
93/// \param[in] xlow low edge of the X axis first bin
94/// \param[in] xup upper edge of the X axis last bin (not included in last bin)
95/// \param[in] nbinsy number of bins along the Y axis
96/// \param[in] ylow low edge of the Y axis first bin
97/// \param[in] yup upper edge of the Y axis last bin (not included in last bin)
98/// \note if xup <= xlow or yup <= ylow, automatic bins are calculated when buffer size is reached
99
100TH2::TH2(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_t xup
101 ,Int_t nbinsy,Double_t ylow,Double_t yup)
102 :TH1(name,title,nbinsx,xlow,xup)
103{
104 fDimension = 2;
105 fScalefactor = 1;
106 fTsumwy = fTsumwy2 = fTsumwxy = 0;
107 if (nbinsy <= 0) {Warning("TH2","nbinsy is <=0 - set to nbinsy = 1"); nbinsy = 1; }
108 fYaxis.Set(nbinsy,ylow,yup);
109 fNcells = fNcells*(nbinsy+2); // fNCells is set in the TH1 constructor
110}
111
113////////////////////////////////////////////////////////////////////////////////
114/// Constructor for variable bin size (along X axis) 2-D histograms using an input array
115/// of type double.
116///
117/// \param[in] name name of histogram (avoid blanks)
118/// \param[in] title histogram title.
119/// If title is of the form `stringt;stringx;stringy;stringz`
120/// the histogram title is set to `stringt`,
121/// the x axis title to `stringx`, the y axis title to `stringy`, etc.
122/// \param[in] nbinsx number of bins
123/// \param[in] xbins array of low-edges for each bin.
124/// This is an array of type double and size nbinsx+1
125/// \param[in] nbinsy number of bins along the Y axis
126/// \param[in] ylow low edge of the Y axis first bin
127/// \param[in] yup upper edge of the Y axis last bin (not included in last bin)
128
129TH2::TH2(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
130 ,Int_t nbinsy,Double_t ylow,Double_t yup)
131 :TH1(name,title,nbinsx,xbins)
132{
133 fDimension = 2;
134 fScalefactor = 1;
135 fTsumwy = fTsumwy2 = fTsumwxy = 0;
136 if (nbinsy <= 0) {Warning("TH2","nbinsy is <=0 - set to nbinsy = 1"); nbinsy = 1; }
137 fYaxis.Set(nbinsy,ylow,yup);
138 fNcells = fNcells*(nbinsy+2); // fNCells is set in the TH1 constructor
139}
140
141
142////////////////////////////////////////////////////////////////////////////////
143/// Constructor for Double_t variable bin size (along Y axis) 2-D histograms.
144///
145/// \param[in] name name of histogram (avoid blanks)
146/// \param[in] title histogram title.
147/// If title is of the form `stringt;stringx;stringy;stringz`
148/// the histogram title is set to `stringt`,
149/// the x axis title to `stringx`, the y axis title to `stringy`, etc.
150/// \param[in] nbinsx number of bins along the X axis
151/// \param[in] xlow low edge of the X axis first bin
152/// \param[in] xup upper edge of the X axis last bin (not included in last bin)
153/// \param[in] nbinsy number of bins
154/// \param[in] ybins array of low-edges for each bin.
155/// This is an array of type double and size nbinsy+1
156
157TH2::TH2(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_t xup
158 ,Int_t nbinsy,const Double_t *ybins)
159 :TH1(name,title,nbinsx,xlow,xup)
160{
161 fDimension = 2;
162 fScalefactor = 1;
163 fTsumwy = fTsumwy2 = fTsumwxy = 0;
164 if (nbinsy <= 0) {Warning("TH2","nbinsy is <=0 - set to nbinsy = 1"); nbinsy = 1; }
166 else fYaxis.Set(nbinsy,0,1);
167 fNcells = fNcells*(nbinsy+2); // fNCells is set in the TH1 constructor
168}
169
170
171////////////////////////////////////////////////////////////////////////////////
172/// Constructor for Double_t variable bin size 2-D histograms.
173///
174/// \param[in] name name of histogram (avoid blanks)
175/// \param[in] title histogram title.
176/// If title is of the form `stringt;stringx;stringy;stringz`
177/// the histogram title is set to `stringt`,
178/// the x axis title to `stringx`, the y axis title to `stringy`, etc.
179/// \param[in] nbinsx number of bins
180/// \param[in] xbins array of low-edges for each bin.
181/// This is an array of type double and size nbinsx+1
182/// \param[in] nbinsy number of bins
183/// \param[in] ybins array of low-edges for each bin.
184/// This is an array of type double and size nbinsy+1
185
186TH2::TH2(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
187 ,Int_t nbinsy,const Double_t *ybins)
188 :TH1(name,title,nbinsx,xbins)
189{
190 fDimension = 2;
191 fScalefactor = 1;
192 fTsumwy = fTsumwy2 = fTsumwxy = 0;
193 if (nbinsy <= 0) {Warning("TH2","nbinsy is <=0 - set to nbinsy = 1"); nbinsy = 1; }
195 else fYaxis.Set(nbinsy,0,1);
196 fNcells = fNcells*(nbinsy+2); // fNCells is set in the TH1 constructor
197}
198
199
200////////////////////////////////////////////////////////////////////////////////
201/// Constructor for variable bin size (along X and Y axis) 2-D histograms using input
202/// arrays of type float.
203///
204/// \param[in] name name of histogram (avoid blanks)
205/// \param[in] title histogram title.
206/// If title is of the form `stringt;stringx;stringy;stringz`
207/// the histogram title is set to `stringt`,
208/// the x axis title to `stringx`, the y axis title to `stringy`, etc.
209/// \param[in] nbinsx number of bins
210/// \param[in] xbins array of low-edges for each bin.
211/// This is an array of type float and size nbinsx+1
212/// \param[in] nbinsy number of bins
213/// \param[in] ybins array of low-edges for each bin.
214/// This is an array of type float and size nbinsy+1
215
216TH2::TH2(const char *name,const char *title,Int_t nbinsx,const Float_t *xbins
217 ,Int_t nbinsy,const Float_t *ybins)
218 :TH1(name,title,nbinsx,xbins)
219{
220 fDimension = 2;
221 fScalefactor = 1;
222 fTsumwy = fTsumwy2 = fTsumwxy = 0;
223 if (nbinsy <= 0) {Warning("TH2","nbinsy is <=0 - set to nbinsy = 1"); nbinsy = 1; }
225 else fYaxis.Set(nbinsy,0,1);
226 fNcells = fNcells*(nbinsy+2); // fNCells is set in the TH1 constructor.
227}
228
229
230////////////////////////////////////////////////////////////////////////////////
231/// Destructor.
232
234{
235}
236
237////////////////////////////////////////////////////////////////////////////////
238/// Fill histogram with all entries in the buffer.
239/// - action = -1 histogram is reset and refilled from the buffer (called by THistPainter::Paint)
240/// - action = 0 histogram is filled from the buffer
241/// - action = 1 histogram is filled and buffer is deleted
242/// The buffer is automatically deleted when the number of entries
243/// in the buffer is greater than the number of entries in the histogram
244
246{
247 // do we need to compute the bin size?
248 if (!fBuffer) return 0;
250
251 // nbentries correspond to the number of entries of histogram
252
253 if (nbentries == 0) return 0;
254 if (nbentries < 0 && action == 0) return 0; // case histogram has been already filled from the buffer
255
256 Double_t *buffer = fBuffer;
257 if (nbentries < 0) {
259 // a reset might call BufferEmpty() giving an infinite loop
260 // Protect it by setting fBuffer = 0
261 fBuffer=nullptr;
262 //do not reset the list of functions
263 Reset("ICES");
264 fBuffer = buffer;
265 }
266
267 const bool xbinAuto = fXaxis.GetXmax() <= fXaxis.GetXmin();
268 const bool ybinAuto = fYaxis.GetXmax() <= fYaxis.GetXmin();
269 const bool extend = CanExtendAllAxes();
270 if (extend || xbinAuto || ybinAuto) {
271 //find min, max of entries in buffer
272 Double_t xmin = xbinAuto || extend ? fBuffer[2] : fXaxis.GetXmin();
273 Double_t xmax = xbinAuto || extend ? xmin : fXaxis.GetXmax();
274 Double_t ymin = ybinAuto || extend ? fBuffer[3] : fYaxis.GetXmin();
275 Double_t ymax = ybinAuto || extend ? ymin : fYaxis.GetXmax();
276 for (Int_t i=1;i<nbentries;i++) {
277 if (extend || xbinAuto) {
278 Double_t x = fBuffer[3*i+2];
279 if (x < xmin) xmin = x;
280 if (x > xmax) xmax = x;
281 }
282 if (extend || ybinAuto) {
283 Double_t y = fBuffer[3*i+3];
284 if (y < ymin) ymin = y;
285 if (y > ymax) ymax = y;
286 }
287 }
288 if (xbinAuto || ybinAuto) {
289 THLimitsFinder::GetLimitsFinder()->FindGoodLimitsXY(
290 this, xmin, xmax, ymin, ymax, xbinAuto ? 0 : fXaxis.GetNbins(), ybinAuto ? 0 : fYaxis.GetNbins());
291 } else {
292 fBuffer = nullptr;
298 fBuffer = buffer;
300 }
301 }
302
303 fBuffer = nullptr;
304 for (Int_t i=0;i<nbentries;i++) {
305 Fill(buffer[3*i+2],buffer[3*i+3],buffer[3*i+1]);
306 }
307 fBuffer = buffer;
308
309 if (action > 0) { delete [] fBuffer; fBuffer = nullptr; fBufferSize = 0;}
310 else {
312 else fBuffer[0] = 0;
313 }
314 return nbentries;
315}
316
317
318////////////////////////////////////////////////////////////////////////////////
319/// accumulate arguments in buffer. When buffer is full, empty the buffer
320/// ~~~ {.cpp}
321/// fBuffer[0] = number of entries in buffer
322/// fBuffer[1] = w of first entry
323/// fBuffer[2] = x of first entry
324/// fBuffer[3] = y of first entry
325/// ~~~
326
328{
329 if (!fBuffer) return -3;
331 if (nbentries < 0) {
333 fBuffer[0] = nbentries;
334 if (fEntries > 0) {
335 Double_t *buffer = fBuffer; fBuffer=nullptr;
336 Reset("ICES");
337 fBuffer = buffer;
338 }
339 }
340 if (3*nbentries+3 >= fBufferSize) {
341 BufferEmpty(1);
342 return Fill(x,y,w);
343 }
344 fBuffer[3*nbentries+1] = w;
345 fBuffer[3*nbentries+2] = x;
346 fBuffer[3*nbentries+3] = y;
347 fBuffer[0] += 1;
348 return -3;
349}
350
351
352////////////////////////////////////////////////////////////////////////////////
353/// Copy.
354
355void TH2::Copy(TObject &obj) const
356{
357 TH1::Copy(obj);
358 ((TH2&)obj).fScalefactor = fScalefactor;
359 ((TH2&)obj).fTsumwy = fTsumwy;
360 ((TH2&)obj).fTsumwy2 = fTsumwy2;
361 ((TH2&)obj).fTsumwxy = fTsumwxy;
362}
363
364
365////////////////////////////////////////////////////////////////////////////////
366/// Invalid Fill method.
367
369{
370 Error("Fill", "Invalid signature - do nothing");
371 return -1;
372}
373
374
375////////////////////////////////////////////////////////////////////////////////
376/// Increment cell defined by x,y by 1.
377///
378/// - if x or/and y is less than the low-edge of the corresponding axis first bin,
379/// the Underflow cell is incremented.
380/// - if x or/and y is equal to or greater than the upper edge of corresponding axis last bin,
381/// the Overflow cell is incremented.
382///
383/// - If the storage of the sum of squares of weights has been triggered,
384/// via the function Sumw2, then the sum of the squares of weights is incremented
385/// by 1 in the cell corresponding to x,y.
386///
387/// The function returns the corresponding global bin number which has its content
388/// incremented by 1
389
391{
392 if (fBuffer) return BufferFill(x,y,1);
393
394 Int_t binx, biny, bin;
395 fEntries++;
396 binx = fXaxis.FindBin(x);
397 biny = fYaxis.FindBin(y);
398 if (binx <0 || biny <0) return -1;
399 bin = biny*(fXaxis.GetNbins()+2) + binx;
401 if (fSumw2.fN) ++fSumw2.fArray[bin];
402 if (binx == 0 || binx > fXaxis.GetNbins()) {
403 if (!GetStatOverflowsBehaviour()) return -1;
404 }
405 if (biny == 0 || biny > fYaxis.GetNbins()) {
406 if (!GetStatOverflowsBehaviour()) return -1;
407 }
408 ++fTsumw;
409 ++fTsumw2;
410 fTsumwx += x;
411 fTsumwx2 += x*x;
412 fTsumwy += y;
413 fTsumwy2 += y*y;
414 fTsumwxy += x*y;
415 return bin;
416}
417
418
419////////////////////////////////////////////////////////////////////////////////
420/// Increment cell defined by x,y by a weight w.
421///
422/// - if x or/and y is less than the low-edge of the corresponding axis first bin,
423/// the Underflow cell is incremented.
424/// - if x or/and y is equal to or greater than the upper edge of corresponding axis last bin,
425/// the Overflow cell is incremented.
426///
427/// - If the weight is not equal to 1, the storage of the sum of squares of
428/// weights is automatically triggered and the sum of the squares of weights is incremented
429/// by w^2 in the bin corresponding to x,y
430///
431/// The function returns the corresponding global bin number which has its content
432/// incremented by w
433
435{
436 if (fBuffer) return BufferFill(x,y,w);
437
438 Int_t binx, biny, bin;
439 fEntries++;
440 binx = fXaxis.FindBin(x);
441 biny = fYaxis.FindBin(y);
442 if (binx <0 || biny <0) return -1;
443 bin = biny*(fXaxis.GetNbins()+2) + binx;
444 if (!fSumw2.fN && w != 1.0 && !TestBit(TH1::kIsNotW)) Sumw2(); // must be called before AddBinContent
445 if (fSumw2.fN) fSumw2.fArray[bin] += w*w;
447 if (binx == 0 || binx > fXaxis.GetNbins()) {
448 if (!GetStatOverflowsBehaviour()) return -1;
449 }
450 if (biny == 0 || biny > fYaxis.GetNbins()) {
451 if (!GetStatOverflowsBehaviour()) return -1;
452 }
453 Double_t z= w;
454 fTsumw += z;
455 fTsumw2 += z*z;
456 fTsumwx += z*x;
457 fTsumwx2 += z*x*x;
458 fTsumwy += z*y;
459 fTsumwy2 += z*y*y;
460 fTsumwxy += z*x*y;
461 return bin;
462}
463
464
465////////////////////////////////////////////////////////////////////////////////
466/// Increment cell defined by namex,namey by a weight w
467///
468/// - if x or/and y is less than the low-edge of the corresponding axis first bin,
469/// the Underflow cell is incremented.
470/// - if x or/and y is equal to or greater than the upper edge of corresponding axis last bin,
471/// the Overflow cell is incremented.
472///
473/// - If the weight is not equal to 1, the storage of the sum of squares of
474/// weights is automatically triggered and the sum of the squares of weights is incremented
475/// by w^2 in the bin corresponding to namex,namey
476///
477/// The function returns the corresponding global bin number which has its content
478/// incremented by w
479
480Int_t TH2::Fill(const char *namex, const char *namey, Double_t w)
481{
482 Int_t binx, biny, bin;
483 fEntries++;
486 if (binx <0 || biny <0) return -1;
487 bin = biny*(fXaxis.GetNbins()+2) + binx;
488 if (!fSumw2.fN && w != 1.0 && !TestBit(TH1::kIsNotW)) Sumw2(); // must be called before AddBinContent
489 if (fSumw2.fN) fSumw2.fArray[bin] += w*w;
491 if (binx == 0 || binx > fXaxis.GetNbins()) return -1;
492 if (biny == 0 || biny > fYaxis.GetNbins()) return -1;
493
494 Double_t z= w;
495 fTsumw += z;
496 fTsumw2 += z*z;
497 // skip computation of the statistics along axis that have labels (can be extended and are alphanumeric)
502 fTsumwx += z * x;
503 fTsumwx2 += z * x * x;
504 fTsumwy += z * y;
505 fTsumwy2 += z * y * y;
506 fTsumwxy += z * x * y;
507 }
508 return bin;
509}
510
511
512////////////////////////////////////////////////////////////////////////////////
513/// Increment cell defined by namex,y by a weight w
514///
515/// - if x or/and y is less than the low-edge of the corresponding axis first bin,
516/// the Underflow cell is incremented.
517/// - if x or/and y is equal to or greater than the upper edge of corresponding axis last bin,
518/// the Overflow cell is incremented.
519///
520/// - If the weight is not equal to 1, the storage of the sum of squares of
521/// weights is automatically triggered and the sum of the squares of weights is incremented
522/// by w^2 in the bin corresponding to namex,y
523///
524/// The function returns the corresponding global bin number which has its content
525/// incremented by w
526
528{
529 Int_t binx, biny, bin;
530 fEntries++;
532 biny = fYaxis.FindBin(y);
533 if (binx <0 || biny <0) return -1;
534 bin = biny*(fXaxis.GetNbins()+2) + binx;
535 if (!fSumw2.fN && w != 1.0 && !TestBit(TH1::kIsNotW)) Sumw2(); // must be called before AddBinContent
536 if (fSumw2.fN) fSumw2.fArray[bin] += w*w;
538 if (binx == 0 || binx > fXaxis.GetNbins()) return -1;
539 if (biny == 0 || biny > fYaxis.GetNbins()) {
540 if (!GetStatOverflowsBehaviour()) return -1;
541 }
542 Double_t z= w; //(w > 0 ? w : -w);
543 fTsumw += z;
544 fTsumw2 += z*z;
545 fTsumwy += z*y;
546 fTsumwy2 += z*y*y;
547 // skip statistics along x axis, for only one axis no need to use bit mask from GetAxisLabelStatus
548 if (!fXaxis.CanExtend() || !fXaxis.IsAlphanumeric()) {
550 fTsumwx += z * x;
551 fTsumwx2 += z * x * x;
552 fTsumwxy += z * x * y;
553 }
554 return bin;
555}
556
557
558////////////////////////////////////////////////////////////////////////////////
559/// Increment cell defined by x,namey by a weight w
560///
561/// - if x or/and y is less than the low-edge of the corresponding axis first bin,
562/// the Underflow cell is incremented.
563/// - if x or/and y is equal to or greater than the upper edge of corresponding axis last bin,
564/// the Overflow cell is incremented.
565///
566/// - If the weight is not equal to 1, the storage of the sum of squares of
567/// weights is automatically triggered and the sum of the squares of weights is incremented
568/// by w^2 in the bin corresponding to x,y.
569///
570/// The function returns the corresponding global bin number which has its content
571/// incremented by w
572
574{
575 Int_t binx, biny, bin;
576 fEntries++;
577 binx = fXaxis.FindBin(x);
579 if (binx <0 || biny <0) return -1;
580 bin = biny*(fXaxis.GetNbins()+2) + binx;
581 if (!fSumw2.fN && w != 1.0 && !TestBit(TH1::kIsNotW)) Sumw2(); // must be called before AddBinContent
582 if (fSumw2.fN) fSumw2.fArray[bin] += w*w;
584 if (binx == 0 || binx > fXaxis.GetNbins()) {
585 if (!GetStatOverflowsBehaviour()) return -1;
586 }
587 if (biny == 0 || biny > fYaxis.GetNbins()) return -1;
588
589 Double_t z= w; //(w > 0 ? w : -w);
590 fTsumw += z;
591 fTsumw2 += z*z;
592 fTsumwx += z*x;
593 fTsumwx2 += z*x*x;
594 // skip statistics along y axis
595 if (!fYaxis.CanExtend() || !fYaxis.IsAlphanumeric()) {
597 fTsumwy += z * y;
598 fTsumwy2 += z * y * y;
599 fTsumwxy += z * x * y;
600 }
601 return bin;
602}
603
604
605////////////////////////////////////////////////////////////////////////////////
606/// Fill a 2-D histogram with an array of values and weights.
607///
608/// - ntimes: number of entries in arrays x and w (array size must be ntimes*stride)
609/// - x: array of x values to be histogrammed
610/// - y: array of y values to be histogrammed
611/// - w: array of weights
612/// - stride: step size through arrays x, y and w
613///
614/// - If the weight is not equal to 1, the storage of the sum of squares of
615/// weights is automatically triggered and the sum of the squares of weights is incremented
616/// by w[i]^2 in the bin corresponding to x[i],y[i].
617/// - If w is NULL each entry is assumed a weight=1
618///
619/// NB: function only valid for a TH2x object
620
622{
623 Int_t binx, biny, bin, i;
624 ntimes *= stride;
625 Int_t ifirst = 0;
626
627 //If a buffer is activated, fill buffer
628 // (note that this function must not be called from TH2::BufferEmpty)
629 if (fBuffer) {
630 for (i=0;i<ntimes;i+=stride) {
631 if (!fBuffer) break; // buffer can be deleted in BufferFill when is empty
632 if (w) BufferFill(x[i],y[i],w[i]);
633 else BufferFill(x[i], y[i], 1.);
634 }
635 // fill the remaining entries if the buffer has been deleted
636 if (i < ntimes && fBuffer==nullptr)
637 ifirst = i;
638 else
639 return;
640 }
641
642 Double_t ww = 1;
643 for (i=ifirst;i<ntimes;i+=stride) {
644 fEntries++;
645 binx = fXaxis.FindBin(x[i]);
646 biny = fYaxis.FindBin(y[i]);
647 if (binx <0 || biny <0) continue;
648 bin = biny*(fXaxis.GetNbins()+2) + binx;
649 if (w) ww = w[i];
650 if (!fSumw2.fN && ww != 1.0 && !TestBit(TH1::kIsNotW)) Sumw2();
651 if (fSumw2.fN) fSumw2.fArray[bin] += ww*ww;
652 AddBinContent(bin,ww);
653 if (binx == 0 || binx > fXaxis.GetNbins()) {
654 if (!GetStatOverflowsBehaviour()) continue;
655 }
656 if (biny == 0 || biny > fYaxis.GetNbins()) {
657 if (!GetStatOverflowsBehaviour()) continue;
658 }
659 Double_t z= ww; //(ww > 0 ? ww : -ww);
660 fTsumw += z;
661 fTsumw2 += z*z;
662 fTsumwx += z*x[i];
663 fTsumwx2 += z*x[i]*x[i];
664 fTsumwy += z*y[i];
665 fTsumwy2 += z*y[i]*y[i];
666 fTsumwxy += z*x[i]*y[i];
667 }
668}
669
670
671////////////////////////////////////////////////////////////////////////////////
672/// Fill histogram following distribution in function `function`.
673///
674/// @param function Function name used for filling the histogram
675/// @param ntimes : number of times the histogram is filled
676/// @param rng : (optional) Random number generator used to sample
677///
678/// The distribution contained in the function fname (TF2) is integrated
679/// over the channel contents.
680/// It is normalized to 1.
681/// Getting one random number implies:
682/// - Generating a random number between 0 and 1 (say r1)
683/// - Look in which bin in the normalized integral r1 corresponds to
684/// - Fill histogram channel
685/// ntimes random numbers are generated
686///
687/// One can also call TF2::GetRandom2 to get a random variate from a function.
688
690{
691 Int_t bin, binx, biny, ibin, loop;
692 Double_t r1, x, y;
693 TF2 * f1 = dynamic_cast<TF2*>(function);
694 if (!f1) { Error("FillRandom", "Function: %s is not a TF2, is a %s",function->GetName(),function->IsA()->GetName()); return; }
695
696
697 TAxis & xAxis = fXaxis;
698 TAxis & yAxis = fYaxis;
699
700 // in case axes of histogram are not defined use the function axis
701 if (fXaxis.GetXmax() <= fXaxis.GetXmin() || fYaxis.GetXmax() <= fYaxis.GetXmin()) {
704 Info("FillRandom","Using function axis and range ([%g,%g],[%g,%g])",xmin, xmax,ymin,ymax);
705 xAxis = *(f1->GetHistogram()->GetXaxis());
706 yAxis = *(f1->GetHistogram()->GetYaxis());
707 }
708
709
710 // Allocate temporary space to store the integral and compute integral
711 Int_t nbinsx = xAxis.GetNbins();
712 Int_t nbinsy = yAxis.GetNbins();
714
715
716 Double_t *integral = new Double_t[nbins+1];
717 ibin = 0;
718 integral[ibin] = 0;
719 for (biny=1;biny<=nbinsy;biny++) {
720 for (binx=1;binx<=nbinsx;binx++) {
721 ibin++;
722 Double_t fint = f1->Integral(xAxis.GetBinLowEdge(binx), xAxis.GetBinUpEdge(binx), yAxis.GetBinLowEdge(biny), yAxis.GetBinUpEdge(biny));
723 integral[ibin] = integral[ibin-1] + fint;
724 }
725 }
726
727 // Normalize integral to 1
728 if (integral[nbins] == 0 ) {
729 delete [] integral;
730 Error("FillRandom", "Integral = zero"); return;
731 }
732 for (bin=1;bin<=nbins;bin++) integral[bin] /= integral[nbins];
733
734 // Start main loop ntimes
735 for (loop=0;loop<ntimes;loop++) {
736 r1 = (rng) ? rng->Rndm() : gRandom->Rndm();
737 ibin = TMath::BinarySearch(nbins,&integral[0],r1);
738 biny = ibin/nbinsx;
739 binx = 1 + ibin - nbinsx*biny;
740 biny++;
741 x = xAxis.GetBinCenter(binx);
742 y = yAxis.GetBinCenter(biny);
743 Fill(x,y);
744 }
745 delete [] integral;
746}
747
748
749////////////////////////////////////////////////////////////////////////////////
750/// Fill histogram following distribution in histogram h.
751///
752/// @param h : Histogram pointer used for sampling random number
753/// @param ntimes : number of times the histogram is filled
754/// @param rng : (optional) Random number generator used for sampling
755///
756/// The distribution contained in the histogram h (TH2) is integrated
757/// over the channel contents.
758/// It is normalized to 1.
759/// Getting one random number implies:
760/// - Generating a random number between 0 and 1 (say r1)
761/// - Look in which bin in the normalized integral r1 corresponds to
762/// - Fill histogram channel
763/// ntimes random numbers are generated
764
766{
767 if (!h) { Error("FillRandom", "Null histogram"); return; }
768 if (fDimension != h->GetDimension()) {
769 Error("FillRandom", "Histograms with different dimensions"); return;
770 }
771
772 if (h->ComputeIntegral() == 0) return;
773
774 Int_t loop;
775 Double_t x,y;
776 TH2 *h2 = (TH2*)h;
777 for (loop=0;loop<ntimes;loop++) {
778 h2->GetRandom2(x,y,rng);
779 Fill(x,y);
780 }
781}
782
783
784////////////////////////////////////////////////////////////////////////////////
785
788{
791
792 Int_t nbins = outerAxis.GetNbins();
793 // get correct first last bins for outer axis
794 // when using default values (0,-1) check if an axis range is set in outer axis
795 // do same as in DoProjection for inner axis
796 if ( lastbin < firstbin && outerAxis.TestBit(TAxis::kAxisRange) ) {
797 firstbin = outerAxis.GetFirst();
798 lastbin = outerAxis.GetLast();
799 // For special case of TAxis::SetRange, when first == 1 and last
800 // = N and the range bit has been set, the TAxis will return 0
801 // for both.
802 if (firstbin == 0 && lastbin == 0) {
803 firstbin = 1;
804 lastbin = nbins;
805 }
806 }
807 if (firstbin < 0) firstbin = 0;
809 if (lastbin < firstbin) {firstbin = 0; lastbin = nbins + 1;}
810
811
812 TString opt = option;
813 TString proj_opt = "e";
814 Int_t i1 = opt.Index("[");
815 Int_t i2 = opt.Index("]");
816 if (i1>=0 && i2>i1) {
817 proj_opt += opt(i1,i2-i1+1);
818 opt.Remove(i1, i2-i1+1);
819 }
820 opt.ToLower();
821 Int_t ngroup = 1;
822 if (opt.Contains("g2")) {ngroup = 2; opt.ReplaceAll("g2","");}
823 if (opt.Contains("g3")) {ngroup = 3; opt.ReplaceAll("g3","");}
824 if (opt.Contains("g4")) {ngroup = 4; opt.ReplaceAll("g4","");}
825 if (opt.Contains("g5")) {ngroup = 5; opt.ReplaceAll("g5","");}
826
827 // implement option S sliding merge for each bin using in conjunction with a given Gn
829 if (opt.Contains("s")) nstep = 1;
830
831 //default is to fit with a gaussian
832 if (f1 == nullptr) {
833 f1 = (TF1*)gROOT->GetFunction("gaus");
834 if (f1 == nullptr) f1 = new TF1("gaus","gaus",innerAxis.GetXmin(),innerAxis.GetXmax());
835 else f1->SetRange(innerAxis.GetXmin(),innerAxis.GetXmax());
836 }
837 Int_t npar = f1->GetNpar();
838 if (npar <= 0) return;
841
842 if (arr) {
843 arr->SetOwner();
844 arr->Expand(npar + 1);
845 }
846
847 //Create one histogram for each function parameter
848 Int_t ipar;
849 TH1D **hlist = new TH1D*[npar];
850 char *name = new char[2000];
851 char *title = new char[2000];
852 const TArrayD *bins = outerAxis.GetXbins();
853 // outer axis boundaries used for creating reported histograms are different
854 // than the limits used in the projection loop (firstbin,lastbin)
855 Int_t firstOutBin = outerAxis.TestBit(TAxis::kAxisRange) ? std::max(firstbin,1) : 1;
856 Int_t lastOutBin = outerAxis.TestBit(TAxis::kAxisRange) ? std::min(lastbin,outerAxis.GetNbins() ) : outerAxis.GetNbins();
858 // merge bins if use nstep > 1 and fixed bins
859 if (bins->fN == 0) nOutBins /= nstep;
860 for (ipar=0;ipar<npar;ipar++) {
861 snprintf(name,2000,"%s_%d",GetName(),ipar);
862 snprintf(title,2000,"Fitted value of par[%d]=%s",ipar,f1->GetParName(ipar));
863 delete gDirectory->FindObject(name);
864 if (bins->fN == 0) {
865 hlist[ipar] = new TH1D(name,title, nOutBins, outerAxis.GetBinLowEdge(firstOutBin), outerAxis.GetBinUpEdge(lastOutBin));
866 } else {
867 hlist[ipar] = new TH1D(name,title, nOutBins, &bins->fArray[firstOutBin-1]);
868 }
869 hlist[ipar]->SetDirectory(gDirectory);
870 hlist[ipar]->GetXaxis()->SetTitle(outerAxis.GetTitle());
871 if (arr)
872 (*arr)[ipar] = hlist[ipar];
873 }
874 snprintf(name,2000,"%s_chi2",GetName());
875 delete gDirectory->FindObject(name);
876 TH1D *hchi2 = nullptr;
877 if (bins->fN == 0) {
878 hchi2 = new TH1D(name,"chisquare", nOutBins, outerAxis.GetBinLowEdge(firstOutBin), outerAxis.GetBinUpEdge(lastOutBin));
879 } else {
880 hchi2 = new TH1D(name,"chisquare", nOutBins, &bins->fArray[firstOutBin-1]);
881 }
882 hchi2->SetDirectory(gDirectory);
883 hchi2->GetXaxis()->SetTitle(outerAxis.GetTitle());
884 if (arr)
885 (*arr)[npar] = hchi2;
886
887 //Loop on all bins in Y, generate a projection along X
888 Int_t bin;
889 // in case of sliding merge nstep=1, i.e. do slices starting for every bin
890 // now do not slices case with overflow (makes more sense)
891 // when fitting add the option "N". We don;t want to display and store the function
892 // for the temporary histograms that are created and fitted
893 opt += " n ";
894 TH1D *hp = nullptr;
895 for (bin=firstbin;bin+ngroup-1<=lastbin;bin += nstep) {
896 if (onX)
897 hp= ProjectionX("_temp",bin,bin+ngroup-1,proj_opt);
898 else
899 hp= ProjectionY("_temp",bin,bin+ngroup-1,proj_opt);
900 if (hp == nullptr) continue;
901 // nentries can be the effective entries and it could be a very small number but not zero!
902 Double_t nentries = hp->GetEntries();
903 if ( nentries <= 0 || nentries < cut) {
904 if (!opt.Contains("q"))
905 Info("DoFitSlices","Slice %d skipped, the number of entries is zero or smaller than the given cut value, n=%f",bin,nentries);
906 continue;
907 }
909 Int_t binOn = hlist[0]->FindBin(outerAxis.GetBinCenter(bin+ngroup/2));
910 if (!opt.Contains("q"))
911 Info("DoFitSlices","Slice fit %d (%f,%f)",binOn,hlist[0]->GetXaxis()->GetBinLowEdge(binOn),hlist[0]->GetXaxis()->GetBinUpEdge(binOn));
912 hp->Fit(f1,opt.Data());
914 if (npfits > npar && npfits >= cut) {
915 for (ipar=0;ipar<npar;ipar++) {
916 hlist[ipar]->SetBinContent(binOn,f1->GetParameter(ipar));
917 hlist[ipar]->SetBinError(binOn,f1->GetParError(ipar));
918 }
919 hchi2->SetBinContent(binOn,f1->GetChisquare()/(npfits-npar));
920 }
921 else {
922 if (!opt.Contains("q"))
923 Info("DoFitSlices","Fitted slice %d skipped, the number of fitted points is too small, n=%d",bin,npfits);
924 }
925 // don't need to delete hp. If histogram has the same name it is re-used in TH2::Projection
926 }
927 delete hp;
928 delete [] parsave;
929 delete [] name;
930 delete [] title;
931 delete [] hlist;
932}
933
934
935////////////////////////////////////////////////////////////////////////////////
936/// Project slices along X in case of a 2-D histogram, then fit each slice
937/// with function f1 and make a histogram for each fit parameter
938/// Only bins along Y between firstybin and lastybin are considered.
939/// By default (firstybin == 0, lastybin == -1), all bins in y including
940/// over- and underflows are taken into account.
941/// If f1=0, a gaussian is assumed
942/// Before invoking this function, one can set a subrange to be fitted along X
943/// via f1->SetRange(xmin,xmax)
944/// The argument option (default="QNR") can be used to change the fit options.
945/// - "Q" means Quiet mode
946/// - "N" means do not show the result of the fit
947/// - "R" means fit the function in the specified function range
948/// - "G2" merge 2 consecutive bins along X
949/// - "G3" merge 3 consecutive bins along X
950/// - "G4" merge 4 consecutive bins along X
951/// - "G5" merge 5 consecutive bins along X
952/// - "S" sliding merge: merge n consecutive bins along X accordingly to what Gn is given.
953/// It makes sense when used together with a Gn option
954///
955/// The generated histograms are returned by adding them to arr, if arr is not NULL.
956/// arr's SetOwner() is called, to signal that it is the user's responsibility to
957/// delete the histograms, possibly by deleting the array.
958/// ~~~ {.cpp}
959/// TObjArray aSlices;
960/// h2->FitSlicesX(func, 0, -1, 0, "QNR", &aSlices);
961/// ~~~
962/// will already delete the histograms once aSlice goes out of scope. aSlices will
963/// contain the histogram for the i-th parameter of the fit function at aSlices[i];
964/// aSlices[n] (n being the number of parameters) contains the chi2 distribution of
965/// the fits.
966///
967/// If arr is NULL, the generated histograms are added to the list of objects
968/// in the current directory. It is the user's responsibility to delete
969/// these histograms.
970///
971/// Example: Assume a 2-d histogram h2
972/// ~~~ {.cpp}
973/// Root > h2->FitSlicesX(); produces 4 TH1D histograms
974/// with h2_0 containing parameter 0(Constant) for a Gaus fit
975/// of each bin in Y projected along X
976/// with h2_1 containing parameter 1(Mean) for a gaus fit
977/// with h2_2 containing parameter 2(StdDev) for a gaus fit
978/// with h2_chi2 containing the chisquare/number of degrees of freedom for a gaus fit
979///
980/// Root > h2->FitSlicesX(0,15,22,10);
981/// same as above, but only for bins 15 to 22 along Y
982/// and only for bins in Y for which the corresponding projection
983/// along X has more than cut bins filled.
984/// ~~~
985/// NOTE: To access the generated histograms in the current directory, do eg:
986/// ~~~ {.cpp}
987/// TH1D *h2_1 = (TH1D*)gDirectory->Get("h2_1");
988/// ~~~
989
995
996
997////////////////////////////////////////////////////////////////////////////////
998/// Project slices along Y in case of a 2-D histogram, then fit each slice
999/// with function f1 and make a histogram for each fit parameter
1000/// Only bins along X between firstxbin and lastxbin are considered.
1001/// By default (firstxbin == 0, lastxbin == -1), all bins in x including
1002/// over- and underflows are taken into account.
1003/// If f1=0, a gaussian is assumed
1004/// Before invoking this function, one can set a subrange to be fitted along Y
1005/// via f1->SetRange(ymin,ymax)
1006/// The argument option (default="QNR") can be used to change the fit options.
1007/// - "Q" means Quiet mode
1008/// - "N" means do not show the result of the fit
1009/// - "R" means fit the function in the specified function range
1010/// - "G2" merge 2 consecutive bins along Y
1011/// - "G3" merge 3 consecutive bins along Y
1012/// - "G4" merge 4 consecutive bins along Y
1013/// - "G5" merge 5 consecutive bins along Y
1014/// - "S" sliding merge: merge n consecutive bins along Y accordingly to what Gn is given.
1015/// It makes sense when used together with a Gn option
1016///
1017/// The generated histograms are returned by adding them to arr, if arr is not NULL.
1018/// arr's SetOwner() is called, to signal that it is the user's responsibility to
1019/// delete the histograms, possibly by deleting the array.
1020/// ~~~ {.cpp}
1021/// TObjArray aSlices;
1022/// h2->FitSlicesY(func, 0, -1, 0, "QNR", &aSlices);
1023/// ~~~
1024/// will already delete the histograms once aSlice goes out of scope. aSlices will
1025/// contain the histogram for the i-th parameter of the fit function at aSlices[i];
1026/// aSlices[n] (n being the number of parameters) contains the chi2 distribution of
1027/// the fits.
1028///
1029/// If arr is NULL, the generated histograms are added to the list of objects
1030/// in the current directory. It is the user's responsibility to delete
1031/// these histograms.
1032///
1033/// Example: Assume a 2-d histogram h2
1034/// ~~~ {.cpp}
1035/// Root > h2->FitSlicesY(); produces 4 TH1D histograms
1036/// with h2_0 containing parameter 0(Constant) for a Gaus fit
1037/// of each bin in X projected along Y
1038/// with h2_1 containing parameter 1(Mean) for a gaus fit
1039/// with h2_2 containing parameter 2(StdDev) for a gaus fit
1040/// with h2_chi2 containing the chisquare/number of degrees of freedom for a gaus fit
1041///
1042/// Root > h2->FitSlicesY(0,15,22,10);
1043/// same as above, but only for bins 15 to 22 along X
1044/// and only for bins in X for which the corresponding projection
1045/// along Y has more than cut bins filled.
1046/// ~~~
1047///
1048/// NOTE: To access the generated histograms in the current directory, do eg:
1049/// ~~~ {.cpp}
1050/// TH1D *h2_1 = (TH1D*)gDirectory->Get("h2_1");
1051/// ~~~
1052///
1053/// A complete example of this function is given in tutorial:fitslicesy.C.
1054
1059
1061{
1062 // See comments in TH1::GetBin
1063 Int_t ofy = fYaxis.GetNbins() + 1; // overflow bin
1064 if (biny < 0) biny = 0;
1065 if (biny > ofy) biny = ofy;
1066
1067 return TH1::GetBin(binx) + (fXaxis.GetNbins() + 2) * biny;
1068}
1069
1070
1071////////////////////////////////////////////////////////////////////////////////
1072/// compute first cell (binx,biny) in the range [firstxbin,lastxbin][firstybin,lastybin] for which
1073/// diff = abs(cell_content-c) <= maxdiff
1074/// In case several cells in the specified range with diff=0 are found
1075/// the first cell found is returned in binx,biny.
1076/// In case several cells in the specified range satisfy diff <=maxdiff
1077/// the cell with the smallest difference is returned in binx,biny.
1078/// In all cases the function returns the smallest difference.
1079///
1080/// NOTE1: if firstxbin < 0, firstxbin is set to 1
1081/// if (lastxbin < firstxbin then lastxbin is set to the number of bins in X
1082/// ie if firstxbin=1 and lastxbin=0 (default) the search is on all bins in X except
1083/// for X's under- and overflow bins.
1084/// if firstybin < 0, firstybin is set to 1
1085/// if (lastybin < firstybin then lastybin is set to the number of bins in Y
1086/// ie if firstybin=1 and lastybin=0 (default) the search is on all bins in Y except
1087/// for Y's under- and overflow bins.
1088///
1089/// NOTE2: if maxdiff=0 (default), the first cell with content=c is returned.
1090
1093{
1094 if (fDimension != 2) {
1095 binx = -1;
1096 biny = -1;
1097 Error("GetBinWithContent2","function is only valid for 2-D histograms");
1098 return 0;
1099 }
1100 if (firstxbin < 0) firstxbin = 1;
1102 if (firstybin < 0) firstybin = 1;
1104 Double_t diff, curmax = 1.e240;
1105 for (Int_t j = firstybin; j <= lastybin; j++) {
1106 for (Int_t i = firstxbin; i <= lastxbin; i++) {
1108 if (diff <= 0) {binx = i; biny=j; return diff;}
1109 if (diff < curmax && diff <= maxdiff) {curmax = diff, binx=i; biny=j;}
1110 }
1111 }
1112 return curmax;
1113}
1114
1115
1116////////////////////////////////////////////////////////////////////////////////
1117/// Return correlation factor between axis1 and axis2.
1118
1120{
1121 if (axis1 < 1 || axis2 < 1 || axis1 > 2 || axis2 > 2) {
1122 Error("GetCorrelationFactor","Wrong parameters");
1123 return 0;
1124 }
1125 if (axis1 == axis2) return 1;
1127 if (stddev1 == 0) return 0;
1129 if (stddev2 == 0) return 0;
1131}
1132
1133
1134////////////////////////////////////////////////////////////////////////////////
1135/// Return covariance between axis1 and axis2.
1136
1138{
1139 if (axis1 < 1 || axis2 < 1 || axis1 > 2 || axis2 > 2) {
1140 Error("GetCovariance","Wrong parameters");
1141 return 0;
1142 }
1143 Double_t stats[kNstat];
1144 GetStats(stats);
1145 Double_t sumw = stats[0];
1146 //Double_t sumw2 = stats[1];
1147 Double_t sumwx = stats[2];
1148 Double_t sumwx2 = stats[3];
1149 Double_t sumwy = stats[4];
1150 Double_t sumwy2 = stats[5];
1151 Double_t sumwxy = stats[6];
1152
1153 if (sumw == 0) return 0;
1154 if (axis1 == 1 && axis2 == 1) {
1156 }
1157 if (axis1 == 2 && axis2 == 2) {
1159 }
1160 return sumwxy/sumw - sumwx/sumw*sumwy/sumw;
1161}
1162
1163////////////////////////////////////////////////////////////////////////////////
1164/// Return 2 random numbers along axis x and y distributed according
1165/// to the cell-contents of this 2-D histogram.
1166///
1167/// Return a NaN if the histogram has a bin with negative content
1168///
1169/// @param[out] x reference to random generated x value
1170/// @param[out] y reference to random generated y value
1171/// @param[in] rng (optional) Random number generator pointer used (default is gRandom)
1172/// @param[in] option (optional) Set it to "width" if your non-uniform bin contents represent a density rather than
1173/// counts
1174
1176{
1180 Double_t integral;
1181 // compute integral checking that all bins have positive content (see ROOT-5894)
1182 if (fIntegral) {
1183 if (fIntegral[nbins + 1] != fEntries)
1184 integral = ComputeIntegral(true, option);
1185 else integral = fIntegral[nbins];
1186 } else {
1187 integral = ComputeIntegral(true, option);
1188 }
1189 if (integral == 0 ) { x = 0; y = 0; return;}
1190 // case histogram has negative bins
1191 if (integral == TMath::QuietNaN() ) { x = TMath::QuietNaN(); y = TMath::QuietNaN(); return;}
1192
1193 if (!rng) rng = gRandom;
1194 Double_t r1 = rng->Rndm();
1199 if (r1 > fIntegral[ibin]) x +=
1201 y = fYaxis.GetBinLowEdge(biny+1) + fYaxis.GetBinWidth(biny+1)*rng->Rndm();
1202}
1203
1204
1205////////////////////////////////////////////////////////////////////////////////
1206/// Fill the array stats from the contents of this histogram
1207/// The array stats must be correctly dimensioned in the calling program.
1208/// ~~~ {.cpp}
1209/// stats[0] = sumw
1210/// stats[1] = sumw2
1211/// stats[2] = sumwx
1212/// stats[3] = sumwx2
1213/// stats[4] = sumwy
1214/// stats[5] = sumwy2
1215/// stats[6] = sumwxy
1216/// ~~~
1217///
1218/// If no axis-subranges are specified (via TAxis::SetRange), the array stats
1219/// is simply a copy of the statistics quantities computed at filling time.
1220/// If sub-ranges are specified, the function recomputes these quantities
1221/// from the bin contents in the current axis ranges.
1222///
1223/// Note that the mean value/StdDev is computed using the bins in the currently
1224/// defined ranges (see TAxis::SetRange). By default the ranges include
1225/// all bins from 1 to nbins included, excluding underflows and overflows.
1226/// To force the underflows and overflows in the computation, one must
1227/// call the static function TH1::StatOverflows(kTRUE) before filling
1228/// the histogram.
1229
1230void TH2::GetStats(Double_t *stats) const
1231{
1232 if (fBuffer) ((TH2*)this)->BufferEmpty();
1233
1235 std::fill(stats, stats + 7, 0);
1236
1241 // include underflow/overflow if TH1::StatOverflows(kTRUE) in case no range is set on the axis
1244 if (firstBinX == 1) firstBinX = 0;
1245 if (lastBinX == fXaxis.GetNbins() ) lastBinX += 1;
1246 }
1248 if (firstBinY == 1) firstBinY = 0;
1249 if (lastBinY == fYaxis.GetNbins() ) lastBinY += 1;
1250 }
1251 }
1252 // check for labels axis. In that case corresponding statistics do not make sense and it is set to zero
1253 Bool_t labelXaxis = ((const_cast<TAxis&>(fXaxis)).GetLabels() && fXaxis.CanExtend() );
1254 Bool_t labelYaxis = ((const_cast<TAxis&>(fYaxis)).GetLabels() && fYaxis.CanExtend() );
1255
1256 for (Int_t biny = firstBinY; biny <= lastBinY; ++biny) {
1258 for (Int_t binx = firstBinX; binx <= lastBinX; ++binx) {
1260 //w = TMath::Abs(GetBinContent(bin));
1263 Double_t wx = w * x; // avoid some extra multiplications at the expense of some clarity
1264 Double_t wy = w * y;
1265
1266 stats[0] += w;
1267 stats[1] += GetBinErrorSqUnchecked(bin);
1268 stats[2] += wx;
1269 stats[3] += wx * x;
1270 stats[4] += wy;
1271 stats[5] += wy * y;
1272 stats[6] += wx * y;
1273 }
1274 }
1275 } else {
1276 stats[0] = fTsumw;
1277 stats[1] = fTsumw2;
1278 stats[2] = fTsumwx;
1279 stats[3] = fTsumwx2;
1280 stats[4] = fTsumwy;
1281 stats[5] = fTsumwy2;
1282 stats[6] = fTsumwxy;
1283 }
1284}
1285
1286
1287////////////////////////////////////////////////////////////////////////////////
1288/// Return integral of bin contents. Only bins in the bins range are considered.
1289/// By default the integral is computed as the sum of bin contents in the range.
1290/// if option "width" is specified, the integral is the sum of
1291/// the bin contents multiplied by the bin width in x and in y.
1292
1298
1299
1300////////////////////////////////////////////////////////////////////////////////
1301/// Return integral of bin contents in range [firstxbin,lastxbin],[firstybin,lastybin]
1302/// for a 2-D histogram
1303/// By default the integral is computed as the sum of bin contents in the range.
1304/// if option "width" is specified, the integral is the sum of
1305/// the bin contents multiplied by the bin width in x and in y.
1306
1312
1313////////////////////////////////////////////////////////////////////////////////
1314/// Return integral of bin contents in range [firstxbin,lastxbin],[firstybin,lastybin]
1315/// for a 2-D histogram. Calculates also the integral error using error propagation
1316/// from the bin errors assuming that all the bins are uncorrelated.
1317/// By default the integral is computed as the sum of bin contents in the range.
1318/// if option "width" is specified, the integral is the sum of
1319/// the bin contents multiplied by the bin width in x and in y.
1320
1325
1326////////////////////////////////////////////////////////////////////////////////
1327///illegal for a TH2
1328
1330{
1331 Error("Interpolate","This function must be called with 2 arguments for a TH2");
1332 return 0;
1333}
1334
1335////////////////////////////////////////////////////////////////////////////////
1336/// Given a point P(x,y), Interpolate approximates the value via bilinear
1337/// interpolation based on the four nearest bin centers
1338/// see Wikipedia, Bilinear Interpolation
1339/// Andy Mastbaum 10/8/2008
1340/// vaguely based on R.Raja 6-Sep-2008
1341
1343{
1344 Double_t f=0;
1345 Double_t x1=0,x2=0,y1=0,y2=0;
1346 Double_t dx,dy;
1350 Error("Interpolate","Cannot interpolate outside histogram domain.");
1351 return 0;
1352 }
1353 Int_t quadrant = 0; // CCW from UR 1,2,3,4
1354 // which quadrant of the bin (bin_P) are we in?
1358 quadrant = 1; // upper right
1360 quadrant = 2; // upper left
1362 quadrant = 3; // lower left
1364 quadrant = 4; // lower right
1365 switch(quadrant) {
1366 case 1:
1371 break;
1372 case 2:
1377 break;
1378 case 3:
1383 break;
1384 case 4:
1389 break;
1390 }
1392 if(bin_x1<1) bin_x1=1;
1396 if(bin_y1<1) bin_y1=1;
1407 Double_t d = 1.0*(x2-x1)*(y2-y1);
1408 f = 1.0*q11/d*(x2-x)*(y2-y)+1.0*q21/d*(x-x1)*(y2-y)+1.0*q12/d*(x2-x)*(y-y1)+1.0*q22/d*(x-x1)*(y-y1);
1409 return f;
1410}
1411
1412
1413////////////////////////////////////////////////////////////////////////////////
1414///illegal for a TH2
1415
1417{
1418 Error("Interpolate","This function must be called with 2 arguments for a TH2");
1419 return 0;
1420}
1421
1422
1423////////////////////////////////////////////////////////////////////////////////
1424/// Statistical test of compatibility in shape between
1425/// THIS histogram and h2, using Kolmogorov test.
1426/// Default: Ignore under- and overflow bins in comparison
1427///
1428/// option is a character string to specify options
1429/// - "U" include Underflows in test
1430/// - "O" include Overflows
1431/// - "N" include comparison of normalizations
1432/// - "D" Put out a line of "Debug" printout
1433/// - "M" Return the Maximum Kolmogorov distance instead of prob
1434///
1435/// The returned function value is the probability of test
1436/// (much less than one means NOT compatible)
1437///
1438/// The KS test uses the distance between the pseudo-CDF's obtained
1439/// from the histogram. Since in 2D the order for generating the pseudo-CDF is
1440/// arbitrary, two pairs of pseudo-CDF are used, one starting from the x axis the
1441/// other from the y axis and the maximum distance is the average of the two maximum
1442/// distances obtained.
1443///
1444/// Code adapted by Rene Brun from original HBOOK routine HDIFF
1445
1447{
1448 TString opt = option;
1449 opt.ToUpper();
1450
1451 Double_t prb = 0;
1452 TH1 *h1 = (TH1*)this;
1453 if (h2 == nullptr) return 0;
1454 const TAxis *xaxis1 = h1->GetXaxis();
1455 const TAxis *xaxis2 = h2->GetXaxis();
1456 const TAxis *yaxis1 = h1->GetYaxis();
1457 const TAxis *yaxis2 = h2->GetYaxis();
1458 Int_t ncx1 = xaxis1->GetNbins();
1459 Int_t ncx2 = xaxis2->GetNbins();
1460 Int_t ncy1 = yaxis1->GetNbins();
1461 Int_t ncy2 = yaxis2->GetNbins();
1462
1463 // Check consistency of dimensions
1464 if (h1->GetDimension() != 2 || h2->GetDimension() != 2) {
1465 Error("KolmogorovTest","Histograms must be 2-D\n");
1466 return 0;
1467 }
1468
1469 // Check consistency in number of channels
1470 if (ncx1 != ncx2) {
1471 Error("KolmogorovTest","Number of channels in X is different, %d and %d\n",ncx1,ncx2);
1472 return 0;
1473 }
1474 if (ncy1 != ncy2) {
1475 Error("KolmogorovTest","Number of channels in Y is different, %d and %d\n",ncy1,ncy2);
1476 return 0;
1477 }
1478
1479 // Check consistency in channel edges
1482 Double_t difprec = 1e-5;
1483 Double_t diff1 = TMath::Abs(xaxis1->GetXmin() - xaxis2->GetXmin());
1484 Double_t diff2 = TMath::Abs(xaxis1->GetXmax() - xaxis2->GetXmax());
1485 if (diff1 > difprec || diff2 > difprec) {
1486 Error("KolmogorovTest","histograms with different binning along X");
1487 return 0;
1488 }
1489 diff1 = TMath::Abs(yaxis1->GetXmin() - yaxis2->GetXmin());
1490 diff2 = TMath::Abs(yaxis1->GetXmax() - yaxis2->GetXmax());
1491 if (diff1 > difprec || diff2 > difprec) {
1492 Error("KolmogorovTest","histograms with different binning along Y");
1493 return 0;
1494 }
1495
1496 // Should we include Uflows, Oflows?
1497 Int_t ibeg = 1, jbeg = 1;
1498 Int_t iend = ncx1, jend = ncy1;
1499 if (opt.Contains("U")) {ibeg = 0; jbeg = 0;}
1500 if (opt.Contains("O")) {iend = ncx1+1; jend = ncy1+1;}
1501
1502 Int_t i,j;
1503 Double_t sum1 = 0;
1504 Double_t sum2 = 0;
1505 Double_t w1 = 0;
1506 Double_t w2 = 0;
1507 for (i = ibeg; i <= iend; i++) {
1508 for (j = jbeg; j <= jend; j++) {
1509 sum1 += h1->GetBinContent(i,j);
1510 sum2 += h2->GetBinContent(i,j);
1511 Double_t ew1 = h1->GetBinError(i,j);
1512 Double_t ew2 = h2->GetBinError(i,j);
1513 w1 += ew1*ew1;
1514 w2 += ew2*ew2;
1515
1516 }
1517 }
1518
1519 // Check that both scatterplots contain events
1520 if (sum1 == 0) {
1521 Error("KolmogorovTest","Integral is zero for h1=%s\n",h1->GetName());
1522 return 0;
1523 }
1524 if (sum2 == 0) {
1525 Error("KolmogorovTest","Integral is zero for h2=%s\n",h2->GetName());
1526 return 0;
1527 }
1528 // calculate the effective entries.
1529 // the case when errors are zero (w1 == 0 or w2 ==0) are equivalent to
1530 // compare to a function. In that case the rescaling is done only on sqrt(esum2) or sqrt(esum1)
1531 Double_t esum1 = 0, esum2 = 0;
1532 if (w1 > 0)
1533 esum1 = sum1 * sum1 / w1;
1534 else
1535 afunc1 = kTRUE; // use later for calculating z
1536
1537 if (w2 > 0)
1538 esum2 = sum2 * sum2 / w2;
1539 else
1540 afunc2 = kTRUE; // use later for calculating z
1541
1542 if (afunc2 && afunc1) {
1543 Error("KolmogorovTest","Errors are zero for both histograms\n");
1544 return 0;
1545 }
1546
1547 // Find first Kolmogorov distance
1548 Double_t s1 = 1/sum1;
1549 Double_t s2 = 1/sum2;
1550 Double_t dfmax1 = 0;
1551 Double_t rsum1=0, rsum2=0;
1552 for (i=ibeg;i<=iend;i++) {
1553 for (j=jbeg;j<=jend;j++) {
1554 rsum1 += s1*h1->GetBinContent(i,j);
1555 rsum2 += s2*h2->GetBinContent(i,j);
1557 }
1558 }
1559
1560 // Find second Kolmogorov distance
1561 Double_t dfmax2 = 0;
1562 rsum1=0, rsum2=0;
1563 for (j=jbeg;j<=jend;j++) {
1564 for (i=ibeg;i<=iend;i++) {
1565 rsum1 += s1*h1->GetBinContent(i,j);
1566 rsum2 += s2*h2->GetBinContent(i,j);
1568 }
1569 }
1570
1571 // Get Kolmogorov probability: use effective entries, esum1 or esum2, for normalizing it
1574 else if (afunc2) factnm = TMath::Sqrt(esum1);
1576
1577 // take average of the two distances
1578 Double_t dfmax = 0.5*(dfmax1+dfmax2);
1579 Double_t z = dfmax*factnm;
1580
1582
1583 Double_t prb1 = 0, prb2 = 0;
1584 // option N to combine normalization makes sense if both afunc1 and afunc2 are false
1585 if (opt.Contains("N") && !(afunc1 || afunc2 ) ) {
1586 // Combine probabilities for shape and normalization
1587 prb1 = prb;
1590 prb2 = TMath::Prob(chi2,1);
1591 // see Eadie et al., section 11.6.2
1592 if (prb > 0 && prb2 > 0) prb = prb*prb2*(1-TMath::Log(prb*prb2));
1593 else prb = 0;
1594 }
1595
1596 // debug printout
1597 if (opt.Contains("D")) {
1598 printf(" Kolmo Prob h1 = %s, sum1=%g\n",h1->GetName(),sum1);
1599 printf(" Kolmo Prob h2 = %s, sum2=%g\n",h2->GetName(),sum2);
1600 printf(" Kolmo Probabil = %f, Max Dist = %g\n",prb,dfmax);
1601 if (opt.Contains("N"))
1602 printf(" Kolmo Probabil = %f for shape alone, =%f for normalisation alone\n",prb1,prb2);
1603 }
1604 // This numerical error condition should never occur:
1605 if (TMath::Abs(rsum1-1) > 0.002) Warning("KolmogorovTest","Numerical problems with h1=%s\n",h1->GetName());
1606 if (TMath::Abs(rsum2-1) > 0.002) Warning("KolmogorovTest","Numerical problems with h2=%s\n",h2->GetName());
1607
1608 if(opt.Contains("M")) return dfmax; // return average of max distance
1609
1610 return prb;
1611}
1612
1613
1614////////////////////////////////////////////////////////////////////////////////
1615/// Rebin only the X axis
1616/// see Rebin2D
1617
1619{
1620 return Rebin2D(ngroup, 1, newname);
1621}
1622
1623
1624////////////////////////////////////////////////////////////////////////////////
1625/// Rebin only the Y axis
1626/// see Rebin2D
1627
1629{
1630 return Rebin2D(1, ngroup, newname);
1631}
1632
1633////////////////////////////////////////////////////////////////////////////////
1634/// Override TH1::Rebin, rebinning only the X axis with the same conventions
1635/// as the TH1 function (`ngroup` is the number of variable size bins when
1636/// `xbins` is given).
1637/// see RebinX and Rebin2D
1638
1640{
1641 if (xbins != nullptr)
1642 return Rebin2D(ngroup, 1, newname, xbins, nullptr);
1643 Info("Rebin","Rebinning only the x-axis. Use Rebin2D for rebinning both axes");
1644 return RebinX(ngroup, newname);
1645}
1646
1647////////////////////////////////////////////////////////////////////////////////
1648/// Rebin this histogram grouping nxgroup/nygroup bins along the xaxis/yaxis together.
1649///
1650/// #### case 1 `xbins`=0 || `ybins`=0
1651///
1652/// if `newname` is not blank a new temporary histogram hnew is created.
1653/// else the current histogram is modified (default)
1654/// The parameters `nxgroup`/`nygroup` indicate how many bins along the xaxis/yaxis of this
1655/// have to me merged into one bin of hnew
1656/// If the original histogram has errors stored (via Sumw2), the resulting
1657/// histograms has new errors correctly calculated.
1658///
1659/// examples: if hpxpy is an existing TH2 histogram with 40 x 40 bins
1660/// ~~~ {.cpp}
1661/// hpxpy->Rebin2D(); // merges two bins along the xaxis and yaxis in one in hpxpy
1662/// // Carefull: previous contents of hpxpy are lost
1663/// hpxpy->RebinX(5); //merges five bins along the xaxis in one in hpxpy
1664/// TH2 *hnew = hpxpy->RebinY(5,"hnew"); // creates a new histogram hnew
1665/// // merging 5 bins of h1 along the yaxis in one bin
1666/// ~~~
1667///
1668/// \note If `nxgroup`/`nygroup` is not an exact divider of the number of bins,
1669/// along the xaxis/yaxis the top limit(s) of the rebinned histogram
1670/// is changed to the upper edge of the xbin=newxbins*nxgroup resp.
1671/// ybin=newybins*nygroup and the corresponding bins are added to
1672/// the overflow bin.
1673/// Statistics will be recomputed from the new bin contents.
1674///
1675/// #### case 2 `xbins`!=0 || `ybins`!=0
1676///
1677/// A new histogram is created and `newname` must be specified.
1678/// For an axis with a non-null bin-edges array, `nxgroup` (`nygroup`) is the
1679/// number of bins of the new x-axis (y-axis) and `xbins` (`ybins`) must hold
1680/// the `nxgroup+1` (`nygroup+1`) edges of the new bins. An axis without an
1681/// array is rebinned in constant groups as in case 1.
1682/// The content of each old bin is added to the new bin containing its center;
1683/// old bins outside the range of the new axes end up in the under-/overflow
1684/// bins. Errors stored via Sumw2 are correctly recalculated.
1685///
1686/// \note The new bin edges should line up with old bin edges: the entries of
1687/// an old bin that is split between two new bins are all transferred to the
1688/// bin containing the old bin center, and a warning is emitted.
1689///
1690/// example: rebinning a TH2F with 100 x 100 bins into 24 x 24 variable bins
1691/// ~~~ {.cpp}
1692/// Double_t xbins[25] = {...}; // low-edges plus upper edge of last bin
1693/// Double_t ybins[25] = {...};
1694/// TH2 *hnew = h2->Rebin2D(24, 24, "hnew", xbins, ybins);
1695/// ~~~
1696
1698{
1699 if (GetDimension() != 2) {
1700 Error("Rebin2D", "Histogram must be TH2. This histogram has %d dimensions.", GetDimension());
1701 return nullptr;
1702 }
1703 // something to do?
1704 if (nxgroup == 1 && nygroup == 1 && !xbins && !ybins) {
1705 return (newname && strlen(newname) > 0) ? (TH2 *)Clone(newname) : this;
1706 }
1707 if ((!newname || strlen(newname) == 0) && (xbins || ybins)) {
1708 Error("Rebin2D", "if xbins or ybins are specified, newname must be given");
1709 return nullptr;
1710 }
1711
1712 const Int_t nxbins = fXaxis.GetNbins();
1713 const Int_t nybins = fYaxis.GetNbins();
1714
1715 // validate the parameters and define the axes of the rebinned histogram
1716 // and the mapping of old to new bins
1718 if (!ROOT::Internal::SetupRebinnedAxis(fXaxis, nxgroup, xbins, 'x', *this, "Rebin2D", infoX) ||
1719 !ROOT::Internal::SetupRebinnedAxis(fYaxis, nygroup, ybins, 'y', *this, "Rebin2D", infoY)) {
1720 return nullptr;
1721 }
1722 const Int_t newxbins = infoX.nNewBins;
1723 const Int_t newybins = infoY.nNewBins;
1724
1725 // Save old bin contents into a new array
1726 std::vector<Double_t> oldBins(fNcells);
1727 for (Int_t i = 0; i < fNcells; ++i)
1729
1730 std::vector<Double_t> oldErrors;
1731 if (fSumw2.fN != 0) {
1732 oldErrors.resize(fNcells);
1733 for (Int_t i = 0; i < fNcells; ++i)
1735 }
1736
1737 // rebinning will not redistribute under-/overflow content into the range
1738 // of new axes that extend beyond the old ones
1740 *this, "Rebin2D");
1742 *this, "Rebin2D");
1743
1744 // create a clone of the old histogram if newname is specified (guaranteed
1745 // when bin edges are passed)
1746 TH2 *hnew = this;
1747 if (newname && strlen(newname) > 0) {
1748 hnew = (TH2 *)Clone(newname);
1749 }
1750
1751 // save the TAttAxis members (reset by SetBins) for x axis
1763 // save the TAttAxis members (reset by SetBins) for y axis
1775
1776 ROOT::Internal::SetRebinnedBins2D(*hnew, infoX.newAxis, infoY.newAxis); // changes also errors array (if any)
1777
1778 // add the content of each old cell (including under- and overflows) to
1779 // the new cell that contains its bin center
1780 const Int_t newncells = (newxbins + 2) * (newybins + 2);
1781 std::vector<Double_t> newBins(newncells, 0.);
1782 std::vector<Double_t> newErrors;
1783 if (oldErrors.empty()) {
1785 {{oldBins.data(), newBins.data()}});
1786 } else {
1787 newErrors.resize(newncells, 0.);
1789 {{oldBins.data(), newBins.data()}, {oldErrors.data(), newErrors.data()}});
1790 }
1791 for (Int_t i = 0; i < newncells; ++i) {
1792 hnew->UpdateBinContent(i, newBins[i]);
1793 if (!oldErrors.empty())
1794 hnew->fSumw2[i] = newErrors[i];
1795 }
1796
1797 // Restore x axis attributes
1798 fXaxis.SetNdivisions(nXdivisions);
1799 fXaxis.SetAxisColor(xAxisColor);
1800 fXaxis.SetLabelColor(xLabelColor);
1801 fXaxis.SetLabelFont(xLabelFont);
1802 fXaxis.SetLabelOffset(xLabelOffset);
1803 fXaxis.SetLabelSize(xLabelSize);
1804 fXaxis.SetTickLength(xTickLength);
1805 fXaxis.SetTitleOffset(xTitleOffset);
1806 fXaxis.SetTitleSize(xTitleSize);
1807 fXaxis.SetTitleColor(xTitleColor);
1808 fXaxis.SetTitleFont(xTitleFont);
1809 // Restore y axis attributes
1810 fYaxis.SetNdivisions(nYdivisions);
1811 fYaxis.SetAxisColor(yAxisColor);
1812 fYaxis.SetLabelColor(yLabelColor);
1813 fYaxis.SetLabelFont(yLabelFont);
1814 fYaxis.SetLabelOffset(yLabelOffset);
1815 fYaxis.SetLabelSize(yLabelSize);
1816 fYaxis.SetTickLength(yTickLength);
1817 fYaxis.SetTitleOffset(yTitleOffset);
1818 fYaxis.SetTitleSize(yTitleSize);
1819 fYaxis.SetTitleColor(yTitleColor);
1820 fYaxis.SetTitleFont(yTitleFont);
1821
1822 // when the group count does not divide the old bin count, the top bins
1823 // moved to the overflow: recompute the statistics from the bin contents
1824 if (infoX.truncated || infoY.truncated)
1825 hnew->ResetStats();
1826
1827 return hnew;
1828}
1829
1830////////////////////////////////////////////////////////////////////////////////
1831
1833{
1834 TString opt = option;
1835 // extract cut infor
1836 TString cut;
1837 Int_t i1 = opt.Index("[");
1838 if (i1>=0) {
1839 Int_t i2 = opt.Index("]");
1840 cut = opt(i1,i2-i1+1);
1841 }
1842 opt.ToLower();
1843 bool originalRange = opt.Contains("o");
1844 bool useWidth = opt.Contains("width");
1845
1846 const TAxis& outAxis = ( onX ? fXaxis : fYaxis );
1847 const TAxis& inAxis = ( onX ? fYaxis : fXaxis );
1848 Int_t inN = inAxis.GetNbins();
1849 const char *expectedName = ( onX ? "_pfx" : "_pfy" );
1850
1851 // outer axis cannot be outside original axis (this fixes ROOT-8781)
1852 // and firstOutBin and lastOutBin cannot be both equal to zero
1853 Int_t firstOutBin = std::max(outAxis.GetFirst(),1);
1854 Int_t lastOutBin = std::min(outAxis.GetLast(),outAxis.GetNbins() ) ;
1855
1856 if ( lastbin < firstbin && inAxis.TestBit(TAxis::kAxisRange) ) {
1857 firstbin = inAxis.GetFirst();
1858 lastbin = inAxis.GetLast();
1859 // For special case of TAxis::SetRange, when first == 1 and last
1860 // = N and the range bit has been set, the TAxis will return 0
1861 // for both.
1862 if (firstbin == 0 && lastbin == 0)
1863 {
1864 firstbin = 1;
1865 lastbin = inAxis.GetNbins();
1866 }
1867 }
1868 if (firstbin < 0) firstbin = 1;
1869 if (lastbin < 0) lastbin = inN;
1870 if (lastbin > inN+1) lastbin = inN;
1871
1872 // Create the profile histogram
1873 char *pname = (char*)name;
1874 if (name && strcmp(name, expectedName) == 0) {
1875 Int_t nch = strlen(GetName()) + 5;
1876 pname = new char[nch];
1877 snprintf(pname,nch,"%s%s",GetName(),name);
1878 }
1879 TProfile *h1=nullptr;
1880 //check if a profile with identical name exist
1881 // if compatible reset and re-use previous histogram
1882 TObject *h1obj = gROOT->FindObject(pname);
1883 if (h1obj && h1obj->InheritsFrom(TH1::Class())) {
1884 if (h1obj->IsA() != TProfile::Class() ) {
1885 Error("DoProfile","Histogram with name %s must be a TProfile and is a %s",name,h1obj->ClassName());
1886 return nullptr;
1887 }
1888 h1 = (TProfile*)h1obj;
1889 // reset the existing histogram and set always the new binning for the axis
1890 // This avoid problems when the histogram already exists and the histograms is rebinned or its range has changed
1891 // (see https://savannah.cern.ch/bugs/?94101 or https://savannah.cern.ch/bugs/?95808 )
1892 h1->Reset();
1893 const TArrayD *xbins = outAxis.GetXbins();
1894 if (xbins->fN == 0) {
1895 if ( originalRange )
1896 h1->SetBins(outAxis.GetNbins(),outAxis.GetXmin(),outAxis.GetXmax());
1897 else
1898 h1->SetBins(lastOutBin-firstOutBin+1,outAxis.GetBinLowEdge(firstOutBin),outAxis.GetBinUpEdge(lastOutBin));
1899 } else {
1900 // case variable bins
1901 if (originalRange )
1902 h1->SetBins(outAxis.GetNbins(),xbins->fArray);
1903 else
1905 }
1906 }
1907
1908 Int_t ncuts = 0;
1909 if (opt.Contains("[")) {
1910 ((TH2 *)this)->GetPainter();
1911 if (fPainter) ncuts = fPainter->MakeCuts((char*)cut.Data());
1912 }
1913
1914 if (!h1) {
1915 const TArrayD *bins = outAxis.GetXbins();
1916 if (bins->fN == 0) {
1917 if ( originalRange )
1918 h1 = new TProfile(pname,GetTitle(),outAxis.GetNbins(),outAxis.GetXmin(),outAxis.GetXmax(),opt);
1919 else
1921 outAxis.GetBinLowEdge(firstOutBin),
1922 outAxis.GetBinUpEdge(lastOutBin), opt);
1923 } else {
1924 // case variable bins
1925 if (originalRange )
1926 h1 = new TProfile(pname,GetTitle(),outAxis.GetNbins(),bins->fArray,opt);
1927 else
1929 }
1930 }
1931 if (pname != name) delete [] pname;
1932
1933 // Copy attributes
1935 THashList* labels=outAxis.GetLabels();
1936 if (labels) {
1937 TIter iL(labels);
1938 TObjString* lb;
1939 Int_t i = 1;
1940 while ((lb=(TObjString*)iL())) {
1941 h1->GetXaxis()->SetBinLabel(i,lb->String().Data());
1942 i++;
1943 }
1944 }
1945
1946 h1->SetLineColor(this->GetLineColor());
1947 h1->SetFillColor(this->GetFillColor());
1948 h1->SetMarkerColor(this->GetMarkerColor());
1949 h1->SetMarkerStyle(this->GetMarkerStyle());
1950
1951 // check if histogram is weighted
1952 // in case need to store sum of weight square/bin for the profile
1953 TArrayD & binSumw2 = *(h1->GetBinSumw2());
1954 bool useWeights = (GetSumw2N() > 0);
1955 if (useWeights && (binSumw2.fN != h1->GetNcells()) ) h1->Sumw2();
1956 // we need to set this bit because we fill the profile using a single Fill for many entries
1957 // This is needed for the changes applied to make automatically the histogram weighted in ROOT 6 versions
1958 else h1->SetBit(TH1::kIsNotW);
1959
1960 // Fill the profile histogram
1961 // no entries/bin is available so can fill only using bin content as weight
1962
1963 // implement filling of projected histogram
1964 // outbin is bin number of outAxis (the projected axis). Loop is done on all bin of TH2 histograms
1965 // inbin is the axis being integrated. Loop is done only on the selected bins
1966 for ( Int_t outbin = 0; outbin <= outAxis.GetNbins() + 1; ++outbin) {
1968
1969 // find corresponding bin number in h1 for outbin (binOut)
1970 Double_t xOut = outAxis.GetBinCenter(outbin);
1972 if (binOut <0) continue;
1973
1974 for (Int_t inbin = firstbin ; inbin <= lastbin ; ++inbin) {
1975 Int_t binx, biny;
1976 if (onX) { binx = outbin; biny=inbin; }
1977 else { binx = inbin; biny=outbin; }
1978
1979 if (ncuts) {
1980 if (!fPainter->IsInside(binx,biny)) continue;
1981 }
1982 Int_t bin = GetBin(binx, biny);
1984 double step = useWidth ? inAxis.GetBinWidth(inbin) : 1;
1985
1986 if (cxy) {
1987 Double_t tmp = 0;
1988 // the following fill update wrongly the fBinSumw2- need to save it before
1989 if ( useWeights ) tmp = binSumw2.fArray[binOut];
1990 h1->Fill( xOut, inAxis.GetBinCenter(inbin), cxy * step);
1991 if ( useWeights ) binSumw2.fArray[binOut] = tmp + fSumw2.fArray[bin];
1992 }
1993
1994 }
1995 }
1996
1997 // the statistics must be recalculated since by using the Fill method the total sum of weight^2 is
1998 // not computed correctly
1999 // for a profile does not much sense to re-use statistics of original TH2
2000 h1->ResetStats();
2001 // Also we need to set the entries since they have not been correctly calculated during the projection
2002 // we can only set them to the effective entries
2004
2005
2006 if (opt.Contains("d")) {
2007 TVirtualPad::TContext ctxt(gROOT->GetSelectedPad(), true, true);
2008 opt.Remove(opt.First("d"),1);
2009 if (!gPad || !gPad->FindObject(h1)) {
2010 h1->Draw(opt);
2011 } else {
2012 h1->Paint(opt);
2013 }
2014 }
2015 return h1;
2016}
2017
2018
2019////////////////////////////////////////////////////////////////////////////////
2020/// Project a 2-D histogram into a profile histogram along X (integration along Y).
2021///
2022/// The projection is made from summing the channels along the Y axis
2023/// ranging from firstybin to lastybin included.
2024/// By default, bins 1 to ny are included
2025/// When all bins are included, the number of entries in the projection
2026/// is set to the number of entries of the 2-D histogram, otherwise
2027/// the number of entries is incremented by 1 for all non empty cells.
2028///
2029/// if option "d" is specified, the profile is drawn in the current pad.
2030///
2031/// if option "o" original axis range of the target axes will be
2032/// kept, but only bins inside the selected range will be filled.
2033///
2034/// if option "width" is specified, each bin content is multiplied
2035/// by its Y bin-width during projection
2036///
2037/// The option can also be used to specify the projected profile error type.
2038/// Values which can be used are 's', 'i', or 'g'. See TProfile::BuildOptions for details
2039///
2040/// Using a TCutG object, it is possible to select a sub-range of a 2-D histogram.
2041/// One must create a graphical cut (mouse or C++) and specify the name
2042/// of the cut between [] in the option.
2043/// For example, with a TCutG named "cutg", one can call:
2044/// myhist->ProfileX(" ",firstybin,lastybin,"[cutg]");
2045/// To invert the cut, it is enough to put a "-" in front of its name:
2046/// myhist->ProfileX(" ",firstybin,lastybin,"[-cutg]");
2047/// It is possible to apply several cuts ("," means logical AND):
2048/// myhist->ProfileX(" ",firstybin,lastybin,"[cutg1,cutg2]");
2049///
2050/// NOTE that if a TProfile named "name" exists in the current directory or pad with
2051/// a compatible axis the profile is reset and filled again with the projected contents of the TH2.
2052/// In the case of axis incompatibility an error is reported and a NULL pointer is returned.
2053///
2054/// NOTE that the X axis attributes of the TH2 are copied to the X axis of the profile.
2055///
2056/// NOTE that the default under- / overflow behavior differs from what ProjectionX
2057/// does! Profiles take the bin center into account, so here the under- and overflow
2058/// bins are ignored by default.
2059///
2060/// NOTE that the return profile histogram is computed using the Y bin center values instead of
2061/// the real Y values which are used to fill the 2d histogram. Therefore the obtained profile is just an approximation of the
2062/// correct profile histogram that would be obtained when filling it directly with the original data (see ROOT-7770)
2063
2064
2066{
2067 return DoProfile(true, name, firstybin, lastybin, option);
2068
2069}
2070
2071
2072////////////////////////////////////////////////////////////////////////////////
2073/// Project a 2-D histogram into a profile histogram along Y (integration along X).
2074///
2075/// The projection is made from summing the channels along the X axis
2076/// ranging from firstxbin to lastxbin included.
2077/// By default, bins 1 to nx are included
2078/// When all bins are included, the number of entries in the projection
2079/// is set to the number of entries of the 2-D histogram, otherwise
2080/// the number of entries is incremented by 1 for all non empty cells.
2081///
2082/// if option "d" is specified, the profile is drawn in the current pad.
2083///
2084/// if option "o" , the original axis range of the target axis will be
2085/// kept, but only bins inside the selected range will be filled.
2086///
2087/// if option "width" is specified, each bin content is multiplied
2088/// by its X bin-width during projection
2089///
2090/// The option can also be used to specify the projected profile error type.
2091/// Values which can be used are 's', 'i', or 'g'. See TProfile::BuildOptions for details
2092/// Using a TCutG object, it is possible to select a sub-range of a 2-D histogram.
2093///
2094/// One must create a graphical cut (mouse or C++) and specify the name
2095/// of the cut between [] in the option.
2096/// For example, with a TCutG named "cutg", one can call:
2097/// myhist->ProfileY(" ",firstybin,lastybin,"[cutg]");
2098/// To invert the cut, it is enough to put a "-" in front of its name:
2099/// myhist->ProfileY(" ",firstybin,lastybin,"[-cutg]");
2100/// It is possible to apply several cuts:
2101/// myhist->ProfileY(" ",firstybin,lastybin,"[cutg1,cutg2]");
2102///
2103/// NOTE that if a TProfile named "name" exists in the current directory or pad with
2104/// a compatible axis the profile is reset and filled again with the projected contents of the TH2.
2105/// In the case of axis incompatibility an error is reported and a NULL pointer is returned.
2106///
2107/// NOTE that the Y axis attributes of the TH2 are copied to the X axis of the profile.
2108///
2109/// NOTE that the default under- / overflow behavior differs from what ProjectionX
2110/// does! Profiles take the bin center into account, so here the under- and overflow
2111/// bins are ignored by default.
2112///
2113/// NOTE that the return profile histogram is computed using the X bin center values instead of
2114/// the real X values which are used to fill the 2d histogram. Therefore the obtained profile is just an approximation of the
2115/// correct profile histogram that would be obtained when filling it directly with the original data (see ROOT-7770)
2116
2117
2119{
2120 return DoProfile(false, name, firstxbin, lastxbin, option);
2121}
2122
2123////////////////////////////////////////////////////////////////////////////////
2124/// Internal (protected) method for performing projection on the X or Y axis
2125/// called by ProjectionX or ProjectionY.
2126/// The histograms created are added to gDirectory.
2127
2129{
2130 const char *expectedName = nullptr;
2131 Int_t inNbin;
2132 const TAxis* outAxis;
2133 const TAxis* inAxis;
2134
2135 TString opt = option;
2136 TString cut;
2137 Int_t i1 = opt.Index("[");
2138 if (i1>=0) {
2139 Int_t i2 = opt.Index("]");
2140 cut = opt(i1,i2-i1+1);
2141 }
2142 opt.ToLower(); //must be called after having parsed the cut name
2143 bool originalRange = opt.Contains("o");
2144 bool useWidth = opt.Contains("width");
2145
2146 if ( onX )
2147 {
2148 expectedName = "_px";
2150 outAxis = GetXaxis();
2151 inAxis = GetYaxis();
2152 }
2153 else
2154 {
2155 expectedName = "_py";
2157 outAxis = GetYaxis();
2158 inAxis = GetXaxis();
2159 }
2160
2161 // outer axis cannot be outside original axis (this fixes ROOT-8781)
2162 // and firstOutBin and lastOutBin cannot be both equal to zero
2163 Int_t firstOutBin = std::max(outAxis->GetFirst(),1);
2164 Int_t lastOutBin = std::min(outAxis->GetLast(),outAxis->GetNbins() ) ;
2165
2167 firstbin = inAxis->GetFirst();
2168 lastbin = inAxis->GetLast();
2169 // For special case of TAxis::SetRange, when first == 1 and last
2170 // = N and the range bit has been set, the TAxis will return 0
2171 // for both.
2172 if (firstbin == 0 && lastbin == 0)
2173 {
2174 firstbin = 1;
2175 lastbin = inAxis->GetNbins();
2176 }
2177 }
2178 if (firstbin < 0) firstbin = 0;
2179 if (lastbin < 0) lastbin = inNbin + 1;
2180 if (lastbin > inNbin+1) lastbin = inNbin + 1;
2181
2182 // Create the projection histogram
2183 char *pname = (char*)name;
2184 if (name && strcmp(name,expectedName) == 0) {
2185 Int_t nch = strlen(GetName()) + 4;
2186 pname = new char[nch];
2187 snprintf(pname,nch,"%s%s",GetName(),name);
2188 }
2189 TH1D *h1=nullptr;
2190 //check if histogram with identical name exist
2191 // if compatible reset and re-use previous histogram
2192 // (see https://savannah.cern.ch/bugs/?54340)
2193 TObject *h1obj = gROOT->FindObject(pname);
2194 if (h1obj && h1obj->InheritsFrom(TH1::Class())) {
2195 if (h1obj->IsA() != TH1D::Class() ) {
2196 Error("DoProjection","Histogram with name %s must be a TH1D and is a %s",name,h1obj->ClassName());
2197 return nullptr;
2198 }
2199 h1 = (TH1D*)h1obj;
2200 // reset the existing histogram and set always the new binning for the axis
2201 // This avoid problems when the histogram already exists and the histograms is rebinned or its range has changed
2202 // (see https://savannah.cern.ch/bugs/?94101 or https://savannah.cern.ch/bugs/?95808 )
2203 h1->Reset();
2204 const TArrayD *xbins = outAxis->GetXbins();
2205 if (xbins->fN == 0) {
2206 if ( originalRange )
2207 h1->SetBins(outAxis->GetNbins(),outAxis->GetXmin(),outAxis->GetXmax());
2208 else
2209 h1->SetBins(lastOutBin-firstOutBin+1,outAxis->GetBinLowEdge(firstOutBin),outAxis->GetBinUpEdge(lastOutBin));
2210 } else {
2211 // case variable bins
2212 if (originalRange )
2213 h1->SetBins(outAxis->GetNbins(),xbins->fArray);
2214 else
2216 }
2217 }
2218
2219 Int_t ncuts = 0;
2220 if (opt.Contains("[")) {
2221 ((TH2 *)this)->GetPainter();
2222 if (fPainter) ncuts = fPainter->MakeCuts((char*)cut.Data());
2223 }
2224
2225 if (!h1) {
2226 const TArrayD *bins = outAxis->GetXbins();
2227 if (bins->fN == 0) {
2228 if ( originalRange )
2229 h1 = new TH1D(pname,GetTitle(),outAxis->GetNbins(),outAxis->GetXmin(),outAxis->GetXmax());
2230 else
2232 outAxis->GetBinLowEdge(firstOutBin),outAxis->GetBinUpEdge(lastOutBin));
2233 } else {
2234 // case variable bins
2235 if (originalRange )
2236 h1 = new TH1D(pname,GetTitle(),outAxis->GetNbins(),bins->fArray);
2237 else
2239 }
2241 if (opt.Contains("e") || GetSumw2N() ) h1->Sumw2();
2242 }
2243 if (pname != name) delete [] pname;
2244
2245 // Copy the axis attributes and the axis labels if needed.
2247 THashList* labels=outAxis->GetLabels();
2248 if (labels) {
2249 TIter iL(labels);
2250 TObjString* lb;
2251 Int_t i = 1;
2252 while ((lb=(TObjString*)iL())) {
2253 h1->GetXaxis()->SetBinLabel(i,lb->String().Data());
2254 i++;
2255 }
2256 }
2257
2258 h1->SetLineColor(this->GetLineColor());
2259 h1->SetFillColor(this->GetFillColor());
2260 h1->SetMarkerColor(this->GetMarkerColor());
2261 h1->SetMarkerStyle(this->GetMarkerStyle());
2262
2263 // Fill the projected histogram
2265 Double_t totcont = 0;
2267
2268 // implement filling of projected histogram
2269 // outbin is bin number of outAxis (the projected axis). Loop is done on all bin of TH2 histograms
2270 // inbin is the axis being integrated. Loop is done only on the selected bins
2271 // if the out axis has labels and is extendable, temporary make it non-extendable to avoid adding extra bins
2272 Bool_t extendable = outAxis->CanExtend();
2273 if ( labels && extendable ) h1->GetXaxis()->SetCanExtend(kFALSE);
2274 for ( Int_t outbin = 0; outbin <= outAxis->GetNbins() + 1; ++outbin) {
2275 err2 = 0;
2276 cont = 0;
2278
2279 for (Int_t inbin = firstbin ; inbin <= lastbin ; ++inbin) {
2280 Int_t binx, biny;
2281 if (onX) { binx = outbin; biny=inbin; }
2282 else { binx = inbin; biny=outbin; }
2283
2284 if (ncuts) {
2285 if (!fPainter->IsInside(binx,biny)) continue;
2286 }
2287 // sum bin content and error if needed
2288 double step = useWidth ? inAxis->GetBinWidth(inbin) : 1;
2289 cont += GetBinContent(binx,biny)*step;
2290 if (computeErrors) {
2292 err2 += exy*exy;
2293 }
2294 }
2295 // find corresponding bin number in h1 for outbin
2296 Int_t binOut = h1->GetXaxis()->FindBin( outAxis->GetBinCenter(outbin) );
2299 // sum all content
2300 totcont += cont;
2301 }
2302 if ( labels ) h1->GetXaxis()->SetCanExtend(extendable);
2303
2304 // check if we can re-use the original statistics from the previous histogram
2305 bool reuseStats = false;
2306 if ( ( GetStatOverflowsBehaviour() == false && firstbin == 1 && lastbin == inNbin ) ||
2307 ( GetStatOverflowsBehaviour() == true && firstbin == 0 && lastbin == inNbin + 1 ) )
2308 reuseStats = true;
2309 else {
2310 // also if total content match we can re-use
2311 double eps = 1.E-12;
2312 if (IsA() == TH2F::Class() ) eps = 1.E-6;
2313 if (fTsumw != 0 && TMath::Abs( fTsumw - totcont) < TMath::Abs(fTsumw) * eps)
2314 reuseStats = true;
2315 }
2316 if (ncuts) reuseStats = false;
2317 // retrieve the statistics and set in projected histogram if we can re-use it
2318 bool reuseEntries = reuseStats;
2319 // can re-use entries if underflow/overflow are included
2320 reuseEntries &= (firstbin==0 && lastbin == inNbin+1);
2321 if (reuseStats) {
2322 Double_t stats[kNstat];
2323 GetStats(stats);
2324 if (!onX) { // case of projection on Y
2325 stats[2] = stats[4];
2326 stats[3] = stats[5];
2327 }
2328 h1->PutStats(stats);
2329 }
2330 else {
2331 // the statistics is automatically recalculated since it is reset by the call to SetBinContent
2332 // we just need to set the entries since they have not been correctly calculated during the projection
2333 // we can only set them to the effective entries
2335 }
2336 if (reuseEntries) {
2338 }
2339 else {
2340 // re-compute the entries
2341 // in case of error calculation (i.e. when Sumw2() is set)
2342 // use the effective entries for the entries
2343 // since this is the only way to estimate them
2344 Double_t entries = TMath::Floor( totcont + 0.5); // to avoid numerical rounding
2346 h1->SetEntries( entries );
2347 }
2348
2349 if (opt.Contains("d")) {
2350 TVirtualPad::TContext ctxt(gROOT->GetSelectedPad(), true, true);
2351 opt.Remove(opt.First("d"),1);
2352 // remove also other options
2353 if (opt.Contains("e")) opt.Remove(opt.First("e"),1);
2354 if (!gPad || !gPad->FindObject(h1)) {
2355 h1->Draw(opt);
2356 } else {
2357 h1->Paint(opt);
2358 }
2359 }
2360
2361 return h1;
2362}
2363
2364
2365////////////////////////////////////////////////////////////////////////////////
2366/// Project a 2-D histogram into a 1-D histogram along X (integration along Y).
2367///
2368/// The projection is always of the type TH1D.
2369/// The projection is made from summing the channels along the Y axis
2370/// ranging from firstybin to lastybin included.
2371/// By default, all bins including under- and overflow are included.
2372/// The number of entries in the projection is estimated from the
2373/// number of effective entries for all the cells included in the projection.
2374///
2375/// To exclude the underflow bins in Y, use firstybin=1.
2376/// To exclude the overflow bins in Y, use lastybin=nx.
2377///
2378/// if option "e" is specified, the errors are computed.
2379/// if option "d" is specified, the projection is drawn in the current pad.
2380/// if option "o" original axis range of the target axes will be
2381/// kept, but only bins inside the selected range will be filled.
2382///
2383/// if option "width" is specified, each bin content is multiplied
2384/// by its Y bin-width during projection
2385///
2386/// Using a TCutG object, it is possible to select a sub-range of a 2-D histogram.
2387/// One must create a graphical cut (mouse or C++) and specify the name
2388/// of the cut between [] in the option.
2389/// For example, with a TCutG named "cutg", one can call:
2390/// myhist->ProjectionX(" ",firstybin,lastybin,"[cutg]");
2391/// To invert the cut, it is enough to put a "-" in front of its name:
2392/// myhist->ProjectionX(" ",firstybin,lastybin,"[-cutg]");
2393/// It is possible to apply several cuts:
2394/// myhist->ProjectionX(" ",firstybin,lastybin,"[cutg1,cutg2]");
2395///
2396/// NOTE that if a TH1D named "name" exists in the current directory or pad
2397/// the histogram is reset and filled again with the projected contents of the TH2.
2398///
2399/// NOTE that the X axis attributes of the TH2 are copied to the X axis of the projection.
2400
2402{
2403 return DoProjection(true, name, firstybin, lastybin, option);
2404}
2405
2406
2407////////////////////////////////////////////////////////////////////////////////
2408/// Project a 2-D histogram into a 1-D histogram along Y (integration along X).
2409///
2410/// The projection is always of the type TH1D.
2411/// The projection is made from summing the channels along the X axis
2412/// ranging from firstxbin to lastxbin included.
2413/// By default, all bins including under- and overflow are included.
2414/// The number of entries in the projection is estimated from the
2415/// number of effective entries for all the cells included in the projection
2416///
2417/// To exclude the underflow bins in X, use firstxbin=1.
2418/// To exclude the overflow bins in X, use lastxbin=nx.
2419///
2420/// if option "e" is specified, the errors are computed.
2421/// if option "d" is specified, the projection is drawn in the current pad.
2422/// if option "o" original axis range of the target axes will be
2423/// kept, but only bins inside the selected range will be filled.
2424///
2425/// if option "width" is specified, each bin content is multiplied
2426/// by its X bin-width during projection
2427///
2428/// Using a TCutG object, it is possible to select a sub-range of a 2-D histogram.
2429/// One must create a graphical cut (mouse or C++) and specify the name
2430/// of the cut between [] in the option.
2431/// For example, with a TCutG named "cutg", one can call:
2432/// myhist->ProjectionY(" ",firstxbin,lastxbin,"[cutg]");
2433/// To invert the cut, it is enough to put a "-" in front of its name:
2434/// myhist->ProjectionY(" ",firstxbin,lastxbin,"[-cutg]");
2435/// It is possible to apply several cuts:
2436/// myhist->ProjectionY(" ",firstxbin,lastxbin,"[cutg1,cutg2]");
2437///
2438/// NOTE that if a TH1D named "name" exists in the current directory or pad and having
2439/// a compatible axis, the histogram is reset and filled again with the projected contents of the TH2.
2440/// In the case of axis incompatibility, an error is reported and a NULL pointer is returned.
2441///
2442/// NOTE that the Y axis attributes of the TH2 are copied to the X axis of the projection.
2443
2445{
2446 return DoProjection(false, name, firstxbin, lastxbin, option);
2447}
2448
2449
2450////////////////////////////////////////////////////////////////////////////////
2451/// Replace current statistics with the values in array stats
2452
2454{
2455 TH1::PutStats(stats);
2456 fTsumwy = stats[4];
2457 fTsumwy2 = stats[5];
2458 fTsumwxy = stats[6];
2459}
2460
2461
2462////////////////////////////////////////////////////////////////////////////////
2463/// Compute the X distribution of quantiles in the other variable Y
2464/// name is the name of the returned histogram
2465/// prob is the probability content for the quantile (0.5 is the default for the median)
2466/// An approximate error for the quantile is computed assuming that the distribution in
2467/// the other variable is normal. According to this approximate formula the error on the quantile is
2468/// estimated as sqrt( p (1-p) / ( n * f(q)^2) ), where p is the probability content of the quantile and
2469/// n is the number of events used to compute the quantile and f(q) is the probability distribution for the
2470/// other variable evaluated at the obtained quantile. In the error estimation the probability is then assumed to be
2471/// a normal distribution.
2472
2473TH1D* TH2::QuantilesX( Double_t prob, const char * name) const
2474{
2475 return DoQuantiles(true, name, prob);
2476}
2477
2478
2479////////////////////////////////////////////////////////////////////////////////
2480/// Compute the Y distribution of quantiles in the other variable X
2481/// name is the name of the returned histogram
2482/// prob is the probability content for the quantile (0.5 is the default for the median)
2483/// An approximate error for the quantile is computed assuming that the distribution in
2484/// the other variable is normal.
2485
2486TH1D* TH2::QuantilesY( Double_t prob, const char * name) const
2487{
2488 return DoQuantiles(false, name, prob);
2489}
2490
2491
2492////////////////////////////////////////////////////////////////////////////////
2493/// Implementation of quantiles for x or y
2494
2495TH1D* TH2::DoQuantiles(bool onX, const char * name, Double_t prob) const
2496{
2497 const TAxis *outAxis = nullptr;
2498 if ( onX ) {
2499 outAxis = GetXaxis();
2500 } else {
2501 outAxis = GetYaxis();
2502 }
2503
2504 // build first name of returned histogram
2505 TString qname = name;
2506 if (qname.IsNull() || qname == "_qx" || qname == "_qy") {
2507 const char * qtype = (onX) ? "qx" : "qy";
2508 qname = TString::Format("%s_%s_%3.2f",GetName(),qtype, prob);
2509 }
2510 // check if the histogram is already existing
2511 TH1D *h1=nullptr;
2512 //check if histogram with identical name exist
2513 TObject *h1obj = gROOT->FindObject(qname);
2514 if (h1obj) {
2515 h1 = dynamic_cast<TH1D*>(h1obj);
2516 if (!h1) {
2517 Error("DoQuantiles","Histogram with name %s must be a TH1D and is a %s",qname.Data(),h1obj->ClassName());
2518 return nullptr;
2519 }
2520 }
2521 if (h1) {
2522 h1->Reset();
2523 } else {
2524 // create the histogram
2525 h1 = new TH1D(qname, GetTitle(), 1, 0, 1);
2526 }
2527 // set the bin content
2528 Int_t firstOutBin = std::max(outAxis->GetFirst(),1);
2529 Int_t lastOutBin = std::max(outAxis->GetLast(),outAxis->GetNbins());
2530 const TArrayD *xbins = outAxis->GetXbins();
2531 if (xbins->fN == 0)
2532 h1->SetBins(lastOutBin-firstOutBin+1,outAxis->GetBinLowEdge(firstOutBin),outAxis->GetBinUpEdge(lastOutBin));
2533 else
2535
2536 // set the bin content of the histogram
2537 Double_t pp[1];
2538 pp[0] = prob;
2539
2540 TH1D * slice = nullptr;
2541 for (int ibin = outAxis->GetFirst() ; ibin <= outAxis->GetLast() ; ++ibin) {
2542 Double_t qq[1];
2543 // do a projection on the opposite axis
2544 slice = DoProjection(!onX, "tmp",ibin,ibin,"");
2545 if (!slice) break;
2546 if (slice->GetSum() == 0) continue;
2547 slice->GetQuantiles(1,qq,pp);
2548 h1->SetBinContent(ibin,qq[0]);
2549 // compute error using normal approximation
2550 // quantile error ~ sqrt (q*(1-q)/ *( n * f(xq)^2 ) from Kendall
2551 // where f(xq) is the p.d.f value at the quantile xq
2552 Double_t n = slice->GetEffectiveEntries();
2553 Double_t f = TMath::Gaus(qq[0], slice->GetMean(), slice->GetStdDev(), kTRUE);
2554 Double_t error = 0;
2555 // set the errors to zero in case of small statistics
2556 if (f > 0 && n > 1)
2557 error = TMath::Sqrt( prob*(1.-prob)/ (n * f * f) );
2558 h1->SetBinError(ibin, error);
2559 }
2560 if (slice) delete slice;
2561 return h1;
2562}
2563
2564
2565////////////////////////////////////////////////////////////////////////////////
2566/// Reset this histogram: contents, errors, etc.
2567
2569{
2571 TString opt = option;
2572 opt.ToUpper();
2573
2574 if (opt.Contains("ICE") && !opt.Contains("S")) return;
2575 fTsumwy = 0;
2576 fTsumwy2 = 0;
2577 fTsumwxy = 0;
2578}
2579
2580
2581////////////////////////////////////////////////////////////////////////////////
2582/// Set bin content
2583
2585{
2586 fEntries++;
2587 fTsumw = 0;
2588 if (bin < 0) return;
2589 if (bin >= fNcells) return;
2591}
2592
2593
2594////////////////////////////////////////////////////////////////////////////////
2595/// When the mouse is moved in a pad containing a 2-d view of this histogram
2596/// a second canvas shows the projection along X corresponding to the
2597/// mouse position along Y.
2598/// To stop the generation of the projections, delete the canvas
2599/// containing the projection.
2600/// \param nbins number of bins in Y to sum across for the projection
2601
2608
2609
2610////////////////////////////////////////////////////////////////////////////////
2611/// When the mouse is moved in a pad containing a 2-d view of this histogram
2612/// a second canvas shows the projection along Y corresponding to the
2613/// mouse position along X.
2614/// To stop the generation of the projections, delete the canvas
2615/// containing the projection.
2616/// \param nbins number of bins in X to sum across for the projection
2617
2624
2625
2626////////////////////////////////////////////////////////////////////////////////
2627/// When the mouse is moved in a pad containing a 2-d view of this histogram
2628/// two canvases show the projection along X and Y corresponding to the
2629/// mouse position along Y and X, respectively.
2630/// To stop the generation of the projections, delete the canvas
2631/// containing the projection.
2632/// \param nbinsY number of bins in Y to sum across for the x projection
2633/// \param nbinsX number of bins in X to sum across for the y projection
2634
2640
2641
2642////////////////////////////////////////////////////////////////////////////////
2643/// This function calculates the background spectrum in this histogram.
2644/// The background is returned as a histogram.
2645
2647{
2648
2649 return (TH1 *)gROOT->ProcessLineFast(
2650 TString::Format("TSpectrum2::StaticBackground((TH1*)0x%zx,%d,%d,\"%s\")", (size_t)this, nIterX, nIterY, option)
2651 .Data());
2652}
2653
2654
2655////////////////////////////////////////////////////////////////////////////////
2656///Interface to TSpectrum2::Search
2657///the function finds peaks in this histogram where the width is > sigma
2658///and the peak maximum greater than threshold*maximum bin content of this.
2659///for more details see TSpectrum::Search.
2660///note the difference in the default value for option compared to TSpectrum2::Search
2661///option="" by default (instead of "goff")
2662
2664{
2665
2666 return (Int_t)gROOT->ProcessLineFast(TString::Format("TSpectrum2::StaticSearch((TH1*)0x%zx,%g,\"%s\",%g)",
2667 (size_t)this, sigma, option, threshold).Data());
2668}
2669
2670
2671////////////////////////////////////////////////////////////////////////////////
2672/// Smooth bin contents of this 2-d histogram using kernel algorithms
2673/// similar to the ones used in the raster graphics community.
2674/// Bin contents in the active range are replaced by their smooth values.
2675/// The algorithm retains the input dimension by using Kernel Crop at the input boundaries.
2676/// Kernel Crop sets any pixel in the kernel that extends past the input to zero and adjusts the
2677/// normalization accordingly.
2678/// If Errors are defined via Sumw2, they are also scaled and computed.
2679/// However, note the resulting errors will be correlated between different-bins, so
2680/// the errors should not be used blindly to perform any calculation involving several bins,
2681/// like fitting the histogram. One would need to compute also the bin by bin correlation matrix.
2682///
2683/// 3 kernels are proposed k5a, k5b and k3a.
2684/// k5a and k5b act on 5x5 cells (i-2,i-1,i,i+1,i+2, and same for j)
2685/// k5b is a bit more stronger in smoothing
2686/// k3a acts only on 3x3 cells (i-1,i,i+1, and same for j).
2687/// By default the kernel "k5a" is used. You can select the kernels "k5b" or "k3a"
2688/// via the option argument.
2689/// If TAxis::SetRange has been called on the x or/and y axis, only the bins
2690/// in the specified range are smoothed.
2691/// In the current implementation if the first argument is not used (default value=1).
2692///
2693/// implementation by David McKee (dmckee@bama.ua.edu). Extended by Rene Brun
2694
2696{
2697 Double_t k5a[5][5] = { { 0, 0, 1, 0, 0 },
2698 { 0, 2, 2, 2, 0 },
2699 { 1, 2, 5, 2, 1 },
2700 { 0, 2, 2, 2, 0 },
2701 { 0, 0, 1, 0, 0 } };
2702 Double_t k5b[5][5] = { { 0, 1, 2, 1, 0 },
2703 { 1, 2, 4, 2, 1 },
2704 { 2, 4, 8, 4, 2 },
2705 { 1, 2, 4, 2, 1 },
2706 { 0, 1, 2, 1, 0 } };
2707 Double_t k3a[3][3] = { { 0, 1, 0 },
2708 { 1, 2, 1 },
2709 { 0, 1, 0 } };
2710
2711 if (ntimes > 1) {
2712 Warning("Smooth","Currently only ntimes=1 is supported");
2713 }
2714 TString opt = option;
2715 opt.ToLower();
2716 Int_t ksize_x=5;
2717 Int_t ksize_y=5;
2718 Double_t *kernel = &k5a[0][0];
2719 if (opt.Contains("k5b")) kernel = &k5b[0][0];
2720 if (opt.Contains("k3a")) {
2721 kernel = &k3a[0][0];
2722 ksize_x=3;
2723 ksize_y=3;
2724 }
2725
2726 // find i,j ranges
2731
2732 // Determine the size of the bin buffer(s) needed
2734 Int_t nx = GetNbinsX();
2735 Int_t ny = GetNbinsY();
2736 Int_t bufSize = (nx+2)*(ny+2);
2737 Double_t *buf = new Double_t[bufSize];
2738 Double_t *ebuf = nullptr;
2739 if (fSumw2.fN) ebuf = new Double_t[bufSize];
2740
2741 // Copy all the data to the temporary buffers
2742 Int_t i,j,bin;
2743 for (i=ifirst; i<=ilast; i++){
2744 for (j=jfirst; j<=jlast; j++){
2745 bin = GetBin(i,j);
2747 if (ebuf) ebuf[bin]=GetBinError(bin);
2748 }
2749 }
2750
2751 // Kernel tail sizes (kernel sizes must be odd for this to work!)
2752 Int_t x_push = (ksize_x-1)/2;
2753 Int_t y_push = (ksize_y-1)/2;
2754
2755 // main work loop
2756 for (i=ifirst; i<=ilast; i++){
2757 for (j=jfirst; j<=jlast; j++) {
2758 Double_t content = 0.0;
2759 Double_t error = 0.0;
2760 Double_t norm = 0.0;
2761
2762 for (Int_t n=0; n<ksize_x; n++) {
2763 for (Int_t m=0; m<ksize_y; m++) {
2764 Int_t xb = i+(n-x_push);
2765 Int_t yb = j+(m-y_push);
2766 if ( (xb >= 1) && (xb <= nx) && (yb >= 1) && (yb <= ny) ) {
2767 bin = GetBin(xb,yb);
2768 Double_t k = kernel[n*ksize_y +m];
2769 //if ( (k != 0.0 ) && (buf[bin] != 0.0) ) { // General version probably does not want the second condition
2770 if ( k != 0.0 ) {
2771 norm += k;
2772 content += k*buf[bin];
2773 if (ebuf) error += k*k*ebuf[bin]*ebuf[bin];
2774 }
2775 }
2776 }
2777 }
2778
2779 if ( norm != 0.0 ) {
2781 if (ebuf) {
2782 error /= (norm*norm);
2783 SetBinError(i,j,sqrt(error));
2784 }
2785 }
2786 }
2787 }
2789
2790 delete [] buf;
2791 delete [] ebuf;
2792}
2793
2794
2795////////////////////////////////////////////////////////////////////////////////
2796/// Stream an object of class TH2.
2797
2799{
2800 if (R__b.IsReading()) {
2801 UInt_t R__s, R__c;
2802 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
2803 if (R__v > 2) {
2804 R__b.ReadClassBuffer(TH2::Class(), this, R__v, R__s, R__c);
2805 return;
2806 }
2807 //====process old versions before automatic schema evolution
2809 R__b >> fScalefactor;
2810 R__b >> fTsumwy;
2811 R__b >> fTsumwy2;
2812 R__b >> fTsumwxy;
2813 //====end of old versions
2814
2815 } else {
2816 R__b.WriteClassBuffer(TH2::Class(),this);
2817 }
2818}
2819
2820
2821//______________________________________________________________________________
2822// TH2C methods
2823// TH2C a 2-D histogram with one byte per cell (char)
2824//______________________________________________________________________________
2825
2826
2827
2828////////////////////////////////////////////////////////////////////////////////
2829/// Constructor.
2830
2832{
2833 SetBinsLength(9);
2834 if (fgDefaultSumw2) Sumw2();
2835}
2836
2837
2838////////////////////////////////////////////////////////////////////////////////
2839/// Destructor.
2840
2842
2843
2844////////////////////////////////////////////////////////////////////////////////
2845/// Constructor
2846/// (see TH2::TH2 for explanation of parameters)
2847
2848TH2C::TH2C(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_t xup
2849 ,Int_t nbinsy,Double_t ylow,Double_t yup)
2850 :TH2(name,title,nbinsx,xlow,xup,nbinsy,ylow,yup)
2851{
2853 if (fgDefaultSumw2) Sumw2();
2854
2855 if (xlow >= xup || ylow >= yup) SetBuffer(fgBufferSize);
2856}
2857
2858
2859////////////////////////////////////////////////////////////////////////////////
2860/// Constructor
2861/// (see TH2::TH2 for explanation of parameters)
2862
2863TH2C::TH2C(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
2864 ,Int_t nbinsy,Double_t ylow,Double_t yup)
2865 :TH2(name,title,nbinsx,xbins,nbinsy,ylow,yup)
2866{
2868 if (fgDefaultSumw2) Sumw2();
2869}
2870
2871
2872////////////////////////////////////////////////////////////////////////////////
2873/// Constructor
2874/// (see TH2::TH2 for explanation of parameters)
2875
2876TH2C::TH2C(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_t xup
2877 ,Int_t nbinsy,const Double_t *ybins)
2878 :TH2(name,title,nbinsx,xlow,xup,nbinsy,ybins)
2879{
2881 if (fgDefaultSumw2) Sumw2();
2882}
2883
2884
2885////////////////////////////////////////////////////////////////////////////////
2886/// Constructor
2887/// (see TH2::TH2 for explanation of parameters)
2888
2889TH2C::TH2C(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
2890 ,Int_t nbinsy,const Double_t *ybins)
2892{
2894 if (fgDefaultSumw2) Sumw2();
2895}
2896
2897
2898////////////////////////////////////////////////////////////////////////////////
2899/// Constructor
2900/// (see TH2::TH2 for explanation of parameters)
2901
2902TH2C::TH2C(const char *name,const char *title,Int_t nbinsx,const Float_t *xbins
2903 ,Int_t nbinsy,const Float_t *ybins)
2905{
2907 if (fgDefaultSumw2) Sumw2();
2908}
2909
2910
2911////////////////////////////////////////////////////////////////////////////////
2912/// Copy constructor.
2913/// The list of functions is not copied. (Use Clone() if needed)
2914
2916{
2917 h2c.TH2C::Copy(*this);
2918}
2919
2920
2921////////////////////////////////////////////////////////////////////////////////
2922/// Increment bin content by 1.
2923/// Passing an out-of-range bin leads to undefined behavior
2924
2926{
2927 if (fArray[bin] < 127) fArray[bin]++;
2928}
2929
2930
2931////////////////////////////////////////////////////////////////////////////////
2932/// Increment bin content by w.
2933/// \warning The value of w is cast to `Int_t` before being added.
2934/// Passing an out-of-range bin leads to undefined behavior
2935
2937{
2938 Int_t newval = fArray[bin] + Int_t(w);
2939 if (newval > -128 && newval < 128) {fArray[bin] = Char_t(newval); return;}
2940 if (newval < -127) fArray[bin] = -127;
2941 if (newval > 127) fArray[bin] = 127;
2942}
2943
2944
2945////////////////////////////////////////////////////////////////////////////////
2946/// Copy.
2947
2949{
2951}
2952
2953
2954////////////////////////////////////////////////////////////////////////////////
2955/// Reset this histogram: contents, errors, etc.
2956
2958{
2961}
2962
2963
2964////////////////////////////////////////////////////////////////////////////////
2965/// Set total number of bins including under/overflow
2966/// Reallocate bin contents array
2967
2969{
2970 if (n < 0) n = (fXaxis.GetNbins()+2)*(fYaxis.GetNbins()+2);
2971 fNcells = n;
2972 TArrayC::Set(n);
2973}
2974
2975
2976////////////////////////////////////////////////////////////////////////////////
2977/// Stream an object of class TH2C.
2978
2980{
2981 if (R__b.IsReading()) {
2982 UInt_t R__s, R__c;
2983 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
2984 if (R__v > 2) {
2985 R__b.ReadClassBuffer(TH2C::Class(), this, R__v, R__s, R__c);
2986 return;
2987 }
2988 //====process old versions before automatic schema evolution
2989 if (R__v < 2) {
2990 R__b.ReadVersion();
2993 R__b.ReadVersion();
2994 R__b >> fScalefactor;
2995 R__b >> fTsumwy;
2996 R__b >> fTsumwy2;
2997 R__b >> fTsumwxy;
2998 } else {
3001 R__b.CheckByteCount(R__s, R__c, TH2C::IsA());
3002 }
3003 //====end of old versions
3004
3005 } else {
3006 R__b.WriteClassBuffer(TH2C::Class(),this);
3007 }
3008}
3009
3010
3011////////////////////////////////////////////////////////////////////////////////
3012/// Operator =
3013
3015{
3016 if (this != &h2c)
3017 h2c.TH2C::Copy(*this);
3018 return *this;
3019}
3020
3021
3022////////////////////////////////////////////////////////////////////////////////
3023/// Operator *
3024
3026{
3027 TH2C hnew = h1;
3028 hnew.Scale(c1);
3029 hnew.SetDirectory(nullptr);
3030 return hnew;
3031}
3032
3033
3034////////////////////////////////////////////////////////////////////////////////
3035/// Operator +
3036
3037TH2C operator+(TH2C const &h1, TH2C const &h2)
3038{
3039 TH2C hnew = h1;
3040 hnew.Add(&h2,1);
3041 hnew.SetDirectory(nullptr);
3042 return hnew;
3043}
3044
3045
3046////////////////////////////////////////////////////////////////////////////////
3047/// Operator -
3048
3049TH2C operator-(TH2C const &h1, TH2C const &h2)
3050{
3051 TH2C hnew = h1;
3052 hnew.Add(&h2,-1);
3053 hnew.SetDirectory(nullptr);
3054 return hnew;
3055}
3056
3057
3058////////////////////////////////////////////////////////////////////////////////
3059/// Operator *
3060
3061TH2C operator*(TH2C const &h1, TH2C const &h2)
3062{
3063 TH2C hnew = h1;
3064 hnew.Multiply(&h2);
3065 hnew.SetDirectory(nullptr);
3066 return hnew;
3067}
3068
3069
3070////////////////////////////////////////////////////////////////////////////////
3071/// Operator /
3072
3073TH2C operator/(TH2C const &h1, TH2C const &h2)
3074{
3075 TH2C hnew = h1;
3076 hnew.Divide(&h2);
3077 hnew.SetDirectory(nullptr);
3078 return hnew;
3079}
3080
3081
3082//______________________________________________________________________________
3083// TH2S methods
3084// TH2S a 2-D histogram with two bytes per cell (short integer)
3085//______________________________________________________________________________
3086
3087
3088
3089////////////////////////////////////////////////////////////////////////////////
3090/// Constructor.
3091
3093{
3094 SetBinsLength(9);
3095 if (fgDefaultSumw2) Sumw2();
3096}
3097
3098
3099////////////////////////////////////////////////////////////////////////////////
3100/// Destructor.
3101
3103{
3104}
3105
3106
3107////////////////////////////////////////////////////////////////////////////////
3108/// Constructor
3109/// (see TH2::TH2 for explanation of parameters)
3110
3111TH2S::TH2S(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_t xup
3112 ,Int_t nbinsy,Double_t ylow,Double_t yup)
3113 :TH2(name,title,nbinsx,xlow,xup,nbinsy,ylow,yup)
3114{
3116 if (fgDefaultSumw2) Sumw2();
3117
3118 if (xlow >= xup || ylow >= yup) SetBuffer(fgBufferSize);
3119}
3120
3121
3122////////////////////////////////////////////////////////////////////////////////
3123/// Constructor
3124/// (see TH2::TH2 for explanation of parameters)
3125
3126TH2S::TH2S(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
3127 ,Int_t nbinsy,Double_t ylow,Double_t yup)
3128 :TH2(name,title,nbinsx,xbins,nbinsy,ylow,yup)
3129{
3131 if (fgDefaultSumw2) Sumw2();
3132}
3133
3134
3135////////////////////////////////////////////////////////////////////////////////
3136/// Constructor
3137/// (see TH2::TH2 for explanation of parameters)
3138
3139TH2S::TH2S(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_t xup
3140 ,Int_t nbinsy,const Double_t *ybins)
3141 :TH2(name,title,nbinsx,xlow,xup,nbinsy,ybins)
3142{
3144 if (fgDefaultSumw2) Sumw2();
3145}
3146
3147
3148////////////////////////////////////////////////////////////////////////////////
3149/// Constructor
3150/// (see TH2::TH2 for explanation of parameters)
3151
3152TH2S::TH2S(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
3153 ,Int_t nbinsy,const Double_t *ybins)
3155{
3157 if (fgDefaultSumw2) Sumw2();
3158}
3159
3160
3161////////////////////////////////////////////////////////////////////////////////
3162/// Constructor
3163/// (see TH2::TH2 for explanation of parameters)
3164
3165TH2S::TH2S(const char *name,const char *title,Int_t nbinsx,const Float_t *xbins
3166 ,Int_t nbinsy,const Float_t *ybins)
3168{
3170 if (fgDefaultSumw2) Sumw2();
3171}
3172
3173
3174////////////////////////////////////////////////////////////////////////////////
3175/// Copy constructor
3176/// The list of functions is not copied. (Use Clone() if needed)
3177
3179{
3180 h2s.TH2S::Copy(*this);
3181}
3182
3183
3184////////////////////////////////////////////////////////////////////////////////
3185/// Increment bin content by 1.
3186/// Passing an out-of-range bin leads to undefined behavior
3187
3189{
3190 if (fArray[bin] < 32767) fArray[bin]++;
3191}
3192
3193
3194////////////////////////////////////////////////////////////////////////////////
3195/// Increment bin content by w.
3196/// \warning The value of w is cast to `Int_t` before being added.
3197/// Passing an out-of-range bin leads to undefined behavior
3198
3200{
3201 Int_t newval = fArray[bin] + Int_t(w);
3202 if (newval > -32768 && newval < 32768) {fArray[bin] = Short_t(newval); return;}
3203 if (newval < -32767) fArray[bin] = -32767;
3204 if (newval > 32767) fArray[bin] = 32767;
3205}
3206
3207
3208////////////////////////////////////////////////////////////////////////////////
3209/// Copy.
3210
3212{
3214}
3215
3216
3217////////////////////////////////////////////////////////////////////////////////
3218/// Reset this histogram: contents, errors, etc.
3219
3221{
3224}
3225
3226
3227////////////////////////////////////////////////////////////////////////////////
3228/// Set total number of bins including under/overflow
3229/// Reallocate bin contents array
3230
3232{
3233 if (n < 0) n = (fXaxis.GetNbins()+2)*(fYaxis.GetNbins()+2);
3234 fNcells = n;
3235 TArrayS::Set(n);
3236}
3237
3238
3239////////////////////////////////////////////////////////////////////////////////
3240/// Stream an object of class TH2S.
3241
3243{
3244 if (R__b.IsReading()) {
3245 UInt_t R__s, R__c;
3246 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
3247 if (R__v > 2) {
3248 R__b.ReadClassBuffer(TH2S::Class(), this, R__v, R__s, R__c);
3249 return;
3250 }
3251 //====process old versions before automatic schema evolution
3252 if (R__v < 2) {
3253 R__b.ReadVersion();
3256 R__b.ReadVersion();
3257 R__b >> fScalefactor;
3258 R__b >> fTsumwy;
3259 R__b >> fTsumwy2;
3260 R__b >> fTsumwxy;
3261 } else {
3264 R__b.CheckByteCount(R__s, R__c, TH2S::IsA());
3265 }
3266 //====end of old versions
3267
3268 } else {
3269 R__b.WriteClassBuffer(TH2S::Class(),this);
3270 }
3271}
3272
3273
3274////////////////////////////////////////////////////////////////////////////////
3275/// Operator =
3276
3278{
3279 if (this != &h2s)
3280 h2s.TH2S::Copy(*this);
3281 return *this;
3282}
3283
3284
3285////////////////////////////////////////////////////////////////////////////////
3286/// Operator *
3287
3289{
3290 TH2S hnew = h2s;
3291 hnew.Scale(c1);
3292 hnew.SetDirectory(nullptr);
3293 return hnew;
3294}
3295
3296
3297////////////////////////////////////////////////////////////////////////////////
3298/// Operator +
3299
3300TH2S operator+(TH2S const &h1, TH2S const &h2)
3301{
3302 TH2S hnew = h1;
3303 hnew.Add(&h2,1);
3304 hnew.SetDirectory(nullptr);
3305 return hnew;
3306}
3307
3308
3309////////////////////////////////////////////////////////////////////////////////
3310/// Operator -
3311
3312TH2S operator-(TH2S const &h1, TH2S const &h2)
3313{
3314 TH2S hnew = h1;
3315 hnew.Add(&h2,-1);
3316 hnew.SetDirectory(nullptr);
3317 return hnew;
3318}
3319
3320
3321////////////////////////////////////////////////////////////////////////////////
3322/// Operator *
3323
3324TH2S operator*(TH2S const &h1, TH2S const &h2)
3325{
3326 TH2S hnew = h1;
3327 hnew.Multiply(&h2);
3328 hnew.SetDirectory(nullptr);
3329 return hnew;
3330}
3331
3332
3333////////////////////////////////////////////////////////////////////////////////
3334/// Operator /
3335
3336TH2S operator/(TH2S const &h1, TH2S const &h2)
3337{
3338 TH2S hnew = h1;
3339 hnew.Divide(&h2);
3340 hnew.SetDirectory(nullptr);
3341 return hnew;
3342}
3343
3344
3345//______________________________________________________________________________
3346// TH2I methods
3347// TH2I a 2-D histogram with four bytes per cell (32 bit integer)
3348//______________________________________________________________________________
3349
3350
3351
3352////////////////////////////////////////////////////////////////////////////////
3353/// Constructor.
3354
3356{
3357 SetBinsLength(9);
3358 if (fgDefaultSumw2) Sumw2();
3359}
3360
3361
3362////////////////////////////////////////////////////////////////////////////////
3363/// Destructor.
3364
3366{
3367}
3368
3369
3370////////////////////////////////////////////////////////////////////////////////
3371/// Constructor
3372/// (see TH2::TH2 for explanation of parameters)
3373
3374TH2I::TH2I(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_t xup
3375 ,Int_t nbinsy,Double_t ylow,Double_t yup)
3376 :TH2(name,title,nbinsx,xlow,xup,nbinsy,ylow,yup)
3377{
3379 if (fgDefaultSumw2) Sumw2();
3380
3381 if (xlow >= xup || ylow >= yup) SetBuffer(fgBufferSize);
3382}
3383
3384
3385////////////////////////////////////////////////////////////////////////////////
3386/// Constructor
3387/// (see TH2::TH2 for explanation of parameters)
3388
3389TH2I::TH2I(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
3390 ,Int_t nbinsy,Double_t ylow,Double_t yup)
3391 :TH2(name,title,nbinsx,xbins,nbinsy,ylow,yup)
3392{
3394 if (fgDefaultSumw2) Sumw2();
3395}
3396
3397
3398////////////////////////////////////////////////////////////////////////////////
3399/// Constructor
3400/// (see TH2::TH2 for explanation of parameters)
3401
3402TH2I::TH2I(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_t xup
3403 ,Int_t nbinsy,const Double_t *ybins)
3404 :TH2(name,title,nbinsx,xlow,xup,nbinsy,ybins)
3405{
3407 if (fgDefaultSumw2) Sumw2();
3408}
3409
3410
3411////////////////////////////////////////////////////////////////////////////////
3412/// Constructor
3413/// (see TH2::TH2 for explanation of parameters)
3414
3415TH2I::TH2I(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
3416 ,Int_t nbinsy,const Double_t *ybins)
3418{
3420 if (fgDefaultSumw2) Sumw2();
3421}
3422
3423
3424////////////////////////////////////////////////////////////////////////////////
3425/// Constructor
3426/// (see TH2::TH2 for explanation of parameters)
3427
3428TH2I::TH2I(const char *name,const char *title,Int_t nbinsx,const Float_t *xbins
3429 ,Int_t nbinsy,const Float_t *ybins)
3431{
3433 if (fgDefaultSumw2) Sumw2();
3434}
3435
3436
3437////////////////////////////////////////////////////////////////////////////////
3438/// Copy constructor.
3439/// The list of functions is not copied. (Use Clone() if needed)
3440
3442{
3443 h2i.TH2I::Copy(*this);
3444}
3445
3446
3447////////////////////////////////////////////////////////////////////////////////
3448/// Increment bin content by 1.
3449/// Passing an out-of-range bin leads to undefined behavior
3450
3452{
3453 if (fArray[bin] < INT_MAX) fArray[bin]++;
3454}
3455
3456
3457////////////////////////////////////////////////////////////////////////////////
3458/// Increment bin content by w.
3459/// \warning The value of w is cast to `Long64_t` before being added.
3460/// Passing an out-of-range bin leads to undefined behavior
3461
3463{
3465 if (newval > -INT_MAX && newval < INT_MAX) {fArray[bin] = Int_t(newval); return;}
3466 if (newval < -INT_MAX) fArray[bin] = -INT_MAX;
3467 if (newval > INT_MAX) fArray[bin] = INT_MAX;
3468}
3469
3470
3471////////////////////////////////////////////////////////////////////////////////
3472/// Copy.
3473
3475{
3477}
3478
3479
3480////////////////////////////////////////////////////////////////////////////////
3481/// Reset this histogram: contents, errors, etc.
3482
3484{
3487}
3488
3489
3490////////////////////////////////////////////////////////////////////////////////
3491/// Set total number of bins including under/overflow
3492/// Reallocate bin contents array
3493
3495{
3496 if (n < 0) n = (fXaxis.GetNbins()+2)*(fYaxis.GetNbins()+2);
3497 fNcells = n;
3498 TArrayI::Set(n);
3499}
3500
3501
3502////////////////////////////////////////////////////////////////////////////////
3503/// Operator =
3504
3506{
3507 if (this != &h2i)
3508 h2i.TH2I::Copy(*this);
3509 return *this;
3510}
3511
3512
3513////////////////////////////////////////////////////////////////////////////////
3514/// Operator *
3515
3517{
3518 TH2I hnew = h2i;
3519 hnew.Scale(c1);
3520 hnew.SetDirectory(nullptr);
3521 return hnew;
3522}
3523
3524
3525////////////////////////////////////////////////////////////////////////////////
3526/// Operator +
3527
3528TH2I operator+(TH2I const &h1, TH2I const &h2)
3529{
3530 TH2I hnew = h1;
3531 hnew.Add(&h2,1);
3532 hnew.SetDirectory(nullptr);
3533 return hnew;
3534}
3535
3536
3537////////////////////////////////////////////////////////////////////////////////
3538/// Operator -
3539
3540TH2I operator-(TH2I const &h1, TH2I const &h2)
3541{
3542 TH2I hnew = h1;
3543 hnew.Add(&h2,-1);
3544 hnew.SetDirectory(nullptr);
3545 return hnew;
3546}
3547
3548
3549////////////////////////////////////////////////////////////////////////////////
3550/// Operator *
3551
3552TH2I operator*(TH2I const &h1, TH2I const &h2)
3553{
3554 TH2I hnew = h1;
3555 hnew.Multiply(&h2);
3556 hnew.SetDirectory(nullptr);
3557 return hnew;
3558}
3559
3560
3561////////////////////////////////////////////////////////////////////////////////
3562/// Operator /
3563
3564TH2I operator/(TH2I const &h1, TH2I const &h2)
3565{
3566 TH2I hnew = h1;
3567 hnew.Divide(&h2);
3568 hnew.SetDirectory(nullptr);
3569 return hnew;
3570}
3571
3572
3573//______________________________________________________________________________
3574// TH2L methods
3575// TH2L a 2-D histogram with eight bytes per cell (64 bit integer)
3576//______________________________________________________________________________
3577
3578
3579
3580////////////////////////////////////////////////////////////////////////////////
3581/// Constructor.
3582
3584{
3585 SetBinsLength(9);
3586 if (fgDefaultSumw2) Sumw2();
3587}
3588
3589
3590////////////////////////////////////////////////////////////////////////////////
3591/// Destructor.
3592
3594{
3595}
3596
3597
3598////////////////////////////////////////////////////////////////////////////////
3599/// Constructor
3600/// (see TH2::TH2 for explanation of parameters)
3601
3602TH2L::TH2L(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_t xup
3603 ,Int_t nbinsy,Double_t ylow,Double_t yup)
3604 :TH2(name,title,nbinsx,xlow,xup,nbinsy,ylow,yup)
3605{
3607 if (fgDefaultSumw2) Sumw2();
3608
3609 if (xlow >= xup || ylow >= yup) SetBuffer(fgBufferSize);
3610}
3611
3612
3613////////////////////////////////////////////////////////////////////////////////
3614/// Constructor
3615/// (see TH2::TH2 for explanation of parameters)
3616
3617TH2L::TH2L(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
3618 ,Int_t nbinsy,Double_t ylow,Double_t yup)
3619 :TH2(name,title,nbinsx,xbins,nbinsy,ylow,yup)
3620{
3622 if (fgDefaultSumw2) Sumw2();
3623}
3624
3625
3626////////////////////////////////////////////////////////////////////////////////
3627/// Constructor
3628/// (see TH2::TH2 for explanation of parameters)
3629
3630TH2L::TH2L(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_t xup
3631 ,Int_t nbinsy,const Double_t *ybins)
3632 :TH2(name,title,nbinsx,xlow,xup,nbinsy,ybins)
3633{
3635 if (fgDefaultSumw2) Sumw2();
3636}
3637
3638
3639////////////////////////////////////////////////////////////////////////////////
3640/// Constructor
3641/// (see TH2::TH2 for explanation of parameters)
3642
3643TH2L::TH2L(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
3644 ,Int_t nbinsy,const Double_t *ybins)
3646{
3648 if (fgDefaultSumw2) Sumw2();
3649}
3650
3651
3652////////////////////////////////////////////////////////////////////////////////
3653/// Constructor
3654/// (see TH2::TH2 for explanation of parameters)
3655
3656TH2L::TH2L(const char *name,const char *title,Int_t nbinsx,const Float_t *xbins
3657 ,Int_t nbinsy,const Float_t *ybins)
3659{
3661 if (fgDefaultSumw2) Sumw2();
3662}
3663
3664
3665////////////////////////////////////////////////////////////////////////////////
3666/// Copy constructor.
3667/// The list of functions is not copied. (Use Clone() if needed)
3668
3670{
3671 h2l.TH2L::Copy(*this);
3672}
3673
3674
3675////////////////////////////////////////////////////////////////////////////////
3676/// Increment bin content by 1.
3677/// Passing an out-of-range bin leads to undefined behavior
3678
3680{
3681 if (fArray[bin] < LLONG_MAX) fArray[bin]++;
3682}
3683
3684
3685////////////////////////////////////////////////////////////////////////////////
3686/// Increment bin content by w.
3687/// \warning The value of w is cast to `Long64_t` before being added.
3688/// Passing an out-of-range bin leads to undefined behavior
3689
3691{
3693 if (newval > -LLONG_MAX && newval < LLONG_MAX) {fArray[bin] = newval; return;}
3694 if (newval < -LLONG_MAX) fArray[bin] = -LLONG_MAX;
3696}
3697
3698
3699////////////////////////////////////////////////////////////////////////////////
3700/// Copy.
3701
3703{
3705}
3706
3707
3708////////////////////////////////////////////////////////////////////////////////
3709/// Reset this histogram: contents, errors, etc.
3710
3716
3717
3718////////////////////////////////////////////////////////////////////////////////
3719/// Set total number of bins including under/overflow
3720/// Reallocate bin contents array
3721
3723{
3724 if (n < 0) n = (fXaxis.GetNbins()+2)*(fYaxis.GetNbins()+2);
3725 fNcells = n;
3727}
3728
3729
3730////////////////////////////////////////////////////////////////////////////////
3731/// Operator =
3732
3734{
3735 if (this != &h2l)
3736 h2l.TH2L::Copy(*this);
3737 return *this;
3738}
3739
3740
3741////////////////////////////////////////////////////////////////////////////////
3742/// Operator *
3743
3745{
3746 TH2L hnew = h1;
3747 hnew.Scale(c1);
3748 hnew.SetDirectory(nullptr);
3749 return hnew;
3750}
3751
3752
3753////////////////////////////////////////////////////////////////////////////////
3754/// Operator +
3755
3756TH2L operator+(TH2L const &h1, TH2L const &h2)
3757{
3758 TH2L hnew = h1;
3759 hnew.Add(&h2,1);
3760 hnew.SetDirectory(nullptr);
3761 return hnew;
3762}
3763
3764
3765////////////////////////////////////////////////////////////////////////////////
3766/// Operator -
3767
3768TH2L operator-(TH2L const &h1, TH2L const &h2)
3769{
3770 TH2L hnew = h1;
3771 hnew.Add(&h2,-1);
3772 hnew.SetDirectory(nullptr);
3773 return hnew;
3774}
3775
3776
3777////////////////////////////////////////////////////////////////////////////////
3778/// Operator *
3779
3780TH2L operator*(TH2L const &h1, TH2L const &h2)
3781{
3782 TH2L hnew = h1;
3783 hnew.Multiply(&h2);
3784 hnew.SetDirectory(nullptr);
3785 return hnew;
3786}
3787
3788
3789////////////////////////////////////////////////////////////////////////////////
3790/// Operator /
3791
3792TH2L operator/(TH2L const &h1, TH2L const &h2)
3793{
3794 TH2L hnew = h1;
3795 hnew.Divide(&h2);
3796 hnew.SetDirectory(nullptr);
3797 return hnew;
3798}
3799
3800
3801//______________________________________________________________________________
3802// TH2F methods
3803// TH2F a 2-D histogram with four bytes per cell (float). Maximum precision 7 digits, maximum integer bin content = +/-16777216
3804//______________________________________________________________________________
3805
3806
3807
3808////////////////////////////////////////////////////////////////////////////////
3809/// Constructor.
3810
3812{
3813 SetBinsLength(9);
3814 if (fgDefaultSumw2) Sumw2();
3815}
3816
3817
3818////////////////////////////////////////////////////////////////////////////////
3819/// Destructor.
3820
3822{
3823}
3824
3825
3826////////////////////////////////////////////////////////////////////////////////
3827/// Constructor
3828/// (see TH2::TH2 for explanation of parameters)
3829
3830TH2F::TH2F(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_t xup
3831 ,Int_t nbinsy,Double_t ylow,Double_t yup)
3832 :TH2(name,title,nbinsx,xlow,xup,nbinsy,ylow,yup)
3833{
3835 if (fgDefaultSumw2) Sumw2();
3836
3837 if (xlow >= xup || ylow >= yup) SetBuffer(fgBufferSize);
3838}
3839
3840
3841////////////////////////////////////////////////////////////////////////////////
3842/// Constructor
3843/// (see TH2::TH2 for explanation of parameters)
3844
3845TH2F::TH2F(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
3846 ,Int_t nbinsy,Double_t ylow,Double_t yup)
3847 :TH2(name,title,nbinsx,xbins,nbinsy,ylow,yup)
3848{
3850 if (fgDefaultSumw2) Sumw2();
3851}
3852
3853
3854////////////////////////////////////////////////////////////////////////////////
3855/// Constructor
3856/// (see TH2::TH2 for explanation of parameters)
3857
3858TH2F::TH2F(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_t xup
3859 ,Int_t nbinsy,const Double_t *ybins)
3860 :TH2(name,title,nbinsx,xlow,xup,nbinsy,ybins)
3861{
3863 if (fgDefaultSumw2) Sumw2();
3864}
3865
3866
3867////////////////////////////////////////////////////////////////////////////////
3868/// Constructor
3869/// (see TH2::TH2 for explanation of parameters)
3870
3871TH2F::TH2F(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
3872 ,Int_t nbinsy,const Double_t *ybins)
3874{
3876 if (fgDefaultSumw2) Sumw2();
3877}
3878
3879
3880////////////////////////////////////////////////////////////////////////////////
3881/// Constructor
3882/// (see TH2::TH2 for explanation of parameters)
3883
3884TH2F::TH2F(const char *name,const char *title,Int_t nbinsx,const Float_t *xbins
3885 ,Int_t nbinsy,const Float_t *ybins)
3887{
3889 if (fgDefaultSumw2) Sumw2();
3890}
3891
3892
3893////////////////////////////////////////////////////////////////////////////////
3894/// Constructor.
3895/// Construct a TH2F from a TMatrixFBase
3896
3898:TH2("TMatrixFBase","",m.GetNcols(),m.GetColLwb(),1+m.GetColUpb(),m.GetNrows(),m.GetRowLwb(),1+m.GetRowUpb())
3899{
3901 Int_t ilow = m.GetRowLwb();
3902 Int_t iup = m.GetRowUpb();
3903 Int_t jlow = m.GetColLwb();
3904 Int_t jup = m.GetColUpb();
3905 for (Int_t i=ilow;i<=iup;i++) {
3906 for (Int_t j=jlow;j<=jup;j++) {
3907 SetBinContent(j-jlow+1,i-ilow+1,m(i,j));
3908 }
3909 }
3910}
3911
3912
3913////////////////////////////////////////////////////////////////////////////////
3914/// Copy constructor.
3915/// The list of functions is not copied. (Use Clone() if needed)
3916
3918{
3919 h2f.TH2F::Copy(*this);
3920}
3921
3922
3923////////////////////////////////////////////////////////////////////////////////
3924/// Copy.
3925
3927{
3929}
3930
3931
3932////////////////////////////////////////////////////////////////////////////////
3933/// Reset this histogram: contents, errors, etc.
3934
3936{
3939}
3940
3941
3942////////////////////////////////////////////////////////////////////////////////
3943/// Set total number of bins including under/overflow
3944/// Reallocate bin contents array
3945
3947{
3948 if (n < 0) n = (fXaxis.GetNbins()+2)*(fYaxis.GetNbins()+2);
3949 fNcells = n;
3950 TArrayF::Set(n);
3951}
3952
3953
3954////////////////////////////////////////////////////////////////////////////////
3955/// Stream an object of class TH2F.
3956
3958{
3959 if (R__b.IsReading()) {
3960 UInt_t R__s, R__c;
3961 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
3962 if (R__v > 2) {
3963 R__b.ReadClassBuffer(TH2F::Class(), this, R__v, R__s, R__c);
3964 return;
3965 }
3966 //====process old versions before automatic schema evolution
3967 if (R__v < 2) {
3968 R__b.ReadVersion();
3971 R__b.ReadVersion();
3972 R__b >> fScalefactor;
3973 R__b >> fTsumwy;
3974 R__b >> fTsumwy2;
3975 R__b >> fTsumwxy;
3976 } else {
3979 R__b.CheckByteCount(R__s, R__c, TH2F::IsA());
3980 }
3981 //====end of old versions
3982
3983 } else {
3984 R__b.WriteClassBuffer(TH2F::Class(),this);
3985 }
3986}
3987
3988
3989////////////////////////////////////////////////////////////////////////////////
3990/// Operator =
3991
3993{
3994 if (this != &h2f)
3995 h2f.TH2F::Copy(*this);
3996 return *this;
3997}
3998
3999
4000////////////////////////////////////////////////////////////////////////////////
4001/// Operator *
4002
4004{
4005 TH2F hnew = h1;
4006 hnew.Scale(c1);
4007 hnew.SetDirectory(nullptr);
4008 return hnew;
4009}
4010
4011
4012////////////////////////////////////////////////////////////////////////////////
4013/// Operator *
4014
4016{
4017 TH2F hnew = h1;
4018 hnew.Scale(c1);
4019 hnew.SetDirectory(nullptr);
4020 return hnew;
4021}
4022
4023
4024////////////////////////////////////////////////////////////////////////////////
4025/// Operator +
4026
4027TH2F operator+(TH2F const &h1, TH2F const &h2)
4028{
4029 TH2F hnew = h1;
4030 hnew.Add(&h2,1);
4031 hnew.SetDirectory(nullptr);
4032 return hnew;
4033}
4034
4035
4036////////////////////////////////////////////////////////////////////////////////
4037/// Operator -
4038
4039TH2F operator-(TH2F const &h1, TH2F const &h2)
4040{
4041 TH2F hnew = h1;
4042 hnew.Add(&h2,-1);
4043 hnew.SetDirectory(nullptr);
4044 return hnew;
4045}
4046
4047
4048////////////////////////////////////////////////////////////////////////////////
4049/// Operator *
4050
4051TH2F operator*(TH2F const &h1, TH2F const &h2)
4052{
4053 TH2F hnew = h1;
4054 hnew.Multiply(&h2);
4055 hnew.SetDirectory(nullptr);
4056 return hnew;
4057}
4058
4059
4060////////////////////////////////////////////////////////////////////////////////
4061/// Operator /
4062
4063TH2F operator/(TH2F const &h1, TH2F const &h2)
4064{
4065 TH2F hnew = h1;
4066 hnew.Divide(&h2);
4067 hnew.SetDirectory(nullptr);
4068 return hnew;
4069}
4070
4071
4072//______________________________________________________________________________
4073// TH2D methods
4074// TH2D a 2-D histogram with eight bytes per cell (double). Maximum precision 14 digits, maximum integer bin content = +/-9007199254740992
4075//______________________________________________________________________________
4076
4077
4078
4079////////////////////////////////////////////////////////////////////////////////
4080/// Constructor.
4081
4083{
4084 SetBinsLength(9);
4085 if (fgDefaultSumw2) Sumw2();
4086}
4087
4088
4089////////////////////////////////////////////////////////////////////////////////
4090/// Destructor.
4091
4093{
4094}
4095
4096
4097////////////////////////////////////////////////////////////////////////////////
4098/// Constructor
4099/// (see TH2::TH2 for explanation of parameters)
4100
4101TH2D::TH2D(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_t xup
4102 ,Int_t nbinsy,Double_t ylow,Double_t yup)
4103 :TH2(name,title,nbinsx,xlow,xup,nbinsy,ylow,yup)
4104{
4106 if (fgDefaultSumw2) Sumw2();
4107
4108 if (xlow >= xup || ylow >= yup) SetBuffer(fgBufferSize);
4109}
4110
4111
4112////////////////////////////////////////////////////////////////////////////////
4113/// Constructor
4114/// (see TH2::TH2 for explanation of parameters)
4115
4116TH2D::TH2D(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
4117 ,Int_t nbinsy,Double_t ylow,Double_t yup)
4118 :TH2(name,title,nbinsx,xbins,nbinsy,ylow,yup)
4119{
4121 if (fgDefaultSumw2) Sumw2();
4122}
4123
4124
4125////////////////////////////////////////////////////////////////////////////////
4126/// Constructor
4127/// (see TH2::TH2 for explanation of parameters)
4128
4129TH2D::TH2D(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_t xup
4130 ,Int_t nbinsy,const Double_t *ybins)
4131 :TH2(name,title,nbinsx,xlow,xup,nbinsy,ybins)
4132{
4134 if (fgDefaultSumw2) Sumw2();
4135}
4136
4137
4138////////////////////////////////////////////////////////////////////////////////
4139/// Constructor
4140/// (see TH2::TH2 for explanation of parameters)
4141
4142TH2D::TH2D(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
4143 ,Int_t nbinsy,const Double_t *ybins)
4145{
4147 if (fgDefaultSumw2) Sumw2();
4148}
4149
4150
4151////////////////////////////////////////////////////////////////////////////////
4152/// Constructor
4153/// (see TH2::TH2 for explanation of parameters)
4154
4155TH2D::TH2D(const char *name,const char *title,Int_t nbinsx,const Float_t *xbins
4156 ,Int_t nbinsy,const Float_t *ybins)
4158{
4160 if (fgDefaultSumw2) Sumw2();
4161}
4162
4163
4164////////////////////////////////////////////////////////////////////////////////
4165/// Constructor
4166/// Construct a 2-D histogram from a TMatrixDBase
4167
4169:TH2("TMatrixDBase","",m.GetNcols(),m.GetColLwb(),1+m.GetColUpb(),m.GetNrows(),m.GetRowLwb(),1+m.GetRowUpb())
4170{
4172 Int_t ilow = m.GetRowLwb();
4173 Int_t iup = m.GetRowUpb();
4174 Int_t jlow = m.GetColLwb();
4175 Int_t jup = m.GetColUpb();
4176 for (Int_t i=ilow;i<=iup;i++) {
4177 for (Int_t j=jlow;j<=jup;j++) {
4178 SetBinContent(j-jlow+1,i-ilow+1,m(i,j));
4179 }
4180 }
4181 if (fgDefaultSumw2) Sumw2();
4182}
4183
4184
4185////////////////////////////////////////////////////////////////////////////////
4186/// Copy constructor.
4187/// The list of functions is not copied. (Use Clone() if needed)
4188
4190{
4191 // intentionally call virtual Copy method to warn if TProfile2D is copied
4192 h2d.Copy(*this);
4193}
4194
4195
4196////////////////////////////////////////////////////////////////////////////////
4197/// Copy.
4198
4200{
4202}
4203
4204
4205////////////////////////////////////////////////////////////////////////////////
4206/// Reset this histogram: contents, errors, etc.
4207
4209{
4212}
4213
4214
4215////////////////////////////////////////////////////////////////////////////////
4216/// Set total number of bins including under/overflow
4217/// Reallocate bin contents array
4218
4220{
4221 if (n < 0) n = (fXaxis.GetNbins()+2)*(fYaxis.GetNbins()+2);
4222 fNcells = n;
4223 TArrayD::Set(n);
4224}
4225
4226
4227////////////////////////////////////////////////////////////////////////////////
4228/// Stream an object of class TH2D.
4229
4231{
4232 if (R__b.IsReading()) {
4233 UInt_t R__s, R__c;
4234 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
4235 if (R__v > 2) {
4236 R__b.ReadClassBuffer(TH2D::Class(), this, R__v, R__s, R__c);
4237 return;
4238 }
4239 //====process old versions before automatic schema evolution
4240 if (R__v < 2) {
4241 R__b.ReadVersion();
4244 R__b.ReadVersion();
4245 R__b >> fScalefactor;
4246 R__b >> fTsumwy;
4247 R__b >> fTsumwy2;
4248 R__b >> fTsumwxy;
4249 } else {
4252 R__b.CheckByteCount(R__s, R__c, TH2D::IsA());
4253 }
4254 //====end of old versions
4255
4256 } else {
4257 R__b.WriteClassBuffer(TH2D::Class(),this);
4258 }
4259}
4260
4261
4262////////////////////////////////////////////////////////////////////////////////
4263/// Operator =
4264
4266{
4267 // intentionally call virtual Copy method to warn if TProfile2D is copied
4268 if (this != &h2d)
4269 h2d.Copy(*this);
4270 return *this;
4271}
4272
4273
4274
4275////////////////////////////////////////////////////////////////////////////////
4276/// Operator *
4277
4279{
4280 TH2D hnew = h2d;
4281 hnew.Scale(c1);
4282 hnew.SetDirectory(nullptr);
4283 return hnew;
4284}
4285
4286
4287////////////////////////////////////////////////////////////////////////////////
4288/// Operator +
4289
4290TH2D operator+(TH2D const &h1, TH2D const &h2)
4291{
4292 TH2D hnew = h1;
4293 hnew.Add(&h2,1);
4294 hnew.SetDirectory(nullptr);
4295 return hnew;
4296}
4297
4298
4299////////////////////////////////////////////////////////////////////////////////
4300/// Operator -
4301
4302TH2D operator-(TH2D const &h1, TH2D const &h2)
4303{
4304 TH2D hnew = h1;
4305 hnew.Add(&h2,-1);
4306 hnew.SetDirectory(nullptr);
4307 return hnew;
4308}
4309
4310
4311////////////////////////////////////////////////////////////////////////////////
4312/// Operator *
4313
4314TH2D operator*(TH2D const &h1, TH2D const &h2)
4315{
4316 TH2D hnew = h1;
4317 hnew.Multiply(&h2);
4318 hnew.SetDirectory(nullptr);
4319 return hnew;
4320}
4321
4322
4323////////////////////////////////////////////////////////////////////////////////
4324/// Operator /
4325
4326TH2D operator/(TH2D const &h1, TH2D const &h2)
4327{
4328 TH2D hnew = h1;
4329 hnew.Divide(&h2);
4330 hnew.SetDirectory(nullptr);
4331 return hnew;
4332}
#define d(i)
Definition RSha256.hxx:102
#define f(i)
Definition RSha256.hxx:104
#define c(i)
Definition RSha256.hxx:101
#define s1(x)
Definition RSha256.hxx:91
#define h(i)
Definition RSha256.hxx:106
#define e(i)
Definition RSha256.hxx:103
short Style_t
Style number (short)
Definition RtypesCore.h:97
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:60
short Color_t
Color number (short)
Definition RtypesCore.h:100
short Version_t
Class version identifier (short)
Definition RtypesCore.h:80
char Char_t
Character 1 byte (char)
Definition RtypesCore.h:52
float Float_t
Float 4 bytes (float)
Definition RtypesCore.h:72
short Short_t
Signed Short integer 2 bytes (short)
Definition RtypesCore.h:54
constexpr Bool_t kFALSE
Definition RtypesCore.h:109
long long Long64_t
Portable signed long integer 8 bytes.
Definition RtypesCore.h:84
constexpr Bool_t kTRUE
Definition RtypesCore.h:108
const char Option_t
Option string (const char)
Definition RtypesCore.h:81
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#define gDirectory
Definition TDirectory.h:385
Option_t Option_t option
Option_t Option_t TPoint TPoint const char x2
Option_t Option_t TPoint TPoint const char x1
Option_t Option_t TPoint TPoint const char y2
Option_t Option_t TPoint TPoint const char y1
char name[80]
Definition TGX11.cxx:142
TH2C operator+(TH2C const &h1, TH2C const &h2)
Operator +.
Definition TH2.cxx:3037
TH2C operator*(Float_t c1, TH2C const &h1)
Operator *.
Definition TH2.cxx:3025
TH2C operator/(TH2C const &h1, TH2C const &h2)
Operator /.
Definition TH2.cxx:3073
TH2C operator-(TH2C const &h1, TH2C const &h2)
Operator -.
Definition TH2.cxx:3049
float xmin
int nentries
float ymin
float xmax
float ymax
#define gROOT
Definition TROOT.h:417
R__EXTERN TRandom * gRandom
Definition TRandom.h:73
#define gPad
Array of chars or bytes (8 bits per element).
Definition TArrayC.h:27
void Streamer(TBuffer &) override
Stream a TArrayC object.
Definition TArrayC.cxx:147
Char_t * fArray
Definition TArrayC.h:30
void Reset(Char_t val=0)
Definition TArrayC.h:47
void Set(Int_t n) override
Set size of this array to n chars.
Definition TArrayC.cxx:104
Array of doubles (64 bits per element).
Definition TArrayD.h:27
Double_t * fArray
Definition TArrayD.h:30
void Streamer(TBuffer &) override
Stream a TArrayD object.
Definition TArrayD.cxx:148
void Set(Int_t n) override
Set size of this array to n doubles.
Definition TArrayD.cxx:105
Stat_t GetSum() const
Definition TArrayD.h:46
void Reset()
Definition TArrayD.h:47
Array of floats (32 bits per element).
Definition TArrayF.h:27
void Reset()
Definition TArrayF.h:47
void Set(Int_t n) override
Set size of this array to n floats.
Definition TArrayF.cxx:104
void Streamer(TBuffer &) override
Stream a TArrayF object.
Definition TArrayF.cxx:147
Array of integers (32 bits per element).
Definition TArrayI.h:27
Int_t * fArray
Definition TArrayI.h:30
void Set(Int_t n) override
Set size of this array to n ints.
Definition TArrayI.cxx:104
void Reset()
Definition TArrayI.h:47
Array of long64s (64 bits per element).
Definition TArrayL64.h:27
Long64_t * fArray
Definition TArrayL64.h:30
void Set(Int_t n) override
Set size of this array to n long64s.
void Reset()
Definition TArrayL64.h:47
Array of shorts (16 bits per element).
Definition TArrayS.h:27
void Set(Int_t n) override
Set size of this array to n shorts.
Definition TArrayS.cxx:104
void Streamer(TBuffer &) override
Stream a TArrayS object.
Definition TArrayS.cxx:147
void Reset()
Definition TArrayS.h:47
Short_t * fArray
Definition TArrayS.h:30
Int_t fN
Definition TArray.h:38
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 Style_t GetTitleFont() const
Definition TAttAxis.h:48
virtual Float_t GetLabelOffset() const
Definition TAttAxis.h:41
virtual Style_t GetLabelFont() const
Definition TAttAxis.h:40
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 Float_t GetTitleOffset() const
Definition TAttAxis.h:44
virtual Color_t GetFillColor() const
Return the fill area color.
Definition TAttFill.h:32
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:40
virtual Color_t GetLineColor() const
Return the line color.
Definition TAttLine.h:36
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:44
virtual Style_t GetMarkerStyle() const
Return the marker style.
Definition TAttMarker.h:35
virtual Color_t GetMarkerColor() const
Return the marker color.
Definition TAttMarker.h:34
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
virtual void SetMarkerColor(Color_t mcolor=1)
Set the marker color.
Class to manage histogram axis.
Definition TAxis.h:32
virtual void SetBinLabel(Int_t bin, const char *label)
Set label for bin.
Definition TAxis.cxx:891
Bool_t IsAlphanumeric() const
Definition TAxis.h:90
virtual Double_t GetBinCenter(Int_t bin) const
Return center of bin.
Definition TAxis.cxx:482
Bool_t CanExtend() const
Definition TAxis.h:88
void SetCanExtend(Bool_t canExtend)
Definition TAxis.h:92
Double_t GetXmax() const
Definition TAxis.h:142
@ kAxisRange
Definition TAxis.h:66
virtual Int_t FindBin(Double_t x)
Find bin number corresponding to abscissa x.
Definition TAxis.cxx:293
virtual Double_t GetBinLowEdge(Int_t bin) const
Return low edge of bin.
Definition TAxis.cxx:522
virtual void Set(Int_t nbins, Double_t xmin, Double_t xmax)
Initialize axis with fix bins.
Definition TAxis.cxx:790
virtual Int_t FindFixBin(Double_t x) const
Find bin number corresponding to abscissa x
Definition TAxis.cxx:422
Int_t GetLast() const
Return last bin on the axis i.e.
Definition TAxis.cxx:473
virtual void ImportAttributes(const TAxis *axis)
Copy axis attributes to this.
Definition TAxis.cxx:685
Double_t GetXmin() const
Definition TAxis.h:141
Int_t GetNbins() const
Definition TAxis.h:127
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
Buffer base class used for serializing objects.
Definition TBuffer.h:43
1-Dim function class
Definition TF1.h:182
virtual TH1 * GetHistogram() const
Return a pointer to the histogram used to visualise the function Note that this histogram is managed ...
Definition TF1.cxx:1635
virtual Double_t GetParError(Int_t ipar) const
Return value of parameter number ipar.
Definition TF1.cxx:1981
Double_t GetChisquare() const
Return the Chisquare after fitting. See ROOT::Fit::FitResult::Chi2()
Definition TF1.h:409
virtual void SetRange(Double_t xmin, Double_t xmax)
Initialize the upper and lower bounds to draw the function.
Definition TF1.cxx:3584
virtual Int_t GetNpar() const
Definition TF1.h:446
virtual Double_t Integral(Double_t a, Double_t b, Double_t epsrel=1.e-12)
IntegralOneDim or analytical integral.
Definition TF1.cxx:2581
virtual Int_t GetNumberFitPoints() const
Definition TF1.h:468
virtual Double_t * GetParameters() const
Definition TF1.h:485
virtual void GetRange(Double_t *xmin, Double_t *xmax) const
Return range of a generic N-D function.
Definition TF1.cxx:2330
virtual const char * GetParName(Int_t ipar) const
Definition TF1.h:494
virtual void SetParameters(const Double_t *params)
Definition TF1.h:618
TClass * IsA() const override
Definition TF1.h:694
virtual Double_t GetParameter(Int_t ipar) const
Definition TF1.h:477
A 2-Dim function with parameters.
Definition TF2.h:29
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:926
static TClass * Class()
void Reset(Option_t *option="") override
Reset.
Definition TH1.cxx:10516
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:109
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:9170
Double_t * fBuffer
[fBufferSize] entry buffer
Definition TH1.h:169
virtual Double_t GetEffectiveEntries() const
Number of effective entries of the histogram.
Definition TH1.cxx:4599
Int_t fNcells
Number of bins(1D), cells (2D) +U/Overflows.
Definition TH1.h:150
void Copy(TObject &hnew) const override
Copy this histogram structure to newth1.
Definition TH1.cxx:2801
Double_t fTsumw
Total Sum of weights.
Definition TH1.h:157
Double_t fTsumw2
Total Sum of squares of weights.
Definition TH1.h:158
static TClass * Class()
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:8206
Double_t fTsumwx2
Total Sum of weight*X*X.
Definition TH1.h:160
virtual Double_t GetStdDev(Int_t axis=1) const
Returns the Standard Deviation (Sigma).
Definition TH1.cxx:7816
virtual Int_t GetNbinsY() const
Definition TH1.h:542
virtual Double_t GetBinError(Int_t bin) const
Return value of error associated to bin number bin.
Definition TH1.cxx:9293
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:7744
virtual Int_t GetDimension() const
Definition TH1.h:527
void Streamer(TBuffer &) override
Stream a class object.
Definition TH1.cxx:7154
@ kIsNotW
Histogram is forced to be not weighted even when the histogram is filled with weighted.
Definition TH1.h:410
virtual Bool_t CanExtendAllAxes() const
Returns true if all axes are extendable.
Definition TH1.cxx:6847
virtual void Reset(Option_t *option="")
Reset this histogram: contents, errors, etc.
Definition TH1.cxx:7324
TAxis * GetXaxis()
Definition TH1.h:571
virtual Int_t GetNcells() const
Definition TH1.h:544
virtual void PutStats(Double_t *stats)
Replace current statistics with the values in array stats.
Definition TH1.cxx:8093
TVirtualHistPainter * GetPainter(Option_t *option="")
Return pointer to painter.
Definition TH1.cxx:4662
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:5137
virtual Int_t GetNbinsX() const
Definition TH1.h:541
Int_t fBufferSize
fBuffer size
Definition TH1.h:168
Int_t fDimension
! Histogram dimension (1, 2 or 3 dim)
Definition TH1.h:171
virtual void SetBinError(Int_t bin, Double_t error)
Set the bin Error Note that this resets the bin eror option to be of Normal Type and for the non-empt...
Definition TH1.cxx:9436
static Int_t fgBufferSize
! Default buffer size for automatic histograms
Definition TH1.h:176
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition TH1.cxx:3489
TAxis * GetYaxis()
Definition TH1.h:572
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3193
virtual Double_t GetBinErrorSqUnchecked(Int_t bin) const
Definition TH1.h:705
virtual void SetBuffer(Int_t bufsize, Option_t *option="")
Set the maximum number of entries to be kept in the buffer.
Definition TH1.cxx:8687
UInt_t GetAxisLabelStatus() const
Internal function used in TH1::Fill to see which axis is full alphanumeric, i.e.
Definition TH1.cxx:6886
Double_t * fIntegral
! Integral of bins used by GetRandom
Definition TH1.h:172
@ kNstat
Size of statistics data (up to TProfile3D)
Definition TH1.h:422
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:9452
virtual Double_t GetBinLowEdge(Int_t bin) const
Return bin lower edge for 1D histogram.
Definition TH1.cxx:9382
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...
void Paint(Option_t *option="") override
Control routine to paint any kind of histograms.
Definition TH1.cxx:6417
virtual void ResetStats()
Reset the statistics including the number of entries and replace with values calculated from bin cont...
Definition TH1.cxx:8111
Double_t fEntries
Number of entries.
Definition TH1.h:156
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
Definition TH1.cxx:5239
TAxis fXaxis
X axis descriptor.
Definition TH1.h:151
virtual void ExtendAxis(Double_t x, TAxis *axis)
Histogram is resized along axis such that x is in the axis range.
Definition TH1.cxx:6715
TArrayD fSumw2
Array of sum of squares of weights.
Definition TH1.h:165
virtual Double_t ComputeIntegral(Bool_t onlyPositive=false, Option_t *option="")
Compute integral (normalized cumulative sum of bins) w/o under/overflows The result is stored in fInt...
Definition TH1.cxx:2581
virtual Int_t GetSumw2N() const
Definition TH1.h:562
Bool_t GetStatOverflowsBehaviour() const
Definition TH1.h:391
virtual Int_t GetQuantiles(Int_t n, Double_t *xp, const Double_t *p=nullptr)
Compute Quantiles for this histogram.
Definition TH1.cxx:4766
TObject * Clone(const char *newname="") const override
Make a complete copy of the underlying object.
Definition TH1.cxx:2882
TAxis fYaxis
Y axis descriptor.
Definition TH1.h:152
@ kXaxis
Definition TH1.h:123
@ kYaxis
Definition TH1.h:124
TVirtualHistPainter * fPainter
! Pointer to histogram painter
Definition TH1.h:173
virtual void SetBins(Int_t nx, Double_t xmin, Double_t xmax)
Redefine x axis parameters.
Definition TH1.cxx:9000
virtual void Sumw2(Bool_t flag=kTRUE)
Create structure to store sum of squares of weights.
Definition TH1.cxx:9253
virtual void SetEntries(Double_t n)
Definition TH1.h:639
static Bool_t fgDefaultSumw2
! Flag to call TH1::Sumw2 automatically at histogram creation time
Definition TH1.h:179
virtual void UpdateBinContent(Int_t bin, Double_t content)=0
Raw update of bin content on internal data structure see convention for numbering bins in TH1::GetBin...
Double_t fTsumwx
Total Sum of weight*X.
Definition TH1.h:159
2-D histogram with a byte per channel (see TH1 documentation)
Definition TH2.h:143
void Reset(Option_t *option="") override
Reset this histogram: contents, errors, etc.
Definition TH2.cxx:2957
static TClass * Class()
TClass * IsA() const override
Definition TH2.h:179
void Streamer(TBuffer &) override
Stream an object of class TH2C.
Definition TH2.cxx:2979
void AddBinContent(Int_t bin) override
Increment bin content by 1.
Definition TH2.cxx:2925
TH2C()
Constructor.
Definition TH2.cxx:2831
TH2C & operator=(const TH2C &h1)
Operator =.
Definition TH2.cxx:3014
~TH2C() override
Destructor.
Definition TH2.cxx:2841
void Copy(TObject &hnew) const override
Copy.
Definition TH2.cxx:2948
void SetBinsLength(Int_t n=-1) override
Set total number of bins including under/overflow Reallocate bin contents array.
Definition TH2.cxx:2968
2-D histogram with a double per channel (see TH1 documentation)
Definition TH2.h:400
void Streamer(TBuffer &) override
Stream an object of class TH2D.
Definition TH2.cxx:4230
static TClass * Class()
TClass * IsA() const override
Definition TH2.h:442
void SetBinsLength(Int_t n=-1) override
Set total number of bins including under/overflow Reallocate bin contents array.
Definition TH2.cxx:4219
~TH2D() override
Destructor.
Definition TH2.cxx:4092
void Copy(TObject &hnew) const override
Copy.
Definition TH2.cxx:4199
TH2D()
Constructor.
Definition TH2.cxx:4082
TH2D & operator=(const TH2D &h1)
Operator =.
Definition TH2.cxx:4265
2-D histogram with a float per channel (see TH1 documentation)
Definition TH2.h:345
TH2F()
Constructor.
Definition TH2.cxx:3811
TClass * IsA() const override
Definition TH2.h:388
TH2F & operator=(const TH2F &h1)
Operator =.
Definition TH2.cxx:3992
~TH2F() override
Destructor.
Definition TH2.cxx:3821
void Copy(TObject &hnew) const override
Copy.
Definition TH2.cxx:3926
static TClass * Class()
void Streamer(TBuffer &) override
Stream an object of class TH2F.
Definition TH2.cxx:3957
void SetBinsLength(Int_t n=-1) override
Set total number of bins including under/overflow Reallocate bin contents array.
Definition TH2.cxx:3946
2-D histogram with an int per channel (see TH1 documentation)
Definition TH2.h:245
TH2I()
Constructor.
Definition TH2.cxx:3355
void Copy(TObject &hnew) const override
Copy.
Definition TH2.cxx:3474
void AddBinContent(Int_t bin) override
Increment bin content by 1.
Definition TH2.cxx:3451
~TH2I() override
Destructor.
Definition TH2.cxx:3365
TH2I & operator=(const TH2I &h1)
Operator =.
Definition TH2.cxx:3505
void SetBinsLength(Int_t n=-1) override
Set total number of bins including under/overflow Reallocate bin contents array.
Definition TH2.cxx:3494
2-D histogram with a long64 per channel (see TH1 documentation)
Definition TH2.h:296
TH2L & operator=(const TH2L &h1)
Operator =.
Definition TH2.cxx:3733
void SetBinsLength(Int_t n=-1) override
Set total number of bins including under/overflow Reallocate bin contents array.
Definition TH2.cxx:3722
~TH2L() override
Destructor.
Definition TH2.cxx:3593
void Copy(TObject &hnew) const override
Copy.
Definition TH2.cxx:3702
TH2L()
Constructor.
Definition TH2.cxx:3583
void AddBinContent(Int_t bin) override
Increment bin content by 1.
Definition TH2.cxx:3679
2-D histogram with a short per channel (see TH1 documentation)
Definition TH2.h:194
void AddBinContent(Int_t bin) override
Increment bin content by 1.
Definition TH2.cxx:3188
~TH2S() override
Destructor.
Definition TH2.cxx:3102
static TClass * Class()
TH2S & operator=(const TH2S &h1)
Operator =.
Definition TH2.cxx:3277
void Copy(TObject &hnew) const override
Copy.
Definition TH2.cxx:3211
TH2S()
Constructor.
Definition TH2.cxx:3092
void Streamer(TBuffer &) override
Stream an object of class TH2S.
Definition TH2.cxx:3242
void SetBinsLength(Int_t n=-1) override
Set total number of bins including under/overflow Reallocate bin contents array.
Definition TH2.cxx:3231
TClass * IsA() const override
Definition TH2.h:230
Service class for 2-D histogram classes.
Definition TH2.h:30
TH1D * ProjectionY(const char *name="_py", Int_t firstxbin=0, Int_t lastxbin=-1, Option_t *option="") const
Project a 2-D histogram into a 1-D histogram along Y (integration along X).
Definition TH2.cxx:2444
void GetStats(Double_t *stats) const override
Fill the array stats from the contents of this histogram The array stats must be correctly dimensione...
Definition TH2.cxx:1230
Int_t ShowPeaks(Double_t sigma=2, Option_t *option="", Double_t threshold=0.05) override
Interface to TSpectrum2::Search the function finds peaks in this histogram where the width is > sigma...
Definition TH2.cxx:2663
virtual Double_t GetCorrelationFactor(Int_t axis1=1, Int_t axis2=2) const
Return correlation factor between axis1 and axis2.
Definition TH2.cxx:1119
virtual TProfile * DoProfile(bool onX, const char *name, Int_t firstbin, Int_t lastbin, Option_t *option) const
Definition TH2.cxx:1832
virtual void GetRandom2(Double_t &x, Double_t &y, TRandom *rng=nullptr, Option_t *option="")
Return 2 random numbers along axis x and y distributed according to the cell-contents of this 2-D his...
Definition TH2.cxx:1175
Double_t KolmogorovTest(const TH1 *h2, Option_t *option="") const override
Statistical test of compatibility in shape between THIS histogram and h2, using Kolmogorov test.
Definition TH2.cxx:1446
virtual void FitSlicesY(TF1 *f1=nullptr, Int_t firstxbin=0, Int_t lastxbin=-1, Int_t cut=0, Option_t *option="QNR", TObjArray *arr=nullptr)
Project slices along Y in case of a 2-D histogram, then fit each slice with function f1 and make a hi...
Definition TH2.cxx:1055
virtual Double_t GetBinWithContent2(Double_t c, Int_t &binx, Int_t &biny, Int_t firstxbin=1, Int_t lastxbin=-1, Int_t firstybin=1, Int_t lastybin=-1, Double_t maxdiff=0) const
compute first cell (binx,biny) in the range [firstxbin,lastxbin][firstybin,lastybin] for which diff =...
Definition TH2.cxx:1091
TProfile * ProfileX(const char *name="_pfx", Int_t firstybin=1, Int_t lastybin=-1, Option_t *option="") const
Project a 2-D histogram into a profile histogram along X (integration along Y).
Definition TH2.cxx:2065
TH2 * Rebin(Int_t ngroup=2, const char *newname="", const Double_t *xbins=nullptr) override
Override TH1::Rebin, rebinning only the X axis with the same conventions as the TH1 function (ngroup ...
Definition TH2.cxx:1639
void FillN(Int_t, const Double_t *, const Double_t *, Int_t) override
Fill this histogram with an array x and weights w.
Definition TH2.h:87
void FillRandom(TF1 *function, Int_t ntimes=5000, TRandom *rng=nullptr) override
Fill histogram following distribution in function function.
Definition TH2.cxx:689
TH1D * QuantilesY(Double_t prob=0.5, const char *name="_qy") const
Compute the Y distribution of quantiles in the other variable X name is the name of the returned hist...
Definition TH2.cxx:2486
void AddBinContent(Int_t binx, Int_t biny)
Increment 2D bin content by 1.
Definition TH2.h:76
TProfile * ProfileY(const char *name="_pfy", Int_t firstxbin=1, Int_t lastxbin=-1, Option_t *option="") const
Project a 2-D histogram into a profile histogram along Y (integration along X).
Definition TH2.cxx:2118
void Copy(TObject &hnew) const override
Copy.
Definition TH2.cxx:355
virtual TH1D * DoQuantiles(bool onX, const char *name, Double_t prob) const
Implementation of quantiles for x or y.
Definition TH2.cxx:2495
virtual TH2 * Rebin2D(Int_t nxgroup=2, Int_t nygroup=2, const char *newname="", const Double_t *xbins=nullptr, const Double_t *ybins=nullptr)
Rebin this histogram grouping nxgroup/nygroup bins along the xaxis/yaxis together.
Definition TH2.cxx:1697
Double_t fTsumwxy
Total Sum of weight*X*Y.
Definition TH2.h:36
void SetBinContent(Int_t bin, Double_t content) override
Set bin content.
Definition TH2.cxx:2584
Int_t BufferEmpty(Int_t action=0) override
Fill histogram with all entries in the buffer.
Definition TH2.cxx:245
virtual void DoFitSlices(bool onX, TF1 *f1, Int_t firstbin, Int_t lastbin, Int_t cut, Option_t *option, TObjArray *arr)
Definition TH2.cxx:786
TH1D * QuantilesX(Double_t prob=0.5, const char *name="_qx") const
Compute the X distribution of quantiles in the other variable Y name is the name of the returned hist...
Definition TH2.cxx:2473
virtual void SetShowProjectionY(Int_t nbins=1)
When the mouse is moved in a pad containing a 2-d view of this histogram a second canvas shows the pr...
Definition TH2.cxx:2618
TClass * IsA() const override
Definition TH2.h:137
void Reset(Option_t *option="") override
Reset this histogram: contents, errors, etc.
Definition TH2.cxx:2568
Double_t fScalefactor
Scale factor.
Definition TH2.h:33
virtual TH1 * ShowBackground2D(Int_t nIterX=20, Int_t nIterY=20, Option_t *option="same")
This function calculates the background spectrum in this histogram.
Definition TH2.cxx:2646
virtual TH1D * DoProjection(bool onX, const char *name, Int_t firstbin, Int_t lastbin, Option_t *option) const
Internal (protected) method for performing projection on the X or Y axis called by ProjectionX or Pro...
Definition TH2.cxx:2128
TH2 * RebinX(Int_t ngroup=2, const char *newname="") override
Rebin only the X axis see Rebin2D.
Definition TH2.cxx:1618
Double_t fTsumwy2
Total Sum of weight*Y*Y.
Definition TH2.h:35
virtual Double_t GetCovariance(Int_t axis1=1, Int_t axis2=2) const
Return covariance between axis1 and axis2.
Definition TH2.cxx:1137
Int_t GetBin(Int_t binx, Int_t biny, Int_t binz=0) const override
Return Global bin number corresponding to binx,y,z.
Definition TH2.cxx:1060
TH1D * ProjectionX(const char *name="_px", Int_t firstybin=0, Int_t lastybin=-1, Option_t *option="") const
Project a 2-D histogram into a 1-D histogram along X (integration along Y).
Definition TH2.cxx:2401
void Smooth(Int_t ntimes=1, Option_t *option="") override
Smooth bin contents of this 2-d histogram using kernel algorithms similar to the ones used in the ras...
Definition TH2.cxx:2695
~TH2() override
Destructor.
Definition TH2.cxx:233
Double_t GetBinContent(Int_t binx, Int_t biny) const override
Definition TH2.h:97
virtual Double_t IntegralAndError(Int_t binx1, Int_t binx2, Int_t biny1, Int_t biny2, Double_t &err, Option_t *option="") const
Return integral of bin contents in range [firstxbin,lastxbin],[firstybin,lastybin] for a 2-D histogra...
Definition TH2.cxx:1321
Double_t fTsumwy
Total Sum of weight*Y.
Definition TH2.h:34
TH2()
2-D histogram default constructor.
Definition TH2.cxx:76
Double_t Interpolate(Double_t x) const override
illegal for a TH2
Definition TH2.cxx:1329
virtual void SetShowProjectionX(Int_t nbins=1)
When the mouse is moved in a pad containing a 2-d view of this histogram a second canvas shows the pr...
Definition TH2.cxx:2602
void Streamer(TBuffer &) override
Stream an object of class TH2.
Definition TH2.cxx:2798
Int_t Fill(Double_t) override
Invalid Fill method.
Definition TH2.cxx:368
static TClass * Class()
virtual void FitSlicesX(TF1 *f1=nullptr, Int_t firstybin=0, Int_t lastybin=-1, Int_t cut=0, Option_t *option="QNR", TObjArray *arr=nullptr)
Project slices along X in case of a 2-D histogram, then fit each slice with function f1 and make a hi...
Definition TH2.cxx:990
virtual Int_t BufferFill(Double_t x, Double_t y, Double_t w)
accumulate arguments in buffer.
Definition TH2.cxx:327
virtual void SetShowProjectionXY(Int_t nbinsY=1, Int_t nbinsX=1)
When the mouse is moved in a pad containing a 2-d view of this histogram two canvases show the projec...
Definition TH2.cxx:2635
Double_t Integral(Option_t *option="") const override
Return integral of bin contents.
Definition TH2.cxx:1293
void PutStats(Double_t *stats) override
Replace current statistics with the values in array stats.
Definition TH2.cxx:2453
virtual TH2 * RebinY(Int_t ngroup=2, const char *newname="")
Rebin only the Y axis see Rebin2D.
Definition TH2.cxx:1628
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
TMatrixTBase.
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
const char * GetTitle() const override
Returns title of object.
Definition TNamed.h:50
An array of TObjects.
Definition TObjArray.h:31
Collectable string class.
Definition TObjString.h:28
Mother of all ROOT objects.
Definition TObject.h:42
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition TObject.h:204
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition TObject.cxx:1082
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition TObject.cxx:886
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:1096
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition TObject.cxx:1070
Profile Histogram.
Definition TProfile.h:32
static TClass * Class()
This is the base class for the ROOT Random number generators.
Definition TRandom.h:28
Double_t Rndm() override
Machine independent random number generator.
Definition TRandom.cxx:558
Basic string class.
Definition TString.h:137
void ToLower()
Change string to lower-case.
Definition TString.cxx:1190
Ssiz_t First(char c) const
Find first occurrence of a character c.
Definition TString.cxx:546
const char * Data() const
Definition TString.h:385
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition TString.h:714
void ToUpper()
Change string to upper case.
Definition TString.cxx:1203
TString & Remove(Ssiz_t pos)
Definition TString.h:695
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:2460
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:642
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition TString.h:661
virtual void SetShowProjection(const char *option, Int_t nbins)=0
virtual Int_t MakeCuts(char *cutsopt)=0
virtual Bool_t IsInside(Int_t x, Int_t y)=0
virtual void SetShowProjectionXY(const char *option, Int_t nbinsY, Int_t nbinsX)=0
small helper class to store/restore gPad context in TPad methods
Definition TVirtualPad.h:61
const Double_t sigma
Double_t y[n]
Definition legend1.C:17
return c1
Definition legend1.C:41
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
TH1F * h1
Definition legend1.C:5
TF1 * f1
Definition legend1.C:11
void WarnAboutUnusedFlowContent(const TAxis &oldAxis, const RebinnedAxisInfo &info, const Double_t *userBins, char axisName, const Double_t *bins, Int_t stride, Int_t nOther, Int_t otherStride, TH1 &hist, const char *where)
Warn when the range of the new axis extends beyond the old one while the corresponding flow bins hold...
void SetRebinnedBins2D(TH1 &hnew, const TAxis &newXaxis, const TAxis &newYaxis)
Apply the axes of the rebinned histogram, using explicit bin edges if any of the two axes has non-uni...
void MergeRebinnedCells(Int_t nOldX, Int_t nOldY, Int_t nNewX, const std::vector< Int_t > &mapX, const std::vector< Int_t > &mapY, std::initializer_list< std::pair< const Double_t *, Double_t * > > arrays)
Accumulate every old cell (including under- and overflow) into the new cell given by the per-axis bin...
bool SetupRebinnedAxis(const TAxis &oldAxis, Int_t ngroup, const Double_t *userBins, char axisName, TH1 &hist, const char *where, RebinnedAxisInfo &info)
Validate the rebinning parameters for one axis and fill the definition of the rebinned axis and the m...
Double_t Gaus(Double_t x, Double_t mean=0, Double_t sigma=1, Bool_t norm=kFALSE)
Calculates a gaussian function with mean and sigma.
Definition TMath.cxx:471
Short_t Max(Short_t a, Short_t b)
Returns the largest of a and b.
Definition TMathBase.h:249
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 QuietNaN()
Returns a quiet NaN as defined by IEEE 754.
Definition TMath.h:915
Double_t Floor(Double_t x)
Rounds x downward, returning the largest integral value that is not greater than x.
Definition TMath.h:693
Double_t Log(Double_t x)
Returns the natural logarithm of x.
Definition TMath.h:769
Double_t Sqrt(Double_t x)
Returns the square root of x.
Definition TMath.h:675
Double_t KolmogorovProb(Double_t z)
Calculates the Kolmogorov distribution function,.
Definition TMath.cxx:679
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:329
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
Definition TMathBase.h:122
The definition of one axis of the rebinned histogram.
TMarker m
Definition textangle.C:8