00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "CalibMuon/DTCalibration/plugins/DTNoiseCalibration.h"
00011
00012
00013
00014 #include "FWCore/Framework/interface/Event.h"
00015 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00016 #include <FWCore/Framework/interface/EventSetup.h>
00017
00018
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
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
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
00049 debug = ps.getUntrackedParameter<bool>("debug");
00050
00051
00052 digiLabel = ps.getUntrackedParameter<string>("digiLabel");
00053
00054
00055 fastAnalysis = ps.getUntrackedParameter<bool>("fastAnalysis", true);
00056
00057 wh = ps.getUntrackedParameter<int>("wheel", 0);
00058 sect = ps.getUntrackedParameter<int>("sector", 6);
00059
00060
00061 cosmicRun = ps.getUntrackedParameter<bool>("cosmicRun", false);
00062
00063
00064 TriggerWidth = ps.getUntrackedParameter<int>("TriggerWidth");
00065
00066
00067 theOffset = ps.getUntrackedParameter<double>("theOffset",500.);
00068
00069
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
00087 setup.get<MuonGeometryRecord>().get(dtGeom);
00088
00089
00090 if (parameters.getUntrackedParameter<bool>("readDB", true))
00091 setup.get<DTTtrigRcd>().get(dbLabel,tTrigMap);
00092
00093
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
00106 edm::Handle<DTDigiCollection> dtdigis;
00107 e.getByLabel(digiLabel, dtdigis);
00108
00109 TH1F *hOccupancyHisto;
00110 TH2F *hEvtPerWireH;
00111 string Histo2Name;
00112
00113
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
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
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
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
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
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
00201 std::map<int,int > DigiPerWirePerEvent;
00202
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
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
00215 for(; l_it != l_end; ++l_it) {
00216 DTLayerId layerId = (*l_it)->id();
00217
00218
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
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
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 }
00245 }
00246 }
00247
00248
00249 if(nevents % 1000 == 0) {
00250 counter++;
00251
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
00270 theFile->cd();
00271 hTDCTriggerWidth->Write();
00272
00273
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
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
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
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