CMS 3D CMS Logo

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

Go to the documentation of this file.
00001 
00002 /*
00003  *  See header file for a description of this class.
00004  *
00005  *  $Date: 2011/06/10 13:23:26 $
00006  *  $Revision: 1.20 $
00007  *  \authors G. Mila , G. Cerminara - INFN Torino
00008  */
00009 
00010 #include "DTNoiseTask.h"
00011 
00012 // Framework
00013 #include "FWCore/Framework/interface/Event.h"
00014 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00015 #include <FWCore/Framework/interface/EventSetup.h>
00016 #include "FWCore/ServiceRegistry/interface/Service.h"
00017 
00018 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00019 
00020 // Geometry
00021 #include "Geometry/Records/interface/MuonGeometryRecord.h"
00022 #include "Geometry/DTGeometry/interface/DTGeometry.h"
00023 #include "Geometry/DTGeometry/interface/DTLayer.h"
00024 #include "Geometry/DTGeometry/interface/DTTopology.h"
00025 
00026 // Digi
00027 #include <DataFormats/DTDigi/interface/DTDigi.h>
00028 #include "DataFormats/DTDigi/interface/DTDigiCollection.h"
00029 #include "CondFormats/DataRecord/interface/DTStatusFlagRcd.h"
00030 #include "CondFormats/DTObjects/interface/DTStatusFlag.h"
00031 #include "CondFormats/DTObjects/interface/DTReadOutMapping.h"
00032 
00033 // RecHit
00034 #include "DataFormats/DTRecHit/interface/DTRecSegment4DCollection.h"
00035 
00036 // Database
00037 #include <CondFormats/DTObjects/interface/DTTtrig.h>
00038 #include <CondFormats/DataRecord/interface/DTTtrigRcd.h>
00039 
00040 #include <sstream>
00041 #include <string>
00042 
00043 using namespace edm;
00044 using namespace std;
00045 
00046 
00047 
00048 DTNoiseTask::DTNoiseTask(const ParameterSet& ps) : evtNumber(0) {
00049 
00050    LogVerbatim("DTNoiseTask") << "[DTNoiseTask]: Constructor"<<endl;
00051 
00052   dbe = edm::Service<DQMStore>().operator->();
00053 
00054   //switch for timeBox booking
00055   doTimeBoxHistos = ps.getUntrackedParameter<bool>("doTbHistos", false);
00056   
00057   // The label to retrieve the digis 
00058   dtDigiLabel = ps.getParameter<InputTag>("dtDigiLabel");
00059 
00060   // the name of the 4D rec hits collection
00061   theRecHits4DLabel = ps.getParameter<string>("recHits4DLabel");
00062   
00063   // switch for segment veto
00064   doSegmentVeto = ps.getUntrackedParameter<bool>("doSegmentVeto", false);
00065 
00066   // safe margin (ns) between ttrig and beginning of counting area
00067   safeMargin = ps.getUntrackedParameter<double>("safeMargin", 200.);
00068 
00069 }
00070 
00071 
00072 
00073 
00074 DTNoiseTask::~DTNoiseTask(){}
00075 
00076 
00077 
00079 void DTNoiseTask::beginJob() {
00080 
00081    LogVerbatim("DTNoiseTask") << "[DTNoiseTask]: BeginJob"<<endl;
00082 
00083 }
00084 
00085 
00086 
00088 void DTNoiseTask::beginLuminosityBlock(const edm::LuminosityBlock&  lumiSeg,
00089                                        const edm::EventSetup& context) {
00090 
00091    LogVerbatim("DTNoiseTask") <<"[DTNoiseTask]: Begin of LS transition"<<endl;
00092 
00093 }
00094 
00095 
00096   
00098 void DTNoiseTask::analyze(const edm::Event& e, const edm::EventSetup& c) {
00099 
00100   evtNumber++;
00101   nEventMonitor->Fill(evtNumber);
00102 
00103   if(evtNumber%1000==0)
00104      LogVerbatim("DTNoiseTask") <<"[DTNoiseTask]: Analyzing evt number :"<<evtNumber<<endl;
00105 
00106   // map of the chambers with at least 1 segment
00107   std::map<DTChamberId, int> segmentsChId;
00108 
00109   // Get the 4D segment collection from the event
00110   edm::Handle<DTRecSegment4DCollection> all4DSegments;
00111   if(doSegmentVeto) {
00112     e.getByLabel(theRecHits4DLabel, all4DSegments);
00113   
00114     // Loop over all chambers containing a segment and look for the number of segments
00115     DTRecSegment4DCollection::id_iterator chamberId;
00116     for (chamberId = all4DSegments->id_begin();
00117          chamberId != all4DSegments->id_end();
00118          ++chamberId){
00119       segmentsChId[*chamberId]=1;
00120     }
00121   }
00122 
00123   // Get the digis from the event
00124   edm::Handle<DTDigiCollection> dtdigis;
00125   e.getByLabel(dtDigiLabel, dtdigis);
00126   
00127   // LOOP OVER ALL THE DIGIS OF THE EVENT
00128   DTDigiCollection::DigiRangeIterator dtLayerId_It;
00129   for (dtLayerId_It=dtdigis->begin(); dtLayerId_It!=dtdigis->end(); ++dtLayerId_It){
00130     for (DTDigiCollection::const_iterator digiIt = ((*dtLayerId_It).second).first;
00131          digiIt!=((*dtLayerId_It).second).second; ++digiIt){
00132   
00133       //Check the TDC trigger width
00134       int tdcTime = (*digiIt).countsTDC();
00135       double upperLimit = tTrigStMap[(*dtLayerId_It).first.superlayerId().chamberId()]-safeMargin;
00136       if(doTimeBoxHistos)
00137         tbHistos[(*dtLayerId_It).first.superlayerId()]->Fill(tdcTime);
00138       if(tdcTime>upperLimit)
00139         continue;
00140 
00141       //Check the chamber has no 4D segments (optional)
00142       if(doSegmentVeto &&
00143          segmentsChId.find((*dtLayerId_It).first.superlayerId().chamberId())!=segmentsChId.end())
00144         continue;
00145 
00146       // fill the occupancy histo
00147       // FIXME: needs to be optimized: no need to rescale the histo for each digi
00148       TH2F* noise_root = noiseHistos[(*dtLayerId_It).first.superlayerId().chamberId()]->getTH2F();
00149       double normalization=0;
00150       if(mapEvt.find((*dtLayerId_It).first.superlayerId().chamberId())!=mapEvt.end()) {
00151         LogVerbatim("DTNoiseTask")  << " Last fill: # of events: "
00152                                     << mapEvt[(*dtLayerId_It).first.superlayerId().chamberId()]
00153                                     << endl;
00154         normalization =  1e-9*upperLimit*mapEvt[(*dtLayerId_It).first.superlayerId().chamberId()];
00155         // revert back to # of entries
00156         noise_root->Scale(normalization);
00157       }
00158       int yBin=(*dtLayerId_It).first.layer()+(4*((*dtLayerId_It).first.superlayerId().superlayer()-1));
00159       noise_root->Fill((*digiIt).wire(),yBin);
00160       // normalize the occupancy histo
00161       mapEvt[(*dtLayerId_It).first.superlayerId().chamberId()] = evtNumber;
00162       LogVerbatim("DTNoiseTask")  << (*dtLayerId_It).first << " wire: " << (*digiIt).wire()
00163                                   << " # counts: " << noise_root->GetBinContent((*digiIt).wire(),yBin)
00164                                   << " Time interval: " << upperLimit
00165                                   << " # of events: " << evtNumber << endl;;
00166       normalization = double( 1e-9*upperLimit*mapEvt[(*dtLayerId_It).first.superlayerId().chamberId()]);
00167       // update the rate
00168       noise_root->Scale(1./normalization);
00169       LogVerbatim("DTNoiseTask")  << "    noise rate: "
00170                                   << noise_root->GetBinContent((*digiIt).wire(),yBin) << endl;
00171     }
00172   }
00173 }
00174   
00175 
00176 
00178 void DTNoiseTask::endJob() {}
00179 
00180 
00181 void DTNoiseTask::bookHistos(DTChamberId chId) {
00182 
00183   // set the folder
00184   stringstream wheel; wheel << chId.wheel();    
00185   stringstream station; station << chId.station();      
00186   stringstream sector; sector << chId.sector(); 
00187   dbe->setCurrentFolder("DT/05-Noise/Wheel" + wheel.str() +
00188 //                      "/Station" + station.str() +
00189                         "/Sector" + sector.str());
00190 
00191   // Build the histo name
00192   string histoName = string("NoiseRate")
00193     + "_W" + wheel.str() 
00194     + "_St" + station.str() 
00195     + "_Sec" + sector.str() ;
00196   
00197    LogVerbatim("DTNoiseTask") << "[DTNoiseTask]: booking chamber histo:"<<endl;
00198    LogVerbatim("DTNoiseTask") << "              folder "<< "DT/05-Noise/Wheel" + wheel.str() +
00199 //     "/Station" + station.str() +
00200     "/Sector" + sector.str() + "/"<<endl; 
00201    LogVerbatim("DTNoiseTask") << "              histoName "<<histoName<<endl;
00202 
00203   // Get the chamber from the geometry
00204   int nWires_max = 0;
00205   const DTChamber* dtchamber = dtGeom->chamber(chId);
00206   const vector<const DTSuperLayer*> superlayers = dtchamber->superLayers();
00207 
00208   // Loop over layers and find the max # of wires
00209   for(vector<const DTSuperLayer*>::const_iterator sl = superlayers.begin();
00210       sl != superlayers.end(); ++sl) { // loop over SLs
00211     vector<const DTLayer*> layers = (*sl)->layers();
00212     for(vector<const DTLayer*>::const_iterator lay = layers.begin();
00213         lay != layers.end(); ++lay) { // loop over layers
00214       int nWires = (*lay)->specificTopology().channels();
00215       if(nWires > nWires_max) nWires_max = nWires;
00216     }
00217   }
00218 
00219   noiseHistos[chId] = dbe->book2D(histoName,"Noise rate (Hz) per channel", nWires_max,1, nWires_max+1,12,1,13);
00220   noiseHistos[chId]->setAxisTitle("wire number",1);
00221   noiseHistos[chId]->setBinLabel(1,"SL1-L1",2);
00222   noiseHistos[chId]->setBinLabel(2,"SL1-L2",2);
00223   noiseHistos[chId]->setBinLabel(3,"SL1-L3",2);
00224   noiseHistos[chId]->setBinLabel(4,"SL1-L4",2);
00225   noiseHistos[chId]->setBinLabel(5,"SL2-L1",2);
00226   noiseHistos[chId]->setBinLabel(6,"SL2-L2",2);
00227   noiseHistos[chId]->setBinLabel(7,"SL2-L3",2);
00228   noiseHistos[chId]->setBinLabel(8,"SL2-L4",2);
00229   noiseHistos[chId]->setBinLabel(9,"SL3-L1",2);
00230   noiseHistos[chId]->setBinLabel(10,"SL3-L2",2);
00231   noiseHistos[chId]->setBinLabel(11,"SL3-L3",2);
00232   noiseHistos[chId]->setBinLabel(12,"SL3-L4",2);
00233 
00234 }
00235 
00236 
00237 void DTNoiseTask::bookHistos(DTSuperLayerId slId) {
00238 
00239   // set the folder
00240   stringstream wheel; wheel << slId.chamberId().wheel();        
00241   stringstream station; station << slId.chamberId().station();  
00242   stringstream sector; sector << slId.chamberId().sector();     
00243   stringstream superlayer; superlayer << slId.superlayer();
00244   dbe->setCurrentFolder("DT/05-Noise/Wheel" + wheel.str() +
00245                         "/Station" + station.str() +
00246                         "/Sector" + sector.str());
00247 
00248   // Build the histo name
00249   string histoName = string("TimeBox")
00250     + "_W" + wheel.str() 
00251     + "_St" + station.str() 
00252     + "_Sec" + sector.str()
00253     + "_SL" + superlayer.str();
00254   
00255    LogVerbatim("DTNoiseTask") <<"[DTNoiseTask]: booking SL histo:"<<endl;
00256    LogVerbatim("DTNoiseTask") <<"              folder "<< "DT/05-Noise/Wheel" + wheel.str() +
00257     "/Station" + station.str() +
00258     "/Sector" + sector.str() + "/" << endl; 
00259    LogVerbatim("DTNoiseTask") <<"              histoName "<<histoName<<endl;
00260 
00261   tbHistos[slId] = dbe->book1D(histoName,"Time Box (TDC counts)", 1000, 0, 6000);
00262 
00263 }
00264 
00265 
00266 void DTNoiseTask::beginRun(const Run& run, const EventSetup& setup) {
00267 
00268   LogVerbatim("DTNoiseTask") <<"[DTNoiseTask]: Begin of run"<<endl;
00269 
00270   // tTrig Map
00271   edm::ESHandle<DTTtrig> tTrigMap;
00272   setup.get<DTTtrigRcd>().get(tTrigMap);
00273 
00274   // get the geometry
00275   setup.get<MuonGeometryRecord>().get(dtGeom);
00276 
00277   dbe->setCurrentFolder("DT/EventInfo/Counters");
00278   nEventMonitor = dbe->bookFloat("nProcessedEventsNoise");
00279 
00280   // Loop over all the chambers          
00281   vector<DTChamber*>::const_iterator ch_it = dtGeom->chambers().begin();         
00282   vector<DTChamber*>::const_iterator ch_end = dtGeom->chambers().end();          
00283   for (; ch_it != ch_end; ++ch_it) {     
00284     DTChamberId chId = (*ch_it)->id();
00285     // histo booking
00286     bookHistos(chId);
00287     vector<const DTSuperLayer*>::const_iterator sl_it = (*ch_it)->superLayers().begin();         
00288     vector<const DTSuperLayer*>::const_iterator sl_end = (*ch_it)->superLayers().end();          
00289     // Loop over the SLs         
00290     for(; sl_it != sl_end; ++sl_it) { 
00291       DTSuperLayerId slId = (*sl_it)->id();
00292       if(doTimeBoxHistos)
00293         bookHistos(slId);
00294       float tTrig, tTrigRMS, kFactor;
00295       tTrigMap->get(slId, tTrig, tTrigRMS,kFactor,DTTimeUnits::ns);
00296       // tTrig mapping per station
00297       // check that the ttrig is the lowest of the 3 SLs
00298       if(tTrigStMap.find(chId)==tTrigStMap.end() || 
00299          (tTrigStMap.find(chId)!=tTrigStMap.end() && tTrig < tTrigStMap[chId]))
00300         tTrigStMap[chId] = tTrig;
00301     }
00302   }
00303 
00304 
00305 }
00306 
00307 void DTNoiseTask::endLuminosityBlock(const LuminosityBlock& lumiSeg, const EventSetup& setup) {
00308   LogVerbatim("DTNoiseTask") << "[DTNoiseTask]: End LS, update rates in all histos" << endl;
00309   
00310   // update the rate of all histos (usefull for histos with few entries: they are not updated very often
00311   for(map<DTChamberId, MonitorElement*>::const_iterator meAndChamber = noiseHistos.begin();
00312       meAndChamber != noiseHistos.end(); ++meAndChamber) {
00313     DTChamberId chId = (*meAndChamber).first;
00314     TH2F* noise_root = (*meAndChamber).second->getTH2F();
00315     double upperLimit = tTrigStMap[chId]-safeMargin;
00316 
00317     double normalization=0;
00318     if(mapEvt.find(chId) != mapEvt.end()) {
00319       LogVerbatim("DTNoiseTask")  << " Ch: " << chId << " Last fill: # of events: " << mapEvt[chId] << endl;
00320         normalization =  1e-9*upperLimit*mapEvt[chId];
00321         // revert back to # of entries
00322         noise_root->Scale(normalization);
00323     }
00324     //check that event analyzed != 0 might happen oline
00325     if (evtNumber) {
00326       // set the # of events analyzed until this update
00327       LogVerbatim("DTNoiseTask")  << "          Update for events: " << evtNumber << endl;
00328       mapEvt[chId] = evtNumber;
00329       // update the rate
00330       normalization = double( 1e-9*upperLimit*evtNumber);
00331       noise_root->Scale(1./normalization);
00332     }
00333   }
00334 }