# Error in plotting inside a for loop using tree-\>Draw()

**URL:** <https://root-forum.cern.ch/t/error-in-plotting-inside-a-for-loop-using-tree-draw/51686>\
**Category:** Newbie\
**Tags:** tree, graphics\
**Created:** [September 23, 2022, 5:24pm UTC](https://root-forum.cern.ch/t/error-in-plotting-inside-a-for-loop-using-tree-draw/51686 "2022-09-23T17:24:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![thegame](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/e495f1/32.png) [@thegame](https://root-forum.cern.ch/u/thegame)\
**Post date:** [September 23, 2022, 5:24pm UTC](https://root-forum.cern.ch/t/error-in-plotting-inside-a-for-loop-using-tree-draw/51686/1 "2022-09-23T17:24:23Z")

</div>

Hi, I’m trying to have a correlation plot (6 columns \* 4 rows) between two branches inside two “for” loops. A snippet of the code is below:

```
 for(int ippac=0; ippac<7; ippac++){
      if (ippac == 5) {
        continue;
    } //This is to ignore ippac = 5 coz that is a faulty detector  
    for (int j=0; j<4; j++){
       
        c1->Divide(ippac,j); 

        c1->cd(ippac+j*6+1);

        gPad-> SetLogz();
        gPad->SetGrid(1,1);
        gStyle->SetPalette(kRainBow);

```

//ch1 is the chain of root files. and greater than \>-9999 just to plot the meaningful values.

```
         ch1->Draw(("PPAC_Tx2[%d][%d] : PPAC_Tx1[%d][%d]",ippac,j,ippac,j), ("PPAC_Tx2[%d][%d]>-9999 && PPAC_Tx1[%d][%d]>-9999",ippac,j,ippac,j));

```

//this is where I’m getting the error. and it’s obvious why I’m getting the error because Draw() takes 5 arguments at most but I’m giving more than that; Not sure how I can accommodate all the indexes;

```
    }
}

```

//FYI: PPAC\_Tx1[ippac][j] or PPAC\_Tx2[ippac][j] has a dimension of [7][4]

Thanks for any suggestions. Also, let me know if you need more info to assist.

---

<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:** [September 23, 2022, 5:29pm UTC](https://root-forum.cern.ch/t/error-in-plotting-inside-a-for-loop-using-tree-draw/51686/2 "2022-09-23T17:29:09Z")

</div>

```auto
ch1->Draw(TString::Format("PPAC_Tx2[%d][%d] : PPAC_Tx1[%d][%d]", ippac, j, ippac, j), TString::Format("PPAC_Tx2[%d][%d]>-9999 && PPAC_Tx1[%d][%d]>-9999", ippac, j , ippac, j));

```

---

<div class="post-metadata">

**Author:** ![thegame](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/e495f1/32.png) [@thegame](https://root-forum.cern.ch/u/thegame)\
**Post date:** [September 23, 2022, 6:48pm UTC](https://root-forum.cern.ch/t/error-in-plotting-inside-a-for-loop-using-tree-draw/51686/3 "2022-09-23T18:48:20Z")

</div>

@Wile_E_Coyote thanks a lot for your help. I replaced that part in my plotting routine, However, it doesn’t plot anything on the canvas (blank canvas) after successful compilation and running for around 10mins. I have attached the plotting routine along with this in case you wanna look at it. Thanks again for your time and help.

[correlationplot.c](https://root-forum.cern.ch/uploads/short-url/4aeyLwBaApg0ENzkzsb09IFwEhr.c) (1.3 KB)

---

<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:** [September 23, 2022, 7:02pm UTC](https://root-forum.cern.ch/t/error-in-plotting-inside-a-for-loop-using-tree-draw/51686/4 "2022-09-23T19:02:52Z")

</div>

Before the first “`for`” loop, add: `c1->Divide(7, 4);`  
Remove: `c1->Divide(ippac,j);`  
Change “`c1->cd(ippac+j*6+1);`” into: `c1->cd(ippac + j * 7 + 1);`  
After the “`Draw`” line, add: `gPad->Modified(); gPad->Update();`
