CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_7/src/Validation/MuonRPCGeometry/plugins/RPCPhiEff.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:    RPCPhiEff
00004 // Class:      RPCPhiEff
00005 // 
00013 //
00014 // Original Author:  Tomasz Fruboes
00015 //         Created:  Wed Mar  7 08:31:57 CET 2007
00016 // $Id: RPCPhiEff.cc,v 1.5 2011/12/05 22:29:19 gowdy Exp $
00017 //
00018 //
00019 
00020 
00021 // system include files
00022 #include <memory>
00023 
00024 // user include files
00025 #include "FWCore/Framework/interface/Frameworkfwd.h"
00026 #include "FWCore/Framework/interface/EDAnalyzer.h"
00027 
00028 #include "FWCore/Framework/interface/Event.h"
00029 
00030 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00031 
00032 #include <SimDataFormats/Track/interface/SimTrackContainer.h>
00033 
00034 #include "DataFormats/MuonDetId/interface/RPCDetId.h"
00035 #include "DataFormats/RPCDigi/interface/RPCDigiCollection.h"
00036 
00037 #include <L1Trigger/RPCTrigger/interface/RPCLogCone.h>
00038 #include <L1Trigger/RPCTrigger/interface/RPCConst.h>
00039 
00040 #include "DataFormats/L1GlobalMuonTrigger/interface/L1MuRegionalCand.h"
00041 
00042 #include "DataFormats/Math/interface/LorentzVector.h"
00043 #include "Validation/MuonRPCGeometry/plugins/RPCPhiEff.h"
00044 
00045 #include <iostream>
00046 #include <set>
00047 #include <fstream>
00048 #include <sstream>
00049 
00050 #include "L1Trigger/RPCTrigger/interface/RPCConst.h"
00051 
00052 #include "DataFormats/Math/interface/deltaR.h"
00053 
00054 //
00055 // constants, enums and typedefs
00056 //
00057 
00058 //
00059 // static data member definitions
00060 //
00061 
00062 //
00063 // constructors and destructor
00064 //
00065 RPCPhiEff::RPCPhiEff(const edm::ParameterSet & ps) :
00066       m_rpcb( ps.getParameter< edm::InputTag >("rpcb") ),
00067       m_rpcf( ps.getParameter< edm::InputTag >("rpcf") ),
00068       m_g4( ps.getParameter< edm::InputTag >("g4") ),
00069       m_rpcdigi( ps.getParameter< edm::InputTag >("rpcdigi") )
00070 {
00071 
00072 
00073     //m_outfileC.open("phieffC.txt");
00074     m_outfileR.open("muons.txt");
00075 
00076 }
00077 
00078 
00079 RPCPhiEff::~RPCPhiEff()
00080 {
00081 
00082     //m_outfileC.close();
00083     m_outfileR.close();
00084 
00085 }
00086 
00087 
00088 //
00089 // member functions
00090 //
00091 
00092 // ------------ method called to for each event  ------------
00093 void
00094  RPCPhiEff::analyze(const edm::Event & iEvent,
00095                     const edm::EventSetup & iSetup)
00096 {
00097 
00098 
00099     edm::Handle<std::vector<L1MuRegionalCand> > rpcBarrel;
00100     edm::Handle<std::vector<L1MuRegionalCand> > rpcForward;
00101   //iEvent.getByLabel("rpctrig","RPCb",rpcBarrel);
00102   //iEvent.getByLabel("rpctrig","RPCf",rpcForward);
00103 
00104     iEvent.getByLabel(m_rpcb,rpcBarrel);
00105     iEvent.getByLabel(m_rpcf,rpcForward);
00106 
00107 
00108     std::vector< edm::Handle<std::vector<L1MuRegionalCand> >  > handleVec;
00109     handleVec.push_back( rpcBarrel );  
00110     handleVec.push_back( rpcForward );  
00111   
00112   // Fetch MCdata
00113     edm::Handle<edm::SimTrackContainer> simTracks;
00114     iEvent.getByLabel(m_g4,simTracks);
00115 
00116     float etaGen = 0, phiGen = 0, ptGen = 0;
00117  
00118     int noOfMuons = 0;
00119     int noOfRecMuons = 0;
00120     int noOfMatchedRecMuons = 0; 
00121 
00122     bool firstRunForMuonMatchingCnt = true;
00123 
00124     // ask MC muons to be separated
00125     double deltarMin = -1;
00126     edm::SimTrackContainer::const_iterator simTrk = simTracks->begin();
00127     for (; simTrk != simTracks->end(); ++simTrk) { 
00128 
00129       if (simTrk->type() != -13 && simTrk->type()!=13) continue;
00130 
00131       edm::SimTrackContainer::const_iterator simTrk2 = simTrk;
00132       ++simTrk2;
00133       for (; simTrk2 != simTracks->end(); ++simTrk2) { 
00134         if (simTrk2->type() != -13 && simTrk2->type()!=13) continue;
00135         double drCand = reco::deltaR(simTrk2->momentum(), simTrk->momentum());
00136         if (drCand < deltarMin || deltarMin < 0) deltarMin = drCand;
00137       }
00138 
00139 
00140     }
00141 
00142     //std::cout << deltarMin << std::endl;
00143     if (deltarMin < 0.7 &&  deltarMin > 0) return;
00144 
00145     simTrk = simTracks->begin();
00146     for (; simTrk != simTracks->end(); simTrk++) {
00147         int type = simTrk->type();
00148         if (type == 13 || type == -13) {
00149             // Get the data
00150             const math::XYZTLorentzVectorD momentum = simTrk->momentum();
00151             etaGen = momentum.eta();
00152             ptGen = momentum.Pt();
00153             phiGen = momentum.phi();
00154             noOfMuons++;
00155 
00156             bool matched = false;
00157             int ptCodeRec = 0;
00158             int towerRec = 0;
00159             int phiRec = 0;
00160             //int muonsFound=0;
00161             int qual = 0;
00162             int ghost = 0; // number of ghost for montecarlo muon
00163 
00164             // Iter rpc muon cands, perform delta R matching
00165             // todo perform matching also using eta...
00166             for (  std::vector< edm::Handle<std::vector<L1MuRegionalCand> >  >::iterator it = handleVec.begin();
00167                    it != handleVec.end();
00168                    ++it  ) 
00169             {  
00170                std::vector<L1MuRegionalCand>::const_iterator itRPC;
00171                for (itRPC=(*it)->begin(); itRPC!=(*it)->end(); itRPC++){
00172                   int ptCode =  itRPC->pt_packed();
00173                   if (ptCode != 0) {
00174 
00175                      if (firstRunForMuonMatchingCnt) ++noOfRecMuons;
00176                      ptCodeRec=ptCode;
00177                      phiRec=itRPC->phi_packed();
00178                      qual = itRPC->quality();
00179                      towerRec = itRPC->eta_packed();
00180                      if (towerRec > 16) {
00181                       towerRec = - ( (~towerRec & 63) + 1);
00182                      }
00183                      // match
00184                      float pi = 3.14159265;
00185                      //float phiPhys = 2*pi*float(phiRec)/144-pi;
00186                      float phiScaled = phiGen;
00187                      if (phiScaled<0)  phiScaled += 2*pi;
00188                      int phiHwGen = (phiScaled)/2/pi*144;
00189 
00190                      //std::cout << "phi " << phiGen << " " << phiHwGen << " " << phiRec
00191                      //          << " eta " << etaGen << " " << m_const.towerNumFromEta(etaGen) << " "<< towerRec << std::endl;
00192                      if (  ( std::abs(phiHwGen-phiRec) < 10) && (std::abs(m_const.towerNumFromEta(etaGen)-towerRec)<1) )
00193                      {
00194                          if (matched) { // we have matched m.c. earlier, this is ghost
00195                             ++ghost;
00196                          }
00197                          matched = true;
00198                          ++noOfMatchedRecMuons;
00199                          m_outfileR << etaGen << " " << phiGen << " " << ptGen 
00200                                     << " "    << towerRec << " " << phiRec << " " << ptCodeRec << " " << qual
00201                                     << " "    << ghost
00202                                     << std::endl;
00203                      }
00204                   } // (ptCode != 0)
00205                } // muon cands iter ends
00206             } // barrell/fwd iter ends
00207             firstRunForMuonMatchingCnt=false;
00208             if (!matched) {
00209                          m_outfileR << etaGen << " " << phiGen << " " << ptGen 
00210                                     << " "    << 0 << " " << 0 << " " << 0 << " " << 0
00211                                     << " "    << 0
00212                                     << std::endl;
00213 
00214             }
00215 
00216 
00217         }
00218     }
00219 
00220     edm::EventID id = iEvent.id();
00221     edm::EventNumber_t evNum = id.event();
00222     edm::RunNumber_t rnNum = id.run();
00223 
00224     if (noOfMatchedRecMuons!=noOfRecMuons)  {
00225       edm::LogInfo("RPCEffWarn") << " MuonCands " << noOfRecMuons
00226                             << " matched " << noOfMatchedRecMuons
00227                             << " in run " << rnNum
00228                             << " event " << evNum;
00229     }
00230 
00231 
00232 
00233       /*
00234     m_outfileC << etaGen << " " << phiGen << " " << ptGen << " "
00235                << phiRec << " " << towerRec << " " << muonsFound  << " " 
00236                <<  fromCones(iEvent) <<std::endl;*/
00237     /*m_outfileR << etaGen << " " << phiGen << " " << ptGen << " " 
00238                << phiRec << " " << towerRec << " " << muonsFound  << " " 
00239                <<  fromRaw(iEvent) <<std::endl;*/
00240 
00241 
00242     /* 
00243     m_outfileR << etaGen << " " << phiGen << " " << ptGen << " " 
00244                << phiRec << " " << towerRec << " " << muonsFound  << " " 
00245                << std::endl;
00246     
00247     */
00248 
00249 
00250 }
00251 
00252 std::string RPCPhiEff::fromCones(const edm::Event & iEvent){
00253 
00254  return "";
00255 }
00256 /*
00257  std::stringstream ss;
00258 
00259  edm::Handle < RPCDigiCollection > rpcDigis;
00260     iEvent.getByLabel("muonRPCDigis", rpcDigis);
00261 
00262     L1RpcLogConesVec conesVec = m_theLinksystem.getCones(rpcDigis);
00263 
00264     L1RpcLogConesVec bestCones;
00265 
00266  //   bool wasRef = false;
00267     int maxfpCount = 0;
00268 
00269 
00270     for (L1RpcLogConesVec::const_iterator it = conesVec.begin();
00271          it != conesVec.end(); ++it) {
00272         int currentFpCount = it->getFiredPlanesCnt();
00273 
00274                 if (currentFpCount > maxfpCount) {      // the cone pointed by it is better
00275                     bestCones.clear();
00276                     bestCones.push_back(*it);
00277                     maxfpCount = currentFpCount;
00278                 }
00279 
00280     }                           // end of conesVec loop
00281 
00282 
00283     //int planePat = 0;
00284     //int one = 1;
00285     //int planeCnt = 0;
00286 
00287     if (bestCones.begin() != bestCones.end()) {
00288     
00289         L1RpcLogConesVec::const_iterator bc = bestCones.begin();
00290         for (int i = 0; i < 6; ++i) {
00291 
00292             if (bc->getHitsCnt(i)!=0) {
00293                 //planePat = planePat | (one << i);
00294                 //planeCnt++;
00295                 ss << " " << i+1;
00296             }
00297         }
00298     }
00299 
00300   return ss.str();
00301 }
00302 */
00303 
00304 // ------------ Check hw planes fired using rpc digis
00305 std::string RPCPhiEff::fromRaw(const edm::Event & iEvent){
00306 
00307    std::stringstream ss;
00308 
00309    // Digi data.
00310     edm::Handle<RPCDigiCollection> rpcDigis;
00311     iEvent.getByLabel(m_rpcdigi,rpcDigis);
00312 
00313     std::set<int> hwPlanes;
00314 
00315     RPCDigiCollection::DigiRangeIterator detUnitIt;
00316     for (detUnitIt=rpcDigis->begin();
00317          detUnitIt!=rpcDigis->end();
00318                  ++detUnitIt)
00319     {
00320 
00321         const RPCDigiCollection::Range& range = (*detUnitIt).second;
00322         bool hasBX0 = false;
00323         for (RPCDigiCollection::const_iterator digiIt = range.first;
00324             digiIt!=range.second;
00325             ++digiIt)
00326         {
00327           if (digiIt->bx() == 0) {
00328              hasBX0 = true;
00329              break;
00330           }
00331         } 
00332 
00333         if (!hasBX0) continue;
00334  
00335         const RPCDetId& id = (*detUnitIt).first;
00336         int station = id.station();
00337         int layer = id.layer();
00338         //int region = id.region();
00339 
00340           if (station == 3)
00341             hwPlanes.insert(5);
00342 
00343           else if (station == 4)
00344             hwPlanes.insert(6);
00345 
00346           else if (station  == 1 && layer == 1)
00347             hwPlanes.insert(1);
00348 
00349           else if (station  == 1 && layer == 2)
00350             hwPlanes.insert(2);
00351 
00352           else if (station  == 2 && layer == 1)
00353             hwPlanes.insert(3);
00354 
00355           else if (station  == 2 && layer == 2)
00356             hwPlanes.insert(4);
00357 
00358           else
00359             std::cout << "??????????????" << std::endl;
00360 
00361     }
00362 
00363 
00364     for (std::set<int>::iterator it= hwPlanes.begin();
00365                                  it!= hwPlanes.end();
00366                                  ++it)
00367     {
00368         ss << " " << *it;
00369     }
00370 
00371 
00372 
00373 
00374   return ss.str();
00375 
00376 
00377 
00378 }
00379 
00380 // ------------ method called once each job just before starting event loop  ------------
00381 void RPCPhiEff::beginJob(const edm::EventSetup &)
00382 {
00383 }
00384 
00385 // ------------ method called once each job just after ending the event loop  ------------
00386 void RPCPhiEff::endJob()
00387 {
00388 }
00389