| 1 | #include "sopnamsp.h"
|
|---|
| 2 | #include "machdefs.h"
|
|---|
| 3 | #include <string.h>
|
|---|
| 4 | #include <stdio.h>
|
|---|
| 5 | #include <math.h>
|
|---|
| 6 | #include "perrors.h"
|
|---|
| 7 | #include "fioarr.h"
|
|---|
| 8 | #include "histerr.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 | : xmin_(1.), xmax_(-1.), nx_(0), dx_(0.)
|
|---|
| 20 | , mCorrel(0)
|
|---|
| 21 | {
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| 24 | /********* Methode *********/
|
|---|
| 25 | /*! Constructeur d'un histo */
|
|---|
| 26 | HistoErr::HistoErr(r_8 xmin,r_8 xmax,int_4 nx)
|
|---|
| 27 | : mCorrel(0)
|
|---|
| 28 | {
|
|---|
| 29 | CreateOrResize(xmin,xmax,nx);
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | /********* Methode *********/
|
|---|
| 33 | /*! Constructeur par copie */
|
|---|
| 34 | HistoErr::HistoErr(const HistoErr& H)
|
|---|
| 35 | : mCorrel(H.mCorrel)
|
|---|
| 36 | {
|
|---|
| 37 | if(H.nx_<=0) return;
|
|---|
| 38 | CreateOrResize(H.xmin_,H.xmax_,H.nx_);
|
|---|
| 39 | data_ = H.data_;
|
|---|
| 40 | err2_ = H.err2_;
|
|---|
| 41 | ndata_ = H.ndata_;
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | /********* Methode *********/
|
|---|
| 45 | /*! Destructeur */
|
|---|
| 46 | HistoErr::~HistoErr(void)
|
|---|
| 47 | {
|
|---|
| 48 | mCorrel = 0;
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | /********* Methode *********/
|
|---|
| 52 | /*! Gestion de l'allocation */
|
|---|
| 53 | void HistoErr::CreateOrResize(r_8 xmin,r_8 xmax,int_4 nx)
|
|---|
| 54 | {
|
|---|
| 55 | xmin_ = xmin; xmax_ = xmax; nx_ = nx; dx_=0.;
|
|---|
| 56 | if(nx_>0) {
|
|---|
| 57 | data_.ReSize(nx_); data_ = 0.;
|
|---|
| 58 | err2_.ReSize(nx_); err2_ = 0.;
|
|---|
| 59 | ndata_.ReSize(nx_); ndata_ = 0.;
|
|---|
| 60 | dx_ = (xmax_-xmin_)/nx_;
|
|---|
| 61 | }
|
|---|
| 62 | mCorrel = 0;
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | /********* Methode *********/
|
|---|
| 66 | /*!
|
|---|
| 67 | Remise a zero
|
|---|
| 68 | */
|
|---|
| 69 | void HistoErr::Zero(void)
|
|---|
| 70 | {
|
|---|
| 71 | if(nx_<=0) return;
|
|---|
| 72 | data_ = 0.;
|
|---|
| 73 | err2_ = 0.;
|
|---|
| 74 | ndata_ = 0.;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | /********* Methode *********/
|
|---|
| 78 | /*!
|
|---|
| 79 | Recompute XMin (and XMax so that
|
|---|
| 80 | the CENTER of the first bin is exactly XMin and
|
|---|
| 81 | the CENTER of the last bin is exactly XMax.
|
|---|
| 82 | Remember that otherwise
|
|---|
| 83 | XMin is the beginning of the first bin
|
|---|
| 84 | and XMax is the end of the last bin
|
|---|
| 85 | */
|
|---|
| 86 | void HistoErr::ReCenterBin(void)
|
|---|
| 87 | {
|
|---|
| 88 | if(nx_<=1) return;
|
|---|
| 89 | double dx = (xmax_-xmin_)/(nx_-1);
|
|---|
| 90 | xmin_ -= dx/2.;
|
|---|
| 91 | xmax_ += dx/2.;
|
|---|
| 92 | dx_ = (xmax_-xmin_)/nx_;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | /********* Methode *********/
|
|---|
| 96 | /*!
|
|---|
| 97 | Compute the correlation histogram.
|
|---|
| 98 | Each bin content is divided by the number of entries in that bin.
|
|---|
| 99 | Each squared error is divided by the number of entries in that bin.
|
|---|
| 100 | The number of entries by bin is NOT set to 1 (calling ToCorrel
|
|---|
| 101 | many time will change the histogram !)
|
|---|
| 102 | */
|
|---|
| 103 | void HistoErr::ToCorrel(void)
|
|---|
| 104 | {
|
|---|
| 105 | if(nx_<1) return;
|
|---|
| 106 | mCorrel++;
|
|---|
| 107 | for(int_4 i=0;i<nx_;i++) {
|
|---|
| 108 | if(ndata_(i)<1.) continue;
|
|---|
| 109 | data_(i) /= ndata_(i);
|
|---|
| 110 | err2_(i) /= ndata_(i);
|
|---|
| 111 | }
|
|---|
| 112 | return;
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | /********* Methode *********/
|
|---|
| 116 | /*!
|
|---|
| 117 | Recompute back the original HistoErr before ToCorrel action
|
|---|
| 118 | */
|
|---|
| 119 | void HistoErr::FromCorrel(void)
|
|---|
| 120 | {
|
|---|
| 121 | if(nx_<1) return;
|
|---|
| 122 | mCorrel--;
|
|---|
| 123 | for(int_4 i=0;i<nx_;i++) {
|
|---|
| 124 | if(ndata_(i)<1.) continue;
|
|---|
| 125 | data_(i) *= ndata_(i);
|
|---|
| 126 | err2_(i) *= ndata_(i);
|
|---|
| 127 | }
|
|---|
| 128 | return;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | /********* Methode *********/
|
|---|
| 132 | /*!
|
|---|
| 133 | Fill the histogram with an other histogram
|
|---|
| 134 | */
|
|---|
| 135 | void HistoErr::FillFrHErr(HistoErr& hfrom)
|
|---|
| 136 | {
|
|---|
| 137 | if(nx_<=0) return;
|
|---|
| 138 | if(hfrom.nx_<=0) return;
|
|---|
| 139 |
|
|---|
| 140 | Zero();
|
|---|
| 141 |
|
|---|
| 142 | for(int_4 i=0;i<hfrom.nx_;i++) {
|
|---|
| 143 | r_8 x = hfrom.BinCenter(i);
|
|---|
| 144 | int ii = FindBin(x);
|
|---|
| 145 | if(ii<0 || ii>=nx_) continue;
|
|---|
| 146 | data_(ii) += hfrom.data_(ii);
|
|---|
| 147 | err2_(ii) += hfrom.err2_(ii);
|
|---|
| 148 | ndata_(ii) += hfrom.ndata_(ii);
|
|---|
| 149 | }
|
|---|
| 150 | mCorrel = hfrom.mCorrel;
|
|---|
| 151 |
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | /********* Methode *********/
|
|---|
| 155 | /*!
|
|---|
| 156 | Operateur egal HistoErr = HistoErr
|
|---|
| 157 | */
|
|---|
| 158 | HistoErr& HistoErr::operator = (const HistoErr& h)
|
|---|
| 159 | {
|
|---|
| 160 | if(this==&h) return *this;
|
|---|
| 161 | CreateOrResize(h.xmin_,h.xmax_,h.nx_);
|
|---|
| 162 | data_ = h.data_;
|
|---|
| 163 | err2_ = h.err2_;
|
|---|
| 164 | ndata_ = h.ndata_;
|
|---|
| 165 | mCorrel = h.mCorrel;
|
|---|
| 166 | return *this;
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | /********* Methode *********/
|
|---|
| 170 | /*!
|
|---|
| 171 | Operateur de multiplication par une constante
|
|---|
| 172 | */
|
|---|
| 173 | HistoErr& HistoErr::operator *= (r_8 b)
|
|---|
| 174 | {
|
|---|
| 175 | r_8 b2 = b*b;
|
|---|
| 176 | for(int_4 i=0;i<nx_;i++) {
|
|---|
| 177 | data_(i) *= b;
|
|---|
| 178 | err2_(i) *= b2;
|
|---|
| 179 | }
|
|---|
| 180 | return *this;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | /********* Methode *********/
|
|---|
| 184 | /*!
|
|---|
| 185 | Print info
|
|---|
| 186 | */
|
|---|
| 187 | void HistoErr::Show(ostream & os) const
|
|---|
| 188 | {
|
|---|
| 189 | os <<"HistoErr(ncorrel="<<mCorrel<<")"<<endl
|
|---|
| 190 | <<" nx="<<nx_<<" ["<<xmin_<<","<<xmax_<<"] dx="<<dx_<<endl;
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | ///////////////////////////////////////////////////////////
|
|---|
| 194 | // --------------------------------------------------------
|
|---|
| 195 | // Les objets delegues pour la gestion de persistance
|
|---|
| 196 | // --------------------------------------------------------
|
|---|
| 197 | ///////////////////////////////////////////////////////////
|
|---|
| 198 |
|
|---|
| 199 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
|---|
| 200 | void ObjFileIO<HistoErr>::ReadSelf(PInPersist& is)
|
|---|
| 201 | {
|
|---|
| 202 | string strg;
|
|---|
| 203 |
|
|---|
| 204 | if(dobj==NULL) dobj = new HistoErr;
|
|---|
| 205 |
|
|---|
| 206 | // Lecture entete
|
|---|
| 207 | is.GetStr(strg);
|
|---|
| 208 |
|
|---|
| 209 | // Nombre d'appels a ToCorrel/FromCorrel
|
|---|
| 210 | is.Get(dobj->mCorrel);
|
|---|
| 211 |
|
|---|
| 212 | // Lecture des parametres HistoErr
|
|---|
| 213 | is.Get(dobj->xmin_);
|
|---|
| 214 | is.Get(dobj->xmax_);
|
|---|
| 215 | is.Get(dobj->nx_);
|
|---|
| 216 | is.Get(dobj->dx_);
|
|---|
| 217 |
|
|---|
| 218 | // Lecture des donnees
|
|---|
| 219 | if(dobj->nx_>0) {
|
|---|
| 220 | is >> dobj->data_;
|
|---|
| 221 | is >> dobj->err2_;
|
|---|
| 222 | is >> dobj->ndata_;
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | return;
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
|---|
| 229 | void ObjFileIO<HistoErr>::WriteSelf(POutPersist& os) const
|
|---|
| 230 | {
|
|---|
| 231 | if(dobj == NULL) return;
|
|---|
| 232 | string strg;
|
|---|
| 233 |
|
|---|
| 234 | // Ecriture entete
|
|---|
| 235 | strg = "HistErr";
|
|---|
| 236 | os.PutStr(strg);
|
|---|
| 237 |
|
|---|
| 238 | // Nombre d'appels a ToCorrel/FromCorrel
|
|---|
| 239 | os.Put(dobj->mCorrel);
|
|---|
| 240 |
|
|---|
| 241 | // Ecriture des parametres HistoErr
|
|---|
| 242 | os.Put(dobj->xmin_);
|
|---|
| 243 | os.Put(dobj->xmax_);
|
|---|
| 244 | os.Put(dobj->nx_);
|
|---|
| 245 | os.Put(dobj->dx_);
|
|---|
| 246 |
|
|---|
| 247 | // Ecriture des donnees
|
|---|
| 248 | if(dobj->nx_>0) {
|
|---|
| 249 | os << dobj->data_;
|
|---|
| 250 | os << dobj->err2_;
|
|---|
| 251 | os << dobj->ndata_;
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | return;
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
|---|
| 258 | #pragma define_template ObjFileIO<HistoErr>
|
|---|
| 259 | #endif
|
|---|
| 260 |
|
|---|
| 261 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
|---|
| 262 | template class SOPHYA::ObjFileIO<HistoErr>;
|
|---|
| 263 | #endif
|
|---|