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

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