TGraph2D array

Hello!

I am trying to create a TGraph2D array and fill the individual elements in a loop. Then I want to plot them. Here is my code:

[code]void databaseerzeugung()
{

Int_t m = 100;
Int_t l = 1;
char histname[m];
char filename[m];
char canvasname[m];
char graphname[m];
int n=0;
double p;
double mean, meana;

TGraph2D** graph =new TGraph2D* [10];
TCanvas** canvas = new TCanvas* [10];

int x, y;
int j;
int i=0;
int o;
int earray[4]={5, 55, 105, 155};

FILE* database;
database = fopen(“database.txt”, “a+”);

for (o=0;o<4;o++)
{

  int energy = earray[o];
  graph[o] = new TGraph2D();

  for (x=-200; x<200; x=x+10)
{


  for (y=-200; y<200;y=y+10)
    {
  
      TH1F *hist_Deg = new TH1F("h1", "title", 250, -0.5, 249.5);

      for (j=5000; j<5010; j++)
	{
	  sprintf(filename,"electronsrough/%d.000000MeV/x%d/y%d/process%d.txt", energy,x,y,j);
	  cout<<filename<<endl;       
	  FILE* pfile =fopen(filename,"r");

	  while(!feof(pfile))
	    {
	      fscanf(pfile, "%lf /n", &p);
	      hist_Deg->Fill(p);
	    } 
	  fclose(pfile);

	}
	  	
      meana = hist_Deg->GetMean();
      graph[o]->SetPoint(i, x, y, meana);
      i=i+1;

      fprintf(database, "%d %d %d %f\n", energy, x, y, meana);
      printf("%f\n", meana);
      fflush(database);
      delete hist_Deg;

    }  
}

sprintf(canvasname,"%d", energy);
canvas[o] = new TCanvas(canvasname);

sprintf(graphname,"%d", energy);
graph[o]->SetTitle(graphname);
graph[o]->GetXaxis()->SetLimits(-200,200);
graph[o]->GetYaxis()->SetRange(0,200);
graph[o]->GetXaxis()->SetTitle(“X”);
graph[o]->GetYaxis()->SetTitle(“Y”);
graph[o]->GetZaxis()->SetTitle(“Z”);
gStyle->SetPalette(1);
graph[o]->Draw(“surf1”);

}

}[/code]

But only the first TGraph2D in the o-loop is properly drawn. For the others I just get a canvas with axis.

Do you have any ideas?

Cheers and thanks to all!!!

At least:

  1. instead of:
    TGraph2D** graph =new TGraph2D* [10];
    TCanvas** canvas = new TCanvas* [10];
    you should have:
    TGraph2D *graph[10];
    TCanvas *canvas[10];

  2. instead of:
    fscanf(pfile, “%lf /n”, &p);
    you probably want:
    fscanf(pfile, “%lf \n”, &p);

Thank you! However, it still produces the exact same thing.

You probably also need:
i = 0;
in the beginning of the “for (o=0;o<4;o++)” loop.

Ahhhh! How true! Thank you!!!

I just wonder, why it does not give me axis titles. Hmmm, interesting.

Instead of:
sprintf(graphname,"%d", energy);
try:
sprintf(graphname, “%d;X;Y;Z”, energy);

Thank you! This did the job, but the caption and the numbers on the axis overlap. I am trying

But it does not work.

Here is the whole code:

[code]
#include “TROOT.h”
#include “TApplication.h”
#include “TCanvas.h”
#include “TF1.h”
#include “TPaveText.h”
#include “TText.h”
#include “TGaxis.h”
#include “TAxis.h”
#include “TAttAxis.h”
#include “TH1.h”
#include “TStyle.h”
#include “TLegend.h”
#include “TGraph.h”
#include “TGraph2D.h”
#include <sys/stat.h>
#include <errno.h>
#include <sys/types.h>

double pi = 3.14;

