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

Last change on this file since 2819 was 2819, checked in by cmv, 20 years ago

FronCorrel() pour revenir en arriere cmv 22/08/2005k

File size: 5.7 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 Remplissage du contenu de l'histo pour le bin numBin poids w et l'erreur e
79*/
80void HistoErr::SetBin(int_4 numBin, r_8 w, r_8 e, r_8 nb)
81{
82Histo::SetBin(numBin,w);
83Histo::SetErr2(numBin,e*e);
84SetNentB(numBin,nb);
85return;
86}
87
88/********* Methode *********/
89/*!
90 Remplissage nombre d'entrees pour le bin numBin
91*/
92void HistoErr::SetNentB(int_4 numBin, r_8 nb)
93{
94 if(numBin>=0 && numBin<mBins) mNData[numBin] = nb;
95return;
96}
97
98/*!
99 Remplissage d'un tableau avec les nombres d'entrees dans le bin de l'histo
100*/
101void HistoErr::GetNBin(TVector<r_8>& v) const
102{
103v.Realloc(mBins);
104for(int_4 i=0;i<mBins;i++) v(i) = mNData[i];
105return;
106}
107
108/********* Methode *********/
109/*!
110 Remplissage du nombre d'entrees dans les bins de l'histo avec les valeurs d'un vecteur
111*/
112void HistoErr::PutNBin(TVector<r_8> &v)
113{
114int_4 n = (v.NElts()<mBins) ? v.NElts(): mBins;
115if(n>0) for(int_4 i=0;i<n;i++) mNData[i] = v(i);
116return;
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*/
128void 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*/
145void 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 Recompute back the original HistoErr before ToCorrel action
159*/
160void HistoErr::FromCorrel(void)
161{
162 if(mBins<1) return;
163 for(int_4 i=0;i<mBins;i++) {
164 if(mNData[i]<1.) continue;
165 mData[i] *= mNData[i];
166 mErr2[i] *= mNData[i];
167 }
168 return;
169}
170
171/********* Methode *********/
172/*!
173 Fill the histogram with an other histogram
174*/
175void HistoErr::FillFrHErr(HistoErr& hfrom)
176{
177 if(mBins<=0) return;
178 if(hfrom.mBins<=0) return;
179
180 Zero();
181
182 for(int_4 i=0;i<hfrom.mBins;i++) {
183 int numBin = FindBin(hfrom.BinCenter(i));
184 if(numBin<0) mUnder += hfrom.mData[i];
185 else if(numBin>=mBins) mOver += hfrom.mData[i];
186 else {
187 mData[numBin] += hfrom.mData[i];
188 mNData[numBin] += hfrom.mNData[i];
189 mErr2[numBin] += hfrom.mErr2[i];
190 nHist += hfrom.mData[i];
191 nEntries++;
192 }
193 }
194
195}
196
197/********* Methode *********/
198/*!
199 Operateur egal HistoErr = HistoErr
200*/
201HistoErr& HistoErr::operator = (const HistoErr& h)
202{
203 if(this==&h) return *this;
204 Delete();
205 if(h.mBins<=0) return *this;
206
207 // Copy the "Histo" part
208 (Histo)(*this) = Histo::operator=(h);
209 // Copy the "entries by bin" table
210 allocate_mNData(h.mBins);
211 memcpy(mNData,h.mNData,mBins*sizeof(r_8));
212
213 return *this;
214}
215
216///////////////////////////////////////////////////////////
217// --------------------------------------------------------
218// Les objets delegues pour la gestion de persistance
219// --------------------------------------------------------
220///////////////////////////////////////////////////////////
221
222DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
223void ObjFileIO<HistoErr>::ReadSelf(PInPersist& is)
224{
225string strg;
226
227if(dobj==NULL) dobj=new HistoErr;
228 else dobj->Delete();
229
230// Lecture entete
231is.GetStr(strg);
232
233// Lecture des donnees HistoErr
234is.Get(dobj->mBins);
235 dobj->allocate_mNData(dobj->mBins);
236is.Get(dobj->mNData, dobj->mBins);
237
238// Lecture de l'histogramme
239is >> (Histo&)(*dobj);
240
241return;
242}
243
244DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
245void ObjFileIO<HistoErr>::WriteSelf(POutPersist& os) const
246{
247if(dobj == NULL) return;
248string strg;
249
250strg = "HistErr";
251os.PutStr(strg);
252
253// Ecriture des valeurs
254os.Put(dobj->mBins);
255
256// Ecriture des donnees HistoErr nombre d entree par bin
257os.Put(dobj->mNData, dobj->mBins);
258
259// Ecriture de l'histogramme
260os << (Histo&)(*dobj);
261
262return;
263}
264
265#ifdef __CXX_PRAGMA_TEMPLATES__
266#pragma define_template ObjFileIO<HistoErr>
267#endif
268
269#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
270template class ObjFileIO<HistoErr>;
271#endif
Note: See TracBrowser for help on using the repository browser.