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]->SetDirectory(gDirectory);
865 hlist[ipar]->GetXaxis()->SetTitle(outerAxis.GetTitle());
866 if (arr)
867 (*arr)[ipar] = hlist[ipar];
868 }
869 snprintf(name,2000,"%s_chi2",GetName());
870 delete gDirectory->FindObject(name);
871 TH1D *hchi2 = nullptr;
872 if (bins->fN == 0) {
873 hchi2 = new TH1D(name,"chisquare", nOutBins, outerAxis.GetBinLowEdge(firstOutBin), outerAxis.GetBinUpEdge(lastOutBin));
874 } else {
875 hchi2 = new TH1D(name,"chisquare", nOutBins, &bins->fArray[firstOutBin-1]);
876 }
877 hchi2->SetDirectory(gDirectory);
878 hchi2->GetXaxis()->SetTitle(outerAxis.GetTitle());
879 if (arr)
880 (*arr)[npar] = hchi2;
881
882 //Loop on all bins in Y, generate a projection along X
883 Int_t bin;
884 // in case of sliding merge nstep=1, i.e. do slices starting for every bin
885 // now do not slices case with overflow (makes more sense)
886 // when fitting add the option "N". We don;t want to display and store the function
887 // for the temporary histograms that are created and fitted
888 opt += " n ";
889 TH1D *hp = nullptr;
890 for (bin=firstbin;bin+ngroup-1<=lastbin;bin += nstep) {
891 if (onX)
892 hp= ProjectionX("_temp",bin,bin+ngroup-1,proj_opt);
893 else
894 hp= ProjectionY("_temp",bin,bin+ngroup-1,proj_opt);
895 if (hp == nullptr) continue;
896 // nentries can be the effective entries and it could be a very small number but not zero!
897 Double_t nentries = hp->GetEntries();
898 if ( nentries <= 0 || nentries < cut) {
899 if (!opt.Contains("q"))
900 Info("DoFitSlices","Slice %d skipped, the number of entries is zero or smaller than the given cut value, n=%f",bin,nentries);
901 continue;
902 }
904 Int_t binOn = hlist[0]->FindBin(outerAxis.GetBinCenter(bin+ngroup/2));
905 if (!opt.Contains("q"))
906 Info("DoFitSlices","Slice fit %d (%f,%f)",binOn,hlist[0]->GetXaxis()->GetBinLowEdge(binOn),hlist[0]->GetXaxis()->GetBinUpEdge(binOn));
907 hp->Fit(f1,opt.Data());
909 if (npfits > npar && npfits >= cut) {
910 for (ipar=0;ipar<npar;ipar++) {
911 hlist[ipar]->SetBinContent(binOn,f1->GetParameter(ipar));
912 hlist[ipar]->SetBinError(binOn,f1->GetParError(ipar));
913 }
914 hchi2->SetBinContent(binOn,f1->GetChisquare()/(npfits-npar));
915 }
916 else {
917 if (!opt.Contains("q"))
918 Info("DoFitSlices","Fitted slice %d skipped, the number of fitted points is too small, n=%d",bin,npfits);
919 }
920 // don't need to delete hp. If histogram has the same name it is re-used in TH2::Projection
921 }
922 delete hp;
923 delete [] parsave;
924 delete [] name;
925 delete [] title;
926 delete [] hlist;
927}
928
929
930////////////////////////////////////////////////////////////////////////////////
931/// Project slices along X in case of a 2-D histogram, then fit each slice
932/// with function f1 and make a histogram for each fit parameter
933/// Only bins along Y between firstybin and lastybin are considered.
934/// By default (firstybin == 0, lastybin == -1), all bins in y including
935/// over- and underflows are taken into account.
936/// If f1=0, a gaussian is assumed
937/// Before invoking this function, one can set a subrange to be fitted along X
938/// via f1->SetRange(xmin,xmax)
939/// The argument option (default="QNR") can be used to change the fit options.
940/// - "Q" means Quiet mode
941/// - "N" means do not show the result of the fit
942/// - "R" means fit the function in the specified function range
943/// - "G2" merge 2 consecutive bins along X
944/// - "G3" merge 3 consecutive bins along X
945/// - "G4" merge 4 consecutive bins along X
946/// - "G5" merge 5 consecutive bins along X
947/// - "S" sliding merge: merge n consecutive bins along X accordingly to what Gn is given.
948/// It makes sense when used together with a Gn option
949///
950/// The generated histograms are returned by adding them to arr, if arr is not NULL.
951/// arr's SetOwner() is called, to signal that it is the user's responsibility to
952/// delete the histograms, possibly by deleting the array.
953/// ~~~ {.cpp}
954/// TObjArray aSlices;
955/// h2->FitSlicesX(func, 0, -1, 0, "QNR", &aSlices);
956/// ~~~
957/// will already delete the histograms once aSlice goes out of scope. aSlices will
958/// contain the histogram for the i-th parameter of the fit function at aSlices[i];
959/// aSlices[n] (n being the number of parameters) contains the chi2 distribution of
960/// the fits.
961///
962/// If arr is NULL, the generated histograms are added to the list of objects
963/// in the current directory. It is the user's responsibility to delete
964/// these histograms.
965///
966/// Example: Assume a 2-d histogram h2
967/// ~~~ {.cpp}
968/// Root > h2->FitSlicesX(); produces 4 TH1D histograms
969/// with h2_0 containing parameter 0(Constant) for a Gaus fit
970/// of each bin in Y projected along X
971/// with h2_1 containing parameter 1(Mean) for a gaus fit
972/// with h2_2 containing parameter 2(StdDev) for a gaus fit
973/// with h2_chi2 containing the chisquare/number of degrees of freedom for a gaus fit
974///
975/// Root > h2->FitSlicesX(0,15,22,10);
976/// same as above, but only for bins 15 to 22 along Y
977/// and only for bins in Y for which the corresponding projection
978/// along X has more than cut bins filled.
979/// ~~~
980/// NOTE: To access the generated histograms in the current directory, do eg:
981/// ~~~ {.cpp}
982/// TH1D *h2_1 = (TH1D*)gDirectory->Get("h2_1");
983/// ~~~
984
990
991
992////////////////////////////////////////////////////////////////////////////////
993/// Project slices along Y in case of a 2-D histogram, then fit each slice
994/// with function f1 and make a histogram for each fit parameter
995/// Only bins along X between firstxbin and lastxbin are considered.
996/// By default (firstxbin == 0, lastxbin == -1), all bins in x including
997/// over- and underflows are taken into account.
998/// If f1=0, a gaussian is assumed
999/// Before invoking this function, one can set a subrange to be fitted along Y
1000/// via f1->SetRange(ymin,ymax)
1001/// The argument option (default="QNR") can be used to change the fit options.
1002/// - "Q" means Quiet mode
1003/// - "N" means do not show the result of the fit
1004/// - "R" means fit the function in the specified function range
1005/// - "G2" merge 2 consecutive bins along Y
1006/// - "G3" merge 3 consecutive bins along Y
1007/// - "G4" merge 4 consecutive bins along Y
1008/// - "G5" merge 5 consecutive bins along Y
1009/// - "S" sliding merge: merge n consecutive bins along Y accordingly to what Gn is given.
1010/// It makes sense when used together with a Gn option
1011///
1012/// The generated histograms are returned by adding them to arr, if arr is not NULL.
1013/// arr's SetOwner() is called, to signal that it is the user's responsibility to
1014/// delete the histograms, possibly by deleting the array.
1015/// ~~~ {.cpp}
1016/// TObjArray aSlices;
1017/// h2->FitSlicesY(func, 0, -1, 0, "QNR", &aSlices);
1018/// ~~~
1019/// will already delete the histograms once aSlice goes out of scope. aSlices will
1020/// contain the histogram for the i-th parameter of the fit function at aSlices[i];
1021/// aSlices[n] (n being the number of parameters) contains the chi2 distribution of
1022/// the fits.
1023///
1024/// If arr is NULL, the generated histograms are added to the list of objects
1025/// in the current directory. It is the user's responsibility to delete
1026/// these histograms.
1027///
1028/// Example: Assume a 2-d histogram h2
1029/// ~~~ {.cpp}
1030/// Root > h2->FitSlicesY(); produces 4 TH1D histograms
1031/// with h2_0 containing parameter 0(Constant) for a Gaus fit
1032/// of each bin in X projected along Y
1033/// with h2_1 containing parameter 1(Mean) for a gaus fit
1034/// with h2_2 containing parameter 2(StdDev) for a gaus fit
1035/// with h2_chi2 containing the chisquare/number of degrees of freedom for a gaus fit
1036///
1037/// Root > h2->FitSlicesY(0,15,22,10);
1038/// same as above, but only for bins 15 to 22 along X
1039/// and only for bins in X for which the corresponding projection
1040/// along Y has more than cut bins filled.
1041/// ~~~
1042///
1043/// NOTE: To access the generated histograms in the current directory, do eg:
1044/// ~~~ {.cpp}
1045/// TH1D *h2_1 = (TH1D*)gDirectory->Get("h2_1");
1046/// ~~~
1047///
1048/// A complete example of this function is given in tutorial:fitslicesy.C.
1049
1054
1056{
1057 // See comments in TH1::GetBin
1058 Int_t ofy = fYaxis.GetNbins() + 1; // overflow bin
1059 if (biny < 0) biny = 0;
1060 if (biny > ofy) biny = ofy;
1061
1062 return TH1::GetBin(binx) + (fXaxis.GetNbins() + 2) * biny;
1063}
1064
1065
1066////////////////////////////////////////////////////////////////////////////////
1067/// compute first cell (binx,biny) in the range [firstxbin,lastxbin][firstybin,lastybin] for which
1068/// diff = abs(cell_content-c) <= maxdiff
1069/// In case several cells in the specified range with diff=0 are found
1070/// the first cell found is returned in binx,biny.
1071/// In case several cells in the specified range satisfy diff <=maxdiff
1072/// the cell with the smallest difference is returned in binx,biny.
1073/// In all cases the function returns the smallest difference.
1074///
1075/// NOTE1: if firstxbin < 0, firstxbin is set to 1
1076/// if (lastxbin < firstxbin then lastxbin is set to the number of bins in X
1077/// ie if firstxbin=1 and lastxbin=0 (default) the search is on all bins in X except
1078/// for X's under- and overflow bins.
1079/// if firstybin < 0, firstybin is set to 1
1080/// if (lastybin < firstybin then lastybin is set to the number of bins in Y
1081/// ie if firstybin=1 and lastybin=0 (default) the search is on all bins in Y except
1082/// for Y's under- and overflow bins.
1083///
1084/// NOTE2: if maxdiff=0 (default), the first cell with content=c is returned.
1085
1088{
1089 if (fDimension != 2) {
1090 binx = -1;
1091 biny = -1;
1092 Error("GetBinWithContent2","function is only valid for 2-D histograms");
1093 return 0;
1094 }
1095 if (firstxbin < 0) firstxbin = 1;
1097 if (firstybin < 0) firstybin = 1;
1099 Double_t diff, curmax = 1.e240;
1100 for (Int_t j = firstybin; j <= lastybin; j++) {
1101 for (Int_t i = firstxbin; i <= lastxbin; i++) {
1103 if (diff <= 0) {binx = i; biny=j; return diff;}
1104 if (diff < curmax && diff <= maxdiff) {curmax = diff, binx=i; biny=j;}
1105 }
1106 }
1107 return curmax;
1108}
1109
1110
1111////////////////////////////////////////////////////////////////////////////////
1112/// Return correlation factor between axis1 and axis2.
1113
1115{
1116 if (axis1 < 1 || axis2 < 1 || axis1 > 2 || axis2 > 2) {
1117 Error("GetCorrelationFactor","Wrong parameters");
1118 return 0;
1119 }
1120 if (axis1 == axis2) return 1;
1122 if (stddev1 == 0) return 0;
1124 if (stddev2 == 0) return 0;
1126}
1127
1128
1129////////////////////////////////////////////////////////////////////////////////
1130/// Return covariance between axis1 and axis2.
1131
1133{
1134 if (axis1 < 1 || axis2 < 1 || axis1 > 2 || axis2 > 2) {
1135 Error("GetCovariance","Wrong parameters");
1136 return 0;
1137 }
1138 Double_t stats[kNstat];
1139 GetStats(stats);
1140 Double_t sumw = stats[0];
1141 //Double_t sumw2 = stats[1];
1142 Double_t sumwx = stats[2];
1143 Double_t sumwx2 = stats[3];
1144 Double_t sumwy = stats[4];
1145 Double_t sumwy2 = stats[5];
1146 Double_t sumwxy = stats[6];
1147
1148 if (sumw == 0) return 0;
1149 if (axis1 == 1 && axis2 == 1) {
1151 }
1152 if (axis1 == 2 && axis2 == 2) {
1154 }
1155 return sumwxy/sumw - sumwx/sumw*sumwy/sumw;
1156}
1157
1158////////////////////////////////////////////////////////////////////////////////
1159/// Return 2 random numbers along axis x and y distributed according
1160/// to the cell-contents of this 2-D histogram.
1161///
1162/// Return a NaN if the histogram has a bin with negative content
1163///
1164/// @param[out] x reference to random generated x value
1165/// @param[out] y reference to random generated y value
1166/// @param[in] rng (optional) Random number generator pointer used (default is gRandom)
1167/// @param[in] option (optional) Set it to "width" if your non-uniform bin contents represent a density rather than
1168/// counts
1169
1171{
1174 Int_t nbins = nbinsx*nbinsy;
1175 Double_t integral;
1176 // compute integral checking that all bins have positive content (see ROOT-5894)
1177 if (fIntegral) {
1178 if (fIntegral[nbins + 1] != fEntries)
1179 integral = ComputeIntegral(true, option);
1180 else integral = fIntegral[nbins];
1181 } else {
1182 integral = ComputeIntegral(true, option);
1183 }
1184 if (integral == 0 ) { x = 0; y = 0; return;}
1185 // case histogram has negative bins
1186 if (integral == TMath::QuietNaN() ) { x = TMath::QuietNaN(); y = TMath::QuietNaN(); return;}
1187
1188 if (!rng) rng = gRandom;
1189 Double_t r1 = rng->Rndm();
1194 if (r1 > fIntegral[ibin]) x +=
1196 y = fYaxis.GetBinLowEdge(biny+1) + fYaxis.GetBinWidth(biny+1)*rng->Rndm();
1197}
1198
1199
1200////////////////////////////////////////////////////////////////////////////////
1201/// Fill the array stats from the contents of this histogram
1202/// The array stats must be correctly dimensioned in the calling program.
1203/// ~~~ {.cpp}
1204/// stats[0] = sumw
1205/// stats[1] = sumw2
1206/// stats[2] = sumwx
1207/// stats[3] = sumwx2
1208/// stats[4] = sumwy
1209/// stats[5] = sumwy2
1210/// stats[6] = sumwxy
1211/// ~~~
1212///
1213/// If no axis-subranges are specified (via TAxis::SetRange), the array stats
1214/// is simply a copy of the statistics quantities computed at filling time.
1215/// If sub-ranges are specified, the function recomputes these quantities
1216/// from the bin contents in the current axis ranges.
1217///
1218/// Note that the mean value/StdDev is computed using the bins in the currently
1219/// defined ranges (see TAxis::SetRange). By default the ranges include
1220/// all bins from 1 to nbins included, excluding underflows and overflows.
1221/// To force the underflows and overflows in the computation, one must
1222/// call the static function TH1::StatOverflows(kTRUE) before filling
1223/// the histogram.
1224
1225void TH2::GetStats(Double_t *stats) const
1226{
1227 if (fBuffer) ((TH2*)this)->BufferEmpty();
1228
1230 std::fill(stats, stats + 7, 0);
1231
1236 // include underflow/overflow if TH1::StatOverflows(kTRUE) in case no range is set on the axis
1239 if (firstBinX == 1) firstBinX = 0;
1240 if (lastBinX == fXaxis.GetNbins() ) lastBinX += 1;
1241 }
1243 if (firstBinY == 1) firstBinY = 0;
1244 if (lastBinY == fYaxis.GetNbins() ) lastBinY += 1;
1245 }
1246 }
1247 // check for labels axis. In that case corresponding statistics do not make sense and it is set to zero
1248 Bool_t labelXaxis = ((const_cast<TAxis&>(fXaxis)).GetLabels() && fXaxis.CanExtend() );
1249 Bool_t labelYaxis = ((const_cast<TAxis&>(fYaxis)).GetLabels() && fYaxis.CanExtend() );
1250
1251 for (Int_t biny = firstBinY; biny <= lastBinY; ++biny) {
1253 for (Int_t binx = firstBinX; binx <= lastBinX; ++binx) {
1255 //w = TMath::Abs(GetBinContent(bin));
1256 Int_t bin = GetBin(binx,biny);
1258 Double_t wx = w * x; // avoid some extra multiplications at the expense of some clarity
1259 Double_t wy = w * y;
1260
1261 stats[0] += w;
1262 stats[1] += GetBinErrorSqUnchecked(bin);
1263 stats[2] += wx;
1264 stats[3] += wx * x;
1265 stats[4] += wy;
1266 stats[5] += wy * y;
1267 stats[6] += wx * y;
1268 }
1269 }
1270 } else {
1271 stats[0] = fTsumw;
1272 stats[1] = fTsumw2;
1273 stats[2] = fTsumwx;
1274 stats[3] = fTsumwx2;
1275 stats[4] = fTsumwy;
1276 stats[5] = fTsumwy2;
1277 stats[6] = fTsumwxy;
1278 }
1279}
1280
1281
1282////////////////////////////////////////////////////////////////////////////////
1283/// Return integral of bin contents. Only bins in the bins range are considered.
1284/// By default the integral is computed as the sum of bin contents in the range.
1285/// if option "width" is specified, the integral is the sum of
1286/// the bin contents multiplied by the bin width in x and in y.
1287
1293
1294
1295////////////////////////////////////////////////////////////////////////////////
1296/// Return integral of bin contents in range [firstxbin,lastxbin],[firstybin,lastybin]
1297/// for a 2-D histogram
1298/// By default the integral is computed as the sum of bin contents in the range.
1299/// if option "width" is specified, the integral is the sum of
1300/// the bin contents multiplied by the bin width in x and in y.
1301
1307
1308////////////////////////////////////////////////////////////////////////////////
1309/// Return integral of bin contents in range [firstxbin,lastxbin],[firstybin,lastybin]
1310/// for a 2-D histogram. Calculates also the integral error using error propagation
1311/// from the bin errors assuming that all the bins are uncorrelated.
1312/// By default the integral is computed as the sum of bin contents in the range.
1313/// if option "width" is specified, the integral is the sum of
1314/// the bin contents multiplied by the bin width in x and in y.
1315
1320
1321////////////////////////////////////////////////////////////////////////////////
1322///illegal for a TH2
1323
1325{
1326 Error("Interpolate","This function must be called with 2 arguments for a TH2");
1327 return 0;
1328}
1329
1330////////////////////////////////////////////////////////////////////////////////
1331/// Given a point P(x,y), Interpolate approximates the value via bilinear
1332/// interpolation based on the four nearest bin centers
1333/// see Wikipedia, Bilinear Interpolation
1334/// Andy Mastbaum 10/8/2008
1335/// vaguely based on R.Raja 6-Sep-2008
1336
1338{
1339 Double_t f=0;
1340 Double_t x1=0,x2=0,y1=0,y2=0;
1341 Double_t dx,dy;
1345 Error("Interpolate","Cannot interpolate outside histogram domain.");
1346 return 0;
1347 }
1348 Int_t quadrant = 0; // CCW from UR 1,2,3,4
1349 // which quadrant of the bin (bin_P) are we in?
1353 quadrant = 1; // upper right
1355 quadrant = 2; // upper left
1357 quadrant = 3; // lower left
1359 quadrant = 4; // lower right
1360 switch(quadrant) {
1361 case 1:
1366 break;
1367 case 2:
1372 break;
1373 case 3:
1378 break;
1379 case 4:
1384 break;
1385 }
1387 if(bin_x1<1) bin_x1=1;
1391 if(bin_y1<1) bin_y1=1;
1402 Double_t d = 1.0*(x2-x1)*(y2-y1);
1403 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);
1404 return f;
1405}
1406
1407
1408////////////////////////////////////////////////////////////////////////////////
1409///illegal for a TH2
1410
1412{
1413 Error("Interpolate","This function must be called with 2 arguments for a TH2");
1414 return 0;
1415}
1416
1417
1418////////////////////////////////////////////////////////////////////////////////
1419/// Statistical test of compatibility in shape between
1420/// THIS histogram and h2, using Kolmogorov test.
1421/// Default: Ignore under- and overflow bins in comparison
1422///
1423/// option is a character string to specify options
1424/// - "U" include Underflows in test
1425/// - "O" include Overflows
1426/// - "N" include comparison of normalizations
1427/// - "D" Put out a line of "Debug" printout
1428/// - "M" Return the Maximum Kolmogorov distance instead of prob
1429///
1430/// The returned function value is the probability of test
1431/// (much less than one means NOT compatible)
1432///
1433/// The KS test uses the distance between the pseudo-CDF's obtained
1434/// from the histogram. Since in 2D the order for generating the pseudo-CDF is
1435/// arbitrary, two pairs of pseudo-CDF are used, one starting from the x axis the
1436/// other from the y axis and the maximum distance is the average of the two maximum
1437/// distances obtained.
1438///
1439/// Code adapted by Rene Brun from original HBOOK routine HDIFF
1440
1442{
1443 TString opt = option;
1444 opt.ToUpper();
1445
1446 Double_t prb = 0;
1447 TH1 *h1 = (TH1*)this;
1448 if (h2 == nullptr) return 0;
1449 const TAxis *xaxis1 = h1->GetXaxis();
1450 const TAxis *xaxis2 = h2->GetXaxis();
1451 const TAxis *yaxis1 = h1->GetYaxis();
1452 const TAxis *yaxis2 = h2->GetYaxis();
1453 Int_t ncx1 = xaxis1->GetNbins();
1454 Int_t ncx2 = xaxis2->GetNbins();
1455 Int_t ncy1 = yaxis1->GetNbins();
1456 Int_t ncy2 = yaxis2->GetNbins();
1457
1458 // Check consistency of dimensions
1459 if (h1->GetDimension() != 2 || h2->GetDimension() != 2) {
1460 Error("KolmogorovTest","Histograms must be 2-D\n");
1461 return 0;
1462 }
1463
1464 // Check consistency in number of channels
1465 if (ncx1 != ncx2) {
1466 Error("KolmogorovTest","Number of channels in X is different, %d and %d\n",ncx1,ncx2);
1467 return 0;
1468 }
1469 if (ncy1 != ncy2) {
1470 Error("KolmogorovTest","Number of channels in Y is different, %d and %d\n",ncy1,ncy2);
1471 return 0;
1472 }
1473
1474 // Check consistency in channel edges
1477 Double_t difprec = 1e-5;
1478 Double_t diff1 = TMath::Abs(xaxis1->GetXmin() - xaxis2->GetXmin());
1479 Double_t diff2 = TMath::Abs(xaxis1->GetXmax() - xaxis2->GetXmax());
1480 if (diff1 > difprec || diff2 > difprec) {
1481 Error("KolmogorovTest","histograms with different binning along X");
1482 return 0;
1483 }
1484 diff1 = TMath::Abs(yaxis1->GetXmin() - yaxis2->GetXmin());
1485 diff2 = TMath::Abs(yaxis1->GetXmax() - yaxis2->GetXmax());
1486 if (diff1 > difprec || diff2 > difprec) {
1487 Error("KolmogorovTest","histograms with different binning along Y");
1488 return 0;
1489 }
1490
1491 // Should we include Uflows, Oflows?
1492 Int_t ibeg = 1, jbeg = 1;
1493 Int_t iend = ncx1, jend = ncy1;
1494 if (opt.Contains("U")) {ibeg = 0; jbeg = 0;}
1495 if (opt.Contains("O")) {iend = ncx1+1; jend = ncy1+1;}
1496
1497 Int_t i,j;
1498 Double_t sum1 = 0;
1499 Double_t sum2 = 0;
1500 Double_t w1 = 0;
1501 Double_t w2 = 0;
1502 for (i = ibeg; i <= iend; i++) {
1503 for (j = jbeg; j <= jend; j++) {
1504 sum1 += h1->GetBinContent(i,j);
1505 sum2 += h2->GetBinContent(i,j);
1506 Double_t ew1 = h1->GetBinError(i,j);
1507 Double_t ew2 = h2->GetBinError(i,j);
1508 w1 += ew1*ew1;
1509 w2 += ew2*ew2;
1510
1511 }
1512 }
1513
1514 // Check that both scatterplots contain events
1515 if (sum1 == 0) {
1516 Error("KolmogorovTest","Integral is zero for h1=%s\n",h1->GetName());
1517 return 0;
1518 }
1519 if (sum2 == 0) {
1520 Error("KolmogorovTest","Integral is zero for h2=%s\n",h2->GetName());
1521 return 0;
1522 }
1523 // calculate the effective entries.
1524 // the case when errors are zero (w1 == 0 or w2 ==0) are equivalent to
1525 // compare to a function. In that case the rescaling is done only on sqrt(esum2) or sqrt(esum1)
1526 Double_t esum1 = 0, esum2 = 0;
1527 if (w1 > 0)
1528 esum1 = sum1 * sum1 / w1;
1529 else
1530 afunc1 = kTRUE; // use later for calculating z
1531
1532 if (w2 > 0)
1533 esum2 = sum2 * sum2 / w2;
1534 else
1535 afunc2 = kTRUE; // use later for calculating z
1536
1537 if (afunc2 && afunc1) {
1538 Error("KolmogorovTest","Errors are zero for both histograms\n");
1539 return 0;
1540 }
1541
1542 // Find first Kolmogorov distance
1543 Double_t s1 = 1/sum1;
1544 Double_t s2 = 1/sum2;
1545 Double_t dfmax1 = 0;
1546 Double_t rsum1=0, rsum2=0;
1547 for (i=ibeg;i<=iend;i++) {
1548 for (j=jbeg;j<=jend;j++) {
1549 rsum1 += s1*h1->GetBinContent(i,j);
1550 rsum2 += s2*h2->GetBinContent(i,j);
1552 }
1553 }
1554
1555 // Find second Kolmogorov distance
1556 Double_t dfmax2 = 0;
1557 rsum1=0, rsum2=0;
1558 for (j=jbeg;j<=jend;j++) {
1559 for (i=ibeg;i<=iend;i++) {
1560 rsum1 += s1*h1->GetBinContent(i,j);
1561 rsum2 += s2*h2->GetBinContent(i,j);
1563 }
1564 }
1565
1566 // Get Kolmogorov probability: use effective entries, esum1 or esum2, for normalizing it
1569 else if (afunc2) factnm = TMath::Sqrt(esum1);
1571
1572 // take average of the two distances
1573 Double_t dfmax = 0.5*(dfmax1+dfmax2);
1574 Double_t z = dfmax*factnm;
1575
1577
1578 Double_t prb1 = 0, prb2 = 0;
1579 // option N to combine normalization makes sense if both afunc1 and afunc2 are false
1580 if (opt.Contains("N") && !(afunc1 || afunc2 ) ) {
1581 // Combine probabilities for shape and normalization
1582 prb1 = prb;
1585 prb2 = TMath::Prob(chi2,1);
1586 // see Eadie et al., section 11.6.2
1587 if (prb > 0 && prb2 > 0) prb = prb*prb2*(1-TMath::Log(prb*prb2));
1588 else prb = 0;
1589 }
1590
1591 // debug printout
1592 if (opt.Contains("D")) {
1593 printf(" Kolmo Prob h1 = %s, sum1=%g\n",h1->GetName(),sum1);
1594 printf(" Kolmo Prob h2 = %s, sum2=%g\n",h2->GetName(),sum2);
1595 printf(" Kolmo Probabil = %f, Max Dist = %g\n",prb,dfmax);
1596 if (opt.Contains("N"))
1597 printf(" Kolmo Probabil = %f for shape alone, =%f for normalisation alone\n",prb1,prb2);
1598 }
1599 // This numerical error condition should never occur:
1600 if (TMath::Abs(rsum1-1) > 0.002) Warning("KolmogorovTest","Numerical problems with h1=%s\n",h1->GetName());
1601 if (TMath::Abs(rsum2-1) > 0.002) Warning("KolmogorovTest","Numerical problems with h2=%s\n",h2->GetName());
1602
1603 if(opt.Contains("M")) return dfmax; // return average of max distance
1604
1605 return prb;
1606}
1607
1608
1609////////////////////////////////////////////////////////////////////////////////
1610/// Rebin only the X axis
1611/// see Rebin2D
1612
1614{
1615 return Rebin2D(ngroup, 1, newname);
1616}
1617
1618
1619////////////////////////////////////////////////////////////////////////////////
1620/// Rebin only the Y axis
1621/// see Rebin2D
1622
1624{
1625 return Rebin2D(1, ngroup, newname);
1626}
1627
1628////////////////////////////////////////////////////////////////////////////////
1629/// Override TH1::Rebin as TH2::RebinX
1630/// Rebinning in variable binning as for TH1 is not allowed
1631/// If a non-null pointer is given an error is flagged
1632/// see RebinX and Rebin2D
1633
1634TH2 * TH2::Rebin( Int_t ngroup, const char*newname, const Double_t *xbins)
1635{
1636 if (xbins != nullptr) {
1637 Error("Rebin","Rebinning a 2-d histogram into variable bins is not supported (it is possible only for 1-d histograms). Return a nullptr");
1638 return nullptr;
1639 }
1640 Info("Rebin","Rebinning only the x-axis. Use Rebin2D for rebinning both axes");
1641 return RebinX(ngroup, newname);
1642}
1643////////////////////////////////////////////////////////////////////////////////
1644/// Rebin this histogram grouping nxgroup/nygroup bins along the xaxis/yaxis together.
1645///
1646/// if newname is not blank a new temporary histogram hnew is created.
1647/// else the current histogram is modified (default)
1648/// The parameter nxgroup/nygroup indicate how many bins along the xaxis/yaxis of this
1649/// have to me merged into one bin of hnew
1650/// If the original histogram has errors stored (via Sumw2), the resulting
1651/// histograms has new errors correctly calculated.
1652///
1653/// examples: if hpxpy is an existing TH2 histogram with 40 x 40 bins
1654/// ~~~ {.cpp}
1655/// hpxpy->Rebin2D(); // merges two bins along the xaxis and yaxis in one in hpxpy
1656/// // Carefull: previous contents of hpxpy are lost
1657/// hpxpy->RebinX(5); //merges five bins along the xaxis in one in hpxpy
1658/// TH2 *hnew = hpxpy->RebinY(5,"hnew"); // creates a new histogram hnew
1659/// // merging 5 bins of h1 along the yaxis in one bin
1660/// ~~~
1661///
1662/// NOTE : If nxgroup/nygroup is not an exact divider of the number of bins,
1663/// along the xaxis/yaxis the top limit(s) of the rebinned histogram
1664/// is changed to the upper edge of the xbin=newxbins*nxgroup resp.
1665/// ybin=newybins*nygroup and the corresponding bins are added to
1666/// the overflow bin.
1667/// Statistics will be recomputed from the new bin contents.
1668
1670{
1673 Int_t nx = nxbins + 2; // normal bins + underflow and overflow
1674 Int_t ny = nybins + 2;
1679
1680 if (GetDimension() != 2) {
1681 Error("Rebin2D", "Histogram must be TH2. This histogram has %d dimensions.", GetDimension());
1682 return nullptr;
1683 }
1684 if ((nxgroup <= 0) || (nxgroup > nxbins)) {
1685 Error("Rebin2D", "Illegal value of nxgroup=%d",nxgroup);
1686 return nullptr;
1687 }
1688 if ((nygroup <= 0) || (nygroup > nybins)) {
1689 Error("Rebin2D", "Illegal value of nygroup=%d",nygroup);
1690 return nullptr;
1691 }
1692
1695 Int_t newnx = newxbins + 2; // regular bins + overflow / underflow
1696 Int_t newny = newybins + 2; // regular bins + overflow / underflow
1697
1698 // Save old bin contents into a new array
1700 for (Int_t i = 0; i < fNcells; ++i) oldBins[i] = RetrieveBinContent(i);
1701
1702 Double_t* oldErrors = nullptr;
1703 if (fSumw2.fN) {
1704 oldErrors = new Double_t[fNcells];
1705 for (Int_t i = 0; i < fNcells; ++i) oldErrors[i] = GetBinErrorSqUnchecked(i);
1706 }
1707
1708 // create a clone of the old histogram if newname is specified
1709 TH2* hnew = this;
1710 if (newname && strlen(newname)) {
1711 hnew = (TH2*)Clone();
1712 hnew->SetName(newname);
1713 }
1714
1715 bool resetStat = false;
1716
1717 // change axis specs and rebuild bin contents array
1718 if(newxbins * nxgroup != nxbins) {
1720 resetStat = true; // stats must be reset because top bins will be moved to overflow bin
1721 }
1722 if(newybins * nygroup != nybins) {
1724 resetStat = true; // stats must be reset because top bins will be moved to overflow bin
1725 }
1726
1727 // save the TAttAxis members (reset by SetBins) for x axis
1739 // save the TAttAxis members (reset by SetBins) for y axis
1751
1752
1753 // copy merged bin contents (ignore under/overflows)
1754 if (nxgroup != 1 || nygroup != 1) {
1755 if(fXaxis.GetXbins()->GetSize() > 0 || fYaxis.GetXbins()->GetSize() > 0){
1756 // variable bin sizes in x or y, don't treat both cases separately
1757 Double_t *xbins = new Double_t[newxbins + 1];
1758 for(Int_t i = 0; i <= newxbins; ++i) xbins[i] = fXaxis.GetBinLowEdge(1 + i * nxgroup);
1759 Double_t *ybins = new Double_t[newybins + 1];
1760 for(Int_t i = 0; i <= newybins; ++i) ybins[i] = fYaxis.GetBinLowEdge(1 + i * nygroup);
1761 hnew->SetBins(newxbins, xbins, newybins, ybins); // changes also errors array (if any)
1762 delete [] xbins;
1763 delete [] ybins;
1764 } else {
1765 hnew->SetBins(newxbins, xmin, xmax, newybins, ymin, ymax); //changes also errors array
1766 }
1767
1768 // (0, 0): x - underflow; y - underflow
1769 hnew->UpdateBinContent(0, oldBins[0]);
1770 if (oldErrors) hnew->fSumw2[0] = 0;
1771
1772 // (x, 0): x - regular / overflow; y - underflow
1773 for(Int_t binx = 1, oldbinx = 1; binx < newnx; ++binx, oldbinx += nxgroup){
1774 Double_t binContent = 0.0, binErrorSq = 0.0;
1775 for (Int_t i = 0; i < nxgroup && (oldbinx + i) < nx; ++i) {
1776 Int_t bin = oldbinx + i;
1777 binContent += oldBins[bin];
1778 if(oldErrors) binErrorSq += oldErrors[bin];
1779 }
1780 Int_t newbin = binx;
1781 hnew->UpdateBinContent(newbin, binContent);
1782 if (oldErrors) hnew->fSumw2[newbin] = binErrorSq;
1783 }
1784
1785 // (0, y): x - underflow; y - regular / overflow
1786 for(Int_t biny = 1, oldbiny = 1; biny < newny; ++biny, oldbiny += nygroup){
1787 Double_t binContent = 0.0, binErrorSq = 0.0;
1788 for (Int_t j = 0; j < nygroup && (oldbiny + j) < ny; ++j) {
1789 Int_t bin = (oldbiny + j) * nx;
1790 binContent += oldBins[bin];
1791 if(oldErrors) binErrorSq += oldErrors[bin];
1792 }
1793 Int_t newbin = biny * newnx;
1794 hnew->UpdateBinContent(newbin, binContent);
1795 if (oldErrors) hnew->fSumw2[newbin] = binErrorSq;
1796 }
1797
1798 // (x, y): x - regular / overflow; y - regular / overflow
1799 for (Int_t binx = 1, oldbinx = 1; binx < newnx; ++binx, oldbinx += nxgroup) {
1800 for (Int_t biny = 1, oldbiny = 1; biny < newny; ++biny, oldbiny += nygroup) {
1801 Double_t binContent = 0.0, binErrorSq = 0.0;
1802 for (Int_t i = 0; i < nxgroup && (oldbinx + i) < nx; ++i) {
1803 for (Int_t j = 0; j < nygroup && (oldbiny + j) < ny; ++j) {
1804 Int_t bin = oldbinx + i + (oldbiny + j) * nx;
1805 binContent += oldBins[bin];
1806 if (oldErrors) binErrorSq += oldErrors[bin];
1807 }
1808 }
1809 Int_t newbin = binx + biny * newnx;
1810 hnew->UpdateBinContent(newbin, binContent);
1811 if (oldErrors) hnew->fSumw2[newbin] = binErrorSq;
1812 }
1813 }
1814 }
1815
1816 // Restore x axis attributes
1828 // Restore y axis attributes
1840
1841 if (resetStat) hnew->ResetStats();
1842
1843 delete [] oldBins;
1844 if (oldErrors) delete [] oldErrors;
1845 return hnew;
1846}
1847
1848
1849////////////////////////////////////////////////////////////////////////////////
1850
1852{
1853 TString opt = option;
1854 // extract cut infor
1855 TString cut;
1856 Int_t i1 = opt.Index("[");
1857 if (i1>=0) {
1858 Int_t i2 = opt.Index("]");
1859 cut = opt(i1,i2-i1+1);
1860 }
1861 opt.ToLower();
1862 bool originalRange = opt.Contains("o");
1863 bool useWidth = opt.Contains("width");
1864
1865 const TAxis& outAxis = ( onX ? fXaxis : fYaxis );
1866 const TAxis& inAxis = ( onX ? fYaxis : fXaxis );
1867 Int_t inN = inAxis.GetNbins();
1868 const char *expectedName = ( onX ? "_pfx" : "_pfy" );
1869
1870 // outer axis cannot be outside original axis (this fixes ROOT-8781)
1871 // and firstOutBin and lastOutBin cannot be both equal to zero
1872 Int_t firstOutBin = std::max(outAxis.GetFirst(),1);
1873 Int_t lastOutBin = std::min(outAxis.GetLast(),outAxis.GetNbins() ) ;
1874
1875 if ( lastbin < firstbin && inAxis.TestBit(TAxis::kAxisRange) ) {
1876 firstbin = inAxis.GetFirst();
1877 lastbin = inAxis.GetLast();
1878 // For special case of TAxis::SetRange, when first == 1 and last
1879 // = N and the range bit has been set, the TAxis will return 0
1880 // for both.
1881 if (firstbin == 0 && lastbin == 0)
1882 {
1883 firstbin = 1;
1884 lastbin = inAxis.GetNbins();
1885 }
1886 }
1887 if (firstbin < 0) firstbin = 1;
1888 if (lastbin < 0) lastbin = inN;
1889 if (lastbin > inN+1) lastbin = inN;
1890
1891 // Create the profile histogram
1892 char *pname = (char*)name;
1893 if (name && strcmp(name, expectedName) == 0) {
1894 Int_t nch = strlen(GetName()) + 5;
1895 pname = new char[nch];
1896 snprintf(pname,nch,"%s%s",GetName(),name);
1897 }
1898 TProfile *h1=nullptr;
1899 //check if a profile with identical name exist
1900 // if compatible reset and re-use previous histogram
1901 TObject *h1obj = gROOT->FindObject(pname);
1902 if (h1obj && h1obj->InheritsFrom(TH1::Class())) {
1903 if (h1obj->IsA() != TProfile::Class() ) {
1904 Error("DoProfile","Histogram with name %s must be a TProfile and is a %s",name,h1obj->ClassName());
1905 return nullptr;
1906 }
1907 h1 = (TProfile*)h1obj;
1908 // reset the existing histogram and set always the new binning for the axis
1909 // This avoid problems when the histogram already exists and the histograms is rebinned or its range has changed
1910 // (see https://savannah.cern.ch/bugs/?94101 or https://savannah.cern.ch/bugs/?95808 )
1911 h1->Reset();
1912 const TArrayD *xbins = outAxis.GetXbins();
1913 if (xbins->fN == 0) {
1914 if ( originalRange )
1915 h1->SetBins(outAxis.GetNbins(),outAxis.GetXmin(),outAxis.GetXmax());
1916 else
1917 h1->SetBins(lastOutBin-firstOutBin+1,outAxis.GetBinLowEdge(firstOutBin),outAxis.GetBinUpEdge(lastOutBin));
1918 } else {
1919 // case variable bins
1920 if (originalRange )
1921 h1->SetBins(outAxis.GetNbins(),xbins->fArray);
1922 else
1923 h1->SetBins(lastOutBin-firstOutBin+1,&xbins->fArray[firstOutBin-1]);
1924 }
1925 }
1926
1927 Int_t ncuts = 0;
1928 if (opt.Contains("[")) {
1929 ((TH2 *)this)->GetPainter();
1930 if (fPainter) ncuts = fPainter->MakeCuts((char*)cut.Data());
1931 }
1932
1933 if (!h1) {
1934 const TArrayD *bins = outAxis.GetXbins();
1935 if (bins->fN == 0) {
1936 if ( originalRange )
1937 h1 = new TProfile(pname,GetTitle(),outAxis.GetNbins(),outAxis.GetXmin(),outAxis.GetXmax(),opt);
1938 else
1940 outAxis.GetBinLowEdge(firstOutBin),
1941 outAxis.GetBinUpEdge(lastOutBin), opt);
1942 } else {
1943 // case variable bins
1944 if (originalRange )
1945 h1 = new TProfile(pname,GetTitle(),outAxis.GetNbins(),bins->fArray,opt);
1946 else
1948 }
1949 }
1950 if (pname != name) delete [] pname;
1951
1952 // Copy attributes
1954 THashList* labels=outAxis.GetLabels();
1955 if (labels) {
1956 TIter iL(labels);
1957 TObjString* lb;
1958 Int_t i = 1;
1959 while ((lb=(TObjString*)iL())) {
1960 h1->GetXaxis()->SetBinLabel(i,lb->String().Data());
1961 i++;
1962 }
1963 }
1964
1965 h1->SetLineColor(this->GetLineColor());
1966 h1->SetFillColor(this->GetFillColor());
1967 h1->SetMarkerColor(this->GetMarkerColor());
1968 h1->SetMarkerStyle(this->GetMarkerStyle());
1969
1970 // check if histogram is weighted
1971 // in case need to store sum of weight square/bin for the profile
1972 TArrayD & binSumw2 = *(h1->GetBinSumw2());
1973 bool useWeights = (GetSumw2N() > 0);
1974 if (useWeights && (binSumw2.fN != h1->GetNcells()) ) h1->Sumw2();
1975 // we need to set this bit because we fill the profile using a single Fill for many entries
1976 // This is needed for the changes applied to make automatically the histogram weighted in ROOT 6 versions
1977 else h1->SetBit(TH1::kIsNotW);
1978
1979 // Fill the profile histogram
1980 // no entries/bin is available so can fill only using bin content as weight
1981
1982 // implement filling of projected histogram
1983 // outbin is bin number of outAxis (the projected axis). Loop is done on all bin of TH2 histograms
1984 // inbin is the axis being integrated. Loop is done only on the selected bins
1985 for ( Int_t outbin = 0; outbin <= outAxis.GetNbins() + 1; ++outbin) {
1987
1988 // find corresponding bin number in h1 for outbin (binOut)
1989 Double_t xOut = outAxis.GetBinCenter(outbin);
1991 if (binOut <0) continue;
1992
1993 for (Int_t inbin = firstbin ; inbin <= lastbin ; ++inbin) {
1994 Int_t binx, biny;
1995 if (onX) { binx = outbin; biny=inbin; }
1996 else { binx = inbin; biny=outbin; }
1997
1998 if (ncuts) {
1999 if (!fPainter->IsInside(binx,biny)) continue;
2000 }
2001 Int_t bin = GetBin(binx, biny);
2003 double step = useWidth ? inAxis.GetBinWidth(inbin) : 1;
2004
2005 if (cxy) {
2006 Double_t tmp = 0;
2007 // the following fill update wrongly the fBinSumw2- need to save it before
2008 if ( useWeights ) tmp = binSumw2.fArray[binOut];
2009 h1->Fill( xOut, inAxis.GetBinCenter(inbin), cxy * step);
2010 if ( useWeights ) binSumw2.fArray[binOut] = tmp + fSumw2.fArray[bin];
2011 }
2012
2013 }
2014 }
2015
2016 // the statistics must be recalculated since by using the Fill method the total sum of weight^2 is
2017 // not computed correctly
2018 // for a profile does not much sense to re-use statistics of original TH2
2019 h1->ResetStats();
2020 // Also we need to set the entries since they have not been correctly calculated during the projection
2021 // we can only set them to the effective entries
2023
2024
2025 if (opt.Contains("d")) {
2026 TVirtualPad::TContext ctxt(gROOT->GetSelectedPad(), true, true);
2027 opt.Remove(opt.First("d"),1);
2028 if (!gPad || !gPad->FindObject(h1)) {
2029 h1->Draw(opt);
2030 } else {
2031 h1->Paint(opt);
2032 }
2033 }
2034 return h1;
2035}
2036
2037
2038////////////////////////////////////////////////////////////////////////////////
2039/// Project a 2-D histogram into a profile histogram along X (integration along Y).
2040///
2041/// The projection is made from summing the channels along the Y axis
2042/// ranging from firstybin to lastybin included.
2043/// By default, bins 1 to ny are included
2044/// When all bins are included, the number of entries in the projection
2045/// is set to the number of entries of the 2-D histogram, otherwise
2046/// the number of entries is incremented by 1 for all non empty cells.
2047///
2048/// if option "d" is specified, the profile is drawn in the current pad.
2049///
2050/// if option "o" original axis range of the target axes will be
2051/// kept, but only bins inside the selected range will be filled.
2052///
2053/// if option "width" is specified, each bin content is multiplied
2054/// by its Y bin-width during projection
2055///
2056/// The option can also be used to specify the projected profile error type.
2057/// Values which can be used are 's', 'i', or 'g'. See TProfile::BuildOptions for details
2058///
2059/// Using a TCutG object, it is possible to select a sub-range of a 2-D histogram.
2060/// One must create a graphical cut (mouse or C++) and specify the name
2061/// of the cut between [] in the option.
2062/// For example, with a TCutG named "cutg", one can call:
2063/// myhist->ProfileX(" ",firstybin,lastybin,"[cutg]");
2064/// To invert the cut, it is enough to put a "-" in front of its name:
2065/// myhist->ProfileX(" ",firstybin,lastybin,"[-cutg]");
2066/// It is possible to apply several cuts ("," means logical AND):
2067/// myhist->ProfileX(" ",firstybin,lastybin,"[cutg1,cutg2]");
2068///
2069/// NOTE that if a TProfile named "name" exists in the current directory or pad with
2070/// a compatible axis the profile is reset and filled again with the projected contents of the TH2.
2071/// In the case of axis incompatibility an error is reported and a NULL pointer is returned.
2072///
2073/// NOTE that the X axis attributes of the TH2 are copied to the X axis of the profile.
2074///
2075/// NOTE that the default under- / overflow behavior differs from what ProjectionX
2076/// does! Profiles take the bin center into account, so here the under- and overflow
2077/// bins are ignored by default.
2078///
2079/// NOTE that the return profile histogram is computed using the Y bin center values instead of
2080/// the real Y values which are used to fill the 2d histogram. Therefore the obtained profile is just an approximation of the
2081/// correct profile histogram that would be obtained when filling it directly with the original data (see ROOT-7770)
2082
2083
2085{
2086 return DoProfile(true, name, firstybin, lastybin, option);
2087
2088}
2089
2090
2091////////////////////////////////////////////////////////////////////////////////
2092/// Project a 2-D histogram into a profile histogram along Y (integration along X).
2093///
2094/// The projection is made from summing the channels along the X axis
2095/// ranging from firstxbin to lastxbin included.
2096/// By default, bins 1 to nx are included
2097/// When all bins are included, the number of entries in the projection
2098/// is set to the number of entries of the 2-D histogram, otherwise
2099/// the number of entries is incremented by 1 for all non empty cells.
2100///
2101/// if option "d" is specified, the profile is drawn in the current pad.
2102///
2103/// if option "o" , the original axis range of the target axis will be
2104/// kept, but only bins inside the selected range will be filled.
2105///
2106/// if option "width" is specified, each bin content is multiplied
2107/// by its X bin-width during projection
2108///
2109/// The option can also be used to specify the projected profile error type.
2110/// Values which can be used are 's', 'i', or 'g'. See TProfile::BuildOptions for details
2111/// Using a TCutG object, it is possible to select a sub-range of a 2-D histogram.
2112///
2113/// One must create a graphical cut (mouse or C++) and specify the name
2114/// of the cut between [] in the option.
2115/// For example, with a TCutG named "cutg", one can call:
2116/// myhist->ProfileY(" ",firstybin,lastybin,"[cutg]");
2117/// To invert the cut, it is enough to put a "-" in front of its name:
2118/// myhist->ProfileY(" ",firstybin,lastybin,"[-cutg]");
2119/// It is possible to apply several cuts:
2120/// myhist->ProfileY(" ",firstybin,lastybin,"[cutg1,cutg2]");
2121///
2122/// NOTE that if a TProfile named "name" exists in the current directory or pad with
2123/// a compatible axis the profile is reset and filled again with the projected contents of the TH2.
2124/// In the case of axis incompatibility an error is reported and a NULL pointer is returned.
2125///
2126/// NOTE that the Y axis attributes of the TH2 are copied to the X axis of the profile.
2127///
2128/// NOTE that the default under- / overflow behavior differs from what ProjectionX
2129/// does! Profiles take the bin center into account, so here the under- and overflow
2130/// bins are ignored by default.
2131///
2132/// NOTE that the return profile histogram is computed using the X bin center values instead of
2133/// the real X values which are used to fill the 2d histogram. Therefore the obtained profile is just an approximation of the
2134/// correct profile histogram that would be obtained when filling it directly with the original data (see ROOT-7770)
2135
2136
2138{
2139 return DoProfile(false, name, firstxbin, lastxbin, option);
2140}
2141
2142////////////////////////////////////////////////////////////////////////////////
2143/// Internal (protected) method for performing projection on the X or Y axis
2144/// called by ProjectionX or ProjectionY.
2145/// The histograms created are added to gDirectory.
2146
2148{
2149 const char *expectedName = nullptr;
2150 Int_t inNbin;
2151 const TAxis* outAxis;
2152 const TAxis* inAxis;
2153
2154 TString opt = option;
2155 TString cut;
2156 Int_t i1 = opt.Index("[");
2157 if (i1>=0) {
2158 Int_t i2 = opt.Index("]");
2159 cut = opt(i1,i2-i1+1);
2160 }
2161 opt.ToLower(); //must be called after having parsed the cut name
2162 bool originalRange = opt.Contains("o");
2163 bool useWidth = opt.Contains("width");
2164
2165 if ( onX )
2166 {
2167 expectedName = "_px";
2169 outAxis = GetXaxis();
2170 inAxis = GetYaxis();
2171 }
2172 else
2173 {
2174 expectedName = "_py";
2176 outAxis = GetYaxis();
2177 inAxis = GetXaxis();
2178 }
2179
2180 // outer axis cannot be outside original axis (this fixes ROOT-8781)
2181 // and firstOutBin and lastOutBin cannot be both equal to zero
2182 Int_t firstOutBin = std::max(outAxis->GetFirst(),1);
2183 Int_t lastOutBin = std::min(outAxis->GetLast(),outAxis->GetNbins() ) ;
2184
2186 firstbin = inAxis->GetFirst();
2187 lastbin = inAxis->GetLast();
2188 // For special case of TAxis::SetRange, when first == 1 and last
2189 // = N and the range bit has been set, the TAxis will return 0
2190 // for both.
2191 if (firstbin == 0 && lastbin == 0)
2192 {
2193 firstbin = 1;
2194 lastbin = inAxis->GetNbins();
2195 }
2196 }
2197 if (firstbin < 0) firstbin = 0;
2198 if (lastbin < 0) lastbin = inNbin + 1;
2199 if (lastbin > inNbin+1) lastbin = inNbin + 1;
2200
2201 // Create the projection histogram
2202 char *pname = (char*)name;
2203 if (name && strcmp(name,expectedName) == 0) {
2204 Int_t nch = strlen(GetName()) + 4;
2205 pname = new char[nch];
2206 snprintf(pname,nch,"%s%s",GetName(),name);
2207 }
2208 TH1D *h1=nullptr;
2209 //check if histogram with identical name exist
2210 // if compatible reset and re-use previous histogram
2211 // (see https://savannah.cern.ch/bugs/?54340)
2212 TObject *h1obj = gROOT->FindObject(pname);
2213 if (h1obj && h1obj->InheritsFrom(TH1::Class())) {
2214 if (h1obj->IsA() != TH1D::Class() ) {
2215 Error("DoProjection","Histogram with name %s must be a TH1D and is a %s",name,h1obj->ClassName());
2216 return nullptr;
2217 }
2218 h1 = (TH1D*)h1obj;
2219 // reset the existing histogram and set always the new binning for the axis
2220 // This avoid problems when the histogram already exists and the histograms is rebinned or its range has changed
2221 // (see https://savannah.cern.ch/bugs/?94101 or https://savannah.cern.ch/bugs/?95808 )
2222 h1->Reset();
2223 const TArrayD *xbins = outAxis->GetXbins();
2224 if (xbins->fN == 0) {
2225 if ( originalRange )
2226 h1->SetBins(outAxis->GetNbins(),outAxis->GetXmin(),outAxis->GetXmax());
2227 else
2228 h1->SetBins(lastOutBin-firstOutBin+1,outAxis->GetBinLowEdge(firstOutBin),outAxis->GetBinUpEdge(lastOutBin));
2229 } else {
2230 // case variable bins
2231 if (originalRange )
2232 h1->SetBins(outAxis->GetNbins(),xbins->fArray);
2233 else
2234 h1->SetBins(lastOutBin-firstOutBin+1,&xbins->fArray[firstOutBin-1]);
2235 }
2236 }
2237
2238 Int_t ncuts = 0;
2239 if (opt.Contains("[")) {
2240 ((TH2 *)this)->GetPainter();
2241 if (fPainter) ncuts = fPainter->MakeCuts((char*)cut.Data());
2242 }
2243
2244 if (!h1) {
2245 const TArrayD *bins = outAxis->GetXbins();
2246 if (bins->fN == 0) {
2247 if ( originalRange )
2248 h1 = new TH1D(pname,GetTitle(),outAxis->GetNbins(),outAxis->GetXmin(),outAxis->GetXmax());
2249 else
2251 outAxis->GetBinLowEdge(firstOutBin),outAxis->GetBinUpEdge(lastOutBin));
2252 } else {
2253 // case variable bins
2254 if (originalRange )
2255 h1 = new TH1D(pname,GetTitle(),outAxis->GetNbins(),bins->fArray);
2256 else
2258 }
2260 if (opt.Contains("e") || GetSumw2N() ) h1->Sumw2();
2261 }
2262 if (pname != name) delete [] pname;
2263
2264 // Copy the axis attributes and the axis labels if needed.
2266 THashList* labels=outAxis->GetLabels();
2267 if (labels) {
2268 TIter iL(labels);
2269 TObjString* lb;
2270 Int_t i = 1;
2271 while ((lb=(TObjString*)iL())) {
2272 h1->GetXaxis()->SetBinLabel(i,lb->String().Data());
2273 i++;
2274 }
2275 }
2276
2277 h1->SetLineColor(this->GetLineColor());
2278 h1->SetFillColor(this->GetFillColor());
2279 h1->SetMarkerColor(this->GetMarkerColor());
2280 h1->SetMarkerStyle(this->GetMarkerStyle());
2281
2282 // Fill the projected histogram
2284 Double_t totcont = 0;
2286
2287 // implement filling of projected histogram
2288 // outbin is bin number of outAxis (the projected axis). Loop is done on all bin of TH2 histograms
2289 // inbin is the axis being integrated. Loop is done only on the selected bins
2290 // if the out axis has labels and is extendable, temporary make it non-extendable to avoid adding extra bins
2291 Bool_t extendable = outAxis->CanExtend();
2292 if ( labels && extendable ) h1->GetXaxis()->SetCanExtend(kFALSE);
2293 for ( Int_t outbin = 0; outbin <= outAxis->GetNbins() + 1; ++outbin) {
2294 err2 = 0;
2295 cont = 0;
2297
2298 for (Int_t inbin = firstbin ; inbin <= lastbin ; ++inbin) {
2299 Int_t binx, biny;
2300 if (onX) { binx = outbin; biny=inbin; }
2301 else { binx = inbin; biny=outbin; }
2302
2303 if (ncuts) {
2304 if (!fPainter->IsInside(binx,biny)) continue;
2305 }
2306 // sum bin content and error if needed
2307 double step = useWidth ? inAxis->GetBinWidth(inbin) : 1;
2308 cont += GetBinContent(binx,biny)*step;
2309 if (computeErrors) {
2311 err2 += exy*exy;
2312 }
2313 }
2314 // find corresponding bin number in h1 for outbin
2315 Int_t binOut = h1->GetXaxis()->FindBin( outAxis->GetBinCenter(outbin) );
2318 // sum all content
2319 totcont += cont;
2320 }
2321 if ( labels ) h1->GetXaxis()->SetCanExtend(extendable);
2322
2323 // check if we can re-use the original statistics from the previous histogram
2324 bool reuseStats = false;
2325 if ( ( GetStatOverflowsBehaviour() == false && firstbin == 1 && lastbin == inNbin ) ||
2326 ( GetStatOverflowsBehaviour() == true && firstbin == 0 && lastbin == inNbin + 1 ) )
2327 reuseStats = true;
2328 else {
2329 // also if total content match we can re-use
2330 double eps = 1.E-12;
2331 if (IsA() == TH2F::Class() ) eps = 1.E-6;
2332 if (fTsumw != 0 && TMath::Abs( fTsumw - totcont) < TMath::Abs(fTsumw) * eps)
2333 reuseStats = true;
2334 }
2335 if (ncuts) reuseStats = false;
2336 // retrieve the statistics and set in projected histogram if we can re-use it
2337 bool reuseEntries = reuseStats;
2338 // can re-use entries if underflow/overflow are included
2339 reuseEntries &= (firstbin==0 && lastbin == inNbin+1);
2340 if (reuseStats) {
2341 Double_t stats[kNstat];
2342 GetStats(stats);
2343 if (!onX) { // case of projection on Y
2344 stats[2] = stats[4];
2345 stats[3] = stats[5];
2346 }
2347 h1->PutStats(stats);
2348 }
2349 else {
2350 // the statistics is automatically recalculated since it is reset by the call to SetBinContent
2351 // we just need to set the entries since they have not been correctly calculated during the projection
2352 // we can only set them to the effective entries
2354 }
2355 if (reuseEntries) {
2357 }
2358 else {
2359 // re-compute the entries
2360 // in case of error calculation (i.e. when Sumw2() is set)
2361 // use the effective entries for the entries
2362 // since this is the only way to estimate them
2363 Double_t entries = TMath::Floor( totcont + 0.5); // to avoid numerical rounding
2364 if (h1->GetSumw2N()) entries = h1->GetEffectiveEntries();
2365 h1->SetEntries( entries );
2366 }
2367
2368 if (opt.Contains("d")) {
2369 TVirtualPad::TContext ctxt(gROOT->GetSelectedPad(), true, true);
2370 opt.Remove(opt.First("d"),1);
2371 // remove also other options
2372 if (opt.Contains("e")) opt.Remove(opt.First("e"),1);
2373 if (!gPad || !gPad->FindObject(h1)) {
2374 h1->Draw(opt);
2375 } else {
2376 h1->Paint(opt);
2377 }
2378 }
2379
2380 return h1;
2381}
2382
2383
2384////////////////////////////////////////////////////////////////////////////////
2385/// Project a 2-D histogram into a 1-D histogram along X (integration along Y).
2386///
2387/// The projection is always of the type TH1D.
2388/// The projection is made from summing the channels along the Y axis
2389/// ranging from firstybin to lastybin included.
2390/// By default, all bins including under- and overflow are included.
2391/// The number of entries in the projection is estimated from the
2392/// number of effective entries for all the cells included in the projection.
2393///
2394/// To exclude the underflow bins in Y, use firstybin=1.
2395/// To exclude the overflow bins in Y, use lastybin=nx.
2396///
2397/// if option "e" is specified, the errors are computed.
2398/// if option "d" is specified, the projection is drawn in the current pad.
2399/// if option "o" original axis range of the target axes will be
2400/// kept, but only bins inside the selected range will be filled.
2401///
2402/// if option "width" is specified, each bin content is multiplied
2403/// by its Y bin-width during projection
2404///
2405/// Using a TCutG object, it is possible to select a sub-range of a 2-D histogram.
2406/// One must create a graphical cut (mouse or C++) and specify the name
2407/// of the cut between [] in the option.
2408/// For example, with a TCutG named "cutg", one can call:
2409/// myhist->ProjectionX(" ",firstybin,lastybin,"[cutg]");
2410/// To invert the cut, it is enough to put a "-" in front of its name:
2411/// myhist->ProjectionX(" ",firstybin,lastybin,"[-cutg]");
2412/// It is possible to apply several cuts:
2413/// myhist->ProjectionX(" ",firstybin,lastybin,"[cutg1,cutg2]");
2414///
2415/// NOTE that if a TH1D named "name" exists in the current directory or pad
2416/// the histogram is reset and filled again with the projected contents of the TH2.
2417///
2418/// NOTE that the X axis attributes of the TH2 are copied to the X axis of the projection.
2419
2421{
2422 return DoProjection(true, name, firstybin, lastybin, option);
2423}
2424
2425
2426////////////////////////////////////////////////////////////////////////////////
2427/// Project a 2-D histogram into a 1-D histogram along Y (integration along X).
2428///
2429/// The projection is always of the type TH1D.
2430/// The projection is made from summing the channels along the X axis
2431/// ranging from firstxbin to lastxbin included.
2432/// By default, all bins including under- and overflow are included.
2433/// The number of entries in the projection is estimated from the
2434/// number of effective entries for all the cells included in the projection
2435///
2436/// To exclude the underflow bins in X, use firstxbin=1.
2437/// To exclude the overflow bins in X, use lastxbin=nx.
2438///
2439/// if option "e" is specified, the errors are computed.
2440/// if option "d" is specified, the projection is drawn in the current pad.
2441/// if option "o" original axis range of the target axes will be
2442/// kept, but only bins inside the selected range will be filled.
2443///
2444/// if option "width" is specified, each bin content is multiplied
2445/// by its X bin-width during projection
2446///
2447/// Using a TCutG object, it is possible to select a sub-range of a 2-D histogram.
2448/// One must create a graphical cut (mouse or C++) and specify the name
2449/// of the cut between [] in the option.
2450/// For example, with a TCutG named "cutg", one can call:
2451/// myhist->ProjectionY(" ",firstxbin,lastxbin,"[cutg]");
2452/// To invert the cut, it is enough to put a "-" in front of its name:
2453/// myhist->ProjectionY(" ",firstxbin,lastxbin,"[-cutg]");
2454/// It is possible to apply several cuts:
2455/// myhist->ProjectionY(" ",firstxbin,lastxbin,"[cutg1,cutg2]");
2456///
2457/// NOTE that if a TH1D named "name" exists in the current directory or pad and having
2458/// a compatible axis, the histogram is reset and filled again with the projected contents of the TH2.
2459/// In the case of axis incompatibility, an error is reported and a NULL pointer is returned.
2460///
2461/// NOTE that the Y axis attributes of the TH2 are copied to the X axis of the projection.
2462
2464{
2465 return DoProjection(false, name, firstxbin, lastxbin, option);
2466}
2467
2468
2469////////////////////////////////////////////////////////////////////////////////
2470/// Replace current statistics with the values in array stats
2471
2473{
2474 TH1::PutStats(stats);
2475 fTsumwy = stats[4];
2476 fTsumwy2 = stats[5];
2477 fTsumwxy = stats[6];
2478}
2479
2480
2481////////////////////////////////////////////////////////////////////////////////
2482/// Compute the X distribution of quantiles in the other variable Y
2483/// name is the name of the returned histogram
2484/// prob is the probability content for the quantile (0.5 is the default for the median)
2485/// An approximate error for the quantile is computed assuming that the distribution in
2486/// the other variable is normal. According to this approximate formula the error on the quantile is
2487/// estimated as sqrt( p (1-p) / ( n * f(q)^2) ), where p is the probability content of the quantile and
2488/// n is the number of events used to compute the quantile and f(q) is the probability distribution for the
2489/// other variable evaluated at the obtained quantile. In the error estimation the probability is then assumed to be
2490/// a normal distribution.
2491
2492TH1D* TH2::QuantilesX( Double_t prob, const char * name) const
2493{
2494 return DoQuantiles(true, name, prob);
2495}
2496
2497
2498////////////////////////////////////////////////////////////////////////////////
2499/// Compute the Y distribution of quantiles in the other variable X
2500/// name is the name of the returned histogram
2501/// prob is the probability content for the quantile (0.5 is the default for the median)
2502/// An approximate error for the quantile is computed assuming that the distribution in
2503/// the other variable is normal.
2504
2505TH1D* TH2::QuantilesY( Double_t prob, const char * name) const
2506{
2507 return DoQuantiles(false, name, prob);
2508}
2509
2510
2511////////////////////////////////////////////////////////////////////////////////
2512/// Implementation of quantiles for x or y
2513
2514TH1D* TH2::DoQuantiles(bool onX, const char * name, Double_t prob) const
2515{
2516 const TAxis *outAxis = nullptr;
2517 if ( onX ) {
2518 outAxis = GetXaxis();
2519 } else {
2520 outAxis = GetYaxis();
2521 }
2522
2523 // build first name of returned histogram
2524 TString qname = name;
2525 if (qname.IsNull() || qname == "_qx" || qname == "_qy") {
2526 const char * qtype = (onX) ? "qx" : "qy";
2527 qname = TString::Format("%s_%s_%3.2f",GetName(),qtype, prob);
2528 }
2529 // check if the histogram is already existing
2530 TH1D *h1=nullptr;
2531 //check if histogram with identical name exist
2532 TObject *h1obj = gROOT->FindObject(qname);
2533 if (h1obj) {
2534 h1 = dynamic_cast<TH1D*>(h1obj);
2535 if (!h1) {
2536 Error("DoQuantiles","Histogram with name %s must be a TH1D and is a %s",qname.Data(),h1obj->ClassName());
2537 return nullptr;
2538 }
2539 }
2540 if (h1) {
2541 h1->Reset();
2542 } else {
2543 // create the histogram
2544 h1 = new TH1D(qname, GetTitle(), 1, 0, 1);
2545 }
2546 // set the bin content
2547 Int_t firstOutBin = std::max(outAxis->GetFirst(),1);
2548 Int_t lastOutBin = std::max(outAxis->GetLast(),outAxis->GetNbins());
2549 const TArrayD *xbins = outAxis->GetXbins();
2550 if (xbins->fN == 0)
2551 h1->SetBins(lastOutBin-firstOutBin+1,outAxis->GetBinLowEdge(firstOutBin),outAxis->GetBinUpEdge(lastOutBin));
2552 else
2553 h1->SetBins(lastOutBin-firstOutBin+1,&xbins->fArray[firstOutBin-1]);
2554
2555 // set the bin content of the histogram
2556 Double_t pp[1];
2557 pp[0] = prob;
2558
2559 TH1D * slice = nullptr;
2560 for (int ibin = outAxis->GetFirst() ; ibin <= outAxis->GetLast() ; ++ibin) {
2561 Double_t qq[1];
2562 // do a projection on the opposite axis
2563 slice = DoProjection(!onX, "tmp",ibin,ibin,"");
2564 if (!slice) break;
2565 if (slice->GetSum() == 0) continue;
2566 slice->GetQuantiles(1,qq,pp);
2567 h1->SetBinContent(ibin,qq[0]);
2568 // compute error using normal approximation
2569 // quantile error ~ sqrt (q*(1-q)/ *( n * f(xq)^2 ) from Kendall
2570 // where f(xq) is the p.d.f value at the quantile xq
2571 Double_t n = slice->GetEffectiveEntries();
2572 Double_t f = TMath::Gaus(qq[0], slice->GetMean(), slice->GetStdDev(), kTRUE);
2573 Double_t error = 0;
2574 // set the errors to zero in case of small statistics
2575 if (f > 0 && n > 1)
2576 error = TMath::Sqrt( prob*(1.-prob)/ (n * f * f) );
2577 h1->SetBinError(ibin, error);
2578 }
2579 if (slice) delete slice;
2580 return h1;
2581}
2582
2583
2584////////////////////////////////////////////////////////////////////////////////
2585/// Reset this histogram: contents, errors, etc.
2586
2588{
2590 TString opt = option;
2591 opt.ToUpper();
2592
2593 if (opt.Contains("ICE") && !opt.Contains("S")) return;
2594 fTsumwy = 0;
2595 fTsumwy2 = 0;
2596 fTsumwxy = 0;
2597}
2598
2599
2600////////////////////////////////////////////////////////////////////////////////
2601/// Set bin content
2602
2604{
2605 fEntries++;
2606 fTsumw = 0;
2607 if (bin < 0) return;
2608 if (bin >= fNcells) return;
2610}
2611
2612
2613////////////////////////////////////////////////////////////////////////////////
2614/// When the mouse is moved in a pad containing a 2-d view of this histogram
2615/// a second canvas shows the projection along X corresponding to the
2616/// mouse position along Y.
2617/// To stop the generation of the projections, delete the canvas
2618/// containing the projection.
2619/// \param nbins number of bins in Y to sum across for the projection
2620
2622{
2623 GetPainter();
2624
2625 if (fPainter) fPainter->SetShowProjection("x",nbins);
2626}
2627
2628
2629////////////////////////////////////////////////////////////////////////////////
2630/// When the mouse is moved in a pad containing a 2-d view of this histogram
2631/// a second canvas shows the projection along Y corresponding to the
2632/// mouse position along X.
2633/// To stop the generation of the projections, delete the canvas
2634/// containing the projection.
2635/// \param nbins number of bins in X to sum across for the projection
2636
2638{
2639 GetPainter();
2640
2641 if (fPainter) fPainter->SetShowProjection("y",nbins);
2642}
2643
2644
2645////////////////////////////////////////////////////////////////////////////////
2646/// When the mouse is moved in a pad containing a 2-d view of this histogram
2647/// two canvases show the projection along X and Y corresponding to the
2648/// mouse position along Y and X, respectively.
2649/// To stop the generation of the projections, delete the canvas
2650/// containing the projection.
2651/// \param nbinsY number of bins in Y to sum across for the x projection
2652/// \param nbinsX number of bins in X to sum across for the y projection
2653
2659
2660
2661////////////////////////////////////////////////////////////////////////////////
2662/// This function calculates the background spectrum in this histogram.
2663/// The background is returned as a histogram.
2664
2666{
2667
2668 return (TH1 *)gROOT->ProcessLineFast(
2669 TString::Format("TSpectrum2::StaticBackground((TH1*)0x%zx,%d,%d,\"%s\")", (size_t)this, nIterX, nIterY, option)
2670 .Data());
2671}
2672
2673
2674////////////////////////////////////////////////////////////////////////////////
2675///Interface to TSpectrum2::Search
2676///the function finds peaks in this histogram where the width is > sigma
2677///and the peak maximum greater than threshold*maximum bin content of this.
2678///for more details see TSpectrum::Search.
2679///note the difference in the default value for option compared to TSpectrum2::Search
2680///option="" by default (instead of "goff")
2681
2683{
2684
2685 return (Int_t)gROOT->ProcessLineFast(TString::Format("TSpectrum2::StaticSearch((TH1*)0x%zx,%g,\"%s\",%g)",
2686 (size_t)this, sigma, option, threshold).Data());
2687}
2688
2689
2690////////////////////////////////////////////////////////////////////////////////
2691/// Smooth bin contents of this 2-d histogram using kernel algorithms
2692/// similar to the ones used in the raster graphics community.
2693/// Bin contents in the active range are replaced by their smooth values.
2694/// The algorithm retains the input dimension by using Kernel Crop at the input boundaries.
2695/// Kernel Crop sets any pixel in the kernel that extends past the input to zero and adjusts the
2696/// normalization accordingly.
2697/// If Errors are defined via Sumw2, they are also scaled and computed.
2698/// However, note the resulting errors will be correlated between different-bins, so
2699/// the errors should not be used blindly to perform any calculation involving several bins,
2700/// like fitting the histogram. One would need to compute also the bin by bin correlation matrix.
2701///
2702/// 3 kernels are proposed k5a, k5b and k3a.
2703/// k5a and k5b act on 5x5 cells (i-2,i-1,i,i+1,i+2, and same for j)
2704/// k5b is a bit more stronger in smoothing
2705/// k3a acts only on 3x3 cells (i-1,i,i+1, and same for j).
2706/// By default the kernel "k5a" is used. You can select the kernels "k5b" or "k3a"
2707/// via the option argument.
2708/// If TAxis::SetRange has been called on the x or/and y axis, only the bins
2709/// in the specified range are smoothed.
2710/// In the current implementation if the first argument is not used (default value=1).
2711///
2712/// implementation by David McKee (dmckee@bama.ua.edu). Extended by Rene Brun
2713
2715{
2716 Double_t k5a[5][5] = { { 0, 0, 1, 0, 0 },
2717 { 0, 2, 2, 2, 0 },
2718 { 1, 2, 5, 2, 1 },
2719 { 0, 2, 2, 2, 0 },
2720 { 0, 0, 1, 0, 0 } };
2721 Double_t k5b[5][5] = { { 0, 1, 2, 1, 0 },
2722 { 1, 2, 4, 2, 1 },
2723 { 2, 4, 8, 4, 2 },
2724 { 1, 2, 4, 2, 1 },
2725 { 0, 1, 2, 1, 0 } };
2726 Double_t k3a[3][3] = { { 0, 1, 0 },
2727 { 1, 2, 1 },
2728 { 0, 1, 0 } };
2729
2730 if (ntimes > 1) {
2731 Warning("Smooth","Currently only ntimes=1 is supported");
2732 }
2733 TString opt = option;
2734 opt.ToLower();
2735 Int_t ksize_x=5;
2736 Int_t ksize_y=5;
2737 Double_t *kernel = &k5a[0][0];
2738 if (opt.Contains("k5b")) kernel = &k5b[0][0];
2739 if (opt.Contains("k3a")) {
2740 kernel = &k3a[0][0];
2741 ksize_x=3;
2742 ksize_y=3;
2743 }
2744
2745 // find i,j ranges
2750
2751 // Determine the size of the bin buffer(s) needed
2753 Int_t nx = GetNbinsX();
2754 Int_t ny = GetNbinsY();
2755 Int_t bufSize = (nx+2)*(ny+2);
2756 Double_t *buf = new Double_t[bufSize];
2757 Double_t *ebuf = nullptr;
2758 if (fSumw2.fN) ebuf = new Double_t[bufSize];
2759
2760 // Copy all the data to the temporary buffers
2761 Int_t i,j,bin;
2762 for (i=ifirst; i<=ilast; i++){
2763 for (j=jfirst; j<=jlast; j++){
2764 bin = GetBin(i,j);
2765 buf[bin] = RetrieveBinContent(bin);
2766 if (ebuf) ebuf[bin]=GetBinError(bin);
2767 }
2768 }
2769
2770 // Kernel tail sizes (kernel sizes must be odd for this to work!)
2771 Int_t x_push = (ksize_x-1)/2;
2772 Int_t y_push = (ksize_y-1)/2;
2773
2774 // main work loop
2775 for (i=ifirst; i<=ilast; i++){
2776 for (j=jfirst; j<=jlast; j++) {
2777 Double_t content = 0.0;
2778 Double_t error = 0.0;
2779 Double_t norm = 0.0;
2780
2781 for (Int_t n=0; n<ksize_x; n++) {
2782 for (Int_t m=0; m<ksize_y; m++) {
2783 Int_t xb = i+(n-x_push);
2784 Int_t yb = j+(m-y_push);
2785 if ( (xb >= 1) && (xb <= nx) && (yb >= 1) && (yb <= ny) ) {
2786 bin = GetBin(xb,yb);
2787 Double_t k = kernel[n*ksize_y +m];
2788 //if ( (k != 0.0 ) && (buf[bin] != 0.0) ) { // General version probably does not want the second condition
2789 if ( k != 0.0 ) {
2790 norm += k;
2791 content += k*buf[bin];
2792 if (ebuf) error += k*k*ebuf[bin]*ebuf[bin];
2793 }
2794 }
2795 }
2796 }
2797
2798 if ( norm != 0.0 ) {
2800 if (ebuf) {
2801 error /= (norm*norm);
2802 SetBinError(i,j,sqrt(error));
2803 }
2804 }
2805 }
2806 }
2808
2809 delete [] buf;
2810 delete [] ebuf;
2811}
2812
2813
2814////////////////////////////////////////////////////////////////////////////////
2815/// Stream an object of class TH2.
2816
2818{
2819 if (R__b.IsReading()) {
2820 UInt_t R__s, R__c;
2821 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
2822 if (R__v > 2) {
2823 R__b.ReadClassBuffer(TH2::Class(), this, R__v, R__s, R__c);
2824 return;
2825 }
2826 //====process old versions before automatic schema evolution
2828 R__b >> fScalefactor;
2829 R__b >> fTsumwy;
2830 R__b >> fTsumwy2;
2831 R__b >> fTsumwxy;
2832 //====end of old versions
2833
2834 } else {
2835 R__b.WriteClassBuffer(TH2::Class(),this);
2836 }
2837}
2838
2839
2840//______________________________________________________________________________
2841// TH2C methods
2842// TH2C a 2-D histogram with one byte per cell (char)
2843//______________________________________________________________________________
2844
2845
2846
2847////////////////////////////////////////////////////////////////////////////////
2848/// Constructor.
2849
2851{
2852 SetBinsLength(9);
2853 if (fgDefaultSumw2) Sumw2();
2854}
2855
2856
2857////////////////////////////////////////////////////////////////////////////////
2858/// Destructor.
2859
2861
2862
2863////////////////////////////////////////////////////////////////////////////////
2864/// Constructor
2865/// (see TH2::TH2 for explanation of parameters)
2866
2867TH2C::TH2C(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_t xup
2868 ,Int_t nbinsy,Double_t ylow,Double_t yup)
2869 :TH2(name,title,nbinsx,xlow,xup,nbinsy,ylow,yup)
2870{
2872 if (fgDefaultSumw2) Sumw2();
2873
2874 if (xlow >= xup || ylow >= yup) SetBuffer(fgBufferSize);
2875}
2876
2877
2878////////////////////////////////////////////////////////////////////////////////
2879/// Constructor
2880/// (see TH2::TH2 for explanation of parameters)
2881
2882TH2C::TH2C(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
2883 ,Int_t nbinsy,Double_t ylow,Double_t yup)
2884 :TH2(name,title,nbinsx,xbins,nbinsy,ylow,yup)
2885{
2887 if (fgDefaultSumw2) Sumw2();
2888}
2889
2890
2891////////////////////////////////////////////////////////////////////////////////
2892/// Constructor
2893/// (see TH2::TH2 for explanation of parameters)
2894
2895TH2C::TH2C(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_t xup
2896 ,Int_t nbinsy,const Double_t *ybins)
2897 :TH2(name,title,nbinsx,xlow,xup,nbinsy,ybins)
2898{
2900 if (fgDefaultSumw2) Sumw2();
2901}
2902
2903
2904////////////////////////////////////////////////////////////////////////////////
2905/// Constructor
2906/// (see TH2::TH2 for explanation of parameters)
2907
2908TH2C::TH2C(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
2909 ,Int_t nbinsy,const Double_t *ybins)
2910 :TH2(name,title,nbinsx,xbins,nbinsy,ybins)
2911{
2913 if (fgDefaultSumw2) Sumw2();
2914}
2915
2916
2917////////////////////////////////////////////////////////////////////////////////
2918/// Constructor
2919/// (see TH2::TH2 for explanation of parameters)
2920
2921TH2C::TH2C(const char *name,const char *title,Int_t nbinsx,const Float_t *xbins
2922 ,Int_t nbinsy,const Float_t *ybins)
2923 :TH2(name,title,nbinsx,xbins,nbinsy,ybins)
2924{
2926 if (fgDefaultSumw2) Sumw2();
2927}
2928
2929
2930////////////////////////////////////////////////////////////////////////////////
2931/// Copy constructor.
2932/// The list of functions is not copied. (Use Clone() if needed)
2933
2935{
2936 h2c.TH2C::Copy(*this);
2937}
2938
2939
2940////////////////////////////////////////////////////////////////////////////////
2941/// Increment bin content by 1.
2942/// Passing an out-of-range bin leads to undefined behavior
2943
2945{
2946 if (fArray[bin] < 127) fArray[bin]++;
2947}
2948
2949
2950////////////////////////////////////////////////////////////////////////////////
2951/// Increment bin content by w.
2952/// \warning The value of w is cast to `Int_t` before being added.
2953/// Passing an out-of-range bin leads to undefined behavior
2954
2956{
2957 Int_t newval = fArray[bin] + Int_t(w);
2958 if (newval > -128 && newval < 128) {fArray[bin] = Char_t(newval); return;}
2959 if (newval < -127) fArray[bin] = -127;
2960 if (newval > 127) fArray[bin] = 127;
2961}
2962
2963
2964////////////////////////////////////////////////////////////////////////////////
2965/// Copy.
2966
2968{
2970}
2971
2972
2973////////////////////////////////////////////////////////////////////////////////
2974/// Reset this histogram: contents, errors, etc.
2975
2977{
2980}
2981
2982
2983////////////////////////////////////////////////////////////////////////////////
2984/// Set total number of bins including under/overflow
2985/// Reallocate bin contents array
2986
2988{
2989 if (n < 0) n = (fXaxis.GetNbins()+2)*(fYaxis.GetNbins()+2);
2990 fNcells = n;
2991 TArrayC::Set(n);
2992}
2993
2994
2995////////////////////////////////////////////////////////////////////////////////
2996/// Stream an object of class TH2C.
2997
2999{
3000 if (R__b.IsReading()) {
3001 UInt_t R__s, R__c;
3002 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
3003 if (R__v > 2) {
3004 R__b.ReadClassBuffer(TH2C::Class(), this, R__v, R__s, R__c);
3005 return;
3006 }
3007 //====process old versions before automatic schema evolution
3008 if (R__v < 2) {
3009 R__b.ReadVersion();
3012 R__b.ReadVersion();
3013 R__b >> fScalefactor;
3014 R__b >> fTsumwy;
3015 R__b >> fTsumwy2;
3016 R__b >> fTsumwxy;
3017 } else {
3020 R__b.CheckByteCount(R__s, R__c, TH2C::IsA());
3021 }
3022 //====end of old versions
3023
3024 } else {
3025 R__b.WriteClassBuffer(TH2C::Class(),this);
3026 }
3027}
3028
3029
3030////////////////////////////////////////////////////////////////////////////////
3031/// Operator =
3032
3034{
3035 if (this != &h2c)
3036 h2c.TH2C::Copy(*this);
3037 return *this;
3038}
3039
3040
3041////////////////////////////////////////////////////////////////////////////////
3042/// Operator *
3043
3045{
3046 TH2C hnew = h1;
3047 hnew.Scale(c1);
3048 hnew.SetDirectory(nullptr);
3049 return hnew;
3050}
3051
3052
3053////////////////////////////////////////////////////////////////////////////////
3054/// Operator +
3055
3056TH2C operator+(TH2C const &h1, TH2C const &h2)
3057{
3058 TH2C hnew = h1;
3059 hnew.Add(&h2,1);
3060 hnew.SetDirectory(nullptr);
3061 return hnew;
3062}
3063
3064
3065////////////////////////////////////////////////////////////////////////////////
3066/// Operator -
3067
3068TH2C operator-(TH2C const &h1, TH2C const &h2)
3069{
3070 TH2C hnew = h1;
3071 hnew.Add(&h2,-1);
3072 hnew.SetDirectory(nullptr);
3073 return hnew;
3074}
3075
3076
3077////////////////////////////////////////////////////////////////////////////////
3078/// Operator *
3079
3080TH2C operator*(TH2C const &h1, TH2C const &h2)
3081{
3082 TH2C hnew = h1;
3083 hnew.Multiply(&h2);
3084 hnew.SetDirectory(nullptr);
3085 return hnew;
3086}
3087
3088
3089////////////////////////////////////////////////////////////////////////////////
3090/// Operator /
3091
3092TH2C operator/(TH2C const &h1, TH2C const &h2)
3093{
3094 TH2C hnew = h1;
3095 hnew.Divide(&h2);
3096 hnew.SetDirectory(nullptr);
3097 return hnew;
3098}
3099
3100
3101//______________________________________________________________________________
3102// TH2S methods
3103// TH2S a 2-D histogram with two bytes per cell (short integer)
3104//______________________________________________________________________________
3105
3106
3107
3108////////////////////////////////////////////////////////////////////////////////
3109/// Constructor.
3110
3112{
3113 SetBinsLength(9);
3114 if (fgDefaultSumw2) Sumw2();
3115}
3116
3117
3118////////////////////////////////////////////////////////////////////////////////
3119/// Destructor.
3120
3122{
3123}
3124
3125
3126////////////////////////////////////////////////////////////////////////////////
3127/// Constructor
3128/// (see TH2::TH2 for explanation of parameters)
3129
3130TH2S::TH2S(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_t xup
3131 ,Int_t nbinsy,Double_t ylow,Double_t yup)
3132 :TH2(name,title,nbinsx,xlow,xup,nbinsy,ylow,yup)
3133{
3135 if (fgDefaultSumw2) Sumw2();
3136
3137 if (xlow >= xup || ylow >= yup) SetBuffer(fgBufferSize);
3138}
3139
3140
3141////////////////////////////////////////////////////////////////////////////////
3142/// Constructor
3143/// (see TH2::TH2 for explanation of parameters)
3144
3145TH2S::TH2S(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
3146 ,Int_t nbinsy,Double_t ylow,Double_t yup)
3147 :TH2(name,title,nbinsx,xbins,nbinsy,ylow,yup)
3148{
3150 if (fgDefaultSumw2) Sumw2();
3151}
3152
3153
3154////////////////////////////////////////////////////////////////////////////////
3155/// Constructor
3156/// (see TH2::TH2 for explanation of parameters)
3157
3158TH2S::TH2S(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_t xup
3159 ,Int_t nbinsy,const Double_t *ybins)
3160 :TH2(name,title,nbinsx,xlow,xup,nbinsy,ybins)
3161{
3163 if (fgDefaultSumw2) Sumw2();
3164}
3165
3166
3167////////////////////////////////////////////////////////////////////////////////
3168/// Constructor
3169/// (see TH2::TH2 for explanation of parameters)
3170
3171TH2S::TH2S(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
3172 ,Int_t nbinsy,const Double_t *ybins)
3173 :TH2(name,title,nbinsx,xbins,nbinsy,ybins)
3174{
3176 if (fgDefaultSumw2) Sumw2();
3177}
3178
3179
3180////////////////////////////////////////////////////////////////////////////////
3181/// Constructor
3182/// (see TH2::TH2 for explanation of parameters)
3183
3184TH2S::TH2S(const char *name,const char *title,Int_t nbinsx,const Float_t *xbins
3185 ,Int_t nbinsy,const Float_t *ybins)
3186 :TH2(name,title,nbinsx,xbins,nbinsy,ybins)
3187{
3189 if (fgDefaultSumw2) Sumw2();
3190}
3191
3192
3193////////////////////////////////////////////////////////////////////////////////
3194/// Copy constructor
3195/// The list of functions is not copied. (Use Clone() if needed)
3196
3198{
3199 h2s.TH2S::Copy(*this);
3200}
3201
3202
3203////////////////////////////////////////////////////////////////////////////////
3204/// Increment bin content by 1.
3205/// Passing an out-of-range bin leads to undefined behavior
3206
3208{
3209 if (fArray[bin] < 32767) fArray[bin]++;
3210}
3211
3212
3213////////////////////////////////////////////////////////////////////////////////
3214/// Increment bin content by w.
3215/// \warning The value of w is cast to `Int_t` before being added.
3216/// Passing an out-of-range bin leads to undefined behavior
3217
3219{
3220 Int_t newval = fArray[bin] + Int_t(w);
3221 if (newval > -32768 && newval < 32768) {fArray[bin] = Short_t(newval); return;}
3222 if (newval < -32767) fArray[bin] = -32767;
3223 if (newval > 32767) fArray[bin] = 32767;
3224}
3225
3226
3227////////////////////////////////////////////////////////////////////////////////
3228/// Copy.
3229
3231{
3233}
3234
3235
3236////////////////////////////////////////////////////////////////////////////////
3237/// Reset this histogram: contents, errors, etc.
3238
3240{
3243}
3244
3245
3246////////////////////////////////////////////////////////////////////////////////
3247/// Set total number of bins including under/overflow
3248/// Reallocate bin contents array
3249
3251{
3252 if (n < 0) n = (fXaxis.GetNbins()+2)*(fYaxis.GetNbins()+2);
3253 fNcells = n;
3254 TArrayS::Set(n);
3255}
3256
3257
3258////////////////////////////////////////////////////////////////////////////////
3259/// Stream an object of class TH2S.
3260
3262{
3263 if (R__b.IsReading()) {
3264 UInt_t R__s, R__c;
3265 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
3266 if (R__v > 2) {
3267 R__b.ReadClassBuffer(TH2S::Class(), this, R__v, R__s, R__c);
3268 return;
3269 }
3270 //====process old versions before automatic schema evolution
3271 if (R__v < 2) {
3272 R__b.ReadVersion();
3275 R__b.ReadVersion();
3276 R__b >> fScalefactor;
3277 R__b >> fTsumwy;
3278 R__b >> fTsumwy2;
3279 R__b >> fTsumwxy;
3280 } else {
3283 R__b.CheckByteCount(R__s, R__c, TH2S::IsA());
3284 }
3285 //====end of old versions
3286
3287 } else {
3288 R__b.WriteClassBuffer(TH2S::Class(),this);
3289 }
3290}
3291
3292
3293////////////////////////////////////////////////////////////////////////////////
3294/// Operator =
3295
3297{
3298 if (this != &h2s)
3299 h2s.TH2S::Copy(*this);
3300 return *this;
3301}
3302
3303
3304////////////////////////////////////////////////////////////////////////////////
3305/// Operator *
3306
3308{
3309 TH2S hnew = h2s;
3310 hnew.Scale(c1);
3311 hnew.SetDirectory(nullptr);
3312 return hnew;
3313}
3314
3315
3316////////////////////////////////////////////////////////////////////////////////
3317/// Operator +
3318
3319TH2S operator+(TH2S const &h1, TH2S const &h2)
3320{
3321 TH2S hnew = h1;
3322 hnew.Add(&h2,1);
3323 hnew.SetDirectory(nullptr);
3324 return hnew;
3325}
3326
3327
3328////////////////////////////////////////////////////////////////////////////////
3329/// Operator -
3330
3331TH2S operator-(TH2S const &h1, TH2S const &h2)
3332{
3333 TH2S hnew = h1;
3334 hnew.Add(&h2,-1);
3335 hnew.SetDirectory(nullptr);
3336 return hnew;
3337}
3338
3339
3340////////////////////////////////////////////////////////////////////////////////
3341/// Operator *
3342
3343TH2S operator*(TH2S const &h1, TH2S const &h2)
3344{
3345 TH2S hnew = h1;
3346 hnew.Multiply(&h2);
3347 hnew.SetDirectory(nullptr);
3348 return hnew;
3349}
3350
3351
3352////////////////////////////////////////////////////////////////////////////////
3353/// Operator /
3354
3355TH2S operator/(TH2S const &h1, TH2S const &h2)
3356{
3357 TH2S hnew = h1;
3358 hnew.Divide(&h2);
3359 hnew.SetDirectory(nullptr);
3360 return hnew;
3361}
3362
3363
3364//______________________________________________________________________________
3365// TH2I methods
3366// TH2I a 2-D histogram with four bytes per cell (32 bit integer)
3367//______________________________________________________________________________
3368
3369
3370
3371////////////////////////////////////////////////////////////////////////////////
3372/// Constructor.
3373
3375{
3376 SetBinsLength(9);
3377 if (fgDefaultSumw2) Sumw2();
3378}
3379
3380
3381////////////////////////////////////////////////////////////////////////////////
3382/// Destructor.
3383
3385{
3386}
3387
3388
3389////////////////////////////////////////////////////////////////////////////////
3390/// Constructor
3391/// (see TH2::TH2 for explanation of parameters)
3392
3393TH2I::TH2I(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_t xup
3394 ,Int_t nbinsy,Double_t ylow,Double_t yup)
3395 :TH2(name,title,nbinsx,xlow,xup,nbinsy,ylow,yup)
3396{
3398 if (fgDefaultSumw2) Sumw2();
3399
3400 if (xlow >= xup || ylow >= yup) SetBuffer(fgBufferSize);
3401}
3402
3403
3404////////////////////////////////////////////////////////////////////////////////
3405/// Constructor
3406/// (see TH2::TH2 for explanation of parameters)
3407
3408TH2I::TH2I(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
3409 ,Int_t nbinsy,Double_t ylow,Double_t yup)
3410 :TH2(name,title,nbinsx,xbins,nbinsy,ylow,yup)
3411{
3413 if (fgDefaultSumw2) Sumw2();
3414}
3415
3416
3417////////////////////////////////////////////////////////////////////////////////
3418/// Constructor
3419/// (see TH2::TH2 for explanation of parameters)
3420
3421TH2I::TH2I(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_t xup
3422 ,Int_t nbinsy,const Double_t *ybins)
3423 :TH2(name,title,nbinsx,xlow,xup,nbinsy,ybins)
3424{
3426 if (fgDefaultSumw2) Sumw2();
3427}
3428
3429
3430////////////////////////////////////////////////////////////////////////////////
3431/// Constructor
3432/// (see TH2::TH2 for explanation of parameters)
3433
3434TH2I::TH2I(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
3435 ,Int_t nbinsy,const Double_t *ybins)
3436 :TH2(name,title,nbinsx,xbins,nbinsy,ybins)
3437{
3439 if (fgDefaultSumw2) Sumw2();
3440}
3441
3442
3443////////////////////////////////////////////////////////////////////////////////
3444/// Constructor
3445/// (see TH2::TH2 for explanation of parameters)
3446
3447TH2I::TH2I(const char *name,const char *title,Int_t nbinsx,const Float_t *xbins
3448 ,Int_t nbinsy,const Float_t *ybins)
3449 :TH2(name,title,nbinsx,xbins,nbinsy,ybins)
3450{
3452 if (fgDefaultSumw2) Sumw2();
3453}
3454
3455
3456////////////////////////////////////////////////////////////////////////////////
3457/// Copy constructor.
3458/// The list of functions is not copied. (Use Clone() if needed)
3459
3461{
3462 h2i.TH2I::Copy(*this);
3463}
3464
3465
3466////////////////////////////////////////////////////////////////////////////////
3467/// Increment bin content by 1.
3468/// Passing an out-of-range bin leads to undefined behavior
3469
3471{
3472 if (fArray[bin] < INT_MAX) fArray[bin]++;
3473}
3474
3475
3476////////////////////////////////////////////////////////////////////////////////
3477/// Increment bin content by w.
3478/// \warning The value of w is cast to `Long64_t` before being added.
3479/// Passing an out-of-range bin leads to undefined behavior
3480
3482{
3483 Long64_t newval = fArray[bin] + Long64_t(w);
3484 if (newval > -INT_MAX && newval < INT_MAX) {fArray[bin] = Int_t(newval); return;}
3485 if (newval < -INT_MAX) fArray[bin] = -INT_MAX;
3486 if (newval > INT_MAX) fArray[bin] = INT_MAX;
3487}
3488
3489
3490////////////////////////////////////////////////////////////////////////////////
3491/// Copy.
3492
3494{
3496}
3497
3498
3499////////////////////////////////////////////////////////////////////////////////
3500/// Reset this histogram: contents, errors, etc.
3501
3503{
3506}
3507
3508
3509////////////////////////////////////////////////////////////////////////////////
3510/// Set total number of bins including under/overflow
3511/// Reallocate bin contents array
3512
3514{
3515 if (n < 0) n = (fXaxis.GetNbins()+2)*(fYaxis.GetNbins()+2);
3516 fNcells = n;
3517 TArrayI::Set(n);
3518}
3519
3520
3521////////////////////////////////////////////////////////////////////////////////
3522/// Operator =
3523
3525{
3526 if (this != &h2i)
3527 h2i.TH2I::Copy(*this);
3528 return *this;
3529}
3530
3531
3532////////////////////////////////////////////////////////////////////////////////
3533/// Operator *
3534
3536{
3537 TH2I hnew = h2i;
3538 hnew.Scale(c1);
3539 hnew.SetDirectory(nullptr);
3540 return hnew;
3541}
3542
3543
3544////////////////////////////////////////////////////////////////////////////////
3545/// Operator +
3546
3547TH2I operator+(TH2I const &h1, TH2I const &h2)
3548{
3549 TH2I hnew = h1;
3550 hnew.Add(&h2,1);
3551 hnew.SetDirectory(nullptr);
3552 return hnew;
3553}
3554
3555
3556////////////////////////////////////////////////////////////////////////////////
3557/// Operator -
3558
3559TH2I operator-(TH2I const &h1, TH2I const &h2)
3560{
3561 TH2I hnew = h1;
3562 hnew.Add(&h2,-1);
3563 hnew.SetDirectory(nullptr);
3564 return hnew;
3565}
3566
3567
3568////////////////////////////////////////////////////////////////////////////////
3569/// Operator *
3570
3571TH2I operator*(TH2I const &h1, TH2I const &h2)
3572{
3573 TH2I hnew = h1;
3574 hnew.Multiply(&h2);
3575 hnew.SetDirectory(nullptr);
3576 return hnew;
3577}
3578
3579
3580////////////////////////////////////////////////////////////////////////////////
3581/// Operator /
3582
3583TH2I operator/(TH2I const &h1, TH2I const &h2)
3584{
3585 TH2I hnew = h1;
3586 hnew.Divide(&h2);
3587 hnew.SetDirectory(nullptr);
3588 return hnew;
3589}
3590
3591
3592//______________________________________________________________________________
3593// TH2L methods
3594// TH2L a 2-D histogram with eight bytes per cell (64 bit integer)
3595//______________________________________________________________________________
3596
3597
3598
3599////////////////////////////////////////////////////////////////////////////////
3600/// Constructor.
3601
3603{
3604 SetBinsLength(9);
3605 if (fgDefaultSumw2) Sumw2();
3606}
3607
3608
3609////////////////////////////////////////////////////////////////////////////////
3610/// Destructor.
3611
3613{
3614}
3615
3616
3617////////////////////////////////////////////////////////////////////////////////
3618/// Constructor
3619/// (see TH2::TH2 for explanation of parameters)
3620
3621TH2L::TH2L(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_t xup
3622 ,Int_t nbinsy,Double_t ylow,Double_t yup)
3623 :TH2(name,title,nbinsx,xlow,xup,nbinsy,ylow,yup)
3624{
3626 if (fgDefaultSumw2) Sumw2();
3627
3628 if (xlow >= xup || ylow >= yup) SetBuffer(fgBufferSize);
3629}
3630
3631
3632////////////////////////////////////////////////////////////////////////////////
3633/// Constructor
3634/// (see TH2::TH2 for explanation of parameters)
3635
3636TH2L::TH2L(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
3637 ,Int_t nbinsy,Double_t ylow,Double_t yup)
3638 :TH2(name,title,nbinsx,xbins,nbinsy,ylow,yup)
3639{
3641 if (fgDefaultSumw2) Sumw2();
3642}
3643
3644
3645////////////////////////////////////////////////////////////////////////////////
3646/// Constructor
3647/// (see TH2::TH2 for explanation of parameters)
3648
3649TH2L::TH2L(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_t xup
3650 ,Int_t nbinsy,const Double_t *ybins)
3651 :TH2(name,title,nbinsx,xlow,xup,nbinsy,ybins)
3652{
3654 if (fgDefaultSumw2) Sumw2();
3655}
3656
3657
3658////////////////////////////////////////////////////////////////////////////////
3659/// Constructor
3660/// (see TH2::TH2 for explanation of parameters)
3661
3662TH2L::TH2L(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
3663 ,Int_t nbinsy,const Double_t *ybins)
3664 :TH2(name,title,nbinsx,xbins,nbinsy,ybins)
3665{
3667 if (fgDefaultSumw2) Sumw2();
3668}
3669
3670
3671////////////////////////////////////////////////////////////////////////////////
3672/// Constructor
3673/// (see TH2::TH2 for explanation of parameters)
3674
3675TH2L::TH2L(const char *name,const char *title,Int_t nbinsx,const Float_t *xbins
3676 ,Int_t nbinsy,const Float_t *ybins)
3677 :TH2(name,title,nbinsx,xbins,nbinsy,ybins)
3678{
3680 if (fgDefaultSumw2) Sumw2();
3681}
3682
3683
3684////////////////////////////////////////////////////////////////////////////////
3685/// Copy constructor.
3686/// The list of functions is not copied. (Use Clone() if needed)
3687
3689{
3690 h2l.TH2L::Copy(*this);
3691}
3692
3693
3694////////////////////////////////////////////////////////////////////////////////
3695/// Increment bin content by 1.
3696/// Passing an out-of-range bin leads to undefined behavior
3697
3699{
3700 if (fArray[bin] < LLONG_MAX) fArray[bin]++;
3701}
3702
3703
3704////////////////////////////////////////////////////////////////////////////////
3705/// Increment bin content by w.
3706/// \warning The value of w is cast to `Long64_t` before being added.
3707/// Passing an out-of-range bin leads to undefined behavior
3708
3710{
3711 Long64_t newval = fArray[bin] + Long64_t(w);
3712 if (newval > -LLONG_MAX && newval < LLONG_MAX) {fArray[bin] = newval; return;}
3713 if (newval < -LLONG_MAX) fArray[bin] = -LLONG_MAX;
3714 if (newval > LLONG_MAX) fArray[bin] = LLONG_MAX;
3715}
3716
3717
3718////////////////////////////////////////////////////////////////////////////////
3719/// Copy.
3720
3722{
3724}
3725
3726
3727////////////////////////////////////////////////////////////////////////////////
3728/// Reset this histogram: contents, errors, etc.
3729
3735
3736
3737////////////////////////////////////////////////////////////////////////////////
3738/// Set total number of bins including under/overflow
3739/// Reallocate bin contents array
3740
3742{
3743 if (n < 0) n = (fXaxis.GetNbins()+2)*(fYaxis.GetNbins()+2);
3744 fNcells = n;
3746}
3747
3748
3749////////////////////////////////////////////////////////////////////////////////
3750/// Operator =
3751
3753{
3754 if (this != &h2l)
3755 h2l.TH2L::Copy(*this);
3756 return *this;
3757}
3758
3759
3760////////////////////////////////////////////////////////////////////////////////
3761/// Operator *
3762
3764{
3765 TH2L hnew = h1;
3766 hnew.Scale(c1);
3767 hnew.SetDirectory(nullptr);
3768 return hnew;
3769}
3770
3771
3772////////////////////////////////////////////////////////////////////////////////
3773/// Operator +
3774
3775TH2L operator+(TH2L const &h1, TH2L const &h2)
3776{
3777 TH2L hnew = h1;
3778 hnew.Add(&h2,1);
3779 hnew.SetDirectory(nullptr);
3780 return hnew;
3781}
3782
3783
3784////////////////////////////////////////////////////////////////////////////////
3785/// Operator -
3786
3787TH2L operator-(TH2L const &h1, TH2L const &h2)
3788{
3789 TH2L hnew = h1;
3790 hnew.Add(&h2,-1);
3791 hnew.SetDirectory(nullptr);
3792 return hnew;
3793}
3794
3795
3796////////////////////////////////////////////////////////////////////////////////
3797/// Operator *
3798
3799TH2L operator*(TH2L const &h1, TH2L const &h2)
3800{
3801 TH2L hnew = h1;
3802 hnew.Multiply(&h2);
3803 hnew.SetDirectory(nullptr);
3804 return hnew;
3805}
3806
3807
3808////////////////////////////////////////////////////////////////////////////////
3809/// Operator /
3810
3811TH2L operator/(TH2L const &h1, TH2L const &h2)
3812{
3813 TH2L hnew = h1;
3814 hnew.Divide(&h2);
3815 hnew.SetDirectory(nullptr);
3816 return hnew;
3817}
3818
3819
3820//______________________________________________________________________________
3821// TH2F methods
3822// TH2F a 2-D histogram with four bytes per cell (float). Maximum precision 7 digits, maximum integer bin content = +/-16777216
3823//______________________________________________________________________________
3824
3825
3826
3827////////////////////////////////////////////////////////////////////////////////
3828/// Constructor.
3829
3831{
3832 SetBinsLength(9);
3833 if (fgDefaultSumw2) Sumw2();
3834}
3835
3836
3837////////////////////////////////////////////////////////////////////////////////
3838/// Destructor.
3839
3841{
3842}
3843
3844
3845////////////////////////////////////////////////////////////////////////////////
3846/// Constructor
3847/// (see TH2::TH2 for explanation of parameters)
3848
3849TH2F::TH2F(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_t xup
3850 ,Int_t nbinsy,Double_t ylow,Double_t yup)
3851 :TH2(name,title,nbinsx,xlow,xup,nbinsy,ylow,yup)
3852{
3854 if (fgDefaultSumw2) Sumw2();
3855
3856 if (xlow >= xup || ylow >= yup) SetBuffer(fgBufferSize);
3857}
3858
3859
3860////////////////////////////////////////////////////////////////////////////////
3861/// Constructor
3862/// (see TH2::TH2 for explanation of parameters)
3863
3864TH2F::TH2F(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
3865 ,Int_t nbinsy,Double_t ylow,Double_t yup)
3866 :TH2(name,title,nbinsx,xbins,nbinsy,ylow,yup)
3867{
3869 if (fgDefaultSumw2) Sumw2();
3870}
3871
3872
3873////////////////////////////////////////////////////////////////////////////////
3874/// Constructor
3875/// (see TH2::TH2 for explanation of parameters)
3876
3877TH2F::TH2F(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_t xup
3878 ,Int_t nbinsy,const Double_t *ybins)
3879 :TH2(name,title,nbinsx,xlow,xup,nbinsy,ybins)
3880{
3882 if (fgDefaultSumw2) Sumw2();
3883}
3884
3885
3886////////////////////////////////////////////////////////////////////////////////
3887/// Constructor
3888/// (see TH2::TH2 for explanation of parameters)
3889
3890TH2F::TH2F(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
3891 ,Int_t nbinsy,const Double_t *ybins)
3892 :TH2(name,title,nbinsx,xbins,nbinsy,ybins)
3893{
3895 if (fgDefaultSumw2) Sumw2();
3896}
3897
3898
3899////////////////////////////////////////////////////////////////////////////////
3900/// Constructor
3901/// (see TH2::TH2 for explanation of parameters)
3902
3903TH2F::TH2F(const char *name,const char *title,Int_t nbinsx,const Float_t *xbins
3904 ,Int_t nbinsy,const Float_t *ybins)
3905 :TH2(name,title,nbinsx,xbins,nbinsy,ybins)
3906{
3908 if (fgDefaultSumw2) Sumw2();
3909}
3910
3911
3912////////////////////////////////////////////////////////////////////////////////
3913/// Constructor.
3914/// Construct a TH2F from a TMatrixFBase
3915
3917:TH2("TMatrixFBase","",m.GetNcols(),m.GetColLwb(),1+m.GetColUpb(),m.GetNrows(),m.GetRowLwb(),1+m.GetRowUpb())
3918{
3920 Int_t ilow = m.GetRowLwb();
3921 Int_t iup = m.GetRowUpb();
3922 Int_t jlow = m.GetColLwb();
3923 Int_t jup = m.GetColUpb();
3924 for (Int_t i=ilow;i<=iup;i++) {
3925 for (Int_t j=jlow;j<=jup;j++) {
3926 SetBinContent(j-jlow+1,i-ilow+1,m(i,j));
3927 }
3928 }
3929}
3930
3931
3932////////////////////////////////////////////////////////////////////////////////
3933/// Copy constructor.
3934/// The list of functions is not copied. (Use Clone() if needed)
3935
3937{
3938 h2f.TH2F::Copy(*this);
3939}
3940
3941
3942////////////////////////////////////////////////////////////////////////////////
3943/// Copy.
3944
3946{
3948}
3949
3950
3951////////////////////////////////////////////////////////////////////////////////
3952/// Reset this histogram: contents, errors, etc.
3953
3955{
3958}
3959
3960
3961////////////////////////////////////////////////////////////////////////////////
3962/// Set total number of bins including under/overflow
3963/// Reallocate bin contents array
3964
3966{
3967 if (n < 0) n = (fXaxis.GetNbins()+2)*(fYaxis.GetNbins()+2);
3968 fNcells = n;
3969 TArrayF::Set(n);
3970}
3971
3972
3973////////////////////////////////////////////////////////////////////////////////
3974/// Stream an object of class TH2F.
3975
3977{
3978 if (R__b.IsReading()) {
3979 UInt_t R__s, R__c;
3980 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
3981 if (R__v > 2) {
3982 R__b.ReadClassBuffer(TH2F::Class(), this, R__v, R__s, R__c);
3983 return;
3984 }
3985 //====process old versions before automatic schema evolution
3986 if (R__v < 2) {
3987 R__b.ReadVersion();
3990 R__b.ReadVersion();
3991 R__b >> fScalefactor;
3992 R__b >> fTsumwy;
3993 R__b >> fTsumwy2;
3994 R__b >> fTsumwxy;
3995 } else {
3998 R__b.CheckByteCount(R__s, R__c, TH2F::IsA());
3999 }
4000 //====end of old versions
4001
4002 } else {
4003 R__b.WriteClassBuffer(TH2F::Class(),this);
4004 }
4005}
4006
4007
4008////////////////////////////////////////////////////////////////////////////////
4009/// Operator =
4010
4012{
4013 if (this != &h2f)
4014 h2f.TH2F::Copy(*this);
4015 return *this;
4016}
4017
4018
4019////////////////////////////////////////////////////////////////////////////////
4020/// Operator *
4021
4023{
4024 TH2F hnew = h1;
4025 hnew.Scale(c1);
4026 hnew.SetDirectory(nullptr);
4027 return hnew;
4028}
4029
4030
4031////////////////////////////////////////////////////////////////////////////////
4032/// Operator *
4033
4035{
4036 TH2F hnew = h1;
4037 hnew.Scale(c1);
4038 hnew.SetDirectory(nullptr);
4039 return hnew;
4040}
4041
4042
4043////////////////////////////////////////////////////////////////////////////////
4044/// Operator +
4045
4046TH2F operator+(TH2F const &h1, TH2F const &h2)
4047{
4048 TH2F hnew = h1;
4049 hnew.Add(&h2,1);
4050 hnew.SetDirectory(nullptr);
4051 return hnew;
4052}
4053
4054
4055////////////////////////////////////////////////////////////////////////////////
4056/// Operator -
4057
4058TH2F operator-(TH2F const &h1, TH2F const &h2)
4059{
4060 TH2F hnew = h1;
4061 hnew.Add(&h2,-1);
4062 hnew.SetDirectory(nullptr);
4063 return hnew;
4064}
4065
4066
4067////////////////////////////////////////////////////////////////////////////////
4068/// Operator *
4069
4070TH2F operator*(TH2F const &h1, TH2F const &h2)
4071{
4072 TH2F hnew = h1;
4073 hnew.Multiply(&h2);
4074 hnew.SetDirectory(nullptr);
4075 return hnew;
4076}
4077
4078
4079////////////////////////////////////////////////////////////////////////////////
4080/// Operator /
4081
4082TH2F operator/(TH2F const &h1, TH2F const &h2)
4083{
4084 TH2F hnew = h1;
4085 hnew.Divide(&h2);
4086 hnew.SetDirectory(nullptr);
4087 return hnew;
4088}
4089
4090
4091//______________________________________________________________________________
4092// TH2D methods
4093// TH2D a 2-D histogram with eight bytes per cell (double). Maximum precision 14 digits, maximum integer bin content = +/-9007199254740992
4094//______________________________________________________________________________
4095
4096
4097
4098////////////////////////////////////////////////////////////////////////////////
4099/// Constructor.
4100
4102{
4103 SetBinsLength(9);
4104 if (fgDefaultSumw2) Sumw2();
4105}
4106
4107
4108////////////////////////////////////////////////////////////////////////////////
4109/// Destructor.
4110
4112{
4113}
4114
4115
4116////////////////////////////////////////////////////////////////////////////////
4117/// Constructor
4118/// (see TH2::TH2 for explanation of parameters)
4119
4120TH2D::TH2D(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_t xup
4121 ,Int_t nbinsy,Double_t ylow,Double_t yup)
4122 :TH2(name,title,nbinsx,xlow,xup,nbinsy,ylow,yup)
4123{
4125 if (fgDefaultSumw2) Sumw2();
4126
4127 if (xlow >= xup || ylow >= yup) SetBuffer(fgBufferSize);
4128}
4129
4130
4131////////////////////////////////////////////////////////////////////////////////
4132/// Constructor
4133/// (see TH2::TH2 for explanation of parameters)
4134
4135TH2D::TH2D(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
4136 ,Int_t nbinsy,Double_t ylow,Double_t yup)
4137 :TH2(name,title,nbinsx,xbins,nbinsy,ylow,yup)
4138{
4140 if (fgDefaultSumw2) Sumw2();
4141}
4142
4143
4144////////////////////////////////////////////////////////////////////////////////
4145/// Constructor
4146/// (see TH2::TH2 for explanation of parameters)
4147
4148TH2D::TH2D(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_t xup
4149 ,Int_t nbinsy,const Double_t *ybins)
4150 :TH2(name,title,nbinsx,xlow,xup,nbinsy,ybins)
4151{
4153 if (fgDefaultSumw2) Sumw2();
4154}
4155
4156
4157////////////////////////////////////////////////////////////////////////////////
4158/// Constructor
4159/// (see TH2::TH2 for explanation of parameters)
4160
4161TH2D::TH2D(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
4162 ,Int_t nbinsy,const Double_t *ybins)
4163 :TH2(name,title,nbinsx,xbins,nbinsy,ybins)
4164{
4166 if (fgDefaultSumw2) Sumw2();
4167}
4168
4169
4170////////////////////////////////////////////////////////////////////////////////
4171/// Constructor
4172/// (see TH2::TH2 for explanation of parameters)
4173
4174TH2D::TH2D(const char *name,const char *title,Int_t nbinsx,const Float_t *xbins
4175 ,Int_t nbinsy,const Float_t *ybins)
4176 :TH2(name,title,nbinsx,xbins,nbinsy,ybins)
4177{
4179 if (fgDefaultSumw2) Sumw2();
4180}
4181
4182
4183////////////////////////////////////////////////////////////////////////////////
4184/// Constructor
4185/// Construct a 2-D histogram from a TMatrixDBase
4186
4188:TH2("TMatrixDBase","",m.GetNcols(),m.GetColLwb(),1+m.GetColUpb(),m.GetNrows(),m.GetRowLwb(),1+m.GetRowUpb())
4189{
4191 Int_t ilow = m.GetRowLwb();
4192 Int_t iup = m.GetRowUpb();
4193 Int_t jlow = m.GetColLwb();
4194 Int_t jup = m.GetColUpb();
4195 for (Int_t i=ilow;i<=iup;i++) {
4196 for (Int_t j=jlow;j<=jup;j++) {
4197 SetBinContent(j-jlow+1,i-ilow+1,m(i,j));
4198 }
4199 }
4200 if (fgDefaultSumw2) Sumw2();
4201}
4202
4203
4204////////////////////////////////////////////////////////////////////////////////
4205/// Copy constructor.
4206/// The list of functions is not copied. (Use Clone() if needed)
4207
4209{
4210 // intentionally call virtual Copy method to warn if TProfile2D is copied
4211 h2d.Copy(*this);
4212}
4213
4214
4215////////////////////////////////////////////////////////////////////////////////
4216/// Copy.
4217
4219{
4221}
4222
4223
4224////////////////////////////////////////////////////////////////////////////////
4225/// Reset this histogram: contents, errors, etc.
4226
4228{
4231}
4232
4233
4234////////////////////////////////////////////////////////////////////////////////
4235/// Set total number of bins including under/overflow
4236/// Reallocate bin contents array
4237
4239{
4240 if (n < 0) n = (fXaxis.GetNbins()+2)*(fYaxis.GetNbins()+2);
4241 fNcells = n;
4242 TArrayD::Set(n);
4243}
4244
4245
4246////////////////////////////////////////////////////////////////////////////////
4247/// Stream an object of class TH2D.
4248
4250{
4251 if (R__b.IsReading()) {
4252 UInt_t R__s, R__c;
4253 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
4254 if (R__v > 2) {
4255 R__b.ReadClassBuffer(TH2D::Class(), this, R__v, R__s, R__c);
4256 return;
4257 }
4258 //====process old versions before automatic schema evolution
4259 if (R__v < 2) {
4260 R__b.ReadVersion();
4263 R__b.ReadVersion();
4264 R__b >> fScalefactor;
4265 R__b >> fTsumwy;
4266 R__b >> fTsumwy2;
4267 R__b >> fTsumwxy;
4268 } else {
4271 R__b.CheckByteCount(R__s, R__c, TH2D::IsA());
4272 }
4273 //====end of old versions
4274
4275 } else {
4276 R__b.WriteClassBuffer(TH2D::Class(),this);
4277 }
4278}
4279
4280
4281////////////////////////////////////////////////////////////////////////////////
4282/// Operator =
4283
4285{
4286 // intentionally call virtual Copy method to warn if TProfile2D is copied
4287 if (this != &h2d)
4288 h2d.Copy(*this);
4289 return *this;
4290}
4291
4292
4293
4294////////////////////////////////////////////////////////////////////////////////
4295/// Operator *
4296
4298{
4299 TH2D hnew = h2d;
4300 hnew.Scale(c1);
4301 hnew.SetDirectory(nullptr);
4302 return hnew;
4303}
4304
4305
4306////////////////////////////////////////////////////////////////////////////////
4307/// Operator +
4308
4309TH2D operator+(TH2D const &h1, TH2D const &h2)
4310{
4311 TH2D hnew = h1;
4312 hnew.Add(&h2,1);
4313 hnew.SetDirectory(nullptr);
4314 return hnew;
4315}
4316
4317
4318////////////////////////////////////////////////////////////////////////////////
4319/// Operator -
4320
4321TH2D operator-(TH2D const &h1, TH2D const &h2)
4322{
4323 TH2D hnew = h1;
4324 hnew.Add(&h2,-1);
4325 hnew.SetDirectory(nullptr);
4326 return hnew;
4327}
4328
4329
4330////////////////////////////////////////////////////////////////////////////////
4331/// Operator *
4332
4333TH2D operator*(TH2D const &h1, TH2D const &h2)
4334{
4335 TH2D hnew = h1;
4336 hnew.Multiply(&h2);
4337 hnew.SetDirectory(nullptr);
4338 return hnew;
4339}
4340
4341
4342////////////////////////////////////////////////////////////////////////////////
4343/// Operator /
4344
4345TH2D operator/(TH2D const &h1, TH2D const &h2)
4346{
4347 TH2D hnew = h1;
4348 hnew.Divide(&h2);
4349 hnew.SetDirectory(nullptr);
4350 return hnew;
4351}
#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:3056
TH2C operator*(Float_t c1, TH2C const &h1)
Operator *.
Definition TH2.cxx:3044
TH2C operator/(TH2C const &h1, TH2C const &h2)
Operator /.
Definition TH2.cxx:3092
TH2C operator-(TH2C const &h1, TH2C const &h2)
Operator -.
Definition TH2.cxx:3068
float xmin
int nentries
float ymin
float xmax
float ymax
#define gROOT
Definition TROOT.h:414
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:1634
virtual Double_t GetParError(Int_t ipar) const
Return value of parameter number ipar.
Definition TF1.cxx:1980
Double_t GetChisquare() const
Return the Chisquare after fitting. See ROOT::Fit::FitResult::Chi2()
Definition TF1.h:409
virtual void SetRange(Double_t xmin, Double_t xmax)
Initialize the upper and lower bounds to draw the function.
Definition TF1.cxx:3583
virtual Int_t GetNpar() const
Definition TF1.h:446
virtual Double_t Integral(Double_t a, Double_t b, Double_t epsrel=1.e-12)
IntegralOneDim or analytical integral.
Definition TF1.cxx:2580
virtual Int_t GetNumberFitPoints() const
Definition TF1.h:468
virtual Double_t * GetParameters() const
Definition TF1.h:485
virtual void GetRange(Double_t *xmin, Double_t *xmax) const
Return range of a generic N-D function.
Definition TF1.cxx:2329
virtual const char * GetParName(Int_t ipar) const
Definition TF1.h:494
virtual void SetParameters(const Double_t *params)
Definition TF1.h:618
virtual Double_t GetParameter(Int_t ipar) const
Definition TF1.h:477
A 2-Dim function with parameters.
Definition TF2.h:29
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:926
static TClass * Class()
void Reset(Option_t *option="") override
Reset.
Definition TH1.cxx:10365
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:109
virtual void SetDirectory(TDirectory *dir)
By default, when a histogram is created, it is added to the list of histogram objects in the current ...
Definition TH1.cxx:9019
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:4463
@ 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:2663
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:8055
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:7680
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:9142
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:7608
virtual Int_t GetDimension() const
Definition TH1.h:527
void Streamer(TBuffer &) override
Stream a class object.
Definition TH1.cxx:7018
@ 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:6711
virtual void Reset(Option_t *option="")
Reset this histogram: contents, errors, etc.
Definition TH1.cxx:7188
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:7957
TVirtualHistPainter * GetPainter(Option_t *option="")
Return pointer to painter.
Definition TH1.cxx:4526
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:5001
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:9285
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:3352
TAxis * GetYaxis()
Definition TH1.h:572
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3056
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:8536
UInt_t GetAxisLabelStatus() const
Internal function used in TH1::Fill to see which axis is full alphanumeric, i.e.
Definition TH1.cxx:6750
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:9301
virtual Double_t GetBinLowEdge(Int_t bin) const
Return bin lower edge for 1D histogram.
Definition TH1.cxx:9231
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:6281
virtual void ResetStats()
Reset the statistics including the number of entries and replace with values calculated from bin cont...
Definition TH1.cxx:7975
@ 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:5103
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:6579
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:2523
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:4630
TObject * Clone(const char *newname="") const override
Make a complete copy of the underlying object.
Definition TH1.cxx:2744
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:8849
virtual void Sumw2(Bool_t flag=kTRUE)
Create structure to store sum of squares of weights.
Definition TH1.cxx:9102
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:2976
static TClass * Class()
TClass * IsA() const override
Definition TH2.h:179
void Streamer(TBuffer &) override
Stream an object of class TH2C.
Definition TH2.cxx:2998
void AddBinContent(Int_t bin) override
Increment bin content by 1.
Definition TH2.cxx:2944
TH2C()
Constructor.
Definition TH2.cxx:2850
TH2C & operator=(const TH2C &h1)
Operator =.
Definition TH2.cxx:3033
~TH2C() override
Destructor.
Definition TH2.cxx:2860
void Copy(TObject &hnew) const override
Copy.
Definition TH2.cxx:2967
void SetBinsLength(Int_t n=-1) override
Set total number of bins including under/overflow Reallocate bin contents array.
Definition TH2.cxx:2987
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:4249
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:4238
~TH2D() override
Destructor.
Definition TH2.cxx:4111
void Copy(TObject &hnew) const override
Copy.
Definition TH2.cxx:4218
TH2D()
Constructor.
Definition TH2.cxx:4101
TH2D & operator=(const TH2D &h1)
Operator =.
Definition TH2.cxx:4284
2-D histogram with a float per channel (see TH1 documentation)
Definition TH2.h:345
TH2F()
Constructor.
Definition TH2.cxx:3830
TClass * IsA() const override
Definition TH2.h:388
TH2F & operator=(const TH2F &h1)
Operator =.
Definition TH2.cxx:4011
~TH2F() override
Destructor.
Definition TH2.cxx:3840
void Copy(TObject &hnew) const override
Copy.
Definition TH2.cxx:3945
static TClass * Class()
void Streamer(TBuffer &) override
Stream an object of class TH2F.
Definition TH2.cxx:3976
void SetBinsLength(Int_t n=-1) override
Set total number of bins including under/overflow Reallocate bin contents array.
Definition TH2.cxx:3965
2-D histogram with an int per channel (see TH1 documentation)
Definition TH2.h:245
TH2I()
Constructor.
Definition TH2.cxx:3374
void Copy(TObject &hnew) const override
Copy.
Definition TH2.cxx:3493
void AddBinContent(Int_t bin) override
Increment bin content by 1.
Definition TH2.cxx:3470
~TH2I() override
Destructor.
Definition TH2.cxx:3384
TH2I & operator=(const TH2I &h1)
Operator =.
Definition TH2.cxx:3524
void SetBinsLength(Int_t n=-1) override
Set total number of bins including under/overflow Reallocate bin contents array.
Definition TH2.cxx:3513
2-D histogram with a long64 per channel (see TH1 documentation)
Definition TH2.h:296
TH2L & operator=(const TH2L &h1)
Operator =.
Definition TH2.cxx:3752
void SetBinsLength(Int_t n=-1) override
Set total number of bins including under/overflow Reallocate bin contents array.
Definition TH2.cxx:3741
~TH2L() override
Destructor.
Definition TH2.cxx:3612
void Copy(TObject &hnew) const override
Copy.
Definition TH2.cxx:3721
TH2L()
Constructor.
Definition TH2.cxx:3602
void AddBinContent(Int_t bin) override
Increment bin content by 1.
Definition TH2.cxx:3698
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:3207
~TH2S() override
Destructor.
Definition TH2.cxx:3121
static TClass * Class()
TH2S & operator=(const TH2S &h1)
Operator =.
Definition TH2.cxx:3296
void Copy(TObject &hnew) const override
Copy.
Definition TH2.cxx:3230
TH2S()
Constructor.
Definition TH2.cxx:3111
void Streamer(TBuffer &) override
Stream an object of class TH2S.
Definition TH2.cxx:3261
void SetBinsLength(Int_t n=-1) override
Set total number of bins including under/overflow Reallocate bin contents array.
Definition TH2.cxx:3250
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:2463
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:1225
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:2682
virtual Double_t GetCorrelationFactor(Int_t axis1=1, Int_t axis2=2) const
Return correlation factor between axis1 and axis2.
Definition TH2.cxx:1114
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:1851
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:1170
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:1441
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:1050
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:1086
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:2084
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:1634
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:2505
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:2137
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:2514
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:2603
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:2492
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:2637
TClass * IsA() const override
Definition TH2.h:137
void Reset(Option_t *option="") override
Reset this histogram: contents, errors, etc.
Definition TH2.cxx:2587
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:2665
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:2147
TH2 * RebinX(Int_t ngroup=2, const char *newname="") override
Rebin only the X axis see Rebin2D.
Definition TH2.cxx:1613
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:1132
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:1055
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:2420
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:2714
~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:1316
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:1324
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:2621
void Streamer(TBuffer &) override
Stream an object of class TH2.
Definition TH2.cxx:2817
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:1669
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:985
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:2654
Double_t Integral(Option_t *option="") const override
Return integral of bin contents.
Definition TH2.cxx:1288
void PutStats(Double_t *stats) override
Replace current statistics with the values in array stats.
Definition TH2.cxx:2472
virtual TH2 * RebinY(Int_t ngroup=2, const char *newname="")
Rebin only the Y axis see Rebin2D.
Definition TH2.cxx:1623
static THLimitsFinder * GetLimitsFinder()
Return pointer to the current finder.
THashList implements a hybrid collection class consisting of a hash table and a list to store TObject...
Definition THashList.h:34
TMatrixTBase.
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
const char * GetTitle() const override
Returns title of object.
Definition TNamed.h:50
An array of TObjects.
Definition TObjArray.h:31
Collectable string class.
Definition TObjString.h:28
Mother of all ROOT objects.
Definition TObject.h:42
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition TObject.h:204
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition TObject.cxx:1075
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition TObject.cxx:882
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:1089
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition TObject.cxx:1063
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