| 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 | // Copy the result back to be accessible by the user
|
|---|
| 235 | if (fit)
|
|---|
| 236 | histon.GetAlphaFitter().Copy(*fit);
|
|---|
| 237 |
|
|---|
| 238 | // Print the result
|
|---|
| 239 | histon.GetAlphaFitter().Print("result");
|
|---|
| 240 |
|
|---|
| 241 | // Store result if requested
|
|---|
| 242 | TObjArray cont;
|
|---|
| 243 | cont.Add(&contin);
|
|---|
| 244 | return WriteContainer(cont, fNameOut);
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | Bool_t MJOptimizeCuts::RunOnCore(MHAlpha &hist, const char *fname, MFilter *filter, MAlphaFitter *fit)
|
|---|
| 248 | {
|
|---|
| 249 | fLog->Separator("Preparing On-only-optimization");
|
|---|
| 250 |
|
|---|
| 251 | MParList parlist;
|
|---|
| 252 |
|
|---|
| 253 | MGeomCamMagic geom; // For GetConvMm2Deg
|
|---|
| 254 | parlist.AddToList(&geom);
|
|---|
| 255 |
|
|---|
| 256 | MHMatrix m("M");
|
|---|
| 257 | AddRulesToMatrix(m);
|
|---|
| 258 | parlist.AddToList(&m);
|
|---|
| 259 |
|
|---|
| 260 | hist.SkipHistTime();
|
|---|
| 261 | hist.SkipHistTheta();
|
|---|
| 262 | hist.SkipHistEnergy();
|
|---|
| 263 | hist.InitMapping(&m);
|
|---|
| 264 |
|
|---|
| 265 | if (filter && filter->InheritsFrom(MFMagicCuts::Class()))
|
|---|
| 266 | ((MFMagicCuts*)filter)->InitMapping(&m);
|
|---|
| 267 |
|
|---|
| 268 | MReadTree read("Events");
|
|---|
| 269 | read.DisableAutoScheme(); // AutoScheme doesn't seem to be faster!
|
|---|
| 270 | if (fname)
|
|---|
| 271 | read.AddFile(fname);
|
|---|
| 272 | else
|
|---|
| 273 | AddSequences(read, fNamesOn);
|
|---|
| 274 | if (!FillMatrix(read, parlist))
|
|---|
| 275 | return kFALSE;
|
|---|
| 276 |
|
|---|
| 277 | MTaskList tasklist;
|
|---|
| 278 | parlist.Replace(&tasklist);
|
|---|
| 279 | if (fit)
|
|---|
| 280 | parlist.AddToList(fit);
|
|---|
| 281 |
|
|---|
| 282 | MFilterList list;
|
|---|
| 283 | SetupFilters(list, filter);
|
|---|
| 284 |
|
|---|
| 285 | MContinue contin(&list);
|
|---|
| 286 | parlist.AddToList(&list);
|
|---|
| 287 |
|
|---|
| 288 | MFillH fill(&hist);
|
|---|
| 289 |
|
|---|
| 290 | MMatrixLoop loop(&m);
|
|---|
| 291 |
|
|---|
| 292 | tasklist.AddToList(&loop);
|
|---|
| 293 | tasklist.AddToList(&list);
|
|---|
| 294 | tasklist.AddToList(&contin);
|
|---|
| 295 | tasklist.AddToList(&fill);
|
|---|
| 296 |
|
|---|
| 297 | // Optimize with the tasklist in this parameterlist
|
|---|
| 298 | if (!Optimize(parlist))
|
|---|
| 299 | return kFALSE;
|
|---|
| 300 |
|
|---|
| 301 | // Copy the result back to be accessible by the user
|
|---|
| 302 | if (fit)
|
|---|
| 303 | hist.GetAlphaFitter().Copy(*fit);
|
|---|
| 304 | // Print the result
|
|---|
| 305 | hist.GetAlphaFitter().Print("result");
|
|---|
| 306 |
|
|---|
| 307 | // Store result if requested
|
|---|
| 308 | TObjArray cont;
|
|---|
| 309 | cont.Add(&contin);
|
|---|
| 310 | return WriteContainer(cont, fNameOut);
|
|---|
| 311 | }
|
|---|
| 312 |
|
|---|
| 313 | //------------------------------------------------------------------------
|
|---|
| 314 | //
|
|---|
| 315 | Bool_t MJOptimizeCuts::RunOnOff(const char *fname, MFilter *filter, MAlphaFitter *fit, const char *tree)
|
|---|
| 316 | {
|
|---|
| 317 | MHAlpha *histon = CreateNewHist("Hist");
|
|---|
| 318 | MHAlpha *histof = CreateNewHist("HistOff");
|
|---|
| 319 |
|
|---|
| 320 | if (!histon || !histof)
|
|---|
| 321 | return kFALSE;
|
|---|
| 322 |
|
|---|
| 323 | const Bool_t rc = RunOnOffCore(*histon, *histof, fname, filter, fit, tree);
|
|---|
| 324 |
|
|---|
| 325 | delete histon;
|
|---|
| 326 | delete histof;
|
|---|
| 327 |
|
|---|
| 328 | return rc;
|
|---|
| 329 | }
|
|---|
| 330 |
|
|---|
| 331 | //------------------------------------------------------------------------
|
|---|
| 332 | //
|
|---|
| 333 | Bool_t MJOptimizeCuts::RunOn(const char *fname, MFilter *filter, MAlphaFitter *fit)
|
|---|
| 334 | {
|
|---|
| 335 | MHAlpha *histon = CreateNewHist();
|
|---|
| 336 |
|
|---|
| 337 | if (!histon)
|
|---|
| 338 | return kFALSE;
|
|---|
| 339 |
|
|---|
| 340 | const Bool_t rc = RunOnCore(*histon, fname, filter, fit);
|
|---|
| 341 |
|
|---|
| 342 | delete histon;
|
|---|
| 343 | return rc;
|
|---|
| 344 | }
|
|---|