source: trunk/MagicSoft/Mars/manalysis/MRanTree.cc@ 1859

Last change on this file since 1859 was 1859, checked in by hengsteb, 22 years ago
*** empty log message ***
File size: 15.3 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 Hengstebeck 3/2003 <mailto:hengsteb@alwa02.physik.uni-siegen.de>
19!
20! Copyright: MAGIC Software Development, 2000-2003
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26// //
27// MRanTree //
28// //
29// ParameterContainer for Tree structure //
30// //
31// //
32/////////////////////////////////////////////////////////////////////////////
33#include "MRanTree.h"
34#include <iostream>
35ClassImp(MRanTree);
36
37// --------------------------------------------------------------------------
38//
39// Default constructor.
40//
41MRanTree::MRanTree(const char *name, const char *title):fData(NULL)
42{
43 fName = name ? name : "MRanTree";
44 fTitle = title ? title : "Storage container for structure of a single tree and additional information";
45}
46
47void MRanTree::SetNdSize(Int_t n)
48{
49 // minimum nodesize of terminal nodes
50 fNdSize=TMath::Max(1,n);//at least 1 event per node
51
52 return;
53}
54
55void MRanTree::SetNumTry(Int_t n)
56{
57 // number of trials in random split selection:
58 // choose at least 1 variable to split in...
59 fNumTry=TMath::Max(1,n);
60 // and not more candidates than available
61 if(fData)
62 fNumTry=TMath::Min(fData->GetNumEntries(),n);
63
64 return;
65}
66
67void MRanTree::GrowTree(TMatrix &mhad,TMatrix &mgam,Int_t numdata, Int_t numdim,TArrayI &hadtrue,TArrayI &datasort,
68 TArrayI &datarang,TArrayF &ginidec,TArrayF &tclasspop,TArrayI &jinbag,
69 TArrayF &winbag,TArrayF &weight,TRandom &rand)
70{
71 //Int_t nrnodes=2*(numdata/fNdSize)+1;
72 Int_t nrnodes=2*numdata+1;
73 Int_t ninbag=0;
74 for (Int_t n=0;n<numdata;n++)
75 if(jinbag[n]==1) ninbag++;
76
77
78 TArrayI bestsplit(nrnodes);
79 TArrayF wl(2);
80 TArrayF wc(2);
81 TArrayF wr(2);
82 TArrayI nc(2);
83
84 TArrayI bestsplitnext(nrnodes);
85 TArrayI nodepop(nrnodes);
86 TArrayI parent(nrnodes);
87 TArrayI nodex(numdata);
88 TArrayI nodestart(nrnodes);
89
90 TArrayI ncase(numdata);
91 TArrayI iv(numdim);
92 TArrayI idmove(numdata);
93
94 idmove.Reset();
95
96 fBestVar.Set(nrnodes);
97 fTreeMap1.Set(nrnodes);
98 fTreeMap2.Set(nrnodes);
99 fBestSplit.Set(nrnodes);
100
101 fTreeMap1.Reset();
102 fTreeMap2.Reset();
103 fBestSplit.Reset();
104
105 // tree growing
106 BuildTree(datasort,datarang,hadtrue,numdim,numdata,bestsplit,
107 bestsplitnext,ginidec,nodepop,nodestart,tclasspop,nrnodes,
108 idmove,ncase,parent,jinbag,iv,winbag,wr,wc,wl,ninbag,rand);
109
110 Int_t nhad=mhad.GetNrows();
111
112 for(Int_t k=0;k<nrnodes;k++)
113 {
114 Int_t bsp=bestsplit[k];
115 Int_t bspn=bestsplitnext[k];
116 Int_t msp=fBestVar[k];
117
118 if (GetNodeStatus(k)!=-1)
119 {
120 fBestSplit[k] = bsp<nhad ? mhad(bsp,msp):mgam(bsp-nhad,msp);
121 fBestSplit[k] += bspn<nhad ? mhad(bspn,msp):mgam(bspn-nhad,msp);
122 fBestSplit[k] /=2.;
123 }
124 }
125
126 fBestVar.Set(fNumNodes);
127 fTreeMap1.Set(fNumNodes);
128 fTreeMap2.Set(fNumNodes);
129 fBestSplit.Set(fNumNodes);
130
131 return;
132}
133
134Int_t MRanTree::FindBestSplit(TArrayI &datasort,TArrayI &datarang,TArrayI &hadtrue,Int_t mdim,
135 Int_t numdata,Int_t ndstart,Int_t ndend,TArrayF &tclasspop,
136 Int_t &msplit,Float_t &decsplit,Int_t &nbest,TArrayI &ncase,
137 TArrayI &jinbag,TArrayI &iv,TArrayF &winbag,TArrayF &wr,
138 TArrayF &wc,TArrayF &wl,Int_t kbuild,TRandom &rand)
139{
140 // For the best split, msplit is the variable split on. decsplit is the dec. in impurity.
141 // nsplit is the case number of value of msplit split on,
142 // and nsplitnext is the case number of the next larger value of msplit.
143
144 Int_t mvar,nc,nbestvar=0,jstat,k;
145 Float_t crit,crit0,critmax,critvar=0;
146 Float_t rrn, rrd, rln, rld, u;
147
148 // compute initial values of numerator and denominator of Gini
149 Float_t pno=0;
150 Float_t pdo=0;
151
152 for (Int_t j=0;j<2;j++)
153 {
154 pno+=tclasspop[j]*tclasspop[j];
155 pdo+=tclasspop[j];
156 }
157 crit0=pno/pdo;
158 jstat=0;
159
160 // start main loop through variables to find best split
161
162 critmax=-1.0e20;
163 for(Int_t mt=0;mt<fNumTry;mt++)
164 {
165 mvar=Int_t(mdim*rand.Rndm());
166
167 rrn=pno;
168 rrd=pdo;
169 rln=0;
170 rld=0;
171 wl.Reset();
172
173 for (Int_t j=0;j<2;j++)
174 {
175 wr[j]=tclasspop[j];
176 }
177
178 critvar=-1.0e20;
179
180 for(Int_t nsp=ndstart;nsp<=ndend-1;nsp++)
181 {
182 nc=datasort[mvar*numdata+nsp];
183
184 u=winbag[nc];
185 k=hadtrue[nc];
186
187 rln=rln+u*(2*wl[k]+u);
188 rrn=rrn+u*(-2*wr[k]+u);
189 rld=rld+u;
190 rrd=rrd-u;
191
192 wl[k]=wl[k]+u;
193 wr[k]=wr[k]-u;
194
195 if (datarang[mvar*numdata+nc]<datarang[mvar*numdata+datasort[mvar*numdata+nsp+1]])
196 {
197 if(TMath::Min(rrd,rld)>1.0e-5)
198 {
199 crit=(rln/rld)+(rrn/rrd);
200 if (crit>critvar)
201 {
202 nbestvar=nsp;
203 critvar=crit;
204 }
205 }
206 }
207 }
208
209 if (critvar>critmax) {
210 msplit=mvar;
211 nbest=nbestvar;
212 critmax=critvar;
213 }
214 }
215
216 decsplit=critmax-crit0;
217 if (critmax<-1.0e10) jstat=1;
218
219 return jstat;
220}
221
222void MRanTree::MoveData(TArrayI &datasort,Int_t mdim,Int_t numdata,Int_t ndstart,
223 Int_t ndend,TArrayI &idmove,TArrayI &ncase,Int_t msplit,
224 Int_t nbest,Int_t &ndendl)
225{
226 // This is the heart of the BuildTree construction. Based on the best split
227 // the data in the part of datasort corresponding to the current node is moved to the
228 // left if it belongs to the left child and right if it belongs to the right child-node.
229
230 Int_t nc,k,ih;
231 TArrayI tdatasort(numdata);
232
233 // compute idmove = indicator of case nos. going left
234
235 for (Int_t nsp=ndstart;nsp<=nbest;nsp++)
236 {
237 nc=datasort[msplit*numdata+nsp];
238 idmove[nc]=1;
239 }
240 for (Int_t nsp=nbest+1;nsp<=ndend;nsp++)
241 {
242 nc=datasort[msplit*numdata+nsp];
243 idmove[nc]=0;
244 }
245 ndendl=nbest;
246
247 // shift case. nos. right and left for numerical variables.
248
249 for(Int_t msh=0;msh<mdim;msh++)
250 {
251 k=ndstart-1;
252 for (Int_t n=ndstart;n<=ndend;n++)
253 {
254 ih=datasort[msh*numdata+n];
255 if (idmove[ih]==1) {
256 k++;
257 tdatasort[k]=datasort[msh*numdata+n];
258 }
259 }
260
261 for (Int_t n=ndstart;n<=ndend;n++)
262 {
263
264 ih=datasort[msh*numdata+n];
265 if (idmove[ih]==0)
266 {
267 k++;
268 tdatasort[k]=datasort[msh*numdata+n];
269 }
270 }
271 for(Int_t k=ndstart;k<=ndend;k++)
272 {
273 datasort[msh*numdata+k]=tdatasort[k];
274 }
275 }
276
277 // compute case nos. for right and left nodes.
278
279 for(Int_t n=ndstart;n<=ndend;n++)
280 {
281 ncase[n]=datasort[msplit*numdata+n];
282 }
283
284 return;
285}
286
287void MRanTree::BuildTree(TArrayI &datasort,TArrayI &datarang,TArrayI &hadtrue,Int_t mdim,
288 Int_t numdata,TArrayI &bestsplit,TArrayI &bestsplitnext,
289 TArrayF &ginidec,TArrayI &nodepop,TArrayI &nodestart,TArrayF &tclasspop,
290 Int_t nrnodes,TArrayI &idmove,TArrayI &ncase,TArrayI &parent,
291 TArrayI &jinbag,TArrayI &iv,TArrayF &winbag,TArrayF &wr,TArrayF &wc,
292 TArrayF &wl,Int_t ninbag,TRandom &rand)
293{
294 // Buildtree consists of repeated calls to two void functions, FindBestSplit and MoveData.
295 // Findbestsplit does just that--it finds the best split of the current node.
296 // MoveData moves the data in the split node right and left so that the data
297 // corresponding to each child node is contiguous.
298 //
299 // buildtree bookkeeping:
300 // ncur is the total number of nodes to date. nodestatus(k)=1 if the kth node has been split.
301 // nodestatus(k)=2 if the node exists but has not yet been split, and =-1 if the node is
302 // terminal. A node is terminal if its size is below a threshold value, or if it is all
303 // one class, or if all the data-values are equal. If the current node k is split, then its
304 // children are numbered ncur+1 (left), and ncur+2(right), ncur increases to ncur+2 and
305 // the next node to be split is numbered k+1. When no more nodes can be split, buildtree
306 // returns.
307
308 Int_t msplit,nbest,ndendl,nc,jstat,ndend,ndstart;
309 Float_t decsplit=0;
310 Float_t popt1,popt2,pp;
311 TArrayF classpop;
312 TArrayI nodestatus;
313
314 nodestatus.Set(nrnodes);
315 classpop.Set(2*nrnodes);
316
317 nodestatus.Reset();
318 nodestart.Reset();
319 nodepop.Reset();
320 classpop.Reset();
321
322
323 for (Int_t j=0;j<2;j++)
324 classpop[j*nrnodes+0]=tclasspop[j];
325
326 Int_t ncur=0;
327 nodestart[0]=0;
328 nodepop[0]=ninbag;
329 nodestatus[0]=2;
330
331 // start main loop
332 for (Int_t kbuild=0;kbuild<nrnodes;kbuild++)
333 {
334 if (kbuild>ncur) break;
335 if (nodestatus[kbuild]!=2) continue;
336
337 // initialize for next call to FindBestSplit
338
339 ndstart=nodestart[kbuild];
340 ndend=ndstart+nodepop[kbuild]-1;
341 for (Int_t j=0;j<2;j++)
342 {
343 tclasspop[j]=classpop[j*nrnodes+kbuild];
344 }
345
346 jstat=FindBestSplit(datasort,datarang,hadtrue,mdim,numdata,
347 ndstart,ndend,tclasspop,msplit,decsplit,
348 nbest,ncase,jinbag,iv,winbag,wr,wc,wl,
349 kbuild,rand);
350
351 if(jstat==1) {
352 nodestatus[kbuild]=-1;
353 continue;
354 }else{
355 fBestVar[kbuild]=msplit;
356 ginidec[msplit]+=decsplit;
357
358 bestsplit[kbuild]=datasort[msplit*numdata+nbest];
359 bestsplitnext[kbuild]=datasort[msplit*numdata+nbest+1];
360 }
361
362 MoveData(datasort,mdim,numdata,ndstart,ndend,idmove,ncase,
363 msplit,nbest,ndendl);
364
365 // leftnode no.= ncur+1, rightnode no. = ncur+2.
366
367 nodepop[ncur+1]=ndendl-ndstart+1;
368 nodepop[ncur+2]=ndend-ndendl;
369 nodestart[ncur+1]=ndstart;
370 nodestart[ncur+2]=ndendl+1;
371
372 // find class populations in both nodes
373
374 for (Int_t n=ndstart;n<=ndendl;n++)
375 {
376 nc=ncase[n];
377 Int_t j=hadtrue[nc];
378 classpop[j*nrnodes+ncur+1]+=winbag[nc];
379 }
380
381 for (Int_t n=ndendl+1;n<=ndend;n++)
382 {
383 nc=ncase[n];
384 Int_t j=hadtrue[nc];
385 classpop[j*nrnodes+ncur+2]+=winbag[nc];
386
387 }
388
389 // check on nodestatus
390
391 nodestatus[ncur+1]=2;
392 nodestatus[ncur+2]=2;
393 if (nodepop[ncur+1]<=fNdSize) nodestatus[ncur+1]=-1;
394 if (nodepop[ncur+2]<=fNdSize) nodestatus[ncur+2]=-1;
395 popt1=0;
396 popt2=0;
397 for (Int_t j=0;j<2;j++)
398 {
399 popt1+=classpop[j*nrnodes+ncur+1];
400 popt2+=classpop[j*nrnodes+ncur+2];
401 }
402
403 for (Int_t j=0;j<2;j++)
404 {
405 if (classpop[j*nrnodes+ncur+1]==popt1) nodestatus[ncur+1]=-1;
406 if (classpop[j*nrnodes+ncur+2]==popt2) nodestatus[ncur+2]=-1;
407 }
408
409 fTreeMap1[kbuild]=ncur+1;
410 fTreeMap2[kbuild]=ncur+2;
411 parent[ncur+1]=kbuild;
412 parent[ncur+2]=kbuild;
413 nodestatus[kbuild]=1;
414 ncur+=2;
415 if (ncur>=nrnodes) break;
416 }
417
418 // determine number of nodes
419 fNumNodes=nrnodes;
420 for (Int_t k=nrnodes-1;k>=0;k--)
421 {
422 if (nodestatus[k]==0) fNumNodes-=1;
423 if (nodestatus[k]==2) nodestatus[k]=-1;
424 }
425
426 fNumEndNodes=0;
427 for (Int_t kn=0;kn<fNumNodes;kn++)
428 {
429 if(nodestatus[kn]==-1)
430 {
431 fNumEndNodes++;
432 pp=0;
433 for (Int_t j=0;j<2;j++)
434 {
435 if(classpop[j*nrnodes+kn]>pp)
436 {
437 // class + status of node kn coded into fBestVar[kn]
438 fBestVar[kn]=j-2;
439 pp=classpop[j*nrnodes+kn];
440 }
441 }
442 fBestSplit[kn] =classpop[1*nrnodes+kn];
443 fBestSplit[kn]/=(classpop[0*nrnodes+kn]+classpop[1*nrnodes+kn]);
444 }
445 }
446 return;
447}
448
449void MRanTree::SetRules(MDataArray *rules)
450{
451 fData=rules;
452
453 return;
454}
455
456Double_t MRanTree::TreeHad(TVector &event)
457{
458 Int_t kt=0;
459 // to optimize on storage space node status and node class
460 // are coded into fBestVar:
461 // status of node kt = TMath::Sign(1,fBestVar[kt])
462 // hadronness assigned to node kt = fBestSplit[kt]
463
464 for (Int_t k=0;k<fNumNodes;k++)
465 {
466 if (fBestVar[kt]<0)
467 break;
468
469 Int_t m=fBestVar[kt];
470 if (event(m)<=fBestSplit[kt])
471 {
472 kt=fTreeMap1[kt];
473 }else{
474 kt=fTreeMap2[kt];
475 }
476 }
477 return fBestSplit[kt];
478}
479
480Double_t MRanTree::TreeHad()
481{
482 const Double_t ncols = fData->GetNumEntries();
483 TVector event(ncols);
484
485 for (int i=0; i<fData->GetNumEntries(); i++)
486 event(i) = (*fData)(i);
487
488 Int_t kt=0;
489 // to optimize on storage space node status and node class
490 // are coded into fBestVar:
491 // status of node kt = TMath::Sign(1,fBestVar[kt])
492 // class of node kt = fBestVar[kt]+2 (class defined by larger
493 // node population, actually not used)
494 // hadronness assigned to node kt = fBestSplit[kt]
495
496 for (Int_t k=0;k<fNumNodes;k++)
497 {
498 if (fBestVar[kt]<0)
499 break;
500
501 Int_t m=fBestVar[kt];
502 if (event(m)<=fBestSplit[kt])
503 {
504 kt=fTreeMap1[kt];
505 }else{
506 kt=fTreeMap2[kt];
507 }
508 }
509 return fBestSplit[kt];
510}
511
512Bool_t MRanTree::AsciiWrite(ostream &out) const
513{
514 TString str;
515 Int_t k;
516
517 out.width(5);out<<fNumNodes<<endl;
518
519 for (k=0;k<fNumNodes;k++)
520 {
521 str=Form("%f",GetBestSplit(k));
522
523 out.width(5); out << k;
524 out.width(5); out << GetNodeStatus(k);
525 out.width(5); out << GetTreeMap1(k);
526 out.width(5); out << GetTreeMap2(k);
527 out.width(5); out << GetBestVar(k);
528 out.width(15); out << str<<endl;
529 out.width(5); out << GetNodeClass(k);
530 }
531 out<<endl;
532
533 return k==fNumNodes;
534}
Note: See TracBrowser for help on using the repository browser.