ROOT logo
// This TableTest class is a simple example of how to use a TGTable
// with a TTreeTableInterface. TableTest inherits from TGMainFrame to
// create a top level frame to embed the TGTable in. First, the
// hsimple.root file is opened to obtain an ntuple. Then a
// TTreeTableInterface is created using this ntuple. A table is then
// created using the interface. 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. For more information about the
// use of TTreeTableInterface and TGTable, see their documentation.

// author: Roel Aaij 13/07/2007

#include <iostream>
#include <TApplication.h>
#include <TGClient.h>
#include <TGButton.h>
#include <TGFrame.h>
#include <TGWindow.h>
#include <TString.h>
#include <TGTable.h>
#include <TTreeTableInterface.h>
#include <TFile.h>
#include <TNtuple.h>
#include <TSelectorDraw.h>


// A little class to automatically handle the generation of unique
// widget ids.
class IDList {
private:
   Int_t nID ;               // Generates unique widget IDs.
public:
   IDList() : nID(0) {}
   ~IDList() {}
   Int_t GetUnID(void) { return ++nID ; }
} ;

class TableTest : public TGMainFrame {

private:
   IDList   fIDs ;      // Generator for unique widget IDs.
   UInt_t   fNTableRows;
   UInt_t   fNTableColumns;
   TGTable *fTable;
   TFile   *fFile;

   TTreeTableInterface *fInterface;

public:
   TableTest(const TGWindow *p, UInt_t ntrows, UInt_t ntcols, 
             UInt_t w = 100, UInt_t h = 100) ;
   virtual ~TableTest() ;

   void DoExit() ;

   TGTable *GetTable() { return fTable; }
   TTreeTableInterface *GetInterface() { return fInterface; }

   ClassDef(TableTest, 0)
};
                          
