CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/DQM/DTMonitorClient/src/DTNoiseTest.cc

Go to the documentation of this file.
00001 /*
00002  * 
00003  * $Date: 2011/11/24 09:17:30 $
00004  * $Revision: 1.22 $
00005  * \authors:
00006  *  A. Gresele - INFN Trento
00007  *  G. Mila - INFN Torino
00008  *  M. Zanetti - CERN PH
00009  *
00010  */
00011 
00012 #include "DQM/DTMonitorClient/src/DTNoiseTest.h"
00013 
00014 // Framework
00015 #include <FWCore/Framework/interface/EventSetup.h>
00016 
00017 
00018 // Geometry
00019 #include "Geometry/Records/interface/MuonGeometryRecord.h"
00020 #include "Geometry/DTGeometry/interface/DTGeometry.h"
00021 #include "Geometry/DTGeometry/interface/DTLayer.h"
00022 #include "Geometry/DTGeometry/interface/DTTopology.h"
00023 
00024 
00025 #include "DQMServices/Core/interface/DQMStore.h"
00026 #include "DQMServices/Core/interface/MonitorElement.h"
00027 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00028 
00029 #include <stdio.h>
00030 #include <sstream>
00031 #include <math.h>
00032 
00033 using namespace edm;
00034 using namespace std;
00035 
00036 
00037 
00038 DTNoiseTest::DTNoiseTest(const edm::ParameterSet& ps){
00039 
00040   edm::LogVerbatim ("noise") <<"[DTNoiseTest]: Constructor";  
00041 
00042   parameters = ps;
00043   
00044   dbe = edm::Service<DQMStore>().operator->();
00045   dbe->setCurrentFolder("DT/Tests/Noise");
00046 
00047   prescaleFactor = parameters.getUntrackedParameter<int>("diagnosticPrescale", 1);
00048 
00049 }
00050 
00051 
00052 
00053 DTNoiseTest::~DTNoiseTest(){
00054 
00055   edm::LogVerbatim ("noise") <<"DTNoiseTest: analyzed " << updates << " events";
00056 
00057 }
00058 
00059 
00060 
00061 void DTNoiseTest::beginJob(){
00062 
00063   edm::LogVerbatim ("noise") <<"[DTNoiseTest]: BeginJob";
00064 
00065   updates = 0;
00066 
00067 }
00068 
00069 
00070 void DTNoiseTest::beginRun(const edm::Run& run, const edm::EventSetup& context){
00071 
00072   edm::LogVerbatim ("noise") <<"[DTNoiseTest]: BeginRun";
00073 
00074   // Get the geometry
00075   context.get<MuonGeometryRecord>().get(muonGeom);
00076 
00077 }
00078 
00079 
00080 
00081 void DTNoiseTest::beginLuminosityBlock(LuminosityBlock const& lumiSeg, EventSetup const& context) {
00082 
00083   edm::LogVerbatim ("noise") <<"[DTNoiseTest]: Begin of LS transition";
00084 
00085   // Get the run number
00086   run = lumiSeg.run();
00087 
00088 }
00089 
00090 
00091 void DTNoiseTest::analyze(const edm::Event& e, const edm::EventSetup& context){
00092 
00093   updates++;
00094   edm::LogVerbatim ("noise") << "[DTNoiseTest]: "<<updates<<" events";
00095 
00096 }
00097 
00098 
00099 void DTNoiseTest::endLuminosityBlock(LuminosityBlock const& lumiSeg, EventSetup const& context) {
00100 
00101   // counts number of updats (online mode) or number of events (standalone mode)
00102   //updates++;
00103   // if running in standalone perform diagnostic only after a reasonalbe amount of events
00104   //if ( parameters.getUntrackedParameter<bool>("runningStandalone", false) &&
00105   //     updates%parameters.getUntrackedParameter<int>("diagnosticPrescale", 1000) != 0 ) return;
00106 
00107   //edm::LogVerbatim ("noise") <<"[DTNoiseTest]: "<<updates<<" updates";
00108 
00109 
00110   edm::LogVerbatim ("noise") <<"[DTNoiseTest]: End of LS transition, performing the DQM client operation";
00111 
00112   // counts number of lumiSegs 
00113   nLumiSegs = lumiSeg.id().luminosityBlock();
00114 
00115   // prescale factor
00116   if ( nLumiSegs%prescaleFactor != 0 ) return;
00117 
00118   edm::LogVerbatim ("noise") <<"[DTNoiseTest]: "<<nLumiSegs<<" updates";
00119 
00120   ESHandle<DTStatusFlag> statusMap;
00121   context.get<DTStatusFlagRcd>().get(statusMap);
00122   
00123   context.get<DTTtrigRcd>().get(tTrigMap);
00124   float tTrig, tTrigRMS, kFactor;
00125 
00126   string histoTag;
00127   // loop over chambers
00128   vector<DTChamber*>::const_iterator ch_it = muonGeom->chambers().begin();
00129   vector<DTChamber*>::const_iterator ch_end = muonGeom->chambers().end();
00130 
00131   for (; ch_it != ch_end; ++ch_it) {
00132     DTChamberId ch = (*ch_it)->id();
00133     vector<const DTSuperLayer*>::const_iterator sl_it = (*ch_it)->superLayers().begin(); 
00134     vector<const DTSuperLayer*>::const_iterator sl_end = (*ch_it)->superLayers().end();
00135             
00136     MonitorElement * noiseME = dbe->get(getMEName(ch));
00137     if (noiseME) {
00138       TH2F * noiseHisto = noiseME->getTH2F();
00139 
00140       // WARNING uncorrect normalization!! TO BE PROVIDED CENTRALLY
00141       double nevents = (int) noiseHisto->GetEntries();  
00142         
00143       double normalization =0;
00144 
00145       float average=0;
00146       float nOfChannels=0;
00147       float noiseStatistics=0;
00148       int newNoiseChannels=0;
00149 
00150       for(; sl_it != sl_end; ++sl_it) {
00151         const DTSuperLayerId & slID = (*sl_it)->id();
00152             
00153         // ttrig and rms are counts
00154         tTrigMap->get(slID, tTrig, tTrigRMS, kFactor, DTTimeUnits::counts);
00155         if (tTrig==0) tTrig=1;
00156         const double ns_s = 1e9*(32/25);
00157         normalization = ns_s/float(tTrig*nevents);
00158             
00159         noiseHisto->Scale(normalization);
00160             
00161         // loop over layers
00162             
00163         for (int binY=(slID.superLayer()-1)*4+1 ; binY <= (slID.superLayer()-1)*4+4; binY++) {
00164               
00165           int Y = binY - 4*(slID.superLayer()-1);
00166               
00167           // the layer
00168               
00169           const DTLayerId theLayer(slID,Y);
00170              
00171           // loop over channels 
00172           for (int binX=1; binX <= noiseHisto->GetNbinsX(); binX++) {
00173                 
00174             if (noiseHisto->GetBinContent(binX,binY) > parameters.getUntrackedParameter<int>("HzThreshold", 300))
00175               theNoisyChannels.push_back(DTWireId(theLayer, binX));
00176                   
00177             // get rid of the dead channels
00178             else {
00179               average += noiseHisto->GetBinContent(binX,binY); 
00180               nOfChannels++; 
00181             }
00182           }
00183         }
00184             
00185         if (nOfChannels) noiseStatistics = average/nOfChannels;
00186         histoTag = "NoiseAverage";
00187 
00188         if (histos[histoTag].find((*ch_it)->id().rawId()) == histos[histoTag].end()) bookHistos((*ch_it)->id(),string("NoiseAverage"), histoTag );
00189         histos[histoTag].find((*ch_it)->id().rawId())->second->setBinContent(slID.superLayer(),noiseStatistics); 
00190 
00191         for ( vector<DTWireId>::const_iterator nb_it = theNoisyChannels.begin();
00192               nb_it != theNoisyChannels.end(); ++nb_it) {
00193               
00194           bool isNoisy = false;
00195           bool isFEMasked = false;
00196           bool isTDCMasked = false;
00197           bool isTrigMask = false;
00198           bool isDead = false;
00199           bool isNohv = false;
00200           statusMap->cellStatus((*nb_it), isNoisy, isFEMasked, isTDCMasked, isTrigMask, isDead, isNohv);
00201                       
00202           if (!isNoisy) newNoiseChannels++;
00203         }
00204         theNoisyChannels.clear();
00205         histoTag = "NewNoisyChannels";
00206         if (histos[histoTag].find((*ch_it)->id().rawId()) == histos[histoTag].end()) bookHistos((*ch_it)->id(),string("NewNoisyChannels"), histoTag );
00207         histos[histoTag].find((*ch_it)->id().rawId())->second->setBinContent(slID.superLayer(), newNoiseChannels);   
00208       }
00209     }
00210     //To compute the Noise Mean test
00211     vector<const DTSuperLayer*>::const_iterator sl2_it = (*ch_it)->superLayers().begin(); 
00212     vector<const DTSuperLayer*>::const_iterator sl2_end = (*ch_it)->superLayers().end();
00213     for(; sl2_it != sl2_end; ++sl2_it) {
00214       vector<const DTLayer*>::const_iterator l_it = (*sl2_it)->layers().begin(); 
00215       vector<const DTLayer*>::const_iterator l_end = (*sl2_it)->layers().end();
00216       for(; l_it != l_end; ++l_it) {
00217         
00218         DTLayerId lID = (*l_it)->id();
00219         MonitorElement * noisePerEventME = dbe->get(getMEName(lID));
00220 
00221         if (noisePerEventME) {
00222           TH2F * noiseHistoPerEvent = noisePerEventME->getTH2F();
00223           int nWires = muonGeom->layer(lID)->specificTopology().channels();
00224           double MeanNumerator=0, MeanDenominator=0;
00225           histoTag = "MeanDigiPerEvent";
00226           for (int w=1; w<=nWires; w++){
00227             for(int numDigi=1; numDigi<=10; numDigi++){
00228               MeanNumerator+=(noiseHistoPerEvent->GetBinContent(w,numDigi)*(numDigi-1));
00229               MeanDenominator+=noiseHistoPerEvent->GetBinContent(w,numDigi);
00230             }
00231             double Mean=MeanNumerator/MeanDenominator;
00232             if (histos[histoTag].find((*l_it)->id().rawId()) == histos[histoTag].end()) bookHistos((*l_it)->id(),nWires, string("MeanDigiPerEvent"), histoTag );
00233             histos[histoTag].find((*l_it)->id().rawId())->second->setBinContent(w, Mean);   
00234           } 
00235         }
00236       }
00237     }
00238   }
00239   
00240   // Noise Mean test 
00241   histoTag = "MeanDigiPerEvent";
00242   string MeanCriterionName = parameters.getUntrackedParameter<string>("meanTestName","NoiseMeanInRange");
00243   for(map<uint32_t, MonitorElement*>::const_iterator hMean = histos[histoTag].begin();
00244       hMean != histos[histoTag].end();
00245       hMean++) {
00246     const QReport * theMeanQReport = (*hMean).second->getQReport(MeanCriterionName);
00247     if(theMeanQReport) {
00248       vector<dqm::me_util::Channel> badChannels = theMeanQReport->getBadChannels();
00249       for (vector<dqm::me_util::Channel>::iterator channel = badChannels.begin(); 
00250            channel != badChannels.end(); channel++) {
00251         LogVerbatim ("tTrigCalibration")<<"LayerId : "<<(*hMean).first<<" Bad mean channels: "<<(*channel).getBin()<<"  Contents : "<<(*channel).getContents();
00252         // FIXME: getMessage() sometimes returns and invalid string (null pointer inside QReport data member)
00253         // LogVerbatim ("tTrigCalibration") << "-------- LayerId : "<<(*hMean).first<<"  "<<theMeanQReport->getMessage()<<" ------- "<<theMeanQReport->getStatus(); 
00254       }
00255     }
00256   }
00257   
00258 }
00259 
00260 
00261 
00262 void DTNoiseTest::endJob(){
00263 
00264   edm::LogVerbatim ("noise") <<"[DTNoiseTest] endjob called!";
00265   
00266   //if ( parameters.getUntrackedParameter<bool>("writeHisto", true) ) 
00267   //  dbe->save(parameters.getUntrackedParameter<string>("outputFile", "DTNoiseTest.root"));
00268   
00269   dbe->rmdir("DT/Tests/Noise");
00270 }
00271 
00272 
00273 string DTNoiseTest::getMEName(const DTChamberId & ch) {
00274   
00275   stringstream wheel; wheel << ch.wheel();      
00276   stringstream station; station << ch.station();        
00277   stringstream sector; sector << ch.sector();   
00278   
00279   string folderRoot = parameters.getUntrackedParameter<string>("folderRoot", "Collector/FU0/");
00280   string folderTag = parameters.getUntrackedParameter<string>("folderTag", "Occupancies");
00281   string folderName = 
00282     folderRoot + "DT/DTDigiTask/Wheel" +  wheel.str() +
00283     "/Station" + station.str() +
00284     "/Sector" + sector.str() + "/" + folderTag + "/";
00285 
00286   string histoTag = parameters.getUntrackedParameter<string>("histoTag", "OccupancyNoise_perCh");
00287   string histoname = folderName + histoTag  
00288     + "_W" + wheel.str() 
00289     + "_St" + station.str() 
00290     + "_Sec" + sector.str(); 
00291     
00292     
00293   return histoname;
00294   
00295 }
00296 
00297 string DTNoiseTest::getMEName(const DTLayerId & ly) {
00298   
00299   stringstream wheel; wheel << ly.wheel();      
00300   stringstream station; station << ly.station();        
00301   stringstream sector; sector << ly.sector();
00302   stringstream superLayer; superLayer << ly.superlayer();
00303   stringstream layer; layer << ly.layer();
00304   
00305   string folderRoot = parameters.getUntrackedParameter<string>("folderRoot", "Collector/FU0/");
00306   string folderTag = parameters.getUntrackedParameter<string>("folderTagForDigiPerEventTest", "DigiPerEvent");
00307   string folderName = 
00308     folderRoot + "DT/DTDigiForNoiseTask/Wheel" +  wheel.str() +
00309     "/Station" + station.str() +
00310     "/Sector" + sector.str() + "/" + folderTag + "/";
00311   
00312   string histoTag = parameters.getUntrackedParameter<string>("histoTagForDigiPerEventTest", "DigiPerEvent");
00313   string histoname = folderName + histoTag  
00314     + "_W" + wheel.str() 
00315     + "_St" + station.str() 
00316     + "_Sec" + sector.str()
00317     + "_SL" + superLayer.str()
00318     + "_L" + layer.str();
00319     
00320     
00321   return histoname;
00322 
00323 }
00324 
00325 
00326 void DTNoiseTest::bookHistos(const DTChamberId & ch, string folder, string histoTag ) {
00327 
00328   stringstream wheel; wheel << ch.wheel();      
00329   stringstream station; station << ch.station();        
00330   stringstream sector; sector << ch.sector();   
00331 
00332   dbe->setCurrentFolder("DT/Tests/Noise/" + folder);
00333 
00334   string histoName =  histoTag + "W" + wheel.str() + "_St" + station.str() + "_Sec" + sector.str(); 
00335  
00336   if (folder == "NoiseAverage")
00337   (histos[histoTag])[ch.rawId()] = dbe->book1D(histoName.c_str(),histoName.c_str(),3,0,3);
00338  
00339   if ( folder == "NewNoisyChannels")
00340   (histos[histoTag])[ch.rawId()] = dbe->book1D(histoName.c_str(),histoName.c_str(),3,0,3);
00341   
00342 }
00343 
00344 
00345 void DTNoiseTest::bookHistos(const DTLayerId & lId, int nWires, string folder, string histoTag) {
00346 
00347   stringstream wheel; wheel << lId.superlayerId().wheel();
00348   stringstream station; station << lId.superlayerId().station();        
00349   stringstream sector; sector << lId.superlayerId().sector();
00350   stringstream superLayer; superLayer << lId.superlayerId().superlayer();
00351   stringstream layer; layer << lId.layer();
00352 
00353   string histoName = histoTag + "_W" + wheel.str() + "_St" + station.str() + "_Sec" + sector.str() +  "_SL" + superLayer.str() +  "_L" + layer.str();
00354 
00355   dbe->setCurrentFolder("DT/Tests/Noise/" + folder +
00356                         "/Wheel" + wheel.str() +
00357                         "/Station" + station.str() +
00358                         "/Sector" + sector.str());
00359 
00360   (histos[histoTag])[lId.rawId()] = dbe->book1D(histoName.c_str(),histoName.c_str(),nWires,0,nWires);
00361 
00362 }