Adding branch to TTree

I’m trying to add a branch with an integer to a TTree but I do not succeed. In the ROOT documentation for the TTree i read under Case A

[quote]if address points to a single numerical variable, the leaflist is optional:
int value;
tree->Branch(branchname, &value); [/quote]
I therefore think that this macro would run:

{ TTree* tree = new TTree("testTree", "To test adding branches"); int value; tree->Branch("intBranch",&value); }
It gives this error message when I run it in ROOT:
Error in TTree::Branch: The pointer specified for intBranch not of a class known to ROOT

When I instead try:

{ TTree tree = TTree("testTree", "To test adding branches"); Int_t a = 42; tree.Branch("intBranch",&a,"I"); tree.Fill(); tree.Draw("intBranch"); }
I get a histogram with one entry at float value 5.885e-44 which is the value you get if you read the int 42 as a float.
What am I doing wrong?

Cheers,
Ulf

root.cern.ch/root/html/TTree.html#TTree:Branch

[quote]The variable name and the variable type are separated by a slash (/) … If the first variable does not have a type, it is assumed of type F by default.[/quote]In your case:tree.Branch("intBranch",&a,"a/I");Jan

Thank you musinsky.

This does not work if I don’t have a variable devoted for each branch but a vector. The leaflist is supposed to be optional but if I remove it I cant compile. What will I have to do in order to not have to specify the variable name?

Thanks for any help,
Ulf

[quote]This does not work if I don’t have a variable devoted for each branch but a vector. [/quote]What do you mean? In the example you provided, their was no vector and Jan correctly pointed out that the leaflist you used (in your example) was incorrect and would indeed lead to the wrong information being stored/retrieved.

[quote]The leaflist is supposed to be optional [/quote]Which version of ROOT are you using. It is a recent feature of ROOT. In ROOT v5.20 and newer it is indeed optional but if and only if the 2nd parameter is a variable of simple type (int, float, etc).

[quote]What will I have to do in order to not have to specify the variable name?[/quote]What did you try? (i.e. complete example including your vector).

Cheers,
Philippe.

I’m using ROOT 5.18 and that is probably why it does not work.

What I ment was that I wanted to store the data in elements of a vector instead of as separet variables, and I do not know what the leaflist would be if the second argument was an element of a vector instead of a variable.

If you hold the variable in a vector you have 2 choices. One is to store the vector itself in a branch, another is to store each element in a separate branch. In the later case, you would pass the address of the element (&(myvector[i])) but you would also need to make sure that the vector never re-size itself (and thus move the elements to a different memory location).

Cheers,
Philippe.

I now use one branch for each element and my problem is then how to specify the data type. What variable name should I have in the leaflist?

(I’m using root 5.18 so the leaflist isn’t optional.)

Cheers,
Ulf

Cheers,
Philippe.

Thank you, that worked.

I also want to add a branch with a map<string,bool> but i can not even create such an object in root. If I do

root [0] map<string,bool> m root [1] m
I get this error:

[quote]Error: no such template unknown map<string,bool> :0:
(unknown map<string,bool>)143885304
*** Interpreter error recovered ***[/quote]

I get similar errors with some other template classes (vector, vector, pair<int,string>, queue, map<int,int>,… but vector,queue, pair<int,int>,… works). I guess the problem is that only some template classes are used in compiled code and therefore compiled, but how do get all of them to work in root?

Hi,

In order to use most of the precompiled STL container dictionary in version of ROOT older than 5.20, you must explicitly include the corresponding header file. So for example for vector do: root [] #include <vector> root [] vector<float> v;
For STL container without a precompiled dictionary, you should generate it yourself. For example with the file loader.C//File loader.C #include <map> #include <string #ifdef __MAKECINT__ #pragma link C++ class map<string,boo>+; #endifdoroot [] .L loader.C+ map<strring, bool> m;

Cheers,
Philippe

I guess you mean that loader.C should look like this

//File loader.C #include <map> #include <string> #ifdef __MAKECINT_ #pragma link C++ class map<string,bool>+; #endif
_MAKECINT isn’t defined (nor MAKECINT) so this doesn’t help.

If I remove the ifdef and endif lines I get this error:

[quote]Note: Link requested for undefined class map<string,bool> (ignore this message) :0:
Error: A dictionary has been requested for map<string,bool> but there is no declaration![/quote]

My mistake it is MAKECINT (with 2 sets of double underscore)

Then I get the error quoted above.

Hi,

I can not reproduce this problem (neither with v5.20 nor v5.16). Did you put the #include in the loader.C file? What platform are you running on?

Cheers,
Philippe

I’m running on a linux platform (on lxplus) with root version 5.18/00a. My loader.C file looks like this:

//File loader.C #include <map> #include <string> #ifdef __MAKECINT__ #pragma link C++ class map<string,bool>+; #endif
and in root I do:

root [0] .L loader.C+ root [1] map<string,bool> m;

I get different results different times when (with restarted root) doing the exact same thing. most of the times I get the error i wrote before at the first line:

[quote]Note: Link requested for undefined class map<string,bool> (ignore this message) :0:
Error: A dictionary has been requested for map<string,bool> but there is no declaration![/quote]

Some times I have not recieved any error from the first line but then instead get this error when executing the second line:

[quote]Error: map<string,bool>() no default constructor (tmpfile):1:
*** Interpreter error recovered ***[/quote]

Cheers,
Ulf

I have figured out when I get which error. The first time (and sometimes also the second) time i load loader.C after haveing saved it I get the first error, and after that I get the second error.

Cheers,
Ulf

[quote]I get different results different times when (with restarted root) doing the exact same thing. most of the times I get the error i wrote before at the first line:[/quote]This is quite strange … in particular ACLiC should not be recompiling the library except if you change loader.C and/or delete the resulting library …

Philippe.

Yes, I get the first error only when it recompiles, but either way I don’t get map<string,bool> to work.

Cheers,
Ulf

[quote]Yes, I get the first error only when it recompiles[/quote]If you see the first error, indeed the dictionary is not generated correctly and it wont work … now I can not reproduce this error at all. Can you remind me your exact configuration (ROOT, compiler, OS version number).

Cheers,
Philippe.

I ran into the same problem when I used ROOT from within the ATLAS software environment (CMT and all that).

What works for me is to create the shared library and the dictionary with

.L VectorVectorDict.h+

from a pure ROOT environment.

Then I can load the library and dictionary within the full ATLAS software setup with the same command because the files are re-created only if VectorVectorDict.h has changed.

Cheers,
Sebastian