RE: [ROOT] Why I can't fill histgram from ntuple when sorce code is compiled.

From: Philippe Canal (pcanal@fnal.gov)
Date: Tue Jul 31 2001 - 20:53:19 MEST


Hi,

I am not quite sure what is the behavior you expect.  In the case of your interpreted code,
since you use an unamed macro, your histogram is created in the global namespace and
is still availaible at the end of the macro (and is filled up).

In your compiled sample, since you use a function main,  the program ends and all memory
object goes away.   

Since you defined the histogram __before__ opening the file, in __both__ case the histogram
is (shoud) not be written to the file.  To write the histogram to the file, in both case, you would
need to created the histogram after creating the TFile object __and__ call file.Write() before
closing the file.

I suspect that you also try to use a simple function (instead of a main) that contain you code.
In this case, since you allocated the histogram on the stack:
      TH1D hist("hist","hist",100,0,10);
The histogram object will go out of scope and be delete at the end of the function.  If you need
an histogram to live longer that the duration of a function, use:
      TH1D * hist = new TH1D("hist","hist",100,0,10);

Cheers,
Philippe.
 

-----Original Message-----
From: owner-roottalk@pcroot.cern.ch [mailto:owner-roottalk@pcroot.cern.ch]On Behalf Of Sugaya Masahiro
Sent: Tuesday, July 31, 2001 1:46 PM
To: ROOT Mailing List
Subject: [ROOT] Why I can't fill histgram from ntuple when sorce code is compiled.




Hi rooters.
I have a probrem.

My root version is 3.01/06 linux egcs.

When I used interpreter, next code works
{
    TH1D hist("hist","hist",100,0,10);
    TFile file("test.root","UPDATE");
    TNtuple* ntup;
    ntup = (TNtuple*) file.Get("ntup");
    ntup->Draw("p>>hist","","goff");
    hist.Draw();
    file.Close();    
}
but , when I compiled next code using gcc

#include <TFile.h>
#include <TROOT.h>
#include <TH1.h>
#include <TNtuple.h>

TROOT root("root","root");
   
int main(){
    TH1D hist("hist","hist",100,0,10);
    TFile file("test.root","UPDATE");
    TNtuple* ntup;
    ntup = (TNtuple*) file.Get("ntup");
    ntup->Draw("p>>hist","","goff");
    hist.Draw();
    file.Close();    
}

The Histgram hist on the root file. did not filled.
Am I someting wrong? 
Is there something I have to do? 



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