CMS 3D CMS Logo

PATObjectUserDataEmbedder.cc
Go to the documentation of this file.
6 
9 
15 
16 namespace pat {
17 
18  namespace helper {
19 
21  typedef bool value_type;
23  template<typename ObjectType>
24  void addData(ObjectType &obj, const std::string & key, const value_type &val) { obj.addUserInt(key, val); }
25  };
26 
27  template<typename A>
29  public:
31  if (iConfig.existsAs<edm::ParameterSet>(main)) {
32  edm::ParameterSet const & srcPSet = iConfig.getParameter<edm::ParameterSet>(main);
33  for (const std::string & label : srcPSet.getParameterNamesForType<edm::InputTag>()) {
34  labelsAndTokens_.emplace_back(label, cc.consumes<typename A::product_type>(srcPSet.getParameter<edm::InputTag>(label)));
35  }
36  }
37  }
38  template<typename T>
39  void addData(const edm::Event &iEvent, const std::vector<edm::Ptr<T>> & ptrs, std::vector<T> & out) const {
40  A adder;
41  unsigned int n = ptrs.size();
43  for (const auto & pair : labelsAndTokens_) {
44  iEvent.getByToken(pair.second, handle);
45  for (unsigned int i = 0; i < n; ++i) {
46  adder.addData(out[i], pair.first, (*handle)[ptrs[i]]);
47  }
48  }
49  }
50  private:
51  std::vector<std::pair<std::string,edm::EDGetTokenT<typename A::product_type>>> labelsAndTokens_;
52  }; // class NamedUserDataLoader
53  } // namespace helper
54 
55  template<typename T>
57 
58  public:
59 
60  explicit PATObjectUserDataEmbedder(const edm::ParameterSet & iConfig) :
61  src_(consumes<edm::View<T>>(iConfig.getParameter<edm::InputTag>("src"))),
62  userFloats_(iConfig, "userFloats", consumesCollector()),
63  userInts_(iConfig, "userInts", consumesCollector()),
64  userIntFromBools_(iConfig, "userIntFromBools", consumesCollector()),
65  userCands_(iConfig, "userCands", consumesCollector())
66  {
67  produces<std::vector<T>>();
68  }
69 
71 
72  void produce(edm::Event & iEvent, const edm::EventSetup& iSetup) override;
73 
74  static void fillDescriptions(edm::ConfigurationDescriptions & descriptions) {
76  desc.add<edm::InputTag>("src");
77  for (const std::string & what : { "userFloats", "userInts", "userIntFromBools", "userCands" }) {
79  descNested.addWildcard<edm::InputTag>("*");
80  desc.add<edm::ParameterSetDescription>(what, descNested);
81  }
82  if (typeid(T) == typeid(pat::Muon)) descriptions.add("muonsWithUserData", desc);
83  else if (typeid(T) == typeid(pat::Electron)) descriptions.add("electronsWithUserData", desc);
84  else if (typeid(T) == typeid(pat::Photon)) descriptions.add("photonsWithUserData", desc);
85  else if (typeid(T) == typeid(pat::Tau)) descriptions.add("tausWithUserData", desc);
86  else if (typeid(T) == typeid(pat::Jet)) descriptions.add("jetsWithUserData", desc);
87  }
88 
89  private:
90  // configurables
96  };
97 
98 }
99 
100 template<typename T>
103  iEvent.getByToken(src_, src);
104 
105  std::unique_ptr<std::vector<T>> out(new std::vector<T>());
106  out->reserve(src->size());
107 
108  std::vector<edm::Ptr<T>> ptrs;
109  ptrs.reserve(src->size());
110  for (unsigned int i = 0, n = src->size(); i < n; ++i) {
111  // copy by value, save the ptr
112  out->push_back((*src)[i]);
113  ptrs.push_back(src->ptrAt(i));
114  }
115  userFloats_.addData(iEvent, ptrs, *out);
116  userInts_.addData(iEvent, ptrs, *out);
117  userIntFromBools_.addData(iEvent, ptrs, *out);
118  userCands_.addData(iEvent, ptrs, *out);
119 
120  iEvent.put(std::move(out));
121 }
122 
128 
helper::NamedUserDataLoader< pat::helper::AddUserInt > userInts_
T getParameter(std::string const &) const
Analysis-level Photon class.
Definition: Photon.h:47
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:136
pat::PATObjectUserDataEmbedder< pat::Electron > PATElectronUserDataEmbedder
Definition: helper.py:1
bool existsAs(std::string const &parameterName, bool trackiness=true) const
checks if a parameter exists as a given type
Definition: ParameterSet.h:186
pat::PATObjectUserDataEmbedder< pat::Tau > PATTauUserDataEmbedder
ParameterWildcardBase * addWildcard(U const &pattern)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:519
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
pat::PATObjectUserDataEmbedder< pat::Photon > PATPhotonUserDataEmbedder
helper::NamedUserDataLoader< pat::helper::AddUserFloat > userFloats_
helper::NamedUserDataLoader< pat::helper::AddUserCand > userCands_
Definition: HeavyIon.h:7
std::vector< std::string > getParameterNamesForType(bool trackiness=true) const
Definition: ParameterSet.h:194
pat::PATObjectUserDataEmbedder< pat::Jet > PATJetUserDataEmbedder
int iEvent
Definition: GenABIO.cc:230
NamedUserDataLoader(const edm::ParameterSet &iConfig, const std::string &main, edm::ConsumesCollector &&cc)
edm::ValueMap< value_type > product_type
std::vector< std::pair< std::string, edm::EDGetTokenT< typename A::product_type > > > labelsAndTokens_
ParameterDescriptionBase * add(U const &iLabel, T const &value)
Analysis-level tau class.
Definition: Tau.h:55
edm::EDGetTokenT< edm::View< T > > src_
PATObjectUserDataEmbedder(const edm::ParameterSet &iConfig)
pat::PATObjectUserDataEmbedder< pat::Muon > PATMuonUserDataEmbedder
Analysis-level electron class.
Definition: Electron.h:52
Analysis-level calorimeter jet class.
Definition: Jet.h:78
void add(std::string const &label, ParameterSetDescription const &psetDescription)
HLT enums.
Definition: main.py:1
helper::NamedUserDataLoader< pat::helper::AddUserIntFromBool > userIntFromBools_
void addData(const edm::Event &iEvent, const std::vector< edm::Ptr< T >> &ptrs, std::vector< T > &out) const
long double T
void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
def move(src, dest)
Definition: eostools.py:510
void addData(ObjectType &obj, const std::string &key, const value_type &val)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)