CMS 3D CMS Logo

PATPhotonProducer.cc

Go to the documentation of this file.
00001 //
00002 // $Id: PATPhotonProducer.cc,v 1.15.2.1 2009/01/13 13:18:31 gpetrucc Exp $
00003 //
00004 
00005 #include "PhysicsTools/PatAlgos/plugins/PATPhotonProducer.h"
00006 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00007 #include "DataFormats/Common/interface/View.h"
00008 #include "DataFormats/HepMCCandidate/interface/GenParticleFwd.h"
00009 #include "DataFormats/HepMCCandidate/interface/GenParticle.h"
00010 #include "DataFormats/EgammaCandidates/interface/PhotonIDAssociation.h"
00011 
00012 #include <memory>
00013 
00014 using namespace pat;
00015 
00016 PATPhotonProducer::PATPhotonProducer(const edm::ParameterSet & iConfig) :
00017   isolator_(iConfig.exists("isolation") ? iConfig.getParameter<edm::ParameterSet>("isolation") : edm::ParameterSet(), false) ,
00018   userDataHelper_ ( iConfig.getParameter<edm::ParameterSet>("userData") )
00019 {
00020   // initialize the configurables
00021   photonSrc_         = iConfig.getParameter<edm::InputTag>("photonSource");
00022   embedSuperCluster_ = iConfig.getParameter<bool>         ("embedSuperCluster");
00023 
00024   // photon ID configurables
00025   addPhotonID_       = iConfig.getParameter<bool>         ("addPhotonID");
00026   photonIDSrc_       = iConfig.getParameter<edm::InputTag>("photonIDSource");
00027 
00028    // MC matching configurables
00029   addGenMatch_       = iConfig.getParameter<bool>         ( "addGenMatch" );
00030   if (addGenMatch_) {
00031       embedGenMatch_ = iConfig.getParameter<bool>         ( "embedGenMatch" );
00032       if (iConfig.existsAs<edm::InputTag>("genParticleMatch")) {
00033           genMatchSrc_.push_back(iConfig.getParameter<edm::InputTag>( "genParticleMatch" ));
00034       } else {
00035           genMatchSrc_ = iConfig.getParameter<std::vector<edm::InputTag> >( "genParticleMatch" );
00036       }
00037   }
00038   
00039   // trigger matching configurables
00040   addTrigMatch_     = iConfig.getParameter<bool>         ( "addTrigMatch" );
00041   trigMatchSrc_     = iConfig.getParameter<std::vector<edm::InputTag> >( "trigPrimMatch" );
00042 
00043   // Efficiency configurables
00044   addEfficiencies_ = iConfig.getParameter<bool>("addEfficiencies");
00045   if (addEfficiencies_) {
00046      efficiencyLoader_ = pat::helper::EfficiencyLoader(iConfig.getParameter<edm::ParameterSet>("efficiencies"));
00047   }
00048  
00049   // Check to see if the user wants to add user data
00050   useUserData_ = false;
00051   if ( iConfig.exists("userData") ) {
00052     useUserData_ = true;
00053   }
00054 
00055  
00056   // produces vector of photons
00057   produces<std::vector<Photon> >();
00058 
00059   if (iConfig.exists("isoDeposits")) {
00060      edm::ParameterSet depconf = iConfig.getParameter<edm::ParameterSet>("isoDeposits");
00061      if (depconf.exists("tracker")) isoDepositLabels_.push_back(std::make_pair(TrackerIso, depconf.getParameter<edm::InputTag>("tracker")));
00062      if (depconf.exists("ecal"))    isoDepositLabels_.push_back(std::make_pair(ECalIso, depconf.getParameter<edm::InputTag>("ecal")));
00063      if (depconf.exists("hcal"))    isoDepositLabels_.push_back(std::make_pair(HCalIso, depconf.getParameter<edm::InputTag>("hcal")));
00064      if (depconf.exists("user")) {
00065         std::vector<edm::InputTag> userdeps = depconf.getParameter<std::vector<edm::InputTag> >("user");
00066         std::vector<edm::InputTag>::const_iterator it = userdeps.begin(), ed = userdeps.end();
00067         int key = UserBaseIso;
00068         for ( ; it != ed; ++it, ++key) {
00069             isoDepositLabels_.push_back(std::make_pair(IsolationKeys(key), *it));
00070         }
00071      }
00072   }
00073 }
00074 
00075 PATPhotonProducer::~PATPhotonProducer() {
00076 }
00077 
00078 void PATPhotonProducer::produce(edm::Event & iEvent, const edm::EventSetup & iSetup) {
00079  
00080   // Get the vector of Photon's from the event
00081   edm::Handle<edm::View<PhotonType> > photons;
00082   iEvent.getByLabel(photonSrc_, photons);
00083 
00084   // prepare the MC matching
00085   std::vector<edm::Handle<edm::Association<reco::GenParticleCollection> > > genMatches(genMatchSrc_.size());
00086   if (addGenMatch_) {
00087         for (size_t j = 0, nd = genMatchSrc_.size(); j < nd; ++j) {
00088             iEvent.getByLabel(genMatchSrc_[j], genMatches[j]);
00089         }
00090   }
00091 
00092   // prepare the PhotonID
00093   edm::Handle<reco::PhotonIDAssociationCollection > photonID;
00094   if (addPhotonID_) {
00095     iEvent.getByLabel(photonIDSrc_, photonID);
00096   }
00097 
00098   if (isolator_.enabled()) isolator_.beginEvent(iEvent,iSetup);
00099 
00100   if (efficiencyLoader_.enabled()) efficiencyLoader_.newEvent(iEvent);
00101 
00102   std::vector<edm::Handle<edm::ValueMap<IsoDeposit> > > deposits(isoDepositLabels_.size());
00103   for (size_t j = 0, nd = deposits.size(); j < nd; ++j) {
00104     iEvent.getByLabel(isoDepositLabels_[j].second, deposits[j]);
00105   }
00106 
00107   // loop over photons
00108   std::vector<Photon> * PATPhotons = new std::vector<Photon>(); 
00109   for (edm::View<PhotonType>::const_iterator itPhoton = photons->begin(); itPhoton != photons->end(); itPhoton++) {
00110     // construct the Photon from the ref -> save ref to original object
00111     unsigned int idx = itPhoton - photons->begin();
00112     edm::RefToBase<PhotonType> photonRef = photons->refAt(idx);
00113     edm::Ptr<PhotonType> photonPtr = photons->ptrAt(idx);
00114     Photon aPhoton(photonRef);
00115     if (embedSuperCluster_) aPhoton.embedSuperCluster();
00116 
00117     // store the match to the generated final state muons
00118     if (addGenMatch_) {
00119       for(size_t i = 0, n = genMatches.size(); i < n; ++i) {
00120           reco::GenParticleRef genPhoton = (*genMatches[i])[photonRef];
00121           aPhoton.addGenParticleRef(genPhoton);
00122       }
00123       if (embedGenMatch_) aPhoton.embedGenParticle();
00124     }
00125     
00126     // matches to trigger primitives
00127     if ( addTrigMatch_ ) {
00128       for ( size_t i = 0; i < trigMatchSrc_.size(); ++i ) {
00129         edm::Handle<edm::Association<TriggerPrimitiveCollection> > trigMatch;
00130         iEvent.getByLabel(trigMatchSrc_[i], trigMatch);
00131         TriggerPrimitiveRef trigPrim = (*trigMatch)[photonRef];
00132         if ( trigPrim.isNonnull() && trigPrim.isAvailable() ) {
00133           aPhoton.addTriggerMatch(*trigPrim);
00134         }
00135       }
00136     }
00137 
00138     // PhotonID
00139     if (addPhotonID_) {
00140         reco::PhotonRef recoPhotonRef = photonRef.castTo<reco::PhotonRef>();
00141         if (recoPhotonRef.isNull()) throw cms::Exception("Bad Input") << "You can't read PhotonIDAssociation from something that's not a PhotonCollection";
00142         aPhoton.setPhotonID( *( (*photonID)[recoPhotonRef] ) );
00143     }
00144 
00145     if (efficiencyLoader_.enabled()) {
00146         efficiencyLoader_.setEfficiencies( aPhoton, photonRef );
00147     }
00148 
00149     // here comes the extra functionality
00150     if (isolator_.enabled()) {
00151         isolator_.fill(*photons, idx, isolatorTmpStorage_);
00152         typedef pat::helper::MultiIsolator::IsolationValuePairs IsolationValuePairs;
00153         // better to loop backwards, so the vector is resized less times
00154         for (IsolationValuePairs::const_reverse_iterator it = isolatorTmpStorage_.rbegin(), ed = isolatorTmpStorage_.rend(); it != ed; ++it) {
00155             aPhoton.setIsolation(it->first, it->second);
00156         }
00157     }
00158 
00159     for (size_t j = 0, nd = deposits.size(); j < nd; ++j) {
00160         aPhoton.setIsoDeposit(isoDepositLabels_[j].first, (*deposits[j])[photonRef]);
00161     }
00162 
00163 
00164     if ( useUserData_ ) {
00165       userDataHelper_.add( aPhoton, iEvent, iSetup );
00166     }
00167     
00168 
00169     // add the Photon to the vector of Photons
00170     PATPhotons->push_back(aPhoton);
00171   }
00172 
00173   // sort Photons in ET
00174   std::sort(PATPhotons->begin(), PATPhotons->end(), eTComparator_);
00175 
00176   // put genEvt object in Event
00177   std::auto_ptr<std::vector<Photon> > myPhotons(PATPhotons);
00178   iEvent.put(myPhotons);
00179   if (isolator_.enabled()) isolator_.endEvent();
00180 
00181 }
00182 
00183 #include "FWCore/Framework/interface/MakerMacros.h"
00184 
00185 DEFINE_FWK_MODULE(PATPhotonProducer);

Generated on Tue Jun 9 17:41:43 2009 for CMSSW by  doxygen 1.5.4