Question about how TTree::Branch uses the passed in pointer

Dear Rooters,

I was wondering if somebody knew exactly what the function

TTree::Branch(const char* name, const char* classname, void* addobj, Int_t bufsize /* = 32000 /, Int_t splitlevel / = 99 */)

does with the pointer at the address of addobj (ie the pointer to the object which will be used for tree i/o). Basically the reason I want to know is that for several reasons I would not like the object I’m storing the in the tree or using to read out the true to be created dynamically. So can I get away with in a function used to set the branch addresses

void setBranchAddresses()
{
SomeClass* pointer = &someClassInstance_;
tree_->Branch(“abranch”,“SomeClass”,&pointer);
}

given that the pointer will go out of scope add the end of the function? If TTree::Branch actually uses the address of the pointer not the address of the object pointed to, this will of course cause problems which will be fun to debug hence I have to know for sure.

Thanks,
Sam

[quote]given that the pointer will go out of scope add the end of the function?[/quote]No, this will not work and might lead to a core dump.

Branch use the pointer to pass back to the user the address of the (new) object in the case where the TTree must creates the object.

[quote]Basically the reason I want to know is that for several reasons I would not like the object I’m storing the in the tree or using to read out the true to be created dynamically. [/quote]I am not sure what you exactly mean. The data object and the pointer whose address is being passed to TTree::Branch have different live time. If you meant “I would not like the pointer whose address is passed to TTree::Branch to be created dynamically”, then you can still declare the pointer on the stack has long as this stack has the same lifetime as your TTree object (an extreme example being to declare it in the main function :slight_smile: ).
If you actually meant to talk about the way the data is allocated then, I simply recommend you extend your class from class NonPersistentContainer { someClass someClassInstance_; someClass *someClassPtr_; // always point to someClassInstance. TTree *tree_; };

Cheers,
Philippe.