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 <TClass.h>
15#include <sstream>
16#include <TROOT.h>
17
18using namespace ROOT::Experimental;
19
20//==============================================================================
21// REveDataTable
22//==============================================================================
23
24REveDataTable::REveDataTable(const std::string& n, const std::string& t) :
25 REveElement(n, t)
26{
27 fChildClass = TClass::GetClass<REveDataColumn>();
28}
29
31{
33
34 for (Int_t i = 0; i< Nit; ++i)
35 {
36 void *data = fCollection->GetDataPtr(i);
37 // const REveDataItem &item = fCollection->RetDataItem(i);
38
39 // !!! printf("| %-20s |", item->GetCName());
40
41 for (auto & chld : fChildren)
42 {
43 auto clmn = dynamic_cast<REveDataColumn*>(chld);
44
45 printf(" %10s |", clmn->EvalExpr(data).c_str());
46 }
47 printf("\n");
48 }
49}
50
51Int_t REveDataTable::WriteCoreJson(nlohmann::json &j, Int_t rnr_offset)
52{
53 int ret = REveElement::WriteCoreJson(j, rnr_offset);
55
56 nlohmann::json jarr = nlohmann::json::array();
57
58 for (Int_t i = 0; i < Nit; ++i) {
59 void *data = fCollection->GetDataPtr(i);
60 nlohmann::json row;
61 for (auto &chld : fChildren) {
62 auto clmn = dynamic_cast<REveDataColumn *>(chld);
63 row[chld->GetCName()] = clmn->EvalExpr(data);
64 }
65 jarr.push_back(row);
66 }
67 j["body"] = jarr;
69 j["fCollectionId"] = fCollection->GetElementId();
70 return ret;
71}
72
73void REveDataTable::AddNewColumn(const std::string& expr, const std::string& title, int prec)
74{
75 auto c = new REveDataColumn(title);
77
78 c->SetExpressionAndType(expr, REveDataColumn::FT_Double);
79 c->SetPrecision(prec);
80
82}
83
84//==============================================================================
85// REveDataColumn
86//==============================================================================
87
88REveDataColumn::REveDataColumn(const std::string& n, const std::string& t) :
89 REveElement(n, t)
90{
91}
92
93//______________________________________________________________________________
94
95void REveDataColumn::SetExpressionAndType(const std::string& expr, FieldType_e type, TClass* icls)
96{
97 fExpression = expr;
98 fType = type;
99
100 const char *rtyp = nullptr;
101 const void *fooptr = nullptr;
102
103 switch (fType)
104 {
105 case FT_Double: rtyp = "double"; fooptr = &fDoubleFoo; break;
106 case FT_Bool: rtyp = "bool"; fooptr = &fBoolFoo; break;
107 case FT_String: rtyp = "std::string"; fooptr = &fStringFoo; break;
108 }
109
110 std::stringstream s;
111 s << "*((std::function<" << rtyp << "(" << icls->GetName() << "*)>*)" << std::hex << std::showbase << (size_t)fooptr
112 << ") = [](" << icls->GetName() << "* p){" << icls->GetName() << " &i=*p; return (" << fExpression.Data()
113 << "); }";
114
115 // printf("%s\n", s.str().c_str());
116 try {
117 gROOT->ProcessLine(s.str().c_str());
118 }
119 catch (const std::exception &exc)
120 {
121 std::cerr << "REveDataColumn::SetExpressionAndType" << exc.what();
122 }
123}
124//______________________________________________________________________________
125
127{
128 auto table = dynamic_cast<REveDataTable*>(fMother);
129 auto coll = table->GetCollection();
130 auto icls = coll->GetItemClass();
131 SetExpressionAndType(expr, type, icls);
132}
133
135{
136 fPrecision = prec;
137}
138
139std::string REveDataColumn::EvalExpr(void *iptr) const
140{
141 switch (fType)
142 {
143 case FT_Double:
144 {
145 TString ostr;
146 ostr.Form("%.*f", fPrecision, fDoubleFoo(iptr));
147 return ostr.Data();
148 }
149 case FT_Bool:
150 {
151 return fBoolFoo(iptr) ? fTrue : fFalse;
152 }
153 case FT_String:
154 {
155 return fStringFoo(iptr);
156 }
157 }
158 return "XYZ";
159}
#define c(i)
Definition RSha256.hxx:101
int type
Definition TGX11.cxx:121
#define gROOT
Definition TROOT.h:406
void StreamPublicMethods(nlohmann::json &cj) const
std::function< double(void *)> fDoubleFoo
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:2309
const Int_t n
Definition legend1.C:16