# How to avoid the repetition of plots being plotted on all the canvas while using for loop for each canvas

**URL:** <https://root-forum.cern.ch/t/how-to-avoid-the-repetition-of-plots-being-plotted-on-all-the-canvas-while-using-for-loop-for-each-canvas/51328>\
**Category:** Newbie\
**Created:** [August 25, 2022, 6:20am UTC](https://root-forum.cern.ch/t/how-to-avoid-the-repetition-of-plots-being-plotted-on-all-the-canvas-while-using-for-loop-for-each-canvas/51328 "2022-08-25T06:20:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![taushy](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/97f17d/32.png) [@taushy](https://root-forum.cern.ch/u/taushy)\
**Post date:** [August 25, 2022, 6:20am UTC](https://root-forum.cern.ch/t/how-to-avoid-the-repetition-of-plots-being-plotted-on-all-the-canvas-while-using-for-loop-for-each-canvas/51328/1 "2022-08-25T06:20:51Z")

</div>

hello

I have plotted Pt Vs mass projections and i have used an array to fill the canvas. code runs fine but when i used more than one loop, in my case i have used three loops for three canvas. The problem I’m facing is the last plot in the loop gets plotted on all the previous canvas at the last position. how can i avoid this problem.

thanks  
taushy  
here is the code:

```auto
TCanvas *c1 = new TCanvas();
c1->Divide(3,2);

TH1F * mpt = (TH1F*)h7->ProjectionY();               
 "mpt"
const Int_t npt =6;                                  
Double_t pt[npt+1] ={0,1,2,3,4,6,10};            
TH1F *mpthisto[npt];                                

for(int n=0;n<npt;n++)
{
c1->cd(n+1);                                        
Int_t bin1 = mpt->FindBin(pt[n]);                   
Int_t bin2 = mpt->FindBin(pt[n+1]);                 

cout << bin1 <<"--------"<<bin2<<endl;               
 mpthisto[n] = (TH1F*)h7->ProjectionX("",bin1,bin2);  
mpthisto[n]->SetTitle(Form("pt-%.1f--%.1f",pt[n],pt[n+1]));  

mpthisto[n]->Draw();                                         
c1->Update();                         
} 

////////// *************************** /////////////////////

TCanvas *c2 = new TCanvas();
c2->Divide(2,2);

TH1F * mptx = (TH1F*)h7->ProjectionX();

const Int_t x=4;
Double_t ptx[x+1]={0,1,1.7,1.9,2.1};
TH1F *pthist[x];

for(int i=0;i<x;i++)
{
c2->cd(i+1);
Int_t bn1 = mptx->FindBin(ptx[i]);
Int_t bn2 = mptx->FindBin(ptx[i+1]);
cout << bn1 <<"--------"<<bn2<<endl;
pthist[i] = (TH1F*)h7->ProjectionY("",bn1,bn2);
pthist[i]->SetTitle(Form("pt-%.2f-%.2f",ptx[i],ptx[i+1]));

pthist[i]->Draw();
c2->Update();
}
////////////////////// ***************** ////////////////

TCanvas *c3=new TCanvas();
c3->Divide();

TH1F *myhist=(TH1F*)h7->ProjectionY();
const int y=1;

double ar[y+1]={1,3};

TH1F *prjct[y];

for(int z=0;z<y;z++)
{
c3->Divide(z+1);
int bn_1 = myhist->FindBin(ar[z]);
int bn_2 = myhist->FindBin(ar[z+1]);
prjct[z]=(TH1F*)h7->ProjectionX("",bn_1,bn_2);
prjct[z]->SetTitle(Form("pt-%.2f--%.2f",ar[z],ar[z+1]));
prjct[z]->Draw();
c3->Update();
}

```

---

<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:** [August 25, 2022, 6:29am UTC](https://root-forum.cern.ch/t/how-to-avoid-the-repetition-of-plots-being-plotted-on-all-the-canvas-while-using-for-loop-for-each-canvas/51328/2 "2022-08-25T06:29:31Z")

</div>

Welcome to the ROOT forum.

Your script cannot be run as it is because it does not have any function like:

```auto
void my_script() {
...
}

```

in order to do:

```auto
root my_script.C

```

Can you make it this way so we can run it and see what’s going wrong ?

---

<div class="post-metadata">

**Author:** ![taushy](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/97f17d/32.png) [@taushy](https://root-forum.cern.ch/u/taushy)\
**Post date:** [August 25, 2022, 6:49am UTC](https://root-forum.cern.ch/t/how-to-avoid-the-repetition-of-plots-being-plotted-on-all-the-canvas-while-using-for-loop-for-each-canvas/51328/3 "2022-08-25T06:49:27Z")

</div>

here is the code …but i think u may need the root file from which I’m extracting this data from …???

void ptvsmass()  
{

TFile \*f1 = new TFile(“AnalysisResults\_Train4214\_2018.root”,“read”);

TKey\* key1 = (TKey\*)f1-\>GetListOfKeys()-\>At(0);  
f1-\>ls();

TDirectoryFile\* d1 = (TDirectoryFile\*)f1-\>Get(key1-\>GetName());  
d1-\>ls();

TKey\* key2=(TKey\*)d1-\>GetListOfKeys()-\>At(1);  
key2-\>ls();

TList\* l1=(TList\*)d1-\>Get(key2-\>GetName());  
l1-\>ls();  
TH2F\* h7=(TH2F\*)l1-\>FindObject(“hPtVsMassNoPid”);

///////////////\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*////////////////

TCanvas \*c1 = new TCanvas();  
c1-\>Divide(3,2);

TH1F \* mpt = (TH1F\*)h7-\>ProjectionY();  
const Int\_t npt =6;  
Double\_t pt[npt+1] ={0,1,2,3,4,6,10};  
TH1F \*mpthisto[npt];

for(int n=0;n\<npt;n++)  
{  
c1-\>cd(n+1);  
Int\_t bin1 = mpt-\>FindBin(pt[n]);  
Int\_t bin2 = mpt-\>FindBin(pt[n+1]);

cout \<\< bin1 \<\<“--------”\<\<bin2\<\<endl;  
mpthisto[n] = (TH1F\*)h7-\>ProjectionX(“”,bin1,bin2);  
mpthisto[n]-\>SetTitle(Form(“pt-%.1f–%.1f”,pt[n],pt[n+1]));

mpthisto[n]-\>Draw();  
c1-\>Update();  
}

//////////\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/////////////////////

TCanvas \*c2 = new TCanvas();  
c2-\>Divide(2,2);

TH1F \* mptx = (TH1F\*)h7-\>ProjectionX();

const Int\_t x=4;  
Double\_t ptx[x+1]={0,1,1.7,1.9,2.1};  
TH1F \*pthist[x];

for(int i=0;i\<x;i++)  
{  
c2-\>cd(i+1);  
Int\_t bn1 = mptx-\>FindBin(ptx[i]);  
Int\_t bn2 = mptx-\>FindBin(ptx[i+1]);  
cout \<\< bn1 \<\<“--------”\<\<bn2\<\<endl;  
pthist[i] = (TH1F\*)h7-\>ProjectionY(“”,bn1,bn2);  
pthist[i]-\>SetTitle(Form(“pt-%.2f-%.2f”,ptx[i],ptx[i+1]));

pthist[i]-\>Draw();  
c2-\>Update();  
}  
//////////////////////\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*////////////////  
TCanvas \*c3=new TCanvas();  
c3-\>Divide();

TH1F _myhist=(TH1F_)h7-\>ProjectionY();  
const int y=1;

double ar[y+1]={1,3};

TH1F \*prjct[y];

for(int z=0;z\<y;z++)  
{  
c3-\>Divide(z+1);  
int bn\_1 = myhist-\>FindBin(ar[z]);  
int bn\_2 = myhist-\>FindBin(ar[z+1]);  
prjct[z]=(TH1F\*)h7-\>ProjectionX(“”,bn\_1,bn\_2);  
prjct[z]-\>SetTitle(Form(“pt-%.2f–%.2f”,ar[z],ar[z+1]));  
prjct[z]-\>Draw();  
//c3-\>Update();  
}

}

---

<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:** [August 25, 2022, 8:12am UTC](https://root-forum.cern.ch/t/how-to-avoid-the-repetition-of-plots-being-plotted-on-all-the-canvas-while-using-for-loop-for-each-canvas/51328/4 "2022-08-25T08:12:11Z")

</div>

At least:

```cpp
// ...
TCanvas *c3=new TCanvas();

TH1F *myhist=(TH1F*)h7->ProjectionY();

const int y=1;

double ar[y+1]={1,3};

TH1F *prjct[y];

c3->Divide(y, 1); // or (1, y)

for(int z=0;z<y;z++)
{
  c3->cd(z+1);
  // ...

```

And I think, each call to `h7->ProjectionX("", ...);` and `h7->ProjectionY("", ...);` will overwrite the previous histogram (with the empty `""` name).

---

<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:** [August 25, 2022, 9:05am UTC](https://root-forum.cern.ch/t/how-to-avoid-the-repetition-of-plots-being-plotted-on-all-the-canvas-while-using-for-loop-for-each-canvas/51328/5 "2022-08-25T09:05:06Z")

</div>

> [@taushy](#):
>
> but i think u may need the root file from which I’m extracting this data from

Yes we need it

---

<div class="post-metadata">

**Author:** ![taushy](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/97f17d/32.png) [@taushy](https://root-forum.cern.ch/u/taushy)\
**Post date:** [August 31, 2022, 6:26am UTC](https://root-forum.cern.ch/t/how-to-avoid-the-repetition-of-plots-being-plotted-on-all-the-canvas-while-using-for-loop-for-each-canvas/51328/6 "2022-08-31T06:26:27Z")

</div>

sorry I couldn’t upload the root file because of its large size …

However one of the problems is fixed… this one—\> " **the last plot in the loop gets plotted on all the previous canvas at the right bottom corner of the canvas.**  
I used this piece of code—\> **TH1::AddDirectory(false);**  
but another problem still persists…this one—\> " **when I click on any of the plots or maximize the canvas, the last plot of the loop displays on all the positions of the canvas…**  
I am outputting 6 plots on a single canvas…  
how could this problem be fixed…kindly help me out.

---

<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:** [August 31, 2022, 1:57pm UTC](https://root-forum.cern.ch/t/how-to-avoid-the-repetition-of-plots-being-plotted-on-all-the-canvas-while-using-for-loop-for-each-canvas/51328/7 "2022-08-31T13:57:53Z")

</div>

As we said we will need a reproducer to test it.

---

<div class="post-metadata">

**Author:** ![taushy](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/97f17d/32.png) [@taushy](https://root-forum.cern.ch/u/taushy)\
**Post date:** [September 1, 2022, 6:13am UTC](https://root-forum.cern.ch/t/how-to-avoid-the-repetition-of-plots-being-plotted-on-all-the-canvas-while-using-for-loop-for-each-canvas/51328/8 "2022-09-01T06:13:13Z")

</div>

@couet@Wile_E_Coyote …thank you so much for responding…

i got my problem fixed actually i had not the set the names for histograms i was plotting…so the code would overwrite the last histogram of the loop everywhere. just this line of code and both the problems solved…such a silly mistake  
mpthisto[n]-\>SetName(Form(“pt-%.1f–%.1f”,pt[n],pt[n+1]));

regards  
taushy
