CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_7/src/PhysicsTools/PatAlgos/plugins/PATMETProducer.cc

Go to the documentation of this file.
00001 //
00002 // $Id: PATMETProducer.cc,v 1.14 2009/06/25 23:49:35 gpetrucc Exp $
00003 //
00004 
00005 #include "PhysicsTools/PatAlgos/plugins/PATMETProducer.h"
00006 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00007 #include "FWCore/ParameterSet/interface/FileInPath.h"
00008 #include "DataFormats/Common/interface/View.h"
00009 
00010 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
00011 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
00012 
00013 #include <memory>
00014 
00015 
00016 using namespace pat;
00017 
00018 
00019 PATMETProducer::PATMETProducer(const edm::ParameterSet & iConfig):
00020   useUserData_(iConfig.exists("userData"))
00021 {
00022   // initialize the configurables
00023   metSrc_         = iConfig.getParameter<edm::InputTag>("metSource");
00024   addGenMET_      = iConfig.getParameter<bool>         ("addGenMET");
00025   genMETSrc_      = iConfig.getParameter<edm::InputTag>("genMETSource");
00026   addResolutions_ = iConfig.getParameter<bool>         ("addResolutions");
00027 
00028   // Efficiency configurables
00029   addEfficiencies_ = iConfig.getParameter<bool>("addEfficiencies");
00030   if (addEfficiencies_) {
00031      efficiencyLoader_ = pat::helper::EfficiencyLoader(iConfig.getParameter<edm::ParameterSet>("efficiencies"));
00032   }
00033 
00034   // Resolution configurables
00035   addResolutions_ = iConfig.getParameter<bool>("addResolutions");
00036   if (addResolutions_) {
00037      resolutionLoader_ = pat::helper::KinResolutionsLoader(iConfig.getParameter<edm::ParameterSet>("resolutions"));
00038   }
00039 
00040   // Check to see if the user wants to add user data
00041   if ( useUserData_ ) {
00042     userDataHelper_ = PATUserDataHelper<MET>(iConfig.getParameter<edm::ParameterSet>("userData"));
00043   }
00044 
00045   
00046   // produces vector of mets
00047   produces<std::vector<MET> >();
00048 }
00049 
00050 
00051 PATMETProducer::~PATMETProducer() {
00052 }
00053 
00054 
00055 void PATMETProducer::produce(edm::Event & iEvent, const edm::EventSetup & iSetup) {
00056  
00057   // Get the vector of MET's from the event
00058   edm::Handle<edm::View<reco::MET> > mets;
00059   iEvent.getByLabel(metSrc_, mets);
00060 
00061   if (mets->size() != 1) throw cms::Exception("Corrupt Data") << "The input MET collection " << metSrc_.encode() << " has size " << mets->size() << " instead of 1 as it should.\n";
00062   if (efficiencyLoader_.enabled()) efficiencyLoader_.newEvent(iEvent);
00063   if (resolutionLoader_.enabled()) resolutionLoader_.newEvent(iEvent, iSetup);
00064 
00065   // Get the vector of generated met from the event if needed
00066   edm::Handle<edm::View<reco::GenMET> > genMETs;
00067   if (addGenMET_) {
00068     iEvent.getByLabel(genMETSrc_, genMETs);
00069   }
00070 
00071   // loop over mets
00072   std::vector<MET> * patMETs = new std::vector<MET>(); 
00073   for (edm::View<reco::MET>::const_iterator itMET = mets->begin(); itMET != mets->end(); itMET++) {
00074     // construct the MET from the ref -> save ref to original object
00075     unsigned int idx = itMET - mets->begin();
00076     edm::RefToBase<reco::MET> metsRef = mets->refAt(idx);
00077     edm::Ptr<reco::MET> metsPtr = mets->ptrAt(idx);
00078     MET amet(metsRef);
00079     // add the generated MET
00080     if (addGenMET_) amet.setGenMET((*genMETs)[idx]);
00081 
00082     if (efficiencyLoader_.enabled()) {
00083         efficiencyLoader_.setEfficiencies( amet, metsRef );
00084     }
00085 
00086     if (resolutionLoader_.enabled()) {
00087         resolutionLoader_.setResolutions(amet);
00088     }
00089 
00090 
00091     if ( useUserData_ ) {
00092       userDataHelper_.add( amet, iEvent, iSetup );
00093     }
00094     
00095 
00096     // correct for muons if demanded... never more: it's now done by JetMETCorrections
00097     // add the MET to the vector of METs
00098     patMETs->push_back(amet);
00099   }
00100 
00101   // sort MET in ET .. don't mess with this
00102   //  std::sort(patMETs->begin(), patMETs->end(), eTComparator_);
00103 
00104   // put genEvt object in Event
00105   std::auto_ptr<std::vector<MET> > myMETs(patMETs);
00106   iEvent.put(myMETs);
00107 
00108 }
00109 
00110 // ParameterSet description for module
00111 void PATMETProducer::fillDescriptions(edm::ConfigurationDescriptions & descriptions)
00112 {
00113   edm::ParameterSetDescription iDesc;
00114   iDesc.setComment("PAT MET producer module");
00115 
00116   // input source 
00117   iDesc.add<edm::InputTag>("metSource", edm::InputTag("no default"))->setComment("input collection");
00118 
00119   // MC configurations
00120   iDesc.add<bool>("addGenMET", false);
00121   iDesc.add<edm::InputTag>("genMETSource", edm::InputTag("genMetCalo"));
00122 
00123   pat::helper::KinResolutionsLoader::fillDescription(iDesc);
00124 
00125   // Efficiency configurables
00126   edm::ParameterSetDescription efficienciesPSet;
00127   efficienciesPSet.setAllowAnything(); // TODO: the pat helper needs to implement a description.
00128   iDesc.add("efficiencies", efficienciesPSet);
00129   iDesc.add<bool>("addEfficiencies", false);
00130 
00131   // Check to see if the user wants to add user data
00132   edm::ParameterSetDescription userDataPSet;
00133   PATUserDataHelper<MET>::fillDescription(userDataPSet);
00134   iDesc.addOptional("userData", userDataPSet);
00135 
00136   // muon correction
00137   iDesc.add<bool>("addMuonCorrections", false);
00138   iDesc.add<edm::InputTag>("muonSource", edm::InputTag("muons"));
00139 
00140 }
00141 
00142 #include "FWCore/Framework/interface/MakerMacros.h"
00143 
00144 DEFINE_FWK_MODULE(PATMETProducer);