2 /* Copyright (C) 2007 Christian Holm Christensen <cholm@nbi.dk>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public License
6 * as published by the Free Software Foundation; either version 2.1 of
7 * the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 #ifndef ALIFMDFLOWSTAT_H
20 #define ALIFMDFLOWSTAT_H
25 //______________________________________________________
26 /** @class AliFMDFlowStat flow/AliFMDFlowStat.h <flow/AliFMDFlowStat.h>
27 @brief Single variable statistics.
30 This class calculates the single variable statistics of a random
33 The average @f$\bar{x}@f$ after @f$n@f$ observations is calculated as
35 \bar{x}^{(n)} = \left\{\begin{array}{rl}
37 \left(1-\frac1n\right)\bar{x}^{(n-1)} + \frac{x^{(n)}}{n} & n > 1
40 and the square variance @f$s^2@f$ after @f$ n@f$ observations is
43 {s^{2}}^{(n)} = \left\{\begin{array}{rl}
45 \left(1-\frac1n\right){s^{2}}^{(n-1)} +
46 \frac{\left(x^{(n)}-\bar{x}^{(n)}\right)^2}{n-1} & n > 1
50 class AliFMDFlowStat : public TObject
54 AliFMDFlowStat() : fAverage(0), fSqVar(0), fN(0) {}
56 @param o Object to copy from. */
57 AliFMDFlowStat(const AliFMDFlowStat& o);
58 /** Assuignement operator
59 @param o Object to assign from.
60 @return Reference to this */
61 AliFMDFlowStat& operator=(const AliFMDFlowStat& o);
63 virtual ~AliFMDFlowStat() {}
65 void Clear(Option_t* option="");
66 /** Add another data point */
68 /** Get the average */
69 Double_t Average() const { return fAverage; }
70 /** Get the square variance */
71 Double_t SqVar() const { return fSqVar; }
72 /** Get the number of data points */
73 ULong_t N() const { return fN; }
74 /** Given the two partial sample of the same stocastic variable, of
75 size @f$ n_1@f$ and @f$ n_2@f$, with averages @f$ m_1@f$ and
76 @f$ m2@f$, and square variances, @f$ s_1^2@f$ and @f$ s_2^2@f$,
77 calculate the full sample average and square variance:
79 m = \frac{n_1 m_1 + n_2 m_2}{n_1 + n_2}
83 s^2 = \frac{n_1 * s_1^2 + n_2 s_2^2}{n_1 + n_2}
84 + \frac{n_1 n_2 (m_1 - m_2)^2}{(n_1 + n_2)^2}
86 @param o Other statistics object to add
87 @return Reference to this */
88 AliFMDFlowStat& operator+=(const AliFMDFlowStat& o);
91 Double_t fAverage; // Running
92 /** Square variance */
93 Double_t fSqVar; // Square variance
94 /** Number of data points */
95 ULong_t fN; // Number of data points
96 /** Define for ROOT I/O */
97 ClassDef(AliFMDFlowStat,1);
100 //__________________________________________________________________
102 AliFMDFlowStat::AliFMDFlowStat(const AliFMDFlowStat& o)
104 fAverage(o.fAverage),
108 //__________________________________________________________________
109 inline AliFMDFlowStat&
110 AliFMDFlowStat::operator=(const AliFMDFlowStat& o)
112 fAverage = o.fAverage;
117 //__________________________________________________________________
118 inline AliFMDFlowStat&
119 AliFMDFlowStat::operator+=(const AliFMDFlowStat& o)
121 if (fN + o.fN <= 0) return *this;
123 Double_t m = (fN * o.fAverage + o.fN * o.fAverage)/(fN + o.fN);
124 Double_t s = ((fN * o.fSqVar + o.fN * o.fSqVar)/(fN + o.fN)
125 + (TMath::Power(fAverage - o.fAverage, 2) * fN * o.fN)
126 / TMath::Power(fN + o.fN, 2));
132 //__________________________________________________________________
134 AliFMDFlowStat::Clear(Option_t*)
136 fAverage = fSqVar = 0;
140 //__________________________________________________________________
142 AliFMDFlowStat::Add(Double_t x)
144 if (TMath::IsNaN(x) || !TMath::Finite(x)) return;
151 Double_t c = (1. - 1. / fN);
155 fSqVar += TMath::Power(x - fAverage, 2) / (fN - 1);