Pushing back events in an array, for RooDataSet

Dear Roofit users,
I am trying to initialize an arrary of RooDataSet type for iterating over different datasets, but I am not able to add arguments to the vector, hence the datasets are not getting filled up. The code does work without the iteration.
Following is a small snippet of the code -

std::string channelStrings[5]={"SR","AR1","AR2","AR3","extraStringToAvoidSegFault"};
    int numberOfChannels = sizeof(channelStrings)/sizeof(channelStrings[0]);  
    RooDataSet datasets[5];
    RooRealVar masses[5];
    RooRealVar weights[5];
    RooArgSet hs[5];
for(int i=0;i< numberOfChannels;i++){
      std::string mass_str = "mass"+channelStrings[i];
      std::string weight_str = "weight"+channelStrings[i];
      std::string hs_str = "hs"+channelStrings[i];
      std::string dataset_str = "dataset"+channelStrings[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[i]=mass;
      std::cout<<mass.getVal()<<std::endl;
      RooRealVar weight(weight_char, weight_char, 0);
      weights[i]=weight;
      RooArgSet hs_var(hs_char);
      hs_var.add(mass);
      hs_var.add(weight);
      hs[i]=hs_var;
      RooDataSet dataset(dataset_char, "", hs_var, RooFit::WeightVar(weight));
      datasets[i]=dataset;
    }
    int indexSR=std::distance(channelStrings, std::find(channelStrings, channelStrings + sizeof(channelStrings)/sizeof(channelStrings[0]), "SR"));  
for (size_t ievent=0; ievent < TTreePointer->GetEntries(); ievent++) {
      int event = TTreePointer->GetEntry(ievent);
      if(isSR_OS == 1 ) {
        int index=indexSR;
        masses[index].setVal(mass);
        weights[index].setVal(weight);  
        datasets[index].add(hs[index], weights[index].getVal());
      }
}
    TCanvas* c2 = new TCanvas();
      RooPlot* frameSR = masses[indexSR].frame(RooFit::Range(12, 77));
      datasets[indexSR].plotOn(frameSR);

The frame gives 0 events, I would like to know if there is another way to iterate over, by defining arrays or vectors. I have tried vectors as well, but it does not seem to work as well.
Thank you for your help, let me know if you would need more details about the same.
Thanks !

Best,
Shreya

Hi,
The code seems correct to me, but there could be an issue with the assignment operator of RooDataSet and/or RooRealVar. Can you try to store instead an array of RooDataSet pointers, or a vector of smart pointers ?

Best

Lorenzo

Hi @moneta ,
Thanks for your reply and sorry for my reply, we managed to fix the issue :))
Best,
Shreya

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