# Can not AddPoint() to a TGraphErrors

**URL:** <https://root-forum.cern.ch/t/can-not-addpoint-to-a-tgrapherrors/55067>\
**Category:** Newbie\
**Tags:** root\
**Created:** [May 28, 2023, 2:32am UTC](https://root-forum.cern.ch/t/can-not-addpoint-to-a-tgrapherrors/55067 "2023-05-28T02:32:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Crisps](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/crisps/32/39101_2.png) [@Crisps](https://root-forum.cern.ch/u/Crisps)\
**Post date:** [May 28, 2023, 2:32am UTC](https://root-forum.cern.ch/t/can-not-addpoint-to-a-tgrapherrors/55067/1 "2023-05-28T02:32:04Z")

</div>

Dear Experts

I want to add points to two different TGraphErrors for comparison, when I add all points in the same graph it works fine, all points are correctly set. But when I try to separate them into two graphs, the second graph always contain points that value is 0. Here is the code:

```cpp
 int nChip = 6;
 int nCh = 8;
 for(int ifile=0; ifile<nChip; ++ifile){
                TFile* f_in = new TFile(Form("../root/AnalogDistributions_U%d.root", ifile + 1), "READ");
                TGraphErrors* g_height = (TGraphErrors*)f_in->Get("g_height");

                if(ifile<3){
                        for(int ich=0; ich<nCh; ++ich){
                                g_left->AddPoint(ifile * nCh + ich, g_height->GetPointY(ich) );
                                g_left->SetPointError(ifile * nCh + ich, 0, g_height->GetErrorY(ich) );
                                std::cout<<ifile<<" "<<ich<<" "<<ifile*nCh+ich<<" "<<g_left->GetPointY(ich)<<std::endl;
                        }
                }
                else{
                        for(int ich=0; ich<nCh; ++ich){
                                g_right->AddPoint(ifile * nCh + ich, g_height->GetPointY(ich) );
                                g_right->SetPointError(ifile * nCh + ich, 0, g_height->GetErrorY(ich) );
                                std::cout<<ifile<<" "<<ich<<" "<<ifile*nCh+ich<<" "<<g_height->GetPointY(ich)<<" "<<g_right->GetPointY(ich)<<std::endl;
                        }
                }
                f_in->Close();
                f_in->Delete();
                g_height->Delete();
        }

```

Did I do something wrong?

---

<div class="post-metadata">

**Author:** ![Crisps](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/crisps/32/39101_2.png) [@Crisps](https://root-forum.cern.ch/u/Crisps)\
**Post date:** [May 28, 2023, 2:48am UTC](https://root-forum.cern.ch/t/can-not-addpoint-to-a-tgrapherrors/55067/2 "2023-05-28T02:48:58Z")

</div>

After some investigation, I found that two TGraphError I made are sharing the parameter `fNPoints`, which means after first 3 loop of `ifile` the `g_right` has already 24 points with no value. Why?

---

<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:** [May 28, 2023, 5:19am UTC](https://root-forum.cern.ch/t/can-not-addpoint-to-a-tgrapherrors/55067/3 "2023-05-28T05:19:13Z")

</div>

You did not include the code where you declare `g_left` and `g_right`.

---

<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:** [May 28, 2023, 7:15am UTC](https://root-forum.cern.ch/t/can-not-addpoint-to-a-tgrapherrors/55067/4 "2023-05-28T07:15:42Z")

</div>

`g_right->SetPointError(g_right->GetN() - 1, 0., g_height->GetErrorY(ich));`
