CMS 3D CMS Logo

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