Histogram writing problem

Hi - I’m using ROOT 3.04/02.

I’m getting the following error:

root [0] .L testHistoCreate.cc
root [1] testHistoCreate()
Error: illegal pointer to class object theHisto1 0x0 150 FILE:testHistoCreate.cc LINE:16
*** Interpreter error recovered ***

If I remove the file stuff, I can see the histograms fine. Or if I create the histograms directly in testHistoCreate() - no problems.

Here is my file testHistoCreate.cc:

void createHisto(TH1F * myHisto, TH1F * myHisto2);

void testHistoCreate(){
  
  gROOT->cd("/");

  TH1F * theHisto1;
  TH1F * theHisto2;

  createHisto(theHisto1,theHisto2);
  
  // TH1::AddDirectory(kFALSE);

  TFile *f = new TFile("plots/mytestfile.root","RECREATE");
  
  theHisto1->Write();
  theHisto2->Write();

  f->Write();

  //  delete theHisto1;
  //  delete theHisto2;

}

void createHisto(TH1F * myHisto, TH1F * myHisto2){
  // TH1::AddDirectory(kFALSE);

  myHisto = new TH1F("test1","test1",100,0,100);
  myHisto2 = new TH1F("test2","test2",100,0,100);

  myHisto->Fill(50);
  myHisto2->Fill(30);

}

Thanks for any help.

-Ed

You have an error when calling CreateHisto. You must pass the pointer
by reference. See code below

Rene

void createHisto(TH1F * &myHisto, TH1F * &myHisto2);

void testHistoCreate(){

gROOT->cd("/");

TH1F * theHisto1=0;
TH1F * theHisto2=0;

createHisto(theHisto1,theHisto2);

// TH1::AddDirectory(kFALSE);

TFile *f = new TFile(“mytestfile.root”,“RECREATE”);

theHisto1->Write();
theHisto2->Write();

// f->Write(); //to be removed. or remove the 2 previous statemen

// delete theHisto1;
// delete theHisto2;

}

void createHisto(TH1F * &myHisto, TH1F * &myHisto2){
// TH1::AddDirectory(kFALSE);

myHisto = new TH1F(“test1”,“test1”,100,0,100);
myHisto2 = new TH1F(“test2”,“test2”,100,0,100);

myHisto->Fill(50);
myHisto2->Fill(30);

}

Hi - this seems really basic, but I’m having trouble.

I have a whole bunch of plots in the directory “/”.

I’d like to save them to a file.

When I run:

TFile *newfile = new TFile(“plots/myfile.root”,“recreate”);

and then:

myPlot->Write();

It can’t find the plot.

Then if I do a: gROOT->cd("/") and then write, it doesn’t find the file.

Thanks for any help.

What is the variable “myplot” in your example? Where is it defined. You should show a complete sample, not just a snippet.
Anyhow, if you want to save all teh histograms already created in gROOT
to a file, do:
TFile *f = new TFile("myfile.root’,“recreate”);
gROOT->GetList()->Write();
delete f;

Rene