Many TGraphs with two y axes drawn in loop

I’m plotting the mean of a quantity as a fcn of a variable, and also the RMS in the same plot, using a second y axis. I want to do this for more than one file, so I’m trying to do it in a loop, but for some reason only the graphs from the first file gets drawn. Any idea on what is wrong? The legend gets drawn indicating the other graphs are there, but they are not in the plot.

...

// Now mean(ToT) vs theta:
	int c(1); // counter used for drawing
	TLegend *l = new TLegend(0.1, 0.9, 0.38, 0.72);
	l->SetTextSize(0.028);
	Double_t xmin;
	Double_t xmax;
	Double_t ymin;
	Double_t ymax;
	
	for (unsigned int i(0); i<fnames.size(); ++i)
	{
		// Open the file and get the histogram I want
		TFile* f = new TFile(fnames.at(i));
		TH3D* ToTvsAnglesNpart1 = (TH3D*)f->Get("ToTvsAnglesNpart1");
		TAxis* theta_ax = (TAxis*)ToTvsAnglesNpart1->GetXaxis();
		int npoints = theta_ax->GetNbins();
		
		// Create the graphs
		TGraphErrors* graphMean = new TGraphErrors(npoints);
		TGraphErrors* graphRMS = new TGraphErrors(npoints);
		
		// Fill the graphs using what's in the histo
		for (int j(1); j<=npoints; ++j) 
		{
			TH1D* ToT = (TH1D*)ToTvsAnglesNpart1->ProjectionZ("ToT", j, j, 0, -1);
			graphMean->SetPoint(j-1, theta_ax->GetBinCenter(j), ToT->GetMean());
			graphRMS->SetPoint(j-1, theta_ax->GetBinCenter(j), ToT->GetRMS());
		}
		
		// Prepare different color for mean and RMS
		graphMean->SetMarkerColor(1);
		graphRMS->SetMarkerColor(2);
		graphMean->SetMarkerSize(0.8);
		graphRMS->SetMarkerSize(0.8);
		
		// Now prepare for drawing mean and RMS on same canvas, with two axes
		// For the first file, draw the main canvas
		if (i==0)
		{
			TPad *pad = new TPad("pad","",0,0,1,1);
			pad->Draw();
			pad->cd();
			// Main frame: Set title and axes titles
			TH1F *hr = canvas->DrawFrame(theta_ax->GetXmin(),0.,theta_ax->GetXmax(),220.);
			hr->SetTitle("Mean ToT vs. inc. angle #theta");
			hr->GetXaxis()->SetTitle(theta_ax->GetTitle());
			hr->GetYaxis()->SetTitle("Mean ToT [BC]");
			// Draw
			graphMean->SetMarkerStyle(20);
			graphMean->Draw("P");
			l->AddEntry(graphMean, legNames.at(i), "p");
			// Now prepare for drawing the RMS, corresponding to the other y axis
			canvas->cd();
			TPad *overlay = new TPad("overlay","",0,0,1,1);
			overlay->SetFillStyle(4000);
			overlay->SetFillColor(0);
			overlay->SetFrameFillStyle(4000);
			overlay->Draw();
			overlay->cd();

			xmin = pad->GetUxmin();
			ymin = 0.;
			xmax = pad->GetUxmax();
			ymax = 1.2*TMath::MaxElement(graphRMS->GetN(), graphRMS->GetY());
			TH1F *hframe = overlay->DrawFrame(xmin,ymin,xmax,ymax);
			hframe->GetXaxis()->SetLabelOffset(99);
			hframe->GetYaxis()->SetLabelOffset(99);
			hframe->GetYaxis()->SetNdivisions(0);
	
			graphRMS->SetMarkerStyle(20);
			graphRMS->SetMarkerColor(kRed);
			graphRMS->Draw("P");
	  
			//Draw an axis on the right side
			TGaxis *axis = new TGaxis(xmax,ymin,xmax, ymax,ymin,ymax,510,"+L");
			axis->SetLineColor(kRed);
			axis->SetLabelColor(kRed);
			axis->Draw();
			axis->SetTitle("Mean ToT RMS [BC]");
			axis->SetTitleOffset(1.2);
		}
		// For the rest of the files, draw everything on top
		else 
		{
			canvas->cd();
			// Transparent pad.
			TPad *overlay = new TPad("overlay","",0,0,1,1);
			overlay->SetFillStyle(4000);
			overlay->SetFillColor(0);
			overlay->SetFrameFillStyle(4000);
			overlay->Draw();
			overlay->cd();
			TH1F *hframe = overlay->DrawFrame(xmin,ymin,xmax,ymax);
			hframe->GetXaxis()->SetLabelOffset(99);
			hframe->GetYaxis()->SetLabelOffset(99);
			hframe->GetYaxis()->SetNdivisions(0);
	
			graphMean->SetMarkerStyle(20+c);
			graphMean->Draw("P");
			l->AddEntry(graphMean, legNames.at(i), "p");


			canvas->cd();
			// Transparent pad.
			TPad *overlay = new TPad("overlay","",0,0,1,1);
			overlay->SetFillStyle(4000);
			overlay->SetFillColor(0);
			overlay->SetFrameFillStyle(4000);
			overlay->Draw();
			overlay->cd();
			TH1F *hframe = overlay->DrawFrame(xmin,ymin,xmax,ymax);
			hframe->GetXaxis()->SetLabelOffset(99);
			hframe->GetYaxis()->SetLabelOffset(99);
			hframe->GetYaxis()->SetNdivisions(0);
			graphRMS->SetMarkerStyle(20+c);
			graphRMS->Draw("P");
		}
		++c;
	}
	l->Draw();
	canvas->SaveAs("ToTmeanVsTheta.png");

OK so I solved this, I was setting the ranges wrong when drawing the other graphs. I realized this was not very well written, as I had to draw a new frame for each graph, which would not have been necessary if I had drawn all the graphs corresponding to the same axis in one loop.