CMS 3D CMS Logo

PATGenericParticleProducer.cc

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

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