Issues with void function inside macro


Please read tips for efficient and successful posting and posting code

ROOT Version: 6.16
Platform: linux8664gcc


Hello,

I’ve been having an error when trying to use a void function inside a macro “testing.c”.

Here is the code I have been trying to use (t_halo is an already created histogram within the root file “test.root”):

void testing(){
  TFile *f = new TFile("test.root","UPDATE");
  t_halo->Draw();
}

I get the following error when running this macro:
Inkedvoid_error_LI

However, this code works if I type the following text into the command line before running the macro: TFile *f = new TFile("test.root","UPDATE");

This code also works if I remove void test(). I’ve included this below:

{
TFile *f = new TFile("test.root","UPDATE");
t_halo->Draw();
}

I’m trying to use functions in a bigger macro and am having the same problem, so if anyone knows why this isn’t working I would greatly appreciate a response.

Thanks in advance.

Hello,

Thanks for your response. Do you know how to properly define a histogram that’s being read from a file as was suggested in the post you linked? I tried the following method:

void testing(){
  TFile f("halo_3D_angles_hists.root","UPDATE");   
  TH1F *h = (TH1F*)f.Get("t_halo");   
  std::cout<<h->Integral()<<std::endl;  
  h->Draw();   
  h->Write();   
}

This no longer gives me an error when I run it, but the canvas is empty when I draw it, and it won’t write to the file. However, it does output the correct integral.

Instead of h->Write();, use: h->SetDirectory(gROOT);