staffTableTest.C

Go to the documentation of this file.
00001 // This TableTest class is a simple example of how to use a TGTable
00002 // with a TTreeTableInterface. TableTest inherits from TGMainFrame to
00003 // create a top level frame to embed the TGTable in. First, the
00004 // staff.root file is opened to obtain a tree. This tree also contains
00005 // strings as data. Then a TTreeTableInterface is created using this
00006 // tree. A table is then created using the interface. In the end, the
00007 // table is added to the TGMainFrame that is the TableTest and the
00008 // necessary calls to correctly draw the window are made. For more
00009 // information about the use of TTreeTableInterface and TGTable, see
00010 // their documentation.
00011 
00012 // author: Roel Aaij 13/07/2007
00013 
00014 #include <iostream>
00015 #include <TApplication.h>
00016 #include <TGClient.h>
00017 #include <TGButton.h>
00018 #include <TGFrame.h>
00019 #include <TGWindow.h>
00020 #include <TString.h>
00021 #include <TGTable.h>
00022 #include <TTreeTableInterface.h>
00023 #include <TFile.h>
00024 #include <TNtuple.h>
00025 #include <TSelectorDraw.h>
00026 
00027 // A little class to automatically handle the generation of unique
00028 // widget ids.
00029 class IDList {
00030 private:
00031    Int_t nID ;               // Generates unique widget IDs.
00032 public:
00033    IDList() : nID(0) {}
00034    ~IDList() {}
00035    Int_t GetUnID(void) { return ++nID ; }
00036 } ;
00037 
00038 class TableTest : public TGMainFrame {
00039 
00040 private:
00041    IDList   fIDs ;      // Generator for unique widget IDs.
00042    UInt_t   fNTableRows;
00043    UInt_t   fNTableColumns;
00044    TGTable *fTable;
00045    TFile   *fFile;
00046 
00047    TTreeTableInterface *fInterface;
00048 
00049 public:
00050    TableTest(const TGWindow *p, UInt_t ntrows, UInt_t ntcols, 
00051              UInt_t w = 100, UInt_t h = 100) ;
00052    virtual ~TableTest() ;
00053 
00054    void DoExit() ;
00055 
00056    TGTable *GetTable() { return fTable; }
00057    TTreeTableInterface *GetInterface() { return fInterface; }
00058 
00059    ClassDef(TableTest, 0)
00060 };
00061                           
00062 TableTest::TableTest(const TGWindow *p, UInt_t ntrows, UInt_t ntcols, 
00063                      UInt_t w, UInt_t h) 
00064    : TGMainFrame(p, w, h),  fNTableRows(ntrows), fNTableColumns(ntcols), 
00065      fTable(0)
00066 {
00067    SetCleanup(kDeepCleanup) ;   
00068    Connect("CloseWindow()", "TableTest", this, "DoExit()") ;
00069    DontCallClose() ;
00070    
00071    // Open root file for the tree
00072    fFile = new TFile("$ROOTSYS/tutorials/tree/cernstaff.root");
00073 
00074    if (!fFile || fFile->IsZombie()) {
00075       printf("Please run <ROOT location>/tutorials/tree/cernbuild.C first.");
00076       return;
00077    }
00078 
00079    // Get the tree from the file.
00080    TTree *tree = (TTree *)fFile->Get("T");
00081    
00082    // Setup the expressions for the column and selection of the interface.
00083    TString varexp = "*";
00084    TString select = "";
00085    TString options = "";
00086    fInterface = new TTreeTableInterface(tree, varexp.Data(), select.Data(),
00087                                         options.Data());
00088 
00089    // Create a table using the interface and add it to the TableTest
00090    // that is a TGMainFrame.
00091    fTable = new TGTable(this, fIDs.GetUnID(), fInterface, fNTableRows, 
00092                                 fNTableColumns);
00093    AddFrame(fTable, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
00094    
00095    // Calls to layout and draw the TableTest that is a TGMainFrame.
00096    SetWindowName("Tree Table Test") ;
00097    MapSubwindows() ;
00098    Layout();
00099    Resize(GetDefaultWidth()+20, 600) ;
00100    MapWindow() ;
00101    
00102 } ;
00103 
00104 TableTest::~TableTest()
00105 {
00106    // Destructor
00107    delete fInterface;
00108    fFile->Close();
00109    Cleanup() ;
00110 }
00111  
00112  void TableTest::DoExit()
00113 {
00114    // Exit this application via the Exit button or Window Manager.
00115    // Use one of the both lines according to your needs.
00116    // Please note to re-run this macro in the same ROOT session,
00117    // you have to compile it to get signals/slots 'on place'.
00118    
00119    DeleteWindow();            // to stay in the ROOT session
00120    //   gApplication->Terminate();   // to exit and close the ROOT session   
00121 }
00122 
00123 TGTable *staffTableTest(UInt_t ntrows = 50, UInt_t ntcols = 10) {
00124    TableTest *test = new TableTest(0, ntrows, ntcols, 500, 200);
00125    return test->GetTable();
00126 }

Generated on Tue Jul 5 15:44:26 2011 for ROOT_528-00b_version by  doxygen 1.5.1