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 | ! Author(s): Thomas Bretz 8/2005 <mailto:tbretz@astro.uni-wuerzburg.de>
|
---|
20 | !
|
---|
21 | ! Copyright: MAGIC Software Development, 2000-2005
|
---|
22 | !
|
---|
23 | !
|
---|
24 | \* ======================================================================== */
|
---|
25 |
|
---|
26 | /////////////////////////////////////////////////////////////////////////////
|
---|
27 | //
|
---|
28 | // MRanForestCalc
|
---|
29 | //
|
---|
30 | //
|
---|
31 | ////////////////////////////////////////////////////////////////////////////
|
---|
32 | #include "MRanForestCalc.h"
|
---|
33 |
|
---|
34 | #include <TVector.h>
|
---|
35 |
|
---|
36 | #include "MHMatrix.h"
|
---|
37 |
|
---|
38 | #include "MLog.h"
|
---|
39 | #include "MLogManip.h"
|
---|
40 |
|
---|
41 | #include "MData.h"
|
---|
42 | #include "MDataArray.h"
|
---|
43 |
|
---|
44 | #include "MRanForest.h"
|
---|
45 | #include "MParameters.h"
|
---|
46 |
|
---|
47 | #include "MParList.h"
|
---|
48 | #include "MTaskList.h"
|
---|
49 | #include "MEvtLoop.h"
|
---|
50 | #include "MRanForestGrow.h"
|
---|
51 | #include "MFillH.h"
|
---|
52 |
|
---|
53 | ClassImp(MRanForestCalc);
|
---|
54 |
|
---|
55 | using namespace std;
|
---|
56 |
|
---|
57 | const TString MRanForestCalc::gsDefName = "MRanForestCalc";
|
---|
58 | const TString MRanForestCalc::gsDefTitle = "RF for energy estimation";
|
---|
59 |
|
---|
60 | const TString MRanForestCalc::gsNameOutput = "RanForestOut";
|
---|
61 |
|
---|
62 | MRanForestCalc::MRanForestCalc(const char *name, const char *title)
|
---|
63 | : fData(0), fRFOut(0), fTestMatrix(0),
|
---|
64 | fNumTrees(-1), fNumTry(-1), fNdSize(-1), fNumObsoleteVariables(1),
|
---|
65 | fLastDataColumnHasWeights(kFALSE),
|
---|
66 | fNameOutput(gsNameOutput), fDebug(kFALSE), fEstimationMode(kMean)
|
---|
67 | {
|
---|
68 | fName = name ? name : gsDefName.Data();
|
---|
69 | fTitle = title ? title : gsDefTitle.Data();
|
---|
70 |
|
---|
71 | // FIXME:
|
---|
72 | fNumTrees = 100; //100
|
---|
73 | fNumTry = 0; //3 0 means: in MRanForest estimated best value will be calculated
|
---|
74 | fNdSize = 1; //1
|
---|
75 | }
|
---|
76 |
|
---|
77 | MRanForestCalc::~MRanForestCalc()
|
---|
78 | {
|
---|
79 | fEForests.Delete();
|
---|
80 | }
|
---|
81 |
|
---|
82 | // --------------------------------------------------------------------------
|
---|
83 | //
|
---|
84 | // ver=0: One yes/no-classification forest is trained for each bin.
|
---|
85 | // the yes/no classification is done using the grid
|
---|
86 | // ver=1: One classification forest is trained. The last column contains a
|
---|
87 | // value which is turned into a classifier by rf itself using the grid
|
---|
88 | // ver=2: One classification forest is trained. The last column already contains
|
---|
89 | // the classifier
|
---|
90 | // ver=3: A regression forest is trained. The last column contains the
|
---|
91 | // classifier
|
---|
92 | //
|
---|
93 | Int_t MRanForestCalc::Train(const MHMatrix &matrixtrain, const TArrayD &grid, Int_t ver)
|
---|
94 | {
|
---|
95 | gLog.Separator("MRanForestCalc - Train");
|
---|
96 |
|
---|
97 | if (!matrixtrain.GetColumns())
|
---|
98 | {
|
---|
99 | *fLog << err << "ERROR - MHMatrix does not contain rules... abort." << endl;
|
---|
100 | return kFALSE;
|
---|
101 | }
|
---|
102 |
|
---|
103 | const Int_t ncols = matrixtrain.GetM().GetNcols();
|
---|
104 | const Int_t nrows = matrixtrain.GetM().GetNrows();
|
---|
105 | if (ncols<=0 || nrows <=0)
|
---|
106 | {
|
---|
107 | *fLog << err << "ERROR - No. of columns or no. of rows of matrixtrain equal 0 ... abort." << endl;
|
---|
108 | return kFALSE;
|
---|
109 | }
|
---|
110 |
|
---|
111 | // rules (= combination of image par) to be used for energy estimation
|
---|
112 | TFile fileRF(fFileName, "recreate");
|
---|
113 | if (!fileRF.IsOpen())
|
---|
114 | {
|
---|
115 | *fLog << err << "ERROR - File to store RFs could not be opened... abort." << endl;
|
---|
116 | return kFALSE;
|
---|
117 | }
|
---|
118 |
|
---|
119 | // The number of columns which have to be removed for the training
|
---|
120 | // The last data column may contain weight which also have to be removed
|
---|
121 | const Int_t nobs = fNumObsoleteVariables + (fLastDataColumnHasWeights?1:0); // Number of obsolete columns
|
---|
122 |
|
---|
123 | const MDataArray &dcol = *matrixtrain.GetColumns();
|
---|
124 |
|
---|
125 | // Make a copy of the rules for accessing the train-data
|
---|
126 | MDataArray usedrules;
|
---|
127 | for (Int_t i=0; i<ncols; i++)
|
---|
128 | if (i<ncols-nobs) // -3 is important!!!
|
---|
129 | usedrules.AddEntry(dcol[i].GetRule());
|
---|
130 | else
|
---|
131 | *fLog << inf << "Skipping " << dcol[i].GetRule() << " for training" << endl;
|
---|
132 |
|
---|
133 | // In the case of regression store the rule to be regessed in the
|
---|
134 | // last entry of your rules
|
---|
135 | MDataArray rules(usedrules);
|
---|
136 | rules.AddEntry(ver<3?"Classification":dcol[ncols-1].GetRule());
|
---|
137 |
|
---|
138 | // prepare train-matrix finally used
|
---|
139 | TMatrix mat(matrixtrain.GetM());
|
---|
140 |
|
---|
141 | // Resize it such that the obsolete columns are removed
|
---|
142 | mat.ResizeTo(nrows, ncols-nobs+1);
|
---|
143 |
|
---|
144 | if (fDebug)
|
---|
145 | gLog.SetNullOutput(kTRUE);
|
---|
146 |
|
---|
147 | // In the case one independant RF is trained for each bin (e.g.
|
---|
148 | // energy-bin) train all of them
|
---|
149 | const Int_t nbins = ver>0 ? 1 : grid.GetSize()-1;
|
---|
150 | for (Int_t ie=0; ie<nbins; ie++)
|
---|
151 | {
|
---|
152 | // In the case weights should be used initialize the
|
---|
153 | // corresponding array
|
---|
154 | TArrayF weights(nrows);
|
---|
155 | if (fLastDataColumnHasWeights)
|
---|
156 | for (Int_t j=0; j<nrows; j++)
|
---|
157 | {
|
---|
158 | weights[j] = matrixtrain.GetM()(j, ncols-nobs);
|
---|
159 | if (j%100==0)
|
---|
160 | cout << weights[j] << " ";
|
---|
161 | }
|
---|
162 |
|
---|
163 | // Setup the matrix such that the last comlumn contains
|
---|
164 | // the classifier or the regeression target value
|
---|
165 | switch (ver)
|
---|
166 | {
|
---|
167 | case 0: // Replace last column by a classification which is 1 in
|
---|
168 | // the case the event belongs to this bin, 0 otherwise
|
---|
169 | {
|
---|
170 | Int_t irows=0;
|
---|
171 | for (Int_t j=0; j<nrows; j++)
|
---|
172 | {
|
---|
173 | const Double_t value = matrixtrain.GetM()(j,ncols-1);
|
---|
174 | const Bool_t inside = value>grid[ie] && value<=grid[ie+1];
|
---|
175 |
|
---|
176 | mat(j, ncols-nobs) = inside ? 1 : 0;
|
---|
177 |
|
---|
178 | if (inside)
|
---|
179 | irows++;
|
---|
180 | }
|
---|
181 | if (irows==0)
|
---|
182 | *fLog << warn << "WARNING - Skipping";
|
---|
183 | else
|
---|
184 | *fLog << inf << "Training RF for";
|
---|
185 |
|
---|
186 | *fLog << " bin " << ie << " (" << grid[ie] << ", " << grid[ie+1] << ") " << irows << "/" << nrows << endl;
|
---|
187 |
|
---|
188 | if (irows==0)
|
---|
189 | continue;
|
---|
190 | }
|
---|
191 | break;
|
---|
192 |
|
---|
193 | case 1: // Use last column as classifier or for regression
|
---|
194 | case 2:
|
---|
195 | case 3:
|
---|
196 | for (Int_t j=0; j<nrows; j++)
|
---|
197 | mat(j, ncols-nobs) = matrixtrain.GetM()(j,ncols-1);
|
---|
198 | break;
|
---|
199 | }
|
---|
200 |
|
---|
201 | MHMatrix matrix(mat, &rules, "MatrixTrain");
|
---|
202 |
|
---|
203 | MParList plist;
|
---|
204 | MTaskList tlist;
|
---|
205 | plist.AddToList(&tlist);
|
---|
206 | plist.AddToList(&matrix);
|
---|
207 |
|
---|
208 | MRanForest rf;
|
---|
209 | rf.SetNumTrees(fNumTrees);
|
---|
210 | rf.SetNumTry(fNumTry);
|
---|
211 | rf.SetNdSize(fNdSize);
|
---|
212 | rf.SetClassify(ver<3 ? kTRUE : kFALSE);
|
---|
213 | if (ver==1)
|
---|
214 | rf.SetGrid(grid);
|
---|
215 | if (fLastDataColumnHasWeights)
|
---|
216 | rf.SetWeights(weights);
|
---|
217 |
|
---|
218 | plist.AddToList(&rf);
|
---|
219 |
|
---|
220 | MRanForestGrow rfgrow;
|
---|
221 | tlist.AddToList(&rfgrow);
|
---|
222 |
|
---|
223 | MFillH fillh("MHRanForestGini");
|
---|
224 | tlist.AddToList(&fillh);
|
---|
225 |
|
---|
226 | MEvtLoop evtloop;
|
---|
227 | evtloop.SetParList(&plist);
|
---|
228 | evtloop.SetDisplay(fDisplay);
|
---|
229 | evtloop.SetLogStream(fLog);
|
---|
230 |
|
---|
231 | if (!evtloop.Eventloop())
|
---|
232 | return kFALSE;
|
---|
233 |
|
---|
234 | if (fDebug)
|
---|
235 | gLog.SetNullOutput(kFALSE);
|
---|
236 |
|
---|
237 | if (ver==0)
|
---|
238 | {
|
---|
239 | // Calculate bin center
|
---|
240 | const Double_t E = (TMath::Log10(grid[ie])+TMath::Log10(grid[ie+1]))/2;
|
---|
241 |
|
---|
242 | // save whole forest
|
---|
243 | rf.SetUserVal(E);
|
---|
244 | rf.SetName(Form("%.10f", E));
|
---|
245 | }
|
---|
246 |
|
---|
247 | rf.Write();
|
---|
248 | }
|
---|
249 |
|
---|
250 | // save rules
|
---|
251 | usedrules.Write("rules");
|
---|
252 |
|
---|
253 | return kTRUE;
|
---|
254 | }
|
---|
255 |
|
---|
256 | Int_t MRanForestCalc::ReadForests(MParList &plist)
|
---|
257 | {
|
---|
258 | TFile fileRF(fFileName, "read");
|
---|
259 | if (!fileRF.IsOpen())
|
---|
260 | {
|
---|
261 | *fLog << err << dbginf << "File containing RFs could not be opened... aborting." << endl;
|
---|
262 | return kFALSE;
|
---|
263 | }
|
---|
264 |
|
---|
265 | fEForests.Delete();
|
---|
266 |
|
---|
267 | TIter Next(fileRF.GetListOfKeys());
|
---|
268 | TObject *o=0;
|
---|
269 | while ((o=Next()))
|
---|
270 | {
|
---|
271 | MRanForest *forest=0;
|
---|
272 | fileRF.GetObject(o->GetName(), forest);
|
---|
273 | if (!forest)
|
---|
274 | continue;
|
---|
275 |
|
---|
276 | forest->SetUserVal(atof(o->GetName()));
|
---|
277 |
|
---|
278 | fEForests.Add(forest);
|
---|
279 | }
|
---|
280 |
|
---|
281 | // Maybe fEForests[0].fRules yould be used instead?
|
---|
282 |
|
---|
283 | if (fData->Read("rules")<=0)
|
---|
284 | {
|
---|
285 | *fLog << err << "ERROR - Reading 'rules' from file " << fFileName << endl;
|
---|
286 | return kFALSE;
|
---|
287 | }
|
---|
288 |
|
---|
289 | return kTRUE;
|
---|
290 | }
|
---|
291 |
|
---|
292 | Int_t MRanForestCalc::PreProcess(MParList *plist)
|
---|
293 | {
|
---|
294 | fRFOut = (MParameterD*)plist->FindCreateObj("MParameterD", fNameOutput);
|
---|
295 | if (!fRFOut)
|
---|
296 | return kFALSE;
|
---|
297 |
|
---|
298 | fData = (MDataArray*)plist->FindCreateObj("MDataArray");
|
---|
299 | if (!fData)
|
---|
300 | return kFALSE;
|
---|
301 |
|
---|
302 | if (!ReadForests(*plist))
|
---|
303 | {
|
---|
304 | *fLog << err << "Reading RFs failed... aborting." << endl;
|
---|
305 | return kFALSE;
|
---|
306 | }
|
---|
307 |
|
---|
308 | *fLog << inf << "RF read from " << fFileName << endl;
|
---|
309 |
|
---|
310 | if (fTestMatrix)
|
---|
311 | return kTRUE;
|
---|
312 |
|
---|
313 | fData->Print();
|
---|
314 |
|
---|
315 | if (!fData->PreProcess(plist))
|
---|
316 | {
|
---|
317 | *fLog << err << "PreProcessing of the MDataArray failed... aborting." << endl;
|
---|
318 | return kFALSE;
|
---|
319 | }
|
---|
320 |
|
---|
321 | return kTRUE;
|
---|
322 | }
|
---|
323 |
|
---|
324 | #include <TGraph.h>
|
---|
325 | #include <TF1.h>
|
---|
326 | Int_t MRanForestCalc::Process()
|
---|
327 | {
|
---|
328 | TVector event;
|
---|
329 | if (fTestMatrix)
|
---|
330 | *fTestMatrix >> event;
|
---|
331 | else
|
---|
332 | *fData >> event;
|
---|
333 |
|
---|
334 | // --------------- Single Tree RF -------------------
|
---|
335 | if (fEForests.GetEntriesFast()==1)
|
---|
336 | {
|
---|
337 | MRanForest *rf = static_cast<MRanForest*>(fEForests.UncheckedAt(0));
|
---|
338 | fRFOut->SetVal(rf->CalcHadroness(event));
|
---|
339 | fRFOut->SetReadyToSave();
|
---|
340 |
|
---|
341 | return kTRUE;
|
---|
342 | }
|
---|
343 |
|
---|
344 | // --------------- Multi Tree RF -------------------
|
---|
345 | static TF1 f1("f1", "gaus");
|
---|
346 |
|
---|
347 | Double_t sume = 0;
|
---|
348 | Double_t sumh = 0;
|
---|
349 | Double_t maxh = 0;
|
---|
350 | Double_t maxe = 0;
|
---|
351 |
|
---|
352 | Double_t max = -1e10;
|
---|
353 | Double_t min = 1e10;
|
---|
354 |
|
---|
355 | TIter Next(&fEForests);
|
---|
356 | MRanForest *rf = 0;
|
---|
357 |
|
---|
358 | TGraph g;
|
---|
359 | while ((rf=(MRanForest*)Next()))
|
---|
360 | {
|
---|
361 | const Double_t h = rf->CalcHadroness(event);
|
---|
362 | const Double_t e = rf->GetUserVal();
|
---|
363 |
|
---|
364 | g.SetPoint(g.GetN(), e, h);
|
---|
365 |
|
---|
366 | sume += e*h;
|
---|
367 | sumh += h;
|
---|
368 |
|
---|
369 | if (h>maxh)
|
---|
370 | {
|
---|
371 | maxh = h;
|
---|
372 | maxe = e;
|
---|
373 | }
|
---|
374 | if (e>max)
|
---|
375 | max = e;
|
---|
376 | if (e<min)
|
---|
377 | min = e;
|
---|
378 | }
|
---|
379 |
|
---|
380 | switch (fEstimationMode)
|
---|
381 | {
|
---|
382 | case kMean:
|
---|
383 | fRFOut->SetVal(pow(10, sume/sumh));
|
---|
384 | break;
|
---|
385 | case kMaximum:
|
---|
386 | fRFOut->SetVal(pow(10, maxe));
|
---|
387 | break;
|
---|
388 | case kFit:
|
---|
389 | f1.SetParameter(0, maxh);
|
---|
390 | f1.SetParameter(1, maxe);
|
---|
391 | f1.SetParameter(2, 0.125);
|
---|
392 | g.Fit(&f1, "Q0N");
|
---|
393 | fRFOut->SetVal(pow(10, f1.GetParameter(1)));
|
---|
394 | break;
|
---|
395 | }
|
---|
396 |
|
---|
397 | fRFOut->SetReadyToSave();
|
---|
398 |
|
---|
399 | return kTRUE;
|
---|
400 | }
|
---|
401 |
|
---|
402 | void MRanForestCalc::Print(Option_t *o) const
|
---|
403 | {
|
---|
404 | *fLog << all;
|
---|
405 | *fLog << GetDescriptor() << ":" << endl;
|
---|
406 | *fLog << " - Forest ";
|
---|
407 | switch (fEForests.GetEntries())
|
---|
408 | {
|
---|
409 | case 0: *fLog << "not yet initialized." << endl; break;
|
---|
410 | case 1: *fLog << "is a single tree forest." << endl; break;
|
---|
411 | default: *fLog << "is a multi tree forest." << endl; break;
|
---|
412 | }
|
---|
413 | /*
|
---|
414 | *fLog << " - Trees: " << fNumTrees << endl;
|
---|
415 | *fLog << " - Trys: " << fNumTry << endl;
|
---|
416 | *fLog << " - Node Size: " << fNdSize << endl;
|
---|
417 | *fLog << " - Node Size: " << fNdSize << endl;
|
---|
418 | */
|
---|
419 | *fLog << " - FileName: " << fFileName << endl;
|
---|
420 | *fLog << " - NameOutput: " << fNameOutput << endl;
|
---|
421 | }
|
---|
422 |
|
---|
423 | // --------------------------------------------------------------------------
|
---|
424 | //
|
---|
425 | //
|
---|
426 | Int_t MRanForestCalc::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
|
---|
427 | {
|
---|
428 | Bool_t rc = kFALSE;
|
---|
429 | if (IsEnvDefined(env, prefix, "FileName", print))
|
---|
430 | {
|
---|
431 | rc = kTRUE;
|
---|
432 | SetFileName(GetEnvValue(env, prefix, "FileName", fFileName));
|
---|
433 | }
|
---|
434 | if (IsEnvDefined(env, prefix, "Debug", print))
|
---|
435 | {
|
---|
436 | rc = kTRUE;
|
---|
437 | SetDebug(GetEnvValue(env, prefix, "Debug", fDebug));
|
---|
438 | }
|
---|
439 | if (IsEnvDefined(env, prefix, "NameOutput", print))
|
---|
440 | {
|
---|
441 | rc = kTRUE;
|
---|
442 | SetNameOutput(GetEnvValue(env, prefix, "NameOutput", fNameOutput));
|
---|
443 | }
|
---|
444 | if (IsEnvDefined(env, prefix, "EstimationMode", print))
|
---|
445 | {
|
---|
446 | TString txt = GetEnvValue(env, prefix, "EstimationMode", "");
|
---|
447 | txt = txt.Strip(TString::kBoth);
|
---|
448 | txt.ToLower();
|
---|
449 | if (txt==(TString)"mean")
|
---|
450 | fEstimationMode = kMean;
|
---|
451 | if (txt==(TString)"maximum")
|
---|
452 | fEstimationMode = kMaximum;
|
---|
453 | if (txt==(TString)"fit")
|
---|
454 | fEstimationMode = kFit;
|
---|
455 | rc = kTRUE;
|
---|
456 | }
|
---|
457 | return rc;
|
---|
458 | }
|
---|