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
00031 static const std::string ITERATING_FIT_STATE = "Iterating";
00032
00033
00034
00035 namespace {
00036
00037 FitState* CreateIteratingFS() { return new FitStateIterating; }
00038
00039
00040 bool registered = FitStateFactory::Instance().RegisterFitState(
00041 ITERATING_FIT_STATE,
00042 CreateIteratingFS);
00043 }
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
00063 if ( context.fNIterationsStep > context.fNMaxIterations ) {
00064
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
00074
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
00100
00101 if ( dchi2 < context.fConvergenceCond &&
00102 context.fNPlanesFit == context.fConvergenceMaster.GetNPlanesCur() ) {
00103
00104 context.fDChi2 = dchi2;
00105
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
00117
00118
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
00130 if ( dchi2 < context.fDChi2 ) {
00131
00132 context.fDChi2 = dchi2;
00133 context.Print();
00134 return;
00135 } else {
00136
00137 context.fNTriesDiverges++;
00138 context.fDChi2 = dchi2;
00139
00140 if ( context.fNTriesDiverges > context.fNMaxDiverging ) {
00141
00142
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