TGo4FitMinuit.cxx

Go to the documentation of this file.
00001 // $Id: TGo4FitMinuit.cxx 478 2009-10-29 12:26:09Z linev $
00002 //-----------------------------------------------------------------------
00003 //       The GSI Online Offline Object Oriented (Go4) Project
00004 //         Experiment Data Processing at EE department, GSI
00005 //-----------------------------------------------------------------------
00006 // Copyright (C) 2000- GSI Helmholtzzentrum für Schwerionenforschung GmbH
00007 //                     Planckstr. 1, 64291 Darmstadt, Germany
00008 // Contact:            http://go4.gsi.de
00009 //-----------------------------------------------------------------------
00010 // This software can be used under the license agreements as stated
00011 // in Go4License.txt file which is part of the distribution.
00012 //-----------------------------------------------------------------------
00013 
00014 #include "TGo4FitMinuit.h"
00015 
00016 #include "Riostream.h"
00017 
00018 #include "TMinuit.h"
00019 #include "TArrayD.h"
00020 #include "TMatrixD.h"
00021 #include "TObjString.h"
00022 
00023 #include "TGo4FitterAbstract.h"
00024 #include "TGo4FitMinuitResult.h"
00025 
00026 class TMinuitEx : public TMinuit {
00027   public:
00028 
00029     TMinuitEx(Int_t NumPars, TGo4FitterAbstract* fitter);
00030     virtual ~TMinuitEx();
00031 
00032     virtual Int_t Eval(Int_t npar, Double_t *grad, Double_t &fval, Double_t *pars, Int_t iflag);
00033 
00034   protected:
00035 
00036     TGo4FitterAbstract* fxFitter;    
00037 };
00038 
00039 TMinuitEx::TMinuitEx(Int_t NumPars, TGo4FitterAbstract* fitter) :
00040   TMinuit(NumPars), fxFitter(fitter) {
00041 }
00042 
00043 TMinuitEx::~TMinuitEx() {
00044 }
00045 
00046 Int_t TMinuitEx::Eval(Int_t npar, Double_t *grad, Double_t &fval, Double_t *pars, Int_t iflag) {
00047    if (fxFitter) fval = fxFitter->CalculateFitFunction(pars);
00048             else fval = 0.;
00049    return 0;
00050 }
00051 
00052 // *******************************************************************************
00053 
00054 TGo4FitMinuit::TGo4FitMinuit() : TGo4FitterAction(), fxCommands(), fxResults() {
00055 }
00056 
00057 TGo4FitMinuit::TGo4FitMinuit(const char* Name) :
00058     TGo4FitterAction(Name,"Fitter minimization using TMinuit object"), fxCommands(), fxResults() {
00059    fxCommands.SetOwner(kTRUE);
00060    fxResults.SetOwner(kTRUE);
00061 }
00062 
00063 TGo4FitMinuit::~TGo4FitMinuit() {
00064 }
00065 
00066 void TGo4FitMinuit::AddCommand(const char* iCommand)
00067 {
00068    fxCommands.Add( new TObjString(iCommand) );
00069 }
00070 
00071 const char* TGo4FitMinuit::GetCommand(Int_t n)
00072 {
00073    return ((TObjString*) fxCommands[n])->GetString().Data();
00074 }
00075 
00076 void TGo4FitMinuit::DoAction(TGo4FitterAbstract* Fitter) {
00077 
00078    TMinuitEx fMinuit(Fitter->NumPars(), Fitter);
00079    fMinuit.SetPrintLevel(-1);
00080 
00081    for(Int_t n=0;n<Fitter->NumPars();n++) {
00082       const char* FullName = Fitter->GetParFullName(n);
00083       Int_t ierflg = 0;
00084       Double_t epsilon = 0, RangeMin = 0, RangeMax = 0;
00085       if (!Fitter->GetParEpsilon(FullName,epsilon)) epsilon = 1.;
00086 
00087       if (!Fitter->GetParRange(FullName,RangeMin,RangeMax)) { RangeMin = 0; RangeMax = 0; }
00088 
00089       fMinuit.mnparm(n, FullName, Fitter->GetParValue(FullName),
00090                         epsilon, RangeMin, RangeMax, ierflg);
00091 
00092       if (Fitter->GetParFixed(FullName)) fMinuit.FixParameter(n);
00093    }
00094 
00095    if (fxCommands.GetLast()<0) fMinuit.Command("MIGRAD 500 1");
00096    else
00097      for(Int_t n=0;n<=fxCommands.GetLast();n++) {
00098        TString cmd ( ((TObjString*) fxCommands[n])->GetString() );
00099        if (cmd[0] == 'r') {
00100            if (cmd.Index("result",6,0,TString::kIgnoreCase) == 0) {
00101               cmd.Remove(0,6);
00102               while ((cmd.Length()>0) && (cmd[0]==' ')) cmd.Remove(0,1);
00103               if (cmd.Length()==0) cmd = "1000";
00104               if (cmd.Length()<4) { cout << "invalid result command syntax" << endl; break; }
00105               Bool_t getpar  = (cmd[0]=='1');
00106               Bool_t geterr  = (cmd[1]=='1');
00107               Bool_t getmatr = (cmd[2]=='1');
00108               Bool_t getcontr = (cmd[3]=='1');
00109               cmd.Remove(0,4);
00110               while ((cmd.Length()>0) && (cmd[0]==' ')) cmd.Remove(0,1);
00111               if (cmd.Length()==0) cmd="Result";
00112 
00113               TGo4FitMinuitResult *res = new TGo4FitMinuitResult(cmd,"TMinuit result object");
00114               fxResults.Add(res);
00115               res->CallMNSTAT(&fMinuit);
00116               if (getpar)  res->CallMNPOUT(&fMinuit,Fitter->NumPars());
00117               if (geterr)  res->CallMNERRS(&fMinuit,Fitter->NumPars());
00118               if (getmatr) res->CallMNEMAT(&fMinuit,Fitter->NumPars(), kTRUE);
00119               if (getcontr) res->GetContourPlot(&fMinuit);
00120           }
00121 
00122        } else fMinuit.Command(cmd);
00123      }
00124 
00125    for(Int_t n=0;n<Fitter->NumPars();n++) {
00126       Double_t value,error;
00127       fMinuit.GetParameter(n, value, error);
00128       Fitter->SetParValue(Fitter->GetParFullName(n),value);
00129       Fitter->SetParError(Fitter->GetParFullName(n),error);
00130    }
00131 }
00132 
00133 TGo4FitMinuitResult* TGo4FitMinuit::GetResult(Int_t indx)
00134 {
00135    return (indx>=0) && (indx<=fxResults.GetLast()) ? (TGo4FitMinuitResult*) fxResults.At(indx) : 0;
00136 }
00137 
00138 TGo4FitMinuitResult* TGo4FitMinuit::FindResult(const char* ResName)
00139 {
00140    return (TGo4FitMinuitResult*) fxResults.FindObject(ResName);
00141 }
00142 
00143 void TGo4FitMinuit::AddResult(TGo4FitMinuitResult* res)
00144 {
00145    fxResults.Add(res);
00146 }
00147 
00148 void TGo4FitMinuit::RemoveResult(TGo4FitMinuitResult* res)
00149 {
00150    fxResults.Remove(res);
00151    fxResults.Compress();
00152 }
00153 
00154 void TGo4FitMinuit::Print(Option_t* option) const {
00155    TGo4FitterAction::Print(option);
00156    if (fxCommands.GetLast()>=0)
00157      cout << "List of commands:" << endl;
00158    for(Int_t n=0;n<=fxCommands.GetLast();n++)
00159      cout << "   " << ((TObjString*) fxCommands[n])->String().Data() << endl;
00160    if (fxResults.GetLast()>=0) {
00161      cout << "List of stored results:" << endl;
00162      fxResults.Print(option);
00163    }
00164 }

Generated on Thu Oct 28 15:54:12 2010 for Go4-Fitpackagev4.04-2 by  doxygen 1.5.1