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