CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC4_patch1/src/PhysicsTools/PatAlgos/plugins/PATPhotonProducer.cc

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