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 | // MJTrainSeparation
|
---|
28 | //
|
---|
29 | ////////////////////////////////////////////////////////////////////////////
|
---|
30 | #include "MJTrainSeparation.h"
|
---|
31 |
|
---|
32 | #include <TH2.h>
|
---|
33 | #include <TGraph.h>
|
---|
34 | #include <TVirtualPad.h>
|
---|
35 |
|
---|
36 | #include "MHMatrix.h"
|
---|
37 |
|
---|
38 | #include "MLog.h"
|
---|
39 | #include "MLogManip.h"
|
---|
40 |
|
---|
41 | // tools
|
---|
42 | #include "MMath.h"
|
---|
43 | #include "MDataSet.h"
|
---|
44 | #include "MTFillMatrix.h"
|
---|
45 | #include "MChisqEval.h"
|
---|
46 | #include "MStatusDisplay.h"
|
---|
47 |
|
---|
48 | // eventloop
|
---|
49 | #include "MParList.h"
|
---|
50 | #include "MTaskList.h"
|
---|
51 | #include "MEvtLoop.h"
|
---|
52 |
|
---|
53 | // tasks
|
---|
54 | #include "MReadMarsFile.h"
|
---|
55 | #include "MContinue.h"
|
---|
56 | #include "MFillH.h"
|
---|
57 | #include "MRanForestCalc.h"
|
---|
58 | #include "MParameterCalc.h"
|
---|
59 |
|
---|
60 | // container
|
---|
61 | #include "MMcEvt.hxx"
|
---|
62 | #include "MParameters.h"
|
---|
63 |
|
---|
64 | // histograms
|
---|
65 | #include "MBinning.h"
|
---|
66 | #include "MH3.h"
|
---|
67 | #include "MHHadronness.h"
|
---|
68 |
|
---|
69 | // filter
|
---|
70 | #include "MF.h"
|
---|
71 | #include "MFEventSelector.h"
|
---|
72 | #include "MFilterList.h"
|
---|
73 |
|
---|
74 | ClassImp(MJTrainSeparation);
|
---|
75 |
|
---|
76 | using namespace std;
|
---|
77 |
|
---|
78 | void MJTrainSeparation::DisplayResult(MH3 &h31, MH3 &h32)
|
---|
79 | {
|
---|
80 | TH2 &g = (TH2&)h32.GetHist();
|
---|
81 | TH2 &h = (TH2&)h31.GetHist();
|
---|
82 |
|
---|
83 | h.SetMarkerColor(kRed);
|
---|
84 | g.SetMarkerColor(kGreen);
|
---|
85 |
|
---|
86 | const Int_t nx = h.GetNbinsX();
|
---|
87 | const Int_t ny = h.GetNbinsY();
|
---|
88 |
|
---|
89 | gROOT->SetSelectedPad(NULL);
|
---|
90 |
|
---|
91 | TGraph gr1;
|
---|
92 | TGraph gr2;
|
---|
93 | for (int x=0; x<nx; x++)
|
---|
94 | {
|
---|
95 | TH1 *hx = h.ProjectionY("H_py", x+1, x+1);
|
---|
96 | TH1 *gx = g.ProjectionY("G_py", x+1, x+1);
|
---|
97 |
|
---|
98 | Double_t max1 = -1;
|
---|
99 | Double_t max2 = -1;
|
---|
100 | Int_t maxy1 = 0;
|
---|
101 | Int_t maxy2 = 0;
|
---|
102 | for (int y=0; y<ny; y++)
|
---|
103 | {
|
---|
104 | const Float_t s = gx->Integral(1, y+1);
|
---|
105 | const Float_t b = hx->Integral(1, y+1);
|
---|
106 | const Float_t sig1 = MMath::SignificanceLiMa(s+b, b);
|
---|
107 | const Float_t sig2 = s<1 ? 0 : MMath::SignificanceLiMa(s+b, b)*TMath::Log10(s);
|
---|
108 | if (sig1>max1)
|
---|
109 | {
|
---|
110 | maxy1 = y;
|
---|
111 | max1 = sig1;
|
---|
112 | }
|
---|
113 | if (sig2>max2)
|
---|
114 | {
|
---|
115 | maxy2 = y;
|
---|
116 | max2 = sig2;
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | gr1.SetPoint(x, h.GetXaxis()->GetBinCenter(x+1), h.GetYaxis()->GetBinCenter(maxy1+1));
|
---|
121 | gr2.SetPoint(x, h.GetXaxis()->GetBinCenter(x+1), h.GetYaxis()->GetBinCenter(maxy2+1));
|
---|
122 |
|
---|
123 | delete hx;
|
---|
124 | delete gx;
|
---|
125 | }
|
---|
126 |
|
---|
127 | fDisplay->AddTab("OptCut");
|
---|
128 | gPad->SetLogx();
|
---|
129 | h.DrawCopy();
|
---|
130 | g.DrawCopy("same");
|
---|
131 | gr1.SetMarkerStyle(kFullDotMedium);
|
---|
132 | gr1.DrawClone("LP")->SetBit(kCanDelete);
|
---|
133 | gr2.SetLineColor(kBlue);
|
---|
134 | gr2.SetMarkerStyle(kFullDotMedium);
|
---|
135 | gr2.DrawClone("LP")->SetBit(kCanDelete);
|
---|
136 | }
|
---|
137 |
|
---|
138 | Bool_t MJTrainSeparation::Train(const char *out)
|
---|
139 | {
|
---|
140 | if (!fDataSetTrain.IsValid())
|
---|
141 | {
|
---|
142 | *fLog << err << "ERROR - DataSet for training invalid!" << endl;
|
---|
143 | return kFALSE;
|
---|
144 | }
|
---|
145 | if (!fDataSetTest.IsValid())
|
---|
146 | {
|
---|
147 | *fLog << err << "ERROR - DataSet for testing invalid!" << endl;
|
---|
148 | return kFALSE;
|
---|
149 | }
|
---|
150 |
|
---|
151 | // --------------------- Setup files --------------------
|
---|
152 | MReadMarsFile read1("Events");
|
---|
153 | MReadMarsFile read2("Events");
|
---|
154 | MReadMarsFile read3("Events");
|
---|
155 | MReadMarsFile read4("Events");
|
---|
156 | read1.DisableAutoScheme();
|
---|
157 | read2.DisableAutoScheme();
|
---|
158 | read3.DisableAutoScheme();
|
---|
159 | read4.DisableAutoScheme();
|
---|
160 |
|
---|
161 | fDataSetTrain.AddFilesOn(read1);
|
---|
162 | fDataSetTrain.AddFilesOff(read3);
|
---|
163 |
|
---|
164 | fDataSetTest.AddFilesOff(read2);
|
---|
165 | fDataSetTest.AddFilesOn(read4);
|
---|
166 |
|
---|
167 | // ----------------------- Setup RF ----------------------
|
---|
168 | MHMatrix train("Train");
|
---|
169 | train.AddColumns(fRules);
|
---|
170 | train.AddColumn("MHadronness.fVal");
|
---|
171 |
|
---|
172 | // ----------------------- Fill Matrix RF ----------------------
|
---|
173 |
|
---|
174 | MParameterD had("MHadronness");
|
---|
175 |
|
---|
176 | MParList plistx;
|
---|
177 | plistx.AddToList(&had);
|
---|
178 | plistx.AddToList(this);
|
---|
179 |
|
---|
180 | MTFillMatrix fill;
|
---|
181 | fill.SetLogStream(fLog);
|
---|
182 | fill.SetDisplay(fDisplay);
|
---|
183 | fill.AddPreCuts(fPreCuts);
|
---|
184 | fill.AddPreCuts(fTrainCuts);
|
---|
185 |
|
---|
186 | // Set classifier for gammas
|
---|
187 | had.SetVal(0);
|
---|
188 | fill.SetName("FillGammas");
|
---|
189 | fill.SetDestMatrix1(&train, fNumTrainOn);
|
---|
190 | fill.SetReader(&read1);
|
---|
191 | if (!fill.Process(plistx))
|
---|
192 | return kFALSE;
|
---|
193 |
|
---|
194 | // Set classifier for hadrons
|
---|
195 | had.SetVal(1);
|
---|
196 | fill.SetName("FillBackground");
|
---|
197 | fill.SetDestMatrix1(&train, fNumTrainOff);
|
---|
198 | fill.SetReader(&read3);
|
---|
199 | if (!fill.Process(plistx))
|
---|
200 | return kFALSE;
|
---|
201 |
|
---|
202 | // ------------------------ Train RF --------------------------
|
---|
203 |
|
---|
204 | MRanForestCalc rf;
|
---|
205 | rf.SetNumObsoleteVariables(1);
|
---|
206 | rf.SetDebug(fDebug);
|
---|
207 | rf.SetDisplay(fDisplay);
|
---|
208 | rf.SetLogStream(fLog);
|
---|
209 | rf.SetFileName(out);
|
---|
210 | rf.SetNameOutput("MHadronness");
|
---|
211 |
|
---|
212 | //MBinning b(2, -0.5, 1.5, "BinningHadronness", "lin");
|
---|
213 |
|
---|
214 | //if (!rf.TrainMultiRF(train, b.GetEdgesD())) // classification
|
---|
215 | // return;
|
---|
216 |
|
---|
217 | //if (!rf.TrainSingleRF(train, b.GetEdgesD())) // classification
|
---|
218 | // return;
|
---|
219 |
|
---|
220 | if (!rf.TrainSingleRF(train)) // regression
|
---|
221 | return kFALSE;
|
---|
222 |
|
---|
223 | //fDisplay = rf.GetDisplay();
|
---|
224 |
|
---|
225 | // --------------------- Display result ----------------------
|
---|
226 | gLog.Separator("Test");
|
---|
227 |
|
---|
228 | MParList plist;
|
---|
229 | MTaskList tlist;
|
---|
230 | plist.AddToList(this);
|
---|
231 | plist.AddToList(&tlist);
|
---|
232 |
|
---|
233 | MMcEvt mcevt;
|
---|
234 | plist.AddToList(&mcevt);
|
---|
235 |
|
---|
236 | // ----- Setup histograms -----
|
---|
237 | MBinning binsy(100, 0 , 1, "BinningMH3Y", "lin");
|
---|
238 | MBinning binsx( 50, 10, 100000, "BinningMH3X", "log");
|
---|
239 |
|
---|
240 | plist.AddToList(&binsx);
|
---|
241 | plist.AddToList(&binsy);
|
---|
242 |
|
---|
243 | MH3 h31("MHillas.fSize", "MHadronness.fVal");
|
---|
244 | MH3 h32("MHillas.fSize", "MHadronness.fVal");
|
---|
245 | h31.SetTitle("Background probability vs. Size:Size [phe]:Hadronness");
|
---|
246 | h32.SetTitle("Background probability vs. Size:Size [phe]:Hadronness");
|
---|
247 |
|
---|
248 | MHHadronness hist;
|
---|
249 |
|
---|
250 | // ----- Setup tasks -----
|
---|
251 | MFillH fillh0(&hist, "", "FillHadronness");
|
---|
252 | MFillH fillh1(&h31);
|
---|
253 | MFillH fillh2(&h32);
|
---|
254 | fillh1.SetNameTab("Background");
|
---|
255 | fillh2.SetNameTab("Gammas");
|
---|
256 | fillh0.SetBit(MFillH::kDoNotDisplay);
|
---|
257 |
|
---|
258 | // ----- Setup filter -----
|
---|
259 | MFilterList precuts;
|
---|
260 | precuts.AddToList(fPreCuts);
|
---|
261 | precuts.AddToList(fTestCuts);
|
---|
262 |
|
---|
263 | MContinue c0(&precuts);
|
---|
264 | c0.SetName("PreCuts");
|
---|
265 | c0.SetInverted();
|
---|
266 |
|
---|
267 | MFEventSelector sel;
|
---|
268 | sel.SetNumSelectEvts(fNumTestOn);
|
---|
269 |
|
---|
270 | MContinue c1(&sel);
|
---|
271 | c1.SetInverted();
|
---|
272 |
|
---|
273 | // ----- Setup tasklist -----
|
---|
274 | tlist.AddToList(&read2);
|
---|
275 | tlist.AddToList(&c0);
|
---|
276 | tlist.AddToList(&c1);
|
---|
277 | tlist.AddToList(&rf);
|
---|
278 | tlist.AddToList(&fillh0);
|
---|
279 | tlist.AddToList(&fillh1);
|
---|
280 |
|
---|
281 | // ----- Run eventloop on gammas -----
|
---|
282 | MEvtLoop loop;
|
---|
283 | loop.SetDisplay(fDisplay);
|
---|
284 | loop.SetLogStream(fLog);
|
---|
285 | loop.SetParList(&plist);
|
---|
286 |
|
---|
287 | if (!loop.Eventloop())
|
---|
288 | return kFALSE;
|
---|
289 |
|
---|
290 | // ----- Setup and run eventloop on background -----
|
---|
291 | sel.SetNumSelectEvts(fNumTestOff);
|
---|
292 | fillh0.ResetBit(MFillH::kDoNotDisplay);
|
---|
293 |
|
---|
294 | tlist.Replace(&read4);
|
---|
295 | tlist.Replace(&fillh2);
|
---|
296 |
|
---|
297 | if (!loop.Eventloop())
|
---|
298 | return kFALSE;
|
---|
299 |
|
---|
300 | DisplayResult(h31, h32);
|
---|
301 |
|
---|
302 | if (!WriteDisplay(out))
|
---|
303 | return kFALSE;
|
---|
304 |
|
---|
305 | return kTRUE;
|
---|
306 | }
|
---|
307 |
|
---|