Unable to Create Histogram

Hello
I have declared a 2D histogram named ‘Backgrnd’. I have filled the histogram and writing with following command.

Backgrnd->Draw(“surf1”);
Backgrnd->Write();

But it is not creating the histogram and showing this message.

Error in TROOT::WriteTObject - The current directory (RootApp) is not associated with a file. The object (Backgrnd) has not been written.

Thank you

To write a TObject you have to open a TFile before

TFile *f = new TFile("myfile.root","RECREATE");
//create your histogram
//fill you histogram
Backgrnd->Draw("surf1"); 
Backgrnd->Write();
f->Close();

No actually I do not want to create histograms within a root file. I want to create them individually in separate canvas.

In such case you do not need to use the Write method.
To create several TCanvas, you can do something like

vector <TCanvas*> canv;
for(int i=0 ; i<10 ; i++) {
  canv.push_back(new TCanvas(Form("c%d",i),Form("c%d",i));
  canvas.back()->cd();
  Backgrnd->Draw("surf1"); 
}

Hi Himanshu,
I think it might help you to take a look at the tutorials, in particular the “histogram tutorials”.

Cheers,
Enrico

I tried. But the canvas is not appearing at all. I am running the code with g++ with linking all the root libs and I am not running this directly with root prompt. I checked this fact. Now I am sure this is the reason that no canvas appearing. Is there any way to do it with g++.

Hi,
if you are compiling a program rather than interpreting a root macro, you have to let the program keep running after it drew your objects: check, for example, this thread.

1 Like

instantiate TApplication app(“app”, nullptr, nullptr); at the beginning of your main

Inside main() or just before.

Must be inside :smiley: but you can just try and see what works :stuck_out_tongue:

Yeah I did!!!
Worked well inside!!! :sweat_smile:
Thank you once again friend :sunglasses:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.