File indexing completed on 2023-03-17 11:16:11
0001
0002
0003
0004 #ifndef PhysicsTools_PatAlgos_PATUserDataHelper_h
0005 #define PhysicsTools_PatAlgos_PATUserDataHelper_h
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028 #include "FWCore/Framework/interface/Event.h"
0029 #include "FWCore/Framework/interface/ConsumesCollector.h"
0030 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0031 #include "FWCore/Utilities/interface/InputTag.h"
0032 #include "DataFormats/Common/interface/View.h"
0033 #include "DataFormats/Common/interface/Ptr.h"
0034 #include "DataFormats/Common/interface/Association.h"
0035 #include "DataFormats/PatCandidates/interface/PATObject.h"
0036
0037 #include "DataFormats/PatCandidates/interface/UserData.h"
0038 #include "DataFormats/Candidate/interface/CandidateFwd.h"
0039 #include "DataFormats/Candidate/interface/Candidate.h"
0040 #include "PhysicsTools/PatAlgos/interface/PATUserDataMerger.h"
0041 #include "CommonTools/Utils/interface/StringObjectFunction.h"
0042
0043 #include <iostream>
0044
0045 namespace pat {
0046
0047 template <class ObjectType>
0048 class PATUserDataHelper {
0049 public:
0050 typedef StringObjectFunction<ObjectType> function_type;
0051
0052 PATUserDataHelper() {}
0053 PATUserDataHelper(const edm::ParameterSet& iConfig, edm::ConsumesCollector&& iC);
0054 ~PATUserDataHelper() {}
0055
0056 static void fillDescription(edm::ParameterSetDescription& iDesc);
0057
0058
0059
0060 void add(ObjectType& patObject, edm::Event const& iEvent, edm::EventSetup const& iSetup);
0061
0062 private:
0063
0064 pat::PATUserDataMerger<ObjectType, pat::helper::AddUserPtr> userDataMerger_;
0065
0066 pat::PATUserDataMerger<ObjectType, pat::helper::AddUserFloat> userFloatMerger_;
0067
0068 pat::PATUserDataMerger<ObjectType, pat::helper::AddUserInt> userIntMerger_;
0069
0070 pat::PATUserDataMerger<ObjectType, pat::helper::AddUserCand> userCandMerger_;
0071
0072
0073 std::vector<std::string> functionNames_;
0074 std::vector<std::string> functionLabels_;
0075 std::vector<function_type> functions_;
0076 };
0077
0078
0079 template <class ObjectType>
0080 PATUserDataHelper<ObjectType>::PATUserDataHelper(const edm::ParameterSet& iConfig, edm::ConsumesCollector&& iC)
0081 : userDataMerger_(iConfig.getParameter<edm::ParameterSet>("userClasses"), iC),
0082 userFloatMerger_(iConfig.getParameter<edm::ParameterSet>("userFloats"), iC),
0083 userIntMerger_(iConfig.getParameter<edm::ParameterSet>("userInts"), iC),
0084 userCandMerger_(iConfig.getParameter<edm::ParameterSet>("userCands"), iC),
0085 functionNames_(iConfig.getParameter<std::vector<std::string> >("userFunctions")),
0086 functionLabels_(iConfig.getParameter<std::vector<std::string> >("userFunctionLabels")) {
0087
0088 if (functionNames_.size() != functionLabels_.size()) {
0089 throw cms::Exception("Size mismatch")
0090 << "userFunctions and userFunctionLabels do not have the same size, they must be the same\n";
0091 }
0092
0093
0094 std::vector<std::string>::const_iterator funcBegin = functionNames_.begin(), funcEnd = functionNames_.end(),
0095 funcIt = funcBegin;
0096 for (; funcIt != funcEnd; ++funcIt) {
0097 functions_.push_back(StringObjectFunction<ObjectType>(*funcIt));
0098 }
0099 }
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112 template <class ObjectType>
0113 void PATUserDataHelper<ObjectType>::add(ObjectType& patObject,
0114 edm::Event const& iEvent,
0115 const edm::EventSetup& iSetup) {
0116
0117 userDataMerger_.add(patObject, iEvent, iSetup);
0118 userFloatMerger_.add(patObject, iEvent, iSetup);
0119 userIntMerger_.add(patObject, iEvent, iSetup);
0120 userCandMerger_.add(patObject, iEvent, iSetup);
0121
0122
0123 typename std::vector<function_type>::const_iterator funcBegin = functions_.begin(), funcEnd = functions_.end(),
0124 funcIt = funcBegin;
0125 if (functionLabels_.size() == functions_.size()) {
0126 for (; funcIt != funcEnd; ++funcIt) {
0127 double d = (*funcIt)(patObject);
0128 patObject.addUserFloat(functionLabels_[funcIt - funcBegin], d);
0129 }
0130 }
0131 }
0132
0133 template <class ObjectType>
0134 void PATUserDataHelper<ObjectType>::fillDescription(edm::ParameterSetDescription& iDesc) {
0135 edm::ParameterSetDescription dataMergerPSet;
0136 pat::PATUserDataMerger<ObjectType, pat::helper::AddUserPtr>::fillDescription(dataMergerPSet);
0137 iDesc.add("userClasses", dataMergerPSet);
0138 iDesc.add("userFloats", dataMergerPSet);
0139 iDesc.add("userInts", dataMergerPSet);
0140 iDesc.add("userCands", dataMergerPSet);
0141 std::vector<std::string> emptyVectorOfStrings;
0142 iDesc.add<std::vector<std::string> >("userFunctions", emptyVectorOfStrings);
0143 iDesc.add<std::vector<std::string> >("userFunctionLabels", emptyVectorOfStrings);
0144 }
0145
0146 }
0147 #endif