source: Sophya/trunk/SophyaLib/NTools/objfitter.cc@ 1159

Last change on this file since 1159 was 1110, checked in by ansari, 25 years ago

on vire imageop.o de objlis.list
instanciations GNU des fct de dynccd.cc Noise... etc...
des fct passees en const dans GeneralFitData
objfitter prend les Histo/Histo2D/HProf/GeneralFitData

cmv 28/7/00

File size: 11.0 KB
RevLine 
[756]1#include "objfitter.h"
2
[1110]3//===========================================================================
4//===========================================================================
5//=========================== ObjectFitter ==================================
6//===========================================================================
7//===========================================================================
8
[756]9TMatrix<r_4>
10ObjectFitter::FitResidus(TMatrix<r_4> const & mtx, GeneralFit& gfit,
11 double xorg, double yorg, double dx, double dy)
12{
13 return( ArrayFitter<r_4>::FitResidus(mtx, gfit, xorg, yorg, dx, dy) );
14}
15
16TMatrix<r_8>
17ObjectFitter::FitResidus(TMatrix<r_8> const & mtx, GeneralFit& gfit,
18 double xorg, double yorg, double dx, double dy)
19{
20 return( ArrayFitter<r_8>::FitResidus(mtx, gfit, xorg, yorg, dx, dy) );
21}
22
23TMatrix<r_4>
24ObjectFitter::FitFunction(TMatrix<r_4> const & mtx, GeneralFit& gfit,
25 double xorg, double yorg, double dx, double dy)
26{
27 return( ArrayFitter<r_4>::FitFunction(mtx, gfit, xorg, yorg, dx, dy) );
28}
29
30TMatrix<r_8>
31ObjectFitter::FitFunction(TMatrix<r_8> const & mtx, GeneralFit& gfit,
32 double xorg, double yorg, double dx, double dy)
33{
34 return( ArrayFitter<r_8>::FitFunction(mtx, gfit, xorg, yorg, dx, dy) );
35}
36
37TVector<r_4>
38ObjectFitter::FitResidus(TVector<r_4> const & vec, GeneralFit& gfit,
39 double xorg, double dx)
40{
41 return( ArrayFitter<r_4>::FitResidus(vec, gfit, xorg, dx) );
42}
43
44TVector<r_8>
45ObjectFitter::FitResidus(TVector<r_8> const & vec, GeneralFit& gfit,
46 double xorg, double dx)
47{
48 return( ArrayFitter<r_8>::FitResidus(vec, gfit, xorg, dx) );
49}
50
51TVector<r_4>
52ObjectFitter::FitFunction(TVector<r_4> const & vec, GeneralFit& gfit,
53 double xorg, double dx)
54{
55 return( ArrayFitter<r_4>::FitFunction(vec, gfit, xorg, dx) );
56}
57
58TVector<r_8>
59ObjectFitter::FitFunction(TVector<r_8> const & vec, GeneralFit& gfit,
60 double xorg, double dx)
61{
62 return( ArrayFitter<r_8>::FitFunction(vec, gfit, xorg, dx) );
63}
64
65
[1110]66//=============================== Histo =====================================
67/*! Retourne une classe contenant les residus du fit ``gfit''. */
68Histo ObjectFitter::FitResidus(Histo const& hh, GeneralFit& gfit)
69{
70Histo h(hh);
71if(h.NBins()<=0)
72 throw(SzMismatchError("Histo::FitResidus: size mismatch\n"));
73GeneralFunction* f = gfit.GetFunction();
74if(f==NULL)
75 throw(NullPtrError("Histo::FitResidus: NULL pointer\n"));
76TVector<r_8> par = gfit.GetParm();
77for(int_4 i=0;i<h.NBins();i++) {
78 r_8 x = h.BinCenter(i);
79 h(i) -= f->Value(&x,par.Data());
80}
81return h;
82}
[756]83
[1110]84/*! Retourne une classe contenant la fonction du fit ``gfit''. */
85Histo ObjectFitter::FitFunction(Histo const& hh, GeneralFit& gfit)
86{
87Histo h(hh);
88if(h.NBins()<=0)
89 throw(SzMismatchError("Histo::FitFunction: size mismatch\n"));
90GeneralFunction* f = gfit.GetFunction();
91if(f==NULL)
92 throw(NullPtrError("Histo::FitFunction: NULL pointer\n"));
93TVector<r_8> par = gfit.GetParm();
94for(int_4 i=0;i<h.NBins();i++) {
95 r_8 x = h.BinCenter(i);
96 h(i) = f->Value(&x,par.Data());
97}
98return h;
99}
100
101/*!
102 Fit de l'histogramme par ``gfit''.
103 \verbatim
104 typ_err = 0 :
105 - erreur attachee au bin si elle existe
106 - sinon 1
107 typ_err = 1 :
108 - erreur attachee au bin si elle existe
109 - sinon max( sqrt(abs(bin) ,1 )
110 typ_err = 2 :
111 - erreur forcee a 1
112 typ_err = 3 :
113 - erreur forcee a max( sqrt(abs(bin) ,1 )
114 typ_err = 4 :
115 - erreur forcee a 1, nulle si bin a zero.
116 typ_err = 5 :
117 - erreur forcee a max( sqrt(abs(bin) ,1 ),
118 nulle si bin a zero.
119 \endverbatim
120*/
121int_4 ObjectFitter::Fit(Histo const& h, GeneralFit& gfit,unsigned short typ_err)
122{
123if(h.NBins()<=0) return -1000;
124if(typ_err>5) typ_err=0;
125
126GeneralFitData mydata(1,h.NBins());
127
128for(int_4 i=0;i<h.NBins();i++) {
129 r_8 x = h.BinCenter(i);
130 r_8 f = h(i);
131 r_8 saf = sqrt(fabs( f)); if(saf<1.) saf=1.;
132 r_8 e=0.;
133 if(typ_err==0) {if(h.HasErrors()) e=h.Error(i); else e=1.;}
134 else if(typ_err==1) {if(h.HasErrors()) e=h.Error(i); else e=saf;}
135 else if(typ_err==2) e=1.;
136 else if(typ_err==3) e=saf;
137 else if(typ_err==4) e=(f==0.)?0.:1.;
138 else if(typ_err==5) e=(f==0.)?0.:saf;
139 mydata.AddData1(x,f,e);
140}
141
142gfit.SetData(&mydata);
143
144return gfit.Fit();
145}
146
147//============================== Histo2D ====================================
148/*! Retourne une classe contenant les residus du fit ``gfit''. */
149Histo2D ObjectFitter::FitResidus(Histo2D const& hh, GeneralFit& gfit)
150{
151Histo2D h(hh);
152if(h.NBinX()<=0 || h.NBinY()<=0)
153 throw(SzMismatchError("Histo2D::FitResidus: size mismatch\n"));
154GeneralFunction* f = gfit.GetFunction();
155if(f==NULL)
156 throw(NullPtrError("Histo2D::FitResidus: NULL pointer\n"));
157TVector<r_8> par = gfit.GetParm();
158for(int_4 i=0;i<h.NBinX();i++) for(int_4 j=0;j<h.NBinY();j++) {
159 r_8 xc,yc;
160 h.BinCenter(i,j,xc,yc);
161 r_8 x[2] = {xc,yc};
162 h(i,j) -= f->Value(x,par.Data());
163}
164return h;
165}
166
167/*! Retourne une classe contenant la fonction du fit ``gfit''. */
168Histo2D ObjectFitter::FitFunction(Histo2D const& hh, GeneralFit& gfit)
169{
170Histo2D h(hh);
171if(h.NBinX()<=0 || h.NBinY()<=0)
172 throw(SzMismatchError("Histo2D::FitFunction: size mismatch\n"));
173GeneralFunction* f = gfit.GetFunction();
174if(f==NULL)
175 throw(NullPtrError("Histo2D::FitFunction: NULL pointer\n"));
176TVector<r_8> par = gfit.GetParm();
177for(int_4 i=0;i<h.NBinX();i++) for(int_4 j=0;j<h.NBinY();j++) {
178 r_8 xc,yc;
179 h.BinCenter(i,j,xc,yc);
180 r_8 x[2] = {xc,yc};
181 h(i,j) = f->Value(x,par.Data());
182}
183return h;
184}
185
186/*!
187 Fit de l'histogramme par ``gfit''.
188 \verbatim
189 typ_err = 0 :
190 - erreur attachee au bin si elle existe
191 - sinon 1
192 typ_err = 1 :
193 - erreur attachee au bin si elle existe
194 - sinon max( sqrt(abs(bin) ,1 )
195 typ_err = 2 :
196 - erreur forcee a 1
197 typ_err = 3 :
198 - erreur forcee a max( sqrt(abs(bin) ,1 )
199 typ_err = 4 :
200 - erreur forcee a 1, nulle si bin a zero.
201 typ_err = 5 :
202 - erreur forcee a max( sqrt(abs(bin) ,1 ),
203 nulle si bin a zero.
204 \endverbatim
205*/
206int_4 ObjectFitter::Fit(Histo2D const & h, GeneralFit& gfit,unsigned short typ_err)
207{
208if(h.NBinX()*h.NBinY()<=0) return -1000;
209if(typ_err>5) typ_err=0;
210
211GeneralFitData mydata(2,h.NBinX()*h.NBinY());
212
213for(int_4 i=0;i<h.NBinX();i++) for(int_4 j=0;j<h.NBinY();j++) {
214 r_8 x,y;
215 h.BinCenter(i,j,x,y);
216 r_8 f = h(i,j);
217 r_8 saf = sqrt(fabs(f)); if(saf<1.) saf=1.;
218 r_8 e=0.;
219 if(typ_err==0) {if(h.HasErrors()) e=h.Error(i,j); else e=1.;}
220 else if(typ_err==1) {if(h.HasErrors()) e=h.Error(i,j); else e=saf;}
221 else if(typ_err==2) e=1.;
222 else if(typ_err==3) e=saf;
223 else if(typ_err==4) e=(f==0.)?0.:1.;
224 else if(typ_err==5) e=(f==0.)?0.:saf;
225 mydata.AddData2(x,y,f,e);
226}
227
228gfit.SetData(&mydata);
229
230return gfit.Fit();
231}
232
233//===========================================================================
234//===========================================================================
235//========================== ArrayFitter<T> =================================
236//===========================================================================
237//===========================================================================
238
[756]239template <class T>
240TMatrix<T>
241ArrayFitter<T>::FitResidus(TMatrix<T> const & mtx, GeneralFit& gfit,
[790]242 double xorg,double yorg,double dx,double dy)
[756]243// Retourne une classe contenant les residus du fit ``gfit''.
244// On suppose que x=j (colonnes) et y=i (lignes) pour m(i,j).
245// Les coordonnees de l'element (i,j) sont :
246// (i,j) -> x = xorg+j*dx , y = yorg+i*dy
247{
248if(mtx.NCols()<=0||mtx.NRows()<=0)
249 throw(SzMismatchError("ArrayFitter::FitResidus(TMatrix<T>...) size mismatch\n"));
250GeneralFunction* f = gfit.GetFunction();
251if(f==NULL)
252 throw(NullPtrError("ArrayFitter::FitResidus(TMatrix<T>...) GeneraFit==NULL\n"));
253int npar = gfit.GetNPar();
254if(npar==0)
255 throw(SzMismatchError("ArrayFitter::FitResidus(TMatrix<T>...) GeneraFit 0 parametre\n"));
256double* par = new double[npar];
257{for(int i=0;i<npar;i++) par[i] = gfit.GetParm(i);}
258TMatrix<T> m(mtx);
259m.SetTemp(true);
260for(uint_4 i=0;i<mtx.NRows();i++) for(uint_4 j=0;j<mtx.NCols();j++) {
261 double x[2] = {xorg+j*dx,yorg+i*dy};
262 m(i,j) -= f->Value(x,par);
263}
264delete [] par;
265return m;
266}
267
268
269template <class T>
270TMatrix<T>
271ArrayFitter<T>::FitFunction(TMatrix<T> const & mtx, GeneralFit& gfit,
[790]272 double xorg,double yorg,double dx,double dy)
[756]273
274// Retourne une classe contenant la fonction du fit ``gfit''.
275// On suppose que x=j (colonnes) et y=i (lignes) pour m(i,j).
276// Les coordonnees de l'element (i,j) sont :
277// (i,j) -> x = xorg + j*dx , y = yorg + i*dy
278
279{
280if(mtx.NCols()<=0||mtx.NRows()<=0)
281 throw(SzMismatchError("ArrayFitter::FitFunction(TMatrix<T>...) size mismatch\n"));
282GeneralFunction* f = gfit.GetFunction();
283if(f==NULL)
284 throw(NullPtrError("ArrayFitter::FitFunction(TMatrix<T>...) GeneraFit==NULL\n"));
285int npar = gfit.GetNPar();
286if(npar==0)
287 throw(SzMismatchError("ArrayFitter::FitFunction(TMatrix<T>...) GeneraFit 0 parametre\n"));
288double* par = new double[npar];
289{for(int i=0;i<npar;i++) par[i] = gfit.GetParm(i);}
290TMatrix<T> m(mtx.NRows(), mtx.NCols());
291m.SetTemp(true);
292for(uint_4 i=0;i<mtx.NRows();i++) for(uint_4 j=0;j<mtx.NCols();j++) {
293 double x[2] = {xorg+j*dx,yorg+i*dy};
294 m(i,j) = f->Value(x,par);
295}
296delete [] par;
297return m;
298
299}
300
301template <class T>
302TVector<T>
303ArrayFitter<T>::FitResidus(TVector<T> const & vec, GeneralFit& gfit,
[790]304 double xorg,double dx)
[756]305// Retourne une classe contenant les residus du fit ``gfit''.
306// La coordonnee de l'element (i) est -> x = xorg + i*dx
307{
308if(vec.NElts()<=0)
309 throw(SzMismatchError("ArrayFitter::FitResidus(TVector<T>...) size mismatch\n"));
310GeneralFunction* f = gfit.GetFunction();
311if(f==NULL)
312 throw(NullPtrError("ArrayFitter::FitResidus(TVector<T>...) GeneraFit==NULL\n"));
313int npar = gfit.GetNPar();
314if(npar==0)
315 throw(SzMismatchError("ArrayFitter::FitResidus(TVector<T>...) GeneraFit 0 parametre\n"));
316double* par = new double[npar];
317{for(int i=0;i<npar;i++) par[i] = gfit.GetParm(i);}
318TVector<T> v(vec);
319v.SetTemp(true);
320for(uint_4 i=0;i<vec.NElts();i++) {
321 double x = xorg+i*dx;
322 v(i) -= f->Value(&x,par);
323}
324delete [] par;
325return v;
326}
327
328template <class T>
329TVector<T>
330ArrayFitter<T>::FitFunction(TVector<T> const & vec, GeneralFit& gfit,
[790]331 double xorg,double dx)
[756]332// Retourne une classe contenant les residus du fit ``gfit''.
333// La coordonnee de l'element (i) est -> x = xorg + i*dx
334{
335if(vec.NElts()<=0)
336 throw(SzMismatchError("ArrayFitter::FitResidus(TVector<T>...) size mismatch\n"));
337GeneralFunction* f = gfit.GetFunction();
338if(f==NULL)
339 throw(NullPtrError("ArrayFitter::FitResidus(TVector<T>...) GeneraFit==NULL\n"));
340int npar = gfit.GetNPar();
341if(npar==0)
342 throw(SzMismatchError("ArrayFitter::FitResidus(TVector<T>...) GeneraFit 0 parametre\n"));
343double* par = new double[npar];
344{for(int i=0;i<npar;i++) par[i] = gfit.GetParm(i);}
345TVector<T> v(vec.NElts());
346v.SetTemp(true);
347for(uint_4 i=0;i<vec.NElts();i++) {
348 double x = xorg+i*dx;
349 v(i) = f->Value(&x,par);
350}
351delete [] par;
352return v;
353}
354
355
356///////////////////////////////////////////////////////////////
357#ifdef __CXX_PRAGMA_TEMPLATES__
358#pragma define_template ArrayFitter<r_4>
359#pragma define_template ArrayFitter<r_8>
360#endif
361
362#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
363template class ArrayFitter<r_4>;
364template class ArrayFitter<r_8>;
365#endif
366
Note: See TracBrowser for help on using the repository browser.