CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_13_patch3/src/RecoMuon/L3MuonIsolationProducer/src/L3MuonCombinedRelativeIsolationProducer.cc

Go to the documentation of this file.
00001 #include "L3MuonCombinedRelativeIsolationProducer.h"
00002 
00003 // Framework
00004 #include "FWCore/Framework/interface/EDProducer.h"
00005 #include "FWCore/Framework/interface/Event.h"
00006 #include "FWCore/Framework/interface/EventSetup.h"
00007 #include "DataFormats/Common/interface/Handle.h"
00008 
00009 #include "FWCore/Framework/interface/ESHandle.h"
00010 
00011 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00012 
00013 #include "DataFormats/Common/interface/AssociationMap.h"
00014 #include "DataFormats/TrackReco/interface/Track.h"
00015 #include "DataFormats/TrackReco/interface/TrackFwd.h"
00016 
00017 #include "DataFormats/RecoCandidate/interface/IsoDeposit.h"
00018 #include "DataFormats/RecoCandidate/interface/IsoDepositFwd.h"
00019 
00020 #include "RecoMuon/MuonIsolation/interface/Range.h"
00021 #include "DataFormats/RecoCandidate/interface/IsoDepositDirection.h"
00022 
00023 #include "PhysicsTools/IsolationAlgos/interface/IsoDepositExtractor.h"
00024 #include "PhysicsTools/IsolationAlgos/interface/IsoDepositExtractorFactory.h"
00025 
00026 #include "L3NominalEfficiencyConfigurator.h"
00027 
00028 #include <string>
00029 
00030 using namespace edm;
00031 using namespace std;
00032 using namespace reco;
00033 using namespace muonisolation;
00034 
00036 L3MuonCombinedRelativeIsolationProducer::L3MuonCombinedRelativeIsolationProducer(const ParameterSet& par) :
00037   theConfig(par),
00038   theMuonCollectionLabel(par.getParameter<InputTag>("inputMuonCollection")),
00039   optOutputIsoDeposits(par.getParameter<bool>("OutputMuIsoDeposits")),
00040   useRhoCorrectedCaloDeps(par.existsAs<bool>("UseRhoCorrectedCaloDeposits") ? 
00041                           par.getParameter<bool>("UseRhoCorrectedCaloDeposits") : false),
00042   theCaloDepsLabel(par.existsAs<InputTag>("CaloDepositsLabel") ? 
00043                    par.getParameter<InputTag>("CaloDepositsLabel") :
00044                    InputTag("hltL3CaloMuonCorrectedIsolations")),
00045   caloExtractor(0),
00046   trkExtractor(0),
00047   theTrackPt_Min(-1),
00048   printDebug (par.getParameter<bool>("printDebug"))
00049   {
00050   LogDebug("RecoMuon|L3MuonCombinedRelativeIsolationProducer")<<" L3MuonCombinedRelativeIsolationProducer CTOR";
00051 
00052   if (optOutputIsoDeposits) {
00053     produces<reco::IsoDepositMap>("trkIsoDeposits");
00054     if( useRhoCorrectedCaloDeps==false ) // otherwise, calo deposits have been previously computed
00055       produces<reco::IsoDepositMap>("caloIsoDeposits");
00056     //produces<std::vector<double> >("combinedRelativeIsoDeposits");
00057     produces<edm::ValueMap<double> >("combinedRelativeIsoDeposits");
00058   }
00059   produces<edm::ValueMap<bool> >();
00060 
00061 }
00062   
00064 L3MuonCombinedRelativeIsolationProducer::~L3MuonCombinedRelativeIsolationProducer(){
00065   LogDebug("RecoMuon|L3MuonCombinedRelativeIsolationProducer")<<" L3MuonCombinedRelativeIsolationProducer DTOR";
00066   if (caloExtractor) delete caloExtractor;
00067   if (trkExtractor) delete trkExtractor;
00068 }
00069 
00070 void L3MuonCombinedRelativeIsolationProducer::beginJob()
00071 {
00072 
00073   //
00074   // Extractor
00075   //
00076   // Calorimeters (ONLY if not previously computed)
00077   //
00078   if( useRhoCorrectedCaloDeps==false ) {
00079     edm::ParameterSet caloExtractorPSet = theConfig.getParameter<edm::ParameterSet>("CaloExtractorPSet");
00080   
00081     theTrackPt_Min = theConfig.getParameter<double>("TrackPt_Min");
00082     std::string caloExtractorName = caloExtractorPSet.getParameter<std::string>("ComponentName");
00083     caloExtractor = IsoDepositExtractorFactory::get()->create( caloExtractorName, caloExtractorPSet);
00084     //std::string caloDepositType = caloExtractorPSet.getUntrackedParameter<std::string>("DepositLabel"); // N.B. Not used in the following!
00085   }
00086 
00087   // Tracker
00088   //
00089   edm::ParameterSet trkExtractorPSet = theConfig.getParameter<edm::ParameterSet>("TrkExtractorPSet");
00090 
00091   std::string trkExtractorName = trkExtractorPSet.getParameter<std::string>("ComponentName");
00092   trkExtractor = IsoDepositExtractorFactory::get()->create( trkExtractorName, trkExtractorPSet);
00093   //std::string trkDepositType = trkExtractorPSet.getUntrackedParameter<std::string>("DepositLabel"); // N.B. Not used in the following!
00094 
00095 
00096   
00097   //
00098   // Cuts for track isolation
00099   //
00100   edm::ParameterSet cutsPSet = theConfig.getParameter<edm::ParameterSet>("CutsPSet");
00101   std::string cutsName = cutsPSet.getParameter<std::string>("ComponentName");
00102   if (cutsName == "SimpleCuts") {
00103     theCuts = Cuts(cutsPSet);
00104   } 
00105   else if (
00106            //        (cutsName== "L3NominalEfficiencyCuts_PXLS" && depositType=="PXLS")
00107            //     || (cutsName== "L3NominalEfficiencyCuts_TRKS" && depositType=="TRKS") 
00109            (cutsName== "L3NominalEfficiencyCuts_PXLS" )
00110            || (cutsName== "L3NominalEfficiencyCuts_TRKS") ) {
00111     theCuts = L3NominalEfficiencyConfigurator(cutsPSet).cuts();
00112   } 
00113   else {
00114     LogError("L3MuonCombinedRelativeIsolationProducer::beginJob")
00115       <<"cutsName: "<<cutsPSet<<" is not recognized:"
00116       <<" theCuts not set!";
00117   }
00118   LogTrace("")<< theCuts.print();
00119 
00120   // (kludge) additional cut on the number of tracks
00121   theMaxNTracks = cutsPSet.getParameter<int>("maxNTracks");
00122   theApplyCutsORmaxNTracks = cutsPSet.getParameter<bool>("applyCutsORmaxNTracks");
00123 }
00124 
00125 void L3MuonCombinedRelativeIsolationProducer::produce(Event& event, const EventSetup& eventSetup){
00126   std::string metname = "RecoMuon|L3MuonCombinedRelativeIsolationProducer";
00127   
00128   if (printDebug) std::cout  <<" L3 Muon Isolation producing..."
00129             <<" BEGINING OF EVENT " <<"================================" <<std::endl;
00130 
00131   // Take the SA container
00132   if (printDebug) std::cout  <<" Taking the muons: "<<theMuonCollectionLabel << std::endl;
00133   Handle<TrackCollection> muons;
00134   event.getByLabel(theMuonCollectionLabel,muons);
00135 
00136   // Take calo deposits with rho corrections (ONLY if previously computed)
00137   Handle< edm::ValueMap<float> > caloDepWithCorrMap;
00138   if( useRhoCorrectedCaloDeps )
00139     event.getByLabel(theCaloDepsLabel, caloDepWithCorrMap);
00140 
00141   std::auto_ptr<reco::IsoDepositMap> caloDepMap( new reco::IsoDepositMap());
00142   std::auto_ptr<reco::IsoDepositMap> trkDepMap( new reco::IsoDepositMap());
00143 
00144   std::auto_ptr<edm::ValueMap<bool> > comboIsoDepMap( new edm::ValueMap<bool> ());
00145 
00146   //std::auto_ptr<std::vector<double> > combinedRelativeDeps(new std::vector<double>());
00147   std::auto_ptr<edm::ValueMap<double> > combinedRelativeDepMap(new edm::ValueMap<double>());
00148 
00149   
00150   //
00151   // get Vetos and deposits
00152   //
00153   unsigned int nMuons = muons->size();
00154 
00155   IsoDeposit::Vetos trkVetos(nMuons);  
00156   std::vector<IsoDeposit> trkDeps(nMuons);
00157   
00158 
00159   // IsoDeposit::Vetos caloVetos(nMuons);  
00160   // std::vector<IsoDeposit> caloDeps(nMuons);
00161   // std::vector<float> caloCorrDeps(nMuons, 0.);  // if calo deposits with corrections available
00162 
00163   IsoDeposit::Vetos caloVetos;  
00164   std::vector<IsoDeposit> caloDeps;
00165   std::vector<float> caloCorrDeps;  // if calo deposits with corrections available
00166 
00167   if(useRhoCorrectedCaloDeps) {
00168     caloCorrDeps.resize(nMuons, 0.);
00169   }
00170   else {
00171     caloVetos.resize(nMuons);
00172     caloDeps.resize(nMuons);
00173   }
00174   
00175   std::vector<double> combinedRelativeDeps(nMuons, 0.);
00176   std::vector<bool> combinedRelativeIsos(nMuons, false);
00177 
00178   for (unsigned int i=0; i<nMuons; i++) {
00179     
00180     TrackRef mu(muons,i);
00181     
00182     trkDeps[i] = trkExtractor->deposit(event, eventSetup, *mu);
00183     trkVetos[i] = trkDeps[i].veto();
00184 
00185     if( useRhoCorrectedCaloDeps ) {
00186       caloCorrDeps[i] = (*caloDepWithCorrMap)[mu];
00187     }
00188     else {
00189       caloDeps[i] = caloExtractor->deposit(event, eventSetup, *mu);
00190       caloVetos[i] = caloDeps[i].veto();
00191     }
00192 
00193   }
00194 
00195   //
00196   // add here additional vetos
00197   //
00198   //.....
00199 
00200   //
00201   // actual cut step
00202   //
00203 
00204   if (printDebug) std::cout  << "Looping over deposits...." << std::endl;
00205   for(unsigned int iMu=0; iMu < nMuons; ++iMu){
00206 
00207     if (printDebug) std::cout  << "Muon number = " << iMu << std::endl;
00208     const reco::Track* mu = &(*muons)[iMu];
00209 
00210     // cuts 
00211     const Cuts::CutSpec & cut = theCuts( mu->eta());
00212 
00213 
00214     if (printDebug) std::cout << "CUTDEBUG: Muon eta = " << mu->eta() << std::endl
00215                               << "CUTDEBUG: Muon pt  = " <<  mu->pt() << std::endl
00216                               << "CUTDEBUG: minEta   = " << cut.etaRange.min() << std::endl
00217                               << "CUTDEBUG: maxEta   = " << cut.etaRange.max() << std::endl
00218                               << "CUTDEBUG: consize  = " << cut.conesize << std::endl
00219                               << "CUTDEBUG: thresho  = " << cut.threshold << std::endl;
00220     
00221     const IsoDeposit & trkDeposit = trkDeps[iMu];
00222     if (printDebug) std::cout  << trkDeposit.print();
00223     std::pair<double, int> trkIsoSumAndCount = trkDeposit.depositAndCountWithin(cut.conesize, trkVetos, theTrackPt_Min);
00224 
00225     double caloIsoSum = 0.;
00226     if( useRhoCorrectedCaloDeps ) {
00227       caloIsoSum = caloCorrDeps[iMu];
00228       if(caloIsoSum<0.) caloIsoSum = 0.;
00229       if(printDebug) std::cout << "Rho-corrected calo deposit (min. 0) = " << caloIsoSum << std::endl;
00230     }
00231     else {
00232       const IsoDeposit & caloDeposit = caloDeps[iMu];
00233       if (printDebug) std::cout  << caloDeposit.print();
00234       caloIsoSum = caloDeposit.depositWithin(cut.conesize, caloVetos);
00235     }
00236 
00237     double trkIsoSum = trkIsoSumAndCount.first;
00238     int count = trkIsoSumAndCount.second;
00239 
00240     double muPt = mu->pt();
00241     if( muPt<1. ) muPt = 1.;
00242     double combinedRelativeDeposit = ((trkIsoSum + caloIsoSum ) / muPt);
00243     bool result = ( combinedRelativeDeposit < cut.threshold); 
00244     if (theApplyCutsORmaxNTracks ) result |= count <= theMaxNTracks;
00245     if (printDebug) std::cout  <<"  trk dep in cone:  " << trkIsoSum << "  with count "<<count <<std::endl
00246               <<"  calo dep in cone: " << caloIsoSum << std::endl
00247               <<"  muPt: " << muPt << std::endl
00248               <<"  relIso:  " <<combinedRelativeDeposit  << std::endl
00249               <<"  is isolated: "<<result << std::endl;
00250 
00251     combinedRelativeIsos[iMu] = result;
00252     //combinedRelativeDeps->push_back(combinedRelativeDeposit);
00253     combinedRelativeDeps[iMu] = combinedRelativeDeposit;
00254   }
00255 
00256   //
00257   // store
00258   //
00259   if (optOutputIsoDeposits){
00260     
00261     reco::IsoDepositMap::Filler depFillerTrk(*trkDepMap);
00262     depFillerTrk.insert(muons, trkDeps.begin(), trkDeps.end());
00263     depFillerTrk.fill();
00264     event.put(trkDepMap, "trkIsoDeposits");
00265 
00266     if( useRhoCorrectedCaloDeps==false ) {
00267       reco::IsoDepositMap::Filler depFillerCalo(*caloDepMap);
00268       depFillerCalo.insert(muons, caloDeps.begin(), caloDeps.end());
00269       depFillerCalo.fill();
00270       event.put(caloDepMap, "caloIsoDeposits");
00271     }
00272 
00273     //event.put(combinedRelativeDeps, "combinedRelativeIsoDeposits");
00274     edm::ValueMap<double>::Filler depFillerCombRel(*combinedRelativeDepMap);
00275     depFillerCombRel.insert(muons, combinedRelativeDeps.begin(), combinedRelativeDeps.end());
00276     depFillerCombRel.fill();
00277     event.put(combinedRelativeDepMap, "combinedRelativeIsoDeposits");
00278 
00279   }
00280   edm::ValueMap<bool>::Filler isoFiller(*comboIsoDepMap);
00281   isoFiller.insert(muons, combinedRelativeIsos.begin(), combinedRelativeIsos.end());
00282   isoFiller.fill();
00283   event.put(comboIsoDepMap);
00284 
00285   if (printDebug) std::cout  <<" END OF EVENT " <<"================================";
00286 }