CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_1/src/DQM/Physics/src/EwkMuDQM.cc

Go to the documentation of this file.
00001 #include "DQM/Physics/src/EwkMuDQM.h"
00002 
00003 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00004 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00005 #include "DataFormats/Common/interface/Handle.h"
00006 
00007 #include "FWCore/ServiceRegistry/interface/Service.h"
00008 #include "CommonTools/UtilAlgos/interface/TFileService.h"
00009 
00010 #include "DQMServices/Core/interface/DQMStore.h"
00011 #include "DQMServices/Core/interface/MonitorElement.h"
00012 
00013 #include "DataFormats/TrackReco/interface/Track.h"
00014 #include "DataFormats/BeamSpot/interface/BeamSpot.h"
00015 #include "DataFormats/VertexReco/interface/Vertex.h"
00016 
00017 #include "DataFormats/MuonReco/interface/Muon.h"
00018 #include "DataFormats/MuonReco/interface/MuonSelectors.h"
00019 #include "DataFormats/METReco/interface/MET.h"
00020 #include "DataFormats/JetReco/interface/Jet.h"
00021 #include "DataFormats/EgammaCandidates/interface/Photon.h" 
00022 
00023 #include "DataFormats/GeometryVector/interface/Phi.h"
00024 
00025 #include "FWCore/Common/interface/TriggerNames.h"
00026 #include "FWCore/Framework/interface/Event.h"
00027 #include "DataFormats/Common/interface/TriggerResults.h"
00028 
00029 #include "DataFormats/Common/interface/View.h"
00030  
00031 #include "HLTrigger/HLTcore/interface/HLTConfigProvider.h"
00032  
00033 using namespace edm;
00034 using namespace std;
00035 using namespace reco;
00036 
00037 EwkMuDQM::EwkMuDQM( const ParameterSet & cfg ) :
00038       // Input collections
00039       trigTag_      (cfg.getUntrackedParameter<edm::InputTag> ("TrigTag", edm::InputTag("TriggerResults::HLT"))),
00040       muonTag_      (cfg.getUntrackedParameter<edm::InputTag> ("MuonTag", edm::InputTag("muons"))),
00041       metTag_       (cfg.getUntrackedParameter<edm::InputTag> ("METTag", edm::InputTag("pfmet"))),
00042       jetTag_       (cfg.getUntrackedParameter<edm::InputTag> ("JetTag", edm::InputTag("ak5PFJets"))),
00043       phoTag_       (cfg.getUntrackedParameter<edm::InputTag> ("phoTag", edm::InputTag("photons"))),
00044       pfPhoTag_     (cfg.getUntrackedParameter<edm::InputTag> ("pfPhoTag", edm::InputTag("pfPhotonTranslator","pfPhot"))),
00045       vertexTag_    (cfg.getUntrackedParameter<edm::InputTag> ("VertexTag", edm::InputTag("offlinePrimaryVertices"))),
00046       trigPathNames_(cfg.getUntrackedParameter<std::vector <std::string> >("TrigPathNames")),           
00047 
00048       // Muon quality cuts
00049       isAlsoTrackerMuon_(cfg.getUntrackedParameter<bool>("IsAlsoTrackerMuon", true)),  // Glb muon also tracker muon 
00050       dxyCut_           (cfg.getUntrackedParameter<double>("DxyCut", 0.2)),            // dxy < 0.2 cm 
00051       normalizedChi2Cut_(cfg.getUntrackedParameter<double>("NormalizedChi2Cut", 10.)), // chi2/ndof (of global fit) <10.0
00052       trackerHitsCut_   (cfg.getUntrackedParameter<int>("TrackerHitsCut", 11)),        // Tracker Hits >10 
00053       pixelHitsCut_     (cfg.getUntrackedParameter<int>("PixelHitsCut", 1)),           // Pixel Hits >0
00054       muonHitsCut_      (cfg.getUntrackedParameter<int>("MuonHitsCut", 1)),            // Valid Muon Hits >0 
00055       nMatchesCut_      (cfg.getUntrackedParameter<int>("NMatchesCut", 2)),            // At least 2 Chambers with matches 
00056 
00057       // W-boson cuts 
00058       isRelativeIso_(cfg.getUntrackedParameter<bool>("IsRelativeIso", true)),
00059       isCombinedIso_(cfg.getUntrackedParameter<bool>("IsCombinedIso", false)),
00060       isoCut03_     (cfg.getUntrackedParameter<double>("IsoCut03", 0.1)),
00061       acopCut_      (cfg.getUntrackedParameter<double>("AcopCut", 999.)),
00062       metMin_       (cfg.getUntrackedParameter<double>("MetMin", -999999.)),
00063       metMax_       (cfg.getUntrackedParameter<double>("MetMax", 999999.)),
00064       mtMin_        (cfg.getUntrackedParameter<double>("MtMin", 50.)),
00065       mtMax_        (cfg.getUntrackedParameter<double>("MtMax", 200.)),
00066       ptCut_        (cfg.getUntrackedParameter<double>("PtCut", 20.)),
00067       etaCut_       (cfg.getUntrackedParameter<double>("EtaCut", 2.4)),
00068 
00069       // Z rejection
00070       ptThrForZ1_  (cfg.getUntrackedParameter<double>("PtThrForZ1", 20.)),
00071       ptThrForZ2_  (cfg.getUntrackedParameter<double>("PtThrForZ2", 10.)),
00072 
00073       // Z selection
00074       dimuonMassMin_(cfg.getUntrackedParameter<double>("dimuonMassMin", 80.)),
00075       dimuonMassMax_(cfg.getUntrackedParameter<double>("dimuonMassMax", 120.)), 
00076 
00077       // Top rejection
00078       eJetMin_     (cfg.getUntrackedParameter<double>("EJetMin", 999999.)),
00079       nJetMax_     (cfg.getUntrackedParameter<int>("NJetMax", 999999)), 
00080 
00081       // Photon cuts 
00082       ptThrForPhoton_(cfg.getUntrackedParameter<double>("ptThrForPhoton",5.)),
00083       nPhoMax_(cfg.getUntrackedParameter<int>("nPhoMax", 999999)) 
00084 {
00085   isValidHltConfig_ = false;
00086 
00087   theDbe = Service<DQMStore>().operator->();
00088   theDbe->setCurrentFolder("Physics/EwkMuDQM");
00089   init_histograms();
00090 
00091 }
00092 
00093 void EwkMuDQM::beginRun(const Run& iRun, const EventSetup& iSet) {
00094       nall = 0;
00095       nsel = 0;
00096       nz   = 0;
00097 
00098       nrec = 0; 
00099       niso = 0; 
00100       nhlt = 0; 
00101       nmet = 0;
00102 
00103      // passed as parameter to HLTConfigProvider::init(), not yet used
00104      bool isConfigChanged = false;
00105      // isValidHltConfig_ used to short-circuit analyze() in case of problems
00106      isValidHltConfig_ = hltConfigProvider_.init( iRun, iSet, "HLT", isConfigChanged );
00107 
00108 }
00109 
00110 void EwkMuDQM::beginJob() {
00111 
00112 }
00113 
00114 void EwkMuDQM::init_histograms() {
00115 
00116   char chtitle[256] = "";
00117 
00118   pt_before_ = theDbe->book1D("PT_BEFORECUTS","Muon transverse momentum (global muon) [GeV]",100,0.,100.);
00119   pt_after_ = theDbe->book1D("PT_AFTERWCUTS","Muon transverse momentum (global muon) [GeV]",100,0.,100.);
00120 
00121   eta_before_ = theDbe->book1D("ETA_BEFORECUTS","Muon pseudo-rapidity",50,-2.5,2.5);
00122   eta_after_ = theDbe->book1D("ETA_AFTERWCUTS","Muon pseudo-rapidity",50,-2.5,2.5);
00123 
00124   dxy_before_ = theDbe->book1D("DXY_BEFORECUTS","Muon transverse distance to beam spot [cm]",1000,-0.5,0.5);
00125   dxy_after_ = theDbe->book1D("DXY_AFTERWCUTS","Muon transverse distance to beam spot [cm]",1000,-0.5,0.5);
00126 
00127   goodewkmuon_before_ = theDbe->book1D("GOODEWKMUON_BEFORECUTS","Quality-muon flag",2,-0.5,1.5);
00128   goodewkmuon_after_ = theDbe->book1D("GOODEWKMUON_AFTERWCUTS","Quality-muon flag",2,-0.5,1.5);
00129 
00130   if (isRelativeIso_) {
00131     if (isCombinedIso_) {
00132       iso_before_ = theDbe->book1D("ISO_BEFORECUTS","Relative (combined) isolation variable",100, 0., 1.);
00133       iso_after_ = theDbe->book1D("ISO_AFTERWCUTS","Relative (combined) isolation variable",100, 0., 1.);
00134     } else {
00135       iso_before_ = theDbe->book1D("ISO_BEFORECUTS","Relative (tracker) isolation variable",100, 0., 1.);
00136       iso_after_ = theDbe->book1D("ISO_AFTERWCUTS","Relative (tracker) isolation variable",100, 0., 1.);
00137     }
00138   } else {
00139     if (isCombinedIso_) {
00140       iso_before_ = theDbe->book1D("ISO_BEFORECUTS","Absolute (combined) isolation variable [GeV]",100, 0., 20.);
00141       iso_after_ = theDbe->book1D("ISO_AFTERWCUTS","Absolute (combined) isolation variable [GeV]",100, 0., 20.);
00142     } else {
00143       iso_before_ = theDbe->book1D("ISO_BEFORECUTS","Absolute (tracker) isolation variable [GeV]",100, 0., 20.);
00144       iso_after_ = theDbe->book1D("ISO_AFTERWCUTS","Absolute (tracker) isolation variable [GeV]",100, 0., 20.);
00145     }
00146   }
00147 
00148   trig_before_ = theDbe->book1D("TRIG_BEFORECUTS","Trigger response (boolean of muon triggers)",2,-0.5,1.5);
00149   trig_after_ = theDbe->book1D("TRIG_AFTERWCUTS","Trigger response (boolean of muon triggers)",2,-0.5,1.5);
00150 
00151   snprintf(chtitle, 255, "Transverse mass (%s) [GeV]", metTag_.label().data());
00152   mt_before_ = theDbe->book1D("MT_BEFORECUTS",chtitle,150,0.,300.);
00153   mt_after_ = theDbe->book1D("MT_AFTERWCUTS",chtitle,150,0.,300.);
00154 
00155   snprintf(chtitle, 255, "Missing transverse energy (%s) [GeV]", metTag_.label().data());
00156   met_before_ = theDbe->book1D("MET_BEFORECUTS",chtitle,100,0.,200.);
00157   met_after_ = theDbe->book1D("MET_AFTERWCUTS",chtitle,100,0.,200.);
00158   met_afterZ_ = theDbe->book1D("MET_AFTERZCUTS",chtitle,100,0.,200.);
00159 
00160   snprintf(chtitle, 255, "MU-MET (%s) acoplanarity", metTag_.label().data());
00161   acop_before_ = theDbe->book1D("ACOP_BEFORECUTS",chtitle,50,0.,M_PI);
00162   acop_after_ = theDbe->book1D("ACOP_AFTERWCUTS",chtitle,50,0.,M_PI);
00163 
00164   /* Clearing space:
00165      snprintf(chtitle, 255, "Z rejection: number of muons above %.2f GeV", ptThrForZ1_);
00166      nz1_before_ = theDbe->book1D("NZ1_BEFORECUTS",chtitle,10,-0.5,9.5);
00167      nz1_after_ = theDbe->book1D("NZ1_AFTERWCUTS",chtitle,10,-0.5,9.5);
00168 
00169      snprintf(chtitle, 255, "Z rejection: number of muons above %.2f GeV", ptThrForZ2_);
00170      nz2_before_ = theDbe->book1D("NZ2_BEFORECUTS",chtitle,10,-0.5,9.5);
00171      nz2_after_ = theDbe->book1D("NZ2_AFTERWCUTS",chtitle,10,-0.5,9.5);
00172   */
00173 
00174   snprintf(chtitle, 255, "Number of jets (%s) above %.2f GeV", jetTag_.label().data(), eJetMin_);
00175   njets_before_ = theDbe->book1D("NJETS_BEFORECUTS",chtitle,10,-0.5,9.5);
00176   njets_after_ = theDbe->book1D("NJETS_AFTERWCUTS",chtitle,10,-0.5,9.5);
00177   njets_afterZ_ = theDbe->book1D("NJETS_AFTERZCUTS",chtitle,10,-0.5,9.5);
00178 
00179   leadingjet_pt_before_ = theDbe->book1D("LEADINGJET_PT_BEFORECUTS","Leading Jet transverse momentum",300,0.,300.);
00180   leadingjet_pt_after_ = theDbe->book1D("LEADINGJET_PT_AFTERWCUTS","Leading Jet transverse momentum",300,0.,300.);
00181   leadingjet_pt_afterZ_ = theDbe->book1D("LEADINGJET_PT_AFTERZCUTS","Leading Jet transverse momentum",300,0.,300.);
00182 
00183   leadingjet_eta_before_ = theDbe->book1D("LEADINGJET_ETA_BEFORECUTS","Leading Jet pseudo-rapidity",50,-2.5,2.5);
00184   leadingjet_eta_after_ = theDbe->book1D("LEADINGJET_ETA_AFTERWCUTS","Leading Jet pseudo-rapidity",50,-2.5,2.5);
00185   leadingjet_eta_afterZ_ = theDbe->book1D("LEADINGJET_ETA_AFTERZCUTS","Leading Jet pseudo-rapidity",50,-2.5,2.5);
00186 
00187 
00190   pt1_afterZ_ = theDbe->book1D("PT1_AFTERZCUTS","Muon transverse momentum (global muon) [GeV]",100,0.,100.);
00191   eta1_afterZ_ = theDbe->book1D("ETA1_AFTERZCUTS","Muon pseudo-rapidity",50,-2.5,2.5);
00192   dxy1_afterZ_ = theDbe->book1D("DXY1_AFTERZCUTS","Muon transverse distance to beam spot [cm]",1000,-0.5,0.5);
00193   goodewkmuon1_afterZ_ = theDbe->book1D("GOODEWKMUON1_AFTERZCUTS","Quality-muon flag",2,-0.5,1.5);
00194 
00195   if (isRelativeIso_) {
00196     if (isCombinedIso_) {
00197       iso1_afterZ_ = theDbe->book1D("ISO1_AFTERZCUTS","Relative (combined) isolation variable",100, 0., 1.);
00198       iso2_afterZ_ = theDbe->book1D("ISO2_AFTERZCUTS","Relative (combined) isolation variable",100, 0., 1.);
00199     } else {
00200       iso1_afterZ_ = theDbe->book1D("ISO1_AFTERZCUTS","Relative (tracker) isolation variable",100, 0., 1.);
00201       iso2_afterZ_ = theDbe->book1D("ISO2_AFTERZCUTS","Relative (tracker) isolation variable",100, 0., 1.);
00202     }
00203   } else {
00204     if (isCombinedIso_) {
00205       iso1_afterZ_ = theDbe->book1D("ISO1_AFTERZCUTS","Absolute (combined) isolation variable [GeV]",100, 0., 20.);
00206       iso2_afterZ_ = theDbe->book1D("ISO2_AFTERZCUTS","Absolute (combined) isolation variable [GeV]",100, 0., 20.);
00207     } else {
00208       iso1_afterZ_ = theDbe->book1D("ISO1_AFTERZCUTS","Absolute (tracker) isolation variable [GeV]",100, 0., 20.);
00209       iso2_afterZ_ = theDbe->book1D("ISO2_AFTERZCUTS","Absolute (tracker) isolation variable [GeV]",100, 0., 20.);
00210     }
00211   }
00212 
00213   pt2_afterZ_ = theDbe->book1D("PT2_AFTERZCUTS","Muon transverse momentum (global muon) [GeV]",100,0.,100.);
00214   eta2_afterZ_ = theDbe->book1D("ETA2_AFTERZCUTS","Muon pseudo-rapidity",50,-2.5,2.5);
00215   dxy2_afterZ_ = theDbe->book1D("DXY2_AFTERZCUTS","Muon transverse distance to beam spot [cm]",1000,-0.5,0.5);
00216   goodewkmuon2_afterZ_ = theDbe->book1D("GOODEWKMUON2_AFTERZCUTS","Quality-muon flag",2,-0.5,1.5);
00217   ztrig_afterZ_ = theDbe->book1D("ZTRIG_AFTERZCUTS","Trigger response (boolean of muon triggers)",2,-0.5,1.5); 
00218   dimuonmass_before_= theDbe->book1D("DIMUONMASS_BEFORECUTS","DiMuonMass (2 globals)",100,0,200);
00219   dimuonmass_afterZ_= theDbe->book1D("DIMUONMASS_AFTERZCUTS","DiMuonMass (2 globals)",100,0,200);
00220   npvs_before_ = theDbe->book1D("NPVs_BEFORECUTS","Number of Valid Primary Vertices",30,-0.5,29.5);
00221   npvs_after_ = theDbe->book1D("NPVs_AFTERZCUTS","Number of Valid Primary Vertices",30,-0.5,29.5);
00222   npvs_afterZ_ = theDbe->book1D("NPVs_AFTERWCUTS","Number of Valid Primary Vertices",30,-0.5,29.5);
00223   muoncharge_before_ = theDbe->book1D("MUONCHARGE_BEFORECUTS","Muon Charge",3,-1.5,1.5);
00224   muoncharge_after_ = theDbe->book1D("MUONCHARGE_AFTERZCUTS","Muon Charge",3,-1.5,1.5);
00225   muoncharge_afterZ_ = theDbe->book1D("MUONCHARGE_AFTERWCUTS","Muon Charge",3,-1.5,1.5);
00226 
00227   // Adding these to replace the NZ ones (more useful, since they are more general?)     
00228   nmuons_ = theDbe->book1D("NMuons","Number of muons in the event",10,-0.5,9.5);
00229   ngoodmuons_ = theDbe->book1D("NGoodMuons","Number of muons passing the quality criteria",10,-0.5,9.5);
00230 
00231   nph_ = theDbe->book1D("nph","Number of photons in the event",20,0.,20.); 
00232   npfph_ = theDbe->book1D("npfph","Number of PF photons in the event",20,0.,20.); 
00233   phPt_ = theDbe->book1D("phPt","Photon transverse momentum [GeV]",1000,0.,1000.);
00234   pfphPt_ = theDbe->book1D("pfphPt","PF Photon transverse momentum [GeV]",1000,0.,1000.); 
00235   phEta_ = theDbe->book1D("phEta","Photon pseudorapidity",100,-2.5,2.5); 
00236   pfphEta_ = theDbe->book1D("pfphEta","PF Photon pseudorapidity",100,-2.5,2.5); 
00237 
00238 }
00239 
00240 
00241 void EwkMuDQM::endJob() {
00242 }
00243 
00244 void EwkMuDQM::endRun(const Run& r, const EventSetup& iSet) {
00245 
00246 }
00247 
00248 void EwkMuDQM::analyze (const Event & ev, const EventSetup & iSet) {
00249       
00250       // Muon collection
00251       Handle<View<Muon> > muonCollection;
00252       if (!ev.getByLabel(muonTag_, muonCollection)) {
00253         //LogWarning("") << ">>> Muon collection does not exist !!!";
00254         return;
00255       }
00256       unsigned int muonCollectionSize = muonCollection->size();
00257 
00258       // Beam spot
00259       Handle<reco::BeamSpot> beamSpotHandle;
00260       if (!ev.getByLabel(InputTag("offlineBeamSpot"), beamSpotHandle)) {
00261         //LogWarning("") << ">>> No beam spot found !!!";
00262         return;
00263       }
00264 
00265 
00266       // Loop to reject/control Z->mumu is done separately
00267       unsigned int nmuonsForZ1 = 0;
00268       unsigned int nmuonsForZ2 = 0;
00269       bool cosmic = false;
00270       for (unsigned int i=0; i<muonCollectionSize; i++) {
00271             const Muon& mu = muonCollection->at(i);
00272             if (!mu.isGlobalMuon()) continue;
00273             double pt = mu.pt();
00274             double dxy = mu.innerTrack()->dxy(beamSpotHandle->position());
00275 
00276             if (fabs(dxy)>1) { cosmic=true; break;} 
00277 
00278             if (pt>ptThrForZ1_) nmuonsForZ1++;
00279             if (pt>ptThrForZ2_) nmuonsForZ2++;
00280 
00281             for (unsigned int j=i+1; j<muonCollectionSize; j++) {
00282                  const Muon& mu2 = muonCollection->at(j);
00283                  if (mu2.isGlobalMuon() && (mu.charge()*mu2.charge()==-1) ){
00284                          const math::XYZTLorentzVector ZRecoGlb (mu.px()+mu2.px(), mu.py()+mu2.py() , mu.pz()+mu2.pz(), mu.p()+mu2.p());
00285                          dimuonmass_before_->Fill(ZRecoGlb.mass());
00286                  }
00287             }
00288       }
00289      if(cosmic) return;
00290 
00291       LogTrace("") << "> Z rejection: muons above " << ptThrForZ1_ << " [GeV]: " << nmuonsForZ1;
00292       LogTrace("") << "> Z rejection: muons above " << ptThrForZ2_ << " [GeV]: " << nmuonsForZ2;
00293       /*nz1_before_->Fill(nmuonsForZ1);
00294       nz2_before_->Fill(nmuonsForZ2);
00295       */
00296 
00297       // MET
00298       Handle<View<MET> > metCollection;
00299       if (!ev.getByLabel(metTag_, metCollection)) {
00300         //LogWarning("") << ">>> MET collection does not exist !!!";
00301         return;
00302       }
00303       const MET& met = metCollection->at(0);
00304       double met_et = met.pt();
00305       LogTrace("") << ">>> MET, MET_px, MET_py: " << met_et << ", " << met.px() << ", " << met.py() << " [GeV]";
00306       met_before_->Fill(met_et);
00307 
00308       // Vertices in the event
00309       Handle<View<reco::Vertex> > vertexCollection;
00310            if (!ev.getByLabel(vertexTag_, vertexCollection)) {
00311                  LogError("") << ">>> Vertex collection does not exist !!!";
00312                  return;
00313             }
00314       unsigned int vertexCollectionSize = vertexCollection->size();
00315 
00316       
00317 
00318       int nvvertex = 0;
00319       for (unsigned int i=0; i<vertexCollectionSize; i++) {
00320             const Vertex& vertex = vertexCollection->at(i);
00321             if (vertex.isValid()) nvvertex++;
00322       }
00323 
00324       npvs_before_->Fill(nvvertex);
00325 
00326       bool trigger_fired = false;
00327       Handle<TriggerResults> triggerResults;
00328       if (!ev.getByLabel(trigTag_, triggerResults)) {
00329         //LogWarning("") << ">>> TRIGGER collection does not exist !!!";
00330         return;
00331       }
00332       const edm::TriggerNames & trigNames = ev.triggerNames(*triggerResults);
00333       //  LogWarning("")<<"Loop over triggers";
00334 
00335 
00336       for (unsigned int i=0; i<triggerResults->size(); i++)
00337       {
00338               const std::string trigName = trigNames.triggerName(i);
00339 
00340               bool found=false; 
00341               for(unsigned int index=0; index<trigPathNames_.size() && found==false; index++) {
00342                    size_t trigPath = trigName.find(trigPathNames_[index]); // 0 if found, pos if not
00343                    if (trigPath==0) found=true;
00344               }
00345               if(!found) {continue;}
00346               
00347               bool prescaled=false;    
00348               for (unsigned int ps= 0; ps<  hltConfigProvider_.prescaleSize(); ps++){
00349                   const unsigned int prescaleValue = hltConfigProvider_.prescaleValue(ps, trigName) ;
00350                   if (prescaleValue != 1) prescaled =true;
00351               }
00352             
00353               if( triggerResults->accept(i) && !prescaled){   trigger_fired=true;}
00354                         // LogWarning("")<<"TrigNo: "<<i<<"  "<<found<<"  "<<trigName<<" ---> FIRED";}
00355       }     
00356       trig_before_->Fill(trigger_fired);
00357 
00358       // Jet collection
00359       Handle<View<Jet> > jetCollection;
00360       if (!ev.getByLabel(jetTag_, jetCollection)) {
00361         //LogError("") << ">>> JET collection does not exist !!!";
00362         return;
00363       }
00364       unsigned int jetCollectionSize = jetCollection->size();
00365       int njets = 0; int LEADJET=-1;  double max_pt=0;
00366       for (unsigned int i=0; i<jetCollectionSize; i++) {
00367             const Jet& jet = jetCollection->at(i);
00368                   double minDistance=99999; // This is in order to use PFJets
00369                   for (unsigned int j=0; j<muonCollectionSize; j++) {
00370                         const Muon& mu = muonCollection->at(j);
00371                         double distance = sqrt( (mu.eta()-jet.eta())*(mu.eta()-jet.eta()) +(mu.phi()-jet.phi())*(mu.phi()-jet.phi()) );      
00372                         if (minDistance>distance) minDistance=distance;
00373                   }
00374                   if (minDistance<0.3) continue; // 0.3 is the isolation cone around the muon
00375             if(jet.et()>max_pt) { LEADJET=i; max_pt=jet.et();}
00376             if (jet.et()>eJetMin_) {njets++;}
00377       }
00378 
00379 
00380       LogTrace("") << ">>> Total number of jets: " << jetCollectionSize;
00381       LogTrace("") << ">>> Number of jets above " << eJetMin_ << " [GeV]: " << njets;
00382       njets_before_->Fill(njets);
00383       double lead_jet_pt=-1;
00384       double lead_jet_eta=-100;
00385       if(LEADJET!=-1){
00386       const Jet& leadJet = jetCollection->at(LEADJET);
00387       leadingjet_pt_before_->Fill(leadJet.pt());
00388       leadingjet_eta_before_->Fill(leadJet.eta());
00389       lead_jet_pt=leadJet.pt();
00390       lead_jet_eta=leadJet.eta();
00391       }
00392       //Photon Collection
00393       Handle<View<Photon> > photonCollection;
00394       if(!ev.getByLabel(phoTag_,photonCollection)){
00395       //LogError("")
00396       return;
00397       }
00398       unsigned int ngam=0;
00399       
00400       for (unsigned int i=0; i<photonCollection->size(); i++){
00401         const Photon &ph = photonCollection->at(i);
00402         double photonPt = ph.pt();
00403         if (photonPt> ptThrForPhoton_) ngam++;
00404         phPt_->Fill(photonPt); 
00405         }
00406       nph_->Fill(ngam); 
00407       LogTrace("") << " >>> N photons " << ngam << std::endl;
00408 
00409       nmuons_->Fill(muonCollectionSize);
00410 
00412       //Handle<View<Photon> > pfPhotonCollection;
00414       //if(!ev.getByLabel(pfPhoTag_,pfPhotonCollection)){
00416       //return;
00417       //}
00418       //unsigned int npfgam=0;
00419       //
00420       //for (unsigned int i=0; i<pfPhotonCollection->size(); i++){
00421       //        const Photon &ph = pfPhotonCollection->at(i);
00422       //        double photonPt = ph.pt();
00423       //        if (photonPt> ptThrForPhoton_) npfgam++;
00424       //  pfphPt_->Fill(photonPt); 
00425       //        }
00426       //npfph_->Fill(npfgam); 
00427       //LogTrace("") << " >>> N PF photons " << npfgam << std::endl;
00428 
00429       // Start counting
00430       nall++;
00431 
00432       // Histograms per event should be done only once, so keep track of them
00433       bool hlt_hist_done = false;
00434       bool zhlt_hist_done = false;
00435       bool zjets_hist_done = false;
00436       bool zfullsel_hist_done = false;
00437       bool met_hist_done = false;
00438       //bool nz1_hist_done = false;
00439       //bool nz2_hist_done = false;
00440       bool njets_hist_done = false;
00441       bool wfullsel_hist_done = false;
00442 
00443       // Central W->mu nu selection criteria
00444       const int NFLAGS = 11;
00445       bool muon_sel[NFLAGS];
00446       const int NFLAGSZ = 13;
00447       bool zmuon_sel[NFLAGSZ];
00448       bool muon4Z=false;
00449 
00450       double number_of_muons=0;
00451       double number_of_goodMuons=0;
00452 
00453 
00454       for (unsigned int i=0; i<muonCollectionSize; i++) {
00455             for (int j=0; j<NFLAGS; ++j) {
00456                   muon_sel[j] = false;
00457             }
00458 
00459             number_of_muons++;
00460 
00461             const Muon& mu = muonCollection->at(i);
00462             if (!mu.isGlobalMuon()) continue;
00463             if (mu.globalTrack().isNull()) continue;
00464             if (mu.innerTrack().isNull()) continue;
00465 
00466             LogTrace("") << "> Wsel: processing muon number " << i << "...";
00467             reco::TrackRef gm = mu.globalTrack();
00468             reco::TrackRef tk = mu.innerTrack();
00469 
00470             // Pt,eta cuts
00471             double pt = mu.pt();
00472             double eta = mu.eta();
00473             LogTrace("") << "\t... pt, eta: " << pt << " [GeV], " << eta;;
00474             if (pt>ptCut_) muon_sel[0] = true; 
00475             if (fabs(eta)<etaCut_) muon_sel[1] = true; 
00476 
00477             double charge=mu.charge();
00478 
00479             // d0, chi2, nhits quality cuts
00480             double dxy = gm->dxy(beamSpotHandle->position());
00481             double normalizedChi2 = gm->normalizedChi2();
00482             double trackerHits = tk->hitPattern().numberOfValidTrackerHits();
00483             int pixelHits = tk->hitPattern().numberOfValidPixelHits();
00484             int muonHits = gm->hitPattern().numberOfValidMuonHits();
00485             int nMatches = mu.numberOfMatches();
00486 
00487             LogTrace("") << "\t... dxy, normalizedChi2, trackerHits, isTrackerMuon?: " << dxy << " [cm], " << normalizedChi2 << ", " << trackerHits << ", " << mu.isTrackerMuon();
00488             if (fabs(dxy)<dxyCut_) muon_sel[2] = true; 
00489 
00490             bool quality=true;
00491             
00492             if (normalizedChi2>normalizedChi2Cut_) quality =false; 
00493             if (trackerHits<trackerHitsCut_) quality =false;
00494             if (pixelHits<pixelHitsCut_) quality =false;
00495             if (muonHits<muonHitsCut_) quality=false;;
00496             if (!mu.isTrackerMuon()) quality=false;
00497             if (nMatches<nMatchesCut_) quality=false;
00498             muon_sel[3]=quality;
00499             if(quality) number_of_goodMuons++;
00500 
00501             pt_before_->Fill(pt);
00502             eta_before_->Fill(eta);
00503             dxy_before_->Fill(dxy);
00504             muoncharge_before_->Fill(charge);
00505             goodewkmuon_before_->Fill(quality);
00506 
00507             // Isolation cuts
00508             double isovar = mu.isolationR03().sumPt;
00509             if (isCombinedIso_) {
00510                   isovar += mu.isolationR03().emEt;
00511                   isovar += mu.isolationR03().hadEt;
00512             }
00513             if (isRelativeIso_) isovar /= pt;
00514             if (isovar<isoCut03_) muon_sel[4] = true; 
00515 
00516             LogTrace("") << "\t... isolation value" << isovar <<", isolated? " << muon_sel[6];
00517             iso_before_->Fill(isovar);
00518 
00519 
00520             // HLT (not mtched to muon for the time being)
00521             if (trigger_fired) muon_sel[5] = true; 
00522 
00523             // For Z:
00524             if (pt>ptThrForZ1_ && fabs(eta)<etaCut_ && fabs(dxy)<dxyCut_ && quality && trigger_fired && isovar<isoCut03_) { muon4Z = true;}
00525 
00526 
00527             // MET/MT cuts
00528             double w_et = met_et+mu.pt();
00529             double w_px = met.px()+mu.px();
00530             double w_py = met.py()+mu.py();
00531             
00532             double massT = w_et*w_et - w_px*w_px - w_py*w_py;
00533             massT = (massT>0) ? sqrt(massT) : 0;
00534 
00535             LogTrace("") << "\t... W mass, W_et, W_px, W_py: " << massT << ", " << w_et << ", " << w_px << ", " << w_py << " [GeV]";
00536             if (massT>mtMin_ && massT<mtMax_) muon_sel[6] = true; 
00537             mt_before_->Fill(massT);
00538             if (met_et>metMin_ && met_et<metMax_) muon_sel[7] = true; 
00539 
00540             // Acoplanarity cuts
00541             Geom::Phi<double> deltaphi(mu.phi()-atan2(met.py(),met.px()));
00542             double acop = deltaphi.value();
00543             if (acop<0) acop = - acop;
00544             acop = M_PI - acop;
00545             LogTrace("") << "\t... acoplanarity: " << acop;
00546             if (acop<acopCut_) muon_sel[8] = true; 
00547             acop_before_->Fill(acop);
00548 
00549             // Remaining flags (from global event information)
00550             if (nmuonsForZ1<1 || nmuonsForZ2<2) muon_sel[9] = true; 
00551             if (njets<=nJetMax_) muon_sel[10] = true; 
00552 
00553             // Collect necessary flags "per muon"
00554             int flags_passed = 0;
00555             for (int j=0; j<NFLAGS; ++j) {
00556               if (muon_sel[j]) flags_passed += 1;
00557             }
00558 
00559             // Do N-1 histograms now (and only once for global event quantities)
00560             if (flags_passed >= (NFLAGS-1)) {
00561                   if (!muon_sel[0] || flags_passed==NFLAGS) 
00562                         pt_after_->Fill(pt);
00563                   if (!muon_sel[1] || flags_passed==NFLAGS) 
00564                         eta_after_->Fill(eta);
00565                   if (!muon_sel[2] || flags_passed==NFLAGS) 
00566                         dxy_after_->Fill(dxy);
00567                   if (!muon_sel[3] || flags_passed==NFLAGS)
00568                         goodewkmuon_after_->Fill(quality);
00569                   if (!muon_sel[4] || flags_passed==NFLAGS) 
00570                         iso_after_->Fill(isovar);
00571                   if (!muon_sel[5] || flags_passed==NFLAGS) 
00572                         if (!hlt_hist_done) trig_after_->Fill(trigger_fired);
00573                         hlt_hist_done = true;
00574                   if (!muon_sel[6] || flags_passed==NFLAGS) 
00575                         mt_after_->Fill(massT);
00576                   if (!muon_sel[7] || flags_passed==NFLAGS) 
00577                         if (!met_hist_done) met_after_->Fill(met_et);
00578                         met_hist_done = true;
00579                   if (!muon_sel[8] || flags_passed==NFLAGS) 
00580                         acop_after_->Fill(acop);
00581                   /* Clearing some space
00582                   if (!muon_sel[9] || flags_passed==NFLAGS) 
00583                         if (!nz1_hist_done) nz1_after_->Fill(nmuonsForZ1);
00584                         nz1_hist_done = true;
00585                   if (!muon_sel[9] || flags_passed==NFLAGS) 
00586                         if (!nz2_hist_done) nz2_after_->Fill(nmuonsForZ2);
00587                         nz2_hist_done = true;
00588                   */
00589                   if (!muon_sel[10] || flags_passed==NFLAGS) { 
00590                         if (!njets_hist_done) {
00591                                     njets_after_->Fill(njets);
00592                                     leadingjet_pt_after_->Fill(lead_jet_pt);
00593                                     leadingjet_eta_after_->Fill(lead_jet_eta);
00594                         }
00595                         njets_hist_done = true;
00596                   if( flags_passed==NFLAGS ) {
00597                         if (!wfullsel_hist_done){
00598                         npvs_after_->Fill(nvvertex);
00599                         muoncharge_after_->Fill(charge);
00600                         }
00601                         wfullsel_hist_done=true;    
00602                   } 
00603                    
00604             }
00605 
00606 
00607             // The cases in which the event is rejected as a Z are considered independently:
00608             if ( muon4Z &&  !muon_sel[9]){
00609 
00610                    // Plots for 2 muons       
00611                    for (unsigned int j=i+1; j<muonCollectionSize; j++) {
00612 
00613                          for (int ij=0; ij<NFLAGSZ; ++ij) {
00614                                zmuon_sel[ij] = false;
00615                          }
00616 
00617                          for (int ji=0; ji<5; ++ji ) {
00618                            zmuon_sel[ji] = muon_sel[ji];
00619                          }
00620 
00621                          const Muon& mu2 = muonCollection->at(j);
00622                               if (!mu2.isGlobalMuon()) continue;
00623                               if (mu2.charge() * charge != -1 ) continue;
00624                                     reco::TrackRef gm2 = mu2.globalTrack();
00625                                     reco::TrackRef tk2 = mu2.innerTrack();
00626                                     double pt2 = mu2.pt(); if (pt2>ptThrForZ1_) zmuon_sel[5] = true;
00627                                     double eta2=mu2.eta(); if (fabs(eta2)<etaCut_) zmuon_sel[6] = true;
00628                                     double dxy2 = gm2->dxy(beamSpotHandle->position()); if (fabs(dxy2)<dxyCut_) zmuon_sel[7] = true;
00629                                     double normalizedChi22 = gm2->normalizedChi2();
00630                                     double trackerHits2    = tk2->hitPattern().numberOfValidTrackerHits();
00631                                     int    pixelHits2      = tk2->hitPattern().numberOfValidPixelHits();
00632                                     int    muonHits2       = gm2->hitPattern().numberOfValidMuonHits();
00633                                     int    nMatches2       = mu2.numberOfMatches();
00634                                     bool quality2=true;
00635                                     if (normalizedChi22>normalizedChi2Cut_) quality2 = false; 
00636                                     if (trackerHits2<trackerHitsCut_)       quality2 = false;
00637                                     if (pixelHits2<pixelHitsCut_)           quality2 = false;
00638                                     if (muonHits2<muonHitsCut_)             quality2 = false;
00639                                     if (!mu2.isTrackerMuon())               quality2 = false;
00640                                     if (nMatches2<nMatchesCut_)             quality2 = false;
00641                                     zmuon_sel[8]=quality2;
00642                                     double isovar2 = mu2.isolationR03().sumPt; 
00643                                     if (isCombinedIso_) {
00644                                           isovar2 += mu2.isolationR03().emEt;
00645                                           isovar2 += mu2.isolationR03().hadEt;
00646                                     }
00647                                     if (isRelativeIso_) isovar2 /= pt2;
00648                                     if (isovar2<isoCut03_) zmuon_sel[9] = true;
00649                                     if (trigger_fired) zmuon_sel[10] = true; 
00650                                const math::XYZTLorentzVector ZRecoGlb (mu.px()+mu2.px(), mu.py()+mu2.py() , mu.pz()+mu2.pz(), mu.p()+mu2.p());
00651                                if (ZRecoGlb.mass()>dimuonMassMin_ && ZRecoGlb.mass()<dimuonMassMax_) zmuon_sel[11] = true; 
00652 
00653                                         //jet flag
00654                                         if (njets <=nJetMax_) zmuon_sel[12] = true; 
00655 
00656                                // start filling histos: N-1 plots
00657                                int  flags_passed_z = 0;
00658 
00659                                for (int jj=0; jj<NFLAGSZ; ++jj) {
00660                                  if (zmuon_sel[jj]) ++flags_passed_z ;
00661                                }
00662 
00663                                if (flags_passed_z >= (NFLAGSZ-1)) {
00664                                        if (!zmuon_sel[0]  || flags_passed_z==NFLAGSZ) {pt1_afterZ_->Fill(pt); } 
00665                                        if (!zmuon_sel[1]  || flags_passed_z==NFLAGSZ) {eta1_afterZ_->Fill(eta); }  
00666                                        if (!zmuon_sel[2]  || flags_passed_z==NFLAGSZ) {dxy1_afterZ_->Fill(dxy); }  
00667                                        if (!zmuon_sel[3]  || flags_passed_z==NFLAGSZ) {goodewkmuon1_afterZ_->Fill(quality); }  
00668                                        if (!zmuon_sel[4]  || flags_passed_z==NFLAGSZ) {iso1_afterZ_->Fill(isovar); }  
00669                                        if (!zmuon_sel[5]  || flags_passed_z==NFLAGSZ) { pt2_afterZ_->Fill(pt2); }  
00670                                        if (!zmuon_sel[6]  || flags_passed_z==NFLAGSZ) { eta2_afterZ_->Fill(eta2); }  
00671                                        if (!zmuon_sel[7]  || flags_passed_z==NFLAGSZ) {dxy2_afterZ_->Fill(dxy2); }  
00672                                        if (!zmuon_sel[8]  || flags_passed_z==NFLAGSZ) {goodewkmuon2_afterZ_->Fill(quality2); }  
00673                                        if (!zmuon_sel[9]  || flags_passed_z==NFLAGSZ) {iso2_afterZ_->Fill(isovar2); }   
00674                                        if (!zmuon_sel[10] || flags_passed_z==NFLAGSZ) { 
00675                                          if (!zhlt_hist_done) ztrig_afterZ_->Fill(trigger_fired); 
00676                                           zhlt_hist_done = true; 
00677                                        }  
00678                                        if (!zmuon_sel[11] || flags_passed_z==NFLAGSZ) {dimuonmass_afterZ_->Fill(ZRecoGlb.mass()); } 
00679                                                  if (!zmuon_sel[12] || flags_passed_z==NFLAGSZ ){
00680                                                             if(!zjets_hist_done){
00681                                                             njets_afterZ_->Fill(njets);
00682                                                             leadingjet_pt_afterZ_->Fill(lead_jet_pt);
00683                                                             leadingjet_eta_afterZ_->Fill(lead_jet_eta);
00684                                                             }
00685                                                             zjets_hist_done=true;
00686                                     }
00687                                if(flags_passed_z==NFLAGSZ) {met_afterZ_->Fill(met_et);
00688                                           if(!zfullsel_hist_done){
00689                                           npvs_after_->Fill(nvvertex);
00690                                           muoncharge_after_->Fill(charge);
00691                                           }
00692                                           zfullsel_hist_done=true;
00693                                }     
00694                   }
00695 
00696                                }
00697 
00698                   }
00699             }
00700 
00701       }
00702 
00703       //nmuons_->Fill(number_of_muons);
00704       //nmuons_->Fill(muonCollectionSize);
00705       ngoodmuons_->Fill(number_of_goodMuons);
00706 
00707       return;
00708 
00709 }
00710