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

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

Fits IO Histo,HProf,HistErr,Histo2D cmv 11/8/2006

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), mCorrel(0)
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), mCorrel(0)
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), mCorrel(H.mCorrel)
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 mCorrel = 0;
75}
76
77/********* Methode *********/
78/*!
79 Remplissage du contenu de l'histo pour le bin numBin poids w et l'erreur e
80*/
81void HistoErr::SetBin(int_4 numBin, r_8 w, r_8 e, r_8 nb)
82{
83Histo::SetBin(numBin,w);
84Histo::SetErr2(numBin,e*e);
85SetNentB(numBin,nb);
86return;
87}
88
89/********* Methode *********/
90/*!
91 Remplissage nombre d'entrees pour le bin numBin
92*/
93void HistoErr::SetNentB(int_4 numBin, r_8 nb)
94{
95 if(numBin>=0 && numBin<mBins) mNData[numBin] = nb;
96return;
97}
98
99/*!
100 Remplissage d'un tableau avec les nombres d'entrees dans le bin de l'histo
101*/
102void HistoErr::GetNBin(TVector<r_8>& v) const
103{
104v.Realloc(mBins);
105for(int_4 i=0;i<mBins;i++) v(i) = mNData[i];
106return;
107}
108
109/********* Methode *********/
110/*!
111 Remplissage du nombre d'entrees dans les bins de l'histo avec les valeurs d'un vecteur
112*/
113void HistoErr::PutNBin(TVector<r_8> &v)
114{
115int_4 n = (v.NElts()<mBins) ? v.NElts(): mBins;
116if(n>0) for(int_4 i=0;i<n;i++) mNData[i] = v(i);
117return;
118}
119
120/********* Methode *********/
121/*!
122 Compute the correlation histogram.
123 Each bin content is divided by the number of entries in that bin.
124 Each squared error is divided by the number of entries in that bin.
125 The number of entries by bin is NOT set to 1 (calling ToCorrel
126 many time will change the histogram !)
127*/
128void HistoErr::ToCorrel(void)
129{
130 if(mBins<1) return;
131 mCorrel++;
132 for(int_4 i=0;i<mBins;i++) {
133 if(mNData[i]<1.) continue;
134 mData[i] /= mNData[i];
135 mErr2[i] /= mNData[i];
136 }
137 return;
138}
139
140/********* Methode *********/
141/*!
142 Recompute back the original HistoErr before ToCorrel action
143*/
144void HistoErr::FromCorrel(void)
145{
146 if(mBins<1) return;
147 mCorrel--;
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 Fill the histogram with an other histogram
159*/
160void HistoErr::FillFrHErr(HistoErr& hfrom)
161{
162 if(mBins<=0) return;
163 if(hfrom.mBins<=0) return;
164
165 Zero();
166
167 for(int_4 i=0;i<hfrom.mBins;i++) {
168 int numBin = FindBin(hfrom.BinCenter(i));
169 if(numBin<0) mUnder += hfrom.mData[i];
170 else if(numBin>=mBins) mOver += hfrom.mData[i];
171 else {
172 mData[numBin] += hfrom.mData[i];
173 mNData[numBin] += hfrom.mNData[i];
174 mErr2[numBin] += hfrom.mErr2[i];
175 nHist += hfrom.mData[i];
176 nEntries++;
177 }
178 }
179 mCorrel = hfrom.mCorrel;
180
181}
182
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(r_8));
198 mCorrel = h.mCorrel;
199
200 return *this;
201}
202
203///////////////////////////////////////////////////////////
204// --------------------------------------------------------
205// Les objets delegues pour la gestion de persistance
206// --------------------------------------------------------
207///////////////////////////////////////////////////////////
208
209DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
210void ObjFileIO<HistoErr>::ReadSelf(PInPersist& is)
211{
212string strg;
213
214if(dobj==NULL) dobj=new HistoErr;
215 else dobj->Delete();
216
217// Lecture entete
218is.GetStr(strg);
219
220// Nombre d'appels a ToCorrel/FromCorrel
221is.Get(dobj->mCorrel);
222
223// Lecture des donnees HistoErr
224is.Get(dobj->mBins);
225 dobj->allocate_mNData(dobj->mBins);
226is.Get(dobj->mNData, dobj->mBins);
227
228// Lecture de l'histogramme
229is >> (Histo&)(*dobj);
230
231return;
232}
233
234DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
235void ObjFileIO<HistoErr>::WriteSelf(POutPersist& os) const
236{
237if(dobj == NULL) return;
238string strg;
239
240// Ecriture entete
241strg = "HistErr";
242os.PutStr(strg);
243
244// Nombre d'appels a ToCorrel/FromCorrel
245os.Put(dobj->mCorrel);
246
247// Ecriture des valeurs
248os.Put(dobj->mBins);
249
250// Ecriture des donnees HistoErr nombre d entree par bin
251os.Put(dobj->mNData, dobj->mBins);
252
253// Ecriture de l'histogramme
254os << (Histo&)(*dobj);
255
256return;
257}
258
259#ifdef __CXX_PRAGMA_TEMPLATES__
260#pragma define_template ObjFileIO<HistoErr>
261#endif
262
263#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
264template class SOPHYA::ObjFileIO<HistoErr>;
265#endif
Note: See TracBrowser for help on using the repository browser.