source: trunk/MagicSoft/Mars/mmontecarlo/MMcEnergyEst.cc@ 2054

Last change on this file since 2054 was 2031, checked in by moralejo, 22 years ago
*** empty log message ***
File size: 8.9 KB
Line 
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@uni-sw.gwdg.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 <TStopwatch.h>
43#include <TVirtualFitter.h>
44#include <TMinuit.h>
45
46#include "MParList.h"
47#include "MTaskList.h"
48#include "MGeomCamCT1.h"
49#include "MFEventSelector.h"
50#include "MReadTree.h"
51#include "MFCT1SelFinal.h"
52#include "MHMatrix.h"
53#include "MEnergyEstParam.h"
54#include "MMatrixLoop.h"
55#include "MChisqEval.h"
56#include "MEvtLoop.h"
57#include "MDataElement.h"
58#include "MDataMember.h"
59#include "MLog.h"
60#include "MLogManip.h"
61
62
63//------------------------------------------------------------------------
64//
65// fcn calculates the function to be minimized (using TMinuit::Migrad)
66//
67void fcn(Int_t &npar, Double_t *gin, Double_t &f, Double_t *par, Int_t iflag)
68{
69 MEvtLoop *evtloop = (MEvtLoop*)gMinuit->GetObjectFit();
70
71 MTaskList *tlist = (MTaskList*)evtloop->GetParList()->FindObject("MTaskList"); // GetTaskList();
72
73 MChisqEval *eval = (MChisqEval*) tlist->FindObject("MChisqEval");
74 MEnergyEstParam *eest = (MEnergyEstParam*)tlist->FindObject("MEnergyEstParam");
75
76 eest->SetCoeff(TArrayD(eest->GetNumCoeff(), par));
77
78 evtloop->Eventloop();
79
80 f = eval->GetChisq();
81}
82
83ClassImp(MMcEnergyEst);
84
85// --------------------------------------------------------------------------
86//
87// Default constructor.
88//
89MMcEnergyEst::MMcEnergyEst(const char *name, const char *title)
90{
91 fName = name ? name : "MMcEnergyEst";
92 fTitle = title ? title : "Optimizer of the energy estimator";
93
94 SetHillasName("MHillas");
95 SetHillasSrcName("MHillasSrc");
96}
97
98//------------------------------------------------------------------------
99//
100// Optimization (via migrad minimization) of parameters of energy estimation.
101//
102void MMcEnergyEst::FindParams()
103{
104 MParList parlist;
105
106 MFEventSelector selector;
107 selector.SetNumSelectEvts(fNevents);
108 *fLog << inf << "Read events from file '" << fInFile << "'" << endl;
109
110 MReadTree read("Events");
111 read.AddFile(fInFile);
112 read.DisableAutoScheme();
113 read.SetSelector(&selector);
114
115 *fLog << inf << "Define columns of matrix" << endl;
116 MHMatrix matrix;
117 Int_t colenergy = matrix.AddColumn("MMcEvt.fEnergy");
118 Int_t colimpact = matrix.AddColumn("MMcEvt.fImpact");
119
120 if (colenergy < 0 || colimpact < 0)
121 {
122 *fLog << err << dbginf << "colenergy, colimpact = " << colenergy << ", "
123 << colimpact << endl;
124 return;
125 }
126
127 MEnergyEstParam eest(fHillasName);
128 eest.Add(fHillasSrcName);
129 eest.InitMapping(&matrix);
130
131 *fLog << inf << "--------------------------------------" << endl;
132 *fLog << inf << "Fill events into the matrix" << endl;
133 if ( !matrix.Fill(&parlist, &read, fEventFilter) )
134 return;
135 *fLog << inf << "Matrix was filled with " << matrix.GetNumRows()
136 << inf << " events" << endl;
137
138 //-----------------------------------------------------------------------
139 //
140 // Setup the eventloop which will be executed in the fcn of MINUIT
141 //
142 *fLog << inf << "--------------------------------------" << endl;
143 *fLog << inf << "Setup event loop to be used in 'fcn'" << endl;
144
145 MTaskList tasklist;
146
147 MMatrixLoop loop(&matrix);
148
149 MChisqEval eval;
150 eval.SetY1(new MDataElement(&matrix, colenergy));
151 eval.SetY2(new MDataMember("MEnergyEst.fEnergy"));
152 eval.SetOwner();
153
154 //
155 // Entries in MParList
156
157 parlist.AddToList(&tasklist);
158
159 //
160 // Entries in MTaskList
161
162 tasklist.AddToList(&loop);
163 tasklist.AddToList(&eest);
164 tasklist.AddToList(&eval);
165
166
167 *fLog << inf << "Event loop was setup" << endl;
168 MEvtLoop evtloop;
169 evtloop.SetParList(&parlist);
170
171 //
172 //---------- Start of minimization part --------------------
173 //
174 // Do the minimization with MINUIT
175 //
176 // Be careful: This is not thread safe
177 //
178 *fLog << inf << "--------------------------------------" << endl;
179 *fLog << inf << "Start minimization" << endl;
180
181 gMinuit = new TMinuit(12);
182 gMinuit->SetPrintLevel(-1);
183
184 gMinuit->SetFCN(fcn);
185 gMinuit->SetObjectFit(&evtloop);
186
187 // Ready for: gMinuit->mnexcm("SET ERR", arglist, 1, ierflg)
188
189 if (gMinuit->SetErrorDef(1))
190 {
191 *fLog << err << dbginf << "SetErrorDef failed." << endl;
192 return;
193 }
194
195 //
196 // Set initial values of the parameters (close enough to the final ones, taken
197 // from previous runs of the procedure). Parameter fA[4] is not used in the default
198 // energy estimation model (from D. Kranich).
199 //
200 fA.Set(5);
201 fB.Set(7);
202
203 fA[0] = 21006.2;
204 fA[1] = -43.2648;
205 fA[2] = -690.403;
206 fA[3] = -0.0428544;
207 fA[4] = 1.;
208 fB[0] = -3391.05;
209 fB[1] = 136.58;
210 fB[2] = 0.253807;
211 fB[3] = 254.363;
212 fB[4] = 61.0386;
213 fB[5] = -0.0190984;
214 fB[6] = -421695;
215
216 //
217 // Set starting values and step sizes for parameters
218 //
219 for (Int_t i=0; i<fA.GetSize(); i++)
220 {
221 TString name = "fA[";
222 name += i;
223 name += "]";
224 Double_t vinit = fA[i];
225 Double_t step = fabs(fA[i]/3);
226
227 Double_t limlo = 0; // limlo=limup=0: no limits
228 Double_t limup = 0;
229
230 Bool_t rc = gMinuit->DefineParameter(i, name, vinit, step, limlo, limup);
231 if (!rc)
232 continue;
233
234 *fLog << err << dbginf << "Error in defining parameter #" << i << endl;
235 return;
236 }
237
238 for (Int_t i=0; i<fB.GetSize(); i++)
239 {
240 TString name = "fB[";
241 name += i;
242 name += "]";
243 Double_t vinit = fB[i];
244 Double_t step = fabs(fB[i]/3);
245
246 Double_t limlo = 0; // limlo=limup=0: no limits
247 Double_t limup = 0;
248
249 Bool_t rc = gMinuit->DefineParameter(i+fA.GetSize(), name, vinit, step, limlo, limup);
250 if (!rc)
251 continue;
252
253 *fLog << err << dbginf << "Error in defining parameter #" << i+fA.GetSize() << endl;
254 return;
255 }
256
257 TStopwatch clock;
258 clock.Start();
259
260 // Now ready for minimization step:
261
262 gLog.SetNullOutput(kTRUE);
263 Bool_t rc = gMinuit->Migrad();
264 gLog.SetNullOutput(kFALSE);
265
266 if (rc)
267 {
268 *fLog << err << dbginf << "Migrad failed." << endl;
269 return;
270 }
271
272 *fLog << inf << endl;
273 clock.Stop();
274 clock.Print();
275 *fLog << inf << endl;
276
277 *fLog << inf << "Resulting Chisq: " << gMinuit->fAmin << endl;
278
279 //
280 // Update values of fA, fB:
281 //
282 for (Int_t i = 0; i < fA.GetSize(); i++)
283 {
284 Double_t x1, x2;
285 gMinuit->GetParameter(i,x1,x2);
286 fA[i] = x1;
287 }
288 for (Int_t i = fA.GetSize(); i < fA.GetSize()+fB.GetSize(); i++)
289 {
290 Double_t x1, x2;
291 gMinuit->GetParameter(i,x1,x2);
292 fB[i-fA.GetSize()] = x1;
293 }
294
295 // eest.Print();
296 eest.StopMapping();
297 *fLog << inf << "Minimization finished" << endl;
298
299}
300
301//------------------------------------------------------------------------
302//
303// Print current values of parameters
304//
305void MMcEnergyEst::Print(Option_t *o)
306{
307 for (Int_t i = 0; i < fA.GetSize(); i++)
308 *fLog << inf << "Parameter " << i << ": " << fA[i] << endl;
309
310 for (Int_t i = fA.GetSize(); i < fA.GetSize()+fB.GetSize(); i++)
311 *fLog << inf << "Parameter " << i << ": " << fB[i-fA.GetSize()] << endl;
312
313 /*
314 // Print results
315 Double_t amin, edm, errdef;
316 Int_t nvpar, nparx, icstat;
317 gMinuit->mnstat(amin, edm, errdef, nvpar, nparx, icstat);
318 gMinuit->mnprin(3, amin);
319 */
320
321}
322
323
Note: See TracBrowser for help on using the repository browser.