Dynamic definition of PDF from sum of gaussians

Dear experts,

I need to performs a loop over some (actually a lot of) histograms, and fit each of them with a superposition of a different number (thought known for each histogram) of gaussians. In other words, I need to define “dynamically” a model, depending on the particular histogram I want to fit (the number of gaussians is read from a configuration file).

Is there a way, within RooFit, to write a function which takes as input the number of gaussians and returns the final PDF (or a pointer to it)? In this case, what kind of object the PDF (or the pointer to it) returned by this function (and which is basically computed through the RooAddPdf method) must be? My first thought was for a RooAbsPdf, but as fas I understood RooAbsPdf is just an abstract interface for PDF, and not a PDF by itself.

Thanks in advance

This may be off topic, but are you describing something similar to a Kernel Density Estimator (KDE)? There is a class to make 1D KDEs available (https://root.cern.ch/doc/v608/classTKDE.html).