Logo ROOT  
Reference Guide
REveDataClasses.cxx
Go to the documentation of this file.
1// @(#)root/eve7:$Id$
2// Authors: Matevz Tadel & Alja Mrak-Tadel: 2006, 2007, 2018
3
4/*************************************************************************
5 * Copyright (C) 1995-2019, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
13#include <ROOT/REveUtil.hxx>
14
15#include "TROOT.h"
16#include "TMethod.h"
17#include "TMethodArg.h"
18#include "TColor.h"
19#include "TClass.h"
20#include "TBaseClass.h"
21
22#include "json.hpp"
23#include <sstream>
24
25
26using namespace ROOT::Experimental;
27namespace REX = ROOT::Experimental;
28
29
31
32//==============================================================================
33// REveDataCollection
34//==============================================================================
35
36REveDataCollection::REveDataCollection(const std::string& n, const std::string& t) :
37 REveElement(n, t)
38{
39 fChildClass = TClass::GetClass<REveDataItem>();
40
42
43 _handler_func = 0;
45}
46
47void REveDataCollection::AddItem(void *data_ptr, const std::string& n, const std::string& t)
48{
49 auto el = new REveDataItem(n, t);
50 AddElement(el);
51 el->SetMainColor(GetMainColor());
52 fItems.emplace_back(data_ptr, el);
53}
54
55//------------------------------------------------------------------------------
56
58{
59 static const REveException eh("REveDataCollection::SetFilterExpr ");
60
61 if (!fItemClass) throw eh + "item class has to be set before the filter expression.";
62
63 fFilterExpr = filter;
64
65 std::stringstream s;
66 s << "*((std::function<bool(" << fItemClass->GetName() << "*)>*)" << std::hex << std::showbase << (size_t)&fFilterFoo
67 << ") = [](" << fItemClass->GetName() << "* p){" << fItemClass->GetName() << " &i=*p; return ("
68 << fFilterExpr.Data() << "); }";
69
70 // printf("%s\n", s.Data());
71 try {
72 gROOT->ProcessLine(s.str().c_str());
73 // AMT I don't know why ApplyFilter call is separated
75 }
76 catch (const std::exception &exc)
77 {
78 std::cerr << "EveDataCollection::SetFilterExpr" << exc.what();
79 }
80
81}
82
84{
85 Ids_t ids;
86 int idx = 0;
87 for (auto &ii : fItems)
88 {
89 bool res = fFilterFoo(ii.fDataPtr);
90
91 // printf("Item:%s -- filter result = %d\n", ii.fItemPtr->GetElementName(), res);
92
93 ii.fItemPtr->SetFiltered( ! res );
94
95 ids.push_back(idx++);
96 }
98 if (_handler_func_ids) _handler_func_ids( this , ids);
99}
100
101//______________________________________________________________________________
102
104{
105 struct PubMethods
106 {
107 void FillJSON(TClass* c, nlohmann::json & arr)
108 {
109 TString ctor = c->GetName(), dtor = "~";
110 {
111 int i = ctor.Last(':');
112 if (i != kNPOS)
113 {
114 ctor.Replace(0, i + 1, "");
115 }
116 dtor += ctor;
117 }
118
119 TMethod *meth;
120 TIter next(c->GetListOfMethods());
121 while ((meth = (TMethod*) next()))
122 {
123 // Filter out ctor, dtor, some ROOT stuff.
124 {
125 TString m(meth->GetName());
126 if (m == ctor || m == dtor ||
127 m == "Class" || m == "Class_Name" || m == "Class_Version" || m == "Dictionary" || m == "IsA" ||
128 m == "DeclFileName" || m == "ImplFileName" || m == "DeclFileLine" || m == "ImplFileLine" ||
129 m == "Streamer" || m == "StreamerNVirtual" || m == "ShowMembers" ||
130 m == "CheckTObjectHashConsistency")
131 {
132 continue;
133 }
134 }
135
136 TString ms;
137 TMethodArg *ma;
138 TIter next_ma(meth->GetListOfMethodArgs());
139 while ((ma = (TMethodArg*) next_ma()))
140 {
141 if ( ! ms.IsNull()) ms += ", ";
142
143 ms += ma->GetTypeName();
144 ms += " ";
145 ms += ma->GetName();
146 }
147 char* entry = Form("i.%s(%s)",meth->GetName(),ms.Data());
148 nlohmann::json jm ;
149 jm["f"] = entry;
150 jm["r"] = meth->GetReturnTypeName();
151 jm["c"] = c->GetName();
152 arr.push_back(jm);
153 }
154 {
155 TBaseClass *base;
156 TIter blnext(c->GetListOfBases());
157 while ((base = (TBaseClass*) blnext()))
158 {
159 FillJSON(base->GetClassPointer(), arr);
160 }
161 }
162 }
163 };
164
165 Int_t ret = REveElement::WriteCoreJson(j, rnr_offset);
166 j["fFilterExpr"] = fFilterExpr.Data();
167 j["fPublicFunctions"] = nlohmann::json::array();
168 PubMethods pm;
169 pm.FillJSON(fItemClass, j["fPublicFunctions"]);
170
171 return ret;
172}
173
174//______________________________________________________________________________
175
177{
178 int idx = 0;
179 Ids_t ids;
180 for (auto & chld : fChildren)
181 {
182 chld->SetMainColor(newv);
183 ids.push_back(idx);
184 idx++;
185 }
186
188 // printf("REveDataCollection::SetCollectionColorRGB color ched to %d ->%d\n", oldv, GetMainColor());
189 if ( _handler_func_ids) _handler_func_ids( this , ids);
190}
191
192//______________________________________________________________________________
193
195{
196 Bool_t ret = REveElement::SetRnrState(iRnrSelf);
197
198 Ids_t ids;
199
200 for (int i = 0; i < GetNItems(); ++i ) {
201 ids.push_back(i);
203 }
204
205 _handler_func_ids( this , ids);
206
207 return ret;
208}
209
210
211
212//______________________________________________________________________________
213
215{
216 int idx = 0;
217 Ids_t ids;
218 for (auto & chld : fChildren)
219 {
220 if (chld == iItem) {
221 ids.push_back(idx);
222 _handler_func_ids( this , ids);
223 return;
224 }
225 idx++;
226 }
227}
228
229//==============================================================================
230// REveDataItem
231//==============================================================================
232
233REveDataItem::REveDataItem(const std::string& n, const std::string& t) :
234 REveElement(n, t)
235{
237}
238
240{
241 Int_t ret = REveElement::WriteCoreJson(j, rnr_offset);
242 j["fFiltered"] = fFiltered;
243 return ret;
244}
245
247{
248 Color_t color = TColor::GetColor(r, g, b);
251 c->ItemChanged(this);
252}
253
255{
256 Bool_t r = REveElement::SetRnrSelf(iRnrSelf);
258 c->ItemChanged(this);
259 return r;
260}
261
263{
264 if (f != fFiltered)
265 {
266 fFiltered = f;
268 }
269}
270
272{
273 for (auto &n : fNieces)
274 {
275 impSelSet.insert(n);
276 n->FillImpliedSelectedSet(impSelSet);
277
278 if (gDebug > 1)
279 {
280 printf("REveDataItem::FillImpliedSelectedSet added niece '%s' [%s]\n",
281 n->GetCName(), n->IsA()->GetName());
282 }
283 }
284}
285
286//==============================================================================
287// REveDataTable
288//==============================================================================
289
290REveDataTable::REveDataTable(const std::string& n, const std::string& t) :
291 REveElement(n, t)
292{
293 fChildClass = TClass::GetClass<REveDataColumn>();
294}
295
297{
298 Int_t Nit = fCollection->GetNItems();
299
300 for (Int_t i = 0; i< Nit; ++i)
301 {
302 void *data = fCollection->GetDataPtr(i);
304
305 printf("| %-20s |", item->GetCName());
306
307 for (auto & chld : fChildren)
308 {
309 auto clmn = dynamic_cast<REveDataColumn*>(chld);
310
311 printf(" %10s |", clmn->EvalExpr(data).c_str());
312 }
313 printf("\n");
314 }
315}
316
318{
319 int ret = REveElement::WriteCoreJson(j, rnr_offset);
320 Int_t Nit = fCollection->GetNItems();
321
322 nlohmann::json jarr = nlohmann::json::array();
323
324 for (Int_t i = 0; i < Nit; ++i) {
325 void *data = fCollection->GetDataPtr(i);
326 nlohmann::json row;
327 for (auto &chld : fChildren) {
328 auto clmn = dynamic_cast<REveDataColumn *>(chld);
329 row[chld->GetCName()] = clmn->EvalExpr(data);
330 }
331 jarr.push_back(row);
332 }
333 j["body"] = jarr;
334 j["fCollectionId"] = fCollection->GetElementId();
335 return ret;
336}
337
338void REveDataTable::AddNewColumn(const std::string& expr, const std::string& title, int prec)
339{
340 auto c = new REX::REveDataColumn(title);
341 AddElement(c);
342
343 c->SetExpressionAndType(expr, REX::REveDataColumn::FT_Double);
344 c->SetPrecision(prec);
345
347}
348
349//==============================================================================
350// REveDataColumn
351//==============================================================================
352
353REveDataColumn::REveDataColumn(const std::string& n, const std::string& t) :
354 REveElement(n, t)
355{
356}
357
359{
360 auto table = dynamic_cast<REveDataTable*>(fMother);
361 auto coll = table->GetCollection();
362 auto icls = coll->GetItemClass();
363
364 fExpression = expr;
365 fType = type;
366
367 const char *rtyp = nullptr;
368 const void *fooptr = nullptr;
369
370 switch (fType)
371 {
372 case FT_Double: rtyp = "double"; fooptr = &fDoubleFoo; break;
373 case FT_Bool: rtyp = "bool"; fooptr = &fBoolFoo; break;
374 case FT_String: rtyp = "std::string"; fooptr = &fStringFoo; break;
375 }
376
377 std::stringstream s;
378 s << "*((std::function<" << rtyp << "(" << icls->GetName() << "*)>*)" << std::hex << std::showbase << (size_t)fooptr
379 << ") = [](" << icls->GetName() << "* p){" << icls->GetName() << " &i=*p; return (" << fExpression.Data()
380 << "); }";
381
382 // printf("%s\n", s.str().c_str());
383 try {
384 gROOT->ProcessLine(s.str().c_str());
385 }
386 catch (const std::exception &exc)
387 {
388 std::cerr << "REveDataColumn::SetExpressionAndType" << exc.what();
389 }
390}
391
393{
394 fPrecision = prec;
395}
396
397std::string REveDataColumn::EvalExpr(void *iptr)
398{
399 switch (fType)
400 {
401 case FT_Double:
402 {
403 TString ostr;
404 ostr.Form("%.*f", fPrecision, fDoubleFoo(iptr));
405 return ostr.Data();
406 }
407 case FT_Bool:
408 {
409 return fBoolFoo(iptr) ? fTrue : fFalse;
410 }
411 case FT_String:
412 {
413 return fStringFoo(iptr);
414 }
415 }
416 return "XYZ";
417}
ROOT::R::TRInterface & r
Definition: Object.C:4
#define b(i)
Definition: RSha256.hxx:100
#define f(i)
Definition: RSha256.hxx:104
#define c(i)
Definition: RSha256.hxx:101
#define g(i)
Definition: RSha256.hxx:105
const Ssiz_t kNPOS
Definition: RtypesCore.h:113
unsigned char UChar_t
Definition: RtypesCore.h:36
short Color_t
Definition: RtypesCore.h:81
R__EXTERN Int_t gDebug
Definition: RtypesCore.h:117
@ kMagenta
Definition: Rtypes.h:64
@ kBlue
Definition: Rtypes.h:64
int type
Definition: TGX11.cxx:120
#define gROOT
Definition: TROOT.h:406
char * Form(const char *fmt,...)
void SetFilterExpr(const TString &filter)
std::function< void(REveDataCollection *)> _handler_func
REveDataItem * GetDataItem(Int_t i) const
std::function< bool(void *)> fFilterFoo
virtual void ItemChanged(REveDataItem *item)
void AddItem(void *data_ptr, const std::string &n, const std::string &t)
Bool_t SetRnrState(Bool_t) override
Set render state of this element and of its children to the same value.
void SetMainColor(Color_t) override
Set main color of the element.
Int_t WriteCoreJson(nlohmann::json &cj, Int_t rnr_offset) override
Write core json.
std::function< void(REveDataCollection *, const Ids_t &)> _handler_func_ids
REveDataCollection(const std::string &n="REveDataCollection", const std::string &t="")
std::function< double(void *)> fDoubleFoo
std::function< bool(void *)> fBoolFoo
REveDataColumn(const std::string &n="REveDataColumn", const std::string &t="")
std::function< std::string(void *)> fStringFoo
void SetExpressionAndType(const std::string &expr, FieldType_e type)
virtual void FillImpliedSelectedSet(Set_t &impSelSet) override
Populate set impSelSet with derived / dependant elements.
Bool_t SetRnrSelf(Bool_t) override
Set render state of this element, i.e.
virtual void SetItemColorRGB(UChar_t r, UChar_t g, UChar_t b)
Int_t WriteCoreJson(nlohmann::json &cj, Int_t rnr_offset) override
Write core json.
REveDataItem(const std::string &n="REveDataItem", const std::string &t="")
virtual Int_t WriteCoreJson(nlohmann::json &cj, Int_t rnr_offset)
Write core json.
const REveDataCollection * GetCollection() const
REveDataTable(const std::string &n="REveDataTable", const std::string &t="")
const REveDataCollection * fCollection
void AddNewColumn(const std::string &expr, const std::string &title, int prec=2)
virtual Int_t WriteCoreJson(nlohmann::json &cj, Int_t rnr_offset)
Write core json.
void SetupDefaultColorAndTransparency(Color_t col, Bool_t can_edit_color, Bool_t can_edit_transparency)
Set up element to use built-in main color and set flags allowing editing of main color and transparen...
const char * GetCName() const
virtual void AddElement(REveElement *el)
Add el to the list of children.
virtual Bool_t SetRnrSelf(Bool_t rnr)
Set render state of this element, i.e.
virtual Bool_t SetRnrState(Bool_t rnr)
Set render state of this element and of its children to the same value.
std::set< REveElement * > Set_t
ElementId_t GetElementId() const
virtual void SetMainColor(Color_t color)
Set main color of the element.
virtual Color_t GetMainColor() const
REveException Exception-type thrown by Eve classes.
Definition: REveTypes.hxx:40
Each class (see TClass) has a linked list of its base class(es).
Definition: TBaseClass.h:33
TClass * GetClassPointer(Bool_t load=kTRUE)
Get pointer to the base class TClass.
Definition: TBaseClass.cxx:63
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition: TClass.h:80
static Int_t GetColor(const char *hexcolor)
Static method returning color number for color specified by hex color string of form: "#rrggbb",...
Definition: TColor.cxx:1769
const char * GetReturnTypeName() const
Get full type description of function return type, e,g.: "class TDirectory*".
Definition: TFunction.cxx:140
Each ROOT method (see TMethod) has a linked list of its arguments.
Definition: TMethodArg.h:31
const char * GetTypeName() const
Get type of method argument, e.g.
Definition: TMethodArg.cxx:67
Each ROOT class (see TClass) has a linked list of methods.
Definition: TMethod.h:38
virtual TList * GetListOfMethodArgs()
Returns methodarg list and additionally updates fDataMember in TMethod by calling FindDataMember();.
Definition: TMethod.cxx:306
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
Basic string class.
Definition: TString.h:131
TString & Replace(Ssiz_t pos, Ssiz_t n, const char *s)
Definition: TString.h:677
const char * Data() const
Definition: TString.h:364
Ssiz_t Last(char c) const
Find last occurrence of a character c.
Definition: TString.cxx:892
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition: TString.cxx:2289
const Int_t n
Definition: legend1.C:16
static constexpr double s
static constexpr double ms
basic_json<> json
default JSON class
Definition: REveElement.hxx:88
auto * m
Definition: textangle.C:8