ROOT  6.06/09
Reference Guide
Binding.C
Go to the documentation of this file.
1 // Author: Omar Zapata
2 #include<TRInterface.h>
3 #include<vector>
4 #include<array>
5 
7 {
8  return x/(x-1);
9 }
10 
12 {
13  return x/(x-1);
14 }
15 //overloaded function to test the operator <<
17 {
18  return x-1;
19 }
20 
21 void Binding(){
22 //creating variables
23 TVectorD v(3);
24 std::vector<Double_t> sv(3);
25 std::array<Int_t,3> a{ {1,2,3} };
26 TString str("ROOTR");
27 TMatrixD m(2,2);
28 Int_t i=9;
29 Double_t d=2.013;
30 Float_t f=0.013;
31 
32 //assinging values
33 v[0]=0.01;
34 v[1]=1.01;
35 v[2]=2.01;
36 
37 sv[0]=0.101;
38 sv[1]=0.202;
39 sv[2]=0.303;
40 
41 m[0][0]=0.01;
42 m[0][1]=1.01;
43 m[1][0]=2.01;
44 m[1][1]=3.01;
45 
47 // r.SetVerbose(kTRUE);
48 
49 //testing operators binding
50 r["a"]<<1;
51 r["v"]<<v;
52 r["sv"]<<sv;
53 r["m"]<<m;
54 r["b"]<<123.456;
55 r["i"]<<i;
56 r["d"]<<d;
57 r["f"]<<f;
58 r["array"]<<a;
59 r["s"]<<"ROOT";
60 
61 
62 //printting results
63 std::cout<<"-----------Printing Results---------\n";
64 r<<"print(a)";
65 std::cout<<"--------------------\n";
66 r<<"print(v)";
67 std::cout<<"--------------------\n";
68 r<<"print(sv)";
69 std::cout<<"--------------------\n";
70 r<<"print(m)";
71 std::cout<<"--------------------\n";
72 r<<"print(b)";
73 std::cout<<"--------------------\n";
74 r<<"print(i)";
75 std::cout<<"--------------------\n";
76 r<<"print(d)";
77 std::cout<<"--------------------\n";
78 r<<"print(f)";
79 std::cout<<"--------------------\n";
80 r<<"print(s)";
81 std::cout<<"--------------------\n";
82 r<<"print(array)";
83 std::cout<<"--------------------\n";
84 
85 //reassigning the variable s
86 r["s"]<<str;//string with string
87 r<<"print(s)";
88 
89 // std::cout<<"--------------------\n";
90 // r["d"]<<str;//double with string
91 // r<<"print(d)";
92 
93 r["Function"]<<ROOT::R::TRFunctionExport(Function);
94 r<<"print(Function(-1))";
95 
96 r<<"print(Function(1))";//division by zero producess Inf.
97 
98 r<<"print('hello ')"<<std::string("print('world ')");
99 r["x"]=123;
100 r["y"]=321;
101 
102 Int_t x;
103 x=r["x"];
104 std::cout<<x<<std::endl;
105 
106 r["y"]>>x;
107 std::cout<<x<<std::endl;
108 
109 r<<"mat<-matrix(c(1,2,3,4),nrow=2)";
110 
111 TMatrixD mat(2,2);
112 r["mat"]>>mat;
113 
114 r["m"]<<mat;
115 
116 Double_t b;
117 Int_t aa;
118 TString str2;
119 r["a"]>>aa;
120 r["v"]>>v;
121 r["sv"]>>sv;
122 r["m"]>>m;
123 r["b"]>>b;
124 r["i"]>>i;
125 r["d"]>>d;
126 r["f"]>>f;
127 r["array"]>>a;
128 r["s"]>>str2;
129 
130 mat.Print();
131 std::cout<<" array={"<<a[0]<<","<<a[1]<<","<<a[2]<<"}";
132 r["func"]<<Function;
133 r<<"print(func(2))";
134 std::cout<<"func="<<Function(2);
135 
136 //passing overloaded functions
137 r["funi"]<<(Int_t (*)(Int_t))Fun;
138 r<<"print(funi(2))";
139 std::cout<<"funi="<<Fun(2)<<std::endl;
140 
141 r["fund"]<<(Double_t (*)(Double_t))Fun;
142 r<<"print(fund(2.01))";
143 std::cout<<"fund="<<Fun(2.01)<<std::endl;
144 
145 //if you uncomment the next line you get a big
146 //traceback because the template can not reslve the overloaded
147 //function.
148 //r["fun"]<<Fun;
149 }
Double_t Fun(Double_t x)
Definition: Binding.C:11
float Float_t
Definition: RtypesCore.h:53
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
TArc * a
Definition: textangle.C:12
Double_t Function(Double_t x)
Definition: Binding.C:6
void Binding()
Definition: Binding.C:21
Double_t x[n]
Definition: legend1.C:17
ROOT::R::TRInterface & r
Definition: Object.C:4
SVector< double, 2 > v
Definition: Dict.h:5
TMarker * m
Definition: textangle.C:8
double f(double x)
double Double_t
Definition: RtypesCore.h:55
static TRInterface & Instance()
static method to get an TRInterface instance reference
This is a class to pass functions from ROOT to R