Ignore:
Timestamp:
05/05/06 14:01:54 (19 years ago)
Author:
tbretz
Message:
*** empty log message ***
Location:
trunk/MagicSoft/Mars/mjtrain
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/MagicSoft/Mars/mjtrain/MJTrainEnergy.cc

    r7552 r7700  
    107107    MHMatrix train("Train");
    108108    train.AddColumns(fRules);
     109    if (fEnableWeights)
     110        train.AddColumn("MWeight.fVal");
    109111    train.AddColumn("MMcEvt.fImpact/100");
    110112    train.AddColumn("MMcEvt.fTelescopeTheta*kRad2Deg");
     
    120122    fill.AddPreCuts(fPreCuts);
    121123    fill.AddPreCuts(fTrainCuts);
     124    fill.AddPreTasks(fPreTasks);
     125    fill.AddPostTasks(fPostTasks);
    122126    if (!fill.Process())
    123127        return kFALSE;
     
    129133    rf.SetNumTry(fNumTry);
    130134    rf.SetNumObsoleteVariables(3);
     135    rf.SetLastDataColumnHasWeights(fEnableWeights);
    131136    rf.SetDisplay(fDisplay);
    132137    rf.SetLogStream(fLog);
     
    168173    MHEnergyEst hist;
    169174    MFillH fillh(&hist);
     175    if (fEnableWeights)
     176        fillh.SetWeight();
    170177
    171178    tlist.AddToList(&readtst);
     179    tlist.AddToList(fPreTasks);
    172180    tlist.AddToList(&cont);
    173181    tlist.AddToList(&rf);
     182    tlist.AddToList(fPostTasks);
    174183    tlist.AddToList(&fillh);
    175184
  • trunk/MagicSoft/Mars/mjtrain/MJTrainSeparation.cc

    r7678 r7700  
    4747#include "MDataSet.h"
    4848#include "MTFillMatrix.h"
    49 #include "MChisqEval.h"
    5049#include "MStatusDisplay.h"
    5150
     
    5958#include "MContinue.h"
    6059#include "MFillH.h"
     60#include "MSrcPosRndm.h"
     61#include "MHillasCalc.h"
    6162#include "MRanForestCalc.h"
    6263#include "MParameterCalc.h"
     
    201202    gPad->SetGridx();
    202203    gPad->SetGridy();
     204    gr4.SetMinimum(0);
    203205    gr4.SetMarkerStyle(kFullDotMedium);
    204206    gr4.DrawClone("ALP")->SetBit(kCanDelete);
     
    400402    // Target spectrum
    401403    TF1 flx("Flux", "[0]/1000*(x/1000)^(-2.6)", min, max);
    402     flx.SetParameter(0, 2e-7);
     404    flx.SetParameter(0, fFlux);
    403405
    404406    // Number n0 of events this spectrum would produce per s and m^2
     
    430432    *fLog << "Total effective on time:                                  " << data/r  << "s" << endl;
    431433
     434    const Double_t ratio = T*r/data;
     435    *fLog << "Ratio of Monte Carlo to data observation time:            " << ratio << endl;
     436
     437    // 3570.5/43440.2 = 0.082
     438
     439
    432440    // this yields a number of n events to be read for training
    433441    const Double_t n = r*T;                        //[#]
     
    443451    *fLog << "Events available in MC sample:                            " << nummc << endl;
    444452
    445     *fLog << "MC read probability:                                      " << data/n << endl;
     453//    *fLog << "MC read probability:                                      " << data/n << endl;
    446454
    447455    // more data requested than available => Scale down num MC events
     
    503511    {
    504512        *fLog << err << "ERROR - DataSet for testing invalid!" << endl;
     513        return kFALSE;
     514    }
     515
     516    if (fDataSetTrain.IsWobbleMode()!=fDataSetTest.IsWobbleMode())
     517    {
     518        *fLog << err << "ERROR - Train- and Test-DataSet have different observation modes!" << endl;
    505519        return kFALSE;
    506520    }
     
    528542    read4.DisableAutoScheme();
    529543
     544    // Setup four reading tasks with the on- and off-data of the two datasets
    530545    fDataSetTrain.AddFilesOn(read1);
    531546    fDataSetTrain.AddFilesOff(read3);
     
    534549    fDataSetTest.AddFilesOn(read4);
    535550
    536     // ----------------------- Setup RF ----------------------
     551    // ----------------------- Setup RF Matrix ----------------------
    537552    MHMatrix train("Train");
    538553    train.AddColumns(fRules);
     554    if (fEnableWeightsOn || fEnableWeightsOff)
     555        train.AddColumn("MWeight.fVal");
    539556    train.AddColumn("MHadronness.fVal");
    540557
    541558    // ----------------------- Fill Matrix RF ----------------------
    542559
     560    // Setup the hadronness container identifying gammas and off-data
     561    // and setup a container for the weights
    543562    MParameterD had("MHadronness");
    544 
     563    MParameterD wgt("MWeight");
     564
     565    // Add them to the parameter list
    545566    MParList plistx;
    546567    plistx.AddToList(&had);
     568    plistx.AddToList(&wgt);
    547569    plistx.AddToList(this);
    548570
     571    // Setup the tool class to fill the matrix
    549572    MTFillMatrix fill;
    550573    fill.SetLogStream(fLog);
     
    555578    // Set classifier for gammas
    556579    had.SetVal(0);
     580    wgt.SetVal(1);
     581
     582    // Setup the tool class to read the gammas and read them
    557583    fill.SetName("FillGammas");
    558584    fill.SetDestMatrix1(&train, fNumTrainOn);
    559585    fill.SetReader(&read1);
     586    fill.AddPreTasks(fPreTasksOn);
     587    fill.AddPreTasks(fPreTasks);
     588    fill.AddPostTasks(fPostTasksOn);
     589    fill.AddPostTasks(fPostTasks);
    560590    if (!fill.Process(plistx))
    561591        return kFALSE;
    562592
     593    // Check the number or read events
    563594    const Int_t numgammastrn = train.GetNumRows();
    564595    if (numgammastrn==0)
     
    568599    }
    569600
    570     // Set classifier for hadrons
     601    // Remove possible post tasks
     602    fill.ClearPreTasks();
     603    fill.ClearPostTasks();
     604
     605    // Set classifier for background
    571606    had.SetVal(1);
     607    wgt.SetVal(1);
     608
     609    // In case of wobble mode we have to do something special
     610    MSrcPosRndm srcrndm;
     611    srcrndm.SetDistOfSource(0.4);
     612
     613    MHillasCalc hcalc;
     614    hcalc.SetFlags(MHillasCalc::kCalcHillasSrc);
     615
     616    if (fDataSetTrain.IsWobbleMode())
     617    {
     618        fPreTasksOff.AddFirst(&hcalc);
     619        fPreTasksOff.AddFirst(&srcrndm);
     620    }
     621
     622    // Setup the tool class to read the background and read them
    572623    fill.SetName("FillBackground");
    573624    fill.SetDestMatrix1(&train, fNumTrainOff);
    574625    fill.SetReader(&read3);
     626    fill.AddPreTasks(fPreTasksOff);
     627    fill.AddPreTasks(fPreTasks);
     628    fill.AddPostTasks(fPostTasksOff);
     629    fill.AddPostTasks(fPostTasks);
    575630    if (!fill.Process(plistx))
    576631        return kFALSE;
    577632
     633    // Check the number or read events
    578634    const Int_t numbackgrndtrn = train.GetNumRows()-numgammastrn;
    579635    if (numbackgrndtrn==0)
     
    590646    rf.SetNumTry(fNumTry);
    591647    rf.SetNumObsoleteVariables(1);
     648    rf.SetLastDataColumnHasWeights(fEnableWeightsOn || fEnableWeightsOff);
    592649    rf.SetDebug(fDebug);
    593650    rf.SetDisplay(fDisplay);
     
    596653    rf.SetNameOutput("MHadronness");
    597654
     655    // Train the random forest either by classification or regression
    598656    if (fUseRegression)
    599657    {
    600         if (!rf.TrainSingleRF(train))                  // regression
     658        if (!rf.TrainRegression(train)) // regression
    601659            return kFALSE;
    602660    }
    603661    else
    604662    {
    605         MBinning b(2, -0.5, 1.5, "BinningHadronness", "lin");
    606         if (!rf.TrainSingleRF(train, b.GetEdgesD()))   // classification
     663        if (!rf.TrainSingleRF(train))   // classification
    607664            return kFALSE;
    608665    }
    609666
    610     //if (!rf.TrainMultiRF(train, b.GetEdgesD()))    // classification
    611     //    return;
    612 
    613     //fDisplay = rf.GetDisplay();
    614 
    615 
     667    // Output information about what was going on so far.
    616668    *fLog << all;
    617     fLog->Separator("The forest was tested with...");
     669    fLog->Separator("The forest was trained with...");
    618670
    619671    *fLog << "Training method:" << endl;
    620672    *fLog << " * " << (fUseRegression?"regression":"classification") << endl;
     673    if (fEnableWeightsOn)
     674        *fLog << " * weights for on-data" << endl;
     675    if (fEnableWeightsOff)
     676        *fLog << " * weights for off-data" << endl;
     677    if (fDataSetTrain.IsWobbleMode())
     678        *fLog << " * random source position in a distance of 0.4°" << endl;
    621679    *fLog << endl;
    622680    *fLog << "Events used for training:"   << endl;
     
    628686    *fLog << " * Result:     " << (float)numgammastrn/numbackgrndtrn << endl;
    629687
     688    // Chekc if testing is requested
    630689    if (!fDataSetTest.IsValid())
    631690        return kTRUE;
     
    634693    fLog->Separator("Test");
    635694
     695    // Setup parlist and tasklist for testing
    636696    MParList  plist;
    637697    MTaskList tlist;
     
    642702    plist.AddToList(&mcevt);
    643703
     704    plist.AddToList(&wgt);
     705
    644706    // ----- Setup histograms -----
    645707    MBinning binsy(50, 0 , 1,      "BinningMH3Y", "lin");
     
    652714    MH3 h32("MHillas.fSize",  "MHadronness.fVal");
    653715    MH3 h40("MMcEvt.fEnergy", "MHadronness.fVal");
    654     h31.SetTitle("Background probability vs. Size:Size [phe]:Hadronness");
    655     h32.SetTitle("Background probability vs. Size:Size [phe]:Hadronness");
    656     h40.SetTitle("Background probability vs. Energy:Energy [GeV]:Hadronness");
     716    h31.SetTitle("Background probability vs. Size:Size [phe]:Hadronness h");
     717    h32.SetTitle("Background probability vs. Size:Size [phe]:Hadronness h");
     718    h40.SetTitle("Background probability vs. Energy:Energy [GeV]:Hadronness h");
    657719
    658720    MHHadronness hist;
     
    663725    MFillH fillh2(&h32);
    664726    MFillH fillh4(&h40);
     727    fillh0.SetWeight("MWeight");
     728    fillh1.SetWeight("MWeight");
     729    fillh2.SetWeight("MWeight");
     730    fillh4.SetWeight("MWeight");
     731    fillh1.SetDrawOption("colz profy");
     732    fillh2.SetDrawOption("colz profy");
     733    fillh4.SetDrawOption("colz profy");
    665734    fillh1.SetNameTab("Background");
    666735    fillh2.SetNameTab("GammasH");
     
    677746    c0.SetInverted();
    678747
    679     MFEventSelector sel;
     748    MFEventSelector sel; // FIXME: USING IT (WITH PROB?) in READ will by much faster!!!
    680749    sel.SetNumSelectEvts(fNumTestOff);
    681750
     
    685754    // ----- Setup tasklist -----
    686755    tlist.AddToList(&read2);
     756    tlist.AddToList(&c1);
     757    tlist.AddToList(fPreTasksOff);
     758    tlist.AddToList(fPreTasks);
    687759    tlist.AddToList(&c0);
    688     tlist.AddToList(&c1);
    689760    tlist.AddToList(&rf);
     761    tlist.AddToList(fPostTasksOff);
     762    tlist.AddToList(fPostTasks);
    690763    tlist.AddToList(&fillh0);
    691764    tlist.AddToList(&fillh1);
    692765
    693     // ----- Run eventloop on gammas -----
     766    // Enable Acceleration
     767    tlist.SetAccelerator(MTask::kAccDontReset|MTask::kAccDontTime);
     768
     769    // ----- Run eventloop on background -----
    694770    MEvtLoop loop;
    695771    loop.SetDisplay(fDisplay);
     
    697773    loop.SetParList(&plist);
    698774
     775    wgt.SetVal(1);
    699776    if (!loop.Eventloop())
    700777        return kFALSE;
    701778
    702     // ----- Setup and run eventloop on background -----
     779    // ----- Setup and run eventloop on gammas -----
    703780    sel.SetNumSelectEvts(fNumTestOn);
    704781    fillh0.ResetBit(MFillH::kDoNotDisplay);
    705782
     783    // Remove PreTasksOff and PostTasksOff from the list
     784    tlist.RemoveFromList(fPreTasksOff);
     785    tlist.RemoveFromList(fPostTasksOff);
     786
     787    // replace the reading task by a new one
    706788    tlist.Replace(&read4);
     789
     790    // Add the PreTasksOn directly after the reading task
     791    tlist.AddToListAfter(fPreTasksOn, &c1);
     792
     793    // Add the PostTasksOn after rf
     794    tlist.AddToListAfter(fPostTasksOn, &rf);
     795
     796    // Replace fillh1 by fillh2
    707797    tlist.Replace(&fillh2);
     798
     799    // Add fillh4 after the new fillh2
    708800    tlist.AddToListAfter(&fillh4, &fillh2);
    709801
     802    // Enable Acceleration
     803    tlist.SetAccelerator(MTask::kAccDontReset|MTask::kAccDontTime);
     804
     805    wgt.SetVal(1);
    710806    if (!loop.Eventloop())
    711807        return kFALSE;
    712808
     809    // Display the result plots
    713810    DisplayResult(h31, h32);
    714811
     812    // Write the display
    715813    if (!WriteDisplay(out))
    716814        return kFALSE;
     815
     816    // Show what was going on in the testing
     817    const Double_t numgammastst   = h32.GetHist().GetEntries();
     818    const Double_t numbackgrndtst = h31.GetHist().GetEntries();
    717819
    718820    *fLog << all;
    719821    fLog->Separator("The forest was tested with...");
    720 
    721     const Double_t numgammastst   = h32.GetHist().GetEntries();
    722     const Double_t numbackgrndtst = h31.GetHist().GetEntries();
    723 
     822    *fLog << "Test method:" << endl;
     823    *fLog << " * Random Forest: " << out << endl;
     824    if (fEnableWeightsOn)
     825        *fLog << " * weights for on-data" << endl;
     826    if (fEnableWeightsOff)
     827        *fLog << " * weights for off-data" << endl;
     828    if (fDataSetTrain.IsWobbleMode())
     829        *fLog << " * random source position in a distance of 0.4°" << endl;
    724830    *fLog << "Events used for test:"   << endl;
    725831    *fLog << " * Gammas:     " << numgammastst   << endl;
Note: See TracChangeset for help on using the changeset viewer.