# Shortcuts to add array elements with TMVA::DataLoader::AddVariable()

**URL:** <https://root-forum.cern.ch/t/shortcuts-to-add-array-elements-with-tmva-addvariable/30176>\
**Category:** TMVA\
**Created:** [August 8, 2018, 9:18pm UTC](https://root-forum.cern.ch/t/shortcuts-to-add-array-elements-with-tmva-addvariable/30176 "2018-08-08T21:18:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jwruss](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@jwruss](https://root-forum.cern.ch/u/jwruss)\
**Post date:** [August 8, 2018, 9:18pm UTC](https://root-forum.cern.ch/t/shortcuts-to-add-array-elements-with-tmva-addvariable/30176/1 "2018-08-08T21:18:30Z")

</div>

I have trees containing 4 leaves of length 10 double arrays. I would like to prepare a sequence of TMVA::DataLoader objects with each array’s elements as individual variables. If a given DataLoader object contains elements from 1 of these 4 arrays, it must contain all 10 elements from that array. This means AddVariable() will be used to add a minimum of 10 elements and a maximum of 40 elements. Calling these arrays `arrA[10], arrB[10], arrC[10], arrD[10]`, with a DataLoader `dlA` which is to contain `arrA` and a DataLoader `dlAB` containing both `arrA` and `arrB`, I can certainly do

```auto
for (int idx = 0; idx < 10; ++idx) {
    TString arrName;
    arrName.Form("arrA[%d]", idx);
    dlA -> AddVariable(arrName);
    dlAB -> AddVariable(arrName);
    arrName.Form("arrB[%d]", idx);
    dlAB -> AddVariable(arrName);
}

```

or I could just do

```auto
dlA -> AddVariable("arrA[10]");
dlAB -> AddVariable("arrA[10]");
dlAB -> AddVariable("arrB[10]");

```

in the hope that each `arrA` and `arrB` element will still implicitly get an individual coefficient. I don’t think this works, however, so I was wondering if there are any conventional ways to do this in TMVA, rather than with the above for loop? In particular, the following [link](https://sft.its.cern.ch/jira/browse/ROOT-5346) makes me think the same can be achieved with

```auto
dlA -> AddSpectator("idx");
dlA -> AddVariable("arrA[idx]");

```

but `idx` isn’t in the trees by default, or perhaps the `max, min` optional arguements inside of AddVariable() and AddSpectator() can be used to set array indices? The purpose of `max, min` aren’t apparent to me from the [DataLoader class reference page](https://root.cern.ch/doc/v608/classTMVA_1_1DataLoader.html).

---

<div class="post-metadata">

**Author:** ![kialbert](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kialbert/32/16537_2.png) [@kialbert](https://root-forum.cern.ch/u/kialbert)\
**Post date:** [August 9, 2018, 1:17pm UTC](https://root-forum.cern.ch/t/shortcuts-to-add-array-elements-with-tmva-addvariable/30176/2 "2018-08-09T13:17:22Z")

</div>

Hi,

Your alternative 1 is the way to go if you want to add each entry in the array as a separate feature. I cannot think of a way of doing it more compact or “easier”. The `min` and `max` arguments represent the minimum and maximum extent of that variable.

The 2nd approach adds only the last element of the array as a features. There is a related syntax `dlA->AddVariable("arrA")` that adds only a single variable (`arrA`) and creates 10 events from that array, however it is my understanding that this is not what you are looking for.

Finally the third approach will not work (a) because of the reason you state, not being in the tree, and (b) because `idx` would take a single value for each event. The result would be much like alt 2 but with a variable index.

I hope that clears things up and good luck!

Cheers,  
Kim

---

<div class="post-metadata">

**Author:** ![jwruss](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@jwruss](https://root-forum.cern.ch/u/jwruss)\
**Post date:** [August 10, 2018, 7:53pm UTC](https://root-forum.cern.ch/t/shortcuts-to-add-array-elements-with-tmva-addvariable/30176/3 "2018-08-10T19:53:28Z")

</div>

Thank you for clearing things up!

Best,  
John
