Opening multiple root files

Dear ROOT experts,

I would like to do a simple “homework” which opens several root files, takes a histo and calculates the integral in a certain range. Below is the code.

  const int N = 50;
  int count = 0;
  int i1 = 1;
  double temp1, temp2, temp3;
  double arr1[N];
  double arr2[N];
  double arr3[N];

char filename1[350], filename2[350], filename3[350];

  // open the files
  while (i1<50) {
    temp1 = 0.; temp2 = 0.; temp3 = 0.;
  
 sprintf(filename1,"../rootFiles_%d/test1.root",i1);
    f1 = new TFile(filename1,"READONLY");
    f1->cd("folder1");
    TH1D* hAlphaT1 = (TH1D*)gDirectory->Get("histo");
    h1->GetXaxis()->SetRangeUser(0.45,2);
    temp1 = h1->Integral();
    h1->Delete();
 
sprintf(filename2,"../rootFiles_%d/test2.root",i1);
    f2 = new TFile(filename2,"READONLY");
    f2->cd("folder1");
    TH1D* h2 = (TH1D*)gDirectory->Get("histo");
    h2->GetXaxis()->SetRangeUser(0.45,2);
    temp2 = h2->Integral();
    h2->Delete();
    
    arr1[count] = temp1;
    arr2[count] = temp2;
    arr3[count] = temp3;

    i1 += 5;
    count += 1;
 }
 
    f1->Delete();
    f2->Delete();

the execution stops because I get a segmentation violation error. It loops 7 times and then displays the following error…

 *** Break *** segmentation violation
(no debugging symbols found)
Using host libthread_db library "/lib64/tls/libthread_db.so.1".
Attaching to program: /proc/4657/exe, process 4657
(no debugging symbols found)...done.
[Thread debugging using libthread_db enabled]
[New Thread 4149835456 (LWP 4657)]
(no debugging symbols found)...done.
(no debugging symbols found)...done.
(no debugging symbols found)...done.
(no debugging symbols found)...done.
0xffffe410 in __kernel_vsyscall
    ()
#1  0x007074b3 in __waitpid_nocancel () from /lib/tls/libc.so.6
#2  0x006b0779 in do_system () from /lib/tls/libc.so.6
#3  0x007e398d in system () from /lib/tls/libpthread.so.0
...
...
...

Could you please help me on this ?

thanks in advance,
loukas

Hi,

there are a few issues with your code, this version should be better:

  const int N = 50;
  int count = 0;
  int i1 = 1;
  double temp1, temp2, temp3;
  double arr1[N];
  double arr2[N];
  double arr3[N];

char filename1[350], filename2[350], filename3[350];

  // open the files
  while (i1<50) {
    temp1 = 0.; temp2 = 0.; temp3 = 0.;
  
    sprintf(filename1,"../rootFiles_%d/test1.root",i1);
    TFile* f1 = new TFile(filename1,"READONLY");
    if (!f1 || f1->IsZombie()) {
       printf("Cannot open %s!", filename1);
       return;
    }
    f1->cd("folder1");
    TH1D* h1 = (TH1D*)gDirectory->Get("histo");
    if (!h1) {
       printf("Cannot read histo!");
       return;
    }
    h1->GetXaxis()->SetRangeUser(0.45,2);
    temp1 = h1->Integral();
    // h1->Delete(); - NO, use delete h1, but histograms are owned by files,
    // so just don't delete it, will be done by delete f1.
 
    sprintf(filename2,"../rootFiles_%d/test2.root",i1);
    TFile* f2 = new TFile(filename2,"READONLY");
    if (!f2 || f2->IsZombie()) {
       printf("Cannot open %s!", filename2);
       return;
    }
    f2->cd("folder1");
    TH1D* h2 = (TH1D*)gDirectory->Get("histo");
    h2->GetXaxis()->SetRangeUser(0.45,2);
    temp2 = h2->Integral();
    // h2->Delete(); - NO, see above
    
    arr1[count] = temp1;
    arr2[count] = temp2;
    arr3[count] = temp3;

    i1 += 5;
    count += 1;

    delete f1;
    delete f2;
 }

If it still doesn’t work we’ll need the actual code you are running.

Cheers, Axel.

Hi Axel,

yes it actually works! Using the “if check statements” I found that the error was caused by a corrupted file.

Thank you very much.

cheers–loukas

Hi,

good! Where did you learn that error checking is optional? :wink:

Cheers, Axel.

Hm … it’s my naive way of programming. But I’ve learned now … :slight_smile:

thanks again!

cheers - -loukas