source: trunk/MagicSoft/Mars/mjoptim/MJOptimizeEnergy.cc@ 9620

Last change on this file since 9620 was 8679, checked in by tbretz, 17 years ago
*** empty log message ***
File size: 4.3 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, 9/2004 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2004
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MJOptimize
28//
29// Class for otimizing a rule to estimate the energy. For more details see
30// MJOptimize.
31//
32// Example:
33// --------
34//
35// MJOptimizeEnergy opt;
36// opt.SetDebug(2);
37// opt.SetOptimizer(MJOptimize::kMigrad);
38// opt.SetNumEvents(20000);
39// opt.EnableTestTrain();
40// opt.AddParameter("MHillas.fSize");
41// opt.SetParameter(0, 1, 0, 2);
42// char *r = "[0]*M[0]"; //Rule to calculate estimated energy
43// MStatusDisplay *d = new MStatusDisplay;
44// opt.SetDisplay(d);
45// opt.RunDisp("ganymed-summary.root", r);
46//
47/////////////////////////////////////////////////////////////////////////////
48#include "MJOptimizeEnergy.h"
49
50#include "MHMatrix.h"
51
52// environment
53#include "MLog.h"
54#include "MLogManip.h"
55#include "MStatusDisplay.h"
56
57// eventloop
58#include "MParList.h"
59#include "MTaskList.h"
60#include "MEvtLoop.h"
61
62// parameters
63#include "MParameters.h"
64#include "MGeomCamMagic.h"
65
66// histograms
67#include "../mhflux/MHEnergyEst.h"
68#include "../mtools/MChisqEval.h"
69
70// tasks
71#include "MReadTree.h"
72#include "MMatrixLoop.h"
73#include "MEnergyEstimate.h"
74#include "MFillH.h"
75
76// filters
77#include "MFDataMember.h"
78
79using namespace std;
80
81ClassImp(MJOptimizeEnergy);
82
83//------------------------------------------------------------------------
84//
85// Read all events from file which do match rules and optimize
86// energy estimator.
87//
88Bool_t MJOptimizeEnergy::RunEnergy(const char *fname, const char *rule, MTask *weights)
89{
90 SetTitle(Form("OptimizeEnergy: %s", fname));
91
92 if (fDisplay)
93 fDisplay->SetTitle(fTitle);
94
95 fLog->Separator("Preparing Energy optimization");
96
97 MParList parlist;
98
99 MParameterI par("DataType");
100 par.SetVal(1);
101 parlist.AddToList(&par);
102
103 MFDataMember filter("DataType.fVal", '>', 0.5);
104 fPreCuts.Add(&filter);
105
106 MGeomCamMagic geom; // For GetConvMm2Deg
107 parlist.AddToList(&geom);
108
109 MHMatrix m("M");
110 AddRulesToMatrix(m);
111 const Int_t map = m.AddColumn("MMcEvt.fEnergy");
112 parlist.AddToList(&m);
113
114 MHEnergyEst hist;
115 hist.InitMapping(&m);
116
117 MParameterCalc est(rule, "MParameters");
118 est.SetNameParameter("MEnergyEst");
119 parlist.AddToList(&est);
120
121 MReadTree read("Events");
122 // NECESSARY BECAUSE OF MDataFormula GetRules missing
123 read.DisableAutoScheme();
124 if (fname)
125 read.AddFile(fname);
126 else
127 AddSequences(read, fNamesOn);
128 if (!FillMatrix(read, parlist, kTRUE))
129 return kFALSE;
130
131 fPreCuts.Remove(&filter);
132
133 MTaskList tasklist;
134 parlist.Replace(&tasklist);
135
136 MFillH fill(&hist);
137 if (weights)
138 fill.SetWeight();
139
140 MChisqEval eval;
141 eval.SetY1(fOptimLog?Form("log10(MEnergyEst.fVal/M[%d])", map):Form("MEnergyEst.fVal-M[%d]", map));
142 if (weights)
143 eval.SetNameWeight();
144
145 MMatrixLoop loop(&m);
146
147 tasklist.AddToList(&loop);
148 tasklist.AddToList(&est);
149 if (weights)
150 tasklist.AddToList(weights);
151 tasklist.AddToList(&fill);
152 tasklist.AddToList(&eval);
153
154 // Optimize with the tasklist in this parameterlist
155 if (!Optimize(parlist))
156 return kFALSE;
157
158 // Print the result
159 *fLog << inf << "Finished processing of " << fname << endl;
160 *fLog << inf << "With Rule: " << rule << endl;
161 hist.Print();
162
163 // Store result if requested
164 TObjArray cont;
165 cont.Add(&est);
166 if (fDisplay)
167 cont.Add(fDisplay);
168 return WriteContainer(cont, fNameOut);
169}
Note: See TracBrowser for help on using the repository browser.