CMS 3D CMS Logo

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   uint 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 (uint 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 using namespace edm;
00184 using namespace std;
00185 using namespace reco;
00186 using namespace muonisolation;
00187 
00189 template<typename BT>
00190 MuIsolatorResultProducer<BT>::MuIsolatorResultProducer(const ParameterSet& par) :
00191   theConfig(par),
00192   theRemoveOtherVetos(par.getParameter<bool>("RemoveOtherVetos")),
00193   theIsolator(0),
00194   theBeam(0,0,0)
00195 {
00196   LogDebug("RecoMuon|MuonIsolation")<<" MuIsolatorResultProducer CTOR";
00197 
00199   std::vector<edm::ParameterSet> depositInputs = 
00200     par.getParameter<std::vector<edm::ParameterSet> >("InputMuIsoDeposits");    
00201 
00202   std::vector<double> dWeights( depositInputs.size());
00203   std::vector<double> dThresholds( depositInputs.size());
00204 
00205   for (uint iDep = 0; iDep < depositInputs.size(); ++iDep){
00206     DepositConf dConf;
00207     dConf.tag = depositInputs[iDep].getParameter<edm::InputTag>("DepositTag");
00208     dConf.weight = depositInputs[iDep].getParameter<double>("DepositWeight");
00209     dConf.threshold = depositInputs[iDep].getParameter<double>("DepositThreshold");
00210     
00211     dWeights[iDep] = dConf.weight;
00212     dThresholds[iDep] = dConf.threshold;
00213 
00214     theDepositConfs.push_back(dConf);
00215   }
00216 
00217   edm::ParameterSet isoPset = par.getParameter<edm::ParameterSet>("IsolatorPSet");
00219   std::string isolatorType = isoPset.getParameter<std::string>("ComponentName");
00220   if ( isolatorType == "IsolatorByDeposit"){    
00221     std::string coneSizeType = isoPset.getParameter<std::string>("ConeSizeType");
00222     if (coneSizeType == "FixedConeSize"){
00223       float coneSize(isoPset.getParameter<double>("coneSize"));
00224 
00225       theIsolator = new IsolatorByDeposit(coneSize, dWeights, dThresholds);
00226 
00227       //      theIsolator = new IsolatorByDeposit(isoPset);
00228     } else if (coneSizeType == "CutsConeSize"){
00230 //       Cuts cuts(isoPset.getParameter<edm::ParameterSet>("CutsPSet"));
00231       
00232 //       theIsolator = new IsolatorByDeposit(coneSize, dWeights, dThresholds);
00233     }
00234   } else if ( isolatorType == "IsolatorByNominalEfficiency"){
00236     theIsolator = new IsolatorByNominalEfficiency("noname", std::vector<std::string>(1,"8:0.97"), dWeights);
00237   } else if ( isolatorType == "IsolatorByDepositCount"){    
00238     std::string coneSizeType = isoPset.getParameter<std::string>("ConeSizeType");
00239     if (coneSizeType == "FixedConeSize"){
00240       float coneSize(isoPset.getParameter<double>("coneSize"));
00241       
00242       theIsolator = new IsolatorByDepositCount(coneSize, dThresholds);
00243       
00244       //      theIsolator = new IsolatorByDeposit(isoPset);
00245     } else if (coneSizeType == "CutsConeSize"){
00246       //       Cuts cuts(isoPset.getParameter<edm::ParameterSet>("CutsPSet"));
00247       
00248       //       theIsolator = new IsolatorByDeposit(coneSize, dWeights, dThresholds);
00249     }
00250   }
00251   
00252   if (theIsolator == 0 ){
00253     edm::LogError("MuonIsolationProducers")<<"Failed to initialize an isolator";
00254   }
00255   theResultType = theIsolator->resultType();
00256 
00257   callWhatProduces();
00258 
00259   if (theRemoveOtherVetos){
00260     edm::ParameterSet vetoPSet = par.getParameter<edm::ParameterSet>("VetoPSet");
00261     theVetoCuts.selectAll = vetoPSet.getParameter<bool>("SelectAll");
00262 
00266     if (! theVetoCuts.selectAll){
00267       theVetoCuts.muAbsEtaMax = vetoPSet.getParameter<double>("MuAbsEtaMax");
00268       theVetoCuts.muPtMin     = vetoPSet.getParameter<double>("MuPtMin");
00269       theVetoCuts.muAbsZMax   = vetoPSet.getParameter<double>("MuAbsZMax");
00270       theVetoCuts.muD0Max      = vetoPSet.getParameter<double>("MuD0Max");    
00271       theBeamlineOption = par.getParameter<string>("BeamlineOption");
00272       theBeamSpotLabel = par.getParameter<edm::InputTag>("BeamSpotLabel");
00273     }
00274   }
00275 }
00276 
00278 template<typename BT>
00279 MuIsolatorResultProducer<BT>::~MuIsolatorResultProducer(){
00280   if (theIsolator) delete theIsolator;
00281   LogDebug("RecoMuon|MuIsolatorResultProducer")<<" MuIsolatorResultProducer DTOR";
00282 }
00283 
00284 
00285 template<typename BT>
00286 void MuIsolatorResultProducer<BT>::produce(Event& event, const EventSetup& eventSetup){
00287   
00288   std::string metname = "RecoMuon|MuonIsolationProducers";
00289   LogDebug(metname)<<" Muon Deposit producing..."
00290                    <<" BEGINING OF EVENT " <<"================================";
00291 
00292   theBeam = reco::TrackBase::Point(0,0, 0); 
00293  
00295   if(theRemoveOtherVetos && ! theVetoCuts.selectAll){
00296     if (theBeamlineOption.compare("BeamSpotFromEvent") == 0){ 
00297       //pick beamSpot 
00298       reco::BeamSpot beamSpot; 
00299       edm::Handle<reco::BeamSpot> beamSpotH; 
00300       
00301       event.getByLabel(theBeamSpotLabel,beamSpotH); 
00302       
00303       if (beamSpotH.isValid()){ 
00304         theBeam = beamSpotH->position(); 
00305         LogTrace(metname)<<"Extracted beam point at "<<theBeam<<std::endl; 
00306       } 
00307     }
00308   } 
00309 
00315   CandMap candMapT;
00316   
00317   uint colSize = initAssociation(event, candMapT);
00318 
00320   Results results(colSize);
00321 
00323   std::vector<reco::IsoDeposit::Vetos*> vetoDeps(theDepositConfs.size(), 0);
00324 
00325   if (colSize != 0){
00326     if (theRemoveOtherVetos){
00327 
00328       initVetos(vetoDeps, candMapT);
00329     }
00330 
00332     for (uint muI=0; muI < colSize; ++muI){
00333       results[muI] = theIsolator->result(candMapT.get()[muI].second, *(candMapT.get()[muI].first));
00334       
00335       if (results[muI].typeF()!= theIsolator->resultType()){
00336         edm::LogError("MuonIsolationProducers")<<"Failed to get result from the isolator";
00337       }
00338     }
00339     
00340   }
00341 
00342   LogDebug(metname)<<"Ready to write out results of size "<<results.size();
00343   writeOut(event, candMapT, results);
00344 
00345   for(uint iDep = 0; iDep< vetoDeps.size(); ++iDep){
00347     if (vetoDeps[iDep]){
00348       delete vetoDeps[iDep];
00349       vetoDeps[iDep] = 0;
00350     }
00351   }
00352 }
00353 
00354 template<typename BT>
00355 uint
00356 MuIsolatorResultProducer<BT>::initAssociation(Event& event, CandMap& candMapT) const {
00357   std::string metname = "RecoMuon|MuonIsolationProducers";
00358   
00359   typedef reco::IsoDepositMap::container CT;
00360 
00361   for (uint iMap = 0; iMap < theDepositConfs.size(); ++iMap){
00362     edm::Handle<reco::IsoDepositMap> depH;
00363     event.getByLabel(theDepositConfs[iMap].tag, depH);
00364     LogDebug(metname) <<"Got Deposits of size "<<depH->size();
00365     if (depH->size()==0) continue;
00366 
00369     typename CandMap::handle_type keyH;
00370     event.get(depH->begin().id(), keyH);
00371     candMapT.setHandle(keyH);
00372     typename CT::const_iterator depHCI = depH->begin().begin();
00373     typename CT::const_iterator depEnd = depH->begin().end();
00374     uint keyI=0;
00375     for (; depHCI != depEnd; ++depHCI, ++keyI){
00376 
00377       typename CandMap::key_type muPtr(keyH->refAt(keyI));
00379       if (iMap == 0) candMapT.get().push_back(typename CandMap::pair_type(muPtr, DepositContainer(theDepositConfs.size())));
00380       typename CandMap::iterator muI = candMapT.get().begin();
00381       for (; muI != candMapT.get().end(); ++muI){
00382         if (muI->first == muPtr) break;
00383       }
00384       if (muI->first != muPtr){
00385         edm::LogError("MuonIsolationProducers")<<"Failed to align muon map";
00386       }
00387       muI->second[iMap].dep = &*depHCI; 
00388     }
00389   }
00390 
00391   LogDebug(metname)<<"Picked and aligned nDeps = "<<candMapT.get().size();
00392   return candMapT.get().size();
00393 }
00394 
00395 template <typename BT >
00396 void MuIsolatorResultProducer<BT>::initVetos(std::vector<reco::IsoDeposit::Vetos*>& vetos, CandMap& candMapT) const {
00397   std::string metname = "RecoMuon|MuonIsolationProducers";
00398   
00399 
00400   if (theRemoveOtherVetos){
00401     LogDebug(metname)<<"Start checking for vetos based on input/expected vetos.size of "<<vetos.size()
00402                      <<" passed at "<<&vetos
00403                      <<" and an input map.size of "<<candMapT.get().size();
00404 
00405     uint muI = 0;
00406     for (; muI < candMapT.get().size(); ++muI) {
00407       typename CandMap::key_type mu = candMapT.get()[muI].first;
00408       double d0 = ( (mu->vx() - theBeam.x() )* mu->py() - (mu->vy() - theBeam.y())* mu->px() ) / mu->pt();
00409       LogDebug(metname)<<"Muon at index "<<muI;
00410       if (theVetoCuts.selectAll 
00411           || (fabs(mu->eta()) < theVetoCuts.muAbsEtaMax
00412               && mu->pt() > theVetoCuts.muPtMin
00413               && fabs(mu->vz()) < theVetoCuts.muAbsZMax
00414               && fabs(d0) < theVetoCuts.muD0Max
00415               )
00416           ){
00417         LogDebug(metname)<<"muon passes the cuts";
00418         for (uint iDep =0; iDep < candMapT.get()[muI].second.size(); ++iDep){
00419           if (vetos[iDep] == 0) vetos[iDep] = new reco::IsoDeposit::Vetos();
00420 
00421           vetos[iDep]->push_back(candMapT.get()[muI].second[iDep].dep->veto());
00422         }
00423       }
00424     }
00425 
00426     LogDebug(metname)<<"Assigning vetos";
00427     muI = 0;
00428     for (; muI < candMapT.get().size(); ++muI) {
00429       for(uint iDep =0; iDep < candMapT.get()[muI].second.size(); ++iDep){
00430         candMapT.get()[muI].second[iDep].vetos = vetos[iDep];
00431       }
00432     }
00433     LogDebug(metname)<<"Done with vetos";
00434   }
00435 }
00436 
00437 
00438   
00439 #endif

Generated on Tue Jun 9 17:44:24 2009 for CMSSW by  doxygen 1.5.4