Problem with plotting spectrum

Hi, I am trying to plot a gamma spectrum (Channels vs Counts) making use standard basic.C program. Of course, I have made some adjustments there for my case, but it does not read in data, to my view, correctly. The file, I am reading data from, looks like the case appended below, and I have 2048 channels which are on left-hand column. I have just shown you 10 channels. Anyway, I have adjusted the basic.C program removing z-coordinate, but it is still reading in the 3 rd column, which I do not need, besides it reads only 3000 points. Can anybody help me with this?

Thanks!
Kirill
P.S. My version of basic.C is attached below.

0 0 -222

1 0 -218.6

2 0 -215.2

3 0 -211.8

4 0 -208.4

5 0 -205

6 0 -201.6

7 0 -198.2

8 0 -194.8

9 0 -191.4

10 0 -188
.5

#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("%sCo_source.dat",dir.Data()));

Float_t x,y;
Int_t nlines = 0;
TFile *f = new TFile(“Co.root”,“RECREATE”);
TH1F *h1 = new TH1F(“h1”,“x distribution”,200,-4,4);
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=%f, y=%f”,x,y);
h1->Fill(x);
ntuple->Fill(x,y);
nlines++;
}
printf(" found %d points\n",nlines);

in.close();

f->Write();
}

Could you post your data file and script as attachments?

Rene

Yes, sure. The data file and the code file are attached.

Thanks!
Kirill
Co_source.txt (28.8 KB)
basic.C (958 Bytes)

You must specify the correct file name, ie replace

in.open(Form("%sCo_source.dat",dir.Data())); by

in.open(Form("%sCo_source.txt",dir.Data()));

Rene

No, I am sorry for confusing you, but I was trying to open Co_source.dat file in my directory. The thing is I could not just, for some unknown reason, upload this Co_source.dat format on ROOT TALK to show you. If you try to rename the file from Co_source.txt back to Co_source.dat you will see that it does not work.

Thanks!
Kirill

When I run your script on your data file, I get:

code [136] root -l basic.C
root [0]
Processing basic.C…
x=0.000000, y=0.000000x=-222.000000, y=1.000000x=0.000000, y=-218.600006x=2.000000, y=0.000000x=-215.199997, y=3.000000 found 3072 points[/code]

Rene

That is right! That is what I obtained initially, but I do not think it is right. Please see my first poster. It takes Z ( 3rd column as well) which I try to ignore. I need to plot y vs x (counts vs channels) - 4096 points.

Thanks!
Kirill

Well, if you want to skip the first column, you must specify it when you read.
I suggest the simple change as shown below in the lines marked with //<=====

Rene

[code] int channel;
Float_t x,y;
Int_t nlines = 0;
TFile *f = new TFile(“Co.root”,“RECREATE”);
TH1F *h1 = new TH1F(“h1”,“x distribution”,200,-4,4);
TNtuple *ntuple = new TNtuple(“ntuple”,“data from ascii file”,“x:y”);

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

Thank you very much! I will try this, though, I needed the first column (x). I need to ignore 3rd (z).