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

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