Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-10-11 04:34:05

0001 //
0002 //
0003 
0004 #ifndef PhysicsTools_PatAlgos_PATUserDataHelper_h
0005 #define PhysicsTools_PatAlgos_PATUserDataHelper_h
0006 
0007 /**
0008   \class    pat::PATUserDataHelper PATUserDataHelper.h "PhysicsTools/PatAlgos/interface/PATUserDataHelper.h"
0009   \brief    Assists in assimilating all pat::UserData into pat objects.
0010 
0011 
0012             This will pull the following from the event stream (if they exist) and put them into the
0013         object in question, all indexed by the reco objects that make up the pat objects in question:
0014 
0015         * ValueMap<double>
0016         * ValueMap<int>
0017         * ValueMap<Ptr<UserData> >
0018         * ValueMap<CandidatePtr>
0019 
0020         This is accomplished by using PATUserDataMergers.
0021 
0022         This also can add "in situ" string-parser-based methods directly.
0023 
0024   \author   Salvatore Rappoccio
0025   \version  $Id: PATUserDataHelper.h,v 1.8 2010/02/20 21:00:13 wmtan Exp $
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     // Adds information from user data to patObject,
0059     // using recoObject as the key
0060     void add(ObjectType& patObject, edm::Event const& iEvent, edm::EventSetup const& iSetup);
0061 
0062   private:
0063     // Custom user data
0064     pat::PATUserDataMerger<ObjectType, pat::helper::AddUserPtr> userDataMerger_;
0065     // User doubles
0066     pat::PATUserDataMerger<ObjectType, pat::helper::AddUserFloat> userFloatMerger_;
0067     // User ints
0068     pat::PATUserDataMerger<ObjectType, pat::helper::AddUserInt> userIntMerger_;
0069     // User candidate ptrs
0070     pat::PATUserDataMerger<ObjectType, pat::helper::AddUserCand> userCandMerger_;
0071 
0072     // Inline functions that operate on ObjectType
0073     std::vector<std::string> functionNames_;
0074     std::vector<std::string> functionLabels_;
0075     std::vector<function_type> functions_;
0076   };
0077 
0078   // Constructor: Initilize user data src
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     // Make sure the sizes match
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     // Loop over the function names, create a new string-parser function object
0093     // with all of them. This operates on ObjectType
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      PATUserDataHelper::add
0103             This expects four inputs:
0104             patObject:         PATObject<ObjectType> to add to
0105         recoObject:        The base for the value maps
0106         iEvent:            Passed to the various data mergers
0107         iSetup:            "                                "
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     // Add "complex" user data to the PAT object
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     // Add "inline" user-selected functions to the PAT object
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 }  // namespace pat
0147 #endif