Re: [ROOT] To Draw or not to Draw?

From: Rene Brun (Rene.Brun@cern.ch)
Date: Wed Nov 14 2001 - 18:42:08 MET


Hi Tommaso,

Your histogram "hp" is created in the current directory by default (here your
file hfile).
When you close this file, the histogram is automatically deleted.
see Users Guide: Chapter on Object Ownership page 101

You have two solutions:
 1- create your histogram before opening hfile
 2- remove the reference to this histogram from the current directory
    by calling TH1::SetDirectory(0); see the line in your code below
    If you opt for this option, you must find a way to get a pointer to
your            histogram later, such that you can delete it (to avoid a
possible memory leak).

Rene Brun

Tommaso Chiarusi wrote:
> 
> Dear Rooters, I cannot see the plot I draw() using the following procedure
> that I put in my class' method Deco::PDistrib() (I generated Deco.C con
> MakeClass)
> 
> void Deco::PDistrib(double pmin, double pmax)
> {
> 
>    TFile *hfile = new TFile("Pdist.root","RECREATE","DATA");
> 
>    _pmin = pmin; _pmax=pmax;
>    Int_t nentries = Int_t(fChain->GetEntries());
> 
>    TH1F *hp = new TH1F("hp","P distrib",_nbin,pmin,pmax);
     hp->SetDirectory(0); //<====================
> 
>    double areamin=100000,areamax=0;
>    Int_t nbytes = 0, nb = 0;
>    for (Int_t jentry=0; jentry<nentries;jentry++) {
>       Int_t ientry = LoadTree(jentry);
>       nb = fChain->GetEntry(jentry);   nbytes += nb;
>       // if (Cut(ientry) < 0) continue;
> 
>       hp->Fill(tp);
>    }
> 
>    hp->Write();
>    hfile->Close();
> 
>    TCanvas *nc = new TCanvas("nc");
>    hp->Draw("");
> 
> }
> 
> The fact is that the plot exist but does not appear in the canvas!
> I think that the problem is in creating and closing of TFile *hfile.
> I tried to change the position of the line
> 
> TFile *hfile = new TFile("Pdist.root","RECREATE","DATA");
> 
> postponing it after the iteration over "jentry" but, naturally, it is the
> same!
> Neither mooving the lines
> 
>    TCanvas *nc = new TCanvas("nc");
>    hp->Draw("");
> 
> out from the "scope"
> 
> TFile *hfile = new TFile("Pdist.root","RECREATE","DATA");
> 
> ...
> 
> hfile->Close();
> 
> but it yelded the same result: no plot on the screen.
> 
> Why does it happen?
> Which mistake do I  commit?
> 
> Thanks and Cheers, Tommaso



This archive was generated by hypermail 2b29 : Tue Jan 01 2002 - 17:51:08 MET