| 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, 9/2004 <mailto:tbretz@astro.uni-wuerzburg.de> | 
|---|
| 19 | ! | 
|---|
| 20 | !   Copyright: MAGIC Software Development, 2000-2004 | 
|---|
| 21 | ! | 
|---|
| 22 | ! | 
|---|
| 23 | \* ======================================================================== */ | 
|---|
| 24 |  | 
|---|
| 25 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| 26 | // | 
|---|
| 27 | // MJOptimizeCuts | 
|---|
| 28 | // | 
|---|
| 29 | // Class for otimizing the parameters of the supercuts | 
|---|
| 30 | // | 
|---|
| 31 | // Minimization Control | 
|---|
| 32 | // ==================== | 
|---|
| 33 | // | 
|---|
| 34 | //   To choose the minimization algorithm use: | 
|---|
| 35 | //         void SetOptimizer(Optimizer_t o); | 
|---|
| 36 | // | 
|---|
| 37 | //   Allowed options are: | 
|---|
| 38 | //        enum Optimizer_t | 
|---|
| 39 | //        { | 
|---|
| 40 | //            kMigrad,      // Minimize by the method of Migrad | 
|---|
| 41 | //            kSimplex,     // Minimize by the method of Simplex | 
|---|
| 42 | //            kMinimize,    // Migrad + Simplex (if Migrad fails) | 
|---|
| 43 | //            kMinos,       // Minos error determination | 
|---|
| 44 | //            kImprove,     // Local minimum search | 
|---|
| 45 | //            kSeek,        // Minimize by the method of Monte Carlo | 
|---|
| 46 | //            kNone         // Skip optimization | 
|---|
| 47 | //        }; | 
|---|
| 48 | // | 
|---|
| 49 | //   For more details on the methods see TMinuit. | 
|---|
| 50 | // | 
|---|
| 51 | // | 
|---|
| 52 | //   You can change the behaviour of the minimization using | 
|---|
| 53 | // | 
|---|
| 54 | //        void SetNumMaxCalls(UInt_t num=0); | 
|---|
| 55 | //        void SetTolerance(Float_t tol=0); | 
|---|
| 56 | // | 
|---|
| 57 | //   While NumMaxCalls is the first, Tolerance the second arguement. | 
|---|
| 58 | //   For more details start root and type | 
|---|
| 59 | // | 
|---|
| 60 | //        gMinuit->mnhelp("command") | 
|---|
| 61 | // | 
|---|
| 62 | //   while command can be | 
|---|
| 63 | //        * MIGRAD | 
|---|
| 64 | //        * SIMPLEX | 
|---|
| 65 | //        * MINIMIZE | 
|---|
| 66 | //        * MINOS | 
|---|
| 67 | //        * IMPROVE | 
|---|
| 68 | //        * SEEK | 
|---|
| 69 | // | 
|---|
| 70 | //   The default (num==0 and tol==0) should always give you the | 
|---|
| 71 | //   corresponding defaults used in Minuit. | 
|---|
| 72 | // | 
|---|
| 73 | // | 
|---|
| 74 | // FIXME: Implement changing cut in hadronness... | 
|---|
| 75 | // FIXME: Show MHSignificance on MStatusDisplay during filling... | 
|---|
| 76 | // FIXME: Choose step-size percentage as static data membewr | 
|---|
| 77 | // FIXME: Choose minimization method | 
|---|
| 78 | // | 
|---|
| 79 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| 80 | #include "MJOptimizeCuts.h" | 
|---|
| 81 |  | 
|---|
| 82 | #include "MHMatrix.h" | 
|---|
| 83 |  | 
|---|
| 84 | // Environment | 
|---|
| 85 | #include "MLog.h" | 
|---|
| 86 | #include "MLogManip.h" | 
|---|
| 87 |  | 
|---|
| 88 | // Eventloop | 
|---|
| 89 | #include "MReadTree.h" | 
|---|
| 90 | #include "MParList.h" | 
|---|
| 91 | #include "MTaskList.h" | 
|---|
| 92 |  | 
|---|
| 93 | // Parameter container | 
|---|
| 94 | #include "MGeomCamMagic.h" | 
|---|
| 95 | #include "MParameters.h" | 
|---|
| 96 | #include "MFilterList.h" | 
|---|
| 97 |  | 
|---|
| 98 | // histograms | 
|---|
| 99 | #include "../mhflux/MHAlpha.h" | 
|---|
| 100 |  | 
|---|
| 101 | // Tasks | 
|---|
| 102 | #include "MF.h" | 
|---|
| 103 | #include "MFillH.h" | 
|---|
| 104 | #include "MContinue.h" | 
|---|
| 105 | #include "MMatrixLoop.h" | 
|---|
| 106 |  | 
|---|
| 107 | #include "MFMagicCuts.h" | 
|---|
| 108 |  | 
|---|
| 109 | ClassImp(MJOptimizeCuts); | 
|---|
| 110 |  | 
|---|
| 111 | using namespace std; | 
|---|
| 112 |  | 
|---|
| 113 | //------------------------------------------------------------------------ | 
|---|
| 114 | // | 
|---|
| 115 | MHAlpha *MJOptimizeCuts::CreateNewHist(const char *name) const | 
|---|
| 116 | { | 
|---|
| 117 | TClass *cls = gROOT->GetClass(fNameHist); | 
|---|
| 118 | if (!cls) | 
|---|
| 119 | { | 
|---|
| 120 | *fLog << err << "Class " << fNameHist << " not found in dictionary... abort." << endl; | 
|---|
| 121 | return NULL; | 
|---|
| 122 | } | 
|---|
| 123 | if (!cls->InheritsFrom(MHAlpha::Class())) | 
|---|
| 124 | { | 
|---|
| 125 | *fLog << err << "Class " << fNameHist << " doesn't inherit from MHAlpha... abort." << endl; | 
|---|
| 126 | return NULL; | 
|---|
| 127 | } | 
|---|
| 128 |  | 
|---|
| 129 | MHAlpha *h = (MHAlpha*)cls->New(); | 
|---|
| 130 | if (h && name) | 
|---|
| 131 | h->SetName(name); | 
|---|
| 132 |  | 
|---|
| 133 | return h; | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 | //------------------------------------------------------------------------ | 
|---|
| 137 | // | 
|---|
| 138 | Bool_t MJOptimizeCuts::RunOnOffCore(MHAlpha &histon, MHAlpha &histof, const char *fname, MFilter *filter, MAlphaFitter *fit, const char *tree) | 
|---|
| 139 | { | 
|---|
| 140 | fLog->Separator("Preparing On/Off-optimization"); | 
|---|
| 141 |  | 
|---|
| 142 | MParList parlist; | 
|---|
| 143 |  | 
|---|
| 144 | MGeomCamMagic geom; // For GetConvMm2Deg | 
|---|
| 145 | parlist.AddToList(&geom); | 
|---|
| 146 |  | 
|---|
| 147 | MHMatrix m("M"); | 
|---|
| 148 | AddRulesToMatrix(m); | 
|---|
| 149 | parlist.AddToList(&m); | 
|---|
| 150 |  | 
|---|
| 151 | const Int_t idxdatatype = m.AddColumn("DataType.fVal"); | 
|---|
| 152 |  | 
|---|
| 153 | histon.SkipHistTime(); | 
|---|
| 154 | histon.SkipHistTheta(); | 
|---|
| 155 | //histon.SkipHistEnergy(); | 
|---|
| 156 | histof.SkipHistTime(); | 
|---|
| 157 | histof.SkipHistTheta(); | 
|---|
| 158 | //histof.SkipHistEnergy(); | 
|---|
| 159 | histon.ForceUsingSize(); | 
|---|
| 160 | histof.ForceUsingSize(); | 
|---|
| 161 | histon.InitMapping(&m, 1); | 
|---|
| 162 | histof.InitMapping(&m, 1); | 
|---|
| 163 |  | 
|---|
| 164 | if (filter && filter->InheritsFrom(MFMagicCuts::Class())) | 
|---|
| 165 | ((MFMagicCuts*)filter)->InitMapping(&m); | 
|---|
| 166 |  | 
|---|
| 167 | parlist.AddToList(&histon); | 
|---|
| 168 | parlist.AddToList(&histof); | 
|---|
| 169 |  | 
|---|
| 170 | if (fname) | 
|---|
| 171 | { | 
|---|
| 172 | MReadTree read(tree); | 
|---|
| 173 | read.DisableAutoScheme(); // AutoScheme doesn't seem to be faster! | 
|---|
| 174 | read.AddFile(fname); | 
|---|
| 175 | if (!FillMatrix(read, parlist)) | 
|---|
| 176 | return kFALSE; | 
|---|
| 177 | } | 
|---|
| 178 | else | 
|---|
| 179 | { | 
|---|
| 180 | MParameterI par("DataType"); | 
|---|
| 181 | parlist.AddToList(&par); | 
|---|
| 182 |  | 
|---|
| 183 | gLog.Separator("Reading On-Data"); | 
|---|
| 184 | par.SetVal(1); | 
|---|
| 185 | MReadTree readon(tree); | 
|---|
| 186 | readon.DisableAutoScheme(); // AutoScheme doesn't seem to be faster! | 
|---|
| 187 | AddSequences(readon, fNamesOn); | 
|---|
| 188 | if (!FillMatrix(readon, parlist)) | 
|---|
| 189 | return kFALSE; | 
|---|
| 190 |  | 
|---|
| 191 | gLog.Separator("Reading Off-Data"); | 
|---|
| 192 | par.SetVal(0); | 
|---|
| 193 | MReadTree readoff(tree); | 
|---|
| 194 | readoff.DisableAutoScheme(); // AutoScheme doesn't seem to be faster! | 
|---|
| 195 | AddSequences(readoff, fNamesOff); | 
|---|
| 196 | if (!FillMatrix(readoff, parlist)) | 
|---|
| 197 | return kFALSE; | 
|---|
| 198 | } | 
|---|
| 199 |  | 
|---|
| 200 | MTaskList tasklist; | 
|---|
| 201 | parlist.Replace(&tasklist); | 
|---|
| 202 | if (fit) | 
|---|
| 203 | parlist.AddToList(fit); | 
|---|
| 204 |  | 
|---|
| 205 | MFilterList list; | 
|---|
| 206 | SetupFilters(list, filter); | 
|---|
| 207 |  | 
|---|
| 208 | MContinue contin(&list); | 
|---|
| 209 | parlist.AddToList(&list); | 
|---|
| 210 |  | 
|---|
| 211 | MFillH fillof(&histof, "", "FillHistOff"); | 
|---|
| 212 | MFillH fillon(&histon, "", "FillHistOn"); | 
|---|
| 213 |  | 
|---|
| 214 | MF f0(Form("M[%d]<0.5", idxdatatype), "FilterOffData"); | 
|---|
| 215 | MF f1(Form("M[%d]>0.5", idxdatatype), "FilterOnData"); | 
|---|
| 216 |  | 
|---|
| 217 | fillof.SetFilter(&f0); | 
|---|
| 218 | fillon.SetFilter(&f1); | 
|---|
| 219 |  | 
|---|
| 220 | MMatrixLoop loop(&m); | 
|---|
| 221 |  | 
|---|
| 222 | tasklist.AddToList(&loop); | 
|---|
| 223 | tasklist.AddToList(&list); | 
|---|
| 224 | tasklist.AddToList(&contin); | 
|---|
| 225 | tasklist.AddToList(&f0); | 
|---|
| 226 | tasklist.AddToList(&f1); | 
|---|
| 227 | tasklist.AddToList(&fillof); | 
|---|
| 228 | tasklist.AddToList(&fillon); | 
|---|
| 229 |  | 
|---|
| 230 | // Optimize with the tasklist in this parameterlist | 
|---|
| 231 | if (!Optimize(parlist)) | 
|---|
| 232 | return kFALSE; | 
|---|
| 233 |  | 
|---|
| 234 | *fLog << inf << "Finished processing of " << fname << endl; | 
|---|
| 235 |  | 
|---|
| 236 | // Copy the result back to be accessible by the user | 
|---|
| 237 | if (fit) | 
|---|
| 238 | histon.GetAlphaFitter().Copy(*fit); | 
|---|
| 239 |  | 
|---|
| 240 | // Print the result | 
|---|
| 241 | histon.GetAlphaFitter().Print("result"); | 
|---|
| 242 |  | 
|---|
| 243 | // Store result if requested | 
|---|
| 244 | TObjArray cont; | 
|---|
| 245 | cont.Add(&contin); | 
|---|
| 246 | return WriteContainer(cont, fNameOut); | 
|---|
| 247 | } | 
|---|
| 248 |  | 
|---|
| 249 | Bool_t MJOptimizeCuts::RunOnCore(MHAlpha &hist, const char *fname, MFilter *filter, MAlphaFitter *fit) | 
|---|
| 250 | { | 
|---|
| 251 | fLog->Separator("Preparing On-only-optimization"); | 
|---|
| 252 |  | 
|---|
| 253 | MParList parlist; | 
|---|
| 254 |  | 
|---|
| 255 | MGeomCamMagic geom; // For GetConvMm2Deg | 
|---|
| 256 | parlist.AddToList(&geom); | 
|---|
| 257 |  | 
|---|
| 258 | MHMatrix m("M"); | 
|---|
| 259 | AddRulesToMatrix(m); | 
|---|
| 260 | parlist.AddToList(&m); | 
|---|
| 261 |  | 
|---|
| 262 | hist.SkipHistTime(); | 
|---|
| 263 | hist.SkipHistTheta(); | 
|---|
| 264 | hist.SkipHistEnergy(); | 
|---|
| 265 | hist.InitMapping(&m); | 
|---|
| 266 |  | 
|---|
| 267 | if (filter && filter->InheritsFrom(MFMagicCuts::Class())) | 
|---|
| 268 | ((MFMagicCuts*)filter)->InitMapping(&m); | 
|---|
| 269 |  | 
|---|
| 270 | MReadTree read("Events"); | 
|---|
| 271 | read.DisableAutoScheme(); // AutoScheme doesn't seem to be faster! | 
|---|
| 272 | if (fname) | 
|---|
| 273 | read.AddFile(fname); | 
|---|
| 274 | else | 
|---|
| 275 | AddSequences(read, fNamesOn); | 
|---|
| 276 | if (!FillMatrix(read, parlist)) | 
|---|
| 277 | return kFALSE; | 
|---|
| 278 |  | 
|---|
| 279 | MTaskList tasklist; | 
|---|
| 280 | parlist.Replace(&tasklist); | 
|---|
| 281 | if (fit) | 
|---|
| 282 | parlist.AddToList(fit); | 
|---|
| 283 |  | 
|---|
| 284 | MFilterList list; | 
|---|
| 285 | SetupFilters(list, filter); | 
|---|
| 286 |  | 
|---|
| 287 | MContinue contin(&list); | 
|---|
| 288 | parlist.AddToList(&list); | 
|---|
| 289 |  | 
|---|
| 290 | MFillH fill(&hist); | 
|---|
| 291 |  | 
|---|
| 292 | MMatrixLoop loop(&m); | 
|---|
| 293 |  | 
|---|
| 294 | tasklist.AddToList(&loop); | 
|---|
| 295 | tasklist.AddToList(&list); | 
|---|
| 296 | tasklist.AddToList(&contin); | 
|---|
| 297 | tasklist.AddToList(&fill); | 
|---|
| 298 |  | 
|---|
| 299 | // Optimize with the tasklist in this parameterlist | 
|---|
| 300 | if (!Optimize(parlist)) | 
|---|
| 301 | return kFALSE; | 
|---|
| 302 |  | 
|---|
| 303 | *fLog << inf << "Finished processing of " << fname << endl; | 
|---|
| 304 |  | 
|---|
| 305 | // Copy the result back to be accessible by the user | 
|---|
| 306 | if (fit) | 
|---|
| 307 | hist.GetAlphaFitter().Copy(*fit); | 
|---|
| 308 | // Print the result | 
|---|
| 309 | hist.GetAlphaFitter().Print("result"); | 
|---|
| 310 |  | 
|---|
| 311 | // Store result if requested | 
|---|
| 312 | TObjArray cont; | 
|---|
| 313 | cont.Add(&contin); | 
|---|
| 314 | return WriteContainer(cont, fNameOut); | 
|---|
| 315 | } | 
|---|
| 316 |  | 
|---|
| 317 | //------------------------------------------------------------------------ | 
|---|
| 318 | // | 
|---|
| 319 | Bool_t MJOptimizeCuts::RunOnOff(const char *fname, MFilter *filter, MAlphaFitter *fit, const char *tree) | 
|---|
| 320 | { | 
|---|
| 321 | MHAlpha *histon = CreateNewHist("Hist"); | 
|---|
| 322 | MHAlpha *histof = CreateNewHist("HistOff"); | 
|---|
| 323 |  | 
|---|
| 324 | if (!histon || !histof) | 
|---|
| 325 | return kFALSE; | 
|---|
| 326 |  | 
|---|
| 327 | const Bool_t rc = RunOnOffCore(*histon, *histof, fname, filter, fit, tree); | 
|---|
| 328 |  | 
|---|
| 329 | delete histon; | 
|---|
| 330 | delete histof; | 
|---|
| 331 |  | 
|---|
| 332 | return rc; | 
|---|
| 333 | } | 
|---|
| 334 |  | 
|---|
| 335 | //------------------------------------------------------------------------ | 
|---|
| 336 | // | 
|---|
| 337 | Bool_t MJOptimizeCuts::RunOn(const char *fname, MFilter *filter, MAlphaFitter *fit) | 
|---|
| 338 | { | 
|---|
| 339 | MHAlpha *histon = CreateNewHist(); | 
|---|
| 340 |  | 
|---|
| 341 | if (!histon) | 
|---|
| 342 | return kFALSE; | 
|---|
| 343 |  | 
|---|
| 344 | const Bool_t rc = RunOnCore(*histon, fname, filter, fit); | 
|---|
| 345 |  | 
|---|
| 346 | delete histon; | 
|---|
| 347 | return rc; | 
|---|
| 348 | } | 
|---|