1 | /* ======================================================================== *\
|
---|
2 | !
|
---|
3 | ! *
|
---|
4 | ! * This file is part of MARS, the MAGIC Analysis and Reconstruction
|
---|
5 | ! * Software. It is distributed to you in the hope that it can be a useful
|
---|
6 | ! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
|
---|
7 | ! * It is distributed WITHOUT ANY WARRANTY.
|
---|
8 | ! *
|
---|
9 | ! * Permission to use, copy, modify and distribute this software and its
|
---|
10 | ! * documentation for any purpose is hereby granted without fee,
|
---|
11 | ! * provided that the above copyright notice appear in all copies and
|
---|
12 | ! * that both that copyright notice and this permission notice appear
|
---|
13 | ! * in supporting documentation. It is provided "as is" without express
|
---|
14 | ! * or implied warranty.
|
---|
15 | ! *
|
---|
16 | !
|
---|
17 | !
|
---|
18 | ! Author(s): Thomas Bretz 1/2002 <mailto:tbretz@astro.uni-wuerzburg.de>
|
---|
19 | ! Author(s): Wolfgang Wittek 1/2002 <mailto:wittek@mppmu.mpg.de>
|
---|
20 | ! Author(s): Abelardo Moralejo 4/2003 <mailto:moralejo@pd.infn.it>
|
---|
21 | !
|
---|
22 | ! Copyright: MAGIC Software Development, 2000-2003
|
---|
23 | !
|
---|
24 | !
|
---|
25 | \* ======================================================================== */
|
---|
26 |
|
---|
27 | /////////////////////////////////////////////////////////////////////////////
|
---|
28 | // //
|
---|
29 | // MMcEnergyEst //
|
---|
30 | // //
|
---|
31 | // Class for otimizing the parameters of the energy estimator //
|
---|
32 | // //
|
---|
33 | // FIXME: the class must be made more flexible, allowing for different //
|
---|
34 | // parametrizations to be used. Also a new class is needed, a container //
|
---|
35 | // for the parameters of the energy estimator. //
|
---|
36 | // FIXME: the starting values of the parameters are now fixed. //
|
---|
37 | // //
|
---|
38 | // //
|
---|
39 | /////////////////////////////////////////////////////////////////////////////
|
---|
40 | #include "MMcEnergyEst.h"
|
---|
41 |
|
---|
42 | #include <math.h> // fabs on Alpha
|
---|
43 |
|
---|
44 | #include <TMinuit.h>
|
---|
45 | #include <TStopwatch.h>
|
---|
46 | #include <TVirtualFitter.h>
|
---|
47 |
|
---|
48 | #include "MParList.h"
|
---|
49 | #include "MTaskList.h"
|
---|
50 | #include "MGeomCamCT1.h"
|
---|
51 | #include "MFEventSelector.h"
|
---|
52 | #include "MReadTree.h"
|
---|
53 | #include "MFCT1SelFinal.h"
|
---|
54 | #include "MHMatrix.h"
|
---|
55 | #include "MEnergyEstParam.h"
|
---|
56 | #include "MMatrixLoop.h"
|
---|
57 | #include "MChisqEval.h"
|
---|
58 | #include "MEvtLoop.h"
|
---|
59 | #include "MDataElement.h"
|
---|
60 | #include "MDataMember.h"
|
---|
61 | #include "MLog.h"
|
---|
62 | #include "MLogManip.h"
|
---|
63 |
|
---|
64 |
|
---|
65 | //------------------------------------------------------------------------
|
---|
66 | //
|
---|
67 | // fcn calculates the function to be minimized (using TMinuit::Migrad)
|
---|
68 | //
|
---|
69 | void fcn(Int_t &npar, Double_t *gin, Double_t &f, Double_t *par, Int_t iflag)
|
---|
70 | {
|
---|
71 | MEvtLoop *evtloop = (MEvtLoop*)gMinuit->GetObjectFit();
|
---|
72 |
|
---|
73 | MTaskList *tlist = (MTaskList*)evtloop->GetParList()->FindObject("MTaskList"); // GetTaskList();
|
---|
74 |
|
---|
75 | MChisqEval *eval = (MChisqEval*) tlist->FindObject("MChisqEval");
|
---|
76 | MEnergyEstParam *eest = (MEnergyEstParam*)tlist->FindObject("MEnergyEstParam");
|
---|
77 |
|
---|
78 | eest->SetCoeff(TArrayD(eest->GetNumCoeff(), par));
|
---|
79 |
|
---|
80 | evtloop->Eventloop();
|
---|
81 |
|
---|
82 | f = eval->GetChisq();
|
---|
83 | }
|
---|
84 |
|
---|
85 | ClassImp(MMcEnergyEst);
|
---|
86 |
|
---|
87 | // --------------------------------------------------------------------------
|
---|
88 | //
|
---|
89 | // Default constructor.
|
---|
90 | //
|
---|
91 | MMcEnergyEst::MMcEnergyEst(const char *name, const char *title)
|
---|
92 | {
|
---|
93 | fName = name ? name : "MMcEnergyEst";
|
---|
94 | fTitle = title ? title : "Optimizer of the energy estimator";
|
---|
95 |
|
---|
96 | SetHillasName("MHillas");
|
---|
97 | SetHillasSrcName("MHillasSrc");
|
---|
98 |
|
---|
99 | //
|
---|
100 | // Set initial values of the parameters (close enough to the final
|
---|
101 | // ones, taken from previous runs of the procedure). Parameter
|
---|
102 | // fA[4] is not used in the default energy estimation model (from
|
---|
103 | // D. Kranich).
|
---|
104 | //
|
---|
105 | fA.Set(5);
|
---|
106 | fB.Set(7);
|
---|
107 |
|
---|
108 | fA[0] = 21006.2;
|
---|
109 | fA[1] = -43.2648;
|
---|
110 | fA[2] = -690.403;
|
---|
111 | fA[3] = -0.0428544;
|
---|
112 | fA[4] = 1.;
|
---|
113 | fB[0] = -3391.05;
|
---|
114 | fB[1] = 136.58;
|
---|
115 | fB[2] = 0.253807;
|
---|
116 | fB[3] = 254.363;
|
---|
117 | fB[4] = 61.0386;
|
---|
118 | fB[5] = -0.0190984;
|
---|
119 | fB[6] = -421695;
|
---|
120 | }
|
---|
121 |
|
---|
122 | Bool_t MMcEnergyEst::SetCoeff(TArrayD &coeff)
|
---|
123 | {
|
---|
124 | if (coeff.GetSize() != fA.GetSize()+fB.GetSize())
|
---|
125 | {
|
---|
126 | *fLog << err << dbginf << "Wrong number of parameters!" << endl;
|
---|
127 | return(kFALSE);
|
---|
128 | }
|
---|
129 |
|
---|
130 | for (Int_t i = 0; i < fA.GetSize(); i++)
|
---|
131 | fA[i] = coeff[i];
|
---|
132 | for (Int_t i = 0; i < fB.GetSize(); i++)
|
---|
133 | fB[i] = coeff[i+fA.GetSize()];
|
---|
134 |
|
---|
135 | return(kTRUE);
|
---|
136 |
|
---|
137 | }
|
---|
138 |
|
---|
139 | //------------------------------------------------------------------------
|
---|
140 | //
|
---|
141 | // Optimization (via migrad minimization) of parameters of energy estimation.
|
---|
142 | //
|
---|
143 | void MMcEnergyEst::FindParams()
|
---|
144 | {
|
---|
145 | MParList parlist;
|
---|
146 |
|
---|
147 | MFEventSelector selector;
|
---|
148 | selector.SetNumSelectEvts(fNevents);
|
---|
149 | *fLog << inf << "Read events from file '" << fInFile << "'" << endl;
|
---|
150 |
|
---|
151 | MReadTree read("Events");
|
---|
152 | read.AddFile(fInFile);
|
---|
153 | read.DisableAutoScheme();
|
---|
154 | read.SetSelector(&selector);
|
---|
155 |
|
---|
156 | *fLog << inf << "Define columns of matrix" << endl;
|
---|
157 | MHMatrix matrix;
|
---|
158 | Int_t colenergy = matrix.AddColumn("MMcEvt.fEnergy");
|
---|
159 | Int_t colimpact = matrix.AddColumn("MMcEvt.fImpact");
|
---|
160 |
|
---|
161 | if (colenergy < 0 || colimpact < 0)
|
---|
162 | {
|
---|
163 | *fLog << err << dbginf << "colenergy, colimpact = " << colenergy << ", "
|
---|
164 | << colimpact << endl;
|
---|
165 | return;
|
---|
166 | }
|
---|
167 |
|
---|
168 | MEnergyEstParam eest(fHillasName);
|
---|
169 | eest.Add(fHillasSrcName);
|
---|
170 | eest.InitMapping(&matrix);
|
---|
171 |
|
---|
172 | *fLog << inf << "--------------------------------------" << endl;
|
---|
173 | *fLog << inf << "Fill events into the matrix" << endl;
|
---|
174 | if ( !matrix.Fill(&parlist, &read, fEventFilter) )
|
---|
175 | return;
|
---|
176 | *fLog << inf << "Matrix was filled with " << matrix.GetNumRows()
|
---|
177 | << inf << " events" << endl;
|
---|
178 |
|
---|
179 | //-----------------------------------------------------------------------
|
---|
180 | //
|
---|
181 | // Setup the eventloop which will be executed in the fcn of MINUIT
|
---|
182 | //
|
---|
183 | *fLog << inf << "--------------------------------------" << endl;
|
---|
184 | *fLog << inf << "Setup event loop to be used in 'fcn'" << endl;
|
---|
185 |
|
---|
186 | MTaskList tasklist;
|
---|
187 |
|
---|
188 | MMatrixLoop loop(&matrix);
|
---|
189 |
|
---|
190 | MChisqEval eval;
|
---|
191 | eval.SetY1(new MDataElement(&matrix, colenergy));
|
---|
192 | eval.SetY2(new MDataMember("MEnergyEst.fEnergy"));
|
---|
193 | eval.SetOwner();
|
---|
194 |
|
---|
195 | //
|
---|
196 | // Entries in MParList
|
---|
197 |
|
---|
198 | parlist.AddToList(&tasklist);
|
---|
199 |
|
---|
200 | //
|
---|
201 | // Entries in MTaskList
|
---|
202 |
|
---|
203 | tasklist.AddToList(&loop);
|
---|
204 | tasklist.AddToList(&eest);
|
---|
205 | tasklist.AddToList(&eval);
|
---|
206 |
|
---|
207 |
|
---|
208 | *fLog << inf << "Event loop was setup" << endl;
|
---|
209 | MEvtLoop evtloop;
|
---|
210 | evtloop.SetParList(&parlist);
|
---|
211 |
|
---|
212 | //
|
---|
213 | //---------- Start of minimization part --------------------
|
---|
214 | //
|
---|
215 | // Do the minimization with MINUIT
|
---|
216 | //
|
---|
217 | // Be careful: This is not thread safe
|
---|
218 | //
|
---|
219 | *fLog << inf << "--------------------------------------" << endl;
|
---|
220 | *fLog << inf << "Start minimization" << endl;
|
---|
221 |
|
---|
222 | gMinuit = new TMinuit(12);
|
---|
223 | gMinuit->SetPrintLevel(-1);
|
---|
224 |
|
---|
225 | gMinuit->SetFCN(fcn);
|
---|
226 | gMinuit->SetObjectFit(&evtloop);
|
---|
227 |
|
---|
228 | // Ready for: gMinuit->mnexcm("SET ERR", arglist, 1, ierflg)
|
---|
229 |
|
---|
230 | if (gMinuit->SetErrorDef(1))
|
---|
231 | {
|
---|
232 | *fLog << err << dbginf << "SetErrorDef failed." << endl;
|
---|
233 | return;
|
---|
234 | }
|
---|
235 |
|
---|
236 | //
|
---|
237 | // Set starting values and step sizes for parameters
|
---|
238 | //
|
---|
239 | for (Int_t i=0; i<fA.GetSize(); i++)
|
---|
240 | {
|
---|
241 | TString name = "fA[";
|
---|
242 | name += i;
|
---|
243 | name += "]";
|
---|
244 | Double_t vinit = fA[i];
|
---|
245 | Double_t step = fabs(fA[i]/3);
|
---|
246 |
|
---|
247 | Double_t limlo = 0; // limlo=limup=0: no limits
|
---|
248 | Double_t limup = 0;
|
---|
249 |
|
---|
250 | Bool_t rc = gMinuit->DefineParameter(i, name, vinit, step, limlo, limup);
|
---|
251 | if (!rc)
|
---|
252 | continue;
|
---|
253 |
|
---|
254 | *fLog << err << dbginf << "Error in defining parameter #" << i << endl;
|
---|
255 | return;
|
---|
256 | }
|
---|
257 |
|
---|
258 | for (Int_t i=0; i<fB.GetSize(); i++)
|
---|
259 | {
|
---|
260 | TString name = "fB[";
|
---|
261 | name += i;
|
---|
262 | name += "]";
|
---|
263 | Double_t vinit = fB[i];
|
---|
264 | Double_t step = fabs(fB[i]/3);
|
---|
265 |
|
---|
266 | Double_t limlo = 0; // limlo=limup=0: no limits
|
---|
267 | Double_t limup = 0;
|
---|
268 |
|
---|
269 | Bool_t rc = gMinuit->DefineParameter(i+fA.GetSize(), name, vinit, step, limlo, limup);
|
---|
270 | if (!rc)
|
---|
271 | continue;
|
---|
272 |
|
---|
273 | *fLog << err << dbginf << "Error in defining parameter #" << i+fA.GetSize() << endl;
|
---|
274 | return;
|
---|
275 | }
|
---|
276 |
|
---|
277 | TStopwatch clock;
|
---|
278 | clock.Start();
|
---|
279 |
|
---|
280 | // Now ready for minimization step:
|
---|
281 |
|
---|
282 | gLog.SetNullOutput(kTRUE);
|
---|
283 | Bool_t rc = gMinuit->Migrad();
|
---|
284 | gLog.SetNullOutput(kFALSE);
|
---|
285 |
|
---|
286 | if (rc)
|
---|
287 | {
|
---|
288 | *fLog << err << dbginf << "Migrad failed." << endl;
|
---|
289 | return;
|
---|
290 | }
|
---|
291 |
|
---|
292 | *fLog << inf << endl;
|
---|
293 | clock.Stop();
|
---|
294 | clock.Print();
|
---|
295 | *fLog << inf << endl;
|
---|
296 |
|
---|
297 | *fLog << inf << "Resulting Chisq: " << gMinuit->fAmin << endl;
|
---|
298 |
|
---|
299 | //
|
---|
300 | // Update values of fA, fB:
|
---|
301 | //
|
---|
302 | for (Int_t i = 0; i < fA.GetSize(); i++)
|
---|
303 | {
|
---|
304 | Double_t x1, x2;
|
---|
305 | gMinuit->GetParameter(i,x1,x2);
|
---|
306 | fA[i] = x1;
|
---|
307 | }
|
---|
308 | for (Int_t i = fA.GetSize(); i < fA.GetSize()+fB.GetSize(); i++)
|
---|
309 | {
|
---|
310 | Double_t x1, x2;
|
---|
311 | gMinuit->GetParameter(i,x1,x2);
|
---|
312 | fB[i-fA.GetSize()] = x1;
|
---|
313 | }
|
---|
314 |
|
---|
315 | // eest.Print();
|
---|
316 | eest.StopMapping();
|
---|
317 | *fLog << inf << "Minimization finished" << endl;
|
---|
318 |
|
---|
319 | }
|
---|
320 |
|
---|
321 | //------------------------------------------------------------------------
|
---|
322 | //
|
---|
323 | // Print current values of parameters
|
---|
324 | //
|
---|
325 | void MMcEnergyEst::Print(Option_t *o)
|
---|
326 | {
|
---|
327 | for (Int_t i = 0; i < fA.GetSize(); i++)
|
---|
328 | *fLog << inf << "Parameter " << i << ": " << fA[i] << endl;
|
---|
329 |
|
---|
330 | for (Int_t i = fA.GetSize(); i < fA.GetSize()+fB.GetSize(); i++)
|
---|
331 | *fLog << inf << "Parameter " << i << ": " << fB[i-fA.GetSize()] << endl;
|
---|
332 |
|
---|
333 | /*
|
---|
334 | // Print results
|
---|
335 | Double_t amin, edm, errdef;
|
---|
336 | Int_t nvpar, nparx, icstat;
|
---|
337 | gMinuit->mnstat(amin, edm, errdef, nvpar, nparx, icstat);
|
---|
338 | gMinuit->mnprin(3, amin);
|
---|
339 | */
|
---|
340 |
|
---|
341 | }
|
---|
342 |
|
---|
343 |
|
---|