Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

FitStateIterating.cxx

Go to the documentation of this file.
00001 //_____________________________________________________________________________
00011 
00012 #include <string>
00013 #include <cmath>
00014 
00015 #include "Algorithm/AlgConfig.h"
00016 #include "MessageService/MsgService.h"
00017 
00018 #include "CandFitTrackSA/ConstFT.h"
00019 #include "CandFitTrackSA/DataFT.h"
00020 #include "CandFitTrackSA/MatrixCalculator.h"
00021 
00022 #include "CandFitTrackSA/FitStateFactory.h"
00023 #include "CandFitTrackSA/FitStateIterating.h"
00024 #include "CandFitTrackSA/FitContext.h"
00025 
00026 CVSID("$Id: FitStateIterating.cxx,v 1.2 2006/12/01 18:40:45 rhatcher Exp $");
00027 
00028 using namespace ConstFT;
00029 
00030 // The ID of class Line
00031 static const std::string ITERATING_FIT_STATE = "Iterating";
00032 
00033 // Create an anonymous namespace
00034 // to make the function invisible from other modules
00035 namespace {
00036 
00037 FitState* CreateIteratingFS() { return new FitStateIterating; }
00038 
00039 // register block
00040 bool registered = FitStateFactory::Instance().RegisterFitState(
00041                                                     ITERATING_FIT_STATE,
00042                                                             CreateIteratingFS);
00043 }  // namespace
00044 
00045 
00049 const std::string& FitStateIterating::Name() const
00050 {
00051     return ITERATING_FIT_STATE;
00052 }    
00053 
00057 void FitStateIterating::Iterate(FitContext& context) const
00058 {
00059     context.fNIterationsStep++;
00060     context.fNIterationsTotal++;
00061     
00062     // check if max number of iterations reached 
00063     if ( context.fNIterationsStep > context.fNMaxIterations ) {
00064         // max number of iterations -> switch to Diverged state
00065         context.fConvergenceMaster.SetDiverged();
00066         context.SetState(FitStateFactory::Instance().GetFitState("Diverged"));
00067         context.Print();
00068         MSG("FitTrackSA",Msg::kInfo) << "Exceeded max # of iterations.\n";
00069         MSG("FitTrackSA",Msg::kInfo) << "Switched from Iterating to Diverged\n";
00070         return;
00071     }
00072 
00073     // protect from bad q/p values
00074     // should be made configurable
00075     if ( fabs(context.fCurrentFit(kQoverP))>4. ) {
00076         MSG("FitTrackSA",Msg::kInfo) << "Bad value of q/p = " 
00077             << context.fCurrentFit(kQoverP) << "\n";
00078         context.fCurrentFit(kQoverP) = 
00079             4.*TMath::Sign(1., context.fCurrentFit(kQoverP));
00080     }
00081         
00082     context.fNPlanesFit = context.fData.SetInitial(context.fCurrentFit, 
00083                 context.fConvergenceMaster.GetNPlanesCur(), *context.fSwimmer);
00084     
00085     if ( context.fNPlanesFit < context.fConvergenceMaster.GetNPlanesMin() ) {
00086         context.fConvergenceMaster.SetDiverged();
00087         context.SetState(FitStateFactory::Instance().GetFitState("Diverged"));
00088         context.Print();
00089         MSG("FitTrackSA",Msg::kInfo) << "fNPlanesFit is smaller than fMinPlanes.\n";
00090         MSG("FitTrackSA",Msg::kInfo) << "Switched from Iterating to Diverged\n";
00091         return;        
00092     }
00093         
00094     //    
00095     context.fMatCalc.Solve(context.fData);
00096     context.fCurrentFit = context.fMatCalc.GetTrackOut();        
00097     Double_t dchi2 = context.fMatCalc.GetDChi2();
00098 
00099     //MSG("FitTrackSA",Msg::kInfo) << "new dchi2=" << dchi2 << "\n";
00100 
00101     if ( dchi2 < context.fConvergenceCond &&
00102             context.fNPlanesFit == context.fConvergenceMaster.GetNPlanesCur() ) {
00103         // excellent - fit for NPlanesToFit converged!
00104         context.fDChi2 = dchi2;
00105         // switching to Converged state
00106         context.fConvergenceMaster.SetConverged();
00107         context.SetState(FitStateFactory::Instance().
00108                                                 GetFitState("Converged"));
00109         context.Print();
00110         MSG("FitTrackSA",Msg::kInfo) << "Switched from Iterating to Converged\n";
00111         return;
00112     }
00113     
00114     if ( dchi2 < context.fConvergenceCond &&
00115             context.fNPlanesFit != context.fConvergenceMaster.GetNPlanesCur() ) {
00116             //context.fNPlanesFit != context.fNPlanesToFit ) {
00117         // fit converged, but not to requested number of planes
00118         // switching to ConvergedPartial state
00119         context.fDChi2 = dchi2;
00120         context.fConvergenceMaster.SetDiverged();
00121         context.SetState(FitStateFactory::Instance().
00122                                             GetFitState("Diverged"));
00123         MSG("FitTrackSA",Msg::kInfo) << "#Planes fit != #Planes requested\n";
00124         MSG("FitTrackSA",Msg::kInfo) << "Switched from Iterating to Diverged\n";
00125         context.Print();
00126         return;
00127     }
00128     
00129     // check if the iterations are converging
00130     if ( dchi2 < context.fDChi2 ) {
00131         // good - fit is converging
00132         context.fDChi2 = dchi2;
00133         context.Print();
00134         return;
00135     } else {
00136         // fit is not converging - bump count
00137         context.fNTriesDiverges++;
00138         context.fDChi2 = dchi2;
00139         
00140         if ( context.fNTriesDiverges > context.fNMaxDiverging ) {
00141             // reached maximal count of diverging steps -
00142             // switch to Diverged state
00143             context.fConvergenceMaster.SetDiverged();
00144             context.SetState(FitStateFactory::Instance().
00145                                                     GetFitState("Diverged"));
00146             context.Print();
00147             MSG("FitTrackSA",Msg::kInfo) << "Delta(Chi2) is not decreasing\n";
00148             MSG("FitTrackSA",Msg::kInfo) << "Switching from Iterating to Diverged\n";
00149             return;
00150         }
00151         return;
00152     }        
00153     
00154     assert(kFALSE && "Should never get here!!");
00155 }
00156 

Generated on Mon Feb 15 11:06:43 2010 for loon by  doxygen 1.3.9.1