Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/root/PoolUtils.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* @(#)root/multiproc:$Id$ */
0002 // Author: Enrico Guiraud July 2015
0003 
0004 /*************************************************************************
0005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
0006  * All rights reserved.                                                  *
0007  *                                                                       *
0008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
0009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
0010  *************************************************************************/
0011 
0012 #ifndef ROOT_PoolUtils
0013 #define ROOT_PoolUtils
0014 
0015 #include "TError.h"
0016 #include "TList.h"
0017 #include "TObject.h"
0018 #include <vector>
0019 
0020 
0021 namespace ROOT {
0022 //////////////////////////////////////////////////////////////////////////
0023 ///
0024 /// This namespace contains pre-defined functions to be used in
0025 /// conjuction with TExecutor::Map and TExecutor::MapReduce.
0026 ///
0027 //////////////////////////////////////////////////////////////////////////
0028    namespace ExecutorUtils {
0029      //////////////////////////////////////////////////////////////////////////
0030      /// Merge collection of TObjects.
0031      /// This functor looks for an implementation of the Merge method
0032      /// (e.g. TH1F::Merge) and calls it on the objects contained in objs.
0033      /// If Merge is not found, a null pointer is returned.
0034       template <class T>
0035       class ReduceObjects{
0036         public:
0037         T operator()(const std::vector<T> &objs){
0038          static_assert(std::is_constructible<TObject *, T>::value,
0039                        "The argument should be a vector of pointers to TObject or derived classes");
0040          if(objs.size() == 0)
0041             return nullptr;
0042 
0043          if(objs.size() == 1)
0044             return objs[0];
0045 
0046          //get first object from objs
0047          auto obj = objs[0];
0048          //get merge function
0049          ROOT::MergeFunc_t merge = obj->IsA()->GetMerge();
0050          if(!merge) {
0051             Error("PoolUtils::ReduceObjects", "could not find merge method for the TObject\n. Aborting operation.");
0052             return nullptr;
0053          }
0054 
0055          //put the rest of the objs in a list
0056          TList mergelist;
0057          unsigned NObjs = objs.size();
0058          for(unsigned i=1; i<NObjs; ++i) //skip first object
0059             mergelist.Add(objs[i]);
0060 
0061          //call merge
0062          merge(obj, &mergelist, nullptr);
0063          mergelist.Delete();
0064 
0065          //return result
0066          return obj;
0067        }
0068      };
0069    }
0070 }
0071 
0072 // For backward compatibility
0073 namespace PoolUtils = ROOT::ExecutorUtils;
0074 
0075 namespace ROOT {
0076    namespace Internal {
0077       namespace ExecutorUtils {
0078          // The caster casts a pointer to a TObject to a specialised type F and leaves
0079          // unaltered the other cases.
0080          template <class O, class F>
0081          class ResultCaster {
0082          public:
0083             static O CastIfNeeded(O &&obj)
0084             {
0085                return obj;
0086             }
0087          };
0088          template <class F>
0089          class ResultCaster<TObject *, F> {
0090          public:
0091             static typename std::enable_if<std::is_pointer<F>::value, F>::type CastIfNeeded(TObject *obj)
0092             {
0093                return static_cast<F>(obj);
0094             }
0095          };
0096       }
0097    // For backward compatibility
0098    namespace PoolUtils = ExecutorUtils;
0099    }
0100 }
0101 
0102 
0103 #endif