12 #ifndef ROOT_TPoolProcessor
13 #define ROOT_TPoolProcessor
30 #include <type_traits>
39 template<class T, typename std::enable_if<std::is_pointer<T>::value && std::is_constructible<TObject*, T>::value>
::type* =
nullptr>
42 auto th1p =
dynamic_cast<TH1*
>(res);
48 auto ttreep =
dynamic_cast<TTree*
>(res);
49 if(ttreep !=
nullptr) {
54 auto tentrylist =
dynamic_cast<TEntryList*
>(res);
55 if(tentrylist !=
nullptr) {
60 auto teventlist =
dynamic_cast<TEventList*
>(res);
61 if(teventlist !=
nullptr) {
73 TPoolProcessor(
F procFunc,
const std::vector<std::string>& fileNames,
const std::string& treeName,
unsigned nWorkers,
ULong64_t maxEntries);
78 void Init(
int fd,
unsigned workerN);
92 const std::string& treeName,
unsigned nWorkers,
ULong64_t maxEntries)
93 :
TMPWorker(fileNames, treeName, nWorkers, maxEntries),
94 fProcFunc(procFunc), fReducedResult(), fCanReduce(false)
101 fProcFunc(procFunc), fReducedResult(), fCanReduce(false)
108 unsigned code = msg.first;
120 std::string reply =
"S" + std::to_string(GetNWorker());
121 reply +=
": unknown code received: " + std::to_string(code);
131 fMaxNEntries = EvalMaxEntries(fMaxNEntries);
141 unsigned nProcessed = 0;
145 Error(
"TPoolProcessor::Process",
"[S]: Process:kProcTree fTree undefined!\n");
149 nProcessed = ReadBuffer<unsigned>(msg.second.get());
151 fileN = nProcessed / fNWorkers;
154 fileN = ReadBuffer<unsigned>(msg.second.get());
157 std::unique_ptr<TFile> fp;
164 fp.reset(
OpenFile(fTree->GetCurrentFile()->GetName()));
166 fp.reset(
OpenFile(fFileNames[fileN]));
175 tree = RetrieveTree(fp.get());
176 if(tree ==
nullptr) {
186 SetupTreeCache(tree);
195 unsigned nBunch = nEntries / fNWorkers;
196 unsigned rangeN = nProcessed % fNWorkers;
197 start = rangeN*nBunch;
198 if(rangeN < (fNWorkers-1))
199 finish = (rangeN+1)*nBunch;
210 if (fProcessedEntries + finish - start > fMaxNEntries)
211 finish = start + fMaxNEntries - fProcessedEntries;
217 std::string reply =
"S" + std::to_string(GetNWorker());
218 reply +=
": could not set TTreeReader to range " + std::to_string(start) +
" " + std::to_string(finish);
224 auto res = fProcFunc(reader);
230 fProcessedEntries += finish - start;
233 PoolUtils::ReduceObjects<TObject *> redfunc;
234 fReducedResult =
static_cast<decltype(fReducedResult)
>(redfunc({res, fReducedResult}));
237 fReducedResult = res;
240 if(fMaxNEntries == fProcessedEntries)
254 if(GetNWorker() < fNWorkers-1)
255 return maxEntries/fNWorkers;
257 return maxEntries - (fNWorkers-1)*(maxEntries/fNWorkers);
std::result_of< F(std::reference_wrapper< TTreeReader >)>::type fReducedResult
the results of the executions of fProcFunc merged together
bool fCanReduce
true if fReducedResult can be reduced with a new result, false until we have produced one result ...
Tell a TPoolProcessor to process the tree that was passed to it at construction time.
TTreeReader is a simple, robust and fast interface to read values from a TTree, TChain or TNtuple...
Tell a TPoolProcessor which tree and entries range to process. The object sent is a TreeRangeInfo...
virtual void SetDirectory(TDirectory *dir)
By default when an histogram is created, it is added to the list of histogram objects in the current ...
TFile * OpenFile(const TString &fin)
This class works in conjuction with TMPClient, reacting to messages received from it as specified by ...
int MPSend(TSocket *s, unsigned code)
Send a message with the specified code on the specified socket.
Tell a TPoolProcessor which tree to process. The object sent is a TreeInfo.
We are ready for the next task.
virtual void SetDirectory(TDirectory *dir)
Add reference to directory dir. dir can be 0.
void Process(unsigned code, MPCodeBufPair &msg)
ULong64_t EvalMaxEntries(ULong64_t maxEntries)
void Error(const char *location, const char *msgfmt,...)
void HandleInput(MPCodeBufPair &msg)
Execute instructions received from a TPool client.
F fProcFunc
the function to be executed
virtual void SetDirectory(TDirectory *dir)
Remove reference to this EventList from current directory and add reference to new directory dir...
TPoolProcessor(F procFunc, const std::vector< std::string > &fileNames, const std::string &treeName, unsigned nWorkers, ULong64_t maxEntries)
A TEventList object is a list of selected events (entries) in a TTree.
The message contains the result of the processing of a TTree.
std::pair< unsigned, std::unique_ptr< TBufferFile >> MPCodeBufPair
An std::pair that wraps the code and optional object contained in a message.
Tell the client there was an error while processing.
virtual void SetDirectory(TDirectory *dir)
Change the tree's directory.
Ask for a kFuncResult/kProcResult.
unsigned long long ULong64_t
virtual void Init(int fd, unsigned workerN)
This method is called by children processes right after forking.
void Init(int fd, unsigned workerN)
This method is called by children processes right after forking.
virtual Long64_t GetEntries() const
A TTree object has a header with a name and a title.
A List of entry numbers in a TTree or TChain.
EEntryStatus SetEntriesRange(Long64_t first, Long64_t last)
Set the range of entries to be processed.