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