source: Sophya/trunk/SophyaLib/HiStats/histerr.cc@ 3053

Last change on this file since 3053 was 3053, checked in by cmv, 19 years ago

correction bug + addaptation pour ecriture fits cmv 12/8/2006

File size: 6.1 KB
RevLine 
[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 */
18HistoErr::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 */
25HistoErr::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 */
33HistoErr::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 */
41HistoErr::~HistoErr(void)
42{
[2619]43 Delete();
[2603]44}
[2619]45
[2608]46/********* Methode *********/
[3053]47/*! Gestion de l'allocation */
48void 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 */
59void 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 */
69void 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*/
81void 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]92void HistoErr::SetBin(int_4 numBin, r_8 w, r_8 e, r_8 nb)
[2604]93{
94Histo::SetBin(numBin,w);
95Histo::SetErr2(numBin,e*e);
96SetNentB(numBin,nb);
97return;
98}
99
100/********* Methode *********/
101/*!
102 Remplissage nombre d'entrees pour le bin numBin
103*/
[2626]104void HistoErr::SetNentB(int_4 numBin, r_8 nb)
[2604]105{
106 if(numBin>=0 && numBin<mBins) mNData[numBin] = nb;
107return;
108}
109
110/*!
[2603]111 Remplissage d'un tableau avec les nombres d'entrees dans le bin de l'histo
112*/
[2626]113void HistoErr::GetNBin(TVector<r_8>& v) const
[2603]114{
115v.Realloc(mBins);
116for(int_4 i=0;i<mBins;i++) v(i) = mNData[i];
117return;
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]124void HistoErr::PutNBin(TVector<r_8> &v)
[2603]125{
[2604]126int_4 n = (v.NElts()<mBins) ? v.NElts(): mBins;
127if(n>0) for(int_4 i=0;i<n;i++) mNData[i] = v(i);
[2603]128return;
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*/
139void 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*/
155void 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*/
171void 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*/
198HistoErr& 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*/
218void 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
230DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
231void ObjFileIO<HistoErr>::ReadSelf(PInPersist& is)
232{
[2608]233string strg;
[2603]234
235if(dobj==NULL) dobj=new HistoErr;
236 else dobj->Delete();
237
238// Lecture entete
[2608]239is.GetStr(strg);
[2603]240
[3049]241// Nombre d'appels a ToCorrel/FromCorrel
242is.Get(dobj->mCorrel);
243
[2603]244// Lecture des donnees HistoErr
245is.Get(dobj->mBins);
[2608]246 dobj->allocate_mNData(dobj->mBins);
[2603]247is.Get(dobj->mNData, dobj->mBins);
248
249// Lecture de l'histogramme
250is >> (Histo&)(*dobj);
251
252return;
253}
254
255DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
256void ObjFileIO<HistoErr>::WriteSelf(POutPersist& os) const
257{
258if(dobj == NULL) return;
[2608]259string strg;
[2603]260
[3049]261// Ecriture entete
[2608]262strg = "HistErr";
263os.PutStr(strg);
[2603]264
[3049]265// Nombre d'appels a ToCorrel/FromCorrel
266os.Put(dobj->mCorrel);
267
[2603]268// Ecriture des valeurs
269os.Put(dobj->mBins);
270
271// Ecriture des donnees HistoErr nombre d entree par bin
272os.Put(dobj->mNData, dobj->mBins);
273
274// Ecriture de l'histogramme
275os << (Histo&)(*dobj);
276
277return;
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]285template class SOPHYA::ObjFileIO<HistoErr>;
[2603]286#endif
Note: See TracBrowser for help on using the repository browser.