Dear Tommaso,
I've something that maybe is useful. It's a function that reads a file the
way you want and returns a double** with all values. The matrix is
organized so that val[0][1] means 2nd value of 1st column so that you can
do:
int cols,lines;
double **vals = readf("myfile.dat",&cols,&lines);
TGraph *gr = new TGraph(vals[0],vals[1],lines);
to create a graf with the 1st and 2nd columns of the data file.
I think it's very ease to you to build a macro that creates the histograms
or whatever you want and save it to a root file.
Maybe its also possible to chande the function so that it returns an
ntuple or something else.
]['s
herique
****************************************
State University at Campinas - BRASIL
"Gleb Wathagin" Physics Institute
Department of Cosmic Rays and Chronology
Henrique Barbosa The Pierre Auger
PhD Student Observatory
****************************************
> Hi all,
> is there anybody who has already wrote a macro capable to read a data file
> organized in columns, no matter how many they are, no matter how long the
> file is and issue a root file?
>
> Thanks, Tommaso
>
>
#include <iostream.h>
#include <fstream.h>
#include <strstream.h>
#include <string.h>
#include <iomanip.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
/* ==================================================================================
*
* function readf - Read a file with 1, 2 or 3 columns
*
* ================================================================================== */
double **readf(char *name, int *ncols, int *nrows) {
int const maxcolums = 20;
int const linwidth = 256;
ifstream arq;
strstream strline;
char line[linwidth];
int sline = sizeof(line);
int i = 0, j = 0;
double firstline[maxcolums];
double **xx;
if ((!(*ncols > 0)) || (!(*nrows > 0))) {
arq.open(name, ios::in);
// Reading first line and determining the number of columns
if (!(*ncols > 0)) {
arq.getline(line,sline);
strline << line;
i++;
do {
strline >> firstline[j++];
} while (!strline.eof());
*ncols = j-1;
strline.clear();
}
// Reading the rest of the file and determining the number of lines
if (!(*nrows > 0)) {
do {
arq.getline(line,sline);
i++;
} while (!arq.eof());
*nrows = i-1;
strline.clear();
}
arq.close();
}
// reopening file for real read
arq.open(name, ios::in);
// allocating memory
xx = new double*[*ncols];
for (j=0;j<*ncols;j++)
xx[j] = new double[*nrows];
// reading file
for (i=0; i<*nrows; i++) {
if ((i%10000)==0) { cout << i << endl; }
arq.getline(line,sizeof(line));
strline << line;
for (j=0;j<*ncols;j++) {
strline >> xx[j][i];
}
strline.clear();
}
arq.close();
cout << "-- Function le_arquivo --\n";
cout << "-- file: " << name << endl;
cout << "-- ncols: " << *ncols << endl;
cout << "-- nlines: " << *nrows << endl;
return(xx);
} // end of **readf(char *name, int *ncols, int *nrows)
This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:50:42 MET