CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/DQMOffline/CalibMuon/src/DTnoiseDBValidation.cc

Go to the documentation of this file.
00001 
00002 /*
00003  *  See header file for a description of this class.
00004  *
00005  *  $Date: 2011/08/06 13:13:43 $
00006  *  $Revision: 1.7 $
00007  *  \author G. Mila - INFN Torino
00008  */
00009 
00010 #include "DQMOffline/CalibMuon/interface/DTnoiseDBValidation.h"
00011 
00012 // Framework
00013 #include "FWCore/Framework/interface/Event.h"
00014 #include "FWCore/Framework/interface/EventSetup.h"
00015 #include "FWCore/Framework/interface/ESHandle.h"
00016 #include "FWCore/ServiceRegistry/interface/Service.h"
00017 
00018 #include "DQMServices/Core/interface/DQMStore.h"
00019 #include "DQMServices/Core/interface/MonitorElement.h"
00020 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00021 
00022 // Geometry
00023 #include "Geometry/Records/interface/MuonGeometryRecord.h"
00024 #include "Geometry/DTGeometry/interface/DTGeometry.h"
00025 #include "Geometry/DTGeometry/interface/DTTopology.h"
00026 #include "DataFormats/MuonDetId/interface/DTLayerId.h"
00027 #include "DataFormats/MuonDetId/interface/DTWireId.h"
00028 
00029 // Noise record
00030 #include "CondFormats/DTObjects/interface/DTStatusFlag.h"
00031 #include "CondFormats/DataRecord/interface/DTStatusFlagRcd.h"
00032 
00033 #include <stdio.h>
00034 #include <sstream>
00035 #include <math.h>
00036 #include "TFile.h"
00037 #include "TH1F.h"
00038 
00039 using namespace edm;
00040 using namespace std;
00041 
00042 DTnoiseDBValidation::DTnoiseDBValidation(const ParameterSet& pset) {
00043 
00044   LogVerbatim("NoiseDBValidation") << "[DTnoiseDBValidation] Constructor called!";
00045 
00046   // Get the DQM needed services
00047   dbe_ = edm::Service<DQMStore>().operator->();
00048   dbe_->setCurrentFolder("DT/DtCalib/NoiseDBValidation");
00049 
00050   // Get dataBase label
00051   labelDBRef_ = pset.getParameter<string>("labelDBRef");
00052   labelDB_ = pset.getParameter<string>("labelDB");
00053 
00054   diffTestName_ = "noiseDifferenceInRange";
00055   if( pset.exists("diffTestName") ) diffTestName_ = pset.getParameter<string>("diffTestName");
00056  
00057   wheelTestName_ = "noiseWheelOccInRange";
00058   if( pset.exists("wheelTestName") ) wheelTestName_ = pset.getParameter<string>("wheelTestName");
00059 
00060   stationTestName_ = "noiseStationOccInRange";
00061   if( pset.exists("stationTestName") ) stationTestName_ = pset.getParameter<string>("stationTestName");
00062 
00063   sectorTestName_ = "noiseSectorOccInRange";
00064   if( pset.exists("sectorTestName") ) sectorTestName_ = pset.getParameter<string>("sectorTestName");
00065 
00066   layerTestName_ = "noiseLayerOccInRange";
00067   if( pset.exists("layerTestName") ) layerTestName_ = pset.getParameter<string>("layerTestName");
00068   
00069   outputMEsInRootFile_ = false;
00070   if( pset.exists("OutputFileName") ){
00071      outputMEsInRootFile_ = true;
00072      outputFileName_ = pset.getParameter<std::string>("OutputFileName");
00073   }
00074 }
00075 
00076 
00077 DTnoiseDBValidation::~DTnoiseDBValidation(){}
00078 
00079 void DTnoiseDBValidation::beginRun(const edm::Run& run, const EventSetup& setup) {
00080   ESHandle<DTStatusFlag> noiseRef;
00081   setup.get<DTStatusFlagRcd>().get(labelDBRef_, noiseRef);
00082   noiseRefMap_ = &*noiseRef;
00083  
00084   ESHandle<DTStatusFlag> noiseValid;
00085   setup.get<DTStatusFlagRcd>().get(labelDB_, noiseValid);
00086   noiseMap_ = &*noiseValid;
00087 
00088   // Get the geometry
00089   setup.get<MuonGeometryRecord>().get(dtGeom_);
00090 
00091   LogVerbatim("NoiseDBValidation")<<"[DTnoiseDBValidation] Parameters initialization";
00092  
00093   noisyCellsRef_ = 0;
00094   noisyCellsValid_ = 0;
00095 
00096   // Histo booking
00097   diffHisto_ = dbe_->book1D("noisyCellDiff", "percentual (wrt the previous db) total number of noisy cells",1, 0.5, 1.5);
00098   diffHisto_->setBinLabel(1,"Diff");
00099   wheelHisto_ = dbe_->book1D("wheelOccupancy", "percentual noisy cells occupancy per wheel",5, -2.5, 2.5);
00100   wheelHisto_->setBinLabel(1,"Wh-2");
00101   wheelHisto_->setBinLabel(2,"Wh-1");
00102   wheelHisto_->setBinLabel(3,"Wh0");
00103   wheelHisto_->setBinLabel(4,"Wh1");
00104   wheelHisto_->setBinLabel(5,"Wh2");
00105   stationHisto_ = dbe_->book1D("stationOccupancy", "percentual noisy cells occupancy per station",4, 0.5, 4.5);
00106   stationHisto_->setBinLabel(1,"St1");
00107   stationHisto_->setBinLabel(2,"St2");
00108   stationHisto_->setBinLabel(3,"St3");
00109   stationHisto_->setBinLabel(4,"St4");
00110   sectorHisto_ = dbe_->book1D("sectorOccupancy", "percentual noisy cells occupancy per sector",12, 0.5, 12.5);
00111   sectorHisto_->setBinLabel(1,"Sect1");
00112   sectorHisto_->setBinLabel(2,"Sect2");
00113   sectorHisto_->setBinLabel(3,"Sect3");
00114   sectorHisto_->setBinLabel(4,"Sect4");
00115   sectorHisto_->setBinLabel(5,"Sect5");
00116   sectorHisto_->setBinLabel(6,"Sect6");
00117   sectorHisto_->setBinLabel(7,"Sect7");
00118   sectorHisto_->setBinLabel(8,"Sect8");
00119   sectorHisto_->setBinLabel(9,"Sect9");
00120   sectorHisto_->setBinLabel(10,"Sect10");
00121   sectorHisto_->setBinLabel(11,"Sect11");
00122   sectorHisto_->setBinLabel(12,"Sect12");
00123   layerHisto_ = dbe_->book1D("layerOccupancy", "percentual noisy cells occupancy per layer",3, 0.5, 3.5);
00124   layerHisto_->setBinLabel(1,"First 10 bins");
00125   layerHisto_->setBinLabel(2,"Middle bins");
00126   layerHisto_->setBinLabel(3,"Last 10 bins");
00127 
00128   // map initialization
00129   map<int, int> whMap;
00130   whMap.clear();
00131   map<int, int> stMap;
00132   stMap.clear();
00133   map<int, int> sectMap;
00134   sectMap.clear();
00135   map<int, int> layerMap;
00136   layerMap.clear();
00137 
00138   // Loop over reference DB entries
00139   for(DTStatusFlag::const_iterator noise = noiseRefMap_->begin();
00140       noise != noiseRefMap_->end(); noise++) {
00141     DTWireId wireId((*noise).first.wheelId,
00142                     (*noise).first.stationId,
00143                     (*noise).first.sectorId,
00144                     (*noise).first.slId,
00145                     (*noise).first.layerId,
00146                     (*noise).first.cellId);
00147     LogVerbatim("NoiseDBValidation") << "Ref. noisy wire: " << wireId;
00148     ++noisyCellsRef_;
00149   }
00150 
00151   // Loop over validation DB entries
00152   for(DTStatusFlag::const_iterator noise = noiseMap_->begin();
00153       noise != noiseMap_->end(); noise++) {
00154     DTWireId wireId((*noise).first.wheelId,
00155                     (*noise).first.stationId,
00156                     (*noise).first.sectorId,
00157                     (*noise).first.slId,
00158                     (*noise).first.layerId,
00159                     (*noise).first.cellId);
00160     LogVerbatim("NoiseDBValidation") << "Valid. noisy wire: " <<  wireId;
00161     ++noisyCellsValid_;
00162 
00163     whMap[(*noise).first.wheelId]++;
00164     stMap[(*noise).first.stationId]++;
00165     sectMap[(*noise).first.sectorId]++;
00166     
00167     const DTTopology& dtTopo = dtGeom_->layer(wireId.layerId())->specificTopology();
00168     const int lastWire = dtTopo.lastChannel();
00169     if((*noise).first.cellId<=10)
00170       layerMap[1]++;
00171     if((*noise).first.cellId>10 && (*noise).first.cellId<(lastWire-10))
00172       layerMap[2]++;
00173     if((*noise).first.cellId>=(lastWire-10))
00174       layerMap[3]++;
00175 
00176     const DTChamberId chId = wireId.layerId().superlayerId().chamberId();
00177     if( noiseHistoMap_.find(chId) == noiseHistoMap_.end() ) bookHisto(chId);
00178     int binNumber = 4*(wireId.superLayer() - 1) + wireId.layer(); 
00179     noiseHistoMap_[chId]->Fill(wireId.wire(),binNumber);
00180   }
00181 
00182   //histo filling
00183   double scale = 1/double(noisyCellsRef_);
00184   diffHisto_->Fill(1,abs(noisyCellsRef_ - noisyCellsValid_)*scale);
00185 
00186   scale = 1/double(noisyCellsValid_);
00187   for(map<int, int >::const_iterator wheel = whMap.begin();
00188         wheel != whMap.end();
00189         wheel++) {
00190     wheelHisto_->Fill((*wheel).first, ((*wheel).second)*scale);
00191   }
00192   for(map<int, int >::const_iterator station = stMap.begin();
00193         station != stMap.end();
00194         station++) {
00195     stationHisto_->Fill((*station).first, ((*station).second)*scale);
00196   }
00197   for(map<int, int >::const_iterator sector = sectMap.begin();
00198         sector != sectMap.end();
00199         sector++) {
00200     sectorHisto_->Fill((*sector).first, ((*sector).second)*scale);
00201   }
00202   for(map<int, int >::const_iterator layer = layerMap.begin();
00203         layer != layerMap.end();
00204         layer++) {
00205     layerHisto_->Fill((*layer).first, ((*layer).second)*scale);
00206   }
00207 
00208 }
00209 
00210 void DTnoiseDBValidation::endRun(edm::Run const& run, edm::EventSetup const& setup) {
00211 
00212   // test on difference histo
00213   //string testCriterionName;
00214   //testCriterionName = parameters.getUntrackedParameter<string>("diffTestName","noiseDifferenceInRange");
00215   const QReport * theDiffQReport = diffHisto_->getQReport(diffTestName_);
00216   if(theDiffQReport) {
00217       vector<dqm::me_util::Channel> badChannels = theDiffQReport->getBadChannels();
00218       for (vector<dqm::me_util::Channel>::iterator channel = badChannels.begin(); 
00219            channel != badChannels.end(); channel++) {
00220         LogWarning("NoiseDBValidation") << " Bad partial difference of noisy channels! Contents : " << (*channel).getContents();
00221       }
00222   }
00223   //testCriterionName = parameters.getUntrackedParameter<string>("wheelTestName","noiseWheelOccInRange");
00224   const QReport * theDiffQReport2 = wheelHisto_->getQReport(wheelTestName_);
00225   if(theDiffQReport2) {
00226       vector<dqm::me_util::Channel> badChannels = theDiffQReport2->getBadChannels();
00227       for (vector<dqm::me_util::Channel>::iterator channel = badChannels.begin(); 
00228            channel != badChannels.end(); channel++) {
00229         int wheel = (*channel).getBin()-3;
00230         LogWarning("NoiseDBValidation") << " Bad percentual occupancy for wheel : " << wheel << "  Contents : " << (*channel).getContents();
00231       }
00232   }
00233   //testCriterionName = parameters.getUntrackedParameter<string>("stationTestName","noiseStationOccInRange");
00234   const QReport * theDiffQReport3 = stationHisto_->getQReport(stationTestName_);
00235   if(theDiffQReport3) {
00236       vector<dqm::me_util::Channel> badChannels = theDiffQReport3->getBadChannels();
00237       for (vector<dqm::me_util::Channel>::iterator channel = badChannels.begin(); 
00238            channel != badChannels.end(); channel++) {
00239         LogWarning("NoiseDBValidation") << " Bad percentual occupancy for station : " << (*channel).getBin() << "  Contents : " << (*channel).getContents();
00240       }
00241   }
00242   //testCriterionName = parameters.getUntrackedParameter<string>("sectorTestName","noiseSectorOccInRange");
00243   const QReport * theDiffQReport4 = sectorHisto_->getQReport(sectorTestName_);
00244   if(theDiffQReport4) {
00245       vector<dqm::me_util::Channel> badChannels = theDiffQReport4->getBadChannels();
00246       for (vector<dqm::me_util::Channel>::iterator channel = badChannels.begin(); 
00247            channel != badChannels.end(); channel++) {
00248         LogWarning("NoiseDBValidation") << " Bad percentual occupancy for sector : " << (*channel).getBin() << "  Contents : " << (*channel).getContents();
00249       }
00250   }
00251   //testCriterionName = parameters.getUntrackedParameter<string>("layerTestName","noiseLayerOccInRange");
00252   const QReport * theDiffQReport5 = layerHisto_->getQReport(layerTestName_);
00253   if(theDiffQReport5) {
00254       vector<dqm::me_util::Channel> badChannels = theDiffQReport5->getBadChannels();
00255       for (vector<dqm::me_util::Channel>::iterator channel = badChannels.begin(); 
00256            channel != badChannels.end(); channel++) {
00257         if((*channel).getBin()==1)
00258           LogWarning("NoiseDBValidation") << " Bad percentual occupancy for the first 10 wires! Contents : " << (*channel).getContents();
00259         if((*channel).getBin()==2)
00260           LogWarning("NoiseDBValidation") << " Bad percentual occupancy for the middle wires! Contents : " << (*channel).getContents();
00261         if((*channel).getBin()==3)
00262           LogWarning("NoiseDBValidation") << " Bad percentual occupancy for the last 10 wires! Contents : "<<(*channel).getContents();
00263       }
00264   }
00265 
00266 }
00267 
00268 void DTnoiseDBValidation::endJob() {
00269   // Write the histos in a ROOT file
00270   if(outputMEsInRootFile_) dbe_->save(outputFileName_);
00271 }
00272 
00273 void DTnoiseDBValidation::bookHisto(const DTChamberId& chId) {
00274   stringstream histoName;
00275   histoName << "NoiseOccupancy" 
00276             << "_W" << chId.wheel() 
00277             <<"_St" << chId.station() 
00278             << "_Sec" << chId.sector();
00279 
00280   if( noiseHistoMap_.find(chId) == noiseHistoMap_.end() ){ // Redundant check
00281      // Get the chamber from the geometry
00282      int nWiresMax = 0;
00283      const DTChamber* dtchamber = dtGeom_->chamber(chId);
00284      const vector<const DTSuperLayer*> superlayers = dtchamber->superLayers();
00285 
00286      // Loop over layers and find the max # of wires
00287      for(vector<const DTSuperLayer*>::const_iterator sl = superlayers.begin();
00288                                                      sl != superlayers.end(); ++sl) { // loop over SLs
00289         vector<const DTLayer*> layers = (*sl)->layers();
00290         for(vector<const DTLayer*>::const_iterator lay = layers.begin();
00291                                                    lay != layers.end(); ++lay) { // loop over layers
00292            int nWires = (*lay)->specificTopology().channels();
00293            if(nWires > nWiresMax) nWiresMax = nWires;
00294         }
00295      }
00296 
00297      noiseHistoMap_[chId] = dbe_->book2D(histoName.str(),"Noise occupancy",nWiresMax,1,(nWiresMax + 1),12,1,13);
00298      for(int i_sl = 1; i_sl <= 3; ++i_sl) {
00299         for(int i_lay = 1; i_lay <= 4; ++i_lay) {
00300            int binNumber = 4*(i_sl - 1) + i_lay;
00301            stringstream label;
00302            label << "SL" << i_sl << ": L" << i_lay;
00303            noiseHistoMap_[chId]->setBinLabel(binNumber,label.str(),2);
00304         }
00305      }
00306   }
00307   
00308 }