CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/TauAnalysis/MCEmbeddingTools/src/MCParticleReplacer.cc

Go to the documentation of this file.
00001 #include "TauAnalysis/MCEmbeddingTools/interface/MCParticleReplacer.h"
00002 #include "TauAnalysis/MCEmbeddingTools/interface/ParticleReplacerFactory.h"
00003 
00004 #include "FWCore/Framework/interface/MakerMacros.h"
00005 #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
00006 #include "DataFormats/Candidate/interface/CompositeCandidate.h"
00007 #include "FWCore/Framework/interface/Event.h"
00008 
00009 #include "SimDataFormats/GeneratorProducts/interface/GenFilterInfo.h"
00010 
00011 // replacementMode =
00012 //      0 - remove Myons from existing HepMCProduct and implant taus (+decay products)
00013 //      1 - build new HepMCProduct only with taus (+decay products)
00014 MCParticleReplacer::MCParticleReplacer(const edm::ParameterSet& pset):
00015   src_(pset.getParameter<edm::InputTag>("src")),
00016   srcHepMC_(pset.getParameter<edm::InputTag>("hepMcSrc")),
00017   hepMcMode_(stringToHepMcMode(pset.getParameter<std::string>("hepMcMode"))),
00018   replacer_(ParticleReplacerFactory::create(pset.getParameter<std::string>("algorithm"), pset)) {
00019 
00020   produces<edm::HepMCProduct>();
00021   produces<GenFilterInfo>("minVisPtFilter");
00022 }
00023 
00024 MCParticleReplacer::~MCParticleReplacer()
00025 {}
00026 
00027 MCParticleReplacer::HepMcMode MCParticleReplacer::stringToHepMcMode(const std::string& name) {
00028   if(name == "new")
00029     return kNew;
00030   else if(name == "replace")
00031     return kReplace;
00032   else
00033     throw cms::Exception("Configuration") << "Unsupported hepMcMode " << name << ", should be 'new' or 'replace'" << std::endl;
00034 }
00035 
00036 // ------------ method called to produce the data  ------------
00037 void
00038 MCParticleReplacer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup)
00039 {
00040         std::vector<reco::Muon> muons;
00041         
00042         edm::Handle< std::vector< reco::CompositeCandidate > > combCandidatesHandle;
00043         if (iEvent.getByLabel(src_, combCandidatesHandle))
00044         {
00045                 if (combCandidatesHandle->size()==0)
00046                         return;
00047                 for (size_t idx = 0; idx < combCandidatesHandle->at(0).numberOfDaughters(); ++idx)
00048                 {
00049                         int charge = combCandidatesHandle->at(0).daughter(idx)->charge();
00050                         reco::Particle::LorentzVector p4 = combCandidatesHandle->at(0).daughter(idx)->p4();
00051                         reco::Particle::Point vtx = combCandidatesHandle->at(0).daughter(idx)->vertex();
00052                         muons.push_back( reco::Muon(charge, p4, vtx));
00053                 }
00054         }
00055         else
00056         {
00057                 edm::Handle< edm::View<reco::Candidate> > candsHandle;
00058                 if (iEvent.getByLabel(src_, candsHandle))
00059                 {
00060                         for (size_t idx = 0; idx < candsHandle->size(); ++idx)
00061                         {
00062                                 int charge = candsHandle->at(idx).charge();
00063                                 reco::Particle::LorentzVector p4 = candsHandle->at(idx).p4();
00064                                 reco::Particle::Point vtx = candsHandle->at(idx).vertex();
00065                                 muons.push_back( reco::Muon(charge, p4, vtx));
00066                         }
00067                 }
00068         }
00069         if (muons.size() == 0)
00070         {
00071                 edm::LogError("MCParticleReplacer") << "No candidates or muons found!";
00072                 return;
00073         }
00074         
00075   std::auto_ptr<HepMC::GenEvent> evt;
00076   if(hepMcMode_ == kReplace) {
00077     edm::Handle<edm::HepMCProduct> HepMCHandle;  
00078     iEvent.getByLabel(srcHepMC_, HepMCHandle);
00079 
00080     evt = replacer_->produce(muons, 0, HepMCHandle->GetEvent());
00081   }
00082   else if(hepMcMode_ == kNew) {
00083     evt = replacer_->produce(muons);
00084   }
00085   else
00086     throw cms::Exception("LogicError") << "Invalid hepMcMode " << hepMcMode_ << std::endl;
00087 
00088 
00089   if(evt.get() != 0) {
00090     std::auto_ptr<edm::HepMCProduct> bare_product(new edm::HepMCProduct());  
00091     bare_product->addHepMCData(evt.release()); // transfer ownership of the HepMC:GenEvent to bare_product
00092 
00093     iEvent.put(bare_product);
00094 
00095     std::auto_ptr<GenFilterInfo> info(new GenFilterInfo(replacer_->tried, replacer_->passed));
00096     iEvent.put(info, std::string("minVisPtFilter"));
00097   }
00098   
00099 }
00100 
00101 void MCParticleReplacer::beginRun(const edm::Run& iRun,const edm::EventSetup& iSetup)
00102 {
00103   replacer_->beginRun(iRun, iSetup);
00104 }
00105 
00106 void MCParticleReplacer::endRun(const edm::Run& iRun,const edm::EventSetup& iSetup)
00107 {
00108   replacer_->endRun();
00109 }
00110 
00111 // ------------ method called once each job just before starting event loop  ------------
00112 void 
00113 MCParticleReplacer::beginJob()
00114 {
00115   replacer_->beginJob();
00116 }
00117 
00118 // ------------ method called once each job just after ending the event loop  ------------
00119 void 
00120 MCParticleReplacer::endJob()
00121 {
00122   replacer_->endJob();
00123 }
00124 
00125 //define this as a plug-in
00126 DEFINE_FWK_MODULE(MCParticleReplacer);