How to Parametric a RooHistPdf

Hi,

I am doing a fit by Roofit.

where mypdf is built by RooHistPdf, I think the RooHistPdf has no parameters (it’s just a non-parametric shape).
But the real pdf is the p*RooHistPdf, and the p is the parameter that needs to be obtained by fitting the data( from the *.root file).
So how to build a pdf to fit data?

My solution is by RooAddPdf, but the only component is RooHistPdf, and the fraction is f1, that is our parameter p.
Unfortunately,the fit result is wrong. And the problem due to the normalization of pdf or the development of RooAddPdf is needed to be considered. I don’t know.

The code is :


#include "RooRealVar.h"
#include "RooDataSet.h"
#include "RooGaussian.h"
#include "RooConstVar.h"
#include "RooPolynomial.h"
#include "RooHistPdf.h"
#include "TCanvas.h"
#include "TAxis.h"
#include "RooPlot.h"
using namespace RooFit;

void paixhistpdf()
{
   // C r e a t e   p d f   f o r   s a m p l i n g
   // ---------------------------------------------
   TH1 *makeTH1();
   TH1 *hh = makeTH1();
   RooRealVar x("x", "x", 0.015,0.045);
	 
   RooDataHist dh("dh","dh",x,Import(*hh));
   RooHistPdf histpdf2("histpdf2", "histpdf2", x, dh, 2);
	
   
//================== MY PDF ===================
   RooRealVar f1("f1","the weight",0.1,0,0.2);
   RooAddPdf model("model","w*HistPdf",RooArgList(histpdf2),RooArgList(f1));


   // =============== samples ====================
   TH1 *sampleTH1();
   TH1 *hzheng10 = sampleTH1();
   RooDataHist samplehist("samplehist","samplehist",x,Import(*hzheng10));

	  

   RooPlot *frame = x.frame(Title("High stats histogram pdf with interpolation"), Bins(300));
	 
//================= Fit ====================
   //model.fitTo(dh);
   model.fitTo(dh,SumW2Error(kTRUE));
   //model.fitTo(dh,PrintLevel(-1),Extended());
   
	samplehist.plotOn(frame);
    model.plotOn(frame);
    model.paramOn(frame,Layout(0.55));
	samplehist.statOn(frame,Layout(0.55,0.99,0.8));

	TCanvas *c = new TCanvas("rf706_histpdf", "rf706_histpdf", 800, 400);
    gPad->SetLeftMargin(0.15);
	frame->GetYaxis()->SetTitleOffset(1.8);
    frame->Draw();
}

 

 //================Creat ROOT TH1D filled with paixy.txt====================
 TH1 *makeTH1()
 {
	 Int_t n = 1000;
     TH1D *hh = new TH1D("H","th1d with weight",n,0.015,0.045);
   // Read file and add to fit object
	    Double_t *x = new Double_t[1000];
	    Double_t *w = new Double_t[1000];
	    Double_t vX, vY;
	    Int_t vNData = 0;
			ifstream vInput;
	    vInput.open("paixy.txt");
	    while (1) {
	      vInput >> vX >> vY;
	      hh->Fill(vX,vY);
          if (!vInput.good())    break;

	      vNData++;
      }//while
	 
	   vInput.close();
    return hh;
 }
  
	//====================== TOY MC samples ==========================
	TH1 *sampleTH1()
	{
    TFile *file1 = new TFile("P = +10%.root","read");
		TH1D *hzheng10;
		hzheng10 = (TH1D*)file1->Get("he5_pfx");

		return hzheng10;
	}
	 

P = +10%.root (12.3 KB)
paixy.txt (41.0 KB)

Hi @hurricane1127,

I am inviting @jonas to this topic as he should be able to provide a concise answer to your issue.

Cheers,
J.

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