Hi, I’m trying to get a TGraph of the log-likelihood of an exponential distribution, but, when i run the program i get
*** Break *** segmentation violation
Generating stack trace...
0x00005631ffa993ea in _start + 0x2a from ./program
after the part where it draws the histogram, can someone explain why ? thank you.
Here’s the code:
#include <iostream>
#include <fstream>
#include "TH1F.h"
#include "TGraph.h"
#include <cmath>
#include <vector>
#include "TCanvas.h"
double esponenziale (double a, double par)
{
if (par == 0.) return 1. ;
return exp ( a * par) * par ;
}
double logver(std::vector<double> dati, double parametro){
double ver = 0.;
for(int h = 0; h < dati.size(); h++){
ver += log(esponenziale(dati.at(h), parametro));
}
return ver;
}
int main( int argc, char ** argv){
double x;
std::ifstream InFile(argv[1]);
std::vector<double> tempo;
if( InFile.good() == false){
std::cout << "Impossibile leggere file. \n";
return 1;
};
while(true){
InFile >> x;
if(InFile.eof()==true)
break;
tempo.push_back(x);
};
InFile.close();
TH1F istogramma ("istogramma", "Tempo di decadimento", 10, 0., 10.);
int i = 0;
for( i = 0; i < tempo.size(); i++){
istogramma.Fill (tempo[i]);};
istogramma.GetXaxis()->SetTitle("Tempo di decadiemto [s]");
istogramma.GetYaxis()->SetTitle("n. conteggi per bean");
istogramma.SetFillColor( kAzure + 7);
TCanvas c1;
istogramma.Draw();
c1.Print("tempo.png","png");
int l = 1;
std::vector<double> k;
std::vector<double> loglike;
k[0]=0.1;
for( l; l < 10; l++){
k[l]=k[l-1]+0.5;
loglike[l-1] = logver( tempo, k[l-1]);
};
TGraph logl(k.size(), &k[0], &loglike[0]);
TCanvas c2 ("c2", "Loglikelihood", 100, 100, 1000, 1000);
logl.GetXaxis() -> SetTitle("k");
logl.GetYaxis() -> SetTitle("Loglikelihood(k)");
logl.Draw("APC");
c2.Print("likelihood.png", "png");
return EXIT_SUCCESS;
}