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 |
|
---|
82 | ClassImp(MJTrainEnergy);
|
---|
83 |
|
---|
84 | using namespace std;
|
---|
85 |
|
---|
86 | Bool_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 | set.AddFilesOn(readtrn);
|
---|
104 | set.AddFilesOff(readtst);
|
---|
105 |
|
---|
106 | // ----------------------- Setup Matrix ------------------
|
---|
107 | MHMatrix train("Train");
|
---|
108 | train.AddColumns(fRules);
|
---|
109 | train.AddColumn("MMcEvt.fImpact/100");
|
---|
110 | train.AddColumn("MMcEvt.fTelescopeTheta*kRad2Deg");
|
---|
111 | train.AddColumn("MMcEvt.fEnergy");
|
---|
112 |
|
---|
113 |
|
---|
114 | // ----------------------- Fill Matrix RF ----------------------
|
---|
115 | MTFillMatrix fill;
|
---|
116 | fill.SetDisplay(fDisplay);
|
---|
117 | fill.SetLogStream(fLog);
|
---|
118 | fill.SetDestMatrix1(&train, num);
|
---|
119 | fill.SetReader(&readtrn);
|
---|
120 | fill.AddPreCuts(fPreCuts);
|
---|
121 | fill.AddPreCuts(fTrainCuts);
|
---|
122 | if (!fill.Process())
|
---|
123 | return kFALSE;
|
---|
124 |
|
---|
125 | // ------------------------ Train RF --------------------------
|
---|
126 | MRanForestCalc rf;
|
---|
127 | rf.SetNumTrees(fNumTrees);
|
---|
128 | rf.SetNdSize(fNdSize);
|
---|
129 | rf.SetNumTry(fNumTry);
|
---|
130 | rf.SetNumObsoleteVariables(3);
|
---|
131 | rf.SetDisplay(fDisplay);
|
---|
132 | rf.SetLogStream(fLog);
|
---|
133 | rf.SetFileName(out);
|
---|
134 | rf.SetDebug(fDebug);
|
---|
135 | rf.SetNameOutput("MEnergyEst");
|
---|
136 |
|
---|
137 | /*
|
---|
138 | MBinning b(32, 10, 100000, "BinningEnergyEst", "log");
|
---|
139 |
|
---|
140 | if (!rf.TrainMultiRF(train, b.GetEdgesD())) // classification with one tree per bin
|
---|
141 | return;
|
---|
142 |
|
---|
143 | if (!rf.TrainSingleRF(train, b.GetEdgesD())) // classification into different bins
|
---|
144 | return;
|
---|
145 | */
|
---|
146 | if (!rf.TrainRegression(train)) // regression (best choice)
|
---|
147 | return kFALSE;
|
---|
148 |
|
---|
149 | // --------------------- Display result ----------------------
|
---|
150 |
|
---|
151 | gLog.Separator("Test");
|
---|
152 |
|
---|
153 | MParList plist;
|
---|
154 | MTaskList tlist;
|
---|
155 | plist.AddToList(this);
|
---|
156 | plist.AddToList(&tlist);
|
---|
157 | //plist.AddToList(&b);
|
---|
158 |
|
---|
159 | MFilterList list;
|
---|
160 | if (!list.AddToList(fPreCuts))
|
---|
161 | *fLog << err << "ERROR - Calling MFilterList::AddToList for fPreCuts failed!" << endl;
|
---|
162 | if (!list.AddToList(fTestCuts))
|
---|
163 | *fLog << err << "ERROR - Calling MFilterList::AddToList for fTestCuts failed!" << endl;
|
---|
164 |
|
---|
165 | MContinue cont(&list);
|
---|
166 | cont.SetInverted();
|
---|
167 |
|
---|
168 | MHEnergyEst hist;
|
---|
169 | MFillH fillh(&hist);
|
---|
170 |
|
---|
171 | tlist.AddToList(&readtst);
|
---|
172 | tlist.AddToList(&cont);
|
---|
173 | tlist.AddToList(&rf);
|
---|
174 | tlist.AddToList(&fillh);
|
---|
175 |
|
---|
176 | MEvtLoop loop;
|
---|
177 | loop.SetLogStream(fLog);
|
---|
178 | loop.SetDisplay(fDisplay);
|
---|
179 | loop.SetParList(&plist);
|
---|
180 |
|
---|
181 | if (!loop.Eventloop())
|
---|
182 | return kFALSE;
|
---|
183 |
|
---|
184 | if (!WriteDisplay(out))
|
---|
185 | return kFALSE;
|
---|
186 |
|
---|
187 | return kTRUE;
|
---|
188 | }
|
---|