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 Hengstebeck 2/2005 <mailto:hengsteb@physik.hu-berlin.de>
|
---|
19 | !
|
---|
20 | ! Copyright: MAGIC Software Development, 2000-2005
|
---|
21 | !
|
---|
22 | !
|
---|
23 | \* ======================================================================== */
|
---|
24 |
|
---|
25 | /////////////////////////////////////////////////////////////////////////////
|
---|
26 | //
|
---|
27 | // MRFEnergyEst
|
---|
28 | //
|
---|
29 | //
|
---|
30 | ////////////////////////////////////////////////////////////////////////////
|
---|
31 | #include "MRFEnergyEst.h"
|
---|
32 |
|
---|
33 | #include <TFile.h>
|
---|
34 | #include <TList.h>
|
---|
35 |
|
---|
36 | #include <TH1.h>
|
---|
37 | #include <TH2.h>
|
---|
38 | #include <TStyle.h>
|
---|
39 | #include <TCanvas.h>
|
---|
40 | #include <TMath.h>
|
---|
41 | #include <TVector.h>
|
---|
42 |
|
---|
43 | #include "MHMatrix.h"
|
---|
44 |
|
---|
45 | #include "MLog.h"
|
---|
46 | #include "MLogManip.h"
|
---|
47 |
|
---|
48 | #include "MParList.h"
|
---|
49 | #include "MTaskList.h"
|
---|
50 | #include "MEvtLoop.h"
|
---|
51 |
|
---|
52 | #include "MRanTree.h"
|
---|
53 | #include "MRanForest.h"
|
---|
54 | #include "MRanForestGrow.h"
|
---|
55 |
|
---|
56 | #include "MData.h"
|
---|
57 | #include "MParameters.h"
|
---|
58 |
|
---|
59 | ClassImp(MRFEnergyEst);
|
---|
60 |
|
---|
61 | using namespace std;
|
---|
62 |
|
---|
63 | static const TString gsDefName = "MRFEnergyEst";
|
---|
64 | static const TString gsDefTitle = "RF for energy estimation";
|
---|
65 |
|
---|
66 | // --------------------------------------------------------------------------
|
---|
67 | //
|
---|
68 | // Default constructor. Set the name and title of this object
|
---|
69 | //
|
---|
70 | MRFEnergyEst::MRFEnergyEst(const char *name, const char *title)
|
---|
71 | : fNumTrees(-1), fNumTry(-1), fNdSize(-1), fData(0), fEnergyEst(0),
|
---|
72 | fTestMatrix(0)
|
---|
73 | {
|
---|
74 | fName = name ? name : gsDefName.Data();
|
---|
75 | fTitle = title ? title : gsDefTitle.Data();
|
---|
76 | }
|
---|
77 |
|
---|
78 | // --------------------------------------------------------------------------
|
---|
79 | //
|
---|
80 | // Train the RF with the goven MHMatrix. The last column must contain the
|
---|
81 | // True energy.
|
---|
82 | //
|
---|
83 | Int_t MRFEnergyEst::Train(const MHMatrix &matrixtrain, const TArrayD &grid)
|
---|
84 | {
|
---|
85 | gLog.Separator("MRFEnergyEst - Train");
|
---|
86 |
|
---|
87 | if (!matrixtrain.GetColumns())
|
---|
88 | {
|
---|
89 | *fLog << err << "ERROR - MHMatrix does not contain rules... abort." << endl;
|
---|
90 | return kFALSE;
|
---|
91 | }
|
---|
92 |
|
---|
93 | const Int_t ncols = matrixtrain.GetM().GetNcols();
|
---|
94 | const Int_t nrows = matrixtrain.GetM().GetNrows();
|
---|
95 | if (ncols<=0 || nrows <=0)
|
---|
96 | {
|
---|
97 | *fLog << err << "ERROR - No. of columns or no. of rows of matrixtrain equal 0 ... abort." << endl;
|
---|
98 | return kFALSE;
|
---|
99 | }
|
---|
100 |
|
---|
101 | const Int_t nbins = grid.GetSize()-1;
|
---|
102 | if (nbins<=0)
|
---|
103 | {
|
---|
104 | *fLog << err << "ERROR - Energy grid not vaild... abort." << endl;
|
---|
105 | return kFALSE;
|
---|
106 | }
|
---|
107 |
|
---|
108 | // rules (= combination of image par) to be used for energy estimation
|
---|
109 | TFile fileRF(fFileName, "recreate");
|
---|
110 | if (!fileRF.IsOpen())
|
---|
111 | {
|
---|
112 | *fLog << err << "ERROR - File to store RFs could not be opened... abort." << endl;
|
---|
113 | return kFALSE;
|
---|
114 | }
|
---|
115 |
|
---|
116 | MDataArray usedrules;
|
---|
117 | for(Int_t i=0; i<ncols-3; i++) // -3 is important!!!
|
---|
118 | usedrules.AddEntry((*matrixtrain.GetColumns())[i].GetRule());
|
---|
119 |
|
---|
120 | const TMatrix &m = matrixtrain.GetM();
|
---|
121 |
|
---|
122 | // training of RF for each energy bin
|
---|
123 | for (Int_t ie=0; ie<nbins; ie++)
|
---|
124 | {
|
---|
125 | TMatrix mat1(nrows, ncols);
|
---|
126 | TMatrix mat0(nrows, ncols);
|
---|
127 |
|
---|
128 | // prepare matrix for current energy bin
|
---|
129 | Int_t irow1=0;
|
---|
130 | Int_t irow0=0;
|
---|
131 |
|
---|
132 | for (Int_t j=0; j<nrows; j++)
|
---|
133 | {
|
---|
134 | const Double_t energy = m(j,ncols-1);
|
---|
135 |
|
---|
136 | if (energy>grid[ie] && energy<=grid[ie+1])
|
---|
137 | TMatrixFRow(mat1, irow1++) = TMatrixFRow_const(m,j);
|
---|
138 | else
|
---|
139 | TMatrixFRow(mat0, irow0++) = TMatrixFRow_const(m,j);
|
---|
140 | }
|
---|
141 |
|
---|
142 | const Bool_t valid = irow1*irow0>0;
|
---|
143 |
|
---|
144 | if (!valid)
|
---|
145 | *fLog << warn << "WARNING - Skipping";
|
---|
146 | else
|
---|
147 | *fLog << inf << "Training RF for";
|
---|
148 |
|
---|
149 | *fLog << " energy bin " << ie << " (" << grid[ie] << ", " << grid[ie+1] << ")" << endl;
|
---|
150 |
|
---|
151 | if (!valid)
|
---|
152 | continue;
|
---|
153 |
|
---|
154 | gLog.SetNullOutput(kTRUE);
|
---|
155 |
|
---|
156 | // last column must be removed (true energy col.)
|
---|
157 | mat1.ResizeTo(irow1, ncols-1);
|
---|
158 | mat0.ResizeTo(irow0, ncols-1);
|
---|
159 |
|
---|
160 | // create MHMatrix as input for RF
|
---|
161 | MHMatrix matrix1(mat1, "MatrixHadrons");
|
---|
162 | MHMatrix matrix0(mat0, "MatrixGammas");
|
---|
163 |
|
---|
164 | matrix1.AddColumns(&usedrules);
|
---|
165 | matrix0.AddColumns(&usedrules);
|
---|
166 |
|
---|
167 | // training of RF
|
---|
168 | MTaskList tlist;
|
---|
169 | MParList plist;
|
---|
170 | plist.AddToList(&tlist);
|
---|
171 | plist.AddToList(&matrix0);
|
---|
172 | plist.AddToList(&matrix1);
|
---|
173 |
|
---|
174 | MRanForestGrow rfgrow;
|
---|
175 | rfgrow.SetNumTrees(fNumTrees); // number of trees
|
---|
176 | rfgrow.SetNumTry(fNumTry); // number of trials in random split selection
|
---|
177 | rfgrow.SetNdSize(fNdSize); // limit for nodesize
|
---|
178 |
|
---|
179 | tlist.AddToList(&rfgrow);
|
---|
180 |
|
---|
181 | MEvtLoop evtloop;
|
---|
182 | evtloop.SetDisplay(fDisplay);
|
---|
183 | evtloop.SetParList(&plist);
|
---|
184 |
|
---|
185 | gLog.SetNullOutput(kFALSE);
|
---|
186 |
|
---|
187 | if (!evtloop.Eventloop())
|
---|
188 | return kFALSE;
|
---|
189 |
|
---|
190 | // save whole forest
|
---|
191 | MRanForest *forest=(MRanForest*)plist.FindObject("MRanForest");
|
---|
192 | const TString title = Form("%f", TMath::Sqrt(grid[ie]*grid[ie+1]));
|
---|
193 | //const TString title = Form("%f", (grid[ie]+grid[ie+1])/2);
|
---|
194 | forest->SetTitle(title);
|
---|
195 | forest->Write(title);
|
---|
196 | }
|
---|
197 |
|
---|
198 | // save rules
|
---|
199 | usedrules.Write("rules");
|
---|
200 |
|
---|
201 | fileRF.Close();
|
---|
202 |
|
---|
203 | return kTRUE;
|
---|
204 | }
|
---|
205 | /*
|
---|
206 | Int_t MRFEnergyEst::Test(const MHMatrix &matrixtest)
|
---|
207 | {
|
---|
208 | gLog.Separator("MRFEnergyEst - Test");
|
---|
209 |
|
---|
210 | const Int_t ncols = matrixtest.GetM().GetNcols();
|
---|
211 | const Int_t nrows = matrixtest.GetM().GetNrows();
|
---|
212 |
|
---|
213 | if (ncols<=0 || nrows <=0)
|
---|
214 | {
|
---|
215 | *fLog << err << dbginf << "No. of columns or no. of rows of matrixtrain equal 0 ... aborting." << endl;
|
---|
216 | return kFALSE;
|
---|
217 | }
|
---|
218 |
|
---|
219 | if (!ReadForests())
|
---|
220 | {
|
---|
221 | *fLog << err << dbginf << "Reading RFs failed... aborting." << endl;
|
---|
222 | return kFALSE;
|
---|
223 | }
|
---|
224 |
|
---|
225 | const Int_t nbins=fEForests.GetSize();
|
---|
226 |
|
---|
227 | Double_t elow = FLT_MAX;
|
---|
228 | Double_t eup = -FLT_MAX;
|
---|
229 |
|
---|
230 | for(Int_t j=0;j<nbins;j++)
|
---|
231 | {
|
---|
232 | elow = TMath::Min(atof(fEForests[j]->GetTitle()), elow);
|
---|
233 | eup = TMath::Max(atof(fEForests[j]->GetTitle()), eup);
|
---|
234 | }
|
---|
235 |
|
---|
236 | TH1F hres("hres", "", 1000, -10, 10);
|
---|
237 | TH2F henergy("henergy", "",100, elow, eup,100, elow, eup);
|
---|
238 |
|
---|
239 | const TMatrix &m=matrixtest.GetM();
|
---|
240 | for(Int_t i=0;i<nrows;i++)
|
---|
241 | {
|
---|
242 | Double_t etrue = m(i,ncols-1);
|
---|
243 | Double_t eest = 0;
|
---|
244 | Double_t hsum = 0;
|
---|
245 |
|
---|
246 | for(Int_t j=0;j<nbins;j++)
|
---|
247 | {
|
---|
248 | const TVector v = TMatrixFRow_const(m,i);
|
---|
249 |
|
---|
250 | const Double_t h = ((MRanForest*)fEForests[j])->CalcHadroness(v);
|
---|
251 | const Double_t e = atof(fEForests[j]->GetTitle());
|
---|
252 |
|
---|
253 | hsum += h;
|
---|
254 | eest += h*e;
|
---|
255 | }
|
---|
256 | eest /= hsum;
|
---|
257 | eest = pow(10., eest);
|
---|
258 |
|
---|
259 | //if (etrue>80.)
|
---|
260 | // hres.Fill((eest-etrue)/etrue);
|
---|
261 |
|
---|
262 | henergy.Fill(log10(etrue),log10(eest));
|
---|
263 | }
|
---|
264 |
|
---|
265 | if(gStyle)
|
---|
266 | gStyle->SetOptFit(1);
|
---|
267 |
|
---|
268 | // show results
|
---|
269 | TCanvas *c=MH::MakeDefCanvas("c","",300,900);
|
---|
270 |
|
---|
271 | c->Divide(1,3);
|
---|
272 | c->cd(1);
|
---|
273 | henergy.SetTitle("Estimated vs true energy");
|
---|
274 | henergy.GetXaxis()->SetTitle("log10(E_{true}[GeV])");
|
---|
275 | henergy.GetYaxis()->SetTitle("log10(E_{est}[GeV])");
|
---|
276 | henergy.DrawCopy();
|
---|
277 |
|
---|
278 | c->cd(2);
|
---|
279 | TH1F *hptr=(TH1F*)henergy.ProfileX("_px",1,100,"S");
|
---|
280 | hptr->SetTitle("Estimated vs true energy - profile");
|
---|
281 | hptr->GetXaxis()->SetTitle("log10(E_{true}[GeV])");
|
---|
282 | hptr->GetYaxis()->SetTitle("log10(E_{est}[GeV])");
|
---|
283 | hptr->DrawCopy();
|
---|
284 |
|
---|
285 | c->cd(3);
|
---|
286 | hres.Fit("gaus");
|
---|
287 | hres.SetTitle("Energy resolution for E_{true}>80Gev");
|
---|
288 | hres.GetXaxis()->SetTitle("(E_{estimated}-E_{true})/E_{true})");
|
---|
289 | hres.GetYaxis()->SetTitle("counts");
|
---|
290 | hres.DrawCopy();
|
---|
291 |
|
---|
292 | c->GetPad(1)->SetGrid();
|
---|
293 | c->GetPad(2)->SetGrid();
|
---|
294 | c->GetPad(3)->SetGrid();
|
---|
295 |
|
---|
296 | return kTRUE;
|
---|
297 | }
|
---|
298 | */
|
---|
299 | Int_t MRFEnergyEst::ReadForests(MParList *plist)
|
---|
300 | {
|
---|
301 | TFile fileRF(fFileName,"read");
|
---|
302 | if (!fileRF.IsOpen())
|
---|
303 | {
|
---|
304 | *fLog << err << dbginf << "File containing RFs could not be opened... aborting." << endl;
|
---|
305 | return kFALSE;
|
---|
306 | }
|
---|
307 |
|
---|
308 | fEForests.Delete();
|
---|
309 |
|
---|
310 | TIter Next(fileRF.GetListOfKeys());
|
---|
311 | TObject *o=0;
|
---|
312 | while ((o=Next()))
|
---|
313 | {
|
---|
314 | MRanForest *forest;
|
---|
315 | fileRF.GetObject(o->GetName(), forest);
|
---|
316 | if (!forest)
|
---|
317 | continue;
|
---|
318 |
|
---|
319 | forest->SetTitle(o->GetTitle());
|
---|
320 | forest->SetBit(kCanDelete);
|
---|
321 |
|
---|
322 | fEForests.Add(forest);
|
---|
323 |
|
---|
324 | }
|
---|
325 |
|
---|
326 | if (plist)
|
---|
327 | {
|
---|
328 | fData = (MDataArray*)plist->FindCreateObj("MDataArray");
|
---|
329 | fData->Read("rules");
|
---|
330 | }
|
---|
331 |
|
---|
332 | fileRF.Close();
|
---|
333 |
|
---|
334 | return kTRUE;
|
---|
335 | }
|
---|
336 |
|
---|
337 | Int_t MRFEnergyEst::PreProcess(MParList *plist)
|
---|
338 | {
|
---|
339 | fEnergyEst = (MParameterD*)plist->FindCreateObj("MParameterD", "MEnergyEst");
|
---|
340 | if (!fEnergyEst)
|
---|
341 | return kFALSE;
|
---|
342 |
|
---|
343 | if (!ReadForests(plist))
|
---|
344 | {
|
---|
345 | *fLog << err << "Reading RFs failed... aborting." << endl;
|
---|
346 | return kFALSE;
|
---|
347 | }
|
---|
348 |
|
---|
349 | if (fTestMatrix)
|
---|
350 | return kTRUE;
|
---|
351 |
|
---|
352 | if (!fData)
|
---|
353 | {
|
---|
354 | *fLog << err << "MDataArray not found... aborting." << endl;
|
---|
355 | return kFALSE;
|
---|
356 | }
|
---|
357 |
|
---|
358 | if (!fData->PreProcess(plist))
|
---|
359 | {
|
---|
360 | *fLog << err << "PreProcessing of the MDataArray failed... aborting." << endl;
|
---|
361 | return kFALSE;
|
---|
362 | }
|
---|
363 |
|
---|
364 | return kTRUE;
|
---|
365 | }
|
---|
366 |
|
---|
367 | // --------------------------------------------------------------------------
|
---|
368 | //
|
---|
369 | //
|
---|
370 | Int_t MRFEnergyEst::Process()
|
---|
371 | {
|
---|
372 | TVector event;
|
---|
373 | if (fTestMatrix)
|
---|
374 | *fTestMatrix >> event;
|
---|
375 | else
|
---|
376 | *fData >> event;
|
---|
377 |
|
---|
378 | Double_t eest = 0;
|
---|
379 | Double_t hsum = 0;
|
---|
380 |
|
---|
381 | TIter Next(&fEForests);
|
---|
382 | MRanForest *rf = 0;
|
---|
383 | while ((rf=(MRanForest*)Next()))
|
---|
384 | {
|
---|
385 | const Double_t h = rf->CalcHadroness(event);
|
---|
386 | const Double_t e = atof(rf->GetTitle());
|
---|
387 |
|
---|
388 | hsum += h;
|
---|
389 | eest += h*e;
|
---|
390 | }
|
---|
391 |
|
---|
392 | fEnergyEst->SetVal(eest/hsum);
|
---|
393 | fEnergyEst->SetReadyToSave();
|
---|
394 |
|
---|
395 | return kTRUE;
|
---|
396 | }
|
---|