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