CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_1/src/RecoMuon/MuonIsolationProducers/plugins/MuIsolatorResultProducer.h

Go to the documentation of this file.
00001 #ifndef MuonIsolationProducers_MuIsolatorResultProducer_H
00002 #define MuonIsolationProducers_MuIsolatorResultProducer_H
00003 
00004 #include "FWCore/Framework/interface/EDProducer.h"
00005 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00006 
00007 #include "RecoMuon/MuonIsolation/interface/MuIsoBaseIsolator.h"
00008 
00009 #include "FWCore/Framework/interface/Event.h"
00010 
00011 #include "DataFormats/Common/interface/RefToBase.h"
00012 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00013 
00014 #include "DataFormats/BeamSpot/interface/BeamSpot.h"
00015 #include "DataFormats/Common/interface/ValueMap.h"
00016 
00017 
00018 #include <string>
00019 
00020 namespace edm { class EventSetup; }
00021 
00022 struct muisorhelper {
00023   typedef muonisolation::MuIsoBaseIsolator Isolator;
00024   typedef Isolator::Result Result;
00025   typedef Isolator::ResultType ResultType;
00026   typedef std::vector<Result> Results;
00027   typedef Isolator::DepositContainer DepositContainer;
00028   
00029   template<typename BT>
00030   class CandMap {
00031   public:
00032     typedef typename edm::RefToBase<BT> key_type;
00033     typedef typename edm::Handle<edm::View<BT> > handle_type;
00034     typedef DepositContainer value_type;
00035     typedef std::pair<key_type, value_type> pair_type;
00036     typedef typename std::vector<pair_type > map_type;
00037     typedef typename map_type::iterator iterator;
00038     
00039     map_type& get() { return cMap_;}
00040     const map_type& get() const {return cMap_;}
00041     const handle_type& handle() const { return handle_;}
00042     void setHandle(const handle_type& rhs) { handle_ = rhs;}
00043   private:
00044     map_type cMap_;
00045     handle_type handle_;
00046   };
00047   
00048 };
00049 
00051 template <typename BT= reco::Candidate>
00052   class MuIsolatorResultProducer : public edm::EDProducer {
00053   
00054   public:
00055   MuIsolatorResultProducer(const edm::ParameterSet&);
00056   
00057   virtual ~MuIsolatorResultProducer();
00058   
00059   virtual void produce(edm::Event&, const edm::EventSetup&);
00060   
00061   private:
00062   typedef muisorhelper::Isolator Isolator;
00063   typedef muisorhelper::Result Result;
00064   typedef muisorhelper::ResultType ResultType;
00065   typedef muisorhelper::Results Results;
00066   typedef muisorhelper::DepositContainer DepositContainer;
00067   
00068   typedef muisorhelper::CandMap<BT> CandMap;
00069   
00070   struct DepositConf { 
00071     edm::InputTag tag; 
00072     double weight; 
00073     double threshold;
00074   };
00075   
00076   struct VetoCuts { 
00077     bool selectAll; 
00078     double muAbsEtaMax; 
00079     double muPtMin;
00080     double muAbsZMax;
00081     double muD0Max;
00082   };
00083   
00084   
00085   
00086   void callWhatProduces();
00087   
00088   unsigned int initAssociation(edm::Event& event, CandMap& candMapT) const;
00089   
00090   void initVetos(std::vector<reco::IsoDeposit::Vetos*>& vetos, CandMap& candMap) const;
00091   
00092   template <typename RT>
00093   void writeOutImpl(edm::Event& event, const CandMap& candMapT, const Results& results) const;
00094   
00095   void writeOut(edm::Event& event, const CandMap& candMap, const Results& results) const;
00096   
00097   edm::ParameterSet theConfig;
00098   std::vector<DepositConf> theDepositConfs;
00099   
00101   bool theRemoveOtherVetos;
00102   VetoCuts theVetoCuts;
00103   
00105   Isolator* theIsolator;
00106   ResultType theResultType;
00107   
00108   
00110   std::string theBeamlineOption;
00111   edm::InputTag theBeamSpotLabel;
00112   reco::TrackBase::Point theBeam;
00113 };
00114 
00115 
00117 template<typename BT>
00118 template<typename RT> inline
00119 void MuIsolatorResultProducer<BT>::writeOutImpl(edm::Event& event, const CandMap& candMapT, 
00120                                                 const Results& results) const {
00122   std::vector<RT> resV(results.size());   
00123   for (unsigned int i = 0; i< resV.size(); ++i) resV[i] = results[i].val<RT>(); 
00124   std::auto_ptr<edm::ValueMap<RT> > outMap(new edm::ValueMap<RT>()); 
00125   typename edm::ValueMap<RT>::Filler filler(*outMap); 
00126 
00128   if (candMapT.get().size()>0){
00129     filler.insert(candMapT.handle(), resV.begin(), resV.end()); 
00130     filler.fill(); 
00131   }
00132   
00133   event.put(outMap); 
00134 }
00135 
00136 
00138 template<typename BT>
00139 inline void MuIsolatorResultProducer<BT>::writeOut(edm::Event& event, 
00140                                                    const CandMap& candMapT, 
00141                                                    const Results& results) const {
00142   
00143   std::string metname = "RecoMuon|MuonIsolationProducers";
00144   LogDebug(metname)<<"Before calling writeOutMap  with result type "<<theIsolator->resultType();
00145   
00146   if (theResultType == Isolator::ISOL_INT_TYPE) writeOutImpl<int>(event, candMapT, results);
00147   if (theResultType == Isolator::ISOL_FLOAT_TYPE) writeOutImpl<float>(event, candMapT, results);
00148   if (theResultType == Isolator::ISOL_BOOL_TYPE) writeOutImpl<bool>(event, candMapT, results);
00149 }
00150 
00151 
00153 template<typename BT>
00154 inline void MuIsolatorResultProducer<BT>::callWhatProduces() {
00155   if (theResultType == Isolator::ISOL_FLOAT_TYPE) produces<edm::ValueMap<float> >();
00156   if (theResultType == Isolator::ISOL_INT_TYPE  ) produces<edm::ValueMap<int> >();
00157   if (theResultType == Isolator::ISOL_BOOL_TYPE ) produces<edm::ValueMap<bool> >();      
00158 }
00159 
00160 // Framework
00161 #include "FWCore/Framework/interface/EDProducer.h"
00162 #include "FWCore/Framework/interface/EventSetup.h"
00163 #include "DataFormats/Common/interface/Handle.h"
00164 
00165 #include "FWCore/Framework/interface/ESHandle.h"
00166 
00167 #include "DataFormats/MuonReco/interface/Muon.h"
00168 #include "DataFormats/RecoCandidate/interface/IsoDeposit.h"
00169 #include "DataFormats/RecoCandidate/interface/IsoDepositFwd.h"
00170 #include "DataFormats/Common/interface/RefToBase.h"
00171 #include "DataFormats/TrackReco/interface/Track.h"
00172 #include "DataFormats/TrackReco/interface/TrackFwd.h"
00173 
00174 #include "RecoMuon/MuonIsolation/interface/Range.h"
00175 #include "DataFormats/RecoCandidate/interface/IsoDepositDirection.h"
00176 
00177 #include "RecoMuon/MuonIsolation/interface/IsolatorByDeposit.h"
00178 #include "RecoMuon/MuonIsolation/interface/IsolatorByDepositCount.h"
00179 #include "RecoMuon/MuonIsolation/interface/IsolatorByNominalEfficiency.h"
00180 
00181 #include <string>
00182 
00183 
00185 template<typename BT>
00186 MuIsolatorResultProducer<BT>::MuIsolatorResultProducer(const edm::ParameterSet& par) :
00187   theConfig(par),
00188   theRemoveOtherVetos(par.getParameter<bool>("RemoveOtherVetos")),
00189   theIsolator(0),
00190   theBeam(0,0,0)
00191 {
00192   LogDebug("RecoMuon|MuonIsolation")<<" MuIsolatorResultProducer CTOR";
00193   
00195   std::vector<edm::ParameterSet> depositInputs = 
00196     par.getParameter<std::vector<edm::ParameterSet> >("InputMuIsoDeposits");    
00197 
00198   std::vector<double> dWeights( depositInputs.size());
00199   std::vector<double> dThresholds( depositInputs.size());
00200 
00201   for (unsigned int iDep = 0; iDep < depositInputs.size(); ++iDep){
00202     DepositConf dConf;
00203     dConf.tag = depositInputs[iDep].getParameter<edm::InputTag>("DepositTag");
00204     dConf.weight = depositInputs[iDep].getParameter<double>("DepositWeight");
00205     dConf.threshold = depositInputs[iDep].getParameter<double>("DepositThreshold");
00206     
00207     dWeights[iDep] = dConf.weight;
00208     dThresholds[iDep] = dConf.threshold;
00209 
00210     theDepositConfs.push_back(dConf);
00211   }
00212 
00213   edm::ParameterSet isoPset = par.getParameter<edm::ParameterSet>("IsolatorPSet");
00215   std::string isolatorType = isoPset.getParameter<std::string>("ComponentName");
00216   if ( isolatorType == "IsolatorByDeposit"){    
00217     std::string coneSizeType = isoPset.getParameter<std::string>("ConeSizeType");
00218     if (coneSizeType == "FixedConeSize"){
00219       float coneSize(isoPset.getParameter<double>("coneSize"));
00220 
00221       theIsolator = new muonisolation::IsolatorByDeposit(coneSize, dWeights, dThresholds);
00222 
00223       //      theIsolator = new IsolatorByDeposit(isoPset);
00224     } else if (coneSizeType == "CutsConeSize"){
00226 //       Cuts cuts(isoPset.getParameter<edm::ParameterSet>("CutsPSet"));
00227       
00228 //       theIsolator = new IsolatorByDeposit(coneSize, dWeights, dThresholds);
00229     }
00230   } else if ( isolatorType == "IsolatorByNominalEfficiency"){
00232     theIsolator = new muonisolation::IsolatorByNominalEfficiency("noname", std::vector<std::string>(1,"8:0.97"), dWeights);
00233   } else if ( isolatorType == "IsolatorByDepositCount"){    
00234     std::string coneSizeType = isoPset.getParameter<std::string>("ConeSizeType");
00235     if (coneSizeType == "FixedConeSize"){
00236       float coneSize(isoPset.getParameter<double>("coneSize"));
00237       
00238       theIsolator = new muonisolation::IsolatorByDepositCount(coneSize, dThresholds);
00239       
00240       //      theIsolator = new IsolatorByDeposit(isoPset);
00241     } else if (coneSizeType == "CutsConeSize"){
00242       //       Cuts cuts(isoPset.getParameter<edm::ParameterSet>("CutsPSet"));
00243       
00244       //       theIsolator = new IsolatorByDeposit(coneSize, dWeights, dThresholds);
00245     }
00246   }
00247   
00248   if (theIsolator == 0 ){
00249     edm::LogError("MuonIsolationProducers")<<"Failed to initialize an isolator";
00250   }
00251   theResultType = theIsolator->resultType();
00252 
00253   callWhatProduces();
00254 
00255   if (theRemoveOtherVetos){
00256     edm::ParameterSet vetoPSet = par.getParameter<edm::ParameterSet>("VetoPSet");
00257     theVetoCuts.selectAll = vetoPSet.getParameter<bool>("SelectAll");
00258 
00262     if (! theVetoCuts.selectAll){
00263       theVetoCuts.muAbsEtaMax = vetoPSet.getParameter<double>("MuAbsEtaMax");
00264       theVetoCuts.muPtMin     = vetoPSet.getParameter<double>("MuPtMin");
00265       theVetoCuts.muAbsZMax   = vetoPSet.getParameter<double>("MuAbsZMax");
00266       theVetoCuts.muD0Max      = vetoPSet.getParameter<double>("MuD0Max");    
00267       theBeamlineOption = par.getParameter<std::string>("BeamlineOption");
00268       theBeamSpotLabel = par.getParameter<edm::InputTag>("BeamSpotLabel");
00269     }
00270   }
00271 }
00272 
00274 template<typename BT>
00275 MuIsolatorResultProducer<BT>::~MuIsolatorResultProducer(){
00276   if (theIsolator) delete theIsolator;
00277   LogDebug("RecoMuon|MuIsolatorResultProducer")<<" MuIsolatorResultProducer DTOR";
00278 }
00279 
00280 
00281 template<typename BT>
00282 void MuIsolatorResultProducer<BT>::produce(edm::Event& event, const edm::EventSetup& eventSetup){
00283   
00284   std::string metname = "RecoMuon|MuonIsolationProducers";
00285   LogDebug(metname)<<" Muon Deposit producing..."
00286                    <<" BEGINING OF EVENT " <<"================================";
00287 
00288   theBeam = reco::TrackBase::Point(0,0, 0); 
00289  
00291   if(theRemoveOtherVetos && ! theVetoCuts.selectAll){
00292     if (theBeamlineOption.compare("BeamSpotFromEvent") == 0){ 
00293       //pick beamSpot 
00294       reco::BeamSpot beamSpot; 
00295       edm::Handle<reco::BeamSpot> beamSpotH; 
00296       
00297       event.getByLabel(theBeamSpotLabel,beamSpotH); 
00298       
00299       if (beamSpotH.isValid()){ 
00300         theBeam = beamSpotH->position(); 
00301         LogTrace(metname)<<"Extracted beam point at "<<theBeam<<std::endl; 
00302       } 
00303     }
00304   } 
00305 
00311   CandMap candMapT;
00312   
00313   unsigned int colSize = initAssociation(event, candMapT);
00314 
00316   Results results(colSize);
00317 
00319   std::vector<reco::IsoDeposit::Vetos*> vetoDeps(theDepositConfs.size(), 0);
00320 
00321   if (colSize != 0){
00322     if (theRemoveOtherVetos){
00323 
00324       initVetos(vetoDeps, candMapT);
00325     }
00326 
00328     for (unsigned int muI=0; muI < colSize; ++muI){
00329       results[muI] = theIsolator->result(candMapT.get()[muI].second, *(candMapT.get()[muI].first));
00330       
00331       if (results[muI].typeF()!= theIsolator->resultType()){
00332         edm::LogError("MuonIsolationProducers")<<"Failed to get result from the isolator";
00333       }
00334     }
00335     
00336   }
00337 
00338   LogDebug(metname)<<"Ready to write out results of size "<<results.size();
00339   writeOut(event, candMapT, results);
00340 
00341   for(unsigned int iDep = 0; iDep< vetoDeps.size(); ++iDep){
00343     if (vetoDeps[iDep]){
00344       delete vetoDeps[iDep];
00345       vetoDeps[iDep] = 0;
00346     }
00347   }
00348 }
00349 
00350 template<typename BT>
00351 unsigned int
00352 MuIsolatorResultProducer<BT>::initAssociation(edm::Event& event, CandMap& candMapT) const {
00353   std::string metname = "RecoMuon|MuonIsolationProducers";
00354   
00355   typedef reco::IsoDepositMap::container CT;
00356 
00357   for (unsigned int iMap = 0; iMap < theDepositConfs.size(); ++iMap){
00358     edm::Handle<reco::IsoDepositMap> depH;
00359     event.getByLabel(theDepositConfs[iMap].tag, depH);
00360     LogDebug(metname) <<"Got Deposits of size "<<depH->size();
00361     if (depH->size()==0) continue;
00362 
00365     typename CandMap::handle_type keyH;
00366     event.get(depH->begin().id(), keyH);
00367     candMapT.setHandle(keyH);
00368     typename CT::const_iterator depHCI = depH->begin().begin();
00369     typename CT::const_iterator depEnd = depH->begin().end();
00370     unsigned int keyI=0;
00371     for (; depHCI != depEnd; ++depHCI, ++keyI){
00372 
00373       typename CandMap::key_type muPtr(keyH->refAt(keyI));
00375       if (iMap == 0) candMapT.get().push_back(typename CandMap::pair_type(muPtr, DepositContainer(theDepositConfs.size())));
00376       typename CandMap::iterator muI = candMapT.get().begin();
00377       for (; muI != candMapT.get().end(); ++muI){
00378         if (muI->first == muPtr) break;
00379       }
00380       if (muI->first != muPtr){
00381         edm::LogError("MuonIsolationProducers")<<"Failed to align muon map";
00382       }
00383       muI->second[iMap].dep = &*depHCI; 
00384     }
00385   }
00386 
00387   LogDebug(metname)<<"Picked and aligned nDeps = "<<candMapT.get().size();
00388   return candMapT.get().size();
00389 }
00390 
00391 template <typename BT >
00392 void MuIsolatorResultProducer<BT>::initVetos(std::vector<reco::IsoDeposit::Vetos*>& vetos, CandMap& candMapT) const {
00393   std::string metname = "RecoMuon|MuonIsolationProducers";
00394   
00395 
00396   if (theRemoveOtherVetos){
00397     LogDebug(metname)<<"Start checking for vetos based on input/expected vetos.size of "<<vetos.size()
00398                      <<" passed at "<<&vetos
00399                      <<" and an input map.size of "<<candMapT.get().size();
00400 
00401     unsigned int muI = 0;
00402     for (; muI < candMapT.get().size(); ++muI) {
00403       typename CandMap::key_type mu = candMapT.get()[muI].first;
00404       double d0 = ( (mu->vx() - theBeam.x() )* mu->py() - (mu->vy() - theBeam.y())* mu->px() ) / mu->pt();
00405       LogDebug(metname)<<"Muon at index "<<muI;
00406       if (theVetoCuts.selectAll 
00407           || (fabs(mu->eta()) < theVetoCuts.muAbsEtaMax
00408               && mu->pt() > theVetoCuts.muPtMin
00409               && fabs(mu->vz()) < theVetoCuts.muAbsZMax
00410               && fabs(d0) < theVetoCuts.muD0Max
00411               )
00412           ){
00413         LogDebug(metname)<<"muon passes the cuts";
00414         for (unsigned int iDep =0; iDep < candMapT.get()[muI].second.size(); ++iDep){
00415           if (vetos[iDep] == 0) vetos[iDep] = new reco::IsoDeposit::Vetos();
00416 
00417           vetos[iDep]->push_back(candMapT.get()[muI].second[iDep].dep->veto());
00418         }
00419       }
00420     }
00421 
00422     LogDebug(metname)<<"Assigning vetos";
00423     muI = 0;
00424     for (; muI < candMapT.get().size(); ++muI) {
00425       for(unsigned int iDep =0; iDep < candMapT.get()[muI].second.size(); ++iDep){
00426         candMapT.get()[muI].second[iDep].vetos = vetos[iDep];
00427       }
00428     }
00429     LogDebug(metname)<<"Done with vetos";
00430   }
00431 }
00432 
00433 
00434   
00435 #endif