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 | : fDebug(kFALSE), fData(0), fRFOut(0),
|
---|
64 | fNumTrees(-1), fNumTry(-1), fNdSize(-1), fNumObsoleteVariables(1),
|
---|
65 | fTestMatrix(0), fEstimationMode(kMean)
|
---|
66 | {
|
---|
67 | fName = name ? name : gsDefName.Data();
|
---|
68 | fTitle = title ? title : gsDefTitle.Data();
|
---|
69 |
|
---|
70 | // FIXME:
|
---|
71 | fNumTrees = 100; //100
|
---|
72 | fNumTry = 0; //3 0 means: in MRanForest estimated best value will be calculated
|
---|
73 | fNdSize = 1; //1
|
---|
74 | }
|
---|
75 |
|
---|
76 | MRanForestCalc::~MRanForestCalc()
|
---|
77 | {
|
---|
78 | fEForests.Delete();
|
---|
79 | }
|
---|
80 |
|
---|
81 | Int_t MRanForestCalc::Train(const MHMatrix &matrixtrain, const TArrayD &grid, Int_t ver)
|
---|
82 | {
|
---|
83 | gLog.Separator("MRanForestCalc - Train");
|
---|
84 |
|
---|
85 | if (!matrixtrain.GetColumns())
|
---|
86 | {
|
---|
87 | *fLog << err << "ERROR - MHMatrix does not contain rules... abort." << endl;
|
---|
88 | return kFALSE;
|
---|
89 | }
|
---|
90 |
|
---|
91 | const Int_t ncols = matrixtrain.GetM().GetNcols();
|
---|
92 | const Int_t nrows = matrixtrain.GetM().GetNrows();
|
---|
93 | if (ncols<=0 || nrows <=0)
|
---|
94 | {
|
---|
95 | *fLog << err << "ERROR - No. of columns or no. of rows of matrixtrain equal 0 ... abort." << endl;
|
---|
96 | return kFALSE;
|
---|
97 | }
|
---|
98 |
|
---|
99 | // rules (= combination of image par) to be used for energy estimation
|
---|
100 | TFile fileRF(fFileName, "recreate");
|
---|
101 | if (!fileRF.IsOpen())
|
---|
102 | {
|
---|
103 | *fLog << err << "ERROR - File to store RFs could not be opened... abort." << endl;
|
---|
104 | return kFALSE;
|
---|
105 | }
|
---|
106 |
|
---|
107 | const Int_t nobs = fNumObsoleteVariables; // Number of obsolete columns
|
---|
108 |
|
---|
109 | const MDataArray &dcol = *matrixtrain.GetColumns();
|
---|
110 |
|
---|
111 | MDataArray usedrules;
|
---|
112 | for (Int_t i=0; i<ncols; i++)
|
---|
113 | if (i<ncols-nobs) // -3 is important!!!
|
---|
114 | usedrules.AddEntry(dcol[i].GetRule());
|
---|
115 | else
|
---|
116 | *fLog << inf << "Skipping " << dcol[i].GetRule() << " for training" << endl;
|
---|
117 |
|
---|
118 | MDataArray rules(usedrules);
|
---|
119 | rules.AddEntry(ver<2?"Classification":dcol[ncols-1].GetRule());
|
---|
120 |
|
---|
121 | // prepare matrix for current energy bin
|
---|
122 | TMatrix mat(matrixtrain.GetM());
|
---|
123 |
|
---|
124 | // last column must be removed (true energy col.)
|
---|
125 | mat.ResizeTo(nrows, ncols-nobs+1);
|
---|
126 |
|
---|
127 | if (fDebug)
|
---|
128 | gLog.SetNullOutput(kTRUE);
|
---|
129 |
|
---|
130 | const Int_t nbins = ver>0 ? 1 : grid.GetSize()-1;
|
---|
131 | for (Int_t ie=0; ie<nbins; ie++)
|
---|
132 | {
|
---|
133 | switch (ver)
|
---|
134 | {
|
---|
135 | case 0: // Replace Energy Grid by classification
|
---|
136 | {
|
---|
137 | Int_t irows=0;
|
---|
138 | for (Int_t j=0; j<nrows; j++)
|
---|
139 | {
|
---|
140 | const Double_t energy = matrixtrain.GetM()(j,ncols-1);
|
---|
141 | const Bool_t inside = energy>grid[ie] && energy<=grid[ie+1];
|
---|
142 |
|
---|
143 | mat(j, ncols-nobs) = inside ? 1 : 0;
|
---|
144 |
|
---|
145 | if (inside)
|
---|
146 | irows++;
|
---|
147 | }
|
---|
148 | if (irows==0)
|
---|
149 | *fLog << warn << "WARNING - Skipping";
|
---|
150 | else
|
---|
151 | *fLog << inf << "Training RF for";
|
---|
152 |
|
---|
153 | *fLog << " energy bin " << ie << " (" << grid[ie] << ", " << grid[ie+1] << ") " << irows << "/" << nrows << endl;
|
---|
154 |
|
---|
155 | if (irows==0)
|
---|
156 | continue;
|
---|
157 | }
|
---|
158 | break;
|
---|
159 |
|
---|
160 | case 1: // Use Energy as classifier
|
---|
161 | case 2:
|
---|
162 | for (Int_t j=0; j<nrows; j++)
|
---|
163 | mat(j, ncols-nobs) = matrixtrain.GetM()(j,ncols-1);
|
---|
164 | break;
|
---|
165 | }
|
---|
166 |
|
---|
167 | MHMatrix matrix(mat, &rules, "MatrixTrain");
|
---|
168 |
|
---|
169 | MParList plist;
|
---|
170 | MTaskList tlist;
|
---|
171 | plist.AddToList(&tlist);
|
---|
172 | plist.AddToList(&matrix);
|
---|
173 |
|
---|
174 | MRanForest rf;
|
---|
175 | rf.SetNumTrees(fNumTrees);
|
---|
176 | rf.SetNumTry(fNumTry);
|
---|
177 | rf.SetNdSize(fNdSize);
|
---|
178 | rf.SetClassify(ver<2 ? 1 : 0);
|
---|
179 | if (ver==1)
|
---|
180 | rf.SetGrid(grid);
|
---|
181 |
|
---|
182 | plist.AddToList(&rf);
|
---|
183 |
|
---|
184 | MRanForestGrow rfgrow;
|
---|
185 | tlist.AddToList(&rfgrow);
|
---|
186 |
|
---|
187 | MFillH fillh("MHRanForestGini");
|
---|
188 | tlist.AddToList(&fillh);
|
---|
189 |
|
---|
190 | MEvtLoop evtloop;
|
---|
191 | evtloop.SetParList(&plist);
|
---|
192 | evtloop.SetDisplay(fDisplay);
|
---|
193 | evtloop.SetLogStream(fLog);
|
---|
194 |
|
---|
195 | if (!evtloop.Eventloop())
|
---|
196 | return kFALSE;
|
---|
197 |
|
---|
198 | if (fDebug)
|
---|
199 | gLog.SetNullOutput(kFALSE);
|
---|
200 |
|
---|
201 | if (ver==0)
|
---|
202 | {
|
---|
203 | // Calculate bin center
|
---|
204 | const Double_t E = (TMath::Log10(grid[ie])+TMath::Log10(grid[ie+1]))/2;
|
---|
205 |
|
---|
206 | // save whole forest
|
---|
207 | rf.SetUserVal(E);
|
---|
208 | rf.SetName(Form("%.10f", E));
|
---|
209 | }
|
---|
210 |
|
---|
211 | rf.Write();
|
---|
212 | }
|
---|
213 |
|
---|
214 | // save rules
|
---|
215 | usedrules.Write("rules");
|
---|
216 |
|
---|
217 | return kTRUE;
|
---|
218 | }
|
---|
219 |
|
---|
220 | Int_t MRanForestCalc::ReadForests(MParList &plist)
|
---|
221 | {
|
---|
222 | TFile fileRF(fFileName, "read");
|
---|
223 | if (!fileRF.IsOpen())
|
---|
224 | {
|
---|
225 | *fLog << err << dbginf << "File containing RFs could not be opened... aborting." << endl;
|
---|
226 | return kFALSE;
|
---|
227 | }
|
---|
228 |
|
---|
229 | fEForests.Delete();
|
---|
230 |
|
---|
231 | TIter Next(fileRF.GetListOfKeys());
|
---|
232 | TObject *o=0;
|
---|
233 | while ((o=Next()))
|
---|
234 | {
|
---|
235 | MRanForest *forest=0;
|
---|
236 | fileRF.GetObject(o->GetName(), forest);
|
---|
237 | if (!forest)
|
---|
238 | continue;
|
---|
239 |
|
---|
240 | forest->SetUserVal(atof(o->GetName()));
|
---|
241 |
|
---|
242 | fEForests.Add(forest);
|
---|
243 | }
|
---|
244 |
|
---|
245 | // Maybe fEForests[0].fRules yould be used instead?
|
---|
246 |
|
---|
247 | if (fData->Read("rules")<=0)
|
---|
248 | {
|
---|
249 | *fLog << err << "ERROR - Reading 'rules' from file " << fFileName << endl;
|
---|
250 | return kFALSE;
|
---|
251 | }
|
---|
252 |
|
---|
253 | return kTRUE;
|
---|
254 | }
|
---|
255 |
|
---|
256 | Int_t MRanForestCalc::PreProcess(MParList *plist)
|
---|
257 | {
|
---|
258 | fRFOut = (MParameterD*)plist->FindCreateObj("MParameterD", fNameOutput);
|
---|
259 | if (!fRFOut)
|
---|
260 | return kFALSE;
|
---|
261 |
|
---|
262 | fData = (MDataArray*)plist->FindCreateObj("MDataArray");
|
---|
263 | if (!fData)
|
---|
264 | return kFALSE;
|
---|
265 |
|
---|
266 | if (!ReadForests(*plist))
|
---|
267 | {
|
---|
268 | *fLog << err << "Reading RFs failed... aborting." << endl;
|
---|
269 | return kFALSE;
|
---|
270 | }
|
---|
271 |
|
---|
272 | *fLog << inf << "RF read from " << fFileName << endl;
|
---|
273 |
|
---|
274 | if (fTestMatrix)
|
---|
275 | return kTRUE;
|
---|
276 |
|
---|
277 | fData->Print();
|
---|
278 |
|
---|
279 | if (!fData->PreProcess(plist))
|
---|
280 | {
|
---|
281 | *fLog << err << "PreProcessing of the MDataArray failed... aborting." << endl;
|
---|
282 | return kFALSE;
|
---|
283 | }
|
---|
284 |
|
---|
285 | return kTRUE;
|
---|
286 | }
|
---|
287 |
|
---|
288 | #include <TGraph.h>
|
---|
289 | #include <TF1.h>
|
---|
290 | Int_t MRanForestCalc::Process()
|
---|
291 | {
|
---|
292 | TVector event;
|
---|
293 | if (fTestMatrix)
|
---|
294 | *fTestMatrix >> event;
|
---|
295 | else
|
---|
296 | *fData >> event;
|
---|
297 |
|
---|
298 | // --------------- Single Tree RF -------------------
|
---|
299 | if (fEForests.GetEntries()==1)
|
---|
300 | {
|
---|
301 | MRanForest *rf = (MRanForest*)fEForests[0];
|
---|
302 | fRFOut->SetVal(rf->CalcHadroness(event));
|
---|
303 | fRFOut->SetReadyToSave();
|
---|
304 |
|
---|
305 | return kTRUE;
|
---|
306 | }
|
---|
307 |
|
---|
308 | // --------------- Multi Tree RF -------------------
|
---|
309 | static TF1 f1("f1", "gaus");
|
---|
310 |
|
---|
311 | Double_t sume = 0;
|
---|
312 | Double_t sumh = 0;
|
---|
313 | Double_t maxh = 0;
|
---|
314 | Double_t maxe = 0;
|
---|
315 |
|
---|
316 | Double_t max = -1e10;
|
---|
317 | Double_t min = 1e10;
|
---|
318 |
|
---|
319 | TIter Next(&fEForests);
|
---|
320 | MRanForest *rf = 0;
|
---|
321 |
|
---|
322 | TGraph g;
|
---|
323 | while ((rf=(MRanForest*)Next()))
|
---|
324 | {
|
---|
325 | const Double_t h = rf->CalcHadroness(event);
|
---|
326 | const Double_t e = rf->GetUserVal();
|
---|
327 |
|
---|
328 | g.SetPoint(g.GetN(), e, h);
|
---|
329 |
|
---|
330 | sume += e*h;
|
---|
331 | sumh += h;
|
---|
332 |
|
---|
333 | if (h>maxh)
|
---|
334 | {
|
---|
335 | maxh = h;
|
---|
336 | maxe = e;
|
---|
337 | }
|
---|
338 | if (e>max)
|
---|
339 | max = e;
|
---|
340 | if (e<min)
|
---|
341 | min = e;
|
---|
342 | }
|
---|
343 |
|
---|
344 | switch (fEstimationMode)
|
---|
345 | {
|
---|
346 | case kMean:
|
---|
347 | fRFOut->SetVal(pow(10, sume/sumh));
|
---|
348 | break;
|
---|
349 | case kMaximum:
|
---|
350 | fRFOut->SetVal(pow(10, maxe));
|
---|
351 | break;
|
---|
352 | case kFit:
|
---|
353 | f1.SetParameter(0, maxh);
|
---|
354 | f1.SetParameter(1, maxe);
|
---|
355 | f1.SetParameter(2, 0.125);
|
---|
356 | g.Fit(&f1, "Q0N");
|
---|
357 | fRFOut->SetVal(pow(10, f1.GetParameter(1)));
|
---|
358 | break;
|
---|
359 | }
|
---|
360 |
|
---|
361 | fRFOut->SetReadyToSave();
|
---|
362 |
|
---|
363 | return kTRUE;
|
---|
364 | }
|
---|
365 |
|
---|
366 | // --------------------------------------------------------------------------
|
---|
367 | //
|
---|
368 | //
|
---|
369 | Int_t MRanForestCalc::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
|
---|
370 | {
|
---|
371 | Bool_t rc = kFALSE;
|
---|
372 | if (IsEnvDefined(env, prefix, "FileName", print))
|
---|
373 | {
|
---|
374 | rc = kTRUE;
|
---|
375 | SetFileName(GetEnvValue(env, prefix, "FileName", fFileName));
|
---|
376 | }
|
---|
377 | if (IsEnvDefined(env, prefix, "Debug", print))
|
---|
378 | {
|
---|
379 | rc = kTRUE;
|
---|
380 | SetDebug(GetEnvValue(env, prefix, "Debug", fDebug));
|
---|
381 | }
|
---|
382 | if (IsEnvDefined(env, prefix, "NameOutput", print))
|
---|
383 | {
|
---|
384 | rc = kTRUE;
|
---|
385 | SetNameOutput(GetEnvValue(env, prefix, "NameOutput", fNameOutput));
|
---|
386 | }
|
---|
387 | if (IsEnvDefined(env, prefix, "EstimationMode", print))
|
---|
388 | {
|
---|
389 | TString txt = GetEnvValue(env, prefix, "EstimationMode", "");
|
---|
390 | txt = txt.Strip(TString::kBoth);
|
---|
391 | txt.ToLower();
|
---|
392 | if (txt==(TString)"mean")
|
---|
393 | fEstimationMode = kMean;
|
---|
394 | if (txt==(TString)"maximum")
|
---|
395 | fEstimationMode = kMaximum;
|
---|
396 | if (txt==(TString)"fit")
|
---|
397 | fEstimationMode = kFit;
|
---|
398 | rc = kTRUE;
|
---|
399 | }
|
---|
400 | return rc;
|
---|
401 | }
|
---|