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
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);
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& e) {
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
#define e(i)
Definition RSha256.hxx:103
int type
Definition TGX11.cxx:121
#define gROOT
Definition TROOT.h:404
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)
virtual Int_t WriteCoreJson(nlohmann::json &cj, Int_t rnr_offset)
Write core json.
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)
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:80
virtual const char * GetName() const
Returns name of object.
Definition TNamed.h:47
Basic string class.
Definition TString.h:136
const char * Data() const
Definition TString.h:369
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition TString.cxx:2314
const Int_t n
Definition legend1.C:16
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