Generate 2D histogram from weighted 3D ascii data

Dear List,

I’ve got an MxN matrix dataset which I’ve converted into an ascii file with three columns, x, y and z, where z is the weighted values of f(x,y). Plotting the ascii file using the TGraph2D constructor is not an issue.

But the problem is that I’ve got a batch of similarly structured files that need analysing. TGraph2D seems to be limited in this regard, so ideally I would like to do this using the 2D histograms. However I can’t get an identical result when using 2D histograms. Is there something I am missing perhaps? :frowning:

Any help would be greatly appreciated! :slight_smile:

See below for macro script and output showing the same plot for TGraph2d vs. TH2F

Thanks.

#include "Riostream.h"

void Sample() {
// Reads data from an ascii file with Z as weighted values of function f(x,y)
// and plot as 2D histogram.
   TString dir = gSystem->UnixPathName(gInterpreter->GetCurrentMacroName());
   dir.ReplaceAll("Sample.C","");
   dir.ReplaceAll("/./","/");
   ifstream in;
   in.open(Form("%sMeasured.dat",dir.Data()));
//
//::::::::: 3 coluns of data x, y, z, where z is weighted function f(x,y)  
//
// First use TGraph2D constructor to see what plot looks like:
   TCanvas *c1 = new TCanvas("c1"," ",0,0,700,350);
   c1->Divide(2,1);
   c1->cd(1);
   TGraph2D *g1 = new TGraph2D("Measured.dat");
   g1->Draw("cont1");
//
// Next repeat process by reading in ascii file into 2D histo and plotting
   c1->cd(2);
   Float_t x,y,z;
   Int_t nlines = 0;
//   TFile *f = new TFile("Sample.root","RECREATE");
   TH2F *h2 = new TH2F("h2","Same Measured Data with 2DHisto",100,-130,130,100,-65,195);
   while (1) {
      in >> x >> y >> z;
      if (!in.good()) break;
      h2->Fill(x,y,z);
      nlines++;
   }
   printf(" found %d points\n",nlines);
   in.close();
   h2->Draw("cont1");
//   f->Write();
   c1->Update();
}

For data used to generate plots see
Measured.dat (12.3 KB)


Why ?

BTW the difference can be easily explained just looking at the picture. Your 2D histogram has only a few bins filled with a lot of empty bins around them. Doing a contour on such histogram ends up contouring each bin.

Hi,

Thanks for the response.

One of the things I want to do is to be able to compare differences in z at a given x,y position for any 2 (or more) datasets obtained from measurements under similar conditions. I also want to be able to illustrate these differences using plots. I know that one way to do this could be to write a script to loop over the data file entries to get what I want and then plot using TGraph. But this limits me because I also want the flexibility and access to bin contents that histograms give. TGraph, on the other hand, only gives access to a small numner of functionalities e.g. TGraphErrors and TGraphPainter, which doesn’t help me manipulate bin contents.

Is there a workaround?

Many thanks.

Can we assumed that x and y are on a regular grid (it looks so …) ?

Ok, … putting a more appropriate number of bins helps.

#include "Riostream.h"

void Sample() {
   ifstream in;
   in.open("Measured.dat");

// First use TGraph2D constructor to see what plot looks like:
   TCanvas *c1 = new TCanvas("c1"," ",0,0,700,350);
   c1->Divide(2,1);
   c1->cd(1);
   TGraph2D *g1 = new TGraph2D("Measured.dat");
   g1->Draw("cont1");

// Next repeat process by reading in ascii file into 2D histo and plotting
   c1->cd(2);
   Float_t x,y,z;
   Int_t nlines = 0;

   TH2F *h2 = new TH2F("h2","Same Measured Data with 2DHisto",27,-130,130,27,-65,195);

   while (1) {
      in >> x >> y >> z;
      if (!in.good()) break;
      h2->Fill(x,y,z);
      nlines++;
   }
   in.close();
   h2->Draw("cont4");
   c1->Update();
}

May be should be done in a more clever way but it already gives you the idea…

yes, the grid size is the same (10mm), but the start and end points are different. This is purely down to the region of detectors that were irradiated.

Quick background info to put question into perspective…the data was generated from a cancer patient pre-treatment verification test. An array of detectors is irradiated with a Linear Accelarator using the exact parameters for patient treatment. The output of the detectors (measured) is then compared against the expected value (Expected). This what I want to do using Root.

See sample raw data (mxn = 27x27) here:
Expected.txt (4.6 KB)
Measured.txt (4.6 KB)

See same data rearranged in 3 columns, x, y, z (where z is the dose output from the detector at x,y):
Expected.dat (12.3 KB)
Measured.dat (12.3 KB)

which is the binning I choose in the last example I sent you … :slight_smile:

Not that the COLZ option is surely more appropriate to render the data

Yes I sent my response to you as you were sending me yours.

Your solution is an elegant start. :smiley: !

Many thanks