Creating multiple Graphs from one data sheet

Hello,

I’m trying to create multiple graphs from one data file. Data is in form
time amplitude

2e-010,0.653698
4e-010,0.650884
6e-010,0.645658
8e-010,0.644854
1e-009,0.649276
1.2e-009,0.653296
1.4e-009,0.65008
1.6e-009,0.64204
1.8e-009,0.637216
2e-009,0.64003

I succesfully created code that creates a Graph for first 50 pairs (time,amplitude). Every 50 steps there is a new series for total 20 series (1000 lines). My code for one series is:
{
double t[50];/* there are 50 points for each series*/
double v[50];
int N = 50;
int count = 0;/when it’s 50 I have all points in the series/
int series = 0;/*there are 20 series */

TGraph *grpreamp = new TGraph();
fstream file;
file.open(“hg_preamp_mod.txt”,ios::in);

//while (!file.eof())
while (series<1) /* checking for one series*/
{
series++;
cout << series;
while(count < N )
{
double t,v;
file >> t >> v;
grpreamp->SetPoint(grpreamp->GetN(),t,v);
count++;
if (file.eof()) break;
}
count = 0;
}
file.close();

grpreamp->SetTitle(“PlaceHolder”);
grpreamp->SetMarkerStyle(3);
grpreamp->SetMarkerSize(1);
grpreamp->Draw(“APC”);
}
I want to create 20 separate graphs and save them all separately (not shown in 1 canvas). Is there a way to code it in a few lines?

Thanks,
Gabe

Before drawing the graphs create a new canvas:

auto c = new TCanvas( ....)

You can also save directly the TGpahs in a ROOT file and then draw them later on when you need.

{
  const char *fn = "hg_preamp_mod.txt"; // file name
  const int np = 50; // number of points in each graph
  const int ng = 20; // number of graphs
  TGraph *g[ng]; // array of (pointers to) graphs
  TCanvas *c = new TCanvas("c", "c");
  c->DivideSquare(ng);
  // c->Divide(5, 4); // hardcoded 20 (= 5 * 4) pads
  TGraph *gg = new TGraph(fn); // a temporary graph
  for (int i = 0; i < ng; i++) {
    g[i] = new TGraph(np, gg->GetX() + i * np, gg->GetY() + i * np);
    // g[i]->SetName(TString::Format("PlaceHolder_%d", i + 1));
    g[i]->SetTitle(TString::Format("PlaceHolder %d;time;amplitude", i + 1));
    g[i]->SetMarkerStyle(3);
    g[i]->SetMarkerSize(1);
    c->cd(i + 1);
    g[i]->Draw("APC");
  }
  delete gg; // no longer needed
  c->cd(0);
}
1 Like

Thank you very much, that’s exactly what I needed.