GSI Object Oriented Online Offline (Go4)  GO4-6.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TGo4FitMinuit.cxx
Go to the documentation of this file.
1 // $Id: TGo4FitMinuit.cxx 2749 2020-04-16 10:05:15Z 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 fuer 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 <iostream>
17 
18 #include "TMinuit.h"
19 #include "TObjString.h"
20 
21 #include "TGo4FitterAbstract.h"
22 #include "TGo4FitMinuitResult.h"
23 
24 class TMinuitEx : public TMinuit {
25  public:
26 
27  TMinuitEx(Int_t NumPars, TGo4FitterAbstract* fitter);
28  virtual ~TMinuitEx();
29 
30  virtual Int_t Eval(Int_t npar, Double_t *grad, Double_t &fval, Double_t *pars, Int_t iflag);
31 
32  protected:
33 
35 };
36 
37 TMinuitEx::TMinuitEx(Int_t NumPars, TGo4FitterAbstract* fitter) :
38  TMinuit(NumPars), fxFitter(fitter) {
39 }
40 
42 }
43 
44 Int_t TMinuitEx::Eval(Int_t npar, Double_t *grad, Double_t &fval, Double_t *pars, Int_t iflag) {
45  if (fxFitter) fval = fxFitter->CalculateFitFunction(pars);
46  else fval = 0.;
47  return 0;
48 }
49 
50 // *******************************************************************************
51 
52 TGo4FitMinuit::TGo4FitMinuit() : TGo4FitterAction(), fxCommands(), fxResults() {
53 }
54 
55 TGo4FitMinuit::TGo4FitMinuit(const char* Name) :
56  TGo4FitterAction(Name,"Fitter minimization using TMinuit object"), fxCommands(), fxResults() {
57  fxCommands.SetOwner(kTRUE);
58  fxResults.SetOwner(kTRUE);
59 }
60 
62 }
63 
64 void TGo4FitMinuit::AddCommand(const char* iCommand)
65 {
66  fxCommands.Add( new TObjString(iCommand) );
67 }
68 
69 const char* TGo4FitMinuit::GetCommand(Int_t n)
70 {
71  return ((TObjString*) fxCommands[n])->GetString().Data();
72 }
73 
75 
76  TMinuitEx fMinuit(Fitter->NumPars(), Fitter);
77  fMinuit.SetPrintLevel(-1);
78 
79  for(Int_t n=0;n<Fitter->NumPars();n++) {
80  const char* FullName = Fitter->GetParFullName(n);
81  Int_t ierflg = 0;
82  Double_t epsilon = 0, RangeMin = 0, RangeMax = 0;
83  if (!Fitter->GetParEpsilon(FullName,epsilon)) epsilon = 1.;
84 
85  if (!Fitter->GetParRange(FullName,RangeMin,RangeMax)) { RangeMin = 0; RangeMax = 0; }
86 
87  fMinuit.mnparm(n, FullName, Fitter->GetParValue(FullName),
88  epsilon, RangeMin, RangeMax, ierflg);
89 
90  if (Fitter->GetParFixed(FullName)) fMinuit.FixParameter(n);
91  }
92 
93  if (fxCommands.GetLast()<0) fMinuit.Command("MIGRAD 500 1");
94  else
95  for(Int_t n=0;n<=fxCommands.GetLast();n++) {
96  TString cmd ( ((TObjString*) fxCommands[n])->GetString() );
97  if (cmd[0] == 'r') {
98  if (cmd.Index("result",6,0,TString::kIgnoreCase) == 0) {
99  cmd.Remove(0,6);
100  while ((cmd.Length()>0) && (cmd[0]==' ')) cmd.Remove(0,1);
101  if (cmd.Length()==0) cmd = "1000";
102  if (cmd.Length()<4) { std::cerr << "invalid result command syntax" << std::endl; break; }
103  Bool_t getpar = (cmd[0]=='1');
104  Bool_t geterr = (cmd[1]=='1');
105  Bool_t getmatr = (cmd[2]=='1');
106  Bool_t getcontr = (cmd[3]=='1');
107  cmd.Remove(0,4);
108  while ((cmd.Length()>0) && (cmd[0]==' ')) cmd.Remove(0,1);
109  if (cmd.Length()==0) cmd="Result";
110 
111  TGo4FitMinuitResult *res = new TGo4FitMinuitResult(cmd,"TMinuit result object");
112  fxResults.Add(res);
113  res->CallMNSTAT(&fMinuit);
114  if (getpar) res->CallMNPOUT(&fMinuit,Fitter->NumPars());
115  if (geterr) res->CallMNERRS(&fMinuit,Fitter->NumPars());
116  if (getmatr) res->CallMNEMAT(&fMinuit,Fitter->NumPars(), kTRUE);
117  if (getcontr) res->GetContourPlot(&fMinuit);
118  }
119 
120  } else fMinuit.Command(cmd);
121  }
122 
123  for(Int_t n=0;n<Fitter->NumPars();n++) {
124  Double_t value,error;
125  fMinuit.GetParameter(n, value, error);
126  Fitter->SetParValue(Fitter->GetParFullName(n),value);
127  Fitter->SetParError(Fitter->GetParFullName(n),error);
128  }
129 }
130 
132 {
133  return (indx>=0) && (indx<=fxResults.GetLast()) ? (TGo4FitMinuitResult*) fxResults.At(indx) : 0;
134 }
135 
137 {
138  return (TGo4FitMinuitResult*) fxResults.FindObject(ResName);
139 }
140 
142 {
143  fxResults.Add(res);
144 }
145 
147 {
148  fxResults.Remove(res);
149  fxResults.Compress();
150 }
151 
152 void TGo4FitMinuit::Print(Option_t* option) const
153 {
154  TGo4FitterAction::Print(option);
155  if (fxCommands.GetLast()>=0)
156  std::cout << "List of commands:" << std::endl;
157  for(Int_t n=0;n<=fxCommands.GetLast();n++)
158  std::cout << " " << ((TObjString*) fxCommands[n])->String().Data() << std::endl;
159  if (fxResults.GetLast()>=0) {
160  std::cout << "List of stored results:" << std::endl;
161  fxResults.Print(option);
162  }
163 }
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)