# RooFit PDF from a TKDE

**URL:** <https://root-forum.cern.ch/t/roofit-pdf-from-a-tkde/15418>\
**Category:** Roofit and RooStats\
**Created:** [December 28, 2012, 5:46pm UTC](https://root-forum.cern.ch/t/roofit-pdf-from-a-tkde/15418 "2012-12-28T17:46:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dhill](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dhill/32/16961_2.png) [@dhill](https://root-forum.cern.ch/u/dhill)\
**Post date:** [December 28, 2012, 5:46pm UTC](https://root-forum.cern.ch/t/roofit-pdf-from-a-tkde/15418/1 "2012-12-28T17:46:00Z")

</div>

Dear experts,

I have used a TKDE ([root.cern.ch/root/htmldoc/TKDE.html](http://root.cern.ch/root/htmldoc/TKDE.html)) to describe an invariant mass distribution in Monte Carlo which is not easily described by analytical PDFs in RooFit. This has the advantage over a RooKeysPdf that one can bin internally, which greatly increases the speed.

I am wondering whether it is possible in some way to form a RooFit PDF (preferably a parametric one) from a TKDE? It was suggested in this post that it may be possible:

> [@Alternative to RooKeysPDF?](https://root-forum.cern.ch/t/alternative-to-rookeyspdf/12889/1):
>
> I’m creating a shape template using RooKeysPDF from MC, which takes very long time (RooDateset used has 100k entries, so this is pretty normal I guess). Is there some alternative (faster) PDF generation method available inside RooFit (most probably smoothing is not needed due to huge number of entries). Alternatively - is there a way inside RooFit to limit the size of the dataset to given number of events (e.g. take first 5k)? Thank you, Tomasz

but I am not exactly sure how to do it. Any help would be greatly appreciated!

Many thanks,  
Donal

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [January 8, 2013, 2:53pm UTC](https://root-forum.cern.ch/t/roofit-pdf-from-a-tkde/15418/2 "2013-01-08T14:53:59Z")

</div>

Hi,

If you want a KDE sampled in n points, you should call the TKDE::GetFunction method to return the TKDE as a TF1. You can then transform the TF1 in a RooAbsPdf using the RooTfnPdfBinding class  
(see [root.cern.ch/root/htmldoc/RooTFnPdfBinding.html](http://root.cern.ch/root/htmldoc/RooTFnPdfBinding.html) )

If you want to use directly the TKDE, without sampling it, but re-computing it every time, you can then first use the ROOT::Math::Functor1D class to transform the TKDE in a 1D function interface ( ROOT::Math::IBaseFunctionOneDim),  
which can then be used by the class RooFunctor1DPdfBinding to be transformed in a RooAbsPdf.

If you need I could you provide an example on how to do this,

Best Regards

Lorenzo

---

<div class="post-metadata">

**Author:** ![acastell](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/439d5e/32.png) [@acastell](https://root-forum.cern.ch/u/acastell)\
**Post date:** [January 11, 2014, 1:20am UTC](https://root-forum.cern.ch/t/roofit-pdf-from-a-tkde/15418/3 "2014-01-11T01:20:06Z")

</div>

[quote=“moneta”]

If you need I could you provide an example on how to do this,

Best Regards

Lorenzo[/quote]

Hi Lorenzo, could you indeed do that?

Thanks

Antonio

---

<div class="post-metadata">

**Author:** ![fsili](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/6de8d8/32.png) [@fsili](https://root-forum.cern.ch/u/fsili)\
**Post date:** [March 17, 2022, 7:44pm UTC](https://root-forum.cern.ch/t/roofit-pdf-from-a-tkde/15418/4 "2022-03-17T19:44:39Z")

</div>

Hi Antonio,

could you manage to do this? I am having the same issue and wondering how this can be done.

Thank you very much!  
Francisco

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [March 18, 2022, 4:43pm UTC](https://root-forum.cern.ch/t/roofit-pdf-from-a-tkde/15418/5 "2022-03-18T16:43:20Z")

</div>

Hi Francisco,

Here is an example where you can use TKDE to build a RooAbsPdf.  
If you have any question please let me know,  
[test\_RooKeys.C](https://root-forum.cern.ch/uploads/short-url/k9Td1mVBhro87mggCONZQquMvbT.C) (1.6 KB)

Cheers

Lorenzo

---

<div class="post-metadata">

**Author:** ![fsili](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/6de8d8/32.png) [@fsili](https://root-forum.cern.ch/u/fsili)\
**Post date:** [April 7, 2022, 7:43pm UTC](https://root-forum.cern.ch/t/roofit-pdf-from-a-tkde/15418/6 "2022-04-07T19:43:31Z")

</div>

Hi Lorenzo,

thank you very much!

Cheers,  
Francisco
