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