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 3/2003 <mailto:hengsteb@alwa02.physik.uni-siegen.de>
|
---|
19 | !
|
---|
20 | ! Copyright: MAGIC Software Development, 2000-2003
|
---|
21 | !
|
---|
22 | !
|
---|
23 | \* ======================================================================== */
|
---|
24 |
|
---|
25 | /////////////////////////////////////////////////////////////////////////////
|
---|
26 | //
|
---|
27 | // MRanForestCalc
|
---|
28 | //
|
---|
29 | // Calculates the hadroness of an event. It calculates a mean value of all
|
---|
30 | // classifications by the trees in a previously grown random forest.
|
---|
31 | //
|
---|
32 | // To use only n trees for your calculation use:
|
---|
33 | // MRanForestCalc::SetUseNumTrees(n);
|
---|
34 | //
|
---|
35 | ////////////////////////////////////////////////////////////////////////////
|
---|
36 | #include "MRanForestCalc.h"
|
---|
37 |
|
---|
38 | #include <TVector.h>
|
---|
39 |
|
---|
40 | #include "MHMatrix.h" // must be before MLogManip.h
|
---|
41 | #include "MDataArray.h"
|
---|
42 |
|
---|
43 | #include "MLog.h"
|
---|
44 | #include "MLogManip.h"
|
---|
45 |
|
---|
46 | #include "MParList.h"
|
---|
47 |
|
---|
48 | #include "MRanTree.h"
|
---|
49 | #include "MRanForest.h"
|
---|
50 |
|
---|
51 | #include "MParameters.h"
|
---|
52 |
|
---|
53 | #include "MEvtLoop.h"
|
---|
54 | #include "MTaskList.h"
|
---|
55 | #include "MFillH.h"
|
---|
56 | #include "MStatusDisplay.h"
|
---|
57 | #include "MRanForestGrow.h"
|
---|
58 | #include "MRanForestFill.h"
|
---|
59 |
|
---|
60 | #include "MWriteRootFile.h"
|
---|
61 | #include "MReadTree.h"
|
---|
62 |
|
---|
63 | ClassImp(MRanForestCalc);
|
---|
64 |
|
---|
65 | using namespace std;
|
---|
66 |
|
---|
67 | static const TString gsDefName = "MRanForestCalc";
|
---|
68 | static const TString gsDefTitle = "Tree Classification Loop 1/2";
|
---|
69 |
|
---|
70 | // --------------------------------------------------------------------------
|
---|
71 | //
|
---|
72 | // Setup histograms and the number of distances which are used for
|
---|
73 | // avaraging in CalcDist
|
---|
74 | //
|
---|
75 | MRanForestCalc::MRanForestCalc(const char *name, const char *title)
|
---|
76 | : fNum(100), fHadronnessName("MHadronness"), fData(NULL), fRanForest(0), fRanTree(0)
|
---|
77 | {
|
---|
78 | //
|
---|
79 | // set the name and title of this object
|
---|
80 | //
|
---|
81 | fName = name ? name : gsDefName.Data();
|
---|
82 | fTitle = title ? title : gsDefTitle.Data();
|
---|
83 | }
|
---|
84 |
|
---|
85 | // --------------------------------------------------------------------------
|
---|
86 | //
|
---|
87 | // Delete the data chains
|
---|
88 | //
|
---|
89 | MRanForestCalc::~MRanForestCalc()
|
---|
90 | {
|
---|
91 | if (!IsOwner())
|
---|
92 | return;
|
---|
93 |
|
---|
94 | delete fRanForest;
|
---|
95 | delete fRanTree;
|
---|
96 | }
|
---|
97 |
|
---|
98 | // --------------------------------------------------------------------------
|
---|
99 | //
|
---|
100 | // Needs:
|
---|
101 | // - MatrixGammas [MHMatrix]
|
---|
102 | // - MatrixHadrons [MHMatrix]
|
---|
103 | // - MHadronness
|
---|
104 | // - all data containers used to build the matrixes
|
---|
105 | //
|
---|
106 | // The matrix object can be filles using MFillH. And must be of the same
|
---|
107 | // number of columns (with the same meaning).
|
---|
108 | //
|
---|
109 | Int_t MRanForestCalc::PreProcess(MParList *plist)
|
---|
110 | {
|
---|
111 | if (!fRanForest)
|
---|
112 | {
|
---|
113 | fRanForest = (MRanForest*)plist->FindObject("MRanForest");
|
---|
114 | if (!fRanForest)
|
---|
115 | {
|
---|
116 | *fLog << err << dbginf << "MRanForest not found... aborting." << endl;
|
---|
117 | return kFALSE;
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | if (!fRanTree)
|
---|
122 | {
|
---|
123 | fRanTree = (MRanTree*)plist->FindObject("MRanTree");
|
---|
124 | if (!fRanTree)
|
---|
125 | {
|
---|
126 | *fLog << err << dbginf << "MRanTree not found... aborting." << endl;
|
---|
127 | return kFALSE;
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | fData = fRanTree->GetRules();
|
---|
132 |
|
---|
133 | if (!fData)
|
---|
134 | {
|
---|
135 | *fLog << err << dbginf << "Error matrix doesn't contain columns... aborting." << endl;
|
---|
136 | return kFALSE;
|
---|
137 | }
|
---|
138 |
|
---|
139 | if (!fData->PreProcess(plist))
|
---|
140 | {
|
---|
141 | *fLog << err << dbginf << "PreProcessing of the MDataArray failed for the columns failed... aborting." << endl;
|
---|
142 | return kFALSE;
|
---|
143 | }
|
---|
144 |
|
---|
145 | fHadroness = (MParameterD*)plist->FindCreateObj("MParameterD", fHadronnessName);
|
---|
146 | if (!fHadroness)
|
---|
147 | return kFALSE;
|
---|
148 |
|
---|
149 | return kTRUE;
|
---|
150 | }
|
---|
151 |
|
---|
152 | // --------------------------------------------------------------------------
|
---|
153 | //
|
---|
154 | //
|
---|
155 | Int_t MRanForestCalc::Process()
|
---|
156 | {
|
---|
157 | // first copy the data from the data array to a vector event
|
---|
158 | TVector event;
|
---|
159 | *fData >> event;
|
---|
160 |
|
---|
161 | Double_t hadroness=fRanForest->CalcHadroness(event);
|
---|
162 | fHadroness->SetVal(hadroness);
|
---|
163 |
|
---|
164 | return kTRUE;
|
---|
165 | }
|
---|
166 |
|
---|
167 | Bool_t MRanForestCalc::Grow(MHMatrix *matrixg,MHMatrix *matrixh,Int_t ntree,
|
---|
168 | Int_t numtry,Int_t ndsize,const char* treefile,
|
---|
169 | const char* treename,const char* contname,
|
---|
170 | const char* hgininame)
|
---|
171 | {
|
---|
172 |
|
---|
173 | treename = treename ? treename : "Tree";
|
---|
174 | contname = contname ? contname : "MRanTree";
|
---|
175 | hgininame = hgininame ? hgininame : "MHRanForestGini";
|
---|
176 |
|
---|
177 | if (!matrixg->IsValid())
|
---|
178 | {
|
---|
179 | *fLog << err << dbginf << " MRanForestCalc::Grow - ERROR: matrixg not valid." << endl;
|
---|
180 | return kFALSE;
|
---|
181 | }
|
---|
182 | if(!matrixh->IsValid())
|
---|
183 | {
|
---|
184 | *fLog << err << dbginf << " MRanForestCalc::Grow - ERROR: matrixh not valid." << endl;
|
---|
185 | return kFALSE;
|
---|
186 | }
|
---|
187 |
|
---|
188 | MEvtLoop run(GetName());
|
---|
189 | MTaskList tlist;
|
---|
190 | MParList plist;
|
---|
191 | plist.AddToList(&tlist);
|
---|
192 | plist.AddToList(matrixg);
|
---|
193 | plist.AddToList(matrixh);
|
---|
194 |
|
---|
195 | // creating training task and setting parameters
|
---|
196 | MRanForestGrow rfgrow;
|
---|
197 | rfgrow.SetNumTrees(ntree); // number of trees
|
---|
198 | rfgrow.SetNumTry(numtry); // number of trials in random split selection
|
---|
199 | rfgrow.SetNdSize(ndsize); // limit for nodesize
|
---|
200 | tlist.AddToList(&rfgrow);
|
---|
201 |
|
---|
202 | if(treefile){
|
---|
203 | MWriteRootFile rfwrite(treefile);
|
---|
204 | rfwrite.AddContainer(contname,treename);
|
---|
205 | tlist.AddToList(&rfwrite);
|
---|
206 | }
|
---|
207 |
|
---|
208 | MFillH fillh(hgininame);
|
---|
209 | tlist.AddToList(&fillh);
|
---|
210 |
|
---|
211 | run.SetParList(&plist);
|
---|
212 |
|
---|
213 | // Execute tree growing
|
---|
214 | if (!run.Eventloop())
|
---|
215 | {
|
---|
216 | *fLog << err << dbginf << "Evtloop in MRanForestCalc::Grow failed." << endl;
|
---|
217 | return kFALSE;
|
---|
218 | }
|
---|
219 | tlist.PrintStatistics(0, kTRUE);
|
---|
220 |
|
---|
221 | if (TestBit(kEnableGraphicalOutput))
|
---|
222 | plist.FindObject(hgininame)->DrawClone();
|
---|
223 |
|
---|
224 | return kTRUE;
|
---|
225 | }
|
---|
226 |
|
---|
227 | Bool_t MRanForestCalc::Fill(Int_t ntree,const char* treefile,const char* treename)
|
---|
228 | {
|
---|
229 | treefile = treefile ? treefile : "RF.root";
|
---|
230 | treename = treename ? treename : "Tree";
|
---|
231 |
|
---|
232 | MParList plist;
|
---|
233 |
|
---|
234 | MTaskList tlist;
|
---|
235 | plist.AddToList(&tlist);
|
---|
236 |
|
---|
237 | MReadTree read(treename,treefile);
|
---|
238 | read.DisableAutoScheme();
|
---|
239 |
|
---|
240 | MRanForestFill rffill;
|
---|
241 | rffill.SetNumTrees(ntree);
|
---|
242 |
|
---|
243 | tlist.AddToList(&read);
|
---|
244 | tlist.AddToList(&rffill);
|
---|
245 |
|
---|
246 | MEvtLoop run(GetName());
|
---|
247 | run.SetParList(&plist);
|
---|
248 |
|
---|
249 | //
|
---|
250 | // Execute tree reading
|
---|
251 | //
|
---|
252 | if (!run.Eventloop())
|
---|
253 | {
|
---|
254 | *fLog << err << dbginf << "Evtloop in MRanForestCalc::Fill failed." << endl;
|
---|
255 | return kFALSE;
|
---|
256 | }
|
---|
257 | tlist.PrintStatistics(0, kTRUE);
|
---|
258 |
|
---|
259 | return kTRUE;
|
---|
260 | }
|
---|
261 |
|
---|
262 | Int_t MRanForestCalc::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
|
---|
263 | {
|
---|
264 | if (!IsEnvDefined(env, prefix, "File", print))
|
---|
265 | return kFALSE;
|
---|
266 |
|
---|
267 | TString fname = GetEnvValue(env, prefix, "File", "RF.root");
|
---|
268 | TString tname = GetEnvValue(env, prefix, "Tree", "Tree");
|
---|
269 | const Int_t num = GetEnvValue(env, prefix, "NumTrees", 100);
|
---|
270 | const Bool_t debug = GetEnvValue(env, prefix, "Debug", kFALSE);
|
---|
271 |
|
---|
272 | fname.ReplaceAll("\015", "");
|
---|
273 | tname.ReplaceAll("\015", "");
|
---|
274 |
|
---|
275 | *fLog << inf << dbginf << "Reading " << num << " trees " << tname << " from file " << fname << endl;
|
---|
276 |
|
---|
277 | gLog.SetNullOutput(!debug);
|
---|
278 | MEvtLoop evtloop;
|
---|
279 | MParList plist;
|
---|
280 | evtloop.SetParList(&plist);
|
---|
281 | MLog l;
|
---|
282 | l.SetNullOutput(!debug);
|
---|
283 | evtloop.SetLogStream(&l);
|
---|
284 | gLog.SetNullOutput(debug);
|
---|
285 |
|
---|
286 | if (IsOwner())
|
---|
287 | {
|
---|
288 | delete fRanForest;
|
---|
289 | delete fRanTree;
|
---|
290 | }
|
---|
291 | fRanForest = new MRanForest;
|
---|
292 | fRanTree = new MRanTree;
|
---|
293 | SetOwner();
|
---|
294 |
|
---|
295 | plist.AddToList(fRanForest);
|
---|
296 | plist.AddToList(fRanTree);
|
---|
297 |
|
---|
298 | MTaskList tlist;
|
---|
299 | plist.AddToList(&tlist);
|
---|
300 |
|
---|
301 | MReadTree read(tname, fname);
|
---|
302 | read.DisableAutoScheme();
|
---|
303 |
|
---|
304 | MRanForestFill rffill;
|
---|
305 | rffill.SetNumTrees(num);
|
---|
306 |
|
---|
307 | tlist.AddToList(&read);
|
---|
308 | tlist.AddToList(&rffill);
|
---|
309 |
|
---|
310 | if (!evtloop.Eventloop())
|
---|
311 | {
|
---|
312 | *fLog << err << "ERROR - Reading " << tname << " from file " << fname << endl;
|
---|
313 | return kERROR;
|
---|
314 | }
|
---|
315 |
|
---|
316 | return kTRUE;
|
---|
317 | }
|
---|