// @(#)root/proof:$Id$
// Author: Long Tran-Thanh   14/09/07

/*************************************************************************
 * Copyright (C) 1995-2002, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#ifndef ROOT_TProofOutputFile
#define ROOT_TProofOutputFile


//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TProofOutputFile                                                           //
//                                                                      //
// Small class to steer the merging of files produced on workers        //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#ifndef ROOT_TNamed
#include "TNamed.h"
#endif

class TCollection;
class TString;
class TList;
class TFile;
class TFileCollection;
class TFileMerger;

class TProofOutputFile : public TNamed {

friend class TProof;
friend class TProofPlayer;
friend class TProofPlayerRemote;

public:
   enum ERunType {  kMerge        = 1,      // Type of run: merge or dataset creation
                    kDataset      = 2};
   enum ETypeOpt {  kRemote       = 1,      // Merge from original copies
                    kLocal        = 2,      // Make local copies before merging
                    kCreate       = 4,      // Create dataset
                    kRegister     = 8,      // Register dataset
                    kOverwrite    = 16,     // Force dataset replacement during registration
                    kVerify       = 32};    // Verify the registered dataset

private:
   TProofOutputFile(const TProofOutputFile&); // Not implemented
   TProofOutputFile& operator=(const TProofOutputFile&); // Not implemented

   TString  fDir;            // name of the directory to be exported
   TString  fRawDir;         // name of the local directory where to create the file
   TString  fFileName;
   TString  fOptionsAnchor;  // options and anchor string including delimiters, e.g. "?myopts#myanchor"
   TString  fOutputFileName;
   TString  fWorkerOrdinal;
   TString  fLocalHost;      // Host where the file was created
   Bool_t   fIsLocal;     // kTRUE if the file is in the sandbox
   Bool_t   fMerged;
   ERunType fRunType;     // Type of run (see enum ERunType)
   UInt_t   fTypeOpt;     // Option (see enum ETypeOpt)
   Bool_t   fMergeHistosOneGo;  // If true merge histos in one go (argument to TFileMerger)

   TFileCollection *fDataSet;  // Instance of the file collection in 'dataset' mode
   TFileMerger *fMerger;  // Instance of the file merger in 'merge' mode

   void Init(const char *path, const char *dsname);
   void SetFileName(const char* name) { fFileName = name; }
   void SetDir(const char* dir, Bool_t raw = kFALSE) { if (raw) { fRawDir = dir; } else { fDir = dir; } }
   void SetMerged(Bool_t merged = kTRUE) { fMerged = merged; }
   void SetWorkerOrdinal(const char* ordinal) { fWorkerOrdinal = ordinal; }

   void AddFile(TFileMerger *merger, const char *path);
   void NotifyError(const char *errmsg);
   void Unlink(const char *path);

protected:

public:
   enum EStatusBits {
      kOutputFileNameSet = BIT(16),
      kRetrieve          = BIT(17), // If set, the file is copied to the final destination via the client
      kSwapFile          = BIT(18)  // Set when the represented file is the result of the automatic
                                    // save-to-file functionality 
   };
   TProofOutputFile() : fDir(), fRawDir(), fFileName(), fOptionsAnchor(), fOutputFileName(),
                        fWorkerOrdinal(), fLocalHost(), fIsLocal(kFALSE), fMerged(kFALSE),
                        fRunType(kMerge), fTypeOpt(kRemote), fMergeHistosOneGo(kFALSE),
                        fDataSet(0), fMerger(0) { }
   TProofOutputFile(const char *path, const char *option = "M", const char *dsname = 0);
   TProofOutputFile(const char *path, ERunType type, UInt_t opt = kRemote, const char *dsname = 0);
   virtual ~TProofOutputFile();

   const char *GetDir(Bool_t raw = kFALSE) const { return (raw) ? fRawDir : fDir; }
   TFileCollection *GetFileCollection();
   TFileMerger *GetFileMerger(Bool_t local = kFALSE);
   const char *GetFileName() const { return fFileName; }
   const char *GetLocalHost() const { return fLocalHost; }
   const char *GetOptionsAnchor() const { return fOptionsAnchor; }
   const char *GetOutputFileName() const { return fOutputFileName; }
   const char *GetWorkerOrdinal() const { return fWorkerOrdinal; }

   ERunType    GetRunType() const { return fRunType; }
   UInt_t      GetTypeOpt() const { return fTypeOpt; }
   Bool_t      IsMerge() const { return (fRunType == kMerge) ? kTRUE : kFALSE; }
   Bool_t      IsMerged() const { return fMerged; }
   Bool_t      IsRegister() const { return ((fTypeOpt & kRegister) || (fTypeOpt & kVerify)) ? kTRUE : kFALSE; }

   Bool_t      IsRetrieve() const { return (TestBit(TProofOutputFile::kRetrieve)) ? kTRUE : kFALSE; }
   void        SetRetrieve(Bool_t on = kTRUE) { if (on) { SetBit(TProofOutputFile::kRetrieve);
                                                        } else { ResetBit(TProofOutputFile::kRetrieve); }}

   Int_t AdoptFile(TFile *f);                    // Adopt a TFile already open
   TFile* OpenFile(const char *opt);             // Open a file with the specified name in fFileName1
   Long64_t Merge(TCollection *list);
   void Print(Option_t *option = "") const;
   void SetOutputFileName(const char *name);
   void ResetFileCollection() { fDataSet = 0; }

   static Int_t AssertDir(const char *dirpath);

   ClassDef(TProofOutputFile,5) // Wrapper class to steer the merging of files produced on workers
};

#endif
 TProofOutputFile.h:1
 TProofOutputFile.h:2
 TProofOutputFile.h:3
 TProofOutputFile.h:4
 TProofOutputFile.h:5
 TProofOutputFile.h:6
 TProofOutputFile.h:7
 TProofOutputFile.h:8
 TProofOutputFile.h:9
 TProofOutputFile.h:10
 TProofOutputFile.h:11
 TProofOutputFile.h:12
 TProofOutputFile.h:13
 TProofOutputFile.h:14
 TProofOutputFile.h:15
 TProofOutputFile.h:16
 TProofOutputFile.h:17
 TProofOutputFile.h:18
 TProofOutputFile.h:19
 TProofOutputFile.h:20
 TProofOutputFile.h:21
 TProofOutputFile.h:22
 TProofOutputFile.h:23
 TProofOutputFile.h:24
 TProofOutputFile.h:25
 TProofOutputFile.h:26
 TProofOutputFile.h:27
 TProofOutputFile.h:28
 TProofOutputFile.h:29
 TProofOutputFile.h:30
 TProofOutputFile.h:31
 TProofOutputFile.h:32
 TProofOutputFile.h:33
 TProofOutputFile.h:34
 TProofOutputFile.h:35
 TProofOutputFile.h:36
 TProofOutputFile.h:37
 TProofOutputFile.h:38
 TProofOutputFile.h:39
 TProofOutputFile.h:40
 TProofOutputFile.h:41
 TProofOutputFile.h:42
 TProofOutputFile.h:43
 TProofOutputFile.h:44
 TProofOutputFile.h:45
 TProofOutputFile.h:46
 TProofOutputFile.h:47
 TProofOutputFile.h:48
 TProofOutputFile.h:49
 TProofOutputFile.h:50
 TProofOutputFile.h:51
 TProofOutputFile.h:52
 TProofOutputFile.h:53
 TProofOutputFile.h:54
 TProofOutputFile.h:55
 TProofOutputFile.h:56
 TProofOutputFile.h:57
 TProofOutputFile.h:58
 TProofOutputFile.h:59
 TProofOutputFile.h:60
 TProofOutputFile.h:61
 TProofOutputFile.h:62
 TProofOutputFile.h:63
 TProofOutputFile.h:64
 TProofOutputFile.h:65
 TProofOutputFile.h:66
 TProofOutputFile.h:67
 TProofOutputFile.h:68
 TProofOutputFile.h:69
 TProofOutputFile.h:70
 TProofOutputFile.h:71
 TProofOutputFile.h:72
 TProofOutputFile.h:73
 TProofOutputFile.h:74
 TProofOutputFile.h:75
 TProofOutputFile.h:76
 TProofOutputFile.h:77
 TProofOutputFile.h:78
 TProofOutputFile.h:79
 TProofOutputFile.h:80
 TProofOutputFile.h:81
 TProofOutputFile.h:82
 TProofOutputFile.h:83
 TProofOutputFile.h:84
 TProofOutputFile.h:85
 TProofOutputFile.h:86
 TProofOutputFile.h:87
 TProofOutputFile.h:88
 TProofOutputFile.h:89
 TProofOutputFile.h:90
 TProofOutputFile.h:91
 TProofOutputFile.h:92
 TProofOutputFile.h:93
 TProofOutputFile.h:94
 TProofOutputFile.h:95
 TProofOutputFile.h:96
 TProofOutputFile.h:97
 TProofOutputFile.h:98
 TProofOutputFile.h:99
 TProofOutputFile.h:100
 TProofOutputFile.h:101
 TProofOutputFile.h:102
 TProofOutputFile.h:103
 TProofOutputFile.h:104
 TProofOutputFile.h:105
 TProofOutputFile.h:106
 TProofOutputFile.h:107
 TProofOutputFile.h:108
 TProofOutputFile.h:109
 TProofOutputFile.h:110
 TProofOutputFile.h:111
 TProofOutputFile.h:112
 TProofOutputFile.h:113
 TProofOutputFile.h:114
 TProofOutputFile.h:115
 TProofOutputFile.h:116
 TProofOutputFile.h:117
 TProofOutputFile.h:118
 TProofOutputFile.h:119
 TProofOutputFile.h:120
 TProofOutputFile.h:121
 TProofOutputFile.h:122
 TProofOutputFile.h:123
 TProofOutputFile.h:124
 TProofOutputFile.h:125
 TProofOutputFile.h:126
 TProofOutputFile.h:127
 TProofOutputFile.h:128
 TProofOutputFile.h:129