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