I am trying to fit a Gaussian curves to multiple sets of points, creating separate fits in separate graphs in a for loop. The first iteration of the loop runs fine: the fit is performed and fit parameters are produced. However, on the second iteration of this loop, the program crashes with the error “A breakpoint instruction (__debugbreak() statement or a similar call) was executed in {program}.exe.”. Strangely, this happens AFTER the fit parameters are displayed in debug output the second time.
My code is below. Some sample data is manually entered, the same for each iteration, but this happens still when the data is different for each iteration.
struct GaussianResult
{
double centroid;
double amplitude;
double width;
double chisq;
};
GaussianResult GaussianFitErrorGraph(std::vector<double> xData, std::vector<double> yData, std::vector<double> yError, double resolution, int sequenceNumber)
{
int nPoints = yData.size();
std::string graphName = "hist" + std::to_string( sequenceNumber );
TGraphErrors* graph = new TGraphErrors();
for( int i = 0; i < nPoints; ++i )
{
graph->SetPoint( i, xData[ i ], yData[ i ] );
graph->SetPointError( i, 1, yError[i] );
}
std::string fitFuncName = "fitFunc" + std::to_string( sequenceNumber );
TF1* fitFunc = new TF1( fitFuncName.c_str(), "gaus", 0.0, xData.size() );
graph->Fit( fitFunc );//Always crashes here the second time.
double centroid = fitFunc->GetParameter( 1 );
double width = fitFunc->GetParameter( 2 );
double magnitude = fitFunc->GetParameter( 0 );
double chiSquare = fitFunc->GetChisquare();
double nDF = fitFunc->GetNDF();
double reducedChiSquare = chiSquare / nDF;
GaussianResult result { centroid, magnitude, width, reducedChiSquare };
delete fitFunc;
delete graph;
return result;
}
int main()
{
{
int j = 0;
while(j < 10)
{
std::vector<double> intensities1 { 34.000000000000000, 35.000000000000000, 38.000000000000000, 40.000000000000000, 35.000000000000000, 37.000000000000000, 41.000000000000000, 49.000000000000000, 56.000000000000000, 132.00000000000000, 179.00000000000000, 181.00000000000000, 187.00000000000000, 190.00000000000000, 193.00000000000000, 191.00000000000000, 193.00000000000000, 196.00000000000000, 192.00000000000000 };
std::vector<double> contrasts1, contrasts2, xs, yErrors;
for( int i = 0; i < intensities1.size() - 1; i++ )
{
contrasts1.push_back( intensities1[ i + 1 ] - intensities1[ i ] );
yErrors.push_back( sqrt( intensities1[ i + 1 ] + intensities1[ i ] ) );
xs.push_back( i );
}
GaussianResult oneResult = GaussianFitErrorGraph( xs, contrasts1, yErrors, 1.0, j );
j++;
}
}
}
ROOT Version: root_v6.30.04.win64.vc17
Platform: Windows 10
Compiler: Visual Studio 2022