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