Logo ROOT  
Reference Guide
TTreeTableInterface.cxx
Go to the documentation of this file.
1// Author: Roel Aaij 15/08/2007
2
3/*************************************************************************
4 * Copyright (C) 1995-2007, Rene Brun and Fons Rademakers. *
5 * All rights reserved. *
6 * *
7 * For the licensing terms see $ROOTSYS/LICENSE. *
8 * For the list of contributors see $ROOTSYS/README/CREDITS. *
9 *************************************************************************/
10
11#include "TClass.h"
12#include "Riostream.h"
13#include "TSystem.h"
14#include "TTreeTableInterface.h"
15#include "TTreeFormula.h"
16#include "TError.h"
17#include "TTree.h"
18#include "TEntryList.h"
19#include "TSelectorDraw.h"
20#include "TTreeFormulaManager.h"
21
23
24/** \class TTreeTableInterface
25
26TTreeTableInterface is used to interface to data that is stored in a
27TTree. When the interface is created, an expression can be
28specified. This expression will define the columns to be shown.
29
30A selection criterium can also be specified. A TEntryList will be
31created and applied to the TTree using this criterium.
32a new TEntryList to use can be specified using SetEntryList.
33TGTable->Update() will need to be called to show the effects.
34
35WARNING: Do not apply an entrylist to the tree in any other way than
36through the interface, this will have undefined consequences.
37
38Columns can be manipulated using the appropriate methods. A
39TGTable->Update is always needed afterwards to make the table aware
40of the changes.
41*/
42
43////////////////////////////////////////////////////////////////////////////////
44/// TTreeTableInterface constructor.
45
47 const char *selection, Option_t *option, Long64_t nentries,
48 Long64_t firstentry)
49 : TVirtualTableInterface(), fTree(tree), fFormulas(0), fEntry(0),
50 fNEntries(nentries), fFirstEntry(firstentry), fManager(0), fSelect(0), fSelector(0), fInput(0),
51 fForceDim(kFALSE), fEntries(0), fNRows(0), fNColumns(0)
52{
53 if (fTree == 0) {
54 Error("TTreeTableInterface", "No tree supplied");
55 return;
56 }
57
58 fFormulas= new TList();
60 fInput = new TList();
61 fInput->Add(new TNamed("varexp",""));
62 fInput->Add(new TNamed("selection",""));
65
66 TString opt = option;
67
68 if (nentries == 0) {
70 Info("TTreeTableInterface", "nentries was 0, setting to maximum number"
71 " available in the tree");
72 }
73
74 // Do stuff with opt.Contains() and options
76 SetSelection(selection);
77
78 if (fNRows == 0) {
79 Warning ("TTreeTableInterface::TTreeTableInterface", "nrows = 0");
80 }
81 if (fNColumns == 0) {
82 Warning ("TTreeTableInterface::TTreeTableInterface", "ncolumns = 0");
83 }
84}
85
86////////////////////////////////////////////////////////////////////////////////
87/// TTreeTableInterface destructor.
88
90{
92 delete fFormulas;
93 delete fInput;
94 delete fSelector;
95
96 if (fTree) fTree->SetEntryList(0);
97 delete fEntries;
98}
99
100////////////////////////////////////////////////////////////////////////////////
101/// Compile the variables expression from the given varexp.
102
104{
105 // FIXME check if enough protection against wrong expressions is in place
106
107 Bool_t allvar = kFALSE;
108
109 if (varexp) {
110 if (!strcmp(varexp, "*")) { allvar = kTRUE; }
111 } else {
112 // if varexp is empty, take all available leaves as a column
113 allvar = kTRUE;
114 }
115
116 if (allvar) {
117 TObjArray *leaves = fTree->GetListOfLeaves();
118 UInt_t nleaves = leaves->GetEntries();
119 if (!nleaves) {
120 Error("TTreeTableInterface", "No leaves in Tree");
121 return;
122 }
123 fNColumns = nleaves;
124 for (UInt_t ui = 0; ui < fNColumns; ui++) {
125 TLeaf *lf = (TLeaf*)leaves->At(ui);
126 fFormulas->Add(new TTreeFormula("Var1", lf->GetName(), fTree));
127 }
128 // otherwise select only the specified columns
129 } else {
130 std::vector<TString> cnames;
131 fNColumns = fSelector->SplitNames(varexp,cnames);
132
133 // Create the TreeFormula objects corresponding to each column
134 for (UInt_t ui = 0; ui < fNColumns; ui++) {
135 fFormulas->Add(new TTreeFormula("Var1", cnames[ui].Data(), fTree));
136 }
137 }
138}
139
140////////////////////////////////////////////////////////////////////////////////
141/// Set the selection expression.
142
143void TTreeTableInterface::SetSelection(const char *selection)
144{
145 // FIXME verify functionality
146 if (fSelect) {
148 delete fSelect;
149 fSelect = 0;
150 }
151 if (selection && strlen(selection)) {
152 fSelect = new TTreeFormula("Selection", selection, fTree);
154 }
155
156 if (fManager) {
157 for (Int_t i = 0; i <= fFormulas->LastIndex(); i++) {
159 }
160 }
161
162 // SyncFormulas() will update the formula manager if needed
163 SyncFormulas();
164 InitEntries();
165}
166
167////////////////////////////////////////////////////////////////////////////////
168/// Sync the formulas using the TTreeFormulaManager.
169
171{
172 // FIXME verify functionality
173
174 Int_t i = 0;
175 if (fFormulas->LastIndex() >= 0) {
176 if (fSelect) {
177 if (fSelect->GetManager()->GetMultiplicity() > 0 ) {
179 for (i = 0; i <= fFormulas->LastIndex(); i++) {
181 }
182 fManager->Sync();
183 }
184 }
185 for (i = 0; i < fFormulas->LastIndex(); i++) {
186 TTreeFormula *form = ((TTreeFormula*)fFormulas->At(i));
187 switch (form->GetManager()->GetMultiplicity()) {
188 case 1:
189 case 2:
190 case -1:
192 break;
193 case 0:
194 break;
195 }
196 }
197 }
198}
199
200////////////////////////////////////////////////////////////////////////////////
201/// Initialise the TEntryList with the entries that match the
202/// selection criterium.
203
205{
206 TEntryList *entrylist = new TEntryList(fTree);
207
208 UInt_t ui = 0;
209 Int_t i = 0;
210
211 Long64_t notSkipped = 0;
212 Int_t tnumber = -1;
213 Long64_t entry = fFirstEntry;
214 Int_t entriesToDisplay = fNEntries;
215
216 while (entriesToDisplay != 0){
217// entryNumber = fTree->GetEntryNumber(entry);
218// if(entryNumber < 0) break;
219 Long64_t localEntry = fTree->LoadTree(entry);
220 if (localEntry < 0) break;
221 if (tnumber != fTree->GetTreeNumber()) {
222 tnumber = fTree->GetTreeNumber();
224 else {
225 for(i = 0; i < fFormulas->LastIndex(); i++)
226 ((TTreeFormula*)fFormulas->At(ui))->UpdateFormulaLeaves();
227 }
228 }
229 Int_t ndata = 1;
230 if (fForceDim){
231 if (fManager)
232 ndata = fManager->GetNdata(kTRUE);
233 else {
234 for (ui = 0; ui < fNColumns; ui++){
235 if (ndata < ((TTreeFormula*)fFormulas->At(ui))->GetNdata())
236 {
237 ndata = ((TTreeFormula*)fFormulas->At(ui))->GetNdata();
238 }
239 }
240 if (fSelect && fSelect->GetNdata() == 0)
241 ndata = 0;
242 }
243 }
244 Bool_t skip = kFALSE;
245
246 // Loop over the instances of the selection condition
247 for (Int_t inst = 0; inst < ndata; inst++){
248 if (fSelect){
249 if (fSelect->EvalInstance(inst) == 0){
250 skip = kTRUE;
251 entry++;
252 }
253 }
254 }
255 if (!skip){
256 entrylist->Enter(entry);
257 notSkipped++;
258 entriesToDisplay--;
259 entry++;
260 }
261 }
262 SetEntryList(entrylist);
263}
264
265////////////////////////////////////////////////////////////////////////////////
266/// Return the value of row,column. If the position does not exist
267/// or does not contain a number, 0 is returned.
268
270{
271 static UInt_t prow = 0;
272
273 if (row < fNRows) {
274 Long64_t entry = 0;
275 if (row == prow + 1) {
276 entry = fEntries->Next();
277 } else {
278 entry = fEntries->GetEntry(row);
279 }
280 prow = row;
281 fTree->LoadTree(entry);
282 } else {
283 Error("TTreeTableInterface", "Row requested does not exist");
284 return 0;
285 }
286 if (column < fNColumns) {
287 TTreeFormula *formula = (TTreeFormula *)fFormulas->At(column);
288 if (!formula->IsString()) {
289 return (Double_t)formula->EvalInstance();
290 } else {
291 Warning("TTreeTableInterface::GetValue", "Value requested is a "
292 "string, returning 0.");
293 return 0;
294 }
295 } else {
296 Error("TTreeTableInterface", "Column requested does not exist");
297 return 0;
298 }
299}
300
301////////////////////////////////////////////////////////////////////////////////
302/// Return the content of row,column as string to use in a
303/// TGTableCell label.
304
306{
307 static UInt_t prow = 0;
308
309 if (row < fNRows) {
310 Long64_t entry = 0;
311 if (row == prow + 1) {
312 entry = fEntries->Next();
313 } else {
314 entry = fEntries->GetEntry(row);
315 }
316 prow = row;
317 fTree->LoadTree(entry);
318 } else {
319 Error("TTreeTableInterface", "Row requested does not exist");
320 return 0;
321 }
322 if (column < fNColumns) {
323 TTreeFormula *formula = (TTreeFormula *)fFormulas->At(column);
324 if(formula->IsString()) {
325 return Form("%s", formula->EvalStringInstance());
326 } else {
327 return Form("%5.2f", (Double_t)formula->EvalInstance());
328 }
329 } else {
330 Error("TTreeTableInterface", "Column requested does not exist");
331 return 0;
332 }
333}
334
335////////////////////////////////////////////////////////////////////////////////
336/// Return a string to use as a label for rowheader at column.
337
339{
340 if (row < fNRows) {
341 return Form("%lld", fEntries->GetEntry(row));
342 } else {
343 Error("TTreeTableInterface", "Row requested does not exist");
344 return "";
345 }
346}
347
348////////////////////////////////////////////////////////////////////////////////
349/// Return a string to use as a label for columnheader at column.
350
352{
353 TTreeFormula *formula = (TTreeFormula *)fFormulas->At(column);
354 if (column < fNColumns) {
355 return formula->GetTitle();
356 } else {
357 Error("TTreeTableInterface", "Column requested does not exist");
358 return "";
359 }
360}
361
362////////////////////////////////////////////////////////////////////////////////
363/// Return the amount of column available.
364
366{
367 return fNColumns;
368}
369
370////////////////////////////////////////////////////////////////////////////////
371/// Return the amount of rows in the Tree.
372
374{
375 return fNRows;
376}
377
378////////////////////////////////////////////////////////////////////////////////
379/// Add column according ot expression at position,
380/// TGTable->Update() is needed afterwards to apply the change to
381/// the TGTable.
382
383void TTreeTableInterface::AddColumn(const char *expression, UInt_t position)
384{
385 TString onerow = expression;
386
387 if (onerow.Contains(':')) {
388 Error("TTreeTableInterface::AddColumn", "Only 1 expression allowed.");
389 return;
390 }
391
392 // Create the TreeFormula objects corresponding to the new expression
393 TTreeFormula *formula = new TTreeFormula("Var1", expression, fTree);
394 fFormulas->AddAt(formula, position);
395
396 if (fManager) {
397 fManager->Add(formula);
398 fManager->Sync();
399 }
400 fNColumns++;
401}
402
403////////////////////////////////////////////////////////////////////////////////
404/// Add column with formula at position, TGTable->Update() is needed
405/// afterwards to apply the change to the TGTable.
406
408{
409 if (position > fNColumns) {
410 Error("TTreeTableInterface::AddColumn", "Please specify a "
411 "valid position.");
412 return;
413 }
414 fFormulas->AddAt(formula, position);
415 if (fManager) {
416 fManager->Add(formula);
417 fManager->Sync();
418 }
419 fNColumns++;
420}
421
422////////////////////////////////////////////////////////////////////////////////
423/// Remove column at position, TGTable->Update() is needed
424/// afterwards to apply the change to the TGTable.
425
427{
428 if (position >= fNColumns) {
429 Error("TTreeTableInterface::RemoveColumn", "Please specify a "
430 "valid column.");
431 return;
432 } else if (fNColumns == 1) {
433 Error("TTreeTableInterface::RemoveColumn", "Can't remove last column");
434 return;
435 }
436
437 TTreeFormula *formula = (TTreeFormula *)fFormulas->RemoveAt(position);
438 if (fManager) {
439 fManager->Remove(formula);
440 fManager->Sync();
441 }
442
443 if (formula) delete formula;
444 fNColumns--;
445}
446
447////////////////////////////////////////////////////////////////////////////////
448/// Set the TTreeFormula of position to formula.
449
451{
452 if (position >= fNColumns) {
453 Error("TTreeTableInterface::SetFormula", "Please specify a "
454 "valid position.");
455 return;
456 }
457 TTreeFormula *form = (TTreeFormula *)fFormulas->RemoveAt(position);
458 if (fSelect) {
459 fManager->Remove(form);
460 }
461 if (form) delete form;
462 fFormulas->AddAt(formula, position);
463 if (fManager) {
464 fManager->Add(formula);
465 fManager->Sync();
466 }
467
468}
469
470////////////////////////////////////////////////////////////////////////////////
471/// Set the currently active entrylist.
472
474{
475 // Untested
476 if (fEntries) delete fEntries;
477 fEntries = entrylist;
478 fNRows = fEntries->GetN();
479 fTree->SetEntryList(entrylist);
480}
const Bool_t kFALSE
Definition: RtypesCore.h:90
double Double_t
Definition: RtypesCore.h:57
long long Long64_t
Definition: RtypesCore.h:71
const Bool_t kTRUE
Definition: RtypesCore.h:89
const char Option_t
Definition: RtypesCore.h:64
#define ClassImp(name)
Definition: Rtypes.h:361
void Info(const char *location, const char *msgfmt,...)
void Error(const char *location, const char *msgfmt,...)
void Warning(const char *location, const char *msgfmt,...)
int nentries
Definition: THbookFile.cxx:89
char * Form(const char *fmt,...)
A List of entry numbers in a TTree or TChain.
Definition: TEntryList.h:26
virtual Long64_t Next()
Return the next non-zero entry index (next after fLastIndexQueried) this function is faster than GetE...
Definition: TEntryList.cxx:896
virtual Bool_t Enter(Long64_t entry, TTree *tree=0)
Add entry #entry to the list.
Definition: TEntryList.cxx:560
virtual Long64_t GetEntry(Int_t index)
Return the number of the entry #index of this TEntryList in the TTree or TChain See also Next().
Definition: TEntryList.cxx:657
virtual Long64_t GetN() const
Definition: TEntryList.h:75
A TLeaf describes individual elements of a TBranch See TBranch structure in TTree.
Definition: TLeaf.h:49
A doubly linked list.
Definition: TList.h:44
virtual void Add(TObject *obj)
Definition: TList.h:87
virtual TObject * Remove(TObject *obj)
Remove object from the list.
Definition: TList.cxx:821
virtual void AddAt(TObject *obj, Int_t idx)
Insert object at position idx in the list.
Definition: TList.cxx:305
virtual TObject * At(Int_t idx) const
Returns the object at position idx. Returns 0 if idx is out of range.
Definition: TList.cxx:356
virtual void Delete(Option_t *option="")
Remove all objects from the list AND delete all heap based objects.
Definition: TList.cxx:469
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:48
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
An array of TObjects.
Definition: TObjArray.h:37
Int_t GetEntries() const
Return the number of objects in array (i.e.
Definition: TObjArray.cxx:523
TObject * At(Int_t idx) const
Definition: TObjArray.h:166
A specialized TSelector for TTree::Draw.
Definition: TSelectorDraw.h:31
virtual UInt_t SplitNames(const TString &varexp, std::vector< TString > &names)
Build Index array for names in varexp.
virtual void SetInputList(TList *input)
Definition: TSelector.h:68
Int_t LastIndex() const
virtual TObject * RemoveAt(Int_t idx)
Basic string class.
Definition: TString.h:131
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:619
Used to coordinate one or more TTreeFormula objects.
virtual void UpdateFormulaLeaves()
This function could be called TTreePlayer::UpdateFormulaLeaves, itself called by TChain::LoadTree whe...
virtual Int_t GetNdata(Bool_t forceLoadDim=kFALSE)
Return number of available instances in the formulas.
virtual Bool_t Sync()
Synchronize all the formulae.
virtual void Add(TTreeFormula *)
Add a new formula to the list of formulas managed The manager of the formula will be changed and the ...
virtual void Remove(TTreeFormula *)
Remove a formula from this manager.
virtual Int_t GetMultiplicity() const
Used to pass a selection expression to the Tree drawing routine.
Definition: TTreeFormula.h:58
TTreeFormulaManager * GetManager() const
Definition: TTreeFormula.h:189
virtual const char * EvalStringInstance(Int_t i=0)
Eval the instance as a string.
T EvalInstance(Int_t i=0, const char *stringStack[]=0)
Evaluate this treeformula.
virtual Bool_t IsString(Int_t oper) const
Return true if the expression at the index 'oper' is to be treated as as string.
virtual Int_t GetNdata()
Return number of available instances in the formula.
TTreeTableInterface is used to interface to data that is stored in a TTree.
virtual Double_t GetValue(UInt_t row, UInt_t column)
Return the value of row,column.
virtual ~TTreeTableInterface()
TTreeTableInterface destructor.
virtual const char * GetValueAsString(UInt_t row, UInt_t column)
Return the content of row,column as string to use in a TGTableCell label.
virtual void SetFormula(TTreeFormula *formula, UInt_t position)
Set the TTreeFormula of position to formula.
void SyncFormulas()
Sync the formulas using the TTreeFormulaManager.
virtual void RemoveColumn(UInt_t position)
Remove column at position, TGTable->Update() is needed afterwards to apply the change to the TGTable.
virtual const char * GetColumnHeader(UInt_t column)
Return a string to use as a label for columnheader at column.
virtual UInt_t GetNRows()
Return the amount of rows in the Tree.
TTreeFormulaManager * fManager
TTreeTableInterface(TTree *tree=0, const char *varexp=0, const char *selection=0, Option_t *option=0, Long64_t nentries=0, Long64_t firstentry=0)
TTreeTableInterface constructor.
void InitEntries()
Initialise the TEntryList with the entries that match the selection criterium.
virtual void SetEntryList(TEntryList *entrylist=0)
Set the currently active entrylist.
virtual void SetSelection(const char *selection)
Set the selection expression.
virtual void AddColumn(const char *expression, UInt_t position)
Add column according ot expression at position, TGTable->Update() is needed afterwards to apply the c...
void SetVariablesExpression(const char *varexp)
Compile the variables expression from the given varexp.
virtual const char * GetRowHeader(UInt_t row)
Return a string to use as a label for rowheader at column.
virtual UInt_t GetNColumns()
Return the amount of column available.
TSelectorDraw * fSelector
A TTree represents a columnar dataset.
Definition: TTree.h:78
virtual TObjArray * GetListOfLeaves()
Definition: TTree.h:483
virtual Long64_t GetEntries() const
Definition: TTree.h:457
virtual void SetEntryList(TEntryList *list, Option_t *opt="")
Set an EntryList.
Definition: TTree.cxx:8893
virtual Long64_t LoadTree(Long64_t entry)
Set current entry.
Definition: TTree.cxx:6376
virtual Int_t GetTreeNumber() const
Definition: TTree.h:513
Definition: tree.py:1