CMS 3D CMS Logo

DTNoiseCalibration.cc
Go to the documentation of this file.
1 /*
2  * See header file for a description of this class.
3  *
4  * \author G. Mila - INFN Torino
5  * A. Vilela Pereira - INFN Torino
6  */
7 
8 #include "DTNoiseCalibration.h"
9 
10 // Framework
16 
17 // Geometry
22 
23 // Digis
29 
30 // Database
33 
35 
36 #include "TH2F.h"
37 #include "TFile.h"
38 
39 using namespace edm;
40 using namespace std;
41 
43  : digiLabel_(pset.getParameter<InputTag>("digiLabel")),
44  useTimeWindow_(pset.getParameter<bool>("useTimeWindow")),
45  triggerWidth_(pset.getParameter<double>("triggerWidth")),
46  timeWindowOffset_(pset.getParameter<int>("timeWindowOffset")),
47  maximumNoiseRate_(pset.getParameter<double>("maximumNoiseRate")),
48  useAbsoluteRate_(pset.getParameter<bool>("useAbsoluteRate")),
49  readDB_(true),
50  defaultTtrig_(0),
51  dbLabel_(pset.getUntrackedParameter<string>("dbLabel", "")),
52  //fastAnalysis_( pset.getParameter<bool>("fastAnalysis", true) ),
53  wireIdWithHisto_(std::vector<DTWireId>()),
54  lumiMax_(3000) {
55  // Get the debug parameter for verbose output
56  //debug = ps.getUntrackedParameter<bool>("debug");
57  /*// The analysis type
58  // The wheel & sector interested for the time-dependent analysis
59  wh = ps.getUntrackedParameter<int>("wheel", 0);
60  sect = ps.getUntrackedParameter<int>("sector", 6);*/
61 
62  if (pset.exists("defaultTtrig")) {
63  readDB_ = false;
64  defaultTtrig_ = pset.getParameter<int>("defaultTtrig");
65  }
66 
67  if (pset.exists("cellsWithHisto")) {
68  vector<string> cellsWithHisto = pset.getParameter<vector<string> >("cellsWithHisto");
69  for (vector<string>::const_iterator cell = cellsWithHisto.begin(); cell != cellsWithHisto.end(); ++cell) {
70  //FIXME: Use regex to check whether format is right
71  if ((!cell->empty()) && (*cell) != "None") {
72  stringstream linestr;
73  int wheel, station, sector, sl, layer, wire;
74  linestr << (*cell);
75  linestr >> wheel >> station >> sector >> sl >> layer >> wire;
76  wireIdWithHisto_.push_back(DTWireId(wheel, station, sector, sl, layer, wire));
77  }
78  }
79  }
80 
81  // The root file which will contain the histos
82  string rootFileName = pset.getUntrackedParameter<string>("rootFileName", "noise.root");
83  rootFile_ = new TFile(rootFileName.c_str(), "RECREATE");
84  rootFile_->cd();
85 }
86 
88  LogVerbatim("Calibration") << "[DTNoiseCalibration]: Begin job";
89 
90  nevents_ = 0;
91 
92  TH1::SetDefaultSumw2(true);
93  int numBin = (triggerWidth_ * 32 / 25) / 50;
94  hTDCTriggerWidth_ = new TH1F("TDC_Time_Distribution", "TDC_Time_Distribution", numBin, 0, triggerWidth_ * 32 / 25);
95 }
96 
98  // Get the DT Geometry
99  setup.get<MuonGeometryRecord>().get(dtGeom_);
100 
101  // tTrig
102  if (readDB_)
103  setup.get<DTTtrigRcd>().get(dbLabel_, tTrigMap_);
104 
105  runBeginTime_ = time_t(run.beginTime().value() >> 32);
106  runEndTime_ = time_t(run.endTime().value() >> 32);
107  /*
108  nevents = 0;
109  counter = 0;
110 
111  // TDC time distribution
112  int numBin = (triggerWidth_*(32/25))/50;
113  hTDCTriggerWidth = new TH1F("TDC_Time_Distribution", "TDC_Time_Distribution", numBin, 0, triggerWidth_*(32/25));*/
114 }
115 
117  ++nevents_;
118 
119  // Get the digis from the event
120  Handle<DTDigiCollection> dtdigis;
121  event.getByLabel(digiLabel_, dtdigis);
122 
123  /*TH1F *hOccupancyHisto;
124  TH2F *hEvtPerWireH;
125  string Histo2Name;*/
126 
127  //RunNumber_t runNumber = event.id().run();
128  time_t eventTime = time_t(event.time().value() >> 32);
129  unsigned int lumiSection = event.luminosityBlock();
130 
131  // Loop over digis
133  for (dtLayerId_It = dtdigis->begin(); dtLayerId_It != dtdigis->end(); ++dtLayerId_It) {
134  // Define time window
135  float upperLimit = 0.;
136  if (useTimeWindow_) {
137  if (readDB_) {
138  float tTrig, tTrigRMS, kFactor;
139  DTSuperLayerId slId = ((*dtLayerId_It).first).superlayerId();
140  int status = tTrigMap_->get(slId, tTrig, tTrigRMS, kFactor, DTTimeUnits::counts);
141  if (status != 0)
142  throw cms::Exception("DTNoiseCalibration") << "Could not find tTrig entry in DB for" << slId << endl;
143  upperLimit = tTrig - timeWindowOffset_;
144  } else {
145  upperLimit = defaultTtrig_ - timeWindowOffset_;
146  }
147  }
148 
149  double triggerWidth_s = 0.;
150  if (useTimeWindow_)
151  triggerWidth_s = ((upperLimit * 25) / 32) / 1e9;
152  else
153  triggerWidth_s = double(triggerWidth_ / 1e9);
154  LogTrace("Calibration") << ((*dtLayerId_It).first).superlayerId() << " Trigger width (s): " << triggerWidth_s;
155 
156  for (DTDigiCollection::const_iterator digiIt = ((*dtLayerId_It).second).first;
157  digiIt != ((*dtLayerId_It).second).second;
158  ++digiIt) {
159  //Check the TDC trigger width
160  int tdcTime = (*digiIt).countsTDC();
161  if (!useTimeWindow_) {
162  if ((((float)tdcTime * 25) / 32) > triggerWidth_) {
163  LogError("Calibration") << "Digi has a TDC time (ns) higher than the pre-defined TDC trigger width: "
164  << ((float)tdcTime * 25) / 32;
165  continue;
166  }
167  }
168 
169  hTDCTriggerWidth_->Fill(tdcTime);
170 
171  if (useTimeWindow_ && tdcTime > upperLimit)
172  continue;
173 
174  /*LogTrace("Calibration") << "TDC time (ns): " << ((float)tdcTime*25)/32
175  <<" --- trigger width (ns): " << ((float)upperLimit*25)/32;*/
176 
177  const DTLayerId dtLId = (*dtLayerId_It).first;
178  const DTTopology& dtTopo = dtGeom_->layer(dtLId)->specificTopology();
179  const int firstWire = dtTopo.firstChannel();
180  const int lastWire = dtTopo.lastChannel();
181  //const int nWires = dtTopo.channels();
182  const int nWires = lastWire - firstWire + 1;
183 
184  // Book the occupancy histos
185  if (theHistoOccupancyMap_.find(dtLId) == theHistoOccupancyMap_.end()) {
186  string histoName = "DigiOccupancy_" + getLayerName(dtLId);
187  rootFile_->cd();
188  TH1F* hOccupancyHisto = new TH1F(histoName.c_str(), histoName.c_str(), nWires, firstWire, lastWire + 1);
189  LogTrace("Calibration") << " Created occupancy Histo: " << hOccupancyHisto->GetName();
190  theHistoOccupancyMap_[dtLId] = hOccupancyHisto;
191  }
192  theHistoOccupancyMap_[dtLId]->Fill((*digiIt).wire(), 1. / triggerWidth_s);
193 
194  // Histos vs lumi section
195  const DTChamberId dtChId = dtLId.chamberId();
196  if (chamberOccupancyVsLumiMap_.find(dtChId) == chamberOccupancyVsLumiMap_.end()) {
197  string histoName = "OccupancyVsLumi_" + getChamberName(dtChId);
198  rootFile_->cd();
199  TH1F* hOccupancyVsLumiHisto = new TH1F(histoName.c_str(), histoName.c_str(), lumiMax_, 0, lumiMax_);
200  LogTrace("Calibration") << " Created occupancy histo: " << hOccupancyVsLumiHisto->GetName();
201  chamberOccupancyVsLumiMap_[dtChId] = hOccupancyVsLumiHisto;
202  }
203  chamberOccupancyVsLumiMap_[dtChId]->Fill(lumiSection, 1. / triggerWidth_s);
204 
205  const DTWireId wireId(dtLId, (*digiIt).wire());
206  if (find(wireIdWithHisto_.begin(), wireIdWithHisto_.end(), wireId) != wireIdWithHisto_.end()) {
207  if (theHistoOccupancyVsLumiMap_.find(wireId) == theHistoOccupancyVsLumiMap_.end()) {
208  string histoName = "OccupancyVsLumi_" + getChannelName(wireId);
209  rootFile_->cd();
210  TH1F* hOccupancyVsLumiHisto = new TH1F(histoName.c_str(), histoName.c_str(), lumiMax_, 0, lumiMax_);
211  LogTrace("Calibration") << " Created occupancy histo: " << hOccupancyVsLumiHisto->GetName();
212  theHistoOccupancyVsLumiMap_[wireId] = hOccupancyVsLumiHisto;
213  }
214  theHistoOccupancyVsLumiMap_[wireId]->Fill(lumiSection, 1. / triggerWidth_s);
215  }
216 
217  // Histos vs time
218  if (chamberOccupancyVsTimeMap_.find(dtChId) == chamberOccupancyVsTimeMap_.end()) {
219  string histoName = "OccupancyVsTime_" + getChamberName(dtChId);
220  float secPerBin = 20.0;
221  unsigned int nBins = ((unsigned int)(runEndTime_ - runBeginTime_)) / secPerBin;
222  rootFile_->cd();
223  TH1F* hOccupancyVsTimeHisto = new TH1F(
224  histoName.c_str(), histoName.c_str(), nBins, (unsigned int)runBeginTime_, (unsigned int)runEndTime_);
225  for (int k = 0; k < hOccupancyVsTimeHisto->GetNbinsX(); ++k) {
226  if (k % 10 == 0) {
227  unsigned int binLowEdge = hOccupancyVsTimeHisto->GetBinLowEdge(k + 1);
228  time_t timeValue = time_t(binLowEdge);
229  hOccupancyVsTimeHisto->GetXaxis()->SetBinLabel((k + 1), ctime(&timeValue));
230  }
231  }
232  size_t lastBin = hOccupancyVsTimeHisto->GetNbinsX();
233  unsigned int binUpperEdge =
234  hOccupancyVsTimeHisto->GetBinLowEdge(lastBin) + hOccupancyVsTimeHisto->GetBinWidth(lastBin);
235  time_t timeValue = time_t(binUpperEdge);
236  hOccupancyVsTimeHisto->GetXaxis()->SetBinLabel((lastBin), ctime(&timeValue));
237 
238  LogTrace("Calibration") << " Created occupancy histo: " << hOccupancyVsTimeHisto->GetName();
239  chamberOccupancyVsTimeMap_[dtChId] = hOccupancyVsTimeHisto;
240  }
241  chamberOccupancyVsTimeMap_[dtChId]->Fill((unsigned int)eventTime, 1. / triggerWidth_s);
242 
243  /*// Book the digi event plot every 1000 events if the analysis is not "fast" and if is the correct sector
244  if(!fastAnalysis &&
245  dtLId.superlayerId().chamberId().wheel()==wh &&
246  dtLId.superlayerId().chamberId().sector()==sect) {
247  if(theHistoEvtPerWireMap.find(dtLId) == theHistoEvtPerWireMap.end() ||
248  (theHistoEvtPerWireMap.find(dtLId) != theHistoEvtPerWireMap.end() &&
249  skippedPlot[dtLId] != counter)){
250  skippedPlot[dtLId] = counter;
251  Histo2Name = "DigiPerWirePerEvent_" + getLayerName(dtLId) + "_" + std::to_string(counter);
252  theFile->cd();
253  hEvtPerWireH = new TH2F(Histo2Name.c_str(), Histo2Name.c_str(), 1000,0.5,1000.5,nWires, firstWire, lastWire+1);
254  if(hEvtPerWireH){
255  if(debug)
256  cout << " New Histo with the number of digi per evt per wire: " << hEvtPerWireH->GetName() << endl;
257  theHistoEvtPerWireMap[dtLId]=hEvtPerWireH;
258  }
259  }
260  }*/
261  }
262  }
263 
264  /*//Fill the plot of the number of digi per event per wire
265  std::map<int,int > DigiPerWirePerEvent;
266  // LOOP OVER ALL THE CHAMBERS
267  vector<DTChamber*>::const_iterator ch_it = dtGeom->chambers().begin();
268  vector<DTChamber*>::const_iterator ch_end = dtGeom->chambers().end();
269  for (; ch_it != ch_end; ++ch_it) {
270  DTChamberId ch = (*ch_it)->id();
271  vector<const DTSuperLayer*>::const_iterator sl_it = (*ch_it)->superLayers().begin();
272  vector<const DTSuperLayer*>::const_iterator sl_end = (*ch_it)->superLayers().end();
273  // Loop over the SLs
274  for(; sl_it != sl_end; ++sl_it) {
275  DTSuperLayerId sl = (*sl_it)->id();
276  vector<const DTLayer*>::const_iterator l_it = (*sl_it)->layers().begin();
277  vector<const DTLayer*>::const_iterator l_end = (*sl_it)->layers().end();
278  // Loop over the Ls
279  for(; l_it != l_end; ++l_it) {
280  DTLayerId layerId = (*l_it)->id();
281 
282  // Get the number of wires
283  const DTTopology& dtTopo = dtGeom->layer(layerId)->specificTopology();
284  const int firstWire = dtTopo.firstChannel();
285  const int lastWire = dtTopo.lastChannel();
286 
287  if (theHistoEvtPerWireMap.find(layerId) != theHistoEvtPerWireMap.end() &&
288  skippedPlot[layerId] == counter) {
289 
290  for (int wire=firstWire; wire<=lastWire; wire++) {
291  DigiPerWirePerEvent[wire]= 0;
292  }
293  // loop over all the digis of the event
294  DTDigiCollection::Range layerDigi= dtdigis->get(layerId);
295  for (DTDigiCollection::const_iterator digi = layerDigi.first;
296  digi!=layerDigi.second;
297  ++digi){
298  if((cosmicRun && (*digi).countsTDC()<upperLimit) || (!cosmicRun))
299  DigiPerWirePerEvent[(*digi).wire()]+=1;
300  }
301  // fill the digi event histo
302  for (int wire=firstWire; wire<=lastWire; wire++) {
303  theFile->cd();
304  int histoEvents = nevents - (counter*1000);
305  theHistoEvtPerWireMap[layerId]->Fill(histoEvents,wire,DigiPerWirePerEvent[wire]);
306  }
307  }
308  } //Loop Ls
309  } //Loop SLs
310  } //Loop chambers
311 
312 
313  if(nevents % 1000 == 0) {
314  counter++;
315  // save the digis event plot on file
316  for(map<DTLayerId, TH2F* >::const_iterator lHisto = theHistoEvtPerWireMap.begin();
317  lHisto != theHistoEvtPerWireMap.end();
318  lHisto++) {
319  theFile->cd();
320  if((*lHisto).second)
321  (*lHisto).second->Write();
322  }
323  theHistoEvtPerWireMap.clear();
324  }*/
325 }
326 
328  //LogVerbatim("Calibration") << "[DTNoiseCalibration] endjob called!";
329  LogVerbatim("Calibration") << "[DTNoiseCalibration] Total number of events analyzed: " << nevents_;
330 
331  // Save the TDC digi plot
332  rootFile_->cd();
333  hTDCTriggerWidth_->Write();
334 
335  double normalization = 1. / double(nevents_);
336 
337  for (map<DTWireId, TH1F*>::const_iterator wHisto = theHistoOccupancyVsLumiMap_.begin();
338  wHisto != theHistoOccupancyVsLumiMap_.end();
339  ++wHisto) {
340  (*wHisto).second->Scale(normalization);
341  (*wHisto).second->Write();
342  }
343 
344  for (map<DTChamberId, TH1F*>::const_iterator chHisto = chamberOccupancyVsLumiMap_.begin();
345  chHisto != chamberOccupancyVsLumiMap_.end();
346  ++chHisto) {
347  (*chHisto).second->Scale(normalization);
348  (*chHisto).second->Write();
349  }
350 
351  for (map<DTChamberId, TH1F*>::const_iterator chHisto = chamberOccupancyVsTimeMap_.begin();
352  chHisto != chamberOccupancyVsTimeMap_.end();
353  ++chHisto) {
354  (*chHisto).second->Scale(normalization);
355  (*chHisto).second->Write();
356  }
357 
358  // Save on file the occupancy histos and write the list of noisy cells
359  DTStatusFlag* statusMap = new DTStatusFlag();
360  for (map<DTLayerId, TH1F*>::const_iterator lHisto = theHistoOccupancyMap_.begin();
361  lHisto != theHistoOccupancyMap_.end();
362  ++lHisto) {
363  /*double triggerWidth_s = 0.;
364  if( useTimeWindow_ ){
365  double triggerWidth_ns = 0.;
366  if( readDB_ ){
367  float tTrig, tTrigRMS, kFactor;
368  DTSuperLayerId slId = ((*lHisto).first).superlayerId();
369  int status = tTrigMap_->get( slId, tTrig, tTrigRMS, kFactor, DTTimeUnits::counts );
370  if(status != 0) throw cms::Exception("DTNoiseCalibration") << "Could not find tTrig entry in DB for" << slId << endl;
371  triggerWidth_ns = tTrig - timeWindowOffset_;
372  } else{
373  triggerWidth_ns = defaultTtrig_ - timeWindowOffset_;
374  }
375  triggerWidth_ns = (triggerWidth_ns*25)/32;
376  triggerWidth_s = triggerWidth_ns/1e9;
377  } else{
378  triggerWidth_s = double(triggerWidth_/1e9);
379  }
380  LogTrace("Calibration") << (*lHisto).second->GetName() << " trigger width (s): " << triggerWidth_s;*/
381 
382  //double normalization = 1./(nevents_*triggerWidth_s);
383  if ((*lHisto).second) {
384  (*lHisto).second->Scale(normalization);
385  rootFile_->cd();
386  (*lHisto).second->Write();
387  const DTTopology& dtTopo = dtGeom_->layer((*lHisto).first)->specificTopology();
388  const int firstWire = dtTopo.firstChannel();
389  const int lastWire = dtTopo.lastChannel();
390  //const int nWires = dtTopo.channels();
391  const int nWires = lastWire - firstWire + 1;
392  // Find average in layer
393  double averageRate = 0.;
394  for (int bin = 1; bin <= (*lHisto).second->GetNbinsX(); ++bin)
395  averageRate += (*lHisto).second->GetBinContent(bin);
396 
397  if (nWires)
398  averageRate /= nWires;
399  LogTrace("Calibration") << " Average rate = " << averageRate;
400 
401  for (int i_wire = firstWire; i_wire <= lastWire; ++i_wire) {
402  // From definition of "noisy cell"
403  int bin = i_wire - firstWire + 1;
404  double channelRate = (*lHisto).second->GetBinContent(bin);
405  double rateOffset = (useAbsoluteRate_) ? 0. : averageRate;
406  if ((channelRate - rateOffset) > maximumNoiseRate_) {
407  DTWireId wireID((*lHisto).first, i_wire);
408  statusMap->setCellNoise(wireID, true);
409  LogVerbatim("Calibration") << ">>> Channel noisy: " << wireID;
410  }
411  }
412  }
413  }
414  LogVerbatim("Calibration") << "Writing noise map object to DB";
415  string record = "DTStatusFlagRcd";
416  DTCalibDBUtils::writeToDB<DTStatusFlag>(record, statusMap);
417 }
418 
420 
422  string channelName = "Wh" + std::to_string(wId.wheel()) + "_St" + std::to_string(wId.station()) + "_Sec" +
423  std::to_string(wId.sector()) + "_SL" + std::to_string(wId.superlayer()) + "_L" +
424  std::to_string(wId.layer()) + "_W" + std::to_string(wId.wire());
425  return channelName;
426 }
427 
428 string DTNoiseCalibration::getLayerName(const DTLayerId& lId) const {
429  const DTSuperLayerId dtSLId = lId.superlayerId();
430  const DTChamberId dtChId = dtSLId.chamberId();
431  string layerName = "W" + std::to_string(dtChId.wheel()) + "_St" + std::to_string(dtChId.station()) + "_Sec" +
432  std::to_string(dtChId.sector()) + "_SL" + std::to_string(dtSLId.superlayer()) + "_L" +
433  std::to_string(lId.layer());
434 
435  return layerName;
436 }
437 
439  const DTChamberId dtChId = dtSLId.chamberId();
440 
441  string superLayerName = "W" + std::to_string(dtChId.wheel()) + "_St" + std::to_string(dtChId.station()) + "_Sec" +
442  std::to_string(dtChId.sector()) + "_SL" + std::to_string(dtSLId.superlayer());
443 
444  return superLayerName;
445 }
446 
447 string DTNoiseCalibration::getChamberName(const DTChamberId& dtChId) const {
448  string chamberName = "W" + std::to_string(dtChId.wheel()) + "_St" + std::to_string(dtChId.station()) + "_Sec" +
449  std::to_string(dtChId.sector());
450 
451  return chamberName;
452 }
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
Timestamp const & endTime() const
Definition: RunBase.h:42
void analyze(const edm::Event &e, const edm::EventSetup &c) override
JetCorrectorParameters::Record record
Definition: classes.h:7
edm::ESHandle< DTGeometry > dtGeom_
DTChamberId chamberId() const
Return the corresponding ChamberId.
edm::InputTag digiLabel_
std::vector< DTWireId > wireIdWithHisto_
std::map< DTLayerId, TH1F * > theHistoOccupancyMap_
bool exists(std::string const &parameterName) const
checks if a parameter exists
void beginJob() override
std::string getChannelName(const DTWireId &) const
void beginRun(const edm::Run &run, const edm::EventSetup &setup) override
int layer() const
Return the layer number.
Definition: DTLayerId.h:42
DTSuperLayerId superlayerId() const
Return the corresponding SuperLayerId.
Definition: DTLayerId.h:45
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
std::map< DTChamberId, TH1F * > chamberOccupancyVsTimeMap_
std::string getChamberName(const DTChamberId &) const
std::map< DTChamberId, TH1F * > chamberOccupancyVsLumiMap_
~DTNoiseCalibration() override
Destructor.
int firstChannel() const
Returns the wire number of the first wire.
Definition: DTTopology.h:79
std::string getLayerName(const DTLayerId &) const
std::map< DTWireId, TH1F * > theHistoOccupancyVsLumiMap_
DTNoiseCalibration(const edm::ParameterSet &ps)
Constructor.
U second(std::pair< T, U > const &p)
int lastChannel() const
Returns the wire number of the last wire.
Definition: DTTopology.h:81
const DTTopology & specificTopology() const
Definition: DTLayer.cc:37
int setCellNoise(int wheelId, int stationId, int sectorId, int slId, int layerId, int cellId, bool flag)
#define LogTrace(id)
std::string channelName(uint32_t, BinningType _btype=kDCC)
int wire() const
Return the wire number.
Definition: DTWireId.h:42
int superlayer() const
Return the superlayer number (deprecated method name)
int get(int wheelId, int stationId, int sectorId, int slId, float &tTrig, float &tTrms, float &kFact, DTTimeUnits::type unit) const
get content
Definition: DTTtrig.cc:59
std::vector< DigiType >::const_iterator const_iterator
Timestamp const & beginTime() const
Definition: RunBase.h:41
HLT enums.
int sector() const
Definition: DTChamberId.h:49
T get() const
Definition: EventSetup.h:73
const DTLayer * layer(const DTLayerId &id) const
Return a layer given its id.
Definition: DTGeometry.cc:96
int station() const
Return the station number.
Definition: DTChamberId.h:42
int wheel() const
Return the wheel number.
Definition: DTChamberId.h:39
void endJob() override
TimeValue_t value() const
Definition: Timestamp.h:45
std::string getSuperLayerName(const DTSuperLayerId &) const
edm::Timestamp time() const
Definition: EventBase.h:60
edm::ESHandle< DTTtrig > tTrigMap_
Definition: event.py:1
Definition: Run.h:45