GSI Object Oriented Online Offline (Go4)  GO4-5.3.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TGo4FitMinuit.cxx
Go to the documentation of this file.
1 // $Id: TGo4FitMinuit.cxx 933 2013-01-29 15:27:58Z linev $
2 //-----------------------------------------------------------------------
3 // The GSI Online Offline Object Oriented (Go4) Project
4 // Experiment Data Processing at EE department, GSI
5 //-----------------------------------------------------------------------
6 // Copyright (C) 2000- GSI Helmholtzzentrum für Schwerionenforschung GmbH
7 // Planckstr. 1, 64291 Darmstadt, Germany
8 // Contact: http://go4.gsi.de
9 //-----------------------------------------------------------------------
10 // This software can be used under the license agreements as stated
11 // in Go4License.txt file which is part of the distribution.
12 //-----------------------------------------------------------------------
13 
14 #include "TGo4FitMinuit.h"
15 
16 #include "Riostream.h"
17 
18 #include "TMinuit.h"
19 #include "TArrayD.h"
20 #include "TMatrixD.h"
21 #include "TObjString.h"
22 
23 #include "TGo4FitterAbstract.h"
24 #include "TGo4FitMinuitResult.h"
25 
26 class TMinuitEx : public TMinuit {
27  public:
28 
29  TMinuitEx(Int_t NumPars, TGo4FitterAbstract* fitter);
30  virtual ~TMinuitEx();
31 
32  virtual Int_t Eval(Int_t npar, Double_t *grad, Double_t &fval, Double_t *pars, Int_t iflag);
33 
34  protected:
35 
37 };
38 
39 TMinuitEx::TMinuitEx(Int_t NumPars, TGo4FitterAbstract* fitter) :
40  TMinuit(NumPars), fxFitter(fitter) {
41 }
42 
44 }
45 
46 Int_t TMinuitEx::Eval(Int_t npar, Double_t *grad, Double_t &fval, Double_t *pars, Int_t iflag) {
47  if (fxFitter) fval = fxFitter->CalculateFitFunction(pars);
48  else fval = 0.;
49  return 0;
50 }
51 
52 // *******************************************************************************
53 
54 TGo4FitMinuit::TGo4FitMinuit() : TGo4FitterAction(), fxCommands(), fxResults() {
55 }
56 
57 TGo4FitMinuit::TGo4FitMinuit(const char* Name) :
58  TGo4FitterAction(Name,"Fitter minimization using TMinuit object"), fxCommands(), fxResults() {
59  fxCommands.SetOwner(kTRUE);
60  fxResults.SetOwner(kTRUE);
61 }
62 
64 }
65 
66 void TGo4FitMinuit::AddCommand(const char* iCommand)
67 {
68  fxCommands.Add( new TObjString(iCommand) );
69 }
70 
71 const char* TGo4FitMinuit::GetCommand(Int_t n)
72 {
73  return ((TObjString*) fxCommands[n])->GetString().Data();
74 }
75 
77 
78  TMinuitEx fMinuit(Fitter->NumPars(), Fitter);
79  fMinuit.SetPrintLevel(-1);
80 
81  for(Int_t n=0;n<Fitter->NumPars();n++) {
82  const char* FullName = Fitter->GetParFullName(n);
83  Int_t ierflg = 0;
84  Double_t epsilon = 0, RangeMin = 0, RangeMax = 0;
85  if (!Fitter->GetParEpsilon(FullName,epsilon)) epsilon = 1.;
86 
87  if (!Fitter->GetParRange(FullName,RangeMin,RangeMax)) { RangeMin = 0; RangeMax = 0; }
88 
89  fMinuit.mnparm(n, FullName, Fitter->GetParValue(FullName),
90  epsilon, RangeMin, RangeMax, ierflg);
91 
92  if (Fitter->GetParFixed(FullName)) fMinuit.FixParameter(n);
93  }
94 
95  if (fxCommands.GetLast()<0) fMinuit.Command("MIGRAD 500 1");
96  else
97  for(Int_t n=0;n<=fxCommands.GetLast();n++) {
98  TString cmd ( ((TObjString*) fxCommands[n])->GetString() );
99  if (cmd[0] == 'r') {
100  if (cmd.Index("result",6,0,TString::kIgnoreCase) == 0) {
101  cmd.Remove(0,6);
102  while ((cmd.Length()>0) && (cmd[0]==' ')) cmd.Remove(0,1);
103  if (cmd.Length()==0) cmd = "1000";
104  if (cmd.Length()<4) { std::cerr << "invalid result command syntax" << std::endl; break; }
105  Bool_t getpar = (cmd[0]=='1');
106  Bool_t geterr = (cmd[1]=='1');
107  Bool_t getmatr = (cmd[2]=='1');
108  Bool_t getcontr = (cmd[3]=='1');
109  cmd.Remove(0,4);
110  while ((cmd.Length()>0) && (cmd[0]==' ')) cmd.Remove(0,1);
111  if (cmd.Length()==0) cmd="Result";
112 
113  TGo4FitMinuitResult *res = new TGo4FitMinuitResult(cmd,"TMinuit result object");
114  fxResults.Add(res);
115  res->CallMNSTAT(&fMinuit);
116  if (getpar) res->CallMNPOUT(&fMinuit,Fitter->NumPars());
117  if (geterr) res->CallMNERRS(&fMinuit,Fitter->NumPars());
118  if (getmatr) res->CallMNEMAT(&fMinuit,Fitter->NumPars(), kTRUE);
119  if (getcontr) res->GetContourPlot(&fMinuit);
120  }
121 
122  } else fMinuit.Command(cmd);
123  }
124 
125  for(Int_t n=0;n<Fitter->NumPars();n++) {
126  Double_t value,error;
127  fMinuit.GetParameter(n, value, error);
128  Fitter->SetParValue(Fitter->GetParFullName(n),value);
129  Fitter->SetParError(Fitter->GetParFullName(n),error);
130  }
131 }
132 
134 {
135  return (indx>=0) && (indx<=fxResults.GetLast()) ? (TGo4FitMinuitResult*) fxResults.At(indx) : 0;
136 }
137 
139 {
140  return (TGo4FitMinuitResult*) fxResults.FindObject(ResName);
141 }
142 
144 {
145  fxResults.Add(res);
146 }
147 
149 {
150  fxResults.Remove(res);
151  fxResults.Compress();
152 }
153 
154 void TGo4FitMinuit::Print(Option_t* option) const
155 {
156  TGo4FitterAction::Print(option);
157  if (fxCommands.GetLast()>=0)
158  std::cout << "List of commands:" << std::endl;
159  for(Int_t n=0;n<=fxCommands.GetLast();n++)
160  std::cout << " " << ((TObjString*) fxCommands[n])->String().Data() << std::endl;
161  if (fxResults.GetLast()>=0) {
162  std::cout << "List of stored results:" << std::endl;
163  fxResults.Print(option);
164  }
165 }
Bool_t SetParValue(const char *ParName, Double_t iValue)
virtual Bool_t GetParEpsilon(const char *ParName, Double_t &Epsilon)
void GetContourPlot(TMinuit *fMinuit)
TGo4FitterAbstract * fxFitter
void RemoveResult(TGo4FitMinuitResult *res)
TObjArray fxResults
TMinuitEx(Int_t NumPars, TGo4FitterAbstract *fitter)
void AddCommand(const char *iCommand)
virtual Int_t NumPars()
virtual Bool_t GetParFixed(const char *ParName)
void CallMNERRS(TMinuit *fMinuit, Int_t nPars)
const char * GetCommand(Int_t n)
void CallMNEMAT(TMinuit *fMinuit, Int_t nPars, Bool_t DoTransform=kTRUE)
Bool_t SetParError(const char *ParName, Double_t iError)
void Print(Option_t *option) const
const char * GetParFullName(Int_t n)
TGo4FitMinuitResult * GetResult(Int_t indx)
virtual ~TMinuitEx()
virtual ~TGo4FitMinuit()
Double_t CalculateFitFunction(Double_t *pars=0)
virtual void DoAction(TGo4FitterAbstract *Fitter)
virtual Bool_t GetParRange(const char *ParName, Double_t &RangeMin, Double_t &RangeMax)
virtual Int_t Eval(Int_t npar, Double_t *grad, Double_t &fval, Double_t *pars, Int_t iflag)
virtual void Print(Option_t *option) const
TGo4FitMinuitResult * FindResult(const char *ResName)
void CallMNSTAT(TMinuit *fMinuit)
void AddResult(TGo4FitMinuitResult *res)
void CallMNPOUT(TMinuit *fMinuit, Int_t nPars)
TObjArray fxCommands
Double_t GetParValue(const char *ParName)