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

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