Using vectors in SetBranchAddress

I’m using root 5.18. The following bit of code works in root:
double jetEta[100]
chain->SetBranchAddress(“jetEta”,&jetEta,&b_jetEta);

However for my code I need to store a lot of variables in a vector, so I’ve tried the following code, but it doesn’t work:
double jetEta[100]
std::vector<double*> jet_vect;
jet_vect.push_back(jetEta);
chain->SetBranchAddress(“jetEta”, &(jet_vect.at(0)),&b_jetEta);

It crashes with a segmentation fault when I try to access the member of jet_vect. Can someone tell me what I’m doing wrong?

Thanks

[quote]I’m using root 5.18. The following bit of code works in root: [/quote]Humm … actually if it really works, it is my accident!

You should use the following (note that the variable is a pointer to a vector):std::vector<std::vector<double> >*jet_vect; .... chain->SetBranchAddress("jetEta", jet_vect),&b_jetEta);

Note that in the code you tried with std::vector<double*>, ROOT has no way to know the length of your internal arrays and hence can not properly stream the content.

Cheers,
Philippe.

Thanks for your quick reply.

I’m confused, my goal is to have several variables, eg jetEta, jetPt, jetPhi, stored in the same vector. These variables are each stored as arrays in the ntuple. Also, how would I be able to specify which variable is at which position in the method you gave me?

The code that works essentially comes from MakeClass, is it not right to follow that?

Thanks again,
Andy

[quote]The code that works essentially comes from MakeClass, is it not right to follow that? [/quote]Yes it is right to follow MakeClass. In answering I mis-understood whether you where creating a new TTree or reading an existing TTree.

For the MakeClass case, you must set up the address as MakeClass does:double jetEta[100] .... chain->SetBranchAddress("jetEta",&jetEta,&b_jetEta);. You can not introduce a vector (and especially not a vector<double*>) at this level.

[quote]I’m confused, my goal is to have several variables, eg jetEta, jetPt, jetPhi, stored in the same vector. These variables are each stored as arrays in the ntuple. Also, how would I be able to specify which variable is at which position in the method you gave me?[/quote]For that you will need to loop over each element of the arrays.

I mean that one option is for you to create a class (let’s MyClass) with a data member for each of jetEta, jetPt, etc. and do use a vector and to loop over each array element or in pseudo code: vector<MyClass> myvect; for i = 0 to number_of_jet do MyClass obj; obj.jetEta = jetEta[i]; obj.jetPt = jetPt[i]; myvect.push_back(obj);

Cheers,
Philippe.