source: Sophya/trunk/SophyaLib/HiStats/histerr.cc@ 3853

Last change on this file since 3853 was 3338, checked in by cmv, 18 years ago

mise a zero de mMean dans ::Zero() cmv 04/10/2007

File size: 8.8 KB
Line 
1#include "machdefs.h"
2#include <string.h>
3#include <stdio.h>
4#include <math.h>
5#include "perrors.h"
6#include "fioarr.h"
7#include "histerr.h"
8
9namespace SOPHYA {
10
11/*!
12 \class HistoErr
13 \ingroup HiStats
14 Classe d'histogrammes 1D avec erreurs donnees par l'utilisateur
15*/
16
17/********* Methode *********/
18/*! Constructeur par defaut */
19HistoErr::HistoErr(void)
20: xmin_(1.), xmax_(-1.), dx_(0.), nx_(0), mMean(0)
21{
22}
23
24/********* Methode *********/
25/*! Constructeur d'un histo */
26HistoErr::HistoErr(r_8 xmin,r_8 xmax,int_4 nx)
27{
28 CreateOrResize(xmin,xmax,nx);
29}
30
31/********* Methode *********/
32/*! Constructeur par copie */
33HistoErr::HistoErr(const HistoErr& H)
34: xmin_(1.), xmax_(-1.), dx_(0.), nx_(0), mMean(0)
35{
36 if(H.nx_<=0) return;
37 CreateOrResize(H.xmin_,H.xmax_,H.nx_);
38 data_ = H.data_;
39 err2_ = H.err2_;
40 ndata_ = H.ndata_;
41 mMean = H.mMean;
42}
43
44/********* Methode *********/
45/*! Destructeur */
46HistoErr::~HistoErr(void)
47{
48 mMean = 0;
49}
50
51/********* Methode *********/
52/*! Gestion de l'allocation */
53void 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*/
69void HistoErr::Zero(void)
70{
71 if(nx_<=0) return;
72 data_ = 0.;
73 err2_ = 0.;
74 ndata_ = 0.;
75 mMean = 0;
76}
77
78/********* Methode *********/
79/*!
80 Recompute XMin and XMax so that
81 the CENTER of the first bin is exactly XMin and
82 the CENTER of the last bin is exactly XMax.
83 Remember that otherwise
84 XMin is the beginning of the first bin
85 and XMax is the end of the last bin
86 WARNING: number of bins is kept, bin width is changed
87*/
88void HistoErr::ReCenterBin(void)
89{
90 if(nx_<=1) return;
91 double dx = (xmax_-xmin_)/(nx_-1);
92 xmin_ -= dx/2.;
93 xmax_ += dx/2.;
94 dx_ = (xmax_-xmin_)/nx_;
95}
96
97/********* Methode *********/
98/*!
99 Recompute XMin and XMax so that
100 the CENTER of the first bin is exactly XMin and
101 the CENTER of the last bin is exactly XMax.
102 Remember that otherwise
103 XMin is the beginning of the first bin
104 and XMax is the end of the last bin
105 WARNING: bin width is kept, number of bins is increased by 1
106*/
107void HistoErr::ReCenterBinW(void)
108{
109 if(nx_<=1) return;
110 CreateOrResize(xmin_-dx_/2.,xmax_+dx_/2.,nx_+1);
111}
112
113/********* Methode *********/
114/*!
115 Compute the mean histogram.
116 Each bin content is divided by the number of entries in the bin.
117 Each squared error is divided by the number of entries in the bin.
118 The number of entries by bin is NOT set to 1
119 (calling ToMean many time will change the histogram !)
120*/
121void HistoErr::ToMean(void)
122{
123 if(nx_<1) return;
124 mMean++;
125 for(int_4 i=0;i<nx_;i++) {
126 if(ndata_(i)<1.) continue;
127 data_(i) /= ndata_(i);
128 err2_(i) /= ndata_(i);
129 }
130 return;
131}
132
133/********* Methode *********/
134/*!
135 Recompute back the original HistoErr after ToMean action
136*/
137void HistoErr::FromMean(void)
138{
139 if(nx_<1) return;
140 mMean--;
141 for(int_4 i=0;i<nx_;i++) {
142 if(ndata_(i)<1.) continue;
143 data_(i) *= ndata_(i);
144 err2_(i) *= ndata_(i);
145 }
146 return;
147}
148
149/********* Methode *********/
150/*!
151 Compute the mean histogram and replace the "error table" by the variance.
152 This should be done if Add(x,w,w) has been used.
153 The "value table" is divided by the number of entries to get the mean
154 The "error table" is replace by the variance
155 The number of entries by bin is NOT set to 1
156 (calling ToMean many time will change the histogram !)
157 Mixing ToMean and ToVariance leads to unpredictable results
158*/
159void HistoErr::ToVariance(void)
160{
161 if(nx_<1) return;
162 mMean++;
163 for(int_4 i=0;i<nx_;i++) {
164 if(ndata_(i)<1.) continue;
165 data_(i) /= ndata_(i);
166 err2_(i) = err2_(i)/ndata_(i) - data_(i)*data_(i);
167 }
168 return;
169}
170
171/********* Methode *********/
172/*!
173 Recompute back the original HistoErr after ToVariance action
174 Mixing FromMean and FromVariance leads to unpredictable results
175*/
176void HistoErr::FromVariance(void)
177{
178 if(nx_<1) return;
179 mMean--;
180 for(int_4 i=0;i<nx_;i++) {
181 if(ndata_(i)<1.) continue;
182 err2_(i) = ndata_(i)*(err2_(i) + data_(i)*data_(i));
183 data_(i) *= ndata_(i);
184 }
185 return;
186}
187
188/********* Methode *********/
189/*!
190 Fill the histogram with an other histogram
191*/
192void HistoErr::FillFrHErr(HistoErr& hfrom)
193{
194 if(nx_<=0) return;
195 if(hfrom.nx_<=0) return;
196
197 Zero();
198
199 for(int_4 i=0;i<hfrom.nx_;i++) {
200 r_8 x = hfrom.BinCenter(i);
201 int ii = FindBin(x);
202 if(ii<0 || ii>=nx_) continue;
203 data_(ii) += hfrom.data_(ii);
204 err2_(ii) += hfrom.err2_(ii);
205 ndata_(ii) += hfrom.ndata_(ii);
206 }
207 mMean = hfrom.mMean;
208
209}
210
211/********* Methode *********/
212/*!
213 Return the sum of bin value
214*/
215double HistoErr::Sum(void)
216{
217 double s = 0.;
218 for(int_4 i=0;i<nx_;i++) {
219 if(ndata_(i)<1.) continue;
220 s += data_(i);
221 }
222 return s;
223}
224
225/********* Methode *********/
226/*!
227 Return the sum of the bin value squared
228*/
229double HistoErr::Sum2(void)
230{
231 double s = 0.;
232 for(int_4 i=0;i<nx_;i++) {
233 if(ndata_(i)<1.) continue;
234 s += data_(i)*data_(i);
235 }
236 return s;
237}
238
239/********* Methode *********/
240/*!
241 Return the sum of the number of entries
242*/
243double HistoErr::SumN(void)
244{
245 double s = 0.;
246 for(int_4 i=0;i<nx_;i++) {
247 if(ndata_(i)<1.) continue;
248 s += ndata_(i);
249 }
250 return s;
251}
252
253/********* Methode *********/
254/*!
255 Operateur egal HistoErr = HistoErr
256*/
257HistoErr& HistoErr::operator = (const HistoErr& h)
258{
259 if(this==&h) return *this;
260 CreateOrResize(h.xmin_,h.xmax_,h.nx_);
261 data_ = h.data_;
262 err2_ = h.err2_;
263 ndata_ = h.ndata_;
264 mMean = h.mMean;
265 return *this;
266}
267
268/********* Methode *********/
269/*!
270 Operateur de multiplication par une constante
271*/
272HistoErr& HistoErr::operator *= (r_8 b)
273{
274r_8 b2 = b*b;
275for(int_4 i=0;i<nx_;i++) {
276 data_(i) *= b;
277 err2_(i) *= b2;
278}
279return *this;
280}
281
282/********* Methode *********/
283/*!
284 Print info
285*/
286void HistoErr::Show(ostream & os) const
287{
288 os <<"HistoErr(nmean="<<mMean<<")"
289 <<" nx="<<nx_<<" ["<<xmin_<<","<<xmax_<<"] dx="<<dx_<<endl;
290}
291
292/********* Methode *********/
293/*!
294 Write to an ASCII file
295*/
296int HistoErr::WriteASCII(string fname)
297{
298 FILE *file = fopen(fname.c_str(),"w");
299 if(file==NULL) {
300 cout<<"HistoErr::WriteASCII_Error: error opening "<<fname<<endl;
301 return -1;
302 }
303
304 if(NBins()<=0) {
305 cout<<"HistoErr::WriteASCII_Error: nbin= "<<NBins()<<endl;
306 return -2;
307 }
308
309 fprintf(file,"%ld %.17e %.17e %d\n",(long)NBins(),XMin(),XMax(),NMean());
310 for(long i=0;i<NBins();i++) {
311 fprintf(file,"%ld %.17e %.17e %.17e %.0f\n"
312 ,i,BinCenter(i),(*this)(i),Error2(i),NEntBin(i));
313 }
314
315 fclose(file);
316 return 0;
317}
318
319/*!
320 Read from an ASCII file
321*/
322#define __LENLINE_HistoErr_ReadASCII__ 2048
323int HistoErr::ReadASCII(string fname)
324{
325 FILE *file = fopen(fname.c_str(),"r");
326 if(file==NULL) {
327 cout<<"HistoErr::ReadASCII_Error: error opening "<<fname<<endl;
328 return -1;
329 }
330
331 char line[__LENLINE_HistoErr_ReadASCII__];
332 long n=0, nbin=0;
333
334 while ( fgets(line,__LENLINE_HistoErr_ReadASCII__,file) != NULL ) {
335
336 if(n==0) {
337
338 r_8 xmin,xmax; long mnmean=1;
339 sscanf(line,"%ld %lf %lf %ld",&nbin,&xmin,&xmax,&mnmean);
340 if(nbin<=0) {
341 cout<<"HistoErr::ReadASCII_Error: nbin= "<<nbin<<endl;
342 return -2;
343 }
344 CreateOrResize(xmin,xmax,nbin);
345 SetMean(mnmean);
346
347 } else {
348
349 long i; r_8 x,v,e2,nb;
350 sscanf(line,"%ld %lf %lf %lf %lf",&i,&x,&v,&e2,&nb);
351 SetBin(i,v);
352 SetErr2(i,e2);
353 SetNentB(i,nb);
354
355 }
356
357 n++;
358 }
359
360 fclose(file);
361 return 0;
362}
363
364
365///////////////////////////////////////////////////////////
366// --------------------------------------------------------
367// Les objets delegues pour la gestion de persistance
368// --------------------------------------------------------
369///////////////////////////////////////////////////////////
370
371DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
372void ObjFileIO<HistoErr>::ReadSelf(PInPersist& is)
373{
374string strg;
375
376if(dobj==NULL) dobj = new HistoErr;
377
378// Lecture entete
379is.GetStr(strg);
380
381// Nombre d'appels a ToMean/FromMean
382is.Get(dobj->mMean);
383
384// Lecture des parametres HistoErr
385is.Get(dobj->xmin_);
386is.Get(dobj->xmax_);
387is.Get(dobj->nx_);
388is.Get(dobj->dx_);
389
390// Lecture des donnees
391if(dobj->nx_>0) {
392 is >> dobj->data_;
393 is >> dobj->err2_;
394 is >> dobj->ndata_;
395}
396
397return;
398}
399
400DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
401void ObjFileIO<HistoErr>::WriteSelf(POutPersist& os) const
402{
403if(dobj == NULL) return;
404string strg;
405
406// Ecriture entete
407strg = "HistErr";
408os.PutStr(strg);
409
410// Nombre d'appels a ToMean/FromMean
411os.Put(dobj->mMean);
412
413// Ecriture des parametres HistoErr
414os.Put(dobj->xmin_);
415os.Put(dobj->xmax_);
416os.Put(dobj->nx_);
417os.Put(dobj->dx_);
418
419// Ecriture des donnees
420if(dobj->nx_>0) {
421 os << dobj->data_;
422 os << dobj->err2_;
423 os << dobj->ndata_;
424}
425
426return;
427}
428
429#ifdef __CXX_PRAGMA_TEMPLATES__
430#pragma define_template ObjFileIO<HistoErr>
431#endif
432
433#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
434template class ObjFileIO<HistoErr>;
435#endif
436
437} // FIN namespace SOPHYA
Note: See TracBrowser for help on using the repository browser.