Adding histogram to RooPolynomial

Hello everyone!

I am trying to add a histogram to a RooPolynomial (straight-line, first order) but it doesn’t seem to work with the command:

h1->Add(poly,+1);

where “poly” is defined as RooPolynomial poly

On the other hand, when I define “poly” in root as a TF1* poly, the command h1->Add(poly,+1); works well!

Is there any other way to define poly in RooFit so that I can add it to h1? Or, a different command to add these two?

Thank you very much!

Hi!

You can’t add the RooPolynomial directly, because it is not a TF1.

What you need to do is to first create a TF1 that wraps the RooPolynomial using RooAbsReal::asTF(). You can use it like `poly->asTF.

I hope this works for you, let me know if you have further questions!
Jonas

Hi @jonas,

Thank you for your reply and suggestion! I am somehow not able to implement the correct syntax for RooAbsReal::asTF() the way you have mentioned it. I looked at its definition in the root documentation and also tried looking for examples online to use it correctly but without any avail. Here’s my RooPolynomial:

RooRealVar E("E","Energy",2190,2260,"keV");
RooRealVar c0("c0","c0", -1e5, -1e4);
RooRealVar c1("c1","c1", 1e2, 1e5);
RooPolynomial poly("poly","poly",E,RooArgList(c0,c1));

How to correctly create a TF1 out of this poly as you mentioned? Can you please help me with the syntax? Thank you again!!

Ah! Sorry, in my suggestion I assumed poly was a pointer.

This should work in this case:

    std::unique_ptr<TF1> polyf{poly.asTF({E}, {c0, c1})};

You have to pass the observables and parameters. I also suggest to wrap the returned object in a std::unique_ptr, such that you don’t have to delete it manually.

In your call to TH1::Add(), you can then use this smart pointer like Add(polyf.get()).

Does that work for you?

Hi @jonas,

I tried it to do the exact way you have defined it,

std::unique_ptr<TF1> polyf{poly.asTF({E}, {c0, c1})};
h1->Add(polyf.get());

and as well as

std::unique_ptr<TF1> polyf{poly.asTF({E}, {c0, c1})};
h1->Add(polyf.get(),1);

going by the definition of TH1::Add() but I keep on getting the error:

Error: Symbol polyf is not defined in current scope 
Error: Failed to evaluate polyf.get()

I later even included the header #include <memory> (which I forgot to include before) since there’s a unique_ptr, but the error persists. Any further help is highly appreciated, thank you!

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