1 | //
|
---|
2 | // $Id: histos.cc,v 1.10 2000-07-26 13:15:15 ansari Exp $
|
---|
3 | //
|
---|
4 |
|
---|
5 | #include "machdefs.h"
|
---|
6 | #include <string.h>
|
---|
7 | #include <stdio.h>
|
---|
8 | #include <math.h>
|
---|
9 | #include "histos.h"
|
---|
10 | #include "perrors.h"
|
---|
11 | #include "poly.h"
|
---|
12 | #include "strutil.h"
|
---|
13 | #include "generalfit.h"
|
---|
14 |
|
---|
15 | /*!
|
---|
16 | \class SOPHYA::Histo
|
---|
17 | \ingroup HiStats
|
---|
18 | Classe d'histogrammes 1D
|
---|
19 | */
|
---|
20 |
|
---|
21 | /********* Methode *********/
|
---|
22 | /*! Constructeur par defaut */
|
---|
23 | Histo::Histo()
|
---|
24 | : mData(NULL), mErr2(NULL),
|
---|
25 | mUnder(0), mOver(0), nHist(0), nEntries(0),
|
---|
26 | mBins(0), mMin(0), mMax(0),
|
---|
27 | binWidth(0)
|
---|
28 | {
|
---|
29 | END_CONSTRUCTOR
|
---|
30 | }
|
---|
31 |
|
---|
32 | /********* Methode *********/
|
---|
33 | /*! Constructeur d'un histo de nBin bins allant de xMin a xMax */
|
---|
34 | Histo::Histo(r_8 xMin, r_8 xMax, int_4 nBin)
|
---|
35 | : mData((nBin>0) ? new r_8[nBin] : NULL),
|
---|
36 | mErr2(NULL),
|
---|
37 | mUnder(0), mOver(0), nHist(0), nEntries(0),
|
---|
38 | mBins(nBin), mMin(xMin), mMax(xMax),
|
---|
39 | binWidth((nBin>0) ? (mMax-mMin)/nBin : 0)
|
---|
40 | {
|
---|
41 | Zero();
|
---|
42 | END_CONSTRUCTOR
|
---|
43 | }
|
---|
44 |
|
---|
45 | /********* Methode *********/
|
---|
46 | /*! Constructeur d'un histo de nBin bins allant de xMin a xMax */
|
---|
47 | Histo::Histo(r_4 xMin, r_4 xMax, int_4 nBin)
|
---|
48 | : mData((nBin>0) ? new r_8[nBin] : NULL),
|
---|
49 | mErr2(NULL),
|
---|
50 | mUnder(0), mOver(0), nHist(0), nEntries(0),
|
---|
51 | mBins(nBin), mMin((r_8)xMin), mMax((r_8)xMax),
|
---|
52 | binWidth((nBin>0) ? (mMax-mMin)/nBin : 0)
|
---|
53 | {
|
---|
54 | Zero();
|
---|
55 | END_CONSTRUCTOR
|
---|
56 | }
|
---|
57 |
|
---|
58 | /********* Methode *********/
|
---|
59 | /*! Constructeur par copie */
|
---|
60 | Histo::Histo(const Histo& H)
|
---|
61 | : mData((H.mBins>0)? new r_8[H.mBins] : NULL),
|
---|
62 | mErr2(NULL),
|
---|
63 | mUnder(H.mUnder), mOver(H.mOver), nHist(H.nHist), nEntries(H.nEntries),
|
---|
64 | mBins(H.mBins), mMin(H.mMin), mMax(H.mMax),
|
---|
65 | binWidth(H.binWidth)
|
---|
66 | {
|
---|
67 | if(mBins>0) {
|
---|
68 | memcpy(mData, H.mData, mBins*sizeof(r_8));
|
---|
69 | if( H.mErr2 != NULL ) {
|
---|
70 | mErr2 = new r_8[mBins];
|
---|
71 | memcpy(mErr2, H.mErr2, mBins*sizeof(r_8));
|
---|
72 | }
|
---|
73 | }
|
---|
74 | END_CONSTRUCTOR
|
---|
75 | }
|
---|
76 |
|
---|
77 | /********* Methode *********/
|
---|
78 | /*! Gestion de la des-allocation */
|
---|
79 | void Histo::Delete()
|
---|
80 | {
|
---|
81 | if( mData != NULL ) { delete[] mData; mData = NULL;}
|
---|
82 | if( mErr2 != NULL ) { delete[] mErr2; mErr2 = NULL;}
|
---|
83 | mUnder = mOver = mMin = mMax = binWidth= 0.;
|
---|
84 | nHist = 0.;
|
---|
85 | nEntries = mBins = 0;
|
---|
86 | }
|
---|
87 |
|
---|
88 | /********* Methode *********/
|
---|
89 | /*! Destructeur */
|
---|
90 | Histo::~Histo()
|
---|
91 | {
|
---|
92 | Delete();
|
---|
93 | }
|
---|
94 |
|
---|
95 | /********* Methode *********/
|
---|
96 | /*!
|
---|
97 | Remise a zero
|
---|
98 | */
|
---|
99 | void Histo::Zero()
|
---|
100 | {
|
---|
101 | if(mBins<=0) return;
|
---|
102 | memset(mData, 0, mBins*sizeof(r_8));
|
---|
103 | mUnder = mOver = 0;
|
---|
104 | nHist = 0;
|
---|
105 | nEntries = 0;
|
---|
106 | if( mErr2 != NULL ) memset(mErr2, 0, mBins*sizeof(r_8));
|
---|
107 | }
|
---|
108 |
|
---|
109 | /********* Methode *********/
|
---|
110 | /*!
|
---|
111 | Pour avoir le calcul des erreurs
|
---|
112 | */
|
---|
113 | void Histo::Errors()
|
---|
114 | {
|
---|
115 | if(mBins<=0) return;
|
---|
116 | if(mErr2==NULL) mErr2 = new r_8[mBins];
|
---|
117 | memset(mErr2, 0, mBins*sizeof(r_8));
|
---|
118 | }
|
---|
119 |
|
---|
120 | /********* Methode *********/
|
---|
121 | /*!
|
---|
122 | Operateur egal Histo = Histo
|
---|
123 | */
|
---|
124 | Histo& Histo::operator = (const Histo& h)
|
---|
125 | {
|
---|
126 | if(this == &h) return *this;
|
---|
127 | if( h.mBins <= 0 ) {Delete(); return *this;}
|
---|
128 | if( h.mBins > mBins ) Delete();
|
---|
129 | if(!h.mErr2 && mErr2 ) { delete [] mErr2; mErr2=NULL;}
|
---|
130 | if(!mData) mData = new r_8[h.mBins];
|
---|
131 | if(h.mErr2 && !mErr2 ) mErr2 = new r_8[h.mBins];
|
---|
132 |
|
---|
133 | mUnder = h.mUnder;
|
---|
134 | mOver = h.mOver;
|
---|
135 | nHist = h.nHist;
|
---|
136 | nEntries = h.nEntries;
|
---|
137 | mBins = h.mBins;
|
---|
138 | mMin = h.mMin;
|
---|
139 | mMax = h.mMax;
|
---|
140 | binWidth = h.binWidth;
|
---|
141 |
|
---|
142 | memcpy(mData, h.mData, mBins*sizeof(r_8));
|
---|
143 | if(mErr2) memcpy(mErr2, h.mErr2, mBins*sizeof(r_8));
|
---|
144 |
|
---|
145 | return *this;
|
---|
146 | }
|
---|
147 |
|
---|
148 | /********* Methode *********/
|
---|
149 | /*!
|
---|
150 | Operateur de multiplication par une constante
|
---|
151 | */
|
---|
152 | Histo& Histo::operator *= (r_8 b)
|
---|
153 | {
|
---|
154 | r_8 b2 = b*b;
|
---|
155 | for(int_4 i=0;i<mBins;i++) {
|
---|
156 | mData[i] *= b;
|
---|
157 | if(mErr2) mErr2[i] *= b2;
|
---|
158 | }
|
---|
159 | mUnder *= b;
|
---|
160 | mOver *= b;
|
---|
161 | nHist *= b;
|
---|
162 |
|
---|
163 | return *this;
|
---|
164 | }
|
---|
165 |
|
---|
166 | /*!
|
---|
167 | Operateur de division par une constante
|
---|
168 | */
|
---|
169 | Histo& Histo::operator /= (r_8 b)
|
---|
170 | {
|
---|
171 | if (b==0.) THROW(inconsistentErr);
|
---|
172 | r_8 b2 = b*b;
|
---|
173 | for(int_4 i=0;i<mBins;i++) {
|
---|
174 | mData[i] /= b;
|
---|
175 | if(mErr2) mErr2[i] /= b2;
|
---|
176 | }
|
---|
177 | mUnder /= b;
|
---|
178 | mOver /= b;
|
---|
179 | nHist /= b;
|
---|
180 |
|
---|
181 | return *this;
|
---|
182 | }
|
---|
183 |
|
---|
184 | /*!
|
---|
185 | Operateur d'addition d'une constante
|
---|
186 | */
|
---|
187 | Histo& Histo::operator += (r_8 b)
|
---|
188 | {
|
---|
189 | for(int_4 i=0;i<mBins;i++) mData[i] += b;
|
---|
190 | mUnder += b;
|
---|
191 | mOver += b;
|
---|
192 | nHist += mBins*b;
|
---|
193 |
|
---|
194 | return *this;
|
---|
195 | }
|
---|
196 |
|
---|
197 | /*!
|
---|
198 | Operateur de soustraction d'une constante
|
---|
199 | */
|
---|
200 | Histo& Histo::operator -= (r_8 b)
|
---|
201 | {
|
---|
202 | for(int_4 i=0;i<mBins;i++) mData[i] -= b;
|
---|
203 | mUnder -= b;
|
---|
204 | mOver -= b;
|
---|
205 | nHist -= mBins*b;
|
---|
206 |
|
---|
207 | return *this;
|
---|
208 | }
|
---|
209 |
|
---|
210 | /********* Methode *********/
|
---|
211 | /*!
|
---|
212 | Operateur H += H1
|
---|
213 | */
|
---|
214 | Histo& Histo::operator += (const Histo& a)
|
---|
215 | {
|
---|
216 | if(mBins!=a.mBins) THROW(sizeMismatchErr);
|
---|
217 | for(int_4 i=0;i<mBins;i++) {
|
---|
218 | mData[i] += a(i);
|
---|
219 | if(mErr2 && a.mErr2) mErr2[i] += a.Error2(i);
|
---|
220 | }
|
---|
221 | mUnder += a.mUnder;
|
---|
222 | mOver += a.mOver;
|
---|
223 | nHist += a.nHist;
|
---|
224 | nEntries += a.nEntries;
|
---|
225 |
|
---|
226 | return *this;
|
---|
227 | }
|
---|
228 |
|
---|
229 | /*!
|
---|
230 | Operateur H -= H1
|
---|
231 | */
|
---|
232 | Histo& Histo::operator -= (const Histo& a)
|
---|
233 | {
|
---|
234 | if(mBins!=a.mBins) THROW(sizeMismatchErr);
|
---|
235 | for(int_4 i=0;i<mBins;i++) {
|
---|
236 | mData[i] -= a(i);
|
---|
237 | if(mErr2 && a.mErr2) mErr2[i] += a.Error2(i);
|
---|
238 | }
|
---|
239 | mUnder -= a.mUnder;
|
---|
240 | mOver -= a.mOver;
|
---|
241 | nHist -= a.nHist;
|
---|
242 | nEntries += a.nEntries;
|
---|
243 |
|
---|
244 | return *this;
|
---|
245 | }
|
---|
246 |
|
---|
247 | /*!
|
---|
248 | Operateur H *= H1
|
---|
249 | */
|
---|
250 | Histo& Histo::operator *= (const Histo& a)
|
---|
251 | {
|
---|
252 | if(mBins!=a.mBins) THROW(sizeMismatchErr);
|
---|
253 | nHist = 0.;
|
---|
254 | for(int_4 i=0;i<mBins;i++) {
|
---|
255 | if(mErr2 && a.mErr2)
|
---|
256 | mErr2[i] = a(i)*a(i)*mErr2[i] + mData[i]*mData[i]*a.Error2(i);
|
---|
257 | mData[i] *= a(i);
|
---|
258 | nHist += mData[i];
|
---|
259 | }
|
---|
260 | mUnder *= a.mUnder;
|
---|
261 | mOver *= a.mOver;
|
---|
262 | nEntries += a.nEntries;
|
---|
263 |
|
---|
264 | return *this;
|
---|
265 | }
|
---|
266 |
|
---|
267 | /*!
|
---|
268 | Operateur H /= H1
|
---|
269 | */
|
---|
270 | Histo& Histo::operator /= (const Histo& a)
|
---|
271 | {
|
---|
272 | if(mBins!=a.mBins) THROW(sizeMismatchErr);
|
---|
273 | nHist = 0.;
|
---|
274 | for(int_4 i=0;i<mBins;i++) {
|
---|
275 | if(a(i)==0.) {
|
---|
276 | mData[i]=0.;
|
---|
277 | if(mErr2) mErr2[i]=0.;
|
---|
278 | continue;
|
---|
279 | }
|
---|
280 | if(mErr2 && a.mErr2)
|
---|
281 | mErr2[i] = (mErr2[i] + mData[i]/a(i)*mData[i]/a(i)*a.Error2(i))
|
---|
282 | /(a(i)*a(i));
|
---|
283 | mData[i] /= a(i);
|
---|
284 | nHist += mData[i];
|
---|
285 | }
|
---|
286 | if(a.mUnder!=0.) mUnder /= a.mUnder; else mUnder = 0.;
|
---|
287 | if(a.mOver!=0.) mOver /= a.mOver; else mOver = 0.;
|
---|
288 | nEntries += a.nEntries;
|
---|
289 |
|
---|
290 | return *this;
|
---|
291 | }
|
---|
292 |
|
---|
293 | /********* Methode *********/
|
---|
294 | /*!
|
---|
295 | Remplissage d'un tableau avec la valeur des abscisses
|
---|
296 | */
|
---|
297 | void Histo::GetAbsc(TVector<r_8> &v)
|
---|
298 | {
|
---|
299 | v.Realloc(mBins);
|
---|
300 | for(int_4 i=0;i<mBins;i++) v(i) = BinLowEdge(i);
|
---|
301 | return;
|
---|
302 | }
|
---|
303 |
|
---|
304 | /*!
|
---|
305 | Remplissage d'un tableau avec la valeur du contenu
|
---|
306 | */
|
---|
307 | void Histo::GetValue(TVector<r_8> &v)
|
---|
308 | {
|
---|
309 | v.Realloc(mBins);
|
---|
310 | for(int_4 i=0;i<mBins;i++) v(i) = mData[i];
|
---|
311 | return;
|
---|
312 | }
|
---|
313 |
|
---|
314 | /*!
|
---|
315 | Remplissage d'un tableau avec la valeur des erreurs au carre
|
---|
316 | */
|
---|
317 | void Histo::GetError2(TVector<r_8> &v)
|
---|
318 | {
|
---|
319 | v.Realloc(mBins);
|
---|
320 | if(!mErr2) {for(int_4 i=0;i<mBins;i++) v(i) = 0.; return;}
|
---|
321 | for(int_4 i=0;i<mBins;i++) v(i) = mErr2[i];
|
---|
322 | return;
|
---|
323 | }
|
---|
324 |
|
---|
325 | /*!
|
---|
326 | Remplissage d'un tableau avec la valeur des erreurs
|
---|
327 | */
|
---|
328 | void Histo::GetError(TVector<r_8> &v)
|
---|
329 | {
|
---|
330 | v.Realloc(mBins);
|
---|
331 | if(!mErr2) {for(int_4 i=0;i<mBins;i++) v(i) = 0.; return;}
|
---|
332 | for(int_4 i=0;i<mBins;i++) v(i) = Error(i);
|
---|
333 | return;
|
---|
334 | }
|
---|
335 |
|
---|
336 | /********* Methode *********/
|
---|
337 | /*!
|
---|
338 | Remplissage du contenu de l'histo avec les valeurs d'un vecteur
|
---|
339 | */
|
---|
340 | void Histo::PutValue(TVector<r_8> &v, int_4 ierr)
|
---|
341 | {
|
---|
342 | //if(v.NElts()<(uint_4) mBins) THROW(sizeMismatchErr);
|
---|
343 | uint_4 n = (v.NElts()<(uint_4) mBins) ? v.NElts(): (uint_4) mBins;
|
---|
344 | if(n>0) for(uint_4 i=0;i<n;i++) {
|
---|
345 | mData[i] = v(i);
|
---|
346 | if(mErr2&&ierr) mErr2[i] = fabs(v(i));
|
---|
347 | }
|
---|
348 | return;
|
---|
349 | }
|
---|
350 |
|
---|
351 | /*!
|
---|
352 | Addition du contenu de l'histo avec les valeurs d'un vecteur
|
---|
353 | */
|
---|
354 | void Histo::PutValueAdd(TVector<r_8> &v, int_4 ierr)
|
---|
355 | {
|
---|
356 | //if(v.NElts()<(uint_4) mBins) THROW(sizeMismatchErr);
|
---|
357 | uint_4 n = (v.NElts()<(uint_4) mBins) ? v.NElts(): (uint_4) mBins;
|
---|
358 | if(n>0) for(uint_4 i=0;i<n;i++) {
|
---|
359 | mData[i] += v(i);
|
---|
360 | if(mErr2 && ierr) mErr2[i] += fabs(v(i));
|
---|
361 | }
|
---|
362 | return;
|
---|
363 | }
|
---|
364 |
|
---|
365 | /*!
|
---|
366 | Remplissage des erreurs au carre de l'histo avec les valeurs d'un vecteur
|
---|
367 | */
|
---|
368 | void Histo::PutError2(TVector<r_8> &v)
|
---|
369 | {
|
---|
370 | //if(v.NElts()<(uint_4) mBins) THROW(sizeMismatchErr);
|
---|
371 | uint_4 n = (v.NElts()<(uint_4) mBins) ? v.NElts(): (uint_4) mBins;
|
---|
372 | if(n>0) {
|
---|
373 | if(!mErr2) Errors();
|
---|
374 | for(uint_4 i=0;i<n;i++) mErr2[i] = v(i);
|
---|
375 | }
|
---|
376 | return;
|
---|
377 | }
|
---|
378 |
|
---|
379 | /*!
|
---|
380 | Addition des erreurs au carre de l'histo avec les valeurs d'un vecteur
|
---|
381 | */
|
---|
382 | void Histo::PutError2Add(TVector<r_8> &v)
|
---|
383 | {
|
---|
384 | //if(v.NElts()<(uint_4) mBins) THROW(sizeMismatchErr);
|
---|
385 | uint_4 n = (v.NElts()<(uint_4) mBins) ? v.NElts(): (uint_4) mBins;
|
---|
386 | if(n>0) {
|
---|
387 | if(!mErr2) Errors();
|
---|
388 | for(uint_4 i=0;i<n;i++) if(v(i)>0.) mErr2[i] += v(i);
|
---|
389 | }
|
---|
390 | return;
|
---|
391 | }
|
---|
392 |
|
---|
393 | /*!
|
---|
394 | Remplissage des erreurs de l'histo avec les valeurs d'un vecteur
|
---|
395 | */
|
---|
396 | void Histo::PutError(TVector<r_8> &v)
|
---|
397 | {
|
---|
398 | //if(v.NElts()<(uint_4) mBins) THROW(sizeMismatchErr);
|
---|
399 | uint_4 n = (v.NElts()<(uint_4) mBins) ? v.NElts(): (uint_4) mBins;
|
---|
400 | if(n>0) {
|
---|
401 | if(!mErr2) Errors();
|
---|
402 | for(uint_4 i=0;i<n;i++)
|
---|
403 | if(v(i)>0.) mErr2[i]=v(i)*v(i); else mErr2[i]=-v(i)*v(i);
|
---|
404 | }
|
---|
405 | return;
|
---|
406 | }
|
---|
407 |
|
---|
408 | /********* Methode *********/
|
---|
409 | /*!
|
---|
410 | Addition du contenu de l'histo pour abscisse x poids w
|
---|
411 | */
|
---|
412 | void Histo::Add(r_8 x, r_8 w)
|
---|
413 | {
|
---|
414 | int_4 numBin = FindBin(x);
|
---|
415 | if (numBin<0) mUnder += w;
|
---|
416 | else if (numBin>=mBins) mOver += w;
|
---|
417 | else {
|
---|
418 | mData[numBin] += w;
|
---|
419 | if(mErr2!=NULL) mErr2[numBin] += w*w;
|
---|
420 | nHist += w;
|
---|
421 | nEntries++;
|
---|
422 | }
|
---|
423 | }
|
---|
424 |
|
---|
425 | /********* Methode *********/
|
---|
426 | /*!
|
---|
427 | Addition du contenu de l'histo bin numBin poids w
|
---|
428 | */
|
---|
429 | void Histo::AddBin(int_4 numBin, r_8 w)
|
---|
430 | {
|
---|
431 | if (numBin<0) mUnder += w;
|
---|
432 | else if (numBin>=mBins) mOver += w;
|
---|
433 | else {
|
---|
434 | mData[numBin] += w;
|
---|
435 | if(mErr2!=NULL) mErr2[numBin] += w*w;
|
---|
436 | nHist += w;
|
---|
437 | nEntries++;
|
---|
438 | }
|
---|
439 | }
|
---|
440 |
|
---|
441 | /********* Methode *********/
|
---|
442 | /*!
|
---|
443 | Remplissage du contenu de l'histo pour abscisse x poids w
|
---|
444 | */
|
---|
445 | void Histo::SetBin(r_8 x, r_8 w)
|
---|
446 | {
|
---|
447 | int_4 numBin = FindBin(x);
|
---|
448 | SetBin(numBin,w);
|
---|
449 | }
|
---|
450 |
|
---|
451 | /********* Methode *********/
|
---|
452 | /*!
|
---|
453 | Remplissage du contenu de l'histo pour numBin poids w
|
---|
454 | */
|
---|
455 | void Histo::SetBin(int_4 numBin, r_8 w)
|
---|
456 | {
|
---|
457 | if (numBin<0) mUnder = w;
|
---|
458 | else if (numBin>=mBins) mOver = w;
|
---|
459 | else {
|
---|
460 | nHist -= mData[numBin];
|
---|
461 | mData[numBin] = w;
|
---|
462 | nHist += w;
|
---|
463 | }
|
---|
464 | }
|
---|
465 |
|
---|
466 | /********* Methode *********/
|
---|
467 | /*!
|
---|
468 | Remplissage des erreurs au carre pour abscisse x
|
---|
469 | */
|
---|
470 | void Histo::SetErr2(r_8 x, r_8 e2)
|
---|
471 | {
|
---|
472 | int_4 numBin = FindBin(x);
|
---|
473 | SetErr2(numBin,e2);
|
---|
474 | }
|
---|
475 |
|
---|
476 | /********* Methode *********/
|
---|
477 | /*!
|
---|
478 | Remplissage des erreurs au carre pour numBin poids
|
---|
479 | */
|
---|
480 | void Histo::SetErr2(int_4 numBin, r_8 e2)
|
---|
481 | {
|
---|
482 | if( mErr2==NULL) return;
|
---|
483 | if ( numBin<0 || numBin>=mBins ) return;
|
---|
484 | mErr2[numBin] = e2;
|
---|
485 | }
|
---|
486 |
|
---|
487 | /********* Methode *********/
|
---|
488 | /*!
|
---|
489 | Remplissage des erreurs pour abscisse x
|
---|
490 | */
|
---|
491 | void Histo::SetErr(r_8 x, r_8 e)
|
---|
492 | {
|
---|
493 | SetErr2(x, e*e);
|
---|
494 | }
|
---|
495 |
|
---|
496 | /********* Methode *********/
|
---|
497 | /*!
|
---|
498 | Remplissage des erreurs pour numBin
|
---|
499 | */
|
---|
500 | void Histo::SetErr(int_4 numBin, r_8 e)
|
---|
501 | {
|
---|
502 | SetErr2(numBin, e*e);
|
---|
503 | }
|
---|
504 |
|
---|
505 | /********* Methode *********/
|
---|
506 | /*!
|
---|
507 | Numero du bin ayant le contenu maximum
|
---|
508 | */
|
---|
509 | int_4 Histo::IMax() const
|
---|
510 | {
|
---|
511 | int_4 imx=0;
|
---|
512 | if(mBins==1) return imx;
|
---|
513 | r_8 mx=mData[0];
|
---|
514 | for (int_4 i=1; i<mBins; i++)
|
---|
515 | if (mData[i]>mx) {imx = i; mx=mData[i];}
|
---|
516 | return imx;
|
---|
517 | }
|
---|
518 |
|
---|
519 | /********* Methode *********/
|
---|
520 | /*!
|
---|
521 | Numero du bin ayant le contenu minimum
|
---|
522 | */
|
---|
523 | int_4 Histo::IMin() const
|
---|
524 | {
|
---|
525 | int_4 imx=0;
|
---|
526 | if(mBins==1) return imx;
|
---|
527 | r_8 mx=mData[0];
|
---|
528 | for (int_4 i=1; i<mBins; i++)
|
---|
529 | if (mData[i]<mx) {imx = i; mx=mData[i];}
|
---|
530 | return imx;
|
---|
531 | }
|
---|
532 |
|
---|
533 | /********* Methode *********/
|
---|
534 | /*!
|
---|
535 | Valeur le contenu maximum
|
---|
536 | */
|
---|
537 | r_8 Histo::VMax() const
|
---|
538 | {
|
---|
539 | r_8 mx=mData[0];
|
---|
540 | if(mBins==1) return mx;
|
---|
541 | for (int_4 i=1;i<mBins;i++) if(mData[i]>mx) mx=mData[i];
|
---|
542 | return mx;
|
---|
543 | }
|
---|
544 |
|
---|
545 | /********* Methode *********/
|
---|
546 | /*!
|
---|
547 | Valeur le contenu minimum
|
---|
548 | */
|
---|
549 | r_8 Histo::VMin() const
|
---|
550 | {
|
---|
551 | r_8 mx=mData[0];
|
---|
552 | if(mBins==1) return mx;
|
---|
553 | for (int_4 i=1;i<mBins;i++) if(mData[i]<mx) mx=mData[i];
|
---|
554 | return mx;
|
---|
555 | }
|
---|
556 |
|
---|
557 | /********* Methode *********/
|
---|
558 | /*!
|
---|
559 | Valeur moyenne
|
---|
560 | */
|
---|
561 | r_8 Histo::Mean() const
|
---|
562 | {
|
---|
563 | r_8 n = 0;
|
---|
564 | r_8 sx = 0;
|
---|
565 | for (int_4 i=0; i<mBins; i++) {
|
---|
566 | r_8 v = (mData[i]>=0.) ? mData[i] : -mData[i];
|
---|
567 | n += v;
|
---|
568 | sx += BinCenter(i)*v;
|
---|
569 | }
|
---|
570 | if(n>0.) return sx/n; else return mMin;
|
---|
571 | }
|
---|
572 |
|
---|
573 | /********* Methode *********/
|
---|
574 | /*!
|
---|
575 | Valeur du sigma
|
---|
576 | */
|
---|
577 | r_8 Histo::Sigma() const
|
---|
578 | {
|
---|
579 | r_8 n = 0;
|
---|
580 | r_8 sx = 0;
|
---|
581 | r_8 sx2= 0;
|
---|
582 | for (int_4 i=0; i<mBins; i++) {
|
---|
583 | r_8 v = (mData[i]>=0.) ? mData[i] : -mData[i];
|
---|
584 | n += v;
|
---|
585 | sx += BinCenter(i)*v;
|
---|
586 | sx2+= BinCenter(i)*BinCenter(i)*v;
|
---|
587 | }
|
---|
588 | // attention a l'erreur d'arrondi si un seul bin rempli!!
|
---|
589 | if( n == 0 ) return 0.;
|
---|
590 | sx2 = sx2/n - (sx/n)*(sx/n);
|
---|
591 | if( sx2>0. ) return sqrt( sx2 );
|
---|
592 | else return 0.;
|
---|
593 | }
|
---|
594 |
|
---|
595 | /********* Methode *********/
|
---|
596 | /*!
|
---|
597 | Valeur de la moyenne calculee entre les bin il et ih
|
---|
598 | */
|
---|
599 | r_8 Histo::MeanLH(int_4 il,int_4 ih) const
|
---|
600 | {
|
---|
601 | if( ih < il ) { il = 0; ih = mBins-1; }
|
---|
602 | if( il < 0 ) il = 0;
|
---|
603 | if( ih >= mBins ) ih = mBins-1;
|
---|
604 | r_8 n = 0;
|
---|
605 | r_8 sx = 0;
|
---|
606 | for (int_4 i=il; i<=ih; i++) {
|
---|
607 | r_8 v = (mData[i]>=0.) ? mData[i] : -mData[i];
|
---|
608 | n += v;
|
---|
609 | sx += BinCenter(i)*v;
|
---|
610 | }
|
---|
611 | if(n>0.) return sx/n; else return BinLowEdge(il);
|
---|
612 | }
|
---|
613 |
|
---|
614 | /********* Methode *********/
|
---|
615 | /*!
|
---|
616 | Valeur de la moyenne calculee entre les bin il et ih
|
---|
617 | */
|
---|
618 | r_8 Histo::SigmaLH(int_4 il,int_4 ih) const
|
---|
619 | {
|
---|
620 | if( ih < il ) { il = 0; ih = mBins - 1; }
|
---|
621 | if( il < 0 ) il = 0;
|
---|
622 | if( ih >= mBins ) ih = mBins - 1;
|
---|
623 | r_8 n = 0;
|
---|
624 | r_8 sx = 0;
|
---|
625 | r_8 sx2= 0;
|
---|
626 | for (int_4 i=il; i<=ih; i++) {
|
---|
627 | r_8 v = (mData[i]>=0.) ? mData[i] : -mData[i];
|
---|
628 | n += v;
|
---|
629 | sx += BinCenter(i)*v;
|
---|
630 | sx2+= BinCenter(i)*BinCenter(i)*v;
|
---|
631 | }
|
---|
632 | if( n == 0 ) return 0.;
|
---|
633 | sx2 = sx2/n - (sx/n)*(sx/n);
|
---|
634 | if( sx2>0. ) return sqrt( sx2 );
|
---|
635 | else return 0.;
|
---|
636 | }
|
---|
637 |
|
---|
638 | /********* Methode *********/
|
---|
639 | /*!
|
---|
640 | Valeur de la moyenne calculee entre x0-dx et x0+dx
|
---|
641 | */
|
---|
642 | r_8 Histo::Mean(r_8 x0, r_8 dx) const
|
---|
643 | {
|
---|
644 | r_8 sdata = 0;
|
---|
645 | r_8 sx = 0;
|
---|
646 | int_4 iMin = FindBin(x0-dx);
|
---|
647 | if (iMin<0) iMin = 0;
|
---|
648 | int_4 iMax = FindBin(x0+dx);
|
---|
649 | if (iMax>mBins) iMax=mBins;
|
---|
650 | for (int_4 i=iMin; i<iMax; i++) {
|
---|
651 | r_8 v = (mData[i]>=0.) ? mData[i] : -mData[i];
|
---|
652 | sx += BinCenter(i)*v;
|
---|
653 | sdata += v;
|
---|
654 | }
|
---|
655 | if(sdata>0.) return sx/sdata; else return mMin;
|
---|
656 | }
|
---|
657 |
|
---|
658 | /********* Methode *********/
|
---|
659 | /*!
|
---|
660 | Valeur du sigma calcule entre x0-dx et x0+dx
|
---|
661 | */
|
---|
662 | r_8 Histo::Sigma(r_8 x0, r_8 dx) const
|
---|
663 | {
|
---|
664 | r_8 sx = 0;
|
---|
665 | r_8 sx2= 0;
|
---|
666 | r_8 sdata = 0;
|
---|
667 | int_4 iMin = FindBin(x0-dx);
|
---|
668 | if (iMin<0) iMin = 0;
|
---|
669 | int_4 iMax = FindBin(x0+dx);
|
---|
670 | if (iMax>mBins) iMax=mBins;
|
---|
671 | for (int_4 i=iMin; i<iMax; i++) {
|
---|
672 | r_8 v = (mData[i]>=0.) ? mData[i] : -mData[i];
|
---|
673 | sx += BinCenter(i)*v;
|
---|
674 | sx2+= BinCenter(i)*BinCenter(i)*v;
|
---|
675 | sdata += v;
|
---|
676 | }
|
---|
677 | if(sdata>0.) return sqrt( sx2/sdata - (sx/sdata)*(sx/sdata) );
|
---|
678 | else return 0.;
|
---|
679 | }
|
---|
680 |
|
---|
681 | /********* Methode *********/
|
---|
682 | /*!
|
---|
683 | Valeur de la moyenne et du sigma nettoyes
|
---|
684 | */
|
---|
685 | r_8 Histo::CleanedMean(r_8& sigma) const
|
---|
686 | {
|
---|
687 | if (!nHist) return 0;
|
---|
688 | // on fait ca de facon bete, on pourra raffiner apres...
|
---|
689 | r_8 x0 = Mean();
|
---|
690 | r_8 s = Sigma()+binWidth;
|
---|
691 |
|
---|
692 | for (int_4 i=0; i<3; i++) {
|
---|
693 | r_8 xx0 = Mean(x0,5*s);
|
---|
694 | s = Sigma(x0,5*s)+binWidth;
|
---|
695 | x0 = xx0;
|
---|
696 | }
|
---|
697 | sigma = s;
|
---|
698 | return x0;
|
---|
699 | }
|
---|
700 |
|
---|
701 | /********* Methode *********/
|
---|
702 | /*!
|
---|
703 | Valeur de la moyenne nettoyee
|
---|
704 | */
|
---|
705 | r_8 Histo::CleanedMean() const
|
---|
706 | {
|
---|
707 | if (!nHist) return 0;
|
---|
708 | r_8 s=0;
|
---|
709 | return CleanedMean(s);
|
---|
710 | }
|
---|
711 |
|
---|
712 | /********* Methode *********/
|
---|
713 | /*!
|
---|
714 | Retourne le nombre de bins non-nul
|
---|
715 | */
|
---|
716 | int_4 Histo::BinNonNul() const
|
---|
717 | {
|
---|
718 | int_4 non=0;
|
---|
719 | for (int_4 i=0;i<mBins;i++) if( mData[i] != 0. ) non++;
|
---|
720 | return non;
|
---|
721 | }
|
---|
722 |
|
---|
723 | /********* Methode *********/
|
---|
724 | /*!
|
---|
725 | Retourne le nombre de bins ayant une erreur non-nulle
|
---|
726 | */
|
---|
727 | int_4 Histo::ErrNonNul() const
|
---|
728 | {
|
---|
729 | if(mErr2==NULL) return -1;
|
---|
730 | int_4 non=0;
|
---|
731 | for (int_4 i=0;i<mBins;i++) if( mErr2[i] != 0. ) non++;
|
---|
732 | return non;
|
---|
733 | }
|
---|
734 |
|
---|
735 | /********* Methode *********/
|
---|
736 | /*!
|
---|
737 | Renvoie le numero de bin tel que il y ait "per" pourcent entrees
|
---|
738 | entre le bin 0 et ce bin (y compris le contenu de ce bin).
|
---|
739 | */
|
---|
740 | int_4 Histo::BinPercent(r_8 per) const
|
---|
741 | {
|
---|
742 | r_8 n = nHist*per;
|
---|
743 | r_8 s = 0.;
|
---|
744 | int_4 i;
|
---|
745 | for(i=0; i<mBins; i++ ) {
|
---|
746 | s += mData[i];
|
---|
747 | if( s >= n ) break;
|
---|
748 | }
|
---|
749 | if( i == mBins ) i = mBins-1;
|
---|
750 | return i;
|
---|
751 | }
|
---|
752 |
|
---|
753 | /********* Methode *********/
|
---|
754 | /*!
|
---|
755 | Renvoie les numeros de bins imin,imax (0=<i<bins) tels que:
|
---|
756 | \verbatim
|
---|
757 | notons I = bin contenant l'abscisse x
|
---|
758 | N1 = nombre d'entrees entre bin 0 et I compris
|
---|
759 | N2 = nombre d'entrees entre bin I et bins-1 compris
|
---|
760 | imin = bin tel que nombre d'entrees entre imin et I = N1 * per
|
---|
761 | imax = bin tel que nombre d'entrees entre I et imax = N2 * per
|
---|
762 | Return: <0 si echec
|
---|
763 | mMin(I-imin,imax-I) si ok
|
---|
764 | \endverbatim
|
---|
765 | */
|
---|
766 | int_4 Histo::BinPercent(r_8 x,r_8 per,int& imin,int& imax)
|
---|
767 | {
|
---|
768 | imin = imax = -1;
|
---|
769 | if( per <= 0. ) return -1;
|
---|
770 |
|
---|
771 | int_4 I = FindBin(x);
|
---|
772 | if( I<0 || I>=mBins ) return -2;
|
---|
773 |
|
---|
774 | r_8 N1 = 0.; for(int_4 i=0; i<=I; i++) N1 += mData[i]; N1 *= per;
|
---|
775 | r_8 N2 = 0.; {for(int_4 i=I; i<mBins; i++) N2 += mData[i]; N2 *= per;}
|
---|
776 |
|
---|
777 | r_8 n = 0.;
|
---|
778 | for(imin=I; imin>=0; imin--) { n += mData[imin]; if(n>=N1) break; }
|
---|
779 | if( imin<0 ) imin = 0;
|
---|
780 | // cout<<"I="<<I<<" imin="<<imin<<" n="<<n<<" N1="<<N1<<endl;
|
---|
781 |
|
---|
782 | n = 0.;
|
---|
783 | for(imax=I; imax<mBins; imax++) { n += mData[imax]; if(n>=N2) break; }
|
---|
784 | if( imax>=mBins ) imax = mBins-1;
|
---|
785 | // cout<<"I="<<I<<" imax="<<imax<<" n="<<n<<" N2="<<N2<<endl;
|
---|
786 |
|
---|
787 | return ( imax-I > I-imin ) ? I-imin : imax-I ;
|
---|
788 | }
|
---|
789 |
|
---|
790 | /********* Methode *********/
|
---|
791 | /*!
|
---|
792 | Idem precedent mais renvoie xmin et xmax
|
---|
793 | */
|
---|
794 | int_4 Histo::BinPercent(r_8 x,r_8 per,r_8& xmin,r_8& xmax)
|
---|
795 | {
|
---|
796 | xmin = xmax = 0.;
|
---|
797 | int_4 imin,imax;
|
---|
798 | int_4 rc = BinPercent(x,per,imin,imax);
|
---|
799 | if( rc >= 0 ) { xmin = BinLowEdge(imin); xmax = BinHighEdge(imax); }
|
---|
800 | return rc;
|
---|
801 | }
|
---|
802 |
|
---|
803 | /********* Methode *********/
|
---|
804 | /*!
|
---|
805 | Remplace l'histogramme par son integrale normalise a norm:
|
---|
806 | si norm <= 0 : pas de normalisation, integration seule
|
---|
807 | */
|
---|
808 | void Histo::HInteg(r_8 norm)
|
---|
809 | {
|
---|
810 | if( mBins <= 0 ) return; // createur par default
|
---|
811 | if(mBins>1)
|
---|
812 | for(int_4 i=1; i<mBins; i++) {
|
---|
813 | mData[i] += mData[i-1];
|
---|
814 | if(mErr2!=NULL) mErr2[i] += mErr2[i-1];
|
---|
815 | }
|
---|
816 | if( norm <= 0. ) return;
|
---|
817 | norm /= mData[mBins-1];
|
---|
818 | for(int_4 i=0; i<mBins; i++) {
|
---|
819 | mData[i] *= norm;
|
---|
820 | if(mErr2!=NULL) mErr2[i] *= norm*norm;
|
---|
821 | }
|
---|
822 | }
|
---|
823 |
|
---|
824 | /********* Methode *********/
|
---|
825 | /*!
|
---|
826 | Remplace l'histogramme par sa derivee
|
---|
827 | */
|
---|
828 | void Histo::HDeriv()
|
---|
829 | {
|
---|
830 | if( mBins <= 0 ) return; // createur par default
|
---|
831 | if( mBins <= 1 ) return;
|
---|
832 | r_8 *temp = new r_8[mBins];
|
---|
833 | memcpy(temp, mData, mBins*sizeof(r_8));
|
---|
834 | if(mBins>=3) for(int_4 i=1; i<mBins-1; i++)
|
---|
835 | temp[i] = (mData[i+1]-mData[i-1])/(2.*binWidth);
|
---|
836 | temp[0] = (mData[1]-mData[0])/binWidth;
|
---|
837 | temp[mBins-1] = (mData[mBins-1]-mData[mBins-2])/binWidth;
|
---|
838 | memcpy(mData, temp, mBins*sizeof(r_8));
|
---|
839 | delete [] temp;
|
---|
840 | }
|
---|
841 |
|
---|
842 | /********* Methode *********/
|
---|
843 | /*!
|
---|
844 | Pour rebinner l'histogramme sur nbinew bins
|
---|
845 | */
|
---|
846 | void Histo::HRebin(int_4 nbinew)
|
---|
847 | {
|
---|
848 | if( mBins <= 0 ) return; // createur par default
|
---|
849 | if( nbinew <= 0 ) return;
|
---|
850 |
|
---|
851 | // memorisation de l'histogramme original
|
---|
852 | Histo H(*this);
|
---|
853 |
|
---|
854 | // Le nombre de bins est il plus grand ??
|
---|
855 | if( nbinew > mBins ) {
|
---|
856 | delete [] mData; mData = NULL;
|
---|
857 | mData = new r_8[nbinew];
|
---|
858 | if( mErr2 != NULL ) {
|
---|
859 | delete [] mErr2; mErr2 = NULL;
|
---|
860 | mErr2 = new r_8[nbinew];
|
---|
861 | }
|
---|
862 | }
|
---|
863 |
|
---|
864 | // remise en forme de this
|
---|
865 | mBins = nbinew;
|
---|
866 | this->Zero();
|
---|
867 | binWidth = (mMax-mMin)/mBins;
|
---|
868 | // On recopie les parametres qui n'ont pas changes
|
---|
869 | mUnder = H.mUnder;
|
---|
870 | mOver = H.mOver;
|
---|
871 |
|
---|
872 |
|
---|
873 | // Remplissage
|
---|
874 | for(int_4 i=0;i<mBins;i++) {
|
---|
875 | r_8 xmi = BinLowEdge(i);
|
---|
876 | r_8 xma = BinHighEdge(i);
|
---|
877 | int_4 imi = H.FindBin(xmi);
|
---|
878 | if( imi < 0 ) imi = 0;
|
---|
879 | int_4 ima = H.FindBin(xma);
|
---|
880 | if( ima >= H.mBins ) ima = H.mBins-1;
|
---|
881 | r_8 w = 0.;
|
---|
882 | if( ima == imi ) {
|
---|
883 | w = H(imi) * binWidth/H.binWidth;
|
---|
884 | } else {
|
---|
885 | w += H(imi) * (H.BinHighEdge(imi)-xmi)/H.binWidth;
|
---|
886 | w += H(ima) * (xma -H.BinLowEdge(ima))/H.binWidth;
|
---|
887 | if( ima > imi+1 )
|
---|
888 | for(int_4 ii=imi+1;ii<ima;ii++) w += H(ii);
|
---|
889 | }
|
---|
890 | AddBin(i, w);
|
---|
891 | }
|
---|
892 |
|
---|
893 | }
|
---|
894 |
|
---|
895 | /********* Methode *********/
|
---|
896 | /*!
|
---|
897 | Retourne le nombre de maxima locaux, la valeur du maximum (maxi)
|
---|
898 | et sa position (imax), ainsi que la valeur du second maximum
|
---|
899 | local (maxn) et sa position (imaxn).
|
---|
900 | Attention: si un maximum a la meme valeur sur plusieurs bins
|
---|
901 | consecutifs, le bin le plus a droite est pris.
|
---|
902 | */
|
---|
903 | int_4 Histo::MaxiLocal(r_8& maxi,int& imax,r_8& maxn,int& imaxn)
|
---|
904 | {
|
---|
905 | int_4 nml = 0;
|
---|
906 | imax = imaxn = -1;
|
---|
907 | maxi = maxn = -1.;
|
---|
908 |
|
---|
909 | bool up = true;
|
---|
910 | bool down = false;
|
---|
911 | for(int_4 i=0;i<mBins;i++) {
|
---|
912 | if( !up ) if( mData[i] > mData[i-1] ) up = true;
|
---|
913 | if( up && !down ) {
|
---|
914 | if( i == mBins-1 ) down=true;
|
---|
915 | else if( mData[i] > mData[i+1] ) down=true;
|
---|
916 | }
|
---|
917 |
|
---|
918 | if( up && down ) {
|
---|
919 | nml++;
|
---|
920 | up = down = false;
|
---|
921 | if( imax < 0 ) {
|
---|
922 | imax = i; maxi = mData[i];
|
---|
923 | } else if( mData[i] >= maxi ) {
|
---|
924 | imaxn = imax; maxn = maxi;
|
---|
925 | imax = i; maxi = mData[i];
|
---|
926 | } else {
|
---|
927 | if( imaxn < 0 || mData[i] >= maxn ) { imaxn = i; maxn = mData[i]; }
|
---|
928 | }
|
---|
929 | }
|
---|
930 |
|
---|
931 | }
|
---|
932 | return nml;
|
---|
933 | }
|
---|
934 |
|
---|
935 | /********* Methode *********/
|
---|
936 | /*!
|
---|
937 | Fit de la position du maximum de l'histo par un polynome
|
---|
938 | de degre `degree' a `frac' pourcent du maximum.
|
---|
939 | L'histo est suppose etre remplit de valeurs positives
|
---|
940 | */
|
---|
941 | r_8 Histo::FitMax(int_4 degree, r_8 frac, int_4 debug) const
|
---|
942 | {
|
---|
943 | if (degree < 2 || degree > 3) degree = 3;
|
---|
944 | if (frac <= 0. || frac >= 1.) frac = 0.5;
|
---|
945 |
|
---|
946 | if (debug > 1)
|
---|
947 | cout<<"Histo::FitMax : Nb Entrees histo ="<<NEntries()<<endl;
|
---|
948 |
|
---|
949 | if (NEntries() < 1) THROW(inconsistentErr);
|
---|
950 |
|
---|
951 | int_4 iMax = IMax();
|
---|
952 | r_8 hmax = (*this)(iMax);
|
---|
953 | r_8 xCenter = BinCenter(iMax);
|
---|
954 |
|
---|
955 | if(debug>1)
|
---|
956 | cout<<"Max histo i="<<iMax<<" x="<<xCenter<<" v="<<hmax<<endl;
|
---|
957 |
|
---|
958 | /* find limits at frac*hmax */
|
---|
959 |
|
---|
960 | r_8 limit = frac*hmax;
|
---|
961 |
|
---|
962 | volatile int_4 iLow = iMax;
|
---|
963 | while (iLow>0 && (*this)(iLow)>limit) iLow--;
|
---|
964 |
|
---|
965 | volatile int_4 iHigh = iMax;
|
---|
966 | while (iHigh<NBins()-1 && (*this)(iHigh)>limit) iHigh++;
|
---|
967 |
|
---|
968 | int_4 nLowHigh;
|
---|
969 | for(;;) {
|
---|
970 | nLowHigh = 0;
|
---|
971 | for (int_4 i=iLow; i<=iHigh; i++) if((*this)(i)>0) {
|
---|
972 | if(!mErr2) nLowHigh++;
|
---|
973 | else if(Error2(i)>0.) nLowHigh++;
|
---|
974 | }
|
---|
975 | if (debug > 1) cout <<"Limites histo "<<iLow<<" - "<<iHigh
|
---|
976 | <<" ("<<nLowHigh<<" non nuls)"<<endl;
|
---|
977 | if( nLowHigh >= degree+1 ) break;
|
---|
978 | iLow--; iHigh++;
|
---|
979 | if(iLow<0 && iHigh>=NBins()) {
|
---|
980 | if (debug>1)
|
---|
981 | cout<<"Mode histogramme = "<<xCenter
|
---|
982 | <<" BinCenter("<<iMax<<")"<<endl;
|
---|
983 | return xCenter;
|
---|
984 | }
|
---|
985 | if(iLow < 0 ) iLow = 0;
|
---|
986 | if(iHigh >= NBins()) iHigh = NBins()-1;
|
---|
987 | }
|
---|
988 |
|
---|
989 | TVector<r_8> xFit(nLowHigh);
|
---|
990 | TVector<r_8> yFit(nLowHigh);
|
---|
991 | TVector<r_8> e2Fit(nLowHigh);
|
---|
992 | TVector<r_8> errcoef(1);
|
---|
993 | int_4 ii = 0;
|
---|
994 | for (int_4 j=iLow; j<=iHigh; j++) {
|
---|
995 | if ((*this)(j)>0) {
|
---|
996 | if(!mErr2) {
|
---|
997 | xFit(ii) = BinCenter(j)-xCenter;
|
---|
998 | yFit(ii) = (*this)(j);
|
---|
999 | e2Fit(ii) = yFit(ii);
|
---|
1000 | ii++;
|
---|
1001 | } else if(Error2(j)>0.) {
|
---|
1002 | xFit(ii) = BinCenter(j)-xCenter;
|
---|
1003 | yFit(ii) = (*this)(j);
|
---|
1004 | e2Fit(ii) = Error2(j);
|
---|
1005 | ii++;
|
---|
1006 | }
|
---|
1007 | }
|
---|
1008 | }
|
---|
1009 | if(debug>4) {
|
---|
1010 | int_4 k;
|
---|
1011 | for(k=0;k<nLowHigh;k++) cout<<" "<<xFit(k); cout<<endl;
|
---|
1012 | for(k=0;k<nLowHigh;k++) cout<<" "<<yFit(k); cout<<endl;
|
---|
1013 | for(k=0;k<nLowHigh;k++) cout<<" "<<e2Fit(k); cout<<endl;
|
---|
1014 | }
|
---|
1015 | if( ii != nLowHigh ) THROW(inconsistentErr);
|
---|
1016 | Poly pol(degree);
|
---|
1017 | TRY {
|
---|
1018 | pol.Fit(xFit, yFit, e2Fit, degree, errcoef);
|
---|
1019 | if (debug>1) cout << "resultat fit = " << pol << endl;
|
---|
1020 | pol.Derivate();
|
---|
1021 | } CATCHALL {
|
---|
1022 | THROW_SAME;
|
---|
1023 | } ENDTRY
|
---|
1024 |
|
---|
1025 | int_4 DPolDeg = pol.Degre();
|
---|
1026 | r_8 fd = 0.;
|
---|
1027 |
|
---|
1028 | if (DPolDeg == 0) {
|
---|
1029 | // on est dans le cas d'un fit de droite
|
---|
1030 | if( pol[0] > 0. ) {
|
---|
1031 | // on a fitte une droite de pente >0.
|
---|
1032 | fd = xFit(nLowHigh-1) + binWidth/2. + xCenter;
|
---|
1033 | } else if( pol[0] < 0. ) {
|
---|
1034 | // on a fitte une droite de pente <0.
|
---|
1035 | fd = xFit(0) - binWidth/2. + xCenter;
|
---|
1036 | } else {
|
---|
1037 | // on a fitte une droite de pente =0. (creneau)
|
---|
1038 | fd = (xFit(0)+xFit(nLowHigh-1))/2. + xCenter;
|
---|
1039 | }
|
---|
1040 | } else if (DPolDeg == 1) {
|
---|
1041 | // on est dans le cas d'un fit de parabole
|
---|
1042 | r_8 r=0;
|
---|
1043 | if (pol.Root1(r)==0) THROW(inconsistentErr);
|
---|
1044 | fd = r + xCenter;
|
---|
1045 | } else if (DPolDeg == 2) {
|
---|
1046 | // on est dans le cas d'un fit de cubique
|
---|
1047 | r_8 r1=0;
|
---|
1048 | r_8 r2=0;
|
---|
1049 | if (pol.Root2(r1,r2) == 0) THROW(inconsistentErr);
|
---|
1050 | pol.Derivate();
|
---|
1051 | fd = (pol(r1)<0) ? r1 + xCenter : r2 + xCenter;
|
---|
1052 | } else {
|
---|
1053 | // on est dans un cas non prevu
|
---|
1054 | THROW(inconsistentErr);
|
---|
1055 | }
|
---|
1056 |
|
---|
1057 | if(fd>mMax) fd = mMax;
|
---|
1058 | if(fd<mMin) fd = mMin;
|
---|
1059 |
|
---|
1060 | if (debug>1)
|
---|
1061 | cout << "Mode histogramme = " << fd
|
---|
1062 | << " (DerPol.degre =" << DPolDeg
|
---|
1063 | << " pol.coeff[0] =" << pol[0]
|
---|
1064 | << ")" << endl;
|
---|
1065 |
|
---|
1066 | return fd;
|
---|
1067 | }
|
---|
1068 |
|
---|
1069 |
|
---|
1070 | /********* Methode *********/
|
---|
1071 | /*!
|
---|
1072 | Calcul de la largeur a frac pourcent du maximum
|
---|
1073 | autour du bin du maximum.
|
---|
1074 | L'histo est suppose etre remplit de valeurs positives
|
---|
1075 | */
|
---|
1076 | r_8 Histo::FindWidth(r_8 frac, int_4 debug) const
|
---|
1077 | {
|
---|
1078 | r_8 xmax = BinCenter( IMax() );
|
---|
1079 | return FindWidth(xmax,frac,debug);
|
---|
1080 | }
|
---|
1081 |
|
---|
1082 | /********* Methode *********/
|
---|
1083 | /*!
|
---|
1084 | Calcul de la largeur a frac pourcent de la valeur du bin xmax.
|
---|
1085 | L'histo est suppose etre remplit de valeurs positives
|
---|
1086 | */
|
---|
1087 | r_8 Histo::FindWidth(r_8 xmax,r_8 frac, int_4 debug) const
|
---|
1088 | {
|
---|
1089 | if (frac <= 0 || frac >= 1.) frac = 0.5;
|
---|
1090 |
|
---|
1091 | if (debug > 1)
|
---|
1092 | cout << "Histo::FindWidth a " << frac
|
---|
1093 | << " de xmax= " << xmax
|
---|
1094 | << " , ndata= " << NData()
|
---|
1095 | << " , nent= " << NEntries()
|
---|
1096 | << " , nbin= " << NBins() << endl;
|
---|
1097 |
|
---|
1098 | if (NEntries() < 1) THROW(inconsistentErr);
|
---|
1099 | if (NBins() < 3) THROW(inconsistentErr);
|
---|
1100 |
|
---|
1101 | int_4 iMax = FindBin(xmax);
|
---|
1102 | if (iMax<0 || iMax>=NBins()) THROW(inconsistentErr);
|
---|
1103 | r_8 hmax = mData[iMax];
|
---|
1104 | r_8 limit = frac*hmax;
|
---|
1105 | if (debug > 1)
|
---|
1106 | cout << " Max histo[" << iMax << "] = " << hmax
|
---|
1107 | << ", limite " << limit << endl;
|
---|
1108 |
|
---|
1109 | int_4 iLow = iMax;
|
---|
1110 | while (iLow>=0 && mData[iLow]>limit) iLow--;
|
---|
1111 | if( iLow < 0 ) iLow = 0;
|
---|
1112 |
|
---|
1113 | int_4 iHigh = iMax;
|
---|
1114 | while (iHigh<NBins() && mData[iHigh]>limit) iHigh++;
|
---|
1115 | if( iHigh >=NBins() ) iHigh = NBins()-1;
|
---|
1116 |
|
---|
1117 | r_8 xlow = BinCenter(iLow);
|
---|
1118 | r_8 ylow = mData[iLow];
|
---|
1119 |
|
---|
1120 | r_8 xlow1=xlow, ylow1=ylow;
|
---|
1121 | if(iLow+1<NBins()) {
|
---|
1122 | xlow1 = BinCenter(iLow+1);
|
---|
1123 | ylow1 = mData[iLow+1];
|
---|
1124 | }
|
---|
1125 |
|
---|
1126 | r_8 xhigh = BinCenter(iHigh);
|
---|
1127 | r_8 yhigh = mData[iHigh];
|
---|
1128 |
|
---|
1129 | r_8 xhigh1=xhigh, yhigh1=yhigh;
|
---|
1130 | if(iHigh-1>=0) {
|
---|
1131 | xhigh1 = BinCenter(iHigh-1);
|
---|
1132 | yhigh1 = mData[iHigh-1];
|
---|
1133 | }
|
---|
1134 |
|
---|
1135 | r_8 xlpred,xhpred,wd;
|
---|
1136 |
|
---|
1137 | if(ylow1>ylow) {
|
---|
1138 | xlpred = xlow + (xlow1-xlow)/(ylow1-ylow)*(limit-ylow);
|
---|
1139 | if(xlpred < xlow) xlpred = xlow;
|
---|
1140 | } else xlpred = xlow;
|
---|
1141 |
|
---|
1142 | if(yhigh1>yhigh) {
|
---|
1143 | xhpred = xhigh + (xhigh1-xhigh)/(yhigh1-yhigh)*(limit-yhigh);
|
---|
1144 | if(xhpred > xhigh) xhpred = xhigh;
|
---|
1145 | } else xhpred = xhigh;
|
---|
1146 |
|
---|
1147 | wd = xhpred - xlpred;
|
---|
1148 |
|
---|
1149 | if (debug > 1) {
|
---|
1150 | cout << "Limites histo: " << " Width " << wd << endl;
|
---|
1151 | cout << " Low: [" << iLow << "]=" << ylow << " pred-> " << xlpred
|
---|
1152 | << " - High: [" << iHigh << "]=" << yhigh << " pred-> " << xhpred
|
---|
1153 | << endl;
|
---|
1154 | }
|
---|
1155 |
|
---|
1156 | return wd;
|
---|
1157 | }
|
---|
1158 |
|
---|
1159 |
|
---|
1160 | /********* Methode *********/
|
---|
1161 | /*!
|
---|
1162 | Cf suivant mais im est le bin du maximum de l'histo
|
---|
1163 | */
|
---|
1164 | int_4 Histo::EstimeMax(r_8& xm,int_4 SzPav)
|
---|
1165 | {
|
---|
1166 | int_4 im = IMax();
|
---|
1167 | return EstimeMax(im,xm,SzPav);
|
---|
1168 | }
|
---|
1169 |
|
---|
1170 | /********* Methode *********/
|
---|
1171 | /*!
|
---|
1172 | Determine l'abscisses du maximum donne par im
|
---|
1173 | en moyennant dans un pave SzPav autour du maximum
|
---|
1174 | \verbatim
|
---|
1175 | Return:
|
---|
1176 | 0 = si fit maximum reussi avec SzPav pixels
|
---|
1177 | 1 = si fit maximum reussi avec moins que SzPav pixels
|
---|
1178 | 2 = si fit maximum echoue et renvoit BinCenter()
|
---|
1179 | -1 = si echec: SzPav <= 0 ou im hors limites
|
---|
1180 | \endverbatim
|
---|
1181 | */
|
---|
1182 | int_4 Histo::EstimeMax(int& im,r_8& xm,int_4 SzPav)
|
---|
1183 | {
|
---|
1184 | xm = 0;
|
---|
1185 | if( SzPav <= 0 ) return -1;
|
---|
1186 | if( im < 0 || im >= mBins ) return -1;
|
---|
1187 |
|
---|
1188 | if( SzPav%2 == 0 ) SzPav++;
|
---|
1189 | SzPav = (SzPav-1)/2;
|
---|
1190 |
|
---|
1191 | int_4 rc = 0;
|
---|
1192 | r_8 dxm = 0, wx = 0;
|
---|
1193 | for(int_4 i=im-SzPav;i<=im+SzPav;i++) {
|
---|
1194 | if( i<0 || i>= mBins ) {rc=1; continue;}
|
---|
1195 | dxm += BinCenter(i) * (*this)(i);
|
---|
1196 | wx += (*this)(i);
|
---|
1197 | }
|
---|
1198 |
|
---|
1199 | if( wx > 0. ) {
|
---|
1200 | xm = dxm/wx;
|
---|
1201 | return rc;
|
---|
1202 | } else {
|
---|
1203 | xm = BinCenter(im);
|
---|
1204 | return 2;
|
---|
1205 | }
|
---|
1206 |
|
---|
1207 | }
|
---|
1208 |
|
---|
1209 | /********* Methode *********/
|
---|
1210 | /*!
|
---|
1211 | Determine la largeur a frac% du maximum a gauche (widthG)
|
---|
1212 | et a droite (widthD)
|
---|
1213 | */
|
---|
1214 | void Histo::EstimeWidthS(r_8 frac,r_8& widthG,r_8& widthD)
|
---|
1215 | {
|
---|
1216 | int_4 i;
|
---|
1217 | widthG = widthD = -1.;
|
---|
1218 | if( mBins<=1 || frac<=0. || frac>=1. ) return;
|
---|
1219 |
|
---|
1220 | int_4 imax = 0;
|
---|
1221 | r_8 maxi = mData[0];
|
---|
1222 | for(i=1;i<mBins;i++) if(mData[i]>maxi) {imax=i; maxi=mData[i];}
|
---|
1223 | r_8 xmax = BinCenter(imax);
|
---|
1224 | r_8 maxf = maxi * frac;
|
---|
1225 |
|
---|
1226 | // recherche du sigma a gauche
|
---|
1227 | widthG = 0.;
|
---|
1228 | for(i=imax;i>=0;i--) if( mData[i] <= maxf ) break;
|
---|
1229 | if(i<0) i=0;
|
---|
1230 | if(i<imax) {
|
---|
1231 | if( mData[i+1] != mData[i] ) {
|
---|
1232 | widthG = BinCenter(i) + binWidth
|
---|
1233 | * (maxf-mData[i])/(mData[i+1]-mData[i]);
|
---|
1234 | widthG = xmax - widthG;
|
---|
1235 | } else widthG = xmax - BinCenter(i);
|
---|
1236 | }
|
---|
1237 |
|
---|
1238 | // recherche du sigma a droite
|
---|
1239 | widthD = 0.;
|
---|
1240 | for(i=imax;i<mBins;i++) if( mData[i] <= maxf ) break;
|
---|
1241 | if(i>=mBins) i=mBins-1;
|
---|
1242 | if(i>imax) {
|
---|
1243 | if( mData[i] != mData[i-1] ) {
|
---|
1244 | widthD = BinCenter(i) - binWidth
|
---|
1245 | * (maxf-mData[i])/(mData[i-1]-mData[i]);
|
---|
1246 | widthD -= xmax;
|
---|
1247 | } else widthD = BinCenter(i) - xmax;
|
---|
1248 | }
|
---|
1249 |
|
---|
1250 | }
|
---|
1251 |
|
---|
1252 | //////////////////////////////////////////////////////////
|
---|
1253 | /*!
|
---|
1254 | Fit de l'histogramme par ``gfit''.
|
---|
1255 | \verbatim
|
---|
1256 | typ_err = 0 :
|
---|
1257 | - erreur attachee au bin si elle existe
|
---|
1258 | - sinon 1
|
---|
1259 | typ_err = 1 :
|
---|
1260 | - erreur attachee au bin si elle existe
|
---|
1261 | - sinon max( sqrt(abs(bin) ,1 )
|
---|
1262 | typ_err = 2 :
|
---|
1263 | - erreur forcee a 1
|
---|
1264 | typ_err = 3 :
|
---|
1265 | - erreur forcee a max( sqrt(abs(bin) ,1 )
|
---|
1266 | typ_err = 4 :
|
---|
1267 | - erreur forcee a 1, nulle si bin a zero.
|
---|
1268 | typ_err = 5 :
|
---|
1269 | - erreur forcee a max( sqrt(abs(bin) ,1 ),
|
---|
1270 | nulle si bin a zero.
|
---|
1271 | \endverbatim
|
---|
1272 | */
|
---|
1273 | int_4 Histo::Fit(GeneralFit& gfit,unsigned short typ_err)
|
---|
1274 | {
|
---|
1275 | if(NBins()<=0) return -1000;
|
---|
1276 | if(typ_err>5) typ_err=0;
|
---|
1277 |
|
---|
1278 | GeneralFitData mydata(1,NBins());
|
---|
1279 |
|
---|
1280 | for(int_4 i=0;i<NBins();i++) {
|
---|
1281 | r_8 x = BinCenter(i);
|
---|
1282 | r_8 f = (*this)(i);
|
---|
1283 | r_8 saf = sqrt(fabs( f)); if(saf<1.) saf=1.;
|
---|
1284 | r_8 e=0.;
|
---|
1285 | if(typ_err==0) {if(HasErrors()) e=Error(i); else e=1.;}
|
---|
1286 | else if(typ_err==1) {if(HasErrors()) e=Error(i); else e=saf;}
|
---|
1287 | else if(typ_err==2) e=1.;
|
---|
1288 | else if(typ_err==3) e=saf;
|
---|
1289 | else if(typ_err==4) e=(f==0.)?0.:1.;
|
---|
1290 | else if(typ_err==5) e=(f==0.)?0.:saf;
|
---|
1291 | mydata.AddData1(x,f,e);
|
---|
1292 | }
|
---|
1293 |
|
---|
1294 | gfit.SetData(&mydata);
|
---|
1295 |
|
---|
1296 | return gfit.Fit();
|
---|
1297 | }
|
---|
1298 |
|
---|
1299 | /*!
|
---|
1300 | Retourne une classe contenant les residus du fit ``gfit''.
|
---|
1301 | */
|
---|
1302 | Histo Histo::FitResidus(GeneralFit& gfit)
|
---|
1303 | {
|
---|
1304 | if(NBins()<=0)
|
---|
1305 | throw(SzMismatchError("Histo::FitResidus: size mismatch\n"));
|
---|
1306 | GeneralFunction* f = gfit.GetFunction();
|
---|
1307 | if(f==NULL)
|
---|
1308 | throw(NullPtrError("Histo::FitResidus: NULL pointer\n"));
|
---|
1309 | TVector<r_8> par = gfit.GetParm();
|
---|
1310 | Histo h(*this);
|
---|
1311 | for(int_4 i=0;i<NBins();i++) {
|
---|
1312 | r_8 x = BinCenter(i);
|
---|
1313 | h(i) -= f->Value(&x,par.Data());
|
---|
1314 | }
|
---|
1315 | return h;
|
---|
1316 | }
|
---|
1317 |
|
---|
1318 | /*!
|
---|
1319 | Retourne une classe contenant la fonction du fit ``gfit''.
|
---|
1320 | */
|
---|
1321 | Histo Histo::FitFunction(GeneralFit& gfit)
|
---|
1322 | {
|
---|
1323 | if(NBins()<=0)
|
---|
1324 | throw(SzMismatchError("Histo::FitFunction: size mismatch\n"));
|
---|
1325 | GeneralFunction* f = gfit.GetFunction();
|
---|
1326 | if(f==NULL)
|
---|
1327 | throw(NullPtrError("Histo::FitFunction: NULL pointer\n"));
|
---|
1328 | TVector<r_8> par = gfit.GetParm();
|
---|
1329 | Histo h(*this);
|
---|
1330 | for(int_4 i=0;i<NBins();i++) {
|
---|
1331 | r_8 x = BinCenter(i);
|
---|
1332 | h(i) = f->Value(&x,par.Data());
|
---|
1333 | }
|
---|
1334 | return h;
|
---|
1335 | }
|
---|
1336 |
|
---|
1337 | /********* Methode *********/
|
---|
1338 | /*!
|
---|
1339 | Impression de l'histogramme dans le fichier fp
|
---|
1340 | \verbatim
|
---|
1341 | hdyn = nombre de colonnes pour imprimer les etoiles
|
---|
1342 | si =0 alors defaut(100),
|
---|
1343 | si <0 pas de print histo seulement infos
|
---|
1344 | hmin = minimum de la dynamique
|
---|
1345 | hmax = maximum de la dynamique
|
---|
1346 | si hmax<=hmin : hmin=VMin() hmax=VMax()
|
---|
1347 | si hmax<=hmin et hmin=0 : hmin=0 hmax=VMax()
|
---|
1348 | sinon : hmin hmax
|
---|
1349 | pflag < 0 : on imprime les informations (nbin,min,...) sans l'histogramme
|
---|
1350 | = 0 : on imprime "BinCenter(i) mData[i]" (note "... ...")
|
---|
1351 | bit 0 on : (v=1): numero_bin "... ..."
|
---|
1352 | bit 1 on : (v=2): "... ..." erreur
|
---|
1353 | \endverbatim
|
---|
1354 | */
|
---|
1355 | void Histo::PrintF(FILE * fp, int_4 hdyn,r_8 hmin, r_8 hmax,int_4 pflag,
|
---|
1356 | int_4 il, int_4 ih)
|
---|
1357 | {
|
---|
1358 |
|
---|
1359 | if( il > ih ) { il = 0; ih = mBins-1; }
|
---|
1360 | if( il < 0 ) il = 0;
|
---|
1361 | if( ih >= mBins ) ih = mBins-1;
|
---|
1362 |
|
---|
1363 | r_8 dhmin = hmin;
|
---|
1364 | r_8 dhmax = hmax;
|
---|
1365 | r_8 hb,hbmn,hbmx;
|
---|
1366 |
|
---|
1367 | if(hdyn==0) hdyn = 100;
|
---|
1368 |
|
---|
1369 | cout << "~Histo::Print "
|
---|
1370 | << " nHist=" << nHist << " nEntries=" << nEntries
|
---|
1371 | << " mUnder=" << mUnder << " mOver=" << mOver << endl;
|
---|
1372 | cout << " mBins=" << mBins
|
---|
1373 | << " min=" << mMin << " mMax=" << mMax
|
---|
1374 | << " binWidth=" << binWidth << endl;
|
---|
1375 | cout << " mean=" << Mean() << " r.m.s=" << Sigma()
|
---|
1376 | << " Errors="<<HasErrors()<< endl;
|
---|
1377 |
|
---|
1378 | if(hdyn<0 || pflag<0 ) return;
|
---|
1379 |
|
---|
1380 | if(dhmax<=dhmin) { if(hmin != 0.) dhmin = VMin(); else dhmin=0.;
|
---|
1381 | dhmax = VMax(); }
|
---|
1382 | if(dhmin>dhmax) return;
|
---|
1383 | if(dhmin==dhmax) {dhmin -= 1.; dhmax += 1.;}
|
---|
1384 | r_8 wb = (dhmax-dhmin) / hdyn;
|
---|
1385 |
|
---|
1386 | // determination de la position de la valeur zero
|
---|
1387 | int_4 i0 = (int_4)(-dhmin/wb);
|
---|
1388 |
|
---|
1389 | // si le zero est dans la dynamique,
|
---|
1390 | // il doit imperativement etre une limite de bin
|
---|
1391 | if( 0 <= i0 && i0 < hdyn ) {
|
---|
1392 | hbmn = dhmin + i0*wb;
|
---|
1393 | hbmx = hbmn + wb;
|
---|
1394 | if( hbmn != 0. ) {
|
---|
1395 | hbmn *= -1.;
|
---|
1396 | if( hbmn < hbmx ) {
|
---|
1397 | // le zero est + pres du bord negatif du bin
|
---|
1398 | dhmin += hbmn;
|
---|
1399 | dhmax += hbmn;
|
---|
1400 | } else {
|
---|
1401 | // le zero est + pres du bord positif du bin
|
---|
1402 | dhmin -= hbmx;
|
---|
1403 | dhmax -= hbmx;
|
---|
1404 | }
|
---|
1405 | wb = (dhmax-dhmin) / hdyn;
|
---|
1406 | i0 = (int_4)(-dhmin/wb);
|
---|
1407 | }
|
---|
1408 | }
|
---|
1409 |
|
---|
1410 | cout <<" bin minimum="<<dhmin<<" bin maximum="<<dhmax
|
---|
1411 | <<" binw="<<wb<< endl;
|
---|
1412 |
|
---|
1413 | char* s = new char[hdyn+1];
|
---|
1414 | s[hdyn] = '\0';
|
---|
1415 |
|
---|
1416 | // premiere ligne
|
---|
1417 | {for(int_4 i=0;i<hdyn;i++) s[i] = '=';}
|
---|
1418 | if( 0 <= i0 && i0 < hdyn ) s[i0] = '0';
|
---|
1419 | if(pflag&1) fprintf( fp, "====");
|
---|
1420 | fprintf( fp, "======================");
|
---|
1421 | if(pflag&2 && mErr2!=NULL) fprintf( fp, "===========");
|
---|
1422 | fprintf( fp, "==%s\n",s);
|
---|
1423 |
|
---|
1424 | // histogramme
|
---|
1425 | {for(int_4 i=il;i<=ih;i++) {
|
---|
1426 | for(int_4 k=0;k<hdyn;k++) s[k] = ' ';
|
---|
1427 | hb = (*this)(i);
|
---|
1428 |
|
---|
1429 | //choix du bin (hdyn bin entre [dhmin,dhmax[
|
---|
1430 | int_4 ii = (int_4)( (hb-dhmin)/wb );
|
---|
1431 | if(ii<0) ii = 0; else if(ii>=hdyn) ii = hdyn-1;
|
---|
1432 |
|
---|
1433 | // limite du bin
|
---|
1434 | hbmn = dhmin + ii*wb;
|
---|
1435 | hbmx = hbmn + wb;
|
---|
1436 |
|
---|
1437 | // remplissage de s[] en tenant compte des courbes +/-
|
---|
1438 | if(i0<0) {
|
---|
1439 | // courbe entierement positive
|
---|
1440 | for(int_4 k=0;k<=ii;k++) s[k] = 'X';
|
---|
1441 | } else if(i0>=hdyn) {
|
---|
1442 | // courbe entierement negative
|
---|
1443 | for(int_4 k=hdyn-1;k>=ii;k--) s[k] = 'X';
|
---|
1444 | } else {
|
---|
1445 | // courbe positive et negative
|
---|
1446 | s[i0] = '|';
|
---|
1447 | if(ii>i0) for(int_4 k=i0+1;k<=ii;k++) s[k] = 'X';
|
---|
1448 | else if(ii<i0) for(int_4 k=ii;k<i0;k++) s[k] = 'X';
|
---|
1449 | else s[ii] = 'X';
|
---|
1450 | }
|
---|
1451 |
|
---|
1452 | // valeur a mettre dans le bin le plus haut/bas
|
---|
1453 | int_4 ib;
|
---|
1454 | if(hb>0.) ib = (int_4)( 10.*(hb-hbmn)/(hbmx-hbmn) );
|
---|
1455 | else if(hb<0.) ib = (int_4)( 10.*(hbmx-hb)/(hbmx-hbmn) );
|
---|
1456 | else ib = -1;
|
---|
1457 | if(ib==-1) s[ii] = '|';
|
---|
1458 | else if(ib==0) s[ii] = '.';
|
---|
1459 | else if(ib>0 && ib<10) s[ii] = (char)((int_4) '0' + ib);
|
---|
1460 |
|
---|
1461 | // traitement des saturations +/-
|
---|
1462 | if( hb < dhmin ) s[0] = '*';
|
---|
1463 | else if( hb > dhmax ) s[hdyn-1] = '*';
|
---|
1464 |
|
---|
1465 | if(pflag&1) fprintf( fp, "%3d ",i);
|
---|
1466 | fprintf( fp, "%10.4g %10.4g ",BinCenter(i),hb);
|
---|
1467 | if(pflag&2 && mErr2!=NULL) fprintf( fp, "%10.4g ",Error(i));
|
---|
1468 | fprintf( fp, "= %s\n",s);
|
---|
1469 | }}
|
---|
1470 |
|
---|
1471 | // derniere ligne
|
---|
1472 | for(int_4 i=0;i<hdyn;i++) s[i] = '=';
|
---|
1473 | if( 0 <= i0 && i0 < hdyn ) s[i0] = '0';
|
---|
1474 | if(pflag&1) fprintf( fp, "====");
|
---|
1475 | fprintf( fp, "======================");
|
---|
1476 | if(pflag&2 && mErr2!=NULL) fprintf( fp, "===========");
|
---|
1477 | fprintf( fp, "==%s\n",s);
|
---|
1478 |
|
---|
1479 | // valeur basse des bins (sur ["ndig-1" digits + signe] = ndig char (>=3))
|
---|
1480 | const int_4 ndig = 7;
|
---|
1481 | char sn[2*ndig+10];
|
---|
1482 | hb = ( fabs(dhmin) > fabs(dhmax) ) ? fabs(dhmin) : fabs(dhmax);
|
---|
1483 | int_4 n;
|
---|
1484 | if( hb > 0. ) n = (int_4)(log10(hb*1.00000001)); else n = 1;
|
---|
1485 | r_8 scaledig = pow(10.,(r_8) ndig-2);
|
---|
1486 | r_8 expo = scaledig/pow(10.,(r_8) n);
|
---|
1487 | // cout <<"n="<<n<<" hb="<<hb<<" scaledig="<<scaledig<<" expo="<<expo<<endl;
|
---|
1488 | for(int_4 k=0;k<ndig;k++) {
|
---|
1489 | for(int_4 i=0;i<hdyn;i++) s[i] = ' ';
|
---|
1490 | {for(int_4 i=0;i<hdyn;i++) {
|
---|
1491 | n = (int_4)( (dhmin + i*wb)*expo );
|
---|
1492 | for(int_4 j=0;j<2*ndig+10;j++) sn[j] = ' ';
|
---|
1493 | sprintf(sn,"%d%c",n,'\0');
|
---|
1494 | strip(sn,'B',' ');
|
---|
1495 | // cout <<"n="<<n<<" sn=("<<sn<<") l="<<strlen(sn)<<" k="<<k;
|
---|
1496 | if( (int_4) strlen(sn) > k ) s[i] = sn[k];
|
---|
1497 | // cout <<" s=("<<s<<")"<<endl;
|
---|
1498 | }}
|
---|
1499 | if(pflag&1) fprintf( fp, " ");
|
---|
1500 | fprintf( fp, " ");
|
---|
1501 | if(pflag&2 && mErr2!=NULL) fprintf( fp, " ");
|
---|
1502 | fprintf( fp, " %s\n",s);
|
---|
1503 | }
|
---|
1504 | fprintf( fp, " (valeurs a multiplier par %.0e)\n",1./expo);
|
---|
1505 |
|
---|
1506 | delete[] s;
|
---|
1507 | }
|
---|
1508 |
|
---|
1509 | /********* Methode *********/
|
---|
1510 | /*!
|
---|
1511 | Impression de l'histogramme sur stdout
|
---|
1512 | */
|
---|
1513 | void Histo::Print(int_4 hdyn,r_8 hmin, r_8 hmax,int_4 pflag,
|
---|
1514 | int_4 il, int_4 ih)
|
---|
1515 | {
|
---|
1516 | Histo::PrintF(stdout, hdyn, hmin, hmax, pflag, il, ih);
|
---|
1517 | }
|
---|
1518 |
|
---|
1519 |
|
---|
1520 | ///////////////////////////////////////////////////////////
|
---|
1521 | // --------------------------------------------------------
|
---|
1522 | // Les objets delegues pour la gestion de persistance
|
---|
1523 | // --------------------------------------------------------
|
---|
1524 | ///////////////////////////////////////////////////////////
|
---|
1525 |
|
---|
1526 | void ObjFileIO<Histo>::ReadSelf(PInPersist& is)
|
---|
1527 | {
|
---|
1528 | char strg[256];
|
---|
1529 |
|
---|
1530 | if(dobj==NULL) dobj=new Histo;
|
---|
1531 | else dobj->Delete();
|
---|
1532 |
|
---|
1533 | // Lecture entete
|
---|
1534 | is.GetLine(strg, 255);
|
---|
1535 | is.GetLine(strg, 255);
|
---|
1536 | is.GetLine(strg, 255);
|
---|
1537 |
|
---|
1538 | // Lecture des valeurs
|
---|
1539 | is.Get(dobj->mBins);
|
---|
1540 | is.Get(dobj->nEntries);
|
---|
1541 | int_4 errok;
|
---|
1542 | is.Get(errok);
|
---|
1543 |
|
---|
1544 | is.Get(dobj->binWidth);
|
---|
1545 | is.Get(dobj->mMin);
|
---|
1546 | is.Get(dobj->mMax);
|
---|
1547 | is.Get(dobj->mUnder);
|
---|
1548 | is.Get(dobj->mOver);
|
---|
1549 |
|
---|
1550 | is.Get(dobj->nHist);
|
---|
1551 |
|
---|
1552 | // Lecture des donnees Histo 1D
|
---|
1553 | dobj->mData = new r_8[dobj->mBins];
|
---|
1554 | is.GetLine(strg, 255);
|
---|
1555 | is.Get(dobj->mData, dobj->mBins);
|
---|
1556 |
|
---|
1557 | // Lecture des erreurs
|
---|
1558 | if(errok) {
|
---|
1559 | is.GetLine(strg, 255);
|
---|
1560 | dobj->mErr2 = new r_8[dobj->mBins];
|
---|
1561 | is.Get(dobj->mErr2, dobj->mBins);
|
---|
1562 | }
|
---|
1563 |
|
---|
1564 | return;
|
---|
1565 | }
|
---|
1566 |
|
---|
1567 | void ObjFileIO<Histo>::WriteSelf(POutPersist& os) const
|
---|
1568 | {
|
---|
1569 | if (dobj == NULL) return;
|
---|
1570 | char strg[256];
|
---|
1571 |
|
---|
1572 | // Que faut-il ecrire?
|
---|
1573 | int_4 errok = (dobj->mErr2) ? 1 : 0;
|
---|
1574 |
|
---|
1575 | // Ecriture entete pour identifier facilement
|
---|
1576 | sprintf(strg,"mBins=%6d NEnt=%15d errok=%1d",dobj->mBins,dobj->nEntries,errok);
|
---|
1577 | os.PutLine(strg);
|
---|
1578 | sprintf(strg,"binw=%g mMin=%g mMax=%g",dobj->binWidth,dobj->mMin,dobj->mMax);
|
---|
1579 | os.PutLine(strg);
|
---|
1580 | sprintf(strg, "mUnder=%g mOver=%g nHist=%g",dobj->mUnder,dobj->mOver,dobj->nHist);
|
---|
1581 | os.PutLine(strg);
|
---|
1582 |
|
---|
1583 | // Ecriture des valeurs
|
---|
1584 | os.Put(dobj->mBins);
|
---|
1585 | os.Put(dobj->nEntries);
|
---|
1586 | os.Put(errok);
|
---|
1587 |
|
---|
1588 | os.Put(dobj->binWidth);
|
---|
1589 | os.Put(dobj->mMin);
|
---|
1590 | os.Put(dobj->mMax);
|
---|
1591 | os.Put(dobj->mUnder);
|
---|
1592 | os.Put(dobj->mOver);
|
---|
1593 |
|
---|
1594 | os.Put(dobj->nHist);
|
---|
1595 |
|
---|
1596 | // Ecriture des donnees Histo 1D
|
---|
1597 | sprintf(strg,"Histo: Tableau des donnees %d",dobj->mBins);
|
---|
1598 | os.PutLine(strg);
|
---|
1599 | os.Put(dobj->mData, dobj->mBins);
|
---|
1600 |
|
---|
1601 | // Ecriture des erreurs
|
---|
1602 | if(errok) {
|
---|
1603 | sprintf(strg,"Histo: Tableau des erreurs %d",dobj->mBins);
|
---|
1604 | os.PutLine(strg);
|
---|
1605 | os.Put(dobj->mErr2, dobj->mBins);
|
---|
1606 | }
|
---|
1607 |
|
---|
1608 | return;
|
---|
1609 | }
|
---|
1610 |
|
---|
1611 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
1612 | #pragma define_template ObjFileIO<Histo>
|
---|
1613 | #endif
|
---|
1614 |
|
---|
1615 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
1616 | template class ObjFileIO<Histo>;
|
---|
1617 | #endif
|
---|