1 | #include "sopnamsp.h"
|
---|
2 | #include "machdefs.h"
|
---|
3 | #include <string.h>
|
---|
4 | #include <stdio.h>
|
---|
5 | #include <math.h>
|
---|
6 | #include "histinit.h"
|
---|
7 | #include "histerr.h"
|
---|
8 | #include "perrors.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 */
|
---|
18 | HistoErr::HistoErr(void)
|
---|
19 | : Histo(), mNData(NULL)
|
---|
20 | {
|
---|
21 | }
|
---|
22 |
|
---|
23 | /********* Methode *********/
|
---|
24 | /*! Constructeur d'un histo de nBin bins allant de xMin a xMax */
|
---|
25 | HistoErr::HistoErr(r_8 xMin, r_8 xMax, int_4 nBin)
|
---|
26 | : Histo(xMin,xMax,nBin), mNData(NULL)
|
---|
27 | {
|
---|
28 | this->Errors();
|
---|
29 | allocate_mNData(nBin);
|
---|
30 | Zero();
|
---|
31 | }
|
---|
32 |
|
---|
33 | /********* Methode *********/
|
---|
34 | /*! Constructeur par copie */
|
---|
35 | HistoErr::HistoErr(const HistoErr& H)
|
---|
36 | : Histo(H), mNData(NULL)
|
---|
37 | {
|
---|
38 | allocate_mNData(H.mBins);
|
---|
39 | if(mBins>0) memcpy(mNData,H.mNData,mBins*sizeof(r_8));
|
---|
40 | }
|
---|
41 |
|
---|
42 | /********* Methode *********/
|
---|
43 | /*! Destructeur */
|
---|
44 | HistoErr::~HistoErr(void)
|
---|
45 | {
|
---|
46 | Delete();
|
---|
47 | }
|
---|
48 |
|
---|
49 | /********* Methode *********/
|
---|
50 | /*! Allocation du tableau mNData */
|
---|
51 | void HistoErr::allocate_mNData(int nbin)
|
---|
52 | {
|
---|
53 | if(nbin<=0) return;
|
---|
54 | if(mNData) {delete [] mNData; mNData=NULL;}
|
---|
55 | mNData = new r_8[nbin];
|
---|
56 | }
|
---|
57 |
|
---|
58 | /********* Methode *********/
|
---|
59 | /*! Delete des tableaux */
|
---|
60 | void HistoErr::Delete(void)
|
---|
61 | {
|
---|
62 | Histo::Delete();
|
---|
63 | if(mNData) {delete [] mNData; mNData=NULL;}
|
---|
64 | }
|
---|
65 |
|
---|
66 | /********* Methode *********/
|
---|
67 | /*!
|
---|
68 | Remise a zero
|
---|
69 | */
|
---|
70 | void HistoErr::Zero(void)
|
---|
71 | {
|
---|
72 | Histo::Zero();
|
---|
73 | if(mNData) memset(mNData,0,mBins*sizeof(r_8));
|
---|
74 | }
|
---|
75 |
|
---|
76 | /********* Methode *********/
|
---|
77 | /*!
|
---|
78 | Addition du contenu de l'histo pour abscisse x poids w et l'erreur e
|
---|
79 | */
|
---|
80 | void HistoErr::Add(r_8 x, r_8 w, r_8 e)
|
---|
81 | {
|
---|
82 | int_4 numBin = (int_4)floor((x-mMin)/binWidth);
|
---|
83 | if(numBin<0) mUnder += w;
|
---|
84 | else if(numBin>=mBins) mOver += w;
|
---|
85 | else {
|
---|
86 | mData[numBin] += w; mNData[numBin]++; mErr2[numBin] += e*e;
|
---|
87 | nHist += w; nEntries++;
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | /********* Methode *********/
|
---|
92 | /*!
|
---|
93 | Addition du contenu de l'histo pour le bin numBin poids w et l'erreur e
|
---|
94 | */
|
---|
95 | void HistoErr::AddBin(int_4 numBin, r_8 w, r_8 e)
|
---|
96 | {
|
---|
97 | if(numBin<0) mUnder += w;
|
---|
98 | else if(numBin>=mBins) mOver += w;
|
---|
99 | else {
|
---|
100 | mData[numBin] += w; mNData[numBin]++; mErr2[numBin] += e*e;
|
---|
101 | nHist += w; nEntries++;
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | /********* Methode *********/
|
---|
106 | /*!
|
---|
107 | Remplissage du contenu de l'histo pour le bin numBin poids w et l'erreur e
|
---|
108 | */
|
---|
109 | void HistoErr::SetBin(int_4 numBin, r_8 w, r_8 e, r_8 nb)
|
---|
110 | {
|
---|
111 | Histo::SetBin(numBin,w);
|
---|
112 | Histo::SetErr2(numBin,e*e);
|
---|
113 | SetNentB(numBin,nb);
|
---|
114 | return;
|
---|
115 | }
|
---|
116 |
|
---|
117 | /********* Methode *********/
|
---|
118 | /*!
|
---|
119 | Remplissage nombre d'entrees pour le bin numBin
|
---|
120 | */
|
---|
121 | void HistoErr::SetNentB(int_4 numBin, r_8 nb)
|
---|
122 | {
|
---|
123 | if(numBin>=0 && numBin<mBins) mNData[numBin] = nb;
|
---|
124 | return;
|
---|
125 | }
|
---|
126 |
|
---|
127 | /*!
|
---|
128 | Remplissage d'un tableau avec les nombres d'entrees dans le bin de l'histo
|
---|
129 | */
|
---|
130 | void HistoErr::GetNBin(TVector<r_8>& v) const
|
---|
131 | {
|
---|
132 | v.Realloc(mBins);
|
---|
133 | for(int_4 i=0;i<mBins;i++) v(i) = mNData[i];
|
---|
134 | return;
|
---|
135 | }
|
---|
136 |
|
---|
137 | /********* Methode *********/
|
---|
138 | /*!
|
---|
139 | Remplissage du nombre d'entrees dans les bins de l'histo avec les valeurs d'un vecteur
|
---|
140 | */
|
---|
141 | void HistoErr::PutNBin(TVector<r_8> &v)
|
---|
142 | {
|
---|
143 | int_4 n = (v.NElts()<mBins) ? v.NElts(): mBins;
|
---|
144 | if(n>0) for(int_4 i=0;i<n;i++) mNData[i] = v(i);
|
---|
145 | return;
|
---|
146 | }
|
---|
147 |
|
---|
148 | /********* Methode *********/
|
---|
149 | /*!
|
---|
150 | Recompute XMin and XMax so that
|
---|
151 | the center of the first bin is exactly xcmin and
|
---|
152 | the center of the last bin is exactly xcmax
|
---|
153 | */
|
---|
154 | void HistoErr::ReCenterBin(r_8 xcmin,r_8 xcmax)
|
---|
155 | {
|
---|
156 | if(xcmax<=xcmin) return;
|
---|
157 | if(mBins<=1) return;
|
---|
158 | double dx = (mMax-mMin)/(mBins-1);
|
---|
159 | mMin -= dx/2.;
|
---|
160 | mMax += dx/2.;
|
---|
161 | binWidth = (mMax-mMin)/mBins;
|
---|
162 | }
|
---|
163 |
|
---|
164 | /********* Methode *********/
|
---|
165 | /*!
|
---|
166 | Compute the correlation histogram.
|
---|
167 | Each bin content is divided by the number of entries in that bin.
|
---|
168 | Each squared error is divided by the number of entries in that bin.
|
---|
169 | The number of entries by bin is NOT set to 1 (calling ToCorrel
|
---|
170 | many time will change the histogram !)
|
---|
171 | */
|
---|
172 | void HistoErr::ToCorrel(void)
|
---|
173 | {
|
---|
174 | if(mBins<1) return;
|
---|
175 | for(int_4 i=0;i<mBins;i++) {
|
---|
176 | if(mNData[i]<1) continue;
|
---|
177 | mData[i] /= mNData[i];
|
---|
178 | mErr2[i] /= mNData[i];
|
---|
179 | }
|
---|
180 | return;
|
---|
181 | }
|
---|
182 |
|
---|
183 | /********* Methode *********/
|
---|
184 | /*!
|
---|
185 | Operateur egal HistoErr = HistoErr
|
---|
186 | */
|
---|
187 | HistoErr& HistoErr::operator = (const HistoErr& h)
|
---|
188 | {
|
---|
189 | if(this==&h) return *this;
|
---|
190 | Delete();
|
---|
191 | if(h.mBins<=0) return *this;
|
---|
192 |
|
---|
193 | // Copy the "Histo" part
|
---|
194 | (Histo)(*this) = Histo::operator=(h);
|
---|
195 | // Copy the "entries by bin" table
|
---|
196 | allocate_mNData(h.mBins);
|
---|
197 | memcpy(mNData,h.mNData,mBins*sizeof(r_8));
|
---|
198 |
|
---|
199 | return *this;
|
---|
200 | }
|
---|
201 |
|
---|
202 | ///////////////////////////////////////////////////////////
|
---|
203 | // --------------------------------------------------------
|
---|
204 | // Les objets delegues pour la gestion de persistance
|
---|
205 | // --------------------------------------------------------
|
---|
206 | ///////////////////////////////////////////////////////////
|
---|
207 |
|
---|
208 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
---|
209 | void ObjFileIO<HistoErr>::ReadSelf(PInPersist& is)
|
---|
210 | {
|
---|
211 | string strg;
|
---|
212 |
|
---|
213 | if(dobj==NULL) dobj=new HistoErr;
|
---|
214 | else dobj->Delete();
|
---|
215 |
|
---|
216 | // Lecture entete
|
---|
217 | is.GetStr(strg);
|
---|
218 |
|
---|
219 | // Lecture des donnees HistoErr
|
---|
220 | is.Get(dobj->mBins);
|
---|
221 | dobj->allocate_mNData(dobj->mBins);
|
---|
222 | is.Get(dobj->mNData, dobj->mBins);
|
---|
223 |
|
---|
224 | // Lecture de l'histogramme
|
---|
225 | is >> (Histo&)(*dobj);
|
---|
226 |
|
---|
227 | return;
|
---|
228 | }
|
---|
229 |
|
---|
230 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
---|
231 | void ObjFileIO<HistoErr>::WriteSelf(POutPersist& os) const
|
---|
232 | {
|
---|
233 | if(dobj == NULL) return;
|
---|
234 | string strg;
|
---|
235 |
|
---|
236 | strg = "HistErr";
|
---|
237 | os.PutStr(strg);
|
---|
238 |
|
---|
239 | // Ecriture des valeurs
|
---|
240 | os.Put(dobj->mBins);
|
---|
241 |
|
---|
242 | // Ecriture des donnees HistoErr nombre d entree par bin
|
---|
243 | os.Put(dobj->mNData, dobj->mBins);
|
---|
244 |
|
---|
245 | // Ecriture de l'histogramme
|
---|
246 | os << (Histo&)(*dobj);
|
---|
247 |
|
---|
248 | return;
|
---|
249 | }
|
---|
250 |
|
---|
251 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
252 | #pragma define_template ObjFileIO<HistoErr>
|
---|
253 | #endif
|
---|
254 |
|
---|
255 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
256 | template class ObjFileIO<HistoErr>;
|
---|
257 | #endif
|
---|