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

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

ReCenterBin deplace methode Histo (was HistErr) cmv 7/8/2006

File size: 5.3 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 Compute the correlation histogram.
122 Each bin content is divided by the number of entries in that bin.
123 Each squared error is divided by the number of entries in that bin.
124 The number of entries by bin is NOT set to 1 (calling ToCorrel
125 many time will change the histogram !)
126*/
127void HistoErr::ToCorrel(void)
128{
129 if(mBins<1) return;
130 for(int_4 i=0;i<mBins;i++) {
131 if(mNData[i]<1.) continue;
132 mData[i] /= mNData[i];
133 mErr2[i] /= mNData[i];
134 }
135 return;
136}
137
138/********* Methode *********/
139/*!
140 Recompute back the original HistoErr before ToCorrel action
141*/
142void HistoErr::FromCorrel(void)
143{
144 if(mBins<1) return;
145 for(int_4 i=0;i<mBins;i++) {
146 if(mNData[i]<1.) continue;
147 mData[i] *= mNData[i];
148 mErr2[i] *= mNData[i];
149 }
150 return;
151}
152
153/********* Methode *********/
154/*!
155 Fill the histogram with an other histogram
156*/
157void HistoErr::FillFrHErr(HistoErr& hfrom)
158{
159 if(mBins<=0) return;
160 if(hfrom.mBins<=0) return;
161
162 Zero();
163
164 for(int_4 i=0;i<hfrom.mBins;i++) {
165 int numBin = FindBin(hfrom.BinCenter(i));
166 if(numBin<0) mUnder += hfrom.mData[i];
167 else if(numBin>=mBins) mOver += hfrom.mData[i];
168 else {
169 mData[numBin] += hfrom.mData[i];
170 mNData[numBin] += hfrom.mNData[i];
171 mErr2[numBin] += hfrom.mErr2[i];
172 nHist += hfrom.mData[i];
173 nEntries++;
174 }
175 }
176
177}
178
179/********* Methode *********/
180/*!
181 Operateur egal HistoErr = HistoErr
182*/
183HistoErr& HistoErr::operator = (const HistoErr& h)
184{
185 if(this==&h) return *this;
186 Delete();
187 if(h.mBins<=0) return *this;
188
189 // Copy the "Histo" part
190 (Histo)(*this) = Histo::operator=(h);
191 // Copy the "entries by bin" table
192 allocate_mNData(h.mBins);
193 memcpy(mNData,h.mNData,mBins*sizeof(r_8));
194
195 return *this;
196}
197
198///////////////////////////////////////////////////////////
199// --------------------------------------------------------
200// Les objets delegues pour la gestion de persistance
201// --------------------------------------------------------
202///////////////////////////////////////////////////////////
203
204DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
205void ObjFileIO<HistoErr>::ReadSelf(PInPersist& is)
206{
207string strg;
208
209if(dobj==NULL) dobj=new HistoErr;
210 else dobj->Delete();
211
212// Lecture entete
213is.GetStr(strg);
214
215// Lecture des donnees HistoErr
216is.Get(dobj->mBins);
217 dobj->allocate_mNData(dobj->mBins);
218is.Get(dobj->mNData, dobj->mBins);
219
220// Lecture de l'histogramme
221is >> (Histo&)(*dobj);
222
223return;
224}
225
226DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
227void ObjFileIO<HistoErr>::WriteSelf(POutPersist& os) const
228{
229if(dobj == NULL) return;
230string strg;
231
232strg = "HistErr";
233os.PutStr(strg);
234
235// Ecriture des valeurs
236os.Put(dobj->mBins);
237
238// Ecriture des donnees HistoErr nombre d entree par bin
239os.Put(dobj->mNData, dobj->mBins);
240
241// Ecriture de l'histogramme
242os << (Histo&)(*dobj);
243
244return;
245}
246
247#ifdef __CXX_PRAGMA_TEMPLATES__
248#pragma define_template ObjFileIO<HistoErr>
249#endif
250
251#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
252template class SOPHYA::ObjFileIO<HistoErr>;
253#endif
Note: See TracBrowser for help on using the repository browser.