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