RE: [ROOT] Pb with TFile

From: Fons Rademakers (Fons.Rademakers@cern.ch)
Date: Thu Apr 11 2002 - 00:37:16 MEST


Hi Sebastian,

  I've tried your macro and it works fine with the latest version of
ROOT. Notice however, that there are some problems in your macro, most
notably you don't initialize myfile=0 and some "un-natural" usage of
TStrings. See corrected macro below. Also notice that to quickly find
out if a file exists you just can call TSystem::AccessPathName().

Cheers, Fons.


#include <iostream> 
#include "TSystem.h"
#include "TString.h"
#include "TROOT.h"
#include "TFile.h"

TFile* FindFile(TString filename, TString pathname)
{
   TString workingdir = gSystem->WorkingDirectory() ;
   gSystem->ChangeDirectory(pathname);
   cout << "I am in the directory : " << gSystem->WorkingDirectory() <<
endl;
   cout << "Searching file " << filename << " .... " << endl;
   void *dirp = gSystem->OpenDirectory(".");
 
   TFile *myfile = 0;
   const char *afile;    
   while ((afile = gSystem->GetDirEntry(dirp))) {
      cout << "afile : " << afile << endl;
      if (filename == afile){
         myfile = new TFile(afile,"RECREATE","",1);
      
         cout << "File : " << filename << " has been found in "<<
                 pathname << endl;
      }
   }
   if (myfile == 0) cout << "No file " << filename << 
                            " has been found in " << pathname << endl; 
   gSystem->FreeDirectory(dirp);
   gSystem->ChangeDirectory(workingdir);
   return myfile ;
}    


On Wed, 2002-04-10 at 19:42, Sebastien Greder wrote:
> Hi Philippe,
> 
> thanks to reply so quickly (it's the first time I use this mailing list !)
> 
> The version I use is : 3.02.07 on Linux with g++.
> 
> To be clear, I give you a small code which give rise to the same problem:
> 
> compil.C :
> 
> {
> gROOT->Reset();
> 
> // Get your Working directory
> 
> TString pwd = gSystem->pwd();
> gSystem->Load("/cern/2001/root.3.02.07/lib/libPhysics.so");
> gSystem->Load("/cern/2001/root.3.02.07/lib/libCore.so");
> 
> // Process some utilities methods
> 
> gROOT->ProcessLine(".L /home/sgreder/root/utilities/FindFile.C+");
> }
> 
> and FindFile.C :
> 
> #include <iostream> 
> #include "TSystem.h"
> #include "TString.h"
> #include "TROOT.h"
> #include "TFile.h"
> 
> TFile* FindFile(TString filename, TString pathname)
>  {
>  TString workingdir = gSystem->WorkingDirectory() ;
>  gSystem->ChangeDirectory(pathname);
>  cout << "I am in the directory : " << gSystem->WorkingDirectory() <<
> endl;
>  cout << "Searching file " << filename.Data() << " .... " << endl;
>  void *dirp = gSystem->OpenDirectory(".");
>  
>  TFile *myfile ;
>     const char *afile;    
>     while(afile = gSystem->GetDirEntry(dirp)) {
>       cout << "afile : " << afile << endl;
>       if (strcmp(afile,filename.Data()) == 0){
>        myfile = new TFile(afile,"RECREATE","",1);
>       
> cout << "File : " << filename.Data() << " has been found in "<<
> pathname.Data() << endl;
>       }
>      }
>      if(myfile == 0) cout << "No file " << filename.Data() << " has been
> found in " << pathname.Data() << endl; 
>  gSystem->FreeDirectory(dirp);
>  gSystem->ChangeDirectory(workingdir);
>  return myfile ;
>  }    
> 
> and then I run : .x compil.C in ROOT
> 
> and I get the following error message :
> 
> Info in <ACLiC>: creating shared library
> /home/sgreder/root/utilities/FindFile_C.so
> /home/sgreder/root/utilities/FindFile.C: In function `class TFile *
> FindFile(class TString, class TString)':
> In file included from /home/sgreder/root/utilities/filetjO4PP.h:29,
>                  from /home/sgreder/root/utilities/filetjO4PP.C:5:
> /home/sgreder/root/utilities/FindFile.C:17: warning: suggest parentheses
> around assignment used as truth value
> dlopen error: /home/sgreder/root/utilities/FindFile_C.so: undefined
> symbol: __5TFilePCcPCcT1i
> Load Error: Failed to load Dynamic link library
> /home/sgreder/root/utilities/FindFile_C.so
> /home/sgreder/root/utilities/FindFile.C: In function `class TFile *
> FindFile(class TString, class TString)':
> In file included from /home/sgreder/root/utilities/filetjO4PP.h:29,
>                  from /home/sgreder/root/utilities/filetjO4PP.C:5:
> /home/sgreder/root/utilities/FindFile.C:17: warning: suggest parentheses
> around assignment used as truth value
> /usr/lib/crt1.o(.text+0x18): undefined reference to `main'
> /home/sgreder/root/utilities/filetjO4PP.o: In function
> `G__Sizep2memfuncfiletjO4PP::G__Sizep2memfuncfiletjO4PP(void)':
> /home/sgreder/root/utilities/filetjO4PP.C(.text+0x212): undefined
> reference to `TFile::TFile(char const *, char const *, char const *, int)'
> collect2: ld returned 1 exit status
> !!!Dictionary position not recorvered because G__unloadfile() in macro!!!
> *** Interpreter error recovered ***
> 
>  *** Break *** segmentation violation
> 
> Cheers,
> seb.
-- 
Org:    CERN, European Laboratory for Particle Physics.
Mail:   1211 Geneve 23, Switzerland
E-Mail: Fons.Rademakers@cern.ch              Phone: +41 22 7679248
WWW:    http://root.cern.ch/~rdm/            Fax:   +41 22 7679480



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