source: trunk/MagicSoft/Mars/mranforest/MHRanForestGini.cc@ 7413

Last change on this file since 7413 was 7413, checked in by tbretz, 19 years ago
*** empty log message ***
File size: 5.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 3/2003 <mailto:hengsteb@alwa02.physik.uni-siegen.de>
19!
20! Copyright: MAGIC Software Development, 2000-2003
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MHRanForest
28//
29// This histogram shows a measure of variable importance (mean decrease in
30// Gini-index)
31//
32////////////////////////////////////////////////////////////////////////////
33#include "MHRanForestGini.h"
34
35#include <TPad.h>
36#include <TGraph.h>
37#include <TStyle.h>
38#include <TCanvas.h>
39#include <TMarker.h>
40
41#include "MParList.h"
42#include "MBinning.h"
43#include "MRanTree.h"
44#include "MRanForest.h"
45#include "MDataArray.h"
46
47#include "MLog.h"
48#include "MLogManip.h"
49
50ClassImp(MHRanForestGini);
51
52using namespace std;
53
54// --------------------------------------------------------------------------
55//
56// Setup histograms, nbins is the number of bins used for the evaluation.
57// The default is 100 bins.
58//
59MHRanForestGini::MHRanForestGini(Int_t nbins, const char *name, const char *title)
60 : fRules(0.01, 0.01, 0.99, 0.99)
61{
62 //
63 // set the name and title of this object
64 //
65 fName = name ? name : "MHRanForestGini";
66 fTitle = title ? title : "Measure of importance of Random Forest-input parameters";
67
68 fGraphGini.SetNameTitle("Gini", "Importance of RF-input parameters measured by mean Gini decrease");
69 fGraphGini.SetMarkerStyle(kFullDotMedium);
70
71 fGraphError.SetNameTitle("ResErr", "Resolution/Error versus train step");
72 fGraphError.SetMarkerStyle(kFullDotMedium);
73
74 fGraphNodes.SetNameTitle("Nodes", "Number of nodes versus train step");
75 fGraphNodes.SetMarkerStyle(kFullDotMedium);
76
77 fRules.SetTextAlign(13);
78 fRules.SetTextSize(0.05);
79}
80
81// --------------------------------------------------------------------------
82//
83// Setup Filling of the histograms. It needs:
84// MMcEvt and MRanForest
85//
86Bool_t MHRanForestGini::SetupFill(const MParList *plist)
87{
88 fRanForest = (MRanForest*)plist->FindObject("MRanForest");
89 if (!fRanForest)
90 {
91 *fLog << err << dbginf << "MRanForest not found... aborting." << endl;
92 return kFALSE;
93 }
94
95 fGini.Set(fRanForest->GetNumDim());
96 return kTRUE;
97}
98
99// --------------------------------------------------------------------------
100//
101// Fill the RanForest from a MRanForest container into the corresponding
102// histogram dependant on the particle id.
103//
104//
105Bool_t MHRanForestGini::Fill(const MParContainer *par, const Stat_t w)
106{
107 MRanTree *t = fRanForest->GetCurTree();
108
109 for (Int_t i=0;i<fRanForest->GetNumDim();i++)
110 fGini[i] += t->GetGiniDec(i);
111
112 fGraphError.SetPoint(fGraphError.GetN(), GetNumExecutions(), t->GetError());
113 fGraphNodes.SetPoint(fGraphError.GetN(), GetNumExecutions(), t->GetNumEndNodes());
114
115 return kTRUE;
116}
117
118// --------------------------------------------------------------------------
119//
120//
121Bool_t MHRanForestGini::Finalize()
122{
123 const Int_t n = fGini.GetSize();
124
125 fGraphGini.Set(n);
126
127 for (Int_t i=0; i<n; i++)
128 {
129 fGini[i] /= fRanForest->GetNumTrees()*fRanForest->GetNumData();
130 fGraphGini.SetPoint(i, i+1, fGini[i]);
131 }
132
133 fRules.AddText("");
134 const MDataArray &arr = *fRanForest->GetRules();
135 int i;
136 for (i=0; i<arr.GetNumEntries(); i++)
137 {
138 TString s;
139 s += i+1;
140 s += ") ";
141 s += arr.GetRule(i);
142 fRules.AddText(s);
143 }
144 for (; i<20; i++)
145 fRules.AddText("");
146
147 return kTRUE;
148}
149
150// --------------------------------------------------------------------------
151//
152// Draw histogram. (For the Meaning see class description)
153//
154void MHRanForestGini::Draw(Option_t *)
155{
156 if (fGraphGini.GetN()==0)
157 return;
158
159 TVirtualPad *pad = gPad ? gPad : MakeDefCanvas(this);
160 pad->SetBorderMode(0);
161
162 AppendPad("");
163
164 pad->Divide(2,2);
165
166 pad->cd(1);
167 gPad->SetBorderMode(0);
168 gPad->SetGridx();
169 gPad->SetGridy();
170 fGraphGini.Draw("ALP");
171
172 TH1 *h = fGraphGini.GetHistogram();
173 if (h)
174 {
175 h->SetXTitle("No.of RF-input parameter");
176 h->SetYTitle("Mean decrease in Gini-index [au]");
177 h->GetXaxis()->SetNdivisions(10);
178 }
179
180 pad->cd(2);
181 gPad->SetBorderMode(0);
182 fGraphError.Draw("ALP");
183 h = fGraphError.GetHistogram();
184 if (h)
185 {
186 h->SetXTitle("Train step/Tree number");
187 h->SetYTitle("Error/Resolution");
188 }
189
190 pad->cd(3);
191 gPad->SetBorderMode(0);
192 fGraphNodes.Draw("ALP");
193 h = fGraphNodes.GetHistogram();
194 if (h)
195 {
196 h->SetXTitle("Train step/Tree number");
197 h->SetYTitle("Number of end nodes");
198 }
199
200 pad->cd(4);
201 gPad->SetBorderMode(0);
202 fRules.Draw();
203}
Note: See TracBrowser for help on using the repository browser.