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 <cstdlib>
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}
43
44
45////////////////////////////////////////////////////////////////////////////////
46/// Create a 1-Dim histogram with fix bins of type float
47/// (see TH1K::TH1 for explanation of parameters)
48
49TH1K::TH1K(const char *name,const char *title,Int_t nbins,Double_t xlow,Double_t xup,Int_t k)
50 : TH1(name,title,nbins,xlow,xup), TArrayF(100)
51{
52 fDimension = 1;
53 fKOrd = k;
54}
55
56////////////////////////////////////////////////////////////////////////////////
57/// Copy this histogram structure to newth1.
58///
59/// Note that this function does not copy the list of associated functions.
60/// Use TObject::Clone to make a full copy of an histogram.
61
62void TH1K::Copy(TObject &obj) const
63{
64 TH1::Copy(obj);
65 ((TH1K&)obj).fNIn = fNIn;
66}
67
68////////////////////////////////////////////////////////////////////////////////
69/// Increment bin with abscissa X by 1.
70///
71/// if x is less than the low-edge of the first bin, the Underflow bin is incremented
72/// if x is greater than the upper edge of last bin, the Overflow bin is incremented
73///
74/// If the storage of the sum of squares of weights has been triggered,
75/// via the function Sumw2, then the sum of the squares of weights is incremented
76/// by 1 in the bin corresponding to x.
77
79{
80 fReady = 0;
81 Int_t bin;
82 fEntries++;
83 bin =fXaxis.FindBin(x);
84 if (bin == 0 || bin > fXaxis.GetNbins()) {
85 if (!GetStatOverflowsBehaviour()) return -1;
86 }
87 ++fTsumw;
88 ++fTsumw2;
89 fTsumwx += x;
90 fTsumwx2 += x*x;
91 fReady = 0;
92 if (fNIn == fN) Set(fN*2);
93 AddAt(x,fNIn++);
94 return bin;
95}
96
97
98////////////////////////////////////////////////////////////////////////////////
99/// Return content of global bin number bin.
100
102{
103 if (!fReady) {
104 ((TH1K*)this)->Sort();
105 ((TH1K*)this)->fReady=1;
106 }
107 if (!fNIn) return 0.;
108 float x = GetBinCenter(bin);
109 int left = TMath::BinarySearch(fNIn,fArray,x);
110 int jl=left,jr=left+1,nk,nkmax =fKOrd;
111 float fl,fr,ff=0.,ffmin=1.e-6;
112 if (!nkmax) {nkmax = 3; ffmin = GetBinWidth(bin);}
113 if (nkmax >= fNIn) nkmax = fNIn-1;
114 for (nk = 1; nk <= nkmax || ff <= ffmin; nk++) {
115 fl = (jl>=0 ) ? TMath::Abs(fArray[jl]-x) : 1.e+20;
116 fr = (jr<fNIn) ? TMath::Abs(fArray[jr]-x) : 1.e+20;
117 if (jl<0 && jr>=fNIn) break;
118 if (fl < fr) { ff = fl; jl--;}
119 else { ff = fr; jr++;}
120 }
121 ((TH1K*)this)->fKCur = nk - 1;
122 return fNIn * 0.5*fKCur/((float)(fNIn+1))*GetBinWidth(bin)/ff;
123}
124
125
126////////////////////////////////////////////////////////////////////////////////
127/// Return content of global bin error.
128
130{
131 return TMath::Sqrt(((double)(fNIn-fKCur+1))/((fNIn+1)*(fKCur-1)))*GetBinContent(bin);
132}
133
134
135////////////////////////////////////////////////////////////////////////////////
136/// Reset.
137
139{
140 fNIn =0;
141 fReady = 0;
143}
144
145
146////////////////////////////////////////////////////////////////////////////////
147/// Save primitive as a C++ statement(s) on output stream out
148/// Note the following restrictions in the code generated:
149/// - variable bin size not implemented
150/// - Objects in list of functions not saved (fits)
151/// - Contours not saved
152
153void TH1K::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
154{
155 char quote = '"';
156 out<<" "<<std::endl;
157 out<<" "<<"TH1 *";
158
159 out<<GetName()<<" = new "<<ClassName()<<"("<<quote<<GetName()<<quote<<","<<quote<<GetTitle()<<quote
160 <<","<<GetXaxis()->GetNbins()
161 <<","<<GetXaxis()->GetXmin()
162 <<","<<GetXaxis()->GetXmax()
163 <<","<<fKOrd;
164 out <<");"<<std::endl;
165
166 if (fDirectory == nullptr) {
167 out<<" "<<GetName()<<"->SetDirectory(0);"<<std::endl;
168 }
169 if (TestBit(kNoStats)) {
170 out<<" "<<GetName()<<"->SetStats(0);"<<std::endl;
171 }
172 if (fOption.Length() != 0) {
173 out<<" "<<GetName()<<"->SetOption("<<quote<<fOption.Data()<<quote<<");"<<std::endl;
174 }
175
176 if (fNIn) {
177 out<<" Float_t Arr[]={"<<std::endl;
178 for (int i=0; i<fNIn; i++) {
179 out<< fArray[i];
180 if (i != fNIn-1) {out<< ",";} else { out<< "};";}
181 if (i%10 == 9) {out<< std::endl;}
182 }
183 out<< std::endl;
184 out<<" for(int i=0;i<" << fNIn << ";i++)"<<GetName()<<"->Fill(Arr[i]);";
185 out<< std::endl;
186 }
187 SaveFillAttributes(out,GetName(),0,1001);
188 SaveLineAttributes(out,GetName(),1,1,1);
189 SaveMarkerAttributes(out,GetName(),1,1,1);
190 fXaxis.SaveAttributes(out,GetName(),"->GetXaxis()");
191 fYaxis.SaveAttributes(out,GetName(),"->GetYaxis()");
192 fZaxis.SaveAttributes(out,GetName(),"->GetZaxis()");
193 TString opt = option;
194 opt.ToLower();
195 if (!opt.Contains("nodraw")) {
196 out<<" "<<GetName()<<"->Draw("
197 <<quote<<option<<quote<<");"<<std::endl;
198 }
199}
200
201
202////////////////////////////////////////////////////////////////////////////////
203/// Compare.
204
205static int TH1K_fcompare(const void *f1,const void *f2)
206{
207 if (*((float*)f1) < *((float*)f2)) return -1;
208 if (*((float*)f1) > *((float*)f2)) return 1;
209 return 0;
210}
211
212
213////////////////////////////////////////////////////////////////////////////////
214/// Sort.
215
217{
218 if (fNIn<2) return;
220}
float Float_t
Definition RtypesCore.h:57
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:374
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t option
char name[80]
Definition TGX11.cxx:110
static int TH1K_fcompare(const void *f1, const void *f2)
Compare.
Definition TH1K.cxx:205
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 AddAt(Float_t c, Int_t i)
Add float c at position i. Check for out of bounds.
Definition TArrayF.cxx:93
void Set(Int_t n) override
Set size of this array to n floats.
Definition TArrayF.cxx:105
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:239
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:275
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:142
virtual Int_t FindBin(Double_t x)
Find bin number corresponding to abscissa x.
Definition TAxis.cxx:288
void SaveAttributes(std::ostream &out, const char *name, const char *subname) override
Save axis attributes as C++ statement(s) on output stream out.
Definition TAxis.cxx:706
Double_t GetXmin() const
Definition TAxis.h:141
Int_t GetNbins() const
Definition TAxis.h:127
TH1K class supports the nearest K Neighbours method, widely used in cluster analysis.
Definition TH1K.h:26
Double_t GetBinError(Int_t bin) const override
Return content of global bin error.
Definition TH1K.cxx:129
Int_t fReady
Definition TH1K.h:31
Int_t fKOrd
Definition TH1K.h:33
void SavePrimitive(std::ostream &out, Option_t *option="") override
Save primitive as a C++ statement(s) on output stream out Note the following restrictions in the code...
Definition TH1K.cxx:153
Int_t fKCur
Definition TH1K.h:34
Int_t fNIn
Definition TH1K.h:32
Int_t Fill(Double_t x) override
Increment bin with abscissa X by 1.
Definition TH1K.cxx:78
void Copy(TObject &obj) const override
Copy this histogram structure to newth1.
Definition TH1K.cxx:62
TH1K()
Constructor.
Definition TH1K.cxx:39
void Sort()
Sort.
Definition TH1K.cxx:216
Double_t GetBinContent(Int_t bin) const override
Return content of global bin number bin.
Definition TH1K.cxx:101
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:59
virtual Double_t GetBinCenter(Int_t bin) const
Return bin center for 1D histogram.
Definition TH1.cxx:9143
void Copy(TObject &hnew) const override
Copy this histogram structure to newth1.
Definition TH1.cxx:2642
Double_t fTsumw
Total Sum of weights.
Definition TH1.h:107
Double_t fTsumw2
Total Sum of squares of weights.
Definition TH1.h:108
Double_t fTsumwx2
Total Sum of weight*X*X.
Definition TH1.h:110
@ kNoStats
Don't draw stats box.
Definition TH1.h:175
TDirectory * fDirectory
! Pointer to directory holding this histogram
Definition TH1.h:120
virtual void Reset(Option_t *option="")
Reset this histogram: contents, errors, etc.
Definition TH1.cxx:7101
TAxis * GetXaxis()
Definition TH1.h:340
Int_t fDimension
! Histogram dimension (1, 2 or 3 dim)
Definition TH1.h:121
Double_t fEntries
Number of entries.
Definition TH1.h:106
TAxis fZaxis
Z axis descriptor.
Definition TH1.h:103
TAxis fXaxis
X axis descriptor.
Definition TH1.h:101
virtual Double_t GetBinWidth(Int_t bin) const
Return bin width for 1D histogram.
Definition TH1.cxx:9165
Bool_t GetStatOverflowsBehaviour() const
Definition TH1.h:163
TAxis fYaxis
Y axis descriptor.
Definition TH1.h:102
Double_t fTsumwx
Total Sum of weight*X.
Definition TH1.h:109
TString fOption
Histogram options.
Definition TH1.h:116
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
const char * GetTitle() const override
Returns title of object.
Definition TNamed.h:48
Mother of all ROOT objects.
Definition TObject.h:41
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition TObject.h:199
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition TObject.cxx:225
Basic string class.
Definition TString.h:139
Ssiz_t Length() const
Definition TString.h:417
void ToLower()
Change string to lower-case.
Definition TString.cxx:1182
const char * Data() const
Definition TString.h:376
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:632
Double_t x[n]
Definition legend1.C:17
TF1 * f1
Definition legend1.C:11
Double_t Sqrt(Double_t x)
Returns the square root of x.
Definition TMath.h:666
Long64_t BinarySearch(Long64_t n, const T *array, T value)
Binary search in an array of n values to locate value.
Definition TMathBase.h:347
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
Definition TMathBase.h:123