Graphs on wrong pad

Hello ROOTers,

I’ve added to my macro two additional graphs that I need to have superimposed on two existing histograms, each one already being on a pad of the canvas (pad #2 and #3). Although I specified in the code the pad to be used for plotting, I suprisingly find the two graphs both on pad #1. I also tried to call separately each pad via TPad* pad# = (TPad*)canvas->GetPad(#) but nothing changes.

Help would be appreciate.

Thanks in advance.

Giuseppe

PS: If you need ASCII files for launching the macro, I’ll post them under request. Here follows the code.


void plotFWHM() {

	gStyle->SetTitleX(0.5);
	gStyle->SetTitleAlign(23);
	gStyle->SetPalette(1,0);
	//gStyle->SetOptStat("RMe");
	gStyle->SetOptStat(0);
	gStyle->SetOptFit(1111);
	gStyle->SetStatX(0.99);	
	gStyle->SetStatY(0.9);
	gStyle->SetStatW(0.23);
	gStyle->SetStatH(0.1);
	gStyle->SetStatFontSize(0.03);

	gROOT->Reset();
	gROOT->ForceStyle();
	
	string filename;
	ifstream dump; 
	Double_t word;
	string mys, junk;
	int nbinx, nbiny, nbinz, nbin;
  	double Xmin, Xmax, Ymin, Ymax, Zmin, Zmax;
/*	
	cout << "Enter filename: ";
	cin >> filename;
	dump.open(filename.c_str());
*/	
	dump.open("film.txt");
	
	for(int k=0; k<3; k++) {
		getline(dump,mys,'\n');
		cout << mys << endl;
	}
	
	if(!(mys.find("to",0) == string::npos)) {
   	junk=mys.substr( mys.find("to",0)-13, mys.length() ); 
   	Xmin=atof(junk.c_str());
  	}
  	if(!(mys.find("cm",0) == string::npos)) {
   	junk=mys.substr( mys.find("cm",0)-13, mys.length() ); 
   	Xmax=atof(junk.c_str());
  	}
	if(!(mys.find("bins",0) == string::npos)) {
   	junk=mys.substr( mys.find("bins",0)-5, mys.length() ); 
   	nbinx=atoi(junk.c_str());
  	}
  	getline(dump,mys,'\n');
  	cout << mys << endl;

	if(!(mys.find("to",0) == string::npos)) {
   	junk=mys.substr( mys.find("to",0)-13, mys.length() ); 
   	Ymin=atof(junk.c_str());
  	}
  	if(!(mys.find("cm",0) == string::npos)) {
   	junk=mys.substr( mys.find("cm",0)-13, mys.length() ); 
   	Ymax=atof(junk.c_str());
  	}
  	if(!(mys.find("bins",0) == string::npos)) {
   	junk=mys.substr( mys.find("bins",0)-5, mys.length() );
   	nbiny=atoi(junk.c_str());
  	}
  	getline(dump,mys,'\n');
  	cout << mys << endl;
  		
	if(!(mys.find("to",0) == string::npos)) {
   	junk=mys.substr( mys.find("to",0)-13, mys.length() ); 
   	Zmin=atof(junk.c_str());
  	}
	if(!(mys.find("cm",0) == string::npos)) {
   	junk=mys.substr( mys.find("cm",0)-13, mys.length() ); 
   	Zmax=atof(junk.c_str());
  	}
  	if(!(mys.find("bins",0) == string::npos)) {
    	junk=mys.substr( mys.find("bins",0)-5, mys.length() );
    	nbinz=atoi(junk.c_str());
   }
  	getline(dump,mys,'\n');
  	//cout << mys << endl;
  	
  	for(int k=0; k<2; k++) {
		getline(dump,mys,'\n');
		cout << mys << std::endl;
	}
  
  	nbin = nbinx*nbiny*nbinz;
  
   cout << " " << endl;
  	cout << "Nbin X = " << nbinx << endl;
  	cout << "Nbin Y = " << nbiny << endl;
  	cout << "Nbin Z = " << nbinz << endl;
  	cout << " " << endl;
	printf("Xmin\t[cm]\t=\t%2.4f\n",Xmin);
	printf("Xmax\t[cm]\t= \t%2.4f\n",Xmax);
	cout << " " << endl;
	printf("Ymin\t[cm]\t=\t%2.4f\n",Ymin);
	printf("Ymax\t[cm]\t= \t%2.4f\n",Ymax);
	cout << " " << endl;
	printf("Zmin\t[cm]\t=\t%2.4f\n",Zmin);
	printf("Zmax\t[cm]\t= \t%2.4f\n",Zmax);
  	cout << " " << endl;
     	
  	double ResX = (Xmax-Xmin)/nbinx;
  	double ResY = (Ymax-Ymin)/nbiny;
  	double ResZ = (Zmax-Zmin)/nbinz;
  	double FirstX = Xmin + ResX/2.;
  	double FirstY = Ymin + ResY/2.;
  	double FirstZ = Zmin + ResZ/2.;
  	
  	TH2F* hMapXY = new TH2F("hEMapXY","E_{dep} [GeV/cm^{3}/p]",nbinx,Xmin,Xmax,nbiny,Ymin,Ymax);
  	hMapXY->SetXTitle("X [cm]");
   hMapXY->GetXaxis()->CenterTitle(kTRUE);
   hMapXY->GetXaxis()->SetTitleOffset(1.1);
   hMapXY->GetXaxis()->SetTitleSize(0.04);
   hMapXY->GetXaxis()->SetLabelSize(0.03);
   hMapXY->GetXaxis()->SetTickLength(0.02);
   hMapXY->GetXaxis()->SetNdivisions(20510);
   hMapXY->SetYTitle("Y [cm]");
   hMapXY->GetYaxis()->CenterTitle(kTRUE);
   hMapXY->GetYaxis()->SetTitleOffset(1.2);
   hMapXY->GetYaxis()->SetTitleSize(0.04);
   hMapXY->GetYaxis()->SetLabelSize(0.03);
   hMapXY->GetYaxis()->SetTickLength(0.02);
   hMapXY->GetYaxis()->SetNdivisions(20510);

  	TFile* f1 = new TFile("FWHM.root","recreate");
	
  	Float_t xPos[100000];
  	Float_t yPos[100000];
  	Float_t zPos[100000];
  	double Counts;
  	//double primaries=300000, voxlvol=0.001;
  	double primaries=1, voxlvol=1;
   Int_t nev, nerr;
   
   ofstream outmc("FWHM.out");
  	
	for (Int_t nz=0; nz<nbinz; nz++) {
  	  	//zPos[nz]=0.05+float(nz)*0.1;
  	  	zPos[nz]=FirstZ+float(nz)*ResZ;
   	for (Int_t ny=0; ny<nbiny; ny++) {
      	yPos[ny]=FirstY+float(ny)*ResY;
      	for (Int_t nx=0; nx<nbinx; nx++) {
      		dump >> word;
				Counts=word*primaries*voxlvol;
				xPos[nx]=FirstX+float(nx)*ResX;
      		yPos[ny]=FirstY+float(ny)*ResY;
				outmc << xPos[nx] << "\t" << yPos[ny] << "\t" << zPos[nz] << "\t" << Counts << endl; 
    			hMapXY->Fill(xPos[nx],yPos[ny],Counts);
    			//hMapXY->SetBinError(nx, ny,0);
				nev++;
      	}
    	}
  	}
  
	cout << "No. of Read Out Depositions: " << nev << endl;
	
	for(int k=0; k<4; k++) {
		getline(dump,mys,'\n');
		cout << mys << std::endl;
	}

 	for (Int_t nz=0; nz<nbinz; nz++) { 
 		for (Int_t ny=0; ny<nbiny; ny++) { 
			for (Int_t nx=0; nx<nbinx; nx++) {
				dump >> word;
				hMapXY->SetBinError(nx,ny,(hMapXY->GetBinContent(nx,ny))*word/100.);
	 			nerr++;
			}
		}
	}
	
	cout << " " << endl;
	cout << "No. of Read Out Deposition Errors: " << nerr << endl;
	cout << " " << endl;
  	
  	TCanvas* c1 = new TCanvas("c1"," ",0,0,1200,400);
  	c1->SetGrid(0,0);
  	c1->Divide(3,1);
  	c1->cd(1)->SetGrid(0,0);
  	  	
	cout << " " << endl;
  	
 	//hMapXY->DrawCopy("COLZ");
  	//hMapXY->Write();
  	hMapXY->Draw("COLZ");
  	gPad->Update(); 
  	TPaletteAxis* palette = (TPaletteAxis*)hMapXY->GetListOfFunctions()->FindObject("palette"); 
	palette->SetLabelSize(0.04);
	palette->SetLabelFont(62);
  	palette->SetX1NDC(0.84);
  	palette->SetX2NDC(0.88);
  	palette->SetY1NDC(0.12);
  	palette->SetY2NDC(0.90);
  	
  	double cmtomm = 10.;
  	
  	TH1D* projX = hMapXY->ProjectionX("projX",hMapXY->GetYaxis()->FindBin(-0.05),hMapXY->GetYaxis()->FindBin(0.05));
	projX->SetLineColor(kRed);
   projX->SetLineWidth(1);
   projX->SetMarkerColor(kRed);
   projX->SetMarkerStyle(kOpenCircle);
	projX->SetTitle("E_{dep} X Profile @ Y = 0 cm");
	projX->SetXTitle("X [cm]");
   projX->GetXaxis()->CenterTitle(kTRUE);
   projX->GetXaxis()->SetTitleOffset(1.1);
   projX->GetXaxis()->SetTitleSize(0.04);
   projX->GetXaxis()->SetLabelSize(0.03);
   projX->GetXaxis()->SetTickLength(0.02);
   projX->GetXaxis()->SetNdivisions(20510);
   projX->SetYTitle("E_{dep} [GeV/cm^{3}/p] (Norm. @ X = 0 cm)");
   projX->GetYaxis()->CenterTitle(kTRUE);
   projX->GetYaxis()->SetTitleOffset(1.2);
   projX->GetYaxis()->SetTitleSize(0.04);
   projX->GetYaxis()->SetLabelSize(0.03);
   projX->GetYaxis()->SetTickLength(0.02);
   projX->GetYaxis()->SetNdivisions(20510);
   
   double normX = projX->GetBinContent(projX->FindBin(0.));
   
   TGraphErrors graphX("Coll_CNAO1_CollFilm_5cm_ISO_X.txt","%lg %lg");
	graphX.SetLineColor(kGreen);
   graphX.SetLineWidth(1);
	graphX.SetMarkerStyle(7);
	graphX.SetMarkerSize(1);
	graphX.SetMarkerColor(kGreen);
	graphX.SetFillColor(0);
	
	double gnormX = graphX.Eval(0);
	for (int i=0;i<graphX.GetN();i++) graphX.GetY()[i] *= 1./gnormX;
	for (int i=0;i<graphX.GetN();i++) graphX.GetX()[i] *= 1./cmtomm;
   
   TH1D* projY = hMapXY->ProjectionY("projY",hMapXY->GetXaxis()->FindBin(-0.05),hMapXY->GetXaxis()->FindBin(0.05));
   projY->SetLineColor(kBlue);
   projY->SetLineWidth(1);
   projY->SetMarkerColor(kBlue);
   projY->SetMarkerStyle(kOpenCircle);
	projY->SetTitle("E_{dep} Y Profile @ X = 0 cm");
	projY->SetXTitle("Y [cm]");
   projY->GetXaxis()->CenterTitle(kTRUE);
   projY->GetXaxis()->SetTitleOffset(1.1);
   projY->GetXaxis()->SetTitleSize(0.04);
   projY->GetXaxis()->SetLabelSize(0.03);
   projY->GetXaxis()->SetTickLength(0.02);
   projY->GetXaxis()->SetNdivisions(20510);
   projY->SetYTitle("E_{dep} [GeV/cm^{3}/p] (Norm. @ Y = 0 cm)");
   projY->GetYaxis()->CenterTitle(kTRUE);
   projY->GetYaxis()->SetTitleOffset(1.2);
   projY->GetYaxis()->SetTitleSize(0.04);
   projY->GetYaxis()->SetLabelSize(0.03);
   projY->GetYaxis()->SetTickLength(0.02);
   projY->GetYaxis()->SetNdivisions(20510);
   
  	double normY = projY->GetBinContent(projY->FindBin(0.));
  	
  	TGraphErrors graphY("Coll_CNAO1_CollFilm_5cm_ISO_Y.txt","%lg %lg");
	graphY.SetLineColor(kGreen);
   graphY.SetLineWidth(1);
	graphY.SetMarkerStyle(7);
	graphY.SetMarkerSize(1);
	graphY.SetMarkerColor(kGreen);
	graphY.SetFillColor(0);
	
	double gnormY = graphY.Eval(0);
	for (int i=0;i<graphY.GetN();i++) graphY.GetY()[i] *= 1./gnormY;
	for (int i=0;i<graphY.GetN();i++) graphY.GetX()[i] *= 1./cmtomm;
   
   c1->cd(2)->SetGrid(0,0);
	projX->Scale(1./normX);
	projX->DrawCopy("B");
	graphX.DrawClone("SamePL");


	
/*	TF1* funcX = new TF1("funcX","gaus",Xmin,Xmax);
	funcX->SetLineColor(kGreen);
	projX->Fit("funcX","RQ");
	
	double FWHMx = 2.35*(funcX->GetParameter(2));
   double ErrFWHMx = 2.35*(funcX->GetParError(2));
   
   cout << " " << endl;
	printf("FWHM profX\t[mm]\t=\t%2.2f",FWHMx*cmtomm);
	printf("\t+/-\t%2.2f\n",ErrFWHMx*cmtomm);
	cout << " " << endl;
	projY->Write();
*/						
	c1->cd(3)->SetGrid(0,0);
	projY->Scale(1./normY);
	projY->DrawCopy("B");
	graphY.DrawClone("SamePL");

/*	TF1* funcY = new TF1("funcX","gaus",Ymin,Ymax);
   funcY->SetLineColor(kGreen);
   projY->Fit("funcX","RQ");

   double FWHMy = 2.35*(funcY->GetParameter(2));
   double ErrFWHMy = 2.35*(funcY->GetParError(2));
   
   cout << " " << endl;
	printf("FWHM profY\t[mm]\t=\t%2.2f",FWHMy*cmtomm);
	printf("\t+/-\t%2.2f\n",ErrFWHMy*cmtomm);
	cout << " " << endl;
	projY->Write();
	
	double FWHM = (FWHMx + FWHMy)/2.;
   double ErrFWHM = TMath::Sqrt(TMath::Power(ErrFWHMx,2)+TMath::Power(ErrFWHMy,2))/2.;
   
   cout << " " << endl;
	printf("FWHM\t\t[mm]\t=\t%2.2f",FWHM*cmtomm);
	printf("\t+/-\t%2.2f\n",ErrFWHM*cmtomm);
	cout << " " << endl;
*/	
/*	cout << " " << endl;
	c1->SaveAs("plotFWHM.png");
	cout << " " << endl;
*/

}

before drawing the new graphs you should make sure the correct pad is selected via:

c1->cd(…) ;

:confused: Of course I tried. It’s the basic instruction to use pads on canvas…

Can you send a small running example reproducing the problem ?

this attachment should work…
test.tar.gz (76.6 KB)

if you remove the lines:

  	gPad->Update();
  	TPaletteAxis* palette = (TPaletteAxis*)hMapXY->GetListOfFunctions()->FindObject("palette"); 
	palette->SetLabelSize(0.04);
	palette->SetLabelFont(62);
  	palette->SetX1NDC(0.84);
  	palette->SetX2NDC(0.88);
  	palette->SetY1NDC(0.12);
  	palette->SetY2NDC(0.90);

Then it is ok … I do not know why yet …

Who knows how to edit the palette now?! :wink: Thanks a lot! :smiley:

Giuseppe

you can post a bug report here. savannah.cern.ch/bugs/?func=add … up=savroot

actually gPad->Update();
is the faulty line.