Accessing variable length arrays in a Tree

I have a Tree that contains a series of events. In each event I have several candidate particles and I store the physics parameters for each particle in a variable length array. For each event the variable length arrays are the same length but they can be different between events. Unfortunately I didn’t put anything in to count the number of candidates per event.

Up until now this has caused no problem because if I want to plot a histogram I can do:

TFile *f = new TFile(“BsJpsiphi.root”);
TTree t = (TTree)f->Get(“CollectionTree”);
t->Draw(“BsMass”, “BsPt > 10000 && BsChi2 < 6”);
// BsMass, BsPt and BsChi2 are all variable length arrays for each event

This will plot all the candidates that pass the cuts. However what I now want to do is plot the candidate with the lowest Chi2 for each event (as long as it meets the other cuts). There might be some fancy ROOT class that already does this but I haven’t found it!

The only way I can think of doing this is to access the events individually using getEntry and then sort them manually myself before filling a histogram. So far I have been unable to use the code below (which I took straight from the manual) because BsMass is an array of floats not just one.

Int_t nentries = (Int_t)t->GetEntries();
Float_t BsMass;
t->SetBranchAddress(“BsMass”, &BsMass);

for (int i = 0; i<nentries; i++) {
cout << "Bs Mass = " << BsMass << endl;

I hope this makes sense. Thanks for any help in advance.

I do not understand how you want to compute the minimal element of an array if you do not have the array length.
Could you explain how you created the Tree?


The short answer is that the software that creates the Tree was written by someone else so I don’t really understand whats happening beyond what I described in my first post. I simply store my candidate particles in a vector and it places them in a variable length array for me! :frowning:

I can however add a branch which counts the size of each array easily. How would I proceed from there?

In order to write your variable length array, you had to specify somewhere the length of the array, otherwise the array could not have been written.
Could you send the output of tree.Print() and indicate which branches are yours?
You can also use TTree::MakeClass or TTree::MakeSelector to automatically generate a skeleton C++ analysis code, eg
will generate two files Anal.h and Anal.C. You can modify Anal.C (see comments in the generated code to see how to use it).


Thanks Rene
It turns out they were vectors rather than variable length arrays. I used the MakeClass thing you suggested and it worked fine.