Plotting multiple histos from root file

Dear Experts,
I am trying to plot in 16 canvases my 256 histograms which I have already stored in root file , subdirectory.
I am getting first 16 histos in first canvas, then next 15 in second, and then my session crashes like in bottom part of my message.
If I don’t draw the histos , the root allows me to plotthem by hand.
Here is my small macro:
Thanks in advance
Avetik

  TFile *f = new TFile("tst337.root");
  f.ls();
  f.cd("TimetoRefChan");
  f->GetListOfKeys()->Print();
//f->cd("subdirectory")
TIter next(gDirectory->GetListOfKeys()); 
//create windows and fill with histograms, save them if filename is not empty
  printf("Drawing histograms...\n");
//AAA f.ls();
  char name2[40];
  char name[40];
  char hname[40];
  TCanvas *c1[16];
  Double_t s=0;
 for(Int_t i=0;i<16;i++)
  {
        sprintf(name,"overview timeDiff  %02d",i);
        c1[i]=new TCanvas(name2,name,1000,1000);
        c1[i]->Divide(4,4);
        for(Int_t j=1;j<=16;j++)
        {
                c1[i]->cd(j);
        sprintf(name2,"TimetoRefChan/TimetoRefChan%02d;1",(j+i*16)-1);
        sprintf(hname,"TimetoRefChan%02d",((j+i*16)-1));
                printf("Name2 = %\s\n",name2);
                printf("hname = %\s\n",hname);
                TH1D *hhh; f->GetObject(name2,hhh);
                hhh->Draw();
        }
}
/////////////////////////////////////////////////////////////////////////////
  printf("done...\n");

}

-----------------------output after plotting histo 31-----------------------------
*** Break *** segmentation violation

===========================================================
There was a crash (#7 0x0076d3ab in SigHandler(ESignals) () from /usr/local/lib/root/libCore.so).
This is the entire stack trace of all threads:

I tried to simplify your macro to keep on the “graphics bit”. It works for me. To investigate further we would need your ROOT file in order to run your macro.

Dear Oliver,
Thanks for your care, please find attached root file.
This is first time I am posting file in root forum, please let me know if I manage it correctly.
Thanks in advance
Avetik
tst337.root (984 KB)

Ok… understood… you could have found it easily … your file does not have the histogram 31…

Here is a cleaned up version of your macro with a protection against none existing histograms:

{
   TFile *f = new TFile("tst337.root");
   f.ls();
   f.cd("TimetoRefChan");
   f->GetListOfKeys()->Print();
   TIter next(gDirectory->GetListOfKeys()); 
   printf("Drawing histograms...\n");
   char name2[40];
   char name[40];
   char hname[40];
   TCanvas *c1[16];
   TH1D *hhh;

   Double_t s=0;
   for (Int_t i=0;i<16;i++) {
      sprintf(name,"overview timeDiff  %02d",i);
      c1[i] = new TCanvas(name,name,1000,1000);
      c1[i]->Divide(4,4);
      for (Int_t j=1;j<=16;j++) {
         c1[i]->cd(j);
         sprintf(name2,"TimetoRefChan/TimetoRefChan%02d;1",(j+i*16)-1);
         sprintf(hname,"TimetoRefChan%02d",((j+i*16)-1));
         printf("name2 = %s hname = %s\n",name2, hname);
         hhh = 0;
         f->GetObject(name2,hhh);
         if (hhh) hhh->Draw();
      }
   }
  printf("done...\n");
}