source: trunk/MagicSoft/Mars/mjtrain/MJTrainEnergy.cc@ 8489

Last change on this file since 8489 was 8223, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 5.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// MJTrainEnergy
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// MJTrainEnergy opt;
37// //opt.SetDebug();
38// opt.AddParameter("MHillas.fSize");
39// opt.AddParameter("MHillasSrc.fDist");
40// MStatusDisplay *d = new MStatusDisplay;
41// opt.SetDisplay(d);
42// opt.AddPreCut("MHillasSrc.fDCA*MGeomCam.fConvMm2Deg<0.3");
43// opt.Train("rf-energy.root", set, 30000); // Number of train events
44//
45// Random Numbers:
46// ---------------
47// Use:
48// if(gRandom)
49// delete gRandom;
50// gRandom = new TRandom3();
51// in advance to change the random number generator.
52//
53////////////////////////////////////////////////////////////////////////////
54#include "MJTrainEnergy.h"
55
56#include "MHMatrix.h"
57
58#include "MLog.h"
59#include "MLogManip.h"
60
61// tools
62#include "MDataSet.h"
63#include "MTFillMatrix.h"
64
65// eventloop
66#include "MParList.h"
67#include "MTaskList.h"
68#include "MEvtLoop.h"
69
70// tasks
71#include "MReadMarsFile.h"
72#include "MContinue.h"
73#include "MFillH.h"
74#include "MRanForestCalc.h"
75
76// histograms
77#include "MHEnergyEst.h"
78
79// filter
80#include "MFilterList.h"
81
82ClassImp(MJTrainEnergy);
83
84using namespace std;
85
86Bool_t MJTrainEnergy::Train(const char *out, const MDataSet &set, Int_t num)
87{
88 if (!set.IsValid())
89 {
90 *fLog << err << "ERROR - DataSet invalid!" << endl;
91 return kFALSE;
92 }
93
94 *fLog << inf;
95 fLog->Separator(GetDescriptor());
96
97 // --------------------- Setup files --------------------
98 MReadMarsFile readtrn("Events");
99 MReadMarsFile readtst("Events");
100 readtrn.DisableAutoScheme();
101 readtst.DisableAutoScheme();
102
103 if (!set.AddFilesOn(readtrn))
104 return kFALSE;
105 if (!set.AddFilesOff(readtst))
106 return kFALSE;
107
108 // ----------------------- Setup Matrix ------------------
109 MHMatrix train("Train");
110 train.AddColumns(fRules);
111 if (fEnableWeights)
112 train.AddColumn("MWeight.fVal");
113 train.AddColumn("MMcEvt.fImpact/100");
114 train.AddColumn("MMcEvt.fTelescopeTheta*kRad2Deg");
115 train.AddColumn("MMcEvt.fEnergy");
116
117
118 // ----------------------- Fill Matrix RF ----------------------
119 MTFillMatrix fill;
120 fill.SetDisplay(fDisplay);
121 fill.SetLogStream(fLog);
122 fill.SetDestMatrix1(&train, num);
123 fill.SetReader(&readtrn);
124 fill.AddPreCuts(fPreCuts);
125 fill.AddPreCuts(fTrainCuts);
126 fill.AddPreTasks(fPreTasks);
127 fill.AddPostTasks(fPostTasks);
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(3);
137 rf.SetLastDataColumnHasWeights(fEnableWeights);
138 rf.SetDisplay(fDisplay);
139 rf.SetLogStream(fLog);
140 rf.SetFileName(out);
141 rf.SetDebug(fDebug);
142 rf.SetNameOutput("MEnergyEst");
143
144 /*
145 MBinning b(32, 10, 100000, "BinningEnergyEst", "log");
146
147 if (!rf.TrainMultiRF(train, b.GetEdgesD())) // classification with one tree per bin
148 return;
149
150 if (!rf.TrainSingleRF(train, b.GetEdgesD())) // classification into different bins
151 return;
152 */
153 if (!rf.TrainRegression(train)) // regression (best choice)
154 return kFALSE;
155
156 // --------------------- Display result ----------------------
157
158 gLog.Separator("Test");
159
160 MParList plist;
161 MTaskList tlist;
162 plist.AddToList(this);
163 plist.AddToList(&tlist);
164 //plist.AddToList(&b);
165
166 MFilterList list;
167 if (!list.AddToList(fPreCuts))
168 *fLog << err << "ERROR - Calling MFilterList::AddToList for fPreCuts failed!" << endl;
169 if (!list.AddToList(fTestCuts))
170 *fLog << err << "ERROR - Calling MFilterList::AddToList for fTestCuts failed!" << endl;
171
172 MContinue cont(&list);
173 cont.SetInverted();
174
175 MHEnergyEst hist;
176 MFillH fillh(&hist);
177 if (fEnableWeights)
178 fillh.SetWeight();
179
180 tlist.AddToList(&readtst);
181 tlist.AddToList(fPreTasks);
182 tlist.AddToList(&cont);
183 tlist.AddToList(&rf);
184 tlist.AddToList(fPostTasks);
185 tlist.AddToList(&fillh);
186
187 MEvtLoop loop;
188 loop.SetLogStream(fLog);
189 loop.SetDisplay(fDisplay);
190 loop.SetParList(&plist);
191
192 if (!loop.Eventloop())
193 return kFALSE;
194
195 if (!WriteDisplay(out))
196 return kFALSE;
197
198 return kTRUE;
199}
Note: See TracBrowser for help on using the repository browser.