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
47 SetFillImpliedSelectedDelegate([&](REveDataItemList *collection, REveElement::Set_t &impSelSet, const std::set<int>& sec_idcs) {
48 REveDataItemList::DummyFillImpliedSelected(collection, impSelSet, sec_idcs);
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
99void REveDataItemList::FillImpliedSelectedSet(Set_t &impSelSet, const std::set<int> &sec_idcs)
100{
101 // printf("REveDataItemList::FillImpliedSelectedSet colecction setsize %zu\n", sec_idcs.size());
102 RefSelectedSet() = sec_idcs;
103 fHandlerFillImplied(this, impSelSet, sec_idcs);
104}
105
106//______________________________________________________________________________
107
108
110{
111 Int_t ret = REveElement::WriteCoreJson(j, rnr_offset);
112 j["items"] = nlohmann::json::array();
113 for (auto & chld : fItems)
114 {
116 i["fFiltered"] = chld->GetFiltered();
117 i["fRnrSelf"] = chld->GetRnrSelf();
118 i["fColor"] = chld->GetMainColor();
119 j["items"].push_back(i);
120 }
121
122 return ret;
123}
124
125//______________________________________________________________________________
126
128{
129 Bool_t ret = REveElement::SetRnrState(iRnrSelf);
130 std::vector<int> ids;
131
132 for (size_t i = 0; i < fItems.size(); ++i ) {
133 ids.push_back(i);
134 fItems[i]->SetRnrSelf(fRnrSelf);
135 }
136
137 fHandlerItemsChange( this , ids);
140
141 return ret;
142}
143
144
145//______________________________________________________________________________
146void REveDataItemList::ProcessSelectionStr(ElementId_t id, bool multi, bool secondary, const char* secondary_idcs)
147{
148 static const REveException eh("REveDataItemList::ProcessSelectionStr ");
149 static const std::regex comma_re("\\s*,\\s*", std::regex::optimize);
150 std::string str(secondary_idcs);
151 std::set<int> sis;
152 std::sregex_token_iterator itr(str.begin(), str.end(), comma_re, -1);
153 std::sregex_token_iterator end;
154
155 try {
156 while (itr != end) sis.insert(std::stoi(*itr++));
157 }
158 catch (const std::invalid_argument&) {
159 throw eh + "invalid secondary index argument '" + *itr + "' - must be int.";
160 }
161
162 ProcessSelection(id, multi, secondary, sis);
163}
164
165
166//______________________________________________________________________________
167void REveDataItemList::ProcessSelection(ElementId_t selectionId, bool multi, bool secondary, const std::set<int>& secondary_idcs)
168{
169 RefSelectedSet() = secondary_idcs;
170 REveSelection* selection = (REveSelection*) ROOT::Experimental::gEve->FindElementById(selectionId);
171 selection->NewElementPicked(GetElementId(), multi, secondary, secondary_idcs);
172}
173
174//______________________________________________________________________________
175std::string REveDataItemList::GetHighlightTooltip(const std::set<int>& secondary_idcs) const
176{
177 if (secondary_idcs.empty()) {
178 return GetName();
179 }
180
181 // print info for first selected index
182 int idx = *secondary_idcs.begin();
183 auto col = dynamic_cast<REveDataCollection*>(fMother);
184 void* data = col->GetDataPtr(idx);
185 std::string name = col->GetName();
186 auto li = name.size();
187 if (li && name[li-1] == 's')
188 {
189 name = name.substr(0, li-1);
190 }
191
192 std::string res;
193 for (auto &z : secondary_idcs)
194 {
195 idx = z;
196 data = col->GetDataPtr(idx);
197 res += std::string(TString::Format("%s %d", name.c_str(), idx));
198 for (auto &t : fTooltipExpressions) {
199 std::string eval = t->fTooltipFunction.EvalExpr(data);
200 res += std::string(TString::Format("\n %s = %s", t->fTooltipTitle.c_str(), eval.c_str()));
201 }
202 res += "\n";
203 }
204 return res;
205}
206
207//______________________________________________________________________________
208void REveDataItemList::AddTooltipExpression(const std::string &title, const std::string &expr, bool init)
209{
210 fTooltipExpressions.push_back(std::unique_ptr<TTip>(new TTip()));
211 TTip *tt = fTooltipExpressions.back().get();
212
213 tt->fTooltipTitle = title;
214 tt->fTooltipFunction.SetPrecision(2);
215 auto col = dynamic_cast<REveDataCollection *>(fMother);
216 auto icls = col->GetItemClass();
217 tt->fTooltipFunction.SetExpressionAndType(expr, REveDataColumn::FT_Double, icls);
218
219 if (init) {
220 auto re = tt->fTooltipFunction.GetFunctionExpressionString();
221 gROOT->ProcessLine(re.c_str());
222 }
223}
224
225//______________________________________________________________________________
227{
228 fHandlerItemsChange = handler_func;
229}
230
231//______________________________________________________________________________
233{
234 fHandlerFillImplied = handler_func;
235}
236
237//______________________________________________________________________________
239{
240 if (gDebug) {
241 printf("REveDataItemList::DummyItemsCahngeDelegate not implemented\n");
242 }
243}
244
245
246//______________________________________________________________________________
248{
249 if (gDebug) {
250 printf("REveDataItemList::DummyFillImpliedSelectedDelegate not implemented\n");
251 }
252}
253
254//==============================================================================
255// REveDataCollection
256//==============================================================================
257
258REveDataCollection::REveDataCollection(const std::string& n, const std::string& t) :
259 REveElement(n, t)
260{
261 std::string lname = n + "Items";
262 fItemList = new REveDataItemList(lname.c_str());
264
266}
267
268void REveDataCollection::AddItem(void *data_ptr, const std::string& /*n*/, const std::string& /*t*/)
269{
270 auto el = new REveDataItem(data_ptr, GetMainColor());
271 fItemList->fItems.emplace_back(el);
272}
273
274//------------------------------------------------------------------------------
275
276void REveDataCollection::SetFilterExpr(const char* filter)
277{
278 static const REveException eh("REveDataCollection::SetFilterExpr ");
279
280 if (!fItemClass)
281 throw eh + "item class has to be set before the filter expression.";
282
283 if (filter) {
284 // printf("filter '%s'\n", filter);
285 int ibeg = 0, iend = strlen(filter);
286 while (ibeg < iend && isspace(filter[ibeg])) ++ibeg;
287 while (iend > ibeg && isspace(filter[iend-1])) --iend;
288 // printf("cleaned up beg=%d end=%d len =%d\n", ibeg, iend, (int)strlen(filter));
289 fFilterExpr = TString(filter + ibeg, iend - ibeg);
290 } else {
291 fFilterExpr = "";
292 }
293
294 if (fFilterExpr.Length())
295 {
296 std::stringstream s;
297 s << "*((std::function<bool(" << fItemClass->GetName() << "*)>*)" << std::hex << std::showbase
298 << (size_t)&fFilterFoo << ") = [](" << fItemClass->GetName() << "* p){" << fItemClass->GetName()
299 << " &i=*p; return (" << fFilterExpr.Data() << "); };";
300
301 // printf("%s\n", s.Data());
302 try {
303 gROOT->ProcessLine(s.str().c_str());
304 // AMT I don't know why ApplyFilter call is separated
305 ApplyFilter();
306 }
307 catch (const std::exception &exc)
308 {
309 R__LOG_ERROR(REveLog()) << "EveDataCollection::SetFilterExpr" << exc.what();
310 }
311 }
312 else
313 {
314 // Remove filter
315 fFilterFoo = nullptr;
316 Ids_t ids;
317 int idx = 0;
318 for (auto &ii : fItemList->fItems) {
319 if (ii->GetFiltered()) {
320 ii->SetFiltered(false);
321 ids.push_back(idx);
322 }
323 idx++;
324 }
325
329 }
330}
331
333{
334 if (!fFilterFoo)
335 return;
336
337 Ids_t ids;
338 int idx = 0;
339 for (auto &ii : fItemList->fItems)
340 {
341 bool res = fFilterFoo(ii->GetDataPtr());
342
343 // printf("Item:%s -- filter result = %d\n", ii.fItemPtr->GetElementName(), res);
344
345 ii->SetFiltered( ! res );
346
347 ids.push_back(idx++);
348 }
352}
353
354//______________________________________________________________________________
355
357{
358 struct PubMethods
359 {
360 void FillJSON(TClass* c, nlohmann::json & arr)
361 {
362 TString ctor = c->GetName(), dtor = "~";
363 {
364 int i = ctor.Last(':');
365 if (i != kNPOS)
366 {
367 ctor.Replace(0, i + 1, "");
368 }
369 dtor += ctor;
370 }
371
372 TMethod *meth;
373 TIter next(c->GetListOfMethods());
374 while ((meth = (TMethod*) next()))
375 {
376 // Filter out ctor, dtor, some ROOT stuff.
377 {
378 TString m(meth->GetName());
379 if (m == ctor || m == dtor ||
380 m == "Class" || m == "Class_Name" || m == "Class_Version" || m == "Dictionary" || m == "IsA" ||
381 m == "DeclFileName" || m == "ImplFileName" || m == "DeclFileLine" || m == "ImplFileLine" ||
382 m == "Streamer" || m == "StreamerNVirtual" || m == "ShowMembers" ||
383 m == "CheckTObjectHashConsistency")
384 {
385 continue;
386 }
387 }
388
389 TString ms;
390 TMethodArg *ma;
391 TIter next_ma(meth->GetListOfMethodArgs());
392 while ((ma = (TMethodArg*) next_ma()))
393 {
394 if ( ! ms.IsNull()) ms += ", ";
395
396 ms += ma->GetTypeName();
397 ms += " ";
398 ms += ma->GetName();
399 }
400 std::string entry(TString::Format("i.%s(%s)",meth->GetName(),ms.Data()).Data());
401 nlohmann::json jm ;
402 jm["f"] = entry;
403 jm["r"] = meth->GetReturnTypeName();
404 jm["c"] = c->GetName();
405 arr.push_back(jm);
406 }
407 {
408 TBaseClass *base;
409 TIter blnext(c->GetListOfBases());
410 while ((base = (TBaseClass*) blnext()))
411 {
412 FillJSON(base->GetClassPointer(), arr);
413 }
414 }
415 }
416 };
417 j["fPublicFunctions"] = nlohmann::json::array();
418 PubMethods pm;
419 pm.FillJSON(fItemClass, j["fPublicFunctions"]);
420}
421
422//______________________________________________________________________________
423
425{
426 int idx = 0;
427 Ids_t ids;
428 for (auto & chld : fItemList->fItems)
429 {
430 chld->SetMainColor(newv);
431 ids.push_back(idx);
432 idx++;
433 }
434
436 for (auto & chld : fItemList->fItems)
437 {
438 chld->SetMainColor(newv);
439 }
441 fItemList->SetMainColor(newv);
443}
444
445//______________________________________________________________________________
446
448{
449 Bool_t ret = REveElement::SetRnrState(iRnrSelf);
450 Ids_t ids;
451 for (int i = 0; i < GetNItems(); ++i ) {
452 ids.push_back(i);
453 fItemList->fItems[i]->SetRnrSelf(fRnrSelf);
454 }
455
458
459 return ret;
460}
461
462
463//______________________________________________________________________________
464
466{
467 Int_t ret = REveElement::WriteCoreJson(j, rnr_offset);
468 j["fFilterExpr"] = fFilterExpr.Data();
469 return ret;
470}
471
#define R__LOG_ERROR(...)
Definition: RLogger.hxx:362
#define c(i)
Definition: RSha256.hxx:101
const Ssiz_t kNPOS
Definition: RtypesCore.h:124
short Color_t
Definition: RtypesCore.h:92
unsigned char UChar_t
Definition: RtypesCore.h:38
@ kBlue
Definition: Rtypes.h:66
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t b
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t g
char name[80]
Definition: TGX11.cxx:110
Int_t gDebug
Definition: TROOT.cxx:585
#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="")
std::function< void(REveDataItemList *, Set_t &, const std::set< int > &)> FillImpliedSelectedFunc_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 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)
void SetFillImpliedSelectedDelegate(FillImpliedSelectedFunc_t)
std::vector< REveDataItem * > fItems
void FillImpliedSelectedSet(Set_t &impSelSet, const std::set< int > &sec_idcs) override
Populate set impSelSet with derived / dependant elements.
static void DummyItemsChange(REveDataItemList *, const std::vector< int > &)
static void DummyFillImpliedSelected(REveDataItemList *, Set_t &impSelSet, const std::set< int > &sec_idcs)
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
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:81
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:1823
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.: "class TDirectory*" -> "TDirectory" Result needs to be used or cop...
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
const char * GetName() const override
Returns name of object.
Definition: TNamed.h:47
Basic string class.
Definition: TString.h:136
Ssiz_t Length() const
Definition: TString.h:410
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:925
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:2345
const Int_t n
Definition: legend1.C:16
R__EXTERN REveManager * gEve
RLogChannel & REveLog()
Log channel for Eve diagnostics.
Definition: REveTypes.cxx:47
void init()
Inspect hardware capabilities, and load the optimal library for RooFit computations.
static constexpr double s
static constexpr double ms
basic_json<> json
Definition: REveElement.hxx:62
TMarker m
Definition: textangle.C:8
auto * tt
Definition: textangle.C:16