| 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 |  Remplissage du contenu de l'histo pour le bin numBin poids w et l'erreur e
 | 
|---|
| 79 | */
 | 
|---|
| 80 | void HistoErr::SetBin(int_4 numBin, r_8 w, r_8 e, r_8 nb)
 | 
|---|
| 81 | {
 | 
|---|
| 82 | Histo::SetBin(numBin,w);
 | 
|---|
| 83 | Histo::SetErr2(numBin,e*e);
 | 
|---|
| 84 | SetNentB(numBin,nb);
 | 
|---|
| 85 | return;
 | 
|---|
| 86 | }
 | 
|---|
| 87 | 
 | 
|---|
| 88 | /********* Methode *********/
 | 
|---|
| 89 | /*!
 | 
|---|
| 90 |  Remplissage nombre d'entrees pour le bin numBin
 | 
|---|
| 91 | */
 | 
|---|
| 92 | void HistoErr::SetNentB(int_4 numBin, r_8 nb)
 | 
|---|
| 93 | {
 | 
|---|
| 94 |  if(numBin>=0 && numBin<mBins) mNData[numBin] = nb;
 | 
|---|
| 95 | return;
 | 
|---|
| 96 | }
 | 
|---|
| 97 | 
 | 
|---|
| 98 | /*!
 | 
|---|
| 99 |   Remplissage d'un tableau avec les nombres d'entrees dans le bin de l'histo
 | 
|---|
| 100 | */
 | 
|---|
| 101 | void HistoErr::GetNBin(TVector<r_8>& v) const
 | 
|---|
| 102 | {
 | 
|---|
| 103 | v.Realloc(mBins);
 | 
|---|
| 104 | for(int_4 i=0;i<mBins;i++) v(i) = mNData[i];
 | 
|---|
| 105 | return;
 | 
|---|
| 106 | }
 | 
|---|
| 107 | 
 | 
|---|
| 108 | /********* Methode *********/
 | 
|---|
| 109 | /*!
 | 
|---|
| 110 |   Remplissage du nombre d'entrees dans les bins de l'histo avec les valeurs d'un vecteur
 | 
|---|
| 111 | */
 | 
|---|
| 112 | void HistoErr::PutNBin(TVector<r_8> &v)
 | 
|---|
| 113 | {
 | 
|---|
| 114 | int_4 n = (v.NElts()<mBins) ? v.NElts(): mBins;
 | 
|---|
| 115 | if(n>0) for(int_4 i=0;i<n;i++) mNData[i] = v(i);
 | 
|---|
| 116 | return;
 | 
|---|
| 117 | }
 | 
|---|
| 118 | 
 | 
|---|
| 119 | /********* Methode *********/
 | 
|---|
| 120 | /*!
 | 
|---|
| 121 |  Recompute XMin and XMax so that
 | 
|---|
| 122 |  the CENTER of the first bin is exactly XMin and
 | 
|---|
| 123 |  the CENTER of the last bin is exactly XMax.
 | 
|---|
| 124 |  Remember that otherwise
 | 
|---|
| 125 |  XMin is the beginning of the first bin
 | 
|---|
| 126 |  and XMax is the end of the last bin
 | 
|---|
| 127 | */
 | 
|---|
| 128 | void HistoErr::ReCenterBin(void)
 | 
|---|
| 129 | {
 | 
|---|
| 130 |  if(mBins<=1) return;
 | 
|---|
| 131 |  double dx = (mMax-mMin)/(mBins-1);
 | 
|---|
| 132 |  mMin -= dx/2.;
 | 
|---|
| 133 |  mMax += dx/2.;
 | 
|---|
| 134 |  binWidth = (mMax-mMin)/mBins;
 | 
|---|
| 135 | }
 | 
|---|
| 136 | 
 | 
|---|
| 137 | /********* Methode *********/
 | 
|---|
| 138 | /*!
 | 
|---|
| 139 |   Compute the correlation histogram.
 | 
|---|
| 140 |   Each bin content is divided by the number of entries in that bin.
 | 
|---|
| 141 |   Each squared error is divided by the number of entries in that bin.
 | 
|---|
| 142 |   The number of entries by bin is NOT set to 1 (calling ToCorrel
 | 
|---|
| 143 |     many time will change the histogram !)
 | 
|---|
| 144 | */
 | 
|---|
| 145 | void HistoErr::ToCorrel(void)
 | 
|---|
| 146 | {
 | 
|---|
| 147 |  if(mBins<1) return;
 | 
|---|
| 148 |  for(int_4 i=0;i<mBins;i++) {
 | 
|---|
| 149 |    if(mNData[i]<1.) continue;
 | 
|---|
| 150 |    mData[i] /= mNData[i];
 | 
|---|
| 151 |    mErr2[i] /= mNData[i];
 | 
|---|
| 152 |  }
 | 
|---|
| 153 |  return;
 | 
|---|
| 154 | }
 | 
|---|
| 155 | 
 | 
|---|
| 156 | /********* Methode *********/
 | 
|---|
| 157 | /*!
 | 
|---|
| 158 |  Fill the histogram with an other histogram
 | 
|---|
| 159 | */
 | 
|---|
| 160 | void HistoErr::FillFrHErr(HistoErr& hfrom)
 | 
