RE: TClonesArray typecasting

From: Philippe Canal <>
Date: Tue, 14 Nov 2006 22:24:48 -0600


> When reading from a file, it's best to use dynamic_cast, as you may not
> know the type of the object returned. Suppose there's an object in the
> file named `o' and it's of class TH1F. If you do
> TH2* h = dynamic_cast<TH2*>(file->Get("o"))
> You will get a null pointer back, since `o' is not of type TH2 or
> any of it's decedents.

However this currently does not quite work in interpreted code because CINT currently degrade all the casts down to the equivalent of C style casts.

TFile and TDirectory offer the following syntax:

     TH2* h; file->GetObject("o",h);

which (besides being a bit shorter) does the same thing as the behavior as the above code with the dynamic_cast but will work __both__ in compiled code and in interpreted code.

Philippe Received on Wed Nov 15 2006 - 05:25:47 MET

This archive was generated by hypermail 2.2.0 : Mon Jan 01 2007 - 16:32:02 MET