Reading data from file

i want to draw a histogram by reading data from file.
i am attaching here the data file too.please help me to write the code
x-special/nautilus-clipboard
copy
file:///home/ravu/Downloads/Co60.dat

Can you upload you file?

I am unable to attach the file.Please just provide me the code.

Sorry, I cannot help much without see structure of your data.
There are many ways how your data can be coded into text or in binary file.
For instance, TGraph class provide direct way to read points from the file - see this constructor. For other use-cases one need to read file line by line and fill histogram out if it.

Try to rename your file into “Co60.txt” and then attach it here.

1 Like

Co60(1).txt (92.8 KB)

please check the attachment…I have attached

Please explain in few words that is in your file?
And that kind of histogram you want to fill?

Hi,

Here is possible way to draw your data as 2-dim histogram:


And this is macro: draw.cxx (241 Bytes)

#include <fstream>

void draw() {
   TH2F *hist = new TH2F("hist","title", 100, 0., 2000., 100, 0., 2000.);

   std::ifstream infile("Co60.txt");
   int x,y;
   while (infile >> x >> y) {
      hist->Fill(x,y);
   }
   hist->Draw("colz");
}

Regards,
Sergey

I need one dimensional histogram.In the file there are two cou
lumns right… i want two different one dimensional histograms

You see - just base on file data I cannot guess that you need.
But it should be easy to modify macro:

#include <fstream>

void draw2() {
   TH1F *hist1 = new TH1F("hist1","title1", 100, 0., 2000);
   TH1F *hist2 = new TH1F("hist2","title2", 100, 0., 2000);

   std::ifstream infile("Co60.txt");
   int x1,x2;
   while (infile >> x1 >> x2) {
      hist1->Fill(x1);
      hist2->Fill(x2);
   }
   auto c = new TCanvas("canvas", "title", 800, 600);
   c->Divide(1,2);
   c->cd(1);
   hist1->Draw();
   c->cd(2);
   hist2->Draw();
}

And as macro file: draw2.cxx (426 Bytes)

it is showing only canvas

Strange, how you run root macro?
For me, when doing root draw2.cxx, I see:

.x co60.cpp because my macro name is co60

But you also have to rename function name draw2 -> co60.

ya i have done it already

Can you submit your code?

And do you rename your data file or change filename in macro?

co60.cpp (427 Bytes)

Do you have “Co60.txt” file? Or filename still “Co60.dat”.
If so, just provide proper file name.
For me everything works.