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
21#include "json.hpp"
22#include <sstream>
23
24
25using namespace ROOT::Experimental;
26namespace REX = ROOT::Experimental;
27
28
30
31//==============================================================================
32// REveDataCollection
33//==============================================================================
34
35REveDataCollection::REveDataCollection(const std::string& n, const std::string& t) :
36 REveElement(n, t)
37{
38 fChildClass = TClass::GetClass<REveDataItem>();
39
41
42 _handler_func = 0;
44}
45
46void REveDataCollection::AddItem(void *data_ptr, const std::string& n, const std::string& t)
47{
48 auto el = new REveDataItem(n, t);
49 AddElement(el);
50 el->SetMainColor(GetMainColor());
51 fItems.emplace_back(data_ptr, el);
52}
53
54//------------------------------------------------------------------------------
55
57{
58 static const REveException eh("REveDataCollection::SetFilterExpr ");
59
60 if (!fItemClass) throw eh + "item class has to be set before the filter expression.";
61
62 fFilterExpr = filter;
63
64 std::stringstream s;
65 s << "*((std::function<bool(" << fItemClass->GetName() << "*)>*)" << std::hex << std::showbase << (size_t)&fFilterFoo
66 << ") = [](" << fItemClass->GetName() << "* p){" << fItemClass->GetName() << " &i=*p; return ("
67 << fFilterExpr.Data() << "); }";
68
69 // printf("%s\n", s.Data());
70 try {
71 gROOT->ProcessLine(s.str().c_str());
72 // AMT I don't know why ApplyFilter call is separated
74 }
75 catch (const std::exception &exc)
76 {
77 std::cerr << "EveDataCollection::SetFilterExpr" << exc.what();
78 }
79
80}
81
83{
84 Ids_t ids;
85 int idx = 0;
86 for (auto &ii : fItems)
87 {
88 bool res = fFilterFoo(ii.fDataPtr);
89
90 // printf("Item:%s -- filter result = %d\n", ii.fItemPtr->GetElementName(), res);
91
92 ii.fItemPtr->SetFiltered( ! res );
93
94 ids.push_back(idx++);
95 }
97 if ( _handler_func_ids) _handler_func_ids( this , ids);
98}
99
100//______________________________________________________________________________
101
103{
104 Int_t ret = REveElement::WriteCoreJson(j, rnr_offset);
105 j["fFilterExpr"] = fFilterExpr.Data();
106 j["publicFunction"] = nlohmann::json::array();
107
109 while (TObject *obj = x()) {
110 TMethod *method = dynamic_cast<TMethod *>(obj);
111
113 m["name"] = method->GetPrototype();
114 j["publicFunction"].push_back(m);
115 }
116
117 return ret;
118}
119
120//______________________________________________________________________________
121
123{
124 Color_t oldv = GetMainColor();
125 Color_t newv = TColor::GetColor(r, g, b);
126 int idx = 0;
127 Ids_t ids;
128 for (auto & chld : fChildren)
129 {
130 // if (chld->GetMainColor() == oldv) {
131 chld->SetMainColor(newv);
132 printf(" REveDataCollection::SetCollectionColorRGB going to change color for idx %d --------------------\n", idx);
133 ids.push_back(idx);
134 // }
135
136 idx++;
137 }
138
140 printf("REveDataCollection::SetCollectionColorRGB color ched to %d ->%d\n", oldv, GetMainColor());
141 _handler_func_ids( this , ids);
142}
143
144//______________________________________________________________________________
145
147{
148 SetRnrSelf(iRnrSelf);
149
150 Ids_t ids;
151
152 for (int i = 0; i < GetNItems(); ++i ) {
153 ids.push_back(i);
155 }
156
157 _handler_func_ids( this , ids);
158}
159
160
161
162//______________________________________________________________________________
163
165{
166 int idx = 0;
167 Ids_t ids;
168 for (auto & chld : fChildren)
169 {
170 if (chld == iItem) {
171 ids.push_back(idx);
172 _handler_func_ids( this , ids);
173 return;
174 }
175 idx++;
176 }
177}
178
179//==============================================================================
180// REveDataItem
181//==============================================================================
182
183REveDataItem::REveDataItem(const std::string& n, const std::string& t) :
184 REveElement(n, t)
185{
187}
188
190{
191 Int_t ret = REveElement::WriteCoreJson(j, rnr_offset);
192 j["fFiltered"] = fFiltered;
193 return ret;
194}
195
197{
198 Color_t color = TColor::GetColor(r, g, b);
201 c->ItemChanged(this);
202}
203
205{
206 REveElement::SetRnrSelf(iRnrSelf);
208 c->ItemChanged(this);
209}
210
212{
213 if (f != fFiltered)
214 {
215 fFiltered = f;
217 }
218}
219
221{
222 for (auto &n : fNieces)
223 {
224 impSelSet.insert(n);
225 n->FillImpliedSelectedSet(impSelSet);
226
227 if (gDebug > 1)
228 {
229 printf("REveDataItem::FillImpliedSelectedSet added niece '%s' [%s]\n",
230 n->GetCName(), n->IsA()->GetName());
231 }
232 }
233}
234
235//==============================================================================
236// REveDataTable
237//==============================================================================
238
239REveDataTable::REveDataTable(const std::string& n, const std::string& t) :
240 REveElement(n, t)
241{
242 fChildClass = TClass::GetClass<REveDataColumn>();
243}
244
246{
247 Int_t Nit = fCollection->GetNItems();
248
249 for (Int_t i = 0; i< Nit; ++i)
250 {
251 void *data = fCollection->GetDataPtr(i);
253
254 printf("| %-20s |", item->GetCName());
255
256 for (auto & chld : fChildren)
257 {
258 auto clmn = dynamic_cast<REveDataColumn*>(chld);
259
260 printf(" %10s |", clmn->EvalExpr(data).c_str());
261 }
262 printf("\n");
263 }
264}
265
267{
268 int ret = REveElement::WriteCoreJson(j, rnr_offset);
269 Int_t Nit = fCollection->GetNItems();
270
271 nlohmann::json jarr = nlohmann::json::array();
272
273 for (Int_t i = 0; i < Nit; ++i) {
274 void *data = fCollection->GetDataPtr(i);
275 nlohmann::json row;
276 for (auto &chld : fChildren) {
277 auto clmn = dynamic_cast<REveDataColumn *>(chld);
278 row[chld->GetCName()] = clmn->EvalExpr(data);
279 }
280 jarr.push_back(row);
281 }
282 j["body"] = jarr;
283 j["fCollectionId"] = fCollection->GetElementId();
284 return ret;
285}
286
287void REveDataTable::AddNewColumn(const std::string& expr, const std::string& title, int prec)
288{
289 auto c = new REX::REveDataColumn(title);
290 AddElement(c);
291
292 c->SetExpressionAndType(expr, REX::REveDataColumn::FT_Double);
293 c->SetPrecision(prec);
294
296}
297
298//==============================================================================
299// REveDataColumn
300//==============================================================================
301
302REveDataColumn::REveDataColumn(const std::string& n, const std::string& t) :
303 REveElement(n, t)
304{
305}
306
308{
309 auto table = dynamic_cast<REveDataTable*>(fMother);
310 auto coll = table->GetCollection();
311 auto icls = coll->GetItemClass();
312
313 fExpression = expr;
314 fType = type;
315
316 const char *rtyp = nullptr;
317 const void *fooptr = nullptr;
318
319 switch (fType)
320 {
321 case FT_Double: rtyp = "double"; fooptr = &fDoubleFoo; break;
322 case FT_Bool: rtyp = "bool"; fooptr = &fBoolFoo; break;
323 case FT_String: rtyp = "std::string"; fooptr = &fStringFoo; break;
324 }
325
326 std::stringstream s;
327 s << "*((std::function<" << rtyp << "(" << icls->GetName() << "*)>*)" << std::hex << std::showbase << (size_t)fooptr
328 << ") = [](" << icls->GetName() << "* p){" << icls->GetName() << " &i=*p; return (" << fExpression.Data()
329 << "); }";
330
331 // printf("%s\n", s.str().c_str());
332 try {
333 gROOT->ProcessLine(s.str().c_str());
334 }
335 catch (const std::exception &exc)
336 {
337 std::cerr << "REveDataColumn::SetExpressionAndType" << exc.what();
338 }
339}
340
342{
343 fPrecision = prec;
344}
345
346std::string REveDataColumn::EvalExpr(void *iptr)
347{
348 switch (fType)
349 {
350 case FT_Double:
351 {
352 TString ostr;
353 ostr.Form("%.*f", fPrecision, fDoubleFoo(iptr));
354 return ostr.Data();
355 }
356 case FT_Bool:
357 {
358 return fBoolFoo(iptr) ? fTrue : fFalse;
359 }
360 case FT_String:
361 {
362 return fStringFoo(iptr);
363 }
364 }
365 return "XYZ";
366}
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
int Int_t
Definition: RtypesCore.h:41
unsigned char UChar_t
Definition: RtypesCore.h:34
short Color_t
Definition: RtypesCore.h:79
@ kMagenta
Definition: Rtypes.h:64
@ kBlue
Definition: Rtypes.h:64
R__EXTERN Int_t gDebug
Definition: Rtypes.h:91
int type
Definition: TGX11.cxx:120
#define gROOT
Definition: TROOT.h:415
virtual void SetCollectionColorRGB(UChar_t r, UChar_t g, UChar_t b)
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)
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.
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.
std::set< REveElement * > Set_t
Definition: REveElement.hxx:79
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
const TList * GetListOfAllPublicMethods(Bool_t load=kTRUE)
Returns a list of all public methods of this class and its base classes.
Definition: TClass.cxx:3688
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
virtual const char * GetPrototype() const
Returns the prototype of a function as defined by CINT, or 0 in case of error.
Definition: TFunction.cxx:245
Each ROOT class (see TClass) has a linked list of methods.
Definition: TMethod.h:38
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
Mother of all ROOT objects.
Definition: TObject.h:37
Basic string class.
Definition: TString.h:131
const char * Data() const
Definition: TString.h:364
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition: TString.cxx:2289
Double_t x[n]
Definition: legend1.C:17
const Int_t n
Definition: legend1.C:16
static constexpr double s
auto * m
Definition: textangle.C:8