Anp::LocalFit Class Reference

#include <LocalFit.h>

Inheritance diagram for Anp::LocalFit:
Anp::Base

List of all members.

Public Member Functions

 LocalFit ()
virtual ~LocalFit ()
bool Add (int index, double x_, double y_)
bool RunFit ()
void Clear ()
void Config (const Registry &reg)
const std::vector
< Anp::FitPoint > & 
GetFit () const
double FitY (double x) const
void SetWindow (int value)
void SetIter (int value)
void SetDegree (int value)
unsigned int Window () const
unsigned int Iter () const
unsigned int Degree () const
void Print (std::ostream &os=std::cout) const

Private Types

typedef std::list< std::pair
< unsigned int, double > > 
List

Private Member Functions

bool RunLFit (FitPoint &point, const std::vector< FitPoint > &fvec) const
bool RunQFit (FitPoint &point, const std::vector< FitPoint > &fvec) const
bool SortCleanCheck (std::vector< Anp::FitPoint > &fvec) const
const List GetList (unsigned int pos) const
double BFunction (double value) const
double WFunction (double value) const

Private Attributes

Bool_t fDebug
Bool_t fValid
UInt_t fDegree
UInt_t fIter
UInt_t fWindow
std::vector< Anp::FitPointfFitVec

Detailed Description

Definition at line 29 of file LocalFit.h.


Member Typedef Documentation

typedef std::list<std::pair<unsigned int,double> > Anp::LocalFit::List [private]

Definition at line 60 of file LocalFit.h.


Constructor & Destructor Documentation

Anp::LocalFit::LocalFit (  ) 

Definition at line 18 of file LocalFit.cxx.

00019    :fDebug(false),
00020     fValid(false),
00021     fDegree(1),
00022     fIter(3),
00023     fWindow(7)
00024 {
00025 }

Anp::LocalFit::~LocalFit (  )  [virtual]

Definition at line 28 of file LocalFit.cxx.

00029 {
00030 }


Member Function Documentation

bool Anp::LocalFit::Add ( int  index,
double  x_,
double  y_ 
)

Definition at line 33 of file LocalFit.cxx.

References fFitVec, and find().

00034 {
00035    if(std::find(fFitVec.begin(), fFitVec.end(), index) != fFitVec.end())
00036    {
00037       cerr << "LocalFit::Add - index already exists: " << index << endl;
00038       return false;
00039    }
00040 
00041    fFitVec.push_back(Anp::FitPoint(index, x_, y_));
00042    return false;
00043 }

double Anp::LocalFit::BFunction ( double  value  )  const [private]

Definition at line 634 of file LocalFit.cxx.

Referenced by RunFit().

00635 {
00636    if(!(std::fabs(value) < 1.0))
00637    {
00638       return 0.0;
00639    }
00640    
00641    const double prod = 1.0 - value * value;
00642    
00643    return (prod * prod);
00644 }

void Anp::LocalFit::Clear ( void   ) 

Definition at line 185 of file LocalFit.cxx.

References fFitVec, and fValid.

00186 {
00187    fFitVec.clear();
00188    fValid = false;
00189 }

void Anp::LocalFit::Config ( const Registry reg  ) 

Definition at line 192 of file LocalFit.cxx.

References fDebug, fDegree, fIter, fWindow, Registry::Get(), SetDegree(), SetIter(), and SetWindow().

00193 {
00194    const char *value_char = 0;
00195    if(reg.Get("LocalFitDebug", value_char) && value_char)
00196    {
00197       if(strcmp(value_char, "yes") == 0)
00198       {
00199          fDebug = true;
00200       }
00201       else if(strcmp(value_char, "no") == 0)
00202       {
00203          fDebug = false;
00204       }
00205    }
00206 
00207    int value_int = 0;
00208    if(reg.Get("LocalFitDegree", value_int))
00209    {
00210       SetDegree(value_int);
00211    }
00212 
00213    value_int = 0;
00214    if(reg.Get("LocalFitWindow", value_int))
00215    {
00216       SetWindow(value_int);
00217    }
00218 
00219    value_int = 0;
00220    if(reg.Get("LocalFitIter", value_int))
00221    {
00222       SetIter(value_int);
00223    }
00224 
00225    value_char = 0;
00226    if(reg.Get("PrintConfig", value_char) && value_char && strcmp(value_char, "yes") == 0)
00227    {
00228       cout << "LocalFit::Config" << endl
00229            << "   Debug = " << fDebug << endl
00230            << "   Degree = " << fDegree << endl
00231            << "   Window = " << fWindow << endl
00232            << "   Iter = " << fIter << endl;
00233    }
00234 }

