source: trunk/MagicSoft/Mars/mjtrain/MJTrainDisp.cc@ 8646

Last change on this file since 8646 was 8644, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 9.7 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-2007
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MJTrainDisp
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// MJTrainDisp opt;
37// //opt.SetDebug();
38// opt.AddParameter("MHillas.fLength");
39// opt.AddParameter("MHillas.fWidth");
40// MStatusDisplay *d = new MStatusDisplay;
41// opt.SetDisplay(d);
42// opt.AddPreCut("MHillasSrc.fDCA*MGeomCam.fConvMm2Deg<0.3");
43// opt.Train("rf-disp.root", set, 30000); // Number of train events
44//
45//
46// Random Numbers:
47// ---------------
48// Use:
49// if(gRandom)
50// delete gRandom;
51// gRandom = new TRandom3();
52// in advance to change the random number generator.
53//
54////////////////////////////////////////////////////////////////////////////
55#include "MJTrainDisp.h"
56
57#include <TLine.h>
58#include <TCanvas.h>
59
60#include "MHMatrix.h"
61
62#include "MLog.h"
63#include "MLogManip.h"
64
65// tools
66#include "MDataSet.h"
67#include "MTFillMatrix.h"
68#include "MChisqEval.h"
69#include "MStatusDisplay.h"
70
71// eventloop
72#include "MParList.h"
73#include "MTaskList.h"
74#include "MEvtLoop.h"
75
76// tasks
77#include "MReadMarsFile.h"
78#include "MContinue.h"
79#include "MFillH.h"
80#include "MRanForestCalc.h"
81#include "MParameterCalc.h"
82
83// container
84#include "MParameters.h"
85
86// histograms
87#include "MBinning.h"
88#include "MH3.h"
89#include "MHThetaSq.h"
90
91// filter
92#include "MFilterList.h"
93
94ClassImp(MJTrainDisp);
95
96using namespace std;
97
98const TString MJTrainDisp::fgTrainParameter = "MHillasSrc.fDist*MGeomCam.fConvMm2Deg";
99
100// --------------------------------------------------------------------------
101//
102// Display a result histogram either vs. size or energy
103// FIXME: Could be moved into a new histogram class.
104//
105void MJTrainDisp::DisplayHist(TCanvas &c, Int_t i, MH3 &mh3) const
106{
107 MH::SetPalette("pretty");
108
109 TH1 &hist = *(TH1*)mh3.GetHist().Clone();
110 hist.SetBit(TH1::kNoStats);
111 hist.SetDirectory(0);
112
113 TLine line;
114 line.SetLineStyle(kDashed);
115 line.SetLineWidth(1);
116
117 c.cd(i);
118 gPad->SetBorderMode(0);
119 gPad->SetFrameBorderMode(0);
120 //gPad->SetFillColor(kWhite);
121 gPad->SetLogx();
122 gPad->SetGridx();
123 gPad->SetGridy();
124 //gPad->SetLeftMargin(0.12);
125 //gPad->SetRightMargin(0.12);
126
127 for (int x=0; x<=hist.GetNbinsX(); x++)
128 {
129 Float_t n = 0;
130 for (int y=1; y<=2; y++)
131 n += hist.GetBinContent(x,y);
132
133 if (n==0)
134 continue;
135
136 for (int y=0; y<=hist.GetNbinsY(); y++)
137 hist.SetBinContent(x, y, 200*hist.GetBinContent(x,y)/n);
138 }
139
140 hist.SetMaximum(100);
141 hist.DrawCopy("colz");
142
143 line.DrawLine(10, 0.04, 31623, 0.04);
144
145 c.cd(i+2);
146 gPad->SetBorderMode(0);
147 gPad->SetFrameBorderMode(0);
148 //gPad->SetFillColor(kWhite);
149 gPad->SetLogx();
150 gPad->SetGridx();
151 gPad->SetGridy();
152 //gPad->SetLeftMargin(0.12);
153 //gPad->SetRightMargin(0.12);
154
155 for (int x=0; x<=hist.GetNbinsX(); x++)
156 {
157 Float_t n = 0;
158 for (int y=0; y<=hist.GetNbinsY(); y++)
159 n += hist.GetBinContent(x,y);
160
161 if (n==0)
162 continue;
163
164 for (int y=0; y<=hist.GetNbinsY(); y++)
165 hist.SetBinContent(x, y, 100*hist.GetBinContent(x,y)/n);
166 }
167
168 hist.SetMaximum(25);
169 hist.DrawCopy("colz");
170
171 line.DrawLine(10, 0.04, 31623, 0.04);
172}
173
174// --------------------------------------------------------------------------
175//
176// Display the result histograms in a new tab.
177//
178void MJTrainDisp::DisplayResult(MH3 &hsize, MH3 &henergy)
179{
180 TCanvas &c = fDisplay->AddTab("Disp");
181 c.Divide(2,2);
182
183 DisplayHist(c, 1, hsize);
184 DisplayHist(c, 2, henergy);
185}
186
187// --------------------------------------------------------------------------
188//
189// Run Disp optimization
190//
191Bool_t MJTrainDisp::Train(const char *out, const MDataSet &set, Int_t num)
192{
193 SetTitle(Form("TrainDisp: %s", out));
194
195 if (fDisplay)
196 fDisplay->SetTitle(fTitle);
197
198 if (!set.IsValid())
199 {
200 *fLog << err << "ERROR - DataSet invalid!" << endl;
201 return kFALSE;
202 }
203
204 if (!HasWritePermission(out))
205 return kFALSE;
206
207 *fLog << inf;
208 fLog->Separator(GetDescriptor());
209
210 // --------------------- Setup files --------------------
211 MReadMarsFile readtrn("Events");
212 MReadMarsFile readtst("Events");
213 readtrn.DisableAutoScheme();
214 readtst.DisableAutoScheme();
215
216 if (!set.AddFilesOn(readtrn))
217 return kFALSE;
218 if (!set.AddFilesOff(readtst))
219 return kFALSE;
220
221 // ----------------------- Setup Matrix ------------------
222 MHMatrix train("Train");
223 train.AddColumns(fRules);
224 if (fEnableWeights)
225 train.AddColumn("MWeight.fVal");
226 train.AddColumn(fgTrainParameter);
227
228 // ----------------------- Fill Matrix RF ----------------------
229 MTFillMatrix fill(fTitle);
230 fill.SetDisplay(fDisplay);
231 fill.SetLogStream(fLog);
232 fill.SetDestMatrix1(&train, num);
233 fill.SetReader(&readtrn);
234 fill.AddPreCuts(fPreCuts);
235 fill.AddPreCuts(fTrainCuts);
236 fill.AddPreTasks(fPreTasks);
237 fill.AddPostTasks(fPostTasks);
238 if (!fill.Process())
239 return kFALSE;
240
241 // ------------------------ Train RF --------------------------
242 MRanForestCalc rf(fTitle);
243 rf.SetNumTrees(fNumTrees);
244 rf.SetNdSize(fNdSize);
245 rf.SetNumTry(fNumTry);
246 rf.SetNumObsoleteVariables(1);
247 rf.SetLastDataColumnHasWeights(fEnableWeights);
248 rf.SetDisplay(fDisplay);
249 rf.SetLogStream(fLog);
250 rf.SetFileName(out);
251 rf.SetDebug(fDebug>1);
252 rf.SetNameOutput("Disp");
253
254 /*
255 MBinning b(32, 10, 100000, "BinningEnergyEst", "log");
256
257 if (!rf.TrainMultiRF(train, b.GetEdgesD())) // classification with one tree per bin
258 return;
259
260 if (!rf.TrainSingleRF(train, b.GetEdgesD())) // classification into different bins
261 return;
262 */
263 if (!rf.TrainRegression(train)) // regression (best choice)
264 return kFALSE;
265
266 // --------------------- Display result ----------------------
267
268 gLog.Separator("Test");
269
270 MParList plist;
271 MTaskList tlist;
272 plist.AddToList(this);
273 plist.AddToList(&tlist);
274 //plist.AddToList(&b);
275
276 MParameterD par("ThetaSquaredCut");
277 par.SetVal(0.2);
278 plist.AddToList(&par);
279
280 MAlphaFitter fit;
281 fit.SetPolynomOrder(0);
282 fit.SetSignalFitMax(0.8);
283 fit.EnableBackgroundFit(kFALSE);
284 fit.SetSignalFunction(MAlphaFitter::kThetaSq);
285 fit.SetMinimizationStrategy(MAlphaFitter::kGaussSigma);
286 plist.AddToList(&fit);
287
288 MFilterList list;
289 if (!list.AddToList(fPreCuts))
290 *fLog << err << "ERROR - Calling MFilterList::AddToList for fPreCuts failed!" << endl;
291 if (!list.AddToList(fTestCuts))
292 *fLog << err << "ERROR - Calling MFilterList::AddToList for fTestCuts failed!" << endl;
293
294 MContinue cont(&list);
295 cont.SetInverted();
296
297 MHThetaSq hist;
298 hist.SkipHistTime();
299 hist.SkipHistTheta();
300 hist.SkipHistEnergy();
301
302 MFillH fillh(&hist, "", "FillThetaSq");
303
304 // 0 = disp^2 - 2*disp*dist*cos(alpha) + dist^2
305
306 // cos^2 -1 = - sin^2
307
308 // disp = +dist* (cos(alpha) +/- sqrt(cos^2(alpha) - 1) )
309
310 const char *rule = "(MHillasSrc.fDist*MGeomCam.fConvMm2Deg)^2 + (Disp.fVal)^2 - (2*MHillasSrc.fDist*MGeomCam.fConvMm2Deg*Disp.fVal*cos(MHillasSrc.fAlpha*kDeg2Rad))";
311
312 MParameterCalc calcthetasq(rule, "MThetaSqCalc");
313 calcthetasq.SetNameParameter("ThetaSquared");
314
315 MChisqEval eval;
316 eval.SetY1("sqrt(ThetaSquared.fVal)");
317
318 MH3 hdisp1("MHillas.fSize", "ThetaSquared.fVal");
319 MH3 hdisp2("MMcEvt.fEnergy", "ThetaSquared.fVal");
320 hdisp1.SetTitle("\\vartheta distribution vs. Size:Size [phe]:\\vartheta [\\circ]");
321 hdisp2.SetTitle("\\vartheta distribution vs. Energy:Enerhy [GeV]:\\vartheta [\\circ]");
322
323 MBinning binsx( 70, 10, 31623, "BinningMH3X", "log");
324 MBinning binsy( 75, 0, 0.3, "BinningMH3Y", "lin");
325
326 plist.AddToList(&binsx);
327 plist.AddToList(&binsy);
328
329 MFillH fillh2a(&hdisp1, "", "FillSize");
330 MFillH fillh2b(&hdisp2, "", "FillEnergy");
331 fillh2a.SetDrawOption("blue profx");
332 fillh2b.SetDrawOption("blue profx");
333 fillh2a.SetNameTab("Size");
334 fillh2b.SetNameTab("Energy");
335
336 tlist.AddToList(&readtst);
337 tlist.AddToList(&cont);
338 tlist.AddToList(&rf);
339 tlist.AddToList(&calcthetasq);
340 tlist.AddToList(&fillh);
341 tlist.AddToList(&fillh2a);
342 tlist.AddToList(&fillh2b);
343 tlist.AddToList(&eval);
344
345 MEvtLoop loop(fTitle);
346 loop.SetLogStream(fLog);
347 loop.SetDisplay(fDisplay);
348 loop.SetParList(&plist);
349 //if (!SetupEnv(loop))
350 // return kFALSE;
351
352 if (!loop.Eventloop())
353 return kFALSE;
354
355 // Print the result
356 *fLog << inf << "Rule: " << rule << endl;
357 hist.GetAlphaFitter().Print("result");
358
359 DisplayResult(hdisp1, hdisp2);
360
361 SetPathOut(out);
362 if (!WriteDisplay(0, "UPDATE"))
363 return kFALSE;
364
365 return kTRUE;
366}
367
Note: See TracBrowser for help on using the repository browser.