Cannot plot data from .dat file

I am trying to draw two histograms from a .dat file. The file contains 3 rows, the first being the “channel” number and the two remaining the bin contents. The file is attached.

When trying to plot the two histograms I get some entries, but not all of them. Although it seems that the data are correctly imported, not all of them are drawn, like the following image illustrates.

My code is the following

[code] #include “Riostream.h”
void telescopeThickness(){

gROOT->SetStyle("Plain");
gStyle->SetOptStat(0000);
gStyle->SetOptFit(1111);
gStyle->SetOptTitle(0);

TPaveText *t = new TPaveText(0.005, 0.995, 0.85, 0.905, "brNDC");//left-up
t->AddText("#delta E detector thickness measurement using ^{244}Cm source");
t->SetBorderSize(1);
t->SetFillColor(gStyle->GetTitleFillColor());

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

TString dir = gSystem->UnixPathName(gInterpreter->GetCurrentMacroName());
dir.ReplaceAll("telescopeThickness.C","");
dir.ReplaceAll("/./","/");
ifstream in;
in.open(Form("%stelescopeThickness.dat",dir.Data()));

Float_t x,y,z;
Int_t nlines = 0;
Int_t channels=1024;

TH1F *h1 = new TH1F("h1","Total",channels,1,channels);
TH1F *h2 = new TH1F("h2","#alpha background",channels,1,channels);
    
while (1) {
in >> x >> y >> z;
if (!in.good()) break;
if (nlines == 494) printf("x=%8f, y=%8f, z=%8f\n",x,y,z);
h1->Fill(x,y);
h2->Fill(x,z);
nlines++;
}
printf("Found %d points\n",nlines);

in.close();

h1->SetLineColor(8);
h1->SetFillColor(8);
h1->SetLineWidth(4);
h1->Draw();
h1->GetXaxis()->SetTitle("Energy[keV]");
h1->GetYaxis()->SetTitle("Events"); 
h1->GetYaxis()->SetRangeUser(1,2.2);

h2->SetLineColor(kBlue);
h2->SetFillColor(kBlue);
h2->SetFillStyle(3395);
h2->SetLineWidth(2);
h2->Draw("same"); 

TLegend *leg = c->BuildLegend();
//c->SetLogy();   
t->Draw();

//c->SaveAs("alphaBG.pdf");

}[/code]

Any idea on what might be wrong? Thank’s a lot in advance!
telescopeThickness.dat (14 KB)

