Changeset 7420


Ignore:
Timestamp:
11/22/05 11:06:56 (19 years ago)
Author:
tbretz
Message:
*** empty log message ***
Location:
trunk/MagicSoft/Mars
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • trunk/MagicSoft/Mars/Changelog

    r7418 r7420  
    3030   * mfbase/MFEventSelector.cc:
    3131     - also reset fSelRatio if totalnumber of events from file.
     32
     33   * mranforest/MHRanForestGini.[h,cc]:
     34     - added a TPaveText containing text information to output
     35
     36   * mranforest/MRanForest.[h,cc]:
     37     - replaced type of fClassify by Bool_t
     38     - improved handling of allocated memory (still to be checked
     39       further)
     40
     41   * mranforest/MRanForestCalc.[h,cc]:
     42     - added another training type
     43     - added some comments
     44
     45   * mranforest/MRanTree.[h,cc]:
     46     - replaced type of fClassify by Bool_t
     47
     48   * mjtrain/MJTrainDisp.cc, mjtrain/MJTrainEnegry.cc:
     49     - renamed TrainSingleRF to TrainRegression
     50
     51   * mjtrain/MJTrainDisp.cc:
     52     - replaced training variable (hypot(dist,dca)) by dist
     53
     54   * mjtrain/MJTrainRanForest.cc:
     55     - added some comment
     56
     57   * mjtrain/MJTrainSeparation.cc:
     58     - updated output
     59     - made it work properly
    3260
    3361
  • trunk/MagicSoft/Mars/mjtrain/MJTrainDisp.cc

    r7412 r7420  
    115115    MHMatrix train("Train");
    116116    train.AddColumns(fRules);
    117     //train.AddColumn("MHillasSrc.fDist*MGeomCam.fConvMm2Deg");
    118     train.AddColumn("TMath::Hypot(MHillasSrc.fDCA, MHillasSrc.fDist)*MGeomCam.fConvMm2Deg");
     117    train.AddColumn("MHillasSrc.fDist*MGeomCam.fConvMm2Deg");
     118    //train.AddColumn("TMath::Hypot(MHillasSrc.fDCA, MHillasSrc.fDist)*MGeomCam.fConvMm2Deg");
    119119
    120120    // ----------------------- Fill Matrix RF ----------------------
     
    147147        return;
    148148    */
    149     if (!rf.TrainSingleRF(train))                  // regression (best choice)
     149    if (!rf.TrainRegression(train))                  // regression (best choice)
    150150        return kFALSE;
    151151
  • trunk/MagicSoft/Mars/mjtrain/MJTrainEnergy.cc

    r7412 r7420  
    141141        return;
    142142    */
    143     if (!rf.TrainSingleRF(train))                  // regression (best choice)
     143    if (!rf.TrainRegression(train))                  // regression (best choice)
    144144        return kFALSE;
    145145
  • trunk/MagicSoft/Mars/mjtrain/MJTrainRanForest.cc

    r7412 r7420  
    2626//
    2727// MJTrainRanForest
     28//
     29// Base class for classes training a random forest
    2830//
    2931/////////////////////////////////////////////////////////////////////////////
  • trunk/MagicSoft/Mars/mjtrain/MJTrainSeparation.cc

    r7412 r7420  
    3030#include "MJTrainSeparation.h"
    3131
     32#include <TH2.h>
     33#include <TGraph.h>
     34#include <TVirtualPad.h>
     35
    3236#include "MHMatrix.h"
    3337
     
    3640
    3741// tools
     42#include "MMath.h"
    3843#include "MDataSet.h"
    3944#include "MTFillMatrix.h"
    4045#include "MChisqEval.h"
     46#include "MStatusDisplay.h"
    4147
    4248// eventloop
     
    6369// filter
    6470#include "MF.h"
     71#include "MFEventSelector.h"
    6572#include "MFilterList.h"
    6673
     
    6875
    6976using namespace std;
     77
     78void 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}
    70137
    71138Bool_t MJTrainSeparation::Train(const char *out)
     
    83150
    84151    // --------------------- Setup files --------------------
    85     MReadMarsFile read("Events");
     152    MReadMarsFile read1("Events");
    86153    MReadMarsFile read2("Events");
    87154    MReadMarsFile read3("Events");
    88     read.DisableAutoScheme();
     155    MReadMarsFile read4("Events");
     156    read1.DisableAutoScheme();
    89157    read2.DisableAutoScheme();
    90158    read3.DisableAutoScheme();
    91 
    92     fDataSetTrain.AddFilesOn(read);
     159    read4.DisableAutoScheme();
     160
     161    fDataSetTrain.AddFilesOn(read1);
    93162    fDataSetTrain.AddFilesOff(read3);
    94163
    95164    fDataSetTest.AddFilesOff(read2);
    96     fDataSetTest.AddFilesOn(read2);
    97 
    98     read2.VetoBranch("MMcTrig");
    99     read2.VetoBranch("MTime");
    100     read2.VetoBranch("MMcRunHeader");
    101     read2.VetoBranch("MMcTrigHeader");
    102     read2.VetoBranch("MMcFadcHeader");
    103     read2.VetoBranch("MMcCorsikaRunHeader");
    104     read2.VetoBranch("MMcConfigRunHeader");
    105 
     165    fDataSetTest.AddFilesOn(read4);
    106166
    107167    // ----------------------- Setup RF ----------------------
     
    116176    MParList plistx;
    117177    plistx.AddToList(&had);
     178    plistx.AddToList(this);
    118179
    119180    MTFillMatrix fill;
     
    127188    fill.SetName("FillGammas");
    128189    fill.SetDestMatrix1(&train, fNumTrainOn);
    129     fill.SetReader(&read);
     190    fill.SetReader(&read1);
    130191    if (!fill.Process(plistx))
    131192        return kFALSE;
     
    160221        return kFALSE;
    161222
     223    //fDisplay = rf.GetDisplay();
     224
    162225    // --------------------- Display result ----------------------
    163 
    164226    gLog.Separator("Test");
    165227
     
    172234    plist.AddToList(&mcevt);
    173235
     236    // ----- Setup histograms -----
    174237    MBinning binsy(100, 0 , 1,      "BinningMH3Y", "lin");
    175     MBinning binsx(100, 10, 100000, "BinningMH3X", "log");
     238    MBinning binsx( 50, 10, 100000, "BinningMH3X", "log");
    176239
    177240    plist.AddToList(&binsx);
     
    183246    h32.SetTitle("Background probability vs. Size:Size [phe]:Hadronness");
    184247
    185     MF f("MRawRunHeader.fRunType>255");
    186     MFilterList list;
    187     list.SetInverted();
    188     list.AddToList(&f);
    189 
    190248    MHHadronness hist;
    191249
    192     MFillH fillh(&hist, "", "FillHadronness");
    193     MFillH fillh1(&h31, "", "FillGammas");
    194     MFillH fillh2(&h32, "", "FillBackground");
    195     fillh1.SetNameTab("Gammas");
    196     fillh2.SetNameTab("Background");
    197 
    198     fillh1.SetFilter(&f);
    199     fillh2.SetFilter(&list);
    200 
     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 -----
    201259    MFilterList precuts;
    202260    precuts.AddToList(fPreCuts);
     
    204262
    205263    MContinue c0(&precuts);
     264    c0.SetName("PreCuts");
    206265    c0.SetInverted();
    207266
     267    MFEventSelector sel;
     268    sel.SetNumSelectEvts(fNumTestOn);
     269
     270    MContinue c1(&sel);
     271    c1.SetInverted();
     272
     273    // ----- Setup tasklist -----
    208274    tlist.AddToList(&read2);
    209275    tlist.AddToList(&c0);
     276    tlist.AddToList(&c1);
    210277    tlist.AddToList(&rf);
    211     tlist.AddToList(&fillh);
    212     tlist.AddToList(&list);
     278    tlist.AddToList(&fillh0);
    213279    tlist.AddToList(&fillh1);
    214     tlist.AddToList(&fillh2);
    215 
     280
     281    // ----- Run eventloop on gammas -----
    216282    MEvtLoop loop;
    217283    loop.SetDisplay(fDisplay);
     
    222288        return kFALSE;
    223289
     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
    224302    if (!WriteDisplay(out))
    225303        return kFALSE;
  • trunk/MagicSoft/Mars/mjtrain/MJTrainSeparation.h

    r7412 r7420  
    1010#endif
    1111
     12class MH3;
     13
    1214class MJTrainSeparation : public MJTrainRanForest
    1315{
     
    1921    UInt_t fNumTrainOff;
    2022
     23    UInt_t fNumTestOn;
     24    UInt_t fNumTestOff;
     25
     26    void DisplayResult(MH3 &h31, MH3 &h32);
     27
    2128public:
    22     MJTrainSeparation() { }
     29    MJTrainSeparation() :
     30        fNumTrainOn((UInt_t)-1), fNumTrainOff((UInt_t)-1), fNumTestOn((UInt_t)-1), fNumTestOff((UInt_t)-1)
     31    { }
    2332
    24     void SetDataSetTrain(const MDataSet &ds, UInt_t non, UInt_t noff)
     33    void SetDataSetTrain(const MDataSet &ds, UInt_t non=(UInt_t)-1, UInt_t noff=(UInt_t)-1)
    2534    {
    2635        ds.Copy(fDataSetTrain);
     
    2837        fNumTrainOff = noff;
    2938    }
    30     void SetDataSetTest(const MDataSet &ds)
     39    void SetDataSetTest(const MDataSet &ds, UInt_t non=(UInt_t)-1, UInt_t noff=(UInt_t)-1)
    3140    {
    3241        ds.Copy(fDataSetTest);
     42        fNumTestOn = non;
     43        fNumTestOff = noff;
    3344    }
    3445
  • trunk/MagicSoft/Mars/mranforest/MRanForest.cc

    r7417 r7420  
    6262// Default constructor.
    6363//
    64 MRanForest::MRanForest(const char *name, const char *title) : fClassify(1), fNumTrees(100), fNumTry(0), fNdSize(1), fRanTree(NULL), fUserVal(-1)
     64MRanForest::MRanForest(const char *name, const char *title)
     65    : fClassify(kTRUE), fNumTrees(100), fNumTry(0), fNdSize(1),
     66    fRanTree(NULL), fRules(NULL), fMatrix(NULL), fUserVal(-1)
    6567{
    6668    fName  = name  ? name  : "MRanForest";
     
    125127{
    126128    delete fForest;
     129    if (fMatrix)
     130        delete fMatrix;
     131    if (fRules)
     132        delete fRules;
    127133}
    128134
     
    152158void MRanForest::SetWeights(const TArrayF &weights)
    153159{
    154     const int n=weights.GetSize();
    155     fWeight.Set(n);
    156160    fWeight=weights;
    157 
    158     return;
    159161}
    160162
     
    175177    //for(int i=0;i<n;i++)
    176178    //    *fLog<<inf<<" "<<i<<") "<<fGrid[i]<<endl;
    177 
    178     return;
    179179}
    180180
     
    227227}
    228228
    229 /*
    230 Bool_t MRanForest::PreProcess(MParList *plist)
    231 {
    232     if (!fRules)
    233     {
    234         *fLog << err << dbginf << "MDataArray with rules not initialized... aborting." << endl;
    235         return kFALSE;
    236     }
    237 
    238     if (!fRules->PreProcess(plist))
    239     {
    240         *fLog << err << dbginf << "PreProcessing of MDataArray failed... aborting." << endl;
    241         return kFALSE;
    242     }
    243 
    244     return kTRUE;
    245 }
    246 */
    247 
    248229Double_t MRanForest::CalcHadroness()
    249230{
     
    285266    // access matrix, copy last column (target) preliminarily
    286267    // into fHadTrue
     268    if (fMatrix)
     269        delete fMatrix;
    287270    fMatrix = new TMatrix(mat->GetM());
    288271
     
    337320    }
    338321
     322    if (fRules)
     323        delete fRules;
    339324    fRules = new MDataArray();
    340325    fRules->Reset();
  • trunk/MagicSoft/Mars/mranforest/MRanForest.h

    r7396 r7420  
    3030{
    3131private:
    32     Int_t fClassify;
     32    Bool_t fClassify;
    3333
    3434    Int_t fNumTrees;       // Number of trees
     
    8383    void SetNdSize(Int_t n);
    8484
    85     void SetClassify(Int_t n){ fClassify=n; }
     85    void SetClassify(Bool_t n){ fClassify=n; }
    8686    void PrepareClasses();
    8787
  • trunk/MagicSoft/Mars/mranforest/MRanForestCalc.cc

    r7413 r7420  
    7979}
    8080
     81// --------------------------------------------------------------------------
     82//
     83// ver=0: One yes/no-classification forest is trained for each bin.
     84//        the yes/no classification is done using the grid
     85// ver=1: One classification forest is trained. The last column contains a
     86//        value which is turned into a classifier by rf itself using the grid
     87// ver=2: One classification forest is trained. The last column already contains
     88//        the classifier
     89// ver=3: A regression forest is trained. The last column contains the
     90//        classifier
     91//
    8192Int_t MRanForestCalc::Train(const MHMatrix &matrixtrain, const TArrayD &grid, Int_t ver)
    8293{
     
    117128
    118129    MDataArray rules(usedrules);
    119     rules.AddEntry(ver<2?"Classification":dcol[ncols-1].GetRule());
     130    rules.AddEntry(ver<3?"Classification":dcol[ncols-1].GetRule());
    120131
    121132    // prepare matrix for current energy bin
     
    133144        switch (ver)
    134145        {
    135         case 0: // Replace Energy Grid by classification
     146        case 0: // Replace last column by a classification
    136147            {
    137148                Int_t irows=0;
     
    158169            break;
    159170
    160         case 1: // Use Energy as classifier
     171        case 1: // Use last column as classifier or for regression
    161172        case 2:
     173        case 3:
    162174            for (Int_t j=0; j<nrows; j++)
    163175                mat(j, ncols-nobs) = matrixtrain.GetM()(j,ncols-1);
     
    176188        rf.SetNumTry(fNumTry);
    177189        rf.SetNdSize(fNdSize);
    178         rf.SetClassify(ver<2 ? 1 : 0);
     190        rf.SetClassify(ver<3 ? kTRUE : kFALSE);
    179191        if (ver==1)
    180192            rf.SetGrid(grid);
  • trunk/MagicSoft/Mars/mranforest/MRanForestCalc.h

    r7413 r7420  
    6565
    6666    // Train Interface
    67     Int_t Train(const MHMatrix &n, const TArrayD &grid, Int_t ver=2);
     67    Int_t Train(const MHMatrix &n, const TArrayD &grid, Int_t ver);
    6868
    6969public:
     
    8787    Int_t TrainMultiRF(const MHMatrix &n, const TArrayD &grid)
    8888    {
     89        // One yes/no-classification forest is trained for each bin
    8990        return Train(n, grid, 0);
    9091    }
    9192    Int_t TrainSingleRF(const MHMatrix &n, const TArrayD &grid=TArrayD())
    9293    {
     94        // w/o Grid: Last Column contains classifier
     95        // w/  Grid: Last Column will be converted by grid into classifier
    9396        return Train(n, grid, grid.GetSize()==0 ? 2 : 1);
     97    }
     98    Int_t TrainRegression(const MHMatrix &n)
     99    {
     100        // Use last column for regression
     101        return Train(n, TArrayD(), 3);
    94102    }
    95103
  • trunk/MagicSoft/Mars/mranforest/MRanTree.cc

    r7396 r7420  
    4949// Default constructor.
    5050//
    51 MRanTree::MRanTree(const char *name, const char *title):fClassify(1),fNdSize(0), fNumTry(3)
     51MRanTree::MRanTree(const char *name, const char *title):fClassify(kTRUE),fNdSize(0), fNumTry(3)
    5252{
    5353
  • trunk/MagicSoft/Mars/mranforest/MRanTree.h

    r7413 r7420  
    2222{
    2323private:
    24     Int_t fClassify;
     24    Bool_t fClassify;
     25
    2526    Int_t fNdSize;
    2627    Int_t fNumTry;
     
    8889    Float_t GetGiniDec(Int_t i)  const { return fGiniDec.At(i); }
    8990
    90     void SetClassify(Int_t n){ fClassify=n; }
     91    void SetClassify(Bool_t n){ fClassify=n; }
    9192
    9293    // functions used in tree growing process
Note: See TracChangeset for help on using the changeset viewer.