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

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:

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();
}

Welcome to the ROOT forum.

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

void my_script() {
...
}

in order to do:

root my_script.C

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

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();
}

}

At least:

// ...
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).

Yes we need it

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.

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

@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