source: Sophya/trunk/SophyaLib/NTools/histos2.cc@ 241

Last change on this file since 241 was 241, checked in by ansari, 26 years ago

ppersist + pexc

File size: 43.8 KB
Line 
1//
2// cmv 05/08/96
3//
4
5#include "defs.h"
6
7#include <string.h>
8#include <new.h>
9#include <stdlib.h>
10#include <stdio.h>
11#ifndef __mac__
12#include <values.h>
13#endif
14
15#include "histos2.h"
16#include "generalfit.h"
17
18//++
19// Class Histo2D
20// Lib Outils++
21// include histos2.h
22//
23// Classe d'histogrammes 2D
24//--
25
26///////////////////////////////////////////////////////////////////
27//++
28// Titre Constructeurs
29//--
30
31//++
32Histo2D::Histo2D(float xMin, float xMax, int nxBin
33 ,float yMin, float yMax, int nyBin)
34//
35// Createur d'un histogramme 2D ayant nxBin,nyBin bins
36// entre xMin,xMax et yMin,yMax.
37//--
38 : data(new float[nxBin*nyBin]), err2(NULL)
39 , nHist(0), nEntries(0)
40 , nx(nxBin), ny(nyBin), nxy(nxBin*nyBin)
41 , xmin(xMin), xmax(xMax), ymin(yMin), ymax(yMax)
42 , wbinx((xMax - xMin)/nxBin), wbiny((yMax - yMin)/nyBin)
43 , hprojx(NULL), hprojy(NULL)
44{
45DBASSERT(nxBin>0 && nyBin>0 && xMin<xMax && yMin<yMax);
46for(int i=0;i<3;i++) for(int j=0;j<3;j++) over[i][j]=0.;
47Zero();
48b_s.H = NULL;
49END_CONSTRUCTOR
50}
51
52//++
53Histo2D::Histo2D(const Histo2D& h)
54//
55// Constructeur par copie.
56//--
57{
58int i,j;
59data = new float[h.nxy];
60memcpy(data, h.data, h.nxy*sizeof(float));
61
62err2 = NULL;
63if(h.err2) {
64 err2 = new double[h.nxy];
65 memcpy(err2, h.err2, h.nxy*sizeof(double));
66}
67
68nHist = h.nHist; nEntries = h.nEntries;
69for(i=0;i<3;i++) for(j=0;j<3;j++) over[i][j]=h.over[i][j];
70nx = h.nx; ny = h.ny; nxy = h.nxy;
71xmin = h.xmin; xmax = h.xmax; ymin = h.ymin; ymax = h.ymax;
72wbinx = h.wbinx; wbiny = h.wbiny;
73b_s.H = NULL;
74
75hprojx = hprojy = NULL;
76if(h.hprojx) {
77 SetProjX();
78 *hprojx = *(h.hprojx);
79}
80if(h.hprojy) {
81 SetProjY();
82 *hprojy = *(h.hprojy);
83}
84
85int nb;
86float min,max;
87nb = h.NSliX();
88if(nb>0) {
89 SetSliX(nb);
90 for(i=0; i<NSliX();i++) *HSliX(i) = *(h.HSliX(i));
91}
92nb = h.NSliY();
93if(nb>0) {
94 SetSliY(nb);
95 for(i=0; i<NSliY();i++) *HSliY(i) = *(h.HSliY(i));
96}
97
98nb = h.NBandX();
99if(nb>0) {
100 for(i=0; i<nb;i++) {
101 h.GetBandX(i,min,max);
102 SetBandX(min,max);
103 *HBandX(i) = *(h.HBandX(i));
104 }
105 for(i=0; i<NBandX();i++) *HBandX(i) = *(h.HBandX(i));
106}
107nb = h.NBandY();
108if(nb>0) {
109 for(i=0; i<nb;i++) {
110 h.GetBandY(i,min,max);
111 SetBandY(min,max);
112 *HBandY(i) = *(h.HBandY(i));
113 }
114 for(i=0; i<NBandY();i++) *HBandY(i) = *(h.HBandY(i));
115}
116
117END_CONSTRUCTOR
118}
119
120//++
121Histo2D::Histo2D()
122//
123// Constructeur par defaut.
124//--
125 : data(NULL), err2(NULL)
126 , nHist(0), nEntries(0)
127 , nx(0), ny(0), nxy(0)
128 , xmin(0), xmax(0), ymin(0), ymax(0)
129 , wbinx(0), wbiny(0)
130 , hprojx(NULL), hprojy(NULL)
131{
132for(int i=0;i<3;i++) for(int j=0;j<3;j++) over[i][j]=0.;
133b_s.H = NULL;
134END_CONSTRUCTOR
135}
136
137//++
138Histo2D::Histo2D(char *flnm)
139//
140// Constructeur par lecture d'un fichier ppersist.
141//--
142 : data(NULL), err2(NULL)
143 , nHist(0), nEntries(0)
144 , nx(0), ny(0), nxy(0)
145 , xmin(0), xmax(0), ymin(0), ymax(0)
146 , wbinx(0), wbiny(0)
147 , hprojx(NULL), hprojy(NULL)
148{
149b_s.H = NULL;
150PInPersist s(flnm);
151Read(s);
152END_CONSTRUCTOR
153}
154
155///////////////////////////////////////////////////////////////////
156//++
157void Histo2D::Delete()
158//
159// Desallocation de la place de l'histogramme (fct privee).
160//--
161{
162 if( data != NULL ) { delete[] data; data = NULL;}
163
164 if( err2 != NULL ) { delete[] err2; err2 = NULL;}
165
166 DelProj();
167
168 DelBandX();
169 DelBandY();
170
171 DelSliX();
172 DelSliY();
173
174 nHist = 0;
175 nEntries = 0;
176 nx = 0; ny = 0; nxy = 0;
177 xmin = 0; xmax = 0; ymin = 0; ymax = 0;
178 wbinx = 0; wbiny = 0;
179 for(int i=0;i<3;i++) for(int j=0;j<3;j++) over[i][j]=0.;
180 b_s.H = NULL;
181}
182
183//++
184Histo2D::~Histo2D()
185//
186// Destructeur.
187//--
188{
189Delete();
190}
191
192///////////////////////////////////////////////////////////////////
193//++
194// Titre Methodes generales
195//--
196
197//++
198void Histo2D::Zero()
199//
200// Remise a zero du contenu, des erreurs et des valeurs.
201//--
202{
203 nHist = nEntries = 0;
204 for(int i=0;i<3;i++) for(int j=0;j<3;j++) over[i][j]=0.;
205 memset(data, 0, nxy*sizeof(float));
206 memset(over, 0, 9*sizeof(float));
207
208 if( err2 != NULL ) memset(err2, 0, nxy*sizeof(double));
209
210 ZeroProj();
211
212 ZeroBandX();
213 ZeroBandY();
214
215 ZeroSliX();
216 ZeroSliY();
217}
218
219///////////////////////////////////////////////////////////////////
220//++
221void Histo2D::Errors()
222//
223// Pour avoir le calcul des erreurs.
224//--
225{
226 if( nxy > 0 ) {
227 if(err2==NULL) err2 = new double[nxy];
228 memset(err2, 0, nxy*sizeof(double));
229 }
230}
231
232///////////////////////////////////////////////////////////////////
233//++
234Histo2D& Histo2D::operator = (const Histo2D& h)
235//
236//--
237{
238 int i,j,nb;
239 float min,max;
240
241 if(this == &h) return *this;
242 if( h.nxy > nxy ) Delete();
243 if(!data) data = new float[h.nxy];
244 if( !h.err2 && err2 ) { delete [] err2; err2=NULL;}
245 if( h.err2 && !err2 ) err2 = new double[h.nxy];
246
247 for(i=0;i<3;i++) for(j=0;j<3;j++) over[i][j] = h.over[i][j];
248 nHist = h.nHist;
249 nEntries = h.nEntries;
250 nx = h.nx; ny = h.ny; nxy = h.nxy;
251 xmin = h.xmin; xmax = h.xmax; wbinx = h.wbinx;
252 ymin = h.ymin; ymax = h.ymax; wbiny = h.wbiny;
253
254 memcpy(data, h.data, nxy*sizeof(float));
255 if(err2) memcpy(err2, h.err2, nxy*sizeof(double));
256
257 DelProjX();
258 if(h.hprojx) {
259 SetProjX();
260 *hprojx = *(h.hprojx);
261 }
262 DelProjY();
263 if(h.hprojy) {
264 SetProjY();
265 *hprojy = *(h.hprojy);
266 }
267
268 DelSliX();
269 nb = h.NSliX();
270 if(nb>0) {
271 SetSliX(nb);
272 for(i=0; i<NSliX();i++) *HSliX(i) = *(h.HSliX(i));
273 }
274 DelSliY();
275 nb = h.NSliY();
276 if(nb>0) {
277 SetSliY(nb);
278 for(i=0; i<NSliY();i++) *HSliY(i) = *(h.HSliY(i));
279 }
280
281 DelBandX();
282 nb = h.NBandX();
283 if(nb>0) {
284 for(i=0; i<nb;i++) {
285 h.GetBandX(i,min,max);
286 SetBandX(min,max);
287 *HBandX(i) = *(h.HBandX(i));
288 }
289 for(i=0; i<NBandX();i++) *HBandX(i) = *(h.HBandX(i));
290 }
291 DelBandY();
292 nb = h.NBandY();
293 if(nb>0) {
294 for(i=0; i<nb;i++) {
295 h.GetBandY(i,min,max);
296 SetBandY(min,max);
297 *HBandY(i) = *(h.HBandY(i));
298 }
299 for(i=0; i<NBandY();i++) *HBandY(i) = *(h.HBandY(i));
300 }
301
302 return *this;
303}
304
305///////////////////////////////////////////////////////////////////
306//++
307Histo2D& Histo2D::operator *= (double b)
308//
309//--
310{
311int i,j;
312double b2 = b*b;
313for(i=0;i<nxy;i++) {
314 data[i] *= b;
315 if(err2) err2[i] *= b2;
316}
317for(i=0;i<3;i++) for(j=0;j<3;j++) over[i][j] *= b;
318nHist *= b;
319
320if(hprojx) *hprojx *= b;
321if(hprojy) *hprojy *= b;
322if(NSliX()>0) for(i=0; i<NSliX();i++) *HSliX(i) *= b;
323if(NSliY()>0) for(i=0; i<NSliY();i++) *HSliY(i) *= b;
324if(NBandX()>0) for(i=0; i<NBandX();i++) *HBandX(i) *= b;
325if(NBandY()>0) for(i=0; i<NBandY();i++) *HBandY(i) *= b;
326
327return *this;
328}
329
330//++
331Histo2D& Histo2D::operator /= (double b)
332//
333//--
334{
335int i,j;
336if (b==0.) THROW(inconsistentErr);
337double b2 = b*b;
338for(i=0;i<nxy;i++) {
339 data[i] /= b;
340 if(err2) err2[i] /= b2;
341}
342for(i=0;i<3;i++) for(j=0;j<3;j++) over[i][j] /= b;
343nHist /= b;
344
345if(hprojx) *hprojx /= b;
346if(hprojy) *hprojy /= b;
347if(NSliX()>0) for(i=0; i<NSliX();i++) *HSliX(i) /= b;
348if(NSliY()>0) for(i=0; i<NSliY();i++) *HSliY(i) /= b;
349if(NBandX()>0) for(i=0; i<NBandX();i++) *HBandX(i) /= b;
350if(NBandY()>0) for(i=0; i<NBandY();i++) *HBandY(i) /= b;
351
352return *this;
353}
354
355//++
356Histo2D& Histo2D::operator += (double b)
357//
358//--
359{
360int i,j;
361float min,max;
362for(i=0;i<nxy;i++) data[i] += b;
363for(i=0;i<3;i++) for(j=0;j<3;j++) over[i][j] += b;
364nHist += nxy*b;
365
366if(hprojx) *hprojx += b*ny;
367if(hprojy) *hprojy += b*nx;
368if(NSliX()>0) for(i=0; i<NSliX();i++) *HSliX(i) += b*ny/NSliX();
369if(NSliY()>0) for(i=0; i<NSliY();i++) *HSliY(i) += b*nx/NSliY();
370if(NBandX()>0) for(i=0; i<NBandX();i++) {
371 GetBandX(i,min,max);
372 *HBandX(i) += b*(max-min)/(ymax-ymin)*ny;
373}
374if(NBandY()>0) for(i=0; i<NBandY();i++) {
375 GetBandY(i,min,max);
376 *HBandY(i) += b*(max-min)/(xmax-xmin)*nx;
377}
378
379return *this;
380}
381
382//++
383Histo2D& Histo2D::operator -= (double b)
384//
385//--
386{
387int i,j;
388float min,max;
389for(i=0;i<nxy;i++) data[i] -= b;
390for(i=0;i<3;i++) for(j=0;j<3;j++) over[i][j] -= b;
391nHist -= nxy*b;
392
393if(hprojx) *hprojx -= b*ny;
394if(hprojy) *hprojy -= b*nx;
395if(NSliX()>0) for(i=0; i<NSliX();i++) *HSliX(i) -= b*ny/NSliX();
396if(NSliY()>0) for(i=0; i<NSliY();i++) *HSliY(i) -= b*nx/NSliY();
397if(NBandX()>0) for(i=0; i<NBandX();i++) {
398 GetBandX(i,min,max);
399 *HBandX(i) -= b*(max-min)/(ymax-ymin)*ny;
400}
401if(NBandY()>0) for(i=0; i<NBandY();i++) {
402 GetBandY(i,min,max);
403 *HBandY(i) -= b*(max-min)/(xmax-xmin)*nx;
404}
405
406return *this;
407}
408
409///////////////////////////////////////////////////////////////////
410//++
411Histo2D operator * (const Histo2D& a, double b)
412//
413//--
414{
415 Histo2D result(a);
416 return (result *= b);
417}
418
419//++
420Histo2D operator * (double b, const Histo2D& a)
421//
422//--
423{
424 Histo2D result(a);
425 return (result *= b);
426}
427
428//++
429Histo2D operator / (const Histo2D& a, double b)
430//
431//--
432{
433 Histo2D result(a);
434 return (result /= b);
435}
436
437//++
438Histo2D operator + (const Histo2D& a, double b)
439//
440//--
441{
442 Histo2D result(a);
443 return (result += b);
444}
445
446//++
447Histo2D operator + (double b, const Histo2D& a)
448//
449//--
450{
451 Histo2D result(a);
452 return (result += b);
453}
454
455//++
456Histo2D operator - (const Histo2D& a, double b)
457//
458//--
459{
460 Histo2D result(a);
461 return (result -= b);
462}
463
464//++
465Histo2D operator - (double b, const Histo2D& a)
466//
467//--
468{
469 Histo2D result(a);
470 result *= -1.;
471 return (result += b);
472}
473
474///////////////////////////////////////////////////////////////////
475//++
476Histo2D& Histo2D::operator += (const Histo2D& a)
477//
478//--
479{
480int i,j;
481if(nx!=a.nx || ny!=a.ny) THROW(sizeMismatchErr);
482for(i=0;i<nxy;i++) {
483 data[i] += a.data[i];
484 if(err2 && a.err2) err2[i] += a.err2[i];
485}
486for(i=0;i<3;i++) for(j=0;j<3;j++) over[i][j] += a.over[i][j];
487nHist += a.nHist;
488nEntries += a.nEntries;
489
490if(hprojx && a.hprojx) *hprojx += *(a.hprojx);
491if(hprojy && a.hprojy) *hprojy += *(a.hprojy);
492ZeroSliX(); ZeroSliY();
493ZeroBandX(); ZeroBandY();
494
495return *this;
496}
497
498//++
499Histo2D& Histo2D::operator -= (const Histo2D& a)
500//
501//--
502{
503int i,j;
504if(nx!=a.nx || ny!=a.ny) THROW(sizeMismatchErr);
505for(i=0;i<nxy;i++) {
506 data[i] -= a.data[i];
507 if(err2 && a.err2) err2[i] += a.err2[i];
508}
509for(i=0;i<3;i++) for(j=0;j<3;j++) over[i][j] += a.over[i][j];
510nHist -= a.nHist;
511nEntries += a.nEntries;
512
513if(hprojx && a.hprojx) *hprojx -= *(a.hprojx);
514if(hprojy && a.hprojy) *hprojy -= *(a.hprojy);
515ZeroSliX(); ZeroSliY();
516ZeroBandX(); ZeroBandY();
517
518return *this;
519}
520
521//++
522Histo2D& Histo2D::operator *= (const Histo2D& a)
523//
524//--
525{
526int i,j;
527if(nx!=a.nx || ny!=a.ny) THROW(sizeMismatchErr);
528nHist = 0.;
529for(i=0;i<nxy;i++) {
530 if(err2 && a.err2)
531 err2[i] = a.data[i]*a.data[i]*err2[i] + data[i]*data[i]*a.err2[i];
532 data[i] *= a.data[i];
533 nHist += data[i];
534}
535for(i=0;i<3;i++) for(j=0;j<3;j++) over[i][j] *= a.over[i][j];
536nEntries += a.nEntries;
537
538if(hprojx && a.hprojx) *hprojx *= *(a.hprojx);
539if(hprojy && a.hprojy) *hprojy *= *(a.hprojy);
540ZeroSliX(); ZeroSliY();
541ZeroBandX(); ZeroBandY();
542
543return *this;
544}
545
546//++
547Histo2D& Histo2D::operator /= (const Histo2D& a)
548//
549//--
550{
551int i,j;
552if(nx!=a.nx || ny!=a.ny) THROW(sizeMismatchErr);
553nHist = 0.;
554for(i=0;i<nxy;i++) {
555 if(a.data[i]==0.) {
556 data[i]=0.;
557 if(err2) err2[i]=0.;
558 continue;
559 }
560 if(err2 && a.err2)
561 err2[i] = (err2[i] + data[i]/a.data[i]*data[i]/a.data[i]*a.err2[i])
562 /(a.data[i]*a.data[i]);
563 data[i] /= a.data[i];
564 nHist += data[i];
565}
566for(i=0;i<3;i++) for(j=0;j<3;j++)
567 if(a.over[i][j]!=0.) over[i][j] *= a.over[i][j]; else over[i][j] = 0.;
568nEntries += a.nEntries;
569
570if(hprojx && a.hprojx) *hprojx /= *(a.hprojx);
571if(hprojy && a.hprojy) *hprojy /= *(a.hprojy);
572ZeroSliX(); ZeroSliY();
573ZeroBandX(); ZeroBandY();
574
575return *this;
576}
577
578///////////////////////////////////////////////////////////////////
579//++
580Histo2D operator + (const Histo2D& a, const Histo2D& b)
581//
582//--
583{
584if (b.nx!=a.nx || b.ny!=a.ny) THROW(sizeMismatchErr);
585Histo2D c(a);
586return (c += b);
587}
588
589//++
590Histo2D operator - (const Histo2D& a, const Histo2D& b)
591//
592//--
593{
594if (b.nx!=a.nx || b.ny!=a.ny) THROW(sizeMismatchErr);
595Histo2D c(a);
596return (c -= b);
597}
598
599//++
600Histo2D operator * (const Histo2D& a, const Histo2D& b)
601//
602//--
603{
604if (b.nx!=a.nx || b.ny!=a.ny) THROW(sizeMismatchErr);
605Histo2D c(a);
606return (c *= b);
607}
608
609//++
610Histo2D operator / (const Histo2D& a, const Histo2D& b)
611//
612//--
613{
614if (b.nx!=a.nx || b.ny!=a.ny) THROW(sizeMismatchErr);
615Histo2D c(a);
616return (c /= b);
617}
618
619///////////////////////////////////////////////////////////////////
620//++
621void Histo2D::GetXCoor(Vector &v)
622//
623// Remplissage d'un tableau avec les valeurs des abscisses.
624//--
625{
626float x,y;
627v.Realloc(nx);
628for(int i=0;i<nx;i++) {BinLowEdge(i,0,x,y); v(i) = x;}
629return;
630}
631
632//++
633void Histo2D::GetYCoor(Vector &v)
634//
635// Remplissage d'un tableau avec les valeurs des ordonnees.
636//--
637{
638float x,y;
639v.Realloc(ny);
640for(int i=0;i<ny;i++) {BinLowEdge(0,i,x,y); v(i) = y;}
641return;
642}
643
644//++
645//| Remarque sur les indices:
646//| H(i,j) -> i = coord x (0<i<nx), j = coord y (0<j<ny)
647//| v(ii,jj) -> ii = ligne (0<i<NRows()), jj = colonne (0<i<NCol())
648//| On fait une correspondance directe i<->ii et j<->jj
649//| ce qui, en representation classique des histos2D et des matrices
650//| entraine une inversion x<->y cad une symetrie / diagonale principale
651//| H(0,...) represente ^ mais v(0,...) represente
652//| |x....... |xxxxxxxx|
653//| |x....... |........|
654//| |x....... |........|
655//| |x....... |........|
656//| |x....... |........|
657//| --------->
658//| colonne no 1 ligne no 1
659//--
660
661//++
662void Histo2D::GetValue(Matrix &v)
663//
664// Remplissage d'un tableau avec les valeurs du contenu.
665//--
666{
667v.Realloc(nx,ny);
668for(int i=0;i<nx;i++)
669 for(int j=0;j<ny;j++) v(i,j) = (*this)(i,j);
670return;
671}
672
673//++
674void Histo2D::GetError2(Matrix &v)
675//
676// Remplissage d'un tableau avec les valeurs du carre des erreurs.
677//--
678{
679int i,j;
680v.Realloc(nx,ny);
681if(!err2) for(i=0;i<nx;i++)
682 for(j=0;j<ny;j++) { v(i,j) = 0.; return;}
683for(i=0;i<nx;i++)
684 for(j=0;j<ny;j++) v(i,j) = Error2(i,j);
685return;
686}
687
688//++
689void Histo2D::GetError(Matrix &v)
690//
691// Remplissage d'un tableau avec les valeurs des erreurs.
692//--
693{
694int i,j;
695v.Realloc(nx,ny);
696if(!err2) for(i=0;i<nx;i++)
697 for(j=0;j<ny;j++) { v(i,j) = 0.; return;}
698for(i=0;i<nx;i++)
699 for(j=0;j<ny;j++) v(i,j) = Error(i,j);
700return;
701}
702
703///////////////////////////////////////////////////////////////////
704//++
705void Histo2D::PutValue(Matrix &v, int ierr)
706//
707// Remplissage du contenu de l'histo avec les valeurs d'un tableau.
708//--
709{
710int i,j;
711if(v.NRows()!=nx || v.NCol()!=ny) THROW(sizeMismatchErr);
712for(i=0;i<nx;i++) for(j=0;j<ny;j++) {
713 (*this)(i,j) = v(i,j);
714 if(err2 && ierr) Error2(i,j) = fabs(v(i,j));
715}
716return;
717}
718
719//++
720void Histo2D::PutValueAdd(Matrix &v, int ierr)
721//
722// Addition du contenu de l'histo avec les valeurs d'un tableau.
723//--
724{
725int i,j;
726if(v.NRows()!=nx || v.NCol()!=ny) THROW(sizeMismatchErr);
727for(i=0;i<nx;i++) for(j=0;j<ny;j++) {
728 (*this)(i,j) += v(i,j);
729 if(err2 && ierr) Error2(i,j) += fabs(v(i,j));
730}
731return;
732}
733
734//++
735void Histo2D::PutError2(Matrix &v)
736//
737// Remplissage des erreurs au carre de l'histo
738// avec les valeurs d'un tableau.
739//--
740{
741int i,j;
742if(v.NRows()!=nx || v.NCol()!=ny) THROW(sizeMismatchErr);
743if(!err2) Errors();
744for(i=0;i<nx;i++) for(j=0;j<ny;j++) Error2(i,j) = v(i,j);
745return;
746}
747
748//++
749void Histo2D::PutError2Add(Matrix &v)
750//
751// Addition des erreurs au carre de l'histo
752// avec les valeurs d'un tableau.
753//--
754{
755int i,j;
756if(v.NRows()!=nx || v.NCol()!=ny) THROW(sizeMismatchErr);
757if(!err2) Errors();
758for(i=0;i<nx;i++) for(j=0;j<ny;j++)
759 if(v(i,j)>0.) Error2(i,j) += v(i,j);
760return;
761}
762
763//++
764void Histo2D::PutError(Matrix &v)
765//
766// Remplissage des erreurs de l'histo avec les valeurs d'un tableau.
767//--
768{
769int i,j;
770if(v.NRows()!=nx || v.NCol()!=ny) THROW(sizeMismatchErr);
771if(!err2) Errors();
772for(i=0;i<nx;i++) for(j=0;j<ny;j++)
773 if(v(i,j)>0.) Error2(i,j)=v(i,j)*v(i,j); else Error2(i,j)= -v(i,j)*v(i,j);
774return;
775}
776
777///////////////////////////////////////////////////////////////////
778/********* Methode *********/
779//++
780void Histo2D::Add(float x, float y, float w)
781//
782// Addition du contenu de l'histo pour x,y poids w.
783//--
784{
785list<bande_slice>::iterator it;
786int i,j;
787FindBin(x,y,i,j);
788
789if( hprojx != NULL ) hprojx->Add(x,w);
790if( hprojy != NULL ) hprojy->Add(y,w);
791
792if(lbandx.size()>0)
793 for( it = lbandx.begin(); it != lbandx.end(); it++)
794 if( (*it).min <= y && y < (*it).max ) (*it).H->Add(x,w);
795
796if(lbandy.size()>0)
797 for( it = lbandy.begin(); it != lbandy.end(); it++)
798 if( (*it).min <= x && x < (*it).max ) (*it).H->Add(y,w);
799
800if(lslix.size()>0)
801 for( it = lslix.begin(); it != lslix.end(); it++)
802 if( (*it).min <= y && y < (*it).max ) (*it).H->Add(x,w);
803
804if(lsliy.size()>0)
805 for( it = lsliy.begin(); it != lsliy.end(); it++)
806 if( (*it).min <= x && x < (*it).max ) (*it).H->Add(y,w);
807
808if( i<0 || i>=nx || j<0 || j>=ny ) {
809 if(i<0) i=0; else if(i>=nx) i=2; else i=1;
810 if(j<0) j=0; else if(j>=ny) j=2; else j=1;
811 over[i][j] += w;
812 over[1][1] += w;
813 return;
814}
815
816data[j*nx+i] += w;
817if(err2!=NULL) err2[j*nx+i] += w*w;
818nHist += w;
819nEntries++;
820}
821
822///////////////////////////////////////////////////////////////////
823//++
824void Histo2D::IJMax(int& imax,int& jmax,int il,int ih,int jl,int jh)
825//
826// Recherche du bin du maximum dans le pave [il,ih][jl,jh].
827//--
828{
829if( il > ih ) { il = 0; ih = nx-1; }
830if( jl > jh ) { jl = 0; jh = ny-1; }
831if( il < 0 ) il = 0;
832if( jl < 0 ) jl = 0;
833if( ih >= nx ) ih = nx-1;
834if( jh >= ny ) jh = ny-1;
835
836imax = jmax = 0;
837if(nxy==1) return;
838
839float mx=(*this)(il,jl);
840for (int i=il; i<=ih; i++)
841 for (int j=jl; j<=jh; j++)
842 if ((*this)(i,j)>mx) {imax = i; jmax = j; mx=(*this)(i,j);}
843}
844
845//++
846void Histo2D::IJMin(int& imax,int& jmax,int il,int ih,int jl,int jh)
847//
848// Recherche du bin du minimum dans le pave [il,ih][jl,jh].
849//--
850{
851if( il > ih ) { il = 0; ih = nx-1; }
852if( jl > jh ) { jl = 0; jh = ny-1; }
853if( il < 0 ) il = 0;
854if( jl < 0 ) jl = 0;
855if( ih >= nx ) ih = nx-1;
856if( jh >= ny ) jh = ny-1;
857
858imax = jmax = 0;
859if(nxy==1) return;
860
861float mx=(*this)(il,jl);
862for (int i=il; i<=ih; i++)
863 for (int j=jl; j<=jh; j++)
864 if ((*this)(i,j)<mx) {imax = i; jmax = j; mx=(*this)(i,j);}
865}
866
867
868//++
869float Histo2D::VMax(int il,int ih,int jl,int jh) const
870//
871// Recherche du maximum dans le pave [il,ih][jl,jh].
872//--
873{
874if( il > ih ) { il = 0; ih = nx-1; }
875if( jl > jh ) { jl = 0; jh = ny-1; }
876if( il < 0 ) il = 0;
877if( jl < 0 ) jl = 0;
878if( ih >= nx ) ih = nx-1;
879if( jh >= ny ) jh = ny-1;
880
881float mx=(*this)(il,jl);
882if(nxy==1) return mx;
883for (int i=il; i<=ih; i++)
884 for (int j=jl; j<=jh; j++)
885 if ((*this)(i,j)>mx) mx=(*this)(i,j);
886return mx;
887}
888
889//++
890float Histo2D::VMin(int il,int ih,int jl,int jh) const
891//
892// Recherche du minimum dans le pave [il,ih][jl,jh].
893//--
894{
895if( il > ih ) { il = 0; ih = nx-1; }
896if( jl > jh ) { jl = 0; jh = ny-1; }
897if( il < 0 ) il = 0;
898if( jl < 0 ) jl = 0;
899if( ih >= nx ) ih = nx-1;
900if( jh >= ny ) jh = ny-1;
901
902float mx=(*this)(il,jl);
903if(nxy==1) return mx;
904for (int i=il; i<=ih; i++)
905 for (int j=jl; j<=jh; j++)
906 if ((*this)(i,j)<mx) mx=(*this)(i,j);
907return mx;
908}
909
910///////////////////////////////////////////////////////////////////
911//++
912float Histo2D::NOver(int i,int j) const
913//
914// Renvoie les under.overflow dans les 8 quadrants.
915//| over[3][3]: 20 | 21 | 22
916//| | |
917//| --------------
918//| | |
919//| 10 | 11 | 12 11 = all overflow+underflow
920//| | |
921//| --------------
922//| | |
923//| 00 | 01 | 02
924//--
925{
926if( i < 0 || i>=3 || j < 0 || j>=3 ) return over[1][1];
927return over[i][j];
928}
929
930
931///////////////////////////////////////////////////////////////////
932//++
933int Histo2D::BinNonNul() const
934//
935// Retourne le nombre de bins non-nuls.
936//--
937{
938int non=0;
939for (int i=0;i<nxy;i++) if( data[i] != 0. ) non++;
940return non;
941}
942
943//++
944int Histo2D::ErrNonNul() const
945//
946// Retourne le nombre de bins avec erreurs non-nulles.
947//--
948{
949if(err2==NULL) return -1;
950int non=0;
951for (int i=0;i<nxy;i++) if( err2[i] != 0. ) non++;
952return non;
953}
954
955///////////////////////////////////////////////////////////////////
956//++
957int Histo2D::EstimeMax(float& xm,float& ym,int SzPav
958 ,int il,int ih,int jl,int jh)
959//
960// Idem EstimeMax(int...) mais retourne x,y.
961//--
962{
963int im,jm;
964IJMax(im,jm,il,ih,jl,jh);
965return EstimeMax(im,jm,xm,ym,SzPav);
966}
967
968//++
969int Histo2D::EstimeMax(int im,int jm,float& xm,float& ym,int SzPav)
970//
971// Determine les abscisses et ordonnees du maximum donne par im,jm
972// en moyennant dans un pave SzPav x SzPav autour du maximum.
973//| Return:
974//| 0 = si fit maximum reussi avec SzPav pixels
975//| 1 = si fit maximum reussi avec moins que SzPav pixels
976//| dans au moins 1 direction
977//| 2 = si fit maximum echoue et renvoit BinCenter()
978//| -1 = si echec: SzPav <= 0 ou im,jm hors limites
979//--
980{
981xm = ym = 0;
982if( SzPav <= 0 ) return -1;
983if( im < 0 || im >= nx ) return -1;
984if( jm < 0 || jm >= ny ) return -1;
985
986if( SzPav%2 == 0 ) SzPav++;
987SzPav = (SzPav-1)/2;
988
989int rc = 0;
990double dxm = 0, dym = 0, wx = 0;
991for(int i=im-SzPav;i<=im+SzPav;i++) {
992 if( i<0 || i>= nx ) {rc=1; continue;}
993 for(int j=jm-SzPav;j<=jm+SzPav;j++) {
994 if( j<0 || j>= ny ) {rc=1; continue;}
995 float x,y;
996 BinCenter(i,j,x,y);
997 dxm += x * (*this)(i,j);
998 dym += y * (*this)(i,j);
999 wx += (*this)(i,j);
1000 }
1001}
1002
1003if( wx > 0. ) {
1004 xm = dxm/wx;
1005 ym = dym/wx;
1006 return rc;
1007} else {
1008 BinCenter(im,jm,xm,ym);
1009 return 2;
1010}
1011
1012}
1013
1014//++
1015int Histo2D::FindMax(int& im,int& jm,int SzPav,float Dz
1016 ,int il,int ih,int jl,int jh)
1017//
1018// Pour trouver le maximum de l'histogramme en tenant compte
1019// des fluctuations.
1020//| Methode:
1021//| 1-/ On recherche le bin maximum MAX de l'histogramme
1022//| 2-/ On considere que tous les pixels compris entre [MAX-Dz,MAX]
1023//| peuvent etre des pixels maxima.
1024//| 3-/ On identifie le bin maximum en choissisant le pixel du 2-/
1025//| tel que la somme des pixels dans un pave SzPav x SzPav soit maximale.
1026//| INPUT:
1027//| SzPav = taille du pave pour departager
1028//| Dz = tolerance pour identifier tous les pixels "maximum"
1029//| OUTPUT:
1030//| im,jm = pixel maximum trouve
1031//| RETURN:
1032//| <0 = Echec
1033//| >0 = nombre de pixels possibles pour le maximum
1034//--
1035{
1036if( il > ih ) { il = 0; ih = nx-1; }
1037if( jl > jh ) { jl = 0; jh = ny-1; }
1038if( il < 0 ) il = 0;
1039if( jl < 0 ) jl = 0;
1040if( ih >= nx ) ih = nx-1;
1041if( jh >= ny ) jh = ny-1;
1042if( SzPav < 0 ) SzPav = 0;
1043 else { if( SzPav%2 == 0 ) SzPav++; SzPav = (SzPav-1)/2;}
1044if( Dz < 0 ) Dz = 0.;
1045float max = VMax(il,ih,jl,jh) - Dz;
1046int nmax = 0;
1047float sumx = -MAXFLOAT;
1048for(int i=il;i<=ih;i++) for(int j=jl;j<=jh;j++) {
1049 if( (*this)(i,j) < max) continue;
1050 nmax++;
1051 float sum = 0.;
1052 for(int ii=i-SzPav;ii<=i+SzPav;ii++) {
1053 if( ii<0 || ii >= nx ) continue;
1054 for(int jj=j-SzPav;jj<=j+SzPav;jj++) {
1055 if( jj<0 || jj >= ny ) continue;
1056 sum += (*this)(ii,jj);
1057 }
1058 }
1059 if( sum > sumx ) { im = i; jm = j; sumx = sum;}
1060}
1061if( nmax <= 0 ) { IJMax(im,jm,il,ih,jl,jh); return 1;}
1062return nmax;
1063}
1064
1065//////////////////////////////////////////////////////////
1066
1067//////////////////////////////////////////////////////////
1068//++
1069int Histo2D::Fit(GeneralFit& gfit,unsigned short typ_err)
1070//
1071// Fit de l'histogramme par ``gfit''.
1072//| typ_err = 0 :
1073//| - erreur attachee au bin si elle existe
1074//| - sinon 1
1075//| typ_err = 1 :
1076//| - erreur attachee au bin si elle existe
1077//| - sinon max( sqrt(abs(bin) ,1 )
1078//| typ_err = 2 :
1079//| - erreur forcee a 1
1080//| typ_err = 3 :
1081//| - erreur forcee a max( sqrt(abs(bin) ,1 )
1082//| typ_err = 4 :
1083//| - erreur forcee a 1, nulle si bin a zero.
1084//| typ_err = 5 :
1085//| - erreur forcee a max( sqrt(abs(bin) ,1 ),
1086//| nulle si bin a zero.
1087//--
1088{
1089if(NBinX()*NBinY()<=0) return -1000;
1090if(typ_err>5) typ_err=0;
1091
1092GeneralFitData mydata(2,NBinX()*NBinY());
1093
1094for(int i=0;i<NBinX();i++) for(int j=0;j<NBinY();j++) {
1095 float x,y;
1096 BinCenter(i,j,x,y);
1097 double f = (double) (*this)(i,j);
1098 double saf = sqrt(fabs(f)); if(saf<1.) saf=1.;
1099 double e;
1100 if(typ_err==0) {if(HasErrors()) e=Error(i,j); else e=1.;}
1101 else if(typ_err==1) {if(HasErrors()) e=Error(i,j); else e=saf;}
1102 else if(typ_err==2) e=1.;
1103 else if(typ_err==3) e=saf;
1104 else if(typ_err==4) e=(f==0.)?0.:1.;
1105 else if(typ_err==5) e=(f==0.)?0.:saf;
1106 mydata.AddData2((double) x,(double) y,f,e);
1107}
1108
1109gfit.SetData(&mydata);
1110
1111return gfit.Fit();
1112}
1113
1114//++
1115Histo2D* Histo2D::FitResidus(GeneralFit& gfit)
1116//
1117// Retourne une classe contenant les residus du fit ``gfit''.
1118//--
1119{
1120if(NBinX()<=0 || NBinY()<=0) return NULL;
1121GeneralFunction* f = gfit.GetFunction();
1122if(f==NULL) return NULL;
1123Vector par = gfit.GetParm();
1124Histo2D* h2 = new Histo2D(*this);
1125for(int i=0;i<NBinX();i++) for(int j=0;j<NBinY();j++) {
1126 float xc,yc;
1127 BinCenter(i,j,xc,yc);
1128 double x[2] = {(double)xc,(double)yc};
1129 (*h2)(i,j) -= (float) f->Value(x,par.Data());
1130}
1131return h2;
1132}
1133
1134//++
1135Histo2D* Histo2D::FitFunction(GeneralFit& gfit)
1136//
1137// Retourne une classe contenant la fonction du fit ``gfit''.
1138//--
1139{
1140if(NBinX()<=0 || NBinY()<=0) return NULL;
1141GeneralFunction* f = gfit.GetFunction();
1142if(f==NULL) return NULL;
1143Vector par = gfit.GetParm();
1144Histo2D* h2 = new Histo2D(*this);
1145for(int i=0;i<NBinX();i++) for(int j=0;j<NBinY();j++) {
1146 float xc,yc;
1147 BinCenter(i,j,xc,yc);
1148 double x[2] = {(double)xc,(double)yc};
1149 (*h2)(i,j) = (float) f->Value(x,par.Data());
1150}
1151return h2;
1152}
1153
1154///////////////////////////////////////////////////////////////////
1155//++
1156void Histo2D::PrintStatus()
1157//
1158// Impression des informations sur l'histogramme.
1159//--
1160{
1161printf("~Histo::Print nHist=%g nEntries=%d\n",nHist,nEntries);
1162printf("over: [ %g %g %g // %g %g %g // %g %g %g ]\n"
1163 ,over[2][0],over[2][1],over[2][2]
1164 ,over[1][0],over[1][1],over[1][2]
1165 ,over[0][0],over[0][1],over[0][2]);
1166printf(" nx=%d xmin=%g xmax=%g binx=%g ",nx,xmin,xmax,wbinx);
1167printf(" ny=%d ymin=%g ymax=%g biny=%g\n",ny,ymin,ymax,wbiny);
1168}
1169
1170///////////////////////////////////////////////////////////////////
1171//++
1172void Histo2D::Print(float min,float max
1173 ,int il,int ih,int jl,int jh)
1174//
1175// Impression de l'histogramme sur stdout entre [il,ih] et [jl,jh].
1176//--
1177{
1178int ns = 35;
1179// numero d'index: 00000000001111111111222222222233333
1180// 01234567890123456789012345678901234
1181// valeur entiere: 00000000001111111111222222222233333
1182// 12345678901234567890123456789012345
1183const char *s = "+23456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1184
1185if( il > ih ) { il = 0; ih = nx-1; }
1186if( jl > jh ) { jl = 0; jh = ny-1; }
1187if( il < 0 ) il = 0;
1188if( jl < 0 ) jl = 0;
1189if( ih >= nx ) ih = nx-1;
1190if( jh >= ny ) jh = ny-1;
1191
1192PrintStatus();
1193
1194if( il != 0 || ih != nx-1 || jl != 0 || jh != ny-1 ) {
1195 float xl,xh,yl,yh;
1196 BinLowEdge(il,jl,xl,yl);
1197 BinHighEdge(ih,jh,xh,yh);
1198 printf(" impression");
1199 printf(" en X: %d=[%d,%d] xmin=%g xmax=%g "
1200 ,ih-il+1,il,ih,xl,xh);
1201 printf(" en Y: %d=[%d,%d] ymin=%g ymax=%g\n"
1202 ,jh-jl+1,jl,jh,yl,yh);
1203}
1204
1205if(min >= max) { if(min != 0.) min = VMin(il,ih,jl,jh); else min=0.;
1206 max = VMax(il,ih,jl,jh); }
1207if(min>max) return;
1208if(min==max) {min -= 1.; max += 1.;}
1209printf(" min=%g max=%g\n",min,max);
1210
1211// imprime numero de bin en colonne
1212printf("\n");
1213if( nx-1 >= 100 ) {
1214 printf(" ");
1215 for(int i=il;i<=ih;i++) printf("%1d",(int) (i%1000)/100);
1216 printf("\n");
1217}
1218if( nx-1 >= 10 ) {
1219 printf(" ");
1220 for(int i=il;i<=ih;i++) printf("%1d",(int) (i%100)/10);
1221 printf("\n");
1222}
1223printf(" ");
1224for(int i=il;i<=ih;i++) printf("%1d",i%10);
1225printf("\n");
1226printf(" "); {for(int i=il;i<=ih;i++) printf("-"); printf("\n");}
1227
1228// imprime histogramme
1229for(int j=jh;j>=jl;j--) {
1230 printf("%3d: ",j);
1231 for(int i=il;i<=ih;i++) {
1232 int h;
1233 if( 1<=max-min && max-min<=35 ) h = (int)( (*this)(i,j) - min ) - 1;
1234 else h = (int)( ((*this)(i,j)-min)/(max-min) * ns ) - 1;
1235 char c;
1236 if(h<0 && (*this)(i,j)>min) c = '.';
1237 else if(h<0) c = ' ';
1238 else if(h>=ns) c = '*';
1239 else c = s[h];
1240 printf("%c",c);
1241 }
1242 printf("\n");
1243}
1244
1245// imprime numero de bin en colonne
1246printf(" "); {for(int i=il;i<=ih;i++) printf("-"); printf("\n");}
1247if( nx-1 >= 100 ) {
1248 printf(" ");
1249 for(int i=il;i<=ih;i++) printf("%1d",(int) (i%1000)/100);
1250 printf("\n");
1251}
1252if( nx-1 >= 10 ) {
1253 printf(" ");
1254 for(int i=il;i<=ih;i++) printf("%1d",(int) (i%100)/10);
1255 printf("\n");
1256}
1257printf(" ");
1258{for(int i=il;i<=ih;i++) printf("%1d",i%10);}
1259printf("\n");
1260
1261}
1262
1263// Rappel des inline functions pour commentaires
1264//++
1265// inline float XMin()
1266// Retourne l'abscisse minimum.
1267//--
1268//++
1269// inline float XMax()
1270// Retourne l'abscisse maximum.
1271//--
1272//++
1273// inline float YMin()
1274// Retourne l'ordonnee minimum.
1275//--
1276//++
1277// inline float YMax()
1278// Retourne l'ordonnee maximum.
1279//--
1280//++
1281// inline int NBinX()
1282// Retourne le nombre de bins selon X.
1283//--
1284//++
1285// inline int NBinY()
1286// Retourne le nombre de bins selon Y.
1287//--
1288//++
1289// inline float WBinX()
1290// Retourne la largeur du bin selon X.
1291//--
1292//++
1293// inline float WBinY()
1294// Retourne la largeur du bin selon Y.
1295//--
1296//++
1297// inline float* Bins() const
1298// Retourne le pointeur sur le tableaux des contenus.
1299//--
1300//++
1301// inline float operator()(int i,int j) const
1302// Retourne le contenu du bin i,j.
1303//--
1304//++
1305// inline float& operator()(int i,int j)
1306// Remplit le contenu du bin i,j.
1307//--
1308//++
1309// inline float Error(int i,int j) const
1310// Retourne l'erreur du bin i,j.
1311//--
1312//++
1313// inline double Error2(int i,int j) const
1314// Retourne l'erreur au carre du bin i,j.
1315//--
1316//++
1317// inline double& Error2(int i,int j)
1318// Remplit l'erreur au carre du bin i,j.
1319//--
1320//++
1321// inline float NData() const
1322// Retourne la somme ponderee.
1323//--
1324//++
1325// inline int NEntries() const
1326// Retourne le nombre d'entrees.
1327//--
1328//++
1329// inline void BinLowEdge(int i,int j,float& x,float& y)
1330// Retourne l'abscisse et l'ordonnee du coin inferieur du bin i,j.
1331//--
1332//++
1333// inline void BinCenter(int i,int j,float& x,float& y)
1334// Retourne l'abscisse et l'ordonnee du centre du bin i,j.
1335//--
1336//++
1337// inline void BinHighEdge(int i,int j,float& x,float& y)
1338// Retourne l'abscisse et l'ordonnee du coin superieur du bin i,j.
1339//--
1340//++
1341// inline void FindBin(float x,float y,int& i,int& j)
1342// Retourne les numeros du bin contenant l'abscisse et l'ordonnee x,y.
1343//--
1344
1345///////////////////////////////////////////////////////////////////
1346//++
1347// Titre Methodes pour gerer les projections
1348//--
1349
1350//++
1351void Histo2D::SetProjX()
1352//
1353// Pour creer la projection X.
1354//--
1355{
1356if( hprojx != NULL ) DelProjX();
1357hprojx = new Histo(xmin,xmax,nx);
1358if( err2 != NULL && hprojx != NULL ) hprojx->Errors();
1359}
1360
1361//++
1362void Histo2D::SetProjY()
1363//
1364// Pour creer la projection Y.
1365//--
1366{
1367if( hprojy != NULL ) DelProjY();
1368hprojy = new Histo(ymin,ymax,ny);
1369if( err2 != NULL && hprojy != NULL ) hprojy->Errors();
1370}
1371
1372//++
1373void Histo2D::SetProj()
1374//
1375// Pour creer les projections X et Y.
1376//--
1377{
1378SetProjX();
1379SetProjY();
1380}
1381
1382//++
1383void Histo2D::ShowProj()
1384//
1385// Informations sur les projections.
1386//--
1387{
1388if( hprojx != NULL ) cout << ">>>> Projection X set : "<< hprojx <<endl;
1389 else cout << ">>>> NO Projection X set"<<endl;
1390if( hprojy != NULL ) cout << ">>>> Projection Y set : "<< hprojy <<endl;
1391 else cout << ">>>> NO Projection Y set"<<endl;
1392}
1393
1394//++
1395void Histo2D::DelProjX()
1396//
1397// Destruction de l'histogramme de la projection selon X.
1398//--
1399{
1400if( hprojx == NULL ) return;
1401delete hprojx;
1402hprojx = NULL;
1403}
1404
1405//++
1406void Histo2D::DelProjY()
1407//
1408// Destruction de l'histogramme de la projection selon X.
1409//--
1410{
1411if( hprojy == NULL ) return;
1412delete hprojy;
1413hprojy = NULL;
1414}
1415
1416//++
1417void Histo2D::DelProj()
1418//
1419// Destruction des histogrammes des projections selon X et Y.
1420//--
1421{
1422DelProjX();
1423DelProjY();
1424}
1425
1426//++
1427void Histo2D::ZeroProjX()
1428//
1429// Remise a zero de la projection selon X.
1430//--
1431{
1432if( hprojx == NULL ) return;
1433hprojx->Zero();
1434}
1435
1436//++
1437void Histo2D::ZeroProjY()
1438//
1439// Remise a zero de la projection selon Y.
1440//--
1441{
1442if( hprojy == NULL ) return;
1443hprojy->Zero();
1444}
1445
1446//++
1447void Histo2D::ZeroProj()
1448//
1449// Remise a zero des projections selon X et Y.
1450//--
1451{
1452ZeroProjX();
1453ZeroProjY();
1454}
1455
1456// Rappel des inline functions pour commentaires
1457//++
1458// inline Histo* HProjX()
1459// Retourne le pointeur sur l'histo 1D de la projection selon X.
1460//--
1461//++
1462// inline Histo* HProjY()
1463// Retourne le pointeur sur l'histo 1D de la projection selon Y.
1464//--
1465
1466///////////////////////////////////////////////////////////////////
1467//++
1468// Titre Methodes pour gerer les bandes
1469//--
1470
1471//++
1472int Histo2D::SetBandX(float ybmin,float ybmax)
1473//
1474// Pour creer une bande en X entre ybmin et ybmax.
1475//--
1476{
1477b_s.num = lbandx.size();
1478b_s.min = ybmin;
1479b_s.max = ybmax;
1480b_s.H = new Histo(xmin,xmax,nx);
1481lbandx.push_back(b_s);
1482b_s.H = NULL;
1483return lbandx.size()-1;
1484}
1485
1486//++
1487int Histo2D::SetBandY(float xbmin,float xbmax)
1488//
1489// Pour creer une bande en Y entre xbmin et xbmax.
1490//--
1491{
1492b_s.num = lbandy.size();
1493b_s.min = xbmin;
1494b_s.max = xbmax;
1495b_s.H = new Histo(ymin,ymax,ny);
1496lbandy.push_back(b_s);
1497b_s.H = NULL;
1498return lbandy.size()-1;
1499}
1500
1501//++
1502void Histo2D::DelBandX()
1503//
1504// Destruction des histogrammes des bandes selon X.
1505//--
1506{
1507if( lbandx.size() <= 0 ) return;
1508for(list<bande_slice>::iterator i = lbandx.begin(); i != lbandx.end(); i++)
1509 if( (*i).H != NULL ) {delete (*i).H; (*i).H=NULL;}
1510lbandx.erase(lbandx.begin(),lbandx.end());
1511}
1512
1513//++
1514void Histo2D::DelBandY()
1515//
1516// Destruction des histogrammes des bandes selon Y.
1517//--
1518{
1519if( lbandy.size() <= 0 ) return;
1520for(list<bande_slice>::iterator i = lbandy.begin(); i != lbandy.end(); i++)
1521 if( (*i).H != NULL ) {delete (*i).H; (*i).H=NULL;}
1522lbandy.erase(lbandy.begin(),lbandy.end());
1523}
1524
1525//++
1526void Histo2D::ZeroBandX()
1527//
1528// Remise a zero des bandes selon X.
1529//--
1530{
1531if( lbandx.size() <= 0 ) return;
1532list<bande_slice>::iterator i;
1533for(i = lbandx.begin(); i != lbandx.end(); i++)
1534 (*i).H->Zero();
1535}
1536
1537//++
1538void Histo2D::ZeroBandY()
1539//
1540// Remise a zero des bandes selon Y.
1541//--
1542{
1543if( lbandy.size() <= 0 ) return;
1544list<bande_slice>::iterator i;
1545for(i = lbandy.begin(); i != lbandy.end(); i++)
1546 (*i).H->Zero();
1547}
1548
1549//++
1550Histo* Histo2D::HBandX(int n) const
1551//
1552// Retourne un pointeur sur la bande numero `n' selon X.
1553//--
1554{
1555if( lbandx.size() <= 0 || n < 0 || n >= (int) lbandx.size() ) return NULL;
1556for(list<bande_slice>::const_iterator i = lbandx.begin(); i != lbandx.end(); i++)
1557 if( (*i).num == n ) return (*i).H;
1558return NULL;
1559}
1560
1561//++
1562Histo* Histo2D::HBandY(int n) const
1563//
1564// Retourne un pointeur sur la bande numero `n' selon Y.
1565//--
1566{
1567if( lbandy.size() <= 0 || n < 0 || n >= (int) lbandy.size() ) return NULL;
1568for(list<bande_slice>::const_iterator i = lbandy.begin(); i != lbandy.end(); i++)
1569 if( (*i).num == n ) return (*i).H;
1570return NULL;
1571}
1572
1573//++
1574void Histo2D::GetBandX(int n,float& ybmin,float& ybmax) const
1575//
1576// Retourne les limites de la bande numero `n' selon X.
1577//--
1578{
1579ybmin = 0.; ybmax = 0.;
1580if( lbandx.size() <= 0 || n < 0 || n >= (int) lbandx.size() ) return;
1581for(list<bande_slice>::const_iterator i = lbandx.begin(); i != lbandx.end(); i++)
1582 if( (*i).num == n ) { ybmin = (*i).min; ybmax = (*i).max; return;}
1583return;
1584}
1585
1586//++
1587void Histo2D::GetBandY(int n,float& xbmin,float& xbmax) const
1588//
1589// Retourne les limites de la bande numero `n' selon Y.
1590//--
1591{
1592xbmin = 0.; xbmax = 0.;
1593if( lbandy.size() <= 0 || n < 0 || n >= (int) lbandy.size() ) return;
1594for(list<bande_slice>::const_iterator i = lbandy.begin(); i != lbandy.end(); i++)
1595 if( (*i).num == n ) { xbmin = (*i).min; xbmax = (*i).max; return;}
1596return;
1597}
1598
1599//++
1600void Histo2D::ShowBand(int lp)
1601//
1602// Informations sur les bandes.
1603//--
1604{
1605 cout << ">>>> Nombre de bande X : " << lbandx.size() << endl;
1606if( lp>0 && lbandx.size()>0 ) {
1607 list<bande_slice>::iterator i;
1608 for(i = lbandx.begin(); i != lbandx.end(); i++) {
1609 cout<<" "<<(*i).num<<" de ymin="<<(*i).min<<" a ymax="<<(*i).max;
1610 if(lp>1) cout << " H=" << (*i).H;
1611 cout << endl;
1612 }
1613}
1614
1615cout << ">>>> Nombre de bande Y : " << lbandy.size() << endl;
1616if( lp>0 && lbandy.size()>0 ) {
1617 list<bande_slice>::iterator i;
1618 for(i = lbandy.begin(); i != lbandy.end(); i++) {
1619 cout<<" "<<(*i).num<<" de xmin="<<(*i).min<<" a xmax="<<(*i).max;
1620 if(lp>1) cout << " H=" << (*i).H;
1621 cout << endl;
1622 }
1623}
1624}
1625
1626// Rappel des inline functions pour commentaires
1627//++
1628// inline int NBandX()
1629// Retourne le nombre de bandes selon X
1630//--
1631//++
1632// inline int NBandY()
1633// Retourne le nombre de bandes selon Y
1634//--
1635
1636///////////////////////////////////////////////////////////////////
1637//++
1638// Titre Methodes pour gerer les bandes equidistantes
1639//--
1640
1641//++
1642int Histo2D::SetSliX(int nsli)
1643//
1644// Pour creer `nsli' bandes equidistantes selon X.
1645//--
1646{
1647if( nsli <= 0 ) return -1;
1648if( nsli > ny ) nsli = ny;
1649if( lslix.size() > 0 ) DelSliX();
1650float w = (ymax-ymin)/nsli;
1651
1652for(int i=0; i<nsli; i++ ) {
1653 b_s.num = i;
1654 b_s.min = ymin + i*w;
1655 b_s.max = b_s.min + w;
1656 b_s.H = new Histo(xmin,xmax,nx);
1657 lslix.push_back(b_s);
1658 b_s.H = NULL;
1659}
1660return (int) lslix.size();
1661}
1662
1663//++
1664int Histo2D::SetSliY(int nsli)
1665//
1666// Pour creer `nsli' bandes equidistantes selon Y.
1667//--
1668{
1669if( nsli <= 0 ) return -1;
1670if( nsli > nx ) nsli = nx;
1671if( lsliy.size() > 0 ) DelSliY();
1672float w = (xmax-xmin)/nsli;
1673
1674for(int i=0; i<nsli; i++ ) {
1675 b_s.num = i;
1676 b_s.min = xmin + i*w;
1677 b_s.max = b_s.min + w;
1678 b_s.H = new Histo(ymin,ymax,ny);
1679 lsliy.push_back(b_s);
1680 b_s.H = NULL;
1681}
1682return (int) lsliy.size();
1683}
1684
1685//++
1686void Histo2D::DelSliX()
1687//
1688// Destruction des bandes equidistantes selon X.
1689//--
1690{
1691if( lslix.size() <= 0 ) return;
1692for(list<bande_slice>::iterator i = lslix.begin(); i != lslix.end(); i++)
1693 if( (*i).H != NULL ) {delete (*i).H; (*i).H=NULL;}
1694lslix.erase(lslix.begin(),lslix.end());
1695}
1696
1697//++
1698void Histo2D::DelSliY()
1699//
1700// Destruction des bandes equidistantes selon Y.
1701//--
1702{
1703if( lsliy.size() <= 0 ) return;
1704for(list<bande_slice>::iterator i = lsliy.begin(); i != lsliy.end(); i++)
1705 if( (*i).H != NULL ) {delete (*i).H; (*i).H=NULL;}
1706lsliy.erase(lsliy.begin(),lsliy.end());
1707}
1708
1709//++
1710void Histo2D::ZeroSliX()
1711//
1712// Remise a zero des bandes equidistantes selon X.
1713//--
1714{
1715if( lslix.size() <= 0 ) return;
1716list<bande_slice>::iterator i;
1717for(i = lslix.begin(); i != lslix.end(); i++)
1718 (*i).H->Zero();
1719}
1720
1721//++
1722void Histo2D::ZeroSliY()
1723//
1724// Remise a zero des bandes equidistantes selon Y.
1725//--
1726{
1727if( lsliy.size() <= 0 ) return;
1728list<bande_slice>::iterator i;
1729for(i = lsliy.begin(); i != lsliy.end(); i++)
1730 (*i).H->Zero();
1731}
1732
1733//++
1734Histo* Histo2D::HSliX(int n) const
1735//
1736// Retourne un pointeur sur la bande equidistante numero `n'
1737// selon X.
1738//--
1739{
1740if( lslix.size() <= 0 || n < 0 || n >= (int) lslix.size() ) return NULL;
1741for(list<bande_slice>::const_iterator i = lslix.begin(); i != lslix.end(); i++)
1742 if( (*i).num == n ) return (*i).H;
1743return NULL;
1744}
1745
1746//++
1747Histo* Histo2D::HSliY(int n) const
1748//
1749// Retourne un pointeur sur la bande equidistante numero `n'
1750// selon Y.
1751//--
1752{
1753if( lsliy.size() <= 0 || n < 0 || n >= (int) lsliy.size() ) return NULL;
1754for(list<bande_slice>::const_iterator i = lsliy.begin(); i != lsliy.end(); i++)
1755 if( (*i).num == n ) return (*i).H;
1756return NULL;
1757}
1758
1759//++
1760void Histo2D::ShowSli(int lp)
1761//
1762// Informations sur les bandes equidistantes.
1763//--
1764{
1765list<bande_slice>::iterator i;
1766cout << ">>>> Nombre de slice X : " << lslix.size() << endl;
1767if( lp>0 && lslix.size() > 0 )
1768 for(i = lslix.begin(); i != lslix.end(); i++) {
1769 cout<<" "<<(*i).num<<" de ymin="<<(*i).min<<" a ymax="<<(*i).max;
1770 if(lp>1) cout << " H=" << (*i).H;
1771 cout << endl;
1772 }
1773
1774cout << ">>>> Nombre de slice Y : " << lsliy.size() << endl;
1775if( lp>0 && lsliy.size()>0 )
1776 for(i = lsliy.begin(); i != lsliy.end(); i++) {
1777 cout<<" "<<(*i).num<<" de xmin="<<(*i).min<<" a xmax="<<(*i).max;
1778 if(lp>1) cout << " H=" << (*i).H;
1779 cout << endl;
1780 }
1781}
1782
1783// Rappel des inline functions pour commentaires
1784//++
1785// inline int NSliX()
1786// Retourne le nombre de slices selon X
1787//--
1788//++
1789// inline int NSliY()
1790// Retourne le nombre de slices selon Y
1791//--
1792
1793
1794///////////////////////////////////////////////////////////////////
1795//++
1796// Titre Methodes pour ecriture ppersist
1797//--
1798
1799//++
1800void Histo2D::WriteSelf(POutPersist& s) const
1801//
1802// Ecriture fichier de type ppersist.
1803//--
1804{
1805char strg[256];
1806int j;
1807float min,max;
1808Histo* h;
1809
1810// Que faut-il ecrire?
1811int_4 errok = (err2) ? 1 : 0;
1812int_4 projx = (hprojx) ? 1 : 0;
1813int_4 projy = (hprojy) ? 1 : 0;
1814int_4 nslix = NSliX();
1815int_4 nsliy = NSliY();
1816int_4 nbanx = NBandX();
1817int_4 nbany = NBandY();
1818
1819// Ecriture entete pour identifier facilement
1820sprintf(strg,"nx=%d ny=%d nxy=%d errok=%1d",nx,ny,nxy,errok);
1821s.PutLine(strg);
1822sprintf(strg,"nHist=%g nEntries=%d",nHist,nEntries);
1823s.PutLine(strg);
1824sprintf(strg,"wbinx=%g wbiny=%g",wbinx,wbiny);
1825s.PutLine(strg);
1826sprintf(strg,"xmin=%g xmax=%g ymin=%g ymax=%g",xmin,xmax,ymin,ymax);
1827s.PutLine(strg);
1828sprintf(strg,"projx/y=%d %d nbandx/y=%d %d nbslix/y=%d %d"
1829 ,projx,projy,nbanx,nbany,nslix,nsliy);
1830s.PutLine(strg);
1831sprintf(strg,"over %g %g %g %g %g %g %g %g %g"
1832 ,over[0][0],over[0][1],over[0][2]
1833 ,over[1][0],over[1][1],over[1][2]
1834 ,over[2][0],over[2][1],over[2][2]);
1835s.PutLine(strg);
1836
1837// Ecriture variables de definitions
1838s.PutI4(nx);
1839s.PutI4(ny);
1840s.PutI4(nxy);
1841s.PutI4(errok);
1842s.PutI4(nEntries);
1843s.PutR8(nHist);
1844
1845s.PutR4(xmin);
1846s.PutR4(xmax);
1847s.PutR4(ymin);
1848s.PutR4(ymax);
1849s.PutR4(wbinx);
1850s.PutR4(wbiny);
1851
1852s.PutR4s(&over[0][0],9);
1853
1854s.PutI4(projx);
1855s.PutI4(projy);
1856s.PutI4(nslix);
1857s.PutI4(nsliy);
1858s.PutI4(nbanx);
1859s.PutI4(nbany);
1860
1861// Ecriture histo2D
1862sprintf(strg,"Histo2D: Tableau des donnees %d = %d * %d",nxy,nx,ny);
1863s.PutLine(strg);
1864for(j=0;j<ny;j++) s.PutR4s(data+j*nx,nx);
1865
1866
1867// Ecriture erreurs
1868if(errok) {
1869 sprintf(strg,"Histo2D: Tableau des erreurs %d = %d * %d",nxy,nx,ny);
1870 s.PutLine(strg);
1871 for(j=0;j<ny;j++) s.PutR8s(err2+j*nx,nx);
1872}
1873
1874// Ecriture des projections
1875if(projx) {
1876 sprintf(strg,"Histo2D: Projection X");
1877 s.PutLine(strg);
1878 hprojx->Write(s);
1879}
1880if(projy) {
1881 sprintf(strg,"Histo2D: Projection Y");
1882 s.PutLine(strg);
1883 hprojy->Write(s);
1884}
1885
1886// Ecriture des slices
1887if(nslix>0) {
1888 sprintf(strg,"Histo2D: Slices X %d",nslix);
1889 s.PutLine(strg);
1890 for(j=0;j<nslix;j++) {
1891 h = HSliX(j);
1892 h->Write(s);
1893 }
1894}
1895if(nsliy>0) {
1896 sprintf(strg,"Histo2D: Slices Y %d",nsliy);
1897 s.PutLine(strg);
1898 for(j=0;j<nsliy;j++) {
1899 h = HSliY(j);
1900 h->Write(s);
1901 }
1902}
1903
1904// Ecriture des bandes
1905if( nbanx>0 ) {
1906 sprintf(strg,"Histo2D: Bandes X %d",nbanx);
1907 s.PutLine(strg);
1908 list<bande_slice>::const_iterator it;
1909 for(it = lbandx.begin(); it != lbandx.end(); it++) {
1910 min = (*it).min;
1911 max = (*it).max;
1912 s.PutR4(min);
1913 s.PutR4(max);
1914 }
1915 for(it = lbandx.begin(); it != lbandx.end(); it++) {
1916 h = (*it).H;
1917 h->Write(s);
1918 }
1919}
1920if( nbany>0 ) {
1921 sprintf(strg,"Histo2D: Bandes Y %d",nbany);
1922 s.PutLine(strg);
1923 list<bande_slice>::const_iterator it;
1924 for(it = lbandy.begin(); it != lbandy.end(); it++) {
1925 min = (*it).min;
1926 max = (*it).max;
1927 s.PutR4(min);
1928 s.PutR4(max);
1929 }
1930 for(it = lbandy.begin(); it != lbandy.end(); it++) {
1931 h = (*it).H;
1932 h->Write(s);
1933 }
1934}
1935
1936return;
1937}
1938
1939//++
1940void Histo2D::ReadSelf(PInPersist& s)
1941//
1942// Lecture fichier de type ppersist.
1943//--
1944{
1945Delete();
1946
1947int j;
1948float min,max;
1949Histo* h;
1950char strg[256];
1951int_4 errok, projx, projy, nslix, nsliy, nbanx, nbany;
1952
1953// Lecture entete
1954s.GetLine(strg, 255);
1955s.GetLine(strg, 255);
1956s.GetLine(strg, 255);
1957s.GetLine(strg, 255);
1958s.GetLine(strg, 255);
1959s.GetLine(strg, 255);
1960
1961
1962// Lecture variables de definitions
1963s.GetI4(nx);
1964s.GetI4(ny);
1965s.GetI4(nxy);
1966s.GetI4(errok);
1967s.GetI4(nEntries);
1968s.GetR8(nHist);
1969
1970s.GetR4(xmin);
1971s.GetR4(xmax);
1972s.GetR4(ymin);
1973s.GetR4(ymax);
1974s.GetR4(wbinx);
1975s.GetR4(wbiny);
1976
1977s.GetR4s(&over[0][0],9);
1978
1979s.GetI4(projx);
1980s.GetI4(projy);
1981s.GetI4(nslix);
1982s.GetI4(nsliy);
1983s.GetI4(nbanx);
1984s.GetI4(nbany);
1985
1986// Lecture histo2D
1987data = new float[nxy];
1988s.GetLine(strg, 255);
1989for(j=0;j<ny;j++) s.GetR4s(data+j*nx,nx);
1990
1991// Lecture erreurs
1992if(errok) {
1993 s.GetLine(strg, 255);
1994 err2 = new double[nxy];
1995 for(j=0;j<ny;j++) s.GetR8s(err2+j*nx,nx);
1996}
1997
1998// Lecture des projections
1999if(projx) {
2000 s.GetLine(strg, 255);
2001 SetProjX();
2002 hprojx->Read(s);
2003}
2004if(projy) {
2005 s.GetLine(strg, 255);
2006 SetProjY();
2007 hprojy->Read(s);
2008}
2009
2010// Lecture des slices
2011if(nslix>0) {
2012 s.GetLine(strg, 255);
2013 SetSliX(nslix);
2014 DBASSERT (nslix==NSliX());
2015 for(j=0;j<NSliX();j++) HSliX(j)->Read(s);
2016}
2017if(nsliy>0) {
2018 s.GetLine(strg, 255);
2019 SetSliY(nsliy);
2020 DBASSERT (nsliy==NSliY());
2021 for(j=0;j<NSliY();j++) HSliY(j)->Read(s);
2022}
2023
2024// Lecture des bandes
2025if( nbanx>0 ) {
2026 s.GetLine(strg, 255);
2027 for( j=0; j<nbanx; j++) {
2028 s.GetR4(min);
2029 s.GetR4(max);
2030 SetBandX(min,max);
2031 }
2032 DBASSERT (nbanx==NBandX());
2033 for( j=0; j<NBandX(); j++) {
2034 h = HBandX(j);
2035 h->Read(s);
2036 }
2037}
2038if( nbany>0 ) {
2039 s.GetLine(strg, 255);
2040 for( j=0; j<nbany; j++) {
2041 s.GetR4(min);
2042 s.GetR4(max);
2043 SetBandY(min,max);
2044 }
2045 DBASSERT (nbany==NBandY());
2046 for( j=0; j<NBandY(); j++) {
2047 h = HBandY(j);
2048 h->Read(s);
2049 }
2050}
2051
2052return;
2053}
Note: See TracBrowser for help on using the repository browser.