CMS 3D CMS Logo

SiPixelCondObjOfflineBuilder.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: SiPixelCondObjOfflineBuilder
4 // Class: SiPixelCondObjOfflineBuilder
5 //
13 //
14 // Original Author: Vincenzo CHIOCHIA
15 // Created: Tue Oct 17 17:40:56 CEST 2006
16 // $Id: SiPixelCondObjOfflineBuilder.h,v 1.7 2009/11/20 19:21:29 rougny Exp $
17 //
18 //
19 
20 // system includes
21 #include <string>
22 #include <memory>
23 #include <iostream>
24 
25 // user includes
43 
44 #include "CLHEP/Random/RandGauss.h"
45 #include "CLHEP/Random/RandFlat.h"
46 
47 namespace cms {
49  public:
50  explicit SiPixelCondObjOfflineBuilder(const edm::ParameterSet& iConfig);
51 
52  void beginJob() override;
53  void analyze(const edm::Event&, const edm::EventSetup&) override;
54  bool loadFromFile();
55 
56  private:
57  const bool appendMode_;
59  std::unique_ptr<SiPixelGainCalibrationOffline> SiPixelGainCalibration_;
62 
63  const double meanPed_;
64  const double rmsPed_;
65  const double meanGain_;
66  const double rmsGain_;
67  const double meanPedFPix_;
68  const double rmsPedFPix_;
69  const double meanGainFPix_;
70  const double rmsGainFPix_;
71  const double deadFraction_;
72  const double noisyFraction_;
74  const double secondRocRowPedOffset_;
75  const int numberOfModules_;
76  const bool fromFile_;
78  const bool generateColumns_;
79 
80  // Internal class
81  class CalParameters {
82  public:
83  float p0;
84  float p1;
85  };
86  // Map for storing calibration constants
87  std::map<int, CalParameters, std::less<int> > calmap_;
88  PixelIndices* pIndexConverter_; // Pointer to the index converter
89  };
90 } // namespace cms
91 
92 namespace cms {
94  : appendMode_(iConfig.getUntrackedParameter<bool>("appendMode", true)),
95  pddToken_(esConsumes()),
96  SiPixelGainCalibration_(nullptr),
97  SiPixelGainCalibrationService_(iConfig, consumesCollector()),
98  recordName_(iConfig.getParameter<std::string>("record")),
99  meanPed_(iConfig.getParameter<double>("meanPed")),
100  rmsPed_(iConfig.getParameter<double>("rmsPed")),
101  meanGain_(iConfig.getParameter<double>("meanGain")),
102  rmsGain_(iConfig.getParameter<double>("rmsGain")),
103  meanPedFPix_(iConfig.getUntrackedParameter<double>("meanPedFPix", meanPed_)),
104  rmsPedFPix_(iConfig.getUntrackedParameter<double>("rmsPedFPix", rmsPed_)),
105  meanGainFPix_(iConfig.getUntrackedParameter<double>("meanGainFPix", meanGain_)),
106  rmsGainFPix_(iConfig.getUntrackedParameter<double>("rmsGainFPix", rmsGain_)),
107  deadFraction_(iConfig.getParameter<double>("deadFraction")),
108  noisyFraction_(iConfig.getParameter<double>("noisyFraction")),
109  secondRocRowGainOffset_(iConfig.getParameter<double>("secondRocRowGainOffset")),
110  secondRocRowPedOffset_(iConfig.getParameter<double>("secondRocRowPedOffset")),
111  numberOfModules_(iConfig.getParameter<int>("numberOfModules")),
112  fromFile_(iConfig.getParameter<bool>("fromFile")),
113  fileName_(iConfig.getParameter<std::string>("fileName")),
114  generateColumns_(iConfig.getUntrackedParameter<bool>("generateColumns", true)) {
115  ::putenv((char*)"CORAL_AUTH_USER=me");
116  ::putenv((char*)"CORAL_AUTH_PASSWORD=test");
117  }
118 
120  using namespace edm;
121  unsigned int run = iEvent.id().run();
122  int nmodules = 0;
123  uint32_t nchannels = 0;
124  // int mycol = 415;
125  // int myrow = 159;
126 
127  edm::LogInfo("SiPixelCondObjOfflineBuilder")
128  << "... creating dummy SiPixelGainCalibration Data for Run " << run << "\n " << std::endl;
129  //
130  // Instantiate Gain calibration offset and define pedestal/gain range
131  //
132  // note: the hard-coded range values are also used in random generation. That is why they're defined here
133 
134  float minped = 0.;
135  float maxped = 100.;
136  float mingain = 0.;
137  float maxgain = 10.;
138  SiPixelGainCalibration_ = std::make_unique<SiPixelGainCalibrationOffline>(minped, maxped, mingain, maxgain);
139 
140  const TrackerGeometry* pDD = &iSetup.getData(pddToken_);
141  edm::LogInfo("SiPixelCondObjOfflineBuilder") << " There are " << pDD->dets().size() << " detectors" << std::endl;
142 
143  for (TrackerGeometry::DetContainer::const_iterator it = pDD->dets().begin(); it != pDD->dets().end(); it++) {
144  if (dynamic_cast<PixelGeomDetUnit const*>((*it)) != nullptr) {
145  uint32_t detid = ((*it)->geographicalId()).rawId();
146 
147  // Stop if module limit reached
148  nmodules++;
149  if (nmodules > numberOfModules_)
150  break;
151 
152  const PixelGeomDetUnit* pixDet = dynamic_cast<const PixelGeomDetUnit*>((*it));
153  const PixelTopology& topol = pixDet->specificTopology();
154  // Get the module sizes.
155  int nrows = topol.nrows(); // rows in x
156  int ncols = topol.ncolumns(); // cols in y
157  //edm::LogPrint("SiPixelCondObjOfflineBuilder") << " ---> PIXEL DETID " << detid << " Cols " << ncols << " Rows " << nrows << std::endl;
158 
159  double meanPedWork = meanPed_;
160  double rmsPedWork = rmsPed_;
161  double meanGainWork = meanGain_;
162  double rmsGainWork = rmsGain_;
163  DetId detId(detid);
164  if (detId.subdetId() == 2) { // FPIX
165  meanPedWork = meanPedFPix_;
166  rmsPedWork = rmsPedFPix_;
167  meanGainWork = meanGainFPix_;
168  rmsGainWork = rmsGainFPix_;
169  }
170 
171  PixelIndices pIndexConverter(ncols, nrows);
172 
173  std::vector<char> theSiPixelGainCalibration;
174 
175  // Loop over columns and rows of this DetID
176  for (int i = 0; i < ncols; i++) {
177  float totalGain = 0.0;
178  for (int j = 0; j < nrows; j++) {
179  nchannels++;
180  bool isDead = false;
181  bool isNoisy = false;
182  float ped = 0.0, gain = 0.0;
183 
184  if (fromFile_) {
185  // Use calibration from a file
186  int chipIndex = 0, colROC = 0, rowROC = 0;
187 
188  pIndexConverter.transformToROC(i, j, chipIndex, colROC, rowROC);
189  int chanROC = PixelIndices::pixelToChannelROC(rowROC, colROC); // use ROC coordinates
190  // float pp0=0, pp1=0;
191  std::map<int, CalParameters, std::less<int> >::const_iterator it = calmap_.find(chanROC);
192  CalParameters theCalParameters = (*it).second;
193  ped = theCalParameters.p0;
194  gain = theCalParameters.p1;
195 
196  } else {
197  if (deadFraction_ > 0) {
198  double val = CLHEP::RandFlat::shoot();
199  if (val < deadFraction_) {
200  isDead = true;
201  // edm::LogPrint("SiPixelCondObjOfflineBuilder") << "dead pixel " << detid << " " << i << "," << j << " " << val << std::endl;
202  }
203  }
204  if (deadFraction_ > 0 && !isDead) {
205  double val = CLHEP::RandFlat::shoot();
206  if (val < noisyFraction_) {
207  isNoisy = true;
208  // edm::LogPrint("SiPixelCondObjOfflineBuilder") << "noisy pixel " << detid << " " << i << "," << j << " " << val << std::endl;
209  }
210  }
211 
212  if (rmsPedWork > 0) {
213  ped = CLHEP::RandGauss::shoot(meanPedWork, rmsPedWork);
214  while (ped < minped || ped > maxped)
215  ped = CLHEP::RandGauss::shoot(meanPedWork, rmsPedWork);
216  } else
217  ped = meanPedWork;
218  if (rmsGainWork > 0) {
219  gain = CLHEP::RandGauss::shoot(meanGainWork, rmsGainWork);
220  while (gain < mingain || gain > maxgain)
221  gain = CLHEP::RandGauss::shoot(meanGainWork, rmsGainWork);
222 
223  } else
224  gain = meanGainWork;
225  }
226 
227  // if(i==mycol && j==myrow) {
228  // edm::LogPrint("SiPixelCondObjOfflineBuilder") << " Col "<<i<<" Row "<<j<<" Ped "<<ped<<" Gain "<<gain<<std::endl;
229  // }
230 
231  // gain = 2.8;
232  // ped = 28.2;
233 
234  //if in the second row of rocs (i.e. a 2xN plaquette) add an offset (if desired) for testing
235  if (j >= 80) {
236  ped += secondRocRowPedOffset_;
238 
239  if (gain > maxgain)
240  gain = maxgain;
241  else if (gain < mingain)
242  gain = mingain;
243 
244  if (ped > maxped)
245  ped = maxped;
246  else if (ped < minped)
247  ped = minped;
248  }
249 
250  totalGain += gain;
251 
252  if (!isDead && !isNoisy) {
253  SiPixelGainCalibration_->setDataPedestal(ped, theSiPixelGainCalibration);
254  } else if (isDead) // dead pixel
255  // edm::LogPrint("SiPixelCondObjOfflineBuilder") << "filling pixel as dead for detid " << detid <<", col " << i << ", row" << j << std::endl;
256  SiPixelGainCalibration_->setDeadPixel(theSiPixelGainCalibration);
257  else if (isNoisy) // dead pixel
258  // edm::LogPrint("SiPixelCondObjOfflineBuilder") << "filling pixel as dead for detid " << detid <<", col " << i << ", row" << j << std::endl;
259  SiPixelGainCalibration_->setNoisyPixel(theSiPixelGainCalibration);
260  if ((j + 1) % 80 == 0) // fill the column average after ever ROC!
261  {
262  float averageGain = totalGain / static_cast<float>(80);
263 
264  if (generateColumns_) {
265  averageGain = gain;
266  }
267 
268  //edm::LogPrint("SiPixelCondObjOfflineBuilder") << "Filling gain " << averageGain << " for col: " << i << " row: " << j << std::endl;
269  SiPixelGainCalibration_->setDataGain(averageGain, 80, theSiPixelGainCalibration);
270  totalGain = 0;
271  }
272  }
273  }
274 
275  SiPixelGainCalibrationOffline::Range range(theSiPixelGainCalibration.begin(), theSiPixelGainCalibration.end());
276  if (!SiPixelGainCalibration_->put(detid, range, ncols))
277  edm::LogError("SiPixelCondObjOfflineBuilder")
278  << "[SiPixelCondObjOfflineBuilder::analyze] detid already exists" << std::endl;
279  }
280  }
281  edm::LogPrint("SiPixelCondObjOfflineBuilder") << " ---> PIXEL Modules " << nmodules << std::endl;
282  edm::LogPrint("SiPixelCondObjOfflineBuilder") << " ---> PIXEL Channels " << nchannels << std::endl;
283 
284  // // Try to read object
285  // int mynmodules =0;
286  // for(TrackerGeometry::DetContainer::const_iterator it = pDD->dets().begin(); it != pDD->dets().end(); it++){
287  // if( dynamic_cast<PixelGeomDetUnit const*>((*it))!=0){
288  // uint32_t mydetid=((*it)->geographicalId()).rawId();
289  // mynmodules++;
290  // if( mynmodules > numberOfModules_) break;
291  // SiPixelGainCalibration::Range myrange = SiPixelGainCalibration_->getRange(mydetid);
292  // float mypedestal = SiPixelGainCalibration_->getPed (mycol,myrow,myrange,416);
293  // float mygain = SiPixelGainCalibration_->getGain(mycol,myrow,myrange,416);
294  // //edm::LogPrint("SiPixelCondObjOfflineBuilder")<<" PEDESTAL "<< mypedestal<<" GAIN "<<mygain<<std::endl;
295  // }
296  // }
297  // Write into DB
298  edm::LogInfo("SiPixelCondOfflineBuilder") << "writing to DB, record = \"" << recordName_ << "\"";
300  if (!mydbservice.isAvailable()) {
301  edm::LogError("SiPixelCondOfflineBuilder") << " db service unavailable";
302  return;
303  } else {
304  edm::LogInfo("SiPixelCondOfflineBuilder") << " DB service OK";
305  }
306 
307  try {
308  if (mydbservice->isNewTagRequest(recordName_)) {
311  } else {
314  }
315  edm::LogInfo(" --- all OK");
316  } catch (const cond::Exception& er) {
317  edm::LogError("SiPixelCondObjOfflineBuilder") << er.what() << std::endl;
318  } catch (const std::exception& er) {
319  edm::LogError("SiPixelCondObjOfflineBuilder") << "caught std::exception " << er.what() << std::endl;
320  } catch (...) {
321  edm::LogError("SiPixelCondObjOfflineBuilder") << "Funny error" << std::endl;
322  }
323  }
324 
325  // ------------ method called once each job just before starting event loop ------------
327  if (fromFile_) {
328  if (loadFromFile()) {
329  edm::LogInfo("SiPixelCondObjOfflineBuilder") << " Calibration loaded: Map size " << calmap_.size() << " max "
330  << calmap_.max_size() << " " << calmap_.empty() << std::endl;
331  }
332  }
333  }
334 
336  float par0, par1; //,par2,par3;
337  int colid, rowid; //rocid
339 
340  std::ifstream in_file; // data file pointer
341  in_file.open(fileName_.c_str(), std::ios::in); // in C++
342  if (in_file.bad()) {
343  edm::LogError("SiPixelCondObjOfflineBuilder") << "Input file not found" << std::endl;
344  }
345  if (in_file.eof() != 0) {
346  edm::LogError("SiPixelCondObjOfflineBuilder") << in_file.eof() << " " << in_file.gcount() << " " << in_file.fail()
347  << " " << in_file.good() << " end of file " << std::endl;
348  return false;
349  }
350  //Load file header
351  char line[500];
352  for (int i = 0; i < 3; i++) {
353  in_file.getline(line, 500, '\n');
354  edm::LogInfo("SiPixelCondObjOfflineBuilder") << line << std::endl;
355  }
356  //Loading calibration constants from file, loop on pixels
357  for (int i = 0; i < (52 * 80); i++) {
358  in_file >> par0 >> par1 >> name >> colid >> rowid;
359 
360  edm::LogPrint("SiPixelCondObjOfflineBuilder")
361  << " Col " << colid << " Row " << rowid << " P0 " << par0 << " P1 " << par1 << std::endl;
362 
363  CalParameters onePix;
364  onePix.p0 = par0;
365  onePix.p1 = par1;
366 
367  // Convert ROC pixel index to channel
368  int chan = PixelIndices::pixelToChannelROC(rowid, colid);
369  calmap_.insert(std::pair<int, CalParameters>(chan, onePix));
370  }
371 
372  bool flag;
373  if (calmap_.size() == 4160) {
374  flag = true;
375  } else {
376  flag = false;
377  }
378  return flag;
379  }
380 
381 } // namespace cms
382 
383 using namespace cms;
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
Base exception class for the object to relational access.
Definition: Exception.h:11
virtual int ncolumns() const =0
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
virtual int nrows() const =0
Log< level::Error, false > LogError
std::pair< ContainerIterator, ContainerIterator > Range
std::unique_ptr< SiPixelGainCalibrationOffline > SiPixelGainCalibration_
void createOneIOV(const T &payload, cond::Time_t firstSinceTime, const std::string &recordName)
SiPixelGainCalibrationOfflineService SiPixelGainCalibrationService_
void appendOneIOV(const T &payload, cond::Time_t sinceTime, const std::string &recordName)
int transformToROC(const int col, const int row, int &rocId, int &colROC, int &rowROC) const
Definition: PixelIndices.h:149
void analyze(const edm::Event &, const edm::EventSetup &) override
int iEvent
Definition: GenABIO.cc:224
bool isNewTagRequest(const std::string &recordName)
std::map< int, CalParameters, std::less< int > > calmap_
SiPixelCondObjOfflineBuilder(const edm::ParameterSet &iConfig)
static int pixelToChannelROC(const int rowROC, const int colROC)
Definition: PixelIndices.h:233
constexpr int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:48
bool getData(T &iHolder) const
Definition: EventSetup.h:122
Log< level::Warning, true > LogPrint
Namespace of DDCMS conversion namespace.
Log< level::Info, false > LogInfo
Definition: DetId.h:17
const edm::ESGetToken< TrackerGeometry, TrackerDigiGeometryRecord > pddToken_
chan
lumi = TPaveText(lowX+0.38, lowY+0.061, lowX+0.45, lowY+0.161, "NDC") lumi.SetBorderSize( 0 ) lumi...
HLT enums.
virtual const PixelTopology & specificTopology() const
Returns a reference to the pixel proxy topology.
bool isAvailable() const
Definition: Service.h:40
char const * what() const noexcept override
Definition: Exception.cc:103