CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/src/DQM/DTMonitorModule/src/DTDigiTask.cc

Go to the documentation of this file.
00001  /*
00002  * \file DTDigiTask.cc
00003  * 
00004  * $Date: 2010/06/21 14:59:53 $
00005  * $Revision: 1.63 $
00006  * \author M. Zanetti - INFN Padova
00007  *
00008  */
00009 
00010 #include <DQM/DTMonitorModule/interface/DTDigiTask.h>
00011 
00012 // Framework
00013 #include <FWCore/Framework/interface/EventSetup.h>
00014 
00015 // Digis
00016 #include <DataFormats/DTDigi/interface/DTDigi.h>
00017 #include <DataFormats/DTDigi/interface/DTDigiCollection.h>
00018 #include <DataFormats/MuonDetId/interface/DTLayerId.h>
00019 #include <DataFormats/MuonDetId/interface/DTChamberId.h>
00020 
00021 // Geometry
00022 #include "Geometry/Records/interface/MuonGeometryRecord.h"
00023 #include "Geometry/DTGeometry/interface/DTGeometry.h"
00024 #include "Geometry/DTGeometry/interface/DTLayer.h"
00025 #include "Geometry/DTGeometry/interface/DTTopology.h"
00026 
00027 // T0s
00028 #include <CondFormats/DTObjects/interface/DTT0.h>
00029 #include <CondFormats/DataRecord/interface/DTT0Rcd.h>
00030 #include <CondFormats/DTObjects/interface/DTTtrig.h>
00031 #include <CondFormats/DataRecord/interface/DTTtrigRcd.h>
00032 
00033 #include "CondFormats/DataRecord/interface/DTStatusFlagRcd.h"
00034 #include "CondFormats/DTObjects/interface/DTStatusFlag.h"
00035 
00036 #include "DQMServices/Core/interface/DQMStore.h"
00037 #include "DQMServices/Core/interface/MonitorElement.h"
00038 #include "FWCore/ServiceRegistry/interface/Service.h"
00039 
00040 #include <sstream>
00041 #include <math.h>
00042 
00043 using namespace edm;
00044 using namespace std;
00045 
00046 
00047 
00048 // Contructor
00049 DTDigiTask::DTDigiTask(const edm::ParameterSet& ps){
00050   // switch for the verbosity
00051   LogTrace("DTDQM|DTMonitorModule|DTDigiTask") << "[DTDigiTask]: Constructor" << endl;
00052 
00053   // The label to retrieve the digis 
00054   dtDigiLabel = ps.getParameter<InputTag>("dtDigiLabel");
00055   // Read the configuration parameters
00056   maxTDCHits = ps.getUntrackedParameter<int>("maxTDCHitsPerChamber",30000);
00057   // Set to true to read the ttrig from DB (useful to determine in-time and out-of-time hits)
00058   readTTrigDB = ps.getUntrackedParameter<bool>("readDB", false);
00059   // Set to true to subtract t0 from test pulses
00060   subtractT0 = ps.getParameter<bool>("performPerWireT0Calibration");
00061   // Tmax value (TDC counts)
00062   defaultTmax = ps.getParameter<int>("defaultTmax");
00063   // Switch from static to dinamic histo booking
00064   doStaticBooking =  ps.getUntrackedParameter<bool>("staticBooking", true);
00065   // Switch for local/global runs
00066   isLocalRun = ps.getUntrackedParameter<bool>("localrun", true);
00067   // Setting for the reset of the ME after n (= ResetCycle) luminosity sections
00068   resetCycle = ps.getUntrackedParameter<int>("ResetCycle", 3);
00069   // Check the DB of noisy channels
00070   checkNoisyChannels = ps.getUntrackedParameter<bool>("checkNoisyChannels","false");
00071   // Default TTrig to be used when not reading the TTrig DB
00072   defaultTTrig = ps.getParameter<int>("defaultTtrig");
00073   inTimeHitsLowerBound = ps.getParameter<int>("inTimeHitsLowerBound");
00074   inTimeHitsUpperBound = ps.getParameter<int>("inTimeHitsUpperBound");
00075   timeBoxGranularity = ps.getUntrackedParameter<int>("timeBoxGranularity",4);
00076   maxTDCCounts = ps.getUntrackedParameter<int>("maxTDCCounts", 6400);
00077 
00078   doAllHitsOccupancies = ps.getUntrackedParameter<bool>("doAllHitsOccupancies", true);
00079   doNoiseOccupancies = ps.getUntrackedParameter<bool>("doNoiseOccupancies", false);
00080   doInTimeOccupancies = ps.getUntrackedParameter<bool>("doInTimeOccupancies", false);
00081 
00082   // switch on the mode for running on test pulses (different top folder)
00083   tpMode = ps.getUntrackedParameter<bool>("testPulseMode", false);
00084   // switch on/off the filtering of synchronous noise events (cutting on the # of digis)
00085   // time-boxes and occupancy plots are not filled and summary plots are created to report the problem
00086   filterSyncNoise = ps.getUntrackedParameter<bool>("filterSyncNoise", false);
00087   // look for synch noisy events, produce histograms but do not filter them
00088   lookForSyncNoise = ps.getUntrackedParameter<bool>("lookForSyncNoise", false);
00089   // switch on production of time-boxes with layer granularity
00090   doLayerTimeBoxes = ps.getUntrackedParameter<bool>("doLayerTimeBoxes", false);
00091 
00092   dbe = edm::Service<DQMStore>().operator->();
00093 
00094   syncNumTot = 0;
00095   syncNum = 0;
00096 
00097 }
00098 
00099 
00100 
00101 // destructor
00102 DTDigiTask::~DTDigiTask(){
00103   LogTrace("DTDQM|DTMonitorModule|DTDigiTask") << "DTDigiTask: analyzed " << nevents << " events" << endl;
00104 
00105 }
00106 
00107 
00108 
00109 
00110 void DTDigiTask::endJob(){
00111   LogTrace("DTDQM|DTMonitorModule|DTDigiTask") <<"[DTDigiTask] endjob called!"<<endl;
00112   
00113 }
00114 
00115 
00116 
00117 
00118 void DTDigiTask::beginJob(){
00119   LogTrace("DTDQM|DTMonitorModule|DTDigiTask") <<"[DTDigiTask]: BeginJob"<<endl;
00120 
00121   nevents = 0;
00122 }
00123 
00124 
00125 void DTDigiTask::beginRun(const edm::Run& run, const edm::EventSetup& context) {
00126   LogTrace("DTDQM|DTMonitorModule|DTDigiTask") << "[DTDigiTask]: begin run" << endl;
00127 
00128   // Get the geometry
00129   context.get<MuonGeometryRecord>().get(muonGeom);
00130 
00131   // tTrig 
00132   if (readTTrigDB) 
00133     context.get<DTTtrigRcd>().get(tTrigMap);
00134   // t0s 
00135   if (subtractT0) 
00136     context.get<DTT0Rcd>().get(t0Map);
00137   // FIXME: tMax (not yet from the DB)
00138   tMax = defaultTmax;
00139   
00140   // ----------------------------------------------------------------------
00141   if(doStaticBooking) {  // Static histo booking
00142     // book the event counter
00143     dbe->setCurrentFolder(topFolder());
00144     nEventMonitor = dbe->bookFloat("nProcessedEvents");
00145     for(int wh = -2; wh <= 2; ++wh) { // loop over wheels
00146       if(doAllHitsOccupancies) bookHistos(wh,string("Occupancies"),"OccupancyAllHits");
00147       if(doNoiseOccupancies) bookHistos(wh,string("Occupancies"),"OccupancyNoiseHits");
00148       if(doInTimeOccupancies) bookHistos(wh,string("Occupancies"),"OccupancyInTimeHits");
00149 
00150       if(lookForSyncNoise || filterSyncNoise) {
00151         bookHistos(wh,string("SynchNoise"),"SyncNoiseEvents");
00152         bookHistos(wh,string("SynchNoise"),"SyncNoiseChambs");
00153       }
00154 
00155       for(int st = 1; st <= 4; ++st) { // loop over stations
00156         for(int sect = 1; sect <= 14; ++sect) { // loop over sectors
00157           if((sect == 13 || sect == 14) && st != 4) continue;
00158           // Get the chamber ID
00159           const  DTChamberId dtChId(wh,st,sect);
00160 
00161           // Occupancies 
00162           if (doAllHitsOccupancies) 
00163             bookHistos(dtChId,string("Occupancies"),"OccupancyAllHits_perCh");
00164           if(doNoiseOccupancies) 
00165             bookHistos(dtChId,string("Occupancies"),"OccupancyNoise_perCh");
00166           if(doInTimeOccupancies)
00167             bookHistos(dtChId,string("Occupancies"),"OccupancyInTimeHits_perCh");
00168 
00169 
00170 
00171 
00172           for(int sl = 1; sl <= 3; ++sl) { // Loop over SLs
00173             if(st == 4 && sl == 2) continue;
00174             const  DTSuperLayerId dtSLId(wh,st,sect,sl);
00175             if(isLocalRun) {
00176               bookHistos(dtSLId,string("TimeBoxes"),"TimeBox");
00177             } else {
00178               // TimeBoxes for different triggers
00179               bookHistos(dtSLId,string("TimeBoxes"),"TimeBoxDTonly");
00180               bookHistos(dtSLId,string("TimeBoxes"),"TimeBoxNoDT");
00181               bookHistos(dtSLId,string("TimeBoxes"),"TimeBoxDTalso");
00182             }
00183           }
00184         }
00185       }
00186     }
00187   }
00188 }
00189 
00190 
00191 
00192 
00193 void DTDigiTask::beginLuminosityBlock(LuminosityBlock const& lumiSeg, EventSetup const& context) {
00194 
00195   LogTrace("DTDQM|DTMonitorModule|DTDigiTask") << "[DTDigiTask]: Begin of LS transition" << endl;
00196   
00197   // Reset the MonitorElements every n (= ResetCycle) Lumi Blocks
00198   int lumiBlock = lumiSeg.id().luminosityBlock(); 
00199   if(lumiBlock % resetCycle == 0) {
00200     LogVerbatim("DTDQM|DTMonitorModule|DTDigiTask")
00201       <<"[DTDigiTask]: Reset at the LS transition : " 
00202       << lumiBlock << endl;
00203     // Loop over all ME
00204     map<string,map<uint32_t,MonitorElement*> >::const_iterator histosIt  = digiHistos.begin();
00205     map<string,map<uint32_t,MonitorElement*> >::const_iterator histosEnd = digiHistos.end();
00206     for(;histosIt != histosEnd ; ++histosIt) {
00207       map<uint32_t,MonitorElement*>::const_iterator histoIt  = (*histosIt).second.begin();
00208       map<uint32_t,MonitorElement*>::const_iterator histoEnd = (*histosIt).second.end();
00209       for(;histoIt != histoEnd; ++histoIt) { (*histoIt).second->Reset(); }
00210     }
00211 
00212     // loop over wheel summaries
00213     map<string,map<int,MonitorElement*> >::const_iterator whHistosIt  = wheelHistos.begin();
00214     map<string,map<int,MonitorElement*> >::const_iterator whHistosEnd = wheelHistos.end();
00215     for(; whHistosIt != whHistosEnd ; ++whHistosIt) {
00216       if ((*whHistosIt).first.find("Sync") == string::npos) { // FIXME skips synch noise plots
00217         map<int,MonitorElement*>::const_iterator histoIt  = (*whHistosIt).second.begin();
00218         map<int,MonitorElement*>::const_iterator histoEnd = (*whHistosIt).second.end();
00219         for(;histoIt != histoEnd; ++histoIt) { (*histoIt).second->Reset(); }
00220       }
00221     }
00222   }
00223   
00224 }
00225 
00226 
00227 
00228 
00229 void DTDigiTask::bookHistos(const DTSuperLayerId& dtSL, string folder, string histoTag) {
00230   // set the folder
00231   stringstream wheel; wheel << dtSL.wheel();    
00232   stringstream station; station << dtSL.station();      
00233   stringstream sector; sector << dtSL.sector(); 
00234   stringstream superLayer; superLayer << dtSL.superlayer();
00235   dbe->setCurrentFolder(topFolder() + "Wheel" + wheel.str() +
00236                         "/Sector" + sector.str() +
00237                         "/Station" + station.str());
00238 
00239   // Build the histo name
00240   string histoName = histoTag 
00241     + "_W" + wheel.str() 
00242     + "_St" + station.str() 
00243     + "_Sec" + sector.str() 
00244     + "_SL" + superLayer.str(); 
00245 
00246   LogTrace("DTDQM|DTMonitorModule|DTDigiTask")
00247     << "[DTDigiTask]: booking SL histo:" << histoName
00248     << " (tag: " << histoTag
00249     << ") folder: " << topFolder() + "Wheel" + wheel.str() +
00250     "/Station" + station.str() +
00251     "/Sector" + sector.str() + "/" + folder << endl;
00252     
00253 
00254   // ttrig and rms are TDC counts
00255   if ( readTTrigDB ) 
00256     tTrigMap->get(dtSL, tTrig, tTrigRMS, kFactor, DTTimeUnits::counts); 
00257   else tTrig = defaultTTrig;
00258   
00259 
00260   if ( folder == "TimeBoxes") {
00261     string histoTitle = histoName + " (TDC Counts)";
00262 
00263     if (!readTTrigDB) {
00264       (digiHistos[histoTag])[dtSL.rawId()] = 
00265         dbe->book1D(histoName,histoTitle, maxTDCCounts/timeBoxGranularity, 0, maxTDCCounts);
00266       if(doLayerTimeBoxes) {      // Book TimeBoxes per layer
00267         for(int layer = 1; layer != 5; ++layer) {
00268           DTLayerId layerId(dtSL, layer);
00269           stringstream layerHistoName; layerHistoName << histoName << "_L" << layer;
00270           (digiHistos[histoTag])[layerId.rawId()] =
00271             dbe->book1D(layerHistoName.str(),layerHistoName.str(), maxTDCCounts/timeBoxGranularity, 0, maxTDCCounts);
00272         }
00273       }
00274     }    
00275     else {
00276       (digiHistos[histoTag])[dtSL.rawId()] = 
00277         dbe->book1D(histoName,histoTitle, 3*tMax/timeBoxGranularity, tTrig-tMax, tTrig+2*tMax);
00278       if(doLayerTimeBoxes) {
00279         // Book TimeBoxes per layer
00280         for(int layer = 1; layer != 5; ++layer) {
00281           DTLayerId layerId(dtSL, layer);
00282           stringstream layerHistoName; layerHistoName << histoName << "_L" << layer;
00283           (digiHistos[histoTag])[layerId.rawId()] =
00284             dbe->book1D(layerHistoName.str(),layerHistoName.str(), 3*tMax/timeBoxGranularity, tTrig-tMax, tTrig+2*tMax);
00285         }
00286       }
00287     }
00288   }
00289 
00290   if ( folder == "CathodPhotoPeaks" ) {
00291     dbe->setCurrentFolder(topFolder() + "Wheel" + wheel.str() +
00292                           "/Sector" + sector.str() + 
00293                           "/Station" + station.str() + "/" + folder);
00294     (digiHistos[histoTag])[dtSL.rawId()] = dbe->book1D(histoName,histoName,500,0,1000);
00295   }
00296   
00297 }
00298 
00299 
00300 
00301 
00302 void DTDigiTask::bookHistos(const DTChamberId& dtCh, string folder, string histoTag) {
00303   // set the current folder
00304   stringstream wheel; wheel << dtCh.wheel();    
00305   stringstream station; station << dtCh.station();      
00306   stringstream sector; sector << dtCh.sector();
00307   dbe->setCurrentFolder(topFolder() + "Wheel" + wheel.str() +
00308                         "/Sector" + sector.str() + 
00309                         "/Station" + station.str());
00310   
00311   // build the histo name
00312   string histoName = histoTag 
00313     + "_W" + wheel.str() 
00314     + "_St" + station.str() 
00315     + "_Sec" + sector.str(); 
00316 
00317 
00318   LogTrace("DTDQM|DTMonitorModule|DTDigiTask")
00319     << "[DTDigiTask]: booking chamber histo:" 
00320     << " (tag: " << histoTag
00321     << ") folder: " << topFolder() + "Wheel" + wheel.str() +
00322     "/Station" + station.str() +
00323     "/Sector" + sector.str() << endl;
00324     
00325   
00326   if (folder == "Occupancies")    {
00327     
00328     const DTChamber* dtchamber = muonGeom->chamber(dtCh);
00329     const std::vector<const DTSuperLayer*> dtSupLylist = dtchamber->superLayers();
00330     std::vector<const DTSuperLayer*>::const_iterator suly = dtSupLylist.begin();
00331     std::vector<const DTSuperLayer*>::const_iterator sulyend = dtSupLylist.end();
00332     
00333     int nWires = 0;
00334     int firstWire = 0;
00335     int nWires_max = 0;
00336     
00337     while(suly != sulyend) {
00338       const std::vector<const DTLayer*> dtLyList = (*suly)->layers();
00339       std::vector<const DTLayer*>::const_iterator ly = dtLyList.begin();
00340       std::vector<const DTLayer*>::const_iterator lyend = dtLyList.end();
00341       stringstream superLayer; superLayer << (*suly)->id().superlayer();
00342       
00343       while(ly != lyend) {
00344         nWires = muonGeom->layer((*ly)->id())->specificTopology().channels();
00345         firstWire = muonGeom->layer((*ly)->id())->specificTopology().firstChannel();
00346         stringstream layer; layer << (*ly)->id().layer();
00347         string histoName_layer = histoName + "_SL" + superLayer.str()  + "_L" + layer.str();
00348         if(histoTag == "OccupancyAllHits_perL" 
00349            || histoTag == "OccupancyNoise_perL"
00350            || histoTag == "OccupancyInTimeHits_perL")
00351           (digiHistos[histoTag])[(*ly)->id().rawId()] = dbe->book1D(histoName_layer,histoName_layer,nWires,firstWire,nWires+firstWire);
00352         ++ly;
00353         if((nWires+firstWire) > nWires_max) nWires_max = (nWires+firstWire);
00354         
00355       }
00356       ++suly;
00357     }
00358    
00359     if(histoTag != "OccupancyAllHits_perL" 
00360            && histoTag != "OccupancyNoise_perL"
00361            && histoTag != "OccupancyInTimeHits_perL"){
00362       // Set the title to show the time interval used (only if unique == not from DB)
00363       string histoTitle = histoName;
00364       if(!readTTrigDB && histoTag == "OccupancyInTimeHits_perCh") {
00365         stringstream title;
00366         int inTimeHitsLowerBoundCorr = int(round(defaultTTrig)) - inTimeHitsLowerBound;
00367         int inTimeHitsUpperBoundCorr = int(round(defaultTTrig)) + defaultTmax + inTimeHitsUpperBound;
00368         title << "Occ. digis in time [" << inTimeHitsLowerBoundCorr << ", "
00369               << inTimeHitsUpperBoundCorr << "] (TDC counts)";
00370         histoTitle = title.str();
00371       }
00372       (digiHistos[histoTag])[dtCh.rawId()] = dbe->book2D(histoName,histoTitle,nWires_max,1,nWires_max+1,12,0,12);
00373     
00374       for(int i=1;i<=12;i++) { 
00375         if(i<5){
00376           stringstream layer;
00377           string layer_name;
00378           layer<<i;
00379           layer>>layer_name;
00380           string label="SL1: L"+layer_name;
00381           (digiHistos[histoTag])[dtCh.rawId()]->setBinLabel(i,label,2);
00382         }
00383         else if(i>4 && i<9){
00384           stringstream layer;
00385           string layer_name;
00386           layer<<(i-4);
00387           layer>>layer_name;
00388           string label="SL2: L"+layer_name;
00389           (digiHistos[histoTag])[dtCh.rawId()]->setBinLabel(i,label,2);
00390         }
00391         else if(i>8 && i<13){
00392           stringstream layer;
00393           string layer_name;
00394           layer<<(i-8);
00395           layer>>layer_name;
00396           string label="SL3: L"+layer_name;
00397           (digiHistos[histoTag])[dtCh.rawId()]->setBinLabel(i,label,2);
00398         }
00399       }
00400     }
00401   }
00402 }
00403 
00404 
00405 
00406 
00407 void DTDigiTask::bookHistos(const int wheelId, string folder, string histoTag) {
00408   // Set the current folder
00409   stringstream wheel; wheel << wheelId; 
00410 
00411 
00412   // build the histo name
00413   string histoName = histoTag + "_W" + wheel.str(); 
00414 
00415 
00416   LogTrace("DTDQM|DTMonitorModule|DTDigiTask")
00417     << "[DTDigiTask]: booking wheel histo:" << histoName
00418     << " (tag: " << histoTag
00419     << ") folder: " << topFolder() + "Wheel" + wheel.str() + "/" <<endl;
00420 
00421   if(folder == "Occupancies") {
00422     dbe->setCurrentFolder(topFolder() + "Wheel" + wheel.str());
00423     string histoTitle = "# of digis per chamber WHEEL: "+wheel.str();
00424     (wheelHistos[histoTag])[wheelId] = dbe->book2D(histoName,histoTitle,12,1,13,4,1,5);
00425     (wheelHistos[histoTag])[wheelId]->setBinLabel(1,"MB1",2);
00426     (wheelHistos[histoTag])[wheelId]->setBinLabel(2,"MB2",2);
00427     (wheelHistos[histoTag])[wheelId]->setBinLabel(3,"MB3",2);
00428     (wheelHistos[histoTag])[wheelId]->setBinLabel(4,"MB4",2);
00429     (wheelHistos[histoTag])[wheelId]->setAxisTitle("sector",1);
00430   } else if(folder == "SynchNoise") {
00431     dbe->setCurrentFolder("DT/05-Noise/SynchNoise");
00432     if (histoTag== "SyncNoiseEvents") {
00433       string histoTitle = "# of Syncronous-noise events WHEEL: "+wheel.str();
00434       (wheelHistos[histoTag])[wheelId] = dbe->book2D(histoName,histoTitle,12,1,13,4,1,5);
00435       (wheelHistos[histoTag])[wheelId]->setBinLabel(1,"MB1",2);
00436       (wheelHistos[histoTag])[wheelId]->setBinLabel(2,"MB2",2);
00437       (wheelHistos[histoTag])[wheelId]->setBinLabel(3,"MB3",2);
00438       (wheelHistos[histoTag])[wheelId]->setBinLabel(4,"MB4",2);
00439       (wheelHistos[histoTag])[wheelId]->setAxisTitle("sector",1);
00440     } else if (histoTag== "SyncNoiseChambs") {
00441       string histoTitle = "# of Synchornous-noise chamb per evt. WHEEL: "+wheel.str();
00442       (wheelHistos[histoTag])[wheelId] = dbe->book1D(histoName,histoTitle,50,0.5,50.5);
00443       (wheelHistos[histoTag])[wheelId]->setAxisTitle("# of noisy chambs.",1);
00444       (wheelHistos[histoTag])[wheelId]->setAxisTitle("# of evts.",2);
00445     }
00446   }
00447 
00448 }
00449 
00450 
00451 
00452 // does the real job
00453 void DTDigiTask::analyze(const edm::Event& event, const edm::EventSetup& c) {
00454   nevents++;
00455   nEventMonitor->Fill(nevents);
00456   if (nevents%1000 == 0) {
00457     LogTrace("DTDQM|DTMonitorModule|DTDigiTask") << "[DTDigiTask] Analyze #Run: " << event.id().run()
00458                                                  << " #Event: " << event.id().event() << endl;
00459   }
00460   
00461   // Get the ingredients from the event
00462   
00463   // Digi collection
00464   edm::Handle<DTDigiCollection> dtdigis;
00465   event.getByLabel(dtDigiLabel, dtdigis);
00466 
00467   // LTC digis
00468   if (!isLocalRun) event.getByType(ltcdigis);
00469 
00470   // Status map (for noisy channels)
00471   ESHandle<DTStatusFlag> statusMap;
00472   if(checkNoisyChannels) {
00473     // Get the map of noisy channels
00474     c.get<DTStatusFlagRcd>().get(statusMap);
00475   }
00476 
00477   string histoTag;
00478 
00479 
00480   // Check if the digi container is empty
00481   if(dtdigis->begin() == dtdigis->end()) {
00482     LogTrace("DTDQM|DTMonitorModule|DTDigiTask") << "Event " << nevents << " empty." << endl;
00483   }
00484 
00485   if (lookForSyncNoise || filterSyncNoise) { // dosync
00486     // Count the # of digis per chamber
00487     DTDigiCollection::DigiRangeIterator dtLayerId_It;
00488     for (dtLayerId_It=dtdigis->begin(); dtLayerId_It!=dtdigis->end(); dtLayerId_It++) {
00489       DTChamberId chId = ((*dtLayerId_It).first).chamberId();
00490       if(hitMap.find(chId) == hitMap.end()) {// new chamber
00491         hitMap[chId] = 0;
00492       }
00493       hitMap[chId] += (((*dtLayerId_It).second).second - ((*dtLayerId_It).second).first);
00494     }
00495 
00496 
00497     
00498     // check chamber with # of digis above threshold and flag them as noisy
00499     map<DTChamberId,int>::const_iterator hitMapIt  = hitMap.begin();
00500     map<DTChamberId,int>::const_iterator hitMapEnd = hitMap.end();
00501 
00502     map<int,int> chMap;
00503 
00504     for (; hitMapIt != hitMapEnd; ++hitMapIt) {
00505       if((hitMapIt->second) > maxTDCHits) { 
00506 
00507         DTChamberId chId = hitMapIt->first;
00508         int wh = chId.wheel();
00509         
00510         LogTrace("DTDQM|DTMonitorModule|DTDigiTask") << "[DTDigiTask] Synch noise in chamber: " << chId
00511                                                      << " with # digis: " << hitMapIt->second << endl;
00512         
00513         if(chMap.find(wh) == chMap.end()) { chMap[wh] = 0; }
00514         chMap[wh]++ ;
00515         
00516         syncNoisyChambers.insert(chId);
00517         
00518         wheelHistos["SyncNoiseEvents"][wh]->Fill(chId.sector(),chId.station()); 
00519         
00520         // Only needed in case of ratio map not used right now
00521         // FIXME check and optimize
00522         // nSynchNoiseEvents[iter->first]++;    
00523         // FIXME: should update all chambers each event 
00524         // wheelHistos["SyncNoiseEvents"][(*iter).first.wheel()]->setBinContent((*iter).first.sector(),(*iter).first.station(),
00525         //                                                              (double)nSynchNoiseEvents[iter->first]/(double)nevents); 
00526 
00527       }
00528     }
00529 
00530     // fill # of noisy ch per wheel plot
00531     map<int,int>::const_iterator chMapIt  = chMap.begin();
00532     map<int,int>::const_iterator chMapEnd = chMap.end();
00533     for (; chMapIt != chMapEnd; ++chMapIt) { 
00534       wheelHistos["SyncNoiseChambs"][(*chMapIt).first]->Fill((*chMapIt).second); 
00535     }   
00536     
00537     // clear the map of # of digis per chamber: not needed anymore
00538     hitMap.clear();
00539 
00540     if (syncNoisyChambers.size() != 0) {
00541       LogVerbatim("DTDQM|DTMonitorModule|DTDigiTask") << "[DTDigiTask] Synch Noise in event: " << nevents;
00542       if(filterSyncNoise) LogVerbatim("DTDQM|DTMonitorModule|DTDigiTask") << "\tnoisy time-boxes and occupancy will not be filled!" << endl; 
00543       syncNumTot++;
00544       syncNum++;
00545     }
00546 
00547     // Logging of "large" synch Noisy events in private DQM
00548     if (syncNoisyChambers.size() > 3) {
00549       time_t eventTime = time_t(event.time().value()>>32);
00550 
00551       LogVerbatim("DTDQM|DTMonitorModule|DTDigiTask|DTSynchNoise") 
00552         << "[DTDigiTask] At least 4 Synch Noisy chambers in Run : " << event.id().run() 
00553         << " Lumi : "  << event.id().luminosityBlock()
00554         << " Event : " << event.id().event()
00555         << " at time : " << ctime(&eventTime) << endl;
00556 
00557       set<DTChamberId>::const_iterator chIt  = syncNoisyChambers.begin();
00558       set<DTChamberId>::const_iterator chEnd = syncNoisyChambers.end();
00559 
00560       stringstream synchNoisyCh;
00561       for (;chIt!=chEnd;++chIt) { synchNoisyCh << " " << (*chIt); }
00562       LogVerbatim("DTDQM|DTMonitorModule|DTDigiTask|DTSynchNoise") << 
00563         "[DTDigiTask] Chamber List :" << synchNoisyCh.str() << endl;
00564       
00565     }
00566 
00567 
00568     if (nevents%1000 == 0) {
00569       LogVerbatim("DTDQM|DTMonitorModule|DTDigiTask") << (syncNumTot*100./nevents) << "% sync noise events since the beginning \n"
00570                                                       << (syncNum*0.1) << "% sync noise events in the last 1000 events " << endl;
00571       syncNum = 0;
00572     }
00573   }
00574 
00575   bool isSyncNoisy = false;
00576 
00577   DTDigiCollection::DigiRangeIterator dtLayerId_It;
00578   for (dtLayerId_It=dtdigis->begin(); dtLayerId_It!=dtdigis->end(); ++dtLayerId_It) { // Loop over layers
00579     isSyncNoisy = false;
00580     // check if chamber labeled as synch noisy
00581     if (filterSyncNoise) {
00582       DTChamberId chId = ((*dtLayerId_It).first).chamberId();
00583       if(syncNoisyChambers.find(chId) != syncNoisyChambers.end()) {
00584         isSyncNoisy = true;
00585       }
00586     }
00587 
00588     for (DTDigiCollection::const_iterator digiIt = ((*dtLayerId_It).second).first;
00589          digiIt!=((*dtLayerId_It).second).second; ++digiIt) { // Loop over all digis
00590 
00591         bool isNoisy = false;
00592         bool isFEMasked = false;
00593         bool isTDCMasked = false;
00594         bool isTrigMask = false;
00595         bool isDead = false;
00596         bool isNohv = false;
00597         if(checkNoisyChannels) {
00598           const DTWireId wireId(((*dtLayerId_It).first), (*digiIt).wire());
00599           statusMap->cellStatus(wireId, isNoisy, isFEMasked, isTDCMasked, isTrigMask, isDead, isNohv);
00600         }
00601         
00602         
00603 
00604         // Get the useful IDs
00605         const  DTSuperLayerId dtSLId = ((*dtLayerId_It).first).superlayerId();
00606         uint32_t indexSL = dtSLId.rawId();
00607         const  DTChamberId dtChId = dtSLId.chamberId(); 
00608         uint32_t indexCh = dtChId.rawId();
00609         int layer_number=((*dtLayerId_It).first).layer();
00610         int superlayer_number=dtSLId.superlayer();
00611         const  DTLayerId dtLId = (*dtLayerId_It).first;
00612         
00613         // Read the ttrig DB or set a rough value from config
00614         // ttrig and rms are TDC counts
00615         if (readTTrigDB)
00616           tTrigMap->get( ((*dtLayerId_It).first).superlayerId(),
00617                          tTrig, tTrigRMS, kFactor, DTTimeUnits::counts); 
00618         else tTrig = defaultTTrig;
00619         
00620         int inTimeHitsLowerBoundCorr = int(round(tTrig)) - inTimeHitsLowerBound;
00621         int inTimeHitsUpperBoundCorr = int(round(tTrig)) + tMax + inTimeHitsUpperBound;
00622         
00623         float t0; float t0RMS;
00624         int tdcTime = (*digiIt).countsTDC();
00625         
00626         if (subtractT0) {
00627           const DTWireId dtWireId(((*dtLayerId_It).first), (*digiIt).wire());
00628           // t0s and rms are TDC counts
00629           t0Map->get(dtWireId, t0, t0RMS, DTTimeUnits::counts) ;
00630           tdcTime += int(round(t0));
00631         }
00632 
00633         
00634 
00635         // Fill Time-Boxes
00636         // NOTE: avoid to fill TB and PhotoPeak with noise. Occupancy are filled anyway
00637         if (( !isNoisy ) && (!isSyncNoisy)) { // Discard noisy channels
00638           // TimeBoxes per SL
00639           histoTag = "TimeBox" + triggerSource();
00640           if (digiHistos[histoTag].find(indexSL) == digiHistos[histoTag].end())
00641             bookHistos( dtSLId, string("TimeBoxes"), histoTag );
00642           (digiHistos.find(histoTag)->second).find(indexSL)->second->Fill(tdcTime);
00643           if(doLayerTimeBoxes)
00644             (digiHistos.find(histoTag)->second).find((*dtLayerId_It).first.rawId())->second->Fill(tdcTime);
00645           // FIXME: remove the time distribution for the after-pulses     
00646           // 2nd - 1st (CathodPhotoPeak) per SL
00647           //      if ( (*digiIt).number() == 1 ) {
00648             
00649           //        DTDigiCollection::const_iterator firstDigiIt = digiIt;
00650           //        firstDigiIt--;
00651             
00652           //        histoTag = "CathodPhotoPeak";
00653           //        if (digiHistos[histoTag].find(indexSL) == digiHistos[histoTag].end())
00654           //          bookHistos( dtSLId, string("CathodPhotoPeaks"), histoTag );
00655           //        (digiHistos.find(histoTag)->second).find(indexSL)->second->Fill((*digiIt).countsTDC()-
00656           //                                                                        (*firstDigiIt).countsTDC());
00657           //      }
00658         }
00659 
00660         // Fill Occupancies
00661         if (!isSyncNoisy) { // Discard synch noisy channels 
00662 
00663           if (doAllHitsOccupancies) { // fill occupancies for all hits
00664             //Occupancies per chamber & layer
00665             histoTag = "OccupancyAllHits_perCh";
00666             map<uint32_t, MonitorElement*>::const_iterator mappedHisto =
00667               digiHistos[histoTag].find(indexCh);
00668             if (mappedHisto == digiHistos[histoTag].end()) { // dynamic booking
00669               bookHistos(dtChId, string("Occupancies"), histoTag);
00670               mappedHisto = digiHistos[histoTag].find(indexCh);
00671             }
00672             mappedHisto->second->Fill((*digiIt).wire(),(layer_number+(superlayer_number-1)*4)-1);
00673 
00674             
00675             // Fill the chamber occupancy
00676             histoTag = "OccupancyAllHits";
00677             map<int, MonitorElement*>::const_iterator histoPerWheel =
00678               wheelHistos[histoTag].find(dtChId.wheel());
00679             if(histoPerWheel ==  wheelHistos[histoTag].end()) { // dynamic booking
00680               bookHistos(dtChId.wheel(), string("Occupancies"), histoTag);
00681               histoPerWheel = wheelHistos[histoTag].find(dtChId.wheel());
00682             }
00683             histoPerWheel->second->Fill(dtChId.sector(),dtChId.station()); // FIXME: normalize to # of layers
00684            
00685             
00686           } 
00687 
00688           if(doNoiseOccupancies) { // fill occupancies for hits before the ttrig
00689             if (tdcTime < inTimeHitsLowerBoundCorr ) { 
00690               // FIXME: what about tdcTime > inTimeHitsUpperBoundCorr ???
00691 
00692               // Noise: Before tTrig
00693               //Occupancies Noise per chamber & layer
00694               histoTag = "OccupancyNoise_perCh";
00695               map<uint32_t, MonitorElement*>::const_iterator mappedHisto =
00696                 digiHistos[histoTag].find(indexCh);
00697               if(mappedHisto == digiHistos[histoTag].end()) {
00698                 bookHistos(dtChId, string("Occupancies"), histoTag);
00699                 mappedHisto = digiHistos[histoTag].find(indexCh);
00700               }
00701               mappedHisto->second->Fill((*digiIt).wire(),
00702                                         (layer_number+(superlayer_number-1)*4)-1);
00703 
00704               // Fill the chamber occupancy
00705               histoTag = "OccupancyNoise";
00706               map<int, MonitorElement*>::const_iterator histoPerWheel =
00707                 wheelHistos[histoTag].find(dtChId.wheel());
00708               if(histoPerWheel ==  wheelHistos[histoTag].end()) { // dynamic booking
00709                 bookHistos(dtChId.wheel(), string("Occupancies"), histoTag);
00710                 histoPerWheel = wheelHistos[histoTag].find(dtChId.wheel());
00711               }
00712               histoPerWheel->second->Fill(dtChId.sector(),dtChId.station()); // FIXME: normalize to # of layers
00713 
00714             } 
00715           }
00716           
00717           if(doInTimeOccupancies) { // fill occpunacies for in-time hits only
00718             if (tdcTime > inTimeHitsLowerBoundCorr && tdcTime < inTimeHitsUpperBoundCorr) { 
00719               // Physical hits: within the time window  
00720 
00721               //Occupancies Signal per chamber & layer
00722               histoTag = "OccupancyInTimeHits_perCh";
00723               map<uint32_t, MonitorElement*>::const_iterator mappedHisto =
00724                 digiHistos[histoTag].find(indexCh);
00725               if(mappedHisto == digiHistos[histoTag].end()) {
00726                 bookHistos(dtChId, string("Occupancies"), histoTag);
00727                 mappedHisto = digiHistos[histoTag].find(indexCh);
00728               }
00729               mappedHisto->second->Fill((*digiIt).wire(),
00730                                         (layer_number+(superlayer_number-1)*4)-1);
00731 
00732               // Fill the chamber occupancy
00733               histoTag = "OccupancyInTimeHits";
00734               map<int, MonitorElement*>::const_iterator histoPerWheel =
00735                 wheelHistos[histoTag].find(dtChId.wheel());
00736               if(histoPerWheel ==  wheelHistos[histoTag].end()) { // dynamic booking
00737                 bookHistos(dtChId.wheel(), string("Occupancies"), histoTag);
00738                 histoPerWheel = wheelHistos[histoTag].find(dtChId.wheel());
00739               }
00740               histoPerWheel->second->Fill(dtChId.sector(),dtChId.station()); // FIXME: normalize to # of layers
00741 
00742             }
00743           }
00744         }
00745     }
00746   }
00747   
00748   syncNoisyChambers.clear();
00749 }
00750 
00751 
00752 string DTDigiTask::triggerSource() {
00753 
00754   string l1ASource;
00755 
00756   if (!isLocalRun) {
00757     for (std::vector<LTCDigi>::const_iterator ltc_it = ltcdigis->begin(); ltc_it != ltcdigis->end(); ltc_it++){
00758       int otherTriggerSum=0;
00759       for (int i = 1; i < 6; i++)
00760         otherTriggerSum += int((*ltc_it).HasTriggered(i));
00761       
00762       if ((*ltc_it).HasTriggered(0) && otherTriggerSum == 0) 
00763         l1ASource = "DTonly";
00764       else if (!(*ltc_it).HasTriggered(0))
00765         l1ASource = "NoDT";
00766       else if ((*ltc_it).HasTriggered(0) && otherTriggerSum > 0)
00767         l1ASource = "DTalso";
00768     }
00769   }
00770 
00771   return l1ASource;
00772 
00773 }
00774 
00775 
00776 string DTDigiTask::topFolder() const {
00777 
00778   if(tpMode) return string("DT/10-TestPulses/");
00779   return string("DT/01-Digi/");
00780 
00781 }
00782 
00783 
00784 
00785 
00786 void DTDigiTask::endLuminosityBlock(const edm::LuminosityBlock& lumiSeg, const edm::EventSetup& setup) {
00787 
00788 // To be used for ratio plots not used right now
00789 //  Update all histos for SynchNoise if needed
00790 //   if(lookForSyncNoise || filterSyncNoise) {
00791 //     //loop over chambers with synch noise events and update their entries in the histos
00792 //     for(map<DTChamberId, int>::const_iterator nEvPerch = nSynchNoiseEvents.begin();
00793 //      nEvPerch != nSynchNoiseEvents.end(); ++nEvPerch) {
00794 //       DTChamberId chId = (*nEvPerch).first;
00795 //       wheelHistos["SyncNoiseEvents"][chId.wheel()]->setBinContent(chId.sector(),chId.station(),
00796 //                                                                (double)nSynchNoiseEvents[chId]/(double)nevents); 
00797 //    }
00798 //  }
00799 
00800 }