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 | //------------------------------------------------------------------------
|
---|
101 | //
|
---|
102 | // Optimization (via migrad minimization) of parameters of energy estimation.
|
---|
103 | //
|
---|
104 | void MMcEnergyEst::FindParams()
|
---|
105 | {
|
---|
106 | MParList parlist;
|
---|
107 |
|
---|
108 | MFEventSelector selector;
|
---|
109 | selector.SetNumSelectEvts(fNevents);
|
---|
110 | *fLog << inf << "Read events from file '" << fInFile << "'" << endl;
|
---|
111 |
|
---|
112 | MReadTree read("Events");
|
---|
113 | read.AddFile(fInFile);
|
---|
114 | read.DisableAutoScheme();
|
---|
115 | read.SetSelector(&selector);
|
---|
116 |
|
---|
117 | *fLog << inf << "Define columns of matrix" << endl;
|
---|
118 | MHMatrix matrix;
|
---|
119 | Int_t colenergy = matrix.AddColumn("MMcEvt.fEnergy");
|
---|
120 | Int_t colimpact = matrix.AddColumn("MMcEvt.fImpact");
|
---|
121 |
|
---|
122 | if (colenergy < 0 || colimpact < 0)
|
---|
123 | {
|
---|
124 | *fLog << err << dbginf << "colenergy, colimpact = " << colenergy << ", "
|
---|
125 | << colimpact << endl;
|
---|
126 | return;
|
---|
127 | }
|
---|
128 |
|
---|
129 | MEnergyEstParam eest(fHillasName);
|
---|
130 | eest.Add(fHillasSrcName);
|
---|
131 | eest.InitMapping(&matrix);
|
---|
132 |
|
---|
133 | *fLog << inf << "--------------------------------------" << endl;
|
---|
134 | *fLog << inf << "Fill events into the matrix" << endl;
|
---|
135 | if ( !matrix.Fill(&parlist, &read, fEventFilter) )
|
---|
136 | return;
|
---|
137 | *fLog << inf << "Matrix was filled with " << matrix.GetNumRows()
|
---|
138 | << inf << " events" << endl;
|
---|
139 |
|
---|
140 | //-----------------------------------------------------------------------
|
---|
141 | //
|
---|
142 | // Setup the eventloop which will be executed in the fcn of MINUIT
|
---|
143 | //
|
---|
144 | *fLog << inf << "--------------------------------------" << endl;
|
---|
145 | *fLog << inf << "Setup event loop to be used in 'fcn'" << endl;
|
---|
146 |
|
---|
147 | MTaskList tasklist;
|
---|
148 |
|
---|
149 | MMatrixLoop loop(&matrix);
|
---|
150 |
|
---|
151 | MChisqEval eval;
|
---|
152 | eval.SetY1(new MDataElement(&matrix, colenergy));
|
---|
153 | eval.SetY2(new MDataMember("MEnergyEst.fEnergy"));
|
---|
154 | eval.SetOwner();
|
---|
155 |
|
---|
156 | //
|
---|
157 | // Entries in MParList
|
---|
158 |
|
---|
159 | parlist.AddToList(&tasklist);
|
---|
160 |
|
---|
161 | //
|
---|
162 | // Entries in MTaskList
|
---|
163 |
|
---|
164 | tasklist.AddToList(&loop);
|
---|
165 | tasklist.AddToList(&eest);
|
---|
166 | tasklist.AddToList(&eval);
|
---|
167 |
|
---|
168 |
|
---|
169 | *fLog << inf << "Event loop was setup" << endl;
|
---|
170 | MEvtLoop evtloop;
|
---|
171 | evtloop.SetParList(&parlist);
|
---|
172 |
|
---|
173 | //
|
---|
174 | //---------- Start of minimization part --------------------
|
---|
175 | //
|
---|
176 | // Do the minimization with MINUIT
|
---|
177 | //
|
---|
178 | // Be careful: This is not thread safe
|
---|
179 | //
|
---|
180 | *fLog << inf << "--------------------------------------" << endl;
|
---|
181 | *fLog << inf << "Start minimization" << endl;
|
---|
182 |
|
---|
183 | gMinuit = new TMinuit(12);
|
---|
184 | gMinuit->SetPrintLevel(-1);
|
---|
185 |
|
---|
186 | gMinuit->SetFCN(fcn);
|
---|
187 | gMinuit->SetObjectFit(&evtloop);
|
---|
188 |
|
---|
189 | // Ready for: gMinuit->mnexcm("SET ERR", arglist, 1, ierflg)
|
---|
190 |
|
---|
191 | if (gMinuit->SetErrorDef(1))
|
---|
192 | {
|
---|
193 | *fLog << err << dbginf << "SetErrorDef failed." << endl;
|
---|
194 | return;
|
---|
195 | }
|
---|
196 |
|
---|
197 | //
|
---|
198 | // Set initial values of the parameters (close enough to the final ones, taken
|
---|
199 | // from previous runs of the procedure). Parameter fA[4] is not used in the default
|
---|
200 | // energy estimation model (from D. Kranich).
|
---|
201 | //
|
---|
202 | fA.Set(5);
|
---|
203 | fB.Set(7);
|
---|
204 |
|
---|
205 | fA[0] = 21006.2;
|
---|
206 | fA[1] = -43.2648;
|
---|
207 | fA[2] = -690.403;
|
---|
208 | fA[3] = -0.0428544;
|
---|
209 | fA[4] = 1.;
|
---|
210 | fB[0] = -3391.05;
|
---|
211 | fB[1] = 136.58;
|
---|
212 | fB[2] = 0.253807;
|
---|
213 | fB[3] = 254.363;
|
---|
214 | fB[4] = 61.0386;
|
---|
215 | fB[5] = -0.0190984;
|
---|
216 | fB[6] = -421695;
|
---|
217 |
|
---|
218 | //
|
---|
219 | // Set starting values and step sizes for parameters
|
---|
220 | //
|
---|
221 | for (Int_t i=0; i<fA.GetSize(); i++)
|
---|
222 | {
|
---|
223 | TString name = "fA[";
|
---|
224 | name += i;
|
---|
225 | name += "]";
|
---|
226 | Double_t vinit = fA[i];
|
---|
227 | Double_t step = fabs(fA[i]/3);
|
---|
228 |
|
---|
229 | Double_t limlo = 0; // limlo=limup=0: no limits
|
---|
230 | Double_t limup = 0;
|
---|
231 |
|
---|
232 | Bool_t rc = gMinuit->DefineParameter(i, name, vinit, step, limlo, limup);
|
---|
233 | if (!rc)
|
---|
234 | continue;
|
---|
235 |
|
---|
236 | *fLog << err << dbginf << "Error in defining parameter #" << i << endl;
|
---|
237 | return;
|
---|
238 | }
|
---|
239 |
|
---|
240 | for (Int_t i=0; i<fB.GetSize(); i++)
|
---|
241 | {
|
---|
242 | TString name = "fB[";
|
---|
243 | name += i;
|
---|
244 | name += "]";
|
---|
245 | Double_t vinit = fB[i];
|
---|
246 | Double_t step = fabs(fB[i]/3);
|
---|
247 |
|
---|
248 | Double_t limlo = 0; // limlo=limup=0: no limits
|
---|
249 | Double_t limup = 0;
|
---|
250 |
|
---|
251 | Bool_t rc = gMinuit->DefineParameter(i+fA.GetSize(), name, vinit, step, limlo, limup);
|
---|
252 | if (!rc)
|
---|
253 | continue;
|
---|
254 |
|
---|
255 | *fLog << err << dbginf << "Error in defining parameter #" << i+fA.GetSize() << endl;
|
---|
256 | return;
|
---|
257 | }
|
---|
258 |
|
---|
259 | TStopwatch clock;
|
---|
260 | clock.Start();
|
---|
261 |
|
---|
262 | // Now ready for minimization step:
|
---|
263 |
|
---|
264 | gLog.SetNullOutput(kTRUE);
|
---|
265 | Bool_t rc = gMinuit->Migrad();
|
---|
266 | gLog.SetNullOutput(kFALSE);
|
---|
267 |
|
---|
268 | if (rc)
|
---|
269 | {
|
---|
270 | *fLog << err << dbginf << "Migrad failed." << endl;
|
---|
271 | return;
|
---|
272 | }
|
---|
273 |
|
---|
274 | *fLog << inf << endl;
|
---|
275 | clock.Stop();
|
---|
276 | clock.Print();
|
---|
277 | *fLog << inf << endl;
|
---|
278 |
|
---|
279 | *fLog << inf << "Resulting Chisq: " << gMinuit->fAmin << endl;
|
---|
280 |
|
---|
281 | //
|
---|
282 | // Update values of fA, fB:
|
---|
283 | //
|
---|
284 | for (Int_t i = 0; i < fA.GetSize(); i++)
|
---|
285 | {
|
---|
286 | Double_t x1, x2;
|
---|
287 | gMinuit->GetParameter(i,x1,x2);
|
---|
288 | fA[i] = x1;
|
---|
289 | }
|
---|
290 | for (Int_t i = fA.GetSize(); i < fA.GetSize()+fB.GetSize(); i++)
|
---|
291 | {
|
---|
292 | Double_t x1, x2;
|
---|
293 | gMinuit->GetParameter(i,x1,x2);
|
---|
294 | fB[i-fA.GetSize()] = x1;
|
---|
295 | }
|
---|
296 |
|
---|
297 | // eest.Print();
|
---|
298 | eest.StopMapping();
|
---|
299 | *fLog << inf << "Minimization finished" << endl;
|
---|
300 |
|
---|
301 | }
|
---|
302 |
|
---|
303 | //------------------------------------------------------------------------
|
---|
304 | //
|
---|
305 | // Print current values of parameters
|
---|
306 | //
|
---|
307 | void MMcEnergyEst::Print(Option_t *o)
|
---|
308 | {
|
---|
309 | for (Int_t i = 0; i < fA.GetSize(); i++)
|
---|
310 | *fLog << inf << "Parameter " << i << ": " << fA[i] << endl;
|
---|
311 |
|
---|
312 | for (Int_t i = fA.GetSize(); i < fA.GetSize()+fB.GetSize(); i++)
|
---|
313 | *fLog << inf << "Parameter " << i << ": " << fB[i-fA.GetSize()] << endl;
|
---|
314 |
|
---|
315 | /*
|
---|
316 | // Print results
|
---|
317 | Double_t amin, edm, errdef;
|
---|
318 | Int_t nvpar, nparx, icstat;
|
---|
319 | gMinuit->mnstat(amin, edm, errdef, nvpar, nparx, icstat);
|
---|
320 | gMinuit->mnprin(3, amin);
|
---|
321 | */
|
---|
322 |
|
---|
323 | }
|
---|
324 |
|
---|
325 |
|
---|