1 |
#include "TString.h"
|
2 |
#include "TDirectory.h"
|
3 |
#include "TH1F.h"
|
4 |
#include "TFile.h"
|
5 |
#include "TCanvas.h"
|
6 |
#include "TLegend.h"
|
7 |
#include "TROOT.h"
|
8 |
#include "TKey.h"
|
9 |
#include "TH2F.h"
|
10 |
#include "TPad.h"
|
11 |
#include "TObjArray.h"
|
12 |
#include "TText.h"
|
13 |
|
14 |
#include "network.C"
|
15 |
|
16 |
void DrawNetworkMovie( TFile* file, const TString& methodType, const TString& methodTitle )
|
17 |
{
|
18 |
|
19 |
TString dirname = methodType + "/" + methodTitle + "/" + "EpochMonitoring";
|
20 |
TDirectory *epochDir = (TDirectory*)file->Get( dirname );
|
21 |
if (!epochDir) {
|
22 |
cout << "Big troubles: could not find directory \"" << dirname << "\"" << endl;
|
23 |
exit(1);
|
24 |
}
|
25 |
epochDir->cd();
|
26 |
|
27 |
// loop over all epoch-wise monitoring histograms
|
28 |
TIter keyIt(epochDir->GetListOfKeys());
|
29 |
TKey *key;
|
30 |
vector<TString> epochList;
|
31 |
Int_t ic = 0;
|
32 |
while ((key = (TKey*)keyIt())) {
|
33 |
|
34 |
if (!gROOT->GetClass(key->GetClassName())->InheritsFrom("TH2F")) continue;
|
35 |
TString name = key->GetName();
|
36 |
|
37 |
if (!name.BeginsWith("epochmonitoring___")) continue;
|
38 |
|
39 |
// extract epoch
|
40 |
TObjArray* tokens = name.Tokenize("_");
|
41 |
TString es = ((TObjString*)tokens->At(2))->GetString();
|
42 |
|
43 |
// check if done already
|
44 |
Bool_t isOld = kFALSE;
|
45 |
for (vector<TString>::const_iterator it = epochList.begin(); it < epochList.end(); it++) {
|
46 |
if (*it == es) isOld = kTRUE;
|
47 |
}
|
48 |
if (isOld) continue;
|
49 |
epochList.push_back( es );
|
50 |
|
51 |
// create bulk file name
|
52 |
TString bulkname = Form( "epochmonitoring___epoch_%s_weights_hist", es.Data() );
|
53 |
|
54 |
// draw the network
|
55 |
if (ic <= 60) draw_network( file, epochDir, bulkname, kTRUE, es );
|
56 |
ic++;
|
57 |
}
|
58 |
}
|
59 |
|
60 |
|
61 |
void DrawMLPoutputMovie( TFile* file, const TString& methodType, const TString& methodTitle )
|
62 |
{
|
63 |
gROOT->SetBatch( 1 );
|
64 |
|
65 |
// define Canvas layout here!
|
66 |
const Int_t width = 600; // size of canvas
|
67 |
|
68 |
// this defines how many canvases we need
|
69 |
TCanvas* c = 0;
|
70 |
|
71 |
Float_t nrms = 4;
|
72 |
Float_t xmin = -1.2;
|
73 |
Float_t xmax = 1.2;
|
74 |
Float_t ymin = 0;
|
75 |
Float_t ymax = 0;
|
76 |
Float_t maxMult = 6.0;
|
77 |
Int_t countCanvas = 0;
|
78 |
Bool_t first = kTRUE;
|
79 |
|
80 |
TString dirname = methodType + "/" + methodTitle + "/" + "EpochMonitoring";
|
81 |
TDirectory *epochDir = (TDirectory*)file->Get( dirname );
|
82 |
if (!epochDir) {
|
83 |
cout << "Big troubles: could not find directory \"" << dirname << "\"" << endl;
|
84 |
exit(1);
|
85 |
}
|
86 |
|
87 |
// now read all evolution histograms
|
88 |
TIter keyItTit(epochDir->GetListOfKeys());
|
89 |
TKey *titkeyTit;
|
90 |
while ((titkeyTit = (TKey*)keyItTit())) {
|
91 |
|
92 |
if (!gROOT->GetClass(titkeyTit->GetClassName())->InheritsFrom("TH1F")) continue;
|
93 |
TString name = titkeyTit->GetName();
|
94 |
|
95 |
if (!name.BeginsWith("convergencetest___")) continue;
|
96 |
if (!name.Contains("_train_")) continue; // only for training so far
|
97 |
if (name.EndsWith( "_B")) continue;
|
98 |
|
99 |
// must be signal histogram
|
100 |
if (!name.EndsWith( "_S")) {
|
101 |
cout << "Big troubles with histogram: " << name << " -> should end with _S" << endl;
|
102 |
exit(1);
|
103 |
}
|
104 |
|
105 |
// create canvas
|
106 |
countCanvas++;
|
107 |
TString ctitle = Form("TMVA response %s",methodTitle.Data());
|
108 |
c = new TCanvas( Form("canvas%d", countCanvas), ctitle, 0, 0, width, (Int_t)width*0.78 );
|
109 |
|
110 |
TH1F* sig = (TH1F*)titkeyTit->ReadObj();
|
111 |
sig->SetTitle( Form("TMVA response for classifier: %s", methodTitle.Data()) );
|
112 |
|
113 |
TString dataType = (name.Contains("_train_") ? "(training sample)" : "(test sample)");
|
114 |
|
115 |
// find background
|
116 |
TString nbn = sig->GetName(); nbn[nbn.Length()-1] = 'B';
|
117 |
TH1F* bgd = dynamic_cast<TH1F*>(epochDir->Get( nbn ));
|
118 |
if (bgd == 0) {
|
119 |
cout << "Big troubles with histogram: " << bgd << " -> cannot find!" << endl;
|
120 |
exit(1);
|
121 |
}
|
122 |
|
123 |
cout << "sig = " << sig->GetName() << endl;
|
124 |
cout << "bgd = " << bgd->GetName() << endl;
|
125 |
|
126 |
// set the histogram style
|
127 |
TMVAGlob::SetSignalAndBackgroundStyle( sig, bgd );
|
128 |
|
129 |
// normalise both signal and background
|
130 |
TMVAGlob::NormalizeHists( sig, bgd );
|
131 |
|
132 |
// set only first time, then same for all plots
|
133 |
if (first) {
|
134 |
if (xmin == 0 && xmax == 0) {
|
135 |
xmin = TMath::Max( TMath::Min(sig->GetMean() - nrms*sig->GetRMS(),
|
136 |
bgd->GetMean() - nrms*bgd->GetRMS() ),
|
137 |
sig->GetXaxis()->GetXmin() );
|
138 |
xmax = TMath::Min( TMath::Max(sig->GetMean() + nrms*sig->GetRMS(),
|
139 |
bgd->GetMean() + nrms*bgd->GetRMS() ),
|
140 |
sig->GetXaxis()->GetXmax() );
|
141 |
}
|
142 |
ymin = 0;
|
143 |
ymax = TMath::Max( sig->GetMaximum(), bgd->GetMaximum() )*maxMult;
|
144 |
first = kFALSE;
|
145 |
}
|
146 |
|
147 |
// build a frame
|
148 |
Int_t nb = 100;
|
149 |
TString hFrameName(TString("frame") + methodTitle);
|
150 |
TObject *o = gROOT->FindObject(hFrameName);
|
151 |
if(o) delete o;
|
152 |
TH2F* frame = new TH2F( hFrameName, sig->GetTitle(),
|
153 |
nb, xmin, xmax, nb, ymin, ymax );
|
154 |
frame->GetXaxis()->SetTitle( methodTitle + " response" );
|
155 |
frame->GetYaxis()->SetTitle("(1/N) dN^{ }/^{ }dx");
|
156 |
TMVAGlob::SetFrameStyle( frame );
|
157 |
|
158 |
// find epoch number (4th token)
|
159 |
TObjArray* tokens = name.Tokenize("_");
|
160 |
TString es = ((TObjString*)tokens->At(4))->GetString();
|
161 |
if (!es.IsFloat()) {
|
162 |
cout << "Big troubles in epoch parsing: \"" << es << "\" is not float" << endl;
|
163 |
exit(1);
|
164 |
}
|
165 |
Int_t epoch = es.Atoi();
|
166 |
|
167 |
// eventually: draw the frame
|
168 |
frame->Draw();
|
169 |
|
170 |
c->GetPad(0)->SetLeftMargin( 0.105 );
|
171 |
frame->GetYaxis()->SetTitleOffset( 1.2 );
|
172 |
|
173 |
// Draw legend
|
174 |
TLegend *legend= new TLegend( c->GetLeftMargin(), 1 - c->GetTopMargin() - 0.12,
|
175 |
c->GetLeftMargin() + 0.5, 1 - c->GetTopMargin() );
|
176 |
legend->SetFillStyle( 1 );
|
177 |
legend->AddEntry(sig,TString("Signal ") + dataType, "F");
|
178 |
legend->AddEntry(bgd,TString("Background ") + dataType, "F");
|
179 |
legend->SetBorderSize(1);
|
180 |
legend->SetMargin( 0.15 );
|
181 |
legend->Draw("same");
|
182 |
|
183 |
TText* t = new TText();
|
184 |
t->SetTextSize( 0.04 );
|
185 |
t->SetTextColor( 1 );
|
186 |
t->SetTextAlign( 31 );
|
187 |
t->DrawTextNDC( 1 - c->GetRightMargin(), 1 - c->GetTopMargin() + 0.015, Form( "Epoch: %i", epoch) );
|
188 |
|
189 |
// overlay signal and background histograms
|
190 |
sig->Draw("samehist");
|
191 |
bgd->Draw("samehist");
|
192 |
|
193 |
// save to file
|
194 |
TString dirname = "movieplots";
|
195 |
TString foutname = dirname + "/" + name;
|
196 |
foutname.Resize( foutname.Length()-2 );
|
197 |
foutname.ReplaceAll("convergencetest___","");
|
198 |
foutname += ".gif";
|
199 |
|
200 |
cout << "storing file: " << foutname << endl;
|
201 |
|
202 |
c->Update();
|
203 |
c->Print(foutname);
|
204 |
}
|
205 |
}
|
206 |
|
207 |
// -----------------------------------------------------------------------------
|
208 |
|
209 |
void MovieMaker( TString methodType = "Method_MLP", TString methodTitle = "MLP" )
|
210 |
{
|
211 |
TString fname = "TMVA.root";
|
212 |
TFile* file = TMVAGlob::OpenFile( fname );
|
213 |
|
214 |
//DrawMLPoutputMovie( file, methodType, methodTitle );
|
215 |
DrawNetworkMovie( file, methodType, methodTitle );
|
216 |
}
|
217 |
|