This is my first question here so feedback on question style and customs is welcomed.
I am trying to write a macro that will read a .txt file with one column of numbers so I can play around with these data and make pretty graphs and the like, specifically histograms. I am having trouble with the code that reads the file. I am using what is essentially the ‘stock code’ for reading files. Ideally I would like it to read a text file called example.txt and push these values into a vector. This code works fine in plain C++ 11 but doesn’t work in ROOT.
terminate called after throwing an instance of 'std::invalid_argument’
what(): stod
It seems rather obvious that the issue is with the stod() but I can’t seem to fix it. I am using ROOT 6.08/6 on 64 bit ubuntu 16.04. The file example.txt is just a single column of random numbers.
[quote=“Liam_F, post:1, topic:25280”]
This code works fine in plain C++ 11 but doesn’t work in ROOT.
[/quote] For me, it looks like it is vice versa. Your code does not even compile in plain C++11 because there is no standard header called Riostream.h, you would need to include <fstream> instead. (Riostream.h is a ROOT header that just includes <fstream>, <iostream>, and <iomanip>). On the other hand, your code does work correctly in ROOT (at least using ROOT 6.11).
Can you try to add try…catch around the stod to check which line is causing the exception?
while (getline(inputFile,line)){
try {
jets.push_back(stod(line));
} catch (...) {
cout << "Line <" << line << "> cannot be converted\n";
}
}
Also it is at least debatable if getline is “‘stock code’ for reading files” in C++. It usually leads to an extra copy that one can avoid when using operator>> instead.
Also, your code happily converts lines like “42.1 add some junk”.
When using ROOT, I would suggest something different instead:
auto t = new TTree("t", "t");
t->ReadFile("example.txt", "col/D");
Define “the correct way”. Does any correct way count? Does correct imply efficient? What do you need the array for? Do you need the whole array in memory at once? One cannot really answer this question without knowing what you want to do, how large the data is and …
Test stod: just start root and type stod("123.456")! It should look like this:
My suspicion is that your input file is corrupt (maybe some empty line? Some non printable characters?). Before thinking about alternative solutions, find out which line stod cannot convert.