source: trunk/MagicSoft/Mars/mranforest/MRanForestCalc.cc@ 4787

Last change on this file since 4787 was 3238, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 6.8 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// 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
61ClassImp(MRanForestCalc);
62
63using namespace std;
64
65static const TString gsDefName = "MRanForestCalc";
66static 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//
73MRanForestCalc::MRanForestCalc(const char *name, const char *title)
74 : fNum(100), fHadronnessName("MHadronness"), fData(NULL)
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//
87MRanForestCalc::~MRanForestCalc()
88{
89 // delete fData;
90}
91
92// --------------------------------------------------------------------------
93//
94// Needs:
95// - MatrixGammas [MHMatrix]
96// - MatrixHadrons [MHMatrix]
97// - MHadronness
98// - all data containers used to build the matrixes
99//
100// The matrix object can be filles using MFillH. And must be of the same
101// number of columns (with the same meaning).
102//
103Int_t MRanForestCalc::PreProcess(MParList *plist)
104{
105 fRanForest = (MRanForest*)plist->FindObject("MRanForest");
106 if (!fRanForest)
107 {
108 *fLog << err << dbginf << "MRanForest not found... aborting." << endl;
109 return kFALSE;
110 }
111
112 fRanTree = (MRanTree*)plist->FindObject("MRanTree");
113 if (!fRanTree)
114 {
115 *fLog << err << dbginf << "MRanTree not found... aborting." << endl;
116 return kFALSE;
117 }
118
119 /*if(!fRanForest->GetCurTree())
120 {
121 *fLog << err << dbginf << "MRanForest does not contain trees... aborting." << endl;
122 return kFALSE;
123 }*/
124
125 fData = fRanTree->GetRules();
126
127 if (!fData)
128 {
129 *fLog << err << dbginf << "Error matrix doesn't contain columns... aborting." << endl;
130 return kFALSE;
131 }
132
133 if (!fData->PreProcess(plist))
134 {
135 *fLog << err << dbginf << "PreProcessing of the MDataArray failed for the columns failed... aborting." << endl;
136 return kFALSE;
137 }
138
139 fHadroness = (MHadronness*)plist->FindCreateObj("MHadronness", fHadronnessName);
140 if (!fHadroness)
141 return kFALSE;
142
143 return kTRUE;
144}
145
146// --------------------------------------------------------------------------
147//
148//
149Int_t MRanForestCalc::Process()
150{
151 // first copy the data from the data array to a vector event
152 TVector event;
153 *fData >> event;
154
155 Double_t hadroness=fRanForest->CalcHadroness(event);
156 fHadroness->SetHadronness(hadroness);
157
158 return kTRUE;
159}
160
161Bool_t MRanForestCalc::Grow(MHMatrix *matrixg,MHMatrix *matrixh,Int_t ntree,
162 Int_t numtry,Int_t ndsize,const char* treefile,
163 const char* treename,const char* contname,
164 const char* hgininame)
165{
166
167 treename = treename ? treename : "Tree";
168 contname = contname ? contname : "MRanTree";
169 hgininame = hgininame ? hgininame : "MHRanForestGini";
170
171 if (!matrixg->IsValid())
172 {
173 *fLog << err << dbginf << " MRanForestCalc::Grow - ERROR: matrixg not valid." << endl;
174 return kFALSE;
175 }
176 if(!matrixh->IsValid())
177 {
178 *fLog << err << dbginf << " MRanForestCalc::Grow - ERROR: matrixh not valid." << endl;
179 return kFALSE;
180 }
181
182 MEvtLoop run(GetName());
183 MTaskList tlist;
184 MParList plist;
185 plist.AddToList(&tlist);
186 plist.AddToList(matrixg);
187 plist.AddToList(matrixh);
188
189 // creating training task and setting parameters
190 MRanForestGrow rfgrow;
191 rfgrow.SetNumTrees(ntree); // number of trees
192 rfgrow.SetNumTry(numtry); // number of trials in random split selection
193 rfgrow.SetNdSize(ndsize); // limit for nodesize
194 tlist.AddToList(&rfgrow);
195
196 if(treefile){
197 MWriteRootFile rfwrite(treefile);
198 rfwrite.AddContainer(contname,treename);
199 tlist.AddToList(&rfwrite);
200 }
201
202 MFillH fillh(hgininame);
203 tlist.AddToList(&fillh);
204
205 run.SetParList(&plist);
206
207 // Execute tree growing
208 if (!run.Eventloop())
209 {
210 *fLog << err << dbginf << "Evtloop in MRanForestCalc::Grow failed." << endl;
211 return kFALSE;
212 }
213 tlist.PrintStatistics(0, kTRUE);
214
215 if (TestBit(kEnableGraphicalOutput))
216 plist.FindObject(hgininame)->DrawClone();
217
218 return kTRUE;
219}
220
221Bool_t MRanForestCalc::Fill(Int_t ntree,const char* treefile,const char* treename)
222{
223 treefile = treefile ? treefile : "RF.root";
224 treename = treename ? treename : "Tree";
225
226 MParList plist;
227
228 MTaskList tlist;
229 plist.AddToList(&tlist);
230
231 MReadTree read(treename,treefile);
232 read.DisableAutoScheme();
233
234 MRanForestFill rffill;
235 rffill.SetNumTrees(ntree);
236
237 tlist.AddToList(&read);
238 tlist.AddToList(&rffill);
239
240 MEvtLoop run(GetName());
241 run.SetParList(&plist);
242
243 //
244 // Execute tree reading
245 //
246 if (!run.Eventloop())
247 {
248 *fLog << err << dbginf << "Evtloop in MRanForestCalc::Fill failed." << endl;
249 return kFALSE;
250 }
251 tlist.PrintStatistics(0, kTRUE);
252
253 return kTRUE;
254}
Note: See TracBrowser for help on using the repository browser.