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
1855 const TAxis& outAxis = ( onX ? fXaxis : fYaxis );
1856 const TAxis& inAxis = ( onX ? fYaxis : fXaxis );
1857 Int_t inN = inAxis.GetNbins();
1858 const char *expectedName = ( onX ? "_pfx" : "_pfy" );
1859
1860 // outer axis cannot be outside original axis (this fixes ROOT-8781)
1861 // and firstOutBin and lastOutBin cannot be both equal to zero
1862 Int_t firstOutBin = std::max(outAxis.GetFirst(),1);
1863 Int_t lastOutBin = std::min(outAxis.GetLast(),outAxis.GetNbins() ) ;
1864
1865 if ( lastbin < firstbin && inAxis.TestBit(TAxis::kAxisRange) ) {
1866 firstbin = inAxis.GetFirst();
1867 lastbin = inAxis.GetLast();
1868 // For special case of TAxis::SetRange, when first == 1 and last
1869 // = N and the range bit has been set, the TAxis will return 0
1870 // for both.
1871 if (firstbin == 0 && lastbin == 0)
1872 {
1873 firstbin = 1;
1874 lastbin = inAxis.GetNbins();
1875 }
1876 }
1877 if (firstbin < 0) firstbin = 1;
1878 if (lastbin < 0) lastbin = inN;
1879 if (lastbin > inN+1) lastbin = inN;
1880
1881 // Create the profile histogram
1882 char *pname = (char*)name;
1883 if (name && strcmp(name, expectedName) == 0) {
1884 Int_t nch = strlen(GetName()) + 5;
1885 pname = new char[nch];
1886 snprintf(pname,nch,"%s%s",GetName(),name);
1887 }
1888 TProfile *h1=nullptr;
1889 //check if a profile with identical name exist
1890 // if compatible reset and re-use previous histogram
1891 TObject *h1obj = gROOT->FindObject(pname);
1892 if (h1obj && h1obj->InheritsFrom(TH1::Class())) {
1893 if (h1obj->IsA() != TProfile::Class() ) {
1894 Error("DoProfile","Histogram with name %s must be a TProfile and is a %s",name,h1obj->ClassName());
1895 return nullptr;
1896 }
1897 h1 = (TProfile*)h1obj;
1898 // reset the existing histogram and set always the new binning for the axis
1899 // This avoid problems when the histogram already exists and the histograms is rebinned or its range has changed
1900 // (see https://savannah.cern.ch/bugs/?94101 or https://savannah.cern.ch/bugs/?95808 )
1901 h1->Reset();
1902 const TArrayD *xbins = outAxis.GetXbins();
1903 if (xbins->fN == 0) {
1904 if ( originalRange )
1905 h1->SetBins(outAxis.GetNbins(),outAxis.GetXmin(),outAxis.GetXmax());
1906 else
1907 h1->SetBins(lastOutBin-firstOutBin+1,outAxis.GetBinLowEdge(firstOutBin),outAxis.GetBinUpEdge(lastOutBin));
1908 } else {
1909 // case variable bins
1910 if (originalRange )
1911 h1->SetBins(outAxis.GetNbins(),xbins->fArray);
1912 else
1913 h1->SetBins(lastOutBin-firstOutBin+1,&xbins->fArray[firstOutBin-1]);
1914 }
1915 }
1916
1917 Int_t ncuts = 0;
1918 if (opt.Contains("[")) {
1919 ((TH2 *)this)->GetPainter();
1920 if (fPainter) ncuts = fPainter->MakeCuts((char*)cut.Data());
1921 }
1922
1923 if (!h1) {
1924 const TArrayD *bins = outAxis.GetXbins();
1925 if (bins->fN == 0) {
1926 if ( originalRange )
1927 h1 = new TProfile(pname,GetTitle(),outAxis.GetNbins(),outAxis.GetXmin(),outAxis.GetXmax(),opt);
1928 else
1930 outAxis.GetBinLowEdge(firstOutBin),
1931 outAxis.GetBinUpEdge(lastOutBin), opt);
1932 } else {
1933 // case variable bins
1934 if (originalRange )
1935 h1 = new TProfile(pname,GetTitle(),outAxis.GetNbins(),bins->fArray,opt);
1936 else
1938 }
1939 }
1940 if (pname != name) delete [] pname;
1941
1942 // Copy attributes
1944 THashList* labels=outAxis.GetLabels();
1945 if (labels) {
1946 TIter iL(labels);
1947 TObjString* lb;
1948 Int_t i = 1;
1949 while ((lb=(TObjString*)iL())) {
1950 h1->GetXaxis()->SetBinLabel(i,lb->String().Data());
1951 i++;
1952 }
1953 }
1954
1955 h1->SetLineColor(this->GetLineColor());
1956 h1->SetFillColor(this->GetFillColor());
1957 h1->SetMarkerColor(this->GetMarkerColor());
1958 h1->SetMarkerStyle(this->GetMarkerStyle());
1959
1960 // check if histogram is weighted
1961 // in case need to store sum of weight square/bin for the profile
1962 TArrayD & binSumw2 = *(h1->GetBinSumw2());
1963 bool useWeights = (GetSumw2N() > 0);
1964 if (useWeights && (binSumw2.fN != h1->GetNcells()) ) h1->Sumw2();
1965 // we need to set this bit because we fill the profile using a single Fill for many entries
1966 // This is needed for the changes applied to make automatically the histogram weighted in ROOT 6 versions
1967 else h1->SetBit(TH1::kIsNotW);
1968
1969 // Fill the profile histogram
1970 // no entries/bin is available so can fill only using bin content as weight
1971
1972 // implement filling of projected histogram
1973 // outbin is bin number of outAxis (the projected axis). Loop is done on all bin of TH2 histograms
1974 // inbin is the axis being integrated. Loop is done only on the selected bins
1975 for ( Int_t outbin = 0; outbin <= outAxis.GetNbins() + 1; ++outbin) {
1977
1978 // find corresponding bin number in h1 for outbin (binOut)
1979 Double_t xOut = outAxis.GetBinCenter(outbin);
1981 if (binOut <0) continue;
1982
1983 for (Int_t inbin = firstbin ; inbin <= lastbin ; ++inbin) {
1984 Int_t binx, biny;
1985 if (onX) { binx = outbin; biny=inbin; }
1986 else { binx = inbin; biny=outbin; }
1987
1988 if (ncuts) {
1989 if (!fPainter->IsInside(binx,biny)) continue;
1990 }
1991 Int_t bin = GetBin(binx, biny);
1993
1994
1995 if (cxy) {
1996 Double_t tmp = 0;
1997 // the following fill update wrongly the fBinSumw2- need to save it before
1998 if ( useWeights ) tmp = binSumw2.fArray[binOut];
1999 h1->Fill( xOut, inAxis.GetBinCenter(inbin), cxy );
2000 if ( useWeights ) binSumw2.fArray[binOut] = tmp + fSumw2.fArray[bin];
2001 }
2002
2003 }
2004 }
2005
2006 // the statistics must be recalculated since by using the Fill method the total sum of weight^2 is
2007 // not computed correctly
2008 // for a profile does not much sense to re-use statistics of original TH2
2009 h1->ResetStats();
2010 // Also we need to set the entries since they have not been correctly calculated during the projection
2011 // we can only set them to the effective entries
2013
2014
2015 if (opt.Contains("d")) {
2016 TVirtualPad::TContext ctxt(gROOT->GetSelectedPad(), true, true);
2017 opt.Remove(opt.First("d"),1);
2018 if (!gPad || !gPad->FindObject(h1)) {
2019 h1->Draw(opt);
2020 } else {
2021 h1->Paint(opt);
2022 }
2023 }
2024 return h1;
2025}
2026
2027
2028////////////////////////////////////////////////////////////////////////////////
2029/// Project a 2-D histogram into a profile histogram along X.
2030///
2031/// The projection is made from the channels along the Y axis
2032/// ranging from firstybin to lastybin included.
2033/// By default, bins 1 to ny are included
2034/// When all bins are included, the number of entries in the projection
2035/// is set to the number of entries of the 2-D histogram, otherwise
2036/// the number of entries is incremented by 1 for all non empty cells.
2037///
2038/// if option "d" is specified, the profile is drawn in the current pad.
2039///
2040/// if option "o" original axis range of the target axes will be
2041/// kept, but only bins inside the selected range will be filled.
2042///
2043/// The option can also be used to specify the projected profile error type.
2044/// Values which can be used are 's', 'i', or 'g'. See TProfile::BuildOptions for details
2045///
2046/// Using a TCutG object, it is possible to select a sub-range of a 2-D histogram.
2047/// One must create a graphical cut (mouse or C++) and specify the name
2048/// of the cut between [] in the option.
2049/// For example, with a TCutG named "cutg", one can call:
2050/// myhist->ProfileX(" ",firstybin,lastybin,"[cutg]");
2051/// To invert the cut, it is enough to put a "-" in front of its name:
2052/// myhist->ProfileX(" ",firstybin,lastybin,"[-cutg]");
2053/// It is possible to apply several cuts ("," means logical AND):
2054/// myhist->ProfileX(" ",firstybin,lastybin,"[cutg1,cutg2]");
2055///
2056/// NOTE that if a TProfile named "name" exists in the current directory or pad with
2057/// a compatible axis the profile is reset and filled again with the projected contents of the TH2.
2058/// In the case of axis incompatibility an error is reported and a NULL pointer is returned.
2059///
2060/// NOTE that the X axis attributes of the TH2 are copied to the X axis of the profile.
2061///
2062/// NOTE that the default under- / overflow behavior differs from what ProjectionX
2063/// does! Profiles take the bin center into account, so here the under- and overflow
2064/// bins are ignored by default.
2065///
2066/// NOTE that the return profile histogram is computed using the Y bin center values instead of
2067/// the real Y values which are used to fill the 2d histogram. Therefore the obtained profile is just an approximation of the
2068/// correct profile histogram that would be obtained when filling it directly with the original data (see ROOT-7770)
2069
2070
2072{
2073 return DoProfile(true, name, firstybin, lastybin, option);
2074
2075}
2076
2077
2078////////////////////////////////////////////////////////////////////////////////
2079/// Project a 2-D histogram into a profile histogram along Y.
2080///
2081/// The projection is made from the channels along the X axis
2082/// ranging from firstxbin to lastxbin included.
2083/// By default, bins 1 to nx are included
2084/// When all bins are included, the number of entries in the projection
2085/// is set to the number of entries of the 2-D histogram, otherwise
2086/// the number of entries is incremented by 1 for all non empty cells.
2087///
2088/// if option "d" is specified, the profile is drawn in the current pad.
2089///
2090/// if option "o" , the original axis range of the target axis will be
2091/// kept, but only bins inside the selected range will be filled.
2092///
2093/// The option can also be used to specify the projected profile error type.
2094/// Values which can be used are 's', 'i', or 'g'. See TProfile::BuildOptions for details
2095/// Using a TCutG object, it is possible to select a sub-range of a 2-D histogram.
2096///
2097/// One must create a graphical cut (mouse or C++) and specify the name
2098/// of the cut between [] in the option.
2099/// For example, with a TCutG named "cutg", one can call:
2100/// myhist->ProfileY(" ",firstybin,lastybin,"[cutg]");
2101/// To invert the cut, it is enough to put a "-" in front of its name:
2102/// myhist->ProfileY(" ",firstybin,lastybin,"[-cutg]");
2103/// It is possible to apply several cuts:
2104/// myhist->ProfileY(" ",firstybin,lastybin,"[cutg1,cutg2]");
2105///
2106/// NOTE that if a TProfile named "name" exists in the current directory or pad with
2107/// a compatible axis the profile is reset and filled again with the projected contents of the TH2.
2108/// In the case of axis incompatibility an error is reported and a NULL pointer is returned.
2109///
2110/// NOTE that the Y axis attributes of the TH2 are copied to the X axis of the profile.
2111///
2112/// NOTE that the default under- / overflow behavior differs from what ProjectionX
2113/// does! Profiles take the bin center into account, so here the under- and overflow
2114/// bins are ignored by default.
2115///
2116/// NOTE that the return profile histogram is computed using the X bin center values instead of
2117/// the real X values which are used to fill the 2d histogram. Therefore the obtained profile is just an approximation of the
2118/// correct profile histogram that would be obtained when filling it directly with the original data (see ROOT-7770)
2119
2120
2122{
2123 return DoProfile(false, name, firstxbin, lastxbin, option);
2124}
2125
2126
2127////////////////////////////////////////////////////////////////////////////////
2128/// Internal (protected) method for performing projection on the X or Y axis
2129/// called by ProjectionX or ProjectionY
2130
2132{
2133 const char *expectedName = nullptr;
2134 Int_t inNbin;
2135 const TAxis* outAxis;
2136 const TAxis* inAxis;
2137
2138 TString opt = option;
2139 TString cut;
2140 Int_t i1 = opt.Index("[");
2141 if (i1>=0) {
2142 Int_t i2 = opt.Index("]");
2143 cut = opt(i1,i2-i1+1);
2144 }
2145 opt.ToLower(); //must be called after having parsed the cut name
2146 bool originalRange = opt.Contains("o");
2147
2148 if ( onX )
2149 {
2150 expectedName = "_px";
2152 outAxis = GetXaxis();
2153 inAxis = GetYaxis();
2154 }
2155 else
2156 {
2157 expectedName = "_py";
2159 outAxis = GetYaxis();
2160 inAxis = GetXaxis();
2161 }
2162
2163 // outer axis cannot be outside original axis (this fixes ROOT-8781)
2164 // and firstOutBin and lastOutBin cannot be both equal to zero
2165 Int_t firstOutBin = std::max(outAxis->GetFirst(),1);
2166 Int_t lastOutBin = std::min(outAxis->GetLast(),outAxis->GetNbins() ) ;
2167
2169 firstbin = inAxis->GetFirst();
2170 lastbin = inAxis->GetLast();
2171 // For special case of TAxis::SetRange, when first == 1 and last
2172 // = N and the range bit has been set, the TAxis will return 0
2173 // for both.
2174 if (firstbin == 0 && lastbin == 0)
2175 {
2176 firstbin = 1;
2177 lastbin = inAxis->GetNbins();
2178 }
2179 }
2180 if (firstbin < 0) firstbin = 0;
2181 if (lastbin < 0) lastbin = inNbin + 1;
2182 if (lastbin > inNbin+1) lastbin = inNbin + 1;
2183
2184 // Create the projection histogram
2185 char *pname = (char*)name;
2186 if (name && strcmp(name,expectedName) == 0) {
2187 Int_t nch = strlen(GetName()) + 4;
2188 pname = new char[nch];
2189 snprintf(pname,nch,"%s%s",GetName(),name);
2190 }
2191 TH1D *h1=nullptr;
2192 //check if histogram with identical name exist
2193 // if compatible reset and re-use previous histogram
2194 // (see https://savannah.cern.ch/bugs/?54340)
2195 TObject *h1obj = gROOT->FindObject(pname);
2196 if (h1obj && h1obj->InheritsFrom(TH1::Class())) {
2197 if (h1obj->IsA() != TH1D::Class() ) {
2198 Error("DoProjection","Histogram with name %s must be a TH1D and is a %s",name,h1obj->ClassName());
2199 return nullptr;
2200 }
2201 h1 = (TH1D*)h1obj;
2202 // reset the existing histogram and set always the new binning for the axis
2203 // This avoid problems when the histogram already exists and the histograms is rebinned or its range has changed
2204 // (see https://savannah.cern.ch/bugs/?94101 or https://savannah.cern.ch/bugs/?95808 )
2205 h1->Reset();
2206 const TArrayD *xbins = outAxis->GetXbins();
2207 if (xbins->fN == 0) {
2208 if ( originalRange )
2209 h1->SetBins(outAxis->GetNbins(),outAxis->GetXmin(),outAxis->GetXmax());
2210 else
2211 h1->SetBins(lastOutBin-firstOutBin+1,outAxis->GetBinLowEdge(firstOutBin),outAxis->GetBinUpEdge(lastOutBin));
2212 } else {
2213 // case variable bins
2214 if (originalRange )
2215 h1->SetBins(outAxis->GetNbins(),xbins->fArray);
2216 else
2217 h1->SetBins(lastOutBin-firstOutBin+1,&xbins->fArray[firstOutBin-1]);
2218 }
2219 }
2220
2221 Int_t ncuts = 0;
2222 if (opt.Contains("[")) {
2223 ((TH2 *)this)->GetPainter();
2224 if (fPainter) ncuts = fPainter->MakeCuts((char*)cut.Data());
2225 }
2226
2227 if (!h1) {
2228 const TArrayD *bins = outAxis->GetXbins();
2229 if (bins->fN == 0) {
2230 if ( originalRange )
2231 h1 = new TH1D(pname,GetTitle(),outAxis->GetNbins(),outAxis->GetXmin(),outAxis->GetXmax());
2232 else
2234 outAxis->GetBinLowEdge(firstOutBin),outAxis->GetBinUpEdge(lastOutBin));
2235 } else {
2236 // case variable bins
2237 if (originalRange )
2238 h1 = new TH1D(pname,GetTitle(),outAxis->GetNbins(),bins->fArray);
2239 else
2241 }
2242 if (opt.Contains("e") || GetSumw2N() ) h1->Sumw2();
2243 }
2244 if (pname != name) delete [] pname;
2245
2246 // Copy the axis attributes and the axis labels if needed.
2248 THashList* labels=outAxis->GetLabels();
2249 if (labels) {
2250 TIter iL(labels);
2251 TObjString* lb;
2252 Int_t i = 1;
2253 while ((lb=(TObjString*)iL())) {
2254 h1->GetXaxis()->SetBinLabel(i,lb->String().Data());
2255 i++;
2256 }
2257 }
2258
2259 h1->SetLineColor(this->GetLineColor());
2260 h1->SetFillColor(this->GetFillColor());
2261 h1->SetMarkerColor(this->GetMarkerColor());
2262 h1->SetMarkerStyle(this->GetMarkerStyle());
2263
2264 // Fill the projected histogram
2266 Double_t totcont = 0;
2268
2269 // implement filling of projected histogram
2270 // outbin is bin number of outAxis (the projected axis). Loop is done on all bin of TH2 histograms
2271 // inbin is the axis being integrated. Loop is done only on the selected bins
2272 // if the out axis has labels and is extendable, temporary make it non-extendable to avoid adding extra bins
2273 Bool_t extendable = outAxis->CanExtend();
2274 if ( labels && extendable ) h1->GetXaxis()->SetCanExtend(kFALSE);
2275 for ( Int_t outbin = 0; outbin <= outAxis->GetNbins() + 1; ++outbin) {
2276 err2 = 0;
2277 cont = 0;
2279
2280 for (Int_t inbin = firstbin ; inbin <= lastbin ; ++inbin) {
2281 Int_t binx, biny;
2282 if (onX) { binx = outbin; biny=inbin; }
2283 else { binx = inbin; biny=outbin; }
2284
2285 if (ncuts) {
2286 if (!fPainter->IsInside(binx,biny)) continue;
2287 }
2288 // sum bin content and error if needed
2290 if (computeErrors) {
2292 err2 += exy*exy;
2293 }
2294 }
2295 // find corresponding bin number in h1 for outbin
2296 Int_t binOut = h1->GetXaxis()->FindBin( outAxis->GetBinCenter(outbin) );
2299 // sum all content
2300 totcont += cont;
2301 }
2302 if ( labels ) h1->GetXaxis()->SetCanExtend(extendable);
2303
2304 // check if we can re-use the original statistics from the previous histogram
2305 bool reuseStats = false;
2306 if ( ( GetStatOverflowsBehaviour() == false && firstbin == 1 && lastbin == inNbin ) ||
2307 ( GetStatOverflowsBehaviour() == true && firstbin == 0 && lastbin == inNbin + 1 ) )
2308 reuseStats = true;
2309 else {
2310 // also if total content match we can re-use
2311 double eps = 1.E-12;
2312 if (IsA() == TH2F::Class() ) eps = 1.E-6;
2313 if (fTsumw != 0 && TMath::Abs( fTsumw - totcont) < TMath::Abs(fTsumw) * eps)
2314 reuseStats = true;
2315 }
2316 if (ncuts) reuseStats = false;
2317 // retrieve the statistics and set in projected histogram if we can re-use it
2318 bool reuseEntries = reuseStats;
2319 // can re-use entries if underflow/overflow are included
2320 reuseEntries &= (firstbin==0 && lastbin == inNbin+1);
2321 if (reuseStats) {
2322 Double_t stats[kNstat];
2323 GetStats(stats);
2324 if (!onX) { // case of projection on Y
2325 stats[2] = stats[4];
2326 stats[3] = stats[5];
2327 }
2328 h1->PutStats(stats);
2329 }
2330 else {
2331 // the statistics is automatically recalculated since it is reset by the call to SetBinContent
2332 // we just need to set the entries since they have not been correctly calculated during the projection
2333 // we can only set them to the effective entries
2335 }
2336 if (reuseEntries) {
2338 }
2339 else {
2340 // re-compute the entries
2341 // in case of error calculation (i.e. when Sumw2() is set)
2342 // use the effective entries for the entries
2343 // since this is the only way to estimate them
2344 Double_t entries = TMath::Floor( totcont + 0.5); // to avoid numerical rounding
2345 if (h1->GetSumw2N()) entries = h1->GetEffectiveEntries();
2346 h1->SetEntries( entries );
2347 }
2348
2349 if (opt.Contains("d")) {
2350 TVirtualPad::TContext ctxt(gROOT->GetSelectedPad(), true, true);
2351 opt.Remove(opt.First("d"),1);
2352 // remove also other options
2353 if (opt.Contains("e")) opt.Remove(opt.First("e"),1);
2354 if (!gPad || !gPad->FindObject(h1)) {
2355 h1->Draw(opt);
2356 } else {
2357 h1->Paint(opt);
2358 }
2359 }
2360
2361 return h1;
2362}
2363
2364
2365////////////////////////////////////////////////////////////////////////////////
2366/// Project a 2-D histogram into a 1-D histogram along X.
2367///
2368/// The projection is always of the type TH1D.
2369/// The projection is made from the channels along the Y axis
2370/// ranging from firstybin to lastybin included.
2371/// By default, all bins including under- and overflow are included.
2372/// The number of entries in the projection is estimated from the
2373/// number of effective entries for all the cells included in the projection.
2374///
2375/// To exclude the underflow bins in Y, use firstybin=1.
2376/// To exclude the overflow bins in Y, use lastybin=nx.
2377///
2378/// if option "e" is specified, the errors are computed.
2379/// if option "d" is specified, the projection is drawn in the current pad.
2380/// if option "o" original axis range of the target axes will be
2381/// kept, but only bins inside the selected range will be filled.
2382///
2383/// Using a TCutG object, it is possible to select a sub-range of a 2-D histogram.
2384/// One must create a graphical cut (mouse or C++) and specify the name
2385/// of the cut between [] in the option.
2386/// For example, with a TCutG named "cutg", one can call:
2387/// myhist->ProjectionX(" ",firstybin,lastybin,"[cutg]");
2388/// To invert the cut, it is enough to put a "-" in front of its name:
2389/// myhist->ProjectionX(" ",firstybin,lastybin,"[-cutg]");
2390/// It is possible to apply several cuts:
2391/// myhist->ProjectionX(" ",firstybin,lastybin,"[cutg1,cutg2]");
2392///
2393/// NOTE that if a TH1D named "name" exists in the current directory or pad
2394/// the histogram is reset and filled again with the projected contents of the TH2.
2395///
2396/// NOTE that the X axis attributes of the TH2 are copied to the X axis of the projection.
2397
2399{
2400 return DoProjection(true, name, firstybin, lastybin, option);
2401}
2402
2403
2404////////////////////////////////////////////////////////////////////////////////
2405/// Project a 2-D histogram into a 1-D histogram along Y.
2406///
2407/// The projection is always of the type TH1D.
2408/// The projection is made from the channels along the X axis
2409/// ranging from firstxbin to lastxbin included.
2410/// By default, all bins including under- and overflow are included.
2411/// The number of entries in the projection is estimated from the
2412/// number of effective entries for all the cells included in the projection
2413///
2414/// To exclude the underflow bins in X, use firstxbin=1.
2415/// To exclude the overflow bins in X, use lastxbin=nx.
2416///
2417/// if option "e" is specified, the errors are computed.
2418/// if option "d" is specified, the projection is drawn in the current pad.
2419/// if option "o" original axis range of the target axes will be
2420/// kept, but only bins inside the selected range will be filled.
2421///
2422/// Using a TCutG object, it is possible to select a sub-range of a 2-D histogram.
2423/// One must create a graphical cut (mouse or C++) and specify the name
2424/// of the cut between [] in the option.
2425/// For example, with a TCutG named "cutg", one can call:
2426/// myhist->ProjectionY(" ",firstxbin,lastxbin,"[cutg]");
2427/// To invert the cut, it is enough to put a "-" in front of its name:
2428/// myhist->ProjectionY(" ",firstxbin,lastxbin,"[-cutg]");
2429/// It is possible to apply several cuts:
2430/// myhist->ProjectionY(" ",firstxbin,lastxbin,"[cutg1,cutg2]");
2431///
2432/// NOTE that if a TH1D named "name" exists in the current directory or pad and having
2433/// a compatible axis, the histogram is reset and filled again with the projected contents of the TH2.
2434/// In the case of axis incompatibility, an error is reported and a NULL pointer is returned.
2435///
2436/// NOTE that the Y axis attributes of the TH2 are copied to the X axis of the projection.
2437
2439{
2440 return DoProjection(false, name, firstxbin, lastxbin, option);
2441}
2442
2443
2444////////////////////////////////////////////////////////////////////////////////
2445/// Replace current statistics with the values in array stats
2446
2448{
2449 TH1::PutStats(stats);
2450 fTsumwy = stats[4];
2451 fTsumwy2 = stats[5];
2452 fTsumwxy = stats[6];
2453}
2454
2455
2456////////////////////////////////////////////////////////////////////////////////
2457/// Compute the X distribution of quantiles in the other variable Y
2458/// name is the name of the returned histogram
2459/// prob is the probability content for the quantile (0.5 is the default for the median)
2460/// An approximate error for the quantile is computed assuming that the distribution in
2461/// the other variable is normal. According to this approximate formula the error on the quantile is
2462/// estimated as sqrt( p (1-p) / ( n * f(q)^2) ), where p is the probability content of the quantile and
2463/// n is the number of events used to compute the quantile and f(q) is the probability distribution for the
2464/// other variable evaluated at the obtained quantile. In the error estimation the probability is then assumed to be
2465/// a normal distribution.
2466
2467TH1D* TH2::QuantilesX( Double_t prob, const char * name) const
2468{
2469 return DoQuantiles(true, name, prob);
2470}
2471
2472
2473////////////////////////////////////////////////////////////////////////////////
2474/// Compute the Y distribution of quantiles in the other variable X
2475/// name is the name of the returned histogram
2476/// prob is the probability content for the quantile (0.5 is the default for the median)
2477/// An approximate error for the quantile is computed assuming that the distribution in
2478/// the other variable is normal.
2479
2480TH1D* TH2::QuantilesY( Double_t prob, const char * name) const
2481{
2482 return DoQuantiles(false, name, prob);
2483}
2484
2485
2486////////////////////////////////////////////////////////////////////////////////
2487/// Implementation of quantiles for x or y
2488
2489TH1D* TH2::DoQuantiles(bool onX, const char * name, Double_t prob) const
2490{
2491 const TAxis *outAxis = nullptr;
2492 if ( onX ) {
2493 outAxis = GetXaxis();
2494 } else {
2495 outAxis = GetYaxis();
2496 }
2497
2498 // build first name of returned histogram
2499 TString qname = name;
2500 if (qname.IsNull() || qname == "_qx" || qname == "_qy") {
2501 const char * qtype = (onX) ? "qx" : "qy";
2502 qname = TString::Format("%s_%s_%3.2f",GetName(),qtype, prob);
2503 }
2504 // check if the histogram is already existing
2505 TH1D *h1=nullptr;
2506 //check if histogram with identical name exist
2507 TObject *h1obj = gROOT->FindObject(qname);
2508 if (h1obj) {
2509 h1 = dynamic_cast<TH1D*>(h1obj);
2510 if (!h1) {
2511 Error("DoQuantiles","Histogram with name %s must be a TH1D and is a %s",qname.Data(),h1obj->ClassName());
2512 return nullptr;
2513 }
2514 }
2515 if (h1) {
2516 h1->Reset();
2517 } else {
2518 // create the histogram
2519 h1 = new TH1D(qname, GetTitle(), 1, 0, 1);
2520 }
2521 // set the bin content
2522 Int_t firstOutBin = std::max(outAxis->GetFirst(),1);
2523 Int_t lastOutBin = std::max(outAxis->GetLast(),outAxis->GetNbins());
2524 const TArrayD *xbins = outAxis->GetXbins();
2525 if (xbins->fN == 0)
2526 h1->SetBins(lastOutBin-firstOutBin+1,outAxis->GetBinLowEdge(firstOutBin),outAxis->GetBinUpEdge(lastOutBin));
2527 else
2528 h1->SetBins(lastOutBin-firstOutBin+1,&xbins->fArray[firstOutBin-1]);
2529
2530 // set the bin content of the histogram
2531 Double_t pp[1];
2532 pp[0] = prob;
2533
2534 TH1D * slice = nullptr;
2535 for (int ibin = outAxis->GetFirst() ; ibin <= outAxis->GetLast() ; ++ibin) {
2536 Double_t qq[1];
2537 // do a projection on the opposite axis
2538 slice = DoProjection(!onX, "tmp",ibin,ibin,"");
2539 if (!slice) break;
2540 if (slice->GetSum() == 0) continue;
2541 slice->GetQuantiles(1,qq,pp);
2542 h1->SetBinContent(ibin,qq[0]);
2543 // compute error using normal approximation
2544 // quantile error ~ sqrt (q*(1-q)/ *( n * f(xq)^2 ) from Kendall
2545 // where f(xq) is the p.d.f value at the quantile xq
2546 Double_t n = slice->GetEffectiveEntries();
2547 Double_t f = TMath::Gaus(qq[0], slice->GetMean(), slice->GetStdDev(), kTRUE);
2548 Double_t error = 0;
2549 // set the errors to zero in case of small statistics
2550 if (f > 0 && n > 1)
2551 error = TMath::Sqrt( prob*(1.-prob)/ (n * f * f) );
2552 h1->SetBinError(ibin, error);
2553 }
2554 if (slice) delete slice;
2555 return h1;
2556}
2557
2558
2559////////////////////////////////////////////////////////////////////////////////
2560/// Reset this histogram: contents, errors, etc.
2561
2563{
2565 TString opt = option;
2566 opt.ToUpper();
2567
2568 if (opt.Contains("ICE") && !opt.Contains("S")) return;
2569 fTsumwy = 0;
2570 fTsumwy2 = 0;
2571 fTsumwxy = 0;
2572}
2573
2574
2575////////////////////////////////////////////////////////////////////////////////
2576/// Set bin content
2577
2579{
2580 fEntries++;
2581 fTsumw = 0;
2582 if (bin < 0) return;
2583 if (bin >= fNcells) return;
2585}
2586
2587
2588////////////////////////////////////////////////////////////////////////////////
2589/// When the mouse is moved in a pad containing a 2-d view of this histogram
2590/// a second canvas shows the projection along X corresponding to the
2591/// mouse position along Y.
2592/// To stop the generation of the projections, delete the canvas
2593/// containing the projection.
2594/// \param nbins number of bins in Y to sum across for the projection
2595
2597{
2598 GetPainter();
2599
2600 if (fPainter) fPainter->SetShowProjection("x",nbins);
2601}
2602
2603
2604////////////////////////////////////////////////////////////////////////////////
2605/// When the mouse is moved in a pad containing a 2-d view of this histogram
2606/// a second canvas shows the projection along Y corresponding to the
2607/// mouse position along X.
2608/// To stop the generation of the projections, delete the canvas
2609/// containing the projection.
2610/// \param nbins number of bins in X to sum across for the projection
2611
2613{
2614 GetPainter();
2615
2616 if (fPainter) fPainter->SetShowProjection("y",nbins);
2617}
2618
2619
2620////////////////////////////////////////////////////////////////////////////////
2621/// When the mouse is moved in a pad containing a 2-d view of this histogram
2622/// two canvases show the projection along X and Y corresponding to the
2623/// mouse position along Y and X, respectively.
2624/// To stop the generation of the projections, delete the canvas
2625/// containing the projection.
2626/// \param nbinsY number of bins in Y to sum across for the x projection
2627/// \param nbinsX number of bins in X to sum across for the y projection
2628
2634
2635
2636////////////////////////////////////////////////////////////////////////////////
2637/// This function calculates the background spectrum in this histogram.
2638/// The background is returned as a histogram.
2639/// to be implemented (may be)
2640
2642{
2643
2644 return (TH1 *)gROOT->ProcessLineFast(TString::Format("TSpectrum2::StaticBackground((TH1*)0x%zx,%d,\"%s\")",
2645 (size_t)this, niter, option).Data());
2646}
2647
2648
2649////////////////////////////////////////////////////////////////////////////////
2650///Interface to TSpectrum2::Search
2651///the function finds peaks in this histogram where the width is > sigma
2652///and the peak maximum greater than threshold*maximum bin content of this.
2653///for more details see TSpectrum::Search.
2654///note the difference in the default value for option compared to TSpectrum2::Search
2655///option="" by default (instead of "goff")
2656
2658{
2659
2660 return (Int_t)gROOT->ProcessLineFast(TString::Format("TSpectrum2::StaticSearch((TH1*)0x%zx,%g,\"%s\",%g)",
2661 (size_t)this, sigma, option, threshold).Data());
2662}
2663
2664
2665////////////////////////////////////////////////////////////////////////////////
2666/// Smooth bin contents of this 2-d histogram using kernel algorithms
2667/// similar to the ones used in the raster graphics community.
2668/// Bin contents in the active range are replaced by their smooth values.
2669/// The algorithm retains the input dimension by using Kernel Crop at the input boundaries.
2670/// Kernel Crop sets any pixel in the kernel that extends past the input to zero and adjusts the
2671/// normalization accordingly.
2672/// If Errors are defined via Sumw2, they are also scaled and computed.
2673/// However, note the resulting errors will be correlated between different-bins, so
2674/// the errors should not be used blindly to perform any calculation involving several bins,
2675/// like fitting the histogram. One would need to compute also the bin by bin correlation matrix.
2676///
2677/// 3 kernels are proposed k5a, k5b and k3a.
2678/// k5a and k5b act on 5x5 cells (i-2,i-1,i,i+1,i+2, and same for j)
2679/// k5b is a bit more stronger in smoothing
2680/// k3a acts only on 3x3 cells (i-1,i,i+1, and same for j).
2681/// By default the kernel "k5a" is used. You can select the kernels "k5b" or "k3a"
2682/// via the option argument.
2683/// If TAxis::SetRange has been called on the x or/and y axis, only the bins
2684/// in the specified range are smoothed.
2685/// In the current implementation if the first argument is not used (default value=1).
2686///
2687/// implementation by David McKee (dmckee@bama.ua.edu). Extended by Rene Brun
2688
2690{
2691 Double_t k5a[5][5] = { { 0, 0, 1, 0, 0 },
2692 { 0, 2, 2, 2, 0 },
2693 { 1, 2, 5, 2, 1 },
2694 { 0, 2, 2, 2, 0 },
2695 { 0, 0, 1, 0, 0 } };
2696 Double_t k5b[5][5] = { { 0, 1, 2, 1, 0 },
2697 { 1, 2, 4, 2, 1 },
2698 { 2, 4, 8, 4, 2 },
2699 { 1, 2, 4, 2, 1 },
2700 { 0, 1, 2, 1, 0 } };
2701 Double_t k3a[3][3] = { { 0, 1, 0 },
2702 { 1, 2, 1 },
2703 { 0, 1, 0 } };
2704
2705 if (ntimes > 1) {
2706 Warning("Smooth","Currently only ntimes=1 is supported");
2707 }
2708 TString opt = option;
2709 opt.ToLower();
2710 Int_t ksize_x=5;
2711 Int_t ksize_y=5;
2712 Double_t *kernel = &k5a[0][0];
2713 if (opt.Contains("k5b")) kernel = &k5b[0][0];
2714 if (opt.Contains("k3a")) {
2715 kernel = &k3a[0][0];
2716 ksize_x=3;
2717 ksize_y=3;
2718 }
2719
2720 // find i,j ranges
2725
2726 // Determine the size of the bin buffer(s) needed
2728 Int_t nx = GetNbinsX();
2729 Int_t ny = GetNbinsY();
2730 Int_t bufSize = (nx+2)*(ny+2);
2731 Double_t *buf = new Double_t[bufSize];
2732 Double_t *ebuf = nullptr;
2733 if (fSumw2.fN) ebuf = new Double_t[bufSize];
2734
2735 // Copy all the data to the temporary buffers
2736 Int_t i,j,bin;
2737 for (i=ifirst; i<=ilast; i++){
2738 for (j=jfirst; j<=jlast; j++){
2739 bin = GetBin(i,j);
2740 buf[bin] = RetrieveBinContent(bin);
2741 if (ebuf) ebuf[bin]=GetBinError(bin);
2742 }
2743 }
2744
2745 // Kernel tail sizes (kernel sizes must be odd for this to work!)
2746 Int_t x_push = (ksize_x-1)/2;
2747 Int_t y_push = (ksize_y-1)/2;
2748
2749 // main work loop
2750 for (i=ifirst; i<=ilast; i++){
2751 for (j=jfirst; j<=jlast; j++) {
2752 Double_t content = 0.0;
2753 Double_t error = 0.0;
2754 Double_t norm = 0.0;
2755
2756 for (Int_t n=0; n<ksize_x; n++) {
2757 for (Int_t m=0; m<ksize_y; m++) {
2758 Int_t xb = i+(n-x_push);
2759 Int_t yb = j+(m-y_push);
2760 if ( (xb >= 1) && (xb <= nx) && (yb >= 1) && (yb <= ny) ) {
2761 bin = GetBin(xb,yb);
2762 Double_t k = kernel[n*ksize_y +m];
2763 //if ( (k != 0.0 ) && (buf[bin] != 0.0) ) { // General version probably does not want the second condition
2764 if ( k != 0.0 ) {
2765 norm += k;
2766 content += k*buf[bin];
2767 if (ebuf) error += k*k*ebuf[bin]*ebuf[bin];
2768 }
2769 }
2770 }
2771 }
2772
2773 if ( norm != 0.0 ) {
2775 if (ebuf) {
2776 error /= (norm*norm);
2777 SetBinError(i,j,sqrt(error));
2778 }
2779 }
2780 }
2781 }
2783
2784 delete [] buf;
2785 delete [] ebuf;
2786}
2787
2788
2789////////////////////////////////////////////////////////////////////////////////
2790/// Stream an object of class TH2.
2791
2793{
2794 if (R__b.IsReading()) {
2795 UInt_t R__s, R__c;
2796 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
2797 if (R__v > 2) {
2798 R__b.ReadClassBuffer(TH2::Class(), this, R__v, R__s, R__c);
2799 return;
2800 }
2801 //====process old versions before automatic schema evolution
2803 R__b >> fScalefactor;
2804 R__b >> fTsumwy;
2805 R__b >> fTsumwy2;
2806 R__b >> fTsumwxy;
2807 //====end of old versions
2808
2809 } else {
2810 R__b.WriteClassBuffer(TH2::Class(),this);
2811 }
2812}
2813
2814
2815//______________________________________________________________________________
2816// TH2C methods
2817// TH2C a 2-D histogram with one byte per cell (char)
2818//______________________________________________________________________________
2819
2820
2821
2822////////////////////////////////////////////////////////////////////////////////
2823/// Constructor.
2824
2826{
2827 SetBinsLength(9);
2828 if (fgDefaultSumw2) Sumw2();
2829}
2830
2831
2832////////////////////////////////////////////////////////////////////////////////
2833/// Destructor.
2834
2836
2837
2838////////////////////////////////////////////////////////////////////////////////
2839/// Constructor
2840/// (see TH2::TH2 for explanation of parameters)
2841
2842TH2C::TH2C(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_t xup
2843 ,Int_t nbinsy,Double_t ylow,Double_t yup)
2844 :TH2(name,title,nbinsx,xlow,xup,nbinsy,ylow,yup)
2845{
2847 if (fgDefaultSumw2) Sumw2();
2848
2849 if (xlow >= xup || ylow >= yup) SetBuffer(fgBufferSize);
2850}
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,const Double_t *xbins
2858 ,Int_t nbinsy,Double_t ylow,Double_t yup)
2859 :TH2(name,title,nbinsx,xbins,nbinsy,ylow,yup)
2860{
2862 if (fgDefaultSumw2) Sumw2();
2863}
2864
2865
2866////////////////////////////////////////////////////////////////////////////////
2867/// Constructor
2868/// (see TH2::TH2 for explanation of parameters)
2869
2870TH2C::TH2C(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_t xup
2871 ,Int_t nbinsy,const Double_t *ybins)
2872 :TH2(name,title,nbinsx,xlow,xup,nbinsy,ybins)
2873{
2875 if (fgDefaultSumw2) Sumw2();
2876}
2877
2878
2879////////////////////////////////////////////////////////////////////////////////
2880/// Constructor
2881/// (see TH2::TH2 for explanation of parameters)
2882
2883TH2C::TH2C(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
2884 ,Int_t nbinsy,const Double_t *ybins)
2885 :TH2(name,title,nbinsx,xbins,nbinsy,ybins)
2886{
2888 if (fgDefaultSumw2) Sumw2();
2889}
2890
2891
2892////////////////////////////////////////////////////////////////////////////////
2893/// Constructor
2894/// (see TH2::TH2 for explanation of parameters)
2895
2896TH2C::TH2C(const char *name,const char *title,Int_t nbinsx,const Float_t *xbins
2897 ,Int_t nbinsy,const Float_t *ybins)
2898 :TH2(name,title,nbinsx,xbins,nbinsy,ybins)
2899{
2901 if (fgDefaultSumw2) Sumw2();
2902}
2903
2904
2905////////////////////////////////////////////////////////////////////////////////
2906/// Copy constructor.
2907/// The list of functions is not copied. (Use Clone() if needed)
2908
2910{
2911 h2c.TH2C::Copy(*this);
2912}
2913
2914
2915////////////////////////////////////////////////////////////////////////////////
2916/// Increment bin content by 1.
2917/// Passing an out-of-range bin leads to undefined behavior
2918
2920{
2921 if (fArray[bin] < 127) fArray[bin]++;
2922}
2923
2924
2925////////////////////////////////////////////////////////////////////////////////
2926/// Increment bin content by w.
2927/// \warning The value of w is cast to `Int_t` before being added.
2928/// Passing an out-of-range bin leads to undefined behavior
2929
2931{
2932 Int_t newval = fArray[bin] + Int_t(w);
2933 if (newval > -128 && newval < 128) {fArray[bin] = Char_t(newval); return;}
2934 if (newval < -127) fArray[bin] = -127;
2935 if (newval > 127) fArray[bin] = 127;
2936}
2937
2938
2939////////////////////////////////////////////////////////////////////////////////
2940/// Copy.
2941
2943{
2945}
2946
2947
2948////////////////////////////////////////////////////////////////////////////////
2949/// Reset this histogram: contents, errors, etc.
2950
2952{
2955}
2956
2957
2958////////////////////////////////////////////////////////////////////////////////
2959/// Set total number of bins including under/overflow
2960/// Reallocate bin contents array
2961
2963{
2964 if (n < 0) n = (fXaxis.GetNbins()+2)*(fYaxis.GetNbins()+2);
2965 fNcells = n;
2966 TArrayC::Set(n);
2967}
2968
2969
2970////////////////////////////////////////////////////////////////////////////////
2971/// Stream an object of class TH2C.
2972
2974{
2975 if (R__b.IsReading()) {
2976 UInt_t R__s, R__c;
2977 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
2978 if (R__v > 2) {
2979 R__b.ReadClassBuffer(TH2C::Class(), this, R__v, R__s, R__c);
2980 return;
2981 }
2982 //====process old versions before automatic schema evolution
2983 if (R__v < 2) {
2984 R__b.ReadVersion();
2987 R__b.ReadVersion();
2988 R__b >> fScalefactor;
2989 R__b >> fTsumwy;
2990 R__b >> fTsumwy2;
2991 R__b >> fTsumwxy;
2992 } else {
2995 R__b.CheckByteCount(R__s, R__c, TH2C::IsA());
2996 }
2997 //====end of old versions
2998
2999 } else {
3000 R__b.WriteClassBuffer(TH2C::Class(),this);
3001 }
3002}
3003
3004
3005////////////////////////////////////////////////////////////////////////////////
3006/// Operator =
3007
3009{
3010 if (this != &h2c)
3011 h2c.TH2C::Copy(*this);
3012 return *this;
3013}
3014
3015
3016////////////////////////////////////////////////////////////////////////////////
3017/// Operator *
3018
3020{
3021 TH2C hnew = h1;
3022 hnew.Scale(c1);
3023 hnew.SetDirectory(nullptr);
3024 return hnew;
3025}
3026
3027
3028////////////////////////////////////////////////////////////////////////////////
3029/// Operator +
3030
3031TH2C operator+(TH2C const &h1, TH2C const &h2)
3032{
3033 TH2C hnew = h1;
3034 hnew.Add(&h2,1);
3035 hnew.SetDirectory(nullptr);
3036 return hnew;
3037}
3038
3039
3040////////////////////////////////////////////////////////////////////////////////
3041/// Operator -
3042
3043TH2C operator-(TH2C const &h1, TH2C const &h2)
3044{
3045 TH2C hnew = h1;
3046 hnew.Add(&h2,-1);
3047 hnew.SetDirectory(nullptr);
3048 return hnew;
3049}
3050
3051
3052////////////////////////////////////////////////////////////////////////////////
3053/// Operator *
3054
3055TH2C operator*(TH2C const &h1, TH2C const &h2)
3056{
3057 TH2C hnew = h1;
3058 hnew.Multiply(&h2);
3059 hnew.SetDirectory(nullptr);
3060 return hnew;
3061}
3062
3063
3064////////////////////////////////////////////////////////////////////////////////
3065/// Operator /
3066
3067TH2C operator/(TH2C const &h1, TH2C const &h2)
3068{
3069 TH2C hnew = h1;
3070 hnew.Divide(&h2);
3071 hnew.SetDirectory(nullptr);
3072 return hnew;
3073}
3074
3075
3076//______________________________________________________________________________
3077// TH2S methods
3078// TH2S a 2-D histogram with two bytes per cell (short integer)
3079//______________________________________________________________________________
3080
3081
3082
3083////////////////////////////////////////////////////////////////////////////////
3084/// Constructor.
3085
3087{
3088 SetBinsLength(9);
3089 if (fgDefaultSumw2) Sumw2();
3090}
3091
3092
3093////////////////////////////////////////////////////////////////////////////////
3094/// Destructor.
3095
3097{
3098}
3099
3100
3101////////////////////////////////////////////////////////////////////////////////
3102/// Constructor
3103/// (see TH2::TH2 for explanation of parameters)
3104
3105TH2S::TH2S(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_t xup
3106 ,Int_t nbinsy,Double_t ylow,Double_t yup)
3107 :TH2(name,title,nbinsx,xlow,xup,nbinsy,ylow,yup)
3108{
3110 if (fgDefaultSumw2) Sumw2();
3111
3112 if (xlow >= xup || ylow >= yup) SetBuffer(fgBufferSize);
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,const Double_t *xbins
3121 ,Int_t nbinsy,Double_t ylow,Double_t yup)
3122 :TH2(name,title,nbinsx,xbins,nbinsy,ylow,yup)
3123{
3125 if (fgDefaultSumw2) Sumw2();
3126}
3127
3128
3129////////////////////////////////////////////////////////////////////////////////
3130/// Constructor
3131/// (see TH2::TH2 for explanation of parameters)
3132
3133TH2S::TH2S(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_t xup
3134 ,Int_t nbinsy,const Double_t *ybins)
3135 :TH2(name,title,nbinsx,xlow,xup,nbinsy,ybins)
3136{
3138 if (fgDefaultSumw2) Sumw2();
3139}
3140
3141
3142////////////////////////////////////////////////////////////////////////////////
3143/// Constructor
3144/// (see TH2::TH2 for explanation of parameters)
3145
3146TH2S::TH2S(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
3147 ,Int_t nbinsy,const Double_t *ybins)
3148 :TH2(name,title,nbinsx,xbins,nbinsy,ybins)
3149{
3151 if (fgDefaultSumw2) Sumw2();
3152}
3153
3154
3155////////////////////////////////////////////////////////////////////////////////
3156/// Constructor
3157/// (see TH2::TH2 for explanation of parameters)
3158
3159TH2S::TH2S(const char *name,const char *title,Int_t nbinsx,const Float_t *xbins
3160 ,Int_t nbinsy,const Float_t *ybins)
3161 :TH2(name,title,nbinsx,xbins,nbinsy,ybins)
3162{
3164 if (fgDefaultSumw2) Sumw2();
3165}
3166
3167
3168////////////////////////////////////////////////////////////////////////////////
3169/// Copy constructor
3170/// The list of functions is not copied. (Use Clone() if needed)
3171
3173{
3174 h2s.TH2S::Copy(*this);
3175}
3176
3177
3178////////////////////////////////////////////////////////////////////////////////
3179/// Increment bin content by 1.
3180/// Passing an out-of-range bin leads to undefined behavior
3181
3183{
3184 if (fArray[bin] < 32767) fArray[bin]++;
3185}
3186
3187
3188////////////////////////////////////////////////////////////////////////////////
3189/// Increment bin content by w.
3190/// \warning The value of w is cast to `Int_t` before being added.
3191/// Passing an out-of-range bin leads to undefined behavior
3192
3194{
3195 Int_t newval = fArray[bin] + Int_t(w);
3196 if (newval > -32768 && newval < 32768) {fArray[bin] = Short_t(newval); return;}
3197 if (newval < -32767) fArray[bin] = -32767;
3198 if (newval > 32767) fArray[bin] = 32767;
3199}
3200
3201
3202////////////////////////////////////////////////////////////////////////////////
3203/// Copy.
3204
3206{
3208}
3209
3210
3211////////////////////////////////////////////////////////////////////////////////
3212/// Reset this histogram: contents, errors, etc.
3213
3215{
3218}
3219
3220
3221////////////////////////////////////////////////////////////////////////////////
3222/// Set total number of bins including under/overflow
3223/// Reallocate bin contents array
3224
3226{
3227 if (n < 0) n = (fXaxis.GetNbins()+2)*(fYaxis.GetNbins()+2);
3228 fNcells = n;
3229 TArrayS::Set(n);
3230}
3231
3232
3233////////////////////////////////////////////////////////////////////////////////
3234/// Stream an object of class TH2S.
3235
3237{
3238 if (R__b.IsReading()) {
3239 UInt_t R__s, R__c;
3240 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
3241 if (R__v > 2) {
3242 R__b.ReadClassBuffer(TH2S::Class(), this, R__v, R__s, R__c);
3243 return;
3244 }
3245 //====process old versions before automatic schema evolution
3246 if (R__v < 2) {
3247 R__b.ReadVersion();
3250 R__b.ReadVersion();
3251 R__b >> fScalefactor;
3252 R__b >> fTsumwy;
3253 R__b >> fTsumwy2;
3254 R__b >> fTsumwxy;
3255 } else {
3258 R__b.CheckByteCount(R__s, R__c, TH2S::IsA());
3259 }
3260 //====end of old versions
3261
3262 } else {
3263 R__b.WriteClassBuffer(TH2S::Class(),this);
3264 }
3265}
3266
3267
3268////////////////////////////////////////////////////////////////////////////////
3269/// Operator =
3270
3272{
3273 if (this != &h2s)
3274 h2s.TH2S::Copy(*this);
3275 return *this;
3276}
3277
3278
3279////////////////////////////////////////////////////////////////////////////////
3280/// Operator *
3281
3283{
3284 TH2S hnew = h2s;
3285 hnew.Scale(c1);
3286 hnew.SetDirectory(nullptr);
3287 return hnew;
3288}
3289
3290
3291////////////////////////////////////////////////////////////////////////////////
3292/// Operator +
3293
3294TH2S operator+(TH2S const &h1, TH2S const &h2)
3295{
3296 TH2S hnew = h1;
3297 hnew.Add(&h2,1);
3298 hnew.SetDirectory(nullptr);
3299 return hnew;
3300}
3301
3302
3303////////////////////////////////////////////////////////////////////////////////
3304/// Operator -
3305
3306TH2S operator-(TH2S const &h1, TH2S const &h2)
3307{
3308 TH2S hnew = h1;
3309 hnew.Add(&h2,-1);
3310 hnew.SetDirectory(nullptr);
3311 return hnew;
3312}
3313
3314
3315////////////////////////////////////////////////////////////////////////////////
3316/// Operator *
3317
3318TH2S operator*(TH2S const &h1, TH2S const &h2)
3319{
3320 TH2S hnew = h1;
3321 hnew.Multiply(&h2);
3322 hnew.SetDirectory(nullptr);
3323 return hnew;
3324}
3325
3326
3327////////////////////////////////////////////////////////////////////////////////
3328/// Operator /
3329
3330TH2S operator/(TH2S const &h1, TH2S const &h2)
3331{
3332 TH2S hnew = h1;
3333 hnew.Divide(&h2);
3334 hnew.SetDirectory(nullptr);
3335 return hnew;
3336}
3337
3338
3339//______________________________________________________________________________
3340// TH2I methods
3341// TH2I a 2-D histogram with four bytes per cell (32 bit integer)
3342//______________________________________________________________________________
3343
3344
3345
3346////////////////////////////////////////////////////////////////////////////////
3347/// Constructor.
3348
3350{
3351 SetBinsLength(9);
3352 if (fgDefaultSumw2) Sumw2();
3353}
3354
3355
3356////////////////////////////////////////////////////////////////////////////////
3357/// Destructor.
3358
3360{
3361}
3362
3363
3364////////////////////////////////////////////////////////////////////////////////
3365/// Constructor
3366/// (see TH2::TH2 for explanation of parameters)
3367
3368TH2I::TH2I(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_t xup
3369 ,Int_t nbinsy,Double_t ylow,Double_t yup)
3370 :TH2(name,title,nbinsx,xlow,xup,nbinsy,ylow,yup)
3371{
3373 if (fgDefaultSumw2) Sumw2();
3374
3375 if (xlow >= xup || ylow >= yup) SetBuffer(fgBufferSize);
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,const Double_t *xbins
3384 ,Int_t nbinsy,Double_t ylow,Double_t yup)
3385 :TH2(name,title,nbinsx,xbins,nbinsy,ylow,yup)
3386{
3388 if (fgDefaultSumw2) Sumw2();
3389}
3390
3391
3392////////////////////////////////////////////////////////////////////////////////
3393/// Constructor
3394/// (see TH2::TH2 for explanation of parameters)
3395
3396TH2I::TH2I(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_t xup
3397 ,Int_t nbinsy,const Double_t *ybins)
3398 :TH2(name,title,nbinsx,xlow,xup,nbinsy,ybins)
3399{
3401 if (fgDefaultSumw2) Sumw2();
3402}
3403
3404
3405////////////////////////////////////////////////////////////////////////////////
3406/// Constructor
3407/// (see TH2::TH2 for explanation of parameters)
3408
3409TH2I::TH2I(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
3410 ,Int_t nbinsy,const Double_t *ybins)
3411 :TH2(name,title,nbinsx,xbins,nbinsy,ybins)
3412{
3414 if (fgDefaultSumw2) Sumw2();
3415}
3416
3417
3418////////////////////////////////////////////////////////////////////////////////
3419/// Constructor
3420/// (see TH2::TH2 for explanation of parameters)
3421
3422TH2I::TH2I(const char *name,const char *title,Int_t nbinsx,const Float_t *xbins
3423 ,Int_t nbinsy,const Float_t *ybins)
3424 :TH2(name,title,nbinsx,xbins,nbinsy,ybins)
3425{
3427 if (fgDefaultSumw2) Sumw2();
3428}
3429
3430
3431////////////////////////////////////////////////////////////////////////////////
3432/// Copy constructor.
3433/// The list of functions is not copied. (Use Clone() if needed)
3434
3436{
3437 h2i.TH2I::Copy(*this);
3438}
3439
3440
3441////////////////////////////////////////////////////////////////////////////////
3442/// Increment bin content by 1.
3443/// Passing an out-of-range bin leads to undefined behavior
3444
3446{
3447 if (fArray[bin] < INT_MAX) fArray[bin]++;
3448}
3449
3450
3451////////////////////////////////////////////////////////////////////////////////
3452/// Increment bin content by w.
3453/// \warning The value of w is cast to `Long64_t` before being added.
3454/// Passing an out-of-range bin leads to undefined behavior
3455
3457{
3458 Long64_t newval = fArray[bin] + Long64_t(w);
3459 if (newval > -INT_MAX && newval < INT_MAX) {fArray[bin] = Int_t(newval); return;}
3460 if (newval < -INT_MAX) fArray[bin] = -INT_MAX;
3461 if (newval > INT_MAX) fArray[bin] = INT_MAX;
3462}
3463
3464
3465////////////////////////////////////////////////////////////////////////////////
3466/// Copy.
3467
3469{
3471}
3472
3473
3474////////////////////////////////////////////////////////////////////////////////
3475/// Reset this histogram: contents, errors, etc.
3476
3478{
3481}
3482
3483
3484////////////////////////////////////////////////////////////////////////////////
3485/// Set total number of bins including under/overflow
3486/// Reallocate bin contents array
3487
3489{
3490 if (n < 0) n = (fXaxis.GetNbins()+2)*(fYaxis.GetNbins()+2);
3491 fNcells = n;
3492 TArrayI::Set(n);
3493}
3494
3495
3496////////////////////////////////////////////////////////////////////////////////
3497/// Operator =
3498
3500{
3501 if (this != &h2i)
3502 h2i.TH2I::Copy(*this);
3503 return *this;
3504}
3505
3506
3507////////////////////////////////////////////////////////////////////////////////
3508/// Operator *
3509
3511{
3512 TH2I hnew = h2i;
3513 hnew.Scale(c1);
3514 hnew.SetDirectory(nullptr);
3515 return hnew;
3516}
3517
3518
3519////////////////////////////////////////////////////////////////////////////////
3520/// Operator +
3521
3522TH2I operator+(TH2I const &h1, TH2I const &h2)
3523{
3524 TH2I hnew = h1;
3525 hnew.Add(&h2,1);
3526 hnew.SetDirectory(nullptr);
3527 return hnew;
3528}
3529
3530
3531////////////////////////////////////////////////////////////////////////////////
3532/// Operator -
3533
3534TH2I operator-(TH2I const &h1, TH2I const &h2)
3535{
3536 TH2I hnew = h1;
3537 hnew.Add(&h2,-1);
3538 hnew.SetDirectory(nullptr);
3539 return hnew;
3540}
3541
3542
3543////////////////////////////////////////////////////////////////////////////////
3544/// Operator *
3545
3546TH2I operator*(TH2I const &h1, TH2I const &h2)
3547{
3548 TH2I hnew = h1;
3549 hnew.Multiply(&h2);
3550 hnew.SetDirectory(nullptr);
3551 return hnew;
3552}
3553
3554
3555////////////////////////////////////////////////////////////////////////////////
3556/// Operator /
3557
3558TH2I operator/(TH2I const &h1, TH2I const &h2)
3559{
3560 TH2I hnew = h1;
3561 hnew.Divide(&h2);
3562 hnew.SetDirectory(nullptr);
3563 return hnew;
3564}
3565
3566
3567//______________________________________________________________________________
3568// TH2L methods
3569// TH2L a 2-D histogram with eight bytes per cell (64 bit integer)
3570//______________________________________________________________________________
3571
3572
3573
3574////////////////////////////////////////////////////////////////////////////////
3575/// Constructor.
3576
3578{
3579 SetBinsLength(9);
3580 if (fgDefaultSumw2) Sumw2();
3581}
3582
3583
3584////////////////////////////////////////////////////////////////////////////////
3585/// Destructor.
3586
3588{
3589}
3590
3591
3592////////////////////////////////////////////////////////////////////////////////
3593/// Constructor
3594/// (see TH2::TH2 for explanation of parameters)
3595
3596TH2L::TH2L(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_t xup
3597 ,Int_t nbinsy,Double_t ylow,Double_t yup)
3598 :TH2(name,title,nbinsx,xlow,xup,nbinsy,ylow,yup)
3599{
3601 if (fgDefaultSumw2) Sumw2();
3602
3603 if (xlow >= xup || ylow >= yup) SetBuffer(fgBufferSize);
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,const Double_t *xbins
3612 ,Int_t nbinsy,Double_t ylow,Double_t yup)
3613 :TH2(name,title,nbinsx,xbins,nbinsy,ylow,yup)
3614{
3616 if (fgDefaultSumw2) Sumw2();
3617}
3618
3619
3620////////////////////////////////////////////////////////////////////////////////
3621/// Constructor
3622/// (see TH2::TH2 for explanation of parameters)
3623
3624TH2L::TH2L(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_t xup
3625 ,Int_t nbinsy,const Double_t *ybins)
3626 :TH2(name,title,nbinsx,xlow,xup,nbinsy,ybins)
3627{
3629 if (fgDefaultSumw2) Sumw2();
3630}
3631
3632
3633////////////////////////////////////////////////////////////////////////////////
3634/// Constructor
3635/// (see TH2::TH2 for explanation of parameters)
3636
3637TH2L::TH2L(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
3638 ,Int_t nbinsy,const Double_t *ybins)
3639 :TH2(name,title,nbinsx,xbins,nbinsy,ybins)
3640{
3642 if (fgDefaultSumw2) Sumw2();
3643}
3644
3645
3646////////////////////////////////////////////////////////////////////////////////
3647/// Constructor
3648/// (see TH2::TH2 for explanation of parameters)
3649
3650TH2L::TH2L(const char *name,const char *title,Int_t nbinsx,const Float_t *xbins
3651 ,Int_t nbinsy,const Float_t *ybins)
3652 :TH2(name,title,nbinsx,xbins,nbinsy,ybins)
3653{
3655 if (fgDefaultSumw2) Sumw2();
3656}
3657
3658
3659////////////////////////////////////////////////////////////////////////////////
3660/// Copy constructor.
3661/// The list of functions is not copied. (Use Clone() if needed)
3662
3664{
3665 h2l.TH2L::Copy(*this);
3666}
3667
3668
3669////////////////////////////////////////////////////////////////////////////////
3670/// Increment bin content by 1.
3671/// Passing an out-of-range bin leads to undefined behavior
3672
3674{
3675 if (fArray[bin] < LLONG_MAX) fArray[bin]++;
3676}
3677
3678
3679////////////////////////////////////////////////////////////////////////////////
3680/// Increment bin content by w.
3681/// \warning The value of w is cast to `Long64_t` before being added.
3682/// Passing an out-of-range bin leads to undefined behavior
3683
3685{
3686 Long64_t newval = fArray[bin] + Long64_t(w);
3687 if (newval > -LLONG_MAX && newval < LLONG_MAX) {fArray[bin] = Int_t(newval); return;}
3688 if (newval < -LLONG_MAX) fArray[bin] = -LLONG_MAX;
3689 if (newval > LLONG_MAX) fArray[bin] = LLONG_MAX;
3690}
3691
3692
3693////////////////////////////////////////////////////////////////////////////////
3694/// Copy.
3695
3697{
3699}
3700
3701
3702////////////////////////////////////////////////////////////////////////////////
3703/// Reset this histogram: contents, errors, etc.
3704
3710
3711
3712////////////////////////////////////////////////////////////////////////////////
3713/// Set total number of bins including under/overflow
3714/// Reallocate bin contents array
3715
3717{
3718 if (n < 0) n = (fXaxis.GetNbins()+2)*(fYaxis.GetNbins()+2);
3719 fNcells = n;
3721}
3722
3723
3724////////////////////////////////////////////////////////////////////////////////
3725/// Operator =
3726
3728{
3729 if (this != &h2l)
3730 h2l.TH2L::Copy(*this);
3731 return *this;
3732}
3733
3734
3735////////////////////////////////////////////////////////////////////////////////
3736/// Operator *
3737
3739{
3740 TH2L hnew = h1;
3741 hnew.Scale(c1);
3742 hnew.SetDirectory(nullptr);
3743 return hnew;
3744}
3745
3746
3747////////////////////////////////////////////////////////////////////////////////
3748/// Operator +
3749
3750TH2L operator+(TH2L const &h1, TH2L const &h2)
3751{
3752 TH2L hnew = h1;
3753 hnew.Add(&h2,1);
3754 hnew.SetDirectory(nullptr);
3755 return hnew;
3756}
3757
3758
3759////////////////////////////////////////////////////////////////////////////////
3760/// Operator -
3761
3762TH2L operator-(TH2L const &h1, TH2L const &h2)
3763{
3764 TH2L hnew = h1;
3765 hnew.Add(&h2,-1);
3766 hnew.SetDirectory(nullptr);
3767 return hnew;
3768}
3769
3770
3771////////////////////////////////////////////////////////////////////////////////
3772/// Operator *
3773
3774TH2L operator*(TH2L const &h1, TH2L const &h2)
3775{
3776 TH2L hnew = h1;
3777 hnew.Multiply(&h2);
3778 hnew.SetDirectory(nullptr);
3779 return hnew;
3780}
3781
3782
3783////////////////////////////////////////////////////////////////////////////////
3784/// Operator /
3785
3786TH2L operator/(TH2L const &h1, TH2L const &h2)
3787{
3788 TH2L hnew = h1;
3789 hnew.Divide(&h2);
3790 hnew.SetDirectory(nullptr);
3791 return hnew;
3792}
3793
3794
3795//______________________________________________________________________________
3796// TH2F methods
3797// TH2F a 2-D histogram with four bytes per cell (float). Maximum precision 7 digits, maximum integer bin content = +/-16777216
3798//______________________________________________________________________________
3799
3800
3801
3802////////////////////////////////////////////////////////////////////////////////
3803/// Constructor.
3804
3806{
3807 SetBinsLength(9);
3808 if (fgDefaultSumw2) Sumw2();
3809}
3810
3811
3812////////////////////////////////////////////////////////////////////////////////
3813/// Destructor.
3814
3816{
3817}
3818
3819
3820////////////////////////////////////////////////////////////////////////////////
3821/// Constructor
3822/// (see TH2::TH2 for explanation of parameters)
3823
3824TH2F::TH2F(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_t xup
3825 ,Int_t nbinsy,Double_t ylow,Double_t yup)
3826 :TH2(name,title,nbinsx,xlow,xup,nbinsy,ylow,yup)
3827{
3829 if (fgDefaultSumw2) Sumw2();
3830
3831 if (xlow >= xup || ylow >= yup) SetBuffer(fgBufferSize);
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,const Double_t *xbins
3840 ,Int_t nbinsy,Double_t ylow,Double_t yup)
3841 :TH2(name,title,nbinsx,xbins,nbinsy,ylow,yup)
3842{
3844 if (fgDefaultSumw2) Sumw2();
3845}
3846
3847
3848////////////////////////////////////////////////////////////////////////////////
3849/// Constructor
3850/// (see TH2::TH2 for explanation of parameters)
3851
3852TH2F::TH2F(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_t xup
3853 ,Int_t nbinsy,const Double_t *ybins)
3854 :TH2(name,title,nbinsx,xlow,xup,nbinsy,ybins)
3855{
3857 if (fgDefaultSumw2) Sumw2();
3858}
3859
3860
3861////////////////////////////////////////////////////////////////////////////////
3862/// Constructor
3863/// (see TH2::TH2 for explanation of parameters)
3864
3865TH2F::TH2F(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
3866 ,Int_t nbinsy,const Double_t *ybins)
3867 :TH2(name,title,nbinsx,xbins,nbinsy,ybins)
3868{
3870 if (fgDefaultSumw2) Sumw2();
3871}
3872
3873
3874////////////////////////////////////////////////////////////////////////////////
3875/// Constructor
3876/// (see TH2::TH2 for explanation of parameters)
3877
3878TH2F::TH2F(const char *name,const char *title,Int_t nbinsx,const Float_t *xbins
3879 ,Int_t nbinsy,const Float_t *ybins)
3880 :TH2(name,title,nbinsx,xbins,nbinsy,ybins)
3881{
3883 if (fgDefaultSumw2) Sumw2();
3884}
3885
3886
3887////////////////////////////////////////////////////////////////////////////////
3888/// Constructor.
3889/// Construct a TH2F from a TMatrixFBase
3890
3892:TH2("TMatrixFBase","",m.GetNcols(),m.GetColLwb(),1+m.GetColUpb(),m.GetNrows(),m.GetRowLwb(),1+m.GetRowUpb())
3893{
3895 Int_t ilow = m.GetRowLwb();
3896 Int_t iup = m.GetRowUpb();
3897 Int_t jlow = m.GetColLwb();
3898 Int_t jup = m.GetColUpb();
3899 for (Int_t i=ilow;i<=iup;i++) {
3900 for (Int_t j=jlow;j<=jup;j++) {
3901 SetBinContent(j-jlow+1,i-ilow+1,m(i,j));
3902 }
3903 }
3904}
3905
3906
3907////////////////////////////////////////////////////////////////////////////////
3908/// Copy constructor.
3909/// The list of functions is not copied. (Use Clone() if needed)
3910
3912{
3913 h2f.TH2F::Copy(*this);
3914}
3915
3916
3917////////////////////////////////////////////////////////////////////////////////
3918/// Copy.
3919
3921{
3923}
3924
3925
3926////////////////////////////////////////////////////////////////////////////////
3927/// Reset this histogram: contents, errors, etc.
3928
3930{
3933}
3934
3935
3936////////////////////////////////////////////////////////////////////////////////
3937/// Set total number of bins including under/overflow
3938/// Reallocate bin contents array
3939
3941{
3942 if (n < 0) n = (fXaxis.GetNbins()+2)*(fYaxis.GetNbins()+2);
3943 fNcells = n;
3944 TArrayF::Set(n);
3945}
3946
3947
3948////////////////////////////////////////////////////////////////////////////////
3949/// Stream an object of class TH2F.
3950
3952{
3953 if (R__b.IsReading()) {
3954 UInt_t R__s, R__c;
3955 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
3956 if (R__v > 2) {
3957 R__b.ReadClassBuffer(TH2F::Class(), this, R__v, R__s, R__c);
3958 return;
3959 }
3960 //====process old versions before automatic schema evolution
3961 if (R__v < 2) {
3962 R__b.ReadVersion();
3965 R__b.ReadVersion();
3966 R__b >> fScalefactor;
3967 R__b >> fTsumwy;
3968 R__b >> fTsumwy2;
3969 R__b >> fTsumwxy;
3970 } else {
3973 R__b.CheckByteCount(R__s, R__c, TH2F::IsA());
3974 }
3975 //====end of old versions
3976
3977 } else {
3978 R__b.WriteClassBuffer(TH2F::Class(),this);
3979 }
3980}
3981
3982
3983////////////////////////////////////////////////////////////////////////////////
3984/// Operator =
3985
3987{
3988 if (this != &h2f)
3989 h2f.TH2F::Copy(*this);
3990 return *this;
3991}
3992
3993
3994////////////////////////////////////////////////////////////////////////////////
3995/// Operator *
3996
3998{
3999 TH2F hnew = h1;
4000 hnew.Scale(c1);
4001 hnew.SetDirectory(nullptr);
4002 return hnew;
4003}
4004
4005
4006////////////////////////////////////////////////////////////////////////////////
4007/// Operator *
4008
4010{
4011 TH2F hnew = h1;
4012 hnew.Scale(c1);
4013 hnew.SetDirectory(nullptr);
4014 return hnew;
4015}
4016
4017
4018////////////////////////////////////////////////////////////////////////////////
4019/// Operator +
4020
4021TH2F operator+(TH2F const &h1, TH2F const &h2)
4022{
4023 TH2F hnew = h1;
4024 hnew.Add(&h2,1);
4025 hnew.SetDirectory(nullptr);
4026 return hnew;
4027}
4028
4029
4030////////////////////////////////////////////////////////////////////////////////
4031/// Operator -
4032
4033TH2F operator-(TH2F const &h1, TH2F const &h2)
4034{
4035 TH2F hnew = h1;
4036 hnew.Add(&h2,-1);
4037 hnew.SetDirectory(nullptr);
4038 return hnew;
4039}
4040
4041
4042////////////////////////////////////////////////////////////////////////////////
4043/// Operator *
4044
4045TH2F operator*(TH2F const &h1, TH2F const &h2)
4046{
4047 TH2F hnew = h1;
4048 hnew.Multiply(&h2);
4049 hnew.SetDirectory(nullptr);
4050 return hnew;
4051}
4052
4053
4054////////////////////////////////////////////////////////////////////////////////
4055/// Operator /
4056
4057TH2F operator/(TH2F const &h1, TH2F const &h2)
4058{
4059 TH2F hnew = h1;
4060 hnew.Divide(&h2);
4061 hnew.SetDirectory(nullptr);
4062 return hnew;
4063}
4064
4065
4066//______________________________________________________________________________
4067// TH2D methods
4068// TH2D a 2-D histogram with eight bytes per cell (double). Maximum precision 14 digits, maximum integer bin content = +/-9007199254740992
4069//______________________________________________________________________________
4070
4071
4072
4073////////////////////////////////////////////////////////////////////////////////
4074/// Constructor.
4075
4077{
4078 SetBinsLength(9);
4079 if (fgDefaultSumw2) Sumw2();
4080}
4081
4082
4083////////////////////////////////////////////////////////////////////////////////
4084/// Destructor.
4085
4087{
4088}
4089
4090
4091////////////////////////////////////////////////////////////////////////////////
4092/// Constructor
4093/// (see TH2::TH2 for explanation of parameters)
4094
4095TH2D::TH2D(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_t xup
4096 ,Int_t nbinsy,Double_t ylow,Double_t yup)
4097 :TH2(name,title,nbinsx,xlow,xup,nbinsy,ylow,yup)
4098{
4100 if (fgDefaultSumw2) Sumw2();
4101
4102 if (xlow >= xup || ylow >= yup) SetBuffer(fgBufferSize);
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,const Double_t *xbins
4111 ,Int_t nbinsy,Double_t ylow,Double_t yup)
4112 :TH2(name,title,nbinsx,xbins,nbinsy,ylow,yup)
4113{
4115 if (fgDefaultSumw2) Sumw2();
4116}
4117
4118
4119////////////////////////////////////////////////////////////////////////////////
4120/// Constructor
4121/// (see TH2::TH2 for explanation of parameters)
4122
4123TH2D::TH2D(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_t xup
4124 ,Int_t nbinsy,const Double_t *ybins)
4125 :TH2(name,title,nbinsx,xlow,xup,nbinsy,ybins)
4126{
4128 if (fgDefaultSumw2) Sumw2();
4129}
4130
4131
4132////////////////////////////////////////////////////////////////////////////////
4133/// Constructor
4134/// (see TH2::TH2 for explanation of parameters)
4135
4136TH2D::TH2D(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
4137 ,Int_t nbinsy,const Double_t *ybins)
4138 :TH2(name,title,nbinsx,xbins,nbinsy,ybins)
4139{
4141 if (fgDefaultSumw2) Sumw2();
4142}
4143
4144
4145////////////////////////////////////////////////////////////////////////////////
4146/// Constructor
4147/// (see TH2::TH2 for explanation of parameters)
4148
4149TH2D::TH2D(const char *name,const char *title,Int_t nbinsx,const Float_t *xbins
4150 ,Int_t nbinsy,const Float_t *ybins)
4151 :TH2(name,title,nbinsx,xbins,nbinsy,ybins)
4152{
4154 if (fgDefaultSumw2) Sumw2();
4155}
4156
4157
4158////////////////////////////////////////////////////////////////////////////////
4159/// Constructor
4160/// Construct a 2-D histogram from a TMatrixDBase
4161
4163:TH2("TMatrixDBase","",m.GetNcols(),m.GetColLwb(),1+m.GetColUpb(),m.GetNrows(),m.GetRowLwb(),1+m.GetRowUpb())
4164{
4166 Int_t ilow = m.GetRowLwb();
4167 Int_t iup = m.GetRowUpb();
4168 Int_t jlow = m.GetColLwb();
4169 Int_t jup = m.GetColUpb();
4170 for (Int_t i=ilow;i<=iup;i++) {
4171 for (Int_t j=jlow;j<=jup;j++) {
4172 SetBinContent(j-jlow+1,i-ilow+1,m(i,j));
4173 }
4174 }
4175 if (fgDefaultSumw2) Sumw2();
4176}
4177
4178
4179////////////////////////////////////////////////////////////////////////////////
4180/// Copy constructor.
4181/// The list of functions is not copied. (Use Clone() if needed)
4182
4184{
4185 // intentionally call virtual Copy method to warn if TProfile2D is copied
4186 h2d.Copy(*this);
4187}
4188
4189
4190////////////////////////////////////////////////////////////////////////////////
4191/// Copy.
4192
4194{
4196}
4197
4198
4199////////////////////////////////////////////////////////////////////////////////
4200/// Reset this histogram: contents, errors, etc.
4201
4203{
4206}
4207
4208
4209////////////////////////////////////////////////////////////////////////////////
4210/// Set total number of bins including under/overflow
4211/// Reallocate bin contents array
4212
4214{
4215 if (n < 0) n = (fXaxis.GetNbins()+2)*(fYaxis.GetNbins()+2);
4216 fNcells = n;
4217 TArrayD::Set(n);
4218}
4219
4220
4221////////////////////////////////////////////////////////////////////////////////
4222/// Stream an object of class TH2D.
4223
4225{
4226 if (R__b.IsReading()) {
4227 UInt_t R__s, R__c;
4228 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
4229 if (R__v > 2) {
4230 R__b.ReadClassBuffer(TH2D::Class(), this, R__v, R__s, R__c);
4231 return;
4232 }
4233 //====process old versions before automatic schema evolution
4234 if (R__v < 2) {
4235 R__b.ReadVersion();
4238 R__b.ReadVersion();
4239 R__b >> fScalefactor;
4240 R__b >> fTsumwy;
4241 R__b >> fTsumwy2;
4242 R__b >> fTsumwxy;
4243 } else {
4246 R__b.CheckByteCount(R__s, R__c, TH2D::IsA());
4247 }
4248 //====end of old versions
4249
4250 } else {
4251 R__b.WriteClassBuffer(TH2D::Class(),this);
4252 }
4253}
4254
4255
4256////////////////////////////////////////////////////////////////////////////////
4257/// Operator =
4258
4260{
4261 // intentionally call virtual Copy method to warn if TProfile2D is copied
4262 if (this != &h2d)
4263 h2d.Copy(*this);
4264 return *this;
4265}
4266
4267
4268
4269////////////////////////////////////////////////////////////////////////////////
4270/// Operator *
4271
4273{
4274 TH2D hnew = h2d;
4275 hnew.Scale(c1);
4276 hnew.SetDirectory(nullptr);
4277 return hnew;
4278}
4279
4280
4281////////////////////////////////////////////////////////////////////////////////
4282/// Operator +
4283
4284TH2D operator+(TH2D const &h1, TH2D const &h2)
4285{
4286 TH2D hnew = h1;
4287 hnew.Add(&h2,1);
4288 hnew.SetDirectory(nullptr);
4289 return hnew;
4290}
4291
4292
4293////////////////////////////////////////////////////////////////////////////////
4294/// Operator -
4295
4296TH2D operator-(TH2D const &h1, TH2D const &h2)
4297{
4298 TH2D hnew = h1;
4299 hnew.Add(&h2,-1);
4300 hnew.SetDirectory(nullptr);
4301 return hnew;
4302}
4303
4304
4305////////////////////////////////////////////////////////////////////////////////
4306/// Operator *
4307
4308TH2D operator*(TH2D const &h1, TH2D const &h2)
4309{
4310 TH2D hnew = h1;
4311 hnew.Multiply(&h2);
4312 hnew.SetDirectory(nullptr);
4313 return hnew;
4314}
4315
4316
4317////////////////////////////////////////////////////////////////////////////////
4318/// Operator /
4319
4320TH2D operator/(TH2D const &h1, TH2D const &h2)
4321{
4322 TH2D hnew = h1;
4323 hnew.Divide(&h2);
4324 hnew.SetDirectory(nullptr);
4325 return hnew;
4326}
#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:3031
TH2C operator*(Float_t c1, TH2C const &h1)
Operator *.
Definition TH2.cxx:3019
TH2C operator/(TH2C const &h1, TH2C const &h2)
Operator /.
Definition TH2.cxx:3067
TH2C operator-(TH2C const &h1, TH2C const &h2)
Operator -.
Definition TH2.cxx:3043
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:875
Bool_t IsAlphanumeric() const
Definition TAxis.h:90
virtual Double_t GetBinCenter(Int_t bin) const
Return center of bin.
Definition TAxis.cxx:481
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:292
virtual Double_t GetBinLowEdge(Int_t bin) const
Return low edge of bin.
Definition TAxis.cxx:521
virtual void Set(Int_t nbins, Double_t xmin, Double_t xmax)
Initialize axis with fix bins.
Definition TAxis.cxx:783
virtual Int_t FindFixBin(Double_t x) const
Find bin number corresponding to abscissa x
Definition TAxis.cxx:421
Int_t GetLast() const
Return last bin on the axis i.e.
Definition TAxis.cxx:472
virtual void ImportAttributes(const TAxis *axis)
Copy axis attributes to this.
Definition TAxis.cxx:684
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:545
virtual Double_t GetBinUpEdge(Int_t bin) const
Return up edge of bin.
Definition TAxis.cxx:531
Int_t GetFirst() const
Return first bin on the axis i.e.
Definition TAxis.cxx:461
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:927
static TClass * Class()
void Reset(Option_t *option="") override
Reset.
Definition TH1.cxx:10324
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:8018
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:7643
virtual Int_t GetNbinsY() const
Definition TH1.h:543
virtual Double_t GetBinError(Int_t bin) const
Return value of error associated to bin number bin.
Definition TH1.cxx:9101
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:7571
virtual Int_t GetDimension() const
Definition TH1.h:528
void Streamer(TBuffer &) override
Stream a class object.
Definition TH1.cxx:6981
@ kIsNotW
Histogram is forced to be not weighted even when the histogram is filled with weighted.
Definition TH1.h:411
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:7151
TAxis * GetXaxis()
Definition TH1.h:572
virtual Int_t GetNcells() const
Definition TH1.h:545
virtual void PutStats(Double_t *stats)
Replace current statistics with the values in array stats.
Definition TH1.cxx:7920
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:542
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:9244
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:573
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:706
virtual void SetBuffer(Int_t bufsize, Option_t *option="")
Set the maximum number of entries to be kept in the buffer.
Definition TH1.cxx:8499
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:9260
virtual Double_t GetBinLowEdge(Int_t bin) const
Return bin lower edge for 1D histogram.
Definition TH1.cxx:9190
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:7938
@ kNstat
Size of statistics data (up to TProfile3D)
Definition TH1.h:423
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:563
Bool_t GetStatOverflowsBehaviour() const
Definition TH1.h:392
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:8808
virtual void Sumw2(Bool_t flag=kTRUE)
Create structure to store sum of squares of weights.
Definition TH1.cxx:9061
virtual void SetEntries(Double_t n)
Definition TH1.h:640
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:2951
static TClass * Class()
TClass * IsA() const override
Definition TH2.h:179
void Streamer(TBuffer &) override
Stream an object of class TH2C.
Definition TH2.cxx:2973
void AddBinContent(Int_t bin) override
Increment bin content by 1.
Definition TH2.cxx:2919
TH2C()
Constructor.
Definition TH2.cxx:2825
TH2C & operator=(const TH2C &h1)
Operator =.
Definition TH2.cxx:3008
~TH2C() override
Destructor.
Definition TH2.cxx:2835
void Copy(TObject &hnew) const override
Copy.
Definition TH2.cxx:2942
void SetBinsLength(Int_t n=-1) override
Set total number of bins including under/overflow Reallocate bin contents array.
Definition TH2.cxx:2962
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:4224
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:4213
~TH2D() override
Destructor.
Definition TH2.cxx:4086
void Copy(TObject &hnew) const override
Copy.
Definition TH2.cxx:4193
TH2D()
Constructor.
Definition TH2.cxx:4076
TH2D & operator=(const TH2D &h1)
Operator =.
Definition TH2.cxx:4259
2-D histogram with a float per channel (see TH1 documentation)
Definition TH2.h:345
TH2F()
Constructor.
Definition TH2.cxx:3805
TClass * IsA() const override
Definition TH2.h:388
TH2F & operator=(const TH2F &h1)
Operator =.
Definition TH2.cxx:3986
~TH2F() override
Destructor.
Definition TH2.cxx:3815
void Copy(TObject &hnew) const override
Copy.
Definition TH2.cxx:3920
static TClass * Class()
void Streamer(TBuffer &) override
Stream an object of class TH2F.
Definition TH2.cxx:3951
void SetBinsLength(Int_t n=-1) override
Set total number of bins including under/overflow Reallocate bin contents array.
Definition TH2.cxx:3940
2-D histogram with an int per channel (see TH1 documentation)
Definition TH2.h:245
TH2I()
Constructor.
Definition TH2.cxx:3349
void Copy(TObject &hnew) const override
Copy.
Definition TH2.cxx:3468
void AddBinContent(Int_t bin) override
Increment bin content by 1.
Definition TH2.cxx:3445
~TH2I() override
Destructor.
Definition TH2.cxx:3359
TH2I & operator=(const TH2I &h1)
Operator =.
Definition TH2.cxx:3499
void SetBinsLength(Int_t n=-1) override
Set total number of bins including under/overflow Reallocate bin contents array.
Definition TH2.cxx:3488
2-D histogram with a long64 per channel (see TH1 documentation)
Definition TH2.h:296
TH2L & operator=(const TH2L &h1)
Operator =.
Definition TH2.cxx:3727
void SetBinsLength(Int_t n=-1) override
Set total number of bins including under/overflow Reallocate bin contents array.
Definition TH2.cxx:3716
~TH2L() override
Destructor.
Definition TH2.cxx:3587
void Copy(TObject &hnew) const override
Copy.
Definition TH2.cxx:3696
TH2L()
Constructor.
Definition TH2.cxx:3577
void AddBinContent(Int_t bin) override
Increment bin content by 1.
Definition TH2.cxx:3673
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:3182
~TH2S() override
Destructor.
Definition TH2.cxx:3096
static TClass * Class()
TH2S & operator=(const TH2S &h1)
Operator =.
Definition TH2.cxx:3271
void Copy(TObject &hnew) const override
Copy.
Definition TH2.cxx:3205
TH2S()
Constructor.
Definition TH2.cxx:3086
void Streamer(TBuffer &) override
Stream an object of class TH2S.
Definition TH2.cxx:3236
void SetBinsLength(Int_t n=-1) override
Set total number of bins including under/overflow Reallocate bin contents array.
Definition TH2.cxx:3225
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.
Definition TH2.cxx:2438
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:2657
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.
Definition TH2.cxx:2071
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:2480
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.
Definition TH2.cxx:2121
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:2489
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:2578
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:2467
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:2612
TClass * IsA() const override
Definition TH2.h:137
void Reset(Option_t *option="") override
Reset this histogram: contents, errors, etc.
Definition TH2.cxx:2562
Double_t fScalefactor
Scale factor.
Definition TH2.h:33
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:2131
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.
Definition TH2.cxx:2398
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:2689
~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
TH1 * ShowBackground(Int_t niter=20, Option_t *option="same") override
This function calculates the background spectrum in this histogram.
Definition TH2.cxx:2641
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:2596
void Streamer(TBuffer &) override
Stream an object of class TH2.
Definition TH2.cxx:2792
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:2629
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:2447
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:1057
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition TObject.cxx:864
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:1071
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition TObject.cxx:1045
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:712
void ToUpper()
Change string to upper case.
Definition TString.cxx:1202
TString & Remove(Ssiz_t pos)
Definition TString.h:693
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2384
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:640
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition TString.h:659
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:251
Double_t Prob(Double_t chi2, Int_t ndf)
Computation of the probability for a certain Chi-squared (chi2) and number of degrees of freedom (ndf...
Definition TMath.cxx:637
Double_t 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:348
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
Definition TMathBase.h:124
TMarker m
Definition textangle.C:8