Filling Tree with an array float

Dear experts

I am trying to build and use an float array and use it/fill the tree. I need to have something like

So, how can I use and fill a TTree ?

I am using something like

and then

for (int j=0;j<nmax;j++){ 
...do something with "j" 
XELDET0[j] = function()

but at the end, the XELDET0 has nmax entries always at zero.

Thanks

Alex

Try something like this (see, for example, the TTree::Branch method description): tree.Branch("XELDET0", &XELDET0[0], TString::Format("XELDET0[%i]/F", nmax)); // a fixed size array

Nop, this does not work. Moreover, I see now my distribution again filled with zeros, but even more entries than [nmax]!

[quote=“Wile E. Coyote”]Try something like this (see, for example, the TTree::Branch method description): tree.Branch("XELDET0", &XELDET0[0], TString::Format("XELDET0[%i]/F", nmax)); // a fixed size array[/quote]
Ok - this “partially” works - I get the right distribution with also a “huge” spike on zero. Further, the entries appear to be 40k while should be 29k

If however I try

tree.Branch("XELDET0",            &XELDET0[0],           "XELDET0[nmax]/F");

then this looks ok.
thanks

The “XELDET0[nmax]/F” denotes a “variable size” array (and so, in advance of the “XELDET0” branch, you need an additional branch which keeps the “nmax” leaf).

Yes, that was already the case :wink: but still does not behave as expected…

In this case you should have something like (note: the address of XELDET0[0] must not change after you assigned it to a branch): const unsigned int kMax_nmax = some_value; // always make sure that nmax <= KMax_nmax const unsigned int kMax_K = some_another_value; // always make sure that K <= KMax_K float XELDETn[kMax_nmax][kMax_K], XELDET0[kMax_nmax];

ok - And how could I recursively create the trees? I ve tried something like

char tree_[20]; for (int j=0;j<2;j++){ sprintf(tree_,"XELDET%i[%i]",j); tree.Branch(TString::Format("XELDET%i",j), &tree_, TString::Format("XELDET%i[NII]/F", j)); }

which compiles but is not filled properly

Thanks again

The second argument in the “tree.Branch” call should be a pointer to the actual data, not to a string with a “name”.
See the TTree class description for what “cases” are available.

BTW. sprintf(tree_,“XELDET%i[%i]”,j); should generate a warning that the format string expects more matching arguments.

Ok thanks!

I am also trying to do something like when filling them

for (unsigned int n=0;n<elements;n++){
     TString::Format("XELDET%i",n) = spectrometer.fill(energy, THELEC,PHELEC,XELEC);
       }

but this fails…How can I call & fill recursively the trees of the form XELDET0[Nmax], XELDET1[Nmax] etc ?

Thanks

After you created the “tree” and added appropriate branches, for each “event”, simply assign values to all relevant variables (i.e. all XELDET0[0], …, XELDET0[nmax - 1]) and then call “tree.Fill()”.

No, I mean that I want to “fill” the branches in a loop, as the number of elements can change . So, I dont want to hardcode XELDET0[], XELDET1[] (as I might now know in advance how many elements will be used), rather than call & fill the trees whose name is following the XELDET0,XELDET1…XELDETelements using TString:Format (or similar) workflow like I described in my last msg.

${ROOTSYS}/tutorials/tree/tree2.C
${ROOTSYS}/tutorials/tree/tree3.C
[url=https://root-forum.cern.ch/t/data-structure-question-storing-data-per-event/18376/1 Structure Question - Storing Data per Event[/url]