TGraph2D problem


I’m currently working on a ROOT macro to plot a surface plot using TGraph2D.
What I want to do is put time on the X-axis, the channel number on the Y-axis, and the value of the current at a specified time and channel on the Z-axis.
So I came up with this :

#include <TMath>
#include <TGraph2D>
#include <TRandom>
#include <TStyle>
#include <TCanvas>
#include <fstream>
#include <iostream>
#include <vector>
#include <stdio>
#include <stdlib>
#include <time>
#include <string>

void graph2d()

  TString filename;
  cin >> filename;
  TFile f(filename) ;
  TTree *T = HVtree ;
  TBranch *br ;
  struct thvinfo {
    int sts;
    float im,vm;
    time_t t;

  thvinfo info;
  time_t tfirst;
  T->SetBranchAddress("offset", &tfirst );
  br = HVtree->GetBranch("offset");
  TCanvas *c = new TCanvas("c","Current Distribution",50,50,900,700);
  TGraph2D *dt = new TGraph2D();
  for (int mod = 0; mod < 320; mod++){
    for (int ch = 0; ch <16>SetBranchAddress( Form( "channel_%d", index ), &info);
      br = HVtree->GetBranch( Form( "channel_%d", index) );
      int n = br->GetEntries();
      cout <<"Module" << mod <<"Channel " << ch << endl;  
      vector<float> Ivect;
      vector<float> Tvect;
      for (int k=0 ; k<n>GetEntry(k);
      float* Current = new float[Ivect.size()];
      float* TimeAxis = new float[Ivect.size()];
      for (unsigned m=0 ; m<Ivect>SetPoint(index,TimeAxis,CH,Current);
  char datemp[100];
  struct tm *ptm;   
  ptm = localtime ( &tfirst );
  strftime(datemp, 100, "%Y-%m-%d %H:%M:%S", ptm);
  TDatime da(datemp);

The input ROOT file is organized by another macro like this :

HVtree ----> channel_xxx ------> t,im (which are leaves)

Now my problem : I think I store the values correctly into the floats vectors, but when I execute it I get the error :

Error: Can’t call TGraph2D::SetPoint(index,TimeAxis,CH,Current)

Maybe it’s because the 3 values are not of the same type, I must say I have no idea…

PLease someone help me !

Thank you.

The only signature of TGraph2D::SetPoint is:

void	SetPoint(Int_t point, Double_t x, Double_t y, Double_t z)

make sure you use it.

I do use it, something was lost when I pasted the code…

Here :

  float* Current = new float[Ivect.size()];
  float* TimeAxis = new float[Ivect.size()];
  for (unsigned m=0 ; m<Ivect>SetPoint(index,TimeAxis,CH,Current);

x,y, and z are respectively a float, an int, an a float, is this allowed ?

OK I can’t seem to paste my code, it gets all messed up… I’ll attach the macro.
graph2d.C (1.95 KB)

No this is not allowed… look at the signature … you should give 1 integer a 3 double values (not arrays of floats).

Thank you for the prompt answer.
I understand, so is there any way I can plot Time vs Channel vs Current ??

You can plot what ever you want, just fill the TGraph2D with your data.
SetPoint allows to fill the TGraph2D point by point but you can also use one of the many constructors. See:
Choose the more suitable one for your case.


Following your advice, I chose to put the data in another file (the 3 columns I want) and I’ve decided to use the constructor which read data from a text file.
however ROOT hangs when I start the procedure…

The text file looks like this :

1088146907 803 0
1088146907 802 0
1088146907 793 0.02
1088146907 792 0.03
1088146907 789 0.04
1088146907 788 0.02
1088146907 787 0.05
1088146907 785 0.05
1088146907 781 0
1088146907 777 0.11

with one space between each column.

And I came up with this to plot the 2d graph :

TGraph2D *d = new TGraph2D("filename.txt","%d %d %f");

What am I doing wrong ??

The follwoing example is working for me:


   TGraph2D *d = new TGraph2D("graph2d.dat");
   d->Draw("TRI1 P0");


323 803 0.00
143 802 0.00
133 793 0.02
423 792 0.03
126 789 0.04
123 788 0.02
153 787 0.05
629 785 0.05
153 781 0.00
223 777 0.11

I’ve tried to draw it with options it works.
Also it doesn’t seem to like the format string, so I didn’t put one.
Anyway it works, thank you very much.