Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 "TTreeTableInterface.h"
12#include "TTreeFormula.h"
13#include "TError.h"
14#include "TTree.h"
15#include "TEntryList.h"
16#include "TSelectorDraw.h"
17#include "TTreeFormulaManager.h"
18
20
21/** \class TTreeTableInterface
22
23TTreeTableInterface is used to interface to data that is stored in a
24TTree. When the interface is created, an expression can be
25specified. This expression will define the columns to be shown.
26
27A selection criterium can also be specified. A TEntryList will be
28created and applied to the TTree using this criterium.
29a new TEntryList to use can be specified using SetEntryList.
30TGTable->Update() will need to be called to show the effects.
31
32WARNING: Do not apply an entrylist to the tree in any other way than
33through the interface, this will have undefined consequences.
34
35Columns can be manipulated using the appropriate methods. A
36TGTable->Update is always needed afterwards to make the table aware
37of the changes.
38*/
39
40////////////////////////////////////////////////////////////////////////////////
41/// TTreeTableInterface constructor.
42
44 const char *selection, Option_t *option, Long64_t nentries,
45 Long64_t firstentry)
46 : TVirtualTableInterface(), fTree(tree), fFormulas(0), fEntry(0),
47 fNEntries(nentries), fFirstEntry(firstentry), fManager(0), fSelect(0), fSelector(0), fInput(0),
48 fForceDim(kFALSE), fEntries(0), fNRows(0), fNColumns(0)
49{
50 if (fTree == 0) {
51 Error("TTreeTableInterface", "No tree supplied");
52 return;
53 }
54
55 fFormulas= new TList();
57 fInput = new TList();
58 fInput->Add(new TNamed("varexp",""));
59 fInput->Add(new TNamed("selection",""));
62
63 TString opt = option;
64
65 if (nentries == 0) {
67 Info("TTreeTableInterface", "nentries was 0, setting to maximum number"
68 " available in the tree");
69 }
70
71 // Do stuff with opt.Contains() and options
73 SetSelection(selection);
74
75 if (fNRows == 0) {
76 Warning ("TTreeTableInterface::TTreeTableInterface", "nrows = 0");
77 }
78 if (fNColumns == 0) {
79 Warning ("TTreeTableInterface::TTreeTableInterface", "ncolumns = 0");
80 }
81}
82
83////////////////////////////////////////////////////////////////////////////////
84/// TTreeTableInterface destructor.
85
87{
89 delete fFormulas;
90 delete fInput;
91 delete fSelector;
92
93 if (fTree) fTree->SetEntryList(0);
94 delete fEntries;
95}
96
97////////////////////////////////////////////////////////////////////////////////
98/// Compile the variables expression from the given varexp.
99
101{
102 // FIXME check if enough protection against wrong expressions is in place
103
104 Bool_t allvar = kFALSE;
105
106 if (varexp) {
107 if (!strcmp(varexp, "*")) { allvar = kTRUE; }
108 } else {
109 // if varexp is empty, take all available leaves as a column
110 allvar = kTRUE;
111 }
112
113 if (allvar) {
114 TObjArray *leaves = fTree->GetListOfLeaves();
115 UInt_t nleaves = leaves->GetEntries();
116 if (!nleaves) {
117 Error("TTreeTableInterface", "No leaves in Tree");
118 return;
119 }
120 fNColumns = nleaves;
121 for (UInt_t ui = 0; ui < fNColumns; ui++) {
122 TLeaf *lf = (TLeaf*)leaves->At(ui);
123 fFormulas->Add(new TTreeFormula("Var1", lf->GetName(), fTree));
124 }
125 // otherwise select only the specified columns
126 } else {
127 std::vector<TString> cnames;
128 fNColumns = fSelector->SplitNames(varexp,cnames);
129
130 // Create the TreeFormula objects corresponding to each column
131 for (UInt_t ui = 0; ui < fNColumns; ui++) {
132 fFormulas->Add(new TTreeFormula("Var1", cnames[ui].Data(), fTree));
133 }
134 }
135}
136
137////////////////////////////////////////////////////////////////////////////////
138/// Set the selection expression.
139
140void TTreeTableInterface::SetSelection(const char *selection)
141{
142 // FIXME verify functionality
143 if (fSelect) {
145 delete fSelect;
146 fSelect = 0;
147 }
148 if (selection && strlen(selection)) {
149 fSelect = new TTreeFormula("Selection", selection, fTree);
151 }
152
153 if (fManager) {
154 for (Int_t i = 0; i <= fFormulas->LastIndex(); i++) {
156 }
157 }
158
159 // SyncFormulas() will update the formula manager if needed
160 SyncFormulas();
161 InitEntries();
162}
163
164////////////////////////////////////////////////////////////////////////////////
165/// Sync the formulas using the TTreeFormulaManager.
166
168{
169 // FIXME verify functionality
170
171 Int_t i = 0;
172 if (fFormulas->LastIndex() >= 0) {
173 if (fSelect) {
174 if (fSelect->GetManager()->GetMultiplicity() > 0 ) {
176 for (i = 0; i <= fFormulas->LastIndex(); i++) {
178 }
179 fManager->Sync();
180 }
181 }
182 for (i = 0; i < fFormulas->LastIndex(); i++) {
183 TTreeFormula *form = ((TTreeFormula*)fFormulas->At(i));
184 switch (form->GetManager()->GetMultiplicity()) {
185 case 1:
186 case 2:
187 case -1:
189 break;
190 case 0:
191 break;
192 }
193 }
194 }
195}
196
197////////////////////////////////////////////////////////////////////////////////
198/// Initialise the TEntryList with the entries that match the
199/// selection criterium.
200
202{
203 TEntryList *entrylist = new TEntryList(fTree);
204
205 UInt_t ui = 0;
206 Int_t i = 0;
207
208 Long64_t notSkipped = 0;
209 Int_t tnumber = -1;
210 Long64_t entry = fFirstEntry;
211 Int_t entriesToDisplay = fNEntries;
212
213 while (entriesToDisplay != 0){
214// entryNumber = fTree->GetEntryNumber(entry);
215// if(entryNumber < 0) break;
216 Long64_t localEntry = fTree->LoadTree(entry);
217 if (localEntry < 0) break;
218 if (tnumber != fTree->GetTreeNumber()) {
219 tnumber = fTree->GetTreeNumber();
221 else {
222 for(i = 0; i < fFormulas->LastIndex(); i++)
223 ((TTreeFormula*)fFormulas->At(ui))->UpdateFormulaLeaves();
224 }
225 }
226 Int_t ndata = 1;
227 if (fForceDim){
228 if (fManager)
229 ndata = fManager->GetNdata(kTRUE);
230 else {
231 for (ui = 0; ui < fNColumns; ui++){
232 if (ndata < ((TTreeFormula*)fFormulas->At(ui))->GetNdata())
233 {
234 ndata = ((TTreeFormula*)fFormulas->At(ui))->GetNdata();
235 }
236 }
237 if (fSelect && fSelect->GetNdata() == 0)
238 ndata = 0;
239 }
240 }
241 Bool_t skip = kFALSE;
242
243 // Loop over the instances of the selection condition
244 for (Int_t inst = 0; inst < ndata; inst++){
245 if (fSelect){
246 if (fSelect->EvalInstance(inst) == 0){
247 skip = kTRUE;
248 entry++;
249 }
250 }
251 }
252 if (!skip){
253 entrylist->Enter(entry);
254 notSkipped++;
255 entriesToDisplay--;
256 entry++;
257 }
258 }
259 SetEntryList(entrylist);
260}
261
262////////////////////////////////////////////////////////////////////////////////
263/// Return the value of row,column. If the position does not exist
264/// or does not contain a number, 0 is returned.
265
267{
268 static UInt_t prow = 0;
269
270 if (row < fNRows) {
271 Long64_t entry = 0;
272 if (row == prow + 1) {
273 entry = fEntries->Next();
274 } else {
275 entry = fEntries->GetEntry(row);
276 }
277 prow = row;
278 fTree->LoadTree(entry);
279 } else {
280 Error("TTreeTableInterface", "Row requested does not exist");
281 return 0;
282 }
283 if (column < fNColumns) {
284 TTreeFormula *formula = (TTreeFormula *)fFormulas->At(column);
285 if (!formula->IsString()) {
286 return (Double_t)formula->EvalInstance();
287 } else {
288 Warning("TTreeTableInterface::GetValue", "Value requested is a "
289 "string, returning 0.");
290 return 0;
291 }
292 } else {
293 Error("TTreeTableInterface", "Column requested does not exist");
294 return 0;
295 }
296}
297
298////////////////////////////////////////////////////////////////////////////////
299/// Return the content of row,column as string to use in a
300/// TGTableCell label.
301
303{
304 static UInt_t prow = 0;
305
306 if (row < fNRows) {
307 Long64_t entry = 0;
308 if (row == prow + 1) {
309 entry = fEntries->Next();
310 } else {
311 entry = fEntries->GetEntry(row);
312 }
313 prow = row;
314 fTree->LoadTree(entry);
315 } else {
316 Error("TTreeTableInterface", "Row requested does not exist");
317 return 0;
318 }
319 if (column < fNColumns) {
320 TTreeFormula *formula = (TTreeFormula *)fFormulas->At(column);
321 if(formula->IsString()) {
322 return Form("%s", formula->EvalStringInstance());
323 } else {
324 return Form("%5.2f", (Double_t)formula->EvalInstance());
325 }
326 } else {
327 Error("TTreeTableInterface", "Column requested does not exist");
328 return 0;
329 }
330}
331
332////////////////////////////////////////////////////////////////////////////////
333/// Return a string to use as a label for rowheader at column.
334
336{
337 if (row < fNRows) {
338 return Form("%lld", fEntries->GetEntry(row));
339 } else {
340 Error("TTreeTableInterface", "Row requested does not exist");
341 return "";
342 }
343}
344
345////////////////////////////////////////////////////////////////////////////////
346/// Return a string to use as a label for columnheader at column.
347
349{
350 TTreeFormula *formula = (TTreeFormula *)fFormulas->At(column);
351 if (column < fNColumns) {
352 return formula->GetTitle();
353 } else {
354 Error("TTreeTableInterface", "Column requested does not exist");
355 return "";
356 }
357}
358
359////////////////////////////////////////////////////////////////////////////////
360/// Return the amount of column available.
361
363{
364 return fNColumns;
365}
366
367////////////////////////////////////////////////////////////////////////////////
368/// Return the amount of rows in the Tree.
369
371{
372 return fNRows;
373}
374
375////////////////////////////////////////////////////////////////////////////////
376/// Add column according ot expression at position,
377/// TGTable->Update() is needed afterwards to apply the change to
378/// the TGTable.
379
380void TTreeTableInterface::AddColumn(const char *expression, UInt_t position)
381{
382 TString onerow = expression;
383
384 if (onerow.Contains(':')) {
385 Error("TTreeTableInterface::AddColumn", "Only 1 expression allowed.");
386 return;
387 }
388
389 // Create the TreeFormula objects corresponding to the new expression
390 TTreeFormula *formula = new TTreeFormula("Var1", expression, fTree);
391 fFormulas->AddAt(formula, position);
392
393 if (fManager) {
394 fManager->Add(formula);
395 fManager->Sync();
396 }
397 fNColumns++;
398}
399
400////////////////////////////////////////////////////////////////////////////////
401/// Add column with formula at position, TGTable->Update() is needed
402/// afterwards to apply the change to the TGTable.
403
405{
406 if (position > fNColumns) {
407 Error("TTreeTableInterface::AddColumn", "Please specify a "
408 "valid position.");
409 return;
410 }
411 fFormulas->AddAt(formula, position);
412 if (fManager) {
413 fManager->Add(formula);
414 fManager->Sync();
415 }
416 fNColumns++;
417}
418
419////////////////////////////////////////////////////////////////////////////////
420/// Remove column at position, TGTable->Update() is needed
421/// afterwards to apply the change to the TGTable.
422
424{
425 if (position >= fNColumns) {
426 Error("TTreeTableInterface::RemoveColumn", "Please specify a "
427 "valid column.");
428 return;
429 } else if (fNColumns == 1) {
430 Error("TTreeTableInterface::RemoveColumn", "Can't remove last column");
431 return;
432 }
433
434 TTreeFormula *formula = (TTreeFormula *)fFormulas->RemoveAt(position);
435 if (fManager) {
436 fManager->Remove(formula);
437 fManager->Sync();
438 }
439
440 if (formula) delete formula;
441 fNColumns--;
442}
443
444////////////////////////////////////////////////////////////////////////////////
445/// Set the TTreeFormula of position to formula.
446
448{
449 if (position >= fNColumns) {
450 Error("TTreeTableInterface::SetFormula", "Please specify a "
451 "valid position.");
452 return;
453 }
454 TTreeFormula *form = (TTreeFormula *)fFormulas->RemoveAt(position);
455 if (fSelect) {
456 fManager->Remove(form);
457 }
458 if (form) delete form;
459 fFormulas->AddAt(formula, position);
460 if (fManager) {
461 fManager->Add(formula);
462 fManager->Sync();
463 }
464
465}
466
467////////////////////////////////////////////////////////////////////////////////
468/// Set the currently active entrylist.
469
471{
472 // Untested
473 if (fEntries) delete fEntries;
474 fEntries = entrylist;
475 fNRows = fEntries->GetN();
476 fTree->SetEntryList(entrylist);
477}
const Bool_t kFALSE
Definition RtypesCore.h:92
double Double_t
Definition RtypesCore.h:59
long long Long64_t
Definition RtypesCore.h:73
const Bool_t kTRUE
Definition RtypesCore.h:91
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:364
void Info(const char *location, const char *msgfmt,...)
Use this function for informational messages.
Definition TError.cxx:220
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:187
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
Definition TError.cxx:231
int nentries
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...
virtual Bool_t Enter(Long64_t entry, TTree *tree=0)
Add entry #entry to the list.
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().
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:57
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:822
virtual void AddAt(TObject *obj, Int_t idx)
Insert object at position idx in the list.
Definition TList.cxx:306
virtual TObject * At(Int_t idx) const
Returns the object at position idx. Returns 0 if idx is out of range.
Definition TList.cxx:357
virtual void Delete(Option_t *option="")
Remove all objects from the list AND delete all heap based objects.
Definition TList.cxx:470
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.
TObject * At(Int_t idx) const
Definition TObjArray.h:166
A specialized TSelector for TTree::Draw.
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:66
Int_t LastIndex() const
virtual TObject * RemoveAt(Int_t idx)
Basic string class.
Definition TString.h:136
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:624
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.
TTreeFormulaManager * GetManager() const
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:79
virtual TObjArray * GetListOfLeaves()
Definition TTree.h:486
virtual Long64_t GetEntries() const
Definition TTree.h:460
virtual void SetEntryList(TEntryList *list, Option_t *opt="")
Set an EntryList.
Definition TTree.cxx:9005
virtual Long64_t LoadTree(Long64_t entry)
Set current entry.
Definition TTree.cxx:6454
virtual Int_t GetTreeNumber() const
Definition TTree.h:516
Definition tree.py:1