unsigned int Anp::LocalFit::Degree (  )  const [inline]

Definition at line 101 of file LocalFit.h.

References fDegree.

00102    {
00103       return fDegree;
00104    }   

double Anp::LocalFit::FitY ( double  x  )  const

Definition at line 237 of file LocalFit.cxx.

References fFitVec, fValid, and Print().

00238 {
00239    if(!fValid || fFitVec.empty())
00240    {
00241       cerr << "LocalFit::FitY - I do not have a valid fit vector" << endl;
00242       return 0.0;
00243    }
00244 
00245    vector<FitPoint>::const_iterator fit = std::upper_bound(fFitVec.begin(), fFitVec.end(), x);
00246    if(fit == fFitVec.end())
00247    {
00248       cerr << "LocalFit::FitY - x = " << x << " is greater than upper bound of fit range " 
00249            << fFitVec.back().X() << endl;
00250    }
00251    else if(fit == fFitVec.begin())
00252    {
00253       cerr << "LocalFit::FitY - x = " << x << " is less than lower bound of fit range " 
00254            << fFitVec.front().X() << endl;
00255    }
00256    
00257    if(fit -> Pass())
00258    {
00259       return fit -> FitY(x);
00260    }
00261 
00262    cerr << "LocalFit::FitY - closest fit point failed local fit:" << endl << "   ";   
00263    fit -> Print(std::cerr);
00264 
00265    return fit -> Y();
00266 }

const std::vector< Anp::FitPoint > & Anp::LocalFit::GetFit ( void   )  const [inline]

Definition at line 89 of file LocalFit.h.

References fFitVec.

00090    {
00091       return fFitVec;
00092    }

const Anp::LocalFit::List Anp::LocalFit::GetList ( unsigned int  pos  )  const [private]

Definition at line 538 of file LocalFit.cxx.

References fDebug, fFitVec, fWindow, Anp::FitPoint::Print(), Munits::second, and Anp::FitPoint::X().

Referenced by RunFit().

00539 {
00540    //
00541    // Return pair of position and distance for fWindow nearest points for point at pos.
00542    // Assume that fFitVec is sorted by X values, but no assumption is made
00543    // about relative distances between X values.
00544    //
00545 
00546    List nlist;
00547 
00548    if(pos >= fFitVec.size())
00549    {
00550       cerr << "LocalFit::GetList - position is out of range: " << pos << endl;
00551       return nlist;
00552    }
00553 
00554    if(fWindow < 1)
00555    {
00556       cerr << "LocalFit::GetList - window is too small " << fWindow << endl;
00557       return nlist;
00558    }
00559 
00560    const int ibeg = std::max<int>(int(pos) - int(fWindow + 0), 0);
00561    const int iend = std::min<int>(int(pos) + int(fWindow + 1), int(fFitVec.size()));   
00562 
00563    assert(ibeg >= 0 && iend <= int(fFitVec.size()) && "invalid range");
00564 
00565    if(fDebug)
00566    {
00567       cout << "LocalFit::GetList - range for searching (ibeg, iend) = (" 
00568            << ibeg << ", " << iend << ")" << endl;
00569    }
00570 
00571    const FitPoint &point = fFitVec[pos];
00572 
00573    for(int i = ibeg; i < iend; ++i)
00574    {
00575       // ignore this (anchor) point
00576       if(i == int(pos))
00577       {
00578          continue;
00579       }
00580 
00581       const FitPoint &other = fFitVec[i];
00582       const double dist = std::fabs(point.X() - other.X());
00583       
00584       if(!(dist > 0.0))
00585       {
00586          cerr << "LocalFit::GetList - ignore zero distance point" << endl;
00587          continue;
00588       }
00589 
00590       // get current farthest distance from anchor point
00591       // and ignore points outside current minimum range
00592       if(!nlist.empty() && nlist.size() == fWindow && !(dist < nlist.back().second))
00593       {
00594          continue;
00595       }
00596 
00597       List::iterator lit = nlist.begin();
00598       for(; lit != nlist.end(); ++lit)
00599       {
00600          if(dist < lit -> second)
00601          {
00602             break;
00603          }
00604          else
00605          {
00606             continue;
00607          }
00608       }
00609       
00610       nlist.insert(lit, pair<unsigned int,double>(i, dist));
00611       
00612       if(nlist.size() > fWindow)
00613       {
00614          nlist.pop_back();
00615       }
00616    }
00617 
00618    if(fDebug)
00619    {
00620       cout << "LocalFit::GetList - list of " << fWindow << " neighbors" << endl;
00621       cout << "   Anchor ";
00622       point.Print();
00623       
00624       for(List::const_iterator lit = nlist.begin(); lit != nlist.end(); ++lit)
00625       {
00626          cout << "   position = " << lit -> first << ", distance = " << lit -> second << endl;
00627       }
00628    }
00629 
00630    return nlist;
00631 }

