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 <memory>
26#include <sstream>
27#include <regex>
28
29#include <nlohmann/json.hpp>
30
31using namespace ROOT::Experimental;
32
33
35
36//==============================================================================
37//==============================================================================
38
39REveDataItemList::REveDataItemList(const std::string& n, const std::string& t):
40 REveElement(n,t)
41{
42 fAlwaysSecSelect = true;
43
46 });
47
48 SetFillImpliedSelectedDelegate([&](REveDataItemList *collection, REveElement::Set_t &impSelSet, const std::set<int>& sec_idcs) {
49 REveDataItemList::DummyFillImpliedSelected(collection, impSelSet, sec_idcs);
50 });
51
53}
54//______________________________________________________________________________
55
57{
58 fItems[idx]->SetRnrSelf(visible);
59 ItemChanged(idx);
61}
62
63//______________________________________________________________________________
64
66{
68 fItems[idx]->SetMainColor(c);
69 ItemChanged(idx);
71}
72//______________________________________________________________________________
73
75{
76 int idx = 0;
77 std::vector<int> ids;
78 for (auto & chld : fItems)
79 {
80 if (chld == iItem) {
81 ids.push_back(idx);
82 fHandlerItemsChange( this , ids);
83 return;
84 }
85 idx++;
86 }
87}
88
89//______________________________________________________________________________
90
92{
93 std::vector<int> ids;
94 ids.push_back(idx);
95 fHandlerItemsChange( this , ids);
96}
97
98//______________________________________________________________________________
99
100void REveDataItemList::FillImpliedSelectedSet(Set_t &impSelSet, const std::set<int> &sec_idcs)
101{
102 // printf("REveDataItemList::FillImpliedSelectedSet colecction setsize %zu\n", sec_idcs.size());
103 RefSelectedSet() = sec_idcs;
104 fHandlerFillImplied(this, impSelSet, sec_idcs);
105}
106
107//______________________________________________________________________________
108
109
110Int_t REveDataItemList::WriteCoreJson(nlohmann::json &j, Int_t rnr_offset)
111{
112 Int_t ret = REveElement::WriteCoreJson(j, rnr_offset);
113 j["items"] = nlohmann::json::array();
114 for (auto & chld : fItems)
115 {
116 nlohmann::json i;
117 i["fFiltered"] = chld->GetFiltered();
118 i["fRnrSelf"] = chld->GetRnrSelf();
119 i["fColor"] = chld->GetMainColor();
120 j["items"].push_back(i);
121 }
122
123 return ret;
124}
125
126//______________________________________________________________________________
127
129{
130 Bool_t ret = REveElement::SetRnrState(iRnrSelf);
131 std::vector<int> ids;
132
133 for (size_t i = 0; i < fItems.size(); ++i ) {
134 ids.push_back(i);
135 fItems[i]->SetRnrSelf(fRnrSelf);
136 }
137
138 fHandlerItemsChange( this , ids);
141
142 return ret;
143}
144
145
146//______________________________________________________________________________
147void REveDataItemList::ProcessSelectionStr(ElementId_t id, bool multi, bool secondary, const char* secondary_idcs)
148{
149 static const REveException eh("REveDataItemList::ProcessSelectionStr ");
150 static const std::regex comma_re("\\s*,\\s*", std::regex::optimize);
151 std::string str(secondary_idcs);
152 std::set<int> sis;
153 std::sregex_token_iterator itr(str.begin(), str.end(), comma_re, -1);
154 std::sregex_token_iterator end;
155
156 try {
157 while (itr != end) sis.insert(std::stoi(*itr++));
158 }
159 catch (const std::invalid_argument&) {
160 throw eh + "invalid secondary index argument '" + *itr + "' - must be int.";
161 }
162
163 ProcessSelection(id, multi, secondary, sis);
164}
165
166
167//______________________________________________________________________________
168void REveDataItemList::ProcessSelection(ElementId_t selectionId, bool multi, bool secondary, const std::set<int>& secondary_idcs)
169{
170 RefSelectedSet() = secondary_idcs;
171 REveSelection* selection = (REveSelection*) ROOT::Experimental::gEve->FindElementById(selectionId);
172 selection->NewElementPicked(GetElementId(), multi, secondary, secondary_idcs);
173}
174
175//______________________________________________________________________________
176std::string REveDataItemList::GetHighlightTooltip(const std::set<int>& secondary_idcs) const
177{
178 if (secondary_idcs.empty()) {
179 return GetName();
180 }
181
182 // print info for first selected index
183 int idx = *secondary_idcs.begin();
184 auto col = dynamic_cast<REveDataCollection*>(fMother);
185 void* data = col->GetDataPtr(idx);
186 std::string name = col->GetName();
187 auto li = name.size();
188 if (li && name[li-1] == 's')
189 {
190 name = name.substr(0, li-1);
191 }
192
193 std::string res;
194 for (auto &z : secondary_idcs)
195 {
196 idx = z;
197 data = col->GetDataPtr(idx);
198 res += std::string(TString::Format("%s %d", name.c_str(), idx));
199 for (auto &t : fTooltipExpressions) {
200 std::string eval = t->fTooltipFunction.EvalExpr(data);
201 res += std::string(TString::Format("\n %s = %s", t->fTooltipTitle.c_str(), eval.c_str()));
202 }
203 res += "\n";
204 }
205 return res;
206}
207
208//______________________________________________________________________________
209void REveDataItemList::AddTooltipExpression(const std::string &title, const std::string &expr, bool init)
210{
211 fTooltipExpressions.push_back(std::make_unique<TTip>());
212 TTip *tt = fTooltipExpressions.back().get();
213
214 tt->fTooltipTitle = title;
215 tt->fTooltipFunction.SetPrecision(2);
216 auto col = dynamic_cast<REveDataCollection *>(fMother);
217 auto icls = col->GetItemClass();
218 tt->fTooltipFunction.SetExpressionAndType(expr, REveDataColumn::FT_Double, icls);
219
220 if (init) {
221 auto re = tt->fTooltipFunction.GetFunctionExpressionString();
222 gROOT->ProcessLine(re.c_str());
223 }
224}
225
226//______________________________________________________________________________
228{
229 fHandlerItemsChange = handler_func;
230}
231
232//______________________________________________________________________________
234{
235 fHandlerFillImplied = handler_func;
236}
237
238//______________________________________________________________________________
240{
241 if (gDebug) {
242 printf("REveDataItemList::DummyItemsCahngeDelegate not implemented\n");
243 }
244}
245
246
247//______________________________________________________________________________
249{
250 if (gDebug) {
251 printf("REveDataItemList::DummyFillImpliedSelectedDelegate not implemented\n");
252 }
253}
254
255//==============================================================================
256// REveDataCollection
257//==============================================================================
258
259REveDataCollection::REveDataCollection(const std::string& n, const std::string& t) :
260 REveElement(n, t)
261{
262 std::string lname = n + "Items";
263 fItemList = new REveDataItemList(lname);
265
267}
268
269void REveDataCollection::AddItem(void *data_ptr, const std::string& /*n*/, const std::string& /*t*/)
270{
271 auto el = new REveDataItem(data_ptr, GetMainColor());
272 fItemList->fItems.emplace_back(el);
273}
274
275//------------------------------------------------------------------------------
276
277void REveDataCollection::SetFilterExpr(const char* filter)
278{
279 static const REveException eh("REveDataCollection::SetFilterExpr ");
280
281 if (!fItemClass)
282 throw eh + "item class has to be set before the filter expression.";
283
284 if (filter) {
285 // printf("filter '%s'\n", filter);
286 int ibeg = 0, iend = strlen(filter);
287 while (ibeg < iend && isspace(filter[ibeg])) ++ibeg;
288 while (iend > ibeg && isspace(filter[iend-1])) --iend;
289 // printf("cleaned up beg=%d end=%d len =%d\n", ibeg, iend, (int)strlen(filter));
290 fFilterExpr = TString(filter + ibeg, iend - ibeg);
291 } else {
292 fFilterExpr = "";
293 }
294
295 if (fFilterExpr.Length())
296 {
297 std::stringstream s;
298 s << "*((std::function<bool(" << fItemClass->GetName() << "*)>*)" << std::hex << std::showbase
299 << (size_t)&fFilterFoo << ") = [](" << fItemClass->GetName() << "* p){" << fItemClass->GetName()
300 << " &i=*p; return (" << fFilterExpr.Data() << "); };";
301
302 // printf("%s\n", s.Data());
303 try {
304 gROOT->ProcessLine(s.str().c_str());
305 // AMT I don't know why ApplyFilter call is separated
306 ApplyFilter();
307 }
308 catch (const std::exception &exc)
309 {
310 R__LOG_ERROR(REveLog()) << "EveDataCollection::SetFilterExpr" << exc.what();
311 }
312 }
313 else
314 {
315 // Remove filter
316 fFilterFoo = nullptr;
317 Ids_t ids;
318 int idx = 0;
319 for (auto &ii : fItemList->fItems) {
320 if (ii->GetFiltered()) {
321 ii->SetFiltered(false);
322 ids.push_back(idx);
323 }
324 idx++;
325 }
326
330 }
331}
332
334{
335 if (!fFilterFoo)
336 return;
337
338 Ids_t ids;
339 int idx = 0;
340 for (auto &ii : fItemList->fItems)
341 {
342 bool res = fFilterFoo(ii->GetDataPtr());
343
344 // printf("Item:%s -- filter result = %d\n", ii.fItemPtr->GetElementName(), res);
345
346 ii->SetFiltered( ! res );
347
348 ids.push_back(idx++);
349 }
353}
354
355//______________________________________________________________________________
356
357void REveDataCollection::StreamPublicMethods(nlohmann::json &j) const
358{
359 j["fPublicFunctions"] = nlohmann::json::array();
360 TMethod *meth;
362 int cnt = 0;
363 while ((meth = (TMethod *)next())) {
364 // Filter out ctor, dtor, some ROOT stuff.
365
366 TString m(meth->GetName());
367
368 if (m == "Class" || m == "Class_Name" || m == "Class_Version" || m == "Dictionary" || m == "IsA" ||
369 m == "DeclFileName" || m == "ImplFileName" || m == "DeclFileLine" || m == "ImplFileLine" || m == "Streamer" ||
370 m == "StreamerNVirtual" || m == "ShowMembers" || m == "CheckTObjectHashConsistency") {
371 continue;
372 }
373
374 if (m.BeginsWith('~'))
375 continue;
376
377 if (m.Contains("operator"))
378 continue;
379
380 if (meth->GetListOfMethodArgs()->GetLast() > 1)
381 continue;
382
383 if (strcmp(meth->GetReturnTypeName(),"void") == 0)
384 continue;
385
386 TString ms;
387 TMethodArg *ma;
388 TIter next_ma(meth->GetListOfMethodArgs());
389 while ((ma = (TMethodArg *)next_ma())) {
390 if (!ms.IsNull())
391 ms += ", ";
392
393 ms += ma->GetTypeName();
394 ms += " ";
395 ms += ma->GetName();
396 }
397 std::string entry(TString::Format("i.%s(%s)", meth->GetName(), ms.Data()).Data());
398 nlohmann::json jm;
399 jm["f"] = entry;
400 jm["r"] = meth->GetReturnTypeName();
401 jm["c"] = meth->GetClass()->GetName();
402 j["fPublicFunctions"].push_back(jm);
403
404 if (m.Contains("charge"))
405 {
406 printf("FOUND chargw methos %s %d\n\n", m.Data(), cnt);
407 }
408
409 cnt++;
410 }
411}
412
413//______________________________________________________________________________
414
416{
417 int idx = 0;
418 Ids_t ids;
419 for (auto & chld : fItemList->fItems)
420 {
421 chld->SetMainColor(newv);
422 ids.push_back(idx);
423 idx++;
424 }
425
427 for (auto & chld : fItemList->fItems)
428 {
429 chld->SetMainColor(newv);
430 }
432 fItemList->SetMainColor(newv);
434}
435
436//______________________________________________________________________________
437
439{
440 Bool_t ret = REveElement::SetRnrState(iRnrSelf);
441 Ids_t ids;
442 for (int i = 0; i < GetNItems(); ++i ) {
443 ids.push_back(i);
444 fItemList->fItems[i]->SetRnrSelf(fRnrSelf);
445 }
446
449
450 return ret;
451}
452
453
454//______________________________________________________________________________
455
456Int_t REveDataCollection::WriteCoreJson(nlohmann::json &j, Int_t rnr_offset)
457{
458 Int_t ret = REveElement::WriteCoreJson(j, rnr_offset);
459 j["fFilterExpr"] = fFilterExpr.Data();
460 return ret;
461}
462
#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
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 r
char name[80]
Definition TGX11.cxx:110
Int_t gDebug
Definition TROOT.cxx:595
#define gROOT
Definition TROOT.h:406
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="")
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 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.
const TList * GetListOfAllPublicMethods(Bool_t load=kTRUE)
Returns a list of all public methods of this class and its base classes.
Definition TClass.cxx:3845
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:1839
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
TClass * GetClass() const
Definition TMethod.h:55
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
virtual Int_t GetLast() const
Returns index of last object in collection.
Basic string class.
Definition TString.h:139
Ssiz_t Length() const
Definition TString.h:417
const char * Data() const
Definition TString.h:376
Bool_t IsNull() const
Definition TString.h:414
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:2378
const Int_t n
Definition legend1.C:16
R__EXTERN REveManager * gEve
RLogChannel & REveLog()
Log channel for Eve diagnostics.
Definition REveTypes.cxx:47
TMarker m
Definition textangle.C:8
auto * tt
Definition textangle.C:16