Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
_rvec.pyzdoc
Go to the documentation of this file.
1/**
2\class ROOT::VecOps::RVec
3\brief \parblock \endparblock
4\htmlonly
5<div class="pyrootbox">
6\endhtmlonly
7## PyROOT
8
9The ROOT::RVec class has additional features in Python, which allow to adopt memory
10from Numpy arrays and vice versa. The purpose of these features is the copyless
11interfacing of Python and C++ using their most common data containers, Numpy arrays
12and RVec with a std::vector interface.
13
14### Conversion of RVecs to Numpy arrays
15
16RVecs of fundamental types (int, float, ...) have in Python the `__array_interface__`
17attribute attached. This information allows Numpy to adopt the memory of RVecs without
18copying the content. You can find further documentation regarding the Numpy array interface
19[here](https://numpy.org/doc/stable/reference/arrays.interface.html). The following code example
20demonstrates the memory adoption mechanism using `numpy.asarray`.
21
22\code{.py}
23rvec = ROOT.RVec('double')((1, 2, 3))
24print(rvec) # { 1.0000000, 2.0000000, 3.0000000 }
25
26npy = numpy.asarray(rvec)
27print(npy) # [1. 2. 3.]
28
29rvec[0] = 42
30print(npy) # [42. 2. 3.]
31\endcode
32
33### Conversion of Numpy arrays to RVecs
34
35Data owned by Numpy arrays with fundamental types (int, float, ...) can be adopted by RVecs. To
36create an RVec from a Numpy array, ROOT offers the facility ROOT.VecOps.AsRVec, which performs
37a similar operation to `numpy.asarray`, but vice versa. A code example demonstrating the feature and
38the adoption of the data owned by the Numpy array is shown below.
39
40\code{.py}
41npy = numpy.array([1.0, 2.0, 3.0])
42print(npy) # [1. 2. 3.]
43
44rvec = ROOT.VecOps.AsRVec(npy)
45print(rvec) # { 1.0000000, 2.0000000, 3.0000000 }
46
47npy[0] = 42
48print(rvec) # { 42.000000, 2.0000000, 3.0000000 }
49\endcode
50
51\htmlonly
52</div>
53\endhtmlonly
54*/