TTree Branch Filling

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

struct TrackParameter{
  float m;
  float px;
  float py;
  float pz;
  float en;
  int pid;
  int charge;
} ;
     
struct EventParameter {
  int esize;
  int eid;
};
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();

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.

  //... 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();

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

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

[quote=“Manisha_Rana, post:1, topic:58574”]

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

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

eventData->Fill();

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.

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?