Histogram to/from file in compiled mode

I save a histogram on a TFile and then open it in another macro. Opening it in interactive mode is not a problem. Opening it is compiled mode (through the macro), I do not get to view it. A hint from a collague was to use TApplication and the function Run(), but when I do this, I lose the connection with interactive mode - my session seems to be locked and I cannot run my macro a second time. Any ideas for solutions?

Please provide more information about what you are trying to do, if possible a script.

Rene

Here is my example code (with unnecessary parts removed):

#include “Riostream.h”
#include “TF1.h”
#include “TGraph.h”
#include “TH1.h”
#include “TCanvas.h”
#include “TFile.h”
#include “TTree.h”
#include “TApplication.h”
#include
#include

Double_t TotalG(Double_t *x,Double_t *par)
{
… lots of code…
}

void Na22test()
{
// This (in combination with app.Run() below) makes it possible to view the histogram,
// but locks the root window so I cannot rerum the script or manipulate my histogram through the command line:
TApplication *app = new TApplication();

TCanvas *canvas = new TCanvas(“canvas”,“Plots”,600,400);
//canvas -> SetFillColor(10);

//This is the part that works great if I type it in line by line at the root prompt:
TFile file2(“Na22.root”);

TH1D* h1 = (TH1D*)file2.Get(“h1n”);

file2.GetListOfKeys()->Print();

// file2.Close(); // this I cannot use at all without getting segmentation fault

h1->Draw();

// up to here

…more code…

app->Run();
}

What I am trying to do is to view the histogram in Na22.root, and keep the data in it for viewing even after my script is done executing

[quote]file2.Close(); // this I cannot use at all without getting segmentation fault [/quote]This is the expected result :slight_smile:. The TFile object owns by default the histogram and TTree object you read from it. When the TFile is Closed, all the object the TFile owns are deleted (and hence your h1->Draw() uses random memory!).

You can simply detach the histogram before closing the file:h1->SetDirectory(0);

Cheers,
Philippe.

That did the trick :smiley: . Amazing that that was all I needed to do :astonished: :blush: , I have been working around the problem all week having to close and reopen terminal windows every time I ran my code.