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

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

petit bug dans methode ReCenterBin cmv 6/10/04

File size: 5.6 KB
Line 
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 */
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)
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 */
35HistoErr::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 */
44HistoErr::~HistoErr(void)
45{
46 Delete();
47}
48
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 r_8[nbin];
56}
57
58/********* Methode *********/
59/*! Delete des tableaux */
60void HistoErr::Delete(void)
61{
62 Histo::Delete();
63 if(mNData) {delete [] mNData; mNData=NULL;}
64}
65
66/********* Methode *********/
67/*!
68 Remise a zero
69*/
70void HistoErr::Zero(void)
71{
72 Histo::Zero();
73 if(mNData) memset(mNData,0,mBins*sizeof(r_8));
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] += 1.; 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] += 1.; mErr2[numBin] += e*e;
101 nHist += w; nEntries++;
102 }
103}
104
105/********* Methode *********/
106/*!
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, r_8 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, r_8 nb)
122{
123 if(numBin>=0 && numBin<mBins) mNData[numBin] = nb;
124return;
125}
126
127/*!
128 Remplissage d'un tableau avec les nombres d'entrees dans le bin de l'histo
129*/
130void HistoErr::GetNBin(TVector<r_8>& 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<r_8> &v)
142{
143int_4 n = (v.NElts()<mBins) ? v.NElts(): mBins;
144if(n>0) for(int_4 i=0;i<n;i++) mNData[i] = v(i);
145return;
146}
147
148/********* Methode *********/
149/*!
150 Recompute XMin and XMax so that
151 the CENTER of the first bin is exactly XMin and
152 the CENTER of the last bin is exactly XMax.
153 Remember that otherwise
154 XMin is the beginning of the first bin
155 and XMax is the end of the last bin
156*/
157void HistoErr::ReCenterBin(void)
158{
159 if(mBins<=1) return;
160 double dx = (mMax-mMin)/(mBins-1);
161 mMin -= dx/2.;
162 mMax += dx/2.;
163 binWidth = (mMax-mMin)/mBins;
164}
165
166/********* Methode *********/
167/*!
168 Compute the correlation histogram.
169 Each bin content is divided by the number of entries in that bin.
170 Each squared error is divided by the number of entries in that bin.
171 The number of entries by bin is NOT set to 1 (calling ToCorrel
172 many time will change the histogram !)
173*/
174void HistoErr::ToCorrel(void)
175{
176 if(mBins<1) return;
177 for(int_4 i=0;i<mBins;i++) {
178 if(mNData[i]<1.) continue;
179 mData[i] /= mNData[i];
180 mErr2[i] /= mNData[i];
181 }
182 return;
183}
184
185/********* Methode *********/
186/*!
187 Operateur egal HistoErr = HistoErr
188*/
189HistoErr& HistoErr::operator = (const HistoErr& h)
190{
191 if(this==&h) return *this;
192 Delete();
193 if(h.mBins<=0) return *this;
194
195 // Copy the "Histo" part
196 (Histo)(*this) = Histo::operator=(h);
197 // Copy the "entries by bin" table
198 allocate_mNData(h.mBins);
199 memcpy(mNData,h.mNData,mBins*sizeof(r_8));
200
201 return *this;
202}
203
204///////////////////////////////////////////////////////////
205// --------------------------------------------------------
206// Les objets delegues pour la gestion de persistance
207// --------------------------------------------------------
208///////////////////////////////////////////////////////////
209
210DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
211void ObjFileIO<HistoErr>::ReadSelf(PInPersist& is)
212{
213string strg;
214
215if(dobj==NULL) dobj=new HistoErr;
216 else dobj->Delete();
217
218// Lecture entete
219is.GetStr(strg);
220
221// Lecture des donnees HistoErr
222is.Get(dobj->mBins);
223 dobj->allocate_mNData(dobj->mBins);
224is.Get(dobj->mNData, dobj->mBins);
225
226// Lecture de l'histogramme
227is >> (Histo&)(*dobj);
228
229return;
230}
231
232DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
233void ObjFileIO<HistoErr>::WriteSelf(POutPersist& os) const
234{
235if(dobj == NULL) return;
236string strg;
237
238strg = "HistErr";
239os.PutStr(strg);
240
241// Ecriture des valeurs
242os.Put(dobj->mBins);
243
244// Ecriture des donnees HistoErr nombre d entree par bin
245os.Put(dobj->mNData, dobj->mBins);
246
247// Ecriture de l'histogramme
248os << (Histo&)(*dobj);
249
250return;
251}
252
253#ifdef __CXX_PRAGMA_TEMPLATES__
254#pragma define_template ObjFileIO<HistoErr>
255#endif
256
257#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
258template class ObjFileIO<HistoErr>;
259#endif
Note: See TracBrowser for help on using the repository browser.