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, 9/2004 <mailto:tbretz@astro.uni-wuerzburg.de>
|
---|
19 | !
|
---|
20 | ! Copyright: MAGIC Software Development, 2000-2004
|
---|
21 | !
|
---|
22 | !
|
---|
23 | \* ======================================================================== */
|
---|
24 |
|
---|
25 | /////////////////////////////////////////////////////////////////////////////
|
---|
26 | //
|
---|
27 | // MJOptimize
|
---|
28 | //
|
---|
29 | // Class for otimizing the parameters of the supercuts
|
---|
30 | //
|
---|
31 | // Minimization Control
|
---|
32 | // ====================
|
---|
33 | //
|
---|
34 | // To choose the minimization algorithm use:
|
---|
35 | // void SetOptimizer(Optimizer_t o);
|
---|
36 | //
|
---|
37 | // Allowed options are:
|
---|
38 | // enum Optimizer_t
|
---|
39 | // {
|
---|
40 | // kMigrad, // Minimize by the method of Migrad
|
---|
41 | // kSimplex, // Minimize by the method of Simplex
|
---|
42 | // kMinimize, // Migrad + Simplex (if Migrad fails)
|
---|
43 | // kMinos, // Minos error determination
|
---|
44 | // kImprove, // Local minimum search
|
---|
45 | // kSeek, // Minimize by the method of Monte Carlo
|
---|
46 | // kNone // Skip optimization
|
---|
47 | // };
|
---|
48 | //
|
---|
49 | // For more details on the methods see TMinuit.
|
---|
50 | //
|
---|
51 | //
|
---|
52 | // You can change the behaviour of the minimization using
|
---|
53 | //
|
---|
54 | // void SetNumMaxCalls(UInt_t num=0);
|
---|
55 | // void SetTolerance(Float_t tol=0);
|
---|
56 | //
|
---|
57 | // While NumMaxCalls is the first, Tolerance the second arguement.
|
---|
58 | // For more details start root and type
|
---|
59 | //
|
---|
60 | // gMinuit->mnhelp("command")
|
---|
61 | //
|
---|
62 | // while command can be
|
---|
63 | // * MIGRAD
|
---|
64 | // * SIMPLEX
|
---|
65 | // * MINIMIZE
|
---|
66 | // * MINOS
|
---|
67 | // * IMPROVE
|
---|
68 | // * SEEK
|
---|
69 | //
|
---|
70 | // The default (num==0 and tol==0) should always give you the
|
---|
71 | // corresponding defaults used in Minuit.
|
---|
72 | //
|
---|
73 | //
|
---|
74 | // FIXME: Implement changing cut in hadronness...
|
---|
75 | // FIXME: Show MHSignificance on MStatusDisplay during filling...
|
---|
76 | // FIXME: Choose step-size percentage as static data membewr
|
---|
77 | // FIXME: Choose minimization method
|
---|
78 | //
|
---|
79 | /////////////////////////////////////////////////////////////////////////////
|
---|
80 | #include "MJOptimize.h"
|
---|
81 |
|
---|
82 | #include <TMinuit.h>
|
---|
83 | #include <TVirtualFitter.h>
|
---|
84 |
|
---|
85 | #include <TStopwatch.h>
|
---|
86 |
|
---|
87 | #include <TCanvas.h>
|
---|
88 |
|
---|
89 | #include <TGraph.h>
|
---|
90 | #include <TMultiGraph.h>
|
---|
91 |
|
---|
92 | #include "MHMatrix.h"
|
---|
93 |
|
---|
94 | // environment
|
---|
95 | #include "MLog.h"
|
---|
96 | #include "MLogManip.h"
|
---|
97 |
|
---|
98 | #include "MDirIter.h"
|
---|
99 | #include "MStatusDisplay.h"
|
---|
100 |
|
---|
101 | // eventloop
|
---|
102 | #include "MParList.h"
|
---|
103 | #include "MTaskList.h"
|
---|
104 | #include "MEvtLoop.h"
|
---|
105 |
|
---|
106 | // parameters
|
---|
107 | #include "MParameters.h"
|
---|
108 |
|
---|
109 | // tasks
|
---|
110 | #include "MReadTree.h"
|
---|
111 | #include "MMatrixLoop.h"
|
---|
112 | #include "MFillH.h"
|
---|
113 |
|
---|
114 | // filters
|
---|
115 | #include "MFDataPhrase.h"
|
---|
116 | #include "MFilterList.h"
|
---|
117 |
|
---|
118 | using namespace std;
|
---|
119 |
|
---|
120 | //------------------------------------------------------------------------
|
---|
121 | //
|
---|
122 | // fcn calculates the function to be minimized (using TMinuit::Migrad)
|
---|
123 | //
|
---|
124 | void MJOptimize::fcn(Int_t &npar, Double_t *gin, Double_t &f, Double_t *par, Int_t iflag)
|
---|
125 | {
|
---|
126 | MJOptimize *optim = (MJOptimize*)gMinuit->GetObjectFit();
|
---|
127 |
|
---|
128 | // WORKAROUND --- FOR WHAT?
|
---|
129 | if (gMinuit->fEpsi<1e-2)
|
---|
130 | {
|
---|
131 | *optim->fLog << warn << "WARNING - For unknown reasons: fEspi<1e-100... resetting to 0.01." << endl;
|
---|
132 | gMinuit->fEpsi = 0.01;
|
---|
133 | }
|
---|
134 |
|
---|
135 | TMinuit *minuit = gMinuit;
|
---|
136 | f = optim->Fcn(TArrayD(TMath::Min(gMinuit->fMaxpar, optim->fParameters.GetSize()), par), minuit);
|
---|
137 | gMinuit = minuit;
|
---|
138 |
|
---|
139 | }
|
---|
140 |
|
---|
141 | Double_t MJOptimize::Fcn(const TArrayD &par, TMinuit *minuit)
|
---|
142 | {
|
---|
143 | if (fEvtLoop->GetDisplay()!=fDisplay)
|
---|
144 | return 0;
|
---|
145 | /*
|
---|
146 | switch(iflag)
|
---|
147 | {
|
---|
148 | case 1: // first call
|
---|
149 | case 2: // calc derivative
|
---|
150 | break;
|
---|
151 | case 3:
|
---|
152 | // last call
|
---|
153 | MStatusDisplay *d = new MStatusDisplay;
|
---|
154 | fEvtLoop->SetDisplay(d);
|
---|
155 | break;
|
---|
156 | }
|
---|
157 | */
|
---|
158 | MParList *plist = fEvtLoop->GetParList();
|
---|
159 |
|
---|
160 | MParameterD *eval = (MParameterD*)plist->FindObject(fNameMinimizationValue, "MParameterD");
|
---|
161 | MParContainer *pars = (MParContainer*)plist->FindObject("MParameters", "MParContainer");
|
---|
162 |
|
---|
163 | MRead *read = (MRead*)plist->FindObject("MTaskList")->FindObject("MRead");
|
---|
164 | if (read)
|
---|
165 | read->Rewind();
|
---|
166 |
|
---|
167 | if (fDebug>=0)
|
---|
168 | {
|
---|
169 | *fLog << inf << "New Set: ";
|
---|
170 | for (Int_t i=0; i<fParameters.GetSize(); i++)
|
---|
171 | *fLog << par[i] << " ";
|
---|
172 | *fLog << endl;
|
---|
173 | }
|
---|
174 |
|
---|
175 | pars->SetVariables(par);
|
---|
176 | eval->SetVal(0);
|
---|
177 |
|
---|
178 | const Bool_t isnull = gLog.IsNullOutput();
|
---|
179 | if (fDebug<3)
|
---|
180 | gLog.SetNullOutput(kTRUE);
|
---|
181 |
|
---|
182 | TStopwatch clock;
|
---|
183 | clock.Start();
|
---|
184 | fEvtLoop->Eventloop(fNumEvents, MEvtLoop::kNoStatistics);
|
---|
185 | clock.Stop();
|
---|
186 |
|
---|
187 | if (fDebug<3)
|
---|
188 | gLog.SetNullOutput(isnull);
|
---|
189 |
|
---|
190 | const Double_t f = eval->GetVal();
|
---|
191 |
|
---|
192 | if (fDebug>=0)
|
---|
193 | *fLog << inf << "Result F=" << f << endl;
|
---|
194 |
|
---|
195 | if (fDebug>=1 && minuit)
|
---|
196 | {
|
---|
197 | Double_t fmin, fedm, errdef;
|
---|
198 | Int_t n1, n2, istat;
|
---|
199 | minuit->mnstat(fmin, fedm, errdef, n1, n2, istat);
|
---|
200 | *fLog << inf << underline << "Minimization Status so far:" << endl;
|
---|
201 | *fLog << " Calls: " << minuit->fNfcn << " (max=" << gMinuit->fMaxIterations << ")" << endl;
|
---|
202 | *fLog << " Parameters: fixed=" << gMinuit->fNpfix << ", free=" << gMinuit->fNpar << endl;
|
---|
203 | *fLog << " Func min: " << fmin << " (Epsi=" << gMinuit->fEpsi << ", Apsi=" << gMinuit->fApsi << ")" << endl;
|
---|
204 | *fLog << " Found edm: " << fedm << endl;
|
---|
205 | *fLog << " ErrDef: " << errdef << endl;
|
---|
206 | *fLog << " Status: ";
|
---|
207 |
|
---|
208 | switch (istat)
|
---|
209 | {
|
---|
210 | case 0: *fLog << "n/a" << endl; break;
|
---|
211 | case 1: *fLog << "approximation only, not accurate" << endl; break;
|
---|
212 | case 2: *fLog << "full matrix, but forced positive-definite" << endl; break;
|
---|
213 | case 3: *fLog << "full accurate covariance matrix" << endl; break;
|
---|
214 | default: *fLog << "undefined" << endl; break;
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 | if (fDebug>=1)
|
---|
219 | {
|
---|
220 | clock.Print();
|
---|
221 | fEvtLoop->GetTaskList()->PrintStatistics();
|
---|
222 | }
|
---|
223 |
|
---|
224 | return f;
|
---|
225 | }
|
---|
226 |
|
---|
227 | MJOptimize::MJOptimize() : fDebug(-1), fNumEvents(0), fType(kSimplex), fNumMaxCalls(0), fTolerance(0), fTestTrain(0), fNameMinimizationValue("MinimizationValue")
|
---|
228 | {
|
---|
229 | fRules.SetOwner();
|
---|
230 | fFilter.SetOwner();
|
---|
231 |
|
---|
232 | fNamesOn.SetOwner();
|
---|
233 | fNamesOff.SetOwner();
|
---|
234 | }
|
---|
235 |
|
---|
236 | //------------------------------------------------------------------------
|
---|
237 | //
|
---|
238 | // Add sequences from list to reader
|
---|
239 | //
|
---|
240 | Bool_t MJOptimize::AddSequences(MRead &read, TList &list) const
|
---|
241 | {
|
---|
242 | MDirIter files;
|
---|
243 |
|
---|
244 | TIter Next(&list);
|
---|
245 | MSequence *seq=0;
|
---|
246 | while ((seq=(MSequence*)Next()))
|
---|
247 | {
|
---|
248 | if (!seq->IsValid())
|
---|
249 | return kFALSE;
|
---|
250 | if (seq->SetupDatRuns(files)<=0)
|
---|
251 | return kFALSE;
|
---|
252 | }
|
---|
253 |
|
---|
254 | return read.AddFiles(files)>0;
|
---|
255 | }
|
---|
256 |
|
---|
257 | //------------------------------------------------------------------------
|
---|
258 | //
|
---|
259 | // Add on-sequences:
|
---|
260 | // - fname: sequence file name (with path)
|
---|
261 | // - dir: directory were image files are stored
|
---|
262 | //
|
---|
263 | void MJOptimize::AddSequenceOn(const char *fname, const char *dir)
|
---|
264 | {
|
---|
265 | fNamesOn.Add(new MSequence(fname, dir));
|
---|
266 | }
|
---|
267 |
|
---|
268 | //------------------------------------------------------------------------
|
---|
269 | //
|
---|
270 | // Add off-sequences:
|
---|
271 | // - fname: sequence file name (with path)
|
---|
272 | // - dir: directory were image files are stored
|
---|
273 | //
|
---|
274 | void MJOptimize::AddSequenceOff(const char *fname, const char *dir)
|
---|
275 | {
|
---|
276 | fNamesOff.Add(new MSequence(fname, dir));
|
---|
277 | }
|
---|
278 |
|
---|
279 | //------------------------------------------------------------------------
|
---|
280 | //
|
---|
281 | // Empty list of on- and off-sequences
|
---|
282 | //
|
---|
283 | void MJOptimize::ResetSequences()
|
---|
284 | {
|
---|
285 | fNamesOn.Delete();
|
---|
286 | fNamesOff.Delete();
|
---|
287 | }
|
---|
288 |
|
---|
289 | //------------------------------------------------------------------------
|
---|
290 | //
|
---|
291 | // Add a parameter used in your filters (see AddFilter) The parameter
|
---|
292 | // index is returned,
|
---|
293 | //
|
---|
294 | // Int_t idx = AddParameter("log10(MHillas.fSize)");
|
---|
295 | //
|
---|
296 | // The indices area starting with 0 always.
|
---|
297 | //
|
---|
298 | Int_t MJOptimize::AddParameter(const char *rule)
|
---|
299 | {
|
---|
300 | fRules.Add(new TNamed(rule, ""));
|
---|
301 | return fRules.GetSize()-1;
|
---|
302 | }
|
---|
303 |
|
---|
304 | //------------------------------------------------------------------------
|
---|
305 | //
|
---|
306 | // Add a filter which can be applied in the optimization (for deatils
|
---|
307 | // see correspodning Run function) You can use the indices you got by
|
---|
308 | // AddParameter, eg
|
---|
309 | //
|
---|
310 | // AddFilter("M[0] < 3.2");
|
---|
311 | //
|
---|
312 | // if used in optimization you can do
|
---|
313 | //
|
---|
314 | // AddFilter("M[0] < [0]");
|
---|
315 | //
|
---|
316 | // for more details, see SetParameter and FixParameter
|
---|
317 | //
|
---|
318 | void MJOptimize::AddFilter(const char *rule)
|
---|
319 | {
|
---|
320 | fFilter.Add(new MFDataPhrase(rule));
|
---|
321 | }
|
---|
322 |
|
---|
323 | //------------------------------------------------------------------------
|
---|
324 | //
|
---|
325 | // Add a cut which is used to fill the matrix, eg "MMcEvt.fOartId<1.5"
|
---|
326 | // (The rule is applied, nit inverted: The matrix is filled with
|
---|
327 | // the events fullfilling the condition)
|
---|
328 | //
|
---|
329 | void MJOptimize::AddPreCut(const char *rule)
|
---|
330 | {
|
---|
331 | MFilter *f = new MFDataPhrase(rule);
|
---|
332 | f->SetBit(kCanDelete);
|
---|
333 | AddPreCut(f);
|
---|
334 | }
|
---|
335 |
|
---|
336 | //------------------------------------------------------------------------
|
---|
337 | //
|
---|
338 | // Add a cut which is used to fill the matrix. If kCanDelete is set
|
---|
339 | // MJOptimize takes the ownership.
|
---|
340 | //
|
---|
341 | void MJOptimize::AddPreCut(MFilter *f)
|
---|
342 | {
|
---|
343 | fPreCuts.Add(f);
|
---|
344 | }
|
---|
345 |
|
---|
346 | //------------------------------------------------------------------------
|
---|
347 | //
|
---|
348 | // Set the fParameters Array accoring to par.
|
---|
349 | //
|
---|
350 | void MJOptimize::SetParameters(const TArrayD &par)
|
---|
351 | {
|
---|
352 | fParameters = par;
|
---|
353 | }
|
---|
354 |
|
---|
355 | //------------------------------------------------------------------------
|
---|
356 | //
|
---|
357 | // Set the number of events processed by the eventloop. (Be carfull,
|
---|
358 | // if you are doing on-off analysis and you only process the first
|
---|
359 | // 1000 events which are on-events only the optimization may not work)
|
---|
360 | //
|
---|
361 | void MJOptimize::SetNumEvents(UInt_t n)
|
---|
362 | {
|
---|
363 | fNumEvents=n;
|
---|
364 | }
|
---|
365 |
|
---|
366 | //------------------------------------------------------------------------
|
---|
367 | //
|
---|
368 | // Set a debug level, which tells the optimization how much information
|
---|
369 | // is displayed about and in the running eventloop.
|
---|
370 | //
|
---|
371 | void MJOptimize::SetDebug(UInt_t n)
|
---|
372 | {
|
---|
373 | fDebug=n;
|
---|
374 | }
|
---|
375 |
|
---|
376 | //------------------------------------------------------------------------
|
---|
377 | //
|
---|
378 | // Set a optimization algorithm to be used. For more information see
|
---|
379 | // TMinuit.
|
---|
380 | //
|
---|
381 | // Available Algorithms are:
|
---|
382 | // kMigrad, // Minimize by the method of Migrad
|
---|
383 | // kSimplex, // Minimize by the method of Simplex
|
---|
384 | // kSeek // Minimize by the method of Monte Carlo
|
---|
385 | //
|
---|
386 | void MJOptimize::SetOptimizer(Optimizer_t o)
|
---|
387 | {
|
---|
388 | fType = o;
|
---|
389 | }
|
---|
390 |
|
---|
391 | //------------------------------------------------------------------------
|
---|
392 | //
|
---|
393 | // If a status didplay is set, search for tab "Optimizer".
|
---|
394 | // If not found, create it.
|
---|
395 | // In the tab search for TMultiGraph "Parameters".
|
---|
396 | // If not found create it.
|
---|
397 | // If empty create TGraphs.
|
---|
398 | // Check number of graphs vs. number of parameters.
|
---|
399 | // return TList with graphs.
|
---|
400 | //
|
---|
401 | TList *MJOptimize::GetPlots() const
|
---|
402 | {
|
---|
403 | if (!fDisplay)
|
---|
404 | return NULL;
|
---|
405 |
|
---|
406 | TCanvas *c=fDisplay->GetCanvas("Optimizer");
|
---|
407 | if (!c)
|
---|
408 | c = &fDisplay->AddTab("Optimizer");
|
---|
409 |
|
---|
410 | TMultiGraph *mg = dynamic_cast<TMultiGraph*>(c->FindObject("Parameters"));
|
---|
411 | if (!mg)
|
---|
412 | mg = new TMultiGraph("Parameters", "Parameters of optimization");
|
---|
413 |
|
---|
414 | TList *l = mg->GetListOfGraphs();
|
---|
415 | if (!l)
|
---|
416 | {
|
---|
417 | const Int_t n = fParameters.GetSize();
|
---|
418 | for (int i=0; i<n+1; i++)
|
---|
419 | {
|
---|
420 | TGraph *g = new TGraph;
|
---|
421 | if (i==n)
|
---|
422 | g->SetLineColor(kBlue);
|
---|
423 | mg->Add(g, "");
|
---|
424 | AddPoint(mg->GetListOfGraphs(), i, i==n?1:fParameters[i]);
|
---|
425 | }
|
---|
426 | mg->SetBit(kCanDelete);
|
---|
427 | mg->Draw("al*");
|
---|
428 |
|
---|
429 | l = mg->GetListOfGraphs();
|
---|
430 | }
|
---|
431 |
|
---|
432 | return l->GetSize() == fParameters.GetSize()+1 ? l : NULL;
|
---|
433 | }
|
---|
434 |
|
---|
435 | //------------------------------------------------------------------------
|
---|
436 | //
|
---|
437 | // Add a point with y=val as last point in idx-th Tgraph of list l.
|
---|
438 | //
|
---|
439 | void MJOptimize::AddPoint(TList *l, Int_t idx, Float_t val) const
|
---|
440 | {
|
---|
441 | if (!l)
|
---|
442 | return;
|
---|
443 |
|
---|
444 | TGraph *gr = (TGraph*)l->At(idx);
|
---|
445 | gr->SetPoint(gr->GetN(), gr->GetN(), val);
|
---|
446 | }
|
---|
447 |
|
---|
448 | Int_t MJOptimize::Minuit(TMinuit &minuit, const char *cmd) const
|
---|
449 | {
|
---|
450 | Int_t err;
|
---|
451 | Double_t tmp[2] = { fNumMaxCalls, fTolerance };
|
---|
452 | minuit.mnexcm(cmd, tmp, 2, err);
|
---|
453 |
|
---|
454 | switch (err)
|
---|
455 | {
|
---|
456 | case 0:
|
---|
457 | *fLog << inf << GetDescriptor() << " TMinuit::mnexcm excuted normally." << endl;
|
---|
458 | break;
|
---|
459 | case 1:
|
---|
460 | *fLog << warn << GetDescriptor() << " TMinuit::mnexcm command is blank... ignored." << endl;
|
---|
461 | break;
|
---|
462 | case 2:
|
---|
463 | *fLog << warn << GetDescriptor() << " TMinuit::mnexcm command-line syntax error... ignored." << endl;
|
---|
464 | break;
|
---|
465 | case 3:
|
---|
466 | *fLog << warn << GetDescriptor() << " TMinuit::mnexcm unknown command... ignored." << endl;
|
---|
467 | break;
|
---|
468 | case 4:
|
---|
469 | *fLog << warn << GetDescriptor() << " TMinuit::mnexcm - Abnormal termination (eg Migrad not converged)" << endl;
|
---|
470 | break;
|
---|
471 | /*
|
---|
472 | case 5:
|
---|
473 | *fLog << inf << GetDescriptor() << " TMinuit::mnexcm - Parameters requested." << endl;
|
---|
474 | break;
|
---|
475 | case 6:
|
---|
476 | *fLog << inf << GetDescriptor() << " TMinuit::mnexcm - SET INPUT returned." << endl;
|
---|
477 | break;
|
---|
478 | case 7:
|
---|
479 | *fLog << inf << GetDescriptor() << " TMinuit::mnexcm - SET TITLE returned." << endl;
|
---|
480 | break;
|
---|
481 | case 8:
|
---|
482 | *fLog << inf << GetDescriptor() << " TMinuit::mnexcm - SET COVAR returned." << endl;
|
---|
483 | break;
|
---|
484 | case 9:
|
---|
485 | *fLog << inf << GetDescriptor() << " TMinuit::mnexcm - reserved." << endl;
|
---|
486 | break;
|
---|
487 | case 10:
|
---|
488 | *fLog << inf << GetDescriptor() << " TMinuit::mnexcm - END returned." << endl;
|
---|
489 | break;
|
---|
490 | case 11:
|
---|
491 | *fLog << inf << GetDescriptor() << " TMinuit::mnexcm - EXIT or STOP returned." << endl;
|
---|
492 | break;
|
---|
493 | case 12:
|
---|
494 | *fLog << inf << GetDescriptor() << " TMinuit::mnexcm - RETURN returned." << endl;
|
---|
495 | break;*/
|
---|
496 | }
|
---|
497 |
|
---|
498 | return err;
|
---|
499 | }
|
---|
500 |
|
---|
501 | Bool_t MJOptimize::Optimize(MEvtLoop &evtloop)
|
---|
502 | {
|
---|
503 | if (fParameters.GetSize()==0)
|
---|
504 | {
|
---|
505 | *fLog << err << GetDescriptor() << "::Optimize: ERROR - Sorry, no parameters defined." << endl;
|
---|
506 | return kFALSE;
|
---|
507 | }
|
---|
508 |
|
---|
509 | if (fType==kNone)
|
---|
510 | return kTRUE;
|
---|
511 |
|
---|
512 | gMinuit = new TMinuit(fParameters.GetSize());
|
---|
513 |
|
---|
514 | gMinuit->SetFCN(fcn);
|
---|
515 | gMinuit->SetObjectFit(this);
|
---|
516 | gMinuit->SetPrintLevel(-1); // Don't print when DefineParameter
|
---|
517 |
|
---|
518 | //
|
---|
519 | // Set starting values and step sizes for parameters
|
---|
520 | //
|
---|
521 | for (Int_t i=0; i<fParameters.GetSize(); i++)
|
---|
522 | {
|
---|
523 | TString name = "par[";
|
---|
524 | name += i;
|
---|
525 | name += "]";
|
---|
526 | Double_t vinit = fParameters[i];
|
---|
527 | Double_t step = fStep[i];
|
---|
528 |
|
---|
529 | Double_t limlo = fLimLo[i];
|
---|
530 | Double_t limup = fLimUp[i];
|
---|
531 |
|
---|
532 | Bool_t rc = gMinuit->DefineParameter(i, name, vinit, step, limlo, limup);
|
---|
533 | if (rc)
|
---|
534 | {
|
---|
535 | *fLog << err << dbginf << "Error in defining parameter #" << i << endl;
|
---|
536 | return kFALSE;
|
---|
537 | }
|
---|
538 |
|
---|
539 | if (step==0)
|
---|
540 | gMinuit->FixParameter(i);
|
---|
541 | }
|
---|
542 |
|
---|
543 | gMinuit->SetPrintLevel(1); // Switch on pritning again
|
---|
544 | gMinuit->mnprin(1,0); // Print all parameters
|
---|
545 |
|
---|
546 | fEvtLoop = &evtloop;
|
---|
547 |
|
---|
548 | TList *g=GetPlots();
|
---|
549 |
|
---|
550 | // Now ready for minimization step:
|
---|
551 | TStopwatch clock;
|
---|
552 | clock.Start();
|
---|
553 | switch (fType)
|
---|
554 | {
|
---|
555 | case kSimplex:
|
---|
556 | Simplex(*gMinuit);
|
---|
557 | break;
|
---|
558 | case kMigrad:
|
---|
559 | Migrad(*gMinuit);
|
---|
560 | break;
|
---|
561 | case kMinimize:
|
---|
562 | Minimize(*gMinuit);
|
---|
563 | break;
|
---|
564 | case kMinos:
|
---|
565 | Minos(*gMinuit);
|
---|
566 | break;
|
---|
567 | case kImprove:
|
---|
568 | Improve(*gMinuit);
|
---|
569 | break;
|
---|
570 | case kSeek:
|
---|
571 | Seek(*gMinuit);
|
---|
572 | break;
|
---|
573 | case kNone: // Should never happen
|
---|
574 | return kFALSE;
|
---|
575 | }
|
---|
576 | clock.Stop();
|
---|
577 | clock.Print();
|
---|
578 |
|
---|
579 | if (evtloop.GetDisplay()!=fDisplay)
|
---|
580 | {
|
---|
581 | *fLog << inf << "Optimization aborted by user." << endl;
|
---|
582 | fDisplay = 0;
|
---|
583 | return kFALSE;
|
---|
584 | }
|
---|
585 |
|
---|
586 | *fLog << inf << "Resulting Chisq: " << gMinuit->fAmin << endl;
|
---|
587 |
|
---|
588 | //
|
---|
589 | // Update values of fA, fB:
|
---|
590 | //
|
---|
591 | for (Int_t i=0; i<fParameters.GetSize(); i++)
|
---|
592 | {
|
---|
593 | Double_t x1, x2;
|
---|
594 | gMinuit->GetParameter(i,x1,x2);
|
---|
595 | fParameters[i] = x1;
|
---|
596 | cout << i << ": " << fParameters[i] << endl;
|
---|
597 |
|
---|
598 | AddPoint(g, i, x1);
|
---|
599 | }
|
---|
600 | AddPoint(g, fParameters.GetSize(), gMinuit->fAmin);
|
---|
601 |
|
---|
602 | delete gMinuit;
|
---|
603 |
|
---|
604 | return kTRUE;
|
---|
605 | }
|
---|
606 |
|
---|
607 | //------------------------------------------------------------------------
|
---|
608 | //
|
---|
609 | // Optimize allows to use the optimizing by an eventloop based on
|
---|
610 | // some requirements.
|
---|
611 | //
|
---|
612 | // 1) The tasklist to be executed must have the name MTaskList and
|
---|
613 | // be an entry in the parameterlist.
|
---|
614 | //
|
---|
615 | // 2) The reading task (MReadMarsFile, MMatrixLoop) must have the name
|
---|
616 | // "MRead". If it derives from MRead Rewind() must be implemented,
|
---|
617 | // otherwise it must start reading from scratch each time its
|
---|
618 | // PreProcess is called.
|
---|
619 | //
|
---|
620 | // 3) The parameters to be optimized must be accessed through (currently)
|
---|
621 | // a single parameter container (MParContainer) called MParameters.
|
---|
622 | // The parameters are set through SetVariables.
|
---|
623 | //
|
---|
624 | // 4) The result of a single function call for minimization (eg. chisquare)
|
---|
625 | // must be available after the eventloop in a container of type
|
---|
626 | // MParameterD with the name "MinimizationResult".
|
---|
627 | //
|
---|
628 | // 5) The parameters to start with must have been set using
|
---|
629 | // MJOptimize::SetParameter or MJOptimize::SetParameters and
|
---|
630 | // MJOptimize::FixParameter
|
---|
631 | //
|
---|
632 | // The behaviour of the optimization can be changed using:
|
---|
633 | // void SetNumEvents(UInt_t n);
|
---|
634 | // void SetDebug(UInt_t n);
|
---|
635 | // void SetOptimizer(Optimizer_t o);
|
---|
636 | //
|
---|
637 | // After optimization the resulting parameters are set and another eventloop
|
---|
638 | // with a MStatusDisplay is set is called. The resulting parameters can be
|
---|
639 | // accessed using: GetParameters()
|
---|
640 | //
|
---|
641 | // To be fixed:
|
---|
642 | // - MStatusDisplay should show status while optimization is running
|
---|
643 | // - write result into MStatusDisplay
|
---|
644 | // - save result
|
---|
645 | //
|
---|
646 | Bool_t MJOptimize::Optimize(MParList &parlist)
|
---|
647 | {
|
---|
648 | // Checks to make sure, that fcn doesn't crash
|
---|
649 | if (!parlist.FindCreateObj("MParameterD", fNameMinimizationValue))
|
---|
650 | return kFALSE;
|
---|
651 |
|
---|
652 | if (!parlist.FindObject("MParameters", "MParContainer"))
|
---|
653 | {
|
---|
654 | *fLog << err << "MParameters [MParContainer] not found... abort." << endl;
|
---|
655 | return kFALSE;
|
---|
656 | }
|
---|
657 |
|
---|
658 | MTaskList *tlist = (MTaskList*)parlist.FindObject("MTaskList");
|
---|
659 | if (!tlist)
|
---|
660 | {
|
---|
661 | *fLog << err << "MTaskList not found... abort." << endl;
|
---|
662 | return kFALSE;
|
---|
663 | }
|
---|
664 | tlist->SetAccelerator(MTask::kAccDontReset|MTask::kAccDontTime);
|
---|
665 |
|
---|
666 | MMatrixLoop *loop = dynamic_cast<MMatrixLoop*>(parlist.FindTask("MRead"));
|
---|
667 |
|
---|
668 | TString txt("Starting ");
|
---|
669 | switch (fType)
|
---|
670 | {
|
---|
671 | case kMigrad: txt += "Migrad"; break;
|
---|
672 | case kMinimize: txt += "Minimize"; break;
|
---|
673 | case kMinos: txt += "Minos"; break;
|
---|
674 | case kImprove: txt += "Improve"; break;
|
---|
675 | case kSimplex: txt += "Simplex"; break;
|
---|
676 | case kSeek: txt += "Seek"; break;
|
---|
677 | case kNone: txt += "no"; break;
|
---|
678 | }
|
---|
679 | txt += " optimization";
|
---|
680 |
|
---|
681 | fLog->Separator(txt);
|
---|
682 |
|
---|
683 | // Setup eventloop
|
---|
684 | MEvtLoop evtloop;
|
---|
685 | evtloop.SetParList(&parlist);
|
---|
686 | evtloop.SetDisplay(fDisplay); // set display for evtloop and all childs
|
---|
687 | parlist.SetDisplay(0); // reset display for all contents of parlist and tasklist
|
---|
688 | evtloop.SetPrivateDisplay(); // prevent display from being cascaded again in PreProcess
|
---|
689 |
|
---|
690 | *fLog << inf << "Number of Parameters: " << fParameters.GetSize() << endl;
|
---|
691 |
|
---|
692 | // In case the reader is the matrix loop and testrain is enabled
|
---|
693 | // switch on even mode...
|
---|
694 | if (loop && TMath::Abs(fTestTrain)>0)
|
---|
695 | loop->SetOperationMode(fTestTrain>0?MMatrixLoop::kEven:MMatrixLoop::kOdd);
|
---|
696 |
|
---|
697 | if (!Optimize(evtloop))
|
---|
698 | return kFALSE;
|
---|
699 |
|
---|
700 | gMinuit = 0;
|
---|
701 |
|
---|
702 | fEvtLoop->SetDisplay(fDisplay);
|
---|
703 | if (!Fcn(fParameters))
|
---|
704 | return kFALSE;
|
---|
705 |
|
---|
706 | // In case the reader is the matrix loop and testrain is enabled
|
---|
707 | // switch on odd mode...
|
---|
708 | if (!loop || fTestTrain==0)
|
---|
709 | return kTRUE;
|
---|
710 |
|
---|
711 | loop->SetOperationMode(fTestTrain<0?MMatrixLoop::kEven:MMatrixLoop::kOdd);
|
---|
712 |
|
---|
713 | // Done already in Fcn
|
---|
714 | // list.SetVariables(fParameters);
|
---|
715 | return Fcn(fParameters);
|
---|
716 | }
|
---|
717 |
|
---|
718 | void MJOptimize::AddRulesToMatrix(MHMatrix &m) const
|
---|
719 | {
|
---|
720 | TIter Next1(&fRules);
|
---|
721 | TObject *o1=0;
|
---|
722 | while ((o1=Next1()))
|
---|
723 | m.AddColumn(o1->GetName());
|
---|
724 | }
|
---|
725 |
|
---|
726 | //------------------------------------------------------------------------
|
---|
727 | //
|
---|
728 | // Fill matrix m by using read. Use rules as a filter if userules.
|
---|
729 | //
|
---|
730 | Bool_t MJOptimize::FillMatrix(MReadTree &read, MParList &parlist, Bool_t userules)
|
---|
731 | {
|
---|
732 | MHMatrix *m = (MHMatrix*)parlist.FindObject("M", "MHMatrix");
|
---|
733 | if (!m)
|
---|
734 | {
|
---|
735 | *fLog << err << "MJOptimize::FillMatrix - ERROR: M [MHMatrix] not found in parlist... abort." << endl;
|
---|
736 | return kFALSE;
|
---|
737 | }
|
---|
738 |
|
---|
739 | m->Print("cols");
|
---|
740 |
|
---|
741 | //MParList parlist;
|
---|
742 |
|
---|
743 | // MGeomCamMagic cam;
|
---|
744 | // parlist.AddToList(&cam);
|
---|
745 |
|
---|
746 | MTaskList tlist;
|
---|
747 | parlist.Replace(&tlist);
|
---|
748 |
|
---|
749 | MFillH fillh(m);
|
---|
750 |
|
---|
751 | tlist.AddToList(&read);
|
---|
752 |
|
---|
753 | MFilterList list;
|
---|
754 | if (!list.AddToList(fPreCuts))
|
---|
755 | *fLog << err << "ERROR - Calling MFilterList::AddToList for fPreCuts failed!" << endl;
|
---|
756 | if (userules)
|
---|
757 | SetupFilters(list);
|
---|
758 | list.SetName("PreCuts"); // reset Name set by SetupFilters
|
---|
759 | list.SetInverted(kFALSE); // reset inversion set by SetupFilters
|
---|
760 | fillh.SetFilter(&list);
|
---|
761 | tlist.AddToList(&list);
|
---|
762 |
|
---|
763 | tlist.AddToList(&fillh);
|
---|
764 |
|
---|
765 | tlist.SetAccelerator(MTask::kAccDontReset|MTask::kAccDontTime);
|
---|
766 |
|
---|
767 | MEvtLoop fillloop;
|
---|
768 | fillloop.SetParList(&parlist);
|
---|
769 | fillloop.SetDisplay(fDisplay);
|
---|
770 | if (!fillloop.Eventloop(fNumEvents))
|
---|
771 | {
|
---|
772 | *fLog << err << "Filling matrix failed..." << endl;
|
---|
773 | return kFALSE;
|
---|
774 | }
|
---|
775 |
|
---|
776 | *fLog << inf << "Read events from file '" << read.GetFileName() << "'" << endl;
|
---|
777 |
|
---|
778 | if (fillloop.GetDisplay()!=fDisplay)
|
---|
779 | {
|
---|
780 | fDisplay = 0;
|
---|
781 | *fLog << inf << "Optimization aborted by user." << endl;
|
---|
782 | return kFALSE;
|
---|
783 | }
|
---|
784 |
|
---|
785 | m->Print("size");
|
---|
786 |
|
---|
787 | return kTRUE;
|
---|
788 | }
|
---|
789 |
|
---|
790 | //------------------------------------------------------------------------
|
---|
791 | //
|
---|
792 | // Adds all filters to MFilterList
|
---|
793 | //
|
---|
794 | void MJOptimize::SetupFilters(MFilterList &list, MFilter *filter) const
|
---|
795 | {
|
---|
796 | list.SetName("MParameters");
|
---|
797 | list.SetInverted();
|
---|
798 |
|
---|
799 | if (filter)
|
---|
800 | {
|
---|
801 | if (fFilter.GetSize()>0)
|
---|
802 | {
|
---|
803 | *fLog << inf;
|
---|
804 | *fLog << "INFORMATION - You are using an external filter and internal filters." << endl;
|
---|
805 | *fLog << " Please make sure that all parameters '[i]' are starting" << endl;
|
---|
806 | *fLog << " behind the number of parameters of the external filter." << endl;
|
---|
807 | }
|
---|
808 | list.AddToList(filter);
|
---|
809 | }
|
---|
810 |
|
---|
811 | if (!list.AddToList(fFilter))
|
---|
812 | *fLog << err << "ERROR - Calling MFilterList::AddToList fFilter failed!" << endl;
|
---|
813 |
|
---|
814 | *fLog << inf << "Filter: ";
|
---|
815 | list.Print();
|
---|
816 | *fLog << endl;
|
---|
817 | }
|
---|