source: trunk/MagicSoft/Mars/macros/estfit.C@ 1912

Last change on this file since 1912 was 1842, checked in by tbretz, 22 years ago
*** empty log message ***
File size: 4.7 KB
Line 
1#include <TStopwatch.h>
2#include <TMinuit.h>
3
4#include "MParList.h"
5#include "MTaskList.h"
6#include "MEvtLoop.h"
7
8#include "MReadTree.h"
9#include "MHMatrix.h"
10#include "MChisqEval.h"
11#include "MMatrixLoop.h"
12#include "MDataMember.h"
13#include "MDataElement.h"
14#include "MEnergyEstParam.h"
15
16// --------------------------------------------------------------------------
17
18void fcn(Int_t &npar, Double_t *gin, Double_t &f, Double_t *par, Int_t iflag)
19{
20 MEvtLoop *evtloop = (MEvtLoop*)gMinuit->GetObjectFit();
21
22 MTaskList *tlist = (MTaskList*)evtloop->GetParList()->FindObject("MTaskList"); //GetTaskList();
23
24 MChisqEval *eval = (MChisqEval*) tlist->FindObject("MChisqEval");
25 MEnergyEstParam *eest = (MEnergyEstParam*)tlist->FindObject("MEnergyEstParam");
26
27 eest->SetCoeff(TArrayD(eest->GetNumCoeff(), par));
28
29 evtloop->Eventloop();
30
31 f = eval->GetChisq();
32}
33
34// --------------------------------------------------------------------------
35
36void estfit(Bool_t evalenergy=kFALSE)
37{
38 //
39 // Fill events into a MHMatrix
40 //
41 MParList parlist;
42 MHMatrix matrix;
43
44 Int_t col = matrix.AddColumn(evalenergy?"MMcEvt.fEnergy":"MMcEvt.fImpact");
45
46 MEnergyEstParam eest("Hillas");
47 eest.Add("HillasSrc");
48 eest.InitMapping(&matrix);
49
50 MReadTree read("Events", "~/ct1/MC_ON2.root");
51 read.DisableAutoScheme();
52
53 if (!matrix.Fill(&parlist, &read))
54 return;
55
56 //
57 // Setup the tasklist used to evaluate the needed chisq
58 //
59 MTaskList tasklist;
60 parlist.AddToList(&tasklist);
61
62 MMatrixLoop loop(&matrix);
63
64 MChisqEval eval;
65 eval.SetY1(new MDataElement(&matrix, col));
66 eval.SetY2(new MDataMember(evalenergy ? "MEnergyEst.fEnergy" : "MEnergyEst.fImpact"));
67 eval.SetOwner();
68
69 tasklist.AddToList(&loop);
70 tasklist.AddToList(&eest);
71 tasklist.AddToList(&eval);
72
73 MEvtLoop evtloop;
74 evtloop.SetParList(&parlist);
75
76 //
77 // Be carefull: This is not thread safe
78 //
79 TMinuit minuit(12);
80 minuit.SetPrintLevel(-1);
81 minuit.SetFCN(fcn);
82
83 // Ready for: minuit.mnexcm("SET ERR", arglist, 1, ierflg)
84 if (minuit.SetErrorDef(1))
85 {
86 cout << "SetErrorDef failed." << endl;
87 return;
88 }
89
90 //
91 // Set initial values
92 //
93 TArrayD fA(5);
94 fA[0] = -3907.74; //4916.4; //-2414.75;
95 fA[1] = 1162.3; //149.549; // 1134.28;
96 fA[2] = 199.351; //-558.209; // 132.932;
97 fA[3] = 0.403192; //0.270725; //0.292845;
98 fA[4] = 121.921; //107.001; // 107.001;
99
100 TArrayD fB(7);
101 fB[0] = -100;
102 fB[1] = 10;
103 fB[2] = 0.1;
104 fB[3] = 10;
105 fB[4] = -1;
106 fB[5] = -0.1;
107 fB[6] = -0.1;
108
109 // Set starting values and step sizes for parameters
110 for (Int_t i=0; i<fA.GetSize(); i++)
111 {
112 TString name = "fA[";
113 name += i;
114 name += "]";
115 Double_t vinit = fA[i];
116 Double_t step = fabs(fA[i]/3);
117
118 Double_t limlo = 0; // limlo=limup=0: no limits
119 Double_t limup = 0;
120
121 Bool_t rc = minuit.DefineParameter(i, name, vinit, step, limlo, limup);
122 if (evalenergy)
123 minuit.FixParameter(i);
124
125 if (!rc)
126 continue;
127
128 cout << "Error in defining parameter #" << i << endl;
129 return;
130 }
131
132 for (Int_t i=0; i<fB.GetSize(); i++)
133 {
134 TString name = "fB[";
135 name += i;
136 name += "]";
137 Double_t vinit = fB[i];
138 Double_t step = fabs(fB[i]/3);
139
140 Double_t limlo = 0; // limlo=limup=0: no limits
141 Double_t limup = 0;
142
143 Bool_t rc = minuit.DefineParameter(i+fA.GetSize(), name, vinit, step, limlo, limup);
144 if (!evalenergy)
145 minuit.FixParameter(i+fA.GetSize());
146
147 if (!rc)
148 continue;
149
150 cout << "Error in defining parameter #" << i+fA.GetSize() << endl;
151 return;
152 }
153
154 //
155 // Setup globals used in FCN
156 //
157 minuit.SetObjectFit(&evtloop);
158
159 TStopwatch clock;
160 clock.Start();
161
162 // Now ready for minimization step: minuit.mnexcm("MIGRAD", arglist, 1, ierflg)
163 gLog.SetNullOutput(kTRUE);
164 Bool_t rc = minuit.Migrad();
165 gLog.SetNullOutput(kFALSE);
166
167 if (rc)
168 {
169 cout << "Migrad failed." << endl;
170 return;
171 }
172
173 cout << endl;
174 clock.Stop();
175 clock.Print();
176 cout << endl;
177
178 for (Int_t i=(evalenergy?fA.GetSize():0); i<(evalenergy?fA.GetSize()+fB.GetSize():fA.GetSize()); i++)
179 {
180 Double_t val;
181 Double_t er;
182
183 if (!minuit.GetParameter(i, val, er))
184 {
185 cout << "Error getting parameter #" << i << endl;
186 return;
187 }
188
189 cout << i << ": " << val << " +- " << er << endl;
190 }
191
192 /*
193 // Print results
194 Double_t amin, edm, errdef;
195 Int_t nvpar, nparx, icstat;
196 minuit.mnstat(amin, edm, errdef, nvpar, nparx, icstat);
197 minuit.mnprin(3, amin);
198 */
199}
Note: See TracBrowser for help on using the repository browser.