CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
PATUserDataMerger.h
Go to the documentation of this file.
1 //
2 //
3 
4 #ifndef PhysicsTools_PatAlgos_PATUserDataMerger_h
5 #define PhysicsTools_PatAlgos_PATUserDataMerger_h
6 
34 
37 
38 #include <iostream>
39 
40 namespace pat {
41  namespace helper {
42  struct AddUserInt {
43  typedef int value_type;
45  template <typename ObjectType>
46  void addData(ObjectType &obj, const std::string &key, const value_type &val) {
47  obj.addUserInt(key, val);
48  }
49  };
50  struct AddUserFloat {
51  typedef float value_type;
53  template <typename ObjectType>
54  void addData(ObjectType &obj, const std::string &key, const value_type &val) {
55  obj.addUserFloat(key, val);
56  }
57  };
58  struct AddUserPtr {
61  template <typename ObjectType>
62  void addData(ObjectType &obj, const std::string &key, const value_type &val) {
63  obj.addUserDataFromPtr(key, val);
64  }
65  };
66  struct AddUserCand {
69  template <typename ObjectType>
70  void addData(ObjectType &obj, const std::string &key, const value_type &val) {
71  obj.addUserCand(key, val);
72  }
73  };
74 
75  } // namespace helper
76 
77  template <typename ObjectType, typename Operation>
79  public:
83 
85 
86  // Method to call from PATUserDataHelper to add information to the PATObject in question.
87  void add(ObjectType &patObject, edm::Event const &iEvent, edm::EventSetup const &iSetup);
88 
89  private:
90  // configurables
91  std::vector<edm::InputTag> userDataSrc_;
92  std::vector<edm::EDGetTokenT<typename Operation::product_type>> userDataSrcTokens_;
93  std::vector<std::string> labelPostfixesToStrip_, labels_;
94  Operation loader_;
95  };
96 
97 } // namespace pat
98 
99 // Constructor: Initilize user data src
100 template <typename ObjectType, typename Operation>
103  : userDataSrc_(iConfig.getParameter<std::vector<edm::InputTag>>("src")),
104  labelPostfixesToStrip_(iConfig.existsAs<std::vector<std::string>>("labelPostfixesToStrip")
105  ? iConfig.getParameter<std::vector<std::string>>("labelPostfixesToStrip")
106  : std::vector<std::string>()) {
107  for (std::vector<edm::InputTag>::const_iterator input_it = userDataSrc_.begin(); input_it != userDataSrc_.end();
108  ++input_it) {
109  userDataSrcTokens_.push_back(iC.consumes<typename Operation::product_type>(*input_it));
110  }
111  for (edm::InputTag tag : userDataSrc_) { // copy by value
112  for (const std::string &stripme : labelPostfixesToStrip_) {
113  auto match = tag.label().rfind(stripme);
114  if (match == (tag.label().length() - stripme.length())) {
115  tag = edm::InputTag(tag.label().substr(0, match), tag.instance(), tag.process());
116  }
117  }
118  labels_.push_back(tag.encode());
119  }
120 }
121 
122 /* ==================================================================================
123  PATUserDataMerger::add
124  This expects four inputs:
125  patObject: ObjectType to add to
126 
127  from Event:
128  userDataSrc: The data to add, which is a ValueMap keyed by recoObject
129 
130  from Setup:
131  none currently
132 
133  This will simply add the UserData *'s from the value map that are
134  indexed by the reco objects, to the pat object's user data vector.
135  ==================================================================================
136 */
137 
138 template <class ObjectType, typename Operation>
140  edm::Event const &iEvent,
141  const edm::EventSetup &iSetup) {
142  typename std::vector<edm::EDGetTokenT<typename Operation::product_type>>::const_iterator
143  token_begin = userDataSrcTokens_.begin(),
144  token_it = userDataSrcTokens_.begin(), token_end = userDataSrcTokens_.end();
145 
146  for (; token_it != token_end; ++token_it) {
147  const std::string &encoded = (labels_.at(token_it - token_begin));
148 
149  // Declare the object handles:
150  // ValueMap containing the values, or edm::Ptr's to the UserData that
151  // is associated to those PAT Objects
153 
154  // Get the objects by label
155  if (encoded.empty())
156  continue;
157  iEvent.getByToken(*token_it, userData);
158 
159  edm::Ptr<reco::Candidate> recoObject = patObject.originalObjectRef();
160  loader_.addData(patObject, encoded, (*userData)[recoObject]);
161  }
162 }
163 
164 template <class ObjectType, typename Operation>
166  iDesc.add<std::vector<edm::InputTag>>("src");
167  iDesc.addOptional<std::vector<std::string>>("labelPostfixesToStrip", std::vector<std::string>());
168 }
169 
170 #endif
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
ParameterDescriptionBase * addOptional(U const &iLabel, T const &value)
edm::ValueMap< value_type > product_type
std::vector< edm::EDGetTokenT< typename Operation::product_type > > userDataSrcTokens_
Assimilates pat::UserData into pat objects.
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:539
reco::CandidatePtr value_type
void addData(ObjectType &obj, const std::string &key, const value_type &val)
edm::ValueMap< value_type > product_type
edm::ValueMap< value_type > product_type
int iEvent
Definition: GenABIO.cc:224
void addData(ObjectType &obj, const std::string &key, const value_type &val)
edm::ValueMap< value_type > product_type
tuple key
prepare the HTCondor submission files and eventually submit them
ParameterDescriptionBase * add(U const &iLabel, T const &value)
void addData(ObjectType &obj, const std::string &key, const value_type &val)
std::vector< edm::InputTag > userDataSrc_
static void fillDescription(edm::ParameterSetDescription &iDesc)
std::vector< std::string > labels_
void addData(ObjectType &obj, const std::string &key, const value_type &val)
void add(ObjectType &patObject, edm::Event const &iEvent, edm::EventSetup const &iSetup)
std::pair< typename Association::data_type::first_type, double > match(Reference key, Association association, bool bestMatchByMaxValue)
Generic matching function.
Definition: Utils.h:10
edm::Ptr< UserData > value_type
std::vector< std::string > labelPostfixesToStrip_