| 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 Bretz 11/2005 <mailto:tbretz@astro.uni-wuerzburg.de> | 
|---|
| 19 | ! | 
|---|
| 20 | !   Copyright: MAGIC Software Development, 2005 | 
|---|
| 21 | ! | 
|---|
| 22 | ! | 
|---|
| 23 | \* ======================================================================== */ | 
|---|
| 24 |  | 
|---|
| 25 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| 26 | // | 
|---|
| 27 | // MJTrainEnergy | 
|---|
| 28 | // | 
|---|
| 29 | // | 
|---|
| 30 | // Example: | 
|---|
| 31 | // -------- | 
|---|
| 32 | // | 
|---|
| 33 | //    // SequencesOn are used for training, SequencesOff for Testing | 
|---|
| 34 | //    MDataSet set("mctesttrain.txt"); | 
|---|
| 35 | //    set.SetNumAnalysis(1);  // Must have a number | 
|---|
| 36 | //    MJTrainEnergy opt; | 
|---|
| 37 | //    //opt.SetDebug(); | 
|---|
| 38 | //    opt.AddParameter("MHillas.fSize"); | 
|---|
| 39 | //    opt.AddParameter("MHillasSrc.fDist"); | 
|---|
| 40 | //    MStatusDisplay *d = new MStatusDisplay; | 
|---|
| 41 | //    opt.SetDisplay(d); | 
|---|
| 42 | //    opt.AddPreCut("MHillasSrc.fDCA*MGeomCam.fConvMm2Deg<0.3"); | 
|---|
| 43 | //    opt.Train("rf-energy.root", set, 30000); // Number of train events | 
|---|
| 44 | // | 
|---|
| 45 | // Random Numbers: | 
|---|
| 46 | // --------------- | 
|---|
| 47 | //   Use: | 
|---|
| 48 | //         if(gRandom) | 
|---|
| 49 | //             delete gRandom; | 
|---|
| 50 | //         gRandom = new TRandom3(); | 
|---|
| 51 | //   in advance to change the random number generator. | 
|---|
| 52 | // | 
|---|
| 53 | //////////////////////////////////////////////////////////////////////////// | 
|---|
| 54 | #include "MJTrainEnergy.h" | 
|---|
| 55 |  | 
|---|
| 56 | #include "MHMatrix.h" | 
|---|
| 57 |  | 
|---|
| 58 | #include "MLog.h" | 
|---|
| 59 | #include "MLogManip.h" | 
|---|
| 60 |  | 
|---|
| 61 | // tools | 
|---|
| 62 | #include "MDataSet.h" | 
|---|
| 63 | #include "MTFillMatrix.h" | 
|---|
| 64 | #include "MStatusDisplay.h" | 
|---|
| 65 |  | 
|---|
| 66 | // eventloop | 
|---|
| 67 | #include "MParList.h" | 
|---|
| 68 | #include "MTaskList.h" | 
|---|
| 69 | #include "MEvtLoop.h" | 
|---|
| 70 |  | 
|---|
| 71 | // tasks | 
|---|
| 72 | #include "MReadMarsFile.h" | 
|---|
| 73 | #include "MContinue.h" | 
|---|
| 74 | #include "MFillH.h" | 
|---|
| 75 | #include "MRanForestCalc.h" | 
|---|
| 76 |  | 
|---|
| 77 | // histograms | 
|---|
| 78 | #include "MHEnergyEst.h" | 
|---|
| 79 |  | 
|---|
| 80 | // filter | 
|---|
| 81 | #include "MFilterList.h" | 
|---|
| 82 |  | 
|---|
| 83 | ClassImp(MJTrainEnergy); | 
|---|
| 84 |  | 
|---|
| 85 | using namespace std; | 
|---|
| 86 |  | 
|---|
| 87 | Bool_t MJTrainEnergy::Train(const char *out, const MDataSet &set, Int_t num) | 
|---|
| 88 | { | 
|---|
| 89 | SetTitle(Form("TrainEnergy: %s", out)); | 
|---|
| 90 |  | 
|---|
| 91 | if (fDisplay) | 
|---|
| 92 | fDisplay->SetTitle(fTitle); | 
|---|
| 93 |  | 
|---|
| 94 | if (!set.IsValid()) | 
|---|
| 95 | { | 
|---|
| 96 | *fLog << err << "ERROR - DataSet invalid!" << endl; | 
|---|
| 97 | return kFALSE; | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | if (!HasWritePermission(out)) | 
|---|
| 101 | return kFALSE; | 
|---|
| 102 |  | 
|---|
| 103 | *fLog << inf; | 
|---|
| 104 | fLog->Separator(GetDescriptor()); | 
|---|
| 105 |  | 
|---|
| 106 | // --------------------- Setup files -------------------- | 
|---|
| 107 | MReadMarsFile readtrn("Events"); | 
|---|
| 108 | MReadMarsFile readtst("Events"); | 
|---|
| 109 | readtrn.DisableAutoScheme(); | 
|---|
| 110 | readtst.DisableAutoScheme(); | 
|---|
| 111 |  | 
|---|
| 112 | if (!set.AddFilesOn(readtrn)) | 
|---|
| 113 | { | 
|---|
| 114 | *fLog << err << "ERROR - Adding SequencesOn." << endl; | 
|---|
| 115 | return kFALSE; | 
|---|
| 116 | } | 
|---|
| 117 | if (!set.AddFilesOff(readtst)) | 
|---|
| 118 | { | 
|---|
| 119 | *fLog << err << "ERROR - Adding SequencesOff." << endl; | 
|---|
| 120 | return kFALSE; | 
|---|
| 121 | } | 
|---|
| 122 |  | 
|---|
| 123 | // ----------------------- Setup Matrix ------------------ | 
|---|
| 124 | MHMatrix train("Train"); | 
|---|
| 125 | train.AddColumns(fRules); | 
|---|
| 126 | if (fEnableWeights) | 
|---|
| 127 | train.AddColumn("MWeight.fVal"); | 
|---|
| 128 | train.AddColumn("MMcEvt.fImpact/100"); | 
|---|
| 129 | train.AddColumn("MMcEvt.fTelescopeTheta*TMath::RadToDeg()"); | 
|---|
| 130 | train.AddColumn("MMcEvt.fEnergy"); | 
|---|
| 131 |  | 
|---|
| 132 |  | 
|---|
| 133 | // ----------------------- Fill Matrix RF ---------------------- | 
|---|
| 134 | MTFillMatrix fill(fTitle); | 
|---|
| 135 | fill.SetDisplay(fDisplay); | 
|---|
| 136 | fill.SetLogStream(fLog); | 
|---|
| 137 | fill.SetDestMatrix1(&train, num); | 
|---|
| 138 | fill.SetReader(&readtrn); | 
|---|
| 139 | fill.AddPreCuts(fPreCuts); | 
|---|
| 140 | fill.AddPreCuts(fTrainCuts); | 
|---|
| 141 | fill.AddPreTasks(fPreTasks); | 
|---|
| 142 | fill.AddPostTasks(fPostTasks); | 
|---|
| 143 | if (!fill.Process()) | 
|---|
| 144 | return kFALSE; | 
|---|
| 145 |  | 
|---|
| 146 | // ------------------------ Train RF -------------------------- | 
|---|
| 147 | MRanForestCalc rf("TrainEnergy", fTitle); | 
|---|
| 148 | rf.SetNumTrees(fNumTrees); | 
|---|
| 149 | rf.SetNdSize(fNdSize); | 
|---|
| 150 | rf.SetNumTry(fNumTry); | 
|---|
| 151 | rf.SetNumObsoleteVariables(3); | 
|---|
| 152 | rf.SetLastDataColumnHasWeights(fEnableWeights); | 
|---|
| 153 | rf.SetDisplay(fDisplay); | 
|---|
| 154 | rf.SetLogStream(fLog); | 
|---|
| 155 | rf.SetFileName(out); | 
|---|
| 156 | rf.SetDebug(fDebug>1); | 
|---|
| 157 | rf.SetNameOutput("MEnergyEst"); | 
|---|
| 158 |  | 
|---|
| 159 | /* | 
|---|
| 160 | MBinning b(32, 10, 100000, "BinningEnergyEst", "log"); | 
|---|
| 161 |  | 
|---|
| 162 | if (!rf.TrainMultiRF(train, b.GetEdgesD()))    // classification with one tree per bin | 
|---|
| 163 | return; | 
|---|
| 164 |  | 
|---|
| 165 | if (!rf.TrainSingleRF(train, b.GetEdgesD()))   // classification into different bins | 
|---|
| 166 | return; | 
|---|
| 167 | */ | 
|---|
| 168 | if (!rf.TrainRegression(train))                  // regression (best choice) | 
|---|
| 169 | return kFALSE; | 
|---|
| 170 |  | 
|---|
| 171 | // --------------------- Display result ---------------------- | 
|---|
| 172 |  | 
|---|
| 173 | gLog.Separator("Test"); | 
|---|
| 174 |  | 
|---|
| 175 | MParList  plist; | 
|---|
| 176 | MTaskList tlist; | 
|---|
| 177 | plist.AddToList(this); | 
|---|
| 178 | plist.AddToList(&tlist); | 
|---|
| 179 | //plist.AddToList(&b); | 
|---|
| 180 |  | 
|---|
| 181 | MFilterList list; | 
|---|
| 182 | if (!list.AddToList(fPreCuts)) | 
|---|
| 183 | *fLog << err << "ERROR - Calling MFilterList::AddToList for fPreCuts failed!" << endl; | 
|---|
| 184 | if (!list.AddToList(fTestCuts)) | 
|---|
| 185 | *fLog << err << "ERROR - Calling MFilterList::AddToList for fTestCuts failed!" << endl; | 
|---|
| 186 |  | 
|---|
| 187 | MContinue cont(&list); | 
|---|
| 188 | cont.SetInverted(); | 
|---|
| 189 |  | 
|---|
| 190 | MHEnergyEst hist; | 
|---|
| 191 | MFillH fillh(&hist); | 
|---|
| 192 | if (fEnableWeights) | 
|---|
| 193 | fillh.SetWeight(); | 
|---|
| 194 |  | 
|---|
| 195 | tlist.AddToList(&readtst); | 
|---|
| 196 | tlist.AddToList(fPreTasks); | 
|---|
| 197 | tlist.AddToList(&cont); | 
|---|
| 198 | tlist.AddToList(&rf); | 
|---|
| 199 | tlist.AddToList(fPostTasks); | 
|---|
| 200 | tlist.AddToList(&fillh); | 
|---|
| 201 |  | 
|---|
| 202 | MEvtLoop loop(fTitle); | 
|---|
| 203 | loop.SetLogStream(fLog); | 
|---|
| 204 | loop.SetDisplay(fDisplay); | 
|---|
| 205 | loop.SetParList(&plist); | 
|---|
| 206 | //if (!SetupEnv(loop)) | 
|---|
| 207 | //   return kFALSE; | 
|---|
| 208 |  | 
|---|
| 209 | if (!loop.Eventloop()) | 
|---|
| 210 | return kFALSE; | 
|---|
| 211 |  | 
|---|
| 212 | SetPathOut(out); | 
|---|
| 213 | if (!WriteDisplay(0, "UPDATE")) | 
|---|
| 214 | return kFALSE; | 
|---|
| 215 |  | 
|---|
| 216 | return kTRUE; | 
|---|
| 217 | } | 
|---|