CMS 3D CMS Logo

PATObjectUserDataEmbedder.cc
Go to the documentation of this file.
6 
9 
15 
16 namespace {
17  template <typename T>
18  bool equal(const T &lhs, const T &rhs) {
19  throw cms::Exception("NotImplimented")
20  << " equal in PATObjectUserDataEmbedder is not implimented for objects of type " << typeid(lhs).name()
21  << " and thus their src coll must be the same collection the valuemaps are keyed off";
22  }
23  template <>
24  bool equal(const reco::GsfElectron &lhs, const reco::GsfElectron &rhs) {
25  return lhs.superCluster()->seed()->seed().rawId() == rhs.superCluster()->seed()->seed().rawId();
26  }
27  template <>
28  bool equal(const reco::Photon &lhs, const reco::Photon &rhs) {
29  return lhs.superCluster()->seed()->seed().rawId() == rhs.superCluster()->seed()->seed().rawId();
30  }
31 
32 } // namespace
33 
34 namespace pat {
35 
36  namespace helper {
37 
39  typedef bool value_type;
41  template <typename ObjectType>
42  void addData(ObjectType &obj, const std::string &key, const value_type &val) {
43  obj.addUserInt(key, val);
44  }
45  };
46 
47  template <typename T, typename TParent, typename TProd>
49  const std::vector<edm::Handle<edm::View<TParent>>> &parentColls,
50  const TProd &prod) {
51  if (prod.contains(ptr.id())) {
52  return edm::Ptr<TParent>(ptr);
53  } else {
54  for (const auto &parentColl : parentColls) {
55  if (parentColl.isValid() && prod.contains(parentColl.id())) {
56  for (size_t indx = 0; indx < parentColl->size(); indx++) {
57  if (equal<TParent>(*ptr, (*parentColl)[indx])) {
58  return edm::Ptr<TParent>(parentColl, indx);
59  }
60  }
61  //note this assumes that another parent coll isnt in the value map
62  //it if its, it'll return null when the other one might work
63  return edm::Ptr<TParent>(parentColl.id());
64  }
65  }
66  }
67  throw cms::Exception("ConfigurationError")
68  << "When accessing value maps in PATObjectUserDataEmbedder, the collection the valuemap was keyed off is not "
69  "either the input src or listed in one of the parentSrcs";
70  }
71 
72  template <typename A>
74  public:
76  if (iConfig.existsAs<edm::ParameterSet>(main)) {
77  edm::ParameterSet const &srcPSet = iConfig.getParameter<edm::ParameterSet>(main);
78  for (const std::string &label : srcPSet.getParameterNamesForType<edm::InputTag>()) {
79  labelsAndTokens_.emplace_back(
80  label, cc.consumes<typename A::product_type>(srcPSet.getParameter<edm::InputTag>(label)));
81  }
82  }
83  }
84  template <typename T, typename TParent = T>
85  void addData(const edm::Event &iEvent,
86  const std::vector<edm::Ptr<T>> &ptrs,
88  std::vector<T> &out) const {
89  A adder;
90  unsigned int n = ptrs.size();
92  for (const auto &pair : labelsAndTokens_) {
93  iEvent.getByToken(pair.second, handle);
94  for (unsigned int i = 0; i < n; ++i) {
95  auto ptr = getPtrForProd(ptrs[i], parents, *handle);
96  adder.addData(out[i], pair.first, (*handle)[ptr]);
97  }
98  }
99  }
100 
101  private:
102  std::vector<std::pair<std::string, edm::EDGetTokenT<typename A::product_type>>> labelsAndTokens_;
103  }; // class NamedUserDataLoader
104  } // namespace helper
105 
106  template <typename T, typename ParentType = T>
108  public:
110  : src_(consumes<edm::View<T>>(iConfig.getParameter<edm::InputTag>("src"))),
111  userFloats_(iConfig, "userFloats", consumesCollector()),
112  userInts_(iConfig, "userInts", consumesCollector()),
113  userIntFromBools_(iConfig, "userIntFromBools", consumesCollector()),
114  userCands_(iConfig, "userCands", consumesCollector()) {
115  for (const auto &parentSrc : iConfig.getParameter<std::vector<edm::InputTag>>("parentSrcs")) {
116  parentSrcs_.push_back(consumes<edm::View<ParentType>>(parentSrc));
117  }
118  produces<std::vector<T>>();
119  }
120 
122 
123  void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override;
124 
127  desc.add<edm::InputTag>("src");
128  desc.add<std::vector<edm::InputTag>>("parentSrcs", std::vector<edm::InputTag>());
129  for (auto &&what : {"userFloats", "userInts", "userIntFromBools", "userCands"}) {
130  edm::ParameterSetDescription descNested;
131  descNested.addWildcard<edm::InputTag>("*");
132  desc.add<edm::ParameterSetDescription>(what, descNested);
133  }
134  if (typeid(T) == typeid(pat::Muon))
135  descriptions.add("muonsWithUserData", desc);
136  else if (typeid(T) == typeid(pat::Electron))
137  descriptions.add("electronsWithUserData", desc);
138  else if (typeid(T) == typeid(pat::Photon))
139  descriptions.add("photonsWithUserData", desc);
140  else if (typeid(T) == typeid(pat::Tau))
141  descriptions.add("tausWithUserData", desc);
142  else if (typeid(T) == typeid(pat::Jet))
143  descriptions.add("jetsWithUserData", desc);
144  }
145 
146  private:
147  // configurables
149  //so valuemaps are keyed to a given collection so if we remake the objects,
150  //are valuemaps are pointing to the wrong collection
151  //this allows us to pass in past collections to try them to see if they are the ones
152  //a valuemap is keyed to
153  //note ParentType must inherit from T
154  std::vector<edm::EDGetTokenT<edm::View<ParentType>>> parentSrcs_;
155 
160  };
161 
162 } // namespace pat
163 
164 template <typename T, typename ParentType>
167  iEvent.getByToken(src_, src);
168 
169  std::vector<edm::Handle<edm::View<ParentType>>> parentSrcs;
170  parentSrcs.reserve(parentSrcs_.size());
171  for (const auto &src : parentSrcs_) {
172  parentSrcs.push_back(iEvent.getHandle(src));
173  }
174 
175  std::unique_ptr<std::vector<T>> out(new std::vector<T>());
176  out->reserve(src->size());
177 
178  std::vector<edm::Ptr<T>> ptrs;
179  ptrs.reserve(src->size());
180  for (unsigned int i = 0, n = src->size(); i < n; ++i) {
181  // copy by value, save the ptr
182  out->push_back((*src)[i]);
183  ptrs.push_back(src->ptrAt(i));
184  }
185 
186  userFloats_.addData(iEvent, ptrs, parentSrcs, *out);
187  userInts_.addData(iEvent, ptrs, parentSrcs, *out);
188  userIntFromBools_.addData(iEvent, ptrs, parentSrcs, *out);
189  userCands_.addData(iEvent, ptrs, parentSrcs, *out);
190 
191  iEvent.put(std::move(out));
192 }
193 
199 
PATObjectUserDataEmbedder(const edm::ParameterSet &iConfig)
Analysis-level Photon class.
Definition: Photon.h:46
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
TPRegexp parents
Definition: eve_filter.cc:21
Definition: helper.py:1
pat::PATObjectUserDataEmbedder< pat::Tau > PATTauUserDataEmbedder
ProductID id() const
Accessor for product ID.
Definition: Ptr.h:160
void addData(const edm::Event &iEvent, const std::vector< edm::Ptr< T >> &ptrs, std::vector< edm::Handle< edm::View< TParent >>> parents, std::vector< T > &out) const
ParameterWildcardBase * addWildcard(U const &pattern)
void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
uint32_t cc[maxCellsPerHit]
Definition: gpuFishbone.h:49
bool existsAs(std::string const &parameterName, bool trackiness=true) const
checks if a parameter exists as a given type
Definition: ParameterSet.h:172
pat::PATObjectUserDataEmbedder< pat::Photon, reco::Photon > PATPhotonUserDataEmbedder
bool equal(const T &first, const T &second)
Definition: Equal.h:32
Definition: HeavyIon.h:7
char const * label
pat::PATObjectUserDataEmbedder< pat::Jet > PATJetUserDataEmbedder
int iEvent
Definition: GenABIO.cc:224
reco::SuperClusterRef superCluster() const override
Ref to SuperCluster.
NamedUserDataLoader(const edm::ParameterSet &iConfig, const std::string &main, edm::ConsumesCollector &&cc)
helper::NamedUserDataLoader< pat::helper::AddUserCand > userCands_
std::vector< std::string > getParameterNamesForType(bool trackiness=true) const
Definition: ParameterSet.h:180
edm::EDGetTokenT< edm::View< T > > src_
helper::NamedUserDataLoader< pat::helper::AddUserFloat > userFloats_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
key
prepare the HTCondor submission files and eventually submit them
edm::ValueMap< value_type > product_type
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
std::vector< std::pair< std::string, edm::EDGetTokenT< typename A::product_type > > > labelsAndTokens_
Analysis-level tau class.
Definition: Tau.h:53
std::vector< edm::EDGetTokenT< edm::View< ParentType > > > parentSrcs_
edm::Ptr< TParent > getPtrForProd(edm::Ptr< T > ptr, const std::vector< edm::Handle< edm::View< TParent >>> &parentColls, const TProd &prod)
pat::PATObjectUserDataEmbedder< pat::Muon > PATMuonUserDataEmbedder
helper::NamedUserDataLoader< pat::helper::AddUserIntFromBool > userIntFromBools_
Analysis-level electron class.
Definition: Electron.h:51
Analysis-level calorimeter jet class.
Definition: Jet.h:77
void add(std::string const &label, ParameterSetDescription const &psetDescription)
HLT enums.
Definition: main.py:1
Definition: APVGainStruct.h:7
long double T
Analysis-level muon class.
Definition: Muon.h:51
def move(src, dest)
Definition: eostools.py:511
SuperClusterRef superCluster() const override
reference to a SuperCluster
Definition: GsfElectron.h:155
void addData(ObjectType &obj, const std::string &key, const value_type &val)
pat::PATObjectUserDataEmbedder< pat::Electron, reco::GsfElectron > PATElectronUserDataEmbedder
helper::NamedUserDataLoader< pat::helper::AddUserInt > userInts_