CMS 3D CMS Logo

/data/git/CMSSW_5_3_11_patch5/src/DQM/DTMonitorModule/src/DTAlbertoBenvenutiTask.cc

Go to the documentation of this file.
00001 
00002 
00003 /*
00004  *  See header file for a description of this class.
00005  *
00006  *  $Date: 2011/11/12 09:18:41 $
00007  *  $Revision: 1.11 $
00008  *  \author G. Mila - INFN Torino
00009  */
00010 
00011 
00012 #include <DQM/DTMonitorModule/src/DTAlbertoBenvenutiTask.h>
00013 
00014 // Framework
00015 #include <FWCore/Framework/interface/EventSetup.h>
00016 
00017 // Digis
00018 #include <DataFormats/DTDigi/interface/DTDigi.h>
00019 #include <DataFormats/DTDigi/interface/DTDigiCollection.h>
00020 #include <DataFormats/MuonDetId/interface/DTLayerId.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 
00038 #include <stdio.h>
00039 #include <sstream>
00040 #include <math.h>
00041 #include "TFile.h"
00042 #include "TPostScript.h"
00043 #include "TCanvas.h"
00044 
00045 using namespace edm;
00046 using namespace std;
00047 
00048 
00049 DTAlbertoBenvenutiTask::DTAlbertoBenvenutiTask(const edm::ParameterSet& ps){
00050   
00051   debug = ps.getUntrackedParameter<bool>("debug", false);
00052   if(debug)
00053     cout<<"[DTAlbertoBenvenutiTask]: Constructor"<<endl;
00054 
00055   outputFile = ps.getUntrackedParameter<string>("outputFile", "DTDigiSources.root");
00056   maxTDCHits = ps.getUntrackedParameter<int>("maxTDCHits",1000);
00057   
00058   // tMax (not yet from the DB)
00059   tMax = parameters.getParameter<int>("defaultTmax");
00060 
00061   parameters = ps; 
00062 
00063 }
00064 
00065 
00066 DTAlbertoBenvenutiTask::~DTAlbertoBenvenutiTask(){
00067 
00068   if(debug)
00069     cout << "DTAlbertoBenvenutiTask: analyzed " << nevents << " events" << endl;
00070 
00071 }
00072 
00073 
00074 void DTAlbertoBenvenutiTask::endJob(){
00075 
00076   if(debug)
00077     cout<<"[DTAlbertoBenvenutiTask] endjob called!"<<endl;
00078 
00079   map< DTChamberId, vector<TH1F*> > TBMap_perChamber;
00080 
00081   for(map<DTWireId, TH1F* >::const_iterator wHisto = TBMap.begin();
00082       wHisto != TBMap.end();
00083       wHisto++) {
00084     DTChamberId chId = (*wHisto).first.layerId().superlayerId().chamberId();
00085     TBMap_perChamber[chId].push_back((*wHisto).second);
00086   }
00087  
00088   
00089   for(map<DTChamberId, vector<TH1F*> >::const_iterator Histo = TBMap_perChamber.begin();
00090       Histo != TBMap_perChamber.end();
00091       Histo++) {
00092     stringstream station; station << (*Histo).first.station();
00093     stringstream sector; sector << (*Histo).first.sector();     
00094     stringstream wheel; wheel << (*Histo).first.wheel();
00095     
00096     string fileTag = "TimeBoxes";
00097     string fileName = fileTag
00098       + "_W" + wheel.str()
00099       + "_Sec" + sector.str()
00100       + "_St" + station.str() 
00101       + ".ps";
00102 
00103     TPostScript psFile(fileName.c_str(),111);
00104     psFile.Range(20,26);
00105     int counter = 0;
00106     TCanvas c1("c1","",600,780);
00107     c1.Divide(4,4);
00108     psFile.NewPage();
00109     
00110     cout<<"[DTAlbertoBenvenutiTask] filling the file: "<<fileName<<endl;
00111     for(vector<TH1F*>::const_iterator tbHisto = (*Histo).second.begin();
00112         tbHisto != (*Histo).second.end();
00113         tbHisto++) {
00114       counter++;
00115       c1.cd(counter);
00116       (*tbHisto)->Draw();      
00117        if(counter%16 == 0 && counter>=16){
00118          c1.Update();
00119          psFile.NewPage();
00120          c1.Clear();
00121          c1.Divide(4,4);
00122          counter=0;
00123        }
00124     } // loop over TB histos (divided per chamber)
00125 
00126   } //loop over the chambers
00127 
00128 }
00129 
00130 
00131 void DTAlbertoBenvenutiTask::beginJob(){
00132 
00133   if(debug)
00134     cout<<"[DTAlbertoBenvenutiTask]: BeginJob"<<endl;
00135 
00136   nevents = 0;
00137 
00138 }
00139 
00140 
00141 
00142 void DTAlbertoBenvenutiTask::beginRun(const edm::Run&, const edm::EventSetup& context) {
00143 
00144   // Get the geometry
00145   context.get<MuonGeometryRecord>().get(muonGeom);
00146 
00147   // tTrig 
00148   if (parameters.getUntrackedParameter<bool>("readDB", true)) 
00149     context.get<DTTtrigRcd>().get(tTrigMap);
00150 
00151   // t0s 
00152   if (parameters.getParameter<bool>("performPerWireT0Calibration")) 
00153     context.get<DTT0Rcd>().get(t0Map);
00154 
00155 }
00156 
00157 
00158 void DTAlbertoBenvenutiTask::bookHistos(const DTWireId dtWire) {
00159 
00160   if (debug) cout<<"[DTAlbertoBenvenutiTask]: booking"<<endl;
00161 
00162   stringstream wheel; wheel << dtWire.layerId().superlayerId().chamberId().wheel();     
00163   stringstream station; station << dtWire.layerId().superlayerId().chamberId().station();       
00164   stringstream sector; sector << dtWire.layerId().superlayerId().chamberId().sector();  
00165   
00166   // Loop over all the chambers
00167   vector<DTChamber*>::const_iterator ch_it = muonGeom->chambers().begin();
00168   vector<DTChamber*>::const_iterator ch_end = muonGeom->chambers().end();
00169   // Loop over the SLs
00170   for (; ch_it != ch_end; ++ch_it) {
00171     DTChamberId ch = (*ch_it)->id();
00172     if(ch == dtWire.layerId().superlayerId().chamberId()){
00173       vector<const DTSuperLayer*>::const_iterator sl_it = (*ch_it)->superLayers().begin(); 
00174       vector<const DTSuperLayer*>::const_iterator sl_end = (*ch_it)->superLayers().end();
00175       // Loop over the SLs
00176       for(; sl_it != sl_end; ++sl_it) {
00177         DTSuperLayerId sl = (*sl_it)->id();
00178         stringstream superLayer; superLayer << sl.superlayer();
00179         vector<const DTLayer*>::const_iterator l_it = (*sl_it)->layers().begin(); 
00180         vector<const DTLayer*>::const_iterator l_end = (*sl_it)->layers().end();
00181         // Loop over the Ls
00182         for(; l_it != l_end; ++l_it) {
00183           DTLayerId layerId = (*l_it)->id();
00184           stringstream layer; layer << layerId.layer();
00185           const int firstWire = muonGeom->layer(layerId)->specificTopology().firstChannel();
00186           const int lastWire = muonGeom->layer(layerId)->specificTopology().lastChannel();
00187           // Loop overt the wires
00188           for(int wr=firstWire; wr -lastWire <= 0; wr++) {
00189 
00190             stringstream wire; wire << wr;
00191             DTWireId wrId(layerId, wr);
00192 
00193             string histoTag = "TimeBox";
00194             string histoName = histoTag
00195               + "_W" + wheel.str() 
00196               + "_St" + station.str() 
00197               + "_Sec" + sector.str() 
00198               + "_SL" + superLayer.str()
00199               + "_L" + layer.str()
00200               + "_wire" + wire.str();
00201 
00202             if (debug) cout<<"[DTAlbertoBenvenutiTask]: histoName "<<histoName<<endl;
00203 
00204             if ( parameters.getUntrackedParameter<bool>("readDB", false) ) 
00205               // ttrig and rms are TDC counts
00206               tTrigMap->get(dtWire.layerId().superlayerId(), tTrig, tTrigRMS, kFactor,
00207                             DTTimeUnits::counts); 
00208             else tTrig = parameters.getParameter<int>("defaultTtrig");
00209   
00210             string histoTitle = histoName + " (TDC Counts)";
00211             int timeBoxGranularity = parameters.getUntrackedParameter<int>("timeBoxGranularity",4);
00212   
00213             if (!parameters.getUntrackedParameter<bool>("readDB", true)) {
00214               int maxTDCCounts = 6400 * parameters.getUntrackedParameter<int>("tdcRescale", 1);
00215               TH1F *TB = new TH1F(histoName.c_str(),histoTitle.c_str(), maxTDCCounts/timeBoxGranularity, 0, maxTDCCounts);
00216               TBMap[wrId] = TB;
00217             }    
00218             else {
00219               TH1F *TB = new TH1F(histoName.c_str(),histoTitle.c_str(), 2*tMax/timeBoxGranularity, tTrig-tMax, tTrig+2*tMax);
00220               TBMap[wrId] = TB;
00221             }
00222 
00223           } // loopover  wires
00224         } // loop over Ls
00225       } // loop over SLs
00226     } // if is the right chamber to book
00227   } // loop over chambers
00228 }
00229 
00230 
00231 void DTAlbertoBenvenutiTask::analyze(const edm::Event& e, const edm::EventSetup& c){
00232   
00233   nevents++;
00234   if (nevents%1000 == 0 && debug) {}
00235   
00236   edm::Handle<DTDigiCollection> dtdigis;
00237   e.getByLabel("dtunpacker", dtdigis);
00238 
00239   if ( !parameters.getUntrackedParameter<bool>("localrun", true) ) e.getByType(ltcdigis);
00240 
00241   bool checkNoisyChannels = parameters.getUntrackedParameter<bool>("checkNoisyChannels",false);
00242   ESHandle<DTStatusFlag> statusMap;
00243   if(checkNoisyChannels) {
00244     // Get the map of noisy channels
00245     c.get<DTStatusFlagRcd>().get(statusMap);
00246   }
00247 
00248   int tdcCount = 0;
00249   DTDigiCollection::DigiRangeIterator dtLayerId_It;
00250   for (dtLayerId_It=dtdigis->begin(); dtLayerId_It!=dtdigis->end(); ++dtLayerId_It){
00251     for (DTDigiCollection::const_iterator digiIt = ((*dtLayerId_It).second).first;
00252          digiIt!=((*dtLayerId_It).second).second; ++digiIt){
00253       tdcCount++;
00254     }
00255   }
00256 
00257   bool isSyncNoisy = false;
00258   if (tdcCount > maxTDCHits) isSyncNoisy = true;
00259   
00260   for (dtLayerId_It=dtdigis->begin(); dtLayerId_It!=dtdigis->end(); ++dtLayerId_It){
00261     for (DTDigiCollection::const_iterator digiIt = ((*dtLayerId_It).second).first;
00262          digiIt!=((*dtLayerId_It).second).second; ++digiIt){
00263       
00264       bool isNoisy = false;
00265       bool isFEMasked = false;
00266       bool isTDCMasked = false;
00267       bool isTrigMask = false;
00268       bool isDead = false;
00269       bool isNohv = false;
00270       const DTWireId wireId(((*dtLayerId_It).first), (*digiIt).wire());
00271       if(checkNoisyChannels) {
00272         statusMap->cellStatus(wireId, isNoisy, isFEMasked, isTDCMasked, isTrigMask, isDead, isNohv);
00273       }      
00274  
00275       // for clearness..
00276 //      const  DTSuperLayerId dtSLId = ((*dtLayerId_It).first).superlayerId();
00277 //       uint32_t indexSL = dtSLId.rawId();
00278 //      const  DTChamberId dtChId = dtSLId.chamberId(); 
00279 //       uint32_t indexCh = dtChId.rawId();
00280 //       int layer_number=((*dtLayerId_It).first).layer();
00281 //       int superlayer_number=dtSLId.superlayer();
00282 //      const  DTLayerId dtLId = (*dtLayerId_It).first;
00283 //       uint32_t indexL = dtLId.rawId();
00284       
00285       float t0; float t0RMS;
00286       int tdcTime = (*digiIt).countsTDC();
00287 
00288       if (parameters.getParameter<bool>("performPerWireT0Calibration")) {
00289         const DTWireId dtWireId(((*dtLayerId_It).first), (*digiIt).wire());
00290         t0Map->get(dtWireId, t0, t0RMS, DTTimeUnits::counts) ;
00291         tdcTime += int(round(t0));
00292       }
00293        
00294       // avoid to fill TB with noise
00295       if ((!isNoisy ) && (!isSyncNoisy)) {
00296         // TimeBoxes per wire
00297         if (TBMap.find(wireId) == TBMap.end()){
00298           bookHistos(wireId);
00299           TBMap[wireId]->Fill(tdcTime);
00300         }
00301         else
00302           TBMap[wireId]->Fill(tdcTime);
00303       }
00304     }
00305   }
00306 }
00307 
00308