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

Last change on this file since 3147 was 3147, checked in by cmv, 19 years ago

correct bug, intro ToVariance et changement nom ToCorrel->ToMean cmv 18/01/2007

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