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

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