source: tags/Mars-V0.10.2/mjtrain/MJTrainEnergy.cc

Last change on this file was 7700, checked in by tbretz, 19 years ago
*** empty log message ***
File size: 5.3 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 Bretz 11/2005 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2005
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MJTrainEnergy
28//
29//
30// Example:
31// --------
32//
33// // SequencesOn are used for training, SequencesOff for Testing
34// MDataSet set("mctesttrain.txt");
35// set.SetNumAnalysis(1); // Must have a number
36// MJTrainEnergy opt;
37// //opt.SetDebug();
38// opt.AddParameter("MHillas.fSize");
39// opt.AddParameter("MHillasSrc.fDist");
40// MStatusDisplay *d = new MStatusDisplay;
41// opt.SetDisplay(d);
42// opt.AddPreCut("MHillasSrc.fDCA*MGeomCam.fConvMm2Deg<0.3");
43// opt.Train("rf-energy.root", set, 30000); // Number of train events
44//
45// Random Numbers:
46// ---------------
47// Use:
48// if(gRandom)
49// delete gRandom;
50// gRandom = new TRandom3();
51// in advance to change the random number generator.
52//
53////////////////////////////////////////////////////////////////////////////
54#include "MJTrainEnergy.h"
55
56#include "MHMatrix.h"
57
58#include "MLog.h"
59#include "MLogManip.h"
60
61// tools
62#include "MDataSet.h"
63#include "MTFillMatrix.h"
64
65// eventloop
66#include "MParList.h"
67#include "MTaskList.h"
68#include "MEvtLoop.h"
69
70// tasks
71#include "MReadMarsFile.h"
72#include "MContinue.h"
73#include "MFillH.h"
74#include "MRanForestCalc.h"
75
76// histograms
77#include "MHEnergyEst.h"
78
79// filter
80#include "MFilterList.h"
81
82ClassImp(MJTrainEnergy);
83
84using namespace std;
85
86Bool_t MJTrainEnergy::Train(const char *out, const MDataSet &set, Int_t num)
87{
88 if (!set.IsValid())
89 {
90 *fLog << err << "ERROR - DataSet invalid!" << endl;
91 return kFALSE;
92 }
93
94 *fLog << inf;
95 fLog->Separator(GetDescriptor());
96
97 // --------------------- Setup files --------------------
98 MReadMarsFile readtrn("Events");
99 MReadMarsFile readtst("Events");
100 readtrn.DisableAutoScheme();
101 readtst.DisableAutoScheme();
102
103 set.AddFilesOn(readtrn);
104 set.AddFilesOff(readtst);
105
106 // ----------------------- Setup Matrix ------------------
107 MHMatrix train("Train");
108 train.AddColumns(fRules);
109 if (fEnableWeights)
110 train.AddColumn("MWeight.fVal");
111 train.AddColumn("MMcEvt.fImpact/100");
112 train.AddColumn("MMcEvt.fTelescopeTheta*kRad2Deg");
113 train.AddColumn("MMcEvt.fEnergy");
114
115
116 // ----------------------- Fill Matrix RF ----------------------
117 MTFillMatrix fill;
118 fill.SetDisplay(fDisplay);
119 fill.SetLogStream(fLog);
120 fill.SetDestMatrix1(&train, num);
121 fill.SetReader(&readtrn);
122 fill.AddPreCuts(fPreCuts);
123 fill.AddPreCuts(fTrainCuts);
124 fill.AddPreTasks(fPreTasks);
125 fill.AddPostTasks(fPostTasks);
126 if (!fill.Process())
127 return kFALSE;
128
129 // ------------------------ Train RF --------------------------
130 MRanForestCalc rf;
131 rf.SetNumTrees(fNumTrees);
132 rf.SetNdSize(fNdSize);
133 rf.SetNumTry(fNumTry);
134 rf.SetNumObsoleteVariables(3);
135 rf.SetLastDataColumnHasWeights(fEnableWeights);
136 rf.SetDisplay(fDisplay);
137 rf.SetLogStream(fLog);
138 rf.SetFileName(out);
139 rf.SetDebug(fDebug);
140 rf.SetNameOutput("MEnergyEst");
141
142 /*
143 MBinning b(32, 10, 100000, "BinningEnergyEst", "log");
144
145 if (!rf.TrainMultiRF(train, b.GetEdgesD())) // classification with one tree per bin
146 return;
147
148 if (!rf.TrainSingleRF(train, b.GetEdgesD())) // classification into different bins
149 return;
150 */
151 if (!rf.TrainRegression(train)) // regression (best choice)
152 return kFALSE;
153
154 // --------------------- Display result ----------------------
155
156 gLog.Separator("Test");
157
158 MParList plist;
159 MTaskList tlist;
160 plist.AddToList(this);
161 plist.AddToList(&tlist);
162 //plist.AddToList(&b);
163
164 MFilterList list;
165 if (!list.AddToList(fPreCuts))
166 *fLog << err << "ERROR - Calling MFilterList::AddToList for fPreCuts failed!" << endl;
167 if (!list.AddToList(fTestCuts))
168 *fLog << err << "ERROR - Calling MFilterList::AddToList for fTestCuts failed!" << endl;
169
170 MContinue cont(&list);
171 cont.SetInverted();
172
173 MHEnergyEst hist;
174 MFillH fillh(&hist);
175 if (fEnableWeights)
176 fillh.SetWeight();
177
178 tlist.AddToList(&readtst);
179 tlist.AddToList(fPreTasks);
180 tlist.AddToList(&cont);
181 tlist.AddToList(&rf);
182 tlist.AddToList(fPostTasks);
183 tlist.AddToList(&fillh);
184
185 MEvtLoop loop;
186 loop.SetLogStream(fLog);
187 loop.SetDisplay(fDisplay);
188 loop.SetParList(&plist);
189
190 if (!loop.Eventloop())
191 return kFALSE;
192
193 if (!WriteDisplay(out))
194 return kFALSE;
195
196 return kTRUE;
197}
Note: See TracBrowser for help on using the repository browser.