CMS 3D CMS Logo

SiPixel2DTemplateDBObjectReader.cc
Go to the documentation of this file.
1 #include <iomanip>
2 #include <fstream>
3 #include <iostream>
4 #include <cmath>
5 #include <memory>
7 
13 
16 
20 
26 
28 public:
31 
32 private:
33  void beginJob() override;
34  void analyze(const edm::Event&, const edm::EventSetup&) override;
35  void endJob() override;
36 
39 
48 };
49 
51  : the2DTemplateCalibrationLocation(iConfig.getParameter<std::string>("siPixel2DTemplateCalibrationLocation")),
52  theDetailed2DTemplateDBErrorOutput(iConfig.getParameter<bool>("wantDetailed2DTemplateDBErrorOutput")),
53  theFull2DTemplateDBOutput(iConfig.getParameter<bool>("wantFull2DTemplateDBOutput")),
54  testGlobalTag(iConfig.getParameter<bool>("TestGlobalTag")),
55  hasTriggeredWatcher(false),
56  magneticFieldToken_(esConsumes()),
57  the2DTemplateESProdToken_(esConsumes()),
58  the2DTemplateToken_(esConsumes()) {}
59 
61 
63 
65  //To test with the ESProducer
67  if (testGlobalTag) {
68  // Get magnetic field
69  GlobalPoint center(0.0, 0.0, 0.0);
71  float theMagField = magfield.product()->inTesla(center).mag();
72 
73  edm::LogPrint("SiPixel2DTemplateDBObjectReader") << "\nTesting global tag at magnetic field = " << theMagField;
74  if (SiPix2DTemplDBObjWatcher_.check(iSetup)) {
75  edm::LogPrint("SiPixel2DTemplateDBObjectESProducerRcd") << "With record SiPixel2DTemplateDBObjectESProducerRcd";
76  dbobject = *&iSetup.getData(the2DTemplateESProdToken_);
77  hasTriggeredWatcher = true;
78  }
79  } else {
80  edm::LogPrint("SiPixel2DTemplateDBObjectReader") << "\nLoading from file " << std::endl;
81  if (SiPix2DTemplDBObjWatcher_.check(iSetup)) {
82  edm::LogPrint("SiPixelTemplateDBObjectReader") << "With record SiPixel2DTemplateDBObjectRcd";
83  dbobject = *&iSetup.getData(the2DTemplateToken_);
84  hasTriggeredWatcher = true;
85  }
86  }
87 
88  if (hasTriggeredWatcher) {
89  std::vector<short> tempMapId;
90 
92  edm::LogPrint("SiPixel2DTemplateDBObjectReader") << "Map info" << std::endl;
93  std::map<unsigned int, short> templMap = dbobject.getTemplateIDs();
94  for (std::map<unsigned int, short>::const_iterator it = templMap.begin(); it != templMap.end(); ++it) {
95  if (tempMapId.empty())
96  tempMapId.push_back(it->second);
97  for (unsigned int i = 0; i < tempMapId.size(); ++i) {
98  if (tempMapId[i] == it->second)
99  continue;
100  else if (i == tempMapId.size() - 1) {
101  tempMapId.push_back(it->second);
102  break;
103  }
104  }
105  edm::LogPrint("SiPixel2DTemplateDBObjectReader")
106  << "DetId: " << it->first << " 2DTemplateID: " << it->second << "\n";
107  }
108  }
109 
110  edm::LogPrint("SiPixel2DTemplateDBObjectReader") << "\nMap stores 2DTemplate Id(s): ";
111  for (unsigned int vindex = 0; vindex < tempMapId.size(); ++vindex)
112  edm::LogPrint("SiPixel2DTemplateDBObjectReader") << tempMapId[vindex] << " ";
113  edm::LogPrint("SiPixel2DTemplateDBObjectReader") << std::endl;
114 
115  //local variables
116  const char* tempfile;
117  int numOfTempl = dbobject.numOfTempl();
118  int index = 0;
119  float tempnum = 0, diff = 0;
120  float tol = 1.0E-23;
121  bool error = false, givenErrorMsg = false;
122  ;
123 
124  edm::LogPrint("SiPixel2DTemplateDBObjectReader")
125  << "\nChecking 2DTemplate DB object version " << dbobject.version() << " containing " << numOfTempl
126  << " calibration(s) at " << dbobject.sVector()[index + 22] << "T\n";
127  for (int i = 0; i < numOfTempl; ++i) {
128  //Removes header in db object from diff
129  index += 20;
130 
131  //Tell the person viewing the output what the 2DTemplate ID and version are -- note that version is only valid for >=13
132  edm::LogPrint("SiPixel2DTemplateDBObjectReader")
133  << "Calibration " << i + 1 << " of " << numOfTempl << ", with 2DTemplate ID " << dbobject.sVector()[index]
134  << "\tand Version " << dbobject.sVector()[index + 1] << "\t-------- ";
135 
136  //Opening the text-based 2DTemplate calibration
137  std::ostringstream tout;
138  //tout << the2DTemplateCalibrationLocation.c_str() << "/data/generror_summary_zp"
139  tout << the2DTemplateCalibrationLocation.c_str() << "/data/template2D_IOV5/template_summary2D_zp" << std::setw(4)
140  << std::setfill('0') << std::right << dbobject.sVector()[index] << ".out" << std::ends;
141 
142  edm::FileInPath file(tout.str());
143  tempfile = (file.fullPath()).c_str();
144  std::ifstream in_file(tempfile, std::ios::in);
145 
146  if (in_file.is_open()) {
147  //Removes header in textfile from diff
148  //First read in from the text file -- this will be compared with index = 20
149  in_file >> tempnum;
150 
151  //Read until the end of the current text file
152  while (!in_file.eof()) {
153  //Calculate the difference between the text file and the db object
154  diff = std::abs(tempnum - dbobject.sVector()[index]);
155 
156  //Is there a difference?
157  if (diff > tol) {
158  //We have the if statement to output the message only once
159  if (!givenErrorMsg)
160  edm::LogPrint("SiPixel2DTemplateDBObjectReader") << "does NOT match\n";
161  //If there is an error we want to display a message upon completion
162  error = true;
163  givenErrorMsg = true;
164  //Do we want more detailed output?
166  edm::LogPrint("SiPixel2DTemplateDBObjectReader")
167  << "from file = " << tempnum << "\t from dbobject = " << dbobject.sVector()[index]
168  << "\tdiff = " << diff << "\t db index = " << index << std::endl;
169  }
170  }
171  //Go to the next entries
172  in_file >> tempnum;
173  ++index;
174  }
175  //There were no errors, the two files match.
176  if (!givenErrorMsg)
177  edm::LogPrint("SiPixel2DTemplateDBObjectReader") << "MATCHES\n";
178  } //end current file
179  in_file.close();
180  givenErrorMsg = false;
181  } //end loop over all files
182 
184  edm::LogPrint("SiPixel2DTemplateDBObjectReader")
185  << "\nThe were differences found between the files and the database.\n"
186  << "If you would like more detailed information please set\n"
187  << "wantDetailedOutput = True in the cfg file. If you would like a\n"
188  << "full output of the contents of the database file please set\n"
189  << "wantFullOutput = True. Make sure that you pipe the output to a\n"
190  << "log file. This could take a few minutes.\n\n";
191 
193  edm::LogPrint("SiPixel2DTemplateDBObjectReader") << dbobject << std::endl;
194  }
195 }
196 
198 
199 std::ostream& operator<<(std::ostream& s, const SiPixel2DTemplateDBObject& dbobject) {
201  int index = 0;
203  int txsize[4] = {7, 13, 0, 0};
204  int tysize[4] = {21, 21, 0, 0};
206  int entries[4] = {0};
208  int i, j, k, l, m, n, entry_it;
210  int sizeSetter = 0, generrorVersion = 0;
211 
212  edm::LogPrint("SiPixel2DTemplateDBObjectReader") << "\n\nDBobject version: " << dbobject.version() << std::endl;
213 
214  for (m = 0; m < dbobject.numOfTempl(); ++m) {
215  //To change the size of the output based on which 2DTemplate version we are using"
216  generrorVersion = (int)dbobject.sVector_[index + 21];
217  if (generrorVersion <= 10) {
218  edm::LogPrint("SiPixel2DTemplateDBObjectReader")
219  << "*****WARNING***** This code will not format this 2DTemplate version properly *****WARNING*****\n";
220  sizeSetter = 0;
221  } else if (generrorVersion <= 16)
222  sizeSetter = 1;
223  else
224  edm::LogPrint("SiPixel2DTemplateDBObjectReader")
225  << "*****WARNING***** This code has not been tested at formatting this version *****WARNING*****\n";
226 
227  edm::LogPrint("SiPixel2DTemplateDBObjectReader")
228  << "\n\n*********************************************************************************************"
229  << std::endl;
230  edm::LogPrint("SiPixel2DTemplateDBObjectReader")
231  << "*************** Reading 2DTemplate ID " << dbobject.sVector_[index + 20] << "\t(" << m + 1
232  << "/" << dbobject.numOfTempl_ << ") ***************" << std::endl;
233  edm::LogPrint("SiPixel2DTemplateDBObjectReader")
234  << "*********************************************************************************************\n\n"
235  << std::endl;
236 
237  //Header Title
239  for (n = 0; n < 20; ++n) {
240  temp.f = dbobject.sVector_[index];
241  s << temp.c[0] << temp.c[1] << temp.c[2] << temp.c[3];
242  ++index;
243  }
244 
245  entries[0] = (int)dbobject.sVector_[index + 3]; // Y
246  entries[1] = (int)(dbobject.sVector_[index + 4] * dbobject.sVector_[index + 5]); // X
247 
248  //Header
249  s << dbobject.sVector_[index] << "\t" << dbobject.sVector_[index + 1] << "\t" << dbobject.sVector_[index + 2]
250  << "\t" << dbobject.sVector_[index + 3] << "\t" << dbobject.sVector_[index + 4] << "\t"
251  << dbobject.sVector_[index + 5] << "\t" << dbobject.sVector_[index + 6] << "\t" << dbobject.sVector_[index + 7]
252  << "\t" << dbobject.sVector_[index + 8] << "\t" << dbobject.sVector_[index + 9] << "\t"
253  << dbobject.sVector_[index + 10] << "\t" << dbobject.sVector_[index + 11] << "\t" << dbobject.sVector_[index + 12]
254  << "\t" << dbobject.sVector_[index + 13] << "\t" << dbobject.sVector_[index + 14] << "\t"
255  << dbobject.sVector_[index + 15] << "\t" << dbobject.sVector_[index + 16] << std::endl;
256  index += 17;
257 
258  //Loop over By,Bx,Fy,Fx
259  for (entry_it = 0; entry_it < 4; ++entry_it) {
260  //Run,costrk,qavg,...,clslenx
261  for (i = 0; i < entries[entry_it]; ++i) {
262  s << dbobject.sVector_[index] << "\t" << dbobject.sVector_[index + 1] << "\t" << dbobject.sVector_[index + 2]
263  << "\t" << dbobject.sVector_[index + 3] << "\n"
264  << dbobject.sVector_[index + 4] << "\t" << dbobject.sVector_[index + 5] << "\t"
265  << dbobject.sVector_[index + 6] << "\t" << dbobject.sVector_[index + 7] << "\t"
266  << dbobject.sVector_[index + 8] << "\t" << dbobject.sVector_[index + 9] << "\t"
267  << dbobject.sVector_[index + 10] << "\t" << dbobject.sVector_[index + 11] << "\n"
268  << dbobject.sVector_[index + 12] << "\t" << dbobject.sVector_[index + 13] << "\t"
269  << dbobject.sVector_[index + 14] << "\t" << dbobject.sVector_[index + 15] << "\t"
270  << dbobject.sVector_[index + 16] << "\t" << dbobject.sVector_[index + 17] << "\t"
271  << dbobject.sVector_[index + 18] << std::endl;
272  index += 19;
273  //YPar
274  for (j = 0; j < 2; ++j) {
275  for (k = 0; k < 5; ++k) {
276  s << dbobject.sVector_[index] << "\t";
277  ++index;
278  }
279  s << std::endl;
280  }
281  //YTemp
282  for (j = 0; j < 9; ++j) {
283  for (k = 0; k < tysize[sizeSetter]; ++k) {
284  s << dbobject.sVector_[index] << "\t";
285  ++index;
286  }
287  s << std::endl;
288  }
289  //XPar
290  for (j = 0; j < 2; ++j) {
291  for (k = 0; k < 5; ++k) {
292  s << dbobject.sVector_[index] << "\t";
293  ++index;
294  }
295  s << std::endl;
296  }
297  //XTemp
298  for (j = 0; j < 9; ++j) {
299  for (k = 0; k < txsize[sizeSetter]; ++k) {
300  s << dbobject.sVector_[index] << "\t";
301  ++index;
302  }
303  s << std::endl;
304  }
305  //Y average reco params
306  for (j = 0; j < 4; ++j) {
307  for (k = 0; k < 4; ++k) {
308  s << dbobject.sVector_[index] << "\t";
309  ++index;
310  }
311  s << std::endl;
312  }
313  //Yflpar
314  for (j = 0; j < 4; ++j) {
315  for (k = 0; k < 6; ++k) {
316  s << dbobject.sVector_[index] << "\t";
317  ++index;
318  }
319  s << std::endl;
320  }
321  //X average reco params
322  for (j = 0; j < 4; ++j) {
323  for (k = 0; k < 4; ++k) {
324  s << dbobject.sVector_[index] << "\t";
325  ++index;
326  }
327  s << std::endl;
328  }
329  //Xflpar
330  for (j = 0; j < 4; ++j) {
331  for (k = 0; k < 6; ++k) {
332  s << dbobject.sVector_[index] << "\t";
333  ++index;
334  }
335  s << std::endl;
336  }
337  //Chi2X,Y
338  for (j = 0; j < 4; ++j) {
339  for (k = 0; k < 2; ++k) {
340  for (l = 0; l < 2; ++l) {
341  s << dbobject.sVector_[index] << "\t";
342  ++index;
343  }
344  }
345  s << std::endl;
346  }
347  //Y average Chi2 params
348  for (j = 0; j < 4; ++j) {
349  for (k = 0; k < 4; ++k) {
350  s << dbobject.sVector_[index] << "\t";
351  ++index;
352  }
353  s << std::endl;
354  }
355  //X average Chi2 params
356  for (j = 0; j < 4; ++j) {
357  for (k = 0; k < 4; ++k) {
358  s << dbobject.sVector_[index] << "\t";
359  ++index;
360  }
361  s << std::endl;
362  }
363  //Y average reco params for CPE Generic
364  for (j = 0; j < 4; ++j) {
365  for (k = 0; k < 4; ++k) {
366  s << dbobject.sVector_[index] << "\t";
367  ++index;
368  }
369  s << std::endl;
370  }
371  //X average reco params for CPE Generic
372  for (j = 0; j < 4; ++j) {
373  for (k = 0; k < 4; ++k) {
374  s << dbobject.sVector_[index] << "\t";
375  ++index;
376  }
377  s << std::endl;
378  }
379  //SpareX,Y
380  for (j = 0; j < 20; ++j) {
381  s << dbobject.sVector_[index] << "\t";
382  ++index;
383  if (j == 9 || j == 19)
384  s << std::endl;
385  }
386  }
387  }
388  }
389  return s;
390 }
391 
std::vector< float > const & sVector() const
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
edm::ESGetToken< MagneticField, IdealMagneticFieldRecord > magneticFieldToken_
std::ostream & operator<<(std::ostream &s, const SiPixel2DTemplateDBObject &dbobject)
void analyze(const edm::Event &, const edm::EventSetup &) override
int iEvent
Definition: GenABIO.cc:224
edm::ESGetToken< SiPixel2DTemplateDBObject, SiPixel2DTemplateDBObjectRcd > the2DTemplateToken_
edm::ESWatcher< SiPixel2DTemplateDBObjectESProducerRcd > SiPix2DTemplDBObjectWatcher_
edm::ESGetToken< SiPixel2DTemplateDBObject, SiPixel2DTemplateDBObjectESProducerRcd > the2DTemplateESProdToken_
SiPixel2DTemplateDBObjectReader(const edm::ParameterSet &)
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
Log< level::Warning, true > LogPrint
ESHandle< T > getHandle(const ESGetToken< T, R > &iToken) const
Definition: EventSetup.h:130
edm::ESWatcher< SiPixel2DTemplateDBObjectRcd > SiPix2DTemplDBObjWatcher_
bool check(const edm::EventSetup &iSetup)
Definition: ESWatcher.h:57
const std::map< unsigned int, short > & getTemplateIDs() const