CMS 3D CMS Logo

SiPixelGainCalibrationReadDQMFile.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: SiPixelGainCalibrationReadDQMFile
4 // Class: SiPixelGainCalibrationReadDQMFile
5 //
13 //
14 // Original Author: Freya BLEKMAN
15 // Created: Tue Aug 5 16:22:46 CEST 2008
16 // $Id: SiPixelGainCalibrationReadDQMFile.cc,v 1.7 2010/01/12 11:29:54 rougny Exp $
17 //
18 //
19 
20 // system include files
21 #include <memory>
22 #include <fstream>
23 #include <sys/stat.h>
24 
25 // user include files
44 
45 // ROOT includes
46 #include "TDirectory.h"
47 #include "TFile.h"
48 #include "TH2F.h"
49 #include "TKey.h"
50 #include "TList.h"
51 #include "TString.h"
52 #include "TTree.h"
53 
54 //
55 // class decleration
56 //
57 
58 class SiPixelGainCalibrationReadDQMFile : public edm::one::EDAnalyzer<edm::one::SharedResources> {
59 public:
61 
62 private:
63  void analyze(const edm::Event &, const edm::EventSetup &) final;
64  // functions added by F.B.
65  void fillDatabase(const edm::EventSetup &iSetup, TFile *);
66  std::unique_ptr<TFile> getHistograms();
67  // ----------member data ---------------------------
68  std::map<uint32_t, std::map<std::string, TString> > bookkeeper_;
69  std::map<uint32_t, std::map<double, double> > Meankeeper_;
70  std::map<uint32_t, std::vector<std::map<int, int> > > noisyPixelsKeeper_;
71 
72  const bool appendMode_;
75  std::unique_ptr<TH2F> defaultGain_;
76  std::unique_ptr<TH2F> defaultPed_;
77  std::unique_ptr<TH2F> defaultChi2_;
78  std::unique_ptr<TH2F> defaultFitResult_;
79  std::unique_ptr<TH1F> meanGainHist_;
80  std::unique_ptr<TH1F> meanPedHist_;
82  // keep track of lowest and highest vals for range
83  float gainlow_;
84  float gainhi_;
85  float pedlow_;
86  float pedhi_;
87  const bool usemeanwhenempty_;
89  float gainmax_;
90  float pedmax_;
91  const double badchi2_;
92  const size_t nmaxcols;
93  const size_t nmaxrows;
94 };
95 
96 void SiPixelGainCalibrationReadDQMFile::fillDatabase(const edm::EventSetup &iSetup, TFile *therootfile) {
97  // only create when necessary.
98  // process the minimum and maximum gain & ped values...
99  edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << "now starting db fill!!!" << std::endl;
100 
101  std::map<uint32_t, std::pair<TString, int> > badresults;
102 
104 
105  // bool usedmean=false;
106  // bool isdead=false;
107  TH1F *VCAL_endpoint =
108  fs->make<TH1F>("VCAL_endpoint", "value where response = 255 ( x = (255 - ped)/gain )", 256, 0, 256);
109  TH1F *goodgains = fs->make<TH1F>("goodgains", "gain values", 100, 0, 10);
110  TH1F *goodpeds = fs->make<TH1F>("goodpeds", "pedestal values", 356, -100, 256);
111  TH1F *totgains = fs->make<TH1F>("totgains", "gain values", 200, 0, 10);
112  TH1F *totpeds = fs->make<TH1F>("totpeds", "pedestal values", 356, -100, 256);
113  TTree *tree = new TTree("tree", "tree");
114  int detidfortree, rowfortree, colfortree, useddefaultfortree;
115  float pedfortree, gainfortree, chi2fortree;
116  tree->Branch("detid", &detidfortree, "detid/I");
117  tree->Branch("row", &rowfortree, "row/I");
118  tree->Branch("col", &colfortree, "col/I");
119  tree->Branch("defaultval", &useddefaultfortree, "defaultval/I");
120  tree->Branch("ped", &pedfortree, "ped/F");
121  tree->Branch("gain", &gainfortree, "gain/F");
122  tree->Branch("chi2", &chi2fortree, "chi2/F");
123 
124  // TH1F *gainPerDetid;
125  // TH1F *pedPerDetid;
126 
127  size_t ntimes = 0;
128  edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << "now filling record " << record_ << std::endl;
129  if (record_ != "SiPixelGainCalibrationForHLTRcd" && record_ != "SiPixelGainCalibrationOfflineRcd") {
130  edm::LogPrint("SiPixelGainCalibrationReadDQMFile")
131  << "you passed record " << record_ << ", which I have no idea what to do with!" << std::endl;
132  return;
133  }
134  if (gainlow_ > gainhi_) {
135  float temp = gainhi_;
136  gainhi_ = gainlow_;
137  gainlow_ = temp;
138  }
139  if (pedlow_ > pedhi_) {
140  float temp = pedhi_;
141  pedhi_ = pedlow_;
142  pedlow_ = temp;
143  }
144  if (gainhi_ > gainmax_)
145  gainhi_ = gainmax_;
146  if (pedhi_ > pedmax_)
147  pedhi_ = pedmax_;
148  float badpedval = pedlow_ - 200;
149  float badgainval = gainlow_ - 200;
150  edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << "now filling db: values: pedlow, hi: " << pedlow_ << ", "
151  << pedhi_ << " and gainlow, hi: " << gainlow_ << ", " << gainhi_;
152  float meangain = meanGainHist_->GetMean();
153  float meanped = meanPedHist_->GetMean();
154  // edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << ", and mean gain " << meangain<< ", ped " << meanped ;
155  // edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << std::endl;
156 
157  // and fill the dummy histos:
158 
159  for (size_t icol = 0; icol < nmaxcols; ++icol) {
160  for (size_t irow = 0; irow < nmaxrows; ++irow) {
161  defaultGain_->SetBinContent(icol + 1, irow + 1, meangain);
162  defaultPed_->SetBinContent(icol + 1, irow + 1, meanped);
163  defaultChi2_->SetBinContent(icol + 1, irow + 1, 1.0);
164  defaultFitResult_->SetBinContent(icol + 1, irow + 1, 0);
165  }
166  }
167 
168  SiPixelGainCalibration theGainCalibrationDbInput(pedlow_ * 0.999, pedhi_ * 1.001, gainlow_ * 0.999, gainhi_ * 1.001);
169  SiPixelGainCalibrationForHLT theGainCalibrationDbInputHLT(
170  pedlow_ * 0.999, pedhi_ * 1.001, gainlow_ * 0.999, gainhi_ * 1.001);
171  SiPixelGainCalibrationOffline theGainCalibrationDbInputOffline(
172  pedlow_ * 0.999, pedhi_ * 1.001, gainlow_ * 0.999, gainhi_ * 1.001);
173 
174  uint32_t nchannels = 0;
175  uint32_t nmodules = 0;
176  edm::LogPrint("SiPixelGainCalibrationReadDQMFile")
177  << "now starting loop on detids, there are " << bookkeeper_.size() << " histograms to consider..." << std::endl;
178  uint32_t detid = 0;
179  therootfile->cd();
180  const TrackerGeometry *pDD = &iSetup.getData(pddToken_);
181  edm::LogInfo("SiPixelCondObjOfflineBuilder") << " There are " << pDD->dets().size() << " detectors" << std::endl;
182 
183  int NDetid = 0;
184  for (TrackerGeometry::DetContainer::const_iterator it = pDD->dets().begin(); it != pDD->dets().end(); it++) {
185  detid = 0;
186  if (dynamic_cast<PixelGeomDetUnit const *>((*it)) != nullptr)
187  detid = ((*it)->geographicalId()).rawId();
188  if (detid == 0)
189  continue;
190  NDetid++;
191  //if(NDetid>=2) continue;
192  //if(detid!=344076812) continue;
193  int badDetId = 0;
194  //if(detid==302123296||detid==302126596) badDetId=1;;
195  //if(detid!=302058516) continue;
196  ntimes = 0;
197  useddefaultfortree = 0;
198  edm::LogPrint("SiPixelGainCalibrationReadDQMFile")
199  << "now creating database object for detid " << detid
200  << std::endl; //<< " " << bookkeeper_[detid]["gain_2d"] << " " << bookkeeper_[detid]["ped_2d"] << std::endl; //edm::LogPrint("SiPixelGainCalibrationReadDQMFile")<< " nrows:" << nrows << " ncols: " << ncols << std::endl;
201 
202  // Get the module sizes.
203  TH2F *tempchi2;
204  TH2F *tempfitresult;
205  TH2F *tempgain;
206  TH2F *tempped;
207  TString tempgainstring;
208 
209  if (!badDetId) {
210  TString tempchi2string = bookkeeper_[detid]["chi2prob_2d"];
211  tempchi2 = dynamic_cast<TH2F *>(therootfile->Get(tempchi2string));
212  if (tempchi2 == nullptr || badDetId) {
213  tempchi2 = defaultChi2_.get();
214  useddefaultfortree = 1;
215  }
216  TString tempfitresultstring = bookkeeper_[detid]["fitresult_2d"];
217  tempfitresult = dynamic_cast<TH2F *>(therootfile->Get(tempfitresultstring));
218  if (tempfitresult == nullptr) {
219  tempfitresult = defaultFitResult_.get();
220  useddefaultfortree = 1;
221  }
222  TString tempgainstring = bookkeeper_[detid]["gain_2d"];
223  tempgain = dynamic_cast<TH2F *>(therootfile->Get(tempgainstring));
224  if (tempgain == nullptr) {
225  // edm::LogPrint("SiPixelGainCalibrationReadDQMFile") <<"WARNING, gain histo " << bookkeeper_[detid]["gain_2d"] << " does not exist, using default instead" << std::endl;
226  tempgain = defaultGain_.get();
227  useddefaultfortree = 1;
228  }
229  TString temppedstring = bookkeeper_[detid]["ped_2d"];
230  tempped = dynamic_cast<TH2F *>(therootfile->Get(temppedstring));
231  if (tempped == nullptr) {
232  // edm::LogPrint("SiPixelGainCalibrationReadDQMFile") <<"WARNING, ped histo " << bookkeeper_[detid]["ped_2d"] << " for detid " << detid << " does not exist, using default instead" << std::endl;
233  std::pair<TString, int> tempval(tempgainstring, 0);
234  badresults[detid] = tempval;
235  tempped = defaultPed_.get();
236  useddefaultfortree = 1;
237  }
238  } else {
239  tempchi2 = defaultChi2_.get();
240  tempgain = defaultGain_.get();
241  tempfitresult = defaultFitResult_.get();
242  std::pair<TString, int> tempval(tempgainstring, 0);
243  badresults[detid] = tempval;
244  tempped = defaultPed_.get();
245  useddefaultfortree = 1;
246  }
247 
248  const PixelGeomDetUnit *pixDet = dynamic_cast<const PixelGeomDetUnit *>((*it));
249  const PixelTopology &topol = pixDet->specificTopology();
250  // Get the module sizes.
251  size_t nrows = topol.nrows(); // rows in x
252  size_t ncols = topol.ncolumns(); // cols in y
253 
254  // int nrows=tempgain->GetNbinsY();
255  // int ncols=tempgain->GetNbinsX();
256  // edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << "next histo " << tempgain->GetTitle() << " has nrow,ncol:" << nrows << ","<< ncols << std::endl;
257  size_t nrowsrocsplit = theGainCalibrationDbInputHLT.getNumberOfRowsToAverageOver();
258  if (theGainCalibrationDbInputOffline.getNumberOfRowsToAverageOver() != nrowsrocsplit)
259  throw cms::Exception("GainCalibration Payload configuration error")
260  << "[SiPixelGainCalibrationAnalysis::fillDatabase] ERROR the SiPixelGainCalibrationOffline and "
261  "SiPixelGainCalibrationForHLT database payloads have different settings for the number of rows per roc: "
262  << theGainCalibrationDbInputHLT.getNumberOfRowsToAverageOver() << "(HLT), "
263  << theGainCalibrationDbInputOffline.getNumberOfRowsToAverageOver() << "(offline)";
264  std::vector<char> theSiPixelGainCalibrationPerPixel;
265  std::vector<char> theSiPixelGainCalibrationPerColumn;
266  std::vector<char> theSiPixelGainCalibrationGainPerColPedPerPixel;
267 
268  //Get mean of gain/pedestal of this Detid
269  meangain = meanGainHist_->GetMean();
270  meanped = meanPedHist_->GetMean();
271  int npix = 0;
272  double meanGainForThisModule = 0;
273  double meanPedForThisModule = 0;
274  for (size_t icol = 1; icol <= ncols; icol++) {
275  for (size_t jrow = 1; jrow <= nrows; jrow++) {
276  if (tempfitresult->GetBinContent(icol, jrow) > 0) {
277  npix++;
278  meanGainForThisModule += tempgain->GetBinContent(icol, jrow);
279  meanPedForThisModule += tempped->GetBinContent(icol, jrow);
280  }
281  }
282  }
283  if (npix != 0)
284  meanPedForThisModule /= npix;
285  if (npix != 0)
286  meanGainForThisModule /= npix;
287  if (usemeanwhenempty_) {
288  if (meanGainForThisModule > gainlow_ && meanGainForThisModule < gainhi_ && npix > 100)
289  meangain = meanGainForThisModule;
290  if (meanPedForThisModule > pedlow_ && meanPedForThisModule < pedhi_ && npix > 100)
291  meanped = meanPedForThisModule;
292  }
293 
294  // Loop over columns and rows of this DetID
295  float peds[160];
296  float gains[160];
297  float pedforthiscol[2];
298  float gainforthiscol[2];
299  int nusedrows[2];
300  size_t nemptypixels = 0;
301  //edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << pedlow_<<" "<<pedhi_<<" "<<gainlow_<<" "<<gainhi_<< std::endl;
302  for (size_t icol = 1; icol <= ncols; icol++) {
303  nusedrows[0] = nusedrows[1] = 0;
304  pedforthiscol[0] = pedforthiscol[1] = 0;
305  gainforthiscol[0] = gainforthiscol[1] = 0;
306  // edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << "now lookign at col " << icol << std::endl;
307  for (size_t jrow = 1; jrow <= nrows; jrow++) {
308  size_t iglobalrow = 0;
309  if (jrow > nrowsrocsplit)
310  iglobalrow = 1;
311  peds[jrow] = badpedval;
312  gains[jrow] = badgainval;
313  float ped = tempped->GetBinContent(icol, jrow);
314  float gain = tempgain->GetBinContent(icol, jrow);
315  float chi2 = tempchi2->GetBinContent(icol, jrow);
316  float fitresult = tempfitresult->GetBinContent(icol, jrow);
317 
318  if (ped > pedlow_ && gain > gainlow_ && ped < pedhi_ && gain < gainhi_ && (fitresult > 0)) {
319  ntimes++;
320  // if(ntimes<=10)
321  // edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << detid << " " << jrow << " " << icol << " " << ped << " " << ped << " " << chi2 << " " << fitresult << std::endl;
322  VCAL_endpoint->Fill((255 - ped) / gain);
323  peds[jrow] = ped;
324  gains[jrow] = gain;
325  pedforthiscol[iglobalrow] += ped;
326  gainforthiscol[iglobalrow] += gain;
327  nusedrows[iglobalrow]++;
328  goodpeds->Fill(ped);
329  goodgains->Fill(gain);
330  detidfortree = detid;
331  rowfortree = jrow - 1;
332  colfortree = icol - 1;
333  gainfortree = gain;
334  pedfortree = ped;
335  chi2fortree = chi2;
336  tree->Fill();
337  } else {
338  nemptypixels++;
339  // if(nemptypixels<0.01*nrows*ncols )
340  // edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << "ped,gain="<< ped << ","<< gain << " row, col " << jrow <<","<< icol << ", detid " << detid << std::endl;
341  if (usemeanwhenempty_) {
342  //ntimes++;
343  //if(nemptypixels<=50)
344  //edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << "USING DEFAULT MEAN GAIN & PED (" << meangain << ","<< meanped << ")!, observed values are gain,ped : "<< gain << "," << ped <<", chi2 " << chi2 << ", fitresult "<< fitresult<< std::endl;
345  // edm::LogPrint("SiPixelGainCalibrationReadDQMFile") <<jrow<<" "<<icol<<std::endl;
346  // }
347  peds[jrow] = meanped;
348  gains[jrow] = meangain;
349  std::pair<TString, int> tempval(tempgainstring, 1);
350  badresults[detid] = tempval;
351  } else {
352  //if(ntimes<=100)
353  // edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << tempgainstring << " bad/dead Pixel, observed values are gain,ped : "<< gain << "," << ped <<", chi2 " << chi2 << ", fitresult "<< fitresult<< std::endl;
354  std::pair<TString, int> tempval(tempgainstring, 2);
355  badresults[detid] = tempval;
356  // if everything else fails: set the gain & ped now to dead
357  peds[jrow] = badpedval;
358  gains[jrow] = badgainval;
359  }
360  }
361  //edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << detid << " " << jrow << " " << icol << " " << peds[jrow] << " " << gains[jrow] << " " << chi2 << " " << fitresult << std::endl;
362 
363  totgains->Fill(gains[jrow]);
364  totpeds->Fill(peds[jrow]);
365 
366  } // now collected all info, actually do the filling
367 
368  for (size_t jrow = 1; jrow <= nrows; jrow++) {
369  nchannels++;
370  size_t iglobalrow = 0;
371  if (jrow > nrowsrocsplit)
372  iglobalrow = 1;
373  float ped = peds[jrow];
374  float gain = gains[jrow];
375 
376  if (ped > pedlow_ && gain > gainlow_ && ped < pedhi_ && gain < gainhi_) {
377  theGainCalibrationDbInput.setData(ped, gain, theSiPixelGainCalibrationPerPixel);
378  theGainCalibrationDbInputOffline.setDataPedestal(ped, theSiPixelGainCalibrationGainPerColPedPerPixel);
379  } else {
380  theGainCalibrationDbInput.setDeadPixel(theSiPixelGainCalibrationPerPixel);
381  theGainCalibrationDbInputOffline.setDeadPixel(theSiPixelGainCalibrationGainPerColPedPerPixel);
382  }
383 
384  if (jrow % nrowsrocsplit == 0) {
385  // edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << "now in col " << icol << " " << jrow << " " << iglobalrow << std::endl;
386  if (nusedrows[iglobalrow] > 0) {
387  pedforthiscol[iglobalrow] /= (float)nusedrows[iglobalrow];
388  gainforthiscol[iglobalrow] /= (float)nusedrows[iglobalrow];
389  }
390  if (gainforthiscol[iglobalrow] > gainlow_ && gainforthiscol[iglobalrow] < gainhi_ &&
391  pedforthiscol[iglobalrow] > pedlow_ && pedforthiscol[iglobalrow] < pedhi_) { // good
392  // edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << "setting ped & col aves: " << pedforthiscol[iglobalrow] << " " << gainforthiscol[iglobalrow]<< std::endl;
393  } else {
394  if (usemeanwhenempty_) {
395  // edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << "setting ped & col aves: " << pedforthiscol[iglobalrow] << " " << gainforthiscol[iglobalrow]<< std::endl;
396  pedforthiscol[iglobalrow] = meanped;
397  gainforthiscol[iglobalrow] = meangain;
398  std::pair<TString, int> tempval(tempgainstring, 3);
399  badresults[detid] = tempval;
400  } else { //make dead
401  //if(ntimes<=100)
402  //edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << tempgainstring << "dead Column, observed values are gain,ped : "<< gainforthiscol[iglobalrow] << "," << pedforthiscol[iglobalrow] << std::endl;
403  pedforthiscol[iglobalrow] = badpedval;
404  gainforthiscol[iglobalrow] = badgainval;
405  std::pair<TString, int> tempval(tempgainstring, 4);
406  badresults[detid] = tempval;
407  }
408  }
409 
410  if (gainforthiscol[iglobalrow] > gainlow_ && gainforthiscol[iglobalrow] < gainhi_ &&
411  pedforthiscol[iglobalrow] > pedlow_ && pedforthiscol[iglobalrow] < pedhi_) {
412  theGainCalibrationDbInputOffline.setDataGain(
413  gainforthiscol[iglobalrow], nrowsrocsplit, theSiPixelGainCalibrationGainPerColPedPerPixel);
414  theGainCalibrationDbInputHLT.setData(
415  pedforthiscol[iglobalrow], gainforthiscol[iglobalrow], theSiPixelGainCalibrationPerColumn);
416  } else {
417  // edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << pedforthiscol[iglobalrow] << " " << gainforthiscol[iglobalrow] << std::endl;
418  theGainCalibrationDbInputOffline.setDeadColumn(nrowsrocsplit,
419  theSiPixelGainCalibrationGainPerColPedPerPixel);
420  theGainCalibrationDbInputHLT.setDeadColumn(nrowsrocsplit, theSiPixelGainCalibrationPerColumn);
421  }
422  }
423  }
424  }
425 
426  // edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << "setting range..." << std::endl;
427  SiPixelGainCalibration::Range range(theSiPixelGainCalibrationPerPixel.begin(),
428  theSiPixelGainCalibrationPerPixel.end());
429  SiPixelGainCalibrationForHLT::Range hltrange(theSiPixelGainCalibrationPerColumn.begin(),
430  theSiPixelGainCalibrationPerColumn.end());
431  SiPixelGainCalibrationOffline::Range offlinerange(theSiPixelGainCalibrationGainPerColPedPerPixel.begin(),
432  theSiPixelGainCalibrationGainPerColPedPerPixel.end());
433 
434  // edm::LogPrint("SiPixelGainCalibrationReadDQMFile") <<"putting things in db..." << std::endl;
435  // now start creating the various database objects
436  if (!theGainCalibrationDbInput.put(detid, range, ncols))
437  edm::LogError("SiPixelGainCalibrationAnalysis")
438  << "warning: detid already exists for Offline (gain per col, ped per pixel) calibration database"
439  << std::endl;
440  if (!theGainCalibrationDbInputOffline.put(detid, offlinerange, ncols))
441  edm::LogError("SiPixelGainCalibrationAnalysis")
442  << "warning: detid already exists for Offline (gain per col, ped per pixel) calibration database"
443  << std::endl;
444  if (!theGainCalibrationDbInputHLT.put(detid, hltrange, ncols))
445  edm::LogError("SiPixelGainCalibrationAnalysis")
446  << "warning: detid already exists for HLT (pedestal and gain per column) calibration database" << std::endl;
447 
448  //delete gainPerDetid;
449  }
450  // now printing out summary:
451  size_t nempty = 0;
452  size_t ndefault = 0;
453  size_t ndead = 0;
454  size_t ncoldefault = 0;
455  size_t ncoldead = 0;
456  for (std::map<uint32_t, std::pair<TString, int> >::const_iterator ibad = badresults.begin(); ibad != badresults.end();
457  ++ibad) {
458  uint32_t detid = ibad->first;
459  if (badresults[detid].second == 0) {
460  // edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << " used pixel mean value";
461  nempty++;
462  } else if (badresults[detid].second == 1) {
463  // edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << " used pixel mean value";
464  ndefault++;
465  } else if (badresults[detid].second == 2) {
466  edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << badresults[detid].first;
467  edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << " has one or more dead pixels";
468  edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << std::endl;
469  ndead++;
470  } else if (badresults[detid].second == 3) {
471  // edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << " used column mean value";
472  ncoldefault++;
473  } else if (badresults[detid].second == 4) {
474  edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << badresults[detid].first;
475  edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << " has one or more dead columns";
476  ncoldead++;
477  edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << std::endl;
478  }
479  }
480  edm::LogPrint("SiPixelGainCalibrationReadDQMFile")
481  << nempty << " modules were empty and now have pixels filled with default values." << std::endl;
482  edm::LogPrint("SiPixelGainCalibrationReadDQMFile")
483  << ndefault << " modules have pixels filled with default values." << std::endl;
484  edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << ndead << " modules have pixels flagged as dead." << std::endl;
485  edm::LogPrint("SiPixelGainCalibrationReadDQMFile")
486  << ncoldefault << " modules have columns filled with default values." << std::endl;
487  edm::LogPrint("SiPixelGainCalibrationReadDQMFile")
488  << ncoldead << " modules have columns filled with dead values." << std::endl;
489  edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << " ---> PIXEL Modules " << nmodules << "\n"
490  << " ---> PIXEL Channels " << nchannels << std::endl;
491 
492  edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << " --- writing to DB!" << std::endl;
494  if (!mydbservice.isAvailable()) {
495  edm::LogError("db service unavailable");
496  return;
497  } else {
498  if (record_ == "SiPixelGainCalibrationForHLTRcd") {
499  edm::LogPrint("SiPixelGainCalibrationReadDQMFile")
500  << "now doing SiPixelGainCalibrationForHLTRcd payload..." << std::endl;
501  if (mydbservice->isNewTagRequest(record_)) {
503  theGainCalibrationDbInputHLT, mydbservice->beginOfTime(), "SiPixelGainCalibrationForHLTRcd");
504  } else {
506  theGainCalibrationDbInputHLT, mydbservice->currentTime(), "SiPixelGainCalibrationForHLTRcd");
507  }
508  } else if (record_ == "SiPixelGainCalibrationOfflineRcd") {
509  edm::LogPrint("SiPixelGainCalibrationReadDQMFile")
510  << "now doing SiPixelGainCalibrationOfflineRcd payload..." << std::endl;
511  if (mydbservice->isNewTagRequest(record_)) {
513  theGainCalibrationDbInputOffline, mydbservice->beginOfTime(), "SiPixelGainCalibrationOfflineRcd");
514  } else {
516  theGainCalibrationDbInputOffline, mydbservice->currentTime(), "SiPixelGainCalibrationOfflineRcd");
517  }
518  }
519  edm::LogInfo(" --- all OK");
520  }
521 }
522 
524  : appendMode_(iConfig.getUntrackedParameter<bool>("appendMode", true)),
525  pddToken_(esConsumes()),
526  theGainCalibrationDbInputService_(iConfig, consumesCollector()),
527  record_(iConfig.getUntrackedParameter<std::string>("record", "SiPixelGainCalibrationOfflineRcd")),
528  gainlow_(10.),
529  gainhi_(0.),
530  pedlow_(255.),
531  pedhi_(-256),
532  usemeanwhenempty_(iConfig.getUntrackedParameter<bool>("useMeanWhenEmpty", false)),
533  rootfilestring_(iConfig.getUntrackedParameter<std::string>("inputrootfile", "inputfile.root")),
534  gainmax_(20),
535  pedmax_(200),
536  badchi2_(iConfig.getUntrackedParameter<double>("badChi2Prob", 0.01)),
537  nmaxcols(10 * 52),
538  nmaxrows(160) {
539  usesResource(TFileService::kSharedResource);
540 
541  //now do what ever initialization is needed
542  ::putenv((char *)"CORAL_AUTH_USER=me");
543  ::putenv((char *)"CORAL_AUTH_PASSWORD=test");
544  meanGainHist_ = std::make_unique<TH1F>("meanGainHist", "mean Gain Hist", 500, 0, gainmax_);
545  meanPedHist_ = std::make_unique<TH1F>("meanPedHist", "mean Ped Hist", 512, -200, pedmax_);
546  defaultGain_ = std::make_unique<TH2F>("defaultGain",
547  "default gain, contains mean",
548  nmaxcols,
549  0,
550  nmaxcols,
551  nmaxrows,
552  0,
553  nmaxrows); // using dummy (largest) module size
554  defaultPed_ = std::make_unique<TH2F>("defaultPed",
555  "default pedestal, contains mean",
556  nmaxcols,
557  0,
558  nmaxcols,
559  nmaxrows,
560  0,
561  nmaxrows); // using dummy (largest) module size
562  defaultFitResult_ = std::make_unique<TH2F>("defaultFitResult",
563  "default fitresult, contains '0'",
564  nmaxcols,
565  0,
566  nmaxcols,
567  nmaxrows,
568  0,
569  nmaxrows); // using dummy (largest) module size
570  defaultChi2_ = std::make_unique<TH2F>("defaultChi2",
571  "default chi2 probability, contains '1'",
572  nmaxcols,
573  0,
574  nmaxcols,
575  nmaxrows,
576  0,
577  nmaxrows); // using dummy (largest) module size
578 }
579 
580 //
581 // member functions
582 //
583 
584 // ------------ method called to for each event ------------
586  auto histfile = getHistograms();
587  fillDatabase(iSetup, histfile.get());
588  // empty but should not be called anyway
589 }
590 
592  edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << "now parsing file " << rootfilestring_ << std::endl;
593  auto therootfile = std::make_unique<TFile>(rootfilestring_.c_str());
594  therootfile->cd();
595  TDirectory *dir = therootfile->GetDirectory("DQMData");
596  TList *list = dir->GetListOfKeys();
597 
598  TString comparestring = "Module";
599 
600  std::vector<TString> keylist;
601  std::vector<TString> hist2list;
602  std::vector<TString> dirlist;
603  std::vector<TString> notdonelist;
604  std::vector<int> nsubdirs;
605  int ikey = 0;
606 
607  for (ikey = 0; ikey < list->GetEntries(); ikey++) {
608  TKey *thekey = (TKey *)list->At(ikey);
609  if (thekey == nullptr)
610  continue;
611  TString keyname = thekey->GetName();
612  TString keytype = thekey->GetClassName();
613  // if(keyname=="EventInfo")
614  // continue;
615  // edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << keytype << " " << keyname << std::endl;
616  if (keytype == "TDirectoryFile") {
617  TString dirname = dir->GetPath();
618  dirname += "/";
619  dirname += keyname;
620  // edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << dirname << std::endl;
621  dir = therootfile->GetDirectory(dirname);
622 
623  list = dir->GetListOfKeys();
624  if (dirname.Contains(comparestring)) {
625  dirlist.push_back(dirname);
626  // edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << dirname << std::endl;
627  } else {
628  notdonelist.push_back(dirname);
629  nsubdirs.push_back(-1);
630  }
631  }
632  }
633  size_t nempty = 0;
634  while (nempty != notdonelist.size()) {
635  for (size_t idir = 0; idir < notdonelist.size(); ++idir) {
636  if (nsubdirs[idir] == 0)
637  continue;
638  // edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << "now examining " << notdonelist[idir]<< " " << nsubdirs[idir] << std::endl;
639  dir = therootfile->GetDirectory(notdonelist[idir]);
640  // edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << dir->GetName() << std::endl;
641  list = dir->GetListOfKeys();
642  // edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << list->GetEntries() << std::endl;
643  int ndirectories = 0;
644  for (ikey = 0; ikey < list->GetEntries(); ikey++) {
645  TKey *thekey = (TKey *)list->At(ikey);
646  if (thekey == nullptr)
647  continue;
648  TString keyname = thekey->GetName();
649  TString keytype = thekey->GetClassName();
650  // edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << keyname << " " << keytype << std::endl;
651  if (keytype == "TDirectoryFile") {
652  TString dirname = dir->GetPath();
653  dirname += "/";
654  dirname += keyname;
655  // edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << dirname << std::endl;
656  ndirectories++;
657  if (dirname.Contains(comparestring)) {
658  // edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << dirname << std::endl;
659  dirlist.push_back(dirname);
660  } else {
661  notdonelist.push_back(dirname);
662  nsubdirs.push_back(-1);
663  }
664  }
665  }
666  nsubdirs[idir] = ndirectories;
667  // edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << "now done examining " << notdonelist[idir]<< " " << nsubdirs[idir] << std::endl;
668  }
669  nempty = 0;
670  for (size_t i = 0; i < nsubdirs.size(); i++) {
671  if (nsubdirs[i] != -1)
672  nempty++;
673  }
674  }
675  // edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << "\n done!" << std::endl;
676 
677  for (size_t idir = 0; idir < dirlist.size(); ++idir) {
678  // edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << "good dir " << dirlist[idir] << std::endl;
679 
680  uint32_t detid = 1;
681 
682  dir = therootfile->GetDirectory(dirlist[idir]);
683  list = dir->GetListOfKeys();
684  for (ikey = 0; ikey < list->GetEntries(); ikey++) {
685  TKey *thekey = (TKey *)list->At(ikey);
686  if (thekey == nullptr)
687  continue;
688  TString keyname = thekey->GetName();
689  TString keytype = thekey->GetClassName();
690  // edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << keyname << " " << keytype << std::endl;
691  if (keytype == "TH2F" && (keyname.Contains("Gain2d") || keyname.Contains("Pedestal2d") ||
692  keyname.Contains("GainChi2Prob2d") || keyname.Contains("GainFitResult2d"))) {
693  TString detidstring = keyname;
694  detidstring.Remove(0, detidstring.Sizeof() - 10);
695 
696  detid = atoi(detidstring.Data());
697 
698  if (keyname.Contains("GainChi2Prob2d")) {
699  TString tempstr = dirlist[idir];
700  tempstr += "/";
701  tempstr += keyname;
702  TString replacestring = rootfilestring_;
703  replacestring += ":";
704  tempstr.ReplaceAll(replacestring, "");
705  // edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << tempstr << std::endl;
706  bookkeeper_[detid]["chi2prob_2d"] = tempstr;
707  } else if (keyname.Contains("GainFitResult2d")) {
708  TString tempstr = dirlist[idir];
709  tempstr += "/";
710  tempstr += keyname;
711  TString replacestring = rootfilestring_;
712  replacestring += ":";
713  tempstr.ReplaceAll(replacestring, "");
714  // edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << tempstr << std::endl;
715  bookkeeper_[detid]["fitresult_2d"] = tempstr;
716  } else if (keyname.Contains("Gain2d")) {
717  // edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << dirlist[idir] << std::endl;
718  std::map<std::string, TString> tempmap;
719  TString tempstr = dirlist[idir];
720  tempstr += "/";
721  tempstr += keyname;
722  TString replacestring = rootfilestring_;
723  replacestring += ":";
724  tempstr.ReplaceAll(replacestring, "");
725  // edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << tempstr << std::endl;
726  bookkeeper_[detid]["gain_2d"] = tempstr;
727  //edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << detidstring << " " << keyname << " " << detid << " " << bookkeeper_[detid]["gain_2d"] << std::endl ;
728  }
729  if (keyname.Contains("Pedestal2d")) {
730  // edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << dirlist[idir] << std::endl;
731  std::map<std::string, TString> tempmap;
732 
733  TString tempstr = dirlist[idir];
734  tempstr += "/";
735  tempstr += keyname;
736  // edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << tempstr << std::endl;
737  TString replacestring = rootfilestring_;
738  replacestring += ":";
739  tempstr.ReplaceAll(replacestring, "");
740  bookkeeper_[detid]["ped_2d"] = tempstr;
741 
742  //edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << detidstring << " " << keyname << " " << detid << " " << bookkeeper_[detid]["ped_2d"] << std::endl ;
743  }
744  // edm::LogPrint("SiPixelGainCalibrationReadDQMFile") << keyname << " " << keytype << std::endl;
745  }
746  }
747 
748  TH2F *temphistoped = dynamic_cast<TH2F *>(therootfile->Get(bookkeeper_[detid]["ped_2d"]));
749  TH2F *temphistogain = dynamic_cast<TH2F *>(therootfile->Get(bookkeeper_[detid]["gain_2d"]));
750  TH2F *temphistofitresult = dynamic_cast<TH2F *>(therootfile->Get(bookkeeper_[detid]["gain_2d"]));
751 
752  for (int xbin = 1; xbin <= temphistoped->GetNbinsX(); ++xbin) {
753  for (int ybin = 1; ybin <= temphistoped->GetNbinsY(); ++ybin) {
754  if (temphistofitresult->GetBinContent(xbin, ybin) <= 0)
755  continue;
756  float val = temphistoped->GetBinContent(xbin, ybin);
757  if (val > pedmax_)
758  continue;
759  if (pedlow_ > val)
760  pedlow_ = val;
761  if (pedhi_ < val)
762  pedhi_ = val;
763  meanPedHist_->Fill(val);
764  }
765  }
766 
767  for (int xbin = 1; xbin <= temphistogain->GetNbinsX(); ++xbin) {
768  for (int ybin = 1; ybin <= temphistogain->GetNbinsY(); ++ybin) {
769  if (temphistofitresult->GetBinContent(xbin, ybin) <= 0)
770  continue;
771  float val = temphistogain->GetBinContent(xbin, ybin);
772  if (val <= 0.0001)
773  continue;
774  if (gainlow_ > val)
775  gainlow_ = val;
776  if (gainhi_ < val)
777  gainhi_ = val;
778  meanGainHist_->Fill(val);
779  }
780  }
781 
782  } // end of loop over dirlist
783  return therootfile;
784 }
785 
786 //define this as a plug-in
static const std::string kSharedResource
Definition: TFileService.h:76
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
bool put(const uint32_t &detID, Range input, const int &nCols)
void setDeadPixel(std::vector< char > &vped)
void analyze(const edm::Event &, const edm::EventSetup &) final
void setData(float ped, float gain, std::vector< char > &vped, bool thisColumnIsDead=false, bool thisColumnIsNoisy=false)
virtual int ncolumns() const =0
void setDataPedestal(float pedestal, std::vector< char > &vped, bool thisPixelIsDead=false, bool thisPixelIsNoisy=false)
virtual int nrows() const =0
const DetContainer & dets() const override
Returm a vector of all GeomDet (including all GeomDetUnits)
unsigned int getNumberOfRowsToAverageOver() const
Log< level::Error, false > LogError
std::pair< ContainerIterator, ContainerIterator > Range
void setDataGain(float gain, const int &nRows, std::vector< char > &vped, bool thisColumnIsDead=false, bool thisColumnIsNoisy=false)
std::pair< ContainerIterator, ContainerIterator > Range
void createOneIOV(const T &payload, cond::Time_t firstSinceTime, const std::string &recordName)
U second(std::pair< T, U > const &p)
void appendOneIOV(const T &payload, cond::Time_t sinceTime, const std::string &recordName)
int iEvent
Definition: GenABIO.cc:224
bool isNewTagRequest(const std::string &recordName)
SiPixelGainCalibrationService theGainCalibrationDbInputService_
std::map< uint32_t, std::map< double, double > > Meankeeper_
T get() const
Definition: EventSetup.h:82
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
bool getData(T &iHolder) const
Definition: EventSetup.h:122
Log< level::Warning, true > LogPrint
void setDeadPixel(std::vector< char > &vped)
constexpr float gains[NGAINS]
Definition: EcalConstants.h:11
Log< level::Info, false > LogInfo
void fillDatabase(const edm::EventSetup &iSetup, TFile *)
bool put(const uint32_t &detID, Range input, const int &nCols)
std::map< uint32_t, std::vector< std::map< int, int > > > noisyPixelsKeeper_
std::map< uint32_t, std::map< std::string, TString > > bookkeeper_
void setDeadColumn(const int &nRows, std::vector< char > &vped)
std::pair< ContainerIterator, ContainerIterator > Range
virtual const PixelTopology & specificTopology() const
Returns a reference to the pixel proxy topology.
bool isAvailable() const
Definition: Service.h:40
Definition: tree.py:1
void setData(float ped, float gain, std::vector< char > &vped, bool thisPixelIsDead=false, bool thisPixelIsNoisy=false)
const edm::ESGetToken< TrackerGeometry, TrackerDigiGeometryRecord > pddToken_
SiPixelGainCalibrationReadDQMFile(const edm::ParameterSet &)
bool put(const uint32_t &detID, Range input, const int &nCols)
void setDeadColumn(const int &nRows, std::vector< char > &vped)