1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
18 ///////////////////////////////////////////////////////////////////////////
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 "Data" 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
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
36 // The correlation between x and y is available via s.GetCor(1,2).
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().
41 //--- Author: Nick van Eijndhoven 30-mar-1996 CERN Geneva
42 //- Modified: NvE $Date$ UU-SAP Utrecht
43 ///////////////////////////////////////////////////////////////////////////
45 #include "AliSample.h"
46 #include "Riostream.h"
48 ClassImp(AliSample) // Class implementation to enable ROOT I/O
50 AliSample::AliSample()
52 // Creation of an Aliample object and resetting the statistics values
53 // The dimension is initialised to maximum
66 ///////////////////////////////////////////////////////////////////////////
67 AliSample::~AliSample()
91 ///////////////////////////////////////////////////////////////////////////
92 void AliSample::Reset()
94 // Resetting the statistics values for a certain Sample object
95 // Dimension and storage flag are NOT changed
97 for (Int_t i=0; i<fDim; i++)
103 for (Int_t j=0; j<fDim; j++)
111 // Set storage arrays to initial size
116 ///////////////////////////////////////////////////////////////////////////
117 void AliSample::Enter(Float_t x)
119 // Entering a value into a 1-dim. sample
120 // In case of first entry the dimension is set to 1
130 cout << " *AliSample::enter* Error : Not a 1-dim sample." << endl;
138 // Store all entered data when storage mode has been selected
141 if (!fX) fX=new TArrayF(10);
142 if (fX->GetSize() < fN) fX->Set(fN+10);
146 // Compute the various statistics
150 ///////////////////////////////////////////////////////////////////////////
151 void AliSample::Remove(Float_t x)
153 // Removing a value from a 1-dim. sample
159 cout << " *AliSample::remove* Error : Not a 1-dim sample." << endl;
167 // Remove data entry from the storage
171 for (Int_t i=0; i<=fN; i++)
174 if (fabs(x-val)>1.e-10) continue;
176 for (Int_t j=i+1; j<=fN; j++)
184 // Compute the various statistics
188 ///////////////////////////////////////////////////////////////////////////
189 void AliSample::Enter(Float_t x,Float_t y)
191 // Entering a pair (x,y) into a 2-dim. sample
192 // In case of first entry the dimension is set to 2
202 cout << " *AliSample::enter* Error : Not a 2-dim sample." << endl;
214 // Store all entered data when storage mode has been selected
217 if (!fX) fX=new TArrayF(10);
218 if (fX->GetSize() < fN) fX->Set(fN+10);
220 if (!fY) fY=new TArrayF(10);
221 if (fY->GetSize() < fN) fY->Set(fN+10);
225 // Compute the various statistics
229 ///////////////////////////////////////////////////////////////////////////
230 void AliSample::Remove(Float_t x,Float_t y)
232 // Removing a pair (x,y) from a 2-dim. sample
238 cout << " *AliSample::remove* Error : Not a 2-dim sample." << endl;
250 // Remove data entry from the storage
251 if (fStore && fX && fY)
254 for (Int_t i=0; i<=fN; i++)
257 if (fabs(x-val)>1.e-10) continue;
259 if (fabs(y-val)>1.e-10) continue;
261 for (Int_t j=i+1; j<=fN; j++)
271 // Compute the various statistics
275 ///////////////////////////////////////////////////////////////////////////
276 void AliSample::Enter(Float_t x,Float_t y,Float_t z)
278 // Entering a set (x,y,z) into a 3-dim. sample
279 // In case of first entry the dimension is set to 3
289 cout << " *AliSample::enter* Error : Not a 3-dim sample." << endl;
307 // Store all entered data when storage mode has been selected
310 if (!fX) fX=new TArrayF(10);
311 if (fX->GetSize() < fN) fX->Set(fN+10);
313 if (!fY) fY=new TArrayF(10);
314 if (fY->GetSize() < fN) fY->Set(fN+10);
316 if (!fZ) fZ=new TArrayF(10);
317 if (fZ->GetSize() < fN) fZ->Set(fN+10);
321 // Compute the various statistics
325 ///////////////////////////////////////////////////////////////////////////
326 void AliSample::Remove(Float_t x,Float_t y,Float_t z)
328 // Removing a set (x,y,z) from a 3-dim. sample
334 cout << " *AliSample::remove* Error : Not a 3-dim sample." << endl;
352 // Remove data entry from the storage
353 if (fStore && fX && fY && fZ)
356 for (Int_t i=0; i<=fN; i++)
359 if (fabs(x-val)>1.e-10) continue;
361 if (fabs(y-val)>1.e-10) continue;
363 if (fabs(z-val)>1.e-10) continue;
365 for (Int_t j=i+1; j<=fN; j++)
377 // Compute the various statistics
381 ///////////////////////////////////////////////////////////////////////////
382 void AliSample::Compute()
384 // Computation of the various statistical values
385 // after each entering or removing action on a certain sample
387 for (Int_t k=0; k<fDim; k++)
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]);
394 for (Int_t i=0; i<fDim; i++)
396 for (Int_t j=0; j<fDim; j++)
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;
404 ///////////////////////////////////////////////////////////////////////////
405 Int_t AliSample::GetDimension() const
407 // Provide the dimension of a certain sample
410 ///////////////////////////////////////////////////////////////////////////
411 Int_t AliSample::GetN() const
413 // Provide the number of entries of a certain sample
416 ///////////////////////////////////////////////////////////////////////////
417 Float_t AliSample::GetSum(Int_t i) const
419 // Provide the sum of a certain variable
422 cout << " *AliSample::sum* Error : Dimension less than " << i << endl;
430 ///////////////////////////////////////////////////////////////////////////
431 Float_t AliSample::GetMean(Int_t i) const
433 // Provide the mean of a certain variable
436 cout << " *AliSample::mean* Error : Dimension less than " << i << endl;
444 ///////////////////////////////////////////////////////////////////////////
445 Float_t AliSample::GetVar(Int_t i) const
447 // Provide the variance of a certain variable
450 cout << " *AliSample::var* Error : Dimension less than " << i << endl;
458 ///////////////////////////////////////////////////////////////////////////
459 Float_t AliSample::GetSigma(Int_t i) const
461 // Provide the standard deviation of a certain variable
464 cout << " *AliSample::sigma* Error : Dimension less than " << i << endl;
472 ///////////////////////////////////////////////////////////////////////////
473 Float_t AliSample::GetCov(Int_t i,Int_t j) const
475 // Provide the covariance between variables i and j
476 if ((fDim < i) || (fDim < j))
480 cout << " *AliSample::cov* Error : Dimension less than " << k << endl;
485 return fCov[i-1][j-1];
488 ///////////////////////////////////////////////////////////////////////////
489 Float_t AliSample::GetCor(Int_t i,Int_t j) const
491 // Provide the correlation between variables i and j
492 if ((fDim < i) || (fDim < j))
496 cout << " *AliSample::cor* Error : Dimension less than " << k << endl;
501 return fCor[i-1][j-1];
504 ///////////////////////////////////////////////////////////////////////////
505 void AliSample::Data()
507 // Printing of statistics of all variables
508 for (Int_t i=0; i<fDim; i++)
510 cout << " " << fNames[i] << " : N = " << fN;
511 cout << " Sum = " << fSum[i] << " Mean = " << fMean[i];
512 cout << " Var = " << fVar[i] << " Sigma = " << fSigma[i];
516 cout << " Minimum = " << GetMinimum(i+1);
517 cout << " Maximum = " << GetMaximum(i+1);
518 cout << " Median = " << GetMedian(i+1);
523 ///////////////////////////////////////////////////////////////////////////
524 void AliSample::Data(Int_t i)
526 // Printing of statistics of ith variable
529 cout << " *AliSample::Data(i)* Error : Dimension less than " << i << endl;
533 cout << " " << fNames[i-1] << " : N = " << fN;
534 cout << " Sum = " << fSum[i-1] << " Mean = " << fMean[i-1];
535 cout << " Var = " << fVar[i-1] << " Sigma = " << fSigma[i-1];
539 cout << " Minimum = " << GetMinimum(i);
540 cout << " Maximum = " << GetMaximum(i);
541 cout << " Median = " << GetMedian(i);
546 ///////////////////////////////////////////////////////////////////////////
547 void AliSample::Data(Int_t i,Int_t j) const
549 // Printing of covariance and correlation between variables i and j
550 if ((fDim < i) || (fDim < j))
554 cout << " *AliSample::Data(i,j)* Error : Dimension less than " << k << endl;
558 cout << " " << fNames[i-1] << "-" << fNames[j-1] << " correlation :";
559 cout << " Cov. = " << fCov[i-1][j-1] << " Cor. = " << fCor[i-1][j-1] << endl;
562 ///////////////////////////////////////////////////////////////////////////
563 void AliSample::SetStoreMode(Int_t mode)
565 // Set storage mode for all entered data.
567 // mode = 0 : Entered data will not be stored
568 // 1 : All data will be stored as entered
570 // By default the storage mode is set to 0 in the constructor of this class.
571 // The default at invokation of this memberfunction is mode=1.
573 // For normal statistics evaluation (e.g. mean, sigma, covariance etc...)
574 // storage of entered data is not needed. This is the default mode
575 // of operation and is the most efficient w.r.t. cpu time and memory.
576 // However, when calculation of a median, minimum or maximum is required,
577 // then the data storage mode has be activated.
579 // Note : Activation of storage mode can only be performed before the
580 // first data item is entered.
584 cout << " *AliSample::SetStore* Storage mode can only be set before first data." << endl;
588 if (mode==0 || mode==1) fStore=mode;
591 ///////////////////////////////////////////////////////////////////////////
592 Int_t AliSample::GetStoreMode() const
594 // Provide the storage mode
597 ///////////////////////////////////////////////////////////////////////////
598 Float_t AliSample::GetMedian(Int_t i)
600 // Provide the median of a certain variable.
601 // For this functionality the storage mode has to be activated.
605 cout << " *AliSample::GetMedian* Error : Dimension less than " << i << endl;
611 cout << " *AliSample::GetMedian* Error : Storage of data entries was not activated." << endl;
621 if (i==1) median=fX->At(0);
622 if (i==2) median=fY->At(0);
623 if (i==3) median=fZ->At(0);
627 // Prepare temp. array to hold the ordered values
630 fArr=new TArrayF(fN);
634 if (fArr->GetSize() < fN) fArr->Set(fN);
637 // Order the values of the specified variable
640 for (Int_t j=0; j<fN; j++)
642 if (i==1) val=fX->At(j);
643 if (i==2) val=fY->At(j);
644 if (i==3) val=fZ->At(j);
654 for (Int_t k=0; k<j; k++)
656 if (val>=fArr->At(k)) continue;
657 // Put value in between the existing ones
658 for (Int_t m=j-1; m>=k; m--)
660 fArr->AddAt(fArr->At(m),m+1);
676 if (fN%2) // Odd number of entries
678 median=fArr->At(index);
680 else // Even number of entries
682 median=(fArr->At(index-1)+fArr->At(index))/2.;
686 ///////////////////////////////////////////////////////////////////////////
687 Float_t AliSample::GetMinimum(Int_t i) const
689 // Provide the minimum value of a certain variable.
690 // For this functionality the storage mode has to be activated.
694 cout << " *AliSample::GetMinimum* Error : Dimension less than " << i << endl;
700 cout << " *AliSample::GetMinimum* Error : Storage of data entries was not activated." << endl;
708 if (i==1) min=fX->At(0);
709 if (i==2) min=fY->At(0);
710 if (i==3) min=fZ->At(0);
712 for (Int_t k=1; k<fN; k++)
714 if (i==1 && fX->At(k)<min) min=fX->At(k);
715 if (i==2 && fY->At(k)<min) min=fY->At(k);
716 if (i==3 && fZ->At(k)<min) min=fZ->At(k);
721 ///////////////////////////////////////////////////////////////////////////
722 Float_t AliSample::GetMaximum(Int_t i) const
724 // Provide the maxmum value of a certain variable.
725 // For this functionality the storage mode has to be activated.
729 cout << " *AliSample::GetMaximum* Error : Dimension less than " << i << endl;
735 cout << " *AliSample::GetMaximum* Error : Storage of data entries was not activated." << endl;
743 if (i==1) max=fX->At(0);
744 if (i==2) max=fY->At(0);
745 if (i==3) max=fZ->At(0);
747 for (Int_t k=1; k<fN; k++)
749 if (i==1 && fX->At(k)>max) max=fX->At(k);
750 if (i==2 && fY->At(k)>max) max=fY->At(k);
751 if (i==3 && fZ->At(k)>max) max=fZ->At(k);
756 ///////////////////////////////////////////////////////////////////////////