CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DTOccupancyTest.cc
Go to the documentation of this file.
1 /*
2  * See header file for a description of this class.
3  *
4  * \author G. Cerminara - University and INFN Torino
5  *
6  * threadsafe version (//-) oct/nov 2014 - WATWanAbdullah -ncpp-um-my
7  *
8  */
9 
10 
13 
23 
24 #include "TMath.h"
25 
26 using namespace edm;
27 using namespace std;
28 
29 
30 
31 
33  LogVerbatim ("DTDQM|DTMonitorClient|DTOccupancyTest") << "[DTOccupancyTest]: Constructor";
34 
35  // Get the DQM service
36 
37  lsCounter = 0;
38 
39  writeRootFile = ps.getUntrackedParameter<bool>("writeRootFile", false);
40  if(writeRootFile) {
41  rootFile = new TFile("DTOccupancyTest.root","RECREATE");
42  ntuple = new TNtuple("OccupancyNtuple", "OccupancyNtuple", "ls:wh:st:se:lay1MeanCell:lay1RMS:lay2MeanCell:lay2RMS:lay3MeanCell:lay3RMS:lay4MeanCell:lay4RMS:lay5MeanCell:lay5RMS:lay6MeanCell:lay6RMS:lay7MeanCell:lay7RMS:lay8MeanCell:lay8RMS:lay9MeanCell:lay9RMS:lay10MeanCell:lay10RMS:lay11MeanCell:lay11RMS:lay12MeanCell:lay12RMS");
43  }
44 
45  // switch on the mode for running on test pulses (different top folder)
46  tpMode = ps.getUntrackedParameter<bool>("testPulseMode", false);
47 
48  runOnAllHitsOccupancies = ps.getUntrackedParameter<bool>("runOnAllHitsOccupancies", true);
49  runOnNoiseOccupancies = ps.getUntrackedParameter<bool>("runOnNoiseOccupancies", false);
50  runOnInTimeOccupancies = ps.getUntrackedParameter<bool>("runOnInTimeOccupancies", false);
51  nMinEvts = ps.getUntrackedParameter<int>("nEventsCert", 5000);
52 
53  bookingdone = 0;
54 
55  // Event counter
56  nevents = 0;
57 
58 }
59 
61  LogVerbatim ("DTDQM|DTMonitorClient|DTOccupancyTest") << " destructor called" << endl;
62 }
63 
64 
65 void DTOccupancyTest::beginRun(const edm::Run& run, const EventSetup& context){
66 
67  LogVerbatim ("DTDQM|DTMonitorClient|DTOccupancyTest") << "[DTOccupancyTest]: BeginRun";
68 
69  // Get the geometry
70  context.get<MuonGeometryRecord>().get(muonGeom);
71 
72 }
73 
75  edm::LuminosityBlock const & lumiSeg, edm::EventSetup const & context) {
76  if (!bookingdone) {
77 
78  // Book the summary histos
79  // - one summary per wheel
80  for(int wh = -2; wh <= 2; ++wh) { // loop over wheels
81  bookHistos(ibooker,wh, string("Occupancies"), "OccupancySummary");
82  }
83 
84  ibooker.setCurrentFolder(topFolder());
85  string title = "Occupancy Summary";
86  if(tpMode) {
87  title = "Test Pulse Occupancy Summary";
88  }
89  // - global summary with alarms
90  summaryHisto = ibooker.book2D("OccupancySummary",title.c_str(),12,1,13,5,-2,3);
91  summaryHisto->setAxisTitle("sector",1);
92  summaryHisto->setAxisTitle("wheel",2);
93 
94  // - global summary with percentages
95  glbSummaryHisto = ibooker.book2D("OccupancyGlbSummary",title.c_str(),12,1,13,5,-2,3);
96  glbSummaryHisto->setAxisTitle("sector",1);
97  glbSummaryHisto->setAxisTitle("wheel",2);
98 
99 
100  // assign the name of the input histogram
101  if(runOnAllHitsOccupancies) {
102  nameMonitoredHisto = "OccupancyAllHits_perCh";
103  } else if(runOnNoiseOccupancies) {
104  nameMonitoredHisto = "OccupancyNoise_perCh";
105  } else if(runOnInTimeOccupancies) {
106  nameMonitoredHisto = "OccupancyInTimeHits_perCh";
107  } else { // default is AllHits histo
108  nameMonitoredHisto = "OccupancyAllHits_perCh";
109  }
110 
111  }
112  bookingdone = 1;
113 
114 
115  LogVerbatim ("DTDQM|DTMonitorClient|DTOccupancyTest")
116  <<"[DTOccupancyTest]: End of LS transition, performing the DQM client operation";
117  lsCounter++;
118 
119 
120 
121  // Reset the global summary
122  summaryHisto->Reset();
123  glbSummaryHisto->Reset();
124 
125  // Get all the DT chambers
126  vector<const DTChamber*> chambers = muonGeom->chambers();
127 
128  for(vector<const DTChamber*>::const_iterator chamber = chambers.begin();
129  chamber != chambers.end(); ++chamber) { // Loop over all chambers
130  DTChamberId chId = (*chamber)->id();
131 
132  MonitorElement * chamberOccupancyHisto = igetter.get(getMEName(nameMonitoredHisto, chId));
133 
134  // Run the tests on the plot for the various granularities
135  if(chamberOccupancyHisto != 0) {
136  // Get the 2D histo
137  TH2F* histo = chamberOccupancyHisto->getTH2F();
138  float chamberPercentage = 1.;
139  int result = runOccupancyTest(histo, chId, chamberPercentage);
140  int sector = chId.sector();
141 
142  if(sector == 13) {
143  sector = 4;
144  float resultSect4 = wheelHistos[chId.wheel()]->getBinContent(sector, chId.station());
145  if(resultSect4 > result) {
146  result = (int)resultSect4;
147  }
148  } else if(sector == 14) {
149  sector = 10;
150  float resultSect10 = wheelHistos[chId.wheel()]->getBinContent(sector, chId.station());
151  if(resultSect10 > result) {
152  result = (int)resultSect10;
153  }
154  }
155 
156  // the 2 MB4 of Sect 4 and 10 count as half a chamber
157  if((sector == 4 || sector == 10) && chId.station() == 4)
158  chamberPercentage = chamberPercentage/2.;
159 
160  wheelHistos[chId.wheel()]->setBinContent(sector, chId.station(),result);
161  if(result > summaryHisto->getBinContent(sector, chId.wheel()+3)) {
162  summaryHisto->setBinContent(sector, chId.wheel()+3, result);
163  }
164  glbSummaryHisto->Fill(sector, chId.wheel(), chamberPercentage*1./4.);
165  } else {
166  LogVerbatim ("DTDQM|DTMonitorClient|DTOccupancyTest") << "[DTOccupancyTest] ME: "
167  << getMEName(nameMonitoredHisto, chId) << " not found!" << endl;
168  }
169 
170  }
171 
172  string nEvtsName = "DT/EventInfo/Counters/nProcessedEventsDigi";
173 
174  MonitorElement * meProcEvts = igetter.get(nEvtsName);
175 
176  if (meProcEvts) {
177  int nProcEvts = meProcEvts->getFloatValue();
178  glbSummaryHisto->setEntries(nProcEvts < nMinEvts ? 10. : nProcEvts);
179  summaryHisto->setEntries(nProcEvts < nMinEvts ? 10. : nProcEvts);
180  } else {
181  glbSummaryHisto->setEntries(nMinEvts +1);
182  summaryHisto->setEntries(nMinEvts + 1);
183  LogVerbatim ("DTDQM|DTMonitorClient|DTOccupancyTest") << "[DTOccupancyTest] ME: "
184  << nEvtsName << " not found!" << endl;
185  }
186 
187  // Fill the global summary
188  // Check for entire sectors off and report them on the global summary
189  //FIXME: TODO
190 
191  if(writeRootFile) ntuple->AutoSave("SaveSelf");
192 
193 }
194 
196 
197  LogVerbatim ("DTDQM|DTMonitorClient|DTOccupancyTest") << "[DTOccupancyTest] endjob called!";
198  if(writeRootFile) {
199  rootFile->cd();
200  ntuple->Write();
201  rootFile->Close();
202  }
203 }
204 
205 
206 
207 // --------------------------------------------------
208 
209 void DTOccupancyTest::bookHistos(DQMStore::IBooker & ibooker, const int wheelId,
210  string folder, string histoTag) {
211  // Set the current folder
212  stringstream wheel; wheel << wheelId;
213 
214  ibooker.setCurrentFolder(topFolder());
215 
216  // build the histo name
217  string histoName = histoTag + "_W" + wheel.str();
218 
219 
220  LogVerbatim ("DTDQM|DTMonitorClient|DTOccupancyTest") <<"[DTOccupancyTest]: booking wheel histo:"
221  << histoName
222  << " (tag "
223  << histoTag
224  << ") in: "
225  << topFolder() + "Wheel"+ wheel.str() + "/" + folder << endl;
226 
227  string histoTitle = "Occupancy summary WHEEL: "+wheel.str();
228  if(tpMode) {
229  histoTitle = "TP Occupancy summary WHEEL: "+wheel.str();
230  }
231 
232  wheelHistos[wheelId] = ibooker.book2D(histoName,histoTitle,12,1,13,4,1,5);
233  wheelHistos[wheelId]->setBinLabel(1,"MB1",2);
234  wheelHistos[wheelId]->setBinLabel(2,"MB2",2);
235  wheelHistos[wheelId]->setBinLabel(3,"MB3",2);
236  wheelHistos[wheelId]->setBinLabel(4,"MB4",2);
237  wheelHistos[wheelId]->setAxisTitle("sector",1);
238 }
239 
240 
241 
242 string DTOccupancyTest::getMEName(string histoTag, const DTChamberId& chId) {
243 
244  stringstream wheel; wheel << chId.wheel();
245  stringstream station; station << chId.station();
246  stringstream sector; sector << chId.sector();
247 
248 
249  string folderRoot = topFolder() + "Wheel" + wheel.str() +
250  "/Sector" + sector.str() +
251  "/Station" + station.str() + "/";
252 
253  string folder = "Occupancies/";
254 
255  // build the histo name
256  string histoName = histoTag
257  + "_W" + wheel.str()
258  + "_St" + station.str()
259  + "_Sec" + sector.str();
260 
261  string histoname = folderRoot + histoName;
262 
263  return histoname;
264 }
265 
266 
267 
268 
269 // Run a test on the occupancy of the chamber
270 // Return values:
271 // 0 -> all ok
272 // 1 -> # consecutive dead channels > N
273 // 2 -> dead layer
274 // 3 -> dead SL
275 // 4 -> dead chamber
277  float& chamberPercentage) {
278  int nBinsX = histo->GetNbinsX();
279 
280  // Reset the error flags
281  bool failSL = false;
282  bool failLayer = false;
283  bool failCells = false;
284 
285  // Check that the chamber has digis
286  if(histo->Integral() == 0) {
287  chamberPercentage = 0;
288  return 4;
289  }
290 
291  LogTrace("DTDQM|DTMonitorClient|DTOccupancyTest") << "--- Occupancy test for chamber: " << chId << endl;
292  // set the # of SLs
293  int nSL = 3;
294  if(chId.station() == 4) nSL = 2;
295 
296  //
297  float values[28];
298  if(writeRootFile) {
299  values[0] = lsCounter;
300  values[1] = chId.wheel();
301  values[2] = chId.station();
302  values[3] = chId.sector();
303  }
304 
305  // Compute the average occupancy per layer and its RMS
306  // we also look of the layer with the smallest RMS in order to find a reference value
307  // for the cell occupancy
308  double totalChamberOccupp = 0;
309  double squaredLayerOccupSum = 0;
310 
311  map<DTLayerId, pair<double, double> > averageCellOccupAndRMS;
312  map<DTLayerId, double> layerOccupancyMap;
313 
314  int index = 3;
315  for(int slay = 1; slay <= 3; ++slay) { // loop over SLs
316  // Skip layer 2 on MB4
317  if(chId.station() == 4 && slay == 2) {
318  if(writeRootFile) {
319  values[12] = -1;
320  values[13] = -1;
321  values[14] = -1;
322  values[15] = -1;
323  values[16] = -1;
324  values[17] = -1;
325  values[18] = -1;
326  values[19] = -1;
327  }
328  index = 19;
329  continue;
330  }
331  // check the SL occupancy
332  int binYlow = ((slay-1)*4)+1;
333  int binYhigh = binYlow+3;
334  double slInteg = histo->Integral(1,nBinsX,binYlow,binYhigh);
335  if(slInteg == 0) {
336  chamberPercentage = 1.-1./(float)nSL;
337  return 3;
338  }
339 
340  for(int lay = 1; lay <= 4; ++lay) { // loop over layers
341  DTLayerId layID(chId,slay,lay);
342 
343  int binY = binYlow+(lay-1);
344 
345  double layerInteg = histo->Integral(1,nBinsX,binY,binY);
346  squaredLayerOccupSum += layerInteg*layerInteg;
347  totalChamberOccupp+= layerInteg;
348 
349  layerOccupancyMap[layID] = layerInteg;
350 
351  // We look for the distribution of hits within the layer
352  int nWires = muonGeom->layer(layID)->specificTopology().channels();
353  int firstWire = muonGeom->layer(layID)->specificTopology().firstChannel();
354  double layerSquaredSum = 0;
355  // reset the alert bit in the plot (used by render plugins)
356  histo->SetBinContent(nBinsX+1,binY,0.);
357 
358  for(int cell = firstWire; cell != (nWires+firstWire); ++cell) { // loop over cells
359  double cellOccup = histo->GetBinContent(cell,binY);
360  layerSquaredSum+=cellOccup*cellOccup;
361  }
362 
363 
364 
365  // compute the average cell occpuancy and RMS
366  double averageCellOccup = layerInteg/nWires;
367  double averageSquaredCellOccup = layerSquaredSum/nWires;
368  double rmsCellOccup = sqrt(averageSquaredCellOccup - averageCellOccup*averageCellOccup);
369  averageCellOccupAndRMS[layID] = make_pair(averageCellOccup, rmsCellOccup);
370  LogTrace("DTDQM|DTMonitorClient|DTOccupancyTest") << " " << layID
371  << " average cell occ.: " << averageCellOccup
372  << " RMS: " << rmsCellOccup << endl;
373  if(writeRootFile) {
374  index++;
375  values[index] = averageCellOccup;
376  index++;
377  values[index] = rmsCellOccup;
378  }
379  }
380  }
381 
382 
383  if(writeRootFile) ntuple->Fill(values);
384 
385  double minCellRMS = 99999999;
386  double referenceCellOccup = -1;
387 
389 
390  // find the cell reference value
391  for(map<DTLayerId, pair<double, double> >::const_iterator layAndValues = averageCellOccupAndRMS.begin();
392  layAndValues != averageCellOccupAndRMS.end(); layAndValues++) {
393  DTLayerId lid = (*layAndValues).first;
394 
395  double rms = (*layAndValues).second.second;
396  double lOcc = layerOccupancyMap[lid]; // FIXME: useless
397  double avCellOcc = (*layAndValues).second.first;
398  LogTrace("DTDQM|DTMonitorClient|DTOccupancyTest") << " " << lid << " tot. occ: " << lOcc
399  << " average cell occ: " << avCellOcc
400  << " RMS: " << rms << endl;
401 
402  if(avCellOcc != 0) {
403  DTOccupancyPoint point(avCellOcc, rms, lid);
404  builder.addPoint(point);
405  } else {
406  if(monitoredLayers.find(lid) == monitoredLayers.end()) monitoredLayers.insert(lid);
407  }
408  }
409 
410  builder.buildClusters();
411  referenceCellOccup = builder.getBestCluster().averageMean();
412  minCellRMS = builder.getBestCluster().averageRMS();
413 
414  double safeFactor = 3.;
415 
416  LogTrace("DTDQM|DTMonitorClient|DTOccupancyTest") << " Reference cell occup.: " << referenceCellOccup
417  << " RMS: " << minCellRMS << endl;
418 
419  int nFailingSLs = 0;
420 
421  // Check the layer occupancy
422  for(int slay = 1; slay <= 3; ++slay) { // loop over SLs
423  // Skip layer 2 on MB4
424  if(chId.station() == 4 && slay == 2) continue;
425 
426  int binYlow = ((slay-1)*4)+1;
427  int nFailingLayers = 0;
428 
429  for(int lay = 1; lay <= 4; ++lay) { // loop over layers
430  DTLayerId layID(chId,slay,lay);
431  int nWires = muonGeom->layer(layID)->specificTopology().channels();
432  int firstWire = muonGeom->layer(layID)->specificTopology().firstChannel();
433  int binY = binYlow+(lay-1);
434 
435  // compute the integral of the layer occupancy
436  double layerInteg = histo->Integral(1,nBinsX,binY,binY);
437 
438  LogTrace("DTDQM|DTMonitorClient|DTOccupancyTest") << " layer: " << layID << " integral: " << layerInteg << endl;
439 
440  // Check if in the list of layers which are monitored
441  bool alreadyMonitored = false;
442  if(monitoredLayers.find(layID) != monitoredLayers.end()) alreadyMonitored = true;
443 
444 
445  if(layerInteg == 0) { // layer is dead (no need to go further
446  LogTrace("DTDQM|DTMonitorClient|DTOccupancyTest") << " fail layer: no entries" << endl;
447  // Add it to the list of of monitored layers
448  if(!alreadyMonitored) monitoredLayers.insert(layID);
449  nFailingLayers++;
450  failLayer = true;
451  histo->SetBinContent(nBinsX+1,binY,-1.);
452  // go to next layer
453  continue;
454  }
455 
456  if(alreadyMonitored || builder.isProblematic(layID)) { // check the layer
457 
458  // Add it to the list of of monitored layers
459  if(monitoredLayers.find(layID) == monitoredLayers.end()) monitoredLayers.insert(layID);
460 
461  int totalDeadCells = 0;
462  int nDeadCellsInARow = 1;
463  int nDeadCellsInARowMax = 0;
464  int nCellsZeroCount = 0;
465  bool previousIsDead = false;
466 
467  int interDeadCells = 0;
468  for(int cell = firstWire; cell != (nWires+firstWire); ++cell) { // loop over cells
469  double cellOccup = histo->GetBinContent(cell,binY);
470  LogTrace("DTDQM|DTMonitorClient|DTOccupancyTest") << " cell occup: " << cellOccup;
471  if(cellOccup == 0 || cellOccup < (referenceCellOccup-safeFactor*sqrt(referenceCellOccup))) {
472  if(cellOccup == 0) nCellsZeroCount++;
473  totalDeadCells++;
474  if(previousIsDead) nDeadCellsInARow++;
475  previousIsDead = true;
476  interDeadCells = 0;
477  LogTrace("DTDQM|DTMonitorClient|DTOccupancyTest") << " below reference" << endl;
478  } else {
479  previousIsDead = false;
480  interDeadCells++;
481 
482  // 3 cells not dead between a group of dead cells don't break the count
483  if(interDeadCells > 3) {
484  if(nDeadCellsInARow > nDeadCellsInARowMax) nDeadCellsInARowMax = nDeadCellsInARow;
485  nDeadCellsInARow = 1;
486  }
487  }
488  }
489  if(nDeadCellsInARow > nDeadCellsInARowMax) nDeadCellsInARowMax = nDeadCellsInARow;
490  LogTrace("DTDQM|DTMonitorClient|DTOccupancyTest") << " # wires: " << nWires
491  << " # cells 0 count: " << nCellsZeroCount
492  << " # dead cells in a row: " << nDeadCellsInARowMax
493  << " total # of dead cells: " << totalDeadCells;
494 
495  if((TMath::Erfc(referenceCellOccup/sqrt(referenceCellOccup)) < 10./(double)nWires &&
496  nDeadCellsInARowMax>= 10.) ||
497  (TMath::Erfc(referenceCellOccup/sqrt(referenceCellOccup)) < 0.5 &&
498  totalDeadCells > nWires/2.)) {
499  LogTrace("DTDQM|DTMonitorClient|DTOccupancyTest") << " -> fail layer!" << endl;
500  nFailingLayers++;
501  failLayer = true;
502  histo->SetBinContent(nBinsX+1,binY,-1.);
503  } else if(referenceCellOccup > 10 &&
504  nCellsZeroCount > nWires/3. &&
505  (double)nCellsZeroCount/(double)nWires >
506  2.*TMath::Erfc(referenceCellOccup/sqrt(referenceCellOccup))) {
507  LogTrace("DTDQM|DTMonitorClient|DTOccupancyTest") << " -> would fail cells!" << endl;
508  LogTrace("DTDQM|DTMonitorClient|DTOccupancyTest") << " # of cells with 0 count: " << nCellsZeroCount
509  << " # wires: " << nWires
510  << " erfc: "
511  << TMath::Erfc(referenceCellOccup/sqrt(referenceCellOccup))
512  << endl;
513  }
514  }
515  }
516  // Check if the whole layer is off
517  if( nFailingLayers == 4) {
518  nFailingSLs++;
519  failSL = true;
520  }
521  }
522 
523  // All the chamber is off
524  if(nFailingSLs == nSL) {
525  chamberPercentage = 0;
526  return 4;
527  } else {
528  chamberPercentage = 1.-(float)nFailingSLs/(float)nSL;
529  }
530 
531  // FIXME add check on cells
532  if(failSL) return 3;
533  if(failLayer) return 2;
534  if(failCells) return 1;
535 
536  return 0;
537 }
538 
539 
541  if(tpMode) return string("DT/10-TestPulses/");
542  return string("DT/01-Digi/");
543 }
DTOccupancyCluster getBestCluster() const
get the cluster correspondig to &quot;normal&quot; cell occupancy.
T getUntrackedParameter(std::string const &, T const &) const
void dqmEndLuminosityBlock(DQMStore::IBooker &, DQMStore::IGetter &, edm::LuminosityBlock const &, edm::EventSetup const &)
DQM Client Diagnostic.
int runOccupancyTest(TH2F *histo, const DTChamberId &chId, float &chamberPercentage)
MonitorElement * get(const std::string &path)
Definition: DQMStore.cc:302
void dqmEndJob(DQMStore::IBooker &, DQMStore::IGetter &)
Endjob.
void setBinLabel(int bin, const std::string &label, int axis=1)
set bin label for x, y or z axis (axis=1, 2, 3 respectively)
virtual ~DTOccupancyTest()
Destructor.
void bookHistos()
Definition: Histogram.h:33
double averageRMS() const
average RMS of the cell occpuancy distributions of the layers in the cluster
DTOccupancyTest(const edm::ParameterSet &ps)
Constructor.
T sqrt(T t)
Definition: SSEVec.h:48
void bookHistos(DQMStore::IBooker &, const int wheelId, std::string folder, std::string histoTag)
book the summary histograms
tuple result
Definition: query.py:137
std::string getMEName(std::string histoTag, const DTChamberId &chId)
Get the ME name.
void beginRun(edm::Run const &run, edm::EventSetup const &context)
BeginRun.
double getFloatValue(void) const
#define LogTrace(id)
double averageMean() const
average cell occupancy of the layers in the cluster
void addPoint(const DTOccupancyPoint &point)
Add an occupancy point for a given layer.
void buildClusters()
build the clusters
void setCurrentFolder(const std::string &fullpath)
Definition: DQMStore.cc:274
MonitorElement * book2D(Args &&...args)
Definition: DQMStore.h:133
const T & get() const
Definition: EventSetup.h:56
int sector() const
Definition: DTChamberId.h:61
bool isProblematic(DTLayerId layerId) const
std::string topFolder() const
int station() const
Return the station number.
Definition: DTChamberId.h:51
TH2F * getTH2F(void) const
static char chambers[264][20]
Definition: ReadPGInfo.cc:243
int wheel() const
Return the wheel number.
Definition: DTChamberId.h:45
void setAxisTitle(const std::string &title, int axis=1)
set x-, y- or z-axis title (axis=1, 2, 3 respectively)
*vegas h *****************************************************used in the default bin number in original ***version of VEGAS is ***a higher bin number might help to derive a more precise ***grade subtle point
Definition: invegas.h:5
Definition: Run.h:43