Problem to plot TH1D

Hello rooters,

I have a very simple macro to read a ttree and then create histograms, but when I try to plot the histograms the canvas are empties. In addition when I do t_per_run->Draw(“time_of_run”); I have an histogram, but I do t_per_run->Draw(“time_of_run>>h_test”); the canvas is empty again.

The histograms are filled correctly because I get good values when I use GetBinContent … I don’t know what is the problem?

Attached the root file and the macro.

Thanks in advance for your help!

cheers
Luis

.Data_Runs_May_4MeVPerFiber_ALL.root (118.3 KB)
testPlots.C (6.2 KB)


ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


Really it is a weird problem. I mange to reduce it to the following simple macro:

void testPlots() {
   TFile f("Data_Runs_May_4MeVPerFiber_ALL.root");
   TH1D* h = new TH1D("h","h",10,0.,1.);
   h->Draw();
}

if you run this macro the canvas is empty. If you comment the TFile line then an empty histogram plot (axis etc … ) appears as it should. I have no idea why just attaching the file messes up the graphics.

It seems that if I declare f as a pointer it does the job, but I don’t know why?

Yes if I do:

void testPlots() {
   auto f = new TFile("Data_Runs_May_4MeVPerFiber_ALL.root");
   TH1D* h = new TH1D("h","h",10,0.,1.);
   h->Draw();
}

It is fine … but that’s really weird as the histogram created is a point and not related at all to the file …

Hi,
could it be that the TFile takes ownership of the histogram and deletes it when it goes out of scope?

Cheers,
Enrico

1 Like

The code reproducing the problem can be even simpler:

void testPlots(){
   TFile f("new.root", "RECREATE");
   TH1D* h = new TH1D("h","h",10,0.,1.);
   h->Draw();
}

Note that it has to be a named macro.

Acutually, yes, it is that. If you do:

void testPlots(){
   TFile f("new.root", "RECREATE");
   TH1D* h = new TH1D("h","h",10,0.,1.);
   h->SetDirectory(0);
   h->Draw();
}

Then it works (thanks @bellenot :slight_smile: )

1 Like

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