CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/CalibMuon/DTCalibration/plugins/DTNoiseCalibration.cc

Go to the documentation of this file.
00001 /*
00002  *  See header file for a description of this class.
00003  *
00004  *  $Date: 2010/02/16 10:03:23 $
00005  *  $Revision: 1.12 $
00006  *  \author G. Mila - INFN Torino
00007  */
00008 
00009 
00010 #include "CalibMuon/DTCalibration/plugins/DTNoiseCalibration.h"
00011 
00012 
00013 // Framework
00014 #include "FWCore/Framework/interface/Event.h"
00015 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00016 #include <FWCore/Framework/interface/EventSetup.h>
00017 
00018 // Geometry
00019 #include "Geometry/DTGeometry/interface/DTLayer.h"
00020 #include "Geometry/DTGeometry/interface/DTGeometry.h"
00021 #include "Geometry/DTGeometry/interface/DTTopology.h"
00022 #include "Geometry/Records/interface/MuonGeometryRecord.h"
00023 
00024 // Digis
00025 #include <DataFormats/DTDigi/interface/DTDigi.h>
00026 #include "DataFormats/DTDigi/interface/DTDigiCollection.h"
00027 #include "CondFormats/DataRecord/interface/DTStatusFlagRcd.h"
00028 #include "CondFormats/DTObjects/interface/DTStatusFlag.h"
00029 #include "CondFormats/DTObjects/interface/DTReadOutMapping.h"
00030 
00031 // Database
00032 #include <CondFormats/DTObjects/interface/DTTtrig.h>
00033 #include <CondFormats/DataRecord/interface/DTTtrigRcd.h>
00034 
00035 #include "CalibMuon/DTCalibration/interface/DTCalibDBUtils.h"
00036 
00037 #include "TH2F.h"
00038 #include "TFile.h"
00039 
00040 using namespace edm;
00041 using namespace std;
00042 
00043 
00044 DTNoiseCalibration::DTNoiseCalibration(const edm::ParameterSet& ps){
00045 
00046   cout << "[DTNoiseCalibration]: Constructor" <<endl;
00047 
00048   // Get the debug parameter for verbose output
00049   debug = ps.getUntrackedParameter<bool>("debug");
00050 
00051   // Get the label to retrieve digis from the event
00052   digiLabel = ps.getUntrackedParameter<string>("digiLabel");
00053 
00054   // The analysis type
00055   fastAnalysis = ps.getUntrackedParameter<bool>("fastAnalysis", true);
00056   // The wheel & sector interested for the time-dependent analysis
00057   wh = ps.getUntrackedParameter<int>("wheel", 0);
00058   sect = ps.getUntrackedParameter<int>("sector", 6);
00059 
00060   // The trigger mode
00061   cosmicRun = ps.getUntrackedParameter<bool>("cosmicRun", false);
00062 
00063   // The trigger width (if noise run)
00064   TriggerWidth = ps.getUntrackedParameter<int>("TriggerWidth");
00065 
00066   //get the offset to look for the noise
00067   theOffset = ps.getUntrackedParameter<double>("theOffset",500.);
00068 
00069   // The root file which will contain the histos
00070   string rootFileName = ps.getUntrackedParameter<string>("rootFileName");
00071   theFile = new TFile(rootFileName.c_str(), "RECREATE");
00072   theFile->cd();
00073 
00074   dbLabel  = ps.getUntrackedParameter<string>("dbLabel", "");
00075 
00076   parameters=ps;
00077 
00078 }
00079 
00080 void DTNoiseCalibration::beginRun(const edm::Run& run, const edm::EventSetup& setup ) {
00081 
00082   cout <<"[DTNoiseCalibration]: BeginJob"<<endl; 
00083   nevents = 0;
00084   counter = 0;
00085 
00086   // Get the DT Geometry
00087   setup.get<MuonGeometryRecord>().get(dtGeom);
00088 
00089   // tTrig 
00090   if (parameters.getUntrackedParameter<bool>("readDB", true)) 
00091     setup.get<DTTtrigRcd>().get(dbLabel,tTrigMap);
00092 
00093   // TDC time distribution
00094   int numBin = (TriggerWidth*(32/25))/50;
00095   hTDCTriggerWidth = new TH1F("TDC_Time_Distribution", "TDC_Time_Distribution", numBin, 0, TriggerWidth*(32/25));
00096 
00097 }
00098 
00099 
00100 void DTNoiseCalibration::analyze(const edm::Event& e, const edm::EventSetup& context){
00101   nevents++;
00102   if(debug)
00103     cout<<"nevents: "<<nevents<<endl;
00104   
00105   // Get the digis from the event
00106   edm::Handle<DTDigiCollection> dtdigis;
00107   e.getByLabel(digiLabel, dtdigis);
00108 
00109   TH1F *hOccupancyHisto;
00110   TH2F *hEvtPerWireH;
00111   string Histo2Name;
00112 
00113   // LOOP OVER ALL THE DIGIS OF THE EVENT
00114   DTDigiCollection::DigiRangeIterator dtLayerId_It;
00115   for (dtLayerId_It=dtdigis->begin(); dtLayerId_It!=dtdigis->end(); ++dtLayerId_It){
00116     for (DTDigiCollection::const_iterator digiIt = ((*dtLayerId_It).second).first;
00117          digiIt!=((*dtLayerId_It).second).second; ++digiIt){
00118 
00119       //Check the TDC trigger width
00120       int tdcTime = (*digiIt).countsTDC();
00121       if(!cosmicRun){   
00122         if(debug)
00123           cout<<"tdcTime (ns): "<<(tdcTime*25)/32<<endl;
00124         if(((tdcTime*25)/32)>TriggerWidth){
00125           cout<<"***Error*** : your digi has a tdcTime (ns) higher than the TDC trigger width :"<<(tdcTime*25)/32<<endl;
00126           abort();
00127         }
00128       } 
00129 
00130       if((!fastAnalysis &&
00131          (*dtLayerId_It).first.superlayerId().chamberId().wheel()==wh &&
00132          (*dtLayerId_It).first.superlayerId().chamberId().sector()==sect) ||
00133          fastAnalysis)
00134         hTDCTriggerWidth->Fill(tdcTime);
00135 
00136       // Set the window of interest if the run is triggered by cosmics
00137       if ( parameters.getUntrackedParameter<bool>("readDB", true) ) {
00138         tTrigMap->get( ((*dtLayerId_It).first).superlayerId(), tTrig, tTrigRMS, kFactor, DTTimeUnits::counts );
00139 
00140         upperLimit = tTrig - theOffset;
00141       }
00142       else { 
00143         tTrig = parameters.getUntrackedParameter<int>("defaultTtrig", 4000);
00144         upperLimit = tTrig - theOffset;
00145       }
00146         
00147       if((cosmicRun && (*digiIt).countsTDC()<upperLimit) || (!cosmicRun) ){
00148 
00149         if(debug && cosmicRun)
00150           cout<<"tdcTime (ns): "<<((*digiIt).countsTDC()*25)/32<<" --- TriggerWidth (ns): "<<(upperLimit*25)/32<<endl;
00151 
00152         // Get the number of wires
00153         const  DTLayerId dtLId = (*dtLayerId_It).first;
00154         const DTTopology& dtTopo = dtGeom->layer(dtLId)->specificTopology();
00155         const int nWires = dtTopo.channels();
00156         const int firstWire = dtTopo.firstChannel();
00157         const int lastWire = dtTopo.lastChannel();
00158         
00159         // book the occupancy histos
00160         theFile->cd();
00161         if((!fastAnalysis &&
00162            dtLId.superlayerId().chamberId().wheel()==wh &&
00163            dtLId.superlayerId().chamberId().sector()==sect) ||
00164            fastAnalysis){
00165           hOccupancyHisto = theHistoOccupancyMap[dtLId];
00166           if(hOccupancyHisto == 0) {
00167             string HistoName = "DigiOccupancy_" + getLayerName(dtLId);
00168             theFile->cd();
00169             hOccupancyHisto = new TH1F(HistoName.c_str(), HistoName.c_str(), nWires, firstWire, lastWire+1);
00170             if(debug)
00171               cout << "  New Occupancy Histo: " << hOccupancyHisto->GetName() << endl;
00172             theHistoOccupancyMap[dtLId] = hOccupancyHisto;
00173           }
00174           hOccupancyHisto->Fill((*digiIt).wire());
00175         }
00176 
00177         // book the digi event plot every 1000 events if the analysis is not "fast" and if is the correct sector
00178         if(!fastAnalysis &&
00179            dtLId.superlayerId().chamberId().wheel()==wh &&
00180            dtLId.superlayerId().chamberId().sector()==sect) {
00181           if(theHistoEvtPerWireMap.find(dtLId) == theHistoEvtPerWireMap.end() ||
00182              (theHistoEvtPerWireMap.find(dtLId) != theHistoEvtPerWireMap.end() &&
00183               skippedPlot[dtLId] != counter)){ 
00184             skippedPlot[dtLId] = counter;
00185             stringstream toAppend; toAppend << counter;
00186             Histo2Name = "DigiPerWirePerEvent_" + getLayerName(dtLId) + "_" + toAppend.str();
00187             theFile->cd();
00188             hEvtPerWireH = new TH2F(Histo2Name.c_str(), Histo2Name.c_str(), 1000,0.5,1000.5,nWires, firstWire, lastWire+1);
00189             if(hEvtPerWireH){
00190               if(debug)
00191                 cout << "  New Histo with the number of digi per evt per wire: " << hEvtPerWireH->GetName() << endl;
00192               theHistoEvtPerWireMap[dtLId]=hEvtPerWireH;
00193             }
00194           }
00195         }
00196       }
00197     }
00198   }
00199     
00200   //Fill the plot of the number of digi per event per wire
00201   std::map<int,int > DigiPerWirePerEvent;
00202   // LOOP OVER ALL THE CHAMBERS
00203   vector<DTChamber*>::const_iterator ch_it = dtGeom->chambers().begin();
00204   vector<DTChamber*>::const_iterator ch_end = dtGeom->chambers().end();
00205   for (; ch_it != ch_end; ++ch_it) {
00206     DTChamberId ch = (*ch_it)->id();
00207     vector<const DTSuperLayer*>::const_iterator sl_it = (*ch_it)->superLayers().begin(); 
00208     vector<const DTSuperLayer*>::const_iterator sl_end = (*ch_it)->superLayers().end();
00209     // Loop over the SLs
00210     for(; sl_it != sl_end; ++sl_it) {
00211       DTSuperLayerId sl = (*sl_it)->id();
00212       vector<const DTLayer*>::const_iterator l_it = (*sl_it)->layers().begin(); 
00213       vector<const DTLayer*>::const_iterator l_end = (*sl_it)->layers().end();
00214       // Loop over the Ls
00215       for(; l_it != l_end; ++l_it) {
00216         DTLayerId layerId = (*l_it)->id();
00217         
00218         // Get the number of wires
00219         const DTTopology& dtTopo = dtGeom->layer(layerId)->specificTopology();
00220         const int firstWire = dtTopo.firstChannel();
00221         const int lastWire = dtTopo.lastChannel();
00222           
00223         if (theHistoEvtPerWireMap.find(layerId) != theHistoEvtPerWireMap.end() &&
00224             skippedPlot[layerId] == counter) {
00225           
00226           for (int wire=firstWire; wire<=lastWire; wire++) {
00227             DigiPerWirePerEvent[wire]= 0;
00228           }     
00229           // loop over all the digis of the event
00230           DTDigiCollection::Range layerDigi= dtdigis->get(layerId);
00231           for (DTDigiCollection::const_iterator digi = layerDigi.first;
00232                digi!=layerDigi.second;
00233                ++digi){
00234             if((cosmicRun && (*digi).countsTDC()<upperLimit) || (!cosmicRun))
00235               DigiPerWirePerEvent[(*digi).wire()]+=1;
00236           }
00237           // fill the digi event histo
00238           for (int wire=firstWire; wire<=lastWire; wire++) {
00239             theFile->cd();
00240             int histoEvents = nevents - (counter*1000);
00241             theHistoEvtPerWireMap[layerId]->Fill(histoEvents,wire,DigiPerWirePerEvent[wire]);
00242           }
00243         }
00244       } //Loop Ls
00245     } //Loop SLs
00246   } //Loop chambers
00247   
00248   
00249   if(nevents % 1000 == 0) {
00250     counter++;
00251     // save the digis event plot on file
00252     for(map<DTLayerId,  TH2F* >::const_iterator lHisto = theHistoEvtPerWireMap.begin();
00253         lHisto != theHistoEvtPerWireMap.end();
00254         lHisto++) {
00255       theFile->cd();
00256       if((*lHisto).second)
00257         (*lHisto).second->Write();
00258     }
00259     theHistoEvtPerWireMap.clear();
00260   }
00261   
00262 }
00263 
00264 
00265 void DTNoiseCalibration::endJob(){
00266 
00267   cout << "[DTNoiseCalibration] endjob called!" <<endl;
00268 
00269   // save the TDC digi plot
00270   theFile->cd();
00271   hTDCTriggerWidth->Write();
00272 
00273   // save on file the occupancy histo and write the list of noisy cells
00274   double TriggerWidth_s=0;
00275   DTStatusFlag *statusMap = new DTStatusFlag();
00276   for(map<DTLayerId, TH1F*>::const_iterator lHisto = theHistoOccupancyMap.begin();
00277       lHisto != theHistoOccupancyMap.end();
00278       lHisto++) {
00279     if(cosmicRun){
00280       if ( parameters.getUntrackedParameter<bool>("readDB", true) ) 
00281         tTrigMap->get( ((*lHisto).first).superlayerId(), tTrig, tTrigRMS, kFactor,
00282                      DTTimeUnits::counts );
00283      else tTrig = parameters.getUntrackedParameter<int>("defaultTtrig", 4000);
00284       double TriggerWidth_ns = ((tTrig-theOffset)*25)/32;
00285       TriggerWidth_s = TriggerWidth_ns/1e9;
00286     }
00287     if(!cosmicRun)
00288       TriggerWidth_s = double(TriggerWidth/1e9);
00289     if(debug)
00290       cout<<"TriggerWidth (s): "<<TriggerWidth_s<<"  TotEvents: "<<nevents<<endl;
00291     double normalization = 1/double(nevents*TriggerWidth_s);
00292     if((*lHisto).second){
00293       (*lHisto).second->Scale(normalization);
00294       theFile->cd();
00295       (*lHisto).second->Write();
00296       const DTTopology& dtTopo = dtGeom->layer((*lHisto).first)->specificTopology();
00297       const int firstWire = dtTopo.firstChannel();
00298       const int lastWire = dtTopo.lastChannel();
00299       for(int bin=firstWire; bin<=lastWire; bin++){
00300         //from definition of "noisy cell"
00301         if((*lHisto).second->GetBinContent(bin)>500){
00302           DTWireId wireID((*lHisto).first, bin);
00303           statusMap->setCellNoise(wireID,1);
00304         }
00305       }
00306     }
00307   }
00308   cout << "Writing Noise Map object to DB!" << endl;
00309   string record = "DTStatusFlagRcd";
00310   DTCalibDBUtils::writeToDB<DTStatusFlag>(record, statusMap);
00311 
00312  
00313   /*
00314   //save the digi event plot per SuperLayer 
00315   bool histo=false;
00316   map<DTSuperLayerId, vector<int> > maxPerSuperLayer;
00317   int numPlot = (nevents/1000);
00318   int num=0;
00319   // loop over the numPlot 
00320   for(int i=0; i<numPlot; i++){  
00321     vector<DTChamber*>::const_iterator ch_it = dtGeom->chambers().begin();
00322     vector<DTChamber*>::const_iterator ch_end = dtGeom->chambers().end();
00323     // Loop over the chambers
00324     for (; ch_it != ch_end; ++ch_it) {
00325       DTChamberId ch = (*ch_it)->id();
00326       vector<const DTSuperLayer*>::const_iterator sl_it = (*ch_it)->superLayers().begin(); 
00327       vector<const DTSuperLayer*>::const_iterator sl_end = (*ch_it)->superLayers().end();
00328       // Loop over the SLs
00329       for(; sl_it != sl_end; ++sl_it) {
00330         DTSuperLayerId sl = (*sl_it)->id();
00331         vector<const DTLayer*>::const_iterator l_it = (*sl_it)->layers().begin(); 
00332         vector<const DTLayer*>::const_iterator l_end = (*sl_it)->layers().end();
00333         double dummy = pow(10.,10.);
00334         maxPerSuperLayer[sl].push_back(0);
00335         // Loop over the Ls
00336         for(; l_it != l_end; ++l_it) {
00337           DTLayerId layerId = (*l_it)->id();
00338 
00339           if (theHistoEvtPerWireMap.find(layerId) != theHistoEvtPerWireMap.end() &&
00340               theHistoEvtPerWireMap[layerId].size() > i){
00341             if (theHistoEvtPerWireMap[layerId][i]->GetMaximum(dummy)>maxPerSuperLayer[sl][i])
00342               maxPerSuperLayer[sl][i] = theHistoEvtPerWireMap[layerId][i]->GetMaximum(dummy);
00343           }
00344         }
00345       } // loop over SLs
00346     } // loop over chambers
00347   } // loop over numPlot
00348 
00349   // loop over the numPlot 
00350   for(int i=0; i<numPlot; i++){
00351     vector<DTChamber*>::const_iterator chamber_it = dtGeom->chambers().begin();
00352     vector<DTChamber*>::const_iterator chamber_end = dtGeom->chambers().end();
00353     // Loop over the chambers
00354     for (; chamber_it != chamber_end; ++chamber_it) {
00355       DTChamberId ch = (*chamber_it)->id();
00356       vector<const DTSuperLayer*>::const_iterator sl_it = (*chamber_it)->superLayers().begin(); 
00357       vector<const DTSuperLayer*>::const_iterator sl_end = (*chamber_it)->superLayers().end();
00358       // Loop over the SLs
00359       for(; sl_it != sl_end; ++sl_it) {
00360         DTSuperLayerId sl = (*sl_it)->id();
00361         vector<const DTLayer*>::const_iterator l_it = (*sl_it)->layers().begin(); 
00362         vector<const DTLayer*>::const_iterator l_end = (*sl_it)->layers().end();
00363 
00364         stringstream num; num << i;
00365         string canvasName = "c" + getSuperLayerName(sl) + "_" + num.str();
00366         TCanvas c1(canvasName.c_str(),canvasName.c_str(),600,780);
00367         TLegend *leg=new TLegend(0.5,0.6,0.7,0.8);
00368         for(; l_it != l_end; ++l_it) {
00369           DTLayerId layerId = (*l_it)->id();
00370 
00371           if (theHistoEvtPerWireMap.find(layerId) != theHistoEvtPerWireMap.end() &&
00372             theHistoEvtPerWireMap[layerId].size() > i){
00373 
00374             string TitleName = "DigiPerWirePerEvent_" + getSuperLayerName(sl) + "_" + num.str();
00375             theHistoEvtPerWireMap[layerId][i]->SetTitle(TitleName.c_str());
00376             stringstream layer; layer << layerId.layer();       
00377             string legendHisto = "layer " + layer.str();
00378             leg->AddEntry(theHistoEvtPerWireMap[layerId][i],legendHisto.c_str(),"L");
00379             theHistoEvtPerWireMap[layerId][i]->SetMaximum(maxPerSuperLayer[sl][i]);
00380             if(histo==false)
00381               theHistoEvtPerWireMap[layerId][i]->Draw("lego");
00382             else
00383               theHistoEvtPerWireMap[layerId][i]->Draw("same , lego");
00384             theHistoEvtPerWireMap[layerId][i]->SetLineColor(layerId.layer());
00385             histo=true;
00386           }
00387         } // loop over Ls
00388         if(histo){
00389           leg->Draw("same");
00390           theFile->cd();
00391           c1.Write();
00392         }
00393         histo=false;
00394       } // loop over SLs
00395     } // loop over chambers 
00396   } // loop over numPlot
00397   */
00398 
00399 }
00400 
00401 
00402 
00403 DTNoiseCalibration::~DTNoiseCalibration(){
00404 
00405   cout << "DTNoiseCalibration: analyzed " << nevents << " events" <<endl;
00406   theFile->Close();
00407 
00408 }
00409 
00410 
00411 
00412 string DTNoiseCalibration::getLayerName(const DTLayerId& lId) const {
00413 
00414   const  DTSuperLayerId dtSLId = lId.superlayerId();
00415   const  DTChamberId dtChId = dtSLId.chamberId(); 
00416   stringstream Layer; Layer << lId.layer();
00417   stringstream superLayer; superLayer << dtSLId.superlayer();
00418   stringstream wheel; wheel << dtChId.wheel();  
00419   stringstream station; station << dtChId.station();    
00420   stringstream sector; sector << dtChId.sector();
00421   
00422   string LayerName = 
00423     "W" + wheel.str()
00424     + "_St" + station.str() 
00425     + "_Sec" + sector.str() 
00426     + "_SL" + superLayer.str()
00427     + "_L" + Layer.str();
00428   
00429   return LayerName;
00430 
00431 }
00432 
00433 
00434 string DTNoiseCalibration::getSuperLayerName(const DTSuperLayerId& dtSLId) const {
00435 
00436   const  DTChamberId dtChId = dtSLId.chamberId(); 
00437   stringstream superLayer; superLayer << dtSLId.superlayer();
00438   stringstream wheel; wheel << dtChId.wheel();  
00439   stringstream station; station << dtChId.station();    
00440   stringstream sector; sector << dtChId.sector();
00441   
00442   string SuperLayerName = 
00443     "W" + wheel.str()
00444     + "_St" + station.str() 
00445     + "_Sec" + sector.str() 
00446     + "_SL" + superLayer.str();
00447   
00448   return SuperLayerName;
00449 
00450 }
00451 
00452 
00453 
00454