source: trunk/MagicSoft/Mars/mjoptim/MJOptimizeDisp.cc@ 7169

Last change on this file since 7169 was 7169, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 5.2 KB
Line 
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
63// eventloop
64#include "MParList.h"
65#include "MTaskList.h"
66#include "MEvtLoop.h"
67
68// parameters
69#include "MParameters.h"
70#include "MGeomCamMagic.h"
71
72// histograms
73#include "../mhflux/MAlphaFitter.h"
74#include "../mhflux/MHThetaSq.h"
75#include "../mtools/MChisqEval.h"
76
77// tasks
78#include "MReadTree.h"
79#include "MMatrixLoop.h"
80#include "MParameterCalc.h"
81#include "MFillH.h"
82
83// filters
84#include "MFDataMember.h"
85
86using namespace std;
87
88ClassImp(MJOptimizeDisp);
89
90//------------------------------------------------------------------------
91//
92// Read all events from file which do match rules and optimize
93// energy estimator.
94//
95Bool_t MJOptimizeDisp::RunDisp(const char *fname, const char *rule, MTask *weights)
96{
97 fLog->Separator("Preparing Energy optimization");
98
99 MParList parlist;
100
101 MParameterI par1("DataType");
102 par1.SetVal(1);
103 parlist.AddToList(&par1);
104
105 MParameterD par2("ThetaSquaredCut");
106 par2.SetVal(0.2);
107 parlist.AddToList(&par2);
108
109 MAlphaFitter fit;
110 fit.SetPolynomOrder(0);
111 fit.SetSignalFitMax(0.8);
112 fit.EnableBackgroundFit(kFALSE);
113 fit.SetSignalFunction(MAlphaFitter::kThetaSq);
114 fit.SetMinimizationStrategy(MAlphaFitter::kGaussSigma);
115 parlist.AddToList(&fit);
116
117 MGeomCamMagic geom; // For GetConvMm2Deg
118 parlist.AddToList(&geom);
119
120 MHMatrix m("M");
121 AddRulesToMatrix(m);
122 parlist.AddToList(&m);
123
124 const Int_t num1 = m.AddColumn("MHillasSrc.fDist*MGeomCam.fConvMm2Deg");
125 const Int_t num2 = m.AddColumn("MHillasSrc.fAlpha*kDegToRad");
126
127 MHThetaSq hist;
128 hist.SkipHistTime();
129 hist.SkipHistTheta();
130 hist.SkipHistEnergy();
131 hist.InitMapping(&m);
132
133 MFDataMember filter("DataType.fVal", '>', 0.5);
134 fPreCuts.Add(&filter);
135
136 MParameterCalc calc1(rule, "MParameters");
137 calc1.SetNameParameter("Disp");
138 parlist.AddToList(&calc1);
139
140 const char *disp = "Disp.fVal";
141 const char *n1 = Form("M[%d]", num1);
142
143 const char *rule2 = Form("(%s*%s) + (%s*%s) - (2*%s*%s*cos(M[%d]))",
144 n1, n1, disp, disp, n1, disp, num2);
145
146 MParameterCalc calc2(rule2, "MThetaSqCalc");
147 calc2.SetNameParameter("ThetaSquared");
148
149 MReadTree read("Events");
150 // NECESSARY BECAUSE OF MDataFormula GetRules missing
151 read.DisableAutoScheme();
152 if (fname)
153 read.AddFile(fname);
154 else
155 AddSequences(read, fNamesOn);
156 if (!FillMatrix(read, parlist, kTRUE))
157 return kFALSE;
158
159 fPreCuts.Remove(&filter);
160
161 MTaskList tasklist;
162 parlist.Replace(&tasklist);
163
164 MFillH fill(&hist);
165 if (weights)
166 fill.SetWeight();
167
168 MChisqEval eval;
169 eval.SetY1("sqrt(ThetaSquared.fVal)");
170
171 MMatrixLoop loop(&m);
172
173 tasklist.AddToList(&loop);
174 tasklist.AddToList(&calc1);
175 tasklist.AddToList(&calc2);
176 if (weights)
177 tasklist.AddToList(weights);
178 tasklist.AddToList(&fill);
179 tasklist.AddToList(&eval);
180
181 // Optimize with the tasklist in this parameterlist
182 if (!Optimize(parlist))
183 return kFALSE;
184
185 // Print the result
186 *fLog << inf << "Rule: " << rule << endl;
187 hist.GetAlphaFitter().Print("result");
188
189 // Store result if requested
190 TObjArray cont;
191 cont.Add(&calc1);
192 return WriteContainer(cont, fNameOut);
193}
Note: See TracBrowser for help on using the repository browser.