Logo ROOT   6.10/00
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
bindings/r/doc/users-guide/ROOTR Users Guide.md
Go to the documentation of this file.
1 # ROOTR Users Guide
2 
3 ## DESCRIPTION
4 ROOT R is an interface in ROOT to call R functions using an R C++ interface (Rcpp, see http://dirk.eddelbuettel.com/code/rcpp.html).
5 This interface opens the possibility in ROOT to use the very large set of mathematical and statistical tools provided by R.
6 With ROOTR you can perform a conversion from ROOT's C++ objects to R's objects, transform the returned R objects into ROOT's C++ objects, then
7 the R functionality can be used directly for statistical studies in ROOT.
8 
9 ## ROOTR BASICS
10 ROOTR creates a working environment to execute R coding called from `C++`. It allows to translate some datatypes from `C++` to R
11 inside the R environment and vice versa in an easy way to get the most from both R and ROOT.
12 To ease the sending and receiving of data in both environments, I overloaded the operators `<<`,`>>` and `[]`
13 which make look the job as a flow of data between environments, we will see more of that later.
14 With this tool you ca use any library or R package wich allows you to access a big amount of benefits to make statistical analysis.
15 ROOTR also has a R events processing system, which allows to use the R graphical system from `C++`.
16 
17 ## INSTALLATION
18 To install ROOTR please read first.
19 
20 - [https://root.cern.ch/building-root](https://root.cern.ch/building-root)
21 - [https://root.cern.ch/build-prerequisites](https://root.cern.ch/build-prerequisites)
22 
23 
24 ### COMPILING ROOTR ON MAC WITH CMAKE:
25 **NOTE:** Mac OSX Yosemite last xcode and without macports
26 
27 
28 **Prerequisites**
29 
30 - xcode
31 - [xquartz](http://xquartz.macosforge.org/)
32 - [R last version](https://www.r-project.org)
33 - [cmake](https://cmake.org/download/)
34 
35 To compile with cmake added into ~/.profile
36 
37 ~~~{.sh}
38 export PATH=$PATH:/Applications/CMake.app/Contents/bin/
39 ~~~
40 and
41 
42 ~~~{.sh}
43 source ~/.profile
44 ~~~
45 
46 Install needed R packages, open R and in the prompt type
47 
48 ~~~{.sh}
49 install.packages(c('Rcpp','RInside'))
50 ~~~
51 select a mirror and install.
52 
53 Install the next additional packages for R TMVA interface
54 
55 ~~~{.sh}
56 install.packages(c('C50','RSNNS','e1071','xgboost'))
57 ~~~
58 
59 
60 Download code from git repo
61 
62 ~~~{.sh}
63 git clone http://root.cern.ch/git/root.git
64 ~~~
65 
66 To compile ROOTR lets to create a compilation directory and to activate it use cmake -Dr=ON ..
67 
68 ~~~{.sh}
69 mkdir compile
70 cd compile
71 cmake -Dr=ON ..
72 make -j 5
73 ~~~
74 
75 ### Compiling ROOTR on Gnu/Linux with CMake:
76 **NOTE:** Tested on Gnu/Linux Debian Jessie with gcc 4.9
77 
78 **Prerequisities**
79 install
80 (For debian-based distros)
81 
82 ~~~{.sh}
83 apt-get install r-base r-base-dev
84 ~~~
85 Install needed R packages, open R and in the prompt type
86 
87 ~~~{.sh}
88 install.packages(c('Rcpp','RInside'))
89 ~~~
90 select a mirror and install.
91 
92 Install the next additional packages for R TMVA interface
93 
94 ~~~{.sh}
95 install.packages(c('C50','RSNNS','e1071','xgboost'))
96 ~~~
97 
98 Download code from git repo
99 
100 ~~~{.sh}
101 git clone http://root.cern.ch/git/root.git
102 ~~~
103 
104 To compile ROOTR lets to create a compilation directory and to activate it use cmake -Dr=ON ..
105 
106 ~~~{.sh}
107 mkdir compile
108 cd compile
109 cmake -Dr=ON ..
110 make -j 5
111 ~~~
112 
113 ## How does it work ?
114 There is a class called TRInterface which is located at the header TRInterface.h and uses the namespace `ROOT::R`, it is in charge
115 of making calls to R to give and obtain data. This class has a series of overcharged operators which ease the passing and obtaining of data
116 and code from R to C++ and vice versa. To create an object of this class the user must use the static methods `ROOT::R::TRInterface::Instance`
117 and `ROOT::R::TRInterface::InstancePtr` which return a reference object and a pointer object respectively.
118 
119 ~~~{.cxx}
120 #include<TRInterface.h>
121 ROOT::R::TRInterface &r=ROOT::R::TRInterface::Instance();
122 ~~~
123 
124 ## Running R code and passing/getting variables.
125 We have different ways to run R code and pass/obtain data to/from R environment: using the methods Execute(code) and
126 Eval(code).
127 
128 ~~~{.cxx}
129 #include<TRInterface.h>
130 
131 //creating an instance
132 ROOT::R::TRInterface &r=ROOT::R::TRInterface::Instance();
133 //executing simple r commands with the operator <<
134 r<<"print('hello ROOTR')";
135 r<<"vec=c(1,2,3)"<<"print(vec)";
136 
137 //executing R's code using the method Execute that doesn't return anything
138 r.Execute("print('hello ROOTR')");
139 
140 //We execute the code using the method Eval which returns an instance of TRObjectProxy
141 //which can be converted to a ROOTR supported classes
142 std::vector<Int_t> v=r.Eval("c(1,2,3)");
143 std::cout<<v[0]<<" "<<v[1]<<" "<<v[2]<<std::endl;
144 
145 std::vector<Double_t> vd(3);
146 
147 //obtaining variables from R environment using the operators [] and >>
148 r["seq(0,1,0.5)"]>>vd;
149 std::cout<<vd[0]<<" "<<vd[1]<<" "<<vd[2]<<std::endl;
150 
151 std::vector<Int_t> v1(3);
152 v1[0]=0;
153 v1[1]=1;
154 v1[2]=2;
155 
156 r["v1"]<<v1;
157 r<<"print(v1)";
158 
159 TMatrixD m(2,2);
160 
161 //Creating a matrix inside r environment and converting it into a TMatrixD
162 r<<"mat<-matrix(c(0.1,0.2,0.3,0.4),nrow=2)";
163 r["mat"]>>m;
164 m.Print();
165 ~~~
166 So, working with ROOTR is like working with flows of data to pass, obtain and process data.
167 
168 ## Passing functions from ROOT to R
169 You can pass functions from ROOT to R using the operators `<<` and `=` or using the class TRFunction, but the arguments and datatypes of the return value cannot be pointers. They must be ROOTR supported datatypes.
170 So instead of using `*Double_t` you must use `std::vector` and instead of `*Char_t` use TString or `std::string`.
171 
172 For this example we need to create a macro, so save it as fun.C
173 
174 ~~~{.cxx}
175 #include<TRInterface.h>
176 #include<TMath.h>
177 
178 Double_t myfun(Double_t x)
179 {
180  return 2*cos(x);
181 }
182 
183 Double_t myfun2(std::vector<Double_t> x) //use std::vector<Double_t> instead Double_t*
184 {
185  return x[1]*cos(x[0]);
186 }
187 
188 void fun()
189 {
190 ROOT::R::TRInterface &r=ROOT::R::TRInterface::Instance();
191 r["dilog"]<<ROOT::R::TRFunctionExport(TMath::DiLog);
192 r["myfun"]<<myfun;
193 r["myfun2"]<<myfun2;
194 r<<"print(dilog(0))";
195 r<<"print(myfun(0))";
196 r<<"print(myfun2(c(0,4)))";
197 }
198 ~~~
199 
200 **IMPORTANT**
201 - For overloaded functions you should pass the function with a explicit cast to the wanted function.
202 - The system works with templates and the template can not resolve the correct type of function because it is overloaded.
203 - If you pass a function without the explicit cast you will get a very ugly traceback.
204 - A lot of common standard functions for example from math.h like sin, cos etc.. are overloaded, take care passing it.
205 
206 ~~~{.cxx}
207 #include<TRInterface.h>
208 
209 Double_t myfun(Double_t x)
210 {
211  return 2*cos(x);
212 }
213 
214 Int_t myfun(Int_t x)
215 {
216  return x;
217 }
218 
219 void fun()
220 {
221 ROOT::R::TRInterface &r=ROOT::R::TRInterface::Instance();
222 r["myfund"]<<(Double_t (*)(Double_t))myfun;
223 r["myfuni"]<<(Int_t (*)(Int_t))myfun;
224 
225 r<<"print(myfund(0.0))";
226 r<<"print(myfuni(1))";
227 }
228 ~~~
229 
230 ## Wrapping a class
231 You can wrap a class and expose it in R environment using only a pair of macrodefinitions and the template class
232 `ROOT::R::class_<>`
233 The `ROOTR_EXPOSED_CLASS(Class)` macro allows you to expose the class as a new datatype of R, but it has to be alongside
234 the `ROOTR_MODULE(Module)` macro which allows you to create an internal R module and make the class wrapping
235 To do this you must use inside the `ROOTR_MODULE` braces the class `ROOT::R::class_<>` and specify
236 each constructor, attribute or method that the class to export has.
237 Then the macrodefinition `LOAD_ROOTR_MODULE(Module)` can load the module and the class in R's environment.
238 You can find a more clear instruction by looking at a example below in Functor section.
239 
240 ##DataFrames
241 DataFrame? is a very important datatype in R and in ROOTR we have a class to manipulate
242 dataframes called TRDataFrame, with a lot of very useful operators overloaded to work with TRDataFrame's objects
243 in a similar way that in the R environment but from c++ in ROOT.
244 Example:
245 
246 Lets to create need data to play with dataframe features
247 
248 ~~~{.cxx}
249 ////////////////////////
250 //creating variables//
251 ////////////////////////
252 TVectorD v1(3);
253 std::vector<Double_t> v2(3);
254 std::array<Int_t,3> v3{ {1,2,3} };
255 std::list<std::string> names;
256 
257 //////////////////////
258 //assigning values//
259 //////////////////////
260 v1[0]=1;
261 v1[1]=2;
262 v1[2]=3;
263 
264 v2[0]=0.101;
265 v2[1]=0.202;
266 v2[2]=0.303;
267 
268 names.push_back("v1");
269 names.push_back("v2");
270 names.push_back("v3");
271 
272 ROOT::R::TRInterface &r=ROOT::R::TRInterface::Instance();
273 ~~~
274 In R the dataframe have associate to every column a label, in ROOTR you can have the same label using the class ROOT::R::Label to create a TRDataFrame where you data
275 have a label associate.
276 
277 ~~~{.cxx}
278 /////////////////////////////////////////////////
279 //creating dataframe object with its labels//
280 /////////////////////////////////////////////////
281 
282 ROOT::R::TRDataFrame df1(ROOT::R::Label["var1"]=v1,ROOT::R::Label["var2"]=v2,ROOT::R::Label["var3"]=v3,ROOT::R::Label["strings"]=names);
283 
284 //////////////////////////////////////////////
285 //Passing dataframe to R's environment//
286 //////////////////////////////////////////////
287 
288 r["df1"]<<df1;
289 r<<"print(df1)";
290 ~~~
291 Output
292 
293 ~~~{.sh}
294 var1 var2 var3 strings
295 1 1 0.101 1 v1
296 2 2 0.202 2 v2
297 3 3 0.303 3 v3
298 ~~~
299 Manipulating data between dataframes
300 
301 ~~~{.cxx}
302 ////////////////////////////////
303 //Adding columns to dataframe //
304 ////////////////////////////////
305 
306 TVectorD v4(3);
307 //filling the vector fro R's environment
308 r["c(-1,-2,-3)"]>>v4;
309 //adding new column to df1 with name var4
310 df1["var4"]=v4;
311 //updating df1 in R's environment
312 r["df1"]<<df1;
313 //printing df1
314 r<<"print(df1)";
315 ~~~
316 Output
317 
318 ~~~{.sh}
319 var1 var2 var3 strings var4
320 1 1 0.101 1 v1 -1
321 2 2 0.202 2 v2 -2
322 3 3 0.303 3 v3 -3
323 ~~~
324 
325 Getting data frames from R's environment
326 
327 ~~~{.cxx}
328 //////////////////////////////////////////
329 //Getting dataframe from R's environment//
330 //////////////////////////////////////////
331 ROOT::R::TRDataFrame df2;
332 
333 r<<"df2<-data.frame(v1=c(0.1,0.2,0.3),v2=c(3,2,1))";
334 r["df2"]>>df2;
335 
336 TVectorD v(3);
337 df2["v1"]>>v;
338 v.Print();
339 
340 df2["v2"]>>v;
341 v.Print();
342 ~~~
343 
344 Output
345 
346 ~~~{.sh}
347 Vector (3) is as follows
348 
349  | 1 |
350 ------------------
351  0 |0.1
352  1 |0.2
353  2 |0.3
354 
355 Vector (3) is as follows
356 
357  | 1 |
358 ------------------
359  0 |3
360  1 |2
361  2 |1
362 ~~~
363 
364 ~~~{.cxx}
365 ///////////////////////////////////////////
366 //Working with columns between dataframes//
367 ///////////////////////////////////////////
368 
369 df2["v3"]<<df1["strings"];
370 
371 //updating df2 in R's environment
372 r["df2"]<<df2;
373 r<<"print(df2)";
374 ~~~
375 Output
376 
377 ~~~{.sh}
378 v1 v2 v3
379 1 0.1 3 v1
380 2 0.2 2 v2
381 3 0.3 1 v3
382 ~~~
383 
384 ~~~{.cxx}
385 ///////////////////////////////////////////
386 //Working with columns between dataframes//
387 ///////////////////////////////////////////
388 
389 //passing values from column v3 of df2 to var1 of df1
390 df2["v3"]>>df1["var1"];
391 //updating df1 in R's environment
392 r["df1"]<<df1;
393 r<<"print(df1)";
394 ~~~
395 
396 Output
397 
398 ~~~{.sh}
399 var1 var2 var3 strings var4
400 1 v1 0.101 1 v1 -1
401 2 v2 0.202 2 v2 -2
402 3 v3 0.303 3 v3 -3
403 ~~~
404 
405 ## Plotting with R's graphical system.
406 ROOTR supports an eventloop for R's graphical system which allows plotting using the R functions to the
407 graphical system or generating images(ps, pdf png, etc).
408 You can find a demo in Interpolation below in examples section.
409 
410 ## Interactive Mode
411 The interactive mode lets you get the R's command line within ROOT's command line to run R code with tab completion support.
412 The variables created in the interactive mode can be passed to ROOT with TRObjectProxy and the method ParseEval?.
413 To initialize the interactive mode just call Interactive() method and type ".q" to exit from R's prompt and to go to the ROOT's prompt again.
414 
415 ~~~{.cxx}
416 [omazapa] [tuxhome] [~]$ root -l
417 root [0] #include<TRInterface.h>
418 root [1] ROOT::R::TRInterface &r=ROOT::R::TRInterface::Instance();
419 root [2] r.Interactive()
420 [r]:a=seq
421 seq seq_along seq.Date seq.default seq.int seq_len seq.POSIXt sequence
422 [r]:a=seq(1,5,0.5)
423 [r]:.q
424 root [3] TVectorD v=r.ParseEval("a");
425 root [4] v.Print()
426 
427 Vector (9) is as follows
428 
429  | 1 |
430 ------------------
431  0 |1
432  1 |1.5
433  2 |2
434  3 |2.5
435  4 |3
436  5 |3.5
437  6 |4
438  7 |4.5
439  8 |5
440 
441 root [4]
442 ~~~
443 
444 ## Examples
445 The examples can also be found in `$ROOTSYS/tutorials/r`
446 
447 ## Creating a Functor
448 A functor is a class which wraps a function, very useful when states and properties
449 associated to that function are needed.
450 In this example I show how to give support to a custom class to be used in R's environment,
451 which at the same time is a functor.
452 
453 ~~~{.cxx}
454 #include<TRInterface.h>
455 #include<TMath.h>
456 
457 typedef Double_t (*Function)(Double_t);
458 
459 //Functor class with the function inside
460 class MyFunctor{
461 public:
462  MyFunctor(){
463  status=false;
464  f=TMath::BesselY1;
465  }
466  void setFunction(Function fun)
467  {
468  f=fun;
469  status=true;
470  }
471  Bool_t getStatus(){return status;}
472  Double_t doEval(Double_t x) {
473  return f(x);
474  }
475 private:
476  Function f;
477  Bool_t status;
478 };
479 //this macro exposes the class into R's environment
480 // and lets you pass objects directly.
481 ROOTR_EXPOSED_CLASS(MyFunctor)
482 
483 //Macro to create a module
484 ROOTR_MODULE(MyFunctorModule) {
485  ROOT::R::class_<MyFunctor>( "MyFunctor" )
486  //creating a default constructor
487  .constructor()
488  //adding the method doEval to evaluate the internal function
489  .method( "doEval", &MyFunctor::doEval )
490  .method( "getStatus", &MyFunctor::getStatus)
491  ;
492 }
493 
494 void Functor()
495 {
496  ROOT::R::TRInterface &r=ROOT::R::TRInterface::Instance();
497  ////////////////////////////////////////////////////////////
498  //Creating a functor with default function TMath::BesselY1//
499  // and status false from R's environment //
500  ////////////////////////////////////////////////////////////
501  //Loading module into R's environment
502  r["MyFunctorModule"]<<LOAD_ROOTR_MODULE(MyFunctorModule);
503 
504  //creating a class variable from the module
505  r<<"MyFunctor <- MyFunctorModule$MyFunctor";
506  //creating a MyFunctor's object
507  r<<"u <- new(MyFunctor)";
508 
509  //printing status
510  r<<"print(u$getStatus())";
511 
512  //printing values from Functor and Function
513  r<<"print(sprintf('value in R = %f',u$doEval( 1 )))";
514  std::cout<<"value in ROOT = "<<TMath::BesselY1(1)<<std::endl;
515 
516  ////////////////////////////////////////////////////////////
517  //creating a MyFunctor's object and passing object to R's //
518  //environment, the status should be true because is not //
519  //using the default function //
520  ////////////////////////////////////////////////////////////
521  MyFunctor functor;
522  functor.setFunction(TMath::Erf);
523  r["functor"]<<functor;
524  //printing the status that should be true
525  r<<"print(functor$getStatus())";
526  r<<"print(sprintf('value in R = %f',functor$doEval( 1 )))";
527  std::cout<<"value in ROOT = "<<TMath::Erf(1)<<std::endl;
528 }
529 ~~~
530 
531 ## Simple fitting in R and plot in ROOT
532 The next example creates an exponential fit.
533 The idea is to create a set of numbers x,y with noise from ROOT,
534 pass them to R and fit the data to `x^3`,
535 get the fitted coefficient(power) and plot the data,
536 the known function and the fitted function using ROOT's classes.
537 
538 ~~~{.cxx}
539 #include<TRInterface.h>
540 #include<TRandom.h>
541 
542 TCanvas *SimpleFitting(){
543  TCanvas *c1 = new TCanvas("c1","Curve Fitting",700,500);
544  c1->SetGrid();
545 
546  // draw a frame to define the range
547  TMultiGraph *mg = new TMultiGraph();
548 
549  // create the first graph (points with gaussian noise)
550  const Int_t n = 24;
551  Double_t x1[n] ;
552  Double_t y1[n] ;
553  //Generate the points along a X^3 with noise
554  TRandom rg;
555  rg.SetSeed(520);
556  for (Int_t i = 0; i < n; i++) {
557  x1[i] = rg.Uniform(0, 1);
558  y1[i] = TMath::Power(x1[i], 3) + rg.Gaus() * 0.06;
559  }
560 
561  TGraph *gr1 = new TGraph(n,x1,y1);
562  gr1->SetMarkerColor(kBlue);
563  gr1->SetMarkerStyle(8);
564  gr1->SetMarkerSize(1);
565  mg->Add(gr1);
566 
567  // create the second graph
568  TF1 *f_known=new TF1("f_known","pow(x,3)",0,1);
569  TGraph *gr2 = new TGraph(f_known);
570  gr2->SetMarkerColor(kRed);
571  gr2->SetMarkerStyle(8);
572  gr2->SetMarkerSize(1);
573  mg->Add(gr2);
574  //passing data to Rfot fitting
575  ROOT::R::TRInterface &r=ROOT::R::TRInterface::Instance();
576  r["x"]<<TVectorD(n, x1);
577  r["y"]<<TVectorD(n, y1);
578  //creating a R data frame
579  r<<"ds<-data.frame(x=x,y=y)";
580  //fitting x and y to X^power using Nonlinear Least Squares
581  r<<"m <- nls(y ~ I(x^power),data = ds, start = list(power = 1),trace = T)";
582  //getting the exponent
583  Double_t power;
584  r["summary(m)$coefficients[1]"]>>power;
585 
586  TF1 *f_fitted=new TF1("f_fitted","pow(x,[0])",0,1);
587  f_fitted->SetParameter(0,power);
588  //plotting the fitted function
589  TGraph *gr3 = new TGraph(f_fitted);
590  gr3->SetMarkerColor(kGreen);
591  gr3->SetMarkerStyle(8);
592  gr3->SetMarkerSize(1);
593 
594  mg->Add(gr3);
595  mg->Draw("ap");
596 
597  //displaying basic results
598  TPaveText *pt = new TPaveText(0.1,0.6,0.5,0.9,"brNDC");
599  pt->SetFillColor(18);
600  pt->SetTextAlign(12);
601  pt->AddText("Fitting x^power ");
602  pt->AddText(" \"Blue\" Points with gaussian noise to be fitted");
603  pt->AddText(" \"Red\" Known function x^3");
604  TString fmsg;
605  fmsg.Form(" \"Green\" Fitted function with power=%.4lf",power);
606  pt->AddText(fmsg);
607  pt->Draw();
608  c1->Update();
609  return c1;
610 }
611 ~~~
612 In the first image you can see the blue dots which are the function `x^3` with gaussian noise, the red dots correspond to
613 the original function and the green ones correspond to the fitted function.
614 
615 \image html R_image1.png
616 
617 ## Global Minimization in R using the package DEoptim
618 DEoptim is a R package for Differential Evolution Minimization that lets you do global
619 Minimization.
620 To install this package you just need to run:
621 
622 ~~~{.cxx}
623 #include<TRInterface.h>
624 ROOT::R::TRInterface &r=ROOT::R::TRInterface::Instance();
625 r<<"install.packages('DEoptim',repos='http://cran.rstudio.com/')";
626 ~~~
627 
628 Then create a macro named GlobalMinimization.C with the next code.
629 
630 ~~~{.cxx}
631 #include<TRInterface.h>
632 #include<TBenchmark.h>
633 #include<math.h>
634 #include<stdlib.h>
635 //In the next function the *double pointer should be changed by a TVectorD datatype,
636 //because the pointer has no meaning in the R environment.
637 //This is a generalization of the RosenBrock function, with the min xi=1 and i>0.
638 Double_t GenRosenBrock(const TVectorD xx )
639 {
640  int length=xx.GetNoElements();
641 
642  Double_t result=0;
643  for(int i=0;i<(length-1);i++)
644  {
645  result+=pow(1-xx[i],2)+100*pow(xx[i+1]-pow(xx[i],2),2);
646  }
647  return result;
648 }
649 
650 //the min xi=0 i>0
651 Double_t Rastrigin(const TVectorD xx)
652 {
653  int length=xx.GetNoElements();
654  Double_t result=10*length;
655  for(int i=0;i<length;i++)
656  {
657  result+=xx[i]*xx[i]-10*cos(6.2831853*xx[i]);
658  }
659  return result;
660 }
661 
662 void GlobalMinimization()
663 {
664  TBenchmark bench;
665  ROOT::R::TRInterface &r=ROOT::R::TRInterface::Instance();
666 
667  Bool_t installed=r.Eval("is.element('DEoptim', installed.packages()[,1])");
668  if(!installed)
669  {
670  std::cout<<"Package DEoptim no installed in R"<<std::endl;
671  std::cout<<"Run install.packages('DEoptim') in R's environment"<<std::endl;
672  return;
673  }
674 
675  //loading DEoptim
676  r<<"suppressMessages(library(DEoptim, quietly = TRUE))";
677 
678 // passing RosenBrock function to R
679  r["GenRosenBrock"]<<GenRosenBrock;
680 
681  //maximun number of iterations
682  r["MaxIter"]<<5000;
683  //n = size of vector that is an argument for GenRosenBrock
684  r["n"]<<3;
685  //lower limits
686  r<<"ll<-rep(-25, n)";
687  //upper limits
688  r<<"ul<-rep(25, n)";
689 
690  bench.Start("GlobalMinimizationRosenBrock");
691  //calling minimization and timing it.
692  r<<"result1<-DEoptim(fn=GenRosenBrock,lower=ll,upper=ul,control=list(NP=10*n,itermax=MaxIter,trace=FALSE))";
693  std::cout<<"-----------------------------------------"<<std::endl;
694  std::cout<<"RosenBrock's minimum in: "<<std::endl;
695  r<<"print(result1$optim$bestmem)";
696  std::cout<<"Bechmark Times"<<std::endl;
697 // printing times
698  bench.Show("GlobalMinimizationRosenBrock");
699 
700 
701  //passing RosenBrock function to R
702  r["Rastrigin"]<<Rastrigin;
703  //maximun number of iterations
704  r["MaxIter"]<<2000;
705  //n = size of a vector which is an argument for Rastrigin
706  r["n"]<<3;
707  //lower limits
708  r<<"ll<-rep(-5, n)";
709  //upper limits
710  r<<"ul<-rep(5, n)";
711 
712  bench.Start("GlobalMinimizationRastrigin");
713  //calling minimization and timing it.
714  r<<"result2<-DEoptim(fn=Rastrigin,lower=ll,upper=ul,control=list(NP=10*n,itermax=MaxIter,trace=FALSE))";
715  std::cout<<"-----------------------------------------"<<std::endl;
716  std::cout<<"Rastrigin's minimum in: "<<std::endl;
717  r<<"print(result2$optim$bestmem)";
718  std::cout<<"Bechmark Times"<<std::endl;
719  //printing times
720  bench.Show("GlobalMinimizationRastrigin");
721  r<<"dev.new(title='RosenBrock Convergence')";
722  r<<"plot(result1,type='o',pch='.')";
723  r<<"dev.new(title='Rastrigin Convergence')";
724  r<<"plot(result2,type='o',pch='.')";
725 }
726 ~~~
727 In the image you can see the convergence plots of the functions and their minimum.
728 For RosenBrock is (1,1,1) and for Rastrigin is (0,0,0).
729 
730 \image html R_image2.png
731 
732 ## Interpolation (Plotting in R)
733 This example shows an interpolation using the function aproxfun and how to make a plot with R's
734 graphical functions.
735 
736 More Information on R interpolation
737 [here](http://stat.ethz.ch/R-manual/R-patched/library/stats/html/approxfun.html).
738 
739 ~~~{.cxx}
740 #include<TRInterface.h>
741 #include<TRandom.h>
742 #include<vector>
743 
744 void Interpolation()
745 {
746  ROOT::R::TRInterface &r=ROOT::R::TRInterface::Instance();
747 //Creating points
748 TRandom rg;
749 std::vector<Double_t> x(10),y(10);
750 for(int i=0;i<10;i++)
751 {
752  x[i]=i;
753  y[i]=rg.Gaus();
754 }
755 
756 r["x"]=x;
757 r["y"]=y;
758 
759 
760 r<<"dev.new()";//Required to activate new window for plotting
761 //Plot parameter. Plotting using two rows and one column
762 r<<"par(mfrow = c(2,1))";
763 
764 //plotting the points
765 r<<"plot(x, y, main = 'approx(.) and approxfun(.)')";
766 
767 //The function "approx" returns a list with components x and y,
768 //containing n coordinates which interpolate the given data points according to the method (and rule) desired.
769 r<<"points(approx(x, y), col = 2, pch = '*')";
770 r<<"points(approx(x, y, method = 'constant'), col = 4, pch = '*')";
771 
772 
773 //The function "approxfun" returns a function performing (linear or constant)
774 //interpolation of the given data.
775 //For a given set of x values, this function will return the corresponding interpolated values.
776 r<<"f <- approxfun(x, y)";
777 
778 r<<"curve(f(x), 0, 11, col = 'green2')";
779 r<<"points(x, y)";
780 
781 
782 //using approxfun with const method
783 r<<"fc <- approxfun(x, y, method = 'const')";
784 r<<"curve(fc(x), 0, 10, col = 'darkblue', add = TRUE)";
785 // different interpolation on left and right side :
786 r<<"plot(approxfun(x, y, rule = 2:1), 0, 11,col = 'tomato', add = TRUE, lty = 3, lwd = 2)";
787 }
788 ~~~
789 The image shows the interpolated function plotted within R:
790 \image html R_image3.png
791 
792 ## Integration (Passing vectorized function to R)
793 Numerical integration using R passing the function from ROOT
794 
795 ~~~{.cxx}
796 #include<TMath.h>
797 #include<TRInterface.h>
798 #include<Math/Integrator.h>
799 #include<TF1.h>
800 
801 //To integrate using R the function must be vectorized
802 //The idea is just to receive a vector like an argument,to evaluate
803 //every element saving the result in another vector
804 //and return the resultant vector.
805 std::vector<Double_t> BreitWignerVectorized(std::vector<Double_t> xx)
806 {
807  std::vector<Double_t> result(xx.size());
808  for(Int_t i=0;i<xx.size();i++)
809  {
810  result[i]=TMath::BreitWigner(xx[i]);
811  }
812  return result;
813 }
814 
815 double BreitWignerWrap( double x){
816  return TMath::BreitWigner(x);
817 }
818 
819 
820 void Integration()
821 {
822 
823  ROOT::R::TRInterface &r=ROOT::R::TRInterface::Instance();
824 
825  r["BreitWigner"]=BreitWignerVectorized;
826 
827  Double_t value=r.Eval("integrate(BreitWigner, lower = -2, upper = 2)$value");
828 
829  std::cout.precision(18);
830  std::cout<<"Integral of the BreitWigner Function in the interval [-2, 2] R = "<<value<<std::endl;
831 
832 
833  ROOT::Math::WrappedFunction<> wf(BreitWignerWrap);
834  ROOT::Math::Integrator i(wf);
835  value=i.Integral(-2,2);
836  std::cout<<"Integral of the BreitWigner Function in the interval [-2, 2] MathMore = "<<value<<std::endl;
837 
838 
839  TF1 f1("BreitWigner","BreitWignerWrap(x)");
840  value=f1.Integral(-2,2);
841  std::cout<<"Integral of the BreitWigner Function in the interval [-2, 2] TF1 = "<<value<<std::endl;
842 
843  //infinte limits
844  value=r.Eval("integrate(BreitWigner, lower = -Inf, upper = Inf)$value");
845  std::cout<<"Integral of BreitWigner Function in the interval [-Inf, Inf] R = "<<value<<std::endl;
846 
847 }
848 ~~~
849 
850 ## Users Guide Sites
851 - http://oproject.org/tiki-index.php?page=ROOT+R+Users+Guide
852 
853 
for(Int_t i=0;i< n;i++)
Definition: legend1.C:18
double read(const std::string &file_name)
reading
TString as(SEXP s)
Definition: RExports.h:71
TArc * a
Definition: textangle.C:12
constexpr std::array< decltype(std::declval< F >)(std::declval< int >))), N > make(F f)
void ROOTR()
Definition: ROOTR.C:9
static double C[]
#define I(x, y, z)
TRandom3 R
a TMatrixD.
Definition: testIO.cxx:28