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.
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]
#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).
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()
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.)
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”.
[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()”.
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)?