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