You create histograms with “xlow = 1” and “xup = 1024”, but “x” values in the “telescopeThickness.dat” file go from “-88.3937” to “+10780.3675”.
Try to play with graphs: [code]{
Double_t x1min, x1max, y1min, y1max;
Double_t x2min, x2max, y2min, y2max;

const char *data_file = “telescopeThickness.dat”;

TGraph *g1 = new TGraph(data_file, “%lg %lg %*lg”);
// g1->SetNameTitle(“g1”, “Total”);
g1->SetNameTitle(“g1”, “Total;Energy[keV];Events”);
g1->ComputeRange(x1min, y1min, x1max, y1max);
g1->GetXaxis()->SetLimits(x1min, x1max);
g1->SetLineColor(8);
g1->SetFillColor(8);
g1->SetFillStyle(3001);
g1->SetLineWidth(2);

TGraph *g2 = new TGraph(data_file, “%lg %*lg %lg”);
// g2->SetNameTitle(“g2”, “#alpha background”);
g2->SetNameTitle(“g2”, “#alpha background;Energy[keV];Events”);
g2->ComputeRange(x2min, y2min, x2max, y2max);
g2->GetXaxis()->SetLimits(x2min, x2max);
g2->SetLineColor(kBlue);
g2->SetFillColor(kBlue);
g2->SetFillStyle(3002);
g2->SetLineWidth(1);

gROOT->SetStyle(“Plain”);
gStyle->SetCanvasColor(5);
gStyle->SetFrameFillColor(10);
gStyle->SetTitleFillColor(10);
gStyle->SetLegendFillColor(19);

TCanvas *c = new TCanvas(“c”, “c”);

gPad->Clear(); // just a precaution
gPad->DrawFrame(TMath::Min(x1min, x2min),
TMath::Min(y1min, y2min),
TMath::Max(x1max, x2max),
1.1 * TMath::Max(y1max, y2max),
"#delta E detector thickness measurement using ^{244}Cm source;Energy[keV];Events");
g1->Draw(“F”); g1->Draw(“LP”);
g2->Draw(“F”); g2->Draw(“LP”);
gPad->Modified(); gPad->Update();

TLegend *l = new TLegend(0.56, 0.61, 0.89, 0.89);
l->AddEntry(g1, “Total”);
l->AddEntry(g2, “#alpha background”);
l->Draw();
gPad->Modified(); gPad->Update();
}[/code] [code]{
Double_t xmin, xmax, ymin, ymax;

const char *data_file = “telescopeThickness.dat”;

TGraph *g1 = new TGraph(data_file, “%lg %lg %*lg”);
g1->SetNameTitle(“g1”, “Total”);
// g1->SetNameTitle(“g1”, “Total;Energy[keV];Events”);
g1->ComputeRange(xmin, ymin, xmax, ymax);
g1->GetXaxis()->SetLimits(xmin, xmax);
g1->SetLineColor(8);
g1->SetFillColor(8);
g1->SetFillStyle(3001);
g1->SetLineWidth(2);

TGraph *g2 = new TGraph(data_file, “%lg %*lg %lg”);
g2->SetNameTitle(“g2”, “#alpha background”);
// g2->SetNameTitle(“g2”, “#alpha background;Energy[keV];Events”);
// g2->ComputeRange(xmin, ymin, xmax, ymax);
g2->GetXaxis()->SetLimits(xmin, xmax);
g2->SetLineColor(kBlue);
g2->SetFillColor(kBlue);
g2->SetFillStyle(3002);
g2->SetLineWidth(1);

TMultiGraph *mg = new TMultiGraph();
// mg->SetNameTitle(“mg”, “Total & #alpha background;Energy[keV];Events”);
mg->SetNameTitle(“mg”, “#delta E detector thickness measurement using ^{244}Cm source;Energy[keV];Events”);
mg->Add(g1);
mg->Add(g2);

gROOT->SetStyle(“Plain”);
gStyle->SetCanvasColor(5);
gStyle->SetFrameFillColor(10);
gStyle->SetTitleFillColor(10);
gStyle->SetLegendFillColor(19);

TCanvas *c = new TCanvas(“c”, “c”);
mg->Draw(“AF”);
c->BuildLegend();
c->Modified(); c->Update();
mg->GetXaxis()->SetLimits(xmin, xmax);
mg->Draw(“LP”);
c->Modified(); c->Update();
}[/code]

I want to use histograms because I want to fill the space from the histogram line to the x-axis.
I was able to draw them, using the following settings

[code]Int_t channels=1024;

TH1F *h1 = new TH1F("h1","with #delta E",channels,1024,channels);
TH1F *h2 = new TH1F("h2","without #delta E",channels,1024,channels);[/code]

and

h1->Fill(x*10.624400-99.018100,y); h2->Fill(x*10.624400-99.018100,z);

as well as

h1->GetYaxis()->SetRangeUser(1,250); h1->GetXaxis()->SetRangeUser(4000,6000);

To be honest I don’t quite understand why is this working, but it does.
What matters to me the most is to understand why is this working!
Any ideas, help or point of direction will be more than welcome!

It seems to me that when you say that you want to “fill the space from the histogram line to the x-axis”, you simply mean that you want to draw them using the “fill area” option.
Note that there exists the “F” plotting option for graphs (and multigraphs) which does what you want (and both example macros from my previous post above use this option, too).

What if I want to apply a different re-binning?
Will I be able to do it using a TGraph object?

And what about, making it work?
I don’t understand why it is working!!!

If you insist on using histograms, then try (“x” values from the “telescopeThickness.dat” file are not needed at all): // ... Double_t xlow = 1.0; // 1.0 or 0.5 or 0.0 TH1F *h1 = new TH1F("h1", "Total", channels, xlow, (channels + xlow)); TH1F *h2 = new TH1F("h2", "#alpha background", channels, xlow, (channels + xlow)); // ... h1->Fill((nlines + xlow + 0.5), y); h2->Fill((nlines + xlow + 0.5), z); // ...

That is exactly my point!
I don’t want to have channel numbers but I want to have a “calibrated” spectrum, that is why I am using a transformation on the x-axis

h1->Fill(x*10.624400-99.018100,y); h2->Fill(x*10.624400-99.018100,z);

So if I use your code, with the calibrated transformation, i.e.

// ... Double_t xlow = 1.0; // 1.0 or 0.5 or 0.0 TH1F *h1 = new TH1F("h1", "Total", channels, xlow, (channels + xlow)); TH1F *h2 = new TH1F("h2", "#alpha background", channels, xlow, (channels + xlow)); // ... h1->Fill((nlines + xlow + 0.5)*10.624400-99.018100, y); h2->Fill((nlines + xlow + 0.5)*10.624400-99.018100, z); // ...

the output is

It seems to me that you want:
(x + 99.018100) / 10.624400 + xlow - 0.5

Even if I do

h1->Fill((x + 99.018100) / 10.624400 + xlow - 0.5, y); h2->Fill((x + 99.018100) / 10.624400 + xlow - 0.5, z);

The output is very weird and not compared to what I want to draw. The range is 1-1024 and the binning is not as appropriate