(use_cases_howto_py_tmatrixd_array_py)= # Macro **"howtoConvertTMatrixDArray.py"** ## Objective The goal of this macro is only to show how to convert a `TMatrixD` object into a `numpy.array` and vice-versa. ## Macro {{uranie}} {{ "```{literalinclude} " + parent_dir + "/roottest/uranie/doc/overview/use_cases/python/howtoConvertTMatrixDArray.py\n" + ":language: python\n" + ":lines: 1-5,11-35\n" + "```" }} The first block simply defines the size of the matrix to be created and creates along, from scratch, a `TMatrixD` with a random gaussian content. Once done, the conversion into a `np.array` is done in a single line manner below that would be breakdown to get the full picture: {{ "```{literalinclude} " + parent_dir + "/roottest/uranie/doc/overview/use_cases/python/howtoConvertTMatrixDArray.py\n" + ":language: python\n" + ":lines: 26-27\n" + "```" }} The content of the original `TMatrixD` can be accessed by calling `GetMatrixArray()`. This returns a pointer to the first element's address, so it can be used by the lowest level array object of numpy, the `np.frombuffer` which also needs the type of data and the number of elements. This is important as the memory will be scanned given these two information. The last step is to call, on this newly created object the `reshape` method to create a properly organised `np.array`. The second part consists in creating a brand new `TMatrixD` from the newly created `np.array`. The idea is simply to create the object empty and use the `SetMatrixArray` method to dump the array content into the matrix, as done below. {{ "```{literalinclude} " + parent_dir + "/roottest/uranie/doc/overview/use_cases/python/howtoConvertTMatrixDArray.py\n" + ":language: python\n" + ":lines: 32-33\n" + "```" }} The output of this macro is shown in [](#use_cases_howto_py_tmatrixd_array_py_console). (use_cases_howto_py_tmatrixd_array_py_console)= ## Console {{ "```{literalinclude} " + parent_dir + "/roottest/build/uranie/doc/overview/use_cases/python/howtoConvertTMatrixDArray.log\n" + ":language: none\n" + "```" }}