source: Sophya/trunk/SophyaLib/HiStats/hist2err.cc@ 3247

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

correction erreur format dans sscanf cmv 15/05/2007

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