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