Cannot load ascii file

Allό to everyone!

I am trying to reproduce a simple spectrum, by loading an ascii file(with only one column) and creating the relevant histogram.
I am using Rene Brun’s code, but it doesn’t seem to be working in my case.

The code I am using is

[code]#include "Riostream.h"
void basic() {
// Read data from an ascii file and create a root file with an histogram and an ntuple.
// see a variant of this macro in basic2.C
//Author: Rene Brun

// read file $ROOTSYS/tutorials/tree/basic.dat
// this file has 3 columns of float data
TString dir = gSystem->UnixPathName(gInterpreter->GetCurrentMacroName());
dir.ReplaceAll(“basic.C”,"");
dir.ReplaceAll("/./","/");
ifstream in;
in.open(Form("%sD800M655.dat",dir.Data()));

Float_t x;
Int_t nlines = 0;
TFile *f = new TFile(“D800M655.root”,“RECREATE”);
TH1F *h1 = new TH1F(“h1”,“x distribution”,100,-4,4);
TNtuple *ntuple = new TNtuple(“ntuple”,“data from ascii file”,“x”);

while (1) {
in >> x;
if (!in.good()) break;
if (nlines < 5) printf(“x=%8f”,x);
h1->Fill(x);
ntuple->Fill(x);
nlines++;
}
printf(" found %d points\n",nlines);

in.close();

f->Write();
}
[/code]

Running this script produces the message
found 0 points

Any ideas on what may be wrong?

Thanks a lot in advance!

Does the “D800M655.dat” file exist in the same subdirectory where the “basic.C” macro?
If yes, check the very first line of it as it breaks on it.
If this doesn’t help, attach that data file (rename it adding a “.txt” in the end) so that we can have a look at it.

Both basic.c and D800M655.dat are on the same directory.
I am attaching .txt file

P.S.:I also tried with the .txt in case there is a bug with .dat files, but it didn’t work.
D800M655.txt (3 KB)

Do you have “basic.c” or “basic.C”?
With your “D800M655.dat” file and your “basic.C” macro, I Immediately get … “found 1024 points”.

Note, you should have:

TH1F *h1 = new TH1F("h1","x distribution", 100, 0, 10000); ... if (nlines < 5) printf("x=%8f\n",x);

I have basic.c, which is the macro I copied from Rene…
I changed it with basic.C and I also get 1024 points.
The thing is that there is no histogram.
I added

but I don’t get any histo, whatsoever…

Try:
root [0] .x basic.C
root [1] h1->Draw()
root [2] ntuple->Draw(“x”)

BTW. Note that the exact name “basic.C” is required by the line:
dir.ReplaceAll(“basic.C”,"");

Anyhow, usually “.c” means C code, while “.C” means C++ code.

I get a printed canvas!
I tried those lines in the macro file but it’s not working.
Is there a way to print it, using lines in the excecutable macro?

[code]…
f->Write();

TCanvas *c = new TCanvas(“c”, “c”);
h1->Draw();
c->SetLogy(1);
c->SaveAs(“c.pdf”);
}[/code]

It’s not working… :frowning:
I also added

just in case but this isn’t working either…

Try the attached “basic.C” file. I get: root [0] .x basic.C x=0.000000 x=0.000000 x=0.000000 x=0.000000 x=0.000000 found 1024 points Info in <TCanvas::Print>: pdf file c.pdf has been created
basic.C (1.2 KB)

It is working!!! =D>
Thank you so much once again!
However, I can’t see the difference;perhaps it is too late 8)

Thank you, thank you, thank you!!! :smiley:

Note: you can also run this macro as ACLiC precompiled. Try: root [0] .x basic.C++ P.S. maybe you will need to replace “ifstream in;” with “std::ifstream in;” (I’ve forgotten to fix it in the file attached in my previous post).

Now I found another problem.
The histo that’s produced is not the proper.
I don’t know why.

So what I did was to make a new .dat file, with two columns x(channel number), y(weight).
The code I used is the following:

[code]#include
#include
#include

#include “TString.h”
#include “TSystem.h”
#include “TInterpreter.h”
#include “TFile.h”
#include “TH1.h”
#include “TNtuple.h”
#include “TCanvas.h”

