Re: initializing TClonesArray (basic question)

From: George Locke <georgelocke.roottalk_at_gmail.com>
Date: Thu, 16 Nov 2006 10:52:21 -0500


thanks for the replies. i guess i might've figured that out myself, but thanks for explaining it to me. i just had to rename them, as you say.

thanks christian for the file i/o tips as well.

Regards,
George

On 11/16/06, Christian Holm Christensen <cholm_at_nbi.dk> wrote:
> Hi George,
>
> On Wed, 2006-11-15 at 13:14 -0500, George Locke wrote:
> > More simple questions. I tried duplicating what i found in the online
> > TClonesArray, and i looked in the user's guide, but my code still
> > won't run properly :( so i tried and RTFM...
> >
> > (root 5.13.04, windows 2000, compiliing with msvc 8)
> >
> > all i want is 8 identical histograms to fill and reference in an array.
> >
> > "
> > TClonesArray bigArr("TH1F", 8);
> >
> > for (chan=0; chan<NUM_CHAN; chan++) {
>
> As Rene said, set a unique name for each histogram
>
> TString name(Form("channel_%03d", chan));
> > new(bigArr[chan]) TH1F(name,title,HIST_SIZE,0,HIST_SIZE);
> > printf("\t after%d\n\n",chan);
> > }
> >
> > [fill histograms, read histograms, write histograms]
> > "
> > this will compile but when i run it i am told "Warning in
> > <th1::Build>: Replacing existing histogram: XX (potential memory
> > leak)"
> > this error doesn't occur the first time, but after 0,1,2,3,4,5,6 it
> > occurs each time.
> >
> > the program doesn't crash, but when I try to fill the histograms with
> > a simple parabolic pattern, every getbincontent(x) returns 0.
> >
> > When i try to write either the bigArr[x] or just bigArr nothing gets
> > into the file that i try to write to (eg i open in Cint and do f1.ls()
> > and it shows an empty file).
>
> What do you write out? The array, a tree with the array as a branch, or
> the individual histograms? In the first 2 cases, you should be aware of
> in which directory you're in when you make the histograms. If the
> current directory (gDirectory) is somewhere in the output file, the
> individual histograms will be flushed to disk, and then deleted. If
> you're not in the file when you make the histograms you will have to
> make sure that they are flushed to disk.
>
> With a TTree or TClonesArray written to disk, you code should probably
> look like
>
> TFile* out = TFile::Open("foo.root", "RECREATE");
> TTree* tree = new TTree("T", "T");
> TClonesArray* array = new TClonesArray("TH1F");
> tree->Branch("channels", &array);
> for (int chan = 0; chan < nChan; chan++) [
> TString name(Form("channel_%03d", chan);
> TString title(Form("Channel %d data", chan));
> TH1F* h = new((*array)[chan]) TH1F(name.Data(),title.Data(), n, 0, n);
> h->SetDirectory(0);
> }
> // fill histograms
> out->cd(); // Only if you have _no_ TTree
> array->Write(); // Only if you have _no_ TTree
> out->Close();
>
> If you plan to write the histograms directly to disk, you need to do
>
> TFile* out = TFile::Open("foo.root", "RECREATE");
> TClonesArray* array = new TClonesArray("TH1F");
> for (int chan = 0; chan < nChan; chan++) [
> out->cd();
> TString name(Form("channel_%03d", chan);
> TString title(Form("Channel %d data", chan));
> TH1F* h = new((*array)[chan]) TH1F(name.Data(),title.Data(), n, 0, n);
> }
> // fill histograms
> out->Close();
>
> Note, that TClonesArray only makes sense if you want to reuse the
> memory. If what you want is basically a container of many histograms,
> use something like a TObjArray, TList, THStack, or similar.
>
> Yours,
>
> --
> ___ | Christian Holm Christensen
> |_| | -------------------------------------------------------------
> | | Address: Sankt Hansgade 23, 1. th. Phone: (+45) 35 35 96 91
> _| DK-2200 Copenhagen N Cell: (+45) 24 61 85 91
> _| Denmark Office: (+45) 353 25 404
> ____| Email: cholm_at_nbi.dk Web: www.nbi.dk/~cholm
> | |
>
>
Received on Thu Nov 16 2006 - 16:52:51 MET

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