Logo ROOT  
Reference Guide
REveDataCollection.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>
15#include <ROOT/REveManager.hxx>
16
17#include "TROOT.h"
18#include "TMethod.h"
19#include "TMethodArg.h"
20#include "TColor.h"
21#include "TClass.h"
22#include "TList.h"
23#include "TBaseClass.h"
24
25#include <sstream>
26#include <regex>
27
28#include <nlohmann/json.hpp>
29
30using namespace ROOT::Experimental;
31
32
33Color_t REveDataCollection::fgDefaultColor = kBlue;
34
35//==============================================================================
36//==============================================================================
37
38REveDataItemList::REveDataItemList(const std::string& n, const std::string& t):
39 REveElement(n,t)
40{
41 fAlwaysSecSelect = true;
42
45 });
46
48 REveDataItemList::DummyFillImpliedSelected(collection, impSelSet);
49 });
50
52}
53//______________________________________________________________________________
54
56{
57 fItems[idx]->SetRnrSelf(visible);
58 ItemChanged(idx);
60}
61
62//______________________________________________________________________________
63
65{
67 fItems[idx]->SetMainColor(c);
68 ItemChanged(idx);
70}
71//______________________________________________________________________________
72
74{
75 int idx = 0;
76 std::vector<int> ids;
77 for (auto & chld : fItems)
78 {
79 if (chld == iItem) {
80 ids.push_back(idx);
81 fHandlerItemsChange( this , ids);
82 return;
83 }
84 idx++;
85 }
86}
87
88//______________________________________________________________________________
89
91{
92 std::vector<int> ids;
93 ids.push_back(idx);
94 fHandlerItemsChange( this , ids);
95}
96
97//______________________________________________________________________________
98
100{
101 /*
102 printf("REveDataItemList::FillImpliedSelectedSet colecction setsize %zu\n", RefSelectedSet().size());
103 for (auto x : RefSelectedSet())
104 printf("%d \n", x);
105 */
106 fHandlerFillImplied( this , impSelSet);
107}
108
109//______________________________________________________________________________
110
111
113{
114 Int_t ret = REveElement::WriteCoreJson(j, rnr_offset);
115 j["items"] = nlohmann::json::array();
116 for (auto & chld : fItems)
117 {
119 i["fFiltered"] = chld->GetFiltered();
120 i["fRnrSelf"] = chld->GetRnrSelf();
121 i["fColor"] = chld->GetMainColor();
122 j["items"].push_back(i);
123 }
124
125 return ret;
126}
127
128//______________________________________________________________________________
129
131{
132 Bool_t ret = REveElement::SetRnrState(iRnrSelf);
133 std::vector<int> ids;
134
135 for (size_t i = 0; i < fItems.size(); ++i ) {
136 ids.push_back(i);
137 fItems[i]->SetRnrSelf(fRnrSelf);
138 }
139
140 fHandlerItemsChange( this , ids);
143
144 return ret;
145}
146
147
148//______________________________________________________________________________
149void REveDataItemList::ProcessSelectionStr(ElementId_t id, bool multi, bool secondary, const char* secondary_idcs)
150{
151 static const REveException eh("REveDataItemList::ProcessSelectionStr ");
152 static const std::regex comma_re("\\s*,\\s*", std::regex::optimize);
153 std::string str(secondary_idcs);
154 std::set<int> sis;
155 std::sregex_token_iterator itr(str.begin(), str.end(), comma_re, -1);
156 std::sregex_token_iterator end;
157
158 try {
159 while (itr != end) sis.insert(std::stoi(*itr++));
160 }
161 catch (const std::invalid_argument& ia) {
162 throw eh + "invalid secondary index argument '" + *itr + "' - must be int.";
163 }
164
165 ProcessSelection(id, multi, secondary, sis);
166}
167
168
169//______________________________________________________________________________
170void REveDataItemList::ProcessSelection(ElementId_t selectionId, bool multi, bool secondary, const std::set<int>& secondary_idcs)
171{
172 RefSelectedSet() = secondary_idcs;
173 REveSelection* selection = (REveSelection*) ROOT::Experimental::gEve->FindElementById(selectionId);
174 selection->NewElementPicked(GetElementId(), multi, secondary, secondary_idcs);
175}
176
177//______________________________________________________________________________
178std::string REveDataItemList::GetHighlightTooltip(const std::set<int>& secondary_idcs) const
179{
180 if (secondary_idcs.empty()) {
181 return GetName();
182 }
183
184 // print info for first selected index
185 int idx = *secondary_idcs.begin();
186 auto col = dynamic_cast<REveDataCollection*>(fMother);
187 void* data = col->GetDataPtr(idx);
188 std::string name = col->GetName();
189 auto li = name.size();
190 if (li && name[li-1] == 's')
191 {
192 name = name.substr(0, li-1);
193 }
194
195 std::string res;
196 for (auto &z : secondary_idcs)
197 {
198 idx = z;
199 data = col->GetDataPtr(idx);
200 res += std::string(TString::Format("%s %d", name.c_str(), idx));
201 for (auto &t : fTooltipExpressions) {
202 std::string eval = t->fTooltipFunction.EvalExpr(data);
203 res += std::string(TString::Format("\n %s = %s", t->fTooltipTitle.c_str(), eval.c_str()));
204 }
205 res += "\n";
206 }
207 return res;
208}
209
210//______________________________________________________________________________
211void REveDataItemList::AddTooltipExpression(const std::string &title, const std::string &expr, bool init)
212{
213 fTooltipExpressions.push_back(std::unique_ptr<TTip>(new TTip()));
214 TTip *tt = fTooltipExpressions.back().get();
215
216 tt->fTooltipTitle = title;
217 tt->fTooltipFunction.SetPrecision(2);
218 auto col = dynamic_cast<REveDataCollection *>(fMother);
219 auto icls = col->GetItemClass();
220 tt->fTooltipFunction.SetExpressionAndType(expr, REveDataColumn::FT_Double, icls);
221
222 if (init) {
223 auto re = tt->fTooltipFunction.GetFunctionExpressionString();
224 gROOT->ProcessLine(re.c_str());
225 }
226}
227
228//______________________________________________________________________________
230{
231 fHandlerItemsChange = handler_func;
232}
233
234//______________________________________________________________________________
236{
237 fHandlerFillImplied = handler_func;
238}
239
240//______________________________________________________________________________
242{
243 if (gDebug) {
244 printf("REveDataItemList::DummyItemsCahngeDelegate not implemented\n");
245 }
246}
247
248
249//______________________________________________________________________________
251{
252 if (gDebug) {
253 printf("REveDataItemList::DummyFillImpliedSelectedDelegate not implemented\n");
254 }
255}
256
257//==============================================================================
258// REveDataCollection
259//==============================================================================
260
261REveDataCollection::REveDataCollection(const std::string& n, const std::string& t) :
262 REveElement(n, t)
263{
264 std::string lname = n + "Items";
265 fItemList = new REveDataItemList(lname.c_str());
267
269}
270
271void REveDataCollection::AddItem(void *data_ptr, const std::string& /*n*/, const std::string& /*t*/)
272{
273 auto el = new REveDataItem(data_ptr, GetMainColor());
274 fItemList->fItems.emplace_back(el);
275}
276
277//------------------------------------------------------------------------------
278
279void REveDataCollection::SetFilterExpr(const char* filter)
280{
281 static const REveException eh("REveDataCollection::SetFilterExpr ");
282
283 if (!fItemClass) throw eh + "item class has to be set before the filter expression.";
284
285 fFilterExpr = filter;
286
287 std::stringstream s;
288 s << "*((std::function<bool(" << fItemClass->GetName() << "*)>*)" << std::hex << std::showbase << (size_t)&fFilterFoo
289 << ") = [](" << fItemClass->GetName() << "* p){" << fItemClass->GetName() << " &i=*p; return ("
290 << fFilterExpr.Data() << "); };";
291
292 // printf("%s\n", s.Data());
293 try {
294 gROOT->ProcessLine(s.str().c_str());
295 // AMT I don't know why ApplyFilter call is separated
296 ApplyFilter();
297 }
298 catch (const std::exception &exc)
299 {
300 R__LOG_ERROR(REveLog()) << "EveDataCollection::SetFilterExpr" << exc.what();
301 }
302}
303
305{
306 if (!fFilterFoo)
307 return;
308
309 Ids_t ids;
310 int idx = 0;
311 for (auto &ii : fItemList->fItems)
312 {
313 bool res = fFilterFoo(ii->GetDataPtr());
314
315 // printf("Item:%s -- filter result = %d\n", ii.fItemPtr->GetElementName(), res);
316
317 ii->SetFiltered( ! res );
318
319 ids.push_back(idx++);
320 }
324}
325
326//______________________________________________________________________________
327
329{
330 struct PubMethods
331 {
332 void FillJSON(TClass* c, nlohmann::json & arr)
333 {
334 TString ctor = c->GetName(), dtor = "~";
335 {
336 int i = ctor.Last(':');
337 if (i != kNPOS)
338 {
339 ctor.Replace(0, i + 1, "");
340 }
341 dtor += ctor;
342 }
343
344 TMethod *meth;
345 TIter next(c->GetListOfMethods());
346 while ((meth = (TMethod*) next()))
347 {
348 // Filter out ctor, dtor, some ROOT stuff.
349 {
350 TString m(meth->GetName());
351 if (m == ctor || m == dtor ||
352 m == "Class" || m == "Class_Name" || m == "Class_Version" || m == "Dictionary" || m == "IsA" ||
353 m == "DeclFileName" || m == "ImplFileName" || m == "DeclFileLine" || m == "ImplFileLine" ||
354 m == "Streamer" || m == "StreamerNVirtual" || m == "ShowMembers" ||
355 m == "CheckTObjectHashConsistency")
356 {
357 continue;
358 }
359 }
360
361 TString ms;
362 TMethodArg *ma;
363 TIter next_ma(meth->GetListOfMethodArgs());
364 while ((ma = (TMethodArg*) next_ma()))
365 {
366 if ( ! ms.IsNull()) ms += ", ";
367
368 ms += ma->GetTypeName();
369 ms += " ";
370 ms += ma->GetName();
371 }
372 std::string entry(TString::Format("i.%s(%s)",meth->GetName(),ms.Data()).Data());
373 nlohmann::json jm ;
374 jm["f"] = entry;
375 jm["r"] = meth->GetReturnTypeName();
376 jm["c"] = c->GetName();
377 arr.push_back(jm);
378 }
379 {
380 TBaseClass *base;
381 TIter blnext(c->GetListOfBases());
382 while ((base = (TBaseClass*) blnext()))
383 {
384 FillJSON(base->GetClassPointer(), arr);
385 }
386 }
387 }
388 };
389 j["fPublicFunctions"] = nlohmann::json::array();
390 PubMethods pm;
391 pm.FillJSON(fItemClass, j["fPublicFunctions"]);
392}
393
394//______________________________________________________________________________
395
397{
398 int idx = 0;
399 Ids_t ids;
400 for (auto & chld : fItemList->fItems)
401 {
402 chld->SetMainColor(newv);
403 ids.push_back(idx);
404 idx++;
405 }
406
408 for (auto & chld : fItemList->fItems)
409 {
410 chld->SetMainColor(newv);
411 }
413 fItemList->SetMainColor(newv);
415}
416
417//______________________________________________________________________________
418
420{
421 Bool_t ret = REveElement::SetRnrState(iRnrSelf);
422 Ids_t ids;
423 for (int i = 0; i < GetNItems(); ++i ) {
424 ids.push_back(i);
425 fItemList->fItems[i]->SetRnrSelf(fRnrSelf);
426 }
427
430
431 return ret;
432}
433
434
435//______________________________________________________________________________
436
438{
439 Int_t ret = REveElement::WriteCoreJson(j, rnr_offset);
440 j["fFilterExpr"] = fFilterExpr.Data();
441 return ret;
442}
443
ROOT::R::TRInterface & r
Definition: Object.C:4
#define R__LOG_ERROR(...)
Definition: RLogger.hxx:362
#define b(i)
Definition: RSha256.hxx:100
#define c(i)
Definition: RSha256.hxx:101
#define g(i)
Definition: RSha256.hxx:105
const Ssiz_t kNPOS
Definition: RtypesCore.h:124
int Int_t
Definition: RtypesCore.h:45
unsigned char UChar_t
Definition: RtypesCore.h:38
bool Bool_t
Definition: RtypesCore.h:63
short Color_t
Definition: RtypesCore.h:92
@ kBlue
Definition: Rtypes.h:66
char name[80]
Definition: TGX11.cxx:110
Int_t gDebug
Definition: TROOT.cxx:592
#define gROOT
Definition: TROOT.h:404
std::function< bool(void *)> fFilterFoo
void StreamPublicMethods(nlohmann::json &cj) const
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.
REveDataCollection(const std::string &n="REveDataCollection", const std::string &t="")
virtual void ItemChanged(REveDataItem *item)
Int_t WriteCoreJson(nlohmann::json &cj, Int_t rnr_offset) override
Write core json.
void AddTooltipExpression(const std::string &title, const std::string &expr, bool init=true)
FillImpliedSelectedFunc_t fHandlerFillImplied
void ProcessSelection(ElementId_t id, bool multi, bool secondary, const std::set< int > &in_secondary_idcs)
void FillImpliedSelectedSet(Set_t &impSelSet) override
Populate set impSelSet with derived / dependant elements.
void ProcessSelectionStr(ElementId_t id, bool multi, bool secondary, const char *in_secondary_idcs)
void SetItemColorRGB(Int_t idx, UChar_t r, UChar_t g, UChar_t b)
std::vector< std::unique_ptr< TTip > > fTooltipExpressions
Bool_t SetRnrState(Bool_t) override
Set render state of this element and of its children to the same value.
std::string GetHighlightTooltip(const std::set< int > &secondary_idcs) const override
void SetItemsChangeDelegate(ItemsChangeFunc_t)
static void DummyFillImpliedSelected(REveDataItemList *, Set_t &impSelSet)
void SetFillImpliedSelectedDelegate(FillImpliedSelectedFunc_t)
std::vector< REveDataItem * > fItems
static void DummyItemsChange(REveDataItemList *, const std::vector< int > &)
std::function< void(REveDataItemList *, Set_t &)> FillImpliedSelectedFunc_t
void SetItemVisible(Int_t idx, Bool_t visible)
std::function< void(REveDataItemList *, const std::vector< int > &)> ItemsChangeFunc_t
const std::string & GetName() const
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...
virtual void AddElement(REveElement *el)
Add el to the list of children.
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
Definition: REveElement.hxx:64
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:41
REveSelection Container for selected and highlighted elements.
void NewElementPicked(ElementId_t id, bool multi, bool secondary, const std::set< int > &secondary_idcs={})
Called from GUI when user picks or un-picks an element.
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:1822
const char * GetReturnTypeName() const
Get full type description of function return type, e,g.: "class TDirectory*".
Definition: TFunction.cxx:141
Each ROOT method (see TMethod) has a linked list of its arguments.
Definition: TMethodArg.h:36
const char * GetTypeName() const
Get type of method argument, e.g.
Definition: TMethodArg.cxx:66
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:307
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
Basic string class.
Definition: TString.h:136
TString & Replace(Ssiz_t pos, Ssiz_t n, const char *s)
Definition: TString.h:682
const char * Data() const
Definition: TString.h:369
Ssiz_t Last(char c) const
Find last occurrence of a character c.
Definition: TString.cxx:916
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition: TString.cxx:2336
const Int_t n
Definition: legend1.C:16
R__EXTERN REveManager * gEve
unsigned int ElementId_t
Definition: REveTypes.hxx:25
RLogChannel & REveLog()
Log channel for Eve diagnostics.
Definition: REveTypes.cxx:47
static constexpr double s
static constexpr double ms
basic_json< std::map, std::vector, std::string, bool, std::int64_t, std::uint64_t, double, std::allocator, adl_serializer, std::vector< std::uint8_t > > json
Definition: REveElement.hxx:36
auto * m
Definition: textangle.C:8
auto * tt
Definition: textangle.C:16