Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
_rvec.pyzdoc
Go to the documentation of this file.
1\pythondoc ROOT::VecOps::RVec
2
3The ROOT::RVec class has additional features in Python, which allow to adopt memory
4from Numpy arrays and vice versa. The purpose of these features is the copyless
5interfacing of Python and C++ using their most common data containers, Numpy arrays
6and RVec with a std::vector interface.
7
8### Conversion of RVecs to Numpy arrays
9
10RVecs of fundamental types (int, float, ...) have in Python the `__array_interface__`
11attribute attached. This information allows Numpy to adopt the memory of RVecs without
12copying the content. You can find further documentation regarding the Numpy array interface
13[here](https://numpy.org/doc/stable/reference/arrays.interface.html). The following code example
14demonstrates the memory adoption mechanism using `numpy.asarray`.
15
16\code{.py}
17rvec = ROOT.RVec('double')((1, 2, 3))
18print(rvec) # { 1.0000000, 2.0000000, 3.0000000 }
19
20npy = numpy.asarray(rvec)
21print(npy) # [1. 2. 3.]
22
23rvec[0] = 42
24print(npy) # [42. 2. 3.]
25\endcode
26
27### Conversion of Numpy arrays to RVecs
28
29Data owned by Numpy arrays with fundamental types (int, float, ...) can be adopted by RVecs. To
30create an RVec from a Numpy array, ROOT offers the facility ROOT.VecOps.AsRVec, which performs
31a similar operation to `numpy.asarray`, but vice versa. A code example demonstrating the feature and
32the adoption of the data owned by the Numpy array is shown below.
33
34\code{.py}
35npy = numpy.array([1.0, 2.0, 3.0])
36print(npy) # [1. 2. 3.]
37
38rvec = ROOT.VecOps.AsRVec(npy)
39print(rvec) # { 1.0000000, 2.0000000, 3.0000000 }
40
41npy[0] = 42
42print(rvec) # { 42.000000, 2.0000000, 3.0000000 }
43\endcode
44
45\endpythondoc
#define a(i)
Definition RSha256.hxx:99
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
A "std::vector"-like collection of values implementing handy operation to analyse them.
Definition RVec.hxx:1529
ROOT::VecOps::RVec< T > RVec
Definition RVec.hxx:69
constexpr Double_t C()
Velocity of light in .
Definition TMath.h:114