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
33\deprecated The `TH1K` class is deprecated and will be removed in 6.40. It did not implement the `TH1` interface consistently, and limited the usability of the k-neighbors method it implemented by closely coupling the algorithm with the histogram class. Please use the new `TMath::KNNDensity` function that implements the same mathematical logic.
34*/
35
36
37
38////////////////////////////////////////////////////////////////////////////////
39/// Constructor.
40
42{
43 fDimension = 1;
44}
45
46
47////////////////////////////////////////////////////////////////////////////////
48/// Create a 1-Dim histogram with fix bins of type float
49/// (see TH1K::TH1 for explanation of parameters)
50
51TH1K::TH1K(const char *name,const char *title,Int_t nbins,Double_t xlow,Double_t xup,Int_t k)
52 : TH1(name,title,nbins,xlow,xup), TArrayF(100)
53{
54 fDimension = 1;
55 fKOrd = k;
56}
57
58////////////////////////////////////////////////////////////////////////////////
59/// Copy this histogram structure to newth1.
60///
61/// Note that this function does not copy the list of associated functions.
62/// Use TObject::Clone to make a full copy of an histogram.
63
64void TH1K::Copy(TObject &obj) const
65{
66 TH1::Copy(obj);
67 ((TH1K&)obj).fNIn = fNIn;
68}
69
70////////////////////////////////////////////////////////////////////////////////
71/// Increment bin with abscissa X by 1.
72///
73/// if x is less than the low-edge of the first bin, the Underflow bin is incremented
74/// if x is greater than the upper edge of last bin, the Overflow bin is incremented
75///
76/// If the storage of the sum of squares of weights has been triggered,
77/// via the function Sumw2, then the sum of the squares of weights is incremented
78/// by 1 in the bin corresponding to x.
79
81{
82 fReady = 0;
83 Int_t bin;
84 fEntries++;
85 bin =fXaxis.FindBin(x);
86 if (bin == 0 || bin > fXaxis.GetNbins()) {
87 if (!GetStatOverflowsBehaviour()) return -1;
88 }
89 ++fTsumw;
90 ++fTsumw2;
91 fTsumwx += x;
92 fTsumwx2 += x*x;
93 fReady = 0;
94 if (fNIn == fN) Set(fN*2);
95 AddAt(x,fNIn++);
96 return bin;
97}
98
99
100////////////////////////////////////////////////////////////////////////////////
101/// Return content of global bin number bin.
102
104{
105 if (!fReady) {
106 ((TH1K*)this)->Sort();
107 ((TH1K*)this)->fReady=1;
108 }
109 if (!fNIn) return 0.;
110 float x = GetBinCenter(bin);
111 int left = TMath::BinarySearch(fNIn,fArray,x);
112 int jl=left,jr=left+1,nk,nkmax =fKOrd;
113 float fl,fr,ff=0.,ffmin=1.e-6;
114 if (!nkmax) {nkmax = 3; ffmin = GetBinWidth(bin);}
115 if (nkmax >= fNIn) nkmax = fNIn-1;
116 for (nk = 1; nk <= nkmax || ff <= ffmin; nk++) {
117 fl = (jl>=0 ) ? TMath::Abs(fArray[jl]-x) : 1.e+20;
118 fr = (jr<fNIn) ? TMath::Abs(fArray[jr]-x) : 1.e+20;
119 if (jl<0 && jr>=fNIn) break;
120 if (fl < fr) { ff = fl; jl--;}
121 else { ff = fr; jr++;}
122 }
123 ((TH1K*)this)->fKCur = nk - 1;
124 return fNIn * 0.5*fKCur/((float)(fNIn+1))*GetBinWidth(bin)/ff;
125}
126
127
128////////////////////////////////////////////////////////////////////////////////
129/// Return content of global bin error.
130
132{
133 return TMath::Sqrt(((double)(fNIn-fKCur+1))/((fNIn+1)*(fKCur-1)))*GetBinContent(bin);
134}
135
136
137////////////////////////////////////////////////////////////////////////////////
138/// Reset.
139
141{
142 fNIn =0;
143 fReady = 0;
145}
146
147
148////////////////////////////////////////////////////////////////////////////////
149/// Save primitive as a C++ statement(s) on output stream out
150/// Note the following restrictions in the code generated:
151/// - variable bin size not implemented, not supported by TH1K
152
153void TH1K::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
154{
156
157 out << " \n";
158 out << " " << ClassName() << " *" << hname << " = new " << ClassName() << "(\"" << GetName() << "\", \""
159 << TString(GetTitle()).ReplaceSpecialCppChars() << "\"," << GetXaxis()->GetNbins() << ","
160 << GetXaxis()->GetXmin() << "," << GetXaxis()->GetXmax() << "," << fKOrd << ");\n";
161
162 if (fNIn) {
163 std::vector<Double_t> content(fNIn);
164 for (int i = 0; i < fNIn; i++)
165 content[i] = fArray[i];
166
168 out << " for(Int_t i = 0; i < " << fNIn << "; i++)\n";
169 out << " " << hname << "->Fill(" << vectname << "[i]);\n";
170 }
171
173}
174
175////////////////////////////////////////////////////////////////////////////////
176/// Compare.
177
178static int TH1K_fcompare(const void *f1,const void *f2)
179{
180 if (*((float*)f1) < *((float*)f2)) return -1;
181 if (*((float*)f1) > *((float*)f2)) return 1;
182 return 0;
183}
184
185
186////////////////////////////////////////////////////////////////////////////////
187/// Sort.
188
190{
191 if (fNIn<2) return;
193}
float Float_t
Float 4 bytes (float)
Definition RtypesCore.h:71
const char Option_t
Option string (const char)
Definition RtypesCore.h:80
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:178
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:92
void Set(Int_t n) override
Set size of this array to n floats.
Definition TArrayF.cxx:104
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:292
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:131
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:80
void Copy(TObject &obj) const override
Copy this histogram structure to newth1.
Definition TH1K.cxx:64
TH1K()
Constructor.
Definition TH1K.cxx:41
void Sort()
Sort.
Definition TH1K.cxx:189
Double_t GetBinContent(Int_t bin) const override
Return content of global bin number bin.
Definition TH1K.cxx:103
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:109
virtual Double_t GetBinCenter(Int_t bin) const
Return bin center for 1D histogram.
Definition TH1.cxx:9179
void Copy(TObject &hnew) const override
Copy this histogram structure to newth1.
Definition TH1.cxx:2653
Double_t fTsumw
Total Sum of weights.
Definition TH1.h:157
Double_t fTsumw2
Total Sum of squares of weights.
Definition TH1.h:158
Double_t fTsumwx2
Total Sum of weight*X*X.
Definition TH1.h:160
virtual void Reset(Option_t *option="")
Reset this histogram: contents, errors, etc.
Definition TH1.cxx:7151
TAxis * GetXaxis()
Definition TH1.h:572
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:7290
Int_t fDimension
! Histogram dimension (1, 2 or 3 dim)
Definition TH1.h:171
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:7417
Double_t fEntries
Number of entries.
Definition TH1.h:156
TAxis fXaxis
X axis descriptor.
Definition TH1.h:151
virtual Double_t GetBinWidth(Int_t bin) const
Return bin width for 1D histogram.
Definition TH1.cxx:9201
Bool_t GetStatOverflowsBehaviour() const
Definition TH1.h:392
Double_t fTsumwx
Total Sum of weight*X.
Definition TH1.h:159
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
static TString SavePrimitiveVector(std::ostream &out, const char *prefix, Int_t len, Double_t *arr, Bool_t empty_line=kFALSE)
Save array in the output stream "out" as vector.
Definition TObject.cxx:788
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition TObject.cxx:226
Basic string class.
Definition TString.h:138
TString & ReplaceSpecialCppChars()
Find special characters which are typically used in printf() calls and replace them by appropriate es...
Definition TString.cxx:1121
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:673
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:348
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
Definition TMathBase.h:124