Seeds for generated Monte Carlo data in RooFit

Hello,

I have a ROOT macro which uses RooFit to define a PDF (with the help of some distributions read in from a TTree), then generates some toy data using the PDF and fits the PDF to the data. The macro is attached (I am using ROOT v5.16.00).

The macro works fine as it is, but I would like to make it generate many sets of toy data (from the same PDF), then fit to each of these sets in turn so that I can see how well the fitted parameters match to the input parameters.

I have two problems:

  1. I don’t know how to ensure that each set of toy data is independent, i.e. not just a copy of the previous set. Basically I can’t find a way to set the seed for the generated data.

2)Also, if I try to run the generation and fitting in a loop, I get a seg. fault. Is there a way to generate a “vector” of sets of toy data and the pdfs, and generate and fit to each element of the vector in turn?

Thanks,

Laurence Carson
ToyData.C (4.1 KB)

Hi Laurence,

  1. I don’t know how to ensure that each set of toy data is independent, i.e. not just a copy > of the previous set. Basically I can’t find a way to set the seed for the generated data.

RooFit has a private copy of the a random generator, that is created once per session.
Thus if you generate multiple sets of data, they should be all different, but when you
quite root and start again, the first set generated will be the same again as the first
set generated in the previous session (provided nothing else changed.)

You access the random generator and explicitly set the seed at any moment as follows

RooRandom::randomGenerator().SetSeed(xxx)

2)Also, if I try to run the generation and fitting in a loop, I get a seg. fault. Is there a way to > generate a “vector” of sets of toy data and the pdfs, and generate and fit to each element > of the vector in turn?

To debug your SEGV I need a self-contained & complete example to reproduce it.

More generally, there is exist a convient tool in RooFit to do all of the above for you:
class RooMCStudy. Use as follows

   RooAbsPdf* pdf ; // your p.d.f (assumed to be defined previously) 
  RooMCStudy mcs(*pdf,x) ; // x is assumed to be your observable (variable in data)  
   mcs.generateAndFit(100,500) ;  // generate & fit 100 samples of 500 events each

The output can be processed in two ways

mcs.fitParDataSet() returns a RooDataSet with the fitted parameters for each toy

or you can use the utility to immediately make plots of parameter distributions,
error distributions and pull distributions

RooPlot* frame1 =  mcs.plotParam(param) ; // Draw distribution of parameter
frame1->Draw()  

RooPlot* frame2 =  mcs.plotError(param) ; // Draw distribution of parameter error
frame2->Draw()  

RooPlot* frame3 =  mcs.plotPull(param) ; // Draw distribution of parameter pull
frame3->Draw()  

where param is any floating parameter of your model.

Wouter

1 Like