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