Hello,
I tried to write a simple script to illustrate. The reason why I don’t want to hardcode is because I have an already written function defined as following :
myfct(vector<TString> varname, vector<double*> varaddr)
varname is referring to variable name used later on in the library I use {“M”, “X”, “Y”, “Z”}
The varaddr is referring to double pointers which are initialized calling Three::SetBranchAddress and set to the correct value with Three::GetEntry.
So I would like to do the same for the mass.
I tried to address different variables such as Px. I have done that in a different way. But I think I am missing something simple…
#include "TROOT.h"
#include "TFile.h"
#include "TTree.h"
#include "TBrowser.h"
#include "TH2.h"
#include "TRandom.h"
void tree1w()
{
TFile f("tree1.root","recreate");
TTree t1("t1","a simple Tree with simple variables");
TLorentzVector lv1;
t1.Branch("lv1.", "TLorentzVector", &lv1);
//fill the tree
for (Int_t i=0;i<10;i++) {
lv1.SetPx(i);
t1.Fill();
}
t1.Write();
}
void tree1r()
{
TFile *f = new TFile("tree1.root");
TTree *t1 = (TTree*)f->Get("t1");
TLorentzVector* lv1 = NULL;
double X;
t1->SetMakeClass(1);
t1->SetBranchAddress("lv1.",&lv1);
t1->SetBranchStatus("lv1.*", 1 );
t1->SetBranchAddress("lv1.fP.fX",&X);
Long64_t nentries = t1->GetEntries();
for (Long64_t i=0;i<nentries;i++) {
t1->GetEntry(i);
lv1->Print();
cout << lv1->Px() << " " << X << endl;
}
}
void tree1() {
tree1w();
tree1r();
}
(x,y,z,t)=(0.000000,0.000000,0.000000,0.000000) (P,eta,phi,E)=(0.000000,0.000000,0.000000,0.000000)
0 0
(x,y,z,t)=(1.000000,0.000000,0.000000,0.000000) (P,eta,phi,E)=(1.000000,-0.000000,0.000000,0.000000)
1 0
(x,y,z,t)=(2.000000,0.000000,0.000000,0.000000) (P,eta,phi,E)=(2.000000,-0.000000,0.000000,0.000000)
2 0
(x,y,z,t)=(3.000000,0.000000,0.000000,0.000000) (P,eta,phi,E)=(3.000000,-0.000000,0.000000,0.000000)
3 0
(x,y,z,t)=(4.000000,0.000000,0.000000,0.000000) (P,eta,phi,E)=(4.000000,-0.000000,0.000000,0.000000)
4 0
(x,y,z,t)=(5.000000,0.000000,0.000000,0.000000) (P,eta,phi,E)=(5.000000,-0.000000,0.000000,0.000000)
5 0
(x,y,z,t)=(6.000000,0.000000,0.000000,0.000000) (P,eta,phi,E)=(6.000000,-0.000000,0.000000,0.000000)
6 0
(x,y,z,t)=(7.000000,0.000000,0.000000,0.000000) (P,eta,phi,E)=(7.000000,-0.000000,0.000000,0.000000)
7 0
(x,y,z,t)=(8.000000,0.000000,0.000000,0.000000) (P,eta,phi,E)=(8.000000,-0.000000,0.000000,0.000000)
8 0
(x,y,z,t)=(9.000000,0.000000,0.000000,0.000000) (P,eta,phi,E)=(9.000000,-0.000000,0.000000,0.000000)
9 0