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
19
20/** \class TTreeTableInterface
21
22TTreeTableInterface is used to interface to data that is stored in a
23TTree. When the interface is created, an expression can be
24specified. This expression will define the columns to be shown.
25
26A selection criterium can also be specified. A TEntryList will be
27created and applied to the TTree using this criterium.
28a new TEntryList to use can be specified using SetEntryList.
29TGTable->Update() will need to be called to show the effects.
30
31WARNING: Do not apply an entrylist to the tree in any other way than
32through the interface, this will have undefined consequences.
33
34Columns can be manipulated using the appropriate methods. A
35TGTable->Update is always needed afterwards to make the table aware
36of the changes.
37*/
38
39////////////////////////////////////////////////////////////////////////////////
40/// TTreeTableInterface constructor.
41
45 : TVirtualTableInterface(), fTree(tree), fFormulas(nullptr), fEntry(0),
46 fNEntries(nentries), fFirstEntry(firstentry), fManager(nullptr), fSelect(nullptr), fSelector(nullptr), fInput(nullptr),
47 fForceDim(false), fEntries(nullptr), fNRows(0), fNColumns(0)
48{
49 if (fTree == nullptr) {
50 Error("TTreeTableInterface", "No tree supplied");
51 return;
52 }
53
54 fFormulas= new TList();
56 fInput = new TList();
57 fInput->Add(new TNamed("varexp",""));
58 fInput->Add(new TNamed("selection",""));
61
62 TString opt = option;
63
64 if (nentries == 0) {
66 Info("TTreeTableInterface", "nentries was 0, setting to maximum number"
67 " available in the tree");
68 }
69
70 // Do stuff with opt.Contains() and options
73
74 if (fNRows == 0) {
75 Warning ("TTreeTableInterface::TTreeTableInterface", "nrows = 0");
76 }
77 if (fNColumns == 0) {
78 Warning ("TTreeTableInterface::TTreeTableInterface", "ncolumns = 0");
79 }
80}
81
82////////////////////////////////////////////////////////////////////////////////
83/// TTreeTableInterface destructor.
84
86{
88 delete fFormulas;
89 delete fInput;
90 delete fSelector;
91
92 if (fTree) fTree->SetEntryList(nullptr);
93 delete fEntries;
94}
95
96////////////////////////////////////////////////////////////////////////////////
97/// Compile the variables expression from the given varexp.
98
100{
101 // FIXME check if enough protection against wrong expressions is in place
102
103 bool allvar = false;
104
105 if (varexp) {
106 if (!strcmp(varexp, "*")) { allvar = true; }
107 } else {
108 // if varexp is empty, take all available leaves as a column
109 allvar = true;
110 }
111
112 if (allvar) {
114 UInt_t nleaves = leaves->GetEntries();
115 if (!nleaves) {
116 Error("TTreeTableInterface", "No leaves in Tree");
117 return;
118 }
120 for (UInt_t ui = 0; ui < fNColumns; ui++) {
121 TLeaf *lf = (TLeaf*)leaves->At(ui);
122 fFormulas->Add(new TTreeFormula("Var1", lf->GetName(), fTree));
123 }
124 // otherwise select only the specified columns
125 } else {
126 std::vector<TString> cnames;
128
129 // Create the TreeFormula objects corresponding to each column
130 for (UInt_t ui = 0; ui < fNColumns; ui++) {
131 fFormulas->Add(new TTreeFormula("Var1", cnames[ui].Data(), fTree));
132 }
133 }
134}
135
136////////////////////////////////////////////////////////////////////////////////
137/// Set the selection expression.
138
140{
141 // FIXME verify functionality
142 if (fSelect) {
144 delete fSelect;
145 fSelect = nullptr;
146 }
147 if (selection && strlen(selection)) {
148 fSelect = new TTreeFormula("Selection", selection, fTree);
150 }
151
152 if (fManager) {
153 for (Int_t i = 0; i <= fFormulas->LastIndex(); i++) {
155 }
156 }
157
158 // SyncFormulas() will update the formula manager if needed
159 SyncFormulas();
160 InitEntries();
161}
162
163////////////////////////////////////////////////////////////////////////////////
164/// Sync the formulas using the TTreeFormulaManager.
165
167{
168 // FIXME verify functionality
169
170 Int_t i = 0;
171 if (fFormulas->LastIndex() >= 0) {
172 if (fSelect) {
173 if (fSelect->GetManager()->GetMultiplicity() > 0 ) {
175 for (i = 0; i <= fFormulas->LastIndex(); i++) {
177 }
178 fManager->Sync();
179 }
180 }
181 for (i = 0; i < fFormulas->LastIndex(); i++) {
183 switch (form->GetManager()->GetMultiplicity()) {
184 case 1:
185 case 2:
186 case -1:
187 fForceDim = true;
188 break;
189 case 0:
190 break;
191 }
192 }
193 }
194}
195
196////////////////////////////////////////////////////////////////////////////////
197/// Initialise the TEntryList with the entries that match the
198/// selection criterium.
199
201{
203
204 UInt_t ui = 0;
205 Int_t i = 0;
206
207 Int_t tnumber = -1;
210
211 while (entriesToDisplay != 0){
212// entryNumber = fTree->GetEntryNumber(entry);
213// if(entryNumber < 0) break;
215 if (localEntry < 0) break;
216 if (tnumber != fTree->GetTreeNumber()) {
219 else {
220 for(i = 0; i < fFormulas->LastIndex(); i++)
221 ((TTreeFormula*)fFormulas->At(ui))->UpdateFormulaLeaves();
222 }
223 }
224 Int_t ndata = 1;
225 if (fForceDim){
226 if (fManager)
227 ndata = fManager->GetNdata(true);
228 else {
229 for (ui = 0; ui < fNColumns; ui++){
230 if (ndata < ((TTreeFormula*)fFormulas->At(ui))->GetNdata())
231 {
232 ndata = ((TTreeFormula*)fFormulas->At(ui))->GetNdata();
233 }
234 }
235 if (fSelect && fSelect->GetNdata() == 0)
236 ndata = 0;
237 }
238 }
239 bool skip = false;
240
241 // Loop over the instances of the selection condition
242 for (Int_t inst = 0; inst < ndata; inst++){
243 if (fSelect){
244 if (fSelect->EvalInstance(inst) == 0){
245 skip = true;
246 entry++;
247 }
248 }
249 }
250 if (!skip){
251 entrylist->Enter(entry);
253 entry++;
254 }
255 }
257}
258
259////////////////////////////////////////////////////////////////////////////////
260/// Return the value of row,column. If the position does not exist
261/// or does not contain a number, 0 is returned.
262
264{
265 static UInt_t prow = 0;
266
267 if (row < fNRows) {
268 Long64_t entry = 0;
269 if (row == prow + 1) {
270 entry = fEntries->Next();
271 } else {
272 entry = fEntries->GetEntry(row);
273 }
274 prow = row;
276 } else {
277 Error("TTreeTableInterface", "Row requested does not exist");
278 return 0;
279 }
280 if (column < fNColumns) {
281 TTreeFormula *formula = (TTreeFormula *)fFormulas->At(column);
282 if (!formula->IsString()) {
283 return (Double_t)formula->EvalInstance();
284 } else {
285 Warning("TTreeTableInterface::GetValue", "Value requested is a "
286 "string, returning 0.");
287 return 0;
288 }
289 } else {
290 Error("TTreeTableInterface", "Column requested does not exist");
291 return 0;
292 }
293}
294
295////////////////////////////////////////////////////////////////////////////////
296/// Return the content of row,column as string to use in a
297/// TGTableCell label.
298
300{
301 static UInt_t prow = 0;
302
303 if (row < fNRows) {
304 Long64_t entry = 0;
305 if (row == prow + 1) {
306 entry = fEntries->Next();
307 } else {
308 entry = fEntries->GetEntry(row);
309 }
310 prow = row;
312 } else {
313 Error("TTreeTableInterface", "Row requested does not exist");
314 return nullptr;
315 }
316 if (column < fNColumns) {
317 TTreeFormula *formula = (TTreeFormula *)fFormulas->At(column);
318 if(formula->IsString()) {
319 return Form("%s", formula->EvalStringInstance());
320 } else {
321 return Form("%5.2f", (Double_t)formula->EvalInstance());
322 }
323 } else {
324 Error("TTreeTableInterface", "Column requested does not exist");
325 return nullptr;
326 }
327}
328
329////////////////////////////////////////////////////////////////////////////////
330/// Return a string to use as a label for rowheader at column.
331
333{
334 if (row < fNRows) {
335 return Form("%lld", fEntries->GetEntry(row));
336 } else {
337 Error("TTreeTableInterface", "Row requested does not exist");
338 return "";
339 }
340}
341
342////////////////////////////////////////////////////////////////////////////////
343/// Return a string to use as a label for columnheader at column.
344
346{
347 TTreeFormula *formula = (TTreeFormula *)fFormulas->At(column);
348 if (column < fNColumns) {
349 return formula->GetTitle();
350 } else {
351 Error("TTreeTableInterface", "Column requested does not exist");
352 return "";
353 }
354}
355
356////////////////////////////////////////////////////////////////////////////////
357/// Return the amount of column available.
358
363
364////////////////////////////////////////////////////////////////////////////////
365/// Return the amount of rows in the Tree.
366
371
372////////////////////////////////////////////////////////////////////////////////
373/// Add column according ot expression at position,
374/// TGTable->Update() is needed afterwards to apply the change to
375/// the TGTable.
376
377void TTreeTableInterface::AddColumn(const char *expression, UInt_t position)
378{
379 TString onerow = expression;
380
381 if (onerow.Contains(':')) {
382 Error("TTreeTableInterface::AddColumn", "Only 1 expression allowed.");
383 return;
384 }
385
386 // Create the TreeFormula objects corresponding to the new expression
387 TTreeFormula *formula = new TTreeFormula("Var1", expression, fTree);
388 fFormulas->AddAt(formula, position);
389
390 if (fManager) {
391 fManager->Add(formula);
392 fManager->Sync();
393 }
394 fNColumns++;
395}
396
397////////////////////////////////////////////////////////////////////////////////
398/// Add column with formula at position, TGTable->Update() is needed
399/// afterwards to apply the change to the TGTable.
400
402{
403 if (position > fNColumns) {
404 Error("TTreeTableInterface::AddColumn", "Please specify a "
405 "valid position.");
406 return;
407 }
408 fFormulas->AddAt(formula, position);
409 if (fManager) {
410 fManager->Add(formula);
411 fManager->Sync();
412 }
413 fNColumns++;
414}
415
416////////////////////////////////////////////////////////////////////////////////
417/// Remove column at position, TGTable->Update() is needed
418/// afterwards to apply the change to the TGTable.
419
421{
422 if (position >= fNColumns) {
423 Error("TTreeTableInterface::RemoveColumn", "Please specify a "
424 "valid column.");
425 return;
426 } else if (fNColumns == 1) {
427 Error("TTreeTableInterface::RemoveColumn", "Can't remove last column");
428 return;
429 }
430
431 TTreeFormula *formula = (TTreeFormula *)fFormulas->RemoveAt(position);
432 if (fManager) {
433 fManager->Remove(formula);
434 fManager->Sync();
435 }
436
437 if (formula) delete formula;
438 fNColumns--;
439}
440
441////////////////////////////////////////////////////////////////////////////////
442/// Set the TTreeFormula of position to formula.
443
445{
446 if (position >= fNColumns) {
447 Error("TTreeTableInterface::SetFormula", "Please specify a "
448 "valid position.");
449 return;
450 }
452 if (fSelect) {
454 }
455 if (form) delete form;
456 fFormulas->AddAt(formula, position);
457 if (fManager) {
458 fManager->Add(formula);
459 fManager->Sync();
460 }
461
462}
463
464////////////////////////////////////////////////////////////////////////////////
465/// Set the currently active entrylist.
466
long long Long64_t
Portable signed long integer 8 bytes.
Definition RtypesCore.h:83
const char Option_t
Option string (const char)
Definition RtypesCore.h:80
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
void Info(const char *location, const char *msgfmt,...)
Use this function for informational messages.
Definition TError.cxx:241
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:208
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
Definition TError.cxx:252
Option_t Option_t option
int nentries
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2495
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 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:303
void Add(TObject *obj) override
Definition TList.h:81
TObject * Remove(TObject *obj) override
Remove object from the list.
Definition TList.cxx:819
void Delete(Option_t *option="") override
Remove all objects from the list AND delete all heap based objects.
Definition TList.cxx:467
TObject * At(Int_t idx) const override
Returns the object at position idx. Returns 0 if idx is out of range.
Definition TList.cxx:354
The TNamed class is the base class for all named ROOT classes.
Definition TNamed.h:29
const char * GetTitle() const override
Returns title of object.
Definition TNamed.h:50
An array of TObjects.
Definition TObjArray.h:31
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:138
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 forceLoadDim=false)
Return number of available instances in the formulas.
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 bool Sync()
Synchronize all the formulae.
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.
bool IsString(Int_t oper) const override
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.
const char * GetRowHeader(UInt_t row) override
Return a string to use as a label for rowheader at column.
~TTreeTableInterface() override
TTreeTableInterface destructor.
Long64_t fFirstEntry
First entry.
Long64_t fEntry
Present entry number in fTree.
const char * GetColumnHeader(UInt_t column) override
Return a string to use as a label for columnheader at column.
UInt_t GetNColumns() override
Return the amount of column available.
UInt_t fNRows
Amount of rows in the data.
virtual void SetFormula(TTreeFormula *formula, UInt_t position)
Set the TTreeFormula of position to formula.
const char * GetValueAsString(UInt_t row, UInt_t column) override
Return the content of row,column as string to use in a TGTableCell label.
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.
UInt_t GetNRows() override
Return the amount of rows in the Tree.
TTreeFormula * fSelect
Selection condition.
TTreeFormulaManager * fManager
Coordinator for the formulas.
Double_t GetValue(UInt_t row, UInt_t column) override
Return the value of row,column.
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.
bool fForceDim
Force dimension.
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.
TSelectorDraw * fSelector
Selector.
TList * fInput
Used for fSelector.
A TTree represents a columnar dataset.
Definition TTree.h:89
virtual TObjArray * GetListOfLeaves()
Definition TTree.h:568
virtual Long64_t GetEntries() const
Definition TTree.h:502
virtual void SetEntryList(TEntryList *list, Option_t *opt="")
Set an EntryList.
Definition TTree.cxx:9253
virtual Long64_t LoadTree(Long64_t entry)
Set current entry.
Definition TTree.cxx:6554
virtual Int_t GetTreeNumber() const
Definition TTree.h:598