| 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  1/2002 <mailto:tbretz@astro.uni-wuerzburg.de> | 
|---|
| 19 | !   Author(s): Wolfgang Wittek 1/2002 <mailto:wittek@mppmu.mpg.de> | 
|---|
| 20 | !   Author(s): Abelardo Moralejo 4/2003 <mailto:moralejo@pd.infn.it> | 
|---|
| 21 | ! | 
|---|
| 22 | !   Copyright: MAGIC Software Development, 2000-2003 | 
|---|
| 23 | ! | 
|---|
| 24 | ! | 
|---|
| 25 | \* ======================================================================== */ | 
|---|
| 26 |  | 
|---|
| 27 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| 28 | // | 
|---|
| 29 | // MMcEnergyEst | 
|---|
| 30 | // | 
|---|
| 31 | // Class for otimizing the parameters of the energy estimator | 
|---|
| 32 | // | 
|---|
| 33 | // FIXME: the class must be made more flexible, allowing for different | 
|---|
| 34 | // parametrizations to be used. Also a new class is needed, a container | 
|---|
| 35 | // for the parameters of the energy estimator. | 
|---|
| 36 | // FIXME: the starting values of the parameters are now fixed. | 
|---|
| 37 | // | 
|---|
| 38 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| 39 | #include "MMcEnergyEst.h" | 
|---|
| 40 |  | 
|---|
| 41 | #include <math.h>            // fabs on Alpha | 
|---|
| 42 |  | 
|---|
| 43 | #include <TMinuit.h> | 
|---|
| 44 | #include <TStopwatch.h> | 
|---|
| 45 | #include <TVirtualFitter.h> | 
|---|
| 46 |  | 
|---|
| 47 | #include "MParList.h" | 
|---|
| 48 | #include "MTaskList.h" | 
|---|
| 49 | #include "MGeomCamCT1.h" | 
|---|
| 50 | #include "MFEventSelector.h" | 
|---|
| 51 | #include "MReadTree.h" | 
|---|
| 52 | #include "MFCT1SelFinal.h" | 
|---|
| 53 | #include "MHMatrix.h" | 
|---|
| 54 | #include "MEnergyEstParam.h" | 
|---|
| 55 | #include "MMatrixLoop.h" | 
|---|
| 56 | #include "MChisqEval.h" | 
|---|
| 57 | #include "MEvtLoop.h" | 
|---|
| 58 | #include "MDataElement.h" | 
|---|
| 59 | #include "MDataMember.h" | 
|---|
| 60 | #include "MLog.h" | 
|---|
| 61 | #include "MLogManip.h" | 
|---|
| 62 | #include "MParameters.h" | 
|---|
| 63 |  | 
|---|
| 64 | ClassImp(MMcEnergyEst); | 
|---|
| 65 |  | 
|---|
| 66 | using namespace std; | 
|---|
| 67 |  | 
|---|
| 68 | //------------------------------------------------------------------------ | 
|---|
| 69 | // | 
|---|
| 70 | // fcn calculates the function to be minimized (using TMinuit::Migrad) | 
|---|
| 71 | // | 
|---|
| 72 | static void fcn(Int_t &npar, Double_t *gin, Double_t &f, Double_t *par, Int_t iflag) | 
|---|
| 73 | { | 
|---|
| 74 | MEvtLoop *evtloop = (MEvtLoop*)gMinuit->GetObjectFit(); | 
|---|
| 75 |  | 
|---|
| 76 | MParList  *plist  = evtloop->GetParList(); | 
|---|
| 77 | MTaskList *tlist  = evtloop->GetTaskList(); | 
|---|
| 78 |  | 
|---|
| 79 | MParameterD     *eval = (MParameterD*)    plist->FindObject("MFitResult", "MParameterD"); | 
|---|
| 80 | MEnergyEstParam *eest = (MEnergyEstParam*)tlist->FindObject("MEnergyEstParam"); | 
|---|
| 81 |  | 
|---|
| 82 | eest->SetCoeff(TArrayD(eest->GetNumCoeff(), par)); | 
|---|
| 83 |  | 
|---|
| 84 | evtloop->Eventloop(); | 
|---|
| 85 |  | 
|---|
| 86 | f = eval->GetVal(); | 
|---|
| 87 | } | 
|---|
| 88 |  | 
|---|
| 89 | // -------------------------------------------------------------------------- | 
|---|
| 90 | // | 
|---|
| 91 | // Default constructor. | 
|---|
| 92 | // | 
|---|
| 93 | MMcEnergyEst::MMcEnergyEst(const char *name, const char *title) | 
|---|
| 94 | { | 
|---|
| 95 | fName  = name  ? name  : "MMcEnergyEst"; | 
|---|
| 96 | fTitle = title ? title : "Optimizer of the energy estimator"; | 
|---|
| 97 |  | 
|---|
| 98 | SetHillasName("MHillas"); | 
|---|
| 99 | SetHillasSrcName("MHillasSrc"); | 
|---|
| 100 |  | 
|---|
| 101 | // | 
|---|
| 102 | // Set initial values of the parameters (close enough to the final | 
|---|
| 103 | // ones, taken from previous runs of the procedure). Parameter | 
|---|
| 104 | // fA[4] is not used in the default energy estimation model (from | 
|---|
| 105 | // D. Kranich). | 
|---|
| 106 | // | 
|---|
| 107 | fA.Set(5); | 
|---|
| 108 | fB.Set(7); | 
|---|
| 109 |  | 
|---|
| 110 | fA[0] =  21006.2; | 
|---|
| 111 | fA[1] = -43.2648; | 
|---|
| 112 | fA[2] = -690.403; | 
|---|
| 113 | fA[3] = -0.0428544; | 
|---|
| 114 | fA[4] =  1.; | 
|---|
| 115 | fB[0] = -3391.05; | 
|---|
| 116 | fB[1] =  136.58; | 
|---|
| 117 | fB[2] =  0.253807; | 
|---|
| 118 | fB[3] =  254.363; | 
|---|
| 119 | fB[4] =  61.0386; | 
|---|
| 120 | fB[5] = -0.0190984; | 
|---|
| 121 | fB[6] = -421695; | 
|---|
| 122 | } | 
|---|
| 123 |  | 
|---|
| 124 | Bool_t MMcEnergyEst::SetCoeff(TArrayD &coeff) | 
|---|
| 125 | { | 
|---|
| 126 | if (coeff.GetSize() != fA.GetSize()+fB.GetSize()) | 
|---|
| 127 | { | 
|---|
| 128 | *fLog << err << dbginf << "Wrong number of parameters!" << endl; | 
|---|
| 129 | return(kFALSE); | 
|---|
| 130 | } | 
|---|
| 131 |  | 
|---|
| 132 | for (Int_t i = 0; i < fA.GetSize(); i++) | 
|---|
| 133 | fA[i] = coeff[i]; | 
|---|
| 134 | for (Int_t i = 0; i < fB.GetSize(); i++) | 
|---|
| 135 | fB[i] = coeff[i+fA.GetSize()]; | 
|---|
| 136 |  | 
|---|
| 137 | return(kTRUE); | 
|---|
| 138 |  | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 | //------------------------------------------------------------------------ | 
|---|
| 142 | // | 
|---|
| 143 | // Optimization (via migrad minimization) of parameters of energy estimation. | 
|---|
| 144 | // | 
|---|
| 145 | void MMcEnergyEst::FindParams() | 
|---|
| 146 | { | 
|---|
| 147 | MParList parlist; | 
|---|
| 148 |  | 
|---|
| 149 | MFEventSelector selector; | 
|---|
| 150 | selector.SetNumSelectEvts(fNevents); | 
|---|
| 151 | *fLog << inf << "Read events from file '" << fInFile << "'" << endl; | 
|---|
| 152 |  | 
|---|
| 153 | MReadTree read("Events"); | 
|---|
| 154 | read.AddFile(fInFile); | 
|---|
| 155 | read.DisableAutoScheme(); | 
|---|
| 156 | read.SetSelector(&selector); | 
|---|
| 157 |  | 
|---|
| 158 | *fLog << inf << "Define columns of matrix" << endl; | 
|---|
| 159 | MHMatrix matrix; | 
|---|
| 160 | Int_t colenergy = matrix.AddColumn("MMcEvt.fEnergy"); | 
|---|
| 161 | Int_t colimpact = matrix.AddColumn("MMcEvt.fImpact"); | 
|---|
| 162 |  | 
|---|
| 163 | if (colenergy < 0  ||  colimpact < 0) | 
|---|
| 164 | { | 
|---|
| 165 | *fLog << err << dbginf << "colenergy, colimpact = " << colenergy << ",  " | 
|---|
| 166 | << colimpact << endl; | 
|---|
| 167 | return; | 
|---|
| 168 | } | 
|---|
| 169 |  | 
|---|
| 170 | MEnergyEstParam eest(fHillasName); | 
|---|
| 171 | eest.Add(fHillasSrcName); | 
|---|
| 172 | eest.InitMapping(&matrix); | 
|---|
| 173 |  | 
|---|
| 174 | *fLog << inf << "--------------------------------------" << endl; | 
|---|
| 175 | *fLog << inf << "Fill events into the matrix" << endl; | 
|---|
| 176 | if ( !matrix.Fill(&parlist, &read, fEventFilter) ) | 
|---|
| 177 | return; | 
|---|
| 178 | *fLog << inf << "Matrix was filled with " << matrix.GetNumRows() | 
|---|
| 179 | << inf << " events" << endl; | 
|---|
| 180 |  | 
|---|
| 181 | //----------------------------------------------------------------------- | 
|---|
| 182 | // | 
|---|
| 183 | // Setup the eventloop which will be executed in the fcn of MINUIT | 
|---|
| 184 | // | 
|---|
| 185 | *fLog << inf << "--------------------------------------" << endl; | 
|---|
| 186 | *fLog << inf << "Setup event loop to be used in 'fcn'" << endl; | 
|---|
| 187 |  | 
|---|
| 188 | MTaskList tasklist; | 
|---|
| 189 |  | 
|---|
| 190 | MMatrixLoop loop(&matrix); | 
|---|
| 191 |  | 
|---|
| 192 | MChisqEval eval; | 
|---|
| 193 | eval.SetY1(new MDataElement(&matrix, colenergy)); | 
|---|
| 194 | eval.SetY2(new MDataMember("MEnergyEst.fEnergy")); | 
|---|
| 195 | eval.SetOwner(); | 
|---|
| 196 |  | 
|---|
| 197 | // | 
|---|
| 198 | // Entries in MParList | 
|---|
| 199 |  | 
|---|
| 200 | parlist.AddToList(&tasklist); | 
|---|
| 201 |  | 
|---|
| 202 | // | 
|---|
| 203 | // Entries in MTaskList | 
|---|
| 204 |  | 
|---|
| 205 | tasklist.AddToList(&loop); | 
|---|
| 206 | tasklist.AddToList(&eest); | 
|---|
| 207 | tasklist.AddToList(&eval); | 
|---|
| 208 |  | 
|---|
| 209 |  | 
|---|
| 210 | *fLog << inf << "Event loop was setup" << endl; | 
|---|
| 211 | MEvtLoop evtloop; | 
|---|
| 212 | evtloop.SetParList(&parlist); | 
|---|
| 213 |  | 
|---|
| 214 | // | 
|---|
| 215 | //----------   Start of minimization part   -------------------- | 
|---|
| 216 | // | 
|---|
| 217 | // Do the minimization with MINUIT | 
|---|
| 218 | // | 
|---|
| 219 | // Be careful: This is not thread safe | 
|---|
| 220 | // | 
|---|
| 221 | *fLog << inf << "--------------------------------------" << endl; | 
|---|
| 222 | *fLog << inf << "Start minimization for the energy estimator" << endl; | 
|---|
| 223 |  | 
|---|
| 224 | gMinuit = new TMinuit(12); | 
|---|
| 225 | gMinuit->SetPrintLevel(-1); | 
|---|
| 226 |  | 
|---|
| 227 | gMinuit->SetFCN(fcn); | 
|---|
| 228 | gMinuit->SetObjectFit(&evtloop); | 
|---|
| 229 |  | 
|---|
| 230 | // Ready for: gMinuit->mnexcm("SET ERR", arglist, 1, ierflg) | 
|---|
| 231 |  | 
|---|
| 232 | if (gMinuit->SetErrorDef(1)) | 
|---|
| 233 | { | 
|---|
| 234 | *fLog << err << dbginf << "SetErrorDef failed." << endl; | 
|---|
| 235 | return; | 
|---|
| 236 | } | 
|---|
| 237 |  | 
|---|
| 238 | // | 
|---|
| 239 | // Set starting values and step sizes for parameters | 
|---|
| 240 | // | 
|---|
| 241 | for (Int_t i=0; i<fA.GetSize(); i++) | 
|---|
| 242 | { | 
|---|
| 243 | TString name = "fA["; | 
|---|
| 244 | name += i; | 
|---|
| 245 | name += "]"; | 
|---|
| 246 | Double_t vinit = fA[i]; | 
|---|
| 247 | Double_t step  = fabs(fA[i]/3); | 
|---|
| 248 |  | 
|---|
| 249 | Double_t limlo = 0; // limlo=limup=0: no limits | 
|---|
| 250 | Double_t limup = 0; | 
|---|
| 251 |  | 
|---|
| 252 | Bool_t rc = gMinuit->DefineParameter(i, name, vinit, step, limlo, limup); | 
|---|
| 253 | if (!rc) | 
|---|
| 254 | continue; | 
|---|
| 255 |  | 
|---|
| 256 | *fLog << err << dbginf << "Error in defining parameter #" << i << endl; | 
|---|
| 257 | return; | 
|---|
| 258 | } | 
|---|
| 259 |  | 
|---|
| 260 | for (Int_t i=0; i<fB.GetSize(); i++) | 
|---|
| 261 | { | 
|---|
| 262 | TString name = "fB["; | 
|---|
| 263 | name += i; | 
|---|
| 264 | name += "]"; | 
|---|
| 265 | Double_t vinit = fB[i]; | 
|---|
| 266 | Double_t step  = fabs(fB[i]/3); | 
|---|
| 267 |  | 
|---|
| 268 | Double_t limlo = 0; // limlo=limup=0: no limits | 
|---|
| 269 | Double_t limup = 0; | 
|---|
| 270 |  | 
|---|
| 271 | Bool_t rc = gMinuit->DefineParameter(i+fA.GetSize(), name, vinit, step, limlo, limup); | 
|---|
| 272 | if (!rc) | 
|---|
| 273 | continue; | 
|---|
| 274 |  | 
|---|
| 275 | *fLog << err << dbginf << "Error in defining parameter #" << i+fA.GetSize() << endl; | 
|---|
| 276 | return; | 
|---|
| 277 | } | 
|---|
| 278 |  | 
|---|
| 279 | TStopwatch clock; | 
|---|
| 280 | clock.Start(); | 
|---|
| 281 |  | 
|---|
| 282 | // Now ready for minimization step: | 
|---|
| 283 |  | 
|---|
| 284 | gLog.SetNullOutput(kTRUE); | 
|---|
| 285 | Bool_t rc = gMinuit->Migrad(); | 
|---|
| 286 | gLog.SetNullOutput(kFALSE); | 
|---|
| 287 |  | 
|---|
| 288 | if (rc) | 
|---|
| 289 | { | 
|---|
| 290 | *fLog << err << dbginf << "Migrad failed." << endl; | 
|---|
| 291 | return; | 
|---|
| 292 | } | 
|---|
| 293 |  | 
|---|
| 294 | *fLog << inf << endl; | 
|---|
| 295 | clock.Stop(); | 
|---|
| 296 | clock.Print(); | 
|---|
| 297 | *fLog << inf << endl; | 
|---|
| 298 |  | 
|---|
| 299 | *fLog << inf << "Resulting Chisq: " << gMinuit->fAmin << endl; | 
|---|
| 300 |  | 
|---|
| 301 | // | 
|---|
| 302 | // Update values of fA, fB: | 
|---|
| 303 | // | 
|---|
| 304 | for (Int_t i = 0; i < fA.GetSize(); i++) | 
|---|
| 305 | { | 
|---|
| 306 | Double_t x1, x2; | 
|---|
| 307 | gMinuit->GetParameter(i,x1,x2); | 
|---|
| 308 | fA[i] = x1; | 
|---|
| 309 | } | 
|---|
| 310 | for (Int_t i = fA.GetSize(); i < fA.GetSize()+fB.GetSize(); i++) | 
|---|
| 311 | { | 
|---|
| 312 | Double_t x1, x2; | 
|---|
| 313 | gMinuit->GetParameter(i,x1,x2); | 
|---|
| 314 | fB[i-fA.GetSize()] = x1; | 
|---|
| 315 | } | 
|---|
| 316 |  | 
|---|
| 317 | //    eest.Print(); | 
|---|
| 318 | eest.StopMapping(); | 
|---|
| 319 | *fLog << inf << "Minimization for the energy estimator finished" << endl; | 
|---|
| 320 |  | 
|---|
| 321 | } | 
|---|
| 322 |  | 
|---|
| 323 | //------------------------------------------------------------------------ | 
|---|
| 324 | // | 
|---|
| 325 | // Print current values of parameters | 
|---|
| 326 | // | 
|---|
| 327 | void MMcEnergyEst::Print(Option_t *o) const | 
|---|
| 328 | { | 
|---|
| 329 | for (Int_t i = 0; i < fA.GetSize(); i++) | 
|---|
| 330 | *fLog << inf << "Parameter " << i << ": " << const_cast<TArrayD&>(fA)[i] << endl; | 
|---|
| 331 |  | 
|---|
| 332 | for (Int_t i = fA.GetSize(); i < fA.GetSize()+fB.GetSize(); i++) | 
|---|
| 333 | *fLog << inf << "Parameter " << i << ": " << const_cast<TArrayD&>(fB)[i-fA.GetSize()] << endl; | 
|---|
| 334 |  | 
|---|
| 335 | /* | 
|---|
| 336 | // Print results | 
|---|
| 337 | Double_t amin, edm, errdef; | 
|---|
| 338 | Int_t nvpar, nparx, icstat; | 
|---|
| 339 | gMinuit->mnstat(amin, edm, errdef, nvpar, nparx, icstat); | 
|---|
| 340 | gMinuit->mnprin(3, amin); | 
|---|
| 341 | */ | 
|---|
| 342 |  | 
|---|
| 343 | } | 
|---|