Accessing information stored by a vector of a struct in a tree

Hello,

I’m trying to extract the information stored in the format of vector<struct> in a tree for some plotting, but my macro file gets stuck after I call tree->GetEntry(ievt). I’m wondering if anyone knows what to do or how to debug it. The following code is the main function in my macro:

void SelectBarrel(const char *openFile = "")
{
  gROOT->ProcessLine(".L loader.c+");

  TFile* file = new TFile(openFile, "READ");
  TTree* tree = (TTree*) file->Get("tree");
  vector<SeedInfo>* seedinfo_vec;
  tree->SetBranchAddress("seedinfo", &seedinfo_vec);

  int nevt = tree->GetEntries();
  cout << nevt << endl;
  
  // loop through all the clusters to select the ones with -1000 < z < 1000
  for (int ievt = 0; ievt < 2; ievt++)
  {
    cout << ievt << endl;
    tree->GetEntry(ievt);
    cout << ievt << endl;
    for (vector<SeedInfo>::iterator info = seedinfo_vec->begin();
         info != seedinfo_vec->end(); ++info)
    {
     cout << "Success" << endl;
     // int clusterCount = info->count;
     // cout << clusterCount << endl;
    }
  }
}

The print before tree->GetEntry(ievt); gives 0, but then root is just stuck without doing anything (as far as I can see) after printing it.

Following is some supplement information:

The struct I used is defined as:

struct SeedInfo
{
    int count;
    double x;
    double y;
    double z;
    double t;
    double q;
    double tot_q;
};

When I was storing it I did:

std::vector<SeedInfo> seedinfo_vec;
tree->Branch("seedinfo",&seedinfo_vec);

// skip to the interesting part
while(std::getline(sInFile, line))
{
    // all members of SeedInfo are assigned values
    seedinfo_vec.push_back({count, x, y, z, t, q, tot_q});

    // storing the information for 1 event
    if (canStore)
    {
        tree->Fill();
        seedinfo_vec->clear();
    }
}

The information in the tree looks like this:

I’m aware there might be some issue with using GetEntry() on information stored as vector<struct> because when I did tree->GetEntry(0) in the root program it says (int) 144, which doesn’t make sense because I don’t have 144 entries for event 0.

Does anyone know what’s wrong with my method or how I can debug this?

Many thanks,

Joanna

ROOT Version: 6.22.02
Platform: Mac OS 10.15.7

This is likely due to:

  vector<SeedInfo>* seedinfo_vec;
  tree->SetBranchAddress("seedinfo", &seedinfo_vec);

which indirectly tells the TTree to use a random address to store the data since the pointer seedinfo_vec is uninitialized.

Try

  vector<SeedInfo>* seedinfo_vec = nullptr;
  tree->SetBranchAddress("seedinfo", &seedinfo_vec);
1 Like

It works! Thank you so much!!