void databaseerzeugung()
{

Int_t m = 100;
Int_t l = 1;
char histname[m];
char filename[m];
char canvasname[m];
char graphname[m];
int n=0;
double p;

double mean, meana;

TGraph2D *graph[10];
TCanvas *canvas[10];

int x, y;
int j;
int o;
int earray[4]={5, 55, 105, 155};

FILE* database;
database = fopen(“database.txt”, “a+”);

for (o=0;o<4;o++)
{
int i = 0;
int energy = earray[o];
graph[o] = new TGraph2D();

  //rough spacial scan
  for (x=-200; x<200; x=x+10)
{


  for (y=-200; y<200;y=y+10)
    {
  
      TH1F *hist_Deg = new TH1F("h1", "title", 250, -0.5, 249.5);

      for (j=5000; j<5010; j++)
	{
	  sprintf(filename,"electronsrough/%d.000000MeV/x%d/y%d/process%d.txt", energy,x,y,j);
	  cout<<filename<<endl;       
	  FILE* pfile =fopen(filename,"r");

	  while(!feof(pfile))
	    {
	      fscanf(pfile, "%lf \n", &p);
	      hist_Deg->Fill(p);
	    } 
	  fclose(pfile);

	}
	  	
      meana = hist_Deg->GetMean();
      graph[o]->SetPoint(i, x, y, meana);
      i=i+1;

      fprintf(database, "%d %d %d %f\n", energy, x, y, meana);
      printf("%f\n", meana);
      fflush(database);
      delete hist_Deg;

    }  

}

//fine spacial scan
for (x=-9; x<16; x=x+1)
{

  for (y=0; y<8;y=y+1)
    {
  
      TH1F *hist_Deg = new TH1F("h1", "title", 250, -0.5, 249.5);

      for (j=5000; j<5010; j++)
	{
	  sprintf(filename,"electronsfine/%d.000000MeV/x%d/y%d/process%d.txt", energy,x,y,j);
	  cout<<filename<<endl;       
	  FILE* pfile =fopen(filename,"r");

	  while(!feof(pfile))
	    {
	      fscanf(pfile, "%lf \n", &p);
	      hist_Deg->Fill(p);
	    } 
	  fclose(pfile);

	}
	  	
      meana = hist_Deg->GetMean();
      graph[o]->SetPoint(i, x, y, meana);
      i=i+1;

      fprintf(database, "%d %d %d %f\n", energy, x, y, meana);
      printf("%f\n", meana);
      fflush(database);
      delete hist_Deg;

    }  

    }


  //sprintf(canvasname,"%d MeV", energy);
  //canvas[o] = new TCanvas(canvasname);

sprintf(graphname,"%d MeV; x[mm]; y[mm]; Number of PEs", energy);
graph[o]->SetTitle(graphname);
graph[o]->GetXaxis()->SetLimits(-200,200);
graph[o]->GetYaxis()->SetRange(0,200);

gStyle->SetPalette(1);

//graph[o]->Draw(“surf1”);

}

TCanvas *c1 = new TCanvas(“c1”, “Detector response electrons rough spacial scan”, 200, 10, 900, 700);

TPad *pad1 = new TPad("5MeV", "5MeV", 0.05, 0.5, 0.5, 0.95);
TPad *pad2 = new TPad("55MeV", "55MeV", 0.5, 0.5, 0.95, 0.95);
TPad *pad3 = new TPad("105MeV", "105MeV", 0.05, 0.05, 0.5, 0.5);
TPad *pad4 = new TPad("155MeV", "155MeV", 0.5, 0.05, 0.95, 0.5);

pad1->Draw();
pad2->Draw();
pad3->Draw();
pad4->Draw();

pad1->cd();
graph[0]->Draw(“pcol”);
graph[0]->GetXaxis()->SetTitleOffset(1.5);
c1->Update();

pad2->cd();
graph[1]->Draw(“pcol”);

pad3->cd();
graph[2]->Draw(“pcol”);

pad4->cd();
graph[3]->Draw(“pcol”);
}[/code]