|---|
| 161 | {
 | 
|---|
| 162 |  if(mBins<=0) return;
 | 
|---|
| 163 |  if(hfrom.mBins<=0) return;
 | 
|---|
| 164 | 
 | 
|---|
| 165 |  Zero();
 | 
|---|
| 166 | 
 | 
|---|
| 167 |  for(int_4 i=0;i<hfrom.mBins;i++) {
 | 
|---|
| 168 |    int numBin = FindBin(hfrom.BinCenter(i));
 | 
|---|
| 169 |    if(numBin<0) mUnder += hfrom.mData[i];
 | 
|---|
| 170 |    else if(numBin>=mBins) mOver += hfrom.mData[i];
 | 
|---|
| 171 |    else {
 | 
|---|
| 172 |      mData[numBin]  += hfrom.mData[i];
 | 
|---|
| 173 |      mNData[numBin] += hfrom.mNData[i];
 | 
|---|
| 174 |      mErr2[numBin]  += hfrom.mErr2[i];
 | 
|---|
| 175 |      nHist          += hfrom.mData[i];
 | 
|---|
| 176 |      nEntries++;
 | 
|---|
| 177 |    }
 | 
|---|
| 178 |  }
 | 
|---|
| 179 | 
 | 
|---|
| 180 | }
 | 
|---|
| 181 | 
 | 
|---|
| 182 | /********* Methode *********/
 | 
|---|
| 183 | /*!
 | 
|---|
| 184 |   Operateur egal HistoErr = HistoErr
 | 
|---|
| 185 | */
 | 
|---|
| 186 | HistoErr& HistoErr::operator = (const HistoErr& h)
 | 
|---|
| 187 | {
 | 
|---|
| 188 |   if(this==&h) return *this;
 | 
|---|
| 189 |   Delete();
 | 
|---|
| 190 |   if(h.mBins<=0) return *this;
 | 
|---|
| 191 | 
 | 
|---|
| 192 |   // Copy the "Histo" part
 | 
|---|
| 193 |   (Histo)(*this) = Histo::operator=(h);
 | 
|---|
| 194 |   // Copy the "entries by bin" table
 | 
|---|
| 195 |   allocate_mNData(h.mBins);
 | 
|---|
| 196 |   memcpy(mNData,h.mNData,mBins*sizeof(r_8));
 | 
|---|
| 197 | 
 | 
|---|
| 198 |   return *this;
 | 
|---|
| 199 | }
 | 
|---|
| 200 | 
 | 
|---|
| 201 | ///////////////////////////////////////////////////////////
 | 
|---|
| 202 | // --------------------------------------------------------
 | 
|---|
| 203 | //   Les objets delegues pour la gestion de persistance 
 | 
|---|
| 204 | // --------------------------------------------------------
 | 
|---|
| 205 | ///////////////////////////////////////////////////////////
 | 
|---|
| 206 | 
 | 
|---|
| 207 | DECL_TEMP_SPEC  /* equivalent a template <> , pour SGI-CC en particulier */
 | 
|---|
| 208 | void ObjFileIO<HistoErr>::ReadSelf(PInPersist& is)
 | 
|---|
| 209 | {
 | 
|---|
| 210 | string strg;
 | 
|---|
| 211 | 
 | 
|---|
| 212 | if(dobj==NULL) dobj=new HistoErr;
 | 
|---|
| 213 |   else         dobj->Delete();
 | 
|---|
| 214 | 
 | 
|---|
| 215 | // Lecture entete
 | 
|---|
| 216 | is.GetStr(strg);
 | 
|---|
| 217 | 
 | 
|---|
| 218 | // Lecture des donnees HistoErr
 | 
|---|
| 219 | is.Get(dobj->mBins);
 | 
|---|
| 220 |   dobj->allocate_mNData(dobj->mBins);
 | 
|---|
| 221 | is.Get(dobj->mNData, dobj->mBins);
 | 
|---|
| 222 | 
 | 
|---|
| 223 | // Lecture de l'histogramme
 | 
|---|
| 224 | is >> (Histo&)(*dobj);
 | 
|---|
| 225 | 
 | 
|---|
| 226 | return;
 | 
|---|
| 227 | }
 | 
|---|
| 228 | 
 | 
|---|
| 229 | DECL_TEMP_SPEC  /* equivalent a template <> , pour SGI-CC en particulier */
 | 
|---|
| 230 | void ObjFileIO<HistoErr>::WriteSelf(POutPersist& os) const
 | 
|---|
| 231 | {
 | 
|---|
| 232 | if(dobj == NULL)   return;
 | 
|---|
| 233 | string strg;
 | 
|---|
| 234 | 
 | 
|---|
| 235 | strg = "HistErr";
 | 
|---|
| 236 | os.PutStr(strg);
 | 
|---|
| 237 | 
 | 
|---|
| 238 | // Ecriture des valeurs
 | 
|---|
| 239 | os.Put(dobj->mBins);
 | 
|---|
| 240 | 
 | 
|---|
| 241 | // Ecriture des donnees HistoErr nombre d entree par bin
 | 
|---|
| 242 | os.Put(dobj->mNData, dobj->mBins);
 | 
|---|
| 243 | 
 | 
|---|
| 244 | // Ecriture de l'histogramme
 | 
|---|
| 245 | os << (Histo&)(*dobj);
 | 
|---|
| 246 | 
 | 
|---|
| 247 | return;
 | 
|---|
| 248 | }
 | 
|---|
| 249 | 
 | 
|---|
| 250 | #ifdef __CXX_PRAGMA_TEMPLATES__
 | 
|---|
| 251 | #pragma define_template ObjFileIO<HistoErr>
 | 
|---|
| 252 | #endif
 | 
|---|
| 253 | 
 | 
|---|
| 254 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
 | 
|---|
| 255 | template class ObjFileIO<HistoErr>;
 | 
|---|
| 256 | #endif
 | 
|---|