CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_7_hltpatch2/src/DataFormats/Common/interface/DetSetAlgorithm.h

Go to the documentation of this file.
00001 #ifndef DataFormats_Common_DetSetAlgorithm_h
00002 #define DataFormats_Common_DetSetAlgorithm_h
00003 
00004 #include "DataFormats/Common/interface/DetSetVectorNew.h"
00005 #include <boost/ref.hpp>
00006 #include <boost/function.hpp>
00007 #include <algorithm>
00008 
00009 //FIXME remove New when ready
00010 namespace edmNew {
00011 
00012   // adapt the RecHit accessors to DSTV
00013   template <typename DSTV, typename A, typename B>
00014   typename DSTV::Range detsetRangeFromPair(DSTV const & v, std::pair<A,B> const & p) {
00015     return v.equal_range(p.first,p.second);
00016   }
00017 
00018 
00019   // invoke f for each object in the range of DataSets selected by sel
00020   // to ease use, f is passed by reference
00021   template <typename DSTV, typename A, typename B, typename F>
00022   void foreachDetSetObject(DSTV const & v, std::pair<A,B> const & sel, F & f) {
00023     typedef typename DSTV::data_type data_type;
00024     typename DSTV::Range range = detsetRangeFromPair(v,sel);
00025     for(typename DSTV::const_iterator id=range.first; id!=range.second; id++)
00026       std::for_each((*id).begin(), (*id).end(),
00027                     boost::function<void(const data_type &)>(boost::ref(f)));
00028   }
00029 
00030   namespace dstvdetails {
00031 
00032     struct Pointer {
00033       template<typename H> 
00034       H const * operator()(H const& h) const { return &h;}
00035     };
00036 
00037   }
00038 
00039   // to write an easy iterator on objects in a range of DataSets selected by sel
00040   // is left as exercise to the reader 
00041   // here we provide this not optimal solution...
00042   template <typename DSTV, typename A, typename B, typename T>
00043   void copyDetSetRange(DSTV const & dstv,    
00044                        std::vector<T const *> & v, 
00045                        std::pair<A,B> const & sel) {
00046     typename DSTV::Range range = dstv.equal_range(sel.first,sel.second);
00047     for(typename DSTV::const_iterator id=range.first; id!=range.second; id++){
00048       size_t cs = v.size();
00049       v.resize(cs+(*id).size());
00050       std::transform((*id).begin(), (*id).end(),v.begin()+cs,dstvdetails::Pointer());
00051     } 
00052   }
00053 }
00054 
00055 
00056 #endif //  DataFormats_Common_DetSetAlgorithm_h