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

Last change on this file since 2619 was 2619, checked in by cmv, 21 years ago

correc bug HistoErr::operator=() + remise en forme createur/destructeur Histos cmv 15/9/04

File size: 5.5 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)
19: Histo(), mNData(NULL)
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)
[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 */
35HistoErr::HistoErr(const HistoErr& H)
[2608]36: Histo(H), mNData(NULL)
[2603]37{
[2608]38 allocate_mNData(H.mBins);
[2603]39 if(mBins>0) memcpy(mNData,H.mNData,mBins*sizeof(uint_4));
40}
41
42/********* Methode *********/
43/*! Destructeur */
44HistoErr::~HistoErr(void)
45{
[2619]46 Delete();
[2603]47}
[2619]48
[2608]49/********* Methode *********/
50/*! Allocation du tableau mNData */
51void HistoErr::allocate_mNData(int nbin)
52{
53 if(nbin<=0) return;
54 if(mNData) {delete [] mNData; mNData=NULL;}
55 mNData = new uint_4[nbin];
56}
[2603]57
58/********* Methode *********/
[2619]59/*! Delete des tableaux */
60void 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*/
70void HistoErr::Zero(void)
71{
72 Histo::Zero();
[2619]73 if(mNData) memset(mNData,0,mBins*sizeof(uint_4));
[2603]74}
75
76/********* Methode *********/
77/*!
78 Addition du contenu de l'histo pour abscisse x poids w et l'erreur e
79*/
80void 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 {
86 mData[numBin] += w; mNData[numBin]++; mErr2[numBin] += e*e;
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*/
95void 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 {
100 mData[numBin] += w; mNData[numBin]++; mErr2[numBin] += e*e;
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*/
109void HistoErr::SetBin(int_4 numBin, r_8 w, r_8 e, uint_4 nb)
110{
111Histo::SetBin(numBin,w);
112Histo::SetErr2(numBin,e*e);
113SetNentB(numBin,nb);
114return;
115}
116
117/********* Methode *********/
118/*!
119 Remplissage nombre d'entrees pour le bin numBin
120*/
121void HistoErr::SetNentB(int_4 numBin, uint_4 nb)
122{
123 if(numBin>=0 && numBin<mBins) mNData[numBin] = nb;
124return;
125}
126
127/*!
[2603]128 Remplissage d'un tableau avec les nombres d'entrees dans le bin de l'histo
129*/
130void HistoErr::GetNBin(TVector<int_4>& v) const
131{
132v.Realloc(mBins);
133for(int_4 i=0;i<mBins;i++) v(i) = mNData[i];
134return;
135}
136
137/********* Methode *********/
138/*!
139 Remplissage du nombre d'entrees dans les bins de l'histo avec les valeurs d'un vecteur
140*/
141void HistoErr::PutNBin(TVector<int_4> &v)
142{
[2604]143int_4 n = (v.NElts()<mBins) ? v.NElts(): mBins;
144if(n>0) for(int_4 i=0;i<n;i++) mNData[i] = v(i);
[2603]145return;
146}
147
[2608]148/********* Methode *********/
149/*!
150 Recompute XMin and XMax so that
151 the center of the first bin is exactly xcmin and
152 the center of the last bin is exactly xcmax
153*/
154void HistoErr::ReCenterBin(r_8 xcmin,r_8 xcmax)
155{
156 if(xcmax<=xcmin) return;
157 if(mBins<=1) return;
158 double dx = (mMax-mMin)/(mBins-1);
159 mMin -= dx/2.;
160 mMax += dx/2.;
161 binWidth = (mMax-mMin)/mBins;
162}
[2604]163
164/********* Methode *********/
165/*!
166 Compute the correlation histogram.
167 Each bin content is divided by the number of entries in that bin.
168 Each squared error is divided by the number of entries in that bin.
169 The number of entries by bin is set to 1
170*/
171void HistoErr::ToCorrel(void)
172{
173 if(mBins<1) return;
174 for(int_4 i=0;i<mBins;i++) {
175 if(mNData[i]<1) continue;
176 mData[i] /= (r_8) mNData[i];
177 mErr2[i] /= (r_8) mNData[i];
178 mNData[i] = 1;
179 }
180 return;
181}
182
[2619]183/********* Methode *********/
184/*!
185 Operateur egal HistoErr = HistoErr
186*/
187HistoErr& HistoErr::operator = (const HistoErr& h)
188{
189 if(this==&h) return *this;
190 Delete();
191 if(h.mBins<=0) return *this;
192
193 // Copy the "Histo" part
194 (Histo)(*this) = Histo::operator=(h);
195 // Copy the "entries by bin" table
196 allocate_mNData(h.mBins);
197 memcpy(mNData,h.mNData,mBins*sizeof(uint_4));
198
199 return *this;
200}
201
[2603]202///////////////////////////////////////////////////////////
203// --------------------------------------------------------
204// Les objets delegues pour la gestion de persistance
205// --------------------------------------------------------
206///////////////////////////////////////////////////////////
207
208DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
209void ObjFileIO<HistoErr>::ReadSelf(PInPersist& is)
210{
[2608]211string strg;
[2603]212
213if(dobj==NULL) dobj=new HistoErr;
214 else dobj->Delete();
215
216// Lecture entete
[2608]217is.GetStr(strg);
[2603]218
219// Lecture des donnees HistoErr
220is.Get(dobj->mBins);
[2608]221 dobj->allocate_mNData(dobj->mBins);
[2603]222is.Get(dobj->mNData, dobj->mBins);
223
224// Lecture de l'histogramme
225is >> (Histo&)(*dobj);
226
227return;
228}
229
230DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
231void ObjFileIO<HistoErr>::WriteSelf(POutPersist& os) const
232{
233if(dobj == NULL) return;
[2608]234string strg;
[2603]235
[2608]236strg = "HistErr";
237os.PutStr(strg);
[2603]238
239// Ecriture des valeurs
240os.Put(dobj->mBins);
241
242// Ecriture des donnees HistoErr nombre d entree par bin
243os.Put(dobj->mNData, dobj->mBins);
244
245// Ecriture de l'histogramme
246os << (Histo&)(*dobj);
247
248return;
249}
250
251#ifdef __CXX_PRAGMA_TEMPLATES__
252#pragma define_template ObjFileIO<HistoErr>
253#endif
254
255#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
256template class ObjFileIO<HistoErr>;
257#endif
Note: See TracBrowser for help on using the repository browser.