ntupleTableTest.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 // hsimple.root file is opened to obtain an ntuple. Then a
00005 // TTreeTableInterface is created using this ntuple. A table is then
00006 // created using the interface. In the end, the table is added to the
00007 // TGMainFrame that is the TableTest and the necessary calls to
00008 // correctly draw the window are made. For more information about the
00009 // use of TTreeTableInterface and TGTable, see their documentation.
00010 
00011 // author: Roel Aaij 13/07/2007
00012 
00013 #include <iostream>
00014 #include <TApplication.h>
00015 #include <TGClient.h>
00016 #include <TGButton.h>
00017 #include <TGFrame.h>
00018 #include <TGWindow.h>
00019 #include <TString.h>
00020 #include <TGTable.h>
00021 #include <TTreeTableInterface.h>
00022 #include <TFile.h>
00023 #include <TNtuple.h>
00024 #include <TSelectorDraw.h>
00025 
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/hsimple.root");
00073 
00074    if (!fFile || fFile->IsZombie()) {
00075       printf("Please run <ROOT location>/tutorials/hsimple.C first.");
00076       return;
00077    }
00078 
00079    // Get the ntuple from the file.
00080    TNtuple *ntuple = (TNtuple *)fFile->Get("ntuple");
00081 
00082    // Setup the expressions for the columns and the selection of the
00083    // interface.
00084    TString varexp = "px:py:pz:random:sin(px):log(px/py):log(pz)";
00085    TString select = "";
00086    TString options = "";
00087 
00088    // Create the interface.
00089    fInterface = new TTreeTableInterface(ntuple, varexp.Data(), select.Data(),
00090                                         options.Data());
00091    
00092    // Create the table and add it to the TableTest that is a TGMainFrame.
00093    fTable = new TGTable(this, fIDs.GetUnID(), fInterface, fNTableRows, 
00094                                 fNTableColumns);
00095    AddFrame(fTable, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
00096    
00097    // Calls to layout and draw the TableTest that is a TGMainFrame.
00098    SetWindowName("Tree Table Test") ;
00099    MapSubwindows() ;
00100    Layout();
00101    Resize(GetDefaultWidth()+20, 600) ;
00102    MapWindow() ;
00103    
00104 } ;
00105 
00106 TableTest::~TableTest()
00107 {
00108    // Destructor
00109    delete fInterface;
00110    fFile->Close();
00111    Cleanup() ;
00112 }
00113  
00114  void TableTest::DoExit()
00115 {
00116    // Exit this application via the Exit button or Window Manager.
00117    // Use one of the both lines according to your needs.
00118    // Please note to re-run this macro in the same ROOT session,
00119    // you have to compile it to get signals/slots 'on place'.
00120    
00121    DeleteWindow();            // to stay in the ROOT session
00122    //   gApplication->Terminate();   // to exit and close the ROOT session   
00123 }
00124 
00125 TGTable *ntupleTableTest(UInt_t ntrows = 50, UInt_t ntcols = 10) {
00126    TableTest *test = new TableTest(0, ntrows, ntcols, 500, 200);
00127    return test->GetTable();
00128 }

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