unsigned int Anp::LocalFit::Iter (  )  const [inline]

Definition at line 97 of file LocalFit.h.

References fIter.

00098    {
00099       return fIter;
00100    }

void Anp::LocalFit::Print ( std::ostream &  os = std::cout  )  const

Referenced by FitY(), and RunFit().

bool Anp::LocalFit::RunFit (  ) 

Definition at line 46 of file LocalFit.cxx.

References BFunction(), fDebug, fDegree, fFitVec, fIter, fValid, fWindow, GetList(), Print(), RunLFit(), RunQFit(), Munits::second, Anp::FitPoint::SetWeight(), SortCleanCheck(), and WFunction().

00047 {
00048    //
00049    // Fit locally data points with 2nd or 3rd degree polynomial
00050    // 
00051 
00052    if(fDebug)
00053    {
00054       cout << "LocalFit::RunFit - starting new fit" << endl;
00055    }
00056 
00057    //
00058    // Check validity of data in fit vector
00059    //
00060    if(!SortCleanCheck(fFitVec))
00061    {
00062       cerr << "LocalFit::RunFit - invalid fit vector" << endl;
00063       return false;
00064    }
00065    
00066    if(fDebug)
00067    {
00068       cout << "LocalFit::RunFit - fit vector size = " <<  fFitVec.size() << endl;
00069    }
00070 
00071    // 
00072    // For each point get list of fWindow nearest points and set local weights
00073    //
00074    for(unsigned int ipos = 0; ipos < fFitVec.size(); ++ipos)
00075    {
00076       const List nlist = GetList(ipos);
00077 
00078       if(nlist.empty() || 
00079          nlist.size() > fWindow || 
00080          (fFitVec.size() > fWindow && nlist.size() != fWindow))
00081       {
00082          cerr << "LocalFit::RunFit - failed to find " << fWindow << " neighbors" << endl;
00083          return false;
00084       }
00085 
00086       FitPoint &point = fFitVec[ipos];
00087 
00088       const double width = nlist.back().second;
00089       if(!(width > 0.0))
00090       {
00091          cerr << "LocalFit::RunFit - interval size is zero" << endl;
00092          return false;   
00093       }
00094       
00095       for(List::const_iterator lit = nlist.begin(); lit != nlist.end(); ++lit)
00096       {
00097          point.SetWeight(lit -> first, WFunction(lit -> second/width));
00098       }
00099    }
00100 
00101    if(fDebug)
00102    {
00103       cout << "LocalFit::RunFit - starting " << fIter << " iterations..." << endl;
00104    }
00105 
00106    for(unsigned int i = 0; i <= fIter; ++i)
00107    {  
00108       std::vector<double> resvec;
00109       for(vector<FitPoint>::iterator fit = fFitVec.begin(); fit != fFitVec.end(); ++fit)
00110       {
00111          if(fDegree == 1)
00112          {
00113             RunLFit(*fit, fFitVec);
00114          }
00115          else if(fDegree == 2)
00116          {
00117             RunQFit(*fit, fFitVec);
00118          }
00119          else
00120          {
00121             cerr << "LocalFit::RunFit - uknown polynomial degree: " << fDegree << endl;
00122             return false;
00123          }
00124 
00125          if(fit -> Pass())
00126          {
00127             resvec.push_back(std::fabs(fit -> Residual()));
00128          }
00129       }
00130       
00131       if(resvec.size() != fFitVec.size())
00132       {
00133          cerr << "LocalFit::RunFit - # of failed points = " << fFitVec.size() -  resvec.size() << endl;
00134          for(vector<FitPoint>::iterator fit = fFitVec.begin(); fit != fFitVec.end(); ++fit)
00135          {
00136             if(!(fit -> Pass()))
00137             {
00138                cout << "   ";
00139                fit -> Print();
00140             }
00141          }
00142          return false;
00143       }
00144 /*
00145       std::sort(resvec.begin(), resvec.end());
00146       const double median = resvec[resvec.size()/2];
00147 
00148       if(!(median > 0.0))
00149       {
00150          cerr << "LocalFit::RunFit - median residual is zero" << endl;
00151          return false;
00152       }      
00153       
00154       for(vector<FitPoint>::iterator fit = fFitVec.begin(); fit != fFitVec.end(); ++fit)
00155       {
00156          fit -> SetWeight(BFunction(fit -> Residual()/(6.0*median)));
00157       }
00158 */
00159 
00160       const double maxres = *std::max_element(resvec.begin(), resvec.end());
00161 
00162       if(!(maxres > 0.0))
00163       {
00164          cerr << "LocalFit::RunFit - maximum residual is zero" << endl;
00165          return false;
00166       }      
00167       
00168       for(vector<FitPoint>::iterator fit = fFitVec.begin(); fit != fFitVec.end(); ++fit)
00169       {
00170          fit -> SetWeight(BFunction(fit -> Residual()/maxres));
00171       }
00172    }
00173 
00174    if(fDebug)
00175    {
00176       cout << "LocalFit::RunFit - finished" << endl;
00177    }
00178 
00179    fValid = true;
00180 
00181    return true;
00182 }

