ROOT  6.06/09
Reference Guide
TRObject.h
Go to the documentation of this file.
1 // @(#)root/r:$Id$
2 // Author: Omar Zapata 29/05/2013
3 
4 /*************************************************************************
5  * Copyright (C) 2013-2014, Omar Andres Zapata Mesa *
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 #ifndef ROOT_R_TRObject
12 #define ROOT_R_TRObject
13 
14 #ifndef ROOT_R_RExports
15 #include<RExports.h>
16 #endif
17 
18 
19 namespace ROOT {
20  namespace R {
21 
22  /**
23 
24  This is a class to get ROOT's objects from R's objects
25  <center><h2>TRObject class</h2></center>
26 
27  <p>
28  The TRObject class lets you obtain ROOT's objects from R's objects.<br>
29  It has some basic template opetarors to convert R's objects into ROOT's datatypes<br>
30  </p>
31  A simple example<br>
32  <p>
33 
34  </p>
35 
36  \code{.cpp}
37  #include<TRInterface.h>
38  void Proxy()
39  {
40  ROOT::R::TRInterface &r=ROOT::R::TRInterface::Instance();
41  ROOT::R::TRObject obj;
42  obj=r.Eval("seq(1,10)");
43  TVectorD v=obj;
44  v.Print();
45  }
46  \endcode
47  Output
48  \code
49 
50  Vector (10) is as follows
51 
52  | 1 |
53  ------------------
54  0 |1
55  1 |2
56  2 |3
57  3 |4
58  4 |5
59  5 |6
60  6 |7
61  7 |8
62  8 |9
63  9 |10
64 
65  \endcode
66 
67  <h2>Users Guide </h2>
68  <a href="http://oproject.org/tiki-index.php?page=ROOT+R+Users+Guide"> http://oproject.org/tiki-index.php?page=ROOT+R+Users+Guide</a><br>
69  <a href="https://root.cern.ch/drupal/content/how-use-r-root-root-r-interface"> https://root.cern.ch/drupal/content/how-use-r-root-root-r-interface</a>
70 
71  @ingroup R
72  */
73  class TRObject: public TObject {
74  friend SEXP Rcpp::wrap<TRObject>(const TRObject &f);
75  private:
76  Rcpp::RObject fObj; //insternal Rcpp::RObject
77  Bool_t fStatus;//status tell if is a valid object
78  public:
79  /**
80  Default constructor
81  */
82  TRObject(): TObject() {};
83  /**
84  Construct a TRObject given a R base object
85  \param robj raw R object
86  */
87  TRObject(SEXP robj);
88  /**
89  Construct a TRObject given a R base object
90  \param robj raw R object
91  \param status if the raw object is valid obj
92  */
93  TRObject(SEXP robj, Bool_t status);
94 
95  /**
96  TRObject is a current valid object?
97  \param status if the current object is valid obj
98  */
100  {
101  fStatus = status;
102  }
103 
104  /**
105  TRObject is a current valid object?
106  \return status if the current object
107  */
109  {
110  return fStatus;
111  }
112 
113  /**
114  The R objects can to have associate attributes
115  with this method you can added attribute to TRObject given an object in the template argument.
116  \param name attribute name
117  \param obj object associated to the attribute name in the current TRObject
118  */
119  template<class T> void SetAttribute(const TString name, T obj)
120  {
121  fObj.attr(name.Data()) = obj;
122  }
123 
124  /**
125  The R objects can to have associate attributes
126  with this method you can added attribute to TRObject given an object in the template argument.
127  \param name attribute name
128  \return object associated to the attribute name in the current TRObject
129  */
131  {
132  return fObj.attr(name.Data());
133  }
134 
135  void operator=(SEXP xx);
136 
137  /**
138  Some datatypes of ROOT or c++ can be wrapped in to a TRObject,
139  this method lets you wrap those datatypes
140  \param obj template object to be wrapped
141  \return TRObject reference of wrapped object
142  */
143  template<class T> TRObject &Wrap(T obj)
144  {
145  fObj =::Rcpp::wrap(obj);
146  return *this;
147  }
148 
149  /**
150  Some datatypes of ROOT or c++ can be wrapped in to a TRObject,
151  this method lets you unwrap those datatypes encapsulate into this TRObject.
152  \note If the current TRObject is not a valid object it will return and empty object and it will print an error message
153  \return template return with the require datatype
154  */
155  template<class T> T As()
156  {
157  if (fStatus) {
158  T data =::Rcpp::as<T>(fObj);
159  return data;
160  } else {
161  Error("Cast Operator", "Can not make the requested data, returning an unknow value");
162  return T();
163  }
164  }
165 
166  template<class T> T operator=(TRObject &obj)
167  {
168  return ::Rcpp::as<T>(obj);
169  }
170 
171  operator SEXP()
172  {
173  return fObj;
174  }
175 
176  operator SEXP() const
177  {
178  return fObj;
179  }
180 
181  operator Rcpp::RObject()
182  {
183  return fObj;
184  }
185 
186  template <class T> operator T()
187  {
188 
189  if (fStatus) {
190  T data =::Rcpp::as<T>(fObj);
191  return data;
192  } else {
193  Error("Cast Operator", "Can not make the requested data, returning an unknow value");
194  return T();
195  }
196  }
197  ClassDef(TRObject, 0) //
198  };
199 
200  }
201 }
202 
203 
204 #endif
Namespace for new ROOT classes and functions.
Definition: ROOT.py:1
double T(double x)
Definition: ChebyshevPol.h:34
Basic string class.
Definition: TString.h:137
TAlienJobStatus * status
Definition: TAlienJob.cxx:51
bool Bool_t
Definition: RtypesCore.h:59
const char * Data() const
Definition: TString.h:349
#define ClassDef(name, id)
Definition: Rtypes.h:254
void operator=(SEXP xx)
Definition: TRObject.cxx:19
Rcpp::RObject fObj
Definition: TRObject.h:76
TRObject GetAttribute(const TString name)
The R objects can to have associate attributes with this method you can added attribute to TRObject g...
Definition: TRObject.h:130
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:918
SEXP wrap(const TString &s)
Definition: RExports.h:81
T As()
Some datatypes of ROOT or c++ can be wrapped in to a TRObject, this method lets you unwrap those data...
Definition: TRObject.h:155
TRObject & Wrap(T obj)
Some datatypes of ROOT or c++ can be wrapped in to a TRObject, this method lets you wrap those dataty...
Definition: TRObject.h:143
This is a class to get ROOT's objects from R's objects
Definition: TRObject.h:73
T operator=(TRObject &obj)
Definition: TRObject.h:166
TRObject()
Default constructor.
Definition: TRObject.h:82
void SetStatus(Bool_t status)
TRObject is a current valid object?
Definition: TRObject.h:99
Bool_t fStatus
Definition: TRObject.h:77
double f(double x)
#define name(a, b)
Definition: linkTestLib0.cpp:5
Mother of all ROOT objects.
Definition: TObject.h:58
Bool_t GetStatus()
TRObject is a current valid object?
Definition: TRObject.h:108
void SetAttribute(const TString name, T obj)
The R objects can to have associate attributes with this method you can added attribute to TRObject g...
Definition: TRObject.h:119
TObject * obj
TRandom3 R
a TMatrixD.
Definition: testIO.cxx:28