Error when trying to create branch with arrays!

Dear all,

I have two codes indeed. One is with arrays and another one is with vectors. I simply try to see how to fill a Tree Branch with a vector. For some reason, macro doesn’t work with array-branch combination.

Code is attached.
macro9.C (2 Bytes)

Reference in pdf is also attached. See page 11.

RootTutorial_TTree.pdf (1.9 MB)


The macro file is empty.

I attach the macro again. Probably, something went wrong when posting. I noticed that Xcode didn’t save the document when I was working on it as well. That’s why I had funny error at that time. Now, it’s worked. Anyways, I post it here.

By the way, macros are designed to create random numbers, but I will read data from a file and do calculation. Then, I will fill the vectors for my work.

macro9.C (1019 Bytes)

Can you give me a website link I can refer to learn TTree examples with vectors?
I also want to know the difference between Branch() and SetBranchAddress() member functions? Both can have the syntax like below:

T->Branch("px", &px);
T->SetBranchAddress("px", &px);

However, I guess the parameters of px values, for instance, should be different. For Branch(), it is a value, but it’s a pointer for SetBranchAddress(). Is that the case?


Just search this Forum (search icon on the top right side), there are many similar questions and replies.

For the Branch, etc. question, again search the Forum, or go to the TTree documentation:
and read and scroll down to the Public functions to read about Branch, SetBranchAddress and all other TTree functions. In general, to search for documentation, click on the Documentation link at the top of this (Forum) page, then click on the Search icon (top right) of the Doc page and search for any object (TTree, TGraph,…) or function (Branch, SetBranchAddress,…) you want to see the reference page and source code for (look for the results saying “Class Reference”).


I need to ask something to learn. On the reference website, one of the branch constructors is like below:

For some reason, when we call this member function, we don’t use all the arguments (for instance T*tree) as far as I noticed. I type a part of the code to give an example.

What rule am I missing in c++ or ROOT when I try to understand the usage of member functions while calling them in my code?

TFile *f = TFile::Open(“myfile.root”, “RECREATE”);
if (!f) { return; }
TTree *T = new TTree(“T”, “Tree with vector”);
std::vector px;
std::vector py;
std::vector pz;

T->Branch("px", &px, "px/F");
T->Branch("py", &py, "py/F");
T->Branch("pz", &pz, "pz/F");

Thanks in advance.

Not really a ROOT question. Anyway, to understand that, read about c++ function overloading and function arguments; then go back to the documentation pages.

TBranch::TBranch ( TTree * tree,
const char * name,
void * address,
const char * leaflist,
Int_t basketsize = 32000,
Int_t compress = ROOT::RCompressionSetting::EAlgorithm::kInherit

Is that the specific concept and example I should know? For instance, to put only three out of 6 arguments (probably the other three were known and given previously, or set to some fixed value) were enough. I mean somebody probably set some of these arguments to some values in the function definition. That’s why it enables us top work with 3 parameters as well even though it seems it needs 6 arguments for function call.

## Causes of function overloading in c++

2. Function with Default Arguments

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.