Re: [ROOT] histograms forget they are histograms

From: Rene Brun (Rene.Brun@cern.ch)
Date: Fri Jun 07 2002 - 19:04:54 MEST


Hi Ken

Before the loop creating the histograms, add the statement
   TH1::AddDirectory(kFALSE);

In case of a TChain, if you create the histograms in the Loop function,
the current directory changes each time a new file is loaded.
By default, histograms are created in the current directory.
By calling the above statement, you instruct the TH1 constructors to not
register the object in the list of objects in memory. They will not
be deleted when you move to a new file.
Using MakeSelector instead of MakeClass facilitates this process.
In this case you create your histograms in the Begin function.
The ProcessFill function being called for each entry.

Rene Brun

On Fri, 7 Jun 2002, Ken Bloom wrote:

> Dear ROOTers,
> 
> 	I have stumbled upon a ROOT behavior that I don't understand.  Here's
> the story -- I've got a TTree (acutally a TChain of TTrees, but that shouldn't
> make a difference), and I've used MakeClass to get some skeleton code.  In the
> .h file, I declare what I believe are arrays of pointers to histograms, such
> as:
> 
>   TH1F* hnh[48];
> 
> Then, in the Loop() method, I actually create the histograms:
> 
>    for (int i = 0; i < 48; i++) {
>      ostringstream hid,htit;
>      hid << "hnh" << i;
>      htit << "Nhits for wedge " << i;
>      hnh[i] = new TH1F(hid.str().c_str(),htit.str().c_str(),5,-0.5,4.5);
>      hnh[i]->Print();
>    }
> 
> With the Print() there, I can see that I successfully create the histogram.  
> When I execute Loop(), I see
> 
> TH1.Print Name= hnh0, Entries= 0, Total sum= 0
> TH1.Print Name= hnh1, Entries= 0, Total sum= 0
> TH1.Print Name= hnh2, Entries= 0, Total sum= 0
> ...
> 
> and thus I think I'm happy.
> 
> 	Then I start looping over the entries of the tree, and I get into 
> trouble -- the TH1F* pointers seem to forget that they are pointing to 
> histograms!  I keep putting in hnh[0]->Print() statements to check on things, 
> and I see that as soon as I call LoadTree(0), the print statement returns
> 
> OBJ: TObject    TObject Basic ROOT object
> 
> and thus I'm screwed.  And it's not that all of my pointers come back as 
> TObject; some just seg fault when I try to print.  It sounds to me like there 
> is weird memory corruption going on.  For what it is worth, I am pre-compiling 
> the class (via .L MyClass.C+), and I'm in ROOT version 3.02/07 which, if I 
> understand things, also has KCC v4.0 compiled in.
> 
> 	Can someone explain what is going wrong here, and how I can get out of 
> it?  Am I making a C++ mistake in my handling of arrays of pointers (I don't 
> believe so), or am I making a ROOT mistake.  Thanks, best wishes.
> 
> 						Ken
> 
> ------------------------------------------------------------------------------
> Ken Bloom, Karma Adjuster                                Department of Physics
> 734-763-2329 / 734-936-1817 (fax)                       University of Michigan
> kenbloom@umich.edu                              http://www.umich.edu/~kenbloom
> 



This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:50:56 MET