[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)
|
---|
[3053] | 19 | : Histo(), mNData(NULL), mCorrel(0)
|
---|
[2603] | 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)
|
---|
[3053] | 26 | : Histo(), mNData(NULL)
|
---|
[2603] | 27 | {
|
---|
[3053] | 28 | CreateOrResize(xMin,xMax,nBin);
|
---|
[2603] | 29 | }
|
---|
| 30 |
|
---|
| 31 | /********* Methode *********/
|
---|
| 32 | /*! Constructeur par copie */
|
---|
| 33 | HistoErr::HistoErr(const HistoErr& H)
|
---|
[3053] | 34 | : Histo(H), mNData((H.mBins>0)? new r_8[H.mBins] : NULL), mCorrel(H.mCorrel)
|
---|
[2603] | 35 | {
|
---|
[3053] | 36 | if(mNData>0) memcpy(mNData,H.mNData,mBins*sizeof(r_8));
|
---|
[2603] | 37 | }
|
---|
| 38 |
|
---|
| 39 | /********* Methode *********/
|
---|
| 40 | /*! Destructeur */
|
---|
| 41 | HistoErr::~HistoErr(void)
|
---|
| 42 | {
|
---|
[2619] | 43 | Delete();
|
---|
[2603] | 44 | }
|
---|
[2619] | 45 |
|
---|
[2608] | 46 | /********* Methode *********/
|
---|
[3053] | 47 | /*! Gestion de l'allocation */
|
---|
| 48 | void HistoErr::CreateOrResize(r_8 xMin, r_8 xMax, int_4 nBin)
|
---|
| 49 | {
|
---|
| 50 | //cout<<"HistoErr::CreateOrResize()"<<endl;
|
---|
| 51 | Histo::CreateOrResize(xMin,xMax,nBin);
|
---|
| 52 | Histo::Errors();
|
---|
| 53 | allocate_mNData(nBin);
|
---|
| 54 | mCorrel = 0;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | /********* Methode *********/
|
---|
[2608] | 58 | /*! Allocation du tableau mNData */
|
---|
| 59 | void HistoErr::allocate_mNData(int nbin)
|
---|
| 60 | {
|
---|
[3053] | 61 | if(mNData) {delete [] mNData; mNData=NULL;}
|
---|
| 62 | if(nbin<=0) return;
|
---|
| 63 | mNData = new r_8[nbin];
|
---|
| 64 | memset(mNData,0,nbin*sizeof(r_8));
|
---|
[2608] | 65 | }
|
---|
[2603] | 66 |
|
---|
| 67 | /********* Methode *********/
|
---|
[2619] | 68 | /*! Delete des tableaux */
|
---|
| 69 | void HistoErr::Delete(void)
|
---|
| 70 | {
|
---|
[3053] | 71 | //cout<<"HistoErr::Delete()"<<endl;
|
---|
| 72 | Histo::Delete();
|
---|
| 73 | if(mNData) {delete [] mNData; mNData=NULL;}
|
---|
| 74 | mCorrel = 0;
|
---|
[2619] | 75 | }
|
---|
| 76 |
|
---|
| 77 | /********* Methode *********/
|
---|
[2603] | 78 | /*!
|
---|
| 79 | Remise a zero
|
---|
| 80 | */
|
---|
| 81 | void HistoErr::Zero(void)
|
---|
| 82 | {
|
---|
[3053] | 83 | Histo::Zero();
|
---|
| 84 | if(mNData) memset(mNData,0,mBins*sizeof(r_8));
|
---|
| 85 | mCorrel = 0;
|
---|
[2603] | 86 | }
|
---|
| 87 |
|
---|
| 88 | /********* Methode *********/
|
---|
| 89 | /*!
|
---|
[2604] | 90 | Remplissage du contenu de l'histo pour le bin numBin poids w et l'erreur e
|
---|
| 91 | */
|
---|
[2626] | 92 | void HistoErr::SetBin(int_4 numBin, r_8 w, r_8 e, r_8 nb)
|
---|
[2604] | 93 | {
|
---|
| 94 | Histo::SetBin(numBin,w);
|
---|
| 95 | Histo::SetErr2(numBin,e*e);
|
---|
| 96 | SetNentB(numBin,nb);
|
---|
| 97 | return;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | /********* Methode *********/
|
---|
| 101 | /*!
|
---|
| 102 | Remplissage nombre d'entrees pour le bin numBin
|
---|
| 103 | */
|
---|
[2626] | 104 | void HistoErr::SetNentB(int_4 numBin, r_8 nb)
|
---|
[2604] | 105 | {
|
---|
| 106 | if(numBin>=0 && numBin<mBins) mNData[numBin] = nb;
|
---|
| 107 | return;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | /*!
|
---|
[2603] | 111 | Remplissage d'un tableau avec les nombres d'entrees dans le bin de l'histo
|
---|
| 112 | */
|
---|
[2626] | 113 | void HistoErr::GetNBin(TVector<r_8>& v) const
|
---|
[2603] | 114 | {
|
---|
| 115 | v.Realloc(mBins);
|
---|
| 116 | for(int_4 i=0;i<mBins;i++) v(i) = mNData[i];
|
---|
| 117 | return;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | /********* Methode *********/
|
---|
| 121 | /*!
|
---|
| 122 | Remplissage du nombre d'entrees dans les bins de l'histo avec les valeurs d'un vecteur
|
---|
| 123 | */
|
---|
[2626] | 124 | void HistoErr::PutNBin(TVector<r_8> &v)
|
---|
[2603] | 125 | {
|
---|
[2604] | 126 | int_4 n = (v.NElts()<mBins) ? v.NElts(): mBins;
|
---|
| 127 | if(n>0) for(int_4 i=0;i<n;i++) mNData[i] = v(i);
|
---|
[2603] | 128 | return;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[2608] | 131 | /********* Methode *********/
|
---|
| 132 | /*!
|
---|
[2604] | 133 | Compute the correlation histogram.
|
---|
| 134 | Each bin content is divided by the number of entries in that bin.
|
---|
| 135 | Each squared error is divided by the number of entries in that bin.
|
---|
[2622] | 136 | The number of entries by bin is NOT set to 1 (calling ToCorrel
|
---|
| 137 | many time will change the histogram !)
|
---|
[2604] | 138 | */
|
---|
| 139 | void HistoErr::ToCorrel(void)
|
---|
| 140 | {
|
---|
| 141 | if(mBins<1) return;
|
---|
[3049] | 142 | mCorrel++;
|
---|
[2604] | 143 | for(int_4 i=0;i<mBins;i++) {
|
---|
[2627] | 144 | if(mNData[i]<1.) continue;
|
---|
[2626] | 145 | mData[i] /= mNData[i];
|
---|
| 146 | mErr2[i] /= mNData[i];
|
---|
[2604] | 147 | }
|
---|
| 148 | return;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
[2619] | 151 | /********* Methode *********/
|
---|
| 152 | /*!
|
---|
[2819] | 153 | Recompute back the original HistoErr before ToCorrel action
|
---|
| 154 | */
|
---|
| 155 | void HistoErr::FromCorrel(void)
|
---|
| 156 | {
|
---|
| 157 | if(mBins<1) return;
|
---|
[3049] | 158 | mCorrel--;
|
---|
[2819] | 159 | for(int_4 i=0;i<mBins;i++) {
|
---|
| 160 | if(mNData[i]<1.) continue;
|
---|
| 161 | mData[i] *= mNData[i];
|
---|
| 162 | mErr2[i] *= mNData[i];
|
---|
| 163 | }
|
---|
| 164 | return;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | /********* Methode *********/
|
---|
| 168 | /*!
|
---|
[2628] | 169 | Fill the histogram with an other histogram
|
---|
| 170 | */
|
---|
| 171 | void HistoErr::FillFrHErr(HistoErr& hfrom)
|
---|
| 172 | {
|
---|
| 173 | if(mBins<=0) return;
|
---|
| 174 | if(hfrom.mBins<=0) return;
|
---|
| 175 |
|
---|
| 176 | Zero();
|
---|
| 177 |
|
---|
| 178 | for(int_4 i=0;i<hfrom.mBins;i++) {
|
---|
| 179 | int numBin = FindBin(hfrom.BinCenter(i));
|
---|
| 180 | if(numBin<0) mUnder += hfrom.mData[i];
|
---|
| 181 | else if(numBin>=mBins) mOver += hfrom.mData[i];
|
---|
| 182 | else {
|
---|
| 183 | mData[numBin] += hfrom.mData[i];
|
---|
| 184 | mNData[numBin] += hfrom.mNData[i];
|
---|
| 185 | mErr2[numBin] += hfrom.mErr2[i];
|
---|
| 186 | nHist += hfrom.mData[i];
|
---|
| 187 | nEntries++;
|
---|
| 188 | }
|
---|
| 189 | }
|
---|
[3049] | 190 | mCorrel = hfrom.mCorrel;
|
---|
[2628] | 191 |
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | /********* Methode *********/
|
---|
| 195 | /*!
|
---|
[2619] | 196 | Operateur egal HistoErr = HistoErr
|
---|
| 197 | */
|
---|
| 198 | HistoErr& HistoErr::operator = (const HistoErr& h)
|
---|
| 199 | {
|
---|
| 200 | if(this==&h) return *this;
|
---|
| 201 | Delete();
|
---|
| 202 | if(h.mBins<=0) return *this;
|
---|
| 203 |
|
---|
| 204 | // Copy the "Histo" part
|
---|
| 205 | (Histo)(*this) = Histo::operator=(h);
|
---|
| 206 | // Copy the "entries by bin" table
|
---|
| 207 | allocate_mNData(h.mBins);
|
---|
[2626] | 208 | memcpy(mNData,h.mNData,mBins*sizeof(r_8));
|
---|
[3049] | 209 | mCorrel = h.mCorrel;
|
---|
[2619] | 210 |
|
---|
| 211 | return *this;
|
---|
| 212 | }
|
---|
| 213 |
|
---|
[3053] | 214 | /********* Methode *********/
|
---|
| 215 | /*!
|
---|
| 216 | Print info
|
---|
| 217 | */
|
---|
| 218 | void HistoErr::Show(ostream & os) const
|
---|
| 219 | {
|
---|
| 220 | Histo::Show(os);
|
---|
| 221 | os <<" ncorrel="<<mCorrel<< endl;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
[2603] | 224 | ///////////////////////////////////////////////////////////
|
---|
| 225 | // --------------------------------------------------------
|
---|
| 226 | // Les objets delegues pour la gestion de persistance
|
---|
| 227 | // --------------------------------------------------------
|
---|
| 228 | ///////////////////////////////////////////////////////////
|
---|
| 229 |
|
---|
| 230 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
---|
| 231 | void ObjFileIO<HistoErr>::ReadSelf(PInPersist& is)
|
---|
| 232 | {
|
---|
[2608] | 233 | string strg;
|
---|
[2603] | 234 |
|
---|
| 235 | if(dobj==NULL) dobj=new HistoErr;
|
---|
| 236 | else dobj->Delete();
|
---|
| 237 |
|
---|
| 238 | // Lecture entete
|
---|
[2608] | 239 | is.GetStr(strg);
|
---|
[2603] | 240 |
|
---|
[3049] | 241 | // Nombre d'appels a ToCorrel/FromCorrel
|
---|
| 242 | is.Get(dobj->mCorrel);
|
---|
| 243 |
|
---|
[2603] | 244 | // Lecture des donnees HistoErr
|
---|
| 245 | is.Get(dobj->mBins);
|
---|
[2608] | 246 | dobj->allocate_mNData(dobj->mBins);
|
---|
[2603] | 247 | is.Get(dobj->mNData, dobj->mBins);
|
---|
| 248 |
|
---|
| 249 | // Lecture de l'histogramme
|
---|
| 250 | is >> (Histo&)(*dobj);
|
---|
| 251 |
|
---|
| 252 | return;
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
---|
| 256 | void ObjFileIO<HistoErr>::WriteSelf(POutPersist& os) const
|
---|
| 257 | {
|
---|
| 258 | if(dobj == NULL) return;
|
---|
[2608] | 259 | string strg;
|
---|
[2603] | 260 |
|
---|
[3049] | 261 | // Ecriture entete
|
---|
[2608] | 262 | strg = "HistErr";
|
---|
| 263 | os.PutStr(strg);
|
---|
[2603] | 264 |
|
---|
[3049] | 265 | // Nombre d'appels a ToCorrel/FromCorrel
|
---|
| 266 | os.Put(dobj->mCorrel);
|
---|
| 267 |
|
---|
[2603] | 268 | // Ecriture des valeurs
|
---|
| 269 | os.Put(dobj->mBins);
|
---|
| 270 |
|
---|
| 271 | // Ecriture des donnees HistoErr nombre d entree par bin
|
---|
| 272 | os.Put(dobj->mNData, dobj->mBins);
|
---|
| 273 |
|
---|
| 274 | // Ecriture de l'histogramme
|
---|
| 275 | os << (Histo&)(*dobj);
|
---|
| 276 |
|
---|
| 277 | return;
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
| 281 | #pragma define_template ObjFileIO<HistoErr>
|
---|
| 282 | #endif
|
---|
| 283 |
|
---|
| 284 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
[2868] | 285 | template class SOPHYA::ObjFileIO<HistoErr>;
|
---|
[2603] | 286 | #endif
|
---|