CMS 3D CMS Logo

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 
35 
38 
39 #include <iostream>
40 
41 namespace pat {
42  namespace helper {
43  struct AddUserInt {
44  typedef int value_type;
46  template <typename ObjectType>
47  void addData(ObjectType &obj, const std::string &key, const value_type &val) {
48  obj.addUserInt(key, val);
49  }
50  };
51  struct AddUserFloat {
52  typedef float value_type;
54  template <typename ObjectType>
55  void addData(ObjectType &obj, const std::string &key, const value_type &val) {
56  obj.addUserFloat(key, val);
57  }
58  };
59  struct AddUserPtr {
62  template <typename ObjectType>
63  void addData(ObjectType &obj, const std::string &key, const value_type &val) {
64  obj.addUserDataFromPtr(key, val);
65  }
66  };
67  struct AddUserCand {
70  template <typename ObjectType>
71  void addData(ObjectType &obj, const std::string &key, const value_type &val) {
72  obj.addUserCand(key, val);
73  }
74  };
75 
76  } // namespace helper
77 
78  template <typename ObjectType, typename Operation>
80  public:
84 
85  static void fillDescription(edm::ParameterSetDescription &iDesc);
86 
87  // Method to call from PATUserDataHelper to add information to the PATObject in question.
88  void add(ObjectType &patObject, edm::Event const &iEvent, edm::EventSetup const &iSetup);
89 
90  private:
91  // configurables
92  std::vector<edm::InputTag> userDataSrc_;
93  std::vector<edm::EDGetTokenT<typename Operation::product_type>> userDataSrcTokens_;
94  std::vector<std::string> labelPostfixesToStrip_, labels_;
95  Operation loader_;
96  };
97 
98 } // namespace pat
99 
100 // Constructor: Initilize user data src
101 template <typename ObjectType, typename Operation>
104  : userDataSrc_(iConfig.getParameter<std::vector<edm::InputTag>>("src")),
105  labelPostfixesToStrip_(iConfig.existsAs<std::vector<std::string>>("labelPostfixesToStrip")
106  ? iConfig.getParameter<std::vector<std::string>>("labelPostfixesToStrip")
107  : std::vector<std::string>()) {
108  for (std::vector<edm::InputTag>::const_iterator input_it = userDataSrc_.begin(); input_it != userDataSrc_.end();
109  ++input_it) {
110  userDataSrcTokens_.push_back(iC.consumes<typename Operation::product_type>(*input_it));
111  }
112  for (edm::InputTag tag : userDataSrc_) { // copy by value
113  for (const std::string &stripme : labelPostfixesToStrip_) {
114  auto match = tag.label().rfind(stripme);
115  if (match == (tag.label().length() - stripme.length())) {
116  tag = edm::InputTag(tag.label().substr(0, match), tag.instance(), tag.process());
117  }
118  }
119  labels_.push_back(tag.encode());
120  }
121 }
122 
123 /* ==================================================================================
124  PATUserDataMerger::add
125  This expects four inputs:
126  patObject: ObjectType to add to
127 
128  from Event:
129  userDataSrc: The data to add, which is a ValueMap keyed by recoObject
130 
131  from Setup:
132  none currently
133 
134  This will simply add the UserData *'s from the value map that are
135  indexed by the reco objects, to the pat object's user data vector.
136  ==================================================================================
137 */
138 
139 template <class ObjectType, typename Operation>
141  edm::Event const &iEvent,
142  const edm::EventSetup &iSetup) {
143  typename std::vector<edm::EDGetTokenT<typename Operation::product_type>>::const_iterator
144  token_begin = userDataSrcTokens_.begin(),
145  token_it = userDataSrcTokens_.begin(), token_end = userDataSrcTokens_.end();
146 
147  for (; token_it != token_end; ++token_it) {
148  const std::string &encoded = (labels_.at(token_it - token_begin));
149 
150  // Declare the object handles:
151  // ValueMap containing the values, or edm::Ptr's to the UserData that
152  // is associated to those PAT Objects
154 
155  // Get the objects by label
156  if (encoded.empty())
157  continue;
158  iEvent.getByToken(*token_it, userData);
159 
160  edm::Ptr<reco::Candidate> recoObject = patObject.originalObjectRef();
161  loader_.addData(patObject, encoded, (*userData)[recoObject]);
162  }
163 }
164 
165 template <class ObjectType, typename Operation>
167  iDesc.add<std::vector<edm::InputTag>>("src");
168  iDesc.addOptional<std::vector<std::string>>("labelPostfixesToStrip", std::vector<std::string>());
169 }
170 
171 #endif
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
ParameterDescriptionBase * addOptional(U const &iLabel, T const &value)
Definition: helper.py:1
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:525
reco::CandidatePtr value_type
void addData(ObjectType &obj, const std::string &key, const value_type &val)
edm::ValueMap< value_type > product_type
Definition: HeavyIon.h:7
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
ParameterDescriptionBase * add(U const &iLabel, T const &value)
void addData(ObjectType &obj, const std::string &key, const value_type &val)
void add(std::map< std::string, TH1 * > &h, TH1 *hist)
std::vector< edm::InputTag > userDataSrc_
static void fillDescription(edm::ParameterSetDescription &iDesc)
std::vector< std::string > labels_
HLT enums.
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_