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 
42 namespace pat {
43  namespace helper {
44  struct AddUserInt {
45  typedef int value_type;
47  template<typename ObjectType>
48  void addData(ObjectType &obj, const std::string & key, const value_type &val) { obj.addUserInt(key, val); }
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) { obj.addUserFloat(key, val); }
55  };
56  struct AddUserPtr {
59  template<typename ObjectType>
60  void addData(ObjectType &obj, const std::string & key, const value_type &val) {
61  obj.addUserDataFromPtr(key, val);
62  }
63  };
64  struct AddUserCand {
67  template<typename ObjectType>
68  void addData(ObjectType &obj, const std::string & key, const value_type &val) { obj.addUserCand(key, val); }
69  };
70 
71  }
72 
73  template<typename ObjectType, typename Operation>
75 
76  public:
77 
81 
82  static void fillDescription(edm::ParameterSetDescription & iDesc);
83 
84  // Method to call from PATUserDataHelper to add information to the PATObject in question.
85  void add(ObjectType & patObject,
86  edm::Event const & iEvent, edm::EventSetup const & iSetup);
87 
88  private:
89 
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 
98 }
99 
100 // Constructor: Initilize user data src
101 template<typename ObjectType, typename Operation>
103  userDataSrc_(iConfig.getParameter<std::vector<edm::InputTag> >("src")),
104  labelPostfixesToStrip_(iConfig.existsAs<std::vector<std::string>>("labelPostfixesToStrip") ? iConfig.getParameter<std::vector<std::string>>("labelPostfixesToStrip") : std::vector<std::string>())
105 {
106  for ( std::vector<edm::InputTag>::const_iterator input_it = userDataSrc_.begin(); input_it != userDataSrc_.end(); ++input_it ) {
107  userDataSrcTokens_.push_back( iC.consumes< typename Operation::product_type >( *input_it ) );
108  }
109  for (edm::InputTag tag : userDataSrc_) { // copy by value
110  for (const std::string & stripme : labelPostfixesToStrip_) {
111  auto match = tag.label().rfind(stripme);
112  if (match == (tag.label().length() - stripme.length())) {
113  tag = edm::InputTag(tag.label().substr(0, match), tag.instance(), tag.process());
114  }
115  }
116  labels_.push_back(tag.encode());
117  }
118 }
119 
120 
121 /* ==================================================================================
122  PATUserDataMerger::add
123  This expects four inputs:
124  patObject: ObjectType to add to
125 
126  from Event:
127  userDataSrc: The data to add, which is a ValueMap keyed by recoObject
128 
129  from Setup:
130  none currently
131 
132  This will simply add the UserData *'s from the value map that are
133  indexed by the reco objects, to the pat object's user data vector.
134  ==================================================================================
135 */
136 
137 template<class ObjectType, typename Operation>
138 void
140  edm::Event const & iEvent,
141  const edm::EventSetup& iSetup)
142 {
143 
144  typename std::vector<edm::EDGetTokenT<typename Operation::product_type> >::const_iterator token_begin = userDataSrcTokens_.begin(), 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() ) continue;
156  iEvent.getByToken( *token_it, userData );
157 
158  edm::Ptr<reco::Candidate> recoObject = patObject.originalObjectRef();
159  loader_.addData( patObject, encoded, (*userData)[recoObject]);
160 
161  }
162 
163 }
164 
165 template<class ObjectType, typename Operation>
166 void
168 {
169  iDesc.add<std::vector<edm::InputTag> >("src");
170  iDesc.addOptional<std::vector<std::string>>("labelPostfixesToStrip", std::vector<std::string>());
171 }
172 
173 #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:519
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:230
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_