CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/PhysicsTools/PatAlgos/plugins/PATPhotonProducer.cc

Go to the documentation of this file.
00001 //
00002 // $Id: PATPhotonProducer.cc,v 1.28 2010/09/03 15:41:27 hegner 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 
00011 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
00012 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
00013 
00014 #include <memory>
00015 
00016 using namespace pat;
00017 
00018 PATPhotonProducer::PATPhotonProducer(const edm::ParameterSet & iConfig) :
00019   isolator_(iConfig.exists("userIsolation") ? iConfig.getParameter<edm::ParameterSet>("userIsolation") : edm::ParameterSet(), false) ,
00020   useUserData_(iConfig.exists("userData"))
00021 {
00022   // initialize the configurables
00023   photonSrc_         = iConfig.getParameter<edm::InputTag>("photonSource");
00024   embedSuperCluster_ = iConfig.getParameter<bool>         ("embedSuperCluster");
00025 
00026    // MC matching configurables
00027   addGenMatch_       = iConfig.getParameter<bool>         ( "addGenMatch" );
00028   if (addGenMatch_) {
00029       embedGenMatch_ = iConfig.getParameter<bool>         ( "embedGenMatch" );
00030       if (iConfig.existsAs<edm::InputTag>("genParticleMatch")) {
00031           genMatchSrc_.push_back(iConfig.getParameter<edm::InputTag>( "genParticleMatch" ));
00032       } else {
00033           genMatchSrc_ = iConfig.getParameter<std::vector<edm::InputTag> >( "genParticleMatch" );
00034       }
00035   }
00036 
00037   // Efficiency configurables
00038   addEfficiencies_ = iConfig.getParameter<bool>("addEfficiencies");
00039   if (addEfficiencies_) {
00040      efficiencyLoader_ = pat::helper::EfficiencyLoader(iConfig.getParameter<edm::ParameterSet>("efficiencies"));
00041   }
00042  
00043   // photon ID configurables
00044   addPhotonID_        = iConfig.getParameter<bool>         ( "addPhotonID" );
00045   if (addPhotonID_) {
00046       // it might be a single photon ID
00047       if (iConfig.existsAs<edm::InputTag>("photonIDSource")) {
00048           photIDSrcs_.push_back(NameTag("", iConfig.getParameter<edm::InputTag>("photonIDSource")));
00049       }
00050       // or there might be many of them
00051       if (iConfig.existsAs<edm::ParameterSet>("photonIDSources")) {
00052           // please don't configure me twice
00053           if (!photIDSrcs_.empty()) throw cms::Exception("Configuration") << 
00054                 "PATPhotonProducer: you can't specify both 'photonIDSource' and 'photonIDSources'\n";
00055           // read the different photon ID names
00056           edm::ParameterSet idps = iConfig.getParameter<edm::ParameterSet>("photonIDSources");
00057           std::vector<std::string> names = idps.getParameterNamesForType<edm::InputTag>();
00058           for (std::vector<std::string>::const_iterator it = names.begin(), ed = names.end(); it != ed; ++it) {
00059               photIDSrcs_.push_back(NameTag(*it, idps.getParameter<edm::InputTag>(*it)));
00060           }
00061       }
00062       // but in any case at least once
00063       if (photIDSrcs_.empty()) throw cms::Exception("Configuration") <<
00064             "PATPhotonProducer: id addPhotonID is true, you must specify either:\n" <<
00065             "\tInputTag photonIDSource = <someTag>\n" << "or\n" <<
00066             "\tPSet photonIDSources = { \n" <<
00067             "\t\tInputTag <someName> = <someTag>   // as many as you want \n " <<
00068             "\t}\n";
00069   }
00070   
00071 
00072 
00073   // Resolution configurables
00074   addResolutions_ = iConfig.getParameter<bool>("addResolutions");
00075   if (addResolutions_) {
00076      resolutionLoader_ = pat::helper::KinResolutionsLoader(iConfig.getParameter<edm::ParameterSet>("resolutions"));
00077   }
00078  
00079   // Check to see if the user wants to add user data
00080   if ( useUserData_ ) {
00081     userDataHelper_ = PATUserDataHelper<Photon>(iConfig.getParameter<edm::ParameterSet>("userData"));
00082   }
00083 
00084  
00085   // produces vector of photons
00086   produces<std::vector<Photon> >();
00087 
00088   if (iConfig.exists("isoDeposits")) {
00089      edm::ParameterSet depconf = iConfig.getParameter<edm::ParameterSet>("isoDeposits");
00090      if (depconf.exists("tracker")) isoDepositLabels_.push_back(std::make_pair(pat::TrackIso, depconf.getParameter<edm::InputTag>("tracker")));
00091      if (depconf.exists("ecal"))    isoDepositLabels_.push_back(std::make_pair(pat::EcalIso, depconf.getParameter<edm::InputTag>("ecal")));
00092      if (depconf.exists("hcal"))    isoDepositLabels_.push_back(std::make_pair(pat::HcalIso, depconf.getParameter<edm::InputTag>("hcal")));
00093      if (depconf.exists("user")) {
00094         std::vector<edm::InputTag> userdeps = depconf.getParameter<std::vector<edm::InputTag> >("user");
00095         std::vector<edm::InputTag>::const_iterator it = userdeps.begin(), ed = userdeps.end();
00096         int key = UserBaseIso;
00097         for ( ; it != ed; ++it, ++key) {
00098             isoDepositLabels_.push_back(std::make_pair(IsolationKeys(key), *it));
00099         }
00100      }
00101   }
00102 }
00103 
00104 PATPhotonProducer::~PATPhotonProducer() {
00105 }
00106 
00107 void PATPhotonProducer::produce(edm::Event & iEvent, const edm::EventSetup & iSetup) {
00108  
00109   // Get the vector of Photon's from the event
00110   edm::Handle<edm::View<reco::Photon> > photons;
00111   iEvent.getByLabel(photonSrc_, photons);
00112 
00113   if (iEvent.isRealData()){
00114     addGenMatch_ = false;
00115     embedGenMatch_ = false;
00116   }
00117 
00118   // prepare the MC matching
00119   std::vector<edm::Handle<edm::Association<reco::GenParticleCollection> > > genMatches(genMatchSrc_.size());
00120   if (addGenMatch_) {
00121         for (size_t j = 0, nd = genMatchSrc_.size(); j < nd; ++j) {
00122             iEvent.getByLabel(genMatchSrc_[j], genMatches[j]);
00123         }
00124   }
00125 
00126   if (isolator_.enabled()) isolator_.beginEvent(iEvent,iSetup);
00127   
00128   if (efficiencyLoader_.enabled()) efficiencyLoader_.newEvent(iEvent);
00129   if (resolutionLoader_.enabled()) resolutionLoader_.newEvent(iEvent, iSetup);
00130   
00131   std::vector<edm::Handle<edm::ValueMap<IsoDeposit> > > deposits(isoDepositLabels_.size());
00132   for (size_t j = 0, nd = deposits.size(); j < nd; ++j) {
00133     iEvent.getByLabel(isoDepositLabels_[j].second, deposits[j]);
00134   }
00135   
00136   // prepare ID extraction 
00137   std::vector<edm::Handle<edm::ValueMap<Bool_t> > > idhandles;
00138   std::vector<pat::Photon::IdPair>               ids;
00139   if (addPhotonID_) {
00140     idhandles.resize(photIDSrcs_.size());
00141     ids.resize(photIDSrcs_.size());
00142     for (size_t i = 0; i < photIDSrcs_.size(); ++i) {
00143       iEvent.getByLabel(photIDSrcs_[i].second, idhandles[i]);
00144       ids[i].first = photIDSrcs_[i].first;
00145     }
00146   }
00147 
00148   // loop over photons
00149   std::vector<Photon> * PATPhotons = new std::vector<Photon>(); 
00150   for (edm::View<reco::Photon>::const_iterator itPhoton = photons->begin(); itPhoton != photons->end(); itPhoton++) {
00151     // construct the Photon from the ref -> save ref to original object
00152     unsigned int idx = itPhoton - photons->begin();
00153     edm::RefToBase<reco::Photon> photonRef = photons->refAt(idx);
00154     edm::Ptr<reco::Photon> photonPtr = photons->ptrAt(idx);
00155     Photon aPhoton(photonRef);
00156     if (embedSuperCluster_) aPhoton.embedSuperCluster();
00157 
00158     // store the match to the generated final state muons
00159     if (addGenMatch_) {
00160       for(size_t i = 0, n = genMatches.size(); i < n; ++i) {
00161           reco::GenParticleRef genPhoton = (*genMatches[i])[photonRef];
00162           aPhoton.addGenParticleRef(genPhoton);
00163       }
00164       if (embedGenMatch_) aPhoton.embedGenParticle();
00165     }
00166 
00167     if (efficiencyLoader_.enabled()) {
00168         efficiencyLoader_.setEfficiencies( aPhoton, photonRef );
00169     }
00170 
00171     if (resolutionLoader_.enabled()) {
00172         resolutionLoader_.setResolutions(aPhoton);
00173     }
00174 
00175     // here comes the extra functionality
00176     if (isolator_.enabled()) {
00177         isolator_.fill(*photons, idx, isolatorTmpStorage_);
00178         typedef pat::helper::MultiIsolator::IsolationValuePairs IsolationValuePairs;
00179         // better to loop backwards, so the vector is resized less times
00180         for (IsolationValuePairs::const_reverse_iterator it = isolatorTmpStorage_.rbegin(), ed = isolatorTmpStorage_.rend(); it != ed; ++it) {
00181             aPhoton.setIsolation(it->first, it->second);
00182         }
00183     }
00184 
00185     for (size_t j = 0, nd = deposits.size(); j < nd; ++j) {
00186         aPhoton.setIsoDeposit(isoDepositLabels_[j].first, (*deposits[j])[photonRef]);
00187     }
00188 
00189 
00190     // add photon ID info
00191     if (addPhotonID_) {
00192       for (size_t i = 0; i < photIDSrcs_.size(); ++i) {
00193         ids[i].second = (*idhandles[i])[photonRef];    
00194       }
00195       aPhoton.setPhotonIDs(ids);
00196     }
00197 
00198     if ( useUserData_ ) {
00199       userDataHelper_.add( aPhoton, iEvent, iSetup );
00200     }
00201     
00202 
00203     // add the Photon to the vector of Photons
00204     PATPhotons->push_back(aPhoton);
00205   }
00206 
00207   // sort Photons in ET
00208   std::sort(PATPhotons->begin(), PATPhotons->end(), eTComparator_);
00209 
00210   // put genEvt object in Event
00211   std::auto_ptr<std::vector<Photon> > myPhotons(PATPhotons);
00212   iEvent.put(myPhotons);
00213   if (isolator_.enabled()) isolator_.endEvent();
00214 
00215 }
00216 
00217 // ParameterSet description for module
00218 void PATPhotonProducer::fillDescriptions(edm::ConfigurationDescriptions & descriptions)
00219 {
00220   edm::ParameterSetDescription iDesc;
00221   iDesc.setComment("PAT photon producer module");
00222 
00223   // input source 
00224   iDesc.add<edm::InputTag>("photonSource", edm::InputTag("no default"))->setComment("input collection");
00225 
00226   iDesc.add<bool>("embedSuperCluster", true)->setComment("embed external super cluster");
00227 
00228   // MC matching configurables
00229   iDesc.add<bool>("addGenMatch", true)->setComment("add MC matching");
00230   iDesc.add<bool>("embedGenMatch", false)->setComment("embed MC matched MC information");
00231   std::vector<edm::InputTag> emptySourceVector;
00232   iDesc.addNode( edm::ParameterDescription<edm::InputTag>("genParticleMatch", edm::InputTag(), true) xor 
00233                  edm::ParameterDescription<std::vector<edm::InputTag> >("genParticleMatch", emptySourceVector, true)
00234                )->setComment("input with MC match information");
00235 
00236   pat::helper::KinResolutionsLoader::fillDescription(iDesc);
00237 
00238   // photon ID configurables
00239   iDesc.add<bool>("addPhotonID",true)->setComment("add photon ID variables");
00240   edm::ParameterSetDescription photonIDSourcesPSet;
00241   photonIDSourcesPSet.setAllowAnything(); 
00242   iDesc.addNode( edm::ParameterDescription<edm::InputTag>("photonIDSource", edm::InputTag(), true) xor
00243                  edm::ParameterDescription<edm::ParameterSetDescription>("photonIDSources", photonIDSourcesPSet, true)
00244                  )->setComment("input with photon ID variables");
00245 
00246   // IsoDeposit configurables
00247   edm::ParameterSetDescription isoDepositsPSet;
00248   isoDepositsPSet.addOptional<edm::InputTag>("tracker"); 
00249   isoDepositsPSet.addOptional<edm::InputTag>("ecal");
00250   isoDepositsPSet.addOptional<edm::InputTag>("hcal");
00251   isoDepositsPSet.addOptional<std::vector<edm::InputTag> >("user");
00252   iDesc.addOptional("isoDeposits", isoDepositsPSet);
00253 
00254   // Efficiency configurables
00255   edm::ParameterSetDescription efficienciesPSet;
00256   efficienciesPSet.setAllowAnything(); // TODO: the pat helper needs to implement a description.
00257   iDesc.add("efficiencies", efficienciesPSet);
00258   iDesc.add<bool>("addEfficiencies", false);
00259 
00260   // Check to see if the user wants to add user data
00261   edm::ParameterSetDescription userDataPSet;
00262   PATUserDataHelper<Photon>::fillDescription(userDataPSet);
00263   iDesc.addOptional("userData", userDataPSet);
00264 
00265   edm::ParameterSetDescription isolationPSet;
00266   isolationPSet.setAllowAnything(); // TODO: the pat helper needs to implement a description.
00267   iDesc.add("userIsolation", isolationPSet);
00268 
00269   descriptions.add("PATPhotonProducer", iDesc);
00270 
00271 }
00272 
00273 #include "FWCore/Framework/interface/MakerMacros.h"
00274 
00275 DEFINE_FWK_MODULE(PATPhotonProducer);