Hi
I have a variable whose distribution is described by weights for different regions,
for example
x:[0,10] 0.5
x:[10,20] 0.2
x:[20,30] 0.3
I want to build the PDF for x, I thought I could do it by summing uniform distributions
with the different weights, i.e. uniform distribution in [0,10] with weight 0.5, plus
uniform distribution in [10,20] with weight 0.2, plus etc. etc.
However when I try to do it with the following code, I just get a uniform
distribution for my variable over the whole range:
#include "RooRealVar.h"
#include "RooAddPdf.h"
#include "RooPlot.h"
#include "RooUniform.h"
using namespace RooFit;
void trial4()
{
// add together uniform distributions of some variable in different ranges
RooRealVar x("x","x",0,50);
RooArgList dist,frac;
double f = 0.5;
for(int i=0;i<5;i++)
{
double min = i*10;
double max = (i+1)*10;
x.setRange(min,max);
dist.addClone(RooUniform(Form("u%d",i),"uniform distribution",x));
if(i<4) frac.addClone(RooRealVar(Form("f%d",i),"fraction",f));
}
x.setRange(0,50);
RooAddPdf* pdf = new RooAddPdf("sum","sum of uniform",dist,frac,kTRUE);
pdf->Print("t");
RooPlot* plot = x.frame();
pdf->plotOn(plot);
plot->Draw();
}
Output: (with ROOT v6.02/05)
0x4d35ef0 RooAddPdf::sum = 1 [Auto,Dirty]
0x505d500/V- RooUniform::u0 = 1 [Auto,Dirty]
0x7fff18489eb0/V- RooRealVar::x = 25
0x4df6d60/V- RooRealVar::f0 = 0.5
0x50bafe0/V- RooUniform::u1 = 1 [Auto,Dirty]
0x7fff18489eb0/V- RooRealVar::x = 25
0x4d36e00/V- RooRecursiveFraction::sum_recursive_fraction_u1 = 0.25 [Auto,Clean]
0x5130ca0/V- RooRealVar::f1 = 0.5
0x4df6d60/V- RooRealVar::f0 = 0.5
0x511a100/V- RooUniform::u2 = 1 [Auto,Dirty]
0x7fff18489eb0/V- RooRealVar::x = 25
0x511cef0/V- RooRecursiveFraction::sum_recursive_fraction_u2 = 0.125 [Auto,Clean]
0x4defcc0/V- RooRealVar::f2 = 0.5
0x5130ca0/V- RooRealVar::f1 = 0.5
0x4df6d60/V- RooRealVar::f0 = 0.5
0x512f1d0/V- RooUniform::u3 = 1 [Auto,Dirty]
0x7fff18489eb0/V- RooRealVar::x = 25
0x511d630/V- RooRecursiveFraction::sum_recursive_fraction_u3 = 0.0625 [Auto,Clean]
0x512ea90/V- RooRealVar::f3 = 0.5
0x4defcc0/V- RooRealVar::f2 = 0.5
0x5130ca0/V- RooRealVar::f1 = 0.5
0x4df6d60/V- RooRealVar::f0 = 0.5
0x511be30/V- RooUniform::u4 = 1 [Auto,Dirty]
0x7fff18489eb0/V- RooRealVar::x = 25
0x511de80/V- RooRecursiveFraction::sum_recursive_fraction_u4 = 0.0625 [Auto,Clean]
0x4fcdc90/V- RooConstVar::1 = 1
0x512ea90/V- RooRealVar::f3 = 0.5
0x4defcc0/V- RooRealVar::f2 = 0.5
0x5130ca0/V- RooRealVar::f1 = 0.5
0x4df6d60/V- RooRealVar::f0 = 0.5