Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
REveDataTable.cxx
Go to the documentation of this file.
1// @(#)root/eve7:$Id$
2// Authors: Matevz Tadel & Alja Mrak-Tadel: 2020
3
4/*************************************************************************
5 * Copyright (C) 1995-2020, 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
14#include <ROOT/REveUtil.hxx>
15#include <ROOT/RLogger.hxx>
16#include "TClass.h"
17#include "TROOT.h"
18
19#include <sstream>
20
21#include <nlohmann/json.hpp>
22
23using namespace ROOT::Experimental;
24
25//==============================================================================
26// REveDataTable
27//==============================================================================
28
29REveDataTable::REveDataTable(const std::string& n, const std::string& t) :
30 REveElement(n, t)
31{
32 fChildClass = TClass::GetClass<REveDataColumn>();
33}
34
36{
38
39 for (Int_t i = 0; i< Nit; ++i)
40 {
41 void *data = fCollection->GetDataPtr(i);
42 // const REveDataItem &item = fCollection->RetDataItem(i);
43
44 // !!! printf("| %-20s |", item->GetCName());
45
46 for (auto & chld : fChildren)
47 {
48 auto clmn = dynamic_cast<REveDataColumn*>(chld);
49
50 printf(" %10s |", clmn->EvalExpr(data).c_str());
51 }
52 printf("\n");
53 }
54}
55
56Int_t REveDataTable::WriteCoreJson(nlohmann::json &j, Int_t rnr_offset)
57{
58 int ret = REveElement::WriteCoreJson(j, rnr_offset);
60
61 nlohmann::json jarr = nlohmann::json::array();
62
63 for (Int_t i = 0; i < Nit; ++i) {
64 void *data = fCollection->GetDataPtr(i);
65 nlohmann::json row;
66 for (auto &chld : fChildren) {
67 auto clmn = dynamic_cast<REveDataColumn *>(chld);
68
69 try {
70 row[chld->GetCName()] = clmn->EvalExpr(data);
71 }
72 catch (const std::exception&) {
73 R__LOG_ERROR(REveLog()) << "can't eval expr " << clmn->fExpression.Data();
74 row[chld->GetCName()] = "err";
75 }
76 }
77 jarr.push_back(row);
78 }
79 j["body"] = jarr;
81 j["fCollectionId"] = fCollection->GetElementId();
82 return ret;
83}
84
85void REveDataTable::AddNewColumn(const std::string &expr, const std::string &title, int prec)
86{
87 auto c = new REveDataColumn(title);
88 c->SetExpressionAndType(expr, REveDataColumn::FT_Double);
89 c->SetPrecision(prec);
90 gROOT->ProcessLine(c->GetFunctionExpressionString().c_str());
91
92 if (c->hasValidExpression()) {
95 }
96}
97
98//==============================================================================
99// REveDataColumn
100//==============================================================================
101
102REveDataColumn::REveDataColumn(const std::string& n, const std::string& t) :
103 REveElement(n, t)
104{
105}
106
107//______________________________________________________________________________
108
109void REveDataColumn::SetExpressionAndType(const std::string& expr, FieldType_e type, TClass* icls)
110{
111 fExpression = expr;
112 fType = type;
113 fClassType = icls;
114}
115
116//______________________________________________________________________________
118{
119 auto table = dynamic_cast<REveDataTable*>(fMother);
120 auto coll = table->GetCollection();
121 auto icls = coll->GetItemClass();
122 SetExpressionAndType(expr, type, icls);
123}
124
125//______________________________________________________________________________
127{
128 fPrecision = prec;
129}
130
131//______________________________________________________________________________
133{
134 const char *rtyp = nullptr;
135 const void *fooptr = nullptr;
136
137 switch (fType)
138 {
139 case FT_Double: rtyp = "double"; fooptr = &fDoubleFoo; break;
140 case FT_Bool: rtyp = "bool"; fooptr = &fBoolFoo; break;
141 case FT_String: rtyp = "std::string"; fooptr = &fStringFoo; break;
142 }
143
144 std::stringstream s;
145 s << " *((std::function<" << rtyp << "(" << fClassType->GetName() << "*)>*)"
146 << std::hex << std::showbase << (size_t)fooptr
147 << ") = [](" << fClassType->GetName() << "* p){" << fClassType->GetName() << " &i=*p; return (" << fExpression.Data()
148 << "); };";
149
150
151 return s.str();
152}
153
154//______________________________________________________________________________
156{
157 return (fDoubleFoo || fBoolFoo || fStringFoo);
158}
159
160//______________________________________________________________________________
161std::string REveDataColumn::EvalExpr(void *iptr) const
162{
163 if (!hasValidExpression())
164 return "ErrFunc";
165
166 switch (fType)
167 {
168 case FT_Double:
169 {
170 TString ostr;
171 ostr.Form("%.*f", fPrecision, fDoubleFoo(iptr));
172 return ostr.Data();
173 }
174 case FT_Bool:
175 {
176 return fBoolFoo(iptr) ? fTrue : fFalse;
177 }
178 case FT_String:
179 {
180 return fStringFoo(iptr);
181 }
182 }
183 return "Nn";
184}
#define R__LOG_ERROR(...)
Definition RLogger.hxx:362
#define c(i)
Definition RSha256.hxx:101
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 Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
#define gROOT
Definition TROOT.h:406
void StreamPublicMethods(nlohmann::json &cj) const
std::function< double(void *)> fDoubleFoo
std::string GetFunctionExpressionString() const
std::function< bool(void *)> fBoolFoo
REveDataColumn(const std::string &n="REveDataColumn", const std::string &t="")
std::function< std::string(void *)> fStringFoo
std::string EvalExpr(void *iptr) const
void SetExpressionAndType(const std::string &expr, FieldType_e type)
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)
Int_t WriteCoreJson(nlohmann::json &cj, Int_t rnr_offset) override
Write core json.
virtual Int_t WriteCoreJson(nlohmann::json &cj, Int_t rnr_offset)
Write core json.
virtual void AddElement(REveElement *el)
Add el to the list of children.
ElementId_t GetElementId() const
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:81
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
Basic string class.
Definition TString.h:139
const char * Data() const
Definition TString.h:376
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition TString.cxx:2356
const Int_t n
Definition legend1.C:16
RLogChannel & REveLog()
Log channel for Eve diagnostics.
Definition REveTypes.cxx:51