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

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