# Save a Graph with TGraphe

**URL:** <https://root-forum.cern.ch/t/save-a-graph-with-tgraphe/45245>\
**Category:** Newbie\
**Tags:** tree\
**Created:** [June 4, 2021, 9:55am UTC](https://root-forum.cern.ch/t/save-a-graph-with-tgraphe/45245 "2021-06-04T09:55:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![IKoro](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/i/3e96dc/32.png) [@IKoro](https://root-forum.cern.ch/u/IKoro)\
**Post date:** [June 4, 2021, 9:55am UTC](https://root-forum.cern.ch/t/save-a-graph-with-tgraphe/45245/1 "2021-06-04T09:55:57Z")

</div>

Hello,  
I don’t understand why i can’t savec my different graph.

```auto
void OMline2() {
   gStyle->SetOptStat(0);
   gStyle->SetOptFit(1111);

   TCanvas *c1 = new TCanvas();

   TFile *input = new TFile("/home/dupuy/Documents/code_dupuy/Exercices/fichier_root/valeurspics10.root","READ");

   TTree *tree = (TTree*)input->Get("valeurspics10_tree;1");

   double Constant1, Mean1, Sigma1, om_number1;
   int pics, intensity, Entries;
   Double_t error1, error2, error3;
   Double_t x[6], y[6],ex[6],ey[6];
   Int_t n = 6;
   Double_t fquality;

   tree->SetBranchAddress("om_number1", &om_number1);
   tree->SetBranchAddress("Constant1", &Constant1);
   tree->SetBranchAddress("Mean1", &Mean1);
   tree->SetBranchAddress("Sigma1", &Sigma1);
   tree->SetBranchAddress("pics", &pics);
   tree->SetBranchAddress("intensity", &intensity);
   tree->SetBranchAddress("error1", &error1);
   tree->SetBranchAddress("error2", &error2);
   tree->SetBranchAddress("error3", &error3);
   tree->SetBranchAddress("fquality", &fquality);
   tree->SetBranchAddress("Entries", &Entries);

   int entries = tree->GetEntries();

   //auto mg = new TMultiGraph();

   for (int j = 306; j <= 310; j++ )
   {
   TF1 *fit7 = new TF1("fit7", "pol1", 0, 400000);
   for (int i=0; i < entries; i++)
   {
      tree->GetEntry(i);

      if (om_number1 == j && Entries > 600 && fquality < 2.0)
      {
            cout << "pics" << pics<< endl;
            x[pics] = intensity;
            cout <<"itensity ==" << intensity << endl;
            y[pics] = Mean1;
            cout << error2 << endl;
            ey[pics] = error2;

            ex[pics] = 0;
         }
         TGraph* gr = new TGraphErrors(n,x,y,ex,ey);
         gr->SetTitle("Linearite ");
         gr->SetMarkerColor(4);
         gr->SetMarkerStyle(20);
         gr->Draw("AP");
         gr->Fit("fit7","R");
         c1->Print(Form("/home/dupuy/Documents/code_dupuy/Exercices/Line2/%03i.png", j));
         delete gr;

      }
   }
}

```

[valeurspics10.root](https://root-forum.cern.ch/uploads/short-url/WVxTLek7yY43pCaYngrmDO3Njc.root) (186.8 KB)

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [June 4, 2021, 10:09am UTC](https://root-forum.cern.ch/t/save-a-graph-with-tgraphe/45245/2 "2021-06-04T10:09:52Z")

</div>

Check your loops, where each one opens and closes. Proper indentation helps with readability.
