Segmentation fault problem with RooWorkspace in a Loop()

Hi all,
I’m trying to fit some datasets wich I obtain using a Loop() created with the ROOT MakeClass. To fit all these distributions I create a convolution model with RooFFTConvPdf. The variable of the distribution is taken from a RooWorkspace in a .root file. When the Loop runs it starts to fit for a while but then the program crashes ending with this message:

followed by a backtrace message and a memory map.
I think there is a problem when I get the variable from the workspace, because if I define a RooRealVar inside the Loop() the program runs correctly, but I don’t undestand where the error is.

I include the entire macro:

[code]#define buggedSis_cxx
#include “buggedSis.h”

#include <TStyle.h>
#include <TCanvas.h>
#include <TPad.h>

#include <stdlib.h>
#include
#include
#include
#include
#include
#include

#include <TChain.h>

#include “TObject.h”
#include “TH1.h”
#include “TKey.h”
#include “TFile.h”
#include “TMath.h”
#include “TError.h”
#include “TDirectory.h”
#include “TTree.h”

#ifndef CINT
#include “RooGlobalFunc.h”
#endif

#include “RooAbsRealLValue.h”
#include “RooRealVar.h”
#include “RooGaussian.h”
#include “RooLandau.h”
#include “RooPlot.h”
#include “RooDataHist.h”
#include “RooNumConvPdf.h”
#include “RooKeysPdf.h”
#include “RooDataSet.h”
#include “RooArgSet.h”
#include “RooArgList.h”
#include “RooFitResult.h”
#include “RooAddPdf.h”
#include “RooFFTConvPdf.h”
#include “RooWorkspace.h”
#include “RooAbsPdf.h”

using namespace std;
using namespace RooFit;

void buggedSis::Loop() {

const Int_t initial_bin = 0;
const Int_t initial_tem = 1;
const Int_t eta_bins = 10;
const Int_t tem_num = 11;

Double_t eta_bin_edge[11]={-2.5,-2.0,-1.4,-1.0,-0.5,0.0,0.5,1.0,1.4,2.0,2.5};

TFile InFile(“ToyTemEta.root”,“READ”);
RooWorkspace* wspace = (RooWorkspace*) InFile.Get(“w”);
RooRealVar* reso = (RooRealVar*) wspace->var(“muonELoss”);

//RooRealVar *reso = new RooRealVar(“reso”,“reso”,-0.8,0.8);

reso->setBins(3000);

RooRealVar meanG(“meanG”,“meanG”,0.001,-0.08,0.08);
RooRealVar sigmaG(“sigmaG”,“sigmaG”,0.05,0.0,0.09);
RooRealVar mpvL(“mpvL”,“Most probable value of Landau”,0.0);
RooRealVar denL(“denL”,“Denominator in parameter lambda”,0.02,0.0,0.05);
RooGaussian gauss(“gauss”,“gauss”,*reso,meanG,sigmaG);
RooLandau landau(“landau”,“landau”,*reso,mpvL,denL);
RooFFTConvPdf conv(“convolution”,“convolution”,*reso,landau,gauss);

if (fChain == 0) return;

Long64_t nentries = fChain->GetEntriesFast();
Long64_t nbytes = 0, nb = 0;
//InFile.Close();

for(Int_t bin_indx=initial_bin; bin_indx<eta_bins; bin_indx++) {

RooDataSet data("Data","Data", RooArgSet(*reso));

for (Long64_t jentry=0; jentry<nentries;jentry++) {
  Long64_t ientry = LoadTree(jentry);
  if (ientry < 0) break;
  nb = fChain->GetEntry(jentry);   nbytes += nb;
  // if (Cut(ientry) < 0) continue;
  
  if(isCombined==1 && (eta>eta_bin_edge[bin_indx]) && (eta<eta_bin_edge[bin_indx+1]) && (nSctHits>=5) && (nPixHits>0) && (combinedPt>5000.) && (combinedPt<20000.) && TMath::Abs((indetP-standaloneP)/indetP)<1.0)  {
    
     
  *reso = (indetP-standaloneP)/indetP;
  data.add(RooArgSet(*reso));
   
  }     
}    
for(Int_t tem_indx=initial_tem; tem_indx<tem_num; tem_indx++) {
  conv.fitTo(data);    
}   

}
}
[/code]

probably it may appear strange to perform the same fit many times in the tem_indx loop but, this is an “approximation” of the original macro I intend to use. I must specify that I need to take the RooRealVar from the workspace because the complete version of the macro is intended to fit models taken from the Workspace and defined with muonELoss variable.

Thank you in advance

Hi Federico,

I created a slightly modified version of your macro that I can run w/o any additional inputs
(where I constuct dummy input data by sampling a Gaussian). In my current ROOT version
(which will be released as ROOT 5.28 tomorrow) I see no problems or crashes. I also ran
with valgrind, which reports to memory errors during the execution of your macro.

You did not mention which ROOT version you used, but several bug fixes have been applied to
both the workspace and RooFFTConvPdf since 5.26, so I suspect the problem that you run
into has already been fixed.

My modified macro is attached for completeness below

                       Wouter

using namespace std;
using namespace RooFit;

void aap() {

const Int_t initial_bin = 0;
const Int_t initial_tem = 1;
const Int_t eta_bins = 10;
const Int_t tem_num = 11;

Double_t eta_bin_edge[11]={-2.5,-2.0,-1.4,-1.0,-0.5,0.0,0.5,1.0,1.4,2.0,2.5};

TFile InFile(“ToyTemEta.root”,“READ”);
RooWorkspace* wspace = new RooWorkspace(“w”,1) ;
wspace->factory(“muonELoss[-10,10]”) ;
RooRealVar* reso = (RooRealVar*) wspace->var(“muonELoss”);

//RooRealVar *reso = new RooRealVar(“reso”,“reso”,-0.8,0.8);

reso->setBins(3000);

RooRealVar meanG(“meanG”,“meanG”,0.001,-10,10);
RooRealVar sigmaG(“sigmaG”,“sigmaG”,3,0.1,10);
RooRealVar mpvL(“mpvL”,“Most probable value of Landau”,0.0);
RooRealVar denL(“denL”,“Denominator in parameter lambda”,1,0.1,5);
RooGaussian gauss(“gauss”,“gauss”,*reso,meanG,sigmaG);
RooLandau landau(“landau”,“landau”,*reso,mpvL,denL);
RooFFTConvPdf conv(“convolution”,“convolution”,*reso,landau,gauss);

for(Int_t bin_indx=initial_bin; bin_indx<eta_bins; bin_indx++) {

cout << "********** THIS IS FIT #" << bin_indx << " **************" << endl ;

RooDataSet data("Data","Data", RooArgSet(*reso));

for (Int_t i=0 ; i<100 ; i++) {
  *reso = gRandom->Gaus(0,3) ;
  data.add(RooArgSet(*reso));
}

for(Int_t tem_indx=initial_tem; tem_indx<tem_num; tem_indx++) {
  conv.fitTo(data);
}

}
}

Hi Wouter,
I used the 5.18 version, which was in fact too old. I moved to the 5.28 and now anything works fine!

Thank you very much for your help,
Federico