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