TFile.h

Go to the documentation of this file.
00001 // @(#)root/io:$Id: TFile.h 35443 2010-09-20 10:06:47Z rdm $
00002 // Author: Rene Brun   28/11/94
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 #ifndef ROOT_TFile
00013 #define ROOT_TFile
00014 
00015 
00016 //////////////////////////////////////////////////////////////////////////
00017 //                                                                      //
00018 // TFile                                                                //
00019 //                                                                      //
00020 // ROOT file.                                                           //
00021 //                                                                      //
00022 //////////////////////////////////////////////////////////////////////////
00023 
00024 #ifndef ROOT_TDirectoryFile
00025 #include "TDirectoryFile.h"
00026 #endif
00027 #ifndef ROOT_TUrl
00028 #include "TUrl.h"
00029 #endif
00030 
00031 class TFree;
00032 class TArrayC;
00033 class TArchiveFile;
00034 class TFileOpenHandle;
00035 class TFileCacheRead;
00036 class TFileCacheWrite;
00037 class TProcessID;
00038 class TStopwatch;
00039 
00040 
00041 class TFile : public TDirectoryFile {
00042   friend class TDirectoryFile;
00043 
00044 public:
00045    // Asynchronous open request status
00046    enum EAsyncOpenStatus { kAOSNotAsync = -1,  kAOSFailure = 0,
00047                            kAOSInProgress = 1, kAOSSuccess = 2 };
00048    // Open timeout constants
00049    enum EOpenTimeOut { kInstantTimeout = 0, kEternalTimeout = 999999999 };
00050 
00051 protected:
00052    Double_t         fSumBuffer;      //Sum of buffer sizes of objects written so far
00053    Double_t         fSum2Buffer;     //Sum of squares of buffer sizes of objects written so far
00054    Long64_t         fBytesWrite;     //Number of bytes written to this file
00055    Long64_t         fBytesRead;      //Number of bytes read from this file
00056    Long64_t         fBytesReadExtra; //Number of extra bytes (overhead) read by the readahead buffer
00057    Long64_t         fBEGIN;          //First used byte in file
00058    Long64_t         fEND;            //Last used byte in file
00059    Long64_t         fSeekFree;       //Location on disk of free segments structure
00060    Long64_t         fSeekInfo;       //Location on disk of StreamerInfo record
00061    Int_t            fD;              //File descriptor
00062    Int_t            fVersion;        //File format version
00063    Int_t            fCompress;       //Compression level from 0(not compressed) to 9 (max compression)
00064    Int_t            fNbytesFree;     //Number of bytes for free segments structure
00065    Int_t            fNbytesInfo;     //Number of bytes for StreamerInfo record
00066    Int_t            fWritten;        //Number of objects written so far
00067    Int_t            fNProcessIDs;    //Number of TProcessID written to this file
00068    Int_t            fReadCalls;      //Number of read calls ( not counting the cache calls )
00069    TString          fRealName;       //Effective real file name (not original url)
00070    TString          fOption;         //File options
00071    Char_t           fUnits;          //Number of bytes for file pointers
00072    TList           *fFree;           //Free segments linked list table
00073    TArrayC         *fClassIndex;     //!Index of TStreamerInfo classes written to this file
00074    TObjArray       *fProcessIDs;     //!Array of pointers to TProcessIDs
00075    Long64_t         fOffset;         //!Seek offset cache
00076    TArchiveFile    *fArchive;        //!Archive file from which we read this file
00077    TFileCacheRead  *fCacheRead;      //!Pointer to the read cache (if any)
00078    TFileCacheWrite *fCacheWrite;     //!Pointer to the write cache (if any)
00079    Long64_t         fArchiveOffset;  //!Offset at which file starts in archive
00080    Bool_t           fIsArchive;      //!True if this is a pure archive file
00081    Bool_t           fNoAnchorInName; //!True if we don't want to force the anchor to be appended to the file name
00082    Bool_t           fIsRootFile;     //!True is this is a ROOT file, raw file otherwise
00083    Bool_t           fInitDone;       //!True if the file has been initialized
00084    Bool_t           fMustFlush;      //!True if the file buffers must be flushed
00085    TFileOpenHandle *fAsyncHandle;    //!For proper automatic cleanup
00086    EAsyncOpenStatus fAsyncOpenStatus; //!Status of an asynchronous open request
00087    TUrl             fUrl;            //!URL of file
00088 
00089    TList           *fInfoCache;      //!Cached list of the streamer infos in this file
00090    TList           *fOpenPhases;     //!Time info about open phases
00091 
00092    static TList    *fgAsyncOpenRequests; //List of handles for pending open requests
00093 
00094    static TString   fgCacheFileDir;          //Directory where to locally stage files
00095    static Bool_t    fgCacheFileDisconnected; //Indicates, we trust in the files in the cache dir without stat on the cached file
00096    static Bool_t    fgCacheFileForce;        //Indicates, to force all READ to CACHEREAD
00097    static UInt_t    fgOpenTimeout;           //Timeout for open operations in ms  - 0 corresponds to blocking i/o
00098    static Bool_t    fgOnlyStaged ;           //Before the file is opened, it is checked, that the file is staged, if not, the open fails
00099    static Long64_t  fgBytesWrite;            //Number of bytes written by all TFile objects
00100    static Long64_t  fgBytesRead;             //Number of bytes read by all TFile objects
00101    static Long64_t  fgFileCounter;           //Counter for all opened files
00102    static Int_t     fgReadCalls;             //Number of bytes read from all TFile objects
00103    static Int_t     fgReadaheadSize;         //Readahead buffer size
00104    static Bool_t    fgReadInfo;              //if true (default) ReadStreamerInfo is called when opening a file
00105 
00106    virtual EAsyncOpenStatus GetAsyncOpenStatus() { return fAsyncOpenStatus; }
00107    virtual void  Init(Bool_t create);
00108    Bool_t        FlushWriteCache();
00109    Int_t         ReadBufferViaCache(char *buf, Int_t len);
00110    Int_t         WriteBufferViaCache(const char *buf, Int_t len);
00111 
00112    // Interface to basic system I/O routines
00113    virtual Int_t    SysOpen(const char *pathname, Int_t flags, UInt_t mode);
00114    virtual Int_t    SysClose(Int_t fd);
00115    virtual Int_t    SysRead(Int_t fd, void *buf, Int_t len);
00116    virtual Int_t    SysWrite(Int_t fd, const void *buf, Int_t len);
00117    virtual Long64_t SysSeek(Int_t fd, Long64_t offset, Int_t whence);
00118    virtual Int_t    SysStat(Int_t fd, Long_t *id, Long64_t *size, Long_t *flags, Long_t *modtime);
00119    virtual Int_t    SysSync(Int_t fd);
00120 
00121    // Interface for text-based TDirectory I/O
00122    virtual Long64_t DirCreateEntry(TDirectory*) { return 0; }
00123    virtual Int_t    DirReadKeys(TDirectory*) { return 0; }
00124    virtual void     DirWriteKeys(TDirectory*) {}
00125    virtual void     DirWriteHeader(TDirectory*) {}
00126 
00127 private:
00128    TFile(const TFile &);            //Files cannot be copied
00129    void operator=(const TFile &);
00130 
00131    static void   CpProgress(Long64_t bytesread, Long64_t size, TStopwatch &watch);
00132    static TFile *OpenFromCache(const char *name, Option_t * = "",
00133                                const char *ftitle = "", Int_t compress = 1,
00134                                Int_t netopt = 0);
00135 
00136 public:
00137    // TFile status bits
00138    enum EStatusBits {
00139       kRecovered     = BIT(10),
00140       kHasReferences = BIT(11),
00141       kDevNull       = BIT(12),
00142       kWriteError    = BIT(14), // BIT(13) is taken up by TObject
00143       kBinaryFile    = BIT(15),
00144       kRedirected    = BIT(16)
00145    };
00146    enum ERelativeTo { kBeg = 0, kCur = 1, kEnd = 2 };
00147    enum { kStartBigFile  = 2000000000 };
00148    // File type
00149    enum EFileType { kDefault = 0, kLocal = 1, kNet = 2, kWeb = 3, kFile = 4};
00150 
00151    TFile();
00152    TFile(const char *fname, Option_t *option="", const char *ftitle="", Int_t compress=1);
00153    virtual ~TFile();
00154    virtual void        Close(Option_t *option=""); // *MENU*
00155    virtual void        Copy(TObject &) const { MayNotUse("Copy(TObject &)"); }
00156    virtual TKey*       CreateKey(TDirectory* mother, const TObject* obj, const char* name, Int_t bufsize);
00157    virtual TKey*       CreateKey(TDirectory* mother, const void* obj, const TClass* cl,
00158                                  const char* name, Int_t bufsize);
00159    virtual void        Delete(const char *namecycle="");
00160    virtual void        Draw(Option_t *option="");
00161    virtual void        DrawMap(const char *keys="*",Option_t *option=""); // *MENU*
00162    virtual void        FillBuffer(char *&buffer);
00163    virtual void        Flush();
00164    TArchiveFile       *GetArchive() const { return fArchive; }
00165    Int_t               GetBestBuffer() const;
00166    virtual Int_t       GetBytesToPrefetch() const;
00167    TFileCacheRead     *GetCacheRead() const;
00168    TFileCacheWrite    *GetCacheWrite() const;
00169    TArrayC            *GetClassIndex() const { return fClassIndex; }
00170    Int_t               GetCompressionLevel() const { return fCompress; }
00171    Float_t             GetCompressionFactor();
00172    virtual Long64_t    GetEND() const { return fEND; }
00173    virtual Int_t       GetErrno() const;
00174    virtual void        ResetErrno() const;
00175    Int_t               GetFd() const { return fD; }
00176    virtual const TUrl *GetEndpointUrl() const { return &fUrl; }
00177    TObjArray          *GetListOfProcessIDs() const {return fProcessIDs;}
00178    TList              *GetListOfFree() const { return fFree; }
00179    virtual Int_t       GetNfree() const { return fFree->GetSize(); }
00180    virtual Int_t       GetNProcessIDs() const { return fNProcessIDs; }
00181    Option_t           *GetOption() const { return fOption.Data(); }
00182    virtual Long64_t    GetBytesRead() const { return fBytesRead; }
00183    virtual Long64_t    GetBytesReadExtra() const { return fBytesReadExtra; }
00184    virtual Long64_t    GetBytesWritten() const;
00185    virtual Int_t       GetReadCalls() const { return fReadCalls; }
00186    Int_t               GetVersion() const { return fVersion; }
00187    Int_t               GetRecordHeader(char *buf, Long64_t first, Int_t maxbytes,
00188                                        Int_t &nbytes, Int_t &objlen, Int_t &keylen);
00189    virtual Int_t       GetNbytesInfo() const {return fNbytesInfo;}
00190    virtual Int_t       GetNbytesFree() const {return fNbytesFree;}
00191    Long64_t            GetRelOffset() const { return fOffset - fArchiveOffset; }
00192    virtual Long64_t    GetSeekFree() const {return fSeekFree;}
00193    virtual Long64_t    GetSeekInfo() const {return fSeekInfo;}
00194    virtual Long64_t    GetSize() const;
00195    virtual TList      *GetStreamerInfoList();
00196    const   TList      *GetStreamerInfoCache();
00197    virtual void        IncrementProcessIDs() { fNProcessIDs++; }
00198    virtual Bool_t      IsArchive() const { return fIsArchive; }
00199            Bool_t      IsBinary() const { return TestBit(kBinaryFile); }
00200            Bool_t      IsRaw() const { return !fIsRootFile; }
00201    virtual Bool_t      IsOpen() const;
00202    virtual void        ls(Option_t *option="") const;
00203    virtual void        MakeFree(Long64_t first, Long64_t last);
00204    virtual void        MakeProject(const char *dirname, const char *classes="*",
00205                                    Option_t *option="new"); // *MENU*
00206    virtual void        Map(); // *MENU*
00207    virtual Bool_t      Matches(const char *name);
00208    virtual Bool_t      MustFlush() const {return fMustFlush;}
00209    virtual void        Paint(Option_t *option="");
00210    virtual void        Print(Option_t *option="") const;
00211    virtual Bool_t      ReadBufferAsync(Long64_t offs, Int_t len);
00212    virtual Bool_t      ReadBuffer(char *buf, Int_t len);
00213    virtual Bool_t      ReadBuffer(char *buf, Long64_t pos, Int_t len);
00214    virtual Bool_t      ReadBuffers(char *buf, Long64_t *pos, Int_t *len, Int_t nbuf);
00215    virtual void        ReadFree();
00216    virtual TProcessID *ReadProcessID(UShort_t pidf);
00217    virtual void        ReadStreamerInfo();
00218    virtual Int_t       Recover();
00219    virtual Int_t       ReOpen(Option_t *mode);
00220    virtual void        Seek(Long64_t offset, ERelativeTo pos = kBeg);
00221    virtual void        SetCacheRead(TFileCacheRead *cache);
00222    virtual void        SetCacheWrite(TFileCacheWrite *cache);
00223    virtual void        SetCompressionLevel(Int_t level=1);
00224    virtual void        SetEND(Long64_t last) { fEND = last; }
00225    virtual void        SetOffset(Long64_t offset, ERelativeTo pos = kBeg);
00226    virtual void        SetOption(Option_t *option=">") { fOption = option; }
00227    virtual void        SetReadCalls(Int_t readcalls = 0) { fReadCalls = readcalls; }
00228    virtual void        ShowStreamerInfo();
00229    virtual Int_t       Sizeof() const;
00230    void                SumBuffer(Int_t bufsize);
00231    virtual void        UseCache(Int_t maxCacheSize = 10, Int_t pageSize = 0);
00232    virtual Bool_t      WriteBuffer(const char *buf, Int_t len);
00233    virtual Int_t       Write(const char *name=0, Int_t opt=0, Int_t bufsiz=0);
00234    virtual Int_t       Write(const char *name=0, Int_t opt=0, Int_t bufsiz=0) const;
00235    virtual void        WriteFree();
00236    virtual void        WriteHeader();
00237    virtual UShort_t    WriteProcessID(TProcessID *pid);
00238    virtual void        WriteStreamerInfo();
00239 
00240    static TFileOpenHandle
00241                       *AsyncOpen(const char *name, Option_t *option = "",
00242                                  const char *ftitle = "", Int_t compress = 1,
00243                                  Int_t netopt = 0);
00244    static TFile       *Open(const char *name, Option_t *option = "",
00245                             const char *ftitle = "", Int_t compress = 1,
00246                             Int_t netopt = 0);
00247    static TFile       *Open(TFileOpenHandle *handle);
00248 
00249    static EFileType    GetType(const char *name, Option_t *option = "", TString *prefix = 0);
00250 
00251    static EAsyncOpenStatus GetAsyncOpenStatus(const char *name);
00252    static EAsyncOpenStatus GetAsyncOpenStatus(TFileOpenHandle *handle);
00253    static const TUrl  *GetEndpointUrl(const char *name);
00254 
00255    static Long64_t     GetFileBytesRead();
00256    static Long64_t     GetFileBytesWritten();
00257    static Int_t        GetFileReadCalls();
00258    static Int_t        GetReadaheadSize();
00259 
00260    static void         SetFileBytesRead(Long64_t bytes = 0);
00261    static void         SetFileBytesWritten(Long64_t bytes = 0);
00262    static void         SetFileReadCalls(Int_t readcalls = 0);
00263    static void         SetReadaheadSize(Int_t bufsize = 256000);
00264    static void         SetReadStreamerInfo(Bool_t readinfo=kTRUE);
00265 
00266    static Long64_t     GetFileCounter();
00267    static void         IncrementFileCounter();
00268 
00269    static Bool_t       SetCacheFileDir(const char *cacheDir, Bool_t operateDisconnected = kTRUE,
00270                                        Bool_t forceCacheread = kFALSE);
00271    static const char  *GetCacheFileDir();
00272    static Bool_t       ShrinkCacheFileDir(Long64_t shrinkSize, Long_t cleanupInteval = 0);
00273    static Bool_t       Cp(const char *src, const char *dst, Bool_t progressbar = kTRUE,
00274                           UInt_t buffersize = 1000000);
00275 
00276    static UInt_t       SetOpenTimeout(UInt_t timeout);  // in ms
00277    static UInt_t       GetOpenTimeout(); // in ms
00278    static Bool_t       SetOnlyStaged(Bool_t onlystaged);
00279    static Bool_t       GetOnlyStaged();
00280 
00281    ClassDef(TFile,8)  //ROOT file
00282 };
00283 
00284 
00285 //
00286 // Class holding info about the file being opened
00287 //
00288 class TFileOpenHandle : public TNamed {
00289 
00290 friend class TFile;
00291 friend class TAlienFile;
00292 
00293 private:
00294    TString  fOpt;        // Options
00295    Int_t    fCompress;   // Compression factor
00296    Int_t    fNetOpt;     // Network options
00297    TFile   *fFile;       // TFile instance of the file being opened
00298 
00299    TFileOpenHandle(TFile *f) : TNamed("",""), fOpt(""), fCompress(1),
00300                                fNetOpt(0), fFile(f) { }
00301    TFileOpenHandle(const char *n, const char *o, const char *t, Int_t cmp,
00302                    Int_t no) : TNamed(n,t), fOpt(o), fCompress(cmp),
00303                                fNetOpt(no), fFile(0) { }
00304    TFileOpenHandle(const TFileOpenHandle&);
00305    TFileOpenHandle& operator=(const TFileOpenHandle&);
00306 
00307    TFile      *GetFile() const { return fFile; }
00308 
00309 public:
00310    ~TFileOpenHandle() { }
00311 
00312    Bool_t      Matches(const char *name);
00313 
00314    const char *GetOpt() const { return fOpt; }
00315    Int_t       GetCompress() const { return fCompress; }
00316    Int_t       GetNetOpt() const { return fNetOpt; }
00317 };
00318 
00319 R__EXTERN TFile   *gFile;
00320 
00321 #endif

Generated on Tue Jul 5 14:26:48 2011 for ROOT_528-00b_version by  doxygen 1.5.1