*** Break *** segmentation violation ->

Hi everybody,

I wrote a script whose aims are to:

  • import a file with values in one column
  • draw a 1D histogram
  • make an adaptive kernel estimation of the PDF
  • select at random values following the PDF
  • export the new data set
  • make tests (Kolmogorov - Smirnov and Chi2)

The input file contains data like that:

But I get the following outcome:

I tried many things to make it run but without any success :cry:
Do someone have an idea to save me ?

Thanks in advance for help !

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <stdlib.h>     /* atof */
#include <algorithm> // std::min_element
#include <iterator>  // std::begin, std::end
#include "TH1.h"
#include "TCanvas.h"
//#include "TRandom.h"
#include "RooRandom.h"
#include "RooDataSet.h"
#include "RooRealVar.h"


//*********************************************************
#ifndef __CINT__
#include "RooGlobalFunc.h"
#endif
#include "RooRealVar.h"
#include "RooDataSet.h"
#include "RooGaussian.h"
#include "RooConstVar.h"
#include "RooPolynomial.h"
#include "RooKeysPdf.h"
#include "RooNDKeysPdf.h"
#include "RooProdPdf.h"
#include "TCanvas.h"
#include "TAxis.h"
#include "TH1.h"
#include "RooPlot.h"
 #include "TTree.h"
using namespace RooFit ;
using namespace std ;
//*********************************************************



//********Global definitions*******************************
string line;
int i=1;
vector<double> myvector;
string paramName = "Eff_Grappes";
string fileName = "ecarts." + paramName + ".dat";



//********main program*************************************

void ReSamplesGen()
  {
	      cout << "10\n";
	      
    //lines import and conversion from string to double
    ifstream stream(fileName.c_str());
    getline(stream, line); // we dont read first three lines of the file
    getline(stream, line);
    getline(stream, line);    
	      cout << "11\n";
	      
    while (getline( stream, line ) )
      {
	cout <<"Ecart C/M[" << i << "] = " << line << endl;
	double expr =atof(line.c_str());
	myvector.push_back (expr);  // putting of shifts C/M to the vector
	i++;
      }
    stream.close();    
	      cout << "12\n";

    
    //definition of min max and size
    double mini = myvector[0];
    double maxi = myvector[0];   
    int Nel = myvector.size();
	      cout << "15\n";

    for (int n= 1; n < Nel; ++n)
      {
	mini = std::min(myvector[n], mini);
	maxi = std::max(myvector[n], maxi);
      }
	      cout << "20\n";    
	      
	      
    int nbin = (int) log(Nel)/log(2) + 1 ;
    cout << endl << "Numero de bins pour l'histogramme selon la formule de Sturges = " << nbin << endl << endl;
	      cout << "21\n";

	      
    //histogram definition
    TH1D * h1 = new TH1D("h1",paramName.c_str(),nbin,mini,maxi);
    for (int j=0; j<Nel; j++)
      {
	h1->Fill(myvector[j]);
      }
	      cout << "3";
	      

    //tree definition
    TTree *tree = new TTree("input data", fileName.c_str());
    double aze;
    tree->Branch("x", &aze);
      for (int k=0; k<Nel; k++)
	{ 
	  aze=myvector[k]; 
	  tree->Fill();
	}
	      cout << "35";

    RooRealVar x("x","input data support",mini,maxi) ;
    RooDataSet *dataset = new RooDataSet( "data_bg","data SM bkg",tree, x );
    RooPlot* frame = x.frame(Title("Mesurments data and Adaptive kernel estimation pdf")) ;
    dataset->plotOn(frame, Binning(nbin)) ;
	      cout << "4";

	      
    // An adaptive kernel estimation pdf on the same data without mirroring option
    RooKeysPdf kest("kest","kest",x, *dataset,RooKeysPdf::NoMirror) ;
    kest.plotOn(frame);

    // Adaptive kernel estimation pdf with increased bandwidth scale factor
    // (promotes smoothness over detail preservation)

    //RooKeysPdf kest2("kest2","kest2",x, *dataset,RooKeysPdf::MirrorBoth,2) ;
    //kest2.plotOn(frame,LineStyle(kDashed),LineColor(kRed)) ; 

    //RooKeysPdf kest3("kest3","kest3",x, *dataset,RooKeysPdf::MirrorBoth,1) ;
    //kest3.plotOn(frame,LineStyle(kDashed),LineColor(kGreen)) ; 
	      cout << "45";
    
	      
    //set the seed for random generator
    RooRandom::randomGenerator()->SetSeed();  
	      cout << "5";
	      

    //generation of the "toy" sample
    int NbSamples = 100000 ;
    RooDataSet* sample = kest.generate(x,NbSamples) ; 

    
    nbin = (int) log(NbSamples)/log(2) + 1 ;
	      cout << "55";
	      
    
    RooPlot* frame2 = x.frame(Title("Adaptive kernel estimation pdf and generated sample")) ;
    sample->plotOn(frame2, Binning(nbin)) ;
    kest.plotOn(frame2);
	      cout << "6";


    //drawings
    TCanvas* c = new TCanvas("kernelestimation","PDF Adaptive Kernel Estimation and Generated sample",800,400) ;
    c->Divide(2,1) ;
    c->cd(1) ; gPad->SetLeftMargin(0.15) ; frame->GetYaxis()->SetTitleOffset(1.4) ; frame->Draw() ;
    c->cd(2) ; gPad->SetLeftMargin(0.15) ; frame2->GetYaxis()->SetTitleOffset(1.8) ; frame2->Draw() ;


    //export data
    fileName = "samples." + paramName + ".dat";
    std::ofstream ofs (fileName.c_str(), std::ofstream::out);

    NbSamples = dataset->numEntries();
    ofs << "Input data from " << fileName << " (" << NbSamples << " samples), keyword for the generated sample is \"***\" " << " : "<<endl<<endl;
    for (int l=0; l<NbSamples; l++)
      { 
	ofs << dataset->get(l)->getRealValue("x") << endl; 	
      }
    NbSamples = sample->numEntries();
    ofs << endl << endl << "Result of sampling of " << NbSamples << " realizations of the " << paramName << ": " << endl ;
	      cout << "7";
}

Your vector “myvector” is empty:

add the check


   if (int(myvector.size()) == 0) {
       printf("Error  myvector has 0 element\n");
       return;
   }

after:

   cout << "12 \n";

and you will get:

root [1] ReSamplesGen()
10
11
12 
Error  myvector has 0 element
root [2] 

Excellent !! It solves everything !

I replace

by

and it works perfectly.

Many thanks !

Phil