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.SetNumObsoleteVariables(3);
|
---|
128 | rf.SetDisplay(fDisplay);
|
---|
129 | rf.SetLogStream(fLog);
|
---|
130 | rf.SetFileName(out);
|
---|
131 | rf.SetDebug(fDebug);
|
---|
132 | rf.SetNameOutput("MEnergyEst");
|
---|
133 |
|
---|
134 | /*
|
---|
135 | MBinning b(32, 10, 100000, "BinningEnergyEst", "log");
|
---|
136 |
|
---|
137 | if (!rf.TrainMultiRF(train, b.GetEdgesD())) // classification with one tree per bin
|
---|
138 | return;
|
---|
139 |
|
---|
140 | if (!rf.TrainSingleRF(train, b.GetEdgesD())) // classification into different bins
|
---|
141 | return;
|
---|
142 | */
|
---|
143 | if (!rf.TrainRegression(train)) // regression (best choice)
|
---|
144 | return kFALSE;
|
---|
145 |
|
---|
146 | // --------------------- Display result ----------------------
|
---|
147 |
|
---|
148 | gLog.Separator("Test");
|
---|
149 |
|
---|
150 | MParList plist;
|
---|
151 | MTaskList tlist;
|
---|
152 | plist.AddToList(this);
|
---|
153 | plist.AddToList(&tlist);
|
---|
154 | //plist.AddToList(&b);
|
---|
155 |
|
---|
156 | MFilterList list;
|
---|
157 | if (!list.AddToList(fPreCuts))
|
---|
158 | *fLog << err << "ERROR - Calling MFilterList::AddToList for fPreCuts failed!" << endl;
|
---|
159 | if (!list.AddToList(fTestCuts))
|
---|
160 | *fLog << err << "ERROR - Calling MFilterList::AddToList for fTestCuts failed!" << endl;
|
---|
161 |
|
---|
162 | MContinue cont(&list);
|
---|
163 | cont.SetInverted();
|
---|
164 |
|
---|
165 | MHEnergyEst hist;
|
---|
166 | MFillH fillh(&hist);
|
---|
167 |
|
---|
168 | tlist.AddToList(&readtst);
|
---|
169 | tlist.AddToList(&cont);
|
---|
170 | tlist.AddToList(&rf);
|
---|
171 | tlist.AddToList(&fillh);
|
---|
172 |
|
---|
173 | MEvtLoop loop;
|
---|
174 | loop.SetLogStream(fLog);
|
---|
175 | loop.SetDisplay(fDisplay);
|
---|
176 | loop.SetParList(&plist);
|
---|
177 |
|
---|
178 | if (!loop.Eventloop())
|
---|
179 | return kFALSE;
|
---|
180 |
|
---|
181 | if (!WriteDisplay(out))
|
---|
182 | return kFALSE;
|
---|
183 |
|
---|
184 | return kTRUE;
|
---|
185 | }
|
---|