Skip to content

Commit 169c77b

Browse files
committed
Guess TGraphErrors text format when not specified
1 parent 854237e commit 169c77b

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

hist/hist/src/TGraphErrors.cxx

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ TGraphErrors::TGraphErrors(const TH1 *h)
223223
/// - format = `"%lg %lg %lg"` read only 3 first columns into X,Y and EY
224224
/// - format = `"%lg %lg %lg %lg"` read only 4 first columns into X,Y,EX,EY.
225225
///
226+
/// If format string is empty, suitable value will be provided based on file extension
227+
///
226228
/// For files separated by a specific delimiter different from ' ' and `\\t` (e.g. `;` in csv files)
227229
/// you can avoid using `%*s` to bypass this delimiter by explicitly specify the `option` argument,
228230
/// e.g. `option=" \\t,;"` for columns of figures separated by any of these characters (`' ', '\\t', ',', ';'`)
@@ -248,18 +250,30 @@ TGraphErrors::TGraphErrors(const char *filename, const char *format, Option_t *o
248250
std::string line;
249251
Int_t np = 0;
250252

251-
if (strcmp(option, "") == 0) { // No delimiters specified (standard constructor).
253+
TString format_ = format;
254+
255+
if (!option || !*option) { // No delimiters specified (standard constructor).
256+
257+
Int_t ncol = 4;
258+
if (format_.IsNull()) {
259+
if (fname.EndsWith(".txt", TString::kIgnoreCase))
260+
format_ = "%lg %lg %lg %lg";
261+
else if (fname.EndsWith(".tsv", TString::kIgnoreCase))
262+
format_ = "%lg\t%lg\t%lg\t%lg";
263+
else
264+
format_ = "%lg,%lg,%lg,%lg";
265+
} else
266+
ncol = CalculateScanfFields(format); //count number of columns in format
252267

253-
Int_t ncol = CalculateScanfFields(format); //count number of columns in format
254268
Int_t res;
255269
while (std::getline(infile, line, '\n')) {
256270
ex = ey = 0;
257271
if (ncol < 3) {
258-
res = sscanf(line.c_str(), format, &x, &y);
272+
res = sscanf(line.c_str(), format_.Data(), &x, &y);
259273
} else if (ncol < 4) {
260-
res = sscanf(line.c_str(), format, &x, &y, &ey);
274+
res = sscanf(line.c_str(), format_.Data(), &x, &y, &ey);
261275
} else {
262-
res = sscanf(line.c_str(), format, &x, &y, &ex, &ey);
276+
res = sscanf(line.c_str(), format_.Data(), &x, &y, &ex, &ey);
263277
}
264278
if (res < 2) {
265279
continue; //skip empty and ill-formed lines
@@ -273,7 +287,6 @@ TGraphErrors::TGraphErrors(const char *filename, const char *format, Option_t *o
273287
} else { // A delimiter has been specified in "option"
274288

275289
// Checking format and creating its boolean equivalent
276-
TString format_ = TString(format) ;
277290
format_.ReplaceAll(" ", "") ;
278291
format_.ReplaceAll("\t", "") ;
279292
format_.ReplaceAll("lg", "") ;

0 commit comments

Comments
 (0)