Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
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)
284 throw eh + "item class has to be set before the filter expression.";
285
286 if (filter) {
287 // printf("filter '%s'\n", filter);
288 int ibeg = 0, iend = strlen(filter);
289 while (ibeg < iend && isspace(filter[ibeg])) ++ibeg;
290 while (iend > ibeg && isspace(filter[iend-1])) --iend;
291 // printf("cleaned up beg=%d end=%d len =%d\n", ibeg, iend, (int)strlen(filter));
292 fFilterExpr = TString(filter + ibeg, iend - ibeg);
293 } else {
294 fFilterExpr = "";
295 }
296
297 if (fFilterExpr.Length())
298 {
299 std::stringstream s;
300 s << "*((std::function<bool(" << fItemClass->GetName() << "*)>*)" << std::hex << std::showbase
301 << (size_t)&fFilterFoo << ") = [](" << fItemClass->GetName() << "* p){" << fItemClass->GetName()
302 << " &i=*p; return (" << fFilterExpr.Data() << "); };";
303
304 // printf("%s\n", s.Data());
305 try {
306 gROOT->ProcessLine(s.str().c_str());
307 // AMT I don't know why ApplyFilter call is separated
308 ApplyFilter();
309 }
310 catch (const std::exception &exc)
311 {
312 R__LOG_ERROR(REveLog()) << "EveDataCollection::SetFilterExpr" << exc.what();
313 }
314 }
315 else
316 {
317 // Remove filter
318 fFilterFoo = nullptr;
319 Ids_t ids;
320 int idx = 0;
321 for (auto &ii : fItemList->fItems) {
322 if (ii->GetFiltered()) {
323 ii->SetFiltered(false);
324 ids.push_back(idx);
325 }
326 idx++;
327 }
328
332 }
333}
334
336{
337 if (!fFilterFoo)
338 return;
339
340 Ids_t ids;
341 int idx = 0;
342 for (auto &ii : fItemList->fItems)
343 {
344 bool res = fFilterFoo(ii->GetDataPtr());
345
346 // printf("Item:%s -- filter result = %d\n", ii.fItemPtr->GetElementName(), res);
347
348 ii->SetFiltered( ! res );
349
350 ids.push_back(idx++);
351 }
355}
356
357//______________________________________________________________________________
358
360{
361 struct PubMethods
362 {
363 void FillJSON(TClass* c, nlohmann::json & arr)
364 {
365 TString ctor = c->GetName(), dtor = "~";
366 {
367 int i = ctor.Last(':');
368 if (i != kNPOS)
369 {
370 ctor.Replace(0, i + 1, "");
371 }
372 dtor += ctor;
373 }
374
375 TMethod *meth;
376 TIter next(c->GetListOfMethods());
377 while ((meth = (TMethod*) next()))
378 {
379 // Filter out ctor, dtor, some ROOT stuff.
380 {
381 TString m(meth->GetName());
382 if (m == ctor || m == dtor ||
383 m == "Class" || m == "Class_Name" || m == "Class_Version" || m == "Dictionary" || m == "IsA" ||
384 m == "DeclFileName" || m == "ImplFileName" || m == "DeclFileLine" || m == "ImplFileLine" ||
385 m == "Streamer" || m == "StreamerNVirtual" || m == "ShowMembers" ||
386 m == "CheckTObjectHashConsistency")
387 {
388 continue;
389 }
390 }
391
392 TString ms;
393 TMethodArg *ma;
394 TIter next_ma(meth->GetListOfMethodArgs());
395 while ((ma = (TMethodArg*) next_ma()))
396 {
397 if ( ! ms.IsNull()) ms += ", ";
398
399 ms += ma->GetTypeName();
400 ms += " ";
401 ms += ma->GetName();
402 }
403 std::string entry(TString::Format("i.%s(%s)",meth->GetName(),ms.Data()).Data());
404 nlohmann::json jm ;
405 jm["f"] = entry;
406 jm["r"] = meth->GetReturnTypeName();
407 jm["c"] = c->GetName();
408 arr.push_back(jm);
409 }
410 {
411 TBaseClass *base;
412 TIter blnext(c->GetListOfBases());
413 while ((base = (TBaseClass*) blnext()))
414 {
415 FillJSON(base->GetClassPointer(), arr);
416 }
417 }
418 }
419 };
420 j["fPublicFunctions"] = nlohmann::json::array();
421 PubMethods pm;
422 pm.FillJSON(fItemClass, j["fPublicFunctions"]);
423}
424
425//______________________________________________________________________________
426
428{
429 int idx = 0;
430 Ids_t ids;
431 for (auto & chld : fItemList->fItems)
432 {
433 chld->SetMainColor(newv);
434 ids.push_back(idx);
435 idx++;
436 }
437
439 for (auto & chld : fItemList->fItems)
440 {
441 chld->SetMainColor(newv);
442 }
444 fItemList->SetMainColor(newv);
446}
447
448//______________________________________________________________________________
449
451{
452 Bool_t ret = REveElement::SetRnrState(iRnrSelf);
453 Ids_t ids;
454 for (int i = 0; i < GetNItems(); ++i ) {
455 ids.push_back(i);
456 fItemList->fItems[i]->SetRnrSelf(fRnrSelf);
457 }
458
461
462 return ret;
463}
464
465
466//______________________________________________________________________________
467
469{
470 Int_t ret = REveElement::WriteCoreJson(j, rnr_offset);
471 j["fFilterExpr"] = fFilterExpr.Data();
472 return ret;
473}
474
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
unsigned char UChar_t
Definition RtypesCore.h:38
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
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="")
REveDataItemList(const std::string &n="Items", 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
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
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.
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*".
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...
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
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:916
Bool_t IsNull() const
Definition TString.h:407
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
RLogChannel & REveLog()
Log channel for Eve diagnostics.
Definition REveTypes.cxx:47
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
auto * m
Definition textangle.C:8
auto * tt
Definition textangle.C:16