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 = TString::Format( "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;
80 Bool_t first = kTRUE;
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 = TString::Format("TMVA response %s",methodTitle.Data());
110 c = new TCanvas( TString::Format("canvas%d", countCanvas), ctitle, 0, 0, width, (Int_t)width*0.78 );
111
112 TH1F* sig = (TH1F*)titkeyTit->ReadObj();
113 sig->SetTitle( TString::Format("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
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");
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;
186 t.SetTextSize( 0.04 );
187 t.SetTextColor( 1 );
188 t.SetTextAlign( 31 );
189 t.DrawTextNDC( 1 - c->GetRightMargin(), 1 - c->GetTopMargin() + 0.015, TString::Format( "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
bool Bool_t
Definition RtypesCore.h:63
int Int_t
Definition RtypesCore.h:45
float Float_t
Definition RtypesCore.h:57
constexpr Bool_t kFALSE
Definition RtypesCore.h:101
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
Option_t Option_t width
char name[80]
Definition TGX11.cxx:110
float xmin
float ymin
float xmax
float ymax
#define gROOT
Definition TROOT.h:406
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition TAttAxis.cxx:298
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:42
virtual void SetTextColor(Color_t tcolor=1)
Set the text color.
Definition TAttText.h:44
virtual void SetTextSize(Float_t tsize=1)
Set the text size.
Definition TAttText.h:47
Double_t GetXmax() const
Definition TAxis.h:140
Double_t GetXmin() const
Definition TAxis.h:139
The Canvas class.
Definition TCanvas.h:23
TDirectory * GetDirectory(const char *apath, Bool_t printError=false, const char *funcname="GetDirectory") override
Find a directory named "apath".
Describe directory structure in memory.
Definition TDirectory.h:45
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
virtual Bool_t cd()
Change current directory to "this" directory.
virtual TList * GetListOfKeys() const
Definition TDirectory.h:223
A ROOT file is composed of a header, followed by consecutive data records (TKey instances) with a wel...
Definition TFile.h:53
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:621
void SetTitle(const char *title) override
Change/set the title.
Definition TH1.cxx:6682
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:7499
TAxis * GetXaxis()
Definition TH1.h:324
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:8509
TAxis * GetYaxis()
Definition TH1.h:325
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3062
Double_t GetRMS(Int_t axis=1) const
This function returns the Standard Deviation (Sigma) of the distribution not the Root Mean Square (RM...
Definition TH1.h:319
2-D histogram with a float per channel (see TH1 documentation)
Definition TH2.h:295
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:75
virtual TObject * ReadObj()
To read a TObject* from the file.
Definition TKey.cxx:758
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:317
void Draw(Option_t *option="") override
Draw this legend with its current attributes.
Definition TLegend.cxx:422
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
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
const char * GetTitle() const override
Returns title of object.
Definition TNamed.h:48
An array of TObjects.
Definition TObjArray.h:31
TObject * At(Int_t idx) const override
Definition TObjArray.h:164
Collectable string class.
Definition TObjString.h:28
Mother of all ROOT objects.
Definition TObject.h:41
virtual TObject * FindObject(const char *name) const
Must be redefined in derived classes.
Definition TObject.cxx:403
virtual void SetBorderSize(Int_t bordersize=4)
Sets the border size of the TPave box and shadow.
Definition TPave.h:77
Basic string class.
Definition TString.h:139
Ssiz_t Length() const
Definition TString.h:417
Int_t Atoi() const
Return integer value of string.
Definition TString.cxx:1988
Bool_t IsFloat() const
Returns kTRUE if string contains a floating point or integer number.
Definition TString.cxx:1858
const char * Data() const
Definition TString.h:376
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition TString.h:704
void Resize(Ssiz_t n)
Resize the string. Truncate or add blanks as necessary.
Definition TString.cxx:1152
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2378
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:202
void SetSignalAndBackgroundStyle(TH1 *sig, TH1 *bkg, TH1 *all=nullptr)
Definition tmvaglob.cxx:8
TFile * OpenFile(const TString &fin)
Definition tmvaglob.cxx:192
void SetFrameStyle(TH1 *frame, Float_t scale=1.0)
Definition tmvaglob.cxx:77
void NormalizeHists(TH1 *sig, TH1 *bkg=nullptr)
Definition tmvaglob.cxx:324
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)
Returns the largest of a and b.
Definition TMathBase.h:250
Short_t Min(Short_t a, Short_t b)
Returns the smallest of a and b.
Definition TMathBase.h:198