Logo ROOT   6.08/07
Reference Guide
PoolUtils.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_PoolUtils
13 #define ROOT_PoolUtils
14 
15 #include "TError.h"
16 #include "TList.h"
17 #include "TObject.h"
18 #include <vector>
19 
20 namespace PoolCode {
21 
22  //////////////////////////////////////////////////////////////////////////
23  ///
24  /// An enumeration of the message codes handled by TPool, TPoolWorker and TPoolProcessor
25  ///
26  //////////////////////////////////////////////////////////////////////////
27 
28  enum EPoolCode : unsigned {
29  //not an enum class because we want to be able to easily cast back and forth from unsigned
30  /* TPool::Map */
31  kExecFunc = 0, ///< Execute function without arguments
32  kExecFuncWithArg, ///< Execute function with the argument contained in the message
33  kFuncResult, ///< The message contains the result of a function execution
34  /* TPool::MapReduce */
35  kIdling, ///< We are ready for the next task
36  kSendResult, ///< Ask for a kFuncResult/kProcResult
37  /* TPool::Process */
38  kProcFile, ///< Tell a TPoolProcessor which tree to process. The object sent is a TreeInfo
39  kProcRange, ///< Tell a TPoolProcessor which tree and entries range to process. The object sent is a TreeRangeInfo
40  kProcTree, ///< Tell a TPoolProcessor to process the tree that was passed to it at construction time
41  kProcSelector, ///< Tell a TPoolProcessor to process the tree using the selector passed to it at construction time
42  kProcResult, ///< The message contains the result of the processing of a TTree
43  kProcEnded, ///< Tell the client we are done processing (i.e. we have reached the target number of entries to process)
44  kProcError, ///< Tell the client there was an error while processing
45  };
46 
47 }
48 
49 //////////////////////////////////////////////////////////////////////////
50 ///
51 /// This namespace contains pre-defined functions to be used in
52 /// conjuction with TExecutor::Map and TExecutor::MapReduce.
53 ///
54 //////////////////////////////////////////////////////////////////////////
55 namespace ROOT {
56  namespace ExecutorUtils {
57  //////////////////////////////////////////////////////////////////////////
58  /// Merge collection of TObjects.
59  /// This functor looks for an implementation of the Merge method
60  /// (e.g. TH1F::Merge) and calls it on the objects contained in objs.
61  /// If Merge is not found, a null pointer is returned.
62  template <class T>
64  public:
65  T operator()(const std::vector<T> &objs){
66  static_assert(std::is_constructible<TObject *, T>::value,
67  "The argument should be a vector of pointers to TObject or derived classes");
68  if(objs.size() == 0)
69  return nullptr;
70 
71  if(objs.size() == 1)
72  return objs[0];
73 
74  //get first object from objs
75  auto obj = objs[0];
76  //get merge function
77  ROOT::MergeFunc_t merge = obj->IsA()->GetMerge();
78  if(!merge) {
79  Error("PoolUtils::ReduceObjects", "could not find merge method for the TObject\n. Aborting operation.");
80  return nullptr;
81  }
82 
83  //put the rest of the objs in a list
84  TList mergelist;
85  unsigned NObjs = objs.size();
86  for(unsigned i=1; i<NObjs; ++i) //skip first object
87  mergelist.Add(objs[i]);
88 
89  //call merge
90  merge(obj, &mergelist, nullptr);
91  mergelist.Delete();
92 
93  //return result
94  return obj;
95  }
96  };
97  }
98 }
99 
100 // For backward compatibility
101 namespace PoolUtils = ROOT::ExecutorUtils;
102 
103 namespace ROOT {
104  namespace Internal {
105  namespace ExecutorUtils {
106  // The caster casts a pointer to a TObject to a specialised type F and leaves
107  // unaltered the other cases.
108  template <class O, class F>
109  class ResultCaster {
110  public:
111  static O CastIfNeeded(O &&obj)
112  {
113  return obj;
114  }
115  };
116  template <class F>
117  class ResultCaster<TObject *, F> {
118  public:
119  static typename std::enable_if<std::is_pointer<F>::value, F>::type CastIfNeeded(TObject *obj)
120  {
121  return static_cast<F>(obj);
122  }
123  };
124  }
125  // For backward compatibility
126  namespace PoolUtils = ExecutorUtils;
127  }
128 }
129 
130 
131 #endif
The message contains the result of a function execution.
Definition: PoolUtils.h:33
virtual void Delete(Option_t *option="")
Remove all objects from the list AND delete all heap based objects.
Definition: TList.cxx:405
Tell a TPoolProcessor to process the tree that was passed to it at construction time.
Definition: PoolUtils.h:40
Tell the client we are done processing (i.e. we have reached the target number of entries to process)...
Definition: PoolUtils.h:43
Tell a TPoolProcessor which tree and entries range to process. The object sent is a TreeRangeInfo...
Definition: PoolUtils.h:39
This namespace contains pre-defined functions to be used in conjuction with TExecutor::Map and TExecu...
Definition: StringConv.hxx:21
Long64_t(* MergeFunc_t)(void *, TCollection *, TFileMergeInfo *)
Definition: Rtypes.h:153
EPoolCode
An enumeration of the message codes handled by TPool, TPoolWorker and TPoolProcessor.
Definition: PoolUtils.h:28
double T(double x)
Definition: ChebyshevPol.h:34
Tell a TPoolProcessor which tree to process. The object sent is a TreeInfo.
Definition: PoolUtils.h:38
T operator()(const std::vector< T > &objs)
Definition: PoolUtils.h:65
We are ready for the next task.
Definition: PoolUtils.h:35
Merge collection of TObjects.
Definition: PoolUtils.h:63
void Error(const char *location, const char *msgfmt,...)
static std::enable_if< std::is_pointer< F >::value, F >::type CastIfNeeded(TObject *obj)
Definition: PoolUtils.h:119
A doubly linked list.
Definition: TList.h:47
#define F(x, y, z)
Execute function with the argument contained in the message.
Definition: PoolUtils.h:32
The message contains the result of the processing of a TTree.
Definition: PoolUtils.h:42
Execute function without arguments.
Definition: PoolUtils.h:31
Tell the client there was an error while processing.
Definition: PoolUtils.h:44
Ask for a kFuncResult/kProcResult.
Definition: PoolUtils.h:36
int type
Definition: TGX11.cxx:120
Mother of all ROOT objects.
Definition: TObject.h:37
virtual void Add(TObject *obj)
Definition: TList.h:81
Tell a TPoolProcessor to process the tree using the selector passed to it at construction time...
Definition: PoolUtils.h:41