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