Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

/Go4Fit/TGo4FitMinuit.cxx

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

Generated on Tue Nov 8 10:55:56 2005 for Go4-v2.10-5 by doxygen1.2.15