Logo ROOT   6.08/07
Reference Guide
TMPWorker.h
Go to the documentation of this file.
1 /* @(#)root/multiproc:$Id$ */
2 // Author: Enrico Guiraud July 2015
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #ifndef ROOT_TMPWorker
13 #define ROOT_TMPWorker
14 
15 #include "MPCode.h"
16 #include "MPSendRecv.h" //MPCodeBufPair
17 #include "PoolUtils.h"
18 #include "TFile.h"
19 #include "TKey.h"
20 #include "TSysEvtHandler.h" //TFileHandler
21 #include "TTree.h"
22 #include "TTreeCache.h"
23 
24 #include <memory> //unique_ptr
25 #include <string>
26 #include <sstream>
27 #include <type_traits> //std::result_of
28 #include <unistd.h> //pid_t
29 
30 class TMPWorker {
31  /// \cond
32  ClassDef(TMPWorker, 0);
33  /// \endcond
34 public:
35  TMPWorker();
36  TMPWorker(const std::vector<std::string>& fileNames, const std::string& treeName, unsigned nWorkers, ULong64_t maxEntries);
37  TMPWorker(TTree *tree, unsigned nWorkers, ULong64_t maxEntries);
38  virtual ~TMPWorker();
39  //it doesn't make sense to copy a TMPWorker (each one has a uniq_ptr to its socket)
40  TMPWorker(const TMPWorker &) = delete;
41  TMPWorker &operator=(const TMPWorker &) = delete;
42 
43  virtual void Init(int fd, unsigned workerN);
44  void Run();
45  TSocket *GetSocket() { return fS.get(); }
46  pid_t GetPid() { return fPid; }
47  unsigned GetNWorker() const { return fNWorker; }
48 
49 protected:
50  std::string fId; ///< identifier string in the form W<nwrk>|P<proc id>
51  std::vector<std::string> fFileNames; ///< the files to be processed by all workers
52  std::string fTreeName; ///< the name of the tree to be processed
53  TTree *fTree; ///< pointer to the tree to be processed. It is only used if the tree is directly passed to TProcessExecutor::Process as argument
54  TFile *fFile; ///< last open file
55  unsigned fNWorkers; ///< the number of workers spawned
56  ULong64_t fMaxNEntries; ///< the maximum number of entries to be processed by this worker
57  ULong64_t fProcessedEntries; ///< the number of entries processed by this worker so far
58 
59  void CloseFile();
60  TFile *OpenFile(const std::string& fileName);
61  TTree *RetrieveTree(TFile *fp);
62  void SendError(const std::string& errmsg, unsigned int code = MPCode::kError);
63  void Setup();
64  void SetupTreeCache(TTree *tree);
65 
66 private:
67  virtual void HandleInput(MPCodeBufPair &msg);
68 
69  std::unique_ptr<TSocket> fS; ///< This worker's socket. The unique_ptr makes sure resources are released.
70  pid_t fPid; ///< the PID of the process in which this worker is running
71  unsigned fNWorker; ///< the ordinal number of this worker (0 to nWorkers-1)
72 
73 
74  // TTree cache handling
75  TTreeCache *fTreeCache; // instance of the tree cache for the tree
76  Bool_t fTreeCacheIsLearning; // Whether cache is in learning phase
77  Bool_t fUseTreeCache; // Control usage of the tree cache
78  Long64_t fCacheSize; // Cache size
79 };
80 
81 #endif
pid_t GetPid()
Definition: TMPWorker.h:46
Bool_t fTreeCacheIsLearning
Definition: TMPWorker.h:76
virtual void HandleInput(MPCodeBufPair &msg)
Handle a message with an EMPCode.
Definition: TMPWorker.cxx:145
long long Long64_t
Definition: RtypesCore.h:69
pid_t fPid
the PID of the process in which this worker is running
Definition: TMPWorker.h:70
TFile * OpenFile(const std::string &fileName)
Handle file opening.
Definition: TMPWorker.cxx:185
unsigned fNWorker
the ordinal number of this worker (0 to nWorkers-1)
Definition: TMPWorker.h:71
A specialized TFileCacheRead object for a TTree.
Definition: TTreeCache.h:34
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:50
This class works in conjuction with TMPClient, reacting to messages received from it as specified by ...
Definition: TMPWorker.h:30
bool Bool_t
Definition: RtypesCore.h:59
Error message.
Definition: MPCode.h:30
ULong64_t fProcessedEntries
the number of entries processed by this worker so far
Definition: TMPWorker.h:57
std::string fTreeName
the name of the tree to be processed
Definition: TMPWorker.h:52
void SendError(const std::string &errmsg, unsigned int code=MPCode::kError)
Error sender.
Definition: TMPWorker.cxx:268
unsigned GetNWorker() const
Definition: TMPWorker.h:47
#define ClassDef(name, id)
Definition: Rtypes.h:254
Bool_t fUseTreeCache
Definition: TMPWorker.h:77
TTree * fTree
pointer to the tree to be processed. It is only used if the tree is directly passed to TProcessExecut...
Definition: TMPWorker.h:53
ULong64_t fMaxNEntries
the maximum number of entries to be processed by this worker
Definition: TMPWorker.h:56
void SetupTreeCache(TTree *tree)
Tree cache handling.
Definition: TMPWorker.cxx:236
std::unique_ptr< TSocket > fS
This worker&#39;s socket. The unique_ptr makes sure resources are released.
Definition: TMPWorker.h:69
void CloseFile()
Handle file closing.
Definition: TMPWorker.cxx:172
TFile * fFile
last open file
Definition: TMPWorker.h:54
Long64_t fCacheSize
Definition: TMPWorker.h:78
std::pair< unsigned, std::unique_ptr< TBufferFile > > MPCodeBufPair
An std::pair that wraps the code and optional object contained in a message.
Definition: MPSendRecv.h:31
TMPWorker()
Class constructors.
Definition: TMPWorker.cxx:56
TTreeCache * fTreeCache
Definition: TMPWorker.h:75
unsigned fNWorkers
the number of workers spawned
Definition: TMPWorker.h:55
TSocket * GetSocket()
Definition: TMPWorker.h:45
TMPWorker & operator=(const TMPWorker &)=delete
void Run()
Definition: TMPWorker.cxx:120
unsigned long long ULong64_t
Definition: RtypesCore.h:70
virtual ~TMPWorker()
Definition: TMPWorker.cxx:88
std::vector< std::string > fFileNames
the files to be processed by all workers
Definition: TMPWorker.h:51
std::string fId
identifier string in the form W<nwrk>|P<proc id>="">
Definition: TMPWorker.h:50
virtual void Init(int fd, unsigned workerN)
This method is called by children processes right after forking.
Definition: TMPWorker.cxx:111
Definition: tree.py:1
A TTree object has a header with a name and a title.
Definition: TTree.h:98
void Setup()
Auxilliary method for common initializations.
Definition: TMPWorker.cxx:96
TTree * RetrieveTree(TFile *fp)
Retrieve a tree from an open file.
Definition: TMPWorker.cxx:204