| 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 <TClass.h> | 
|---|
| 83 |  | 
|---|
| 84 | #include "MHMatrix.h" | 
|---|
| 85 |  | 
|---|
| 86 | // Environment | 
|---|
| 87 | #include "MLog.h" | 
|---|
| 88 | #include "MLogManip.h" | 
|---|
| 89 | #include "MStatusDisplay.h" | 
|---|
| 90 |  | 
|---|
| 91 | // Eventloop | 
|---|
| 92 | #include "MReadTree.h" | 
|---|
| 93 | #include "MParList.h" | 
|---|
| 94 | #include "MTaskList.h" | 
|---|
| 95 |  | 
|---|
| 96 | // Parameter container | 
|---|
| 97 | #include "MGeomCamFACT.h" | 
|---|
| 98 | #include "MParameters.h" | 
|---|
| 99 | #include "MFilterList.h" | 
|---|
| 100 |  | 
|---|
| 101 | // histograms | 
|---|
| 102 | #include "../mhflux/MHAlpha.h" | 
|---|
| 103 |  | 
|---|
| 104 | // Tasks | 
|---|
| 105 | #include "MF.h" | 
|---|
| 106 | #include "MFillH.h" | 
|---|
| 107 | #include "MContinue.h" | 
|---|
| 108 | #include "MMatrixLoop.h" | 
|---|
| 109 |  | 
|---|
| 110 | #include "MFMagicCuts.h" | 
|---|
| 111 |  | 
|---|
| 112 | ClassImp(MJOptimizeCuts); | 
|---|
| 113 |  | 
|---|
| 114 | using namespace std; | 
|---|
| 115 |  | 
|---|
| 116 | //------------------------------------------------------------------------ | 
|---|
| 117 | // | 
|---|
| 118 | MHAlpha *MJOptimizeCuts::CreateNewHist(const char *name) const | 
|---|
| 119 | { | 
|---|
| 120 | TClass *cls = gROOT->GetClass(fNameHist); | 
|---|
| 121 | if (!cls) | 
|---|
| 122 | { | 
|---|
| 123 | *fLog << err << "Class " << fNameHist << " not found in dictionary... abort." << endl; | 
|---|
| 124 | return NULL; | 
|---|
| 125 | } | 
|---|
| 126 | if (!cls->InheritsFrom(MHAlpha::Class())) | 
|---|
| 127 | { | 
|---|
| 128 | *fLog << err << "Class " << fNameHist << " doesn't inherit from MHAlpha... abort." << endl; | 
|---|
| 129 | return NULL; | 
|---|
| 130 | } | 
|---|
| 131 |  | 
|---|
| 132 | MHAlpha *h = (MHAlpha*)cls->New(); | 
|---|
| 133 | if (h && name) | 
|---|
| 134 | h->SetName(name); | 
|---|
| 135 |  | 
|---|
| 136 | return h; | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 | //------------------------------------------------------------------------ | 
|---|
| 140 | // | 
|---|
| 141 | Bool_t MJOptimizeCuts::RunOnOffCore(MHAlpha &histon, MHAlpha &histof, const char *fname, MFilter *filter, MAlphaFitter *fit, const char *tree) | 
|---|
| 142 | { | 
|---|
| 143 | SetTitle(Form("OptimizeCuts: %s", fname)); | 
|---|
| 144 |  | 
|---|
| 145 | if (fDisplay) | 
|---|
| 146 | fDisplay->SetTitle(fTitle); | 
|---|
| 147 |  | 
|---|
| 148 | fLog->Separator("Preparing On/Off-optimization"); | 
|---|
| 149 |  | 
|---|
| 150 | MParList parlist; | 
|---|
| 151 |  | 
|---|
| 152 | MGeomCamFACT geom; // For GetConvMm2Deg | 
|---|
| 153 | parlist.AddToList(&geom); | 
|---|
| 154 |  | 
|---|
| 155 | MHMatrix m("M"); | 
|---|
| 156 | AddRulesToMatrix(m); | 
|---|
| 157 | parlist.AddToList(&m); | 
|---|
| 158 |  | 
|---|
| 159 | const Int_t idxdatatype = m.AddColumn("DataType.fVal"); | 
|---|
| 160 |  | 
|---|
| 161 | histon.SkipHistTime(); | 
|---|
| 162 | histon.SkipHistTheta(); | 
|---|
| 163 | //histon.SkipHistEnergy(); | 
|---|
| 164 | histof.SkipHistTime(); | 
|---|
| 165 | histof.SkipHistTheta(); | 
|---|
| 166 | //histof.SkipHistEnergy(); | 
|---|
| 167 | histon.ForceUsingSize(); | 
|---|
| 168 | histof.ForceUsingSize(); | 
|---|
| 169 | histon.InitMapping(&m, 1); | 
|---|
| 170 | histof.InitMapping(&m, 1); | 
|---|
| 171 |  | 
|---|
| 172 | if (filter) | 
|---|
| 173 | { | 
|---|
| 174 | if (filter->InheritsFrom(MFMagicCuts::Class())) | 
|---|
| 175 | ((MFMagicCuts*)filter)->InitMapping(&m); | 
|---|
| 176 | else | 
|---|
| 177 | { | 
|---|
| 178 | *fLog << err << "ERROR - Currently only MFMagicCuts is supported." << endl; | 
|---|
| 179 | return kFALSE; | 
|---|
| 180 | } | 
|---|
| 181 | } | 
|---|
| 182 |  | 
|---|
| 183 | parlist.AddToList(&histon); | 
|---|
| 184 | parlist.AddToList(&histof); | 
|---|
| 185 |  | 
|---|
| 186 | if (fname) | 
|---|
| 187 | { | 
|---|
| 188 | MReadTree read(tree); | 
|---|
| 189 | read.DisableAutoScheme(); // AutoScheme doesn't seem to be faster! | 
|---|
| 190 | read.AddFile(fname); | 
|---|
| 191 | if (!FillMatrix(read, parlist)) | 
|---|
| 192 | return kFALSE; | 
|---|
| 193 | } | 
|---|
| 194 | else | 
|---|
| 195 | { | 
|---|
| 196 | MParameterI par("DataType"); | 
|---|
| 197 | parlist.AddToList(&par); | 
|---|
| 198 |  | 
|---|
| 199 | gLog.Separator("Reading On-Data"); | 
|---|
| 200 | par.SetVal(1); | 
|---|
| 201 | MReadTree readon(tree); | 
|---|
| 202 | readon.DisableAutoScheme(); // AutoScheme doesn't seem to be faster! | 
|---|
| 203 | AddSequences(readon, fNamesOn); | 
|---|
| 204 | if (!FillMatrix(readon, parlist)) | 
|---|
| 205 | return kFALSE; | 
|---|
| 206 |  | 
|---|
| 207 | gLog.Separator("Reading Off-Data"); | 
|---|
| 208 | par.SetVal(0); | 
|---|
| 209 | MReadTree readoff(tree); | 
|---|
| 210 | readoff.DisableAutoScheme(); // AutoScheme doesn't seem to be faster! | 
|---|
| 211 | AddSequences(readoff, fNamesOff); | 
|---|
| 212 | if (!FillMatrix(readoff, parlist)) | 
|---|
| 213 | return kFALSE; | 
|---|
| 214 | } | 
|---|
| 215 |  | 
|---|
| 216 | MTaskList tasklist; | 
|---|
| 217 | parlist.Replace(&tasklist); | 
|---|
| 218 | if (fit) | 
|---|
| 219 | parlist.AddToList(fit); | 
|---|
| 220 |  | 
|---|
| 221 | MFilterList list; | 
|---|
| 222 | SetupFilters(list, filter); | 
|---|
| 223 |  | 
|---|
| 224 | MContinue contin(&list); | 
|---|
| 225 | parlist.AddToList(&list); | 
|---|
| 226 |  | 
|---|
| 227 | MFillH fillof(&histof, "", "FillHistOff"); | 
|---|
| 228 | MFillH fillon(&histon, "", "FillHistOn"); | 
|---|
| 229 |  | 
|---|
| 230 | MF f0(Form("M[%d]<0.5", idxdatatype), "FilterOffData"); | 
|---|
| 231 | MF f1(Form("M[%d]>0.5", idxdatatype), "FilterOnData"); | 
|---|
| 232 |  | 
|---|
| 233 | fillof.SetFilter(&f0); | 
|---|
| 234 | fillon.SetFilter(&f1); | 
|---|
| 235 |  | 
|---|
| 236 | MMatrixLoop loop(&m); | 
|---|
| 237 |  | 
|---|
| 238 | tasklist.AddToList(&loop); | 
|---|
| 239 | tasklist.AddToList(&list); | 
|---|
| 240 | tasklist.AddToList(&contin); | 
|---|
| 241 | tasklist.AddToList(&f0); | 
|---|
| 242 | tasklist.AddToList(&f1); | 
|---|
| 243 | tasklist.AddToList(&fillof); | 
|---|
| 244 | tasklist.AddToList(&fillon); | 
|---|
| 245 |  | 
|---|
| 246 | // Optimize with the tasklist in this parameterlist | 
|---|
| 247 | if (!Optimize(parlist)) | 
|---|
| 248 | return kFALSE; | 
|---|
| 249 |  | 
|---|
| 250 | *fLog << inf << "Finished processing of " << fname << endl; | 
|---|
| 251 |  | 
|---|
| 252 | // Copy the result back to be accessible by the user | 
|---|
| 253 | if (fit) | 
|---|
| 254 | histon.GetAlphaFitter().Copy(*fit); | 
|---|
| 255 |  | 
|---|
| 256 | // Print the result | 
|---|
| 257 | histon.GetAlphaFitter().Print("result"); | 
|---|
| 258 |  | 
|---|
| 259 | // Store result if requested | 
|---|
| 260 | TObjArray cont; | 
|---|
| 261 | cont.Add(&contin); | 
|---|
| 262 | return WriteContainer(cont, fNameOut); | 
|---|
| 263 | } | 
|---|
| 264 |  | 
|---|
| 265 | Bool_t MJOptimizeCuts::RunOnCore(MHAlpha &hist, const char *fname, MFilter *filter, MAlphaFitter *fit) | 
|---|
| 266 | { | 
|---|
| 267 | SetTitle(Form("OptimizeCuts: %s", fname)); | 
|---|
| 268 |  | 
|---|
| 269 | if (fDisplay) | 
|---|
| 270 | fDisplay->SetTitle(fTitle); | 
|---|
| 271 |  | 
|---|
| 272 | fLog->Separator("Preparing On-only-optimization"); | 
|---|
| 273 |  | 
|---|
| 274 | MParList parlist; | 
|---|
| 275 |  | 
|---|
| 276 | MGeomCamFACT geom; // For GetConvMm2Deg | 
|---|
| 277 | parlist.AddToList(&geom); | 
|---|
| 278 |  | 
|---|
| 279 | MHMatrix m("M"); | 
|---|
| 280 | AddRulesToMatrix(m); | 
|---|
| 281 | parlist.AddToList(&m); | 
|---|
| 282 |  | 
|---|
| 283 | hist.SkipHistTime(); | 
|---|
| 284 | hist.SkipHistTheta(); | 
|---|
| 285 | hist.SkipHistEnergy(); | 
|---|
| 286 | hist.InitMapping(&m); | 
|---|
| 287 |  | 
|---|
| 288 | if (filter && filter->InheritsFrom(MFMagicCuts::Class())) | 
|---|
| 289 | ((MFMagicCuts*)filter)->InitMapping(&m); | 
|---|
| 290 |  | 
|---|
| 291 | MReadTree read("Events"); | 
|---|
| 292 | read.DisableAutoScheme(); // AutoScheme doesn't seem to be faster! | 
|---|
| 293 | if (fname) | 
|---|
| 294 | read.AddFile(fname); | 
|---|
| 295 | else | 
|---|
| 296 | AddSequences(read, fNamesOn); | 
|---|
| 297 | if (!FillMatrix(read, parlist)) | 
|---|
| 298 | return kFALSE; | 
|---|
| 299 |  | 
|---|
| 300 | MTaskList tasklist; | 
|---|
| 301 | parlist.Replace(&tasklist); | 
|---|
| 302 | if (fit) | 
|---|
| 303 | parlist.AddToList(fit); | 
|---|
| 304 |  | 
|---|
| 305 | MFilterList list; | 
|---|
| 306 | SetupFilters(list, filter); | 
|---|
| 307 |  | 
|---|
| 308 | MContinue contin(&list); | 
|---|
| 309 | parlist.AddToList(&list); | 
|---|
| 310 |  | 
|---|
| 311 | MFillH fill(&hist); | 
|---|
| 312 |  | 
|---|
| 313 | MMatrixLoop loop(&m); | 
|---|
| 314 |  | 
|---|
| 315 | tasklist.AddToList(&loop); | 
|---|
| 316 | tasklist.AddToList(&list); | 
|---|
| 317 | tasklist.AddToList(&contin); | 
|---|
| 318 | tasklist.AddToList(&fill); | 
|---|
| 319 |  | 
|---|
| 320 | // Optimize with the tasklist in this parameterlist | 
|---|
| 321 | if (!Optimize(parlist)) | 
|---|
| 322 | return kFALSE; | 
|---|
| 323 |  | 
|---|
| 324 | *fLog << inf << "Finished processing of " << fname << endl; | 
|---|
| 325 |  | 
|---|
| 326 | // Copy the result back to be accessible by the user | 
|---|
| 327 | if (fit) | 
|---|
| 328 | hist.GetAlphaFitter().Copy(*fit); | 
|---|
| 329 | // Print the result | 
|---|
| 330 | hist.GetAlphaFitter().Print("result"); | 
|---|
| 331 |  | 
|---|
| 332 | // Store result if requested | 
|---|
| 333 | TObjArray cont; | 
|---|
| 334 | cont.Add(&contin); | 
|---|
| 335 | if (fDisplay) | 
|---|
| 336 | cont.Add(fDisplay); | 
|---|
| 337 | return WriteContainer(cont, fNameOut); | 
|---|
| 338 | } | 
|---|
| 339 |  | 
|---|
| 340 | //------------------------------------------------------------------------ | 
|---|
| 341 | // | 
|---|
| 342 | Bool_t MJOptimizeCuts::RunOnOff(const char *fname, MFilter *filter, MAlphaFitter *fit, const char *tree) | 
|---|
| 343 | { | 
|---|
| 344 | MHAlpha *histon = CreateNewHist("Hist"); | 
|---|
| 345 | MHAlpha *histof = CreateNewHist("HistOff"); | 
|---|
| 346 |  | 
|---|
| 347 | if (!histon || !histof) | 
|---|
| 348 | return kFALSE; | 
|---|
| 349 |  | 
|---|
| 350 | const Bool_t rc = RunOnOffCore(*histon, *histof, fname, filter, fit, tree); | 
|---|
| 351 |  | 
|---|
| 352 | delete histon; | 
|---|
| 353 | delete histof; | 
|---|
| 354 |  | 
|---|
| 355 | return rc; | 
|---|
| 356 | } | 
|---|
| 357 |  | 
|---|
| 358 | //------------------------------------------------------------------------ | 
|---|
| 359 | // | 
|---|
| 360 | Bool_t MJOptimizeCuts::RunOn(const char *fname, MFilter *filter, MAlphaFitter *fit) | 
|---|
| 361 | { | 
|---|
| 362 | MHAlpha *histon = CreateNewHist(); | 
|---|
| 363 |  | 
|---|
| 364 | if (!histon) | 
|---|
| 365 | return kFALSE; | 
|---|
| 366 |  | 
|---|
| 367 | const Bool_t rc = RunOnCore(*histon, fname, filter, fit); | 
|---|
| 368 |  | 
|---|
| 369 | delete histon; | 
|---|
| 370 | return rc; | 
|---|
| 371 | } | 
|---|