Segmentation fault with RooDataSet

Hi everyone,

First time posting here. I have an issue that I believe stems from using the .add() function for a RooDataSet.

ROOT Version: 6.20.06
Language: C++
Compiler: gcc 8

The main goal of the macro is to open .root files, read the ttrees inside them, and then fill some RooDataSet variables which will be used later. I am simplifying a bunch of lines of code. What I used to have was multiple lines doing the same thing but for different variables:

  RooRealVar massSR("massSR", "m_{#mu#mu}", 12,77);
  RooRealVar weightSR("weightSR", "weightSR", 0);
  RooArgSet hsSR("hsSR");
  hsSR.add(massSR);
  hsSR.add(weightSR);
  RooDataSet datasetSR("datasetSR", "", hsSR, RooFit::WeightVar(weightSR));

  RooRealVar massAR1("massAR1", "m_{#mu#mu}", 12,77);
  RooRealVar weightAR1("weightAR1", "weightAR1", 0);
  RooArgSet hsAR1("hsAR1");
  hsAR1.add(massAR1);
  hsAR1.add(weightAR1);
  RooDataSet datasetAR1("datasetAR1", "", hsAR1, RooFit::WeightVar(weightAR1));

  RooRealVar massAR2("massAR2", "m_{#mu#mu}", 12,77);
  RooRealVar weightAR2("weightAR2", "weightAR2", 0);
  RooArgSet hsAR2("hsAR2");
  hsAR2.add(massAR2);
  hsAR2.add(weightAR2);
  RooDataSet datasetAR2("datasetAR2", "", hsAR2, RooFit::WeightVar(weightAR2));

  RooRealVar massAR3("massAR3", "m_{#mu#mu}", 12,77);
  RooRealVar weightAR3("weightAR3", "weightAR3", 0);
  RooArgSet hsAR3("hsAR3");
  hsAR3.add(massAR3);
  hsAR3.add(weightAR3);
  RooDataSet datasetAR3("datasetAR3", "", hsAR3, RooFit::WeightVar(weightAR3));

To simplify things, I am instead looping over the different regions I want (SR, AR1, AR2, AR3), and pushing the variables into vectors. I then call the elements within these vectors when I want to use them:

  std::__cxx11::string regionStrings[4]={"SR","AR1","AR2","AR3"};
  std::vector<RooRealVar>masses;
  std::vector<RooRealVar>weights;
  std::vector<RooArgSet>hs;
  std::vector<RooDataSet>datasets;

  for(int i=0;i< sizeof(regionStrings)/sizeof(regionStrings[0]);i++){
    std::string mass_str = "mass"+regionStrings[i];
    std::string weight_str = "weight"+regionStrings[i];
    std::string hs_str = "hs"+regionStrings[i];
    std::string dataset_str = "dataset"+regionStrings[i];
    const char* mass_char = mass_str.c_str();
    const char* weight_char = weight_str.c_str();
    const char* hs_char = hs_str.c_str();
    const char* dataset_char = dataset_str.c_str();
    RooRealVar mass(mass_char, "m_{#mu#mu}", 12,77);
    masses.push_back(mass);
    RooRealVar weight(weight_char, weight_char, 0);
    weights.push_back(weight);
    RooArgSet hs_var(hs_char);
    hs_var.add(mass);
    hs_var.add(weight);
    hs.push_back(hs_var);
    RooDataSet dataset(dataset_char, "", hs_var, RooFit::WeightVar(weight));
    datasets.push_back(dataset);
  }

