Storing a histogram after variable rebin

Hi all,

I have a class with a TH1F* member (h_data). At some point in my code I rebin my histogram,

h_data=(TH1F*)h_data->Rebin(9,“mass_jj”,xbins);

Later in another member function, when I try to access this member it gives me a segmentation violation.

I know that rebin returns a new histogram, but I haven’t managed to copy the object in the already initialised h_data member.

Is this possible? In other words… how can I store the result of a rebin (variable size bins) in an already initialised histogram?

Thanks in advance!

Diego.

Is “xbins == 0” or not? If it is not, is it “double xbins[(9 + 1)]” (at least)?

Hi,

xbins!=0

I am trying to use variable bins. And yes xbins size is correct. I don’t have any problems with the Rebin function.

It is just that I can not find a way to copy the returned object into h_data.

Cheers, Diego.

You do not “copy” any data. You just set a new value of the pointer.

Right before and then also right after the “Rebin” line, try to add:

if (h_data) h_data->Print("base"); else std::cout << "No h_data!" << std::endl;

Hi,

This is what I obtain,

BEFORE REBIN
TH1.Print Name = mass_jj, Entries= 19948, Total sum= 20158.2
Title = Invariant mass di_jet system
NbinsX= 3000, xmin= 0, xmax=3000

AFTER REBIN
TH1.Print Name = mass_jj, Entries= 19948, Total sum= 20158.2
Title = Invariant mass di_jet system
NbinsX= 9, xmin= 0, xmax=3000

But after the rebin, the problem is that when I try to call
h_data->GetNbinsX()

I get a segmentation violation…

In these three places (note: before and after “Rebin” and then before “GetNbinsX”), try to add (compare the returned addresses): std::cout << h_data << std::endl;

Hello,

I fixed the problem. This histogram was owned by a file that I opened previously to the call of Rebin.

Just calling,
TH1::AddDirectory(kFALSE);

Kept the histogram alive!

Thanks!

Well, yes, if you closed the file then also the rebinned histogram was gone.
After the “Rebin”, you could also simply: h_data->SetDirectory(gROOT); // (gROOT) or (0)

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