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 Int_t tnumber = -1;
209 Long64_t entry = fFirstEntry;
210 Int_t entriesToDisplay = fNEntries;
211
212 while (entriesToDisplay != 0){
213// entryNumber = fTree->GetEntryNumber(entry);
214// if(entryNumber < 0) break;
215 Long64_t localEntry = fTree->LoadTree(entry);
216 if (localEntry < 0) break;
217 if (tnumber != fTree->GetTreeNumber()) {
218 tnumber = fTree->GetTreeNumber();
220 else {
221 for(i = 0; i < fFormulas->LastIndex(); i++)
222 ((TTreeFormula*)fFormulas->At(ui))->UpdateFormulaLeaves();
223 }
224 }
225 Int_t ndata = 1;
226 if (fForceDim){
227 if (fManager)
228 ndata = fManager->GetNdata(kTRUE);
229 else {
230 for (ui = 0; ui < fNColumns; ui++){
231 if (ndata < ((TTreeFormula*)fFormulas->At(ui))->GetNdata())
232 {
233 ndata = ((TTreeFormula*)fFormulas->At(ui))->GetNdata();
234 }
235 }
236 if (fSelect && fSelect->GetNdata() == 0)
237 ndata = 0;
238 }
239 }
240 Bool_t skip = kFALSE;
241
242 // Loop over the instances of the selection condition
243 for (Int_t inst = 0; inst < ndata; inst++){
244 if (fSelect){
245 if (fSelect->EvalInstance(inst) == 0){
246 skip = kTRUE;
247 entry++;
248 }
249 }
250 }
251 if (!skip){
252 entrylist->Enter(entry);
253 entriesToDisplay--;
254 entry++;
255 }
256 }
257 SetEntryList(entrylist);
258}
259
260////////////////////////////////////////////////////////////////////////////////
261/// Return the value of row,column. If the position does not exist
262/// or does not contain a number, 0 is returned.
263
265{
266 static UInt_t prow = 0;
267
268 if (row < fNRows) {
269 Long64_t entry = 0;
270 if (row == prow + 1) {
271 entry = fEntries->Next();
272 } else {
273 entry = fEntries->GetEntry(row);
274 }
275 prow = row;
276 fTree->LoadTree(entry);
277 } else {
278 Error("TTreeTableInterface", "Row requested does not exist");
279 return 0;
280 }
281 if (column < fNColumns) {
282 TTreeFormula *formula = (TTreeFormula *)fFormulas->At(column);
283 if (!formula->IsString()) {
284 return (Double_t)formula->EvalInstance();
285 } else {
286 Warning("TTreeTableInterface::GetValue", "Value requested is a "
287 "string, returning 0.");
288 return 0;
289 }
290 } else {
291 Error("TTreeTableInterface", "Column requested does not exist");
292 return 0;
293 }
294}
295
296////////////////////////////////////////////////////////////////////////////////
297/// Return the content of row,column as string to use in a
298/// TGTableCell label.
299
301{
302 static UInt_t prow = 0;
303
304 if (row < fNRows) {
305 Long64_t entry = 0;
306 if (row == prow + 1) {
307 entry = fEntries->Next();
308 } else {
309 entry = fEntries->GetEntry(row);
310 }
311 prow = row;
312 fTree->LoadTree(entry);
313 } else {
314 Error("TTreeTableInterface", "Row requested does not exist");
315 return 0;
316 }
317 if (column < fNColumns) {
318 TTreeFormula *formula = (TTreeFormula *)fFormulas->At(column);
319 if(formula->IsString()) {
320 return Form("%s", formula->EvalStringInstance());
321 } else {
322 return Form("%5.2f", (Double_t)formula->EvalInstance());
323 }
324 } else {
325 Error("TTreeTableInterface", "Column requested does not exist");
326 return 0;
327 }
328}
329
330////////////////////////////////////////////////////////////////////////////////
331/// Return a string to use as a label for rowheader at column.
332
334{
335 if (row < fNRows) {
336 return Form("%lld", fEntries->GetEntry(row));
337 } else {
338 Error("TTreeTableInterface", "Row requested does not exist");
339 return "";
340 }
341}
342
343////////////////////////////////////////////////////////////////////////////////
344/// Return a string to use as a label for columnheader at column.
345
347{
348 TTreeFormula *formula = (TTreeFormula *)fFormulas->At(column);
349 if (column < fNColumns) {
350 return formula->GetTitle();
351 } else {
352 Error("TTreeTableInterface", "Column requested does not exist");
353 return "";
354 }
355}
356
357////////////////////////////////////////////////////////////////////////////////
358/// Return the amount of column available.
359
361{
362 return fNColumns;
363}
364
365////////////////////////////////////////////////////////////////////////////////
366/// Return the amount of rows in the Tree.
367
369{
370 return fNRows;
371}
372
373////////////////////////////////////////////////////////////////////////////////
374/// Add column according ot expression at position,
375/// TGTable->Update() is needed afterwards to apply the change to
376/// the TGTable.
377
378void TTreeTableInterface::AddColumn(const char *expression, UInt_t position)
379{
380 TString onerow = expression;
381
382 if (onerow.Contains(':')) {
383 Error("TTreeTableInterface::AddColumn", "Only 1 expression allowed.");
384 return;
385 }
386
387 // Create the TreeFormula objects corresponding to the new expression
388 TTreeFormula *formula = new TTreeFormula("Var1", expression, fTree);
389 fFormulas->AddAt(formula, position);
390
391 if (fManager) {
392 fManager->Add(formula);
393 fManager->Sync();
394 }
395 fNColumns++;
396}
397
398////////////////////////////////////////////////////////////////////////////////
399/// Add column with formula at position, TGTable->Update() is needed
400/// afterwards to apply the change to the TGTable.
401
403{
404 if (position > fNColumns) {
405 Error("TTreeTableInterface::AddColumn", "Please specify a "
406 "valid position.");
407 return;
408 }
409 fFormulas->AddAt(formula, position);
410 if (fManager) {
411 fManager->Add(formula);
412 fManager->Sync();
413 }
414 fNColumns++;
415}
416
417////////////////////////////////////////////////////////////////////////////////
418/// Remove column at position, TGTable->Update() is needed
419/// afterwards to apply the change to the TGTable.
420
422{
423 if (position >= fNColumns) {
424 Error("TTreeTableInterface::RemoveColumn", "Please specify a "
425 "valid column.");
426 return;
427 } else if (fNColumns == 1) {
428 Error("TTreeTableInterface::RemoveColumn", "Can't remove last column");
429 return;
430 }
431
432 TTreeFormula *formula = (TTreeFormula *)fFormulas->RemoveAt(position);
433 if (fManager) {
434 fManager->Remove(formula);
435 fManager->Sync();
436 }
437
438 if (formula) delete formula;
439 fNColumns--;
440}
441
442////////////////////////////////////////////////////////////////////////////////
443/// Set the TTreeFormula of position to formula.
444
446{
447 if (position >= fNColumns) {
448 Error("TTreeTableInterface::SetFormula", "Please specify a "
449 "valid position.");
450 return;
451 }
452 TTreeFormula *form = (TTreeFormula *)fFormulas->RemoveAt(position);
453 if (fSelect) {
454 fManager->Remove(form);
455 }
456 if (form) delete form;
457 fFormulas->AddAt(formula, position);
458 if (fManager) {
459 fManager->Add(formula);
460 fManager->Sync();
461 }
462
463}
464
465////////////////////////////////////////////////////////////////////////////////
466/// Set the currently active entrylist.
467
469{
470 // Untested
471 if (fEntries) delete fEntries;
472 fEntries = entrylist;
473 fNRows = fEntries->GetN();
474 fTree->SetEntryList(entrylist);
475}
constexpr Bool_t kFALSE
Definition RtypesCore.h:101
long long Long64_t
Definition RtypesCore.h:80
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:377
void Info(const char *location, const char *msgfmt,...)
Use this function for informational messages.
Definition TError.cxx:230
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:197
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
Definition TError.cxx:241
Option_t Option_t option
int nentries
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2467
A List of entry numbers in a TTree or TChain.
Definition TEntryList.h:26
virtual Bool_t Enter(Long64_t entry, TTree *tree=nullptr)
Add entry #entry to the list.
virtual Long64_t Next()
Return the next non-zero entry index (next after fLastIndexQueried) this function is faster than GetE...
virtual Long64_t GetEntry(Long64_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:78
A TLeaf describes individual elements of a TBranch See TBranch structure in TTree.
Definition TLeaf.h:57
A doubly linked list.
Definition TList.h:38
void AddAt(TObject *obj, Int_t idx) override
Insert object at position idx in the list.
Definition TList.cxx:306
void Add(TObject *obj) override
Definition TList.h:81
TObject * Remove(TObject *obj) override
Remove object from the list.
Definition TList.cxx:822
void Delete(Option_t *option="") override
Remove all objects from the list AND delete all heap based objects.
Definition TList.cxx:470
TObject * At(Int_t idx) const override
Returns the object at position idx. Returns 0 if idx is out of range.
Definition TList.cxx:357
The TNamed class is the base class for all named ROOT classes.
Definition TNamed.h:29
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
const char * GetTitle() const override
Returns title of object.
Definition TNamed.h:48
An array of TObjects.
Definition TObjArray.h:31
Int_t GetEntries() const override
Return the number of objects in array (i.e.
TObject * At(Int_t idx) const override
Definition TObjArray.h:164
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:139
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:636
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[]=nullptr)
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.
Long64_t fFirstEntry
First entry.
Long64_t fEntry
Present entry number in fTree.
virtual const char * GetValueAsString(UInt_t row, UInt_t column)
Return the content of row,column as string to use in a TGTableCell label.
UInt_t fNRows
Amount of rows in the data.
virtual void SetFormula(TTreeFormula *formula, UInt_t position)
Set the TTreeFormula of position to formula.
void SyncFormulas()
Sync the formulas using the TTreeFormulaManager.
UInt_t fNColumns
Amount of columns in the data.
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.
Bool_t fForceDim
Force dimension.
virtual UInt_t GetNRows()
Return the amount of rows in the Tree.
TTreeFormula * fSelect
Selection condition.
TTreeFormulaManager * fManager
Coordinator for the formulas.
virtual void SetEntryList(TEntryList *entrylist=nullptr)
Set the currently active entrylist.
void InitEntries()
Initialise the TEntryList with the entries that match the selection criterium.
TTree * fTree
Data in a TTree.
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...
TList * fFormulas
Array of TTreeFormulas to display values.
Long64_t fNEntries
Number of entries in the tree.
void SetVariablesExpression(const char *varexp)
Compile the variables expression from the given varexp.
TEntryList * fEntries
Currently active entries.
TTreeTableInterface(TTree *tree=nullptr, const char *varexp=nullptr, const char *selection=nullptr, Option_t *option=nullptr, Long64_t nentries=0, Long64_t firstentry=0)
TTreeTableInterface constructor.
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
Selector.
TList * fInput
Used for 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:9027
virtual Long64_t LoadTree(Long64_t entry)
Set current entry.
Definition TTree.cxx:6464
virtual Int_t GetTreeNumber() const
Definition TTree.h:516
Definition tree.py:1