CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/PhysicsTools/PatAlgos/interface/PATUserDataHelper.h

Go to the documentation of this file.
00001 //
00002 // $Id: PATUserDataHelper.h,v 1.8 2010/02/20 21:00:13 wmtan Exp $
00003 //
00004 
00005 #ifndef PhysicsTools_PatAlgos_PATUserDataHelper_h
00006 #define PhysicsTools_PatAlgos_PATUserDataHelper_h
00007 
00030 #include "FWCore/Framework/interface/EDProducer.h"
00031 #include "FWCore/Framework/interface/Event.h"
00032 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00033 #include "FWCore/Utilities/interface/InputTag.h"
00034 #include "DataFormats/Common/interface/View.h"
00035 #include "DataFormats/Common/interface/Ptr.h"
00036 #include "DataFormats/Common/interface/Association.h"
00037 #include "DataFormats/PatCandidates/interface/PATObject.h"
00038 
00039 #include "DataFormats/PatCandidates/interface/UserData.h"
00040 #include "DataFormats/Candidate/interface/CandidateFwd.h"
00041 #include "DataFormats/Candidate/interface/Candidate.h"
00042 #include "PhysicsTools/PatAlgos/interface/PATUserDataMerger.h"
00043 #include "CommonTools/Utils/interface/StringObjectFunction.h"
00044 
00045 
00046 #include <iostream>
00047 
00048 
00049 namespace pat {
00050 
00051 
00052   template<class ObjectType>
00053   class PATUserDataHelper {
00054     
00055   public:
00056 
00057     typedef StringObjectFunction<ObjectType>                      function_type;
00058 
00059     PATUserDataHelper() {}
00060     PATUserDataHelper(const edm::ParameterSet & iConfig);
00061     ~PATUserDataHelper() {}
00062 
00063     static void fillDescription(edm::ParameterSetDescription & iDesc);
00064 
00065     // Adds information from user data to patObject,
00066     // using recoObject as the key
00067     void add(ObjectType & patObject,
00068              edm::Event const & iEvent, edm::EventSetup const & iSetup);
00069 
00070   private:
00071 
00072     // Custom user data
00073     pat::PATUserDataMerger<ObjectType, pat::helper::AddUserPtr>      userDataMerger_;
00074     // User doubles
00075     pat::PATUserDataMerger<ObjectType, pat::helper::AddUserFloat>    userFloatMerger_;
00076     // User ints
00077     pat::PATUserDataMerger<ObjectType, pat::helper::AddUserInt>      userIntMerger_;
00078     // User candidate ptrs
00079     pat::PATUserDataMerger<ObjectType, pat::helper::AddUserCand>     userCandMerger_;
00080     
00081     // Inline functions that operate on ObjectType
00082     std::vector<std::string>                                          functionNames_;
00083     std::vector<std::string>                                          functionLabels_;
00084     std::vector<function_type >                                       functions_;
00085 
00086   };
00087 
00088 // Constructor: Initilize user data src
00089 template<class ObjectType>
00090 PATUserDataHelper<ObjectType>::PATUserDataHelper(const edm::ParameterSet & iConfig) :
00091   userDataMerger_   (iConfig.getParameter<edm::ParameterSet>("userClasses")),
00092   userFloatMerger_  (iConfig.getParameter<edm::ParameterSet>("userFloats")),
00093   userIntMerger_    (iConfig.getParameter<edm::ParameterSet>("userInts")),
00094   userCandMerger_   (iConfig.getParameter<edm::ParameterSet>("userCands")),
00095   functionNames_    (iConfig.getParameter<std::vector<std::string> >("userFunctions")),
00096   functionLabels_   (iConfig.getParameter<std::vector<std::string> >("userFunctionLabels"))
00097 {
00098 
00099   // Make sure the sizes match
00100   if ( functionNames_.size() != functionLabels_.size() ) {
00101     throw cms::Exception("Size mismatch") << "userFunctions and userFunctionLabels do not have the same size, they must be the same\n";
00102   }
00103   // Loop over the function names, create a new string-parser function object 
00104   // with all of them. This operates on ObjectType
00105   std::vector<std::string>::const_iterator funcBegin = functionNames_.begin(),
00106     funcEnd = functionNames_.end(),
00107     funcIt = funcBegin;
00108   for ( ; funcIt != funcEnd; ++funcIt) {
00109     functions_.push_back(  StringObjectFunction<ObjectType>( *funcIt ) );
00110   }
00111 }
00112 
00113 
00114 /* ==================================================================================
00115      PATUserDataHelper::add 
00116             This expects four inputs:
00117                 patObject:         PATObject<ObjectType> to add to
00118                 recoObject:        The base for the value maps
00119                 iEvent:            Passed to the various data mergers
00120                 iSetup:            "                                "
00121 
00122    ==================================================================================
00123 */
00124 
00125 template<class ObjectType>
00126 void PATUserDataHelper<ObjectType>::add(ObjectType & patObject,
00127                                         edm::Event const & iEvent, 
00128                                         const edm::EventSetup & iSetup ) 
00129 {
00130 
00131   // Add "complex" user data to the PAT object
00132   userDataMerger_.add(   patObject, iEvent, iSetup );
00133   userFloatMerger_.add(  patObject, iEvent, iSetup );
00134   userIntMerger_.add(    patObject, iEvent, iSetup );
00135   userCandMerger_.add(   patObject, iEvent, iSetup );
00136 
00137   // Add "inline" user-selected functions to the PAT object
00138   typename std::vector<function_type>::const_iterator funcBegin = functions_.begin(),
00139     funcEnd = functions_.end(),
00140     funcIt = funcBegin;
00141   if ( functionLabels_.size() == functions_.size() ) {
00142     for ( ; funcIt != funcEnd; ++funcIt) {
00143       double d = (*funcIt)( patObject );
00144       patObject.addUserFloat( functionLabels_[funcIt - funcBegin], d );
00145     }
00146   }
00147 
00148   
00149 }
00150 
00151 
00152 template<class ObjectType>
00153 void PATUserDataHelper<ObjectType>::fillDescription(edm::ParameterSetDescription & iDesc)
00154 {
00155   edm::ParameterSetDescription dataMergerPSet;
00156   pat::PATUserDataMerger<ObjectType, pat::helper::AddUserPtr>::fillDescription(dataMergerPSet);
00157   iDesc.add("userClasses", dataMergerPSet);
00158   iDesc.add("userFloats", dataMergerPSet);
00159   iDesc.add("userInts", dataMergerPSet);
00160   iDesc.add("userCands", dataMergerPSet);
00161   std::vector<std::string> emptyVectorOfStrings;
00162   iDesc.add<std::vector<std::string> >("userFunctions",emptyVectorOfStrings);
00163   iDesc.add<std::vector<std::string> >("userFunctionLabels",emptyVectorOfStrings);
00164 }
00165 
00166 }
00167 #endif