| 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, 6/2005 <mailto:tbretz@astro.uni-wuerzburg.de> | 
|---|
| 19 | ! | 
|---|
| 20 | !   Copyright: MAGIC Software Development, 2000-2005 | 
|---|
| 21 | ! | 
|---|
| 22 | ! | 
|---|
| 23 | \* ======================================================================== */ | 
|---|
| 24 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| 25 | // | 
|---|
| 26 | // MJOptimizeDisp | 
|---|
| 27 | // | 
|---|
| 28 | // Class for otimizing the disp parameters. For more details see | 
|---|
| 29 | // MJOptimize. | 
|---|
| 30 | // | 
|---|
| 31 | // MJOptimizeDisp minimzes the width of the Theta^2 distribution by | 
|---|
| 32 | // minimizing <Theta^2> (the RMS^2 of Theta^2), while Theta^2 is | 
|---|
| 33 | // calculated as: | 
|---|
| 34 | //        Theta^2 = d^2 + p^2 - 2*d*p*cos(a) | 
|---|
| 35 | // with: | 
|---|
| 36 | //        d: MHillasSrc.fDist [deg] | 
|---|
| 37 | //        p: Disp as calculated by the given rule, eg: [0] (a constant) | 
|---|
| 38 | //        a: MHillasSrc.fAlpha [rad] | 
|---|
| 39 | // | 
|---|
| 40 | // Example: | 
|---|
| 41 | // -------- | 
|---|
| 42 | //    MJOptimizeDisp opt; | 
|---|
| 43 | //    opt.SetDebug(2); | 
|---|
| 44 | //    opt.SetOptimizer(MJOptimize::kMigrad); | 
|---|
| 45 | //    opt.SetNumEvents(20000); | 
|---|
| 46 | //    opt.EnableTestTrain(); | 
|---|
| 47 | //    opt.AddParameter("1-(MHillas.fWidth/MHillas.fLength)"); | 
|---|
| 48 | //    opt.SetParameter(0, 1, 0, 2); | 
|---|
| 49 | //    char *r = "[0]*M[0]"; //Rule to calculate disp | 
|---|
| 50 | //    MStatusDisplay *d = new MStatusDisplay; | 
|---|
| 51 | //    opt.SetDisplay(d); | 
|---|
| 52 | //    opt.RunDisp("ganymed-summary.root", r); | 
|---|
| 53 | // | 
|---|
| 54 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| 55 | #include "MJOptimizeDisp.h" | 
|---|
| 56 |  | 
|---|
| 57 | #include "MHMatrix.h" | 
|---|
| 58 |  | 
|---|
| 59 | // environment | 
|---|
| 60 | #include "MLog.h" | 
|---|
| 61 | #include "MLogManip.h" | 
|---|
| 62 | #include "MStatusDisplay.h" | 
|---|
| 63 |  | 
|---|
| 64 | // eventloop | 
|---|
| 65 | #include "MParList.h" | 
|---|
| 66 | #include "MTaskList.h" | 
|---|
| 67 | #include "MEvtLoop.h" | 
|---|
| 68 |  | 
|---|
| 69 | // parameters | 
|---|
| 70 | #include "MParameters.h" | 
|---|
| 71 | #include "MGeomCamMagic.h" | 
|---|
| 72 |  | 
|---|
| 73 | // histograms | 
|---|
| 74 | #include "MH3.h" | 
|---|
| 75 | #include "MBinning.h" | 
|---|
| 76 | #include "../mhflux/MAlphaFitter.h" | 
|---|
| 77 | #include "../mhflux/MHThetaSq.h" | 
|---|
| 78 | #include "../mtools/MChisqEval.h" | 
|---|
| 79 |  | 
|---|
| 80 | // tasks | 
|---|
| 81 | #include "MReadTree.h" | 
|---|
| 82 | #include "MMatrixLoop.h" | 
|---|
| 83 | #include "MParameterCalc.h" | 
|---|
| 84 | #include "MFillH.h" | 
|---|
| 85 |  | 
|---|
| 86 | // filters | 
|---|
| 87 | #include "MFDataMember.h" | 
|---|
| 88 |  | 
|---|
| 89 | using namespace std; | 
|---|
| 90 |  | 
|---|
| 91 | ClassImp(MJOptimizeDisp); | 
|---|
| 92 |  | 
|---|
| 93 | //------------------------------------------------------------------------ | 
|---|
| 94 | // | 
|---|
| 95 | // Read all events from file which do match rules and optimize | 
|---|
| 96 | // disp estimator. | 
|---|
| 97 | // | 
|---|
| 98 | Bool_t MJOptimizeDisp::RunDisp(const char *fname, const char *rule, MTask *weights) | 
|---|
| 99 | { | 
|---|
| 100 | SetTitle(Form("OptimizeDisp: %s", fname)); | 
|---|
| 101 |  | 
|---|
| 102 | if (fDisplay) | 
|---|
| 103 | fDisplay->SetTitle(fTitle); | 
|---|
| 104 |  | 
|---|
| 105 | fLog->Separator("Preparing Disp optimization"); | 
|---|
| 106 |  | 
|---|
| 107 | MParList parlist; | 
|---|
| 108 |  | 
|---|
| 109 | MParameterI par1("DataType"); | 
|---|
| 110 | par1.SetVal(1); | 
|---|
| 111 | parlist.AddToList(&par1); | 
|---|
| 112 |  | 
|---|
| 113 | MParameterD par2("ThetaSquaredCut"); | 
|---|
| 114 | par2.SetVal(0.2); | 
|---|
| 115 | parlist.AddToList(&par2); | 
|---|
| 116 |  | 
|---|
| 117 | MAlphaFitter fit; | 
|---|
| 118 | fit.SetPolynomOrder(0); | 
|---|
| 119 | fit.SetSignalFitMax(0.8); | 
|---|
| 120 | fit.EnableBackgroundFit(kFALSE); | 
|---|
| 121 | fit.SetSignalFunction(MAlphaFitter::kThetaSq); | 
|---|
| 122 | fit.SetMinimizationStrategy(MAlphaFitter::kGaussSigma); | 
|---|
| 123 | parlist.AddToList(&fit); | 
|---|
| 124 |  | 
|---|
| 125 | // To avoid this hard-coded we have to switch to MReadMarsFile | 
|---|
| 126 | MGeomCamMagic geom; // For GetConvMm2Deg | 
|---|
| 127 | parlist.AddToList(&geom); | 
|---|
| 128 |  | 
|---|
| 129 | MHMatrix m("M"); | 
|---|
| 130 | AddRulesToMatrix(m); | 
|---|
| 131 | parlist.AddToList(&m); | 
|---|
| 132 |  | 
|---|
| 133 | const Int_t num1 = m.AddColumn("MHillasSrc.fDist*MGeomCam.fConvMm2Deg"); | 
|---|
| 134 | const Int_t num2 = m.AddColumn("MHillasSrc.fAlpha*TMath::DegToRad()"); | 
|---|
| 135 | const Int_t num3 = m.AddColumn("MHillas.fSize"); | 
|---|
| 136 |  | 
|---|
| 137 | MHThetaSq hist; | 
|---|
| 138 | hist.SkipHistTime(); | 
|---|
| 139 | hist.SkipHistTheta(); | 
|---|
| 140 | //hist.SkipHistEnergy(); | 
|---|
| 141 | //hist.ForceUsingSize(); | 
|---|
| 142 | hist.InitMapping(&m, 1); | 
|---|
| 143 |  | 
|---|
| 144 | MFDataMember filter("DataType.fVal", '>', 0.5); | 
|---|
| 145 | fPreCuts.Add(&filter); | 
|---|
| 146 |  | 
|---|
| 147 | MParameterCalc calc1(rule, "MParameters"); | 
|---|
| 148 | calc1.SetNameParameter("Disp"); | 
|---|
| 149 | parlist.AddToList(&calc1); | 
|---|
| 150 |  | 
|---|
| 151 | const char *disp = "Disp.fVal"; | 
|---|
| 152 | const char *n1   = Form("M[%d]", num1); | 
|---|
| 153 |  | 
|---|
| 154 | const char *rule2 = Form("(%s*%s) + (%s*%s) - (2*%s*%s*cos(M[%d]))", | 
|---|
| 155 | n1, n1, disp, disp, n1, disp, num2); | 
|---|
| 156 |  | 
|---|
| 157 | MParameterCalc calc2(rule2, "MThetaSqCalc"); | 
|---|
| 158 | calc2.SetNameParameter("ThetaSquared"); | 
|---|
| 159 |  | 
|---|
| 160 | MReadTree read("Events"); | 
|---|
| 161 | // NECESSARY BECAUSE OF MDataFormula GetRules missing | 
|---|
| 162 | read.DisableAutoScheme(); | 
|---|
| 163 | if (fname) | 
|---|
| 164 | read.AddFile(fname); | 
|---|
| 165 | else | 
|---|
| 166 | AddSequences(read, fNamesOn); | 
|---|
| 167 | if (!FillMatrix(read, parlist, kTRUE)) | 
|---|
| 168 | return kFALSE; | 
|---|
| 169 |  | 
|---|
| 170 | fPreCuts.Remove(&filter); | 
|---|
| 171 |  | 
|---|
| 172 | MTaskList tasklist; | 
|---|
| 173 | parlist.Replace(&tasklist); | 
|---|
| 174 |  | 
|---|
| 175 | MFillH fill(&hist); | 
|---|
| 176 | if (weights) | 
|---|
| 177 | fill.SetWeight(); | 
|---|
| 178 |  | 
|---|
| 179 | MChisqEval eval; | 
|---|
| 180 | eval.SetY1(fUseThetaSq?"ThetaSquared.fVal":"sqrt(ThetaSquared.fVal)"); | 
|---|
| 181 | if (weights) | 
|---|
| 182 | eval.SetNameWeight(); | 
|---|
| 183 |  | 
|---|
| 184 | MMatrixLoop loop(&m); | 
|---|
| 185 |  | 
|---|
| 186 | const char *n3   = Form("M[%d]", num3); | 
|---|
| 187 | MH3 hdisp(n3, "sqrt(ThetaSquared.fVal)"); | 
|---|
| 188 | hdisp.SetTitle("\\vartheta^{2} distribution vs. Size:Size [phe]:\\vartheta^{2} [\\circ^{2}]"); | 
|---|
| 189 |  | 
|---|
| 190 | MBinning binsx(100, 10, 100000, "BinningMH3X", "log"); | 
|---|
| 191 | MBinning binsy(100, 0,  2,      "BinningMH3Y", "lin"); | 
|---|
| 192 |  | 
|---|
| 193 | parlist.AddToList(&binsx); | 
|---|
| 194 | parlist.AddToList(&binsy); | 
|---|
| 195 |  | 
|---|
| 196 | MFillH fillh2(&hdisp); | 
|---|
| 197 | fillh2.SetDrawOption("blue profx"); | 
|---|
| 198 |  | 
|---|
| 199 | tasklist.AddToList(&loop); | 
|---|
| 200 | tasklist.AddToList(&calc1); | 
|---|
| 201 | tasklist.AddToList(&calc2); | 
|---|
| 202 | if (weights) | 
|---|
| 203 | tasklist.AddToList(weights); | 
|---|
| 204 | tasklist.AddToList(&fill); | 
|---|
| 205 | tasklist.AddToList(&fillh2); | 
|---|
| 206 | tasklist.AddToList(&eval); | 
|---|
| 207 |  | 
|---|
| 208 | // Optimize with the tasklist in this parameterlist | 
|---|
| 209 | if (!Optimize(parlist)) | 
|---|
| 210 | return kFALSE; | 
|---|
| 211 |  | 
|---|
| 212 | // Print the result | 
|---|
| 213 | *fLog << inf; | 
|---|
| 214 | *fLog << "Finished processing of " << fname << endl; | 
|---|
| 215 | *fLog << "With Rule: " << rule << endl; | 
|---|
| 216 | hist.GetAlphaFitter().Print("result"); | 
|---|
| 217 |  | 
|---|
| 218 | // Store result if requested | 
|---|
| 219 | TObjArray cont; | 
|---|
| 220 | if (fDisplay) | 
|---|
| 221 | cont.Add(fDisplay); | 
|---|
| 222 | cont.Add(&calc1); | 
|---|
| 223 | return WriteContainer(cont, fNameOut); | 
|---|
| 224 | } | 
|---|