# TTree Branch Filling

**URL:** <https://root-forum.cern.ch/t/ttree-branch-filling/58574>\
**Category:** Newbie\
**Created:** [March 18, 2024, 9:21am UTC](https://root-forum.cern.ch/t/ttree-branch-filling/58574 "2024-03-18T09:21:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Manisha\_Rana](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/manisha_rana/32/34574_2.png) [@Manisha\_Rana](https://root-forum.cern.ch/u/Manisha_Rana)\
**Post date:** [March 18, 2024, 9:21am UTC](https://root-forum.cern.ch/t/ttree-branch-filling/58574/1 "2024-03-18T09:21:11Z")

</div>

Hi, I am trying to fill individual Branchs of a Tree using Struct .  
but it is not working.

```auto
struct TrackParameter{
  float m;
  float px;
  float py;
  float pz;
  float en;
  int pid;
  int charge;
} ;
     
struct EventParameter {
  int esize;
  int eid;
};

```

```auto
TTree *eventData = new TTree("eventData","Tree for event parameters data");
TBranch *eventP = eventData->Branch("eventP",&eventParameter,"eid/I:esize/I");
TBranch *trackP=eventData->Branch("trackP",&trackParameter,"m/F:px/F:py/F:pz/F:en/F:pid/I:charge/I");
eventP->Fill();
trackP->Fill();

```

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [March 18, 2024, 10:37am UTC](https://root-forum.cern.ch/t/ttree-branch-filling/58574/2 "2024-03-18T10:37:09Z")

</div>

You have to instantiate (“create objects from”) the structures and associate those instances to the Branches. And then assign values to the variables of those instances before filling the branches. E.g.

```auto
  //... struct...
  TrackParamater tp;
  EventParameter ep;
  //...
  eventData->Branch("eventP",&ep.esize,"esize/I:eid/I"); // same order as in the struct!
  eventData->Branch("trackP",&tp.m,"m/F:px/F:py/F:pz/F:en/F:pid/I:charge/I");
  // Fill values:
  ep.esize = 1;
  tp.m = 1;  
  // etc. Then fill the tree:
  eventData->Fill();

```

---

<div class="post-metadata">

**Author:** ![Manisha\_Rana](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/manisha_rana/32/34574_2.png) [@Manisha\_Rana](https://root-forum.cern.ch/u/Manisha_Rana)\
**Post date:** [March 18, 2024, 10:57am UTC](https://root-forum.cern.ch/t/ttree-branch-filling/58574/3 "2024-03-18T10:57:51Z")

</div>

Thank you for your response but this is also not working.

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [March 18, 2024, 12:06pm UTC](https://root-forum.cern.ch/t/ttree-branch-filling/58574/4 "2024-03-18T12:06:48Z")

</div>

Sorry, corrected now. Create the branches directly on the TTree (and fill the tree).

---

<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:** [March 18, 2024, 8:13pm UTC](https://root-forum.cern.ch/t/ttree-branch-filling/58574/5 "2024-03-18T20:13:20Z")

</div>

[quote=“Manisha\_Rana, post:1, topic:58574”]

```auto
eventP->Fill();
trackP->Fill();

```

Do not call fill on the branch, call it on the `TTree`:

```auto
eventData->Fill();

```

---

<div class="post-metadata">

**Author:** ![Manisha\_Rana](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/manisha_rana/32/34574_2.png) [@Manisha\_Rana](https://root-forum.cern.ch/u/Manisha_Rana)\
**Post date:** [March 19, 2024, 12:05pm UTC](https://root-forum.cern.ch/t/ttree-branch-filling/58574/6 "2024-03-19T12:05:28Z")

</div>

But I want to fill Both the branches in two different loops. Because They contain different Leafs.If it can be done that will be helpful for me.

---

<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:** [March 19, 2024, 3:25pm UTC](https://root-forum.cern.ch/t/ttree-branch-filling/58574/7 "2024-03-19T15:25:09Z")

</div>

You might want 2 different tree then. Most tools expect all the branches to have the same number of entries (same number of call to `Fill`) but the entries can be variable size array (so each branch might have a different total number of values stored). (So usually the track branch contains arrays or std collections).

But let’s take the problem from a different angle. How are your planning on reading and using this information?
