CMS 3D CMS Logo

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