Logo ROOT   6.10/09
Reference Guide
staffTableTest.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_gui
3 /// This TableTest class is a simple example of how to use a TGTable with a TTreeTableInterface.
4 /// TableTest inherits from TGMainFrame to create a top level frame to embed the TGTable in.
5 /// First, the staff.root file is opened to obtain a tree. This tree also contains strings as data.
6 /// Then a TTreeTableInterface is created using this tree. A table is then created using the interface.
7 /// In the end, the table is added to the TGMainFrame that is the TableTest and the necessary calls to correctly draw the window are made.
8 /// For more information about the use of TTreeTableInterface and TGTable, see their documentation.
9 ///
10 /// \macro_code
11 ///
12 /// \author Roel Aaij 13/07/2007
13 
14 #include <iostream>
15 #include <TApplication.h>
16 #include <TGClient.h>
17 #include <TGButton.h>
18 #include <TGFrame.h>
19 #include <TGWindow.h>
20 #include <TString.h>
21 #include <TGTable.h>
22 #include <TTreeTableInterface.h>
23 #include <TFile.h>
24 #include <TNtuple.h>
25 #include <TSelectorDraw.h>
26 
27 // A little class to automatically handle the generation of unique
28 // widget ids.
29 class IDList {
30 private:
31  Int_t nID ; // Generates unique widget IDs.
32 public:
33  IDList() : nID(0) {}
34  ~IDList() {}
35  Int_t GetUnID(void) { return ++nID ; }
36 } ;
37 
38 class TableTest : public TGMainFrame {
39 
40 private:
41  IDList fIDs ; // Generator for unique widget IDs.
42  UInt_t fNTableRows;
43  UInt_t fNTableColumns;
44  TGTable *fTable;
45  TFile *fFile;
46 
47  TTreeTableInterface *fInterface;
48 
49 public:
50  TableTest(const TGWindow *p, UInt_t ntrows, UInt_t ntcols,
51  UInt_t w = 100, UInt_t h = 100) ;
52  virtual ~TableTest() ;
53 
54  void DoExit() ;
55 
56  TGTable *GetTable() { return fTable; }
57  TTreeTableInterface *GetInterface() { return fInterface; }
58 
59  ClassDef(TableTest, 0)
60 };
61 
62 TableTest::TableTest(const TGWindow *p, UInt_t ntrows, UInt_t ntcols,
63  UInt_t w, UInt_t h)
64  : TGMainFrame(p, w, h), fNTableRows(ntrows), fNTableColumns(ntcols),
65  fTable(0)
66 {
67  SetCleanup(kDeepCleanup) ;
68  Connect("CloseWindow()", "TableTest", this, "DoExit()") ;
69  DontCallClose() ;
70 
71  // Open root file for the tree
72  fFile = new TFile("cernstaff.root");
73 
74  if (!fFile || fFile->IsZombie()) {
75  printf("Please run <ROOT location>/tutorials/tree/cernbuild.C first.");
76  return;
77  }
78 
79  // Get the tree from the file.
80  TTree *tree = (TTree *)fFile->Get("T");
81 
82  // Setup the expressions for the column and selection of the interface.
83  TString varexp = "*";
84  TString select = "";
85  TString options = "";
86  fInterface = new TTreeTableInterface(tree, varexp.Data(), select.Data(),
87  options.Data());
88 
89  // Create a table using the interface and add it to the TableTest
90  // that is a TGMainFrame.
91  fTable = new TGTable(this, fIDs.GetUnID(), fInterface, fNTableRows,
92  fNTableColumns);
93  AddFrame(fTable, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
94 
95  // Calls to layout and draw the TableTest that is a TGMainFrame.
96  SetWindowName("Tree Table Test") ;
97  MapSubwindows() ;
98  Layout();
99  Resize(GetDefaultWidth()+20, 600) ;
100  MapWindow() ;
101 
102 } ;
103 
104 TableTest::~TableTest()
105 {
106  // Destructor
107  delete fInterface;
108  fFile->Close();
109  Cleanup() ;
110 }
111 
112  void TableTest::DoExit()
113 {
114  // Exit this application via the Exit button or Window Manager.
115  // Use one of the both lines according to your needs.
116  // Please note to re-run this macro in the same ROOT session,
117  // you have to compile it to get signals/slots 'on place'.
118 
119  DeleteWindow(); // to stay in the ROOT session
120  // gApplication->Terminate(); // to exit and close the ROOT session
121 }
122 
123 TGTable *staffTableTest(UInt_t ntrows = 50, UInt_t ntcols = 10) {
124  TableTest *test = new TableTest(0, ntrows, ntcols, 500, 200);
125  return test->GetTable();
126 }
TH1 * h
Definition: legend2.C:5
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:46
Definition: test.py:1
Basic string class.
Definition: TString.h:129
int Int_t
Definition: RtypesCore.h:41
#define ClassDef(name, id)
Definition: Rtypes.h:297
unsigned int UInt_t
Definition: RtypesCore.h:42
RooCmdArg Layout(Double_t xmin, Double_t xmax=0.99, Double_t ymin=0.95)
Definition: tree.py:1
TTreeTableInterface is used to interface to data that is stored in a TTree.
A TTree object has a header with a name and a title.
Definition: TTree.h:78
const char * Data() const
Definition: TString.h:347