TableTest::TableTest(const TGWindow *p, UInt_t ntrows, UInt_t ntcols, 
                     UInt_t w, UInt_t h) 
   : TGMainFrame(p, w, h),  fNTableRows(ntrows), fNTableColumns(ntcols), 
     fTable(0)
{
   SetCleanup(kDeepCleanup) ;   
   Connect("CloseWindow()", "TableTest", this, "DoExit()") ;
   DontCallClose() ;
   
   // Open root file for the tree
   fFile = new TFile("$ROOTSYS/tutorials/hsimple.root");

   if (!fFile || fFile->IsZombie()) {
      printf("Please run <ROOT location>/tutorials/hsimple.C first.");
      return;
   }

   // Get the ntuple from the file.
   TNtuple *ntuple = (TNtuple *)fFile->Get("ntuple");

   // Setup the expressions for the columns and the selection of the
   // interface.
   TString varexp = "px:py:pz:random:sin(px):log(px/py):log(pz)";
   TString select = "";
   TString options = "";

   // Create the interface.
   fInterface = new TTreeTableInterface(ntuple, varexp.Data(), select.Data(),
                                        options.Data());
   
   // Create the table and add it to the TableTest that is a TGMainFrame.
   fTable = new TGTable(this, fIDs.GetUnID(), fInterface, fNTableRows, 
                                fNTableColumns);
   AddFrame(fTable, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
   
   // Calls to layout and draw the TableTest that is a TGMainFrame.
   SetWindowName("Tree Table Test") ;
   MapSubwindows() ;
   Layout();
   Resize() ;
   MapWindow() ;
   
} ;

TableTest::~TableTest()
{
   // Destructor
   delete fInterface;
   fFile->Close();
   Cleanup() ;
}
 
 void TableTest::DoExit()
{
   // Exit this application via the Exit button or Window Manager.
   // Use one of the both lines according to your needs.
   // Please note to re-run this macro in the same ROOT session,
   // you have to compile it to get signals/slots 'on place'.
   
   DeleteWindow();            // to stay in the ROOT session
   //   gApplication->Terminate();   // to exit and close the ROOT session   
}

TGTable *ntupleTableTest(UInt_t ntrows = 50, UInt_t ntcols = 10) {
   TableTest *test = new TableTest(0, ntrows, ntcols, 500, 200);
   return test->GetTable();
}
 ntupleTableTest.C:1
 ntupleTableTest.C:2
 ntupleTableTest.C:3
 ntupleTableTest.C:4
 ntupleTableTest.C:5
 ntupleTableTest.C:6
 ntupleTableTest.C:7
 ntupleTableTest.C:8
 ntupleTableTest.C:9
 ntupleTableTest.C:10
 ntupleTableTest.C:11
 ntupleTableTest.C:12
 ntupleTableTest.C:13
 ntupleTableTest.C:14
 ntupleTableTest.C:15
 ntupleTableTest.C:16
 ntupleTableTest.C:17
 ntupleTableTest.C:18
 ntupleTableTest.C:19
 ntupleTableTest.C:20
 ntupleTableTest.C:21
 ntupleTableTest.C:22
 ntupleTableTest.C:23
 ntupleTableTest.C:24
 ntupleTableTest.C:25
 ntupleTableTest.C:26
 ntupleTableTest.C:27
 ntupleTableTest.C:28
 ntupleTableTest.C:29
 ntupleTableTest.C:30
 ntupleTableTest.C:31
 ntupleTableTest.C:32
 ntupleTableTest.C:33
 ntupleTableTest.C:34
 ntupleTableTest.C:35
 ntupleTableTest.C:36
 ntupleTableTest.C:37
 ntupleTableTest.C:38
 ntupleTableTest.C:39
 ntupleTableTest.C:40
 ntupleTableTest.C:41
 ntupleTableTest.C:42
 ntupleTableTest.C:43
 ntupleTableTest.C:44
 ntupleTableTest.C:45
 ntupleTableTest.C:46
 ntupleTableTest.C:47
 ntupleTableTest.C:48
 ntupleTableTest.C:49
 ntupleTableTest.C:50
 ntupleTableTest.C:51
 ntupleTableTest.C:52
 ntupleTableTest.C:53
 ntupleTableTest.C:54
 ntupleTableTest.C:55
 ntupleTableTest.C:56
 ntupleTableTest.C:57
 ntupleTableTest.C:58
 ntupleTableTest.C:59
 ntupleTableTest.C:60
 ntupleTableTest.C:61
 ntupleTableTest.C:62
 ntupleTableTest.C:63
 ntupleTableTest.C:64
 ntupleTableTest.C:65
 ntupleTableTest.C:66
 ntupleTableTest.C:67
 ntupleTableTest.C:68
 ntupleTableTest.C:69
 ntupleTableTest.C:70
 ntupleTableTest.C:71
 ntupleTableTest.C:72
 ntupleTableTest.C:73
 ntupleTableTest.C:74
 ntupleTableTest.C:75
 ntupleTableTest.C:76
 ntupleTableTest.C:77
 ntupleTableTest.C:78
 ntupleTableTest.C:79
 ntupleTableTest.C:80
 ntupleTableTest.C:81
 ntupleTableTest.C:82
 ntupleTableTest.C:83
 ntupleTableTest.C:84
 ntupleTableTest.C:85
 ntupleTableTest.C:86
 ntupleTableTest.C:87
 ntupleTableTest.C:88
 ntupleTableTest.C:89
 ntupleTableTest.C:90
 ntupleTableTest.C:91
 ntupleTableTest.C:92
 ntupleTableTest.C:93
 ntupleTableTest.C:94
 ntupleTableTest.C:95
 ntupleTableTest.C:96
 ntupleTableTest.C:97
 ntupleTableTest.C:98
 ntupleTableTest.C:99
 ntupleTableTest.C:100
 ntupleTableTest.C:101
 ntupleTableTest.C:102
 ntupleTableTest.C:103
 ntupleTableTest.C:104
 ntupleTableTest.C:105
 ntupleTableTest.C:106
 ntupleTableTest.C:107
 ntupleTableTest.C:108
 ntupleTableTest.C:109
 ntupleTableTest.C:110
 ntupleTableTest.C:111
 ntupleTableTest.C:112
 ntupleTableTest.C:113
 ntupleTableTest.C:114
 ntupleTableTest.C:115
 ntupleTableTest.C:116
 ntupleTableTest.C:117
 ntupleTableTest.C:118
 ntupleTableTest.C:119
 ntupleTableTest.C:120
 ntupleTableTest.C:121
 ntupleTableTest.C:122
 ntupleTableTest.C:123
 ntupleTableTest.C:124
 ntupleTableTest.C:125
 ntupleTableTest.C:126
 ntupleTableTest.C:127
 ntupleTableTest.C:128
 ntupleTableTest.C:129