13.2.1. Macro “howtoConvertTDataServerArray.py”
13.2.1.1. Objective
The goal of this macro is only to show how to convert a TDataServer into a numpy.array and vice-versa.
13.2.1.2. Macro Uranie
"""
Example of TDataServer into numpy array converter
"""
import numpy as np
from URANIE import DataServer
# import data into a dataserver
tds = DataServer.TDataServer("tds", "pouet")
tds.fileDataRead("myData.dat")
print("Dumping tds")
tds.scan()
# Define the list of variable to be read
VarList = "x:y"
# Create an array by defining the shape and the type (this array is a matrix)
TdsData = np.empty(shape=(tds.getNPatterns(), len(VarList.split(":"))),
dtype=np.float64)
# Dump the data into the created array (the last field empty unless a select is done on the sample)
tds.getTuple().extractData(TdsData, TdsData.size, VarList, tds.getCut().GetTitle())
# Check the content
print("Dumping the array")
print(TdsData)
# Create a new Dataserver and feed it
tds2 = DataServer.TDataServer("brandnew", "pouet")
# Add the attribute in the dataserver and create the tuple
for name in VarList.split(":"):
index = VarList.split(":").index(name)
VarData = np.ascontiguousarray(TdsData.transpose()[index])
tds2.addAttributeUsingData(name, VarData, VarData.size)
print("Dumping new tds")
tds2.Scan("*")
The first block reads an input file myData.dat and store the data in the TDataServer as usually done.
The new block is what we recommend: define the list of variable that one wants to store
VarList = "x:y"
Once done, the np.array has to be constructed by defining its content and size. Thanks to the
TDataServer object and the list of variable this can be done easily as
TdsData = np.empty(shape=(tds.getNPatterns(), len(VarList.split(":"))),
dtype=np.float64)
Finally, the extractData method is called with the given created array and list of variables.
tds.getTuple().extractData(TdsData, TdsData.size, VarList, tds.getCut().GetTitle())
The second part consists in creating a brand new TDataServer from a np.array, which basically will
consist in our case in re-creatting the original TDataServer with a different name and title. To do so,
one starts by creating the TDataServer
tds2 = DataServer.TDataServer("brandnew", "pouet")
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
addAttributeUsingDatathat needs the name of the attribute, the buffer and the size of the array provded
for name in VarList.split(":"):
index = VarList.split(":").index(name)
VarData = np.ascontiguousarray(TdsData.transpose()[index])
tds2.addAttributeUsingData(name, VarData, VarData.size)
The output of this macro is shown in Console.
13.2.1.3. Console
--- Uranie v4.11/0 --- Developed with ROOT (6.36.06)
Copyright (C) 2013-2026 CEA/DES
Contact: support-uranie@cea.fr
Date: Thu Feb 12, 2026
Dumping tds
************************************************
* Row * tds__n__i * x.x * y.y *
************************************************
* 0 * 1 * -5 * 25 *
* 1 * 2 * -4 * 16 *
* 2 * 3 * -3 * 9 *
* 3 * 4 * -2 * 4 *
* 4 * 5 * -1 * 1 *
* 5 * 6 * 0 * 0 *
* 6 * 7 * 1 * 1 *
* 7 * 8 * 2 * 4 *
* 8 * 9 * 3 * 9 *
* 9 * 10 * 4 * 16 *
* 10 * 11 * 5 * 25 *
************************************************
Dumping the array
[[-5. 25.]
[-4. 16.]
[-3. 9.]
[-2. 4.]
[-1. 1.]
[ 0. 0.]
[ 1. 1.]
[ 2. 4.]
[ 3. 9.]
[ 4. 16.]
[ 5. 25.]]
Dumping new tds
************************************************
* Row * brandnew_ * x.x * y.y *
************************************************
* 0 * 1 * -5 * 25 *
* 1 * 2 * -4 * 16 *
* 2 * 3 * -3 * 9 *
* 3 * 4 * -2 * 4 *
* 4 * 5 * -1 * 1 *
* 5 * 6 * 0 * 0 *
* 6 * 7 * 1 * 1 *
* 7 * 8 * 2 * 4 *
* 8 * 9 * 3 * 9 *
* 9 * 10 * 4 * 16 *
* 10 * 11 * 5 * 25 *
************************************************