Dear Rooters,
consider the following situation: there is a distribution F that describes my spectrum. I want to simulate how the spectrum will appear in the data (that means:I want to add the statistical error to the theoretical spectrum).
I try to do it manually, adding/subtracting to each bin content a random value from a Gaussian with mean 0 and sigma sqrt(N), where N are the counts of the bin (i.e. the bin content):
void test_1() {
Int_t nBins = 500;
Float_t xMin = 1.94;
Float_t xMax = 2.14;
Float_t BinW = (xMax-xMin)/nBins;
TF1 * f_MI = new TF1 ("f_MI","[0]*[1]/(2*TMath::Pi()*((x-[2])^2+[1]^2/4))",xMin,xMax);
f_MI->SetParameter(0,1.0);
f_MI->SetParameter(1,0.01);
f_MI->SetParameter(2,2.05);
TCanvas *c1 = new TCanvas ("c1","c1");
TH1F *h1 = new TH1F("H_with_statErr","H_with_statErr",nBins,xMin,xMax);
gRandom = new TRandom3();
Double_t r, sigma, x, y;
for (Int_t i=0;i<nBins;i++) {
x = i*BinW+xMin;
y = f_MI->Eval(x);
sigma = sqrt(y);
TF1 *fGaus = new TF1("fGaus","TMath::Gaus(0,[0])",0,5);
fGaus->SetParameter(0,sigma);
r = fGaus->GetRandom();
y = f_MI->Eval(x)+r;
h1->SetBinContent(i,y);
}
h1->Draw();
}
and with the method TH1F::FillRandom():
void test_2() {
Int_t nBins = 500;
Float_t xMin = 1.94;
Float_t xMax = 2.14;
Float_t BinW = (xMax-xMin)/nBins;
TF1 * f_MI = new TF1 ("f_MI","[0]*[1]/(2*TMath::Pi()*((x-[2])^2+[1]^2/4))",xMin,xMax);
f_MI->SetParameter(0,1.0);
f_MI->SetParameter(1,0.01);
f_MI->SetParameter(2,2.05);
TCanvas *c1 = new TCanvas ("c1","c1");
TH1F *h1 = new TH1F("H_FillRandom","H_FillRandom",nBins,xMin,xMax);
h1->FillRandom("f_MI",1000);
h1->Draw();
}
I would have expected the same results… but it is not the case.
Why? How does FillRandom work precisely?
Thanks