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

Last change on this file since 9492 was 8888, checked in by tbretz, 17 years ago
*** empty log message ***
File size: 10.2 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 binsF(35, 10, 31623, "BinningEnergyEst", "log");
196 MBinning binsG(50, -10, 10, "BinningSlope", "lin");
197 MBinning binsR(50, -1, 1, "BinningEnergyResidual", "lin");
198 MBinning binsL(50, 0, 0.3, "BinningLeakage", "lin");
199 MBinning binsT(51, -0.005, 0.505, "BinningTheta", "asin");
200 MBinning binsD(50, 0, 1.6, "BinningDist", "lin");
201 MBinning binsC(50, 1e-2, 1, "BinningConc", "log");
202 MBinning binsI(16, 0, 800, "BinningImpact", "lin");
203
204 plist.AddToList(&binsG);
205 plist.AddToList(&binsS);
206 plist.AddToList(&binsR);
207 plist.AddToList(&binsE);
208 plist.AddToList(&binsF);
209 plist.AddToList(&binsL);
210 plist.AddToList(&binsT);
211 plist.AddToList(&binsD);
212 plist.AddToList(&binsC);
213 plist.AddToList(&binsI);
214
215 MHEnergyEst hist;
216
217 // To speed it up we could precalculate it.
218 const char *res = "log10(MEnergyEst.fVal)-log10(MMcEvt.fEnergy)";
219
220 MHn hres1("Energy1", "Energy Residual (lg E_{est} - lg E_{mc})");
221 hres1.AddHist("MHillas.fSize", res);
222 hres1.InitName("ResSize;Size;EnergyResidual");
223 hres1.InitTitle(";S [phe];\\Delta lg E;");
224 hres1.SetDrawOption("colz profx");
225 hres1.AddHist("MHillasExt.fSlopeLong*sign(MHillasSrc.fCosDeltaAlpha)/3.37e-3", res);
226 hres1.InitName("ResSlope;Slope;EnergyResidual");
227 hres1.InitTitle(";Slope;\\Delta lg E;");
228 hres1.SetDrawOption("colz profx");
229 hres1.AddHist("MNewImagePar.fLeakage1", res);
230 hres1.InitName("ResLeak;Leakage;EnergyResidual");
231 hres1.InitTitle(";Leak;\\Delta lg E;");
232 hres1.SetDrawOption("colz profx");
233 hres1.AddHist("MHillasSrc.fDist*3.37e-3", res);
234 hres1.InitName("ResDist;Dist;EnergyResidual");
235 hres1.InitTitle(";D [\\circ];\\Delta lg E;");
236 hres1.SetDrawOption("colz profx");
237
238 MHn hres2("Energy2", "Energy Residual (lg E_{est} - lg E_{mc})");
239 hres2.AddHist("MMcEvt.fEnergy", res);
240 hres2.InitName("ResEmc;Energy;EnergyResidual");
241 hres2.InitTitle(";E_{mc} [GeV];\\Delta lg E;");
242 hres2.SetDrawOption("colz profx");
243 hres2.SetAutoRange(kFALSE, kFALSE, kFALSE);
244 hres2.AddHist("MPointingPos.fZd", res);
245 hres2.InitName("ResTheta;Theta;EnergyResidual");
246 hres2.InitTitle(";Zd [\\circ];\\Delta lg E;");
247 hres2.SetDrawOption("colz profx");
248 hres2.AddHist("MEnergyEst.fVal", res);
249 hres2.InitName("ResEest;Energy;EnergyResidual");
250 hres2.InitTitle(";E_{est} [GeV];\\Delta lg E;");
251 hres2.SetDrawOption("colz profx");
252 hres2.SetAutoRange(kFALSE, kFALSE, kFALSE);
253 hres2.AddHist("MNewImagePar.fConc1", res);
254 hres2.InitName("ResConc1;Conc;EnergyResidual");
255 hres2.InitTitle(";C;\\Delta lg E;");
256 hres2.SetDrawOption("colz profx");
257
258 MHn hres3("Resolution", "Energy Resolution");
259 hres3.AddHist("MEnergyEst.fVal", "(MMcEvt.fEnergy/MEnergyEst.fVal-1)^2", MH3::kProfile);
260 hres3.InitName("ResEest;EnergyEst;");
261 hres3.InitTitle(";E_{est} [GeV];Resolution (E_{mc}/E_{est}-1)^{2};");
262
263 hres3.AddHist("MHillas.fSize", "(MMcEvt.fEnergy/MEnergyEst.fVal-1)^2", MH3::kProfile);
264 hres3.InitName("ResSize;Size;");
265 hres3.InitTitle(";S [phe];Resolution (E_{mc}/E_{est}-1)^{2};");
266/*
267 hres3.AddHist("MMcEvt.fEnergy", "(MEnergyEst.fVal/MMcEvt.fEnergy-1)^2", MH3::kProfile);
268 hres3.InitName("ResEmc;EnergyEst;");
269 hres3.InitTitle(";E_{mc} [GeV];Resolution (E_{est}/E_{mc}-1)^{2};");
270 */
271 hres3.AddHist("MPointingPos.fZd", "(MMcEvt.fEnergy/MEnergyEst.fVal-1)^2", MH3::kProfile);
272 hres3.InitName("ResTheta;Theta;");
273 hres3.InitTitle(";\\Theta [\\circ];Resolution (E_{mc}/E_{est}-1)^{2};");
274 hres3.AddHist("MMcEvt.fImpact/100", "(MMcEvt.fEnergy/MEnergyEst.fVal-1)^2", MH3::kProfile);
275 hres3.InitName("ResImpact;Impact;");
276 hres3.InitTitle(";I [m];Resolution (E_{mc}/E_{est}-1)^{2};");
277
278 MFillH fillh0(&hist);
279 MFillH fillh1(&hres1, "", "FillResiduals1");
280 MFillH fillh2(&hres2, "", "FillResiduals2");
281 MFillH fillh3(&hres3, "", "FillResolution");
282
283 if (fEnableWeights)
284 {
285 fillh0.SetWeight();
286 fillh1.SetWeight();
287 fillh2.SetWeight();
288 fillh3.SetWeight();
289 }
290
291 tlist.AddToList(&readtst);
292 tlist.AddToList(fPreTasks);
293 tlist.AddToList(&cont);
294 tlist.AddToList(&rf);
295 tlist.AddToList(fPostTasks);
296 tlist.AddToList(&fillh0);
297 tlist.AddToList(&fillh1);
298 tlist.AddToList(&fillh2);
299 tlist.AddToList(&fillh3);
300 tlist.AddToList(fTestTasks);
301
302 MEvtLoop loop(fTitle);
303 loop.SetLogStream(fLog);
304 loop.SetDisplay(fDisplay);
305 loop.SetParList(&plist);
306 //if (!SetupEnv(loop))
307 // return kFALSE;
308
309 if (!loop.Eventloop())
310 return kFALSE;
311
312 TObjArray arr;
313 arr.Add(const_cast<MDataSet*>(&set));
314 if (fDisplay)
315 arr.Add(fDisplay);
316
317 SetPathOut(out);
318 return WriteContainer(arr, 0, "UPDATE");
319}
Note: See TracBrowser for help on using the repository browser.