source: trunk/MagicSoft/Mars/macros/estfit.C@ 5489

Last change on this file since 5489 was 2744, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 5.6 KB
Line 
1#include <TStopwatch.h>
2#include <TMinuit.h>
3
4#include "MParList.h"
5#include "MTaskList.h"
6#include "MEvtLoop.h"
7
8#include "MReadTree.h"
9#include "MHMatrix.h"
10#include "MChisqEval.h"
11#include "MMatrixLoop.h"
12#include "MParameterD.h"
13#include "MDataMember.h"
14#include "MDataElement.h"
15#include "MEnergyEstParam.h"
16
17// --------------------------------------------------------------------------
18
19void fcn(Int_t &npar, Double_t *gin, Double_t &f, Double_t *par, Int_t iflag)
20{
21 MEvtLoop *evtloop = (MEvtLoop*)gMinuit->GetObjectFit();
22
23 MParList *plist = evtloop->GetParList();
24 MTaskList *tlist = evtloop->GetTaskList();
25
26 MChisqEval *eval = (MChisqEval*) plist->FindObject("MFitResult", "MParameterD");
27 MEnergyEstParam *eest = (MEnergyEstParam*)tlist->FindObject("MEnergyEstParam");
28
29 eest->SetCoeff(TArrayD(eest->GetNumCoeff(), par));
30
31 evtloop->Eventloop();
32
33 f = eval->GetVal();
34}
35
36// --------------------------------------------------------------------------
37//
38// 0: fit impact parameter only
39// 1: fit energy only
40// 2: fit all parameters with respect to the energy resolution
41//
42void estfit(Int_t evalenergy=0)
43{
44 //
45 // Fill events into a MHMatrix
46 //
47 MParList parlist;
48 MHMatrix matrix;
49
50 Int_t col = matrix.AddColumn(evalenergy?"MMcEvt.fEnergy":"MMcEvt.fImpact");
51
52 MEnergyEstParam eest("Hillas");
53 eest.Add("HillasSrc");
54 eest.InitMapping(&matrix);
55
56 MReadTree read("Events", "MC_ON2_short.root");
57 read.DisableAutoScheme();
58
59 if (!matrix.Fill(&parlist, &read))
60 return;
61
62 //
63 // Setup the tasklist used to evaluate the needed chisq
64 //
65 MTaskList tasklist;
66 parlist.AddToList(&tasklist);
67
68 MMatrixLoop loop(&matrix);
69
70 MChisqEval eval;
71 eval.SetY1(new MDataElement(&matrix, col));
72 eval.SetY2(new MDataMember(evalenergy ? "MEnergyEst.fEnergy" : "MEnergyEst.fImpact"));
73 eval.SetOwner();
74
75 tasklist.AddToList(&loop);
76 tasklist.AddToList(&eest);
77 tasklist.AddToList(&eval);
78
79 MEvtLoop evtloop;
80 evtloop.SetParList(&parlist);
81
82 //
83 // Be carefull: This is not thread safe
84 //
85 TMinuit minuit(12);
86 minuit.SetPrintLevel(-1);
87 minuit.SetFCN(fcn);
88
89 // Ready for: minuit.mnexcm("SET ERR", arglist, 1, ierflg)
90 if (minuit.SetErrorDef(1))
91 {
92 cout << "SetErrorDef failed." << endl;
93 return;
94 }
95
96 //
97 // Set initial values
98 //
99 TArrayD fA(5);
100 TArrayD fB(7);
101 fA[0] = 392957;
102 fA[1] = 135;
103 fA[2] = -37449;
104 fA[3] = 0.3464;
105 fA[4] = 1;
106 fB[0] = 1;
107 fB[1] = 1;
108 fB[2] = 1;
109 fB[3] = 1;
110 fB[4] = 1;
111 fB[5] = 1;
112 fB[6] = 1;
113 /*
114 fA[0] = 6122.97;
115 fA[1] = 144.889;
116 fA[2] = -601.256;
117 fA[3] = 0.00171985;
118 fA[4] = 116.451;
119 fB[0] = -2368.21;
120 fB[1] = 1186.26;
121 fB[2] = 0.147235;
122 fB[3] = 144.49;
123 fB[4] = 42.7681;
124 fB[5] = -0.757817;
125 fB[6] = 0.182482;
126 */
127 /*
128 TArrayD fA(5);
129 fA[0] = 6122.97;
130 fA[1] = 144.889;
131 fA[2] = -601.256;
132 fA[3] = 0.00171985;
133 fA[4] = 116.451;
134
135 TArrayD fB(7);
136 fB[0] = -10445.5;
137 fB[1] = 2172.05;
138 fB[2] = 0.69;
139 fB[3] = 491.2;
140 fB[4] = 4.71444;
141 fB[5] = -0.0331926;
142 fB[6] = -0.014833;
143 */
144 // Set starting values and step sizes for parameters
145 for (Int_t i=0; i<fA.GetSize(); i++)
146 {
147 TString name = "fA[";
148 name += i;
149 name += "]";
150 Double_t vinit = fA[i];
151 Double_t step = fabs(fA[i]/3);
152
153 Double_t limlo = 0; // limlo=limup=0: no limits
154 Double_t limup = 0;
155
156 Bool_t rc = minuit.DefineParameter(i, name, vinit, step, limlo, limup);
157 if (evalenergy==1)
158 {
159 minuit.FixParameter(i);
160 cout << "Fixed Parameter #" << i << endl;
161 }
162
163 if (!rc)
164 continue;
165
166 cout << "Error in defining parameter #" << i << endl;
167 return;
168 }
169
170 for (Int_t i=0; i<fB.GetSize(); i++)
171 {
172 TString name = "fB[";
173 name += i;
174 name += "]";
175 Double_t vinit = fB[i];
176 Double_t step = fabs(fB[i]/3);
177
178 Double_t limlo = 0; // limlo=limup=0: no limits
179 Double_t limup = 0;
180
181 Bool_t rc = minuit.DefineParameter(i+fA.GetSize(), name, vinit, step, limlo, limup);
182 if (evalenergy==0)
183 {
184 minuit.FixParameter(i+fA.GetSize());
185 cout << "Fixed Parameter #" << i+fA.GetSize() << endl;
186 }
187
188 if (!rc)
189 continue;
190
191 cout << "Error in defining parameter #" << i+fA.GetSize() << endl;
192 return;
193 }
194
195 //
196 // Setup globals used in FCN
197 //
198 minuit.SetObjectFit(&evtloop);
199
200 cout << endl << "Fitting procedure running..." << endl;
201
202 TStopwatch clock;
203 clock.Start();
204
205 // Now ready for minimization step: minuit.mnexcm("MIGRAD", arglist, 1, ierflg)
206 gLog.SetNullOutput(kTRUE);
207 Bool_t rc = minuit.Migrad();
208 gLog.SetNullOutput(kFALSE);
209 if (rc)
210 {
211 cout << "Migrad failed." << endl;
212 return;
213 }
214
215 cout << endl;
216 clock.Stop();
217 clock.Print();
218 cout << endl;
219
220 for (Int_t i=(evalenergy==1?fA.GetSize():0); i<(evalenergy>0?fA.GetSize()+fB.GetSize():fA.GetSize()); i++)
221 //for (Int_t i=0; i<fA.GetSize()+fB.GetSize(); i++)
222 {
223 Double_t val;
224 Double_t er;
225
226 if (!minuit.GetParameter(i, val, er))
227 cout << "Error getting parameter #" << i << endl;
228
229 cout << i << ": " << val << " +- " << er << endl;
230 }
231
232 /*
233 // Print results
234 Double_t amin, edm, errdef;
235 Int_t nvpar, nparx, icstat;
236 minuit.mnstat(amin, edm, errdef, nvpar, nparx, icstat);
237 minuit.mnprin(3, amin);
238 */
239}
Note: See TracBrowser for help on using the repository browser.