11-oct-2005 NvE SetMJD invoked at the end of AliTimestamp::Add instead of just updati...
[u/mrichter/AliRoot.git] / RALICE / AliSample.cxx
CommitLineData
4c039060 1/**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
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
6516b62d 20// Perform statistics on various multi-dimensional data samples.
959fbac5 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.
84bb7c66 25// The "Data" function provides all statistics data for a certain sample.
959fbac5 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).
84bb7c66 37// The x-statistics are obtained via s.Data(1), y-statistics via s.Data(2),
38// and the covariance and correlation between x and y via s.Data(1,2).
39// All statistics of a sample are obtained via s.Data().
959fbac5 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"
c72198f1 46#include "Riostream.h"
d88f97cc 47
6516b62d 48ClassImp(AliSample) // Class implementation to enable ROOT I/O
49
d88f97cc 50AliSample::AliSample()
51{
52// Creation of an Aliample object and resetting the statistics values
53// The dimension is initialised to maximum
54 fDim=fMaxdim;
55 fNames[0]='X';
56 fNames[1]='Y';
57 fNames[2]='Z';
58 fN=0;
59 Reset();
60}
61///////////////////////////////////////////////////////////////////////////
62AliSample::~AliSample()
63{
64// Default destructor
65}
66///////////////////////////////////////////////////////////////////////////
67void AliSample::Reset()
68{
69// Resetting the statistics values for a certain Sample object
70// Dimension is NOT changed
71 fN=0;
72 for (Int_t i=0; i<fDim; i++)
73 {
74 fSum[i]=0.;
75 fMean[i]=0.;
76 fVar[i]=0.;
77 fSigma[i]=0.;
78 for (Int_t j=0; j<fDim; j++)
79 {
80 fSum2[i][j]=0.;
81 fCov[i][j]=0.;
82 fCor[i][j]=0.;
83 }
84 }
85}
86///////////////////////////////////////////////////////////////////////////
87void AliSample::Enter(Float_t x)
88{
89// Entering a value into a 1-dim. sample
90// In case of first entry the dimension is set to 1
91 if (fN == 0)
92 {
93 fDim=1;
94 fNames[0]='X';
95 fNames[1]='-';
96 fNames[2]='-';
97 }
98 if (fDim != 1)
99 {
100 cout << " *AliSample::enter* Error : Not a 1-dim sample." << endl;
101 }
102 else
103 {
104 fN+=1;
105 fSum[0]+=x;
106 fSum2[0][0]+=x*x;
107 Compute();
108 }
109}
110///////////////////////////////////////////////////////////////////////////
111void AliSample::Remove(Float_t x)
112{
113// Removing a value from a 1-dim. sample
114 if (fDim != 1)
115 {
116 cout << " *AliSample::remove* Error : Not a 1-dim sample." << endl;
117 }
118 else
119 {
120 fN-=1;
121 fSum[0]-=x;
122 fSum2[0][0]-=x*x;
123 Compute();
124 }
125}
126///////////////////////////////////////////////////////////////////////////
127void AliSample::Enter(Float_t x,Float_t y)
128{
129// Entering a pair (x,y) into a 2-dim. sample
130// In case of first entry the dimension is set to 2
131 if (fN == 0)
132 {
133 fDim=2;
134 fNames[0]='X';
135 fNames[1]='Y';
136 fNames[2]='-';
137 }
138 if (fDim != 2)
139 {
140 cout << " *AliSample::enter* Error : Not a 2-dim sample." << endl;
141 }
142 else
143 {
144 fN+=1;
145 fSum[0]+=x;
146 fSum[1]+=y;
147 fSum2[0][0]+=x*x;
148 fSum2[0][1]+=x*y;
149 fSum2[1][0]+=y*x;
150 fSum2[1][1]+=y*y;
151 Compute();
152 }
153}
154///////////////////////////////////////////////////////////////////////////
155void AliSample::Remove(Float_t x,Float_t y)
156{
157// Removing a pair (x,y) from a 2-dim. sample
158 if (fDim != 2)
159 {
160 cout << " *AliSample::remove* Error : Not a 2-dim sample." << endl;
161 }
162 else
163 {
164 fN-=1;
165 fSum[0]-=x;
166 fSum[1]-=y;
167 fSum2[0][0]-=x*x;
168 fSum2[0][1]-=x*y;
169 fSum2[1][0]-=y*x;
170 fSum2[1][1]-=y*y;
171 Compute();
172 }
173}
174///////////////////////////////////////////////////////////////////////////
175void AliSample::Enter(Float_t x,Float_t y,Float_t z)
176{
177// Entering a set (x,y,z) into a 3-dim. sample
178// In case of first entry the dimension is set to 3
179 if (fN == 0)
180 {
181 fDim=3;
182 fNames[0]='X';
183 fNames[1]='Y';
184 fNames[2]='Z';
185 }
186 if (fDim != 3)
187 {
188 cout << " *AliSample::enter* Error : Not a 3-dim sample." << endl;
189 }
190 else
191 {
192 fN+=1;
193 fSum[0]+=x;
194 fSum[1]+=y;
195 fSum[2]+=z;
196 fSum2[0][0]+=x*x;
197 fSum2[0][1]+=x*y;
198 fSum2[0][2]+=x*z;
199 fSum2[1][0]+=y*x;
200 fSum2[1][1]+=y*y;
201 fSum2[1][2]+=y*z;
202 fSum2[2][0]+=z*x;
203 fSum2[2][1]+=z*y;
204 fSum2[2][2]+=z*z;
205 Compute();
206 }
207}
208///////////////////////////////////////////////////////////////////////////
209void AliSample::Remove(Float_t x,Float_t y,Float_t z)
210{
211// Removing a set (x,y,z) from a 3-dim. sample
212 if (fDim != 3)
213 {
214 cout << " *AliSample::remove* Error : Not a 3-dim sample." << endl;
215 }
216 else
217 {
218 fN-=1;
219 fSum[0]-=x;
220 fSum[1]-=y;
221 fSum[2]-=z;
222 fSum2[0][0]-=x*x;
223 fSum2[0][1]-=x*y;
224 fSum2[0][2]-=x*z;
225 fSum2[1][0]-=y*x;
226 fSum2[1][1]-=y*y;
227 fSum2[1][2]-=y*z;
228 fSum2[2][0]-=z*x;
229 fSum2[2][1]-=z*y;
230 fSum2[2][2]-=z*z;
231 Compute();
232 }
233}
234///////////////////////////////////////////////////////////////////////////
235void AliSample::Compute()
236{
237// Computation of the various statistical values
238// after each entering or removing action on a certain sample
239 Float_t rn=fN;
240 for (Int_t k=0; k<fDim; k++)
241 {
242 fMean[k]=fSum[k]/rn;
243 fVar[k]=(fSum2[k][k]/rn)-(fMean[k]*fMean[k]);
244 if (fVar[k] < 0.) fVar[k]=0.;
245 fSigma[k]=sqrt(fVar[k]);
246 }
247 for (Int_t i=0; i<fDim; i++)
248 {
249 for (Int_t j=0; j<fDim; j++)
250 {
251 fCov[i][j]=(fSum2[i][j]/rn)-(fMean[i]*fMean[j]);
252 Float_t sigij=fSigma[i]*fSigma[j];
253 if (sigij != 0.) fCor[i][j]=fCov[i][j]/sigij;
254 }
255 }
256}
257///////////////////////////////////////////////////////////////////////////
261c0caf 258Int_t AliSample::GetDimension() const
d88f97cc 259{
260// Provide the dimension of a certain sample
261 return fDim;
262}
263///////////////////////////////////////////////////////////////////////////
261c0caf 264Int_t AliSample::GetN() const
d88f97cc 265{
266// Provide the number of entries of a certain sample
267 return fN;
268}
269///////////////////////////////////////////////////////////////////////////
261c0caf 270Float_t AliSample::GetSum(Int_t i) const
d88f97cc 271{
272// Provide the sum of a certain variable
273 if (fDim < i)
274 {
275 cout << " *AliSample::sum* Error : Dimension less than " << i << endl;
276 return 0.;
277 }
278 else
279 {
280 return fSum[i-1];
281 }
282}
283///////////////////////////////////////////////////////////////////////////
261c0caf 284Float_t AliSample::GetMean(Int_t i) const
d88f97cc 285{
286// Provide the mean of a certain variable
287 if (fDim < i)
288 {
289 cout << " *AliSample::mean* Error : Dimension less than " << i << endl;
290 return 0.;
291 }
292 else
293 {
294 return fMean[i-1];
295 }
296}
297///////////////////////////////////////////////////////////////////////////
261c0caf 298Float_t AliSample::GetVar(Int_t i) const
d88f97cc 299{
300// Provide the variance of a certain variable
301 if (fDim < i)
302 {
303 cout << " *AliSample::var* Error : Dimension less than " << i << endl;
304 return 0.;
305 }
306 else
307 {
308 return fVar[i-1];
309 }
310}
311///////////////////////////////////////////////////////////////////////////
261c0caf 312Float_t AliSample::GetSigma(Int_t i) const
d88f97cc 313{
314// Provide the standard deviation of a certain variable
315 if (fDim < i)
316 {
317 cout << " *AliSample::sigma* Error : Dimension less than " << i << endl;
318 return 0.;
319 }
320 else
321 {
322 return fSigma[i-1];
323 }
324}
325///////////////////////////////////////////////////////////////////////////
261c0caf 326Float_t AliSample::GetCov(Int_t i,Int_t j) const
d88f97cc 327{
328// Provide the covariance between variables i and j
329 if ((fDim < i) || (fDim < j))
330 {
331 Int_t k=i;
332 if (j > i) k=j;
333 cout << " *AliSample::cov* Error : Dimension less than " << k << endl;
334 return 0.;
335 }
336 else
337 {
338 return fCov[i-1][j-1];
339 }
340}
341///////////////////////////////////////////////////////////////////////////
261c0caf 342Float_t AliSample::GetCor(Int_t i,Int_t j) const
d88f97cc 343{
344// Provide the correlation between variables i and j
345 if ((fDim < i) || (fDim < j))
346 {
347 Int_t k=i;
348 if (j > i) k=j;
349 cout << " *AliSample::cor* Error : Dimension less than " << k << endl;
350 return 0.;
351 }
352 else
353 {
354 return fCor[i-1][j-1];
355 }
356}
357///////////////////////////////////////////////////////////////////////////
261c0caf 358void AliSample::Data() const
d88f97cc 359{
360// Printing of statistics of all variables
361 for (Int_t i=0; i<fDim; i++)
362 {
363 cout << " " << fNames[i] << " : N = " << fN;
364 cout << " Sum = " << fSum[i] << " Mean = " << fMean[i];
365 cout << " Var = " << fVar[i] << " Sigma = " << fSigma[i] << endl;
366 }
367}
368///////////////////////////////////////////////////////////////////////////
261c0caf 369void AliSample::Data(Int_t i) const
d88f97cc 370{
371// Printing of statistics of ith variable
372 if (fDim < i)
373 {
84bb7c66 374 cout << " *AliSample::Data(i)* Error : Dimension less than " << i << endl;
d88f97cc 375 }
376 else
377 {
378 cout << " " << fNames[i-1] << " : N = " << fN;
379 cout << " Sum = " << fSum[i-1] << " Mean = " << fMean[i-1];
380 cout << " Var = " << fVar[i-1] << " Sigma = " << fSigma[i-1] << endl;
381 }
382}
383///////////////////////////////////////////////////////////////////////////
261c0caf 384void AliSample::Data(Int_t i,Int_t j) const
d88f97cc 385{
386// Printing of covariance and correlation between variables i and j
387 if ((fDim < i) || (fDim < j))
388 {
389 Int_t k=i;
390 if (j > i) k=j;
84bb7c66 391 cout << " *AliSample::Data(i,j)* Error : Dimension less than " << k << endl;
d88f97cc 392 }
393 else
394 {
395 cout << " " << fNames[i-1] << "-" << fNames[j-1] << " correlation :";
396 cout << " Cov. = " << fCov[i-1][j-1] << " Cor. = " << fCor[i-1][j-1] << endl;
397 }
398}
399///////////////////////////////////////////////////////////////////////////