Here the boiled down code:
while ( myloop)
{
if (counter == 0)
{
TFile *CurrentFile = new TFile(filename,"RECREATE",title);
TCanvas *c1 = new TCanvas("c1", "c1",283,182,521,515);
c1->Modified();
c1->cd();
pad1 = new TPad("pad1","TimeDistribX",0.02,0.52,0.48,0.98,21);
pad2 = new TPad("pad2","TimeDistribUV",0.52,0.52,0.98,0.98,21);
pad1->Draw();
pad2->Draw();
TH1F *TimeDistribX;
TH1F *TimeDistribUV;
pad1->cd();
TimeDistribX = new TH1F("TimeDistribX","Drift-Time-Distribution_X-wires",100,-0.5,699.5);
TimeDistribX->Draw();
pad2->cd();
TimeDistribUV = new TH1F("TimeDistribUV","Drift-Time-Distribution_UV-wires",100,-0.5,699.5);
TimeDistribUV->Draw();
}
// "other stuff" goes here...
//Update the canvas
pad1->cd();
TimeDistribX->Draw();
pad2->cd();
TimeDistribUV->Draw();
c1->Update();
// after 10 events I save the file and close it
if (counter == 10)
{
cout << "Saving file\n" << filename;
CurrentFile->Write();
CurrentFile->Close();
filenumber++;
counter = 0;
}
}