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 | // // Two of the displayed histograms show the cut efficiency for
|
---|
46 | // // a given Theta-Cut. The default is 0.215. It can be overwritten
|
---|
47 | // // by
|
---|
48 | // opt.SetThetaCut(0.165);
|
---|
49 | //
|
---|
50 | //
|
---|
51 | // Random Numbers:
|
---|
52 | // ---------------
|
---|
53 | // Use:
|
---|
54 | // if(gRandom)
|
---|
55 | // delete gRandom;
|
---|
56 | // gRandom = new TRandom3();
|
---|
57 | // in advance to change the random number generator.
|
---|
58 | //
|
---|
59 | ////////////////////////////////////////////////////////////////////////////
|
---|
60 | #include "MJTrainDisp.h"
|
---|
61 |
|
---|
62 | #include <TH2.h>
|
---|
63 | #include <TLine.h>
|
---|
64 | #include <TCanvas.h>
|
---|
65 |
|
---|
66 | #include "MHMatrix.h"
|
---|
67 |
|
---|
68 | #include "MLog.h"
|
---|
69 | #include "MLogManip.h"
|
---|
70 |
|
---|
71 | // tools
|
---|
72 | #include "MDataSet.h"
|
---|
73 | #include "MTFillMatrix.h"
|
---|
74 | #include "MChisqEval.h"
|
---|
75 | #include "MStatusDisplay.h"
|
---|
76 |
|
---|
77 | // eventloop
|
---|
78 | #include "MParList.h"
|
---|
79 | #include "MTaskList.h"
|
---|
80 | #include "MEvtLoop.h"
|
---|
81 |
|
---|
82 | // tasks
|
---|
83 | #include "MReadMarsFile.h"
|
---|
84 | #include "MContinue.h"
|
---|
85 | #include "MFillH.h"
|
---|
86 | #include "MRanForestCalc.h"
|
---|
87 | #include "MParameterCalc.h"
|
---|
88 |
|
---|
89 | // container
|
---|
90 | #include "MParameters.h"
|
---|
91 |
|
---|
92 | // histograms
|
---|
93 | #include "MBinning.h"
|
---|
94 | #include "MH3.h"
|
---|
95 | #include "MHn.h"
|
---|
96 | #include "MHThetaSq.h"
|
---|
97 |
|
---|
98 | // filter
|
---|
99 | #include "MFilterList.h"
|
---|
100 |
|
---|
101 | ClassImp(MJTrainDisp);
|
---|
102 |
|
---|
103 | using namespace std;
|
---|
104 |
|
---|
105 | const TString MJTrainDisp::fgTrainParameter = "MHillasSrc.fDist*MGeomCam.fConvMm2Deg";
|
---|
106 |
|
---|
107 | // --------------------------------------------------------------------------
|
---|
108 | //
|
---|
109 | // Display a result histogram either vs. size or energy
|
---|
110 | // FIXME: Could be moved into a new histogram class.
|
---|
111 | //
|
---|
112 | void MJTrainDisp::DisplayHist(TCanvas &c, Int_t i, MH3 &mh3) const
|
---|
113 | {
|
---|
114 | MH::SetPalette("pretty");
|
---|
115 |
|
---|
116 | TH2 &hist = *static_cast<TH2*>(mh3.GetHist().Clone());
|
---|
117 | hist.SetBit(TH1::kNoStats);
|
---|
118 | hist.SetDirectory(0);
|
---|
119 |
|
---|
120 | TLine line;
|
---|
121 | line.SetLineStyle(kDashed);
|
---|
122 | line.SetLineWidth(1);
|
---|
123 |
|
---|
124 | c.cd(i+4);
|
---|
125 | gPad->SetBorderMode(0);
|
---|
126 | gPad->SetFrameBorderMode(0);
|
---|
127 | //gPad->SetFillColor(kWhite);
|
---|
128 | gPad->SetLogx();
|
---|
129 | gPad->SetGridx();
|
---|
130 | gPad->SetGridy();
|
---|
131 | //gPad->SetLeftMargin(0.12);
|
---|
132 | //gPad->SetRightMargin(0.12);
|
---|
133 |
|
---|
134 | const Float_t cutval = hist.GetYaxis()->GetBinLowEdge(4);
|
---|
135 |
|
---|
136 | TH1D heff;
|
---|
137 | heff.SetName(Form("Eff%s", hist.GetName()));
|
---|
138 | heff.SetTitle(Form("Cut efficiency vs. %s for \\vartheta<%.3f", hist.GetName(), TMath::Sqrt(cutval)));
|
---|
139 | heff.SetDirectory(0);
|
---|
140 | heff.SetXTitle(hist.GetXaxis()->GetTitle());
|
---|
141 | heff.SetYTitle("Efficiency");
|
---|
142 |
|
---|
143 | MH::SetBinning(&heff, hist.GetXaxis());
|
---|
144 |
|
---|
145 |
|
---|
146 | for (int x=0; x<=hist.GetNbinsX()+1; x++)
|
---|
147 | {
|
---|
148 | const Double_t n0 = hist.Integral(x, x, -1, -1);
|
---|
149 | if (n0>0)
|
---|
150 | heff.SetBinContent(x, hist.Integral(x, x, -1, 3)/n0);
|
---|
151 | }
|
---|
152 |
|
---|
153 | heff.SetMinimum(0);
|
---|
154 | heff.SetMaximum(1);
|
---|
155 | heff.DrawCopy();
|
---|
156 |
|
---|
157 | line.DrawLine(10, 0.5, 31623, 0.5);
|
---|
158 |
|
---|
159 | c.cd(i+0);
|
---|
160 | gPad->SetBorderMode(0);
|
---|
161 | gPad->SetFrameBorderMode(0);
|
---|
162 | //gPad->SetFillColor(kWhite);
|
---|
163 | gPad->SetLogx();
|
---|
164 | gPad->SetGridx();
|
---|
165 | gPad->SetGridy();
|
---|
166 | //gPad->SetLeftMargin(0.12);
|
---|
167 | //gPad->SetRightMargin(0.12);
|
---|
168 |
|
---|
169 | for (int x=0; x<=hist.GetNbinsX(); x++)
|
---|
170 | {
|
---|
171 | Float_t n = 0;
|
---|
172 | for (int y=1; y<=2; y++)
|
---|
173 | n += hist.GetBinContent(x,y);
|
---|
174 |
|
---|
175 | if (n==0)
|
---|
176 | continue;
|
---|
177 |
|
---|
178 | for (int y=0; y<=hist.GetNbinsY(); y++)
|
---|
179 | hist.SetBinContent(x, y, 200*hist.GetBinContent(x,y)/n);
|
---|
180 | }
|
---|
181 |
|
---|
182 | hist.SetMaximum(100);
|
---|
183 | hist.DrawCopy("colz");
|
---|
184 |
|
---|
185 | line.DrawLine(10, 0.04, 31623, 0.04);
|
---|
186 |
|
---|
187 | c.cd(i+2);
|
---|
188 | gPad->SetBorderMode(0);
|
---|
189 | gPad->SetFrameBorderMode(0);
|
---|
190 | //gPad->SetFillColor(kWhite);
|
---|
191 | gPad->SetLogx();
|
---|
192 | gPad->SetGridx();
|
---|
193 | gPad->SetGridy();
|
---|
194 | //gPad->SetLeftMargin(0.12);
|
---|
195 | //gPad->SetRightMargin(0.12);
|
---|
196 |
|
---|
197 | for (int x=0; x<=hist.GetNbinsX(); x++)
|
---|
198 | {
|
---|
199 | Float_t n = 0;
|
---|
200 | for (int y=0; y<=hist.GetNbinsY(); y++)
|
---|
201 | n += hist.GetBinContent(x,y);
|
---|
202 |
|
---|
203 | if (n==0)
|
---|
204 | continue;
|
---|
205 |
|
---|
206 | for (int y=0; y<=hist.GetNbinsY(); y++)
|
---|
207 | hist.SetBinContent(x, y, 100*hist.GetBinContent(x,y)/n);
|
---|
208 | }
|
---|
209 |
|
---|
210 | hist.SetMaximum(25);
|
---|
211 | hist.DrawCopy("colz");
|
---|
212 |
|
---|
213 | line.DrawLine(10, 0.04, 31623, 0.04);
|
---|
214 | }
|
---|
215 |
|
---|
216 | // --------------------------------------------------------------------------
|
---|
217 | //
|
---|
218 | // Display the result histograms in a new tab.
|
---|
219 | //
|
---|
220 | void MJTrainDisp::DisplayResult(MH3 &hsize, MH3 &henergy)
|
---|
221 | {
|
---|
222 | TCanvas &c = fDisplay->AddTab("Disp");
|
---|
223 | c.Divide(2,3);
|
---|
224 |
|
---|
225 | DisplayHist(c, 1, hsize);
|
---|
226 | DisplayHist(c, 2, henergy);
|
---|
227 | }
|
---|
228 |
|
---|
229 | // --------------------------------------------------------------------------
|
---|
230 | //
|
---|
231 | // Run Disp optimization
|
---|
232 | //
|
---|
233 | Bool_t MJTrainDisp::Train(const char *out, const MDataSet &set, Int_t num)
|
---|
234 | {
|
---|
235 | SetTitle(Form("Train%s: %s", fNameOutput.Data(), out));
|
---|
236 |
|
---|
237 | if (fDisplay)
|
---|
238 | fDisplay->SetTitle(fTitle);
|
---|
239 |
|
---|
240 | if (!set.IsValid())
|
---|
241 | {
|
---|
242 | *fLog << err << "ERROR - DataSet invalid!" << endl;
|
---|
243 | return kFALSE;
|
---|
244 | }
|
---|
245 |
|
---|
246 | if (!HasWritePermission(out))
|
---|
247 | return kFALSE;
|
---|
248 |
|
---|
249 | *fLog << inf;
|
---|
250 | fLog->Separator(GetDescriptor());
|
---|
251 |
|
---|
252 | // --------------------- Setup files --------------------
|
---|
253 | MReadMarsFile readtrn("Events");
|
---|
254 | MReadMarsFile readtst("Events");
|
---|
255 | readtrn.DisableAutoScheme();
|
---|
256 | readtst.DisableAutoScheme();
|
---|
257 |
|
---|
258 | if (!set.AddFilesOn(readtrn))
|
---|
259 | {
|
---|
260 | *fLog << err << "ERROR - Adding SequencesOn." << endl;
|
---|
261 | return kFALSE;
|
---|
262 | }
|
---|
263 | if (!set.AddFilesOff(readtst))
|
---|
264 | {
|
---|
265 | *fLog << err << "ERROR - Adding SequencesOff." << endl;
|
---|
266 | return kFALSE;
|
---|
267 | }
|
---|
268 |
|
---|
269 | // ----------------------- Setup Matrix ------------------
|
---|
270 | MHMatrix train("Train");
|
---|
271 | train.AddColumns(fRules);
|
---|
272 | if (fEnableWeights)
|
---|
273 | train.AddColumn("MWeight.fVal");
|
---|
274 | train.AddColumn(fTrainParameter);
|
---|
275 |
|
---|
276 | // ----------------------- Fill Matrix RF ----------------------
|
---|
277 | MTFillMatrix fill(fTitle);
|
---|
278 | fill.SetDisplay(fDisplay);
|
---|
279 | fill.SetLogStream(fLog);
|
---|
280 | fill.SetDestMatrix1(&train, num);
|
---|
281 | fill.SetReader(&readtrn);
|
---|
282 | fill.AddPreCuts(fPreCuts);
|
---|
283 | fill.AddPreCuts(fTrainCuts);
|
---|
284 | fill.AddPreTasks(fPreTasks);
|
---|
285 | fill.AddPostTasks(fPostTasks);
|
---|
286 | if (!fill.Process())
|
---|
287 | return kFALSE;
|
---|
288 |
|
---|
289 | // ------------------------ Train RF --------------------------
|
---|
290 | MRanForestCalc rf("Train", fTitle);
|
---|
291 | rf.SetNumTrees(fNumTrees);
|
---|
292 | rf.SetNdSize(fNdSize);
|
---|
293 | rf.SetNumTry(fNumTry);
|
---|
294 | rf.SetNumObsoleteVariables(1);
|
---|
295 | rf.SetLastDataColumnHasWeights(fEnableWeights);
|
---|
296 | rf.SetDisplay(fDisplay);
|
---|
297 | rf.SetLogStream(fLog);
|
---|
298 | rf.SetFileName(out);
|
---|
299 | rf.SetDebug(fDebug>1);
|
---|
300 | rf.SetNameOutput(fNameOutput);
|
---|
301 | rf.SetFunction(fResultFunction);
|
---|
302 |
|
---|
303 | /*
|
---|
304 | MBinning b(32, 10, 100000, "BinningEnergyEst", "log");
|
---|
305 |
|
---|
306 | if (!rf.TrainMultiRF(train, b.GetEdgesD())) // classification with one tree per bin
|
---|
307 | return;
|
---|
308 |
|
---|
309 | if (!rf.TrainSingleRF(train, b.GetEdgesD())) // classification into different bins
|
---|
310 | return;
|
---|
311 | */
|
---|
312 | if (!rf.TrainRegression(train)) // regression (best choice)
|
---|
313 | return kFALSE;
|
---|
314 |
|
---|
315 | // --------------------- Display result ----------------------
|
---|
316 |
|
---|
317 | gLog.Separator("Test");
|
---|
318 |
|
---|
319 | MH::SetPalette("pretty");
|
---|
320 |
|
---|
321 | MParList plist;
|
---|
322 | MTaskList tlist;
|
---|
323 | plist.AddToList(this);
|
---|
324 | plist.AddToList(&tlist);
|
---|
325 | //plist.AddToList(&b);
|
---|
326 |
|
---|
327 | MParameterD par("ThetaSquaredCut");
|
---|
328 | par.SetVal(fThetaCut*fThetaCut);
|
---|
329 | plist.AddToList(&par);
|
---|
330 |
|
---|
331 | MAlphaFitter fit;
|
---|
332 | fit.SetPolynomOrder(0);
|
---|
333 | fit.SetSignalFitMax(0.8);
|
---|
334 | fit.EnableBackgroundFit(kFALSE);
|
---|
335 | fit.SetSignalFunction(MAlphaFitter::kThetaSq);
|
---|
336 | fit.SetMinimizationStrategy(MAlphaFitter::kGaussSigma);
|
---|
337 | plist.AddToList(&fit);
|
---|
338 |
|
---|
339 | MFilterList list;
|
---|
340 | if (!list.AddToList(fPreCuts))
|
---|
341 | *fLog << err << "ERROR - Calling MFilterList::AddToList for fPreCuts failed!" << endl;
|
---|
342 | if (!list.AddToList(fTestCuts))
|
---|
343 | *fLog << err << "ERROR - Calling MFilterList::AddToList for fTestCuts failed!" << endl;
|
---|
344 |
|
---|
345 | MContinue cont(&list);
|
---|
346 | cont.SetInverted();
|
---|
347 |
|
---|
348 | const char *rule = "(MHillasSrc.fDist*MGeomCam.fConvMm2Deg)^2 + (Disp.fVal)^2 - (2*MHillasSrc.fDist*MGeomCam.fConvMm2Deg*Disp.fVal*cos(MHillasSrc.fAlpha*kDeg2Rad))";
|
---|
349 |
|
---|
350 | MParameterCalc calcthetasq(rule, "MThetaSqCalc");
|
---|
351 | calcthetasq.SetNameParameter("ThetaSquared");
|
---|
352 |
|
---|
353 | MChisqEval eval;
|
---|
354 | eval.SetY1("sqrt(ThetaSquared.fVal)");
|
---|
355 |
|
---|
356 | // ----------- Setup binnings ----------------
|
---|
357 | MBinning binsS(50, 10, 100000, "BinningSize", "log");
|
---|
358 | MBinning binsE(70, 10, 31623, "BinningEnergy", "log");
|
---|
359 | MBinning binsG(50, -10, 10, "BinningSlope", "lin");
|
---|
360 | MBinning binsX(50, -1, 1, "BinningResidualDist", "lin");
|
---|
361 | MBinning binsL(50, 0, 0.3, "BinningLeakage", "lin");
|
---|
362 | MBinning binsT(51, -0.005, 0.505, "BinningTheta", "asin");
|
---|
363 | MBinning binsC(50, 1e-2, 1, "BinningConc", "log");
|
---|
364 | MBinning binsW(50, 0, 0.5, "BinningLength", "lin");
|
---|
365 | MBinning binsM(50, 0, 0.3, "BinningWidth", "lin");
|
---|
366 | MBinning binsV(75, 0, par.GetVal()*25, "BinningThetaSq", "lin");
|
---|
367 |
|
---|
368 | plist.AddToList(&binsG);
|
---|
369 | plist.AddToList(&binsS);
|
---|
370 | plist.AddToList(&binsX);
|
---|
371 | plist.AddToList(&binsE);
|
---|
372 | plist.AddToList(&binsL);
|
---|
373 | plist.AddToList(&binsT);
|
---|
374 | plist.AddToList(&binsC);
|
---|
375 | plist.AddToList(&binsV);
|
---|
376 | plist.AddToList(&binsW);
|
---|
377 | plist.AddToList(&binsM);
|
---|
378 |
|
---|
379 | // ----------- Setup some histograms ----------------
|
---|
380 |
|
---|
381 | MHThetaSq hist;
|
---|
382 | hist.SkipHistTime();
|
---|
383 | hist.SkipHistTheta();
|
---|
384 | hist.SkipHistEnergy();
|
---|
385 |
|
---|
386 | // To speed it up we could precalculate it.
|
---|
387 | const char *res = "Disp.fVal-MHillasSrc.fDist*3.37e-3";
|
---|
388 |
|
---|
389 | MHn hres1("Disp1", "Xi Residual (Dist/Disp)");
|
---|
390 | hres1.AddHist("MHillas.fSize", res);
|
---|
391 | hres1.InitName("ResSize;Size;ResidualDist");
|
---|
392 | hres1.InitTitle(";S [phe];Disp-Dist [\\circ];");
|
---|
393 | hres1.SetDrawOption("colz profx");
|
---|
394 | hres1.AddHist("MHillasExt.fSlopeLong*sign(MHillasSrc.fCosDeltaAlpha)/3.37e-3", res);
|
---|
395 | hres1.InitName("ResSlope;Slope;ResidualDist");
|
---|
396 | hres1.InitTitle(";Slope;Disp-Dist [\\circ];");
|
---|
397 | hres1.SetDrawOption("colz profx");
|
---|
398 | hres1.AddHist("MNewImagePar.fLeakage1", res);
|
---|
399 | hres1.InitName("ResLeak;Leakage;ResidualDist");
|
---|
400 | hres1.InitTitle(";Leak;Disp-Dist [\\circ];");
|
---|
401 | hres1.SetDrawOption("colz profx");
|
---|
402 | hres1.AddHist("MPointingPos.fZd", res);
|
---|
403 | hres1.InitName("ResTheta;Theta;ResidualDist");
|
---|
404 | hres1.InitTitle(";Zd [\\circ];Disp-Dist [\\circ];");
|
---|
405 | hres1.SetDrawOption("colz profx");
|
---|
406 |
|
---|
407 | MHn hres2("Disp2", "Dist Residual (Disp-Dist)");
|
---|
408 | hres2.AddHist("MHillas.fLength*3.37e-3", res);
|
---|
409 | hres2.InitName("ResLength;Length;ResidualDist");
|
---|
410 | hres2.InitTitle(";L [\\circ];Disp-Dist [\\circ];");
|
---|
411 | hres2.SetDrawOption("colz profx");
|
---|
412 | hres2.AddHist("MNewImagePar.fConc1", res);
|
---|
413 | hres2.InitName("ResConc1;Conc;ResidualDist");
|
---|
414 | hres2.InitTitle(";C;Disp-Dist [\\circ];");
|
---|
415 | hres2.SetDrawOption("colz profx");
|
---|
416 | hres2.AddHist("MHillas.fWidth*3.37e-3", res);
|
---|
417 | hres2.InitName("ResWidth;Width;ResidualDist");
|
---|
418 | hres2.InitTitle(";W [\\circ];Disp-Dist [\\circ];");
|
---|
419 | hres2.SetDrawOption("colz profx");
|
---|
420 | hres2.AddHist("MMcEvt.fEnergy", res);
|
---|
421 | hres2.InitName("ResEmc;Energy;ResidualDist");
|
---|
422 | hres2.InitTitle(";E_{mc} [GeV];Disp-Dist [\\circ];");
|
---|
423 | hres2.SetDrawOption("colz profx");
|
---|
424 |
|
---|
425 | MH3 hdisp1("MHillas.fSize", "ThetaSquared.fVal");
|
---|
426 | MH3 hdisp2("MMcEvt.fEnergy", "ThetaSquared.fVal");
|
---|
427 | hdisp1.SetName("Size;Size;ThetaSq");
|
---|
428 | hdisp2.SetName("Energy;Energy;ThetaSq");
|
---|
429 | hdisp1.SetTitle("\\vartheta distribution vs. Size:Size [phe]:\\vartheta^2 [\\circ]");
|
---|
430 | hdisp2.SetTitle("\\vartheta distribution vs. Energy:Energy [GeV]:\\vartheta^2 [\\circ]");
|
---|
431 |
|
---|
432 | // -------------- Setup fill tasks ----------------
|
---|
433 |
|
---|
434 | MFillH fillh(&hist, "", "FillThetaSq");
|
---|
435 | MFillH fillh2a(&hres1, "", "FillResiduals1");
|
---|
436 | MFillH fillh2b(&hres2, "", "FillResiduals2");
|
---|
437 | MFillH fillh2c(&hdisp1, "", "FillSize");
|
---|
438 | MFillH fillh2d(&hdisp2, "", "FillEnergy");
|
---|
439 | fillh2c.SetBit(MFillH::kDoNotDisplay);
|
---|
440 | fillh2d.SetBit(MFillH::kDoNotDisplay);
|
---|
441 |
|
---|
442 | // --------------- Setup weighting -------------------
|
---|
443 |
|
---|
444 | if (fEnableWeights)
|
---|
445 | {
|
---|
446 | fillh.SetWeight();
|
---|
447 | fillh2a.SetWeight();
|
---|
448 | fillh2b.SetWeight();
|
---|
449 | fillh2c.SetWeight();
|
---|
450 | fillh2d.SetWeight();
|
---|
451 | eval.SetNameWeight();
|
---|
452 | }
|
---|
453 |
|
---|
454 | // --------------- Setup tasklist -------------------
|
---|
455 |
|
---|
456 | tlist.AddToList(&readtst);
|
---|
457 | tlist.AddToList(fPreTasks);
|
---|
458 | tlist.AddToList(&cont);
|
---|
459 | tlist.AddToList(&rf);
|
---|
460 | tlist.AddToList(&calcthetasq);
|
---|
461 | tlist.AddToList(fPostTasks);
|
---|
462 | tlist.AddToList(&fillh);
|
---|
463 | tlist.AddToList(&fillh2a);
|
---|
464 | tlist.AddToList(&fillh2b);
|
---|
465 | tlist.AddToList(&fillh2c);
|
---|
466 | tlist.AddToList(&fillh2d);
|
---|
467 | tlist.AddToList(fTestTasks);
|
---|
468 | tlist.AddToList(&eval);
|
---|
469 |
|
---|
470 | // ------------- Setup/run eventloop -----------------
|
---|
471 |
|
---|
472 | MEvtLoop loop(fTitle);
|
---|
473 | loop.SetLogStream(fLog);
|
---|
474 | loop.SetDisplay(fDisplay);
|
---|
475 | loop.SetParList(&plist);
|
---|
476 | //if (!SetupEnv(loop))
|
---|
477 | // return kFALSE;
|
---|
478 |
|
---|
479 | if (!loop.Eventloop())
|
---|
480 | return kFALSE;
|
---|
481 |
|
---|
482 | // ---------------- Prepare result -------------------
|
---|
483 |
|
---|
484 | // Print the result
|
---|
485 | *fLog << inf;
|
---|
486 | *fLog << "Rule: " << rule << endl;
|
---|
487 | *fLog << "Disp: " << fTrainParameter << endl;
|
---|
488 | hist.GetAlphaFitter().Print("result");
|
---|
489 |
|
---|
490 | // The user has closed the display
|
---|
491 | if (!fDisplay)
|
---|
492 | return kTRUE;
|
---|
493 |
|
---|
494 | DisplayResult(hdisp1, hdisp2);
|
---|
495 |
|
---|
496 | TObjArray arr;
|
---|
497 | arr.Add(const_cast<MDataSet*>(&set));
|
---|
498 | if (fDisplay)
|
---|
499 | arr.Add(fDisplay);
|
---|
500 |
|
---|
501 | SetPathOut(out);
|
---|
502 | return WriteContainer(arr, 0, "UPDATE");
|
---|
503 | }
|
---|
504 |
|
---|
505 | /*
|
---|
506 | #include "MParameterCalc.h"
|
---|
507 | #include "MHillasCalc.h"
|
---|
508 | #include "../mpointing/MSrcPosRndm.h"
|
---|
509 |
|
---|
510 | Bool_t MJTrainDisp::TrainGhostbuster(const char *out, const MDataSet &set, Int_t num)
|
---|
511 | {
|
---|
512 | SetTitle(Form("TrainGhostbuster: %s", out));
|
---|
513 |
|
---|
514 | if (fDisplay)
|
---|
515 | fDisplay->SetTitle(fTitle);
|
---|
516 |
|
---|
517 | if (!set.IsValid())
|
---|
518 | {
|
---|
519 | *fLog << err << "ERROR - DataSet invalid!" << endl;
|
---|
520 | return kFALSE;
|
---|
521 | }
|
---|
522 |
|
---|
523 | if (!HasWritePermission(out))
|
---|
524 | return kFALSE;
|
---|
525 |
|
---|
526 | *fLog << inf;
|
---|
527 | fLog->Separator(GetDescriptor());
|
---|
528 |
|
---|
529 | // --------------------- Setup files --------------------
|
---|
530 | MReadMarsFile readtrn("Events");
|
---|
531 | MReadMarsFile readtst("Events");
|
---|
532 | readtrn.DisableAutoScheme();
|
---|
533 | readtst.DisableAutoScheme();
|
---|
534 |
|
---|
535 | if (!set.AddFilesOn(readtrn))
|
---|
536 | return kFALSE;
|
---|
537 | if (!set.AddFilesOff(readtst))
|
---|
538 | return kFALSE;
|
---|
539 |
|
---|
540 | // ----------------------- Setup Matrix ------------------
|
---|
541 | MHMatrix train("Train");
|
---|
542 | train.AddColumns(fRules);
|
---|
543 | if (fEnableWeights)
|
---|
544 | train.AddColumn("MWeight.fVal");
|
---|
545 | train.AddColumn("sign(MHillasSrc.fCosDeltaAlpha)==sign(SignStore.fVal)");
|
---|
546 |
|
---|
547 | MParameterCalc calc("MHillasSrc.fCosDeltaAlpha", "SignStore");
|
---|
548 | calc.SetNameParameter("SignStore");
|
---|
549 |
|
---|
550 | MSrcPosRndm rndm;
|
---|
551 | rndm.SetRule(fTrainParameter);
|
---|
552 | //rndm.SetDistOfSource(120*3.37e-3);
|
---|
553 |
|
---|
554 | MHillasCalc hcalc;
|
---|
555 | hcalc.SetFlags(MHillasCalc::kCalcHillasSrc);
|
---|
556 |
|
---|
557 | // ----------------------- Fill Matrix RF ----------------------
|
---|
558 | MTFillMatrix fill(fTitle);
|
---|
559 | fill.SetDisplay(fDisplay);
|
---|
560 | fill.SetLogStream(fLog);
|
---|
561 | fill.SetDestMatrix1(&train, num);
|
---|
562 | fill.SetReader(&readtrn);
|
---|
563 | fill.AddPreCuts(fPreCuts);
|
---|
564 | fill.AddPreCuts(fTrainCuts);
|
---|
565 | fill.AddPreTasks(fPreTasks);
|
---|
566 | fill.AddPostTasks(fPostTasks);
|
---|
567 | fill.AddPostTask(&calc);
|
---|
568 | fill.AddPostTask(&rndm);
|
---|
569 | fill.AddPostTask(&hcalc);
|
---|
570 | if (!fill.Process())
|
---|
571 | return kFALSE;
|
---|
572 |
|
---|
573 | // ------------------------ Train RF --------------------------
|
---|
574 | MRanForestCalc rf("TrainGhostbuster", fTitle);
|
---|
575 | rf.SetNumTrees(fNumTrees);
|
---|
576 | rf.SetNdSize(fNdSize);
|
---|
577 | rf.SetNumTry(fNumTry);
|
---|
578 | rf.SetNumObsoleteVariables(1);
|
---|
579 | rf.SetLastDataColumnHasWeights(fEnableWeights);
|
---|
580 | rf.SetDisplay(fDisplay);
|
---|
581 | rf.SetLogStream(fLog);
|
---|
582 | rf.SetFileName(out);
|
---|
583 | rf.SetDebug(fDebug>1);
|
---|
584 | rf.SetNameOutput("Sign");
|
---|
585 |
|
---|
586 | if (!rf.TrainRegression(train)) // regression (best choice)
|
---|
587 | return kFALSE;
|
---|
588 |
|
---|
589 | // --------------------- Display result ----------------------
|
---|
590 |
|
---|
591 | gLog.Separator("Test");
|
---|
592 |
|
---|
593 | MH::SetPalette("pretty");
|
---|
594 |
|
---|
595 | MParList plist;
|
---|
596 | MTaskList tlist;
|
---|
597 | plist.AddToList(this);
|
---|
598 | plist.AddToList(&tlist);
|
---|
599 | //plist.AddToList(&b);
|
---|
600 |
|
---|
601 | MFilterList list;
|
---|
602 | if (!list.AddToList(fPreCuts))
|
---|
603 | *fLog << err << "ERROR - Calling MFilterList::AddToList for fPreCuts failed!" << endl;
|
---|
604 | if (!list.AddToList(fTestCuts))
|
---|
605 | *fLog << err << "ERROR - Calling MFilterList::AddToList for fTestCuts failed!" << endl;
|
---|
606 |
|
---|
607 | MContinue cont(&list);
|
---|
608 | cont.SetInverted();
|
---|
609 |
|
---|
610 | const char *rule = "abs(Sign.fVal-(sign(MHillasSrc.fCosDeltaAlpha)==sign(SignStore.fVal)))";
|
---|
611 |
|
---|
612 | //MChisqEval eval;
|
---|
613 | //eval.SetY1("sqrt(ThetaSquared.fVal)");
|
---|
614 |
|
---|
615 | MH3 hsign1("MHillas.fSize", rule);
|
---|
616 | MH3 hsign2("MMcEvt.fEnergy", rule);
|
---|
617 | hsign1.SetTitle("Was ist das? vs. Size:Size [phe]:XXX");
|
---|
618 | hsign2.SetTitle("Was ist das? vs. Energy:Enerhy [GeV]:XXXX");
|
---|
619 |
|
---|
620 | MBinning binsx( 70, 10, 31623, "BinningMH3X", "log");
|
---|
621 | MBinning binsy( 51, 0, 1, "BinningMH3Y", "lin");
|
---|
622 |
|
---|
623 | plist.AddToList(&binsx);
|
---|
624 | plist.AddToList(&binsy);
|
---|
625 |
|
---|
626 | MFillH fillh2a(&hsign1, "", "FillSize");
|
---|
627 | MFillH fillh2b(&hsign2, "", "FillEnergy");
|
---|
628 | fillh2a.SetDrawOption("profx colz");
|
---|
629 | fillh2b.SetDrawOption("profx colz");
|
---|
630 | fillh2a.SetNameTab("Size");
|
---|
631 | fillh2b.SetNameTab("Energy");
|
---|
632 |
|
---|
633 | tlist.AddToList(&readtst);
|
---|
634 | tlist.AddToList(&cont);
|
---|
635 | tlist.AddToList(&calc);
|
---|
636 | tlist.AddToList(&rndm);
|
---|
637 | tlist.AddToList(&hcalc);
|
---|
638 | tlist.AddToList(&rf);
|
---|
639 | tlist.AddToList(&fillh2a);
|
---|
640 | tlist.AddToList(&fillh2b);
|
---|
641 | //tlist.AddToList(&eval);
|
---|
642 |
|
---|
643 | MEvtLoop loop(fTitle);
|
---|
644 | loop.SetLogStream(fLog);
|
---|
645 | loop.SetDisplay(fDisplay);
|
---|
646 | loop.SetParList(&plist);
|
---|
647 | //if (!SetupEnv(loop))
|
---|
648 | // return kFALSE;
|
---|
649 |
|
---|
650 | if (!loop.Eventloop())
|
---|
651 | return kFALSE;
|
---|
652 |
|
---|
653 | // Print the result
|
---|
654 | *fLog << inf << "Rule: " << rule << endl;
|
---|
655 |
|
---|
656 | //DisplayResult(hdisp1, hdisp2);
|
---|
657 |
|
---|
658 | SetPathOut(out);
|
---|
659 | if (!WriteDisplay(0, "UPDATE"))
|
---|
660 | return kFALSE;
|
---|
661 |
|
---|
662 | return kTRUE;
|
---|
663 | }
|
---|
664 | */
|
---|