Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
19TH1K class supports the nearest K Neighbours method, widely used in cluster analysis.
20This method is especially useful for small statistics.
21In 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
30This class has been implemented by Victor Perevoztchikov <perev@bnl.gov>
31*/
32
34
35
36////////////////////////////////////////////////////////////////////////////////
37/// Constructor.
38
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
53TH1K::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
77void 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 (!GetStatOverflowsBehaviour()) 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
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
168void 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
220static 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}
double Double_t
Definition RtypesCore.h:59
float Float_t
Definition RtypesCore.h:57
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:364
char name[80]
Definition TGX11.cxx:110
static int TH1K_fcompare(const void *f1, const void *f2)
Compare.
Definition TH1K.cxx:220
Array of floats (32 bits per element).
Definition TArrayF.h:27
Float_t * fArray
Definition TArrayF.h:30
void Reset()
Definition TArrayF.h:47
const Float_t * GetArray() const
Definition TArrayF.h:43
void Set(Int_t n)
Set size of this array to n floats.
Definition TArrayF.cxx:105
void AddAt(Float_t c, Int_t i)
Add float c at position i. Check for out of bounds.
Definition TArrayF.cxx:93
Int_t fN
Definition TArray.h:38
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:234
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:270
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.
Double_t GetXmax() const
Definition TAxis.h:134
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:661
virtual Int_t FindBin(Double_t x)
Find bin number corresponding to abscissa x.
Definition TAxis.cxx:293
Double_t GetXmin() const
Definition TAxis.h:133
Int_t GetNbins() const
Definition TAxis.h:121
TH1K class supports the nearest K Neighbours method, widely used in cluster analysis.
Definition TH1K.h:27
Int_t fReady
Definition TH1K.h:32
Int_t fKOrd
Definition TH1K.h:34
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition TH1K.cxx:93
Int_t fKCur
Definition TH1K.h:35
virtual void Copy(TObject &obj) const
Copy this histogram structure to newth1.
Definition TH1K.cxx:77
Int_t fNIn
Definition TH1K.h:33
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 Double_t GetBinError(Int_t bin) const
Return content of global bin error.
Definition TH1K.cxx:144
virtual Double_t GetBinContent(Int_t bin) const
Return content of global bin number bin.
Definition TH1K.cxx:116
TH1K()
Constructor.
Definition TH1K.cxx:39
void Sort()
Sort.
Definition TH1K.cxx:231
virtual ~TH1K()
Destructor.
Definition TH1K.cxx:67
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:58
virtual Double_t GetBinCenter(Int_t bin) const
Return bin center for 1D histogram.
Definition TH1.cxx:8981
Double_t fTsumw
Total Sum of weights.
Definition TH1.h:95
Double_t fTsumw2
Total Sum of squares of weights.
Definition TH1.h:96
Double_t fTsumwx2
Total Sum of weight*X*X.
Definition TH1.h:98
@ kNoStats
don't draw stats box
Definition TH1.h:164
TDirectory * fDirectory
!Pointer to directory holding this histogram
Definition TH1.h:108
virtual void Reset(Option_t *option="")
Reset this histogram: contents, errors, etc.
Definition TH1.cxx:7069
TAxis * GetXaxis()
Get the behaviour adopted by the object about the statoverflows. See EStatOverflows for more informat...
Definition TH1.h:320
Int_t fDimension
!Histogram dimension (1, 2 or 3 dim)
Definition TH1.h:109
virtual void Copy(TObject &hnew) const
Copy this histogram structure to newth1.
Definition TH1.cxx:2663
Double_t fEntries
Number of entries.
Definition TH1.h:94
TAxis fZaxis
Z axis descriptor.
Definition TH1.h:91
TAxis fXaxis
X axis descriptor.
Definition TH1.h:89
virtual Double_t GetBinWidth(Int_t bin) const
Return bin width for 1D histogram.
Definition TH1.cxx:9003
Bool_t GetStatOverflowsBehaviour() const
Definition TH1.h:152
TAxis fYaxis
Y axis descriptor.
Definition TH1.h:90
Double_t fTsumwx
Total Sum of weight*X.
Definition TH1.h:97
TString fOption
histogram options
Definition TH1.h:104
virtual const char * GetTitle() const
Returns title of object.
Definition TNamed.h:48
virtual const char * GetName() const
Returns name of object.
Definition TNamed.h:47
Mother of all ROOT objects.
Definition TObject.h:37
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition TObject.h:187
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition TObject.cxx:130
Basic string class.
Definition TString.h:136
Ssiz_t Length() const
Definition TString.h:410
void ToLower()
Change string to lower-case.
Definition TString.cxx:1145
const char * Data() const
Definition TString.h:369
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:624
Double_t x[n]
Definition legend1.C:17
TF1 * f1
Definition legend1.C:11
Double_t Sqrt(Double_t x)
Definition TMath.h:691
Long64_t BinarySearch(Long64_t n, const T *array, T value)
Definition TMathBase.h:278
Short_t Abs(Short_t d)
Definition TMathBase.h:120