TGraph

Hi, If I have 2 variables, I can plot them using the TH2F but using it, ROOT draws an area like this


I know I can use TGraph to have something like this
root.cern.ch/root/html534/guide … 00005D.png

but I’ve a problem. I don’t have ASCII file with data…I just have unformatted files made by CORSIKA and a program to read CORSIKA files and to make root file. The program has many functions, so using the TH1F, TH2F and TH3F, I can declare the histogram at the beginning of the program:

int nbin5=1000; float xmin5=10000.; float xmax5=0.; TH2F *hngameprim = new TH2F("hngameprim","Energia del Primario- Numero di Fotoni;Energia (GeV)];#Fotoni",nbin5,xmin5,xmax5,nbin5,xmin5,xmax5);

and after write and filling them in each function

  hngameprim->Write();
 hngameprim->Fill(qdata[2],qdata[7],w);

Using the TGraph, I can’t use the “Fill”, so I can’t declare the graph at the beginning of the program and after calling it in the functions as I can do for TH1F, TH2F and TH3F…for example I can’t write something like this:

  TGraph *gngameprim = new TGraph("gngameprim","Energia del Primario- Numero di Fotoni;Energia (GeV)];#Fotoni",nbin5,xmin5,xmax5,nbin5,xmin5,xmax5);

and after writing

gngameprim->Write();
  gngameprim->Fill(qdata[2],qdata[7]);

So I wanted to know if it is possible draw a TH2F like a TH1F, or if it is possible to use TH1F class using 2 variables or if exist something like ->Fill(qdata[2],qdata[7]); that I can use with TGraph.
Thanks

Hi,

the method to add a point to a TGraph is SetPoint: root.cern.ch/doc/master/classTG … 45a0b32318

Hi dpiparo and thanks, but there are 2 problems:

  1. I wrote at the beginning of the program

in main() function

gngameprim->Write(); and in other function

if (ok==1) gngameprim->SetPoint(10000,qdata[2],qdata[7]);

compiling the program I don’t have errors, but running it I’ve this error:

it creates the root file, but opening the root file and trying to draw the graph I’ve this error:

and it doen’t draw the plot.

  1. I saw that SetPoint options are

Graph::SetPoint ( Int_t i, Double_t x, Double_t y )

where i is the number of points…just to try I wrote 1000, but the data file isn’t an ascii file, it is an unformatted file, so I can’t see the number of points. So is it possibile to use it without setting i value?
Thanks

// ... create the graph ... TGraph *gngameprim = new TGraph(); gngameprim->SetNameTitle("gngameprim", "Energia del Primario- Numero di Fotoni;Energia (GeV)];#Fotoni"); // ... "append" point(s) ... if (ok==1) gngameprim->SetPoint(gngameprim->GetN(), qdata[2], qdata[7]); // ... save the graph to some opened TFile ... // someTFilePointer->cd(); gngameprim->Write();

Hi pepe thanks, unfortunatly I’ve this error

at this line

gngameprim->SetNameTitle("gngameprim", "Energia del Primario- Numero di Fotoni;Energia (GeV)];#Fotoni");

Thanks

{ // ... create the graph ... TGraph *gngameprim = new TGraph(); gngameprim->SetNameTitle("gngameprim", "Energia del Primario- Numero di Fotoni;Energia (GeV)];#Fotoni"); // ... "append" points ... gngameprim->SetPoint(gngameprim->GetN(), 1, 2); gngameprim->SetPoint(gngameprim->GetN(), 2, 2); gngameprim->SetPoint(gngameprim->GetN(), 1, 1); gngameprim->SetPoint(gngameprim->GetN(), 2, 1); // gngameprim->Draw("AL*"); // ... save the graph to some TFile ... TFile *fout = TFile::Open("gngameprim.root", "recreate"); gngameprim->Write(); delete fout; // ... retrieve the graph from some TFile ... delete gngameprim; // ... first delete the original graph ... TFile *fin = TFile::Open("gngameprim.root"); fin->ls(); fin->GetObject("gngameprim", gngameprim); delete fin; if (gngameprim) gngameprim->Draw("AL*"); }

Hi pepe…the macro works…but if I write that line in the program to read the CORSIKA file, I’ve that error…I don’t know why…

Is there a way to resolve please?

I found the problem…the problem is because I need to declare

TGraph *gngameprim = new TGraph(); gngameprim->SetNameTitle("gngameprim", "Energia del Primario- Numero di Fotoni;Energia (GeV);#Fotoni");

before the main of the program…if I declare the gngameprim in the main I don’t have errors…but I need to declare it at the beginning of the program because gngameprim is used by more functions, not just in the main!