TTree Branch Leaf to hold a structure of arrays

Hi all,

I have a really strange feature with branch leafs for structure of arrays.
I define a strucuture of arrays like this

namespace ExoDiPhotons
{

struct PFCandidateInfo_t {
Int_t numPFCands;
Double_t pt[2000];
Double_t energy[2000];
Double_t eta[2000];
Double_t phi[2000];
Int_t status[2000];
Int_t pdgid[2000];
// position in ECAL - caloPosition;

};

and create a branch to hold it like this

ExoDiPhotons::PFCandidateInfo_t fPFCandidateInfo;
fTreeAll->Branch(“PFCandidate”,&fPFCandidateInfo,ExoDiPhotons::recoPFCandidateBranchDefString.c_str());

But then during the tree filling, something happens. Very weird values are stored in.

root [2] fTreeAll->Scan(“PFCandidate.pt”)


  • Row * Instance * PFCandida *

  •    0 *        0 * -2.6e+154 *
    
  •    0 *        1 * 5.33e-315 *
    
  •    0 *        2 * -2.6e+154 *
    
  •    0 *        3 * 2.0000023 *
    
  •    0 *        4 * 1.90e-192 *
    
  •    0 *        5 * 1.25e+196 *
    
  •    0 *        6 * -1.4e-154 *
    
  •    0 *        7 * 1.9999992 *
    
  •    0 *        8 * 3.29e+222 *
    
  •    0 *        9 * -1.4e-154 *
    
  •    0 *       10 * -1.283718 *
    
  •    0 *       11 * 9.33e+251 *
    
  •    0 *       12 * -1.4e-154 *
    
  •    0 *       13 * -6.54e-54 *
    
  •    0 *       14 * 1.9999992 *
    
  •    0 *       15 * -1.4e-154 *
    
  •    0 *       16 * -2.000000 *
    
  •    0 *       17 * -5.3e-315 *
    
  •    0 *       18 * -3.63e-21 *
    
  •    0 *       19 * 1.49e-154 *
    
  •    0 *       20 * 2.0000004 *
    
  •    0 *       21 * -5.3e-315 *
    
  •    0 *       22 *       nan *
    
  •    0 *       23 * 1.87e+290 *
    
  •    0 *       24 * 2.68e+154 *
    

Type to continue or q to quit ==> q


(Long64_t)25
root [3]

Strangely enough, if I remove the declaration of numPFCands from the structure, it works just fine.

Does anyone have any idea ???

Try to move “Int_t numPFCands;” to the last position in your structure (i.e. below “Int_t pdgid[2000];”).
If it helps, see [url]TTree with ‘C struct’

Thanks very much for this.
That is indeed something I had in mind as a possibility.

The reason why I actually put it first is because I want to use later this variable (numPFCands) to declare the followings arrays with variable lengths based on the value of numPFCands.

So, while I am testing what you suggested, I may ask the following question :
“Is it possible to have variable length arrays in the structure ??”

If not, I would be really grateful to you showing me how to create a dictionary and library and how that works, i.e. do i need to create it each time i run my executable or once for all ???

Thanks a lot.

See, for example, http://root.cern.ch/drupal/content/interacting-shared-libraries-rootcint and http://root.cern.ch/download/doc/ROOTUsersGuideHTML/ch15s05.html and http://root.cern.ch/download/doc/ROOTUsersGuideHTML/ch15s06.html

See also “Adding a Branch with a Fixed Length Array” and “Adding a Branch with a Variable Length Array” in http://root.cern.ch/download/doc/ROOTUsersGuideHTML/ch12s15.html
Search also for “array” in http://root.cern.ch/root/html/TTree.html#TTree:Branch@3
See also http://root.cern.ch/download/doc/ROOTUsersGuideHTML/ch11s03.html#d5e12547

Hi all,

thank you very much for this. I know however how to create fxed and variable length size arrays in general. My question was more related to the structures. Is it possible to have branches that can hold structures of variable size arrays? If yes, do I necessarily have to declare numPFCands at the beginning of the structure or can i put it anywhere ???

Thanks a lot for this.

Hi,

Having variable size array in the middle of a leaflist is not supported. You can either create a separate branch for each of the arrays (they still can be inside the same structure though), or you go via the object-method of branch creation (in which case you only need to generate the dictionary for the struct.

In all the cases, the member determining the size of the array should come before the arrays.

Cheers,
Philippe.

Just to make it clear. You don’t really expect this to be working (no matter which “method” one chooses), do you: struct MyStruct { int n; double x[n]; };
Do you expect this makes any sense for the streamer (in the “object-method of branch creation” case): struct MyStruct { int n; double x[1000]; //[n] };

Yes, thank you pcanal. I knew that the size determining the array length had to come first in the leaflist for “normal” variable length arrays but in structures, I thought that it could go anywhere, hence my question Pepe le Pew but i just tried and it did not work.
I guess I’m gonna have to generate dictionary then.

Thanks again.