CommitLineData
4c039060 1/**************************************************************************
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
15
f531a546 16// \$Id\$
4c039060 17
959fbac5 18///////////////////////////////////////////////////////////////////////////
19// Class AliSample
20// Perform statistics on various multi-dimensional data samples
21// A data sample can be filled using the "Enter" and/or "Remove" functions,
22// whereas the "Reset" function resets the complete sample to 'empty'.
23// The info which can be extracted from a certain data sample are the
24// sum, mean, variance, sigma, covariance and correlation.
25// The "Info" function provides all statistics data for a certain sample.
26// The variables for which these stat. parameters have to be calculated
27// are indicated by the index of the variable which is passed as an
28// argument to the various member functions.
29// The index convention for a data point (x,y) is : x=1 y=2
30//
31// Example :
32// ---------
33// For an AliSample s a data point (x,y) can be entered as s.Enter(x,y) and
34// the mean_x can be obtained as s.GetMean(1) whereas the mean_y is obtained
35// via s.GetMean(2).
36// The correlation between x and y is available via s.GetCor(1,2).
37// The x-statistics are obtained via s.Info(1), y-statistics via s.Info(2),
38// and the covariance and correlation between x and y via s.Info(1,2).
39// All statistics of a sample are obtained via s.Info().
40//
41//--- Author: Nick van Eijndhoven 30-mar-1996 CERN Geneva
f531a546 42//- Modified: NvE \$Date\$ UU-SAP Utrecht
959fbac5 43///////////////////////////////////////////////////////////////////////////
44
d88f97cc 45#include "AliSample.h"
46
47AliSample::AliSample()
48{
49// Creation of an Aliample object and resetting the statistics values
50// The dimension is initialised to maximum
51 fDim=fMaxdim;
52 fNames[0]='X';
53 fNames[1]='Y';
54 fNames[2]='Z';
55 fN=0;
56 Reset();
57}
58///////////////////////////////////////////////////////////////////////////
59AliSample::~AliSample()
60{
61// Default destructor
62}
63///////////////////////////////////////////////////////////////////////////
64void AliSample::Reset()
65{
66// Resetting the statistics values for a certain Sample object
67// Dimension is NOT changed
68 fN=0;
69 for (Int_t i=0; i<fDim; i++)
70 {
71 fSum[i]=0.;
72 fMean[i]=0.;
73 fVar[i]=0.;
74 fSigma[i]=0.;
75 for (Int_t j=0; j<fDim; j++)
76 {
77 fSum2[i][j]=0.;
78 fCov[i][j]=0.;
79 fCor[i][j]=0.;
80 }
81 }
82}
83///////////////////////////////////////////////////////////////////////////
84void AliSample::Enter(Float_t x)
85{
86// Entering a value into a 1-dim. sample
87// In case of first entry the dimension is set to 1
88 if (fN == 0)
89 {
90 fDim=1;
91 fNames[0]='X';
92 fNames[1]='-';
93 fNames[2]='-';
94 }
95 if (fDim != 1)
96 {
97 cout << " *AliSample::enter* Error : Not a 1-dim sample." << endl;
98 }
99 else
100 {
101 fN+=1;
102 fSum[0]+=x;
103 fSum2[0][0]+=x*x;
104 Compute();
105 }
106}
107///////////////////////////////////////////////////////////////////////////
108void AliSample::Remove(Float_t x)
109{
110// Removing a value from a 1-dim. sample
111 if (fDim != 1)
112 {
113 cout << " *AliSample::remove* Error : Not a 1-dim sample." << endl;
114 }
115 else
116 {
117 fN-=1;
118 fSum[0]-=x;
119 fSum2[0][0]-=x*x;
120 Compute();
121 }
122}
123///////////////////////////////////////////////////////////////////////////
124void AliSample::Enter(Float_t x,Float_t y)
125{
126// Entering a pair (x,y) into a 2-dim. sample
127// In case of first entry the dimension is set to 2
128 if (fN == 0)
129 {
130 fDim=2;
131 fNames[0]='X';
132 fNames[1]='Y';
133 fNames[2]='-';
134 }
135 if (fDim != 2)
136 {
137 cout << " *AliSample::enter* Error : Not a 2-dim sample." << endl;
138 }
139 else
140 {
141 fN+=1;
142 fSum[0]+=x;
143 fSum[1]+=y;
144 fSum2[0][0]+=x*x;
145 fSum2[0][1]+=x*y;
146 fSum2[1][0]+=y*x;
147 fSum2[1][1]+=y*y;
148 Compute();
149 }
150}
151///////////////////////////////////////////////////////////////////////////
152void AliSample::Remove(Float_t x,Float_t y)
153{
154// Removing a pair (x,y) from a 2-dim. sample
155 if (fDim != 2)
156 {
157 cout << " *AliSample::remove* Error : Not a 2-dim sample." << endl;
158 }
159 else
160 {
161 fN-=1;
162 fSum[0]-=x;
163 fSum[1]-=y;
164 fSum2[0][0]-=x*x;
165 fSum2[0][1]-=x*y;
166 fSum2[1][0]-=y*x;
167 fSum2[1][1]-=y*y;
168 Compute();
169 }
170}
171///////////////////////////////////////////////////////////////////////////
172void AliSample::Enter(Float_t x,Float_t y,Float_t z)
173{
174// Entering a set (x,y,z) into a 3-dim. sample
175// In case of first entry the dimension is set to 3
176 if (fN == 0)
177 {
178 fDim=3;
179 fNames[0]='X';
180 fNames[1]='Y';
181 fNames[2]='Z';
182 }
183 if (fDim != 3)
184 {
185 cout << " *AliSample::enter* Error : Not a 3-dim sample." << endl;
186 }
187 else
188 {
189 fN+=1;
190 fSum[0]+=x;
191 fSum[1]+=y;
192 fSum[2]+=z;
193 fSum2[0][0]+=x*x;
194 fSum2[0][1]+=x*y;
195 fSum2[0][2]+=x*z;
196 fSum2[1][0]+=y*x;
197 fSum2[1][1]+=y*y;
198 fSum2[1][2]+=y*z;
199 fSum2[2][0]+=z*x;
200 fSum2[2][1]+=z*y;
201 fSum2[2][2]+=z*z;
202 Compute();
203 }
204}
205///////////////////////////////////////////////////////////////////////////
206void AliSample::Remove(Float_t x,Float_t y,Float_t z)
207{
208// Removing a set (x,y,z) from a 3-dim. sample
209 if (fDim != 3)
210 {
211 cout << " *AliSample::remove* Error : Not a 3-dim sample." << endl;
212 }
213 else
214 {
215 fN-=1;
216 fSum[0]-=x;
217 fSum[1]-=y;
218 fSum[2]-=z;
219 fSum2[0][0]-=x*x;
220 fSum2[0][1]-=x*y;
221 fSum2[0][2]-=x*z;
222 fSum2[1][0]-=y*x;
223 fSum2[1][1]-=y*y;
224 fSum2[1][2]-=y*z;
225 fSum2[2][0]-=z*x;
226 fSum2[2][1]-=z*y;
227 fSum2[2][2]-=z*z;
228 Compute();
229 }
230}
231///////////////////////////////////////////////////////////////////////////
232void AliSample::Compute()
233{
234// Computation of the various statistical values
235// after each entering or removing action on a certain sample
236 Float_t rn=fN;
237 for (Int_t k=0; k<fDim; k++)
238 {
239 fMean[k]=fSum[k]/rn;
240 fVar[k]=(fSum2[k][k]/rn)-(fMean[k]*fMean[k]);
241 if (fVar[k] < 0.) fVar[k]=0.;
242 fSigma[k]=sqrt(fVar[k]);
243 }
244 for (Int_t i=0; i<fDim; i++)
245 {
246 for (Int_t j=0; j<fDim; j++)
247 {
248 fCov[i][j]=(fSum2[i][j]/rn)-(fMean[i]*fMean[j]);
249 Float_t sigij=fSigma[i]*fSigma[j];
250 if (sigij != 0.) fCor[i][j]=fCov[i][j]/sigij;
251 }
252 }
253}
254///////////////////////////////////////////////////////////////////////////
255Int_t AliSample::GetDimension()
256{
257// Provide the dimension of a certain sample
258 return fDim;
259}
260///////////////////////////////////////////////////////////////////////////
261Int_t AliSample::GetN()
262{
263// Provide the number of entries of a certain sample
264 return fN;
265}
266///////////////////////////////////////////////////////////////////////////
267Float_t AliSample::GetSum(Int_t i)
268{
269// Provide the sum of a certain variable
270 if (fDim < i)
271 {
272 cout << " *AliSample::sum* Error : Dimension less than " << i << endl;
273 return 0.;
274 }
275 else
276 {
277 return fSum[i-1];
278 }
279}
280///////////////////////////////////////////////////////////////////////////
281Float_t AliSample::GetMean(Int_t i)
282{
283// Provide the mean of a certain variable
284 if (fDim < i)
285 {
286 cout << " *AliSample::mean* Error : Dimension less than " << i << endl;
287 return 0.;
288 }
289 else
290 {
291 return fMean[i-1];
292 }
293}
294///////////////////////////////////////////////////////////////////////////
295Float_t AliSample::GetVar(Int_t i)
296{
297// Provide the variance of a certain variable
298 if (fDim < i)
299 {
300 cout << " *AliSample::var* Error : Dimension less than " << i << endl;
301 return 0.;
302 }
303 else
304 {
305 return fVar[i-1];
306 }
307}
308///////////////////////////////////////////////////////////////////////////
309Float_t AliSample::GetSigma(Int_t i)
310{
311// Provide the standard deviation of a certain variable
312 if (fDim < i)
313 {
314 cout << " *AliSample::sigma* Error : Dimension less than " << i << endl;
315 return 0.;
316 }
317 else
318 {
319 return fSigma[i-1];
320 }
321}
322///////////////////////////////////////////////////////////////////////////
323Float_t AliSample::GetCov(Int_t i,Int_t j)
324{
325// Provide the covariance between variables i and j
326 if ((fDim < i) || (fDim < j))
327 {
328 Int_t k=i;
329 if (j > i) k=j;
330 cout << " *AliSample::cov* Error : Dimension less than " << k << endl;
331 return 0.;
332 }
333 else
334 {
335 return fCov[i-1][j-1];
336 }
337}
338///////////////////////////////////////////////////////////////////////////
339Float_t AliSample::GetCor(Int_t i,Int_t j)
340{
341// Provide the correlation between variables i and j
342 if ((fDim < i) || (fDim < j))
343 {
344 Int_t k=i;
345 if (j > i) k=j;
346 cout << " *AliSample::cor* Error : Dimension less than " << k << endl;
347 return 0.;
348 }
349 else
350 {
351 return fCor[i-1][j-1];
352 }
353}
354///////////////////////////////////////////////////////////////////////////
355void AliSample::Info()
356{
357// Printing of statistics of all variables
358 for (Int_t i=0; i<fDim; i++)
359 {
360 cout << " " << fNames[i] << " : N = " << fN;
361 cout << " Sum = " << fSum[i] << " Mean = " << fMean[i];
362 cout << " Var = " << fVar[i] << " Sigma = " << fSigma[i] << endl;
363 }
364}
365///////////////////////////////////////////////////////////////////////////
366void AliSample::Info(Int_t i)
367{
368// Printing of statistics of ith variable
369 if (fDim < i)
370 {
371 cout << " *AliSample::Info(i)* Error : Dimension less than " << i << endl;
372 }
373 else
374 {
375 cout << " " << fNames[i-1] << " : N = " << fN;
376 cout << " Sum = " << fSum[i-1] << " Mean = " << fMean[i-1];
377 cout << " Var = " << fVar[i-1] << " Sigma = " << fSigma[i-1] << endl;
378 }
379}
380///////////////////////////////////////////////////////////////////////////
381void AliSample::Info(Int_t i,Int_t j)
382{
383// Printing of covariance and correlation between variables i and j
384 if ((fDim < i) || (fDim < j))
385 {
386 Int_t k=i;
387 if (j > i) k=j;
388 cout << " *AliSample::Info(i,j)* Error : Dimension less than " << k << endl;
389 }
390 else
391 {
392 cout << " " << fNames[i-1] << "-" << fNames[j-1] << " correlation :";
393 cout << " Cov. = " << fCov[i-1][j-1] << " Cor. = " << fCor[i-1][j-1] << endl;
394 }
395}
396///////////////////////////////////////////////////////////////////////////