CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/Validation/DTRecHits/plugins/DTSegment2DSLPhiQuality.cc

Go to the documentation of this file.
00001 /*
00002  *  See header file for a description of this class.
00003  *
00004  *  $Date: 2009/11/04 17:22:32 $
00005  *  $Revision: 1.10 $
00006  *  \author S. Bolognesi and G. Cerminara - INFN Torino
00007  */
00008 
00009 #include "DTSegment2DSLPhiQuality.h"
00010 #include "Validation/DTRecHits/interface/DTHitQualityUtils.h"
00011 
00012 #include "DataFormats/MuonDetId/interface/DTWireId.h"
00013 #include "Geometry/DTGeometry/interface/DTChamber.h"
00014 #include "Geometry/DTGeometry/interface/DTGeometry.h"
00015 #include "Geometry/Records/interface/MuonGeometryRecord.h"
00016 
00017 #include "SimDataFormats/TrackingHit/interface/PSimHitContainer.h"
00018 #include "DataFormats/DTRecHit/interface/DTRecHitCollection.h"
00019 #include "DataFormats/DTRecHit/interface/DTRecSegment4DCollection.h"
00020 
00021 #include "FWCore/Framework/interface/MakerMacros.h"
00022 #include "FWCore/Framework/interface/Frameworkfwd.h"
00023 #include "FWCore/Framework/interface/Event.h"
00024 #include "FWCore/Framework/interface/ESHandle.h"
00025 #include "DQMServices/Core/interface/DQMStore.h"
00026 #include "DQMServices/Core/interface/MonitorElement.h"
00027 
00028 #include "Histograms.h"
00029 #include "TStyle.h"
00030 #include "TFile.h"
00031 
00032 #include <iostream>
00033 #include <map>
00034 //#include "utils.C"
00035 
00036 using namespace std;
00037 using namespace edm;
00038 //TStyle * mystyle;
00039 
00040 
00041 // Constructor
00042 DTSegment2DSLPhiQuality::DTSegment2DSLPhiQuality(const ParameterSet& pset)  {
00043    // Get the debug parameter for verbose output
00044   debug = pset.getUntrackedParameter<bool>("debug");
00045   DTHitQualityUtils::debug = debug;
00046 
00047   // the name of the simhit collection
00048   simHitLabel = pset.getUntrackedParameter<InputTag>("simHitLabel");
00049   // the name of the 4D rec hit collection
00050   segment4DLabel = pset.getUntrackedParameter<InputTag>("segment4DLabel");
00051 
00052   //sigma resolution on position
00053   sigmaResPos = pset.getParameter<double>("sigmaResPos");
00054   //sigma resolution on angle
00055   sigmaResAngle = pset.getParameter<double>("sigmaResAngle");
00056   doall = pset.getUntrackedParameter<bool>("doall", false);
00057   local = pset.getUntrackedParameter<bool>("local", false);
00058 
00059   // Create the root file
00060   //theFile = new TFile(rootFileName.c_str(), "RECREATE");
00061   //theFile->cd();
00062   // ----------------------                 
00063   // get hold of back-end interface 
00064   dbe_ = 0;
00065   dbe_ = Service<DQMStore>().operator->();
00066   if ( dbe_ ) {
00067     if (debug) {
00068       dbe_->setVerbose(1);
00069     } else {
00070       dbe_->setVerbose(0);
00071     }
00072   }
00073   if ( dbe_ ) {
00074     if ( debug ) dbe_->showDirStructure();
00075   }
00076 
00077   // Book the histos
00078   h2DHitSuperPhi = new HRes2DHit ("SuperPhi",dbe_,doall,local);
00079   if(doall) h2DHitEff_SuperPhi = new HEff2DHit ("SuperPhi",dbe_);
00080 }
00081 
00082 // Destructor
00083 DTSegment2DSLPhiQuality::~DTSegment2DSLPhiQuality(){
00084 }
00085 
00086 void DTSegment2DSLPhiQuality::endLuminosityBlock(edm::LuminosityBlock const& lumiSeg,
00087     edm::EventSetup const& c){
00088 }
00089 
00090 
00091 
00092 
00093 void DTSegment2DSLPhiQuality::endJob() {
00094   // Write the histos to file
00095   //theFile->cd();
00096   //h2DHitSuperPhi->Write();
00097 
00098   if(doall) h2DHitEff_SuperPhi->ComputeEfficiency();
00099   //h2DHitEff_SuperPhi->Write();
00100 
00101   //if ( rootFileName.size() != 0 && dbe_ ) dbe_->save(rootFileName); 
00102   //theFile->Close();
00103 } 
00104 
00105 // The real analysis
00106 void DTSegment2DSLPhiQuality::analyze(const Event & event, const EventSetup& eventSetup){
00107   //theFile->cd();
00108 
00109   // Get the DT Geometry
00110   ESHandle<DTGeometry> dtGeom;
00111   eventSetup.get<MuonGeometryRecord>().get(dtGeom);
00112 
00113   // Get the SimHit collection from the event
00114   edm::Handle<PSimHitContainer> simHits;
00115   event.getByLabel(simHitLabel, simHits); //FIXME: second string to be removed
00116 
00117   //Map simHits by chamber
00118   map<DTChamberId, PSimHitContainer > simHitsPerCh;
00119   for(PSimHitContainer::const_iterator simHit = simHits->begin();
00120       simHit != simHits->end(); simHit++){
00121     // Create the id of the chamber (the simHits in the DT known their wireId)
00122     DTChamberId chamberId = (((DTWireId(simHit->detUnitId())).layerId()).superlayerId()).chamberId();
00123     // Fill the map
00124     simHitsPerCh[chamberId].push_back(*simHit);
00125   }
00126 
00127   // Get the 4D rechits from the event
00128   Handle<DTRecSegment4DCollection> segment4Ds;
00129   event.getByLabel(segment4DLabel, segment4Ds);
00130 
00131   if(!segment4Ds.isValid()) {
00132     if(debug) cout << "[DTSegment2DSLPhiQuality]**Warning: no 4D Segments with label: " << segment4DLabel
00133    << " in this event, skipping!" << endl;
00134     return;
00135   }
00136     
00137   // Loop over all chambers containing a segment
00138   DTRecSegment4DCollection::id_iterator chamberId;
00139   for (chamberId = segment4Ds->id_begin();
00140        chamberId != segment4Ds->id_end();
00141        ++chamberId){
00142     
00143     //------------------------- simHits ---------------------------//
00144     //Get simHits of each chamber
00145     PSimHitContainer simHits =  simHitsPerCh[(*chamberId)];
00146        
00147     // Map simhits per wire
00148     map<DTWireId, PSimHitContainer > simHitsPerWire = DTHitQualityUtils::mapSimHitsPerWire(simHits);
00149     map<DTWireId, const PSimHit*> muSimHitPerWire = DTHitQualityUtils::mapMuSimHitsPerWire(simHitsPerWire);
00150     int nMuSimHit = muSimHitPerWire.size();
00151     if(nMuSimHit == 0 || nMuSimHit == 1) {
00152       if(debug && nMuSimHit == 1)
00153         cout << "[DTSegment2DSLPhiQuality] Only " << nMuSimHit << " mu SimHit in this chamber, skipping!" << endl;
00154       continue; // If no or only one mu SimHit is found skip this chamber
00155     } 
00156     if(debug)
00157       cout << "=== Chamber " << (*chamberId) << " has " << nMuSimHit << " SimHits" << endl;
00158   
00159     //Find outer and inner mu SimHit to build a segment
00160     pair<const PSimHit*, const PSimHit*> inAndOutSimHit = DTHitQualityUtils::findMuSimSegment(muSimHitPerWire); 
00161 
00162     //Find direction and position of the sim Segment in Chamber RF
00163     pair<LocalVector, LocalPoint> dirAndPosSimSegm = DTHitQualityUtils::findMuSimSegmentDirAndPos(inAndOutSimHit,
00164                                                                                                   (*chamberId),&(*dtGeom));
00165 
00166     LocalVector simSegmLocalDir = dirAndPosSimSegm.first;
00167     LocalPoint simSegmLocalPos = dirAndPosSimSegm.second;
00168     const DTChamber* chamber = dtGeom->chamber(*chamberId);
00169     GlobalPoint simSegmGlobalPos = chamber->toGlobal(simSegmLocalPos);
00170 
00171     //Atan(x/z) angle and x position in SL RF
00172     float angleSimSeg = DTHitQualityUtils::findSegmentAlphaAndBeta(simSegmLocalDir).first;
00173     float posSimSeg = simSegmLocalPos.x(); 
00174     //Position (in eta,phi coordinates) in lobal RF
00175     float etaSimSeg = simSegmGlobalPos.eta(); 
00176     float phiSimSeg = simSegmGlobalPos.phi();
00177 
00178     if(debug)
00179       cout<<"  Simulated segment:  local direction "<<simSegmLocalDir<<endl
00180           <<"                      local position  "<<simSegmLocalPos<<endl
00181           <<"                      angle           "<<angleSimSeg<<endl;
00182     
00183     //---------------------------- recHits --------------------------//
00184     // Get the range of rechit for the corresponding chamberId
00185     bool recHitFound = false;
00186     DTRecSegment4DCollection::range range = segment4Ds->get(*chamberId);
00187     int nsegm = distance(range.first, range.second);
00188     if(debug)
00189       cout << "   Chamber: " << *chamberId << " has " << nsegm
00190            << " 4D segments" << endl;
00191 
00192     if (nsegm!=0) {
00193       // Find the best RecHit: look for the 4D RecHit with the phi angle closest
00194       // to that of segment made of SimHits. 
00195       // RecHits must have delta alpha and delta position within 5 sigma of
00196       // the residual distribution (we are looking for residuals of segments
00197       // usefull to the track fit) for efficency purpose
00198       const DTRecSegment2D* bestRecHit = 0;
00199       bool bestRecHitFound = false;
00200       double deltaAlpha = 99999;
00201 
00202       // Loop over the recHits of this chamberId
00203       for (DTRecSegment4DCollection::const_iterator segment4D = range.first;
00204            segment4D!=range.second;
00205            ++segment4D){
00206         // Check the dimension
00207         if((*segment4D).dimension() != 4) {
00208           if(debug) cout << "[DTSegment2DSLPhiQuality]***Error: This is not 4D segment!!!" << endl;
00209           continue;
00210         }
00211 
00212         //Get 2D superPhi segments from 4D segments
00213         const DTChamberRecSegment2D* phiSegment2D = (*segment4D).phiSegment();
00214         if((*phiSegment2D).dimension() != 2) {
00215           if(debug) cout << "[DTSegment2DQuality]***Error: This is not 2D segment!!!" << endl;
00216           abort();
00217         }
00218 
00219         // Segment Local Direction and position (in Chamber RF)
00220         LocalVector recSegDirection = (*phiSegment2D).localDirection();
00221 
00222         float recSegAlpha = DTHitQualityUtils::findSegmentAlphaAndBeta(recSegDirection).first;
00223         if(debug)
00224           cout << "  RecSegment direction: " << recSegDirection << endl
00225             << "             position : " <<  (*phiSegment2D).localPosition() << endl
00226             << "             alpha    : " << recSegAlpha << endl;
00227 
00228         if(fabs(recSegAlpha - angleSimSeg) < deltaAlpha) {
00229           deltaAlpha = fabs(recSegAlpha - angleSimSeg);
00230           bestRecHit = &(*phiSegment2D);
00231           bestRecHitFound = true;
00232         }
00233       }  // End of Loop over all 4D RecHits of this chambers
00234 
00235       if(bestRecHitFound) {
00236         // Best rechit direction and position in Chamber RF
00237         LocalPoint bestRecHitLocalPos = bestRecHit->localPosition();
00238         LocalVector bestRecHitLocalDir = bestRecHit->localDirection();
00239 
00240         LocalError bestRecHitLocalPosErr = bestRecHit->localPositionError();
00241         LocalError bestRecHitLocalDirErr = bestRecHit->localDirectionError();
00242 
00243         float angleBestRHit = DTHitQualityUtils::findSegmentAlphaAndBeta(bestRecHitLocalDir).first;
00244         if(fabs(angleBestRHit - angleSimSeg) < 5*sigmaResAngle &&
00245            fabs(bestRecHitLocalPos.x() - posSimSeg) < 5*sigmaResPos) {
00246           recHitFound = true;
00247         }
00248 
00249         // Fill Residual histos
00250         h2DHitSuperPhi->Fill(angleSimSeg,
00251                             angleBestRHit,
00252                             posSimSeg,
00253                             bestRecHitLocalPos.x(),
00254                             etaSimSeg,
00255                             phiSimSeg,
00256                             sqrt(bestRecHitLocalPosErr.xx()),
00257                             sqrt(bestRecHitLocalDirErr.xx())
00258                            );
00259       }
00260     } //end of if(nsegm!=0)
00261 
00262       // Fill Efficiency plot
00263     if(doall) {h2DHitEff_SuperPhi->Fill(etaSimSeg,
00264                             phiSimSeg,
00265                             posSimSeg,
00266                             angleSimSeg,
00267                                         recHitFound);}
00268   } // End of loop over chambers
00269 }
00270 
00271 
00272 // Fit a histogram in the range (minfit, maxfit) with a gaussian and
00273 // draw it in the range (min, max)