Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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, not supported by TH1K
150
151void TH1K::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
152{
154
155 out << " \n";
156 out << " " << ClassName() << " *" << hname << " = new " << ClassName() << "(\"" << GetName() << "\", \""
157 << TString(GetTitle()).ReplaceSpecialCppChars() << "\"," << GetXaxis()->GetNbins() << ","
158 << GetXaxis()->GetXmin() << "," << GetXaxis()->GetXmax() << "," << fKOrd << ");\n";
159
160 if (fNIn) {
161 std::vector<Double_t> content(fNIn);
162 for (int i = 0; i < fNIn; i++)
163 content[i] = fArray[i];
164
166 out << " for(Int_t i = 0; i < " << fNIn << "; i++)\n";
167 out << " " << hname << "->Fill(" << arrname << "[i]);\n";
168 }
169
171}
172
173////////////////////////////////////////////////////////////////////////////////
174/// Compare.
175
176static int TH1K_fcompare(const void *f1,const void *f2)
177{
178 if (*((float*)f1) < *((float*)f2)) return -1;
179 if (*((float*)f1) > *((float*)f2)) return 1;
180 return 0;
181}
182
183
184////////////////////////////////////////////////////////////////////////////////
185/// Sort.
186
188{
189 if (fNIn<2) return;
191}
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:176
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
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:296
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:151
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:187
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:9133
void Copy(TObject &hnew) const override
Copy this histogram structure to newth1.
Definition TH1.cxx:2643
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
virtual void Reset(Option_t *option="")
Reset this histogram: contents, errors, etc.
Definition TH1.cxx:7106
TAxis * GetXaxis()
Definition TH1.h:341
TString ProvideSaveName(Option_t *option, Bool_t testfdir=kFALSE)
Provide variable name for histogram for saving as primitive Histogram pointer has by default the hist...
Definition TH1.cxx:7245
Int_t fDimension
! Histogram dimension (1, 2 or 3 dim)
Definition TH1.h:121
virtual void SavePrimitiveHelp(std::ostream &out, const char *hname, Option_t *option="")
Helper function for the SavePrimitive functions from TH1 or classes derived from TH1,...
Definition TH1.cxx:7372
Double_t fEntries
Number of entries.
Definition TH1.h:106
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:9155
Bool_t GetStatOverflowsBehaviour() const
Definition TH1.h:164
Double_t fTsumwx
Total Sum of weight*X.
Definition TH1.h:109
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
const char * GetTitle() const override
Returns title of object.
Definition TNamed.h:50
Mother of all ROOT objects.
Definition TObject.h:41
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition TObject.cxx:226
static TString SavePrimitiveArray(std::ostream &out, const char *prefix, Int_t len, Double_t *arr, Bool_t empty_line=kFALSE)
Save array in the output stream "out".
Definition TObject.cxx:786
Basic string class.
Definition TString.h:139
TString & ReplaceSpecialCppChars()
Find special characters which are typically used in printf() calls and replace them by appropriate es...
Definition TString.cxx:1114
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