Reading vectors of vectors from flat trees

I’m trying to pass information (stored as a c++ vector<vector > from one program to another in the form of a flat tree in a .root file. My current reading code looks something like this:

  TFile* inFile = new TFile("path_to_file/file.root");
  TTree* storageTree = (TTree*)inFile->Get("flatTree"); 

  vector<double> hitCoords;
  vector<vector<double> > track;

  int nhits = storageTree->GetEntries();

  for(int i =0 ; i < nhits ; i++)
    {
      storageTree->GetEntry(i);
         for(vector<vector<double> >::iterator it = track.begin(); it != track.end(); ++it)
	{ 
            for(vector<double>::iterator hitit = it->begin(); hitit != it->end(); ++hitit)
	    {
              	      if(*hitit ==  *(it->begin()) )
		{
		  z = * hitit;
		  intZ =  (int)z;
		}
	      if(*hitit == (it->back()) )
		{
		  xy = * hitit;
		}
             }
        }
    }

I’m not sure I put the brackets in when I copied this, but they are all in place in the code. Is there something obvious I’ve done wrong, I can provide more code if this lot isn’t sufficient.

Hello,

You need to set the branch address. Something like

 // Declaration of leaf type
 std::vector<std::vector<double> > *fTrack = 0;
 TBranch        *b_Track = 0;
 // NB: In the following replace "track" with the real branch name
 storageTree->SetBranchAddress("track", &fTrack, &b_Track);

 // Then
 for(std::vector<std::vector<double> >::iterator it = fTrack->begin(); it != fTrack->end(); ++it) {
     ...

If you use ROOT v6, you can also use TTreeReader: see https://root.cern.ch/doc/master/classTTreeReader.html .

G Ganis

Thanks for getting back to me, that seems to have solved it. I didn’t know about TTreeReader, that might be worth a look.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.