This seems to work, but there’s an issue when I want to add elements to the datasets. While looping over the elements in my ttrees, I have certain conditions that, once met, will lead to different datasets being filled:

      if(base_mu_Pt->at(0) >= 10 && base_mu_Pt->at(0) <= 20){       
        int index=std::distance(regionStrings, std::find(regionStrings, regionStrings + sizeof(regionStrings)/sizeof(regionStrings[0]), "AR1"));
        masses.at(index).setVal(mass);
        weights.at(index).setVal(weight);
        datasets.at(index).add(hs.at(index), weights.at(index).getVal());   
      }
      if (base_mu_Pt->at(0) > 20 && base_mu_Pt->at(0) <= 40){
        int index=std::distance(regionStrings, std::find(regionStrings, regionStrings + sizeof(regionStrings)/sizeof(regionStrings[0]), "AR2"));
        masses.at(index).setVal(mass);
        weights.at(index).setVal(weight);
        datasets.at(index).add(hs.at(index), weights.at(index).getVal());      
      }
      if (base_mu_Pt->at(0) > 40){
        int index=std::distance(regionStrings, std::find(regionStrings, regionStrings + sizeof(regionStrings)/sizeof(regionStrings[0]), "AR3")); \\get index corresponding to the string "AR3" in my array of regions
        masses.at(index).setVal(mass);
        weights.at(index).setVal(weight);
        datasets.at(index).add(hs.at(index), weights.at(index).getVal());

For the first two conditions things work fine. For the third condition though, I get this error:

 *** Break *** segmentation violation



===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0  0x00007f7ce2f744fc in waitpid () from /lib64/libc.so.6
#1  0x00007f7ce2ef1fb2 in do_system () from /lib64/libc.so.6
#2  0x00007f7ce8aac663 in TUnixSystem::StackTrace() () from /cvmfs/sft.cern.ch/lcg/releases/LCG_97a/ROOT/v6.20.06/x86_64-centos7-gcc8-opt/lib/libCore.so
#3  0x00007f7ce8aaeeb4 in TUnixSystem::DispatchSignals(ESignals) () from /cvmfs/sft.cern.ch/lcg/releases/LCG_97a/ROOT/v6.20.06/x86_64-centos7-gcc8-opt/lib/libCore.so
#4  <signal handler called>
#5  0x000000000000013f in ?? ()
#6  0x00007f7ce3f9e685 in RooAbsCollection::operator=(RooAbsCollection const&) () from /cvmfs/sft.cern.ch/lcg/releases/LCG_97a/ROOT/v6.20.06/x86_64-centos7-gcc8-opt/lib/libRooFitCore.so
#7  0x00007f7ce405454c in RooDataSet::add(RooArgSet const&, double, double) () from /cvmfs/sft.cern.ch/lcg/releases/LCG_97a/ROOT/v6.20.06/x86_64-centos7-gcc8-opt/lib/libRooFitCore.so
#8  0x0000000000414770 in main () at /cvmfs/sft.cern.ch/lcg/releases/gcc/8.3.0-cebb0/x86_64-centos7/include/c++/8.3.0/bits/stl_vector.h:930
#9  0x00007f7ce2ed1555 in __libc_start_main () from /lib64/libc.so.6
#10 0x00000000004182da in _start () at /cvmfs/sft.cern.ch/lcg/releases/gcc/8.3.0-cebb0/x86_64-centos7/include/c++/8.3.0/ext/new_allocator.h:86
===========================================================


The lines below might hint at the cause of the crash.
You may get help by asking at the ROOT forum http://root.cern.ch/forum
Only if you are really convinced it is a bug in ROOT then please submit a
report at http://root.cern.ch/bugs Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.
===========================================================
#5  0x000000000000013f in ?? ()
#6  0x00007f7ce3f9e685 in RooAbsCollection::operator=(RooAbsCollection const&) () from /cvmfs/sft.cern.ch/lcg/releases/LCG_97a/ROOT/v6.20.06/x86_64-centos7-gcc8-opt/lib/libRooFitCore.so
#7  0x00007f7ce405454c in RooDataSet::add(RooArgSet const&, double, double) () from /cvmfs/sft.cern.ch/lcg/releases/LCG_97a/ROOT/v6.20.06/x86_64-centos7-gcc8-opt/lib/libRooFitCore.so
#8  0x0000000000414770 in main () at /cvmfs/sft.cern.ch/lcg/releases/gcc/8.3.0-cebb0/x86_64-centos7/include/c++/8.3.0/bits/stl_vector.h:930
#9  0x00007f7ce2ed1555 in __libc_start_main () from /lib64/libc.so.6
#10 0x00000000004182da in _start () at /cvmfs/sft.cern.ch/lcg/releases/gcc/8.3.0-cebb0/x86_64-centos7/include/c++/8.3.0/ext/new_allocator.h:86
===========================================================

I’m pretty sure the error has to do with the .add() function, as calling all the elements separately produces no issues, i.e.:

masses.at(index).setVal(mass);
weights.at(index).setVal(weight);
datasets.at(index);
//no issues produced

I’ve been trying to use valgrind to get to the bottom of this, but I’ve yet to solve this problem. Hopefully this post wasn’t too long, I tried to just include the important parts. If anyone has any ideas, please let me know! Thanks! :slight_smile:

I think @moneta can help you. But it might be helpful to provide a running example reproducing the crash.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.