CMS 3D CMS Logo

RefHelper.h
Go to the documentation of this file.
1 #ifndef PhysicsTools_PatUtils_RefHelper_h
2 #define PhysicsTools_PatUtils_RefHelper_h
3 
6 namespace pat { namespace helper {
7 /*** \brief A class to help manage references and associative containers in some tricky cases (e.g. selection by copy)
8 */
9 template<typename T>
10 class RefHelper {
11  public:
12  typedef typename edm::Ptr<T> Ref;
13 
15  RefHelper(const edm::ValueMap< Ref > &backRefMap) : backRefMap_(backRefMap) { }
16 
18  Ref parentOrNull(const Ref &ref) const ;
19 
21  Ref parentOrSelf(const Ref &ref) const ;
22 
24  Ref ancestorOrSelf(const Ref &ref) const ;
25 
26  bool isRoot(const Ref &ref) const ;
27 
29  bool isAncestorOf(const Ref &old, const Ref &young) const ;
30 
32  bool sharedAncestor(const Ref &ref1, const Ref &ref2) const ;
33 
36  template<typename V, typename SomeRef>
37  V recursiveLookup(const SomeRef &ref, const edm::ValueMap<V> &map) const ;
38 
41  template<typename V, typename SomeRef>
42  V ancestorLookup(const SomeRef &ref, const edm::ValueMap<V> &map) const ;
43 
44  private:
46 };
47 
48 template<typename T>
50  if (backRefMap_.contains(ref.id())) {
51  try {
52  return backRefMap_[ref];
53  } catch (edm::Exception &e) {
54  if (e.categoryCode() == edm::errors::InvalidReference) {
55  return Ref();
56  } else {
57  throw;
58  }
59  }
60  } else {
61  return Ref();
62  }
63 }
64 
65 template<typename T>
67  Ref ret = parentOrNull(ref);
68  return ret.isNonnull() ? ret : ref;
69 }
70 
71 template<typename T>
73  Ref ret = ref;
74  do {
75  Ref test = parentOrNull(ret);
76  if (test.isNull()) return ret;
77  ret = test;
78  } while (true);
79 }
80 
81 
82 template<typename T>
83 bool RefHelper<T>::isRoot(const RefHelper<T>::Ref &ref) const {
84  return parentOrNull(ref).isNull();
85 }
86 
87 template<typename T>
88 bool RefHelper<T>::isAncestorOf(const RefHelper<T>::Ref &old, const RefHelper<T>::Ref &young) const {
89  Ref test = young;
90  do {
91  if (test == old) return true;
92  test = parentOrNull(test);
93  } while (test.isNonnull());
94  return false;
95 }
96 
97 template<typename T>
98 bool RefHelper<T>::sharedAncestor(const RefHelper<T>::Ref &ref1, const RefHelper<T>::Ref &ref2) const {
99  return ( ancestorOrSelf(ref1) == ancestorOrSelf(ref2) );
100 }
101 
102 template<typename T>
103 template<typename V, typename SomeRef>
104 V RefHelper<T>::recursiveLookup(const SomeRef &ref, const edm::ValueMap<V> &map) const {
105  Ref test(ref);
106  do {
107  if (map.contains(test.id())) {
108  try {
109  return map[test];
110  } catch (edm::Exception &e) {
111  if (e.categoryCode() != edm::errors::InvalidReference) {
112  throw;
113  }
114  }
115  }
116  test = parentOrNull(test);
117  } while (test.isNonnull());
119  "RefHelper: recursive Lookup failed: neither the specified ref nor any of its parents are in the map.\n";
120 }
121 
122 template<typename T>
123 template<typename V, typename SomeRef>
124 V RefHelper<T>::ancestorLookup(const SomeRef &ref, const edm::ValueMap<V> &map) const {
125  Ref tref(ref);
126  return map[ancestorOrSelf(tref)];
127 }
128 
129 } } // namespace
130 #endif
runTheMatrix.ret
ret
prodAgent to be discontinued
Definition: runTheMatrix.py:542
cms::cuda::V
cudaStream_t T uint32_t const T *__restrict__ const uint32_t *__restrict__ uint32_t int cudaStream_t V
Definition: HistoContainer.h:99
pat::helper::RefHelper::isAncestorOf
bool isAncestorOf(const Ref &old, const Ref &young) const
true if old is some ancestor of young (it does not have to be the root)
Definition: RefHelper.h:88
edm::errors::InvalidReference
Definition: EDMException.h:39
pat::helper::RefHelper::parentOrNull
Ref parentOrNull(const Ref &ref) const
Returns a Ref to the direct parent of "ref", or a null Ref if "ref" is already root.
Definition: RefHelper.h:49
pat::helper::RefHelper::RefHelper
RefHelper(const edm::ValueMap< Ref > &backRefMap)
Constructor taking a ValueMap of back-references daughter => mother.
Definition: RefHelper.h:15
edm::Exception
Definition: EDMException.h:77
test
Definition: SmallWORMDict.h:13
ctpps_dqm_sourceclient-live_cfg.test
test
Definition: ctpps_dqm_sourceclient-live_cfg.py:7
pat::helper::RefHelper::Ref
edm::Ptr< T > Ref
Definition: RefHelper.h:12
edm::Ptr::id
ProductID id() const
Accessor for product ID.
Definition: Ptr.h:158
pat::helper::RefHelper::backRefMap_
const edm::ValueMap< Ref > & backRefMap_
Definition: RefHelper.h:45
helper
Definition: helper.py:1
pat::helper::RefHelper::isRoot
bool isRoot(const Ref &ref) const
Definition: RefHelper.h:83
Ptr.h
pat::helper::RefHelper::recursiveLookup
V recursiveLookup(const SomeRef &ref, const edm::ValueMap< V > &map) const
Definition: RefHelper.h:104
pat
Definition: HeavyIon.h:7
edm::Ptr
Definition: AssociationVector.h:31
pat::helper::RefHelper::sharedAncestor
bool sharedAncestor(const Ref &ref1, const Ref &ref2) const
true if the two share the same root
Definition: RefHelper.h:98
ValueMap.h
pat::helper::RefHelper::parentOrSelf
Ref parentOrSelf(const Ref &ref) const
Returns a Ref to the direct parent of "ref", or "ref" itself if it's already root.
Definition: RefHelper.h:66
reco::JetExtendedAssociation::Ref
edm::Ref< Container > Ref
Definition: JetExtendedAssociation.h:32
edm::ValueMap
Definition: ValueMap.h:107
Exception
Definition: hltDiff.cc:245
pat::helper::RefHelper::ancestorLookup
V ancestorLookup(const SomeRef &ref, const edm::ValueMap< V > &map) const
Definition: RefHelper.h:124
genParticles_cff.map
map
Definition: genParticles_cff.py:11
pat::helper::RefHelper
Definition: RefHelper.h:10
pat::helper::RefHelper::ancestorOrSelf
Ref ancestorOrSelf(const Ref &ref) const
Climbs back the Ref chain and returns the root of the branch starting from "ref".
Definition: RefHelper.h:72
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37