--- myst: substitutions: bloc: python: 3-7 cpp: 3,6-9 bloc2: python: 15-16,20-23 cpp: 4,12-16 bloc3: python: 1-4,6-21 cpp: 3,6-18 console: python: 7-19 cpp: 9-21 --- (dataserver_data_add_attributes_tds)= # Adding attributes to a {{tds}} Attributes can always be added to an existing {{tds}} object, whether it is *empty* (just after its constructor) or *not* (after the data loading from either an ASCII file, or a TTree or a database of type **SQL**). A simple example is provided and decomposed in [](#use_cases_macro_dataserver_attributes). First of all, let us consider the case of an empty {{tds}}. We add attributes using the method `addAttribute`( **TAttribute** \*att). {{ "```{literalinclude} " + parent_dir + "/roottest/uranie/doc/dataserver/data/" + language + "/add_attribute_tds." + extension + "\n" + ":language: " + language + "\n" + ":lines: " + bloc[language] + "\n" + "```" }} **Description of attributes adding to an empty TDataServer** 1. Adding a new attribute **x1** to the TDataServer from a `TAttribute` with a name (minimal constructor). 2. Adding a new attribute **x2** to the TDataServer from a `TAttribute` with a name and the extreme values (minimal and maximal). 3. Equivalent to previous one: adding a new attribute **x3** to the TDataServer just by giving its name (minimal constructor). 4. Equivalent to previous one: adding a new attribute **x4** to the TDataServer by giving its name and the extreme values (minimal and maximal). The difference between the methods with new in it and the others, is basically arising from the way one handles the memory. The last ones (3 and 4) allow the user not to worry about anything, while, in the case of implementation 1 and 2, one should be aware that every `new` should imply at some point a `delete`. For most user, this is not of utmost importance as usual scripts would contain very few `new` command and no loop. If this is not the case (for instance if one does have loop and many object creation in it) do not hesitate to contact the {{uranie}}-team to prevent any slowing down of the code. The specification of a `TAttribute` is further detailed in [](#dataserver_attribute). We can define new attributes using mathematical expressions with respect to other existing attributes. The name and the mathematical expression are the only mandatory arguments; its title and unit can also be precised, but both arguments are optional. {{ "```{literalinclude} " + parent_dir + "/roottest/uranie/doc/dataserver/data/" + language + "/draw_geyser_scatterplot_attributes." + extension + "\n" + ":language: " + language + "\n" + ":lines: " + bloc2[language] + "\n" + "```" }} **Description attribute adding to a TDataServer from formulas** 1. Adding a new attribute **cd1** to the TDataServer defined by a mathematical expression as a function of *x1* and *x2* attributes: ```{math} cd1 \; = \; x_1 * \sqrt{x_2} ``` 2. Adding a new attribute **cd2** to the TDataServer with a mathematical formula, precising its title and unit: ```{math} cd2 \; = \; \sqrt{x_2 * x_1} ``` 3. Plots the scatterplot of the variable **cd2** versus the variable **cd1** The obtained graph is: {{ "```{" "figure" "} " + parent_dir + "/roottest/build/uranie/doc/dataserver/data/" + language + "/geyser_1/geyser_scatterplot_attributes.png\n" ":align: center\n" ":name: dataserver_geyser_scatterplot_attributes\n" + figure_scale + "\n" "\n" 'Scatterplot of added attributes\n' "```" }} This operation is available with vector-type attribute as well. The results depends on the nature of the attributes involved in the formula, their content and the nature of the operation. As an example, a simple dataserver is created from the dummy file `tdstest.dat`: {{ "```{literalinclude} " + parent_dir + "/roottest/uranie/references/dataserver/tdstest.dat\n" + ":language: none\n" + "```" }} It contains two vectors whose size are not constant and a double. The four usual operations have been performed (addition, subtraction, multiplication and division) using the double and a vector but also using the two vectors. The code is shown here: {{ "```{literalinclude} " + parent_dir + "/roottest/uranie/doc/dataserver/data/" + language + "/dataserverAddAttribute." + extension + "\n" + ":language: " + language + "\n" + ":lines: " + bloc3[language] + "\n" + "```" }} and it gives as a results: {{ "```{literalinclude} " + parent_dir + "/roottest/build/uranie/doc/dataserver/data/" + language + "/dataserverAddAttribute.log\n" + ":language: none\n" + ":lines: " + console[language] + "\n" + "```" }} ```{admonition} Summary: Adding attributes Two kinds of method, allow to add an attribute to a {{tds}}: 1. With attribute properties: `addAttribute` ( **TAttribute** \*att) / `addAttribute` ( **TString** name) The pointer `att` is either of `TAttribute` type or a derived class `TAttributeFormula` or `TStochasticAttribute` type. 2. By means of other existing attributes: `addAttribute` ( **TString** name, **TString** formula, **TString** label="", **TString** unity="" ) Adding an attribute by specifying its name and its mathematical formula (these two arguments are mandatory), and its title and its unit (optional arguments). A warning has been added if formula is requested using a string-type branch. In the case of vector, the behaviour should depend on the nature of the branches in the formula. ```