CMS 3D CMS Logo

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