Reading Histogram, Histogram drawing Empty

[quote=“Pepe Le Pew”]TH1F hpx = (TH1F)f->Get(“File_Resident_TH1F_Name_Required_Here”);
[/quote]
How is this different than the syntax in my macro?

TH1D *lab_profile = (TH1D*)fhist->Get("Blab_smear");

I edited as you directed and still am unable to use the histogram, ie. fit the histogram.

[quote]TH1F *hpx;
f->GetObject(“File_Resident_TH1F_Name_Required_Here”, hpx);
if (!hpx) { std::cout << “Histogram NOT found!” << std::endl; }[/quote]

I also have edited to above prescription, and still cannot fit histogram.

EDIT: Since I provided a working macro, are you able to accomplish what I cannot?

The only problem that I see in your macro is … I believe, before you can use your “f2” function, you need to initialize the parameter “0” (e.g. do it right after you create it using “new TF1”):
f2->SetParameter(0, SomeValue); // e.g. put SomeValue = 1

I get no problem with your code (I added “f2->SetParameter(0, 99);”): root [0] .x gethisto.cxx Info in <TCanvas::MakeDefCanvas>: created default TCanvas with name c1 FCN=2.73954e-05 FROM MIGRAD STATUS=CONVERGED 14 CALLS 15 TOTAL EDM=2.72271e-17 STRATEGY= 1 ERROR MATRIX ACCURATE EXT PARAMETER STEP FIRST NO. NAME VALUE ERROR SIZE DERIVATIVE 1 p0 1.00046e+00 3.76929e-02 2.97000e-02 -1.95774e-07

P.S. The “fitexample” contains a line:
TH1F hpx = (TH1F)f->Get(*hpx);
which doesn’t seem to be correct to me … I thought you used the same construct somewhere in your code.

I added the “f2->SetParameter(0,1)” as prescribed

root [0] .x gethisto.C 
Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1
Warning in <Fit>: Fit data is empty

I cannot reproduce your result, and it seems you cannot reproduce mine. Which now leads me to the question as to why?

Are you sure you use exactly the same “beam_Profile.root” file that is attached to your first post?
Is this file present in the subdirectory in which you run the “gethisto.cxx” macro?

[quote=“Pepe Le Pew”]Are you sure you use exactly the same “beam_Profile.root” file that is attached to your first post?
Is this file present in the subdirectory in which you run the “gethisto.cxx” macro?[/quote]

Yes, and yes.

The histogram draws, just will not be fitted.

Can you repost your current “gethisto.cxx” (just to make sure that there’s no any typo and that I can run it, too)?


#include <TFile.h>
#include <TH1.h>
#include <TF1.h>

void gethisto()

{
//Open TFile and get histograms
  
  TFile *fhist = new TFile("beam_Profile.root");

  
  TH1D *lab_profile;
  fhist->GetObject("Blab_smear",lab_profile);
  if (!lab_profile) { std::cout << "Histogram NOT found!" << std::endl; }
  lab_profile->SetDirectory(0);
  fhist->Close();
  lab_profile->Draw("ep");
  TF1 * f2 = new TF1("f2","[0]/x",1.2,5.5);
  f2->SetParameter(0, 99);
  lab_profile->Fit("f2","R");
 
  
  //To draw a line with number fitted by Pep Le Pew
  //TF1 * f3 = new TF1("f3","  1.00046e+00/x",1.2,5.5);
  //f3->Draw("same");

  
}






Again, I get no problem … root [0] .x gethisto.cxx Info in <TCanvas::MakeDefCanvas>: created default TCanvas with name c1 FCN=2.73954e-05 FROM MIGRAD STATUS=CONVERGED 14 CALLS 15 TOTAL EDM=2.72271e-17 STRATEGY= 1 ERROR MATRIX ACCURATE EXT PARAMETER STEP FIRST NO. NAME VALUE ERROR SIZE DERIVATIVE 1 p0 1.00046e+00 3.76929e-02 2.97000e-02 -1.95774e-07

Maybe try:
TF1 *f2 = new TF1(“f2”, “[0]/x”, 1.5, 4.5);

If the problem is still there … can you try another version of ROOT (I tried 5.28 and 5.34)?

If the problem is “unsolved”, report a bug … give the details of your “Operating System” (version) and your compiler (version).

BTW. You should add “delete fhist;” (so that there’s no memory leak) after “fhist->Close();” or simply instead of it (no need to “Close” it, if you “delete” it).

I have tried ROOT versions
5.26
5.30
5.32
5.34

and I get same error with them all.

I will have to report this

Thanks

See resolution at http://savannah.cern.ch/bugs/?97198

Philippe, could you please explain how calling “Sumw2()” may produce a histogram that misbehaves when fitting? And what’s “Sumw()”?

Hi,

By using Sumw2() you will use as errors the values stored in the histogram and not the square root of the bin content. For example a bin with zero content can have a non-zero error when using Sumw2().
This can cause a very different behaviour when fitting. In case of the zero bin, it will be for example excluded from the fit (in case of a default least square fit) if you are not using Sumw2()

