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

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

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

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 makes me think the same can be achieved with

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.


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!


Thank you for clearing things up!