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