Changeset 1110 in Sophya for trunk/SophyaLib/NTools/objfitter.cc
- Timestamp:
- Jul 28, 2000, 6:32:08 PM (25 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaLib/NTools/objfitter.cc
r790 r1110 1 1 #include "objfitter.h" 2 3 //=========================================================================== 4 //=========================================================================== 5 //=========================== ObjectFitter ================================== 6 //=========================================================================== 7 //=========================================================================== 2 8 3 9 TMatrix<r_4> … … 58 64 59 65 60 // --------------------------------------------------------------------------- 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 } 83 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 //=========================================================================== 61 238 62 239 template <class T>
Note:
See TracChangeset
for help on using the changeset viewer.