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

Last change on this file since 8708 was 8704, checked in by tbretz, 18 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("TrainEnergy: %s", 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("MMcEvt.fImpact/100");
131 train.AddColumn("MMcEvt.fTelescopeTheta*TMath::RadToDeg()");
132 train.AddColumn(fTrainParameter);
133
134
135 // ----------------------- Fill Matrix RF ----------------------
136 MTFillMatrix fill(fTitle);
137 fill.SetDisplay(fDisplay);
138 fill.SetLogStream(fLog);
139 fill.SetDestMatrix1(&train, num);
140 fill.SetReader(&readtrn);
141 fill.AddPreCuts(fPreCuts);
142 fill.AddPreCuts(fTrainCuts);
143 fill.AddPreTasks(fPreTasks);
144 fill.AddPostTasks(fPostTasks);
145 if (!fill.Process())
146 return kFALSE;
147
148 // ------------------------ Train RF --------------------------
149 MRanForestCalc rf("TrainEnergy", fTitle);
150 rf.SetNumTrees(fNumTrees);
151 rf.SetNdSize(fNdSize);
152 rf.SetNumTry(fNumTry);
153 rf.SetNumObsoleteVariables(3);
154 rf.SetLastDataColumnHasWeights(fEnableWeights);
155 rf.SetDisplay(fDisplay);
156 rf.SetLogStream(fLog);
157 rf.SetFileName(out);
158 rf.SetDebug(fDebug>1);
159 rf.SetNameOutput("MEnergyEst");
160 rf.SetFunction(fResultFunction);
161
162 /*
163 MBinning b(32, 10, 100000, "BinningEnergyEst", "log");
164
165 if (!rf.TrainMultiRF(train, b.GetEdgesD())) // classification with one tree per bin
166 return;
167
168 if (!rf.TrainSingleRF(train, b.GetEdgesD())) // classification into different bins
169 return;
170 */
171 if (!rf.TrainRegression(train)) // regression (best choice)
172 return kFALSE;
173
174 // --------------------- Display result ----------------------
175
176 gLog.Separator("Test");
177
178 MParList plist;
179 MTaskList tlist;
180 plist.AddToList(this);
181 plist.AddToList(&tlist);
182 //plist.AddToList(&b);
183
184 MFilterList list;
185 if (!list.AddToList(fPreCuts))
186 *fLog << err << "ERROR - Calling MFilterList::AddToList for fPreCuts failed!" << endl;
187 if (!list.AddToList(fTestCuts))
188 *fLog << err << "ERROR - Calling MFilterList::AddToList for fTestCuts failed!" << endl;
189
190 MContinue cont(&list);
191 cont.SetInverted();
192
193 // -------------------------------------------------------------
194 MBinning binsS(50, 10, 100000, "BinningSize", "log");
195 MBinning binsE(70, 10, 31623, "BinningEnergy", "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
203 plist.AddToList(&binsG);
204 plist.AddToList(&binsS);
205 plist.AddToList(&binsR);
206 plist.AddToList(&binsE);
207 plist.AddToList(&binsL);
208 plist.AddToList(&binsT);
209 plist.AddToList(&binsD);
210 plist.AddToList(&binsC);
211
212 MHEnergyEst hist;
213
214 // To speed it up we could precalculate it.
215 const char *res = "log10(MEnergyEst.fVal)-log10(MMcEvt.fEnergy)";
216
217 MHn hres1("Energy1", "Energy Residual (lg E_{est} - lg E_{mc})");
218 hres1.AddHist("MHillas.fSize", res);
219 hres1.InitName("ResSize;Size;EnergyResidual");
220 hres1.InitTitle(";S [phe];\\Delta lg E;");
221 hres1.SetDrawOption("colz profx");
222 hres1.AddHist("MHillasExt.fSlopeLong*sign(MHillasSrc.fCosDeltaAlpha)/3.37e-3", res);
223 hres1.InitName("ResSlope;Slope;EnergyResidual");
224 hres1.InitTitle(";Slope;\\Delta lg E;");
225 hres1.SetDrawOption("colz profx");
226 hres1.AddHist("MNewImagePar.fLeakage1", res);
227 hres1.InitName("ResLeak;Leakage;EnergyResidual");
228 hres1.InitTitle(";Leak;\\Delta lg E;");
229 hres1.SetDrawOption("colz profx");
230 hres1.AddHist("MHillasSrc.fDist*3.37e-3", res);
231 hres1.InitName("ResDist;Dist;EnergyResidual");
232 hres1.InitTitle(";D [\\circ];\\Delta lg E;");
233 hres1.SetDrawOption("colz profx");
234
235 MHn hres2("Energy2", "Energy Residual (lg E_{est} - lg E_{mc})");
236 hres2.AddHist("MMcEvt.fEnergy", res);
237 hres2.InitName("ResEmc;Energy;EnergyResidual");
238 hres2.InitTitle(";E_{mc} [GeV];\\Delta lg E;");
239 hres2.SetDrawOption("colz profx");
240 hres2.SetAutoRange(kFALSE, kFALSE, kFALSE);
241 hres2.AddHist("MPointingPos.fZd", res);
242 hres2.InitName("ResTheta;Theta;EnergyResidual");
243 hres2.InitTitle(";Zd [\\circ];\\Delta lg E;");
244 hres2.SetDrawOption("colz profx");
245 hres2.AddHist("MEnergyEst.fVal", res);
246 hres2.InitName("ResEest;Energy;EnergyResidual");
247 hres2.InitTitle(";E_{est} [GeV];\\Delta lg E;");
248 hres2.SetDrawOption("colz profx");
249 hres2.SetAutoRange(kFALSE, kFALSE, kFALSE);
250 hres2.AddHist("MNewImagePar.fConc1", res);
251 hres2.InitName("ResConc1;Conc;EnergyResidual");
252 hres2.InitTitle(";C;\\Delta lg E;");
253 hres2.SetDrawOption("colz profx");
254
255 MFillH fillh(&hist);
256 MFillH fillh1(&hres1, "", "FillResiduals1");
257 MFillH fillh2(&hres2, "", "FillResiduals2");
258
259 if (fEnableWeights)
260 {
261 fillh.SetWeight();
262 fillh1.SetWeight();
263 fillh2.SetWeight();
264 }
265
266 tlist.AddToList(&readtst);
267 tlist.AddToList(fPreTasks);
268 tlist.AddToList(&cont);
269 tlist.AddToList(&rf);
270 tlist.AddToList(fPostTasks);
271 tlist.AddToList(&fillh);
272 tlist.AddToList(&fillh1);
273 tlist.AddToList(&fillh2);
274 tlist.AddToList(fTestTasks);
275
276 MEvtLoop loop(fTitle);
277 loop.SetLogStream(fLog);
278 loop.SetDisplay(fDisplay);
279 loop.SetParList(&plist);
280 //if (!SetupEnv(loop))
281 // return kFALSE;
282
283 if (!loop.Eventloop())
284 return kFALSE;
285
286 SetPathOut(out);
287 if (!WriteDisplay(0, "UPDATE"))
288 return kFALSE;
289
290 return kTRUE;
291}
Note: See TracBrowser for help on using the repository browser.