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