source: Sophya/trunk/SophyaLib/HiStats/histerr.h@ 3136

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

intro operator+=double HistoErr Histo2DErr cmv 16/01/07

File size: 4.7 KB
RevLine 
[2603]1#ifndef HISTERR_SEEN
2#define HISTERR_SEEN
3
4#include "objfio.h"
5#include <iostream>
6#include <stdio.h>
[3123]7#include "tvector.h"
[2603]8
9namespace SOPHYA {
10
[3049]11// Forward class declaration for Fits handler
12template <class T> class FitsHandler;
13
[3123]14
15//! 1 dimensions histograms with errors given by user
16class HistoErr : public AnyDataObj {
[2603]17 friend class ObjFileIO<HistoErr>;
[3123]18 friend class FitsHandler<HistoErr>;
[2603]19public:
20
[3123]21 //! Create or destroy
[2603]22 HistoErr(void);
[3123]23 HistoErr(r_8 xmin,r_8 xmax,int_4 nx);
[2603]24 HistoErr(const HistoErr& H);
25 virtual ~HistoErr(void);
26
[3123]27 //! Updating or Setting
28 void ReCenterBin(void);
[2603]29 void Zero(void);
[3123]30
31 //! Getting Info
32 int_4 NBins(void) {return nx_;}
33 r_8 XMin(void) {return xmin_;}
34 r_8 XMax(void) {return xmax_;}
35 r_8 BinWidth(void) {return dx_;}
36
37 //! Retourne le contenu du bin
38 inline r_8 operator()(int_4 i) const
39 {
40 if(i<0 || i>=nx_) return 0.;
41 return data_(i);
42 }
43 inline r_8& operator()(int_4 i)
44 {
45 return data_(i);
46 }
47
48 //! Retourne le nombre l'erreur au carre dans le bin
49 inline r_8 Error2(int_4 i) const
50 {
51 if(i<0 || i>=nx_) return 0.;
52 return err2_(i);
53 }
[3124]54 inline r_8 Error(int_4 i) const
55 {
56 if(i<0 || i>=nx_) return 0.;
57 if(err2_(i)>0.) return sqrt(err2_(i)); else return 0.;
58 }
[3123]59
60 //! Retourne le nombre d'entree dans le bin
61 inline r_8 NEntBin(int_4 i) const
62 {
63 if(i<0 || i>=nx_) return 0.;
64 return ndata_(i);
65 }
66
67 //! Retourne l'abscisse et l'ordonnee du coin inferieur du bin i.
68 inline r_8 BinLowEdge(int_4 i) const {return xmin_ + i*dx_;}
69 //! Retourne l'abscisse et l'ordonnee du centre du bin i.
70 inline r_8 BinCenter(int_4 i) const {return xmin_ + (i+0.5)*dx_;}
71 //! Retourne l'abscisse et l'ordonnee du coin superieur du bin i.
72 inline r_8 BinHighEdge(int_4 i) const {return xmin_ + (i+1)*dx_;}
73 //! Retourne les numeros du bin contenant l'abscisse et l'ordonnee x,y.
74 inline int_4 FindBin(r_8 x) const {return (int_4)floor((x-xmin_)/dx_);}
75
[2603]76 //! Addition du contenu de l'histo pour abscisse x poids w et l'erreur e
[2630]77 inline void Add(r_8 x, r_8 w, r_8 e)
[3123]78 {
79 int_4 i = FindBin(x);
80 if(i<0 || i>=nx_) return;
81 data_(i) += w; ndata_(i) += 1.; err2_(i) += e*e;
82 }
[2630]83 inline void Add(r_8 x, r_8 w) {Add(x,w,1.);}
84 inline void Add(r_8 x) {Add(x,1.,1.);}
[2603]85
[3123]86 //! Addition du contenu de l'histo pour le bin i poids w et l'erreur e
87 inline void AddBin(int_4 i, r_8 w, r_8 e)
88 {
89 if(i<0 || i>=nx_) return;
90 data_(i) += w; ndata_(i) += 1.; err2_(i) += e*e;
91 }
92 inline void AddBin(int_4 i, r_8 w) {AddBin(i,w,1.);}
93 inline void AddBin(int_4 i) {AddBin(i,1.,1.);}
[2603]94
[3123]95 //! remplissage contenu de l'histo pour le bin i poids w et l'erreur e
96 inline void SetBin(int_4 i, r_8 w, r_8 e, r_8 nb)
97 {
98 if(i<0 || i>=nx_) return;
99 data_(i) = w;
100 err2_(i) = e*e;
101 ndata_(i) = nb;
102 }
103 inline void SetBin(int_4 i, r_8 w, r_8 e) {SetBin(i,w,e,1.);}
104 inline void SetBin(int_4 i, r_8 w) {SetBin(i,w,1.,1.);}
105 inline void SetBin(int_4 i) {SetBin(i,1.,1.,1.);}
106 //! remplissage de l'erreur carree pour le bin i
107 void SetErr2(int_4 i, r_8 e2)
108 {
109 if(i<0 || i>=nx_) return;
110 err2_(i) = e2;
111 }
112 //! remplissage nombre d'entrees pour le bin i
113 void SetNentB(int_4 i, r_8 nb)
114 {
115 if(i<0 || i>=nx_) return;
116 ndata_(i) = nb;
117 }
[2603]118
[2608]119 //! Compute the correlation histogram
[2604]120 void ToCorrel(void);
[2819]121 void FromCorrel(void);
[3049]122 int_4 NCorrel(void) {return mCorrel;}
[3118]123 void SetCorrel(int_4 mcorrel) {mCorrel = mcorrel;}
[2604]124
[2628]125 //! Fill an histogram with an histogram
126 void FillFrHErr(HistoErr& hfrom);
127
[3123]128 //! Recuperation des matrices elementaires
[3136]129 void GetData(TVector<r_8>& data) {data = data_;}
130 void GetError2(TVector<r_8>& err2) {err2 = err2_;}
131 void GetNData(TVector<r_8>& ndata) {ndata = ndata_;}
[3123]132
[2619]133 // Operators
134 HistoErr& operator = (const HistoErr& h);
[3136]135 HistoErr& operator *= (r_8 b);
[2619]136
[3053]137 // Print
[3136]138 void Show(ostream& os) const;
139 void Show() const { Show(cout); }
[3053]140
[2603]141protected:
[3123]142 void CreateOrResize(r_8 xmin,r_8 xmax,int_4 nx);
[2619]143
[3123]144 r_4 xmin_,xmax_,dx_;
145 int_4 nx_;
146 TVector<r_8> data_;
147 TVector<r_8> err2_;
148 TVector<r_8> ndata_;
[3049]149 int_4 mCorrel; //!< Nombre d'appels a ToCorrel(+1) ou FromCorrel(-1)
[2603]150};
151
[3053]152/*! Prints histogram information on stream \b s (h.Show(s)) */
153inline ostream& operator << (ostream& s, HistoErr const & h)
154 { h.Show(s); return(s); }
155
[2603]156/*! \ingroup HiStats \fn operator<<(POuttPersist&,HistoErr)
157 \brief Persistance management */
158inline POutPersist& operator << (POutPersist& os, HistoErr & obj)
159{ ObjFileIO<HistoErr> fio(&obj); fio.Write(os); return(os); }
160/*! \ingroup HiStats \fn operator<<(POuttPersist&,HistoErr)
161 \brief Persistance management */
162inline PInPersist& operator >> (PInPersist& is, HistoErr & obj)
163{ ObjFileIO<HistoErr> fio(&obj); is.SkipToNextObject(); fio.Read(is); return(is); }
164
165} // Fin du namespace
166
167#endif // HISTERR_SEEN
Note: See TracBrowser for help on using the repository browser.