# Creating multiple Graphs from one data sheet

**URL:** <https://root-forum.cern.ch/t/creating-multiple-graphs-from-one-data-sheet/50690>\
**Category:** Newbie\
**Tags:** graphics, root\
**Created:** [July 6, 2022, 2:23pm UTC](https://root-forum.cern.ch/t/creating-multiple-graphs-from-one-data-sheet/50690 "2022-07-06T14:23:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![GabeO](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/g/76d3ee/32.png) [@GabeO](https://root-forum.cern.ch/u/GabeO)\
**Post date:** [July 6, 2022, 2:23pm UTC](https://root-forum.cern.ch/t/creating-multiple-graphs-from-one-data-sheet/50690/1 "2022-07-06T14:23:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [July 6, 2022, 2:42pm UTC](https://root-forum.cern.ch/t/creating-multiple-graphs-from-one-data-sheet/50690/2 "2022-07-06T14:42:04Z")

</div>

Before drawing the graphs create a new canvas:

```auto
auto c = new TCanvas( ....)

```

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

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [July 6, 2022, 3:15pm UTC](https://root-forum.cern.ch/t/creating-multiple-graphs-from-one-data-sheet/50690/3 "2022-07-06T15:15:24Z")

</div>

```cpp
{
  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);
}

```

---

<div class="post-metadata">

**Author:** ![GabeO](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/g/76d3ee/32.png) [@GabeO](https://root-forum.cern.ch/u/GabeO)\
**Post date:** [July 6, 2022, 3:53pm UTC](https://root-forum.cern.ch/t/creating-multiple-graphs-from-one-data-sheet/50690/4 "2022-07-06T15:53:16Z")

</div>

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