Logo ROOT   6.08/07
Reference Guide
TH1K.cxx
Go to the documentation of this file.
1 // @(#)root/hist:$Id$
2 // Author: Victor Perevoztchikov <perev@bnl.gov> 21/02/2001
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 <stdlib.h>
13 
14 #include "Riostream.h"
15 #include "TH1K.h"
16 #include "TMath.h"
17 
18 /** \class TH1K
19 TH1K class supports the nearest K Neighbours method, widely used in cluster analysis.
20 This method is especially useful for small statistics.
21 In this method :
22 
23  DensityOfProbability ~ 1/DistanceToNearestKthNeighbour
24  Ctr TH1K::TH1K(name,title,nbins,xlow,xup,K=0)
25  differs from TH1F only by "K"
26  K - is the order of K Neighbours method, usually >=3
27  K = 0, means default, where K is selected by TH1K in such a way
28  that DistanceToNearestKthNeighbour > BinWidth and K >=3
29 
30 This class has been implemented by Victor Perevoztchikov <perev@bnl.gov>
31 */
32 
34 
35 
36 ////////////////////////////////////////////////////////////////////////////////
37 /// Constructor.
38 
39 TH1K::TH1K(): TH1(), TArrayF()
40 {
41  fDimension = 1;
42  fNIn = 0;
43  fReady = 0;
44  fKOrd = 3;
45  fKCur = 0;
46 }
47 
48 
49 ////////////////////////////////////////////////////////////////////////////////
50 /// Create a 1-Dim histogram with fix bins of type float
51 /// (see TH1K::TH1 for explanation of parameters)
52 
53 TH1K::TH1K(const char *name,const char *title,Int_t nbins,Double_t xlow,Double_t xup,Int_t k)
54  : TH1(name,title,nbins,xlow,xup), TArrayF(100)
55 {
56  fDimension = 1;
57  fNIn = 0;
58  fReady = 0;
59  fKOrd = k;
60  fKCur = 0;
61 }
62 
63 
64 ////////////////////////////////////////////////////////////////////////////////
65 /// Destructor.
66 
68 {
69 }
70 
71 ////////////////////////////////////////////////////////////////////////////////
72 /// Copy this histogram structure to newth1.
73 ///
74 /// Note that this function does not copy the list of associated functions.
75 /// Use TObject::Clone to make a full copy of an histogram.
76 
77 void TH1K::Copy(TObject &obj) const
78 {
79  TH1::Copy(obj);
80  ((TH1K&)obj).fNIn = fNIn;
81 }
82 
83 ////////////////////////////////////////////////////////////////////////////////
84 /// Increment bin with abscissa X by 1.
85 ///
86 /// if x is less than the low-edge of the first bin, the Underflow bin is incremented
87 /// if x is greater than the upper edge of last bin, the Overflow bin is incremented
88 ///
89 /// If the storage of the sum of squares of weights has been triggered,
90 /// via the function Sumw2, then the sum of the squares of weights is incremented
91 /// by 1 in the bin corresponding to x.
92 
94 {
95  fReady = 0;
96  Int_t bin;
97  fEntries++;
98  bin =fXaxis.FindBin(x);
99  if (bin == 0 || bin > fXaxis.GetNbins()) {
100  if (!fgStatOverflows) return -1;
101  }
102  ++fTsumw;
103  ++fTsumw2;
104  fTsumwx += x;
105  fTsumwx2 += x*x;
106  fReady = 0;
107  if (fNIn == fN) Set(fN*2);
108  AddAt(x,fNIn++);
109  return bin;
110 }
111 
112 
113 ////////////////////////////////////////////////////////////////////////////////
114 /// Return content of global bin number bin.
115 
117 {
118  if (!fReady) {
119  ((TH1K*)this)->Sort();
120  ((TH1K*)this)->fReady=1;
121  }
122  if (!fNIn) return 0.;
123  float x = GetBinCenter(bin);
124  int left = TMath::BinarySearch(fNIn,fArray,x);
125  int jl=left,jr=left+1,nk,nkmax =fKOrd;
126  float fl,fr,ff=0.,ffmin=1.e-6;
127  if (!nkmax) {nkmax = 3; ffmin = GetBinWidth(bin);}
128  if (nkmax >= fNIn) nkmax = fNIn-1;
129  for (nk = 1; nk <= nkmax || ff <= ffmin; nk++) {
130  fl = (jl>=0 ) ? TMath::Abs(fArray[jl]-x) : 1.e+20;
131  fr = (jr<fNIn) ? TMath::Abs(fArray[jr]-x) : 1.e+20;
132  if (jl<0 && jr>=fNIn) break;
133  if (fl < fr) { ff = fl; jl--;}
134  else { ff = fr; jr++;}
135  }
136  ((TH1K*)this)->fKCur = nk - 1;
137  return fNIn * 0.5*fKCur/((float)(fNIn+1))*GetBinWidth(bin)/ff;
138 }
139 
140 
141 ////////////////////////////////////////////////////////////////////////////////
142 /// Return content of global bin error.
143 
145 {
146  return TMath::Sqrt(((double)(fNIn-fKCur+1))/((fNIn+1)*(fKCur-1)))*GetBinContent(bin);
147 }
148 
149 
150 ////////////////////////////////////////////////////////////////////////////////
151 /// Reset.
152 
153 void TH1K::Reset(Option_t *option)
154 {
155  fNIn =0;
156  fReady = 0;
157  TH1::Reset(option);
158 }
159 
160 
161 ////////////////////////////////////////////////////////////////////////////////
162 /// Save primitive as a C++ statement(s) on output stream out
163 /// Note the following restrictions in the code generated:
164 /// - variable bin size not implemented
165 /// - Objects in list of functions not saved (fits)
166 /// - Contours not saved
167 
168 void TH1K::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
169 {
170  char quote = '"';
171  out<<" "<<std::endl;
172  out<<" "<<"TH1 *";
173 
174  out<<GetName()<<" = new "<<ClassName()<<"("<<quote<<GetName()<<quote<<","<<quote<<GetTitle()<<quote
175  <<","<<GetXaxis()->GetNbins()
176  <<","<<GetXaxis()->GetXmin()
177  <<","<<GetXaxis()->GetXmax()
178  <<","<<fKOrd;
179  out <<");"<<std::endl;
180 
181  if (fDirectory == 0) {
182  out<<" "<<GetName()<<"->SetDirectory(0);"<<std::endl;
183  }
184  if (TestBit(kNoStats)) {
185  out<<" "<<GetName()<<"->SetStats(0);"<<std::endl;
186  }
187  if (fOption.Length() != 0) {
188  out<<" "<<GetName()<<"->SetOption("<<quote<<fOption.Data()<<quote<<");"<<std::endl;
189  }
190 
191  if (fNIn) {
192  out<<" Float_t Arr[]={"<<std::endl;
193  for (int i=0; i<fNIn; i++) {
194  out<< fArray[i];
195  if (i != fNIn-1) {out<< ",";} else { out<< "};";}
196  if (i%10 == 9) {out<< std::endl;}
197  }
198  out<< std::endl;
199  out<<" for(int i=0;i<" << fNIn << ";i++)"<<GetName()<<"->Fill(Arr[i]);";
200  out<< std::endl;
201  }
202  SaveFillAttributes(out,GetName(),0,1001);
203  SaveLineAttributes(out,GetName(),1,1,1);
204  SaveMarkerAttributes(out,GetName(),1,1,1);
205  fXaxis.SaveAttributes(out,GetName(),"->GetXaxis()");
206  fYaxis.SaveAttributes(out,GetName(),"->GetYaxis()");
207  fZaxis.SaveAttributes(out,GetName(),"->GetZaxis()");
208  TString opt = option;
209  opt.ToLower();
210  if (!opt.Contains("nodraw")) {
211  out<<" "<<GetName()<<"->Draw("
212  <<quote<<option<<quote<<");"<<std::endl;
213  }
214 }
215 
216 
217 ////////////////////////////////////////////////////////////////////////////////
218 /// Compare.
219 
220 static int TH1K_fcompare(const void *f1,const void *f2)
221 {
222  if (*((float*)f1) < *((float*)f2)) return -1;
223  if (*((float*)f1) > *((float*)f2)) return 1;
224  return 0;
225 }
226 
227 
228 ////////////////////////////////////////////////////////////////////////////////
229 /// Sort.
230 
232 {
233  if (fNIn<2) return;
234  qsort(GetArray(),fNIn,sizeof(Float_t),&TH1K_fcompare);
235 }
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
Float_t * fArray
Definition: TArrayF.h:32
virtual void SaveAttributes(std::ostream &out, const char *name, const char *subname)
Save axis attributes as C++ statement(s) on output stream out.
Definition: TAxis.cxx:646
virtual Double_t GetBinCenter(Int_t bin) const
Return bin center for 1D histogram.
Definition: TH1.cxx:8251
void AddAt(Float_t c, Int_t i)
Add float c at position i. Check for out of bounds.
Definition: TArrayF.cxx:93
const Float_t * GetArray() const
Definition: TArrayF.h:45
float Float_t
Definition: RtypesCore.h:53
Int_t fReady
Definition: TH1K.h:32
Int_t fNIn
Definition: TH1K.h:33
const char Option_t
Definition: RtypesCore.h:62
TAxis fYaxis
Y axis descriptor.
Definition: TH1.h:105
Bool_t TestBit(UInt_t f) const
Definition: TObject.h:157
static Bool_t fgStatOverflows
!flag to use under/overflows in statistics
Definition: TH1.h:130
Basic string class.
Definition: TString.h:137
Array of floats (32 bits per element).
Definition: TArrayF.h:29
virtual ~TH1K()
Destructor.
Definition: TH1K.cxx:67
void ToLower()
Change string to lower-case.
Definition: TString.cxx:1089
int Int_t
Definition: RtypesCore.h:41
int nbins[3]
void Reset()
Definition: TArrayF.h:49
TAxis fZaxis
Z axis descriptor.
Definition: TH1.h:106
Short_t Abs(Short_t d)
Definition: TMathBase.h:110
Double_t fTsumwx2
Total Sum of weight*X*X.
Definition: TH1.h:113
virtual void Reset(Option_t *option="")
Reset this histogram: contents, errors, etc.
Definition: TH1.cxx:6418
TDirectory * fDirectory
!Pointer to directory holding this histogram
Definition: TH1.h:123
Double_t GetXmin() const
Definition: TAxis.h:139
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition: TObject.cxx:188
Double_t x[n]
Definition: legend1.C:17
virtual void SaveLineAttributes(std::ostream &out, const char *name, Int_t coldef=1, Int_t stydef=1, Int_t widdef=1)
Save line attributes as C++ statement(s) on output stream out.
Definition: TAttLine.cxx:260
Int_t fKOrd
Definition: TH1K.h:34
TH1K()
Constructor.
Definition: TH1K.cxx:39
TString fOption
histogram options
Definition: TH1.h:119
virtual void SaveMarkerAttributes(std::ostream &out, const char *name, Int_t coldef=1, Int_t stydef=1, Int_t sizdef=1)
Save line attributes as C++ statement(s) on output stream out.
Definition: TAttMarker.cxx:229
virtual void Copy(TObject &obj) const
Copy this histogram structure to newth1.
Definition: TH1K.cxx:77
Double_t fTsumwx
Total Sum of weight*X.
Definition: TH1.h:112
Int_t fN
Definition: TArray.h:40
Int_t fKCur
Definition: TH1K.h:35
Double_t fEntries
Number of entries.
Definition: TH1.h:109
virtual void SaveFillAttributes(std::ostream &out, const char *name, Int_t coldef=1, Int_t stydef=1001)
Save fill attributes as C++ statement(s) on output stream out.
Definition: TAttFill.cxx:232
Ssiz_t Length() const
Definition: TString.h:390
virtual void Copy(TObject &hnew) const
Copy this histogram structure to newth1.
Definition: TH1.cxx:2468
virtual Double_t GetBinContent(Int_t bin) const
Return content of global bin number bin.
Definition: TH1K.cxx:116
don&#39;t draw stats box
Definition: TH1.h:172
virtual void SavePrimitive(std::ostream &out, Option_t *option="")
Save primitive as a C++ statement(s) on output stream out Note the following restrictions in the code...
Definition: TH1K.cxx:168
virtual Int_t FindBin(Double_t x)
Find bin number corresponding to abscissa x.
Definition: TAxis.cxx:279
TH1K class supports the nearest K Neighbours method, widely used in cluster analysis.
Definition: TH1K.h:27
Double_t fTsumw2
Total Sum of squares of weights.
Definition: TH1.h:111
virtual Double_t GetBinWidth(Int_t bin) const
Return bin width for 1D histogram.
Definition: TH1.cxx:8273
#define ClassImp(name)
Definition: Rtypes.h:279
double Double_t
Definition: RtypesCore.h:55
Double_t fTsumw
Total Sum of weights.
Definition: TH1.h:110
static int TH1K_fcompare(const void *f1, const void *f2)
Compare.
Definition: TH1K.cxx:220
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:567
The TH1 histogram class.
Definition: TH1.h:80
Mother of all ROOT objects.
Definition: TObject.h:37
double f2(const double *x)
TF1 * f1
Definition: legend1.C:11
Int_t fDimension
!Histogram dimension (1, 2 or 3 dim)
Definition: TH1.h:124
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1K.cxx:93
void Sort()
Sort.
Definition: TH1K.cxx:231
void Set(Int_t n)
Set size of this array to n floats.
Definition: TArrayF.cxx:105
TAxis fXaxis
X axis descriptor.
Definition: TH1.h:104
Double_t Sqrt(Double_t x)
Definition: TMath.h:464
Int_t GetNbins() const
Definition: TAxis.h:127
Double_t GetXmax() const
Definition: TAxis.h:140
Long64_t BinarySearch(Long64_t n, const T *array, T value)
Definition: TMath.h:931
char name[80]
Definition: TGX11.cxx:109
TAxis * GetXaxis()
Definition: TH1.h:324
virtual Double_t GetBinError(Int_t bin) const
Return content of global bin error.
Definition: TH1K.cxx:144
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:52
const char * Data() const
Definition: TString.h:349