Root Crashes When Using Ofstream

,

ROOT Version: 6.24.02
Platform: Windows OS (Windows 10)
Compiler: MSVC 19.23.28107.0

I am trying to get my code to output some data to a spreadsheet (e.g. I want all of the “time” data points in one one column or row and all of the “reactivity” and “precursors” (etc.) data points in another row so the data can be parsed automatically).

To be honest I just need to get maximum values at their specified times, but as far as I have learned, C++ has no way of retrieving only a maximum or minimum, and I am stuck using the I/O system.

When I try and write the specified variables to a spreadsheet (in my macro) and run ROOT, it crashes. I’ve included a snippet of my code below.

Does anyone have any suggestions? Thank you very much, and I apologize if this is not very well articulated.

for(long j = 1; j < timeSteps / plotPoints; j++){
			tempTime[j]        = tempTime[j - 1] + deltaTime;
			tempPower[j]       = tempPower[j - 1] +
								 tempPower[j - 1] * ((tempReactivity[j - 1] - dnf1) / ngt) * deltaTime +
								 tempPrecursors1[j - 1] * pdc1 * deltaTime+tempPrecursors2[j-1]*pdc2*deltaTime;
			tempPrecursors1[j]  = tempPrecursors1[j - 1] + tempPower[j - 1] * (dnf1 / ngt) * deltaTime -
								 tempPrecursors1[j - 1] * pdc1 * deltaTime;
			tempPrecursors2[j]  = tempPrecursors2[j - 1] + tempPower[j - 1] * (dnf2 / ngt) * deltaTime -
								 tempPrecursors2[j - 1] * pdc2 * deltaTime;
			tempReactivity[j]  = initReactivity -
								 tcr * (tempTemperature[j - 1] - initTemperature);
			tempTemperature[j] = tempTemperature[j - 1] +
								 rhc * (tempPower[j - 1]) * deltaTime -
								 htr * (tempTemperature[j - 1] - initTemperature) * deltaTime;

			//cout << "Time: "        << tempTime[j]        << "\r\n";
			//cout << "Power: "       << tempPower[j]       << "\r\n";
			//cout << "Precursors1: "  << tempPrecursors1[j]  << "\r\n";
			//cout << "Precursors2: "  << tempPrecursors2[j]  << "\r\n";
			//cout << "Reactivity: "  << tempReactivity[j]  << "\r\n";
			//cout << "Temperature: " << tempTemperature[j] << "\r\n";
			//cout << "\r\n";
			ofstream outfile;
			outfile.open ("example.csv");
			outfile << "time:" <<tempTime[j] << "\r\n";
			outfile.close();
		}

Hi Spencer,

How does it crash? What is the exact error you see?

If it is a segmentation error it’s probably due to array indices going out of range, but it’s better, as already mentioned, if you provide the error you are getting.

Other things:

is that ratio an integer? while it may not give an error, it may not do the number of iterations you want.

And here:

    ofstream outfile;
    outfile.open ("example.csv");

    outfile.close();

you are opening and closing the file at every iteration, and overwriting it, meaning only the last iteration values will be saved, which may slow down things a lot, and I don’t know if it could be an issue given the speed and number of iterations. In any case, better put these lines outside (before/after as appropriate) the ‘for’ loop.

yus,
my apologies for the ambiguous statement. The root window closes unexpectedly.

Hi Spencer,

Do you mean some sort of graphics window (with a histogram maybe) by the “root window”? If so, then the code snippet you posted in the first message does not have anything to do with it, it does not even deal with any histograms.

Do you do anything special to keep the graphics window open? Do you have a minimal reproducer for your issue?