Read floats from a file

I have a file with numbers in it and I want to read those and make a graph of it and find the minumum value.

My code is this

#include <iostream>
#include <fstream>
using namespace std;
#include "TCanvas.h"
#include "TFile.h"
#include "TGraph.h"
#include "TH1.h"
#include "TNtuple.h"

void runnmb_omegap() {
ifstream inFile;"runnmb-time-omegap.txt",ios::in);
Float_t  runnmb, time, wp, wperror;
TNtuple* nt = new TNtuple("nt","runnmb_omegap", "runnmb:time:wp:wperror");

// read the file and store the data in a Ntuple.
while(! {
	inFile >> runnmb >> time >> wp >> wperror;

Float_t lrunnmb, ltime, lwp, lwperror;
const int n=967;
Float_t x[n], y[n];
Double_t minx=0;
Double_t miny=0;
Double_t ymin1;

// get the data from the Ntuple and fill x[n] and y[n] in order to make the graph and find the minumum	
for(Int_t i=0; i<n; i++) {
	if(y[i] < ymin1) { miny = y[i]; minx = x[i]; }
	printf("x=%f y=%f minx=%f miny=%f\n",x[i],y[i],minx, miny);

// make a graph

Reading the file kinda works but it does not have any precision.

The inputfile is (this is only a part of the 967 line file):
[code]11366	4342567.5	61791314.9 15
11367	4346353	  61791312.7 15
11368	4349794	  61791309.1 15
11370	4353513.5	61791305   15
11373	4360240	  61791303   15
11375	4380758	  61791304.3 15
11376	4384057.5	61791307   15
11379	4387444.5	61791306.6 15
11380	4389611.5	61791313.1 15
11381	4390846	  61791311.4 15
11384	4392854	  61791311   15

The ouput is this (x is the first column of the inpuy and y is the third column):

x=11366.000000 y=61791316.000000 minx=11366.00000 miny=61791316.00000
x=11367.000000 y=61791312.000000 minx=11367.00000 miny=61791312.00000
x=11368.000000 y=61791308.000000 minx=11368.00000 miny=61791308.00000
x=11370.000000 y=61791304.000000 minx=11370.00000 miny=61791304.00000
x=11373.000000 y=61791304.000000 minx=11370.00000 miny=61791304.00000
x=11375.000000 y=61791304.000000 minx=11370.00000 miny=61791304.00000
x=11376.000000 y=61791308.000000 minx=11370.00000 miny=61791304.00000
x=11379.000000 y=61791308.000000 minx=11370.00000 miny=61791304.00000
x=11380.000000 y=61791312.000000 minx=11370.00000 miny=61791304.00000
x=11381.000000 y=61791312.000000 minx=11370.00000 miny=61791304.00000
x=11384.000000 y=61791312.000000 minx=11370.00000 miny=61791304.00000

Can you help me reading the file in with precision (only one decimal is needed).

if I do:

I get

[quote]Error: Function setprecision(5) is not defined in current scope makegraph.C:19:
Error: >> Illegal operator for real number makegraph.C:19:
Error: >> Illegal operator for real number makegraph.C:19:
Error: >> Illegal operator for real number makegraph.C:19:
Error: >> Illegal operator for real number makegraph.C:19:[/quote]


You could try to read doubles (and use TNtupleD).


That will give the same output.

Humm … strange … Can you post all your code (and data file) as a tar file?


I also find it strange.
The attachement contains: runnmb_omegap.C and runnmb-time-omegap.txt.
runnmb_omegap.tar (40 KB)

humm … I am confused :slight_smile:. You example still uses Floats …

and indeed if I replace all the Float_t by Double_t and TNtuple by TNtupleD, it works as you wanted. (this is because C++ float do not have enough precision for your data).


It works, thank you.

When I tried to use doubles yesterday I forgot to change x[n] and y[n] to doubles.