ROOT
v6-32
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
9
The ROOT::RVec class has additional features in Python, which allow to adopt memory
10
from Numpy arrays and vice versa. The purpose of these features is the copyless
11
interfacing of Python and C++ using their most common data containers, Numpy arrays
12
and RVec with a std::vector interface.
13
14
### Conversion of RVecs to Numpy arrays
15
16
RVecs of fundamental types (int, float, ...) have in Python the `__array_interface__`
17
attribute attached. This information allows Numpy to adopt the memory of RVecs without
18
copying 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
20
demonstrates the memory adoption mechanism using `numpy.asarray`.
21
22
\code{.py}
23
rvec = ROOT.RVec('double')((1, 2, 3))
24
print(rvec) # { 1.0000000, 2.0000000, 3.0000000 }
25
26
npy = numpy.asarray(rvec)
27
print(npy) # [1. 2. 3.]
28
29
rvec[0] = 42
30
print(npy) # [42. 2. 3.]
31
\endcode
32
33
### Conversion of Numpy arrays to RVecs
34
35
Data owned by Numpy arrays with fundamental types (int, float, ...) can be adopted by RVecs. To
36
create an RVec from a Numpy array, ROOT offers the facility ROOT.VecOps.AsRVec, which performs
37
a similar operation to `numpy.asarray`, but vice versa. A code example demonstrating the feature and
38
the adoption of the data owned by the Numpy array is shown below.
39
40
\code{.py}
41
npy = numpy.array([1.0, 2.0, 3.0])
42
print(npy) # [1. 2. 3.]
43
44
rvec = ROOT.VecOps.AsRVec(npy)
45
print(rvec) # { 1.0000000, 2.0000000, 3.0000000 }
46
47
npy[0] = 42
48
print(rvec) # { 42.000000, 2.0000000, 3.0000000 }
49
\endcode
50
51
\htmlonly
52
</div>
53
\endhtmlonly
54
*/
v632_TMP
pyzdoc
_rvec.pyzdoc
ROOT v6-32 - Reference Guide Generated on Thu Dec 12 2024 15:06:30 (GVA Time) using Doxygen 1.9.8