source: trunk/MagicSoft/Mars/mjtrain/MJTrainEnergy.cc@ 8785

Last change on this file since 8785 was 8719, checked in by tbretz, 17 years ago
*** empty log message ***
File size: 8.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 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#include "MStatusDisplay.h"
65
66// eventloop
67#include "MParList.h"
68#include "MTaskList.h"
69#include "MEvtLoop.h"
70
71// tasks
72#include "MReadMarsFile.h"
73#include "MContinue.h"
74#include "MFillH.h"
75#include "MRanForestCalc.h"
76
77// histograms
78#include "MHn.h"
79#include "MBinning.h"
80#include "MHEnergyEst.h"
81
82// filter
83#include "MFilterList.h"
84
85ClassImp(MJTrainEnergy);
86
87using namespace std;
88
89Bool_t MJTrainEnergy::Train(const char *out, const MDataSet &set, Int_t num)
90{
91 SetTitle(Form("Train%s: %s", fNameOutput.Data(), out));
92
93 if (fDisplay)
94 fDisplay->SetTitle(fTitle);
95
96 if (!set.IsValid())
97 {
98 *fLog << err << "ERROR - DataSet invalid!" << endl;
99 return kFALSE;
100 }
101
102 if (!HasWritePermission(out))
103 return kFALSE;
104
105 *fLog << inf;
106 fLog->Separator(GetDescriptor());
107
108 // --------------------- Setup files --------------------
109 MReadMarsFile readtrn("Events");
110 MReadMarsFile readtst("Events");
111 readtrn.DisableAutoScheme();
112 readtst.DisableAutoScheme();
113
114 if (!set.AddFilesOn(readtrn))
115 {
116 *fLog << err << "ERROR - Adding SequencesOn." << endl;
117 return kFALSE;
118 }
119 if (!set.AddFilesOff(readtst))
120 {
121 *fLog << err << "ERROR - Adding SequencesOff." << endl;
122 return kFALSE;
123 }
124
125 // ----------------------- Setup Matrix ------------------
126 MHMatrix train("Train");
127 train.AddColumns(fRules);
128 if (fEnableWeights)
129 train.AddColumn("MWeight.fVal");
130 train.AddColumn(fTrainParameter);
131
132 // ----------------------- Fill Matrix RF ----------------------
133 MTFillMatrix fill(fTitle);
134 fill.SetDisplay(fDisplay);
135 fill.SetLogStream(fLog);
136 fill.SetDestMatrix1(&train, num);
137 fill.SetReader(&readtrn);
138 fill.AddPreCuts(fPreCuts);
139 fill.AddPreCuts(fTrainCuts);
140 fill.AddPreTasks(fPreTasks);
141 fill.AddPostTasks(fPostTasks);
142 if (!fill.Process())
143 return kFALSE;
144
145 // ------------------------ Train RF --------------------------
146 MRanForestCalc rf("Train", fTitle);
147 rf.SetNumTrees(fNumTrees);
148 rf.SetNdSize(fNdSize);
149 rf.SetNumTry(fNumTry);
150 rf.SetNumObsoleteVariables(1);
151 rf.SetLastDataColumnHasWeights(fEnableWeights);
152 rf.SetDisplay(fDisplay);
153 rf.SetLogStream(fLog);
154 rf.SetFileName(out);
155 rf.SetDebug(fDebug>1);
156 rf.SetNameOutput(fNameOutput);
157 rf.SetFunction(fResultFunction);
158
159 /*
160 MBinning b(32, 10, 100000, "BinningEnergyEst", "log");
161
162 if (!rf.TrainMultiRF(train, b.GetEdgesD())) // classification with one tree per bin
163 return;
164
165 if (!rf.TrainSingleRF(train, b.GetEdgesD())) // classification into different bins
166 return;
167 */
168 if (!rf.TrainRegression(train)) // regression (best choice)
169 return kFALSE;
170
171 // --------------------- Display result ----------------------
172
173 gLog.Separator("Test");
174
175 MH::SetPalette("pretty");
176
177 MParList plist;
178 MTaskList tlist;
179 plist.AddToList(this);
180 plist.AddToList(&tlist);
181 //plist.AddToList(&b);
182
183 MFilterList list;
184 if (!list.AddToList(fPreCuts))
185 *fLog << err << "ERROR - Calling MFilterList::AddToList for fPreCuts failed!" << endl;
186 if (!list.AddToList(fTestCuts))
187 *fLog << err << "ERROR - Calling MFilterList::AddToList for fTestCuts failed!" << endl;
188
189 MContinue cont(&list);
190 cont.SetInverted();
191
192 // -------------------------------------------------------------
193 MBinning binsS(50, 10, 100000, "BinningSize", "log");
194 MBinning binsE(70, 10, 31623, "BinningEnergy", "log");
195 MBinning binsG(50,-10, 10, "BinningSlope", "lin");
196 MBinning binsR(50, -1, 1, "BinningEnergyResidual", "lin");
197 MBinning binsL(50, 0, 0.3, "BinningLeakage", "lin");
198 MBinning binsT(51, -0.005, 0.505, "BinningTheta", "asin");
199 MBinning binsD(50, 0, 1.6, "BinningDist", "lin");
200 MBinning binsC(50, 1e-2, 1, "BinningConc", "log");
201
202 plist.AddToList(&binsG);
203 plist.AddToList(&binsS);
204 plist.AddToList(&binsR);
205 plist.AddToList(&binsE);
206 plist.AddToList(&binsL);
207 plist.AddToList(&binsT);
208 plist.AddToList(&binsD);
209 plist.AddToList(&binsC);
210
211 MHEnergyEst hist;
212
213 // To speed it up we could precalculate it.
214 const char *res = "log10(MEnergyEst.fVal)-log10(MMcEvt.fEnergy)";
215
216 MHn hres1("Energy1", "Energy Residual (lg E_{est} - lg E_{mc})");
217 hres1.AddHist("MHillas.fSize", res);
218 hres1.InitName("ResSize;Size;EnergyResidual");
219 hres1.InitTitle(";S [phe];\\Delta lg E;");
220 hres1.SetDrawOption("colz profx");
221 hres1.AddHist("MHillasExt.fSlopeLong*sign(MHillasSrc.fCosDeltaAlpha)/3.37e-3", res);
222 hres1.InitName("ResSlope;Slope;EnergyResidual");
223 hres1.InitTitle(";Slope;\\Delta lg E;");
224 hres1.SetDrawOption("colz profx");
225 hres1.AddHist("MNewImagePar.fLeakage1", res);
226 hres1.InitName("ResLeak;Leakage;EnergyResidual");
227 hres1.InitTitle(";Leak;\\Delta lg E;");
228 hres1.SetDrawOption("colz profx");
229 hres1.AddHist("MHillasSrc.fDist*3.37e-3", res);
230 hres1.InitName("ResDist;Dist;EnergyResidual");
231 hres1.InitTitle(";D [\\circ];\\Delta lg E;");
232 hres1.SetDrawOption("colz profx");
233
234 MHn hres2("Energy2", "Energy Residual (lg E_{est} - lg E_{mc})");
235 hres2.AddHist("MMcEvt.fEnergy", res);
236 hres2.InitName("ResEmc;Energy;EnergyResidual");
237 hres2.InitTitle(";E_{mc} [GeV];\\Delta lg E;");
238 hres2.SetDrawOption("colz profx");
239 hres2.SetAutoRange(kFALSE, kFALSE, kFALSE);
240 hres2.AddHist("MPointingPos.fZd", res);
241 hres2.InitName("ResTheta;Theta;EnergyResidual");
242 hres2.InitTitle(";Zd [\\circ];\\Delta lg E;");
243 hres2.SetDrawOption("colz profx");
244 hres2.AddHist("MEnergyEst.fVal", res);
245 hres2.InitName("ResEest;Energy;EnergyResidual");
246 hres2.InitTitle(";E_{est} [GeV];\\Delta lg E;");
247 hres2.SetDrawOption("colz profx");
248 hres2.SetAutoRange(kFALSE, kFALSE, kFALSE);
249 hres2.AddHist("MNewImagePar.fConc1", res);
250 hres2.InitName("ResConc1;Conc;EnergyResidual");
251 hres2.InitTitle(";C;\\Delta lg E;");
252 hres2.SetDrawOption("colz profx");
253
254 MFillH fillh(&hist);
255 MFillH fillh1(&hres1, "", "FillResiduals1");
256 MFillH fillh2(&hres2, "", "FillResiduals2");
257
258 if (fEnableWeights)
259 {
260 fillh.SetWeight();
261 fillh1.SetWeight();
262 fillh2.SetWeight();
263 }
264
265 tlist.AddToList(&readtst);
266 tlist.AddToList(fPreTasks);
267 tlist.AddToList(&cont);
268 tlist.AddToList(&rf);
269 tlist.AddToList(fPostTasks);
270 tlist.AddToList(&fillh);
271 tlist.AddToList(&fillh1);
272 tlist.AddToList(&fillh2);
273 tlist.AddToList(fTestTasks);
274
275 MEvtLoop loop(fTitle);
276 loop.SetLogStream(fLog);
277 loop.SetDisplay(fDisplay);
278 loop.SetParList(&plist);
279 //if (!SetupEnv(loop))
280 // return kFALSE;
281
282 if (!loop.Eventloop())
283 return kFALSE;
284
285 SetPathOut(out);
286 if (!WriteDisplay(0, "UPDATE"))
287 return kFALSE;
288
289 return kTRUE;
290}
Note: See TracBrowser for help on using the repository browser.