Logo ROOT   6.10/09
Reference Guide
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 "TPad.h"
12 #include "TObjArray.h"
13 #include "TText.h"
14 
15 #include <vector>
16 #include "TMVA/network.h"
17 
18 void 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 
63 void 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 = 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
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 
211 void 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 
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title Offset is a correction factor with respect to the "s...
Definition: TAttAxis.cxx:262
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
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:7666
An array of TObjects.
Definition: TObjArray.h:37
float xmin
Definition: THbookFile.cxx:93
virtual TList * GetListOfKeys() const
Definition: TDirectory.h:148
This class displays a legend box (TPaveText) containing several legend entries.
Definition: TLegend.h:23
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
Definition: TDirectory.cxx:729
Collectable string class.
Definition: TObjString.h:28
float Float_t
Definition: RtypesCore.h:53
float ymin
Definition: THbookFile.cxx:93
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:640
TFile * OpenFile(const TString &fin)
Definition: tmvaglob.cxx:192
void NormalizeHists(TH1 *sig, TH1 *bkg=0)
Definition: tmvaglob.cxx:317
virtual void Draw(Option_t *option="")
Draw this legend with its current attributes.
Definition: TLegend.cxx:452
Bool_t IsFloat() const
Returns kTRUE if string contains a floating point or integer number.
Definition: TString.cxx:1845
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:46
void SetSignalAndBackgroundStyle(TH1 *sig, TH1 *bkg, TH1 *all=0)
Definition: tmvaglob.cxx:8
#define gROOT
Definition: TROOT.h:375
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:6763
void SetFrameStyle(TH1 *frame, Float_t scale=1.0)
Definition: tmvaglob.cxx:77
Basic string class.
Definition: TString.h:129
tomato 1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:551
Short_t Min(Short_t a, Short_t b)
Definition: TMathBase.h:168
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
void SetMargin(Float_t margin)
Definition: TLegend.h:69
Double_t GetRMS(Int_t axis=1) const
Definition: TH1.h:296
virtual void SetFillStyle(Style_t fstyle)
Set the fill area style.
Definition: TAttFill.h:39
void MovieMaker(TString dataset, TString methodType="Method_MLP", TString methodTitle="MLP")
TObject * At(Int_t idx) const
Definition: TObjArray.h:165
TLegend * legend
Definition: pirndm.C:35
Double_t GetXmin() const
Definition: TAxis.h:133
virtual TText * DrawTextNDC(Double_t x, Double_t y, const char *text)
Draw this text with new coordinates in NDC.
Definition: TText.cxx:202
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
Return true if string ends with the specified string.
Definition: TString.cxx:2231
Base class for several text objects.
Definition: TText.h:23
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition: TKey.h:24
void draw_network(TString dataset, TFile *f, TDirectory *d, const TString &hName="weights_hist", Bool_t movieMode=kFALSE, const TString &epoch="")
virtual void SetTextAlign(Short_t align=11)
Set the text alignment.
Definition: TAttText.h:41
float ymax
Definition: THbookFile.cxx:93
void DrawMLPoutputMovie(TString dataset, TFile *file, const TString &methodType, const TString &methodTitle)
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2851
tomato 2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:249
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition: TString.h:563
char * Form(const char *fmt,...)
Ssiz_t Length() const
Definition: TString.h:388
TAxis * GetYaxis()
Definition: TH1.h:301
float xmax
Definition: THbookFile.cxx:93
virtual TDirectory * GetDirectory(const char *apath, Bool_t printError=false, const char *funcname="GetDirectory")
Find a directory named "apath".
const Bool_t kFALSE
Definition: RtypesCore.h:92
The Canvas class.
Definition: TCanvas.h:31
TObjArray * Tokenize(const TString &delim) const
This function is used to isolate sequential tokens in a TString.
Definition: TString.cxx:2251
TLegendEntry * AddEntry(const TObject *obj, const char *label="", Option_t *option="lpf")
Add a new entry to this legend.
Definition: TLegend.cxx:359
Describe directory structure in memory.
Definition: TDirectory.h:34
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:572
Mother of all ROOT objects.
Definition: TObject.h:37
void DrawNetworkMovie(TString dataset, TFile *file, const TString &methodType, const TString &methodTitle)
virtual Bool_t cd(const char *path=0)
Change current directory to "this" directory.
Definition: TDirectory.cxx:435
Definition: file.py:1
Short_t Max(Short_t a, Short_t b)
Definition: TMathBase.h:200
virtual void SetTextColor(Color_t tcolor=1)
Set the text color.
Definition: TAttText.h:43
Int_t Atoi() const
Return integer value of string.
Definition: TString.cxx:1975
virtual void SetTitle(const char *title)
Change (i.e.
Definition: TH1.cxx:6028
Definition: first.py:1
virtual void SetTextSize(Float_t tsize=1)
Set the text size.
Definition: TAttText.h:46
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition: TNamed.cxx:155
THist< 2, float, THistStatContent, THistStatUncertainty > TH2F
Definition: THist.hxx:317
const Bool_t kTRUE
Definition: RtypesCore.h:91
Double_t GetXmax() const
Definition: TAxis.h:134
virtual void SetBorderSize(Int_t bordersize=4)
Definition: TPave.h:70
TAxis * GetXaxis()
Definition: TH1.h:300
void Resize(Ssiz_t n)
Resize the string. Truncate or add blanks as necessary.
Definition: TString.cxx:1069
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:48
const char * Data() const
Definition: TString.h:347