CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/DQM/DTMonitorModule/src/DTDigiTask.cc

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