(use_cases_howto_py_tds_array_py)= # Macro **"howtoConvertTDataServerArray.py"** ## Objective The goal of this macro is only to show how to convert a `TDataServer` into a `numpy.array` and vice-versa. ## Macro {{uranie}} {{ "```{literalinclude} " + parent_dir + "/roottest/uranie/doc/overview/use_cases/python/howtoConvertTDataServerArray.py\n" + ":language: python\n" + ":lines: 1-5,11-40\n" + "```" }} The first block reads an input file `myData.dat` and store the data in the {{tds}} as usually done. The new block is what we recommend: define the list of variable that one wants to store {{ "```{literalinclude} " + parent_dir + "/roottest/uranie/doc/overview/use_cases/python/howtoConvertTDataServerArray.py\n" + ":language: python\n" + ":lines: 19\n" + "```" }} Once done, the `np.array` has to be constructed by defining its content and size. Thanks to the {{tds}} object and the list of variable this can be done easily as {{ "```{literalinclude} " + parent_dir + "/roottest/uranie/doc/overview/use_cases/python/howtoConvertTDataServerArray.py\n" + ":language: python\n" + ":lines: 22-23\n" + "```" }} Finally, the `extractData` method is called with the given created array and list of variables. {{ "```{literalinclude} " + parent_dir + "/roottest/uranie/doc/overview/use_cases/python/howtoConvertTDataServerArray.py\n" + ":language: python\n" + ":lines: 25\n" + "```" }} The second part consists in creating a brand new {{tds}} from a `np.array`, which basically will consist in our case in re-creatting the original {{tds}} with a different name and title. To do so, one starts by creating the {{tds}} {{ "```{literalinclude} " + parent_dir + "/roottest/uranie/doc/overview/use_cases/python/howtoConvertTDataServerArray.py\n" + ":language: python\n" + ":lines: 32\n" + "```" }} Then, one loops over the list of variable and in the loop two things are done : - a new array is created by transposing the data, selecting the proper array of data and construct this as an memory-continuous array (for C++-consistency) - a new attribute is created using the method `addAttributeUsingData` that needs the name of the attribute, the buffer and the size of the array provded {{ "```{literalinclude} " + parent_dir + "/roottest/uranie/doc/overview/use_cases/python/howtoConvertTDataServerArray.py\n" + ":language: python\n" + ":lines: 34-37\n" + "```" }} The output of this macro is shown in [](#use_cases_howto_py_tds_array_py_console). (use_cases_howto_py_tds_array_py_console)= ## Console {{ "```{literalinclude} " + parent_dir + "/roottest/build/uranie/doc/overview/use_cases/python/howtoConvertTDataServerArray_clean.log\n" + ":language: none\n" + ":lines: 2-34,36-51\n" + "```" }}