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), fDebug(kFALSE),
|
---|
72 | fData(0), fEnergyEst(0), fTestMatrix(0), fEstimationMode(kMean)
|
---|
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 | const Int_t nobs = 3; // Number of obsolete columns
|
---|
117 |
|
---|
118 | MDataArray usedrules;
|
---|
119 | for(Int_t i=0; i<ncols-nobs; i++) // -3 is important!!!
|
---|
120 | usedrules.AddEntry((*matrixtrain.GetColumns())[i].GetRule());
|
---|
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 | const TMatrix &m = matrixtrain.GetM();
|
---|
133 | for (Int_t j=0; j<nrows; j++)
|
---|
134 | {
|
---|
135 | const Double_t energy = m(j,ncols-1);
|
---|
136 |
|
---|
137 | if (energy>grid[ie] && energy<=grid[ie+1])
|
---|
138 | TMatrixFRow(mat1, irow1++) = TMatrixFRow_const(m,j);
|
---|
139 | else
|
---|
140 | TMatrixFRow(mat0, irow0++) = TMatrixFRow_const(m,j);
|
---|
141 | }
|
---|
142 |
|
---|
143 | const Bool_t invalid = irow1==0 || irow0==0;
|
---|
144 |
|
---|
145 | if (invalid)
|
---|
146 | *fLog << warn << "WARNING - Skipping";
|
---|
147 | else
|
---|
148 | *fLog << inf << "Training RF for";
|
---|
149 |
|
---|
150 | *fLog << " energy bin " << ie << " (" << grid[ie] << ", " << grid[ie+1] << ") " << irow0 << " " << irow1 << endl;
|
---|
151 |
|
---|
152 | if (invalid)
|
---|
153 | continue;
|
---|
154 |
|
---|
155 | if (fDebug)
|
---|
156 | gLog.SetNullOutput(kTRUE);
|
---|
157 |
|
---|
158 | // last column must be removed (true energy col.)
|
---|
159 | mat1.ResizeTo(irow1, ncols-nobs);
|
---|
160 | mat0.ResizeTo(irow0, ncols-nobs);
|
---|
161 |
|
---|
162 | // create MHMatrix as input for RF
|
---|
163 | MHMatrix matrix1(mat1, "MatrixHadrons");
|
---|
164 | MHMatrix matrix0(mat0, "MatrixGammas");
|
---|
165 |
|
---|
166 | //matrix1.AddColumns(&usedrules);
|
---|
167 | //matrix0.AddColumns(&usedrules);
|
---|
168 |
|
---|
169 | // training of RF
|
---|
170 | MTaskList tlist;
|
---|
171 | MParList plist;
|
---|
172 | plist.AddToList(&tlist);
|
---|
173 | plist.AddToList(&matrix0);
|
---|
174 | plist.AddToList(&matrix1);
|
---|
175 |
|
---|
176 | MRanForestGrow rfgrow;
|
---|
177 | rfgrow.SetNumTrees(fNumTrees); // number of trees
|
---|
178 | rfgrow.SetNumTry(fNumTry); // number of trials in random split selection
|
---|
179 | rfgrow.SetNdSize(fNdSize); // limit for nodesize
|
---|
180 |
|
---|
181 | tlist.AddToList(&rfgrow);
|
---|
182 |
|
---|
183 | MEvtLoop evtloop;
|
---|
184 | evtloop.SetDisplay(fDisplay);
|
---|
185 | evtloop.SetParList(&plist);
|
---|
186 |
|
---|
187 | if (!evtloop.Eventloop())
|
---|
188 | return kFALSE;
|
---|
189 |
|
---|
190 | if (fDebug)
|
---|
191 | gLog.SetNullOutput(kFALSE);
|
---|
192 |
|
---|
193 | const Double_t E = (log10(grid[ie])+log10(grid[ie+1]))/2;
|
---|
194 |
|
---|
195 | // save whole forest
|
---|
196 | MRanForest *forest=(MRanForest*)plist.FindObject("MRanForest");
|
---|
197 | const TString title = Form("%.10f", E);
|
---|
198 | forest->SetTitle(title);
|
---|
199 | forest->Write(title);
|
---|
200 | }
|
---|
201 |
|
---|
202 | // save rules
|
---|
203 | usedrules.Write("rules");
|
---|
204 |
|
---|
205 | fileRF.Close();
|
---|
206 |
|
---|
207 | return kTRUE;
|
---|
208 | }
|
---|
209 |
|
---|
210 | Int_t MRFEnergyEst::ReadForests(MParList &plist)
|
---|
211 | {
|
---|
212 | TFile fileRF(fFileName,"read");
|
---|
213 | if (!fileRF.IsOpen())
|
---|
214 | {
|
---|
215 | *fLog << err << dbginf << "File containing RFs could not be opened... aborting." << endl;
|
---|
216 | return kFALSE;
|
---|
217 | }
|
---|
218 |
|
---|
219 | fEForests.Delete();
|
---|
220 |
|
---|
221 | Int_t i=0;
|
---|
222 |
|
---|
223 | TIter Next(fileRF.GetListOfKeys());
|
---|
224 | TObject *o=0;
|
---|
225 | while ((o=Next()))
|
---|
226 | {
|
---|
227 | MRanForest *forest;
|
---|
228 | fileRF.GetObject(o->GetName(), forest);
|
---|
229 | if (!forest)
|
---|
230 | continue;
|
---|
231 |
|
---|
232 | forest->SetTitle(o->GetTitle());
|
---|
233 | forest->SetBit(kCanDelete);
|
---|
234 |
|
---|
235 | fBinning.Set(i+1);
|
---|
236 | fBinning[i++] = atof(o->GetTitle());
|
---|
237 |
|
---|
238 | fEForests.Add(forest);
|
---|
239 | }
|
---|
240 |
|
---|
241 | if (fData->Read("rules")<=0)
|
---|
242 | {
|
---|
243 | *fLog << err << "ERROR - Reading 'rules' from file " << fFileName << endl;
|
---|
244 | return kFALSE;
|
---|
245 | }
|
---|
246 |
|
---|
247 | return kTRUE;
|
---|
248 | }
|
---|
249 |
|
---|
250 | Int_t MRFEnergyEst::PreProcess(MParList *plist)
|
---|
251 | {
|
---|
252 | fEnergyEst = (MParameterD*)plist->FindCreateObj("MParameterD", "MEnergyEst");
|
---|
253 | if (!fEnergyEst)
|
---|
254 | return kFALSE;
|
---|
255 |
|
---|
256 | fData = (MDataArray*)plist->FindCreateObj("MDataArray");
|
---|
257 | if (!fData)
|
---|
258 | return kFALSE;
|
---|
259 |
|
---|
260 | if (!ReadForests(*plist))
|
---|
261 | {
|
---|
262 | *fLog << err << "Reading RFs failed... aborting." << endl;
|
---|
263 | return kFALSE;
|
---|
264 | }
|
---|
265 |
|
---|
266 | *fLog << inf << "RF read from " << fFileName << endl;
|
---|
267 |
|
---|
268 | if (fTestMatrix)
|
---|
269 | return kTRUE;
|
---|
270 |
|
---|
271 | fData->Print();
|
---|
272 |
|
---|
273 | if (!fData->PreProcess(plist))
|
---|
274 | {
|
---|
275 | *fLog << err << "PreProcessing of the MDataArray failed... aborting." << endl;
|
---|
276 | return kFALSE;
|
---|
277 | }
|
---|
278 |
|
---|
279 | return kTRUE;
|
---|
280 | }
|
---|
281 |
|
---|
282 | // --------------------------------------------------------------------------
|
---|
283 | //
|
---|
284 | //
|
---|
285 | Int_t MRFEnergyEst::Process()
|
---|
286 | {
|
---|
287 | TVector event;
|
---|
288 | if (fTestMatrix)
|
---|
289 | *fTestMatrix >> event;
|
---|
290 | else
|
---|
291 | *fData >> event;
|
---|
292 |
|
---|
293 | Double_t eest = 0;
|
---|
294 | Double_t hsum = 0;
|
---|
295 | Double_t maxh = 0;
|
---|
296 | Double_t maxe = 0;
|
---|
297 |
|
---|
298 | Int_t i=0;
|
---|
299 |
|
---|
300 | TIter Next(&fEForests);
|
---|
301 | MRanForest *rf = 0;
|
---|
302 | while ((rf=(MRanForest*)Next()))
|
---|
303 | {
|
---|
304 | const Double_t h = rf->CalcHadroness(event);
|
---|
305 | const Double_t e = fBinning[i++];
|
---|
306 |
|
---|
307 | hsum += h;
|
---|
308 | eest += e*h;
|
---|
309 | if (h>maxh)
|
---|
310 | {
|
---|
311 | maxh = h;
|
---|
312 | maxe = e;
|
---|
313 | }
|
---|
314 | }
|
---|
315 |
|
---|
316 | switch (fEstimationMode)
|
---|
317 | {
|
---|
318 | case kMean:
|
---|
319 | fEnergyEst->SetVal(pow(10, eest/hsum));
|
---|
320 | break;
|
---|
321 | case kMaximum:
|
---|
322 | fEnergyEst->SetVal(pow(10, maxe));
|
---|
323 | break;
|
---|
324 | }
|
---|
325 | fEnergyEst->SetReadyToSave();
|
---|
326 |
|
---|
327 | return kTRUE;
|
---|
328 | }
|
---|
329 |
|
---|
330 | // --------------------------------------------------------------------------
|
---|
331 | //
|
---|
332 | //
|
---|
333 | Int_t MRFEnergyEst::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
|
---|
334 | {
|
---|
335 | Bool_t rc = kFALSE;
|
---|
336 | if (IsEnvDefined(env, prefix, "FileName", print))
|
---|
337 | {
|
---|
338 | rc = kTRUE;
|
---|
339 | SetFileName(GetEnvValue(env, prefix, "FileName", fFileName));
|
---|
340 | }
|
---|
341 | if (IsEnvDefined(env, prefix, "Debug", print))
|
---|
342 | {
|
---|
343 | rc = kTRUE;
|
---|
344 | SetDebug(GetEnvValue(env, prefix, "Debug", fDebug));
|
---|
345 | }
|
---|
346 | if (IsEnvDefined(env, prefix, "EstimationMode", print))
|
---|
347 | {
|
---|
348 | TString txt = GetEnvValue(env, prefix, "EstimationMode", "");
|
---|
349 | txt = txt.Strip(TString::kBoth);
|
---|
350 | txt.ToLower();
|
---|
351 | if (txt==(TString)"mean")
|
---|
352 | fEstimationMode = kMean;
|
---|
353 | if (txt==(TString)"maximum")
|
---|
354 | fEstimationMode = kMaximum;
|
---|
355 | rc = kTRUE;
|
---|
356 | }
|
---|
357 | return rc;
|
---|
358 | }
|
---|