Problem with storing double variables in TTree

Hi ,
I have a simple macro to check the TTree functionality with different variable types.
I realised that this code works well and the root file is ok :

{
  struct MCtau_{
    float eta,phi,pt;
  };
  MCtau_  MCtau;
  TTree *t=new TTree("t","tree");
  
  t->Branch("MCtau",&MCtau.eta,"eta:phi:pt/F");
MCtau.pt=2;
MCtau.eta=3;
MCtau.phi=4;
t->Fill();
TFile f("m.root","recreate");
t->Write();
f.Close();
}

but when I change the type to double like this code :

{
  struct MCtau_{
    double eta,phi,pt;
  };
  MCtau_  MCtau;
  TTree *t=new TTree("t","tree");
  
  t->Branch("MCtau",&MCtau.eta,"eta:phi:pt/D");
MCtau.pt=2;
MCtau.eta=3;
MCtau.phi=4;
t->Fill();
TFile f("m.root","recreate");
t->Write();
f.Close();
}

this macro produces strange values as can be seen in the root file (m.root)
What is wrong?
I am using ROOT4.3.2 gcc3.2.3 I am running on SLC3 cern scientific linux.
Thanks for hints.
Cheers,
Majid

Replace the line
t->Branch(“MCtau”,&MCtau.eta,“eta:phi:pt/D”);
by
t->Branch(“MCtau”,&MCtau.eta,“eta/D:phi:pt”);

The default type is /F

Rene

Hi Rene,
thanks it works now well but why when I try to use a variable length array
it again gives wrong values in the root file :

{
struct MCtau_{
  double pt[2],eta,phi;
};
MCtau_  MCtau;
TTree *t=new TTree("t","tree");
int n;
t->Branch("n",&n,"n/I");
t->Branch("pt",&MCtau.pt,"pt[n]/D:eta:phi");
n=2;
MCtau.pt[0]=2;
MCtau.pt[1]=3;
MCtau.eta=3;
MCtau.phi=4;
t->Fill();
TFile f("m.root","recreate");
t->Write();
f.Close();
}

Thanks,
Majid

If you have a variable length array, it must be the last one and the only one in your branch definition. ie in your case do:
eta/D:phi:pt[n]

Rene

Hi Rene,
thanks it works well now but as a question and/or comment:
Since for each event one has different number of reconstructed objects
of a given type it would be very nice if a struct like :

struct Rectau_{
double pt[10],eta[10],phi[10];
};
Rectau_ Rectau;

could be used in a tree with all three components pt,eta,phi as variable length which depends on how many reconstructed taus one has for each event.
Is there any possibility to develop the framework for such a code :

{
struct Rectau_{
  double pt[2],eta[2],phi[2];
};
Rectau_  Rectau;
TTree *t=new TTree("t","tree");
int n;
t->Branch("n",&n,"n/I");
t->Branch("Rectau",Rectau.pt,"pt[n]/D:eta[n]:phi[n]");
n=2;
MCtau.pt[0]=2;
MCtau.pt[1]=3;
MCtau.eta[0]=3;
MCtau.eta[1]=4;
MCtau.phi[0]=4;
MCtau.phi[1]=5;
t->Fill();
TFile f("m.root","recreate");
t->Write();
f.Close();
}

when I define t->Branch(“Rectau”,Rectau.pt,“pt[2]/D:eta[2]:phi[2]”);
it works nice. But variable length array for all three pt,eta,phi would be the best if there is not any technical problem for having such things.
Cheers,
Majid

Use a class instead of a struct and what you want will be possible.

Rene