void basic() {
// Read data from an ascii file and create a root file with an histogram and an ntuple.
// see a variant of this macro in basic2.C
//Author: Rene Brun

// read file $ROOTSYS/tutorials/tree/basic.dat
// this file has 3 columns of float data
TString dir = gSystem->UnixPathName(gInterpreter->GetCurrentMacroName());
dir.ReplaceAll(“basic.C”,"");
dir.ReplaceAll("/./","/");
ifstream in;
in.open(Form("%sFirstStripSignal.dat",dir.Data()));

Float_t x,y;
Int_t nlines = 0;
TFile *f = new TFile(“FirstStripSignal.root”,“RECREATE”);
TH1F *h1 = new TH1F(“h1”,“x distribution”, 1024, 1, 1024);
TNtuple *ntuple = new TNtuple(“ntuple”,“data from ascii file”,“x:y”);

while (1) {
in >> x >> y;
if (!in.good()) break;
if (nlines < 5) printf(“x=%8f, y=%8f/n”,x);
h1->Fill(x,y);
ntuple->Fill(x,y);
nlines++;
}
printf(" found %d points\n",nlines);

in.close();

f->Write();

TCanvas *c = new TCanvas(“c”, “c”);
h1->Draw();
c->SetLogy(0);
c->SaveAs(“c.pdf”);
}[/code]

In this case, someone can reproduce a spectrum in root!

Try with: TH1F *h1 = new TH1F("h1","x distribution", 1024, 1, 1025); ... if (nlines < 5) printf("x=%8f, y=%8f\n", x, y); or with: TH1F *h1 = new TH1F("h1","x distribution", 1024, 0.5, 1024.5); ... if (nlines < 5) printf("x=%8f, y=%8f\n", x, y);

Perhaps you didn’t see the code :unamused:
I’ve managed to get the spectrum :smiley:

I will attach the spectrum file and the spectrum itself.

Now I have another problem…
I want to fit a specific range and get in the stat box the fitting parameters(centroid and FWHM).
I have tried several lines I found on the web but nothing seems to work.
Here comes the code I am using:

[code]#include
#include
#include

#include “TString.h”
#include “TSystem.h”
#include “TInterpreter.h”
#include “TFile.h”
#include “TH1.h”
#include “TNtuple.h”
#include “TCanvas.h”

void basic() {
// Read data from an ascii file and create a root file with an histogram and an ntuple.
// see a variant of this macro in basic2.C
//Author: Rene Brun

// read file $ROOTSYS/tutorials/tree/basic.dat
// this file has 3 columns of float data
TString dir = gSystem->UnixPathName(gInterpreter->GetCurrentMacroName());
dir.ReplaceAll(“basic.C”,"");
dir.ReplaceAll("/./","/");
ifstream in;
in.open(Form("%sFirstStripSignal.dat",dir.Data()));

Float_t x,y;
Int_t nlines = 0;
TFile *f = new TFile(“FirstStripSignal.root”,“RECREATE”);
TH1F *h1 = new TH1F(“h1”,“x distribution”, 1024, 1, 1024);
TNtuple *ntuple = new TNtuple(“ntuple”,“data from ascii file”,“x:y”);

while (1) {
in >> x >> y;
if (!in.good()) break;
if (nlines < 5) printf(“x=%8f, y=%8f/n”,x);
h1->Fill(x,y);
ntuple->Fill(x,y);
nlines++;
}
printf(" found %d points\n",nlines);

in.close();

f->Write();

gStyle->SetOptStat(1111);

TCanvas *c = new TCanvas(“c”, “c”);
h1->Draw();
h1->Fit(“gaus”,“W”,NULL,200,400);

//TPaveStats st = ((TPaveStats)(h1->GetListOfFunctions()->FindObject(“stats”)));

c->SetLogy(0);
c->SaveAs(“c.pdf”);
}[/code]
c.pdf (21.1 KB)
FirstStripSignal.txt (7.5 KB)

gStyle->SetOptFit(1111);

That was my first option, but it doesn’t seem to be working… :frowning:


basic.C (1.6 KB)

Yessssssssssssssssssssssssss!!! =D>
I can’t really find words to thank you!!!
You are awesome :smiley: