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

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

gestion des param par default, cmv+rz 26/10/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);
[2626]39 if(mBins>0) memcpy(mNData,H.mNData,mBins*sizeof(r_8));
[2603]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;}
[2626]55 mNData = new r_8[nbin];
[2608]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();
[2626]73 if(mNData) memset(mNData,0,mBins*sizeof(r_8));
[2603]74}
75
76/********* Methode *********/
77/*!
[2604]78 Remplissage du contenu de l'histo pour le bin numBin poids w et l'erreur e
79*/
[2626]80void HistoErr::SetBin(int_4 numBin, r_8 w, r_8 e, r_8 nb)
[2604]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*/
[2626]92void HistoErr::SetNentB(int_4 numBin, r_8 nb)
[2604]93{
94 if(numBin>=0 && numBin<mBins) mNData[numBin] = nb;
95return;
96}
97
98/*!
[2603]99 Remplissage d'un tableau avec les nombres d'entrees dans le bin de l'histo
100*/
[2626]101void HistoErr::GetNBin(TVector<r_8>& v) const
[2603]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*/
[2626]112void HistoErr::PutNBin(TVector<r_8> &v)
[2603]113{
[2604]114int_4 n = (v.NElts()<mBins) ? v.NElts(): mBins;
115if(n>0) for(int_4 i=0;i<n;i++) mNData[i] = v(i);
[2603]116return;
117}
118
[2608]119/********* Methode *********/
120/*!
121 Recompute XMin and XMax so that
[2627]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
[2608]127*/
[2627]128void HistoErr::ReCenterBin(void)
[2608]129{
130 if(mBins<=1) return;
131 double dx = (mMax-mMin)/(mBins-1);
[2627]132 mMin -= dx/2.;
133 mMax += dx/2.;
134 binWidth = (mMax-mMin)/mBins;
[2608]135}
[2604]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.
[2622]142 The number of entries by bin is NOT set to 1 (calling ToCorrel
143 many time will change the histogram !)
[2604]144*/
145void HistoErr::ToCorrel(void)
146{
147 if(mBins<1) return;
148 for(int_4 i=0;i<mBins;i++) {
[2627]149 if(mNData[i]<1.) continue;
[2626]150 mData[i] /= mNData[i];
151 mErr2[i] /= mNData[i];
[2604]152 }
153 return;
154}
155
[2619]156/********* Methode *********/
157/*!
[2628]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
180}
181
182/********* Methode *********/
183/*!
[2619]184 Operateur egal HistoErr = HistoErr
185*/
186HistoErr& HistoErr::operator = (const HistoErr& h)
187{
188 if(this==&h) return *this;
189 Delete();
190 if(h.mBins<=0) return *this;
191
192 // Copy the "Histo" part
193 (Histo)(*this) = Histo::operator=(h);
194 // Copy the "entries by bin" table
195 allocate_mNData(h.mBins);
[2626]196 memcpy(mNData,h.mNData,mBins*sizeof(r_8));
[2619]197
198 return *this;
199}
200
[2603]201///////////////////////////////////////////////////////////
202// --------------------------------------------------------
203// Les objets delegues pour la gestion de persistance
204// --------------------------------------------------------
205///////////////////////////////////////////////////////////
206
207DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
208void ObjFileIO<HistoErr>::ReadSelf(PInPersist& is)
209{
[2608]210string strg;
[2603]211
212if(dobj==NULL) dobj=new HistoErr;
213 else dobj->Delete();
214
215// Lecture entete
[2608]216is.GetStr(strg);
[2603]217
218// Lecture des donnees HistoErr
219is.Get(dobj->mBins);
[2608]220 dobj->allocate_mNData(dobj->mBins);
[2603]221is.Get(dobj->mNData, dobj->mBins);
222
223// Lecture de l'histogramme
224is >> (Histo&)(*dobj);
225
226return;
227}
228
229DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
230void ObjFileIO<HistoErr>::WriteSelf(POutPersist& os) const
231{
232if(dobj == NULL) return;
[2608]233string strg;
[2603]234
[2608]235strg = "HistErr";
236os.PutStr(strg);
[2603]237
238// Ecriture des valeurs
239os.Put(dobj->mBins);
240
241// Ecriture des donnees HistoErr nombre d entree par bin
242os.Put(dobj->mNData, dobj->mBins);
243
244// Ecriture de l'histogramme
245os << (Histo&)(*dobj);
246
247return;
248}
249
250#ifdef __CXX_PRAGMA_TEMPLATES__
251#pragma define_template ObjFileIO<HistoErr>
252#endif
253
254#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
255template class ObjFileIO<HistoErr>;
256#endif
Note: See TracBrowser for help on using the repository browser.