Get class name for a TClonesArray in branch

I need to check the name of a TClonesArray (TClonesArray::GetName(), i.e., the class name of the contained objects) contained in a branch of a TTree. The problem is that the address of that branch might have already been set in an external routine, so I cannot simply set the branch address to a temporary TClonesArray buffer, read the first entry and call temporary->GetName(). I tried to clone the branch in this way:

TClonesArray *temporary = NULL;
TBranch *cloneBranch = (TBranch*) (branch->Clone("tempCloneBranch"));
cloneBranch->SetAddress(&temporary);
cloneBranch->GetFirstEntry();
delete cloneBranch;
if (!strcmpt(emporary->GetName(), "MyClasss")){...

The code behaves as desired, but I get this kind of errors on the console:

Warning in <TBranchElement::InitializeOffsets>: Branch 'tempCloneBranch' has no mother!

So I guess that cloning a branch is not a 100% safe operation, and even if the above code fits my purpose the error messages are very annoying. Can anyone please suggest me a smarter way to do this? Thanks.

I solved the problem using a temporary buffer in this way:

char *oldBuffer = branch->GetAddress();
Long64_t oldReadEntry = branch->GetReadEntry();
TClonesArray *buffer = NULL;
branch->SetAddress(&buffer);
branch->GetEntry(0);
branch->SetAddress(oldBuffer);
if (oldReadEntry > -1) {
  branch->GetEntry(oldReadEntry);
}
if (!strcmp(buffer->GetName(),"MyClasss")){...

It seems to work, no error messages nor strange behaviors. Let’s hope I did not screw anything up…

Hi,

You can probably also dobranch->GetEntry(0); char *oldBuffer = branch->GetAddress(); TClonesArray *buffer = (TClonesArray*)oldBuffer; if (!strcmp(buffer->GetName(),"MyClasss")){...

Note that neither solution works in all cases of TTree layout …

Cheers,
Philippe.

See TTreeFormula.cxx TBranchElement* bc = (TBranchElement*) branch; if (bc == bc->GetMother()) { // Top level branch //clones = *((TClonesArray**) bc->GetAddress()); clones = (TClonesArray*) bc->GetObject(); } else if (!leafcur || !leafcur->IsOnTerminalBranch()) { TStreamerElement* element = (TStreamerElement*) bc->GetInfo()->GetElems()[bc->GetID()]; if (element->IsaPointer()) { clones = *((TClonesArray**) bc->GetAddress()); //clones = *((TClonesArray**) bc->GetObject()); } else { //clones = (TClonesArray*) bc->GetAddress(); clones = (TClonesArray*) bc->GetObject(); } }

Thanks Philippe, I’ll try with your solution if mine shows some problem (I’m lazy and since my impolementation seems to work I don’t want to touch it :smiley: )