[ROOT] Segmentation fault with object functions in a for loop

From: Ben Morgan (B.Morgan@sheffield.ac.uk)
Date: Fri Aug 02 2002 - 18:18:59 MEST


Hi ROOTers,
           I have some ROOT files, each containing a TTree that holds 
events. I've generated MakeClass code, RawClass, to read these TTrees, and 
added a method HistEvent(Int_t entry) to draw out some parameters for 
each event and plot these in a canvas.
	I've been trying to write a named script to loop over several 
files and plot each event in turn using HistEvent (code included below). 
For each file I create the RawClass object, and then loop over the entries 
with HistEvent being called for each one. As I want to pause between each 
event to study the canvas, I added a cin.get(). However, running the code 
produces the following seg fault:

root [0] .L libTDriftData.so     (Load appropriate library for TTree)
root [1] .L RawClass.C
root [2] .L rdplotevents.C
root [3] rdplotevents()
Enter path to directory containing runXXXXX.root files:
/driftdata0/data5/raw
Enter run number bounds to plot events over: 5000 5010
 
Plotting Events from file /driftdata0/data5/raw/run05000.root
 
 *** Break *** segmentation violation
Root > Function rdplotevents() busy flag cleared
Function HistEvent() busy flag cleared

This appears to be something to do with the inner for loop, as I have 
another very similar (perfectly working) script that calls the Loop() 
method of RawClass at the same point as the inner for loop. I'm using 
ROOT v3.02/07 on a PC running RH7.1. Any 
suggestions for a solution will be gratefully received! Thanks,

Ben Morgan.

#include<iostream.h>

Int_t rdplotevents()
{
  char filedirectory[100];
  char filename[100];
  Int_t lowrun, uprun;

  cout<<"Enter path to directory containing runXXXXX.root files: "<<endl;
  cin>>filedirectory;
  cout<<"Enter run number bounds to plot events over: ";
  cin>>lowrun>>uprun;
  cout<<endl;
 
  for(Int_t i=lowrun; i<(uprun+1); i++)
    {
      if(i<10000) sprintf(filename,"%s/run0%d.root",filedirectory,i);
      else sprintf(filename,"%s/run%d.root",filedirectory,i);
      cout<<"Plotting Events from file "<<filename<<endl;

      //Open File, Get Tree and make RawClass object
      TFile *f = new TFile(filename,"read");
      if(f->IsZombie())
	{
	  cout<<"File "<<filename<<" does not exist, continuing....."<<endl;
	  continue;
	}
      TTree *t1 = (TTree*)f->Get("DriftRawData");
      RawClass *RC = new RawClass(t1);

      Int_t nentries = (Int_t)(RC->fChain->GetEntries());

      for(Int_t j=0; j<nentries; j++)
	{
	  RC->HistEvent(j);
	  cin.get();
	}

      delete RC;
    }

  return 0;
}


-- 
-------------------------------------------------------------------------------
Mr. Ben Morgan                               Tel: 44 (0)114 22 23553 (Work)
Postgraduate Student
University of Sheffield
Department of Physics & Astronomy
Hicks Building
Hounsfield Road
Sheffield  S3 7RH
-------------------------------------------------------------------------------



This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:51:03 MET