// @(#)root/proof:$Id$
// Author: Fons Rademakers   11/01/02

/*************************************************************************
 * Copyright (C) 1995-2001, 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_TDSet
#define ROOT_TDSet


//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TDSet                                                                //
//                                                                      //
// This class implements a data set to be used for PROOF processing.    //
// The TDSet defines the class of which objects will be processed,      //
// the directory in the file where the objects of that type can be      //
// found and the list of files to be processed. The files can be        //
// specified as logical file names (LFN's) or as physical file names    //
// (PFN's). In case of LFN's the resolution to PFN's will be done       //
// according to the currently active GRID interface.                    //
// Examples:                                                            //
//   TDSet treeset("TTree", "AOD");                                     //
//   treeset.Add("lfn:/alien.cern.ch/alice/prod2002/file1");            //
//   ...                                                                //
//   treeset.AddFriend(friendset);                                      //
//                                                                      //
// or                                                                   //
//                                                                      //
//   TDSet objset("MyEvent", "*", "/events");                           //
//   objset.Add("root://cms.cern.ch/user/prod2002/hprod_1.root");       //
//   ...                                                                //
//   objset.Add(set2003);                                               //
//                                                                      //
// Validity of file names will only be checked at processing time       //
// (typically on the PROOF master server), not at creation time.        //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

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

class TChain;
class TCollection;
class TCut;
class TDSet;
class TEventList;
class TEntryList;
class TFileInfo;
class THashList;
class TIter;
class TList;
class TProof;
class TProofChain;
class TSelector;
class TTree;

// For backward compatibility (handle correctly requests from old clients)
#include <set>
#include <list>

class TDSetElement : public TNamed {
public:
   typedef  std::list<std::pair<TDSetElement*, TString> > FriendsList_t;
   // TDSetElement status bits
   enum EStatusBits {
      kHasBeenLookedUp = BIT(15),
      kWriteV3         = BIT(16),
      kEmpty           = BIT(17),
      kCorrupted       = BIT(18),
      kNewRun          = BIT(19),
      kNewPacket       = BIT(20)
   };

private:
   TString          fDirectory;  // directory in file where to look for objects
   Long64_t         fFirst;      // first entry to process
   Long64_t         fNum;        // number of entries to process
   TString          fMsd;        // mass storage domain name
   Long64_t         fTDSetOffset;// the global offset in the TDSet of the first
                                 // entry in this element
   TObject         *fEntryList;  // entry (or event) list to be used in processing
   Bool_t           fValid;      // whether or not the input values are valid
   Long64_t         fEntries;    // total number of possible entries in file
   TList           *fFriends;    // friend elements

   TString          fDataSet;    // Name of the dataset of which this element is part
   TList           *fAssocObjList;  // List of objects associated to this element
                                   // (e.g. TObjString describing associated files)
   Float_t          fMaxProcTime; // Max processing time in secs; -1 no limit

   Bool_t           HasBeenLookedUp() const { return TestBit(kHasBeenLookedUp); }

   TDSetElement& operator=(const TDSetElement &); // Not implemented

public:
   TDSetElement();
   TDSetElement(const char *file, const char *objname = 0,
                const char *dir = 0, Long64_t first = 0, Long64_t num = -1,
                const char *msd = 0, const char *dataset = 0);
   TDSetElement(const TDSetElement& elem);
   virtual ~TDSetElement();

   virtual TList   *GetListOfFriends() const { return fFriends; }
   virtual void     AddFriend(TDSetElement *friendElement, const char *alias);
   virtual void     DeleteFriends();
   const char      *GetFileName() const { return GetName(); }
   Long64_t         GetFirst() const { return fFirst; }
   void             SetFirst(Long64_t first) { fFirst = first; }
   Long64_t         GetNum() const { return fNum; }
   Long64_t         GetEntries(Bool_t istree = kTRUE, Bool_t openfile = kTRUE);
   void             SetEntries(Long64_t ent) { fEntries = ent; }
   const char      *GetMsd() const { return fMsd; }
   void             SetNum(Long64_t num) { fNum = num; }
   Bool_t           GetValid() const { return fValid; }
   const char      *GetObjName() const { return GetTitle(); }
   const char      *GetDirectory() const;
   const char      *GetDataSet() const { return fDataSet; }
   void             SetDataSet(const char *dataset) { fDataSet = dataset; }
   void             AddAssocObj(TObject *assocobj);
   TList           *GetListOfAssocObjs() const { return fAssocObjList; }
   TObject         *GetAssocObj(Long64_t i, Bool_t isentry = kFALSE);
   void             Print(Option_t *options="") const;
   Long64_t         GetTDSetOffset() const { return fTDSetOffset; }
   void             SetTDSetOffset(Long64_t offset) { fTDSetOffset = offset; }
   void             SetEntryList(TObject *aList, Long64_t first = -1, Long64_t num = -1);
   TObject         *GetEntryList() const { return fEntryList; }
   void             Validate(Bool_t isTree);
   void             Validate(TDSetElement *elem);
   void             Invalidate() { fValid = kFALSE; }
   void             SetValid() { fValid = kTRUE; }
   Int_t            Compare(const TObject *obj) const;
   Bool_t           IsSortable() const { return kTRUE; }
   Int_t            Lookup(Bool_t force = kFALSE);
   void             SetLookedUp() { SetBit(kHasBeenLookedUp); }
   TFileInfo       *GetFileInfo(const char *type = "TTree");

   Float_t          GetMaxProcTime() const { return fMaxProcTime; }
   void             SetMaxProcTime(Float_t mpt) { fMaxProcTime = mpt; }

   Int_t            MergeElement(TDSetElement *elem);

   ClassDef(TDSetElement,9)  // A TDSet element
};


class TDSet : public TNamed {

public:
   // TDSet status bits
   enum EStatusBits {
      kWriteV3         = BIT(16),
      kEmpty           = BIT(17),
      kValidityChecked = BIT(18),  // Set if elements validiy has been checked
      kSomeInvalid     = BIT(19),  // Set if at least one element is invalid
      kMultiDSet       = BIT(20),   // Set if fElements is a list of datasets
      kIsLocal         = BIT(21)   // Set if local, non-PROOF, processing
   };

private:
   Bool_t         fIsTree;      // true if type is a TTree (or TTree derived)
   TObject       *fEntryList;   //! entry (or event) list for processing
   TProofChain   *fProofChain;  //! for browsing purposes

   void           SplitEntryList(); //Split entry list between elements

   TDSet(const TDSet &);           // not implemented
   void operator=(const TDSet &);  // not implemented

protected:
   TString        fDir;         // name of the directory
   TString        fType;        // type of objects (e.g. TTree);
   TString        fObjName;     // name of objects to be analyzed (e.g. TTree name)
   THashList     *fElements;    //-> list of TDSetElements (or TDSets, if in multi mode)
   TIter         *fIterator;    //! iterator on fElements
   TDSetElement  *fCurrent;     //! current element
   TList         *fSrvMaps;     //! list for mapping server coordinates for files
   TIter         *fSrvMapsIter; //! iterator on fSrvMaps

public:
   TDSet();
   TDSet(const char *name, const char *objname = "*",
         const char *dir = "/", const char *type = 0);
   TDSet(const TChain &chain, Bool_t withfriends = kTRUE);
   virtual ~TDSet();

   virtual Bool_t        Add(const char *file, const char *objname = 0,
                             const char *dir = 0, Long64_t first = 0,
                             Long64_t num = -1, const char *msd = 0);
   virtual Bool_t        Add(TDSet *set);
   virtual Bool_t        Add(TCollection *fileinfo, const char *meta = 0,
                             Bool_t availableOnly = kFALSE, TCollection *badlist = 0);
   virtual Bool_t        Add(TFileInfo *fileinfo, const char *meta = 0);
   virtual void          AddFriend(TDSet *friendset, const char *alias);

   virtual Long64_t      Process(TSelector *selector, Option_t *option = "",
                                 Long64_t nentries = -1,
                                 Long64_t firstentry = 0,
                                 TObject *enl = 0); // *MENU*
   virtual Long64_t      Process(const char *selector, Option_t *option = "",
                                 Long64_t nentries = -1,
                                 Long64_t firstentry = 0,
                                 TObject *enl = 0); // *MENU*
   virtual Long64_t      Draw(const char *varexp, const char *selection,
                              Option_t *option = "", Long64_t nentries = -1,
                              Long64_t firstentry = 0); // *MENU*
   virtual Long64_t      Draw(const char *varexp, const TCut &selection,
                              Option_t *option = "", Long64_t nentries = -1,
                              Long64_t firstentry = 0); // *MENU*
   virtual void          Draw(Option_t *opt) { Draw(opt, "", "", 1000000000, 0); }

   Int_t                 ExportFileList(const char *filepath, Option_t *opt = "");

   void                  Print(Option_t *option="") const;

   void                  SetObjName(const char *objname);
   void                  SetDirectory(const char *dir);

   Bool_t                IsTree() const { return fIsTree; }
   Bool_t                IsValid() const { return !fType.IsNull(); }
   Bool_t                ElementsValid();
   const char           *GetType() const { return fType; }
   const char           *GetObjName() const { return fObjName; }
   const char           *GetDirectory() const { return fDir; }
   TList                *GetListOfElements() const { return (TList *)fElements; }
   Int_t                 GetNumOfFiles();

   Int_t                 Remove(TDSetElement *elem, Bool_t deleteElem = kTRUE);

   virtual void          Reset();
   virtual TDSetElement *Next(Long64_t totalEntries = -1);
   TDSetElement         *Current() const { return fCurrent; };

   static Long64_t       GetEntries(Bool_t isTree, const char *filename,
                                    const char *path, TString &objname);

   void                  AddInput(TObject *obj);
   void                  ClearInput();
   TObject              *GetOutput(const char *name);
   TList                *GetOutputList();
   virtual void          StartViewer(); // *MENU*

   virtual TTree        *GetTreeHeader(TProof *proof);
   virtual void          SetEntryList(TObject *aList);
   TObject              *GetEntryList() const { return fEntryList; }
   void                  Validate();
   void                  Validate(TDSet *dset);

   void                  Lookup(Bool_t removeMissing = kFALSE, TList **missingFiles = 0);
   void                  SetLookedUp();

   void                  SetSrvMaps(TList *srvmaps = 0);

   void                  SetWriteV3(Bool_t on = kTRUE);

   ClassDef(TDSet,9)  // Data set for remote processing (PROOF)
};

#endif
 TDSet.h:1
 TDSet.h:2
 TDSet.h:3
 TDSet.h:4
 TDSet.h:5
 TDSet.h:6
 TDSet.h:7
 TDSet.h:8
 TDSet.h:9
 TDSet.h:10
 TDSet.h:11
 TDSet.h:12
 TDSet.h:13
 TDSet.h:14
 TDSet.h:15
 TDSet.h:16
 TDSet.h:17
 TDSet.h:18
 TDSet.h:19
 TDSet.h:20
 TDSet.h:21
 TDSet.h:22
 TDSet.h:23
 TDSet.h:24
 TDSet.h:25
 TDSet.h:26
 TDSet.h:27
 TDSet.h:28
 TDSet.h:29
 TDSet.h:30
 TDSet.h:31
 TDSet.h:32
 TDSet.h:33
 TDSet.h:34
 TDSet.h:35
 TDSet.h:36
 TDSet.h:37
 TDSet.h:38
 TDSet.h:39
 TDSet.h:40
 TDSet.h:41
 TDSet.h:42
 TDSet.h:43
 TDSet.h:44
 TDSet.h:45
 TDSet.h:46
 TDSet.h:47
 TDSet.h:48
 TDSet.h:49
 TDSet.h:50
 TDSet.h:51
 TDSet.h:52
 TDSet.h:53
 TDSet.h:54
 TDSet.h:55
 TDSet.h:56
 TDSet.h:57
 TDSet.h:58
 TDSet.h:59
 TDSet.h:60
 TDSet.h:61
 TDSet.h:62
 TDSet.h:63
 TDSet.h:64
 TDSet.h:65
 TDSet.h:66
 TDSet.h:67
 TDSet.h:68
 TDSet.h:69
 TDSet.h:70
 TDSet.h:71
 TDSet.h:72
 TDSet.h:73
 TDSet.h:74
 TDSet.h:75
 TDSet.h:76
 TDSet.h:77
 TDSet.h:78
 TDSet.h:79
 TDSet.h:80
 TDSet.h:81
 TDSet.h:82
 TDSet.h:83
 TDSet.h:84
 TDSet.h:85
 TDSet.h:86
 TDSet.h:87
 TDSet.h:88
 TDSet.h:89
 TDSet.h:90
 TDSet.h:91
 TDSet.h:92
 TDSet.h:93
 TDSet.h:94
 TDSet.h:95
 TDSet.h:96
 TDSet.h:97
 TDSet.h:98
 TDSet.h:99
 TDSet.h:100
 TDSet.h:101
 TDSet.h:102
 TDSet.h:103
 TDSet.h:104
 TDSet.h:105
 TDSet.h:106
 TDSet.h:107
 TDSet.h:108
 TDSet.h:109
 TDSet.h:110
 TDSet.h:111
 TDSet.h:112
 TDSet.h:113
 TDSet.h:114
 TDSet.h:115
 TDSet.h:116
 TDSet.h:117
 TDSet.h:118
 TDSet.h:119
 TDSet.h:120
 TDSet.h:121
 TDSet.h:122
 TDSet.h:123
 TDSet.h:124
 TDSet.h:125
 TDSet.h:126
 TDSet.h:127
 TDSet.h:128
 TDSet.h:129
 TDSet.h:130
 TDSet.h:131
 TDSet.h:132
 TDSet.h:133
 TDSet.h:134
 TDSet.h:135
 TDSet.h:136
 TDSet.h:137
 TDSet.h:138
 TDSet.h:139
 TDSet.h:140
 TDSet.h:141
 TDSet.h:142
 TDSet.h:143
 TDSet.h:144
 TDSet.h:145
 TDSet.h:146
 TDSet.h:147
 TDSet.h:148
 TDSet.h:149
 TDSet.h:150
 TDSet.h:151
 TDSet.h:152
 TDSet.h:153
 TDSet.h:154
 TDSet.h:155
 TDSet.h:156
 TDSet.h:157
 TDSet.h:158
 TDSet.h:159
 TDSet.h:160
 TDSet.h:161
 TDSet.h:162
 TDSet.h:163
 TDSet.h:164
 TDSet.h:165
 TDSet.h:166
 TDSet.h:167
 TDSet.h:168
 TDSet.h:169
 TDSet.h:170
 TDSet.h:171
 TDSet.h:172
 TDSet.h:173
 TDSet.h:174
 TDSet.h:175
 TDSet.h:176
 TDSet.h:177
 TDSet.h:178
 TDSet.h:179
 TDSet.h:180
 TDSet.h:181
 TDSet.h:182
 TDSet.h:183
 TDSet.h:184
 TDSet.h:185
 TDSet.h:186
 TDSet.h:187
 TDSet.h:188
 TDSet.h:189
 TDSet.h:190
 TDSet.h:191
 TDSet.h:192
 TDSet.h:193
 TDSet.h:194
 TDSet.h:195
 TDSet.h:196
 TDSet.h:197
 TDSet.h:198
 TDSet.h:199
 TDSet.h:200
 TDSet.h:201
 TDSet.h:202
 TDSet.h:203
 TDSet.h:204
 TDSet.h:205
 TDSet.h:206
 TDSet.h:207
 TDSet.h:208
 TDSet.h:209
 TDSet.h:210
 TDSet.h:211
 TDSet.h:212
 TDSet.h:213
 TDSet.h:214
 TDSet.h:215
 TDSet.h:216
 TDSet.h:217
 TDSet.h:218
 TDSet.h:219
 TDSet.h:220
 TDSet.h:221
 TDSet.h:222
 TDSet.h:223
 TDSet.h:224
 TDSet.h:225
 TDSet.h:226
 TDSet.h:227
 TDSet.h:228
 TDSet.h:229
 TDSet.h:230
 TDSet.h:231
 TDSet.h:232
 TDSet.h:233
 TDSet.h:234
 TDSet.h:235
 TDSet.h:236
 TDSet.h:237
 TDSet.h:238
 TDSet.h:239
 TDSet.h:240
 TDSet.h:241
 TDSet.h:242
 TDSet.h:243
 TDSet.h:244
 TDSet.h:245
 TDSet.h:246
 TDSet.h:247
 TDSet.h:248
 TDSet.h:249
 TDSet.h:250
 TDSet.h:251
 TDSet.h:252
 TDSet.h:253
 TDSet.h:254
 TDSet.h:255
 TDSet.h:256
 TDSet.h:257
 TDSet.h:258
 TDSet.h:259
 TDSet.h:260
 TDSet.h:261
 TDSet.h:262
 TDSet.h:263
 TDSet.h:264
 TDSet.h:265