PyROOT RooDataSet::write, how to print 10 decimal places to text file?

Dear all,

I am using Roofit and PyROOT, I am currently trying to use the RooDataSet::write function to save my RooDataSet to txt file. This dataset is 2D dataset containing a time-of-flight in ns and an integer number.

Here is schematically what I am doing :

root_file = ROOT.TFile.Open(‘myworkspace.root’)
w = root_file.Get(‘w’)
data = w.data(“ds”)
data.write(“myfile.txt”,)

I obtain a text file which is looking like this :

2.39625e+07 0
2.39625e+07 0
2.39625e+07 0
2.39625e+07 0

My problem is that the data in first column is supposed to have 9 decimal places by it is displayed in the text file in scientific notation with only 5 decimals.

Is there a way to make the write function display the full 9 decimals?

Thank you very much in advance.

@StephanH

In the end Python just forwards the double value to C++, which I assume uses standard C++ streams to write to the file.

@StephanH is there a tweak possible? Exporting workspaces to text files might become more relevant… and steering the export might be useful. Or should we just wait for the future workspace serialization?

In C++, you can do this:

std::ofstream filestream(filename);
filestream << std::setprecision(9);
dataset.write(filestream);

Now, the gold question is how to do that in Python.

Maybe (untested):

ROOT.gInterpreter.Declare("""
std::ofstream* makeFileStream(const char* filename, int precision) {
  auto filestream = new std::ofstream(filename);
  *filestream << std::setprecision(precision);
  return filestream;
}
""")
theFile = ROOT.makeFileStream("filename.txt", 9)
dataset.write(theFile)
theFile.close()

You can pass any other c++ stream to the function.

Dear all,

I tested the proposed solution from @StephanH and it works very well in actual fact.

Thank you very much !

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