Root cannot properly import an ascii file

I am facing a rather weird problem, when importing an ascii file into root. The file has the following format

9.998780E-01 0.000000E+00 1.999630 0.000000E+00 2.999390 0.000000E+00 3.999150 0.000000E+00 4.998900 0.000000E+00 5.998660 0.000000E+00 6.998410 0.000000E+00 7.998170 0.000000E+00 8.997920 0.000000E+00 9.997680 0.000000E+00 10.997400 0.000000E+00 11.997200 0.000000E+00 12.996900 0.000000E+00 13.996700 0.000000E+00 14.996500 0.000000E+00 15.996200 0.000000E+00 16.996000 0.000000E+00 17.995701 0.000000E+00 18.995501 0.000000E+00 19.995199 0.000000E+00 20.995001 0.000000E+00 21.994801 0.000000E+00 22.994499 0.000000E+00 23.994301 0.000000E+00

I am using the following simple code to draw a histogram.

[code]#include “Riostream.h”
void HistogramFromAscii(char * file_c){

TString file(file_c);
gROOT->SetStyle(“Plain”);
gStyle->SetOptStat(1111);
gStyle->SetOptFit(1111);

TCanvas *c = new TCanvas(“c”, “c”);
c->SetFillColor(5);
c->SetFrameFillColor(10);

TString dir = gSystem->UnixPathName(gInterpreter->GetCurrentMacroName());
dir.ReplaceAll(“HistogramFromAscii.C”,"");
dir.ReplaceAll("/./","/");
ifstream in;
in.open(TString::Format("%s%s.dat",dir.Data(),file.Data()));

Float_t x,y;
Int_t nlines = 0;
Int_t channels=4096;
//TFile *f = new TFile(“HistogramFromAscii.root”,“RECREATE”);
TH1F *h1 = new TH1F(“h1”,“Histogram From Ascii”,channels,1,channels);
//TNtuple *ntuple = new TNtuple(“ntuple”,“data from ascii file”,“x:y:z”);

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

in.close();
h1->SetLineColor(6);
h1->Draw();
/**h1->SetBins(h1->GetNbinsX(), 0, 1024);
c->Update();
h1->Draw();
h1->Rebin(4);
TString histfilename =TString::Format("%s_Rebined.dat",file.Data());
SingleExportAscii(h1,histfilename);*/
//c->SetLogy();
//c->SaveAs(“alpha_in_Si.pdf”);

}[/code]

The output canvas is the following

I thought that it is very weird, therefore I imported the same file into other software, for instance qtiplot or simNRA and the plot is very different

QTIplot

Why is this happening? What can I do to fix it? Thank you very much in advance!
FC2500.dat (115 KB)

I do not think it’s possible to help without any real data - can you provide it, please?. It’s not even clear from your plots if you have the same number of entries in both or not. And plot types seem to be different.

I have attached the file!
Thank you very much for your time!

[quote=“atha.stam”]I have attached the file!
Thank you very much for your time![/quote]

Hmm…

If you work with bins, use bins (probably, SetBinContent function will help you). With your floating point numbers for X, it happens from time to time that two different values go into the same bin.

Even this can help (while still a nonsense):

[code] while (1) {
in >> x >> y;
if (!in.good()) break;
if (nlines < 5) printf(“x=%8f, y=%8f\n”,x,y);
h1->Fill(int(x),y);
//ntuple->Fill(x,y,z);
nlines++;

}[/code]

may be better:

[code] while (1) {
if (nlines >= channels)
break;

  in >> x >> y;
  if (!in.good()) break;
  
  h1->SetBinContent(nlines++, y);

}
[/code]

or even better use a TGraph to plot x/y sequence.


As Timur said, I think in your case you should not use binned data (histogram).
A TGraph seems more appropriate …

I would like it to be a histogram, because I want to apply some functions provided in TH1, like Smooth() and Rebin().

in addition

h1->Fill(int(x),y);

worked like a charm, while

h1->SetBinContent(nlines++, y);

didn’t give me the desired output, as shown in the next figure

If you use Fill() you fill the bins which means that several different X value can fall in the same bins depending of your binning. Timur and I gave you all the options … up to you to choose.

The Fill() method, surprisingly works better the the SetBinContent(). That’s weird in my opinion, because the

SetBinContent(nlines++, y);

looks more straightforward to me.