I am trying to selectively plot some data from a file with column, which isn’t .csv or .tsv
A typical file is http://pastebin.com/pgSjezdh
You can see that there is some info at first, which of course should be somehow skipped.
Then there are some column, from which I would like to plot one over the other. For instance the first column being x and the fourth being y.
How to do that?
I think it is also very confusing that latter, the 5 columns become 2 and it’s rather confusing how to pick the disired data.
Of course you need some code to open the file and read it line by line and parse each line to find the first line with “--------” line and the last line with “--------” in between them read the five columns and use the two desired ones. See for example the following two tutorial macros in ROOT how to do something like that:
Thank you very much for your answer!!!
I’ll check your links!!!
In the meantime I got help with a small code on C which does part of the job…
[code]#include <stdlib.h> #include <stdio.h>
#define die(x) do {
fprintf(stderr, “Fatal: %s\n”, x);
exit(1);
} while (0)
#define MAX 10 #define MAXLEN 500
typedef struct { /* Text slice into a char buffer */
const char str; / start pointer /
int len; / slice length */
} Slice;
int main(int argc, char *argv[])
{
FILE f;
int index[MAX]; / column index /
int nindex = 0; / number of columns to write /
Slice cols[MAX]; / column substrings /
int context = 0; / Are we scaning columns? */
int i;
if (argc < 3) die("Usage: col file columns ...");
for (i = 2; i < argc; i++) {
int n = atoi(argv[i]);
if (n < 1 || n > MAX) die("Illegal index");
index[nindex++] = n - 1;
}
f = fopen(argv[1], "r");
if (f == NULL) die("Could not open file.");
for (;;) {
char line[MAXLEN];
if (fgets(line, MAXLEN, f) == NULL) break;
if (context) {
if (line[0] == '-') break;
for (i = 0; i < nindex; i++) {
int j = index[i];
printf(" %.*s", cols[j].len, cols[j].str);
}
putchar(10);
}
if (line[0] == '-') {
const char *p = line;
int n = 0;
while (*p == '-' || *p == ' ') {
cols[n].str = p;
while (*p == '-') p++;
cols[n].len = p- cols[n].str;
while (*p == ' ') p++;
if (++n == MAX) break;
}
for (i = 0; i < nindex; i++) {
if (index[i] >= n) die("Columns index out of range");
}
context = 1;
}
}
fclose(f);
return 0;
}[/code]
It is run using
where data.txt is the “ascii” file and the numbers 2 4 6 indicate which columns are going to be processed.
The compile is done by using
The thing is that the output columns contain their units. Is there a way to get rid of the units and print the output in a seperate file?
Is there a chance that the execution of this code, could become in a macro along with the plot of the desired columns?