CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/PhysicsTools/PatAlgos/interface/PATUserDataMerger.h

Go to the documentation of this file.
00001 //
00002 // $Id: PATUserDataMerger.h,v 1.10 2011/10/26 17:01:25 vadler Exp $
00003 //
00004 
00005 #ifndef PhysicsTools_PatAlgos_PATUserDataMerger_h
00006 #define PhysicsTools_PatAlgos_PATUserDataMerger_h
00007 
00024 #include "FWCore/Framework/interface/EDProducer.h"
00025 #include "FWCore/Framework/interface/Event.h"
00026 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00027 #include "FWCore/Utilities/interface/InputTag.h"
00028 #include "DataFormats/Common/interface/View.h"
00029 #include "DataFormats/Common/interface/Ptr.h"
00030 #include "DataFormats/Common/interface/Association.h"
00031 #include "DataFormats/Candidate/interface/CandidateFwd.h"
00032 #include "DataFormats/Candidate/interface/Candidate.h"
00033 #include "DataFormats/PatCandidates/interface/PATObject.h"
00034 
00035 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
00036 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
00037 
00038 #include <iostream>
00039 
00040 
00041 namespace pat {
00042   namespace helper {
00043     struct AddUserInt {
00044         typedef int                       value_type;
00045         typedef edm::ValueMap<value_type> product_type;
00046         template<typename ObjectType>
00047         void addData(ObjectType &obj, const std::string & key, const value_type &val) { obj.addUserInt(key, val); }
00048     };
00049     struct AddUserFloat {
00050         typedef float                     value_type;
00051         typedef edm::ValueMap<value_type> product_type;
00052         template<typename ObjectType>
00053         void addData(ObjectType &obj, const std::string & key, const value_type &val) { obj.addUserFloat(key, val); }
00054     };
00055     struct AddUserPtr {
00056         typedef edm::Ptr<UserData>        value_type;
00057         typedef edm::ValueMap<value_type> product_type;
00058         template<typename ObjectType>
00059         void addData(ObjectType &obj, const std::string & key, const value_type &val) {
00060               obj.addUserDataFromPtr(key, val);
00061         }
00062     };
00063     struct AddUserCand {
00064         typedef reco::CandidatePtr        value_type;
00065         typedef edm::ValueMap<value_type> product_type;
00066         template<typename ObjectType>
00067         void addData(ObjectType &obj, const std::string & key, const value_type &val) { obj.addUserCand(key, val); }
00068     };
00069 
00070   }
00071 
00072   template<typename ObjectType, typename Operation>
00073   class PATUserDataMerger {
00074 
00075   public:
00076 
00077     PATUserDataMerger() {}
00078     PATUserDataMerger(const edm::ParameterSet & iConfig);
00079     ~PATUserDataMerger() {}
00080 
00081     static void fillDescription(edm::ParameterSetDescription & iDesc);
00082 
00083     // Method to call from PATUserDataHelper to add information to the PATObject in question.
00084     void add(ObjectType & patObject,
00085              edm::Event const & iEvent, edm::EventSetup const & iSetup);
00086 
00087   private:
00088 
00089     // configurables
00090     std::vector<edm::InputTag>  userDataSrc_;   // ValueMap containing the user data
00091     Operation                   loader_;
00092 
00093   };
00094 
00095 }
00096 
00097 // Constructor: Initilize user data src
00098 template<typename ObjectType, typename Operation>
00099 pat::PATUserDataMerger<ObjectType, Operation>::PATUserDataMerger(const edm::ParameterSet & iConfig) :
00100   userDataSrc_(iConfig.getParameter<std::vector<edm::InputTag> >("src") )
00101 {
00102 }
00103 
00104 
00105 /* ==================================================================================
00106      PATUserDataMerger::add
00107             This expects four inputs:
00108                 patObject:         ObjectType to add to
00109 
00110                 from Event:
00111                 userDataSrc:       The data to add, which is a ValueMap keyed by recoObject
00112 
00113                 from Setup:
00114                 none currently
00115 
00116                 This will simply add the UserData *'s from the value map that are
00117                 indexed by the reco objects, to the pat object's user data vector.
00118    ==================================================================================
00119 */
00120 
00121 template<class ObjectType, typename Operation>
00122 void
00123 pat::PATUserDataMerger<ObjectType, Operation>::add(ObjectType & patObject,
00124                                                    edm::Event const & iEvent,
00125                                                    const edm::EventSetup & iSetup )
00126 {
00127 
00128   std::vector<edm::InputTag>::const_iterator input_it = userDataSrc_.begin(),
00129 //     input_begin = userDataSrc_.begin(), // warning from gcc461: variable 'input_begin' set but not used [-Wunused-but-set-variable]
00130     input_end = userDataSrc_.end();
00131 
00132   for ( ; input_it != input_end; ++input_it ) {
00133 
00134     // Declare the object handles:
00135     // ValueMap containing the values, or edm::Ptr's to the UserData that
00136     //   is associated to those PAT Objects
00137     edm::Handle<typename Operation::product_type> userData;
00138 
00139     // Get the objects by label
00140     if ( input_it->encode().size() == 0 ) continue;
00141     iEvent.getByLabel( *input_it, userData );
00142 
00143     edm::Ptr<reco::Candidate> recoObject = patObject.originalObjectRef();
00144     if ( userData->contains( recoObject.id() ) ) {
00145       loader_.addData( patObject, input_it->encode(), (*userData)[recoObject]);
00146     }
00147 
00148   }
00149 
00150 }
00151 
00152 template<class ObjectType, typename Operation>
00153 void
00154 pat::PATUserDataMerger<ObjectType, Operation>::fillDescription(edm::ParameterSetDescription & iDesc)
00155 {
00156   iDesc.add<std::vector<edm::InputTag> >("src");
00157 }
00158 
00159 #endif