CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/DQM/DTMonitorModule/src/DTResolutionAnalysisTask.cc

Go to the documentation of this file.
00001 
00002 /*
00003  *  See header file for a description of this class.
00004  *
00005  *  $Date: 2012/02/17 16:04:49 $
00006  *  $Revision: 1.23 $
00007  *  \author G. Cerminara - INFN Torino
00008  */
00009 
00010 #include "DTResolutionAnalysisTask.h"
00011 
00012 // Framework
00013 #include "FWCore/Framework/interface/Event.h"
00014 #include "FWCore/Framework/interface/EventSetup.h"
00015 #include "FWCore/ServiceRegistry/interface/Service.h"
00016 #include "FWCore/Framework/interface/LuminosityBlock.h"
00017 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00018 
00019 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00020 
00021 #include "DQMServices/Core/interface/DQMStore.h"
00022 #include "DQMServices/Core/interface/MonitorElement.h"
00023 
00024 //Geometry
00025 #include "Geometry/DTGeometry/interface/DTGeometry.h"
00026 #include "Geometry/Records/interface/MuonGeometryRecord.h"
00027 
00028 //RecHit
00029 #include "DataFormats/DTRecHit/interface/DTRecSegment4DCollection.h"
00030 #include "DataFormats/DTRecHit/interface/DTRecHitCollection.h"
00031 
00032 
00033 #include <iterator>
00034 
00035 using namespace edm;
00036 using namespace std;
00037 
00038 DTResolutionAnalysisTask::DTResolutionAnalysisTask(const ParameterSet& pset) {
00039 
00040   edm::LogVerbatim ("DTDQM|DTMonitorModule|DTResolutionAnalysisTask") << "[DTResolutionAnalysisTask] Constructor called!" << endl;
00041 
00042   // the name of the 4D rec hits collection
00043   theRecHits4DLabel = pset.getParameter<string>("recHits4DLabel");
00044   
00045   prescaleFactor = pset.getUntrackedParameter<int>("diagnosticPrescale", 1);
00046   resetCycle = pset.getUntrackedParameter<int>("ResetCycle", -1);
00047   // top folder for the histograms in DQMStore
00048   topHistoFolder = pset.getUntrackedParameter<string>("topHistoFolder","DT/02-Segments");
00049 
00050   thePhiHitsCut = pset.getUntrackedParameter<u_int32_t>("phiHitsCut",8);
00051   theZHitsCut = pset.getUntrackedParameter<u_int32_t>("zHitsCut",4);
00052 
00053 }
00054 
00055 
00056 DTResolutionAnalysisTask::~DTResolutionAnalysisTask(){
00057 
00058   edm::LogVerbatim ("DTDQM|DTMonitorModule|DTResolutionAnalysisTask") << "[DTResolutionAnalysisTask] Destructor called!" << endl;
00059 
00060 }
00061 
00062 void DTResolutionAnalysisTask::beginRun(const Run& run, const EventSetup& setup) {
00063 
00064   // Get the DQM needed services
00065   theDbe = edm::Service<DQMStore>().operator->();
00066   //   theDbe->setCurrentFolder("DT/02-Segments");
00067 
00068   // Get the DT Geometry
00069   setup.get<MuonGeometryRecord>().get(dtGeom);
00070 
00071   // Book the histograms
00072   vector<DTChamber*> chambers = dtGeom->chambers();
00073   for(vector<DTChamber*>::const_iterator chamber = chambers.begin();
00074       chamber != chambers.end(); ++chamber) {  // Loop over all chambers
00075     DTChamberId dtChId = (*chamber)->id();
00076     for(int sl = 1; sl <= 3; ++sl) { // Loop over SLs
00077       if(dtChId.station() == 4 && sl == 2) continue;
00078       const  DTSuperLayerId dtSLId(dtChId,sl);
00079       bookHistos(dtSLId);
00080     }
00081   }
00082 
00083 }
00084 
00085 
00086 
00087 
00088 void DTResolutionAnalysisTask::beginLuminosityBlock(const LuminosityBlock& lumiSeg,
00089                                                     const EventSetup& context) {
00090 
00091   edm::LogVerbatim ("DTDQM|DTMonitorModule|DTResolutionAnalysisTask") << "[DTResolutionTask]: Begin of LS transition"<<endl;
00092   
00093   if(resetCycle != -1 && lumiSeg.id().luminosityBlock() % resetCycle == 0) {
00094     for(map<DTSuperLayerId, vector<MonitorElement*> > ::const_iterator histo = histosPerSL.begin();
00095         histo != histosPerSL.end();
00096         histo++) {
00097       int size = (*histo).second.size();
00098       for(int i=0; i<size; i++){
00099         (*histo).second[i]->Reset();
00100       }
00101     }
00102   }
00103 }
00104 
00105 
00106 void DTResolutionAnalysisTask::endJob(){
00107 
00108  edm::LogVerbatim ("DTDQM|DTMonitorModule|DTResolutionAnalysisTask") << "[DTResolutionAnalysisTask] endjob called!"<<endl;
00109 
00110 }
00111   
00112 
00113 
00114 void DTResolutionAnalysisTask::analyze(const edm::Event& event, const edm::EventSetup& setup) {
00115 
00116   edm::LogVerbatim ("DTDQM|DTMonitorModule|DTResolutionAnalysisTask") << "[DTResolutionAnalysisTask] Analyze #Run: " << event.id().run()
00117          << " #Event: " << event.id().event() << endl;
00118 
00119   
00120   // Get the 4D segment collection from the event
00121   edm::Handle<DTRecSegment4DCollection> all4DSegments;
00122   event.getByLabel(theRecHits4DLabel, all4DSegments);
00123 
00124   // check the validity of the collection 
00125   if(!all4DSegments.isValid()) return;
00126 
00127   // Loop over all chambers containing a segment
00128   DTRecSegment4DCollection::id_iterator chamberId;
00129   for (chamberId = all4DSegments->id_begin();
00130        chamberId != all4DSegments->id_end();
00131        ++chamberId) {
00132     // Get the range for the corresponding ChamerId
00133     DTRecSegment4DCollection::range  range = all4DSegments->get(*chamberId);
00134     //     int nsegm = distance(range.first, range.second);
00135     //edm::LogVerbatim ("DTDQM|DTMonitorModule|DTResolutionAnalysisTask") << "   Chamber: " << *chamberId << " has " << nsegm
00136     //<< " 4D segments" << endl;
00137     // Get the chamber
00138     const DTChamber* chamber = dtGeom->chamber(*chamberId);
00139 
00140     // Loop over the rechits of this ChamerId
00141     for (DTRecSegment4DCollection::const_iterator segment4D = range.first;
00142          segment4D!=range.second;
00143          ++segment4D) {
00144       //edm::LogVerbatim ("DTDQM|DTMonitorModule|DTResolutionAnalysisTask") << "   == RecSegment dimension: " << (*segment4D).dimension() << endl;
00145       
00146       // If Statio != 4 skip RecHits with dimension != 4
00147       // For the Station 4 consider 2D RecHits
00148       if((*chamberId).station() != 4 && (*segment4D).dimension() != 4) {
00149         edm::LogVerbatim ("DTDQM|DTMonitorModule|DTResolutionAnalysisTask") << "[DTResolutionAnalysisTask]***Warning: RecSegment dimension is not 4 but "
00150                << (*segment4D).dimension() << "!" << endl;
00151         continue;
00152       } else if((*chamberId).station() == 4 && (*segment4D).dimension() != 2) {
00153         edm::LogVerbatim ("DTDQM|DTMonitorModule|DTResolutionAnalysisTask") << "[DTResolutionAnalysisTask]***Warning: RecSegment dimension is not 2 but "
00154                << (*segment4D).dimension() << "!" << endl;
00155         continue;
00156       }
00157 
00158 
00159       // Get all 1D RecHits at step 3 within the 4D segment
00160       vector<DTRecHit1D> recHits1D_S3;
00161     
00162 
00163       // Get 1D RecHits at Step 3 and select only events with
00164       // 8 hits in phi and 4 hits in theta (if any)
00165 
00166       if((*segment4D).hasPhi()) { // has phi component
00167         const DTChamberRecSegment2D* phiSeg = (*segment4D).phiSegment();
00168         vector<DTRecHit1D> phiRecHits = phiSeg->specificRecHits();
00169 
00170         if(phiRecHits.size() < thePhiHitsCut) {
00171           //edm::LogVerbatim ("DTDQM|DTMonitorModule|DTResolutionAnalysisTask") << "[DTResolutionAnalysisTask] Phi segments has: " << phiRecHits.size()
00172           //<< " hits" << endl; // FIXME: info output
00173           continue;
00174         }
00175         copy(phiRecHits.begin(), phiRecHits.end(), back_inserter(recHits1D_S3));
00176       } else {
00177         //edm::LogVerbatim ("DTDQM|DTMonitorModule|DTResolutionAnalysisTask") << "[DTResolutionAnalysisTask] 4D segment has not phi component!" << endl;
00178       }
00179 
00180       if((*segment4D).hasZed()) {
00181         const DTSLRecSegment2D* zSeg = (*segment4D).zSegment();
00182         vector<DTRecHit1D> zRecHits = zSeg->specificRecHits();
00183         if(zRecHits.size() < theZHitsCut) {
00184           //edm::LogVerbatim ("DTDQM|DTMonitorModule|DTResolutionAnalysisTask") << "[DTResolutionAnalysisTask] Theta segments has: " << zRecHits.size()
00185           //<< " hits, skipping" << endl; // FIXME: info output
00186           continue;
00187         }
00188         copy(zRecHits.begin(), zRecHits.end(), back_inserter(recHits1D_S3));
00189       }
00190 
00191       // Loop over 1D RecHit inside 4D segment
00192       for(vector<DTRecHit1D>::const_iterator recHit1D = recHits1D_S3.begin();
00193           recHit1D != recHits1D_S3.end();
00194           recHit1D++) {
00195         const DTWireId wireId = (*recHit1D).wireId();
00196         
00197         // Get the layer and the wire position
00198         const DTLayer* layer = chamber->superLayer(wireId.superlayerId())->layer(wireId.layerId());
00199         float wireX = layer->specificTopology().wirePosition(wireId.wire());
00200 
00201         // Distance of the 1D rechit from the wire
00202         float distRecHitToWire = fabs(wireX - (*recHit1D).localPosition().x());
00203         
00204         // Extrapolate the segment to the z of the wire
00205         
00206         // Get wire position in chamber RF
00207         LocalPoint wirePosInLay(wireX,(*recHit1D).localPosition().y(),(*recHit1D).localPosition().z());
00208         GlobalPoint wirePosGlob = layer->toGlobal(wirePosInLay);
00209         LocalPoint wirePosInChamber = chamber->toLocal(wirePosGlob);
00210 
00211         // Segment position at Wire z in chamber local frame
00212         LocalPoint segPosAtZWire = (*segment4D).localPosition()
00213           + (*segment4D).localDirection()*wirePosInChamber.z()/cos((*segment4D).localDirection().theta());
00214         
00215         // Compute the distance of the segment from the wire
00216         int sl = wireId.superlayer();
00217   
00218         double distSegmToWire = -1;     
00219         if(sl == 1 || sl == 3) {
00220           // RPhi SL
00221           distSegmToWire = fabs(wirePosInChamber.x() - segPosAtZWire.x());
00222         } else if(sl == 2) {
00223           // RZ SL
00224           distSegmToWire = fabs(wirePosInChamber.y() - segPosAtZWire.y());
00225         }
00226 
00227         if(distSegmToWire > 2.1)
00228           edm::LogVerbatim ("DTDQM|DTMonitorModule|DTResolutionAnalysisTask") << "  Warning: dist segment-wire: " << distSegmToWire << endl;
00229 
00230         double residual = distRecHitToWire - distSegmToWire;
00231         // FIXME: Fill the histos
00232         fillHistos(wireId.superlayerId(), distSegmToWire, residual);
00233         
00234         //      edm::LogVerbatim ("DTDQM|DTMonitorModule|DTResolutionAnalysisTask") << "     Dist. segment extrapolation - wire (cm): " << distSegmToWire << endl;
00235         //edm::LogVerbatim ("DTDQM|DTMonitorModule|DTResolutionAnalysisTask") << "     Dist. RecHit - wire (cm): " << distRecHitToWire << endl;
00236         //edm::LogVerbatim ("DTDQM|DTMonitorModule|DTResolutionAnalysisTask") << "     Residual (cm): " << residual << endl;
00237         
00238                           
00239       }// End of loop over 1D RecHit inside 4D segment
00240     }// End of loop over the rechits of this ChamerId
00241   }
00242   // -----------------------------------------------------------------------------
00243 }
00244 
00245 
00246 // Book a set of histograms for a given SL
00247 void DTResolutionAnalysisTask::bookHistos(DTSuperLayerId slId) {
00248   
00249   edm::LogVerbatim ("DTDQM|DTMonitorModule|DTResolutionAnalysisTask") << "   Booking histos for SL: " << slId << endl;
00250 
00251   // Compose the chamber name
00252   stringstream wheel; wheel << slId.wheel();    
00253   stringstream station; station << slId.station();      
00254   stringstream sector; sector << slId.sector(); 
00255   stringstream superLayer; superLayer << slId.superlayer();     
00256 
00257   
00258   string slHistoName =
00259     "_W" + wheel.str() +
00260     "_St" + station.str() +
00261     "_Sec" + sector.str() +
00262     "_SL" + superLayer.str();
00263   
00264   theDbe->setCurrentFolder(topHistoFolder + "/Wheel" + wheel.str() +
00265                            "/Sector" + sector.str() +
00266                            "/Station" + station.str());
00267   // Create the monitor elements
00268   vector<MonitorElement *> histos;
00269   // Note hte order matters
00270   histos.push_back(theDbe->book1D("hResDist"+slHistoName,
00271                                   "Residuals on the distance from wire (rec_hit - segm_extr) (cm)",
00272                                   200, -0.4, 0.4));
00273   //FIXME: 2D plot removed to reduce the # of ME
00274 //   histos.push_back(theDbe->book2D("hResDistVsDist"+slHistoName,
00275 //                                "Residuals on the distance (cm) from wire (rec_hit - segm_extr) vs distance  (cm)",
00276 //                                100, 0, 2.5, 200, -0.4, 0.4));
00277   histosPerSL[slId] = histos;
00278 }
00279 
00280 
00281 // Fill a set of histograms for a given SL 
00282 void DTResolutionAnalysisTask::fillHistos(DTSuperLayerId slId,
00283                                       float distExtr,
00284                                       float residual) {
00285   vector<MonitorElement *> histos =  histosPerSL[slId];                          
00286   histos[0]->Fill(residual);
00287   //FIXME: 2D plot removed to reduce the # of ME
00288   //   histos[1]->Fill(distExtr, residual); 
00289 
00290 }
00291 
00292 
00293