CMS 3D CMS Logo

RPCEfficiencyFromTrack.cc

Go to the documentation of this file.
00001 /**********************************************
00002  *                                            *
00003  *           Giuseppe Roselli                 *
00004  *         INFN, Sezione di Bari              *
00005  *      Via Amendola 173, 70126 Bari          *
00006  *         Phone: +390805443218               *
00007  *      giuseppe.roselli@ba.infn.it           *
00008  *                                            *
00009  *                                            *
00010  **********************************************/
00011 
00012 
00013 #include "DQM/RPCMonitorDigi/interface/RPCEfficiencyFromTrack.h"
00014 
00015 #include "DataFormats/RPCDigi/interface/RPCDigi.h"
00016 #include "DataFormats/RPCDigi/interface/RPCDigiCollection.h"
00017 #include "FWCore/Framework/interface/Frameworkfwd.h"
00018 #include "FWCore/Framework/interface/EDAnalyzer.h"
00019 #include "FWCore/Framework/interface/Event.h"
00020 #include "FWCore/Framework/interface/MakerMacros.h"
00021 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00022 #include "DataFormats/Common/interface/Handle.h"
00023 #include "FWCore/Framework/interface/ESHandle.h"
00024 #include "Geometry/CommonTopologies/interface/RectangularStripTopology.h"
00025 #include "Geometry/CommonTopologies/interface/TrapezoidalStripTopology.h"
00026 #include <DataFormats/RPCRecHit/interface/RPCRecHit.h>
00027 #include "DataFormats/RPCRecHit/interface/RPCRecHitCollection.h"
00028 
00029 #include <DataFormats/MuonDetId/interface/DTChamberId.h>
00030 #include <Geometry/DTGeometry/interface/DTGeometry.h>
00031 #include <DataFormats/DTRecHit/interface/DTRecSegment4DCollection.h>
00032 
00033 #include <DataFormats/MuonDetId/interface/RPCDetId.h>
00034 #include <Geometry/RPCGeometry/interface/RPCGeometry.h>
00035 #include "Geometry/RPCGeometry/interface/RPCGeomServ.h"
00036 #include "MagneticField/Engine/interface/MagneticField.h"
00037 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
00038 #include <Geometry/Records/interface/MuonGeometryRecord.h>
00039 #include "Geometry/Records/interface/GlobalTrackingGeometryRecord.h"
00040 #include "Geometry/CommonDetUnit/interface/GeomDet.h"
00041 #include <DataFormats/GeometrySurface/interface/LocalError.h>
00042 #include <DataFormats/GeometryVector/interface/LocalPoint.h>
00043 #include "DataFormats/GeometrySurface/interface/Surface.h"
00044 #include "TrackingTools/TransientTrack/interface/TransientTrack.h"
00045 #include "DataFormats/DetId/interface/DetId.h"
00046 
00047 #include "TrackingTools/GeomPropagators/interface/Propagator.h"
00048 #include "TrackingTools/GeomPropagators/interface/AnalyticalPropagator.h"
00049 #include "TrackPropagation/SteppingHelixPropagator/interface/SteppingHelixPropagator.h"
00050 #include "TrackingTools/Records/interface/TrackingComponentsRecord.h"
00051 #include "RecoMuon/TrackingTools/interface/MuonPatternRecoDumper.h"
00052 
00053 #include "DataFormats/TrajectorySeed/interface/TrajectorySeedCollection.h"
00054 #include "TrackingTools/PatternTools/interface/Trajectory.h"
00055 #include "TrackingTools/PatternTools/interface/TrajTrackAssociation.h"
00056 
00057 #include "DataFormats/L1GlobalMuonTrigger/interface/L1MuRegionalCand.h"
00058 #include "DataFormats/L1GlobalMuonTrigger/interface/L1MuGMTCand.h"
00059 #include "DataFormats/L1GlobalMuonTrigger/interface/L1MuGMTExtendedCand.h"
00060 #include "DataFormats/L1GlobalMuonTrigger/interface/L1MuGMTReadoutCollection.h"
00061 
00062 #include <memory>
00063 #include <cmath>
00064 #include "TFile.h"
00065 #include "TH1F.h"
00066 #include "TH2F.h"
00067 #include "TCanvas.h"
00068 #include "TAxis.h"
00069 
00070 
00071 
00072 using namespace edm;
00073 using namespace reco;
00074 using namespace std;
00075 
00076 
00077 RPCEfficiencyFromTrack::RPCEfficiencyFromTrack(const edm::ParameterSet& iConfig){
00078 
00079   std::map<RPCDetId, int> buff;
00080   counter.clear();
00081   counter.reserve(3);
00082   counter.push_back(buff);
00083   counter.push_back(buff);
00084   counter.push_back(buff);
00085   totalcounter.clear();
00086   totalcounter.reserve(3);
00087   totalcounter[0]=0;
00088   totalcounter[1]=0;
00089   totalcounter[2]=0;
00090   
00091   MeasureEndCap = iConfig.getParameter<bool>("ReadEndCap");
00092   MeasureBarrel = iConfig.getParameter<bool>("ReadBarrel");
00093   maxRes = iConfig.getParameter<double>("EfficiencyCut");
00094   gmtSource_=iConfig.getParameter< InputTag >("gmtSource");
00095   EffSaveRootFile  = iConfig.getUntrackedParameter<bool>("EffSaveRootFile", true); 
00096   EffSaveRootFileEventsInterval  = iConfig.getUntrackedParameter<int>("EffEventsInterval", 1000); 
00097   DTTrigValue = iConfig.getUntrackedParameter<int>("dtTrigger",-10); 
00098 
00099   EffRootFileName  = iConfig.getUntrackedParameter<std::string>("EffRootFileName", "RPCEfficiencyFromTrack.root"); 
00100   TjInput  = iConfig.getUntrackedParameter<std::string>("trajectoryInput");
00101   RPCDataLabel = iConfig.getUntrackedParameter<std::string>("rpcRecHitLabel");
00102   digiLabel = iConfig.getUntrackedParameter<std::string>("rpcDigiLabel");
00103   thePropagatorName = iConfig.getParameter<std::string>("PropagatorName");
00104   thePropagator = 0;
00105 
00106   GlobalRootLabel= iConfig.getUntrackedParameter<std::string>("GlobalRootFileName","GlobalEfficiencyFromTrack.root");
00107   fOutputFile  = new TFile(GlobalRootLabel.c_str(), "RECREATE" );
00108 
00109   hGlobalRes = new TH1F("GlobalResiduals","GlobalRPCResiduals",50,-15.,15.);
00110   hGlobalPull = new TH1F("GlobalPull","GlobalRPCPull",50,-15.,15.);
00111   histoMean = new TH1F("MeanEfficincy","MeanEfficiency_vs_Ch",60,20.5,120.5);
00112 
00113 
00114   // get hold of back-end interface
00115   dbe = edm::Service<DQMStore>().operator->();
00116   _idList.clear(); 
00117   Run=0;
00118   effres = new ofstream("EfficiencyResults.dat");
00119 }
00120 
00121 
00122 RPCEfficiencyFromTrack::~RPCEfficiencyFromTrack(){
00123   effres->close();
00124   delete effres;
00125 
00126   fOutputFile->WriteTObject(hGlobalRes);
00127   fOutputFile->WriteTObject(hGlobalPull);
00128   fOutputFile->WriteTObject(histoMean);
00129 
00130 
00131   fOutputFile->Close();
00132 }
00133 
00134 
00135 
00136 void RPCEfficiencyFromTrack::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup){
00137   char layerLabel[128];
00138   char meIdRPC [128];
00139   char meIdTrack [128];
00140   std::map<RPCDetId, int> buff;
00141 
00142   edm::ESHandle<RPCGeometry> rpcGeo;
00143   iSetup.get<MuonGeometryRecord>().get(rpcGeo);
00144 
00145   edm::Handle<RPCRecHitCollection> rpcHits;
00146   iEvent.getByLabel(RPCDataLabel,rpcHits);
00147 
00148   edm::Handle<RPCDigiCollection> rpcDigis;
00149   iEvent.getByLabel(digiLabel, rpcDigis);
00150 
00151   ESHandle<MagneticField> theMGField;
00152   iSetup.get<IdealMagneticFieldRecord>().get(theMGField);
00153   
00154   ESHandle<GlobalTrackingGeometry> theTrackingGeometry;
00155   iSetup.get<GlobalTrackingGeometryRecord>().get(theTrackingGeometry);
00156 
00157 
00158   int nDTTF = 0;
00159   if(DTTrigValue!=-10){
00160     edm::Handle<std::vector<L1MuRegionalCand> > rpcBarrel;
00161     edm::Handle<std::vector<L1MuRegionalCand> > rpcForward;
00162     iEvent.getByLabel ("gtDigis","RPCb",rpcBarrel);
00163     iEvent.getByLabel ("gtDigis","RPCf",rpcForward);
00164     edm::Handle<L1MuGMTReadoutCollection> pCollection;
00165     try {
00166       iEvent.getByLabel(gmtSource_,pCollection);
00167     }
00168     catch (...) {
00169       return;
00170     }
00171 
00172     L1MuGMTReadoutCollection const* gmtrc = pCollection.product();
00173     vector<L1MuGMTReadoutRecord> gmt_records = gmtrc->getRecords();
00174     vector<L1MuGMTReadoutRecord>::const_iterator RRItr;
00175 
00176     for(RRItr = gmt_records.begin(); RRItr != gmt_records.end(); RRItr++ ){    
00177       vector<L1MuRegionalCand> DTTFCands  = RRItr->getDTBXCands();
00178       vector<L1MuGMTExtendedCand>::const_iterator GMTItr;
00179       int BxInEvent = RRItr->getBxInEvent();
00180     
00181       if(BxInEvent!=0) continue;
00182       vector<L1MuRegionalCand>::const_iterator DTTFItr;
00183       for( DTTFItr = DTTFCands.begin(); DTTFItr != DTTFCands.end(); ++DTTFItr ) {
00184         if(!DTTFItr->empty()) nDTTF++;
00185       }
00186     }
00187   }
00188 
00189   Handle<reco::TrackCollection> staTracks;
00190   iEvent.getByLabel(TjInput, staTracks);
00191 
00192   reco::TrackCollection::const_iterator staTrack;
00193 
00194   std::cout<<"cacca"<<std::endl;
00195 
00196   ESHandle<Propagator> prop;
00197   iSetup.get<TrackingComponentsRecord>().get(thePropagatorName, prop);
00198   thePropagator = prop->clone();
00199   thePropagator->setPropagationDirection(anyDirection);
00200  
00201 
00202   std::map<RPCDetId,TrajectoryStateOnSurface> RPCstate;
00203 
00204 
00205   if(staTracks->size()!=0 && nDTTF>DTTrigValue){
00206     for (staTrack = staTracks->begin(); staTrack != staTracks->end(); ++staTrack){
00207       reco::TransientTrack track(*staTrack,&*theMGField,theTrackingGeometry);
00208  
00209       RPCstate.clear();
00210  
00211       if(track.numberOfValidHits()>24.){
00212         for (TrackingGeometry::DetContainer::const_iterator it=rpcGeo->dets().begin();it<rpcGeo->dets().end();it++){
00213           if( dynamic_cast< RPCChamber* >( *it ) != 0 ){
00214             RPCChamber* ch = dynamic_cast< RPCChamber* >( *it );
00215             std::vector< const RPCRoll*> rolhit = (ch->rolls());
00216 
00217             for(std::vector<const RPCRoll*>::const_iterator itRoll = rolhit.begin();itRoll != rolhit.end(); ++itRoll){
00218               RPCDetId rollId=(*itRoll)->id();
00219               const BoundPlane *rpcPlane =  &((*itRoll)->surface());
00220 
00221               //Barrel
00222               if(MeasureBarrel==true && rollId.region()==0){            
00223                 const RectangularStripTopology* top_= dynamic_cast<const RectangularStripTopology*> 
00224                   (&((*itRoll)->topology()));
00225                 LocalPoint xmin = top_->localPosition(0.);
00226                 LocalPoint xmax = top_->localPosition((float)(*itRoll)->nstrips());
00227                 float rsize = fabs( xmax.x()-xmin.x() )*0.5;
00228                 float stripl = top_->stripLength();
00229                 
00230                 TrajectoryStateClosestToPoint tcp = track.impactPointTSCP();
00231                 const FreeTrajectoryState &fTS=tcp.theState();
00232                 const FreeTrajectoryState *FreeState = &fTS;
00233                 TrajectoryStateOnSurface tsosAtRPC = thePropagator->propagate(*FreeState,*rpcPlane);
00234               
00235                 if(tsosAtRPC.isValid()
00236                    && fabs(tsosAtRPC.localPosition().z()) < 0.01 
00237                    && fabs(tsosAtRPC.localPosition().x()) < rsize 
00238                    && fabs(tsosAtRPC.localPosition().y()) < stripl*0.5){
00239                   //&& tsosAtRPC.localError().positionError().xx()<1.
00240                   //&& tsosAtRPC.localError().positionError().yy()<1.){
00241                   RPCstate[rollId]=tsosAtRPC;
00242                 }             
00243               }
00244 
00245               //EndCap
00246               if(MeasureEndCap==true && rollId.region()!=0){          
00247                 const TrapezoidalStripTopology* top_= dynamic_cast<const TrapezoidalStripTopology*> 
00248                   (&((*itRoll)->topology()));
00249                 LocalPoint xmin = top_->localPosition(0.);
00250                 LocalPoint xmax = top_->localPosition((float)(*itRoll)->nstrips());
00251                 float rsize = fabs( xmax.x()-xmin.x() )*0.5;
00252                 float stripl = top_->stripLength();
00253 
00254                 TrajectoryStateClosestToPoint tcp = track.impactPointTSCP();
00255                 const FreeTrajectoryState &fTS=tcp.theState();
00256                 const FreeTrajectoryState *FreeState = &fTS;
00257                 TrajectoryStateOnSurface tsosAtRPC = thePropagator->propagate(*FreeState,*rpcPlane);
00258               
00259                 if(tsosAtRPC.isValid()
00260                    && fabs(tsosAtRPC.localPosition().z()) < 0.01 
00261                    && fabs(tsosAtRPC.localPosition().x()) < rsize 
00262                    && fabs(tsosAtRPC.localPosition().y()) < stripl*0.5){
00263                   //&& tsosAtRPC.localError().positionError().xx()<1.
00264                   //&& tsosAtRPC.localError().positionError().yy()<1.){
00265                   RPCstate[rollId]=tsosAtRPC;
00266                 }             
00267               }
00268             }
00269           }
00270         }
00271       }
00272     
00273       //Efficiency      
00274       std::map<RPCDetId,TrajectoryStateOnSurface>::iterator irpc;
00275       for (irpc=RPCstate.begin(); irpc!=RPCstate.end();irpc++){
00276         RPCDetId rollId=irpc->first;
00277         const RPCRoll* rollasociated = rpcGeo->roll(rollId);
00278         TrajectoryStateOnSurface tsosAtRoll = RPCstate[rollId];
00279         
00280         const float stripPredicted =rollasociated->strip
00281           (LocalPoint(tsosAtRoll.localPosition().x(),tsosAtRoll.localPosition().y(),0.));
00282         const float xextrap = tsosAtRoll.localPosition().x();
00283         
00284         totalcounter[0]++;
00285         buff=counter[0];
00286         buff[rollId]++;
00287         counter[0]=buff;
00288 
00289 
00290         RPCGeomServ RPCname(rollId);
00291         std::string nameRoll = RPCname.name();
00292         
00293         _idList.push_back(nameRoll);
00294         char detUnitLabel[128];
00295         sprintf(detUnitLabel ,"%s",nameRoll.c_str());
00296         sprintf(layerLabel ,"%s",nameRoll.c_str());
00297         std::map<std::string, std::map<std::string,MonitorElement*> >::iterator meItr = meCollection.find(nameRoll);
00298         if (meItr == meCollection.end()){
00299           meCollection[nameRoll] = bookDetUnitTrackEff(rollId,iSetup);
00300         }
00301         std::map<std::string, MonitorElement*> meMap=meCollection[nameRoll];
00302         
00303         sprintf(meIdTrack,"ExpectedOccupancyFromTrack_%s",detUnitLabel);
00304         meMap[meIdTrack]->Fill(stripPredicted);
00305 
00306         Run=iEvent.id().run();
00307         aTime=iEvent.time().value();     
00308         
00309         RPCRecHitCollection::range rpcRecHitRange = rpcHits->get(rollasociated->id());
00310         RPCRecHitCollection::const_iterator recIt;        
00311         
00312         float res=0.;
00313         
00314         for (recIt = rpcRecHitRange.first; recIt!=rpcRecHitRange.second; ++recIt){
00315           LocalPoint rhitlocal = (*recIt).localPosition();
00316           double rhitpos = rhitlocal.x();  
00317           
00318           LocalError RecError = (*recIt).localPositionError();
00319           double sigmaRec = RecError.xx();
00320           res = (double)(xextrap - rhitpos);
00321           
00322           sprintf(meIdRPC,"ClusterSize_%s",detUnitLabel);
00323           meMap[meIdRPC]->Fill((*recIt).clusterSize());
00324           
00325           sprintf(meIdRPC,"BunchX_%s",detUnitLabel);
00326           meMap[meIdRPC]->Fill((*recIt).BunchX());       
00327           
00328           sprintf(meIdRPC,"Residuals_%s",detUnitLabel);
00329           meMap[meIdRPC]->Fill(res);
00330           
00331           hGlobalRes->Fill(res);
00332           hGlobalPull->Fill(res/sigmaRec);
00333         }
00334         
00335         int stripDetected=0;
00336         bool anycoincidence=false;
00337         
00338         RPCDigiCollection::Range rpcRangeDigi=rpcDigis->get(rollasociated->id());       
00339         for (RPCDigiCollection::const_iterator digiIt = rpcRangeDigi.first;digiIt!=rpcRangeDigi.second;++digiIt){
00340           stripDetected=digiIt->strip();
00341           res = (float)(stripDetected) - stripPredicted;
00342           if(fabs(res)<maxRes){
00343             anycoincidence=true;
00344             break;
00345           }
00346         }
00347         
00348         if(anycoincidence==true){
00349           
00350           std::cout<<"Good Match "<<"\t"<<"Residuals = "<<res<<"\t"<<nameRoll<<std::endl;
00351           
00352           totalcounter[1]++;
00353           buff=counter[1];
00354           buff[rollId]++;
00355           counter[1]=buff;
00356           
00357           sprintf(meIdRPC,"RPCDataOccupancy_%s",detUnitLabel);
00358           meMap[meIdRPC]->Fill(stripPredicted);
00359           
00360         }else if(anycoincidence==false){
00361           if(res==0){
00362             std::cout<<"No Fired "<<nameRoll<<std::endl;
00363           }
00364           if(res!=0){
00365             std::cout<<"No Match "<<"\t"<<"Residuals = "<<res<<"\t"<<nameRoll<<std::endl;
00366           }
00367 
00368           totalcounter[2]++;
00369           buff=counter[2];
00370           buff[rollId]++;
00371           counter[2]=buff;
00372         }
00373       }
00374     }
00375   }
00376 }
00377 
00378 
00379 
00380 
00381 void RPCEfficiencyFromTrack::beginJob(const edm::EventSetup&){
00382 
00383 }
00384 
00385 void RPCEfficiencyFromTrack::endJob(){ 
00386   std::map<RPCDetId, int> pred = counter[0];
00387   std::map<RPCDetId, int> obse = counter[1];
00388   std::map<RPCDetId, int> reje = counter[2];
00389   std::map<RPCDetId, int>::iterator irpc;
00390   int f=0;
00391   for (irpc=pred.begin(); irpc!=pred.end();irpc++){
00392     RPCDetId id=irpc->first;
00393     RPCGeomServ RPCname(id);
00394     std::string nameRoll = RPCname.name();
00395     std::string wheel;
00396     std::string rpc;
00397     std::string partition;
00398 
00399     int p=pred[id]; 
00400     int o=obse[id]; 
00401     int r=reje[id]; 
00402     assert(p==o+r);
00403    
00404     if(p!=0){
00405       float ef = float(o)/float(p); 
00406       float er = sqrt(ef*(1.-ef)/float(p));
00407       if(ef>0.){
00408         *effres << nameRoll <<"\t Eff = "<<ef*100.<<" % +/- "<<er*100.<<" %"<<"\t Run= "<<Run<<"\t"<<ctime(&aTime)<<" ";
00409         histoMean->Fill(ef*100.);
00410       }
00411     }
00412     else{
00413       *effres <<"No predictions in this file predicted=0"<<std::endl;
00414     }
00415     f++;
00416   }
00417   if(totalcounter[0]!=0){
00418     float tote = float(totalcounter[1])/float(totalcounter[0]);
00419     float totr = sqrt(tote*(1.-tote)/float(totalcounter[0]));
00420     std::cout<<"Total Eff = "<<tote<<" +/- "<<totr<<std::endl;
00421   }
00422   else{
00423     std::cout<<"No predictions in this file = 0!!!"<<std::endl;
00424   }
00425 
00426   std::vector<std::string>::iterator meIt;
00427   int id=0;
00428   for(meIt = _idList.begin(); meIt != _idList.end(); ++meIt){
00429     id++;
00430     char detUnitLabel[128];
00431     char meIdRPC [128];
00432     char meIdTrack [128];
00433     char effIdRPC [128];
00434 
00435     sprintf(detUnitLabel ,"%s",(*meIt).c_str());
00436     sprintf(meIdRPC,"RPCDataOccupancy_%s",detUnitLabel);
00437     sprintf(meIdTrack,"ExpectedOccupancyFromTrack_%s",detUnitLabel);
00438     sprintf(effIdRPC,"EfficienyFromTrackExtrapolation_%s",detUnitLabel);
00439 
00440     std::map<std::string, MonitorElement*> meMap=meCollection[*meIt];
00441 
00442     for(unsigned int i=1;i<=100;++i){
00443       if(meMap[meIdTrack]->getBinContent(i) != 0){
00444         float eff = meMap[meIdRPC]->getBinContent(i)/meMap[meIdTrack]->getBinContent(i);
00445         float erreff = sqrt(eff*(1-eff)/meMap[meIdTrack]->getBinContent(i));
00446         meMap[effIdRPC]->setBinContent(i,eff*100.);
00447         meMap[effIdRPC]->setBinError(i,erreff*100.);
00448       }
00449     }
00450   } 
00451   if(EffSaveRootFile) dbe->save(EffRootFileName);
00452 }
00453 
00454 DEFINE_FWK_MODULE(RPCEfficiencyFromTrack);

Generated on Tue Jun 9 17:33:18 2009 for CMSSW by  doxygen 1.5.4