source: trunk/MagicSoft/Mars/mtools/MHSimulatedAnnealing.cc@ 7238

Last change on this file since 7238 was 5832, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 6.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): Markus Gaug <mailto:markus@ifae.es>
19!
20! Copyright: MAGIC Software Development, 2000-2002
21!
22!
23\* ======================================================================== */
24
25///////////////////////////////////////////////////////////////////////
26//
27// MHSimulatedAnnealing
28//
29// This class contains different histograms of the Simulated Annealing
30// Snapshort during optimization and the final results
31//
32///////////////////////////////////////////////////////////////////////
33#include "MHSimulatedAnnealing.h"
34
35#include <TVector.h>
36#include <TMatrix.h>
37#include <TObjArray.h>
38
39#include <TStyle.h>
40#include <TCanvas.h>
41
42#include "MBinning.h"
43
44ClassImp(MHSimulatedAnnealing);
45
46// --------------------------------------------------------------------------
47//
48// Setup histograms
49//
50MHSimulatedAnnealing::MHSimulatedAnnealing(UShort_t moves, UShort_t ndim,
51 const char *name,
52 const char *title)
53 : fDim(ndim), fMoves(moves), fTimeEvolution(NULL), fBestEver(), fBestFuncEval()
54{
55
56 //
57 // set the name and title of this object
58 //
59 fName = name ? name : "MHSimulatedAnnealing";
60 fTitle = title ? title : "Output Histograms of a Simulated Annealing Run";
61
62 fBestEver.SetName("Hist_BestEver");
63 fBestEver.SetTitle("Best Param. Combinations");
64 fBestEver.SetXTitle("Run Duration");
65 fBestEver.SetYTitle("Parameter Nr.");
66 fBestEver.SetZTitle("Parameter Value");
67 fBestEver.SetDirectory(NULL);
68
69 fBestFuncEval.SetName("Hist_BestFuncEval");
70 fBestFuncEval.SetTitle("Best Function Evaluation");
71 fBestFuncEval.SetXTitle("Run Duration");
72 fBestFuncEval.SetYTitle("Function Value");
73 fBestFuncEval.SetDirectory(NULL);
74
75 MBinning binsx, binsy;
76 binsx.SetEdges(fMoves+1, 0, 1);
77 binsy.SetEdges(fDim, 0.5, fDim+0.5);
78 MH::SetBinning(&fBestEver, &binsx, &binsy);
79
80 // For better visibility, omit the first entry in fBestFuncEval
81 // It has nothing significant, anyway
82 binsx.SetEdges(fMoves,1./(fMoves+1), 1);
83 binsx.Apply(fBestFuncEval);
84
85}
86
87void MHSimulatedAnnealing::InitFullSimplex()
88{
89 if (fTimeEvolution)
90 delete fTimeEvolution;
91
92 fTimeEvolution = new TObjArray;
93 fTimeEvolution->SetOwner();
94
95 for (Int_t i=0;i<fDim;i++)
96 {
97 TH2F *hist = new TH2F(Form("Hist_%d", i), Form("Parameter %d", i),
98 fMoves+1, 0., 1.,fDim+1,0.5,fDim+1.5);
99 hist->SetXTitle("Run Duration");
100 hist->SetYTitle("Point Nr. Simplex");
101 hist->SetZTitle(Form("Value of Parameter %d",i));
102 fTimeEvolution->Add(hist);
103 }
104}
105
106Bool_t MHSimulatedAnnealing::StoreFullSimplex(const TMatrix &p, const UShort_t move)
107{
108
109 if (!fTimeEvolution)
110 return kFALSE;
111
112 Int_t idx=0;
113 const Axis_t bin = (move-0.5)/(fMoves+1);
114
115 TIter Next(fTimeEvolution);
116 TH2F *hist=NULL;
117 while ((hist=(TH2F*)Next()))
118 {
119 for (Int_t i=0;i<fDim+1;i++)
120 hist->Fill(bin,i,p(i,idx));
121 idx++;
122 }
123 return kTRUE;
124}
125
126Bool_t MHSimulatedAnnealing::StoreBestValueEver(const TVector &y, const Float_t yb, const UShort_t move)
127{
128 if (y.GetNrows() != fDim)
129 return kFALSE;
130
131 const Axis_t bin = (move-0.5)/(fMoves+1);
132
133 for (Int_t i=0;i<fDim;i++)
134 fBestEver.Fill(bin,0.5+i,((TVector)y)(i));
135
136 fBestFuncEval.Fill(bin,yb);
137
138 return kTRUE;
139}
140
141Bool_t MHSimulatedAnnealing::ChangeTitle(const UShort_t index, const char* title)
142{
143 if (!fTimeEvolution)
144 return kFALSE;
145
146 TH2F *hist = NULL;
147 if (!(hist = (TH2F*)fTimeEvolution->At(index)))
148 return kFALSE;
149
150 hist->SetNameTitle(Form("Hist_%s",title),title);
151 hist->SetYTitle(Form("Value of Parameter %s",title));
152
153 return kTRUE;
154}
155
156void MHSimulatedAnnealing::ChangeFuncTitle(const char* title)
157{
158 fBestFuncEval.SetTitle(title);
159}
160
161TObject *MHSimulatedAnnealing::DrawClone(Option_t *opt) const
162{
163 UShort_t i=2;
164
165 TCanvas *c = MakeDefCanvas(this, 720, 810);
166 if (fTimeEvolution)
167 c->Divide(2,(int)(fDim/2.)+1);
168 else
169 gPad->Divide(1,2);
170
171 gROOT->SetSelectedPad(NULL);
172
173 c->cd(1);
174 gStyle->SetOptStat(0);
175 ((TH1&)fBestFuncEval).DrawCopy();
176
177 c->cd(2);
178 gStyle->SetOptStat(10);
179 ((TH2&)fBestEver).DrawCopy(opt);
180
181 if (fTimeEvolution)
182 {
183 TH2F *hist = NULL;
184 TIter Next(fTimeEvolution);
185 while ((hist=(TH2F*)Next()))
186 {
187 c->cd(++i);
188 hist->DrawCopy(opt);
189 }
190 }
191 c->Modified();
192 c->Update();
193
194 return c;
195}
196
197
198
199// --------------------------------------------------------------------------
200//
201// Draw all histograms.
202//
203void MHSimulatedAnnealing::Draw(Option_t *opt)
204{
205 UShort_t i=2;
206
207 if (!gPad)
208 MakeDefCanvas(this,780,940);
209
210 if (fTimeEvolution)
211 gPad->Divide(2,(int)(fDim/2.)+1);
212 else
213 gPad->Divide(1,2);
214
215 gPad->cd(1);
216 gStyle->SetOptStat(0);
217 fBestFuncEval.Draw();
218 gPad->Modified();
219 gPad->Update();
220
221 gPad->cd(2);
222 gStyle->SetOptStat(10);
223 fBestEver.Draw(opt);
224 gPad->Modified();
225 gPad->Update();
226
227 if (!fTimeEvolution)
228 return;
229
230 TH2F *hist = NULL;
231 TIter Next(fTimeEvolution);
232 while ((hist=(TH2F*)Next()))
233 {
234 gPad->cd(++i);
235 hist->Draw(opt);
236 }
237 gPad->Modified();
238 gPad->Update();
239}
240
241// --------------------------------------------------------------------------
242//
243// Delete the histograms.
244//
245MHSimulatedAnnealing::~MHSimulatedAnnealing()
246{
247 if (fTimeEvolution)
248 delete fTimeEvolution;
249}
250
Note: See TracBrowser for help on using the repository browser.