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 Hengstebeck 3/2003 <mailto:hengsteb@physik.hu-berlin.de>
|
---|
19 | !
|
---|
20 | ! Copyright: MAGIC Software Development, 2000-2005
|
---|
21 | !
|
---|
22 | !
|
---|
23 | \* ======================================================================== */
|
---|
24 |
|
---|
25 | /////////////////////////////////////////////////////////////////////////////
|
---|
26 | //
|
---|
27 | // MRanTree
|
---|
28 | //
|
---|
29 | // ParameterContainer for Tree structure
|
---|
30 | //
|
---|
31 | /////////////////////////////////////////////////////////////////////////////
|
---|
32 | #include "MRanTree.h"
|
---|
33 |
|
---|
34 | #include <iostream>
|
---|
35 |
|
---|
36 | #include <TRandom.h>
|
---|
37 |
|
---|
38 | #include "MArrayI.h"
|
---|
39 | #include "MArrayF.h"
|
---|
40 |
|
---|
41 | #include "MMath.h"
|
---|
42 |
|
---|
43 | #include "MLog.h"
|
---|
44 | #include "MLogManip.h"
|
---|
45 |
|
---|
46 | ClassImp(MRanTree);
|
---|
47 |
|
---|
48 | using namespace std;
|
---|
49 |
|
---|
50 |
|
---|
51 | // --------------------------------------------------------------------------
|
---|
52 | // Default constructor.
|
---|
53 | //
|
---|
54 | MRanTree::MRanTree(const char *name, const char *title):fClassify(kTRUE),fNdSize(0), fNumTry(3)
|
---|
55 | {
|
---|
56 |
|
---|
57 | fName = name ? name : "MRanTree";
|
---|
58 | fTitle = title ? title : "Storage container for structure of a single tree";
|
---|
59 | }
|
---|
60 |
|
---|
61 | // --------------------------------------------------------------------------
|
---|
62 | // Copy constructor
|
---|
63 | //
|
---|
64 | MRanTree::MRanTree(const MRanTree &tree)
|
---|
65 | {
|
---|
66 | fName = tree.fName;
|
---|
67 | fTitle = tree.fTitle;
|
---|
68 |
|
---|
69 | fClassify = tree.fClassify;
|
---|
70 | fNdSize = tree.fNdSize;
|
---|
71 | fNumTry = tree.fNumTry;
|
---|
72 |
|
---|
73 | fNumNodes = tree.fNumNodes;
|
---|
74 | fNumEndNodes = tree.fNumEndNodes;
|
---|
75 |
|
---|
76 | fBestVar = tree.fBestVar;
|
---|
77 | fTreeMap1 = tree.fTreeMap1;
|
---|
78 | fTreeMap2 = tree.fTreeMap2;
|
---|
79 | fBestSplit = tree.fBestSplit;
|
---|
80 | fGiniDec = tree.fGiniDec;
|
---|
81 | }
|
---|
82 |
|
---|
83 | void MRanTree::SetNdSize(Int_t n)
|
---|
84 | {
|
---|
85 | // threshold nodesize of terminal nodes, i.e. the training data is splitted
|
---|
86 | // until there is only pure date in the subsets(=terminal nodes) or the
|
---|
87 | // subset size is LE n
|
---|
88 |
|
---|
89 | fNdSize=TMath::Max(1,n);//at least 1 event per node
|
---|
90 | }
|
---|
91 |
|
---|
92 | void MRanTree::SetNumTry(Int_t n)
|
---|
93 | {
|
---|
94 | // number of trials in random split selection:
|
---|
95 | // choose at least 1 variable to split in
|
---|
96 |
|
---|
97 | fNumTry=TMath::Max(1,n);
|
---|
98 | }
|
---|
99 |
|
---|
100 | void MRanTree::GrowTree(TMatrix *mat, const MArrayF &hadtrue, const MArrayI &idclass,
|
---|
101 | MArrayI &datasort, const MArrayI &datarang, const MArrayF &tclasspop,
|
---|
102 | const Float_t &mean, const Float_t &square, const MArrayI &jinbag, const MArrayF &winbag,
|
---|
103 | const int nclass)
|
---|
104 | {
|
---|
105 | // arrays have to be initialized with generous size, so number of total nodes (nrnodes)
|
---|
106 | // is estimated for worst case
|
---|
107 | const Int_t numdim =mat->GetNcols();
|
---|
108 | const Int_t numdata=winbag.GetSize();
|
---|
109 | const Int_t nrnodes=2*numdata+1;
|
---|
110 |
|
---|
111 | // number of events in bootstrap sample
|
---|
112 | Int_t ninbag=0;
|
---|
113 | for (Int_t n=0;n<numdata;n++) if(jinbag[n]==1) ninbag++;
|
---|
114 |
|
---|
115 | MArrayI bestsplit(nrnodes);
|
---|
116 | MArrayI bestsplitnext(nrnodes);
|
---|
117 |
|
---|
118 | fBestVar.Set(nrnodes); fBestVar.Reset();
|
---|
119 | fTreeMap1.Set(nrnodes); fTreeMap1.Reset();
|
---|
120 | fTreeMap2.Set(nrnodes); fTreeMap2.Reset();
|
---|
121 | fBestSplit.Set(nrnodes); fBestSplit.Reset();
|
---|
122 | fGiniDec.Set(numdim); fGiniDec.Reset();
|
---|
123 |
|
---|
124 |
|
---|
125 | if(fClassify)
|
---|
126 | FindBestSplit=&MRanTree::FindBestSplitGini;
|
---|
127 | else
|
---|
128 | FindBestSplit=&MRanTree::FindBestSplitSigma;
|
---|
129 |
|
---|
130 | // tree growing
|
---|
131 | BuildTree(datasort,datarang,hadtrue,idclass,bestsplit, bestsplitnext,
|
---|
132 | tclasspop,mean,square,winbag,ninbag,nclass);
|
---|
133 |
|
---|
134 | // post processing, determine cut (or split) values fBestSplit
|
---|
135 | for(Int_t k=0; k<nrnodes; k++)
|
---|
136 | {
|
---|
137 | if (GetNodeStatus(k)==-1)
|
---|
138 | continue;
|
---|
139 |
|
---|
140 | const Int_t &bsp =bestsplit[k];
|
---|
141 | const Int_t &bspn=bestsplitnext[k];
|
---|
142 | const Int_t &msp =fBestVar[k];
|
---|
143 |
|
---|
144 | fBestSplit[k] = ((*mat)(bsp, msp)+(*mat)(bspn,msp))/2;
|
---|
145 | }
|
---|
146 |
|
---|
147 | // resizing arrays to save memory
|
---|
148 | fBestVar.Set(fNumNodes);
|
---|
149 | fTreeMap1.Set(fNumNodes);
|
---|
150 | fTreeMap2.Set(fNumNodes);
|
---|
151 | fBestSplit.Set(fNumNodes);
|
---|
152 | }
|
---|
153 |
|
---|
154 | int MRanTree::FindBestSplitGini(const MArrayI &datasort,const MArrayI &datarang,
|
---|
155 | const MArrayF &hadtrue,const MArrayI &idclass,
|
---|
156 | Int_t ndstart,Int_t ndend, const MArrayF &tclasspop,
|
---|
157 | const Float_t &mean, const Float_t &square, Int_t &msplit,
|
---|
158 | Float_t &decsplit,Int_t &nbest, const MArrayF &winbag,
|
---|
159 | const int nclass)
|
---|
160 | {
|
---|
161 | const Int_t nrnodes = fBestSplit.GetSize();
|
---|
162 | const Int_t numdata = (nrnodes-1)/2;
|
---|
163 | const Int_t mdim = fGiniDec.GetSize();
|
---|
164 |
|
---|
165 | // For the best split, msplit is the index of the variable (e.g Hillas par.,
|
---|
166 | // zenith angle ,...)
|
---|
167 | // split on. decsplit is the decreae in impurity measured by Gini-index.
|
---|
168 | // nsplit is the case number of value of msplit split on,
|
---|
169 | // and nsplitnext is the case number of the next larger value of msplit.
|
---|
170 |
|
---|
171 | Int_t nbestvar=0;
|
---|
172 |
|
---|
173 | // compute initial values of numerator and denominator of Gini-index,
|
---|
174 | // Gini index= pno/dno
|
---|
175 | Double_t pno=0;
|
---|
176 | Double_t pdo=0;
|
---|
177 |
|
---|
178 | // tclasspop: sum of weights for events in class
|
---|
179 | for (Int_t j=0; j<nclass; j++) // loop over number of classes to classifiy
|
---|
180 | {
|
---|
181 | pno+=tclasspop[j]*tclasspop[j];
|
---|
182 | pdo+=tclasspop[j];
|
---|
183 | }
|
---|
184 |
|
---|
185 | const Double_t crit0=pno/pdo; // weighted mean of weights
|
---|
186 |
|
---|
187 | // start main loop through variables to find best split,
|
---|
188 | // (Gini-index as criterium crit)
|
---|
189 |
|
---|
190 | Double_t critmax=-FLT_MAX;
|
---|
191 |
|
---|
192 | // random split selection, number of trials = fNumTry
|
---|
193 | for (Int_t mt=0; mt<fNumTry; mt++) // we could try ALL variables???
|
---|
194 | {
|
---|
195 | const Int_t mvar= gRandom->Integer(mdim);
|
---|
196 | const Int_t mn = mvar*numdata;
|
---|
197 |
|
---|
198 | // Gini index = rrn/rrd+rln/rld
|
---|
199 | Double_t rrn=pno;
|
---|
200 | Double_t rrd=pdo;
|
---|
201 | Double_t rln=0;
|
---|
202 | Double_t rld=0;
|
---|
203 |
|
---|
204 | MArrayF wl(nclass); // left node //nclass
|
---|
205 | MArrayF wr(tclasspop); // right node//nclass
|
---|
206 |
|
---|
207 | Double_t critvar=-FLT_MAX;
|
---|
208 | for(Int_t nsp=ndstart;nsp<=ndend-1;nsp++)
|
---|
209 | {
|
---|
210 | const Int_t &nc = datasort[mn+nsp];
|
---|
211 | const Int_t &k = idclass[nc];
|
---|
212 | const Float_t &u = winbag[nc];
|
---|
213 |
|
---|
214 | // do classification, Gini index as split rule
|
---|
215 | rln +=u*(2*wl[k]+u); // += u*(wl[k]{i-1} + wl[k]{i-1}+u{i})
|
---|
216 | rld +=u; // sum of weights left from cut total
|
---|
217 | wl[k] +=u; // sum of weights left from cut for class k
|
---|
218 |
|
---|
219 | rrn -=u*(2*wr[k]-u); // -= u*(wr[k]{i-1} + wr[k]{i-1}-u{i})
|
---|
220 | // rr0=0; rr0+=u*2*tclasspop[k]
|
---|
221 | // rrn = pno - rr0 + rln
|
---|
222 | rrd -=u; // sum of weights right from cut total
|
---|
223 | wr[k] -=u; // sum of weights right from cut for class k
|
---|
224 |
|
---|
225 | // REPLACE BY?
|
---|
226 | // rr0 = 0
|
---|
227 | // rr0 += u*2*tclasspop[k]
|
---|
228 | // rrn = pno - rr0 + rln
|
---|
229 | // rrd = pdo - rld
|
---|
230 | // wr[k] = tclasspop[k] - wl[k]
|
---|
231 |
|
---|
232 | // crit = (rln*(pdo - rld + 1) + pno - rr0) / rld*(pdo - rld)
|
---|
233 |
|
---|
234 | /*
|
---|
235 | if (k==background)
|
---|
236 | continue;
|
---|
237 | crit = TMath::Max(MMath::SignificanceLiMa(rld, rld-wl[k]),
|
---|
238 | MMath::SignificanceLiMa(rrd, rrd-wr[k]))
|
---|
239 | */
|
---|
240 |
|
---|
241 | // This condition is in fact a == (> cannot happen at all)
|
---|
242 | // This is because we cannot set the cut between two identical values
|
---|
243 | //if (datarang[mn+datasort[mn+nsp]]>=datarang[mn+datasort[mn+nsp+1]])
|
---|
244 | if (datarang[mn+nc]>=datarang[mn+datasort[mn+nsp+1]])
|
---|
245 | continue;
|
---|
246 |
|
---|
247 | // If crit starts to become pretty large do WHAT???
|
---|
248 | //if (TMath::Min(rrd,rld)<=1.0e-5) // FIXME: CHECKIT FOR WEIGHTS!
|
---|
249 | // continue;
|
---|
250 |
|
---|
251 | const Double_t crit=(rln/rld)+(rrn/rrd);
|
---|
252 | if (!TMath::Finite(crit))
|
---|
253 | continue;
|
---|
254 |
|
---|
255 | // Search for the highest value of crit
|
---|
256 | if (crit<=critvar) continue;
|
---|
257 |
|
---|
258 | // store the highest crit value and the corresponding event to cut at
|
---|
259 | nbestvar=nsp;
|
---|
260 | critvar=crit;
|
---|
261 | }
|
---|
262 |
|
---|
263 | if (critvar<=critmax) continue;
|
---|
264 |
|
---|
265 | msplit=mvar; // Variable in which to split
|
---|
266 | nbest=nbestvar; // event at which the best split was found
|
---|
267 | critmax=critvar;
|
---|
268 | }
|
---|
269 |
|
---|
270 | // crit0 = MMath::SignificanceLiMa(pdo, pdo-tclasspop[0])
|
---|
271 | // mean increase of sensitivity
|
---|
272 | // decsplit = sqrt(critmax/crit0)
|
---|
273 | decsplit=critmax-crit0;
|
---|
274 |
|
---|
275 | return critmax<-1.0e10 ? 1 : 0;
|
---|
276 | }
|
---|
277 |
|
---|
278 | int MRanTree::FindBestSplitSigma(const MArrayI &datasort,const MArrayI &datarang,
|
---|
279 | const MArrayF &hadtrue, const MArrayI &idclass,
|
---|
280 | Int_t ndstart,Int_t ndend, const MArrayF &tclasspop,
|
---|
281 | const Float_t &mean, const Float_t &square, Int_t &msplit,
|
---|
282 | Float_t &decsplit,Int_t &nbest, const MArrayF &winbag,
|
---|
283 | const int nclass)
|
---|
284 | {
|
---|
285 | const Int_t nrnodes = fBestSplit.GetSize();
|
---|
286 | const Int_t numdata = (nrnodes-1)/2;
|
---|
287 | const Int_t mdim = fGiniDec.GetSize();
|
---|
288 |
|
---|
289 | // For the best split, msplit is the index of the variable (e.g Hillas par., zenith angle ,...)
|
---|
290 | // split on. decsplit is the decreae in impurity measured by Gini-index.
|
---|
291 | // nsplit is the case number of value of msplit split on,
|
---|
292 | // and nsplitnext is the case number of the next larger value of msplit.
|
---|
293 |
|
---|
294 | Int_t nbestvar=0;
|
---|
295 |
|
---|
296 | // compute initial values of numerator and denominator of split-index,
|
---|
297 |
|
---|
298 | // resolution
|
---|
299 | //Double_t pno=-(tclasspop[0]*square-mean*mean)*tclasspop[0];
|
---|
300 | //Double_t pdo= (tclasspop[0]-1.)*mean*mean;
|
---|
301 |
|
---|
302 | // n*resolution
|
---|
303 | //Double_t pno=-(tclasspop[0]*square-mean*mean)*tclasspop[0];
|
---|
304 | //Double_t pdo= mean*mean;
|
---|
305 |
|
---|
306 | // variance
|
---|
307 | //Double_t pno=-(square-mean*mean/tclasspop[0]);
|
---|
308 | //Double_t pdo= (tclasspop[0]-1.);
|
---|
309 |
|
---|
310 | // n*variance
|
---|
311 | Double_t pno= (square-mean*mean/tclasspop[0]);
|
---|
312 | Double_t pdo= 1.;
|
---|
313 |
|
---|
314 | // 1./(n*variance)
|
---|
315 | //Double_t pno= 1.;
|
---|
316 | //Double_t pdo= (square-mean*mean/tclasspop[0]);
|
---|
317 |
|
---|
318 | const Double_t crit0=pno/pdo;
|
---|
319 |
|
---|
320 | // start main loop through variables to find best split,
|
---|
321 |
|
---|
322 | Double_t critmin=FLT_MAX;
|
---|
323 |
|
---|
324 | // random split selection, number of trials = fNumTry
|
---|
325 | for (Int_t mt=0; mt<fNumTry; mt++)
|
---|
326 | {
|
---|
327 | const Int_t mvar= gRandom->Integer(mdim);
|
---|
328 | const Int_t mn = mvar*numdata;
|
---|
329 |
|
---|
330 | Double_t esumr =mean;
|
---|
331 | Double_t e2sumr=square;
|
---|
332 | Double_t esuml =0;
|
---|
333 | Double_t e2suml=0;
|
---|
334 |
|
---|
335 | float wl=0.;// left node
|
---|
336 | float wr=tclasspop[0]; // right node
|
---|
337 |
|
---|
338 | Double_t critvar=critmin;
|
---|
339 | for(Int_t nsp=ndstart;nsp<=ndend-1;nsp++)
|
---|
340 | {
|
---|
341 | const Int_t &nc=datasort[mn+nsp];
|
---|
342 | const Float_t &f=hadtrue[nc];;
|
---|
343 | const Float_t &u=winbag[nc];
|
---|
344 |
|
---|
345 | e2suml+=u*f*f;
|
---|
346 | esuml +=u*f;
|
---|
347 | wl +=u;
|
---|
348 |
|
---|
349 | //-------------------------------------------
|
---|
350 | // resolution
|
---|
351 | //const Double_t rln=(wl*e2suml-esuml*esuml)*wl;
|
---|
352 | //const Double_t rld=(wl-1.)*esuml*esuml;
|
---|
353 |
|
---|
354 | // resolution times n
|
---|
355 | //const Double_t rln=(wl*e2suml-esuml*esuml)*wl;
|
---|
356 | //const Double_t rld=esuml*esuml;
|
---|
357 |
|
---|
358 | // sigma
|
---|
359 | //const Double_t rln=(e2suml-esuml*esuml/wl);
|
---|
360 | //const Double_t rld=(wl-1.);
|
---|
361 |
|
---|
362 | // sigma times n
|
---|
363 | Double_t rln=(e2suml-esuml*esuml/wl);
|
---|
364 | Double_t rld=1.;
|
---|
365 |
|
---|
366 | // 1./(n*variance)
|
---|
367 | //const Double_t rln=1.;
|
---|
368 | //const Double_t rld=(e2suml-esuml*esuml/wl);
|
---|
369 | //-------------------------------------------
|
---|
370 |
|
---|
371 | // REPLACE BY???
|
---|
372 | e2sumr-=u*f*f; // e2sumr = square - e2suml
|
---|
373 | esumr -=u*f; // esumr = mean - esuml
|
---|
374 | wr -=u; // wr = tclasspop[0] - wl
|
---|
375 |
|
---|
376 | //-------------------------------------------
|
---|
377 | // resolution
|
---|
378 | //const Double_t rrn=(wr*e2sumr-esumr*esumr)*wr;
|
---|
379 | //const Double_t rrd=(wr-1.)*esumr*esumr;
|
---|
380 |
|
---|
381 | // resolution times n
|
---|
382 | //const Double_t rrn=(wr*e2sumr-esumr*esumr)*wr;
|
---|
383 | //const Double_t rrd=esumr*esumr;
|
---|
384 |
|
---|
385 | // sigma
|
---|
386 | //const Double_t rrn=(e2sumr-esumr*esumr/wr);
|
---|
387 | //const Double_t rrd=(wr-1.);
|
---|
388 |
|
---|
389 | // sigma times n
|
---|
390 | const Double_t rrn=(e2sumr-esumr*esumr/wr);
|
---|
391 | const Double_t rrd=1.;
|
---|
392 |
|
---|
393 | // 1./(n*variance)
|
---|
394 | //const Double_t rrn=1.;
|
---|
395 | //const Double_t rrd=(e2sumr-esumr*esumr/wr);
|
---|
396 | //-------------------------------------------
|
---|
397 |
|
---|
398 | if (datarang[mn+nc]>=datarang[mn+datasort[mn+nsp+1]])
|
---|
399 | continue;
|
---|
400 |
|
---|
401 | //if (TMath::Min(rrd,rld)<=1.0e-5)
|
---|
402 | // continue;
|
---|
403 |
|
---|
404 | const Double_t crit=(rln/rld)+(rrn/rrd);
|
---|
405 | if (!TMath::Finite(crit))
|
---|
406 | continue;
|
---|
407 |
|
---|
408 | if (crit>=critvar) continue;
|
---|
409 |
|
---|
410 | nbestvar=nsp;
|
---|
411 | critvar=crit;
|
---|
412 | }
|
---|
413 |
|
---|
414 | if (critvar>=critmin) continue;
|
---|
415 |
|
---|
416 | msplit=mvar;
|
---|
417 | nbest=nbestvar;
|
---|
418 | critmin=critvar;
|
---|
419 | }
|
---|
420 |
|
---|
421 | decsplit=crit0-critmin;
|
---|
422 |
|
---|
423 | //return critmin>1.0e20 ? 1 : 0;
|
---|
424 | return decsplit<0 ? 1 : 0;
|
---|
425 | }
|
---|
426 |
|
---|
427 | void MRanTree::MoveData(MArrayI &datasort,Int_t ndstart, Int_t ndend,
|
---|
428 | MArrayI &idmove,MArrayI &ncase,Int_t msplit,
|
---|
429 | Int_t nbest,Int_t &ndendl)
|
---|
430 | {
|
---|
431 | // This is the heart of the BuildTree construction. Based on the best split
|
---|
432 | // the data in the part of datasort corresponding to the current node is moved to the
|
---|
433 | // left if it belongs to the left child and right if it belongs to the right child-node.
|
---|
434 | const Int_t numdata = ncase.GetSize();
|
---|
435 | const Int_t mdim = fGiniDec.GetSize();
|
---|
436 |
|
---|
437 | MArrayI tdatasort(numdata);
|
---|
438 |
|
---|
439 | // compute idmove = indicator of case nos. going left
|
---|
440 | for (Int_t nsp=ndstart;nsp<=ndend;nsp++)
|
---|
441 | {
|
---|
442 | const Int_t &nc=datasort[msplit*numdata+nsp];
|
---|
443 | idmove[nc]= nsp<=nbest?1:0;
|
---|
444 | }
|
---|
445 | ndendl=nbest;
|
---|
446 |
|
---|
447 | // shift case. nos. right and left for numerical variables.
|
---|
448 | for(Int_t msh=0;msh<mdim;msh++)
|
---|
449 | {
|
---|
450 | Int_t k=ndstart-1;
|
---|
451 | for (Int_t n=ndstart;n<=ndend;n++)
|
---|
452 | {
|
---|
453 | const Int_t &ih=datasort[msh*numdata+n];
|
---|
454 | if (idmove[ih]==1)
|
---|
455 | tdatasort[++k]=datasort[msh*numdata+n];
|
---|
456 | }
|
---|
457 |
|
---|
458 | for (Int_t n=ndstart;n<=ndend;n++)
|
---|
459 | {
|
---|
460 | const Int_t &ih=datasort[msh*numdata+n];
|
---|
461 | if (idmove[ih]==0)
|
---|
462 | tdatasort[++k]=datasort[msh*numdata+n];
|
---|
463 | }
|
---|
464 |
|
---|
465 | for(Int_t m=ndstart;m<=ndend;m++)
|
---|
466 | datasort[msh*numdata+m]=tdatasort[m];
|
---|
467 | }
|
---|
468 |
|
---|
469 | // compute case nos. for right and left nodes.
|
---|
470 |
|
---|
471 | for(Int_t n=ndstart;n<=ndend;n++)
|
---|
472 | ncase[n]=datasort[msplit*numdata+n];
|
---|
473 | }
|
---|
474 |
|
---|
475 | void MRanTree::BuildTree(MArrayI &datasort,const MArrayI &datarang, const MArrayF &hadtrue,
|
---|
476 | const MArrayI &idclass, MArrayI &bestsplit, MArrayI &bestsplitnext,
|
---|
477 | const MArrayF &tclasspop, const Float_t &tmean, const Float_t &tsquare, const MArrayF &winbag,
|
---|
478 | Int_t ninbag, const int nclass)
|
---|
479 | {
|
---|
480 | // Buildtree consists of repeated calls to two void functions, FindBestSplit and MoveData.
|
---|
481 | // Findbestsplit does just that--it finds the best split of the current node.
|
---|
482 | // MoveData moves the data in the split node right and left so that the data
|
---|
483 | // corresponding to each child node is contiguous.
|
---|
484 | //
|
---|
485 | // buildtree bookkeeping:
|
---|
486 | // ncur is the total number of nodes to date. nodestatus(k)=1 if the kth node has been split.
|
---|
487 | // nodestatus(k)=2 if the node exists but has not yet been split, and =-1 if the node is
|
---|
488 | // terminal. A node is terminal if its size is below a threshold value, or if it is all
|
---|
489 | // one class, or if all the data-values are equal. If the current node k is split, then its
|
---|
490 | // children are numbered ncur+1 (left), and ncur+2(right), ncur increases to ncur+2 and
|
---|
491 | // the next node to be split is numbered k+1. When no more nodes can be split, buildtree
|
---|
492 | // returns.
|
---|
493 | const Int_t mdim = fGiniDec.GetSize();
|
---|
494 | const Int_t nrnodes = fBestSplit.GetSize();
|
---|
495 | const Int_t numdata = (nrnodes-1)/2;
|
---|
496 |
|
---|
497 | MArrayI nodepop(nrnodes);
|
---|
498 | MArrayI nodestart(nrnodes);
|
---|
499 | MArrayI parent(nrnodes);
|
---|
500 |
|
---|
501 | MArrayI ncase(numdata);
|
---|
502 | MArrayI idmove(numdata);
|
---|
503 | MArrayI iv(mdim);
|
---|
504 |
|
---|
505 | MArrayF classpop(nrnodes*nclass);//nclass
|
---|
506 | MArrayI nodestatus(nrnodes);
|
---|
507 |
|
---|
508 | for (Int_t j=0;j<nclass;j++)
|
---|
509 | classpop[j*nrnodes+0]=tclasspop[j];
|
---|
510 |
|
---|
511 | MArrayF mean(nrnodes);
|
---|
512 | MArrayF square(nrnodes);
|
---|
513 | MArrayF lclasspop(tclasspop);
|
---|
514 |
|
---|
515 | mean[0]=tmean;
|
---|
516 | square[0]=tsquare;
|
---|
517 |
|
---|
518 |
|
---|
519 | Int_t ncur=0;
|
---|
520 | nodepop[0]=ninbag;
|
---|
521 | nodestatus[0]=2;
|
---|
522 |
|
---|
523 | // start main loop
|
---|
524 | for (Int_t kbuild=0; kbuild<nrnodes; kbuild++)
|
---|
525 | {
|
---|
526 | if (kbuild>ncur) break;
|
---|
527 | if (nodestatus[kbuild]!=2) continue;
|
---|
528 |
|
---|
529 | // initialize for next call to FindBestSplit
|
---|
530 |
|
---|
531 | const Int_t ndstart=nodestart[kbuild];
|
---|
532 | const Int_t ndend=ndstart+nodepop[kbuild]-1;
|
---|
533 |
|
---|
534 | for (Int_t j=0;j<nclass;j++)
|
---|
535 | lclasspop[j]=classpop[j*nrnodes+kbuild];
|
---|
536 |
|
---|
537 | Int_t msplit, nbest;
|
---|
538 | Float_t decsplit=0;
|
---|
539 |
|
---|
540 | if ((this->*FindBestSplit)(datasort,datarang,hadtrue,idclass,ndstart,
|
---|
541 | ndend, lclasspop,mean[kbuild],square[kbuild],msplit,decsplit,
|
---|
542 | nbest,winbag,nclass))
|
---|
543 | {
|
---|
544 | nodestatus[kbuild]=-1;
|
---|
545 | continue;
|
---|
546 | }
|
---|
547 |
|
---|
548 | fBestVar[kbuild]=msplit;
|
---|
549 | fGiniDec[msplit]+=decsplit;
|
---|
550 |
|
---|
551 | bestsplit[kbuild]=datasort[msplit*numdata+nbest];
|
---|
552 | bestsplitnext[kbuild]=datasort[msplit*numdata+nbest+1];
|
---|
553 |
|
---|
554 | Int_t ndendl;
|
---|
555 | MoveData(datasort,ndstart,ndend,idmove,ncase,
|
---|
556 | msplit,nbest,ndendl);
|
---|
557 |
|
---|
558 | // leftnode no.= ncur+1, rightnode no. = ncur+2.
|
---|
559 | nodepop[ncur+1]=ndendl-ndstart+1;
|
---|
560 | nodepop[ncur+2]=ndend-ndendl;
|
---|
561 | nodestart[ncur+1]=ndstart;
|
---|
562 | nodestart[ncur+2]=ndendl+1;
|
---|
563 |
|
---|
564 | // find class populations in both nodes
|
---|
565 | for (Int_t n=ndstart;n<=ndendl;n++)
|
---|
566 | {
|
---|
567 | const Int_t &nc=ncase[n];
|
---|
568 | const int j=idclass[nc];
|
---|
569 |
|
---|
570 | // statistics left from cut
|
---|
571 | mean[ncur+1]+=hadtrue[nc]*winbag[nc];
|
---|
572 | square[ncur+1]+=hadtrue[nc]*hadtrue[nc]*winbag[nc];
|
---|
573 |
|
---|
574 | // sum of weights left from cut
|
---|
575 | classpop[j*nrnodes+ncur+1]+=winbag[nc];
|
---|
576 | }
|
---|
577 |
|
---|
578 | for (Int_t n=ndendl+1;n<=ndend;n++)
|
---|
579 | {
|
---|
580 | const Int_t &nc=ncase[n];
|
---|
581 | const int j=idclass[nc];
|
---|
582 |
|
---|
583 | // statistics right from cut
|
---|
584 | mean[ncur+2] +=hadtrue[nc]*winbag[nc];
|
---|
585 | square[ncur+2]+=hadtrue[nc]*hadtrue[nc]*winbag[nc];
|
---|
586 |
|
---|
587 | // sum of weights right from cut
|
---|
588 | classpop[j*nrnodes+ncur+2]+=winbag[nc];
|
---|
589 | }
|
---|
590 |
|
---|
591 | // check on nodestatus
|
---|
592 |
|
---|
593 | nodestatus[ncur+1]=2;
|
---|
594 | nodestatus[ncur+2]=2;
|
---|
595 | if (nodepop[ncur+1]<=fNdSize) nodestatus[ncur+1]=-1;
|
---|
596 | if (nodepop[ncur+2]<=fNdSize) nodestatus[ncur+2]=-1;
|
---|
597 |
|
---|
598 |
|
---|
599 | Double_t popt1=0;
|
---|
600 | Double_t popt2=0;
|
---|
601 | for (Int_t j=0;j<nclass;j++)
|
---|
602 | {
|
---|
603 | popt1+=classpop[j*nrnodes+ncur+1];
|
---|
604 | popt2+=classpop[j*nrnodes+ncur+2];
|
---|
605 | }
|
---|
606 |
|
---|
607 | if(fClassify)
|
---|
608 | {
|
---|
609 | // check if only members of one class in node
|
---|
610 | for (Int_t j=0;j<nclass;j++)
|
---|
611 | {
|
---|
612 | if (classpop[j*nrnodes+ncur+1]==popt1) nodestatus[ncur+1]=-1;
|
---|
613 | if (classpop[j*nrnodes+ncur+2]==popt2) nodestatus[ncur+2]=-1;
|
---|
614 | }
|
---|
615 | }
|
---|
616 |
|
---|
617 | fTreeMap1[kbuild]=ncur+1;
|
---|
618 | fTreeMap2[kbuild]=ncur+2;
|
---|
619 | parent[ncur+1]=kbuild;
|
---|
620 | parent[ncur+2]=kbuild;
|
---|
621 | nodestatus[kbuild]=1;
|
---|
622 | ncur+=2;
|
---|
623 | if (ncur>=nrnodes) break;
|
---|
624 | }
|
---|
625 |
|
---|
626 | // determine number of nodes
|
---|
627 | fNumNodes=nrnodes;
|
---|
628 | for (Int_t k=nrnodes-1;k>=0;k--)
|
---|
629 | {
|
---|
630 | if (nodestatus[k]==0) fNumNodes-=1;
|
---|
631 | if (nodestatus[k]==2) nodestatus[k]=-1;
|
---|
632 | }
|
---|
633 |
|
---|
634 | fNumEndNodes=0;
|
---|
635 | for (Int_t kn=0;kn<fNumNodes;kn++)
|
---|
636 | if(nodestatus[kn]==-1)
|
---|
637 | {
|
---|
638 | fNumEndNodes++;
|
---|
639 |
|
---|
640 | Double_t pp=0;
|
---|
641 | for (Int_t j=0;j<nclass;j++)
|
---|
642 | {
|
---|
643 | if(classpop[j*nrnodes+kn]>pp)
|
---|
644 | {
|
---|
645 | // class + status of node kn coded into fBestVar[kn]
|
---|
646 | fBestVar[kn]=j-nclass;
|
---|
647 | pp=classpop[j*nrnodes+kn];
|
---|
648 | }
|
---|
649 | }
|
---|
650 |
|
---|
651 | float sum=0;
|
---|
652 | for(int i=0;i<nclass;i++) sum+=classpop[i*nrnodes+kn];
|
---|
653 |
|
---|
654 | fBestSplit[kn]=mean[kn]/sum;
|
---|
655 | }
|
---|
656 | }
|
---|
657 |
|
---|
658 | Double_t MRanTree::TreeHad(const Float_t *evt)
|
---|
659 | {
|
---|
660 | // to optimize on storage space node status and node class
|
---|
661 | // are coded into fBestVar:
|
---|
662 | // status of node kt = TMath::Sign(1,fBestVar[kt])
|
---|
663 | // class of node kt = fBestVar[kt]+2 (class defined by larger
|
---|
664 | // node population, actually not used)
|
---|
665 | // hadronness assigned to node kt = fBestSplit[kt]
|
---|
666 |
|
---|
667 | // To get rid of the range check of the root classes
|
---|
668 | const Float_t *split = fBestSplit.GetArray();
|
---|
669 | const Int_t *map1 = fTreeMap1.GetArray();
|
---|
670 | const Int_t *map2 = fTreeMap2.GetArray();
|
---|
671 | const Int_t *best = fBestVar.GetArray();
|
---|
672 |
|
---|
673 | Int_t kt=0;
|
---|
674 | for (Int_t k=0; k<fNumNodes; k++)
|
---|
675 | {
|
---|
676 | if (best[kt]<0)
|
---|
677 | break;
|
---|
678 |
|
---|
679 | const Int_t m=best[kt];
|
---|
680 | kt = evt[m]<=split[kt] ? map1[kt] : map2[kt];
|
---|
681 | }
|
---|
682 |
|
---|
683 | return split[kt];
|
---|
684 | }
|
---|
685 |
|
---|
686 | Double_t MRanTree::TreeHad(const TVector &event)
|
---|
687 | {
|
---|
688 | return TreeHad(event.GetMatrixArray());
|
---|
689 | }
|
---|
690 |
|
---|
691 | Double_t MRanTree::TreeHad(const TMatrixFRow_const &event)
|
---|
692 | {
|
---|
693 | return TreeHad(event.GetPtr());
|
---|
694 | }
|
---|
695 |
|
---|
696 | Double_t MRanTree::TreeHad(const TMatrix &m, Int_t ievt)
|
---|
697 | {
|
---|
698 | #if ROOT_VERSION_CODE < ROOT_VERSION(4,00,8)
|
---|
699 | return TreeHad(TMatrixRow(m, ievt));
|
---|
700 | #else
|
---|
701 | return TreeHad(TMatrixFRow_const(m, ievt));
|
---|
702 | #endif
|
---|
703 | }
|
---|
704 |
|
---|
705 | Bool_t MRanTree::AsciiWrite(ostream &out) const
|
---|
706 | {
|
---|
707 | TString str;
|
---|
708 | Int_t k;
|
---|
709 |
|
---|
710 | out.width(5);out<<fNumNodes<<endl;
|
---|
711 |
|
---|
712 | for (k=0;k<fNumNodes;k++)
|
---|
713 | {
|
---|
714 | str=Form("%f",GetBestSplit(k));
|
---|
715 |
|
---|
716 | out.width(5); out << k;
|
---|
717 | out.width(5); out << GetNodeStatus(k);
|
---|
718 | out.width(5); out << GetTreeMap1(k);
|
---|
719 | out.width(5); out << GetTreeMap2(k);
|
---|
720 | out.width(5); out << GetBestVar(k);
|
---|
721 | out.width(15); out << str<<endl;
|
---|
722 | out.width(5); out << GetNodeClass(k);
|
---|
723 | }
|
---|
724 | out<<endl;
|
---|
725 |
|
---|
726 | return k==fNumNodes;
|
---|
727 | }
|
---|