bool Anp::LocalFit::RunLFit ( FitPoint point,
const std::vector< FitPoint > &  fvec 
) const [private]

Referenced by RunFit().

bool Anp::LocalFit::RunQFit ( FitPoint point,
const std::vector< FitPoint > &  fvec 
) const [private]

Referenced by RunFit().

void Anp::LocalFit::SetDegree ( int  value  ) 

Definition at line 291 of file LocalFit.cxx.

References fDegree.

Referenced by Config().

00292 {
00293    if(value != 1 && value != 2)
00294    {
00295       cerr << "LocalFit::SetDegree - invalid degree parameter: " << value << endl;
00296    }
00297    
00298    fDegree = static_cast<unsigned int>(value);
00299 }

void Anp::LocalFit::SetIter ( int  value  ) 

Definition at line 280 of file LocalFit.cxx.

References fIter.

Referenced by Config().

00281 {
00282    if(value < 0)
00283    {
00284       value = 0;
00285    }
00286    
00287    fIter = static_cast<unsigned int>(value);
00288 }

void Anp::LocalFit::SetWindow ( int  value  ) 

Definition at line 269 of file LocalFit.cxx.

References fWindow.

Referenced by Config().

00270 {
00271    if(value < 2)
00272    {
00273       cerr << "LocalFit::SetWindow - invalid window parameter: " << value << endl;
00274    }
00275    
00276    fWindow = static_cast<unsigned int>(value);
00277 }

bool Anp::LocalFit::SortCleanCheck ( std::vector< Anp::FitPoint > &  fvec  )  const [private]

Referenced by RunFit().

double Anp::LocalFit::WFunction ( double  value  )  const [private]

Definition at line 647 of file LocalFit.cxx.

Referenced by RunFit().

00648 {
00649    const double avalue = std::fabs(value);
00650 
00651    if(!(avalue < 1.0))
00652    {
00653       return 0.0;
00654    }  
00655 
00656    const double prod = 1.0 - avalue * avalue * avalue;
00657 
00658    return (prod * prod * prod);
00659 }

unsigned int Anp::LocalFit::Window (  )  const [inline]

Definition at line 93 of file LocalFit.h.

References fWindow.

00094    {
00095       return fWindow;
00096    }


Member Data Documentation

Bool_t Anp::LocalFit::fDebug [private]

Definition at line 76 of file LocalFit.h.

Referenced by Config(), GetList(), and RunFit().

UInt_t Anp::LocalFit::fDegree [private]

Definition at line 79 of file LocalFit.h.

Referenced by Config(), Degree(), RunFit(), and SetDegree().

std::vector<Anp::FitPoint> Anp::LocalFit::fFitVec [private]

Definition at line 83 of file LocalFit.h.

Referenced by Add(), Clear(), FitY(), GetFit(), GetList(), and RunFit().

UInt_t Anp::LocalFit::fIter [private]

Definition at line 80 of file LocalFit.h.

Referenced by Config(), Iter(), RunFit(), and SetIter().

Bool_t Anp::LocalFit::fValid [private]

Definition at line 77 of file LocalFit.h.

Referenced by Clear(), FitY(), and RunFit().

UInt_t Anp::LocalFit::fWindow [private]

Definition at line 81 of file LocalFit.h.

Referenced by Config(), GetList(), RunFit(), SetWindow(), and Window().


The documentation for this class was generated from the following files:

Generated on 11 Jan 2018 for loon by  doxygen 1.6.1