/* @(#)root/multiproc:$Id$ */
// Author: Enrico Guiraud July 2015

/*************************************************************************
 * Copyright (C) 1995-2000, 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_TMPWorker
#define ROOT_TMPWorker

#include "MPCode.h"
#include "MPSendRecv.h" //MPCodeBufPair
#include "PoolUtils.h"
#include "TFile.h"
#include "TKey.h"
#include "TSysEvtHandler.h" //TFileHandler
#include "TTree.h"
#include "TTreeCache.h"

#include <memory> //unique_ptr
#include <string>
#include <sstream>
#include <type_traits> //std::result_of
#include <unistd.h> //pid_t

class TMPWorker {
   /// \cond
   ClassDef(TMPWorker, 0);
   /// \endcond
public:
   TMPWorker();
   TMPWorker(const std::vector<std::string>& fileNames, const std::string& treeName, unsigned nWorkers, ULong64_t maxEntries);
   TMPWorker(TTree *tree, unsigned nWorkers, ULong64_t maxEntries);
   virtual ~TMPWorker();
   //it doesn't make sense to copy a TMPWorker (each one has a uniq_ptr to its socket)
   TMPWorker(const TMPWorker &) = delete;
   TMPWorker &operator=(const TMPWorker &) = delete;

   virtual void Init(int fd, unsigned workerN);
   void Run();
   TSocket *GetSocket() { return fS.get(); }
   pid_t GetPid() { return fPid; }
   unsigned GetNWorker() const { return fNWorker; }

protected:
   std::string fId; ///< identifier string in the form W<nwrk>|P<proc id>
   std::vector<std::string> fFileNames; ///< the files to be processed by all workers
   std::string fTreeName; ///< the name of the tree to be processed
   TTree *fTree; ///< pointer to the tree to be processed. It is only used if the tree is directly passed to TProcessExecutor::Process as argument
   TFile *fFile; ///< last open file
   unsigned fNWorkers; ///< the number of workers spawned
   ULong64_t fMaxNEntries; ///< the maximum number of entries to be processed by this worker
   ULong64_t fProcessedEntries; ///< the number of entries processed by this worker so far

   void   CloseFile();
   TFile *OpenFile(const std::string& fileName);
   TTree *RetrieveTree(TFile *fp);
   void   SendError(const std::string& errmsg, unsigned int code = MPCode::kError);
   void   Setup();
   void   SetupTreeCache(TTree *tree);

private:
   virtual void HandleInput(MPCodeBufPair &msg);

   std::unique_ptr<TSocket> fS; ///< This worker's socket. The unique_ptr makes sure resources are released.
   pid_t fPid; ///< the PID of the process in which this worker is running
   unsigned fNWorker; ///< the ordinal number of this worker (0 to nWorkers-1)


   // TTree cache handling
   TTreeCache *fTreeCache;    // instance of the tree cache for the tree
   Bool_t      fTreeCacheIsLearning; // Whether cache is in learning phase
   Bool_t      fUseTreeCache; // Control usage of the tree cache
   Long64_t    fCacheSize;    // Cache size
};

#endif
 TMPWorker.h:1
 TMPWorker.h:2
 TMPWorker.h:3
 TMPWorker.h:4
 TMPWorker.h:5
 TMPWorker.h:6
 TMPWorker.h:7
 TMPWorker.h:8
 TMPWorker.h:9
 TMPWorker.h:10
 TMPWorker.h:11
 TMPWorker.h:12
 TMPWorker.h:13
 TMPWorker.h:14
 TMPWorker.h:15
 TMPWorker.h:16
 TMPWorker.h:17
 TMPWorker.h:18
 TMPWorker.h:19
 TMPWorker.h:20
 TMPWorker.h:21
 TMPWorker.h:22
 TMPWorker.h:23
 TMPWorker.h:24
 TMPWorker.h:25
 TMPWorker.h:26
 TMPWorker.h:27
 TMPWorker.h:28
 TMPWorker.h:29
 TMPWorker.h:30
 TMPWorker.h:31
 TMPWorker.h:32
 TMPWorker.h:33
 TMPWorker.h:34
 TMPWorker.h:35
 TMPWorker.h:36
 TMPWorker.h:37
 TMPWorker.h:38
 TMPWorker.h:39
 TMPWorker.h:40
 TMPWorker.h:41
 TMPWorker.h:42
 TMPWorker.h:43
 TMPWorker.h:44
 TMPWorker.h:45
 TMPWorker.h:46
 TMPWorker.h:47
 TMPWorker.h:48
 TMPWorker.h:49
 TMPWorker.h:50
 TMPWorker.h:51
 TMPWorker.h:52
 TMPWorker.h:53
 TMPWorker.h:54
 TMPWorker.h:55
 TMPWorker.h:56
 TMPWorker.h:57
 TMPWorker.h:58
 TMPWorker.h:59
 TMPWorker.h:60
 TMPWorker.h:61
 TMPWorker.h:62
 TMPWorker.h:63
 TMPWorker.h:64
 TMPWorker.h:65
 TMPWorker.h:66
 TMPWorker.h:67
 TMPWorker.h:68
 TMPWorker.h:69
 TMPWorker.h:70
 TMPWorker.h:71
 TMPWorker.h:72
 TMPWorker.h:73
 TMPWorker.h:74
 TMPWorker.h:75
 TMPWorker.h:76
 TMPWorker.h:77
 TMPWorker.h:78
 TMPWorker.h:79
 TMPWorker.h:80
 TMPWorker.h:81