Logo ROOT   6.10/09
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 
21 namespace ROOT {
22 //////////////////////////////////////////////////////////////////////////
23 ///
24 /// This namespace contains pre-defined functions to be used in
25 /// conjuction with TExecutor::Map and TExecutor::MapReduce.
26 ///
27 //////////////////////////////////////////////////////////////////////////
28  namespace ExecutorUtils {
29  //////////////////////////////////////////////////////////////////////////
30  /// Merge collection of TObjects.
31  /// This functor looks for an implementation of the Merge method
32  /// (e.g. TH1F::Merge) and calls it on the objects contained in objs.
33  /// If Merge is not found, a null pointer is returned.
34  template <class T>
36  public:
37  T operator()(const std::vector<T> &objs){
38  static_assert(std::is_constructible<TObject *, T>::value,
39  "The argument should be a vector of pointers to TObject or derived classes");
40  if(objs.size() == 0)
41  return nullptr;
42 
43  if(objs.size() == 1)
44  return objs[0];
45 
46  //get first object from objs
47  auto obj = objs[0];
48  //get merge function
49  ROOT::MergeFunc_t merge = obj->IsA()->GetMerge();
50  if(!merge) {
51  Error("PoolUtils::ReduceObjects", "could not find merge method for the TObject\n. Aborting operation.");
52  return nullptr;
53  }
54 
55  //put the rest of the objs in a list
56  TList mergelist;
57  unsigned NObjs = objs.size();
58  for(unsigned i=1; i<NObjs; ++i) //skip first object
59  mergelist.Add(objs[i]);
60 
61  //call merge
62  merge(obj, &mergelist, nullptr);
63  mergelist.Delete();
64 
65  //return result
66  return obj;
67  }
68  };
69  }
70 }
71 
72 // For backward compatibility
73 namespace PoolUtils = ROOT::ExecutorUtils;
74 
75 namespace ROOT {
76  namespace Internal {
77  namespace ExecutorUtils {
78  // The caster casts a pointer to a TObject to a specialised type F and leaves
79  // unaltered the other cases.
80  template <class O, class F>
81  class ResultCaster {
82  public:
83  static O CastIfNeeded(O &&obj)
84  {
85  return obj;
86  }
87  };
88  template <class F>
89  class ResultCaster<TObject *, F> {
90  public:
91  static typename std::enable_if<std::is_pointer<F>::value, F>::type CastIfNeeded(TObject *obj)
92  {
93  return static_cast<F>(obj);
94  }
95  };
96  }
97  // For backward compatibility
98  namespace PoolUtils = ExecutorUtils;
99  }
100 }
101 
102 
103 #endif
virtual void Delete(Option_t *option="")
Remove all objects from the list AND delete all heap based objects.
Definition: TList.cxx:409
This namespace contains pre-defined functions to be used in conjuction with TExecutor::Map and TExecu...
Definition: PoolUtils.h:28
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
Long64_t(* MergeFunc_t)(void *, TCollection *, TFileMergeInfo *)
Definition: Rtypes.h:108
double T(double x)
Definition: ChebyshevPol.h:34
T operator()(const std::vector< T > &objs)
Definition: PoolUtils.h:37
Merge collection of TObjects.
Definition: PoolUtils.h:35
static std::enable_if< std::is_pointer< F >::value, F >::type CastIfNeeded(TObject *obj)
Definition: PoolUtils.h:91
A doubly linked list.
Definition: TList.h:43
#define F(x, y, z)
int type
Definition: TGX11.cxx:120
Mother of all ROOT objects.
Definition: TObject.h:37
virtual void Add(TObject *obj)
Definition: TList.h:77
void Error(ErrorHandler_t func, int code, const char *va_(fmt),...)
Write error message and call a handler, if required.