CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/CalibMuon/DTCalibration/plugins/DTResidualCalibration.cc

Go to the documentation of this file.
00001 
00002 /*
00003  *  See header file for a description of this class.
00004  *
00005  *  $Date: 2011/02/22 18:43:20 $
00006  *  $Revision: 1.1 $
00007  */
00008 
00009 #include "DTResidualCalibration.h"
00010 
00011 // Framework
00012 #include "FWCore/Framework/interface/Event.h"
00013 #include "FWCore/Framework/interface/EventSetup.h"
00014 #include "FWCore/Framework/interface/ESHandle.h"
00015 #include "FWCore/Utilities/interface/InputTag.h"
00016 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00017 
00018 //Geometry
00019 #include "Geometry/DTGeometry/interface/DTGeometry.h"
00020 #include "Geometry/Records/interface/MuonGeometryRecord.h"
00021 
00022 //RecHit
00023 #include "DataFormats/DTRecHit/interface/DTRecSegment4DCollection.h"
00024 #include "DataFormats/DTRecHit/interface/DTRecHitCollection.h"
00025 
00026 #include "CommonTools/Utils/interface/TH1AddDirectorySentry.h"
00027 #include "CalibMuon/DTCalibration/interface/DTSegmentSelector.h"
00028 #include "CalibMuon/DTCalibration/interface/DTRecHitSegmentResidual.h"
00029 
00030 #include "TFile.h"
00031 #include "TH1F.h"
00032 #include "TH2F.h"
00033 
00034 #include <algorithm>
00035 
00036 DTResidualCalibration::DTResidualCalibration(const edm::ParameterSet& pset):
00037   select_(pset),
00038   segment4DLabel_(pset.getParameter<edm::InputTag>("segment4DLabel")),
00039   rootBaseDir_(pset.getUntrackedParameter<std::string>("rootBaseDir","DT/Residuals")) {
00040 
00041   edm::LogVerbatim("Calibration") << "[DTResidualCalibration] Constructor called.";
00042 
00043   std::string rootFileName = pset.getUntrackedParameter<std::string>("rootFileName","residuals.root");
00044   rootFile_ = new TFile(rootFileName.c_str(), "RECREATE");
00045   rootFile_->cd();
00046 }
00047 
00048 DTResidualCalibration::~DTResidualCalibration() {
00049   edm::LogVerbatim("Calibration") << "[DTResidualCalibration] Destructor called.";
00050 }
00051 
00052 void DTResidualCalibration::beginJob() {
00053   TH1::SetDefaultSumw2(true);
00054 }
00055 
00056 void DTResidualCalibration::beginRun(const edm::Run& run, const edm::EventSetup& setup) {
00057   
00058   // get the geometry
00059   edm::ESHandle<DTGeometry> dtGeomH; 
00060   setup.get<MuonGeometryRecord>().get(dtGeomH);
00061   dtGeom_ = dtGeomH.product();
00062 
00063   // Loop over all the chambers
00064   if(histoMapTH1F_.size() == 0){         
00065      std::vector<DTChamber*>::const_iterator ch_it = dtGeom_->chambers().begin();        
00066      std::vector<DTChamber*>::const_iterator ch_end = dtGeom_->chambers().end();         
00067      for (; ch_it != ch_end; ++ch_it) {          
00068         DTChamberId chId = (*ch_it)->id();
00069         std::vector<const DTSuperLayer*>::const_iterator sl_it = (*ch_it)->superLayers().begin();        
00070         std::vector<const DTSuperLayer*>::const_iterator sl_end = (*ch_it)->superLayers().end();         
00071         // Loop over the SLs     
00072         for(; sl_it != sl_end; ++sl_it) { 
00073            DTSuperLayerId slId = (*sl_it)->id();
00074            bookHistos(slId);
00075         }
00076      }
00077   }
00078 }
00079 
00080 void DTResidualCalibration::analyze(const edm::Event& event, const edm::EventSetup& setup) {
00081   rootFile_->cd();
00082 
00083   // Get the 4D rechits from the event
00084   edm::Handle<DTRecSegment4DCollection> segment4Ds;
00085   event.getByLabel(segment4DLabel_, segment4Ds);
00086  
00087   // Loop over segments by chamber
00088   DTRecSegment4DCollection::id_iterator chamberIdIt;
00089   for(chamberIdIt = segment4Ds->id_begin(); chamberIdIt != segment4Ds->id_end(); ++chamberIdIt){
00090 
00091      const DTChamber* chamber = dtGeom_->chamber(*chamberIdIt);
00092 
00093      // Get the range for the corresponding ChamberId
00094      DTRecSegment4DCollection::range range = segment4Ds->get((*chamberIdIt));
00095      // Loop over the rechits of this DetUnit
00096      for(DTRecSegment4DCollection::const_iterator segment  = range.first;
00097                                                   segment != range.second; ++segment){
00098 
00099         LogTrace("Calibration") << "Segment local pos (in chamber RF): " << (*segment).localPosition()
00100                                 << "\nSegment global pos: " << chamber->toGlobal((*segment).localPosition());
00101 
00102         if( !select_(*segment, event, setup) ) continue;
00103 
00104         // Get all 1D RecHits at step 3 within the 4D segment
00105         std::vector<DTRecHit1D> recHits1D_S3;
00106   
00107         if( (*segment).hasPhi() ){
00108            const DTChamberRecSegment2D* phiSeg = (*segment).phiSegment();
00109            const std::vector<DTRecHit1D>& phiRecHits = phiSeg->specificRecHits();
00110            std::copy(phiRecHits.begin(), phiRecHits.end(), back_inserter(recHits1D_S3));
00111         }
00112 
00113         if( (*segment).hasZed() ){
00114            const DTSLRecSegment2D* zSeg = (*segment).zSegment();
00115            const std::vector<DTRecHit1D>& zRecHits = zSeg->specificRecHits();
00116            std::copy(zRecHits.begin(), zRecHits.end(), back_inserter(recHits1D_S3));
00117         }
00118 
00119         // Loop over 1D RecHit inside 4D segment
00120         for(std::vector<DTRecHit1D>::const_iterator recHit1D = recHits1D_S3.begin();
00121                                                     recHit1D != recHits1D_S3.end(); ++recHit1D) {
00122            const DTWireId wireId = recHit1D->wireId();
00123 
00124            float segmDistance = segmentToWireDistance(*recHit1D,*segment);
00125            if(segmDistance > 2.1) LogTrace("Calibration") << "WARNING: segment-wire distance: " << segmDistance;
00126            else                   LogTrace("Calibration") << "segment-wire distance: " << segmDistance;
00127 
00128            float residualOnDistance = DTRecHitSegmentResidual().compute(dtGeom_,*recHit1D,*segment);
00129            LogTrace("Calibration") << "Wire Id " << wireId << " residual on distance: " << residualOnDistance;
00130 
00131            fillHistos(wireId.superlayerId(), segmDistance, residualOnDistance);
00132         }
00133      }
00134   }
00135 
00136 }
00137 
00138 float DTResidualCalibration::segmentToWireDistance(const DTRecHit1D& recHit1D, const DTRecSegment4D& segment){
00139 
00140   // Get the layer and the wire position
00141   const DTWireId wireId = recHit1D.wireId();
00142   const DTLayer* layer = dtGeom_->layer(wireId);
00143   float wireX = layer->specificTopology().wirePosition(wireId.wire());
00144       
00145   // Extrapolate the segment to the z of the wire
00146   // Get wire position in chamber RF
00147   // (y and z must be those of the hit to be coherent in the transf. of RF in case of rotations of the layer alignment)
00148   LocalPoint wirePosInLay(wireX,recHit1D.localPosition().y(),recHit1D.localPosition().z());
00149   GlobalPoint wirePosGlob = layer->toGlobal(wirePosInLay);
00150   const DTChamber* chamber = dtGeom_->chamber(wireId.layerId().chamberId());
00151   LocalPoint wirePosInChamber = chamber->toLocal(wirePosGlob);
00152       
00153   // Segment position at Wire z in chamber local frame
00154   LocalPoint segPosAtZWire = segment.localPosition()    + segment.localDirection()*wirePosInChamber.z()/cos(segment.localDirection().theta());
00155       
00156   // Compute the distance of the segment from the wire
00157   int sl = wireId.superlayer();
00158   float segmDistance = -1;
00159   if(sl == 1 || sl == 3) segmDistance = fabs(wirePosInChamber.x() - segPosAtZWire.x());
00160   else if(sl == 2)       segmDistance =  fabs(segPosAtZWire.y() - wirePosInChamber.y());
00161      
00162   return segmDistance;
00163 }
00164 
00165 void DTResidualCalibration::endJob(){
00166   
00167   edm::LogVerbatim("Calibration") << "[DTResidualCalibration] Writing histos to file.";
00168   rootFile_->cd();
00169   rootFile_->Write();
00170   rootFile_->Close();
00171 
00172   /*std::map<DTSuperLayerId, std::vector<TH1F*> >::const_iterator itSlHistos = histoMapTH1F_.begin();
00173   std::map<DTSuperLayerId, std::vector<TH1F*> >::const_iterator itSlHistos_end = histoMapTH1F_.end(); 
00174   for(; itSlHistos != itSlHistos_end; ++itSlHistos){
00175      std::vector<TH1F*>::const_iterator itHistTH1F = (*itSlHistos).second.begin();
00176      std::vector<TH1F*>::const_iterator itHistTH1F_end = (*itSlHistos).second.end();
00177      for(; itHistTH1F != itHistTH1F_end; ++itHistTH1F) (*itHistTH1F)->Write();
00178 
00179      std::vector<TH2F*>::const_iterator itHistTH2F = histoMapTH2F_[(*itSlHistos).first].begin();
00180      std::vector<TH2F*>::const_iterator itHistTH2F_end = histoMapTH2F_[(*itSlHistos).first].end();
00181      for(; itHistTH2F != itHistTH2F_end; ++itHistTH2F) (*itHistTH2F)->Write();
00182   }*/
00183 
00184 }
00185 
00186 void DTResidualCalibration::bookHistos(DTSuperLayerId slId) {
00187   TH1AddDirectorySentry addDir;
00188   rootFile_->cd();
00189 
00190   edm::LogVerbatim("Calibration") << "[DTResidualCalibration] Booking histos for SL: " << slId;
00191 
00192   // Compose the chamber name
00193   std::stringstream wheelStr; wheelStr << slId.wheel(); 
00194   std::stringstream stationStr; stationStr << slId.station();   
00195   std::stringstream sectorStr; sectorStr << slId.sector();      
00196   std::stringstream superLayerStr; superLayerStr << slId.superlayer();
00197   // Define the step
00198   int step = 3;
00199   std::stringstream stepStr; stepStr << step;
00200 
00201   std::string slHistoName =
00202     "_STEP" + stepStr.str() +
00203     "_W" + wheelStr.str() +
00204     "_St" + stationStr.str() +
00205     "_Sec" + sectorStr.str() +
00206     "_SL" + superLayerStr.str();
00207   
00208   edm::LogVerbatim("Calibration") << "Accessing " << rootBaseDir_;
00209   TDirectory* baseDir = rootFile_->GetDirectory(rootBaseDir_.c_str());
00210   if(!baseDir) baseDir = rootFile_->mkdir(rootBaseDir_.c_str());
00211   edm::LogVerbatim("Calibration") << "Accessing " << ("Wheel" + wheelStr.str());
00212   TDirectory* wheelDir = baseDir->GetDirectory(("Wheel" + wheelStr.str()).c_str());
00213   if(!wheelDir) wheelDir = baseDir->mkdir(("Wheel" + wheelStr.str()).c_str());
00214   edm::LogVerbatim("Calibration") << "Accessing " << ("Station" + stationStr.str());
00215   TDirectory* stationDir = wheelDir->GetDirectory(("Station" + stationStr.str()).c_str());
00216   if(!stationDir) stationDir = wheelDir->mkdir(("Station" + stationStr.str()).c_str());
00217   edm::LogVerbatim("Calibration") << "Accessing " << ("Sector" + sectorStr.str());
00218   TDirectory* sectorDir = stationDir->GetDirectory(("Sector" + sectorStr.str()).c_str());
00219   if(!sectorDir) sectorDir = stationDir->mkdir(("Sector" + sectorStr.str()).c_str()); 
00220 
00221   /*std::string dirName = rootBaseDir_ + "/Wheel" + wheelStr.str() +
00222                                        "/Station" + stationStr.str() +
00223                                        "/Sector" + sectorStr.str();
00224 
00225   TDirectory* dir = rootFile_->GetDirectory(dirName.c_str());
00226   if(!dir) dir = rootFile_->mkdir(dirName.c_str());
00227   dir->cd();*/
00228   sectorDir->cd();
00229   // Create the monitor elements
00230   std::vector<TH1F*> histosTH1F;
00231   histosTH1F.push_back(new TH1F(("hResDist"+slHistoName).c_str(),
00232                                  "Residuals on the distance from wire (rec_hit - segm_extr) (cm)",
00233                                  200, -0.4, 0.4));
00234   std::vector<TH2F*> histosTH2F;
00235   histosTH2F.push_back(new TH2F(("hResDistVsDist"+slHistoName).c_str(),
00236                                  "Residuals on the dist. (cm) from wire (rec_hit - segm_extr) vs dist. (cm)",
00237                                  100, 0, 2.5, 200, -0.4, 0.4));
00238   histoMapTH1F_[slId] = histosTH1F;
00239   histoMapTH2F_[slId] = histosTH2F;
00240 }
00241 
00242 // Fill a set of histograms for a given SL 
00243 void DTResidualCalibration::fillHistos(DTSuperLayerId slId,
00244                                        float distance,
00245                                        float residualOnDistance) {
00246   std::vector<TH1F*> const& histosTH1F = histoMapTH1F_[slId];
00247   std::vector<TH2F*> const& histosTH2F = histoMapTH2F_[slId];                          
00248   histosTH1F[0]->Fill(residualOnDistance);
00249   histosTH2F[0]->Fill(distance, residualOnDistance);
00250 }