Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
MovieMaker.cxx
Go to the documentation of this file.
1#include "TMVA/MovieMaker.h"
2#include "TString.h"
3#include "TDirectory.h"
4#include "TH1F.h"
5#include "TFile.h"
6#include "TCanvas.h"
7#include "TLegend.h"
8#include "TROOT.h"
9#include "TKey.h"
10#include "TH2F.h"
11#include "TObjArray.h"
12#include "TText.h"
13#include "TObjString.h"
14
15#include <vector>
16#include "TMVA/network.h"
17
18void TMVA::DrawNetworkMovie(TString dataset, TFile* file, const TString& methodType, const TString& methodTitle )
19{
20
21 TString dirname = methodType + "/" + methodTitle + "/" + "EpochMonitoring";
22 TDirectory *epochDir = (TDirectory*)file->GetDirectory(dataset.Data())->Get( dirname );
23 if (!epochDir) {
24 cout << "Big troubles: could not find directory \"" << dirname << "\"" << endl;
25 exit(1);
26 }
27 epochDir->cd();
28
29 // loop over all epoch-wise monitoring histograms
30 TIter keyIt(epochDir->GetListOfKeys());
31 TKey *key;
32 std::vector<TString> epochList;
33 Int_t ic = 0;
34 while ((key = (TKey*)keyIt())) {
35
36 if (!gROOT->GetClass(key->GetClassName())->InheritsFrom("TH2F")) continue;
37 TString name = key->GetName();
38
39 if (!name.BeginsWith("epochmonitoring___")) continue;
40
41 // extract epoch
42 TObjArray* tokens = name.Tokenize("_");
43 TString es = ((TObjString*)tokens->At(2))->GetString();
44
45 // check if done already
46 Bool_t isOld = kFALSE;
47 for (std::vector<TString>::const_iterator it = epochList.begin(); it < epochList.end(); ++it) {
48 if (*it == es) isOld = kTRUE;
49 }
50 if (isOld) continue;
51 epochList.push_back( es );
52
53 // create bulk file name
54 TString bulkname = Form( "epochmonitoring___epoch_%s_weights_hist", es.Data() );
55
56 // draw the network
57 if (ic <= 60) draw_network(dataset, file, epochDir, bulkname, kTRUE, es );
58 ic++;
59 }
60}
61
62
63void TMVA::DrawMLPoutputMovie(TString dataset, TFile* file, const TString& methodType, const TString& methodTitle )
64{
65 gROOT->SetBatch( 1 );
66
67 // define Canvas layout here!
68 const Int_t width = 600; // size of canvas
69
70 // this defines how many canvases we need
71 TCanvas* c = 0;
72
73 Float_t nrms = 4;
74 Float_t xmin = -1.2;
75 Float_t xmax = 1.2;
76 Float_t ymin = 0;
77 Float_t ymax = 0;
78 Float_t maxMult = 6.0;
79 Int_t countCanvas = 0;
81
82 TString dirname = methodType + "/" + methodTitle + "/" + "EpochMonitoring";
83 TDirectory *epochDir = (TDirectory*)file->GetDirectory(dataset.Data())->Get( dirname );
84 if (!epochDir) {
85 cout << "Big troubles: could not find directory \"" << dirname << "\"" << endl;
86 exit(1);
87 }
88
89 // now read all evolution histograms
90 TIter keyItTit(epochDir->GetListOfKeys());
91 TKey *titkeyTit;
92 while ((titkeyTit = (TKey*)keyItTit())) {
93
94 if (!gROOT->GetClass(titkeyTit->GetClassName())->InheritsFrom("TH1F")) continue;
95 TString name = titkeyTit->GetName();
96
97 if (!name.BeginsWith("convergencetest___")) continue;
98 if (!name.Contains("_train_")) continue; // only for training so far
99 if (name.EndsWith( "_B")) continue;
100
101 // must be signal histogram
102 if (!name.EndsWith( "_S")) {
103 cout << "Big troubles with histogram: " << name << " -> should end with _S" << endl;
104 exit(1);
105 }
106
107 // create canvas
108 countCanvas++;
109 TString ctitle = Form("TMVA response %s",methodTitle.Data());
110 c = new TCanvas( Form("canvas%d", countCanvas), ctitle, 0, 0, width, (Int_t)width*0.78 );
111
112 TH1F* sig = (TH1F*)titkeyTit->ReadObj();
113 sig->SetTitle( Form("TMVA response for classifier: %s", methodTitle.Data()) );
114
115 TString dataType = (name.Contains("_train_") ? "(training sample)" : "(test sample)");
116
117 // find background
118 TString nbn = sig->GetName(); nbn[nbn.Length()-1] = 'B';
119 TH1F* bgd = dynamic_cast<TH1F*>(epochDir->Get( nbn ));
120 if (bgd == 0) {
121 cout << "Big troubles with histogram: " << bgd << " -> cannot find!" << endl;
122 exit(1);
123 }
124
125 cout << "sig = " << sig->GetName() << endl;
126 cout << "bgd = " << bgd->GetName() << endl;
127
128 // set the histogram style
129 TMVAGlob::SetSignalAndBackgroundStyle( sig, bgd );
130
131 // normalise both signal and background
132 TMVAGlob::NormalizeHists( sig, bgd );
133
134 // set only first time, then same for all plots
135 if (first) {
136 if (xmin == 0 && xmax == 0) {
137 xmin = TMath::Max( TMath::Min(sig->GetMean() - nrms*sig->GetRMS(),
138 bgd->GetMean() - nrms*bgd->GetRMS() ),
139 sig->GetXaxis()->GetXmin() );
140 xmax = TMath::Min( TMath::Max(sig->GetMean() + nrms*sig->GetRMS(),
141 bgd->GetMean() + nrms*bgd->GetRMS() ),
142 sig->GetXaxis()->GetXmax() );
143 }
144 ymin = 0;
145 ymax = TMath::Max( sig->GetMaximum(), bgd->GetMaximum() )*maxMult;
146 first = kFALSE;
147 }
148
149 // build a frame
150 Int_t nb = 100;
151 TString hFrameName(TString("frame") + methodTitle);
152 TObject *o = gROOT->FindObject(hFrameName);
153 if(o) delete o;
154 TH2F* frame = new TH2F( hFrameName, sig->GetTitle(),
155 nb, xmin, xmax, nb, ymin, ymax );
156 frame->GetXaxis()->SetTitle( methodTitle + " response" );
157 frame->GetYaxis()->SetTitle("(1/N) dN^{ }/^{ }dx");
158 TMVAGlob::SetFrameStyle( frame );
159
160 // find epoch number (4th token)
161 TObjArray* tokens = name.Tokenize("_");
162 TString es = ((TObjString*)tokens->At(4))->GetString();
163 if (!es.IsFloat()) {
164 cout << "Big troubles in epoch parsing: \"" << es << "\" is not float" << endl;
165 exit(1);
166 }
167 Int_t epoch = es.Atoi();
168
169 // eventually: draw the frame
170 frame->Draw();
171
172 c->GetPad(0)->SetLeftMargin( 0.105 );
173 frame->GetYaxis()->SetTitleOffset( 1.2 );
174
175 // Draw legend
176 TLegend *legend= new TLegend( c->GetLeftMargin(), 1 - c->GetTopMargin() - 0.12,
177 c->GetLeftMargin() + 0.5, 1 - c->GetTopMargin() );
178 legend->SetFillStyle( 1 );
179 legend->AddEntry(sig,TString("Signal ") + dataType, "F");
180 legend->AddEntry(bgd,TString("Background ") + dataType, "F");
181 legend->SetBorderSize(1);
182 legend->SetMargin( 0.15 );
183 legend->Draw("same");
184
185 TText* t = new TText();
186 t->SetTextSize( 0.04 );
187 t->SetTextColor( 1 );
188 t->SetTextAlign( 31 );
189 t->DrawTextNDC( 1 - c->GetRightMargin(), 1 - c->GetTopMargin() + 0.015, Form( "Epoch: %i", epoch) );
190
191 // overlay signal and background histograms
192 sig->Draw("samehist");
193 bgd->Draw("samehist");
194
195 // save to file
196 TString outdirname = "movieplots";
197 TString foutname = outdirname + "/" + name;
198 foutname.Resize( foutname.Length()-2 );
199 foutname.ReplaceAll("convergencetest___","");
200 foutname += ".gif";
201
202 cout << "storing file: " << foutname << endl;
203
204 c->Update();
205 c->Print(foutname);
206 }
207}
208
209// -----------------------------------------------------------------------------
210
211void TMVA::MovieMaker(TString dataset, TString methodType , TString methodTitle )
212{
213 TString fname = "TMVA.root";
214 TFile* file = TMVAGlob::OpenFile( fname );
215
216 //DrawMLPoutputMovie( file, methodType, methodTitle );
217 DrawNetworkMovie(dataset, file, methodType, methodTitle );
218}
219
#define c(i)
Definition RSha256.hxx:101
int Int_t
Definition RtypesCore.h:45
const Bool_t kFALSE
Definition RtypesCore.h:92
bool Bool_t
Definition RtypesCore.h:63
float Float_t
Definition RtypesCore.h:57
const Bool_t kTRUE
Definition RtypesCore.h:91
include TDocParser_001 C image html pict1_TDocParser_001 png width
char name[80]
Definition TGX11.cxx:110
float xmin
float ymin
float xmax
float ymax
#define gROOT
Definition TROOT.h:406
char * Form(const char *fmt,...)
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition TAttAxis.cxx:293
virtual void SetFillStyle(Style_t fstyle)
Set the fill area style.
Definition TAttFill.h:39
virtual void SetTextAlign(Short_t align=11)
Set the text alignment.
Definition TAttText.h:41
virtual void SetTextColor(Color_t tcolor=1)
Set the text color.
Definition TAttText.h:43
virtual void SetTextSize(Float_t tsize=1)
Set the text size.
Definition TAttText.h:46
Double_t GetXmax() const
Definition TAxis.h:134
Double_t GetXmin() const
Definition TAxis.h:133
The Canvas class.
Definition TCanvas.h:23
Describe directory structure in memory.
Definition TDirectory.h:45
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
virtual TList * GetListOfKeys() const
Definition TDirectory.h:177
virtual Bool_t cd(const char *path=nullptr)
Change current directory to "this" directory.
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition TFile.h:54
1-D histogram with a float per channel (see TH1 documentation)}
Definition TH1.h:575
virtual void SetTitle(const char *title)
See GetStatOverflows for more information.
Definition TH1.cxx:6678
virtual Double_t GetMean(Int_t axis=1) const
For axis = 1,2 or 3 returns the mean value of the histogram along X,Y or Z axis.
Definition TH1.cxx:7428
TAxis * GetXaxis()
Get the behaviour adopted by the object about the statoverflows. See EStatOverflows for more informat...
Definition TH1.h:320
virtual Double_t GetMaximum(Double_t maxval=FLT_MAX) const
Return maximum value smaller than maxval of bins in the range, unless the value has been overridden b...
Definition TH1.cxx:8390
TAxis * GetYaxis()
Definition TH1.h:321
Double_t GetRMS(Int_t axis=1) const
Definition TH1.h:315
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition TH1.cxx:3073
2-D histogram with a float per channel (see TH1 documentation)}
Definition TH2.h:251
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition TKey.h:28
virtual const char * GetClassName() const
Definition TKey.h:76
virtual TObject * ReadObj()
To read a TObject* from the file.
Definition TKey.cxx:750
This class displays a legend box (TPaveText) containing several legend entries.
Definition TLegend.h:23
TLegendEntry * AddEntry(const TObject *obj, const char *label="", Option_t *option="lpf")
Add a new entry to this legend.
Definition TLegend.cxx:330
virtual void Draw(Option_t *option="")
Draw this legend with its current attributes.
Definition TLegend.cxx:423
void SetMargin(Float_t margin)
Definition TLegend.h:69
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition TNamed.cxx:164
virtual const char * GetTitle() const
Returns title of object.
Definition TNamed.h:48
virtual const char * GetName() const
Returns name of object.
Definition TNamed.h:47
An array of TObjects.
Definition TObjArray.h:37
TObject * At(Int_t idx) const
Definition TObjArray.h:166
Collectable string class.
Definition TObjString.h:28
Mother of all ROOT objects.
Definition TObject.h:37
virtual TObject * FindObject(const char *name) const
Must be redefined in derived classes.
Definition TObject.cxx:323
virtual void SetBorderSize(Int_t bordersize=4)
Definition TPave.h:73
Basic string class.
Definition TString.h:136
Ssiz_t Length() const
Definition TString.h:410
Int_t Atoi() const
Return integer value of string.
Definition TString.cxx:1941
Bool_t IsFloat() const
Returns kTRUE if string contains a floating point or integer number.
Definition TString.cxx:1811
const char * Data() const
Definition TString.h:369
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition TString.h:692
void Resize(Ssiz_t n)
Resize the string. Truncate or add blanks as necessary.
Definition TString.cxx:1115
Base class for several text objects.
Definition TText.h:22
virtual TText * DrawTextNDC(Double_t x, Double_t y, const char *text)
Draw this text with new coordinates in NDC.
Definition TText.cxx:201
void draw_network(TString dataset, TFile *f, TDirectory *d, const TString &hName="weights_hist", Bool_t movieMode=kFALSE, const TString &epoch="")
void DrawMLPoutputMovie(TString dataset, TFile *file, const TString &methodType, const TString &methodTitle)
void DrawNetworkMovie(TString dataset, TFile *file, const TString &methodType, const TString &methodTitle)
void MovieMaker(TString dataset, TString methodType="Method_MLP", TString methodTitle="MLP")
Short_t Max(Short_t a, Short_t b)
Definition TMathBase.h:212
Short_t Min(Short_t a, Short_t b)
Definition TMathBase.h:180
Definition file.py:1
Definition first.py:1