Roofit: adding two datasets, one scalled

I don’t know why this has to be so hard for me, but I can’t perform the simple operation of adding two datasets together, with one scaled.

  double wCCBar;
  ccbar->SetBranchAddress("weight", &wCCBar);
  RooRealVar ccbarweight("ccbarweight", "ccbarweight", wCCBar);

//full background set
RooDataSet *backgroundset = new RooDataSet("datad0_back", "background dataset with d0mass ", ntupleVarSet);

RooDataSet ccbarset = RooDataSet("ccbar_set", "ccbar dataset", ccbar, ntupleVarSet);

//weight the dataset
RooDataSet wgtedccbarset = RooDataSet("weighted_ccbar_set", "weighted_ccbar_set", &ccbarset, ntupleVarSet, 0, "ccbarweight");


//add to total set

Now as soon as I append this weighted dataset (the last line), it looses information about the weighting. I’d like to append several datasets each with their own independent weight, which I will later fit (the complete set), so setwgtVar on backgroundset is not an option. Everytime I try to append the dataset it seems to return to the nonweighted value.

Can anyone help me out with this simple yet frustrating problem?



Wouter will answer your mail once he will be back online.


Hi James,

Sorry for the late reply. I’ve looked at the code that handles the append and it doesn’t handle weights correctly so this is a bug. I’ll fix this a.s.a.p and get back to you.


Thanks for all your hard work Wouter!

I look forward for the update.

Hi James,

I realized that I was looking at the source code of an outdated dev branch
earlier this week. I just looked again at the roofit code at the SVN head and I
think it is already fixed in there. If I run the following test everything appears
to work fine in the head.

RooRealVar x(“x”,“x”,-10,10) ;
RooRealVar w(“w”,“w”,0,10) ;
RooPolynomial p(“p”,“p”,x) ;

RooDataSet* d1 = p.generate(x,1000) ;
w=0.5 ;
d1->addColumn(w) ;
d1->setWeightVar(w) ;

RooDataSet* d2 = p.generate(x,1000) ;
w=0.2 ;
d2->addColumn(w) ;
d2->setWeightVar(w) ;

d1->append(*d2) ;

d1->Print(“v”) ;
d1->tree().Draw(“w”) ;

The fixes that were necessary should have already gone into
5.17/08. Can you give that a try and confirm that all is OK for you in that release?

Thanks, Wouter

Thanks for your help Wouter.

I’m sorry but it’s still not working for me. I’ve parred down my code to the shortest runnable code. Could you glance at my code and see if anything jumps out at you:

void fitd0Mass(double d0Massmin, double d0Massmax,
               double delMassmin, double delMassmax,
               double gamma1PCmsmin, double gamma1PCmsmax,
               double gamma2PCmsmin, double gamma2PCmsmax,
               double dStarPCmsmin, double dStarPCmsmax,
               double pi0Vetomin, double pi0Vetomax,
               TString customVarText = "", double customVarmin = 0, double customVarmax = 0

  //get weights values
  double wCCBar;
  ccbar->SetBranchAddress("weight", &wCCBar);
  RooRealVar ccbarweight("ccbarweight", "ccbarweight", wCCBar);
  double wUds;
  uds->SetBranchAddress("weight", &wUds);
  RooRealVar udsweight("udsweight", "udsweight", wUds);


  int nbins = 40;

  RooRealVar delMass("delMass", "delMass", delMassmin, delMassmax);
  RooRealVar d0Mass("d0Mass" , "d0Mass" , d0Massmin, d0Massmax);

  //cut vars
  RooRealVar gamma1PCms("gamma1PCms", "gamma1PCms", gamma1PCmsmin, gamma1PCmsmax);
  RooRealVar gamma2PCms("gamma2PCms", "gamma2PCms", gamma2PCmsmin, gamma2PCmsmax);
  RooRealVar dStarPCms("dStarPCms", "dStarPCms", dStarPCmsmin, dStarPCmsmax);
  RooRealVar pi0background("pi0background", "pi0background", pi0vetomin, pi0vetomax);

  cout << "d0Mass: " << d0Massmin << " to " << d0Massmax << endl;
  cout << "delMass: " << delMassmin << " to " << delMassmax  << endl;
  cout << "gamma1PCms: " << gamma1PCmsmin << " to " << gamma1PCmsmax << endl;
  cout << "gamma2PCms: " << gamma2PCmsmin << " to " << gamma2PCmsmax << endl;
  cout << "dStarPCms: " << dStarPCmsmin << " to " << dStarPCmsmax << endl;
  cout << "pi0background: " << pi0vetomin << " to " << pi0vetomax << endl;

  //var set
  using namespace RooFit;
  RooArgSet ntupleVarSet(d0Mass, gamma1PCms, gamma2PCms, dStarPCms, pi0background, delMass);

  RooDataSet *backgroundset = new RooDataSet("datad0_back", "background dataset with d0mass ", ntupleVarSet);

  RooDataSet ccbarset = RooDataSet("ccbar_set", "ccbar dataset", ccbar, ntupleVarSet);
  RooDataSet udsset = RooDataSet("uds_set", "uds dataset", uds, ntupleVarSet);

  RooPlot* tempFrame=d0Mass.frame();
  TCanvas* tempcanvas = new TCanvas("beforeuds", "beforeuds"); // make new canvas
  tempFrame->Draw(); // Put our plot on the canvas.

  RooPlot* temp1Frame=d0Mass.frame();
  TCanvas* temp1canvas = new TCanvas("beforeccbar", "beforeccbar"); // make new canvas
  temp1Frame->Draw(); // Put our plot on the canvas.



  RooPlot* temp2Frame=d0Mass.frame();
  TCanvas* temp2canvas = new TCanvas("afteruds", "afteruds"); // make new canvas
  temp2Frame->Draw(); // Put our plot on the canvas.

  RooPlot* temp4Frame=d0Mass.frame();
  TCanvas* temp4canvas = new TCanvas("afterccbar", "afterccbar"); // make new canvas
  temp4Frame->Draw(); // Put our plot on the canvas.



  RooPlot* temp3Frame=d0Mass.frame();
  TCanvas* temp3canvas = new TCanvas("aftertotal", "aftertotal"); // make new canvas
  temp3Frame->Draw(); // Put our plot on the canvas.

I print out 5 plots for a test. I have the two datasets before scaling:

With the scalings that my code prints out:
RooRealVar::ccbarweight[ ] = 0.598669
RooRealVar::udsweight[ ] = 0.967881

I have the plots after the scaling:

Looking at the first bin we see that the scaling is looking correctly:
ccbar:570.59 =~ 34
uds: 27
0.96 =~ 26

So everything seems to be working fine. But when I append uds to ccbar I don’t get the answer I expect (34+26) = 60

As you can see, I’m getting 50 for the first bin.

Is my syntax wrong somewhere? I feel like I’ve tried everything.



Hi James,

The problems is that you give your weight variables different names in the two datasets that you append. If you have a dataset D1(x,w1)
and you append a dataset D2(x,w2), the extended dataset D1 will
continue to have only variables x and w1 as the append operation
only adds data points, not columns to the dataset definition. If w1 is interpreted as event weight in D1 and w2 in D2, things will thus clearly go wrong.

The only correct way out of this is to make sure that if you append weighted datasets the name of the weight variable is the same.


Thank you Wouter,

After using the same weight variable everything works much better. This problem has been bothering me for a month.

Thanks again!