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

Last change on this file since 8244 was 8244, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 7.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// 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 "MHMatrix.h"
58
59#include "MLog.h"
60#include "MLogManip.h"
61
62// tools
63#include "MDataSet.h"
64#include "MTFillMatrix.h"
65#include "MChisqEval.h"
66
67// eventloop
68#include "MParList.h"
69#include "MTaskList.h"
70#include "MEvtLoop.h"
71
72// tasks
73#include "MReadMarsFile.h"
74#include "MContinue.h"
75#include "MFillH.h"
76#include "MRanForestCalc.h"
77#include "MParameterCalc.h"
78
79// container
80#include "MParameters.h"
81
82// histograms
83#include "MBinning.h"
84#include "MH3.h"
85#include "MHThetaSq.h"
86
87// filter
88#include "MFilterList.h"
89
90ClassImp(MJTrainDisp);
91
92using namespace std;
93
94Bool_t MJTrainDisp::Train(const char *out, const MDataSet &set, Int_t num)
95{
96 if (!set.IsValid())
97 {
98 *fLog << err << "ERROR - DataSet invalid!" << endl;
99 return kFALSE;
100 }
101
102 *fLog << inf;
103 fLog->Separator(GetDescriptor());
104
105 // --------------------- Setup files --------------------
106 MReadMarsFile readtrn("Events");
107 MReadMarsFile readtst("Events");
108 readtrn.DisableAutoScheme();
109 readtst.DisableAutoScheme();
110
111 if (!set.AddFilesOn(readtrn))
112 return kFALSE;
113 if (!set.AddFilesOff(readtst))
114 return kFALSE;
115
116 // ----------------------- Setup Matrix ------------------
117 MHMatrix train("Train");
118 train.AddColumns(fRules);
119 if (fEnableWeights)
120 train.AddColumn("MWeight.fVal");
121 train.AddColumn("MHillasSrc.fDist*MGeomCam.fConvMm2Deg");
122 //train.AddColumn("TMath::Hypot(MHillasSrc.fDCA, MHillasSrc.fDist)*MGeomCam.fConvMm2Deg");
123
124 // ----------------------- Fill Matrix RF ----------------------
125 MTFillMatrix fill;
126 fill.SetDisplay(fDisplay);
127 fill.SetLogStream(fLog);
128 fill.SetDestMatrix1(&train, num);
129 fill.SetReader(&readtrn);
130 fill.AddPreCuts(fPreCuts);
131 fill.AddPreCuts(fTrainCuts);
132 fill.AddPreTasks(fPreTasks);
133 fill.AddPostTasks(fPostTasks);
134 if (!fill.Process())
135 return kFALSE;
136
137 // ------------------------ Train RF --------------------------
138 MRanForestCalc rf;
139 rf.SetNumTrees(fNumTrees);
140 rf.SetNdSize(fNdSize);
141 rf.SetNumTry(fNumTry);
142 rf.SetNumObsoleteVariables(1);
143 rf.SetLastDataColumnHasWeights(fEnableWeights);
144 rf.SetDisplay(fDisplay);
145 rf.SetLogStream(fLog);
146 rf.SetFileName(out);
147 rf.SetDebug(fDebug);
148 rf.SetNameOutput("Disp");
149
150 /*
151 MBinning b(32, 10, 100000, "BinningEnergyEst", "log");
152
153 if (!rf.TrainMultiRF(train, b.GetEdgesD())) // classification with one tree per bin
154 return;
155
156 if (!rf.TrainSingleRF(train, b.GetEdgesD())) // classification into different bins
157 return;
158 */
159 if (!rf.TrainRegression(train)) // regression (best choice)
160 return kFALSE;
161
162 // --------------------- Display result ----------------------
163
164 gLog.Separator("Test");
165
166 MParList plist;
167 MTaskList tlist;
168 plist.AddToList(this);
169 plist.AddToList(&tlist);
170 //plist.AddToList(&b);
171
172 MParameterD par("ThetaSquaredCut");
173 par.SetVal(0.2);
174 plist.AddToList(&par);
175
176 MAlphaFitter fit;
177 fit.SetPolynomOrder(0);
178 fit.SetSignalFitMax(0.8);
179 fit.EnableBackgroundFit(kFALSE);
180 fit.SetSignalFunction(MAlphaFitter::kThetaSq);
181 fit.SetMinimizationStrategy(MAlphaFitter::kGaussSigma);
182 plist.AddToList(&fit);
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 MHThetaSq hist;
194 hist.SkipHistTime();
195 hist.SkipHistTheta();
196 hist.SkipHistEnergy();
197
198 MFillH fillh(&hist, "", "FillThetaSq");
199
200 // 0 = disp^2 - 2*disp*dist*cos(alpha) + dist^2
201
202 // cos^2 -1 = - sin^2
203
204 // disp = +dist* (cos(alpha) +/- sqrt(cos^2(alpha) - 1) )
205
206 const char *rule = "(MHillasSrc.fDist*MGeomCam.fConvMm2Deg)^2 + (Disp.fVal)^2 - (2*MHillasSrc.fDist*MGeomCam.fConvMm2Deg*Disp.fVal*cos(MHillasSrc.fAlpha*kDeg2Rad))";
207
208 MParameterCalc calcthetasq(rule, "MThetaSqCalc");
209 calcthetasq.SetNameParameter("ThetaSquared");
210
211 MChisqEval eval;
212 eval.SetY1("sqrt(ThetaSquared.fVal)");
213
214 MH3 hdisp1("MHillas.fSize", "sqrt(ThetaSquared.fVal)");
215 MH3 hdisp2("MMcEvt.fEnergy", "sqrt(ThetaSquared.fVal)");
216 hdisp1.SetTitle("\\vartheta distribution vs. Size:Size [phe]:\\vartheta [\\circ]");
217 hdisp2.SetTitle("\\vartheta distribution vs. Energy:Enerhy [GeV]:\\vartheta [\\circ]");
218
219 MBinning binsx(50, 10, 100000, "BinningMH3X", "log");
220 MBinning binsy(50, 0, 1, "BinningMH3Y", "lin");
221
222 plist.AddToList(&binsx);
223 plist.AddToList(&binsy);
224
225 MFillH fillh2a(&hdisp1, "", "FillSize");
226 MFillH fillh2b(&hdisp2, "", "FillEnergy");
227 fillh2a.SetDrawOption("blue profx");
228 fillh2b.SetDrawOption("blue profx");
229 fillh2a.SetNameTab("Size");
230 fillh2b.SetNameTab("Energy");
231
232 tlist.AddToList(&readtst);
233 tlist.AddToList(&cont);
234 tlist.AddToList(&rf);
235 tlist.AddToList(&calcthetasq);
236 tlist.AddToList(&fillh);
237 tlist.AddToList(&fillh2a);
238 tlist.AddToList(&fillh2b);
239 tlist.AddToList(&eval);
240
241 MEvtLoop loop;
242 loop.SetLogStream(fLog);
243 loop.SetDisplay(fDisplay);
244 loop.SetParList(&plist);
245
246 if (!loop.Eventloop())
247 return kFALSE;
248
249 // Print the result
250 *fLog << inf << "Rule: " << rule << endl;
251 hist.GetAlphaFitter().Print("result");
252
253 if (!WriteDisplay(out))
254 return kFALSE;
255
256 return kTRUE;
257}
258
Note: See TracBrowser for help on using the repository browser.