]> git.uio.no Git - u/mrichter/AliRoot.git/blame - RALICE/AliSample.cxx
Add calibration macros by Raphaelle
[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;
219e9b7e 59 fStore=0;
60 fX=0;
61 fY=0;
62 fZ=0;
63 fArr=0;
d88f97cc 64 Reset();
65}
66///////////////////////////////////////////////////////////////////////////
67AliSample::~AliSample()
68{
69// Default destructor
219e9b7e 70 if (fX)
71 {
72 delete fX;
73 fX=0;
74 }
75 if (fY)
76 {
77 delete fY;
78 fY=0;
79 }
80 if (fZ)
81 {
82 delete fZ;
83 fZ=0;
84 }
85 if (fArr)
86 {
87 delete fArr;
88 fArr=0;
89 }
d88f97cc 90}
91///////////////////////////////////////////////////////////////////////////
92void AliSample::Reset()
93{
94// Resetting the statistics values for a certain Sample object
219e9b7e 95// Dimension and storage flag are NOT changed
d88f97cc 96 fN=0;
97 for (Int_t i=0; i<fDim; i++)
98 {
99 fSum[i]=0.;
100 fMean[i]=0.;
101 fVar[i]=0.;
102 fSigma[i]=0.;
103 for (Int_t j=0; j<fDim; j++)
104 {
105 fSum2[i][j]=0.;
106 fCov[i][j]=0.;
107 fCor[i][j]=0.;
108 }
109 }
219e9b7e 110
111 // Set storage arrays to initial size
112 if (fX) fX->Set(10);
113 if (fY) fY->Set(10);
114 if (fZ) fZ->Set(10);
d88f97cc 115}
116///////////////////////////////////////////////////////////////////////////
117void AliSample::Enter(Float_t x)
118{
119// Entering a value into a 1-dim. sample
120// In case of first entry the dimension is set to 1
121 if (fN == 0)
122 {
123 fDim=1;
124 fNames[0]='X';
125 fNames[1]='-';
126 fNames[2]='-';
127 }
128 if (fDim != 1)
129 {
130 cout << " *AliSample::enter* Error : Not a 1-dim sample." << endl;
131 }
132 else
133 {
134 fN+=1;
135 fSum[0]+=x;
136 fSum2[0][0]+=x*x;
219e9b7e 137
138 // Store all entered data when storage mode has been selected
139 if (fStore)
140 {
141 if (!fX) fX=new TArrayF(10);
142 if (fX->GetSize() < fN) fX->Set(fN+10);
143 fX->AddAt(x,fN-1);
144 }
145
146 // Compute the various statistics
d88f97cc 147 Compute();
148 }
149}
150///////////////////////////////////////////////////////////////////////////
151void AliSample::Remove(Float_t x)
152{
153// Removing a value from a 1-dim. sample
219e9b7e 154
155 if (!fN) return;
156
d88f97cc 157 if (fDim != 1)
158 {
159 cout << " *AliSample::remove* Error : Not a 1-dim sample." << endl;
160 }
161 else
162 {
163 fN-=1;
164 fSum[0]-=x;
165 fSum2[0][0]-=x*x;
219e9b7e 166
167 // Remove data entry from the storage
168 if (fStore && fX)
169 {
170 Float_t val=0;
171 for (Int_t i=0; i<=fN; i++)
172 {
173 val=fX->At(i);
174 if (fabs(x-val)>1.e-10) continue;
175
176 for (Int_t j=i+1; j<=fN; j++)
177 {
178 val=fX->At(j);
179 fX->AddAt(val,j-1);
180 }
181 }
182 }
183
184 // Compute the various statistics
d88f97cc 185 Compute();
186 }
187}
188///////////////////////////////////////////////////////////////////////////
189void AliSample::Enter(Float_t x,Float_t y)
190{
191// Entering a pair (x,y) into a 2-dim. sample
192// In case of first entry the dimension is set to 2
193 if (fN == 0)
194 {
195 fDim=2;
196 fNames[0]='X';
197 fNames[1]='Y';
198 fNames[2]='-';
199 }
200 if (fDim != 2)
201 {
202 cout << " *AliSample::enter* Error : Not a 2-dim sample." << endl;
203 }
204 else
205 {
206 fN+=1;
207 fSum[0]+=x;
208 fSum[1]+=y;
209 fSum2[0][0]+=x*x;
210 fSum2[0][1]+=x*y;
211 fSum2[1][0]+=y*x;
212 fSum2[1][1]+=y*y;
219e9b7e 213
214 // Store all entered data when storage mode has been selected
215 if (fStore)
216 {
217 if (!fX) fX=new TArrayF(10);
218 if (fX->GetSize() < fN) fX->Set(fN+10);
219 fX->AddAt(x,fN-1);
220 if (!fY) fY=new TArrayF(10);
221 if (fY->GetSize() < fN) fY->Set(fN+10);
222 fY->AddAt(y,fN-1);
223 }
224
225 // Compute the various statistics
d88f97cc 226 Compute();
227 }
228}
229///////////////////////////////////////////////////////////////////////////
230void AliSample::Remove(Float_t x,Float_t y)
231{
232// Removing a pair (x,y) from a 2-dim. sample
219e9b7e 233
234 if (!fN) return;
235
d88f97cc 236 if (fDim != 2)
237 {
238 cout << " *AliSample::remove* Error : Not a 2-dim sample." << endl;
239 }
240 else
241 {
242 fN-=1;
243 fSum[0]-=x;
244 fSum[1]-=y;
245 fSum2[0][0]-=x*x;
246 fSum2[0][1]-=x*y;
247 fSum2[1][0]-=y*x;
248 fSum2[1][1]-=y*y;
219e9b7e 249
250 // Remove data entry from the storage
251 if (fStore && fX && fY)
252 {
253 Float_t val=0;
254 for (Int_t i=0; i<=fN; i++)
255 {
256 val=fX->At(i);
257 if (fabs(x-val)>1.e-10) continue;
258 val=fY->At(i);
259 if (fabs(y-val)>1.e-10) continue;
260
261 for (Int_t j=i+1; j<=fN; j++)
262 {
263 val=fX->At(j);
264 fX->AddAt(val,j-1);
265 val=fY->At(j);
266 fY->AddAt(val,j-1);
267 }
268 }
269 }
270
271 // Compute the various statistics
d88f97cc 272 Compute();
273 }
274}
275///////////////////////////////////////////////////////////////////////////
276void AliSample::Enter(Float_t x,Float_t y,Float_t z)
277{
278// Entering a set (x,y,z) into a 3-dim. sample
279// In case of first entry the dimension is set to 3
280 if (fN == 0)
281 {
282 fDim=3;
283 fNames[0]='X';
284 fNames[1]='Y';
285 fNames[2]='Z';
286 }
287 if (fDim != 3)
288 {
289 cout << " *AliSample::enter* Error : Not a 3-dim sample." << endl;
290 }
291 else
292 {
293 fN+=1;
294 fSum[0]+=x;
295 fSum[1]+=y;
296 fSum[2]+=z;
297 fSum2[0][0]+=x*x;
298 fSum2[0][1]+=x*y;
299 fSum2[0][2]+=x*z;
300 fSum2[1][0]+=y*x;
301 fSum2[1][1]+=y*y;
302 fSum2[1][2]+=y*z;
303 fSum2[2][0]+=z*x;
304 fSum2[2][1]+=z*y;
305 fSum2[2][2]+=z*z;
219e9b7e 306
307 // Store all entered data when storage mode has been selected
308 if (fStore)
309 {
310 if (!fX) fX=new TArrayF(10);
311 if (fX->GetSize() < fN) fX->Set(fN+10);
312 fX->AddAt(x,fN-1);
313 if (!fY) fY=new TArrayF(10);
314 if (fY->GetSize() < fN) fY->Set(fN+10);
315 fY->AddAt(y,fN-1);
316 if (!fZ) fZ=new TArrayF(10);
317 if (fZ->GetSize() < fN) fZ->Set(fN+10);
318 fZ->AddAt(z,fN-1);
319 }
320
321 // Compute the various statistics
d88f97cc 322 Compute();
323 }
324}
325///////////////////////////////////////////////////////////////////////////
326void AliSample::Remove(Float_t x,Float_t y,Float_t z)
327{
328// Removing a set (x,y,z) from a 3-dim. sample
219e9b7e 329
330 if (!fN) return;
331
d88f97cc 332 if (fDim != 3)
333 {
334 cout << " *AliSample::remove* Error : Not a 3-dim sample." << endl;
335 }
336 else
337 {
338 fN-=1;
339 fSum[0]-=x;
340 fSum[1]-=y;
341 fSum[2]-=z;
342 fSum2[0][0]-=x*x;
343 fSum2[0][1]-=x*y;
344 fSum2[0][2]-=x*z;
345 fSum2[1][0]-=y*x;
346 fSum2[1][1]-=y*y;
347 fSum2[1][2]-=y*z;
348 fSum2[2][0]-=z*x;
349 fSum2[2][1]-=z*y;
350 fSum2[2][2]-=z*z;
219e9b7e 351
352 // Remove data entry from the storage
353 if (fStore && fX && fY && fZ)
354 {
355 Float_t val=0;
356 for (Int_t i=0; i<=fN; i++)
357 {
358 val=fX->At(i);
359 if (fabs(x-val)>1.e-10) continue;
360 val=fY->At(i);
361 if (fabs(y-val)>1.e-10) continue;
362 val=fZ->At(i);
363 if (fabs(z-val)>1.e-10) continue;
364
365 for (Int_t j=i+1; j<=fN; j++)
366 {
367 val=fX->At(j);
368 fX->AddAt(val,j-1);
369 val=fY->At(j);
370 fY->AddAt(val,j-1);
371 val=fZ->At(j);
372 fZ->AddAt(val,j-1);
373 }
374 }
375 }
376
377 // Compute the various statistics
d88f97cc 378 Compute();
379 }
380}
381///////////////////////////////////////////////////////////////////////////
382void AliSample::Compute()
383{
384// Computation of the various statistical values
385// after each entering or removing action on a certain sample
386 Float_t rn=fN;
387 for (Int_t k=0; k<fDim; k++)
388 {
389 fMean[k]=fSum[k]/rn;
390 fVar[k]=(fSum2[k][k]/rn)-(fMean[k]*fMean[k]);
391 if (fVar[k] < 0.) fVar[k]=0.;
392 fSigma[k]=sqrt(fVar[k]);
393 }
394 for (Int_t i=0; i<fDim; i++)
395 {
396 for (Int_t j=0; j<fDim; j++)
397 {
398 fCov[i][j]=(fSum2[i][j]/rn)-(fMean[i]*fMean[j]);
399 Float_t sigij=fSigma[i]*fSigma[j];
400 if (sigij != 0.) fCor[i][j]=fCov[i][j]/sigij;
401 }
402 }
403}
404///////////////////////////////////////////////////////////////////////////
261c0caf 405Int_t AliSample::GetDimension() const
d88f97cc 406{
407// Provide the dimension of a certain sample
408 return fDim;
409}
410///////////////////////////////////////////////////////////////////////////
261c0caf 411Int_t AliSample::GetN() const
d88f97cc 412{
413// Provide the number of entries of a certain sample
414 return fN;
415}
416///////////////////////////////////////////////////////////////////////////
261c0caf 417Float_t AliSample::GetSum(Int_t i) const
d88f97cc 418{
419// Provide the sum of a certain variable
420 if (fDim < i)
421 {
422 cout << " *AliSample::sum* Error : Dimension less than " << i << endl;
423 return 0.;
424 }
425 else
426 {
427 return fSum[i-1];
428 }
429}
430///////////////////////////////////////////////////////////////////////////
261c0caf 431Float_t AliSample::GetMean(Int_t i) const
d88f97cc 432{
433// Provide the mean of a certain variable
434 if (fDim < i)
435 {
436 cout << " *AliSample::mean* Error : Dimension less than " << i << endl;
437 return 0.;
438 }
439 else
440 {
441 return fMean[i-1];
442 }
443}
444///////////////////////////////////////////////////////////////////////////
261c0caf 445Float_t AliSample::GetVar(Int_t i) const
d88f97cc 446{
447// Provide the variance of a certain variable
448 if (fDim < i)
449 {
450 cout << " *AliSample::var* Error : Dimension less than " << i << endl;
451 return 0.;
452 }
453 else
454 {
455 return fVar[i-1];
456 }
457}
458///////////////////////////////////////////////////////////////////////////
261c0caf 459Float_t AliSample::GetSigma(Int_t i) const
d88f97cc 460{
461// Provide the standard deviation of a certain variable
462 if (fDim < i)
463 {
464 cout << " *AliSample::sigma* Error : Dimension less than " << i << endl;
465 return 0.;
466 }
467 else
468 {
469 return fSigma[i-1];
470 }
471}
472///////////////////////////////////////////////////////////////////////////
261c0caf 473Float_t AliSample::GetCov(Int_t i,Int_t j) const
d88f97cc 474{
475// Provide the covariance between variables i and j
476 if ((fDim < i) || (fDim < j))
477 {
478 Int_t k=i;
479 if (j > i) k=j;
480 cout << " *AliSample::cov* Error : Dimension less than " << k << endl;
481 return 0.;
482 }
483 else
484 {
485 return fCov[i-1][j-1];
486 }
487}
488///////////////////////////////////////////////////////////////////////////
261c0caf 489Float_t AliSample::GetCor(Int_t i,Int_t j) const
d88f97cc 490{
491// Provide the correlation between variables i and j
492 if ((fDim < i) || (fDim < j))
493 {
494 Int_t k=i;
495 if (j > i) k=j;
496 cout << " *AliSample::cor* Error : Dimension less than " << k << endl;
497 return 0.;
498 }
499 else
500 {
501 return fCor[i-1][j-1];
502 }
503}
504///////////////////////////////////////////////////////////////////////////
219e9b7e 505void AliSample::Data()
d88f97cc 506{
507// Printing of statistics of all variables
508 for (Int_t i=0; i<fDim; i++)
509 {
510 cout << " " << fNames[i] << " : N = " << fN;
511 cout << " Sum = " << fSum[i] << " Mean = " << fMean[i];
219e9b7e 512 cout << " Var = " << fVar[i] << " Sigma = " << fSigma[i];
513 if (fStore) cout << " Median = " << GetMedian(i+1);
514 cout << endl;
d88f97cc 515 }
516}
517///////////////////////////////////////////////////////////////////////////
219e9b7e 518void AliSample::Data(Int_t i)
d88f97cc 519{
520// Printing of statistics of ith variable
521 if (fDim < i)
522 {
84bb7c66 523 cout << " *AliSample::Data(i)* Error : Dimension less than " << i << endl;
d88f97cc 524 }
525 else
526 {
527 cout << " " << fNames[i-1] << " : N = " << fN;
528 cout << " Sum = " << fSum[i-1] << " Mean = " << fMean[i-1];
219e9b7e 529 cout << " Var = " << fVar[i-1] << " Sigma = " << fSigma[i-1];
530 if (fStore) cout << " Median = " << GetMedian(i);
531 cout << endl;
d88f97cc 532 }
533}
534///////////////////////////////////////////////////////////////////////////
261c0caf 535void AliSample::Data(Int_t i,Int_t j) const
d88f97cc 536{
537// Printing of covariance and correlation between variables i and j
538 if ((fDim < i) || (fDim < j))
539 {
540 Int_t k=i;
541 if (j > i) k=j;
84bb7c66 542 cout << " *AliSample::Data(i,j)* Error : Dimension less than " << k << endl;
d88f97cc 543 }
544 else
545 {
546 cout << " " << fNames[i-1] << "-" << fNames[j-1] << " correlation :";
547 cout << " Cov. = " << fCov[i-1][j-1] << " Cor. = " << fCor[i-1][j-1] << endl;
548 }
549}
550///////////////////////////////////////////////////////////////////////////
219e9b7e 551void AliSample::SetStoreMode(Int_t mode)
552{
553// Set storage mode for all entered data.
554//
555// mode = 0 : Entered data will not be stored
556// 1 : All data will be stored as entered
557//
558// By default the storage mode is set to 0 in the constructor of this class.
559// The default at invokation of this memberfunction is mode=1.
560//
561// For normal statistics evaluation (e.g. mean, sigma, covariance etc...)
562// storage of entered data is not needed. This is the default mode
563// of operation and is the most efficient w.r.t. cpu time and memory.
564// However, when calculation of a median is required, then the data
565// storage mode has be activated.
566//
567// Note : Activation of storage mode can only be performed before the
568// first data item is entered.
569
570 if (fN)
571 {
572 cout << " *AliSample::SetStore* Storage mode can only be set before first data." << endl;
573 }
574 else
575 {
576 if (mode==0 || mode==1) fStore=mode;
577 }
578}
579///////////////////////////////////////////////////////////////////////////
580Int_t AliSample::GetStoreMode() const
581{
582// Provide the storage mode
583 return fStore;
584}
585///////////////////////////////////////////////////////////////////////////
586Float_t AliSample::GetMedian(Int_t i)
587{
588// Provide the median of a certain variable
589
590 if (fDim < i)
591 {
592 cout << " *AliSample::mean* Error : Dimension less than " << i << endl;
593 return 0;
594 }
595
596 if (!fStore)
597 {
598 cout << " *AliSample::GetMedian* Error : Storage of data entries was not activated." << endl;
599 return 0;
600 }
601
602 // Prepare temp. array to hold the ordered values
603 if (!fArr)
604 {
605 fArr=new TArrayF(fN);
606 }
607 else
608 {
609 if (fArr->GetSize() < fN) fArr->Set(fN);
610 }
611
612 // Order the values of the specified variable
613 Float_t val=0;
614 Int_t iadd=0;
615 for (Int_t j=0; j<fN; j++)
616 {
617 if (i==1) val=fX->At(j);
618 if (i==2) val=fY->At(j);
619 if (i==3) val=fZ->At(j);
620
621 iadd=0;
622 if (j==0)
623 {
624 fArr->AddAt(val,j);
625 iadd=1;
626 }
627 else
628 {
629 for (Int_t k=0; k<j; k++)
630 {
631 if (val>=fArr->At(k)) continue;
632 // Put value in between the existing ones
633 for (Int_t m=j-1; m>=k; m--)
634 {
635 fArr->AddAt(fArr->At(m),m+1);
636 }
637 fArr->AddAt(val,k);
638 iadd=1;
639 break;
640 }
641
642 if (!iadd)
643 {
644 fArr->AddAt(val,j);
645 }
646 }
647 }
648
649 Float_t median=0;
650 Int_t index=fN/2;
651 if (fN%2) // Odd number of entries
652 {
653 median=fArr->At(index);
654 }
655 else // Even number of entries
656 {
657 median=(fArr->At(index-1)+fArr->At(index))/2.;
658 }
659 return median;
660}
661///////////////////////////////////////////////////////////////////////////