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

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

intro operator+=double HistoErr Histo2DErr cmv 16/01/07

File size: 5.2 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 Operateur de multiplication par une constante
172*/
173HistoErr& HistoErr::operator *= (r_8 b)
174{
175r_8 b2 = b*b;
176for(int_4 i=0;i<nx_;i++) {
177 data_(i) *= b;
178 err2_(i) *= b2;
179}
180return *this;
181}
182
183/********* Methode *********/
184/*!
185 Print info
186*/
187void HistoErr::Show(ostream & os) const
188{
189 os <<"HistoErr(ncorrel="<<mCorrel<<")"<<endl
190 <<" nx="<<nx_<<" ["<<xmin_<<","<<xmax_<<"] dx="<<dx_<<endl;
191}
192
193///////////////////////////////////////////////////////////
194// --------------------------------------------------------
195// Les objets delegues pour la gestion de persistance
196// --------------------------------------------------------
197///////////////////////////////////////////////////////////
198
199DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
200void ObjFileIO<HistoErr>::ReadSelf(PInPersist& is)
201{
202string strg;
203
204if(dobj==NULL) dobj = new HistoErr;
205
206// Lecture entete
207is.GetStr(strg);
208
209// Nombre d'appels a ToCorrel/FromCorrel
210is.Get(dobj->mCorrel);
211
212// Lecture des parametres HistoErr
213is.Get(dobj->xmin_);
214is.Get(dobj->xmax_);
215is.Get(dobj->nx_);
216is.Get(dobj->dx_);
217
218// Lecture des donnees
219if(dobj->nx_>0) {
220 is >> dobj->data_;
221 is >> dobj->err2_;
222 is >> dobj->ndata_;
223}
224
225return;
226}
227
228DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
229void ObjFileIO<HistoErr>::WriteSelf(POutPersist& os) const
230{
231if(dobj == NULL) return;
232string strg;
233
234// Ecriture entete
235strg = "HistErr";
236os.PutStr(strg);
237
238// Nombre d'appels a ToCorrel/FromCorrel
239os.Put(dobj->mCorrel);
240
241// Ecriture des parametres HistoErr
242os.Put(dobj->xmin_);
243os.Put(dobj->xmax_);
244os.Put(dobj->nx_);
245os.Put(dobj->dx_);
246
247// Ecriture des donnees
248if(dobj->nx_>0) {
249 os << dobj->data_;
250 os << dobj->err2_;
251 os << dobj->ndata_;
252}
253
254return;
255}
256
257#ifdef __CXX_PRAGMA_TEMPLATES__
258#pragma define_template ObjFileIO<HistoErr>
259#endif
260
261#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
262template class SOPHYA::ObjFileIO<HistoErr>;
263#endif
Note: See TracBrowser for help on using the repository browser.