source: tags/Mars-V0.10.2/mjtrain/MJTrainDisp.cc

Last change on this file was 7749, checked in by tbretz, 19 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 set.AddFilesOn(readtrn);
112 set.AddFilesOff(readtst);
113
114 // ----------------------- Setup Matrix ------------------
115 MHMatrix train("Train");
116 train.AddColumns(fRules);
117 if (fEnableWeights)
118 train.AddColumn("MWeight.fVal");
119 train.AddColumn("MHillasSrc.fDist*MGeomCam.fConvMm2Deg");
120 //train.AddColumn("TMath::Hypot(MHillasSrc.fDCA, MHillasSrc.fDist)*MGeomCam.fConvMm2Deg");
121
122 // ----------------------- Fill Matrix RF ----------------------
123 MTFillMatrix fill;
124 fill.SetDisplay(fDisplay);
125 fill.SetLogStream(fLog);
126 fill.SetDestMatrix1(&train, num);
127 fill.SetReader(&readtrn);
128 fill.AddPreCuts(fPreCuts);
129 fill.AddPreCuts(fTrainCuts);
130 fill.AddPreTasks(fPreTasks);
131 fill.AddPostTasks(fPostTasks);
132 if (!fill.Process())
133 return kFALSE;
134
135 // ------------------------ Train RF --------------------------
136 MRanForestCalc rf;
137 rf.SetNumTrees(fNumTrees);
138 rf.SetNdSize(fNdSize);
139 rf.SetNumTry(fNumTry);
140 rf.SetNumObsoleteVariables(1);
141 rf.SetLastDataColumnHasWeights(fEnableWeights);
142 rf.SetDisplay(fDisplay);
143 rf.SetLogStream(fLog);
144 rf.SetFileName(out);
145 rf.SetDebug(fDebug);
146 rf.SetNameOutput("Disp");
147
148 /*
149 MBinning b(32, 10, 100000, "BinningEnergyEst", "log");
150
151 if (!rf.TrainMultiRF(train, b.GetEdgesD())) // classification with one tree per bin
152 return;
153
154 if (!rf.TrainSingleRF(train, b.GetEdgesD())) // classification into different bins
155 return;
156 */
157 if (!rf.TrainRegression(train)) // regression (best choice)
158 return kFALSE;
159
160 // --------------------- Display result ----------------------
161
162 gLog.Separator("Test");
163
164 MParList plist;
165 MTaskList tlist;
166 plist.AddToList(this);
167 plist.AddToList(&tlist);
168 //plist.AddToList(&b);
169
170 MParameterD par("ThetaSquaredCut");
171 par.SetVal(0.2);
172 plist.AddToList(&par);
173
174 MAlphaFitter fit;
175 fit.SetPolynomOrder(0);
176 fit.SetSignalFitMax(0.8);
177 fit.EnableBackgroundFit(kFALSE);
178 fit.SetSignalFunction(MAlphaFitter::kThetaSq);
179 fit.SetMinimizationStrategy(MAlphaFitter::kGaussSigma);
180 plist.AddToList(&fit);
181
182 MFilterList list;
183 if (!list.AddToList(fPreCuts))
184 *fLog << err << "ERROR - Calling MFilterList::AddToList for fPreCuts failed!" << endl;
185 if (!list.AddToList(fTestCuts))
186 *fLog << err << "ERROR - Calling MFilterList::AddToList for fTestCuts failed!" << endl;
187
188 MContinue cont(&list);
189 cont.SetInverted();
190
191 MHThetaSq hist;
192 hist.SkipHistTime();
193 hist.SkipHistTheta();
194 hist.SkipHistEnergy();
195
196 MFillH fillh(&hist, "", "FillThetaSq");
197
198 // 0 = disp^2 - 2*disp*dist*cos(alpha) + dist^2
199
200 // cos^2 -1 = - sin^2
201
202 // disp = +dist* (cos(alpha) +/- sqrt(cos^2(alpha) - 1) )
203
204 const char *rule = "(MHillasSrc.fDist*MGeomCam.fConvMm2Deg)^2 + (Disp.fVal)^2 - (2*MHillasSrc.fDist*MGeomCam.fConvMm2Deg*Disp.fVal*cos(MHillasSrc.fAlpha*kDeg2Rad))";
205
206 MParameterCalc calcthetasq(rule, "MThetaSqCalc");
207 calcthetasq.SetNameParameter("ThetaSquared");
208
209 MChisqEval eval;
210 eval.SetY1("sqrt(ThetaSquared.fVal)");
211
212 MH3 hdisp1("MHillas.fSize", "sqrt(ThetaSquared.fVal)");
213 MH3 hdisp2("MMcEvt.fEnergy", "sqrt(ThetaSquared.fVal)");
214 hdisp1.SetTitle("\\vartheta distribution vs. Size:Size [phe]:\\vartheta [\\circ]");
215 hdisp2.SetTitle("\\vartheta distribution vs. Energy:Enerhy [GeV]:\\vartheta [\\circ]");
216
217 MBinning binsx(50, 10, 100000, "BinningMH3X", "log");
218 MBinning binsy(50, 0, 1, "BinningMH3Y", "lin");
219
220 plist.AddToList(&binsx);
221 plist.AddToList(&binsy);
222
223 MFillH fillh2a(&hdisp1, "", "FillSize");
224 MFillH fillh2b(&hdisp2, "", "FillEnergy");
225 fillh2a.SetDrawOption("blue profx");
226 fillh2b.SetDrawOption("blue profx");
227 fillh2a.SetNameTab("Size");
228 fillh2b.SetNameTab("Energy");
229
230 tlist.AddToList(&readtst);
231 tlist.AddToList(&cont);
232 tlist.AddToList(&rf);
233 tlist.AddToList(&calcthetasq);
234 tlist.AddToList(&fillh);
235 tlist.AddToList(&fillh2a);
236 tlist.AddToList(&fillh2b);
237 tlist.AddToList(&eval);
238
239 MEvtLoop loop;
240 loop.SetLogStream(fLog);
241 loop.SetDisplay(fDisplay);
242 loop.SetParList(&plist);
243
244 if (!loop.Eventloop())
245 return kFALSE;
246
247 // Print the result
248 *fLog << inf << "Rule: " << rule << endl;
249 hist.GetAlphaFitter().Print("result");
250
251 if (!WriteDisplay(out))
252 return kFALSE;
253
254 return kTRUE;
255}
256
Note: See TracBrowser for help on using the repository browser.