Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ntupleTableTest.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 hsimple.root file is opened to obtain an ntuple. Then a TTreeTableInterface is created using this ntuple.
6/// 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
28// A little class to automatically handle the generation of unique
29// widget ids.
30class IDList {
31private:
32 Int_t nID ; // Generates unique widget IDs.
33public:
34 IDList() : nID(0) {}
35 ~IDList() {}
36 Int_t GetUnID(void) { return ++nID ; }
37} ;
38
39class TableTest : public TGMainFrame {
40
41private:
42 IDList fIDs ; // Generator for unique widget IDs.
43 UInt_t fNTableRows;
44 UInt_t fNTableColumns;
45 TGTable *fTable;
46 TFile *fFile;
47
48 TTreeTableInterface *fInterface;
49
50public:
51 TableTest(const TGWindow *p, UInt_t ntrows, UInt_t ntcols,
52 UInt_t w = 100, UInt_t h = 100) ;
53 virtual ~TableTest() ;
54
55 void DoExit() ;
56
57 TGTable *GetTable() { return fTable; }
58 TTreeTableInterface *GetInterface() { return fInterface; }
59
60 ClassDef(TableTest, 0)
61};
62
63TableTest::TableTest(const TGWindow *p, UInt_t ntrows, UInt_t ntcols,
64 UInt_t w, UInt_t h)
65 : TGMainFrame(p, w, h), fNTableRows(ntrows), fNTableColumns(ntcols),
66 fTable(0)
67{
68 SetCleanup(kDeepCleanup) ;
69 Connect("CloseWindow()", "TableTest", this, "DoExit()") ;
70 DontCallClose() ;
71
72 // Open root file for the tree
73 fFile = new TFile("$ROOTSYS/tutorials/hsimple.root");
74
75 if (!fFile || fFile->IsZombie()) {
76 printf("Please run <ROOT location>/tutorials/hsimple.C first.");
77 return;
78 }
79
80 // Get the ntuple from the file.
81 TNtuple *ntuple = (TNtuple *)fFile->Get("ntuple");
82
83 // Setup the expressions for the columns and the selection of the
84 // interface.
85 TString varexp = "px:py:pz:random:sin(px):log(px/py):log(pz)";
86 TString select = "";
87 TString options = "";
88
89 // Create the interface.
90 fInterface = new TTreeTableInterface(ntuple, varexp.Data(), select.Data(),
91 options.Data());
92
93 // Create the table and add it to the TableTest that is a TGMainFrame.
94 fTable = new TGTable(this, fIDs.GetUnID(), fInterface, fNTableRows,
95 fNTableColumns);
96 AddFrame(fTable, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
97
98 // Calls to layout and draw the TableTest that is a TGMainFrame.
99 SetWindowName("Tree Table Test") ;
100 MapSubwindows() ;
101 Layout();
102 Resize(GetDefaultWidth()+20, 600) ;
103 MapWindow() ;
104
105} ;
106
107TableTest::~TableTest()
108{
109 // Destructor
110 delete fInterface;
111 fFile->Close();
112 Cleanup() ;
113}
114
115 void TableTest::DoExit()
116{
117 // Exit this application via the Exit button or Window Manager.
118 // Use one of the both lines according to your needs.
119 // Please note to re-run this macro in the same ROOT session,
120 // you have to compile it to get signals/slots 'on place'.
121
122 DeleteWindow(); // to stay in the ROOT session
123 // gApplication->Terminate(); // to exit and close the ROOT session
124}
125
126TGTable *ntupleTableTest(UInt_t ntrows = 50, UInt_t ntcols = 10) {
127 TableTest *test = new TableTest(0, ntrows, ntcols, 500, 200);
128 return test->GetTable();
129}
#define h(i)
Definition RSha256.hxx:106
int Int_t
Definition RtypesCore.h:45
unsigned int UInt_t
Definition RtypesCore.h:46
#define ClassDef(name, id)
Definition Rtypes.h:325
@ kDeepCleanup
Definition TGFrame.h:42
@ kLHintsExpandY
Definition TGLayout.h:31
@ kLHintsExpandX
Definition TGLayout.h:30
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition TFile.h:54
This class describes layout hints used by the layout classes.
Definition TGLayout.h:50
Defines top level windows that interact with the system Window Manager.
Definition TGFrame.h:398
Create an array to hold a bunch of numbers.
Definition TGTable.h:34
ROOT GUI Window base class.
Definition TGWindow.h:23
A simple TTree restricted to a list of float variables only.
Definition TNtuple.h:28
Basic string class.
Definition TString.h:136
const char * Data() const
Definition TString.h:369
TTreeTableInterface is used to interface to data that is stored in a TTree.
RooCmdArg Layout(Double_t xmin, Double_t xmax=0.99, Double_t ymin=0.95)
Definition test.py:1