CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/src/PhysicsTools/PatAlgos/plugins/PATGenericParticleProducer.cc

Go to the documentation of this file.
00001 //
00002 // $Id: PATGenericParticleProducer.cc,v 1.12 2009/10/15 17:17:26 rwolf Exp $
00003 //
00004 
00005 #include "PhysicsTools/PatAlgos/plugins/PATGenericParticleProducer.h"
00006 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00007 #include "DataFormats/Common/interface/View.h"
00008 #include <memory>
00009 
00010 using namespace pat;
00011 
00012 PATGenericParticleProducer::PATGenericParticleProducer(const edm::ParameterSet & iConfig) :
00013   isolator_(iConfig.exists("userIsolation") ? iConfig.getParameter<edm::ParameterSet>("userIsolation") : edm::ParameterSet(), false),
00014   userDataHelper_ ( iConfig.getParameter<edm::ParameterSet>("userData") )
00015 {
00016   // initialize the configurables
00017   src_ = iConfig.getParameter<edm::InputTag>( "src" );
00018 
00019   // RECO embedding
00020   embedTrack_        = iConfig.getParameter<bool>( "embedTrack" );
00021   embedGsfTrack_     = iConfig.getParameter<bool>( "embedGsfTrack" );
00022   embedStandalone_   = iConfig.getParameter<bool>( "embedStandAloneMuon" );
00023   embedCombined_     = iConfig.getParameter<bool>( "embedCombinedMuon" );
00024   embedSuperCluster_ = iConfig.getParameter<bool>( "embedSuperCluster" );
00025   embedTracks_       = iConfig.getParameter<bool>( "embedMultipleTracks" );
00026   embedCaloTower_    = iConfig.getParameter<bool>( "embedCaloTower" );
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   // quality
00040   addQuality_ = iConfig.getParameter<bool>("addQuality");
00041   qualitySrc_ = iConfig.getParameter<edm::InputTag>("qualitySource");
00042 
00043   // produces vector of particles
00044   produces<std::vector<GenericParticle> >();
00045 
00046   if (iConfig.exists("isoDeposits")) {
00047      edm::ParameterSet depconf = iConfig.getParameter<edm::ParameterSet>("isoDeposits");
00048      if (depconf.exists("tracker")) isoDepositLabels_.push_back(std::make_pair(pat::TrackIso, depconf.getParameter<edm::InputTag>("tracker")));
00049      if (depconf.exists("ecal"))    isoDepositLabels_.push_back(std::make_pair(pat::EcalIso, depconf.getParameter<edm::InputTag>("ecal")));
00050      if (depconf.exists("hcal"))    isoDepositLabels_.push_back(std::make_pair(pat::HcalIso, depconf.getParameter<edm::InputTag>("hcal")));
00051      if (depconf.exists("user")) {
00052         std::vector<edm::InputTag> userdeps = depconf.getParameter<std::vector<edm::InputTag> >("user");
00053         std::vector<edm::InputTag>::const_iterator it = userdeps.begin(), ed = userdeps.end();
00054         int key = UserBaseIso;
00055         for ( ; it != ed; ++it, ++key) {
00056             isoDepositLabels_.push_back(std::make_pair(IsolationKeys(key), *it));
00057         }
00058      }
00059   }
00060 
00061   // Efficiency configurables
00062   addEfficiencies_ = iConfig.getParameter<bool>("addEfficiencies");
00063   if (addEfficiencies_) {
00064      efficiencyLoader_ = pat::helper::EfficiencyLoader(iConfig.getParameter<edm::ParameterSet>("efficiencies"));
00065   }
00066 
00067   // Resolution configurables
00068   addResolutions_ = iConfig.getParameter<bool>("addResolutions");
00069   if (addResolutions_) {
00070      resolutionLoader_ = pat::helper::KinResolutionsLoader(iConfig.getParameter<edm::ParameterSet>("resolutions"));
00071   }
00072 
00073   if (iConfig.exists("vertexing")) {
00074      vertexingHelper_ = pat::helper::VertexingHelper(iConfig.getParameter<edm::ParameterSet>("vertexing")); 
00075   }
00076 
00077   // Check to see if the user wants to add user data
00078   useUserData_ = false;
00079   if ( iConfig.exists("userData") ) {
00080     useUserData_ = true;
00081   }
00082 }
00083 
00084 PATGenericParticleProducer::~PATGenericParticleProducer() {
00085 }
00086 
00087 void PATGenericParticleProducer::produce(edm::Event & iEvent, const edm::EventSetup & iSetup) {
00088   // Get the vector of GenericParticle's from the event
00089   edm::Handle<edm::View<reco::Candidate> > cands;
00090   iEvent.getByLabel(src_, cands);
00091 
00092   // prepare isolation
00093   if (isolator_.enabled()) isolator_.beginEvent(iEvent,iSetup);
00094 
00095   if (efficiencyLoader_.enabled()) efficiencyLoader_.newEvent(iEvent);
00096   if (resolutionLoader_.enabled()) resolutionLoader_.newEvent(iEvent, iSetup);
00097   if (vertexingHelper_.enabled())  vertexingHelper_.newEvent(iEvent,iSetup);
00098 
00099   // prepare IsoDeposits
00100   std::vector<edm::Handle<edm::ValueMap<IsoDeposit> > > deposits(isoDepositLabels_.size());
00101   for (size_t j = 0, nd = deposits.size(); j < nd; ++j) {
00102     iEvent.getByLabel(isoDepositLabels_[j].second, deposits[j]);
00103   }
00104 
00105   // prepare the MC matching
00106   std::vector<edm::Handle<edm::Association<reco::GenParticleCollection> > > genMatches(genMatchSrc_.size());
00107   if (addGenMatch_) {
00108         for (size_t j = 0, nd = genMatchSrc_.size(); j < nd; ++j) {
00109             iEvent.getByLabel(genMatchSrc_[j], genMatches[j]);
00110         }
00111   }
00112 
00113   // prepare the quality
00114   edm::Handle<edm::ValueMap<float> > qualities;
00115   if (addQuality_) iEvent.getByLabel(qualitySrc_, qualities);
00116 
00117   // loop over cands
00118   std::vector<GenericParticle> * PATGenericParticles = new std::vector<GenericParticle>(); 
00119   for (edm::View<reco::Candidate>::const_iterator itGenericParticle = cands->begin(); itGenericParticle != cands->end(); itGenericParticle++) {
00120     // construct the GenericParticle from the ref -> save ref to original object
00121     unsigned int idx = itGenericParticle - cands->begin();
00122     edm::RefToBase<reco::Candidate> candRef = cands->refAt(idx);
00123 
00124     PATGenericParticles->push_back(GenericParticle(candRef));
00125     GenericParticle & aGenericParticle = PATGenericParticles->back();
00126 
00127     // embed RECO
00128     if (embedTrack_)        aGenericParticle.embedTrack();
00129     if (embedGsfTrack_)     aGenericParticle.embedGsfTrack();
00130     if (embedTracks_)       aGenericParticle.embedTracks();
00131     if (embedStandalone_)   aGenericParticle.embedStandalone();
00132     if (embedCombined_)     aGenericParticle.embedCombined();
00133     if (embedSuperCluster_) aGenericParticle.embedSuperCluster();
00134     if (embedCaloTower_)    aGenericParticle.embedCaloTower();
00135 
00136     // isolation
00137     if (isolator_.enabled()) {
00138         isolator_.fill(*cands, idx, isolatorTmpStorage_);
00139         typedef pat::helper::MultiIsolator::IsolationValuePairs IsolationValuePairs;
00140         // better to loop backwards, so the vector is resized less times
00141         for (IsolationValuePairs::const_reverse_iterator it = isolatorTmpStorage_.rbegin(), ed = isolatorTmpStorage_.rend(); it != ed; ++it) {
00142             aGenericParticle.setIsolation(it->first, it->second);
00143         }
00144     }
00145 
00146     // isodeposit
00147     for (size_t j = 0, nd = deposits.size(); j < nd; ++j) {
00148         aGenericParticle.setIsoDeposit(isoDepositLabels_[j].first, (*deposits[j])[candRef]);
00149     }
00150 
00151     // store the match to the generated final state muons
00152     if (addGenMatch_) {
00153       for(size_t i = 0, n = genMatches.size(); i < n; ++i) {
00154           reco::GenParticleRef genGenericParticle = (*genMatches[i])[candRef];
00155           aGenericParticle.addGenParticleRef(genGenericParticle);
00156       }
00157       if (embedGenMatch_) aGenericParticle.embedGenParticle();
00158     }
00159 
00160     if (addQuality_) {
00161       aGenericParticle.setQuality( (*qualities)[candRef] );
00162     }
00163 
00164     if (efficiencyLoader_.enabled()) {
00165         efficiencyLoader_.setEfficiencies( aGenericParticle, candRef );
00166     }
00167 
00168     if (resolutionLoader_.enabled()) {
00169         resolutionLoader_.setResolutions(aGenericParticle);
00170     }
00171 
00172     if (vertexingHelper_.enabled()) {
00173         aGenericParticle.setVertexAssociation( vertexingHelper_(candRef) );
00174     }
00175 
00176     if ( useUserData_ ) {
00177         userDataHelper_.add( aGenericParticle, iEvent, iSetup );
00178     }
00179 
00180     // PATGenericParticles->push_back(aGenericParticle); // NOOOOO!!!!
00181     // We have already pushed_back this generic particle in the collection
00182     // (we first push an empty particle and then fill it, to avoid useless copies)
00183   }
00184 
00185   // sort GenericParticles in ET
00186   std::sort(PATGenericParticles->begin(), PATGenericParticles->end(), eTComparator_);
00187 
00188   // put genEvt object in Event
00189   std::auto_ptr<std::vector<GenericParticle> > myGenericParticles(PATGenericParticles);
00190   iEvent.put(myGenericParticles);
00191   if (isolator_.enabled()) isolator_.endEvent();
00192 
00193 }
00194 
00195 #include "FWCore/Framework/interface/MakerMacros.h"
00196 
00197 DEFINE_FWK_MODULE(PATGenericParticleProducer);