CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/Validation/DTRecHits/plugins/DTSegment4DQuality.cc

Go to the documentation of this file.
00001 /*
00002  *  See header file for a description of this class.
00003  *
00004  *  \author S. Bolognesi and G. Cerminara - INFN Torino
00005  */
00006 
00007 #include "DTSegment4DQuality.h"
00008 #include "Validation/DTRecHits/interface/DTHitQualityUtils.h"
00009 
00010 #include "DataFormats/MuonDetId/interface/DTWireId.h"
00011 #include "Geometry/DTGeometry/interface/DTChamber.h"
00012 #include "Geometry/DTGeometry/interface/DTGeometry.h"
00013 #include "Geometry/Records/interface/MuonGeometryRecord.h"
00014 
00015 #include "SimDataFormats/TrackingHit/interface/PSimHitContainer.h"
00016 #include "DataFormats/DTRecHit/interface/DTRecHitCollection.h"
00017 #include "DataFormats/DTRecHit/interface/DTRecSegment4DCollection.h"
00018 
00019 #include "FWCore/Framework/interface/MakerMacros.h"
00020 #include "FWCore/Framework/interface/Frameworkfwd.h"
00021 #include "FWCore/Framework/interface/Event.h"
00022 #include "FWCore/Framework/interface/ESHandle.h"
00023 #include "DQMServices/Core/interface/DQMStore.h"
00024 #include "DQMServices/Core/interface/MonitorElement.h"
00025 
00026 #include "Histograms.h"
00027 
00028 #include "TFile.h"
00029 #include <iostream>
00030 #include <map>
00031 
00032 
00033 using namespace std;
00034 using namespace edm;
00035 
00036 // Book the histos
00037 
00038 // Constructor
00039 DTSegment4DQuality::DTSegment4DQuality(const ParameterSet& pset)  {
00040   // Get the debug parameter for verbose output
00041   debug = pset.getUntrackedParameter<bool>("debug");
00042   DTHitQualityUtils::debug = debug;
00043 
00044   rootFileName = pset.getUntrackedParameter<string>("rootFileName");
00045   // the name of the simhit collection
00046   simHitLabel = pset.getUntrackedParameter<InputTag>("simHitLabel");
00047   // the name of the 4D rec hit collection
00048   segment4DLabel = pset.getUntrackedParameter<InputTag>("segment4DLabel");
00049 
00050   //sigma resolution on position
00051   sigmaResX = pset.getParameter<double>("sigmaResX");
00052   sigmaResY = pset.getParameter<double>("sigmaResY");
00053   //sigma resolution on angle
00054   sigmaResAlpha = pset.getParameter<double>("sigmaResAlpha");
00055   sigmaResBeta = pset.getParameter<double>("sigmaResBeta");
00056   doall = pset.getUntrackedParameter<bool>("doall", false);
00057   local = pset.getUntrackedParameter<bool>("local", false);
00058 
00059 
00060   // Create the root file
00061   //theFile = new TFile(rootFileName.c_str(), "RECREATE");
00062   //theFile->cd();
00063 // ----------------------                 
00064   // get hold of back-end interface 
00065   dbe_ = 0;
00066   dbe_ = Service<DQMStore>().operator->();
00067   if ( dbe_ ) {
00068     if (debug) {
00069       dbe_->setVerbose(1);
00070     } else {
00071       dbe_->setVerbose(0);
00072     }
00073   }
00074   if ( dbe_ ) {
00075     if ( debug ) dbe_->showDirStructure();
00076   }
00077 
00078   h4DHit= new HRes4DHit ("All",dbe_,doall,local);
00079   h4DHit_W0= new HRes4DHit ("W0",dbe_,doall,local);
00080   h4DHit_W1= new HRes4DHit ("W1",dbe_,doall,local);
00081   h4DHit_W2= new HRes4DHit ("W2",dbe_,doall,local);
00082 
00083   if(doall) {
00084     hEff_All= new HEff4DHit ("All",dbe_);
00085     hEff_W0= new HEff4DHit ("W0",dbe_);
00086     hEff_W1= new HEff4DHit ("W1",dbe_);
00087     hEff_W2= new HEff4DHit ("W2",dbe_);
00088   }
00089 
00090   if (local) {
00091     // Plots with finer granularity, not to be included in DQM
00092     TString name="W";
00093     for (long w=0;w<=2;++w) {
00094       for (long s=1;s<=4;++s){
00095         // FIXME station 4 is not filled
00096         TString nameWS=(name+w+"_St"+s);
00097         h4DHitWS[w][s-1] = new HRes4DHit(nameWS.Data(),dbe_,doall,local);
00098         hEffWS[w][s-1]   = new HEff4DHit (nameWS.Data(),dbe_);
00099         dbe_->setCurrentFolder("DT/4DSegments/");
00100         hHitMult[w][s-1] = dbe_->book2D("4D_" +nameWS+ "_hNHits", "NHits",12,0,12, 6,0,6);
00101         ht0[w][s-1] = dbe_->book2D("4D_" +nameWS+ "_ht0", "t0",200,-25,25,200,-25,25);
00102       }
00103     }
00104   }
00105 }
00106 
00107 // Destructor
00108 DTSegment4DQuality::~DTSegment4DQuality(){
00109 
00110 }
00111 void DTSegment4DQuality::endLuminosityBlock(edm::LuminosityBlock const& lumiSeg,
00112     edm::EventSetup const& c){
00113 
00114 
00115 }
00116 
00117 void DTSegment4DQuality::endJob() {
00118   // Write the histos to file
00119   //theFile->cd();
00120 
00121   //h4DHit->Write();
00122   //h4DHit_W0->Write();
00123   //h4DHit_W1->Write();
00124   //h4DHit_W2->Write();
00125 
00126   if(doall){
00127     hEff_All->ComputeEfficiency();
00128     hEff_W0->ComputeEfficiency();
00129     hEff_W1->ComputeEfficiency();
00130     hEff_W2->ComputeEfficiency();
00131   }
00132   //hEff_All->Write();
00133   //hEff_W0->Write();
00134   //hEff_W1->Write();
00135   //hEff_W2->Write();
00136   //if ( rootFileName.size() != 0 && dbe_ ) dbe_->save(rootFileName); 
00137 
00138   //theFile->Close();
00139 } 
00140 
00141 // The real analysis
00142   void DTSegment4DQuality::analyze(const Event & event, const EventSetup& eventSetup){
00143     //theFile->cd();
00144 
00145     // Get the DT Geometry
00146     ESHandle<DTGeometry> dtGeom;
00147     eventSetup.get<MuonGeometryRecord>().get(dtGeom);
00148 
00149     // Get the SimHit collection from the event
00150     edm::Handle<PSimHitContainer> simHits;
00151     event.getByLabel(simHitLabel, simHits); //FIXME: second string to be removed
00152 
00153     //Map simHits by chamber
00154     map<DTChamberId, PSimHitContainer > simHitsPerCh;
00155     for(PSimHitContainer::const_iterator simHit = simHits->begin();
00156         simHit != simHits->end(); simHit++){
00157       // Create the id of the chamber (the simHits in the DT known their wireId)
00158       DTChamberId chamberId = (((DTWireId(simHit->detUnitId())).layerId()).superlayerId()).chamberId();
00159       // Fill the map
00160       simHitsPerCh[chamberId].push_back(*simHit);
00161     }
00162 
00163     // Get the 4D rechits from the event
00164     Handle<DTRecSegment4DCollection> segment4Ds;
00165     event.getByLabel(segment4DLabel, segment4Ds);
00166 
00167     if(!segment4Ds.isValid()) {
00168       if(debug) cout << "[DTSegment4DQuality]**Warning: no 4D Segments with label: " <<segment4DLabel
00169                 << " in this event, skipping!" << endl;
00170       return;
00171     }    
00172 
00173     // Loop over all chambers containing a segment
00174     DTRecSegment4DCollection::id_iterator chamberId;
00175     for (chamberId = segment4Ds->id_begin();
00176          chamberId != segment4Ds->id_end();
00177          ++chamberId){
00178 
00179       if((*chamberId).station() == 4)
00180         continue; //use DTSegment2DSLPhiQuality to analyze MB4 performaces
00181 
00182       //------------------------- simHits ---------------------------//
00183       //Get simHits of each chamber
00184       PSimHitContainer simHits =  simHitsPerCh[(*chamberId)];
00185 
00186       // Map simhits per wire
00187       map<DTWireId, PSimHitContainer > simHitsPerWire = DTHitQualityUtils::mapSimHitsPerWire(simHits);
00188       map<DTWireId, const PSimHit*> muSimHitPerWire = DTHitQualityUtils::mapMuSimHitsPerWire(simHitsPerWire);
00189       int nMuSimHit = muSimHitPerWire.size();
00190       if(nMuSimHit == 0 || nMuSimHit == 1) {
00191         if(debug && nMuSimHit == 1)
00192           cout << "[DTSegment4DQuality] Only " << nMuSimHit << " mu SimHit in this chamber, skipping!" << endl;
00193         continue; // If no or only one mu SimHit is found skip this chamber
00194       } 
00195       if(debug)
00196         cout << "=== Chamber " << (*chamberId) << " has " << nMuSimHit << " SimHits" << endl;
00197 
00198       //Find outer and inner mu SimHit to build a segment
00199       pair<const PSimHit*, const PSimHit*> inAndOutSimHit = DTHitQualityUtils::findMuSimSegment(muSimHitPerWire); 
00200 
00201       //Find direction and position of the sim Segment in Chamber RF
00202       pair<LocalVector, LocalPoint> dirAndPosSimSegm = DTHitQualityUtils::findMuSimSegmentDirAndPos(inAndOutSimHit,
00203                                                                                                     (*chamberId),&(*dtGeom));
00204 
00205       LocalVector simSegmLocalDir = dirAndPosSimSegm.first;
00206       LocalPoint simSegmLocalPos = dirAndPosSimSegm.second;
00207       const DTChamber* chamber = dtGeom->chamber(*chamberId);
00208       GlobalPoint simSegmGlobalPos = chamber->toGlobal(simSegmLocalPos);
00209       GlobalVector simSegmGlobalDir = chamber->toGlobal(simSegmLocalDir);
00210 
00211       //phi and theta angle of simulated segment in Chamber RF
00212       float alphaSimSeg = DTHitQualityUtils::findSegmentAlphaAndBeta(simSegmLocalDir).first;
00213       float betaSimSeg = DTHitQualityUtils::findSegmentAlphaAndBeta(simSegmLocalDir).second;
00214       //x,y position of simulated segment in Chamber RF
00215       float xSimSeg = simSegmLocalPos.x(); 
00216       float ySimSeg = simSegmLocalPos.y(); 
00217       //Position (in eta,phi coordinates) in lobal RF
00218       float etaSimSeg = simSegmGlobalPos.eta(); 
00219       float phiSimSeg = simSegmGlobalPos.phi();
00220 
00221       if(debug)
00222         cout<<"  Simulated segment:  local direction "<<simSegmLocalDir<<endl
00223           <<"                      local position  "<<simSegmLocalPos<<endl
00224           <<"                      alpha           "<<alphaSimSeg<<endl
00225           <<"                      beta            "<<betaSimSeg<<endl;
00226 
00227       //---------------------------- recHits --------------------------//
00228       // Get the range of rechit for the corresponding chamberId
00229       bool recHitFound = false;
00230       DTRecSegment4DCollection::range range = segment4Ds->get(*chamberId);
00231       int nsegm = distance(range.first, range.second);
00232       if(debug)
00233         cout << "   Chamber: " << *chamberId << " has " << nsegm
00234           << " 4D segments" << endl;
00235 
00236       if (nsegm!=0) {
00237         // Find the best RecHit: look for the 4D RecHit with the phi angle closest
00238         // to that of segment made of SimHits. 
00239         // RecHits must have delta alpha and delta position within 5 sigma of
00240         // the residual distribution (we are looking for residuals of segments
00241         // usefull to the track fit) for efficency purpose
00242         const DTRecSegment4D* bestRecHit = 0;
00243         bool bestRecHitFound = false;
00244         double deltaAlpha = 99999;
00245 
00246         // Loop over the recHits of this chamberId
00247         for (DTRecSegment4DCollection::const_iterator segment4D = range.first;
00248              segment4D!=range.second;
00249              ++segment4D){
00250 
00251           if (local) {
00252             const DTChamberRecSegment2D*  phiSeg = (*segment4D).phiSegment();
00253             const DTSLRecSegment2D* zSeg = (*segment4D).zSegment();
00254 
00255             float t0phi = -999;
00256             float t0z   = -999;
00257             int nHitPhi=0;
00258             int nHitZ=0;
00259             if (phiSeg) {
00260               t0phi = phiSeg->t0();
00261               nHitPhi = phiSeg->recHits().size();
00262             }
00263           
00264             if (zSeg) {  
00265               t0z = zSeg->t0();
00266               nHitZ   = zSeg->recHits().size();
00267             }
00268           
00269             hHitMult[abs((*chamberId).wheel())][(*chamberId).station()-1]->Fill(nHitPhi,nHitZ);
00270             ht0[abs((*chamberId).wheel())][(*chamberId).station()-1]->Fill(t0phi,t0z);
00271 
00272             // Uncomment to skip segments w/o t0 computed
00273             //    if (!(t0phi>-998&&t0z>-998)) continue
00274           }
00275 
00276           // Check the dimension
00277           if((*segment4D).dimension() != 4) {
00278             if(debug)cout << "[DTSegment4DQuality]***Error: This is not 4D segment!!!" << endl;
00279             continue;
00280           }
00281           // Segment Local Direction and position (in Chamber RF)
00282           LocalVector recSegDirection = (*segment4D).localDirection();
00283           //LocalPoint recSegPosition = (*segment4D).localPosition();
00284 
00285           float recSegAlpha = DTHitQualityUtils::findSegmentAlphaAndBeta(recSegDirection).first;
00286           if(debug)
00287             cout << "  RecSegment direction: " << recSegDirection << endl
00288               << "             position : " <<  (*segment4D).localPosition() << endl
00289               << "             alpha    : " << recSegAlpha << endl
00290               << "             beta     : " <<  DTHitQualityUtils::findSegmentAlphaAndBeta(recSegDirection).second << endl;
00291 
00292           if(fabs(recSegAlpha - alphaSimSeg) < deltaAlpha) {
00293             deltaAlpha = fabs(recSegAlpha - alphaSimSeg);
00294             bestRecHit = &(*segment4D);
00295             bestRecHitFound = true;
00296           }
00297         }  // End of Loop over all 4D RecHits
00298 
00299         if(bestRecHitFound) {
00300           // Best rechit direction and position in Chamber RF
00301           LocalPoint bestRecHitLocalPos = bestRecHit->localPosition();
00302           LocalVector bestRecHitLocalDir = bestRecHit->localDirection();
00303           // Errors on x and y
00304           LocalError bestRecHitLocalPosErr = bestRecHit->localPositionError();
00305           LocalError bestRecHitLocalDirErr = bestRecHit->localDirectionError();
00306 
00307           float alphaBestRHit = DTHitQualityUtils::findSegmentAlphaAndBeta(bestRecHitLocalDir).first;
00308           float betaBestRHit = DTHitQualityUtils::findSegmentAlphaAndBeta(bestRecHitLocalDir).second;
00309           // Errors on alpha and beta
00310 
00311           // Get position and direction using the rx projection (so in SL
00312           // reference frame). Note that x (and y) are swapped wrt to Chamber
00313           // frame
00314           //if (bestRecHit->hasZed() ) {
00315           const DTSLRecSegment2D * zedRecSeg = bestRecHit->zSegment();
00316           LocalPoint bestRecHitLocalPosRZ = zedRecSeg->localPosition();
00317           LocalVector bestRecHitLocalDirRZ = zedRecSeg->localDirection();
00318           // Errors on x and y
00319           LocalError bestRecHitLocalPosErrRZ = zedRecSeg->localPositionError();
00320           LocalError bestRecHitLocalDirErrRZ = zedRecSeg->localDirectionError();
00321 
00322           float alphaBestRHitRZ = DTHitQualityUtils::findSegmentAlphaAndBeta(bestRecHitLocalDirRZ).first;
00323 
00324           // Get SimSeg position and Direction in rZ SL frame 
00325           const DTSuperLayer* sl = dtGeom->superLayer(zedRecSeg->superLayerId());
00326           LocalPoint simSegLocalPosRZTmp = sl->toLocal(simSegmGlobalPos);
00327           LocalVector simSegLocalDirRZ = sl->toLocal(simSegmGlobalDir);
00328           LocalPoint simSegLocalPosRZ =
00329             simSegLocalPosRZTmp + simSegLocalDirRZ*(-simSegLocalPosRZTmp.z()/(cos(simSegLocalDirRZ.theta())));
00330           float alphaSimSegRZ = DTHitQualityUtils::findSegmentAlphaAndBeta(simSegLocalDirRZ).first;
00331 
00332           if (debug) cout <<
00333             "RZ SL: recPos " << bestRecHitLocalPosRZ <<
00334               "recDir " << bestRecHitLocalDirRZ <<
00335               "recAlpha " << alphaBestRHitRZ << endl <<
00336               "RZ SL: simPos " << simSegLocalPosRZ <<
00337               "simDir " << simSegLocalDirRZ <<
00338               "simAlpha " << alphaSimSegRZ << endl ;
00339           //}
00340 
00341 
00342           if(fabs(alphaBestRHit - alphaSimSeg) < 5*sigmaResAlpha &&
00343              fabs(betaBestRHit - betaSimSeg) < 5*sigmaResBeta &&
00344              fabs(bestRecHitLocalPos.x() - xSimSeg) < 5*sigmaResX &&
00345              fabs(bestRecHitLocalPos.y() - ySimSeg) < 5*sigmaResY) {
00346             recHitFound = true;
00347           }
00348 
00349           // Fill Residual histos
00350           HRes4DHit *histo=0;
00351 
00352           if((*chamberId).wheel() == 0)
00353             histo = h4DHit_W0;
00354           else if(abs((*chamberId).wheel()) == 1)
00355             histo = h4DHit_W1;
00356           else if(abs((*chamberId).wheel()) == 2)
00357             histo = h4DHit_W2;
00358 
00359           histo->Fill(alphaSimSeg,
00360                       alphaBestRHit,
00361                       betaSimSeg,
00362                       betaBestRHit,
00363                       xSimSeg, 
00364                       bestRecHitLocalPos.x(),
00365                       ySimSeg,
00366                       bestRecHitLocalPos.y(),
00367                       etaSimSeg, 
00368                       phiSimSeg,
00369                       bestRecHitLocalPosRZ.x(),
00370                       simSegLocalPosRZ.x(),
00371                       alphaBestRHitRZ,
00372                       alphaSimSegRZ,
00373                       sqrt(bestRecHitLocalDirErr.xx()),
00374                       sqrt(bestRecHitLocalDirErr.yy()),
00375                       sqrt(bestRecHitLocalPosErr.xx()),
00376                       sqrt(bestRecHitLocalPosErr.yy()),
00377                       sqrt(bestRecHitLocalDirErrRZ.xx()),
00378                       sqrt(bestRecHitLocalPosErrRZ.xx())
00379                      );
00380 
00381           h4DHit->Fill(alphaSimSeg,
00382                        alphaBestRHit,
00383                        betaSimSeg,
00384                        betaBestRHit,
00385                        xSimSeg, 
00386                        bestRecHitLocalPos.x(),
00387                        ySimSeg,
00388                        bestRecHitLocalPos.y(),
00389                        etaSimSeg, 
00390                        phiSimSeg,
00391                        bestRecHitLocalPosRZ.x(),
00392                        simSegLocalPosRZ.x(),
00393                        alphaBestRHitRZ,
00394                        alphaSimSegRZ,
00395                        sqrt(bestRecHitLocalDirErr.xx()),
00396                        sqrt(bestRecHitLocalDirErr.yy()),
00397                        sqrt(bestRecHitLocalPosErr.xx()),
00398                        sqrt(bestRecHitLocalPosErr.yy()),
00399                        sqrt(bestRecHitLocalDirErrRZ.xx()),
00400                        sqrt(bestRecHitLocalPosErrRZ.xx())
00401                       );
00402 
00403           if (local) h4DHitWS[abs((*chamberId).wheel())][(*chamberId).station()-1]->Fill(alphaSimSeg,
00404                            alphaBestRHit,
00405                            betaSimSeg,
00406                            betaBestRHit,
00407                            xSimSeg, 
00408                            bestRecHitLocalPos.x(),
00409                            ySimSeg,
00410                            bestRecHitLocalPos.y(),
00411                            etaSimSeg, 
00412                            phiSimSeg,
00413                            bestRecHitLocalPosRZ.x(),
00414                            simSegLocalPosRZ.x(),
00415                            alphaBestRHitRZ,
00416                            alphaSimSegRZ,
00417                            sqrt(bestRecHitLocalDirErr.xx()),
00418                            sqrt(bestRecHitLocalDirErr.yy()),
00419                            sqrt(bestRecHitLocalPosErr.xx()),
00420                            sqrt(bestRecHitLocalPosErr.yy()),
00421                            sqrt(bestRecHitLocalDirErrRZ.xx()),
00422                            sqrt(bestRecHitLocalPosErrRZ.xx())
00423                            );
00424 
00425 
00426         } //end of if(bestRecHitFound)
00427 
00428       } //end of if(nsegm!=0)
00429 
00430       // Fill Efficiency plot
00431       if(doall){
00432         HEff4DHit *heff = 0;
00433         
00434         if((*chamberId).wheel() == 0)
00435           heff = hEff_W0;
00436         else if(abs((*chamberId).wheel()) == 1)
00437           heff = hEff_W1;
00438         else if(abs((*chamberId).wheel()) == 2)
00439           heff = hEff_W2;
00440         heff->Fill(etaSimSeg, phiSimSeg, xSimSeg, ySimSeg, alphaSimSeg, betaSimSeg, recHitFound);
00441         hEff_All->Fill(etaSimSeg, phiSimSeg, xSimSeg, ySimSeg, alphaSimSeg, betaSimSeg, recHitFound);
00442         if (local) hEffWS[abs((*chamberId).wheel())][(*chamberId).station()-1]->Fill(etaSimSeg, phiSimSeg, xSimSeg, ySimSeg, alphaSimSeg, betaSimSeg, recHitFound);
00443 
00444       }
00445     } // End of loop over chambers
00446   }