ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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( TFile* file, const TString& methodType, const TString& methodTitle )
19 {
20 
21  TString dirname = methodType + "/" + methodTitle + "/" + "EpochMonitoring";
22  TDirectory *epochDir = (TDirectory*)file->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( file, epochDir, bulkname, kTRUE, es );
58  ic++;
59  }
60 }
61 
62 
63 void TMVA::DrawMLPoutputMovie( 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->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 methodType , TString methodTitle )
212 {
213  TString fname = "TMVA.root";
214  TFile* file = TMVAGlob::OpenFile( fname );
215 
216  //DrawMLPoutputMovie( file, methodType, methodTitle );
217  DrawNetworkMovie( 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:245
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:52
An array of TObjects.
Definition: TObjArray.h:39
float xmin
Definition: THbookFile.cxx:93
This class displays a legend box (TPaveText) containing several legend entries.
Definition: TLegend.h:35
virtual TList * GetListOfKeys() const
Definition: TDirectory.h:158
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
Definition: TDirectory.cxx:727
Ssiz_t Length() const
Definition: TString.h:390
Collectable string class.
Definition: TObjString.h:32
float Float_t
Definition: RtypesCore.h:53
return c
float ymin
Definition: THbookFile.cxx:93
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:635
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:373
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:45
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
void SetSignalAndBackgroundStyle(TH1 *sig, TH1 *bkg, TH1 *all=0)
Definition: tmvaglob.cxx:8
#define gROOT
Definition: TROOT.h:344
void SetFrameStyle(TH1 *frame, Float_t scale=1.0)
Definition: tmvaglob.cxx:77
Basic string class.
Definition: TString.h:137
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:570
Short_t Min(Short_t a, Short_t b)
Definition: TMathBase.h:170
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
void SetMargin(Float_t margin)
Definition: TLegend.h:80
const Bool_t kFALSE
Definition: Rtypes.h:92
virtual void SetFillStyle(Style_t fstyle)
Definition: TAttFill.h:52
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition: TString.h:558
TLegend * legend
Definition: pirndm.C:35
const char * Data() const
Definition: TString.h:349
virtual TText * DrawTextNDC(Double_t x, Double_t y, const char *text)
Draw this text with new coordinates in NDC.
Definition: TText.cxx:197
Base class for several text objects.
Definition: TText.h:42
Int_t Atoi() const
Return integer value of string.
Definition: TString.cxx:1951
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition: TKey.h:30
Double_t GetXmin() const
Definition: TAxis.h:137
virtual void SetTextAlign(Short_t align=11)
Definition: TAttText.h:55
TThread * t[5]
Definition: threadsh1.C:13
float ymax
Definition: THbookFile.cxx:93
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:7014
void draw_network(TFile *f, TDirectory *d, const TString &hName="weights_hist", Bool_t movieMode=kFALSE, const TString &epoch="")
Definition: network.cxx:29
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
Return true if string ends with the specified string.
Definition: TString.cxx:2207
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2878
2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:256
char * Form(const char *fmt,...)
bool first
Definition: line3Dfit.C:48
void DrawMLPoutputMovie(TFile *file, const TString &methodType, const TString &methodTitle)
Definition: MovieMaker.cxx:63
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
TAxis * GetYaxis()
Definition: TH1.h:320
float xmax
Definition: THbookFile.cxx:93
Double_t GetRMS(Int_t axis=1) const
Definition: TH1.h:315
void MovieMaker(TString methodType="Method_MLP", TString methodTitle="MLP")
Definition: MovieMaker.cxx:211
TObjArray * Tokenize(const TString &delim) const
This function is used to isolate sequential tokens in a TString.
Definition: TString.cxx:2227
The Canvas class.
Definition: TCanvas.h:48
void DrawNetworkMovie(TFile *file, const TString &methodType, const TString &methodTitle)
Definition: MovieMaker.cxx:18
tuple file
Definition: fildir.py:20
TLegendEntry * AddEntry(const TObject *obj, const char *label="", Option_t *option="lpf")
Add a new entry to this legend.
Definition: TLegend.cxx:280
Describe directory structure in memory.
Definition: TDirectory.h:44
Double_t GetXmax() const
Definition: TAxis.h:138
Bool_t IsFloat() const
Returns kTRUE if string contains a floating point or integer number.
Definition: TString.cxx:1821
#define name(a, b)
Definition: linkTestLib0.cpp:5
Mother of all ROOT objects.
Definition: TObject.h:58
virtual Bool_t cd(const char *path=0)
Change current directory to "this" directory.
Definition: TDirectory.cxx:433
Short_t Max(Short_t a, Short_t b)
Definition: TMathBase.h:202
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:567
virtual void SetTextColor(Color_t tcolor=1)
Definition: TAttText.h:57
virtual void SetTitle(const char *title)
Change (i.e.
Definition: TH1.cxx:6268
virtual void SetTextSize(Float_t tsize=1)
Definition: TAttText.h:60
TObject * At(Int_t idx) const
Definition: TObjArray.h:167
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:7921
const Bool_t kTRUE
Definition: Rtypes.h:91
virtual void SetTitle(const char *title="")
Change (i.e. set) the title of the TNamed.
Definition: TNamed.cxx:152
virtual void SetBorderSize(Int_t bordersize=4)
Definition: TPave.h:82
TAxis * GetXaxis()
Definition: TH1.h:319
void Resize(Ssiz_t n)
Resize the string. Truncate or add blanks as necessary.
Definition: TString.cxx:1045