--- myst: substitutions: bloc1: python: 4-7 cpp: 4-7 bloc2: python: 10-17 cpp: 11-16 dedent: python: ":dedent:\n" cpp: "" --- # Adding {{ tattr }} when data are already available It is possible to add new attribute in a given `TDataServer` object that would contain data using two different methods. The first one rely on the already existing data to create a new variable by simply writting the equation, which internally is calling a `TAttributeFormula` object. The following piece of code shows how to create a third variable from the `geyser.dat` file, simply as an equation from the existing variables: {{ "```{" "literalinclude" "} " + parent_dir + "/roottest/uranie/doc/dataserver/attribute/" + language + "/adding_attribute_when_data_available." + extension + "\n" + ":language: " + language + "\n" + ":lines: " + bloc1[language] + "\n" + dedent[language] + "```" }} This method can deal with double and vector-based attributes (of course no equation can be estimate when one of the input variable in the formula is a string one, so this method will crash). Another way recently introduced is to add an attribute from an array of double (or it's equivalent in `python`, meaning a `numpy.array`) by calling the method `addAttributeUsingData`. The idea is to be able to add information that would have been processed by methods aside from the {{uranie}} ones. The signature of the function is the name of the new attribute as first argumet, the second one is an array of double and the third one is the size of the array. There are two ways, recommended, that uses object with built-in method that provide the size (to prevent from mis-typing problem between the array and its size). Obviously, the size of the array must be equal to the number of patterns in the existing `TDataServer` object. Here is an example using the `myData.dat`, in C++: {{ "```{" "literalinclude" "} " + parent_dir + "/roottest/uranie/doc/dataserver/attribute/" + language + "/adding_attribute_when_data_available." + extension + "\n" + ":language: " + language + "\n" + ":lines: " + bloc2[language] + "\n" + dedent[language] + "```" }} This method should only be used to create double-based attributes (as the size of the array would be chaotic if it were to be a vector of varying size). Obviously, no string-based attribute can be constructed like this. ```{warning} The method `addAttributeUsingData` should only be called either when no data AND no attribute are stored in the dataserver or when there are data and the new array of double provided has the same size (number of patterns) as the data already available within the dataserver. ```