Best Regards

Lorenzo

As far as I know, calling “Sumw2()” may never change the contents of any bins of the histogram.
And as you say, “Sumw2()” may actually increase the number of bins that are used in the fit.
The error, after calling “Sumw2()”, was: “Warning in : Fit data is empty”. How come?

(P.S. The histogram present in the root file that’s attached to the first post in this thread contains no bins with zero contents and it nicely goes like “1/x”, like it was created by simply calling this function when filling. I don’t really understand how calling “Sumw2()” could make it misbehave.)

Hi,

How ws created the bin with Sumw2() ? If you just call TH1::Sumw2() and then TH1::SetBinContent without calling also TH1::SetBinError, all the errors of the histograms are zero. In this case the histogram cannot be fitted and you get that error message, “Warning in : Fit data is empty”.

Lorenzo

O.K. That’s a good point. If he used “SetBinContent” then “Sumw2()” might have been lethal.

What is “Sumw()”? He mentions calling “Sumw()” instead of “Sumw2()”.

[quote]O.K. That’s a good point. If he used “SetBinContent” then “Sumw2()” might have been lethal.
[/quote]
Yes, if you don’t call also SetBinError

There is no TH1::Sumw(),

Lorenzo

Well, I’d like to propose that the description of how the “empty bins” are treated in the fit is changed.

For example, in http://root.cern.ch/root/html/TH1.html#TH1:Fit@1 I find:

[quote]The likelihood method has the advantage of treating correctly the empty bins and use them in the fit procedure.
In the chi2 method the empty bins are skipped and not considered in the fit.[/quote]

Well, according to the discussion here, that’s wrong.
Actually it should talk about bins with zero errors, as even if the bin is “empty” but it has a non-zero error, it will be considered in the fit. Moreover, if the bin is non-empty but it’s error is zero, it will be skipped and not considered in the fit.
That concerns the “chi2 method”. The “likelihood method” doesn’t seem to care whether the bin errors are zero or non-zero (it considers all available bins in the fit).
I don’t think I have ever found these informations anywhere in ROOT’s docs.
Of course, if “Sumw2()” has not been called, errors are sqrt(N) and so empty bins automatically have zero errors. However, if “Sumw2()” has been called, then the bin contents and bin errors are “unrelated”.

P.S. I know TH1::Sumw() doesn’t exist. I hoped you could have any idea what he meant by “Sumw()”.

Seeing the fSumw above lead me to comment out my Sumw2(), which lead to the source of the error.

Michael, could you please say how you fill your histogram? Do you use “Histo->Fill(x, w);” or do you use “Histo->SetBinContent(i, w);”? Also, what’s the “Sumw()” that you mention in your bug report reply (not to be confused with “Sumw2()”, which you mention, too)?

I use Histo->SetBinContent(i, w)

What I had done was when I declared the histogram, I initially declared it as follows

  TH1D *B_smear = new TH1D("B_smear","Beam Smear",nbins,q_min,q_max);
  B_smear->SetTitle("Beam smear in c.m."); ///HepData/7694/d47x1y1
  B_smear->GetXaxis()->SetTitle("c.m. [GeV]"); 
  B_smear->GetYaxis()->SetTitle("Profile"); 
  B_smear->Sumw2();
  
  TH1D *Blab_smear = new TH1D("Blab_smear","Beam Smear lab",nbins,ebeam_min,ebeam_max);
  Blab_smear->SetTitle("Beam smear in lab"); ///HepData/7694/d47x1y1
  Blab_smear->GetXaxis()->SetTitle("E_beam [GeV]"); 
 Blab_smear->GetYaxis()->SetTitle("Profile");
  Blab_smear->Sumw2();

Which was when created did not allow me to operate on the produced histograms.

I changed it to

  TH1D *B_smear = new TH1D("B_smear","Beam Smear",nbins,q_min,q_max);
  B_smear->SetTitle("Beam smear in c.m."); ///HepData/7694/d47x1y1
  B_smear->GetXaxis()->SetTitle("c.m. [GeV]"); 
	B_smear->GetYaxis()->SetTitle("Profile"); 
  //B_smear->Sumw2();
  
  TH1D *Blab_smear = new TH1D("Blab_smear","Beam Smear lab",nbins,ebeam_min,ebeam_max);
  Blab_smear->SetTitle("Beam smear in lab"); ///HepData/7694/d47x1y1
  Blab_smear->GetXaxis()->SetTitle("E_beam [GeV]"); 
	Blab_smear->GetYaxis()->SetTitle("Profile");
  //Blab_smear->Sumw2();

Notice I commented out the “Sumw2();”. This enabled me to operate on the histograms that were saved in a ROOT file.

I got the idea that the Sumw2() was the cause of the problem because when Phillipe replied with

I noticed the fSumw was there, and I know the file was created with Sumw2(). That is what I meant by Sumw.

Michael