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

**URL:** <https://root-forum.cern.ch/t/accessing-information-stored-by-a-vector-of-a-struct-in-a-tree/43690>\
**Category:** Newbie\
**Tags:** tree\
**Created:** [February 24, 2021, 4:24pm UTC](https://root-forum.cern.ch/t/accessing-information-stored-by-a-vector-of-a-struct-in-a-tree/43690 "2021-02-24T16:24:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joanna-Gao](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/joanna-gao/32/21539_2.png) [@Joanna-Gao](https://root-forum.cern.ch/u/Joanna-Gao)\
**Post date:** [February 24, 2021, 4:24pm UTC](https://root-forum.cern.ch/t/accessing-information-stored-by-a-vector-of-a-struct-in-a-tree/43690/1 "2021-02-24T16:24:45Z")

</div>

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:

```auto
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:

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

```

When I was storing it I did:

```auto
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:

 ![Screenshot 2021-02-24 at 14.56.23](https://root-forum.cern.ch/uploads/default/original/3X/6/0/60df194d2b6c8a84e766364739bf7b0327ddb05b.png)

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

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [February 24, 2021, 4:53pm UTC](https://root-forum.cern.ch/t/accessing-information-stored-by-a-vector-of-a-struct-in-a-tree/43690/2 "2021-02-24T16:53:36Z")

</div>

> [@Joanna-Gao](#):
>
> but then root is just stuck without doing anything (as far as I can see) after printing it.

This is likely due to:

```auto
  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

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

```

---

<div class="post-metadata">

**Author:** ![Joanna-Gao](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/joanna-gao/32/21539_2.png) [@Joanna-Gao](https://root-forum.cern.ch/u/Joanna-Gao)\
**Post date:** [February 25, 2021, 4:29pm UTC](https://root-forum.cern.ch/t/accessing-information-stored-by-a-vector-of-a-struct-in-a-tree/43690/4 "2021-02-25T16:29:17Z")

</div>

It works! Thank you so much!!
