Logo ROOT   6.21/01
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 
12 #include <ROOT/REveDataClasses.hxx>
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 
26 using namespace ROOT::Experimental;
27 namespace REX = ROOT::Experimental;
28 
29 
31 
32 //==============================================================================
33 // REveDataCollection
34 //==============================================================================
35 
36 REveDataCollection::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 
47 void 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
74  ApplyFilter();
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  }
97  StampObjProps();
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  Color_t oldv = GetMainColor();
179  Color_t newv = TColor::GetColor(r, g, b);
180  int idx = 0;
181  Ids_t ids;
182  for (auto & chld : fChildren)
183  {
184  // if (chld->GetMainColor() == oldv) {
185  chld->SetMainColor(newv);
186  printf(" REveDataCollection::SetCollectionColorRGB going to change color for idx %d --------------------\n", idx);
187  ids.push_back(idx);
188  // }
189 
190  idx++;
191  }
192 
194  printf("REveDataCollection::SetCollectionColorRGB color ched to %d ->%d\n", oldv, GetMainColor());
195  _handler_func_ids( this , ids);
196 }
197 
198 //______________________________________________________________________________
199 
201 {
202  SetRnrSelf(iRnrSelf);
203 
204  Ids_t ids;
205 
206  for (int i = 0; i < GetNItems(); ++i ) {
207  ids.push_back(i);
209  }
210 
211  _handler_func_ids( this , ids);
212 }
213 
214 
215 
216 //______________________________________________________________________________
217 
219 {
220  int idx = 0;
221  Ids_t ids;
222  for (auto & chld : fChildren)
223  {
224  if (chld == iItem) {
225  ids.push_back(idx);
226  _handler_func_ids( this , ids);
227  return;
228  }
229  idx++;
230  }
231 }
232 
233 //==============================================================================
234 // REveDataItem
235 //==============================================================================
236 
237 REveDataItem::REveDataItem(const std::string& n, const std::string& t) :
238  REveElement(n, t)
239 {
241 }
242 
244 {
245  Int_t ret = REveElement::WriteCoreJson(j, rnr_offset);
246  j["fFiltered"] = fFiltered;
247  return ret;
248 }
249 
251 {
252  Color_t color = TColor::GetColor(r, g, b);
254  REveDataCollection* c = dynamic_cast<REveDataCollection*>(fMother);
255  c->ItemChanged(this);
256 }
257 
258 void REveDataItem::SetItemRnrSelf(bool iRnrSelf)
259 {
260  REveElement::SetRnrSelf(iRnrSelf);
261  REveDataCollection* c = dynamic_cast<REveDataCollection*>(fMother);
262  c->ItemChanged(this);
263 }
264 
266 {
267  if (f != fFiltered)
268  {
269  fFiltered = f;
270  StampObjProps();
271  }
272 }
273 
275 {
276  for (auto &n : fNieces)
277  {
278  impSelSet.insert(n);
279  n->FillImpliedSelectedSet(impSelSet);
280 
281  if (gDebug > 1)
282  {
283  printf("REveDataItem::FillImpliedSelectedSet added niece '%s' [%s]\n",
284  n->GetCName(), n->IsA()->GetName());
285  }
286  }
287 }
288 
289 //==============================================================================
290 // REveDataTable
291 //==============================================================================
292 
293 REveDataTable::REveDataTable(const std::string& n, const std::string& t) :
294  REveElement(n, t)
295 {
296  fChildClass = TClass::GetClass<REveDataColumn>();
297 }
298 
300 {
301  Int_t Nit = fCollection->GetNItems();
302 
303  for (Int_t i = 0; i< Nit; ++i)
304  {
305  void *data = fCollection->GetDataPtr(i);
307 
308  printf("| %-20s |", item->GetCName());
309 
310  for (auto & chld : fChildren)
311  {
312  auto clmn = dynamic_cast<REveDataColumn*>(chld);
313 
314  printf(" %10s |", clmn->EvalExpr(data).c_str());
315  }
316  printf("\n");
317  }
318 }
319 
321 {
322  int ret = REveElement::WriteCoreJson(j, rnr_offset);
323  Int_t Nit = fCollection->GetNItems();
324 
325  nlohmann::json jarr = nlohmann::json::array();
326 
327  for (Int_t i = 0; i < Nit; ++i) {
328  void *data = fCollection->GetDataPtr(i);
329  nlohmann::json row;
330  for (auto &chld : fChildren) {
331  auto clmn = dynamic_cast<REveDataColumn *>(chld);
332  row[chld->GetCName()] = clmn->EvalExpr(data);
333  }
334  jarr.push_back(row);
335  }
336  j["body"] = jarr;
337  j["fCollectionId"] = fCollection->GetElementId();
338  return ret;
339 }
340 
341 void REveDataTable::AddNewColumn(const std::string& expr, const std::string& title, int prec)
342 {
343  auto c = new REX::REveDataColumn(title);
344  AddElement(c);
345 
346  c->SetExpressionAndType(expr, REX::REveDataColumn::FT_Double);
347  c->SetPrecision(prec);
348 
349  StampObjProps();
350 }
351 
352 //==============================================================================
353 // REveDataColumn
354 //==============================================================================
355 
356 REveDataColumn::REveDataColumn(const std::string& n, const std::string& t) :
357  REveElement(n, t)
358 {
359 }
360 
362 {
363  auto table = dynamic_cast<REveDataTable*>(fMother);
364  auto coll = table->GetCollection();
365  auto icls = coll->GetItemClass();
366 
367  fExpression = expr;
368  fType = type;
369 
370  const char *rtyp = nullptr;
371  const void *fooptr = nullptr;
372 
373  switch (fType)
374  {
375  case FT_Double: rtyp = "double"; fooptr = &fDoubleFoo; break;
376  case FT_Bool: rtyp = "bool"; fooptr = &fBoolFoo; break;
377  case FT_String: rtyp = "std::string"; fooptr = &fStringFoo; break;
378  }
379 
380  std::stringstream s;
381  s << "*((std::function<" << rtyp << "(" << icls->GetName() << "*)>*)" << std::hex << std::showbase << (size_t)fooptr
382  << ") = [](" << icls->GetName() << "* p){" << icls->GetName() << " &i=*p; return (" << fExpression.Data()
383  << "); }";
384 
385  // printf("%s\n", s.str().c_str());
386  try {
387  gROOT->ProcessLine(s.str().c_str());
388  }
389  catch (const std::exception &exc)
390  {
391  std::cerr << "REveDataColumn::SetExpressionAndType" << exc.what();
392  }
393 }
394 
396 {
397  fPrecision = prec;
398 }
399 
400 std::string REveDataColumn::EvalExpr(void *iptr)
401 {
402  switch (fType)
403  {
404  case FT_Double:
405  {
406  TString ostr;
407  ostr.Form("%.*f", fPrecision, fDoubleFoo(iptr));
408  return ostr.Data();
409  }
410  case FT_Bool:
411  {
412  return fBoolFoo(iptr) ? fTrue : fFalse;
413  }
414  case FT_String:
415  {
416  return fStringFoo(iptr);
417  }
418  }
419  return "XYZ";
420 }
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
REveDataTable(const std::string &n="REveDataTable", const std::string &t="")
REveDataCollection(const std::string &n="REveDataCollection", const std::string &t="")
auto * m
Definition: textangle.C:8
virtual void FillImpliedSelectedSet(Set_t &impSelSet) override
Populate set impSelSet with derived / dependant elements.
const char * GetReturnTypeName() const
Get full type description of function return type, e,g.: "class TDirectory*".
Definition: TFunction.cxx:140
std::function< double(void *)> fDoubleFoo
#define g(i)
Definition: RSha256.hxx:105
const Ssiz_t kNPOS
Definition: RtypesCore.h:111
TClass * GetClassPointer(Bool_t load=kTRUE)
Get pointer to the base class TClass.
Definition: TBaseClass.cxx:63
const REveDataCollection * GetCollection() const
#define gROOT
Definition: TROOT.h:415
REveDataItem(const std::string &n="REveDataItem", const std::string &t="")
Basic string class.
Definition: TString.h:131
#define f(i)
Definition: RSha256.hxx:104
int Int_t
Definition: RtypesCore.h:41
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...
Each ROOT method (see TMethod) has a linked list of its arguments.
Definition: TMethodArg.h:31
const char * GetCName() const
const char * GetTypeName() const
Get type of method argument, e.g.
Definition: TMethodArg.cxx:67
std::function< void(REveDataCollection *, const Ids_t &)> _handler_func_ids
const REveDataCollection * fCollection
virtual void ItemChanged(REveDataItem *item)
TString & Replace(Ssiz_t pos, Ssiz_t n, const char *s)
Definition: TString.h:677
void AddNewColumn(const std::string &expr, const std::string &title, int prec=2)
ElementId_t GetElementId() const
Int_t WriteCoreJson(nlohmann::json &cj, Int_t rnr_offset) override
Write core json.
static constexpr double s
virtual void AddElement(REveElement *el)
Add el to the list of children.
short Color_t
Definition: RtypesCore.h:79
REveDataItem * GetDataItem(Int_t i) const
virtual TList * GetListOfMethodArgs()
Returns methodarg list and additionally updates fDataMember in TMethod by calling FindDataMember();...
Definition: TMethod.cxx:306
REveDataColumn(const std::string &n="REveDataColumn", const std::string &t="")
ROOT::R::TRInterface & r
Definition: Object.C:4
virtual void SetMainColor(Color_t color)
Set main color of the element.
static constexpr double ms
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition: TString.cxx:2289
char * Form(const char *fmt,...)
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:1764
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition: TClass.h:75
std::function< std::string(void *)> fStringFoo
virtual Int_t WriteCoreJson(nlohmann::json &cj, Int_t rnr_offset)
Write core json.
Each class (see TClass) has a linked list of its base class(es).
Definition: TBaseClass.h:33
virtual void SetCollectionColorRGB(UChar_t r, UChar_t g, UChar_t b)
std::function< bool(void *)> fFilterFoo
Ssiz_t Last(char c) const
Find last occurrence of a character c.
Definition: TString.cxx:892
std::set< REveElement * > Set_t
Definition: REveElement.hxx:79
virtual Int_t WriteCoreJson(nlohmann::json &cj, Int_t rnr_offset)
Write core json.
void AddItem(void *data_ptr, const std::string &n, const std::string &t)
int type
Definition: TGX11.cxx:120
virtual Bool_t SetRnrSelf(Bool_t rnr)
Set render state of this element, i.e.
std::function< bool(void *)> fBoolFoo
Each ROOT class (see TClass) has a linked list of methods.
Definition: TMethod.h:38
void SetExpressionAndType(const std::string &expr, FieldType_e type)
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Definition: TRolke.cxx:630
R__EXTERN Int_t gDebug
Definition: Rtypes.h:91
#define c(i)
Definition: RSha256.hxx:101
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.
Definition: Rtypes.h:64
unsigned char UChar_t
Definition: RtypesCore.h:34
std::function< void(REveDataCollection *)> _handler_func
virtual Color_t GetMainColor() const
const Int_t n
Definition: legend1.C:16
REveException Exception-type thrown by Eve classes.
Definition: REveTypes.hxx:40
void SetFilterExpr(const TString &filter)
const char * Data() const
Definition: TString.h:364