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

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

modif HistoErr Histo2DErr cmv 10/01/07

File size: 5.0 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 "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 */
18HistoErr::HistoErr(void)
19: xmin_(1.), xmax_(-1.), nx_(0), dx_(0.)
20, mCorrel(0)
21{
22}
23
24/********* Methode *********/
25/*! Constructeur d'un histo */
26HistoErr::HistoErr(r_8 xmin,r_8 xmax,int_4 nx)
27: mCorrel(0)
28{
29 CreateOrResize(xmin,xmax,nx);
30}
31
32/********* Methode *********/
33/*! Constructeur par copie */
34HistoErr::HistoErr(const HistoErr& H)
35: mCorrel(H.mCorrel)
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 */
46HistoErr::~HistoErr(void)
47{
48 mCorrel = 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 mCorrel = 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}
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*/
86void 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 correlation histogram.
98 Each bin content is divided by the number of entries in that bin.
99 Each squared error is divided by the number of entries in that bin.
100 The number of entries by bin is NOT set to 1 (calling ToCorrel
101 many time will change the histogram !)
102*/
103void HistoErr::ToCorrel(void)
104{
105 if(nx_<1) return;
106 mCorrel++;
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 before ToCorrel action
118*/
119void HistoErr::FromCorrel(void)
120{
121 if(nx_<1) return;
122 mCorrel--;
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 Fill the histogram with an other histogram
134*/
135void HistoErr::FillFrHErr(HistoErr& hfrom)
136{
137 if(nx_<=0) return;
138 if(hfrom.nx_<=0) return;
139
140 Zero();
141
142 for(int_4 i=0;i<hfrom.nx_;i++) {
143 r_8 x = hfrom.BinCenter(i);
144 int ii = FindBin(x);
145 if(ii<0 || ii>=nx_) continue;
146 data_(ii) += hfrom.data_(ii);
147 err2_(ii) += hfrom.err2_(ii);
148 ndata_(ii) += hfrom.ndata_(ii);
149 }
150 mCorrel = hfrom.mCorrel;
151
152}
153
154/********* Methode *********/
155/*!
156 Operateur egal HistoErr = HistoErr
157*/
158HistoErr& HistoErr::operator = (const HistoErr& h)
159{
160 if(this==&h) return *this;
161 CreateOrResize(h.xmin_,h.xmax_,h.nx_);
162 data_ = h.data_;
163 err2_ = h.err2_;
164 ndata_ = h.ndata_;
165 mCorrel = h.mCorrel;
166 return *this;
167}
168
169/********* Methode *********/
170/*!
171 Print info
172*/
173void HistoErr::Show(ostream & os) const
174{
175 os <<"HistoErr(ncorrel="<<mCorrel<<")"<<endl
176 <<" nx="<<nx_<<" ["<<xmin_<<","<<xmax_<<"] dx="<<dx_<<endl;
177}
178
179///////////////////////////////////////////////////////////
180// --------------------------------------------------------
181// Les objets delegues pour la gestion de persistance
182// --------------------------------------------------------
183///////////////////////////////////////////////////////////
184
185DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
186void ObjFileIO<HistoErr>::ReadSelf(PInPersist& is)
187{
188string strg;
189
190if(dobj==NULL) dobj = new HistoErr;
191
192// Lecture entete
193is.GetStr(strg);
194
195// Nombre d'appels a ToCorrel/FromCorrel
196is.Get(dobj->mCorrel);
197
198// Lecture des parametres HistoErr
199is.Get(dobj->xmin_);
200is.Get(dobj->xmax_);
201is.Get(dobj->nx_);
202is.Get(dobj->dx_);
203
204// Lecture des donnees
205if(dobj->nx_>0) {
206 is >> dobj->data_;
207 is >> dobj->err2_;
208 is >> dobj->ndata_;
209}
210
211return;
212}
213
214DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
215void ObjFileIO<HistoErr>::WriteSelf(POutPersist& os) const
216{
217if(dobj == NULL) return;
218string strg;
219
220// Ecriture entete
221strg = "HistErr";
222os.PutStr(strg);
223
224// Nombre d'appels a ToCorrel/FromCorrel
225os.Put(dobj->mCorrel);
226
227// Ecriture des parametres HistoErr
228os.Put(dobj->xmin_);
229os.Put(dobj->xmax_);
230os.Put(dobj->nx_);
231os.Put(dobj->dx_);
232
233// Ecriture des donnees
234if(dobj->nx_>0) {
235 os << dobj->data_;
236 os << dobj->err2_;
237 os << dobj->ndata_;
238}
239
240return;
241}
242
243#ifdef __CXX_PRAGMA_TEMPLATES__
244#pragma define_template ObjFileIO<HistoErr>
245#endif
246
247#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
248template class SOPHYA::ObjFileIO<HistoErr>;
249#endif
Note: See TracBrowser for help on using the repository browser.