[756] | 1 | #include "objfitter.h"
|
---|
| 2 |
|
---|
[1110] | 3 | //===========================================================================
|
---|
| 4 | //===========================================================================
|
---|
| 5 | //=========================== ObjectFitter ==================================
|
---|
| 6 | //===========================================================================
|
---|
| 7 | //===========================================================================
|
---|
| 8 |
|
---|
[756] | 9 | TMatrix<r_4>
|
---|
| 10 | ObjectFitter::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 |
|
---|
| 16 | TMatrix<r_8>
|
---|
| 17 | ObjectFitter::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 |
|
---|
| 23 | TMatrix<r_4>
|
---|
| 24 | ObjectFitter::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 |
|
---|
| 30 | TMatrix<r_8>
|
---|
| 31 | ObjectFitter::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 |
|
---|
| 37 | TVector<r_4>
|
---|
| 38 | ObjectFitter::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 |
|
---|
| 44 | TVector<r_8>
|
---|
| 45 | ObjectFitter::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 |
|
---|
| 51 | TVector<r_4>
|
---|
| 52 | ObjectFitter::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 |
|
---|
| 58 | TVector<r_8>
|
---|
| 59 | ObjectFitter::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''. */
|
---|
| 68 | Histo ObjectFitter::FitResidus(Histo const& hh, GeneralFit& gfit)
|
---|
| 69 | {
|
---|
| 70 | Histo h(hh);
|
---|
| 71 | if(h.NBins()<=0)
|
---|
| 72 | throw(SzMismatchError("Histo::FitResidus: size mismatch\n"));
|
---|
| 73 | GeneralFunction* f = gfit.GetFunction();
|
---|
| 74 | if(f==NULL)
|
---|
| 75 | throw(NullPtrError("Histo::FitResidus: NULL pointer\n"));
|
---|
| 76 | TVector<r_8> par = gfit.GetParm();
|
---|
| 77 | for(int_4 i=0;i<h.NBins();i++) {
|
---|
| 78 | r_8 x = h.BinCenter(i);
|
---|
| 79 | h(i) -= f->Value(&x,par.Data());
|
---|
| 80 | }
|
---|
| 81 | return h;
|
---|
| 82 | }
|
---|
[756] | 83 |
|
---|
[1110] | 84 | /*! Retourne une classe contenant la fonction du fit ``gfit''. */
|
---|
| 85 | Histo ObjectFitter::FitFunction(Histo const& hh, GeneralFit& gfit)
|
---|
| 86 | {
|
---|
| 87 | Histo h(hh);
|
---|
| 88 | if(h.NBins()<=0)
|
---|
| 89 | throw(SzMismatchError("Histo::FitFunction: size mismatch\n"));
|
---|
| 90 | GeneralFunction* f = gfit.GetFunction();
|
---|
| 91 | if(f==NULL)
|
---|
| 92 | throw(NullPtrError("Histo::FitFunction: NULL pointer\n"));
|
---|
| 93 | TVector<r_8> par = gfit.GetParm();
|
---|
| 94 | for(int_4 i=0;i<h.NBins();i++) {
|
---|
| 95 | r_8 x = h.BinCenter(i);
|
---|
| 96 | h(i) = f->Value(&x,par.Data());
|
---|
| 97 | }
|
---|
| 98 | return 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 | */
|
---|
| 121 | int_4 ObjectFitter::Fit(Histo const& h, GeneralFit& gfit,unsigned short typ_err)
|
---|
| 122 | {
|
---|
| 123 | if(h.NBins()<=0) return -1000;
|
---|
| 124 | if(typ_err>5) typ_err=0;
|
---|
| 125 |
|
---|
| 126 | GeneralFitData mydata(1,h.NBins());
|
---|
| 127 |
|
---|
| 128 | for(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 |
|
---|
| 142 | gfit.SetData(&mydata);
|
---|
| 143 |
|
---|
| 144 | return gfit.Fit();
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | //============================== Histo2D ====================================
|
---|
| 148 | /*! Retourne une classe contenant les residus du fit ``gfit''. */
|
---|
| 149 | Histo2D ObjectFitter::FitResidus(Histo2D const& hh, GeneralFit& gfit)
|
---|
| 150 | {
|
---|
| 151 | Histo2D h(hh);
|
---|
| 152 | if(h.NBinX()<=0 || h.NBinY()<=0)
|
---|
| 153 | throw(SzMismatchError("Histo2D::FitResidus: size mismatch\n"));
|
---|
| 154 | GeneralFunction* f = gfit.GetFunction();
|
---|
| 155 | if(f==NULL)
|
---|
| 156 | throw(NullPtrError("Histo2D::FitResidus: NULL pointer\n"));
|
---|
| 157 | TVector<r_8> par = gfit.GetParm();
|
---|
| 158 | for(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 | }
|
---|
| 164 | return h;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | /*! Retourne une classe contenant la fonction du fit ``gfit''. */
|
---|
| 168 | Histo2D ObjectFitter::FitFunction(Histo2D const& hh, GeneralFit& gfit)
|
---|
| 169 | {
|
---|
| 170 | Histo2D h(hh);
|
---|
| 171 | if(h.NBinX()<=0 || h.NBinY()<=0)
|
---|
| 172 | throw(SzMismatchError("Histo2D::FitFunction: size mismatch\n"));
|
---|
| 173 | GeneralFunction* f = gfit.GetFunction();
|
---|
| 174 | if(f==NULL)
|
---|
| 175 | throw(NullPtrError("Histo2D::FitFunction: NULL pointer\n"));
|
---|
| 176 | TVector<r_8> par = gfit.GetParm();
|
---|
| 177 | for(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 | }
|
---|
| 183 | return 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 | */
|
---|
| 206 | int_4 ObjectFitter::Fit(Histo2D const & h, GeneralFit& gfit,unsigned short typ_err)
|
---|
| 207 | {
|
---|
| 208 | if(h.NBinX()*h.NBinY()<=0) return -1000;
|
---|
| 209 | if(typ_err>5) typ_err=0;
|
---|
| 210 |
|
---|
| 211 | GeneralFitData mydata(2,h.NBinX()*h.NBinY());
|
---|
| 212 |
|
---|
| 213 | for(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 |
|
---|
| 228 | gfit.SetData(&mydata);
|
---|
| 229 |
|
---|
| 230 | return gfit.Fit();
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | //===========================================================================
|
---|
| 234 | //===========================================================================
|
---|
| 235 | //========================== ArrayFitter<T> =================================
|
---|
| 236 | //===========================================================================
|
---|
| 237 | //===========================================================================
|
---|
| 238 |
|
---|
[756] | 239 | template <class T>
|
---|
| 240 | TMatrix<T>
|
---|
| 241 | ArrayFitter<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 | {
|
---|
| 248 | if(mtx.NCols()<=0||mtx.NRows()<=0)
|
---|
| 249 | throw(SzMismatchError("ArrayFitter::FitResidus(TMatrix<T>...) size mismatch\n"));
|
---|
| 250 | GeneralFunction* f = gfit.GetFunction();
|
---|
| 251 | if(f==NULL)
|
---|
| 252 | throw(NullPtrError("ArrayFitter::FitResidus(TMatrix<T>...) GeneraFit==NULL\n"));
|
---|
| 253 | int npar = gfit.GetNPar();
|
---|
| 254 | if(npar==0)
|
---|
| 255 | throw(SzMismatchError("ArrayFitter::FitResidus(TMatrix<T>...) GeneraFit 0 parametre\n"));
|
---|
| 256 | double* par = new double[npar];
|
---|
| 257 | {for(int i=0;i<npar;i++) par[i] = gfit.GetParm(i);}
|
---|
| 258 | TMatrix<T> m(mtx);
|
---|
| 259 | m.SetTemp(true);
|
---|
| 260 | for(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 | }
|
---|
| 264 | delete [] par;
|
---|
| 265 | return m;
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 |
|
---|
| 269 | template <class T>
|
---|
| 270 | TMatrix<T>
|
---|
| 271 | ArrayFitter<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 | {
|
---|
| 280 | if(mtx.NCols()<=0||mtx.NRows()<=0)
|
---|
| 281 | throw(SzMismatchError("ArrayFitter::FitFunction(TMatrix<T>...) size mismatch\n"));
|
---|
| 282 | GeneralFunction* f = gfit.GetFunction();
|
---|
| 283 | if(f==NULL)
|
---|
| 284 | throw(NullPtrError("ArrayFitter::FitFunction(TMatrix<T>...) GeneraFit==NULL\n"));
|
---|
| 285 | int npar = gfit.GetNPar();
|
---|
| 286 | if(npar==0)
|
---|
| 287 | throw(SzMismatchError("ArrayFitter::FitFunction(TMatrix<T>...) GeneraFit 0 parametre\n"));
|
---|
| 288 | double* par = new double[npar];
|
---|
| 289 | {for(int i=0;i<npar;i++) par[i] = gfit.GetParm(i);}
|
---|
| 290 | TMatrix<T> m(mtx.NRows(), mtx.NCols());
|
---|
| 291 | m.SetTemp(true);
|
---|
| 292 | for(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 | }
|
---|
| 296 | delete [] par;
|
---|
| 297 | return m;
|
---|
| 298 |
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | template <class T>
|
---|
| 302 | TVector<T>
|
---|
| 303 | ArrayFitter<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 | {
|
---|
| 308 | if(vec.NElts()<=0)
|
---|
| 309 | throw(SzMismatchError("ArrayFitter::FitResidus(TVector<T>...) size mismatch\n"));
|
---|
| 310 | GeneralFunction* f = gfit.GetFunction();
|
---|
| 311 | if(f==NULL)
|
---|
| 312 | throw(NullPtrError("ArrayFitter::FitResidus(TVector<T>...) GeneraFit==NULL\n"));
|
---|
| 313 | int npar = gfit.GetNPar();
|
---|
| 314 | if(npar==0)
|
---|
| 315 | throw(SzMismatchError("ArrayFitter::FitResidus(TVector<T>...) GeneraFit 0 parametre\n"));
|
---|
| 316 | double* par = new double[npar];
|
---|
| 317 | {for(int i=0;i<npar;i++) par[i] = gfit.GetParm(i);}
|
---|
| 318 | TVector<T> v(vec);
|
---|
| 319 | v.SetTemp(true);
|
---|
| 320 | for(uint_4 i=0;i<vec.NElts();i++) {
|
---|
| 321 | double x = xorg+i*dx;
|
---|
| 322 | v(i) -= f->Value(&x,par);
|
---|
| 323 | }
|
---|
| 324 | delete [] par;
|
---|
| 325 | return v;
|
---|
| 326 | }
|
---|
| 327 |
|
---|
| 328 | template <class T>
|
---|
| 329 | TVector<T>
|
---|
| 330 | ArrayFitter<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 | {
|
---|
| 335 | if(vec.NElts()<=0)
|
---|
| 336 | throw(SzMismatchError("ArrayFitter::FitResidus(TVector<T>...) size mismatch\n"));
|
---|
| 337 | GeneralFunction* f = gfit.GetFunction();
|
---|
| 338 | if(f==NULL)
|
---|
| 339 | throw(NullPtrError("ArrayFitter::FitResidus(TVector<T>...) GeneraFit==NULL\n"));
|
---|
| 340 | int npar = gfit.GetNPar();
|
---|
| 341 | if(npar==0)
|
---|
| 342 | throw(SzMismatchError("ArrayFitter::FitResidus(TVector<T>...) GeneraFit 0 parametre\n"));
|
---|
| 343 | double* par = new double[npar];
|
---|
| 344 | {for(int i=0;i<npar;i++) par[i] = gfit.GetParm(i);}
|
---|
| 345 | TVector<T> v(vec.NElts());
|
---|
| 346 | v.SetTemp(true);
|
---|
| 347 | for(uint_4 i=0;i<vec.NElts();i++) {
|
---|
| 348 | double x = xorg+i*dx;
|
---|
| 349 | v(i) = f->Value(&x,par);
|
---|
| 350 | }
|
---|
| 351 | delete [] par;
|
---|
| 352 | return 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)
|
---|
| 363 | template class ArrayFitter<r_4>;
|
---|
| 364 | template class ArrayFitter<r_8>;
|
---|
| 365 | #endif
|
---|
| 366 |
|
---|