1 | #include "sopnamsp.h"
|
---|
2 | #include "machdefs.h"
|
---|
3 | #include <string.h>
|
---|
4 | #include <stdio.h>
|
---|
5 | #include <math.h>
|
---|
6 | #include "perrors.h"
|
---|
7 | #include "fioarr.h"
|
---|
8 | #include "histerr.h"
|
---|
9 |
|
---|
10 | /*!
|
---|
11 | \class SOPHYA::HistoErr
|
---|
12 | \ingroup HiStats
|
---|
13 | Classe d'histogrammes 1D avec erreurs donnees par l'utilisateur
|
---|
14 | */
|
---|
15 |
|
---|
16 | /********* Methode *********/
|
---|
17 | /*! Constructeur par defaut */
|
---|
18 | HistoErr::HistoErr(void)
|
---|
19 | : xmin_(1.), xmax_(-1.), nx_(0), dx_(0.)
|
---|
20 | , mMean(0)
|
---|
21 | {
|
---|
22 | }
|
---|
23 |
|
---|
24 | /********* Methode *********/
|
---|
25 | /*! Constructeur d'un histo */
|
---|
26 | HistoErr::HistoErr(r_8 xmin,r_8 xmax,int_4 nx)
|
---|
27 | : mMean(0)
|
---|
28 | {
|
---|
29 | CreateOrResize(xmin,xmax,nx);
|
---|
30 | }
|
---|
31 |
|
---|
32 | /********* Methode *********/
|
---|
33 | /*! Constructeur par copie */
|
---|
34 | HistoErr::HistoErr(const HistoErr& H)
|
---|
35 | : mMean(H.mMean)
|
---|
36 | {
|
---|
37 | if(H.nx_<=0) return;
|
---|
38 | CreateOrResize(H.xmin_,H.xmax_,H.nx_);
|
---|
39 | data_ = H.data_;
|
---|
40 | err2_ = H.err2_;
|
---|
41 | ndata_ = H.ndata_;
|
---|
42 | }
|
---|
43 |
|
---|
44 | /********* Methode *********/
|
---|
45 | /*! Destructeur */
|
---|
46 | HistoErr::~HistoErr(void)
|
---|
47 | {
|
---|
48 | mMean = 0;
|
---|
49 | }
|
---|
50 |
|
---|
51 | /********* Methode *********/
|
---|
52 | /*! Gestion de l'allocation */
|
---|
53 | void HistoErr::CreateOrResize(r_8 xmin,r_8 xmax,int_4 nx)
|
---|
54 | {
|
---|
55 | xmin_ = xmin; xmax_ = xmax; nx_ = nx; dx_=0.;
|
---|
56 | if(nx_>0) {
|
---|
57 | data_.ReSize(nx_); data_ = 0.;
|
---|
58 | err2_.ReSize(nx_); err2_ = 0.;
|
---|
59 | ndata_.ReSize(nx_); ndata_ = 0.;
|
---|
60 | dx_ = (xmax_-xmin_)/nx_;
|
---|
61 | }
|
---|
62 | mMean = 0;
|
---|
63 | }
|
---|
64 |
|
---|
65 | /********* Methode *********/
|
---|
66 | /*!
|
---|
67 | Remise a zero
|
---|
68 | */
|
---|
69 | void HistoErr::Zero(void)
|
---|
70 | {
|
---|
71 | if(nx_<=0) return;
|
---|
72 | data_ = 0.;
|
---|
73 | err2_ = 0.;
|
---|
74 | ndata_ = 0.;
|
---|
75 | }
|
---|
76 |
|
---|
77 | /********* Methode *********/
|
---|
78 | /*!
|
---|
79 | Recompute XMin (and XMax so that
|
---|
80 | the CENTER of the first bin is exactly XMin and
|
---|
81 | the CENTER of the last bin is exactly XMax.
|
---|
82 | Remember that otherwise
|
---|
83 | XMin is the beginning of the first bin
|
---|
84 | and XMax is the end of the last bin
|
---|
85 | */
|
---|
86 | void HistoErr::ReCenterBin(void)
|
---|
87 | {
|
---|
88 | if(nx_<=1) return;
|
---|
89 | double dx = (xmax_-xmin_)/(nx_-1);
|
---|
90 | xmin_ -= dx/2.;
|
---|
91 | xmax_ += dx/2.;
|
---|
92 | dx_ = (xmax_-xmin_)/nx_;
|
---|
93 | }
|
---|
94 |
|
---|
95 | /********* Methode *********/
|
---|
96 | /*!
|
---|
97 | Compute the mean histogram.
|
---|
98 | Each bin content is divided by the number of entries in the bin.
|
---|
99 | Each squared error is divided by the number of entries in the bin.
|
---|
100 | The number of entries by bin is NOT set to 1
|
---|
101 | (calling ToMean many time will change the histogram !)
|
---|
102 | */
|
---|
103 | void HistoErr::ToMean(void)
|
---|
104 | {
|
---|
105 | if(nx_<1) return;
|
---|
106 | mMean++;
|
---|
107 | for(int_4 i=0;i<nx_;i++) {
|
---|
108 | if(ndata_(i)<1.) continue;
|
---|
109 | data_(i) /= ndata_(i);
|
---|
110 | err2_(i) /= ndata_(i);
|
---|
111 | }
|
---|
112 | return;
|
---|
113 | }
|
---|
114 |
|
---|
115 | /********* Methode *********/
|
---|
116 | /*!
|
---|
117 | Recompute back the original HistoErr after ToMean action
|
---|
118 | */
|
---|
119 | void HistoErr::FromMean(void)
|
---|
120 | {
|
---|
121 | if(nx_<1) return;
|
---|
122 | mMean--;
|
---|
123 | for(int_4 i=0;i<nx_;i++) {
|
---|
124 | if(ndata_(i)<1.) continue;
|
---|
125 | data_(i) *= ndata_(i);
|
---|
126 | err2_(i) *= ndata_(i);
|
---|
127 | }
|
---|
128 | return;
|
---|
129 | }
|
---|
130 |
|
---|
131 | /********* Methode *********/
|
---|
132 | /*!
|
---|
133 | Compute the mean histogram and replace the "error table" by the variance.
|
---|
134 | This should be done if Add(x,w,w) has been used.
|
---|
135 | The "value table" is divided by the number of entries to get the mean
|
---|
136 | The "error table" is replace by the variance
|
---|
137 | The number of entries by bin is NOT set to 1
|
---|
138 | (calling ToMean many time will change the histogram !)
|
---|
139 | Mixing ToMean and ToVariance leads to unpredictable results
|
---|
140 | */
|
---|
141 | void HistoErr::ToVariance(void)
|
---|
142 | {
|
---|
143 | if(nx_<1) return;
|
---|
144 | mMean++;
|
---|
145 | for(int_4 i=0;i<nx_;i++) {
|
---|
146 | if(ndata_(i)<1.) continue;
|
---|
147 | data_(i) /= ndata_(i);
|
---|
148 | err2_(i) = err2_(i)/ndata_(i) - data_(i)*data_(i);
|
---|
149 | }
|
---|
150 | return;
|
---|
151 | }
|
---|
152 |
|
---|
153 | /********* Methode *********/
|
---|
154 | /*!
|
---|
155 | Recompute back the original HistoErr after ToVariance action
|
---|
156 | Mixing FromMean and FromVariance leads to unpredictable results
|
---|
157 | */
|
---|
158 | void HistoErr::FromVariance(void)
|
---|
159 | {
|
---|
160 | if(nx_<1) return;
|
---|
161 | mMean--;
|
---|
162 | for(int_4 i=0;i<nx_;i++) {
|
---|
163 | if(ndata_(i)<1.) continue;
|
---|
164 | err2_(i) = ndata_(i)*(err2_(i) + data_(i)*data_(i));
|
---|
165 | data_(i) *= ndata_(i);
|
---|
166 | }
|
---|
167 | return;
|
---|
168 | }
|
---|
169 |
|
---|
170 | /********* Methode *********/
|
---|
171 | /*!
|
---|
172 | Fill the histogram with an other histogram
|
---|
173 | */
|
---|
174 | void HistoErr::FillFrHErr(HistoErr& hfrom)
|
---|
175 | {
|
---|
176 | if(nx_<=0) return;
|
---|
177 | if(hfrom.nx_<=0) return;
|
---|
178 |
|
---|
179 | Zero();
|
---|
180 |
|
---|
181 | for(int_4 i=0;i<hfrom.nx_;i++) {
|
---|
182 | r_8 x = hfrom.BinCenter(i);
|
---|
183 | int ii = FindBin(x);
|
---|
184 | if(ii<0 || ii>=nx_) continue;
|
---|
185 | data_(ii) += hfrom.data_(ii);
|
---|
186 | err2_(ii) += hfrom.err2_(ii);
|
---|
187 | ndata_(ii) += hfrom.ndata_(ii);
|
---|
188 | }
|
---|
189 | mMean = hfrom.mMean;
|
---|
190 |
|
---|
191 | }
|
---|
192 |
|
---|
193 | /********* Methode *********/
|
---|
194 | /*!
|
---|
195 | Operateur egal HistoErr = HistoErr
|
---|
196 | */
|
---|
197 | HistoErr& HistoErr::operator = (const HistoErr& h)
|
---|
198 | {
|
---|
199 | if(this==&h) return *this;
|
---|
200 | CreateOrResize(h.xmin_,h.xmax_,h.nx_);
|
---|
201 | data_ = h.data_;
|
---|
202 | err2_ = h.err2_;
|
---|
203 | ndata_ = h.ndata_;
|
---|
204 | mMean = h.mMean;
|
---|
205 | return *this;
|
---|
206 | }
|
---|
207 |
|
---|
208 | /********* Methode *********/
|
---|
209 | /*!
|
---|
210 | Operateur de multiplication par une constante
|
---|
211 | */
|
---|
212 | HistoErr& HistoErr::operator *= (r_8 b)
|
---|
213 | {
|
---|
214 | r_8 b2 = b*b;
|
---|
215 | for(int_4 i=0;i<nx_;i++) {
|
---|
216 | data_(i) *= b;
|
---|
217 | err2_(i) *= b2;
|
---|
218 | }
|
---|
219 | return *this;
|
---|
220 | }
|
---|
221 |
|
---|
222 | /********* Methode *********/
|
---|
223 | /*!
|
---|
224 | Print info
|
---|
225 | */
|
---|
226 | void HistoErr::Show(ostream & os) const
|
---|
227 | {
|
---|
228 | os <<"HistoErr(nmean="<<mMean<<")"<<endl
|
---|
229 | <<" nx="<<nx_<<" ["<<xmin_<<","<<xmax_<<"] dx="<<dx_<<endl;
|
---|
230 | }
|
---|
231 |
|
---|
232 | ///////////////////////////////////////////////////////////
|
---|
233 | // --------------------------------------------------------
|
---|
234 | // Les objets delegues pour la gestion de persistance
|
---|
235 | // --------------------------------------------------------
|
---|
236 | ///////////////////////////////////////////////////////////
|
---|
237 |
|
---|
238 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
---|
239 | void ObjFileIO<HistoErr>::ReadSelf(PInPersist& is)
|
---|
240 | {
|
---|
241 | string strg;
|
---|
242 |
|
---|
243 | if(dobj==NULL) dobj = new HistoErr;
|
---|
244 |
|
---|
245 | // Lecture entete
|
---|
246 | is.GetStr(strg);
|
---|
247 |
|
---|
248 | // Nombre d'appels a ToMean/FromMean
|
---|
249 | is.Get(dobj->mMean);
|
---|
250 |
|
---|
251 | // Lecture des parametres HistoErr
|
---|
252 | is.Get(dobj->xmin_);
|
---|
253 | is.Get(dobj->xmax_);
|
---|
254 | is.Get(dobj->nx_);
|
---|
255 | is.Get(dobj->dx_);
|
---|
256 |
|
---|
257 | // Lecture des donnees
|
---|
258 | if(dobj->nx_>0) {
|
---|
259 | is >> dobj->data_;
|
---|
260 | is >> dobj->err2_;
|
---|
261 | is >> dobj->ndata_;
|
---|
262 | }
|
---|
263 |
|
---|
264 | return;
|
---|
265 | }
|
---|
266 |
|
---|
267 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
---|
268 | void ObjFileIO<HistoErr>::WriteSelf(POutPersist& os) const
|
---|
269 | {
|
---|
270 | if(dobj == NULL) return;
|
---|
271 | string strg;
|
---|
272 |
|
---|
273 | // Ecriture entete
|
---|
274 | strg = "HistErr";
|
---|
275 | os.PutStr(strg);
|
---|
276 |
|
---|
277 | // Nombre d'appels a ToMean/FromMean
|
---|
278 | os.Put(dobj->mMean);
|
---|
279 |
|
---|
280 | // Ecriture des parametres HistoErr
|
---|
281 | os.Put(dobj->xmin_);
|
---|
282 | os.Put(dobj->xmax_);
|
---|
283 | os.Put(dobj->nx_);
|
---|
284 | os.Put(dobj->dx_);
|
---|
285 |
|
---|
286 | // Ecriture des donnees
|
---|
287 | if(dobj->nx_>0) {
|
---|
288 | os << dobj->data_;
|
---|
289 | os << dobj->err2_;
|
---|
290 | os << dobj->ndata_;
|
---|
291 | }
|
---|
292 |
|
---|
293 | return;
|
---|
294 | }
|
---|
295 |
|
---|
296 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
297 | #pragma define_template ObjFileIO<HistoErr>
|
---|
298 | #endif
|
---|
299 |
|
---|
300 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
301 | template class SOPHYA::ObjFileIO<HistoErr>;
|
---|
302 | #endif
|
---|