source: trunk/MagicSoft/Mars/mranforest/MRFEnergyEst.cc@ 7143

Last change on this file since 7143 was 7142, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 8.4 KB
Line 
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
59ClassImp(MRFEnergyEst);
60
61using namespace std;
62
63static const TString gsDefName = "MRFEnergyEst";
64static const TString gsDefTitle = "RF for energy estimation";
65
66// --------------------------------------------------------------------------
67//
68// Default constructor. Set the name and title of this object
69//
70MRFEnergyEst::MRFEnergyEst(const char *name, const char *title)
71 : fNumTrees(-1), fNumTry(-1), fNdSize(-1), fDebug(kFALSE),
72 fData(0), fEnergyEst(0), 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//
83Int_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 TMatrix &m = matrixtrain.GetM();
94
95 const Int_t ncols = m.GetNcols();
96 const Int_t nrows = m.GetNrows();
97 if (ncols<=0 || nrows <=0)
98 {
99 *fLog << err << "ERROR - No. of columns or no. of rows of matrixtrain equal 0 ... abort." << endl;
100 return kFALSE;
101 }
102
103 const Int_t nbins = grid.GetSize()-1;
104 if (nbins<=0)
105 {
106 *fLog << err << "ERROR - Energy grid not vaild... abort." << endl;
107 return kFALSE;
108 }
109
110 // rules (= combination of image par) to be used for energy estimation
111 TFile fileRF(fFileName, "recreate");
112 if (!fileRF.IsOpen())
113 {
114 *fLog << err << "ERROR - File to store RFs could not be opened... abort." << endl;
115 return kFALSE;
116 }
117
118 const Int_t nobs = 3; // Number of obsolete columns
119
120 MDataArray usedrules;
121 for(Int_t i=0; i<ncols-nobs; i++) // -3 is important!!!
122 usedrules.AddEntry((*matrixtrain.GetColumns())[i].GetRule());
123
124 // training of RF for each energy bin
125 for (Int_t ie=0; ie<nbins; ie++)
126 {
127 TMatrix mat1(nrows, ncols);
128 TMatrix mat0(nrows, ncols);
129
130 // prepare matrix for current energy bin
131 Int_t irow1=0;
132 Int_t irow0=0;
133
134 for (Int_t j=0; j<nrows; j++)
135 {
136 const Double_t energy = m(j,ncols-1);
137
138 if (energy>grid[ie] && energy<=grid[ie+1])
139 TMatrixFRow(mat1, irow1++) = TMatrixFRow_const(m,j);
140 else
141 TMatrixFRow(mat0, irow0++) = TMatrixFRow_const(m,j);
142 }
143
144 const Bool_t invalid = irow1==0 || irow0==0;
145
146 if (invalid)
147 *fLog << warn << "WARNING - Skipping";
148 else
149 *fLog << inf << "Training RF for";
150
151 *fLog << " energy bin " << ie << " (" << grid[ie] << ", " << grid[ie+1] << ") " << irow0 << " " << irow1 << endl;
152
153 if (invalid)
154 continue;
155
156 if (fDebug)
157 gLog.SetNullOutput(kTRUE);
158
159 // last column must be removed (true energy col.)
160 mat1.ResizeTo(irow1, ncols-nobs);
161 mat0.ResizeTo(irow0, ncols-nobs);
162
163 // create MHMatrix as input for RF
164 MHMatrix matrix1(mat1, "MatrixHadrons");
165 MHMatrix matrix0(mat0, "MatrixGammas");
166
167 matrix1.AddColumns(&usedrules);
168 matrix0.AddColumns(&usedrules);
169
170 // training of RF
171 MTaskList tlist;
172 MParList plist;
173 plist.AddToList(&tlist);
174 plist.AddToList(&matrix0);
175 plist.AddToList(&matrix1);
176
177 MRanForestGrow rfgrow;
178 rfgrow.SetNumTrees(fNumTrees); // number of trees
179 rfgrow.SetNumTry(fNumTry); // number of trials in random split selection
180 rfgrow.SetNdSize(fNdSize); // limit for nodesize
181
182 tlist.AddToList(&rfgrow);
183
184 MEvtLoop evtloop;
185 evtloop.SetDisplay(fDisplay);
186 evtloop.SetParList(&plist);
187
188 if (fDebug)
189 gLog.SetNullOutput(kFALSE);
190
191 if (!evtloop.Eventloop())
192 return kFALSE;
193
194 const Double_t E = (log10(grid[ie])+log10(grid[ie+1]))/2;
195
196 // save whole forest
197 MRanForest *forest=(MRanForest*)plist.FindObject("MRanForest");
198 const TString title = Form("%.10f", E);
199 forest->SetTitle(title);
200 forest->Write(title);
201 }
202
203 // save rules
204 usedrules.Write("rules");
205
206 fileRF.Close();
207
208 return kTRUE;
209}
210
211Int_t MRFEnergyEst::ReadForests(MParList &plist)
212{
213 TFile fileRF(fFileName,"read");
214 if (!fileRF.IsOpen())
215 {
216 *fLog << err << dbginf << "File containing RFs could not be opened... aborting." << endl;
217 return kFALSE;
218 }
219
220 fEForests.Delete();
221
222 TIter Next(fileRF.GetListOfKeys());
223 TObject *o=0;
224 while ((o=Next()))
225 {
226 MRanForest *forest;
227 fileRF.GetObject(o->GetName(), forest);
228 if (!forest)
229 continue;
230
231 forest->SetTitle(o->GetTitle());
232 forest->SetBit(kCanDelete);
233
234 fEForests.Add(forest);
235 }
236
237 if (fData->Read("rules")<=0)
238 {
239 *fLog << err << "ERROR - Reading 'rules' from file " << fFileName << endl;
240 return kFALSE;
241 }
242
243 return kTRUE;
244}
245
246Int_t MRFEnergyEst::PreProcess(MParList *plist)
247{
248 fEnergyEst = (MParameterD*)plist->FindCreateObj("MParameterD", "MEnergyEst");
249 if (!fEnergyEst)
250 return kFALSE;
251
252 fData = (MDataArray*)plist->FindCreateObj("MDataArray");
253 if (!fData)
254 return kFALSE;
255
256 if (!ReadForests(*plist))
257 {
258 *fLog << err << "Reading RFs failed... aborting." << endl;
259 return kFALSE;
260 }
261
262 *fLog << inf << "RF read from " << fFileName << endl;
263
264 if (fTestMatrix)
265 return kTRUE;
266
267 fData->Print();
268
269 if (!fData->PreProcess(plist))
270 {
271 *fLog << err << "PreProcessing of the MDataArray failed... aborting." << endl;
272 return kFALSE;
273 }
274
275 return kTRUE;
276}
277
278// --------------------------------------------------------------------------
279//
280//
281Int_t MRFEnergyEst::Process()
282{
283 TVector event;
284 if (fTestMatrix)
285 *fTestMatrix >> event;
286 else
287 *fData >> event;
288
289 Double_t eest = 0;
290 Double_t hsum = 0;
291
292 TIter Next(&fEForests);
293 MRanForest *rf = 0;
294 while ((rf=(MRanForest*)Next()))
295 {
296 const Double_t h = rf->CalcHadroness(event);
297 const Double_t e = atof(rf->GetTitle());
298
299 hsum += h;
300 eest += e*h;
301 }
302
303 fEnergyEst->SetVal(pow(10, eest/hsum));
304 fEnergyEst->SetReadyToSave();
305
306 return kTRUE;
307}
308
309// --------------------------------------------------------------------------
310//
311//
312Int_t MRFEnergyEst::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
313{
314 Bool_t rc = kFALSE;
315 if (IsEnvDefined(env, prefix, "FileName", print))
316 {
317 rc = kTRUE;
318 SetFileName(GetEnvValue(env, prefix, "FileName", fFileName));
319 }
320 if (IsEnvDefined(env, prefix, "Debug", print))
321 {
322 rc = kTRUE;
323 SetDebug(GetEnvValue(env, prefix, "Debug", fDebug));
324 }
325 return rc;
326}
Note: See TracBrowser for help on using the repository browser.