| 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 |  | 
|---|
| 50 | ClassImp(MHRanForestGini); | 
|---|
| 51 |  | 
|---|
| 52 | using 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 | // | 
|---|
| 59 | MHRanForestGini::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 | // | 
|---|
| 86 | Bool_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 | // | 
|---|
| 105 | Bool_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 | // | 
|---|
| 121 | Bool_t MHRanForestGini::Finalize() | 
|---|
| 122 | { | 
|---|
| 123 | // --- Calculate mean decrease of gini index --- | 
|---|
| 124 | const Int_t n = fGini.GetSize(); | 
|---|
| 125 |  | 
|---|
| 126 | fGraphGini.Set(n); | 
|---|
| 127 |  | 
|---|
| 128 | for (Int_t i=0; i<n; i++) | 
|---|
| 129 | { | 
|---|
| 130 | fGini[i] /= fRanForest->GetNumTrees()*fRanForest->GetNumData(); | 
|---|
| 131 | fGraphGini.SetPoint(i, i+1, fGini[i]); | 
|---|
| 132 | } | 
|---|
| 133 |  | 
|---|
| 134 | // --- Produce some text information --- | 
|---|
| 135 | fRules.AddText(""); | 
|---|
| 136 | fRules.AddText(Form("%s w/ %d trees of node size %d trained by %d events", | 
|---|
| 137 | fRanForest->IsClassify()?"Classification":"Regression", | 
|---|
| 138 | fRanForest->GetNumTrees(), | 
|---|
| 139 | fRanForest->GetNdSize(), | 
|---|
| 140 | fRanForest->GetNumData())); | 
|---|
| 141 | fRules.AddText("---");//Form("---> %s", fRanForest->GetTargetRule().Data())); | 
|---|
| 142 |  | 
|---|
| 143 | const MDataArray &arr = *fRanForest->GetRules(); | 
|---|
| 144 |  | 
|---|
| 145 | int i; | 
|---|
| 146 | for (i=0; i<arr.GetNumEntries(); i++) | 
|---|
| 147 | fRules.AddText(Form("%d) %s", i+1, arr.GetRule(i).Data())); | 
|---|
| 148 |  | 
|---|
| 149 | for (; i<20; i++) | 
|---|
| 150 | fRules.AddText(""); | 
|---|
| 151 |  | 
|---|
| 152 | return kTRUE; | 
|---|
| 153 | } | 
|---|
| 154 |  | 
|---|
| 155 | // -------------------------------------------------------------------------- | 
|---|
| 156 | // | 
|---|
| 157 | // Draw histogram. (For the Meaning see class description) | 
|---|
| 158 | // | 
|---|
| 159 | void MHRanForestGini::Draw(Option_t *) | 
|---|
| 160 | { | 
|---|
| 161 | if (fGraphGini.GetN()==0) | 
|---|
| 162 | return; | 
|---|
| 163 |  | 
|---|
| 164 | TVirtualPad *pad = gPad ? gPad : MakeDefCanvas(this); | 
|---|
| 165 | pad->SetBorderMode(0); | 
|---|
| 166 |  | 
|---|
| 167 | AppendPad(""); | 
|---|
| 168 |  | 
|---|
| 169 | pad->Divide(2,2); | 
|---|
| 170 |  | 
|---|
| 171 | pad->cd(1); | 
|---|
| 172 | gPad->SetBorderMode(0); | 
|---|
| 173 | gPad->SetGridx(); | 
|---|
| 174 | gPad->SetGridy(); | 
|---|
| 175 | fGraphGini.Draw("ALP"); | 
|---|
| 176 |  | 
|---|
| 177 | TH1 *h = fGraphGini.GetHistogram(); | 
|---|
| 178 | if (h) | 
|---|
| 179 | { | 
|---|
| 180 | h->SetXTitle("No.of RF-input parameter"); | 
|---|
| 181 | h->SetYTitle("Mean decrease in Gini-index [au]"); | 
|---|
| 182 | h->GetXaxis()->SetNdivisions(10); | 
|---|
| 183 | } | 
|---|
| 184 |  | 
|---|
| 185 | pad->cd(2); | 
|---|
| 186 | gPad->SetBorderMode(0); | 
|---|
| 187 | fGraphError.Draw("ALP"); | 
|---|
| 188 | h = fGraphError.GetHistogram(); | 
|---|
| 189 | if (h) | 
|---|
| 190 | { | 
|---|
| 191 | h->SetXTitle("Train step/Tree number"); | 
|---|
| 192 | h->SetYTitle("Error/Resolution"); | 
|---|
| 193 | } | 
|---|
| 194 |  | 
|---|
| 195 | pad->cd(3); | 
|---|
| 196 | gPad->SetBorderMode(0); | 
|---|
| 197 | fGraphNodes.Draw("ALP"); | 
|---|
| 198 | h = fGraphNodes.GetHistogram(); | 
|---|
| 199 | if (h) | 
|---|
| 200 | { | 
|---|
| 201 | h->SetXTitle("Train step/Tree number"); | 
|---|
| 202 | h->SetYTitle("Number of end nodes"); | 
|---|
| 203 | } | 
|---|
| 204 |  | 
|---|
| 205 | pad->cd(4); | 
|---|
| 206 | gPad->SetBorderMode(0); | 
|---|
| 207 | fRules.Draw(); | 
|---|
| 208 | } | 
|---|