Using an array of C structure to make a tree

I want to use an array of C structres to make a tree. Firstly, I define the C structre:

typedef struct {
  Int_t        Nrun;
  Int_t        Nevt;
  Int_t        Ndate;
  Int_t        Ntime;
  Int_t        Nhits_id;
  
  Float_t      Qsk_id[11146];    //[Nhits_id]
  Float_t      Tsk_id[11146];    //[Nhits_id]
  Int_t        Icab_id[11146];   //[Nhits_id]   
}SUBEVENT;

And then I declare an array of this C structre and use it to make a tree.

void makeFOGEvent(){

  SUBEVENT subEvt[501];

  TFile *fogtree;
  TTree *t1;
  fogtree =  new TFile("../data/fog.root","RECREATE");
  t1 = new TTree("t1","fog tree");  
  TString s;
  Int_t i;
  //make 501 branches
  //each branch helds a sub-event,i.e. a C strctre
  for (i = 0; i < 501; i++){
    s.Form("%d",i);
    s = "subevt_"+ s;
    t1->Branch(s,&subEvt[i],"Nrun/I:Nevt/I:Ndate/I:Ntime/I:Nhits_id/I:Qsk_id[Nhits_id]/F:Tsk_id[Nhits_id]/F:Icab_id[Nhits_id]/I");
  }
 
  for (i = 0; i < 100; i++){          // generate 100 events
    for (Int_t j = 0; j < 501; j++){
      //do assignment for subEvt[j] here
    }
    t1->Fill();
  }
  fogtree->Write();
  fogtree->Close();
}

Everything goes well but the result is confusing.When I examine the tree I find that it is not properly filled as expectation. Qsk_id[] is filled properly but the contents of Tsk_id[] and Icab_id[] are always just the same as Qsk_id rather than the contents they should be filled.That’s quite strange.
Do you know what the problem is?
Thanks very much!

Instead oft1->Branch(s,&subEvt[i],"Nrun/I:Nevt/I:Ndate/I:Ntime/I:Nhits_id/I:Qsk_id[Nhits_id]/F:Tsk_id[Nhits_id]/F:Icab_id[Nhits_id]/I");
uset1->Branch(s,&subEvt[i].Nrun,"Nrun/I"); t1->Branch(s,&subEvt[i].Nevt,"Nevt/I"); t1->Branch(s,&subEvt[i].Ndate,"Ndate/I"); t1->Branch(s,&subEvt[i].Ntime,"Ntime/I"); t1->Branch(s,&subEvt[i].Nhits_id,"Nhits_id/I"); t1->Branch(s,&subEvt[i].Qsk_id,"Qsk_id[Nhits_id]/F"); t1->Branch(s,&subEvt[i].Tsk_id,"Tsk_id[Nhits_id]/F"); t1->Branch(s,&subEvt[i].Icab_id,"Icab_id[Nhits_id]/I");
In your example, the TTree has no way of knowing the sizeof your array in memory and hence can not guess where the Tsk_id and Icab_id are located.

Cheers,
Philippe.

Wait, how can you bind several different variables to one branch in that way?Several branchs with the same name?
I tried your method, and it seems that your method does’t work.
Am i right?

[quote] I tried your method, and it seems that your method does’t work. [/quote]I had several omissions (missing the data member names in the address) witht the ‘fixed’ code it should work. How does it fail for you?

t1->Branch(Form(“%s_%s”,s,“Nrun”),&subEvt[i].Nrun,“Nrun/I”);
t1->Branch(Form(“%s_%s”,s,“Nevt”),&subEvt[i].Nevt,“Nevt/I”);
etc…[/code]

Cheers,
Philippe[/quote]