// compile with g++ $(root-config --cflags) -o RooHistPdf_and_derived_variables_1 RooHistPdf_and_derived_variables_1.cc $(root-config --libs) -lRooFitCore -lRooFit #include "RooAddPdf.h" #include "RooDataSet.h" #include "RooExponential.h" #include "RooGaussian.h" #include "RooPlot.h" #include "RooFormulaVar.h" #include "RooDataHist.h" #include "RooHistPdf.h" #include "RooPolyVar.h" #include "RooRealSumPdf.h" #include "RooRealVar.h" #include "RooBinning.h" #include "TH1D.h" #include "TCanvas.h" int main(int argc, char const *argv[]) { // Elementary variables with event weight RooRealVar px("px","px",10,0,100); RooRealVar py("py","py",10,0,100); RooRealVar weight("weight","weight",10,0,100); // Create RooDataSet for unbinned data RooDataSet ds("ds", "ds", RooArgSet(px, py, weight), "weight"); // Fill the data set int n_vals = 3; std::vector px_vals{2.0, 7.0, 11.0}; std::vector py_vals{4.58257569495584, 13.2664991614216, 22.44994432064365}; std::vector weight_vals{3, 1, 0.1}; for(int i = 0; i < n_vals; ++i) { px.setVal(px_vals[i]); py.setVal(py_vals[i]); ds.add(RooArgSet(px, py), weight_vals[i]); } // Derived variables RooFormulaVar pt_func("pt","sqrt(px**2 + py**2)", RooArgList(px,py)); // This is the important line to get the derived variable in the data set auto pt = (RooRealVar*) ds.addColumn(pt_func) ; // Set the binning RooBinning pt_bins(10,0,100); pt->setBinning(pt_bins); // Get some into pt->Print(); ds.Print(); // Check the values and binning TCanvas canvas("canvas", "canvas", 500, 500); ds.createHistogram("pt")->Draw(); canvas.SaveAs("canvas.png"); // This does now work. RooDataHist rdh("rdh","rdh", *pt, ds); // Make a RooHistPdf RooHistPdf pdf("pdf", "pdf", *pt, rdh); return 0; }