[2615] | 1 | #include "sopnamsp.h"
|
---|
[2603] | 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)
|
---|
[2608] | 26 | : Histo(xMin,xMax,nBin), mNData(NULL)
|
---|
[2603] | 27 | {
|
---|
| 28 | this->Errors();
|
---|
[2608] | 29 | allocate_mNData(nBin);
|
---|
| 30 | Zero();
|
---|
[2603] | 31 | }
|
---|
| 32 |
|
---|
| 33 | /********* Methode *********/
|
---|
| 34 | /*! Constructeur par copie */
|
---|
| 35 | HistoErr::HistoErr(const HistoErr& H)
|
---|
[2608] | 36 | : Histo(H), mNData(NULL)
|
---|
[2603] | 37 | {
|
---|
[2608] | 38 | allocate_mNData(H.mBins);
|
---|
[2626] | 39 | if(mBins>0) memcpy(mNData,H.mNData,mBins*sizeof(r_8));
|
---|
[2603] | 40 | }
|
---|
| 41 |
|
---|
| 42 | /********* Methode *********/
|
---|
| 43 | /*! Destructeur */
|
---|
| 44 | HistoErr::~HistoErr(void)
|
---|
| 45 | {
|
---|
[2619] | 46 | Delete();
|
---|
[2603] | 47 | }
|
---|
[2619] | 48 |
|
---|
[2608] | 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;}
|
---|
[2626] | 55 | mNData = new r_8[nbin];
|
---|
[2608] | 56 | }
|
---|
[2603] | 57 |
|
---|
| 58 | /********* Methode *********/
|
---|
[2619] | 59 | /*! Delete des tableaux */
|
---|
| 60 | void HistoErr::Delete(void)
|
---|
| 61 | {
|
---|
| 62 | Histo::Delete();
|
---|
| 63 | if(mNData) {delete [] mNData; mNData=NULL;}
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | /********* Methode *********/
|
---|
[2603] | 67 | /*!
|
---|
| 68 | Remise a zero
|
---|
| 69 | */
|
---|
| 70 | void HistoErr::Zero(void)
|
---|
| 71 | {
|
---|
| 72 | Histo::Zero();
|
---|
[2626] | 73 | if(mNData) memset(mNData,0,mBins*sizeof(r_8));
|
---|
[2603] | 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 {
|
---|
[2627] | 86 | mData[numBin] += w; mNData[numBin] += 1.; mErr2[numBin] += e*e;
|
---|
[2603] | 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 {
|
---|
[2627] | 100 | mData[numBin] += w; mNData[numBin] += 1.; mErr2[numBin] += e*e;
|
---|
[2603] | 101 | nHist += w; nEntries++;
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[2604] | 105 | /********* Methode *********/
|
---|
[2603] | 106 | /*!
|
---|
[2604] | 107 | Remplissage du contenu de l'histo pour le bin numBin poids w et l'erreur e
|
---|
| 108 | */
|
---|
[2626] | 109 | void HistoErr::SetBin(int_4 numBin, r_8 w, r_8 e, r_8 nb)
|
---|
[2604] | 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 | */
|
---|
[2626] | 121 | void HistoErr::SetNentB(int_4 numBin, r_8 nb)
|
---|
[2604] | 122 | {
|
---|
| 123 | if(numBin>=0 && numBin<mBins) mNData[numBin] = nb;
|
---|
| 124 | return;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | /*!
|
---|
[2603] | 128 | Remplissage d'un tableau avec les nombres d'entrees dans le bin de l'histo
|
---|
| 129 | */
|
---|
[2626] | 130 | void HistoErr::GetNBin(TVector<r_8>& v) const
|
---|
[2603] | 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 | */
|
---|
[2626] | 141 | void HistoErr::PutNBin(TVector<r_8> &v)
|
---|
[2603] | 142 | {
|
---|
[2604] | 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);
|
---|
[2603] | 145 | return;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
[2608] | 148 | /********* Methode *********/
|
---|
| 149 | /*!
|
---|
| 150 | Recompute XMin and XMax so that
|
---|
[2627] | 151 | the CENTER of the first bin is exactly XMin and
|
---|
| 152 | the CENTER of the last bin is exactly XMax.
|
---|
| 153 | Remember that otherwise
|
---|
| 154 | XMin is the beginning of the first bin
|
---|
| 155 | and XMax is the end of the last bin
|
---|
[2608] | 156 | */
|
---|
[2627] | 157 | void HistoErr::ReCenterBin(void)
|
---|
[2608] | 158 | {
|
---|
| 159 | if(mBins<=1) return;
|
---|
| 160 | double dx = (mMax-mMin)/(mBins-1);
|
---|
[2627] | 161 | mMin -= dx/2.;
|
---|
| 162 | mMax += dx/2.;
|
---|
| 163 | binWidth = (mMax-mMin)/mBins;
|
---|
[2608] | 164 | }
|
---|
[2604] | 165 |
|
---|
| 166 | /********* Methode *********/
|
---|
| 167 | /*!
|
---|
| 168 | Compute the correlation histogram.
|
---|
| 169 | Each bin content is divided by the number of entries in that bin.
|
---|
| 170 | Each squared error is divided by the number of entries in that bin.
|
---|
[2622] | 171 | The number of entries by bin is NOT set to 1 (calling ToCorrel
|
---|
| 172 | many time will change the histogram !)
|
---|
[2604] | 173 | */
|
---|
| 174 | void HistoErr::ToCorrel(void)
|
---|
| 175 | {
|
---|
| 176 | if(mBins<1) return;
|
---|
| 177 | for(int_4 i=0;i<mBins;i++) {
|
---|
[2627] | 178 | if(mNData[i]<1.) continue;
|
---|
[2626] | 179 | mData[i] /= mNData[i];
|
---|
| 180 | mErr2[i] /= mNData[i];
|
---|
[2604] | 181 | }
|
---|
| 182 | return;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
[2619] | 185 | /********* Methode *********/
|
---|
| 186 | /*!
|
---|
[2628] | 187 | Fill the histogram with an other histogram
|
---|
| 188 | */
|
---|
| 189 | void HistoErr::FillFrHErr(HistoErr& hfrom)
|
---|
| 190 | {
|
---|
| 191 | if(mBins<=0) return;
|
---|
| 192 | if(hfrom.mBins<=0) return;
|
---|
| 193 |
|
---|
| 194 | Zero();
|
---|
| 195 |
|
---|
| 196 | for(int_4 i=0;i<hfrom.mBins;i++) {
|
---|
| 197 | int numBin = FindBin(hfrom.BinCenter(i));
|
---|
| 198 | if(numBin<0) mUnder += hfrom.mData[i];
|
---|
| 199 | else if(numBin>=mBins) mOver += hfrom.mData[i];
|
---|
| 200 | else {
|
---|
| 201 | mData[numBin] += hfrom.mData[i];
|
---|
| 202 | mNData[numBin] += hfrom.mNData[i];
|
---|
| 203 | mErr2[numBin] += hfrom.mErr2[i];
|
---|
| 204 | nHist += hfrom.mData[i];
|
---|
| 205 | nEntries++;
|
---|
| 206 | }
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | /********* Methode *********/
|
---|
| 212 | /*!
|
---|
[2619] | 213 | Operateur egal HistoErr = HistoErr
|
---|
| 214 | */
|
---|
| 215 | HistoErr& HistoErr::operator = (const HistoErr& h)
|
---|
| 216 | {
|
---|
| 217 | if(this==&h) return *this;
|
---|
| 218 | Delete();
|
---|
| 219 | if(h.mBins<=0) return *this;
|
---|
| 220 |
|
---|
| 221 | // Copy the "Histo" part
|
---|
| 222 | (Histo)(*this) = Histo::operator=(h);
|
---|
| 223 | // Copy the "entries by bin" table
|
---|
| 224 | allocate_mNData(h.mBins);
|
---|
[2626] | 225 | memcpy(mNData,h.mNData,mBins*sizeof(r_8));
|
---|
[2619] | 226 |
|
---|
| 227 | return *this;
|
---|
| 228 | }
|
---|
| 229 |
|
---|
[2603] | 230 | ///////////////////////////////////////////////////////////
|
---|
| 231 | // --------------------------------------------------------
|
---|
| 232 | // Les objets delegues pour la gestion de persistance
|
---|
| 233 | // --------------------------------------------------------
|
---|
| 234 | ///////////////////////////////////////////////////////////
|
---|
| 235 |
|
---|
| 236 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
---|
| 237 | void ObjFileIO<HistoErr>::ReadSelf(PInPersist& is)
|
---|
| 238 | {
|
---|
[2608] | 239 | string strg;
|
---|
[2603] | 240 |
|
---|
| 241 | if(dobj==NULL) dobj=new HistoErr;
|
---|
| 242 | else dobj->Delete();
|
---|
| 243 |
|
---|
| 244 | // Lecture entete
|
---|
[2608] | 245 | is.GetStr(strg);
|
---|
[2603] | 246 |
|
---|
| 247 | // Lecture des donnees HistoErr
|
---|
| 248 | is.Get(dobj->mBins);
|
---|
[2608] | 249 | dobj->allocate_mNData(dobj->mBins);
|
---|
[2603] | 250 | is.Get(dobj->mNData, dobj->mBins);
|
---|
| 251 |
|
---|
| 252 | // Lecture de l'histogramme
|
---|
| 253 | is >> (Histo&)(*dobj);
|
---|
| 254 |
|
---|
| 255 | return;
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
---|
| 259 | void ObjFileIO<HistoErr>::WriteSelf(POutPersist& os) const
|
---|
| 260 | {
|
---|
| 261 | if(dobj == NULL) return;
|
---|
[2608] | 262 | string strg;
|
---|
[2603] | 263 |
|
---|
[2608] | 264 | strg = "HistErr";
|
---|
| 265 | os.PutStr(strg);
|
---|
[2603] | 266 |
|
---|
| 267 | // Ecriture des valeurs
|
---|
| 268 | os.Put(dobj->mBins);
|
---|
| 269 |
|
---|
| 270 | // Ecriture des donnees HistoErr nombre d entree par bin
|
---|
| 271 | os.Put(dobj->mNData, dobj->mBins);
|
---|
| 272 |
|
---|
| 273 | // Ecriture de l'histogramme
|
---|
| 274 | os << (Histo&)(*dobj);
|
---|
| 275 |
|
---|
| 276 | return;
|
---|
| 277 | }
|
---|
| 278 |
|
---|
| 279 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
| 280 | #pragma define_template ObjFileIO<HistoErr>
|
---|
| 281 | #endif
|
---|
| 282 |
|
---|
| 283 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
| 284 | template class ObjFileIO<HistoErr>;
|
---|
| 285 | #endif
|
---|