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 
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
pat::helper::AddUserInt::product_type
edm::ValueMap< value_type > product_type
Definition: PATUserDataMerger.h:45
ConfigurationDescriptions.h
pat::helper::AddUserInt::value_type
int value_type
Definition: PATUserDataMerger.h:44
pat::helper::AddUserPtr::addData
void addData(ObjectType &obj, const std::string &key, const value_type &val)
Definition: PATUserDataMerger.h:63
edm::ParameterSetDescription::add
ParameterDescriptionBase * add(U const &iLabel, T const &value)
Definition: ParameterSetDescription.h:95
EDProducer.h
pat::helper::AddUserFloat::product_type
edm::ValueMap< value_type > product_type
Definition: PATUserDataMerger.h:53
edm
HLT enums.
Definition: AlignableModifier.h:19
edm::ParameterSetDescription
Definition: ParameterSetDescription.h:52
pat::PATUserDataMerger
Assimilates pat::UserData into pat objects.
Definition: PATUserDataMerger.h:79
pat::helper::AddUserCand::value_type
reco::CandidatePtr value_type
Definition: PATUserDataMerger.h:68
electronProducer_cfi.userData
userData
Definition: electronProducer_cfi.py:19
pat::PATUserDataMerger::fillDescription
static void fillDescription(edm::ParameterSetDescription &iDesc)
Definition: PATUserDataMerger.h:166
pat::helper::AddUserCand::product_type
edm::ValueMap< value_type > product_type
Definition: PATUserDataMerger.h:69
Association.h
pat::helper::AddUserCand
Definition: PATUserDataMerger.h:67
edm::Handle
Definition: AssociativeIterator.h:50
edm::ParameterSetDescription::addOptional
ParameterDescriptionBase * addOptional(U const &iLabel, T const &value)
Definition: ParameterSetDescription.h:105
CandidateFwd.h
pat::PATUserDataMerger::labels_
std::vector< std::string > labels_
Definition: PATUserDataMerger.h:94
GlobalPosition_Frontier_DevDB_cff.tag
tag
Definition: GlobalPosition_Frontier_DevDB_cff.py:11
pat::helper::AddUserFloat::addData
void addData(ObjectType &obj, const std::string &key, const value_type &val)
Definition: PATUserDataMerger.h:55
edm::ConsumesCollector::consumes
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
Definition: ConsumesCollector.h:49
ParameterSetDescription.h
getGTfromDQMFile.obj
obj
Definition: getGTfromDQMFile.py:32
pat::helper::AddUserFloat
Definition: PATUserDataMerger.h:51
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
pat::helper::AddUserPtr::value_type
edm::Ptr< UserData > value_type
Definition: PATUserDataMerger.h:60
HLT_2018_cff.InputTag
InputTag
Definition: HLT_2018_cff.py:79016
edm::ParameterSet
Definition: ParameterSet.h:36
pat::PATUserDataMerger::~PATUserDataMerger
~PATUserDataMerger()
Definition: PATUserDataMerger.h:83
Event.h
ecaldqm::binning::ObjectType
ObjectType
Definition: MESetBinningUtils.h:17
pat::helper::AddUserCand::addData
void addData(ObjectType &obj, const std::string &key, const value_type &val)
Definition: PATUserDataMerger.h:71
match
std::pair< typename Association::data_type::first_type, double > match(Reference key, Association association, bool bestMatchByMaxValue)
Generic matching function.
Definition: Utils.h:10
helper
Definition: helper.py:1
Ptr.h
iEvent
int iEvent
Definition: GenABIO.cc:224
pat::PATUserDataMerger::add
void add(ObjectType &patObject, edm::Event const &iEvent, edm::EventSetup const &iSetup)
Definition: PATUserDataMerger.h:140
edm::EventSetup
Definition: EventSetup.h:57
pat
Definition: HeavyIon.h:7
pat::helper::AddUserInt::addData
void addData(ObjectType &obj, const std::string &key, const value_type &val)
Definition: PATUserDataMerger.h:47
InputTag.h
edm::Ptr
Definition: AssociationVector.h:31
pat::helper::AddUserPtr
Definition: PATUserDataMerger.h:59
heppy_batch.val
val
Definition: heppy_batch.py:351
std
Definition: JetResolutionObject.h:76
pat::helper::AddUserPtr::product_type
edm::ValueMap< value_type > product_type
Definition: PATUserDataMerger.h:61
transform.h
pat::PATUserDataMerger::labelPostfixesToStrip_
std::vector< std::string > labelPostfixesToStrip_
Definition: PATUserDataMerger.h:94
edm::ValueMap
Definition: ValueMap.h:107
pat::PATUserDataMerger::PATUserDataMerger
PATUserDataMerger()
Definition: PATUserDataMerger.h:81
pat::PATUserDataMerger::loader_
Operation loader_
Definition: PATUserDataMerger.h:95
ConsumesCollector.h
Candidate.h
View.h
ParameterSet.h
pat::helper::AddUserFloat::value_type
float value_type
Definition: PATUserDataMerger.h:52
edm::Event
Definition: Event.h:73
crabWrapper.key
key
Definition: crabWrapper.py:19
pat::PATUserDataMerger::userDataSrcTokens_
std::vector< edm::EDGetTokenT< typename Operation::product_type > > userDataSrcTokens_
Definition: PATUserDataMerger.h:93
edm::InputTag
Definition: InputTag.h:15
pat::helper::AddUserInt
Definition: PATUserDataMerger.h:43
edm::ConsumesCollector
Definition: ConsumesCollector.h:39
pat::PATUserDataMerger::userDataSrc_
std::vector< edm::InputTag > userDataSrc_
Definition: PATUserDataMerger.h:92
PATObject.h