CMS 3D CMS Logo

HcalLutManager.cc
Go to the documentation of this file.
1 #include <fstream>
2 #include <sstream>
3 #include <sys/time.h>
4 #include <cstdlib>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <unistd.h>
8 
9 #include <cstdlib> // For srand() and rand()
10 
11 #ifdef HAVE_XDAQ
12 #include <toolbox/string.h>
13 #else
14 #include "CaloOnlineTools/HcalOnlineDb/interface/xdaq_compat.h" // Replaces toolbox::toString
15 #endif
16 
18 
20 
30 
33 using namespace std;
34 using namespace oracle::occi;
35 using namespace hcal;
36 
46 {
47  init();
48 }
49 
50 
51 HcalLutManager::HcalLutManager(std::vector<HcalGenericDetId> & map)
52 {
53  init();
54  _iter . init(map);
55 }
56 
57 
59  const HcalChannelQuality * _cq,
60  uint32_t _status_word_to_mask)
61 {
62  init();
63  emap = _emap;
64  cq = _cq;
65  status_word_to_mask = _status_word_to_mask;
66 }
67 
69  const HcalChannelQuality * _cq,
70  uint32_t _status_word_to_mask)
71 {
72  init();
73  conditions=_conditions;
74  emap = conditions->getHcalMapping();
75  cq = _cq;
76  status_word_to_mask = _status_word_to_mask;
77 }
78 
79 
81 {
82  lut_xml = nullptr;
83  lut_checksums_xml = nullptr;
84  db = nullptr;
85  lmap = nullptr;
86  emap = nullptr;
87  cq = nullptr;
88  conditions = nullptr;
89  status_word_to_mask = 0x0000;
90 }
91 
92 
93 
95 {
96  delete lut_xml;
97  delete lut_checksums_xml;
98  delete db;
99  delete lmap;
100 }
101 
102 
103 int HcalLutManager::initChannelIterator(std::vector<HcalGenericDetId> & map)
104 {
105  _iter . init(map);
106  return _iter.size();
107 }
108 
109 
110 std::string & HcalLutManager::getLutXml( std::vector<unsigned int> & _lut )
111 {
112 
113  if (lut_xml) delete lut_xml;
114 
115  lut_xml = new LutXml();
116 
117  LutXml::Config _config;
118  _config.lut = _lut;
119  lut_xml -> addLut( _config );
120  lut_xml -> addLut( _config );
121  lut_xml -> addLut( _config );
122 
123  //return lut_xml->getString();
124  return lut_xml->getCurrentBrick();
125 
126 }
127 
128 
130 {
131  int result;
132  sscanf(number.c_str(), "%d", &result);
133  return result;
134 }
135 
137 {
139  if ( _det.find("HB") != std::string::npos ) result = HcalBarrel;
140  else if ( _det.find("HE") != std::string::npos ) result = HcalEndcap;
141  else if ( _det.find("HF") != std::string::npos ) result = HcalForward;
142  else if ( _det.find("HO") != std::string::npos ) result = HcalOuter;
143  else result = HcalOther;
144 
145  return result;
146 }
147 
148 
150 {
151  HcalLutManager _manager;
152  HcalLutSet _set = _manager . getLutSetFromFile( _filename );
153  std::stringstream s;
154  s << "===> Test of HcalLutSet HcalLutManager::getLutSetFromFile( std::string _filename )" << std::endl << std::endl;
155  s << _set . label << std::endl;
156  for (unsigned int i = 0; i != _set.subdet.size(); i++) s << _set.subdet[i] << " ";
157  s << std::endl;
158  for (unsigned int i = 0; i != _set.eta_min.size(); i++) s << _set.eta_min[i] << " ";
159  s << std::endl;
160  for (unsigned int i = 0; i != _set.eta_max.size(); i++) s << _set.eta_max[i] << " ";
161  s << std::endl;
162  for (unsigned int i = 0; i != _set.phi_min.size(); i++) s << _set.phi_min[i] << " ";
163  s << std::endl;
164  for (unsigned int i = 0; i != _set.phi_max.size(); i++) s << _set.phi_max[i] << " ";
165  s << std::endl;
166  for (unsigned int i = 0; i != _set.depth_min.size(); i++) s << _set.depth_min[i] << " ";
167  s << std::endl;
168  for (unsigned int i = 0; i != _set.depth_max.size(); i++) s << _set.depth_max[i] << " ";
169  s << std::endl;
170  for (unsigned int j = 0; j != _set.lut[0].size(); j++){
171  for (unsigned int i = 0; i != _set.lut.size(); i++){
172  s << _set.lut[i][j] << " ";
173  }
174  s << "---> " << j << std::endl;
175  }
176  edm::LogInfo("HcalLutManager") << s.str();
177  return 0;
178 }
179 
180 
182 {
183  HcalLutSet _lutset;
184 
185  ifstream infile( _filename . c_str() );
186  std::string buf;
187 
188  if ( infile . is_open() ){
189  edm::LogInfo("HcalLutManager") << "File " << _filename << " is open..." << std::endl
190  << "Reading LUTs and their eta/phi/depth/subdet ranges...";
191 
192  // get label
193  getline( infile, _lutset . label );
194 
195  if ( _type == 1 ){ // for linearization LUTs get subdetectors (default)
196  //get subdetectors
197  getline( infile, buf );
198  _lutset . subdet = HcalQIEManager::splitString( buf );
199  }
200 
201  //get min etas
202  std::vector<std::string> buf_vec;
203  getline( infile, buf );
204  buf_vec = HcalQIEManager::splitString( buf );
205  for (std::vector<std::string>::const_iterator iter = buf_vec.begin(); iter != buf_vec.end(); iter++){
206  _lutset.eta_min.push_back(HcalLutManager::getInt(*iter));
207  }
208 
209  //get max etas
210  getline( infile, buf );
211  buf_vec = HcalQIEManager::splitString( buf );
212  for (std::vector<std::string>::const_iterator iter = buf_vec.begin(); iter != buf_vec.end(); iter++){
213  _lutset.eta_max.push_back(HcalLutManager::getInt(*iter));
214  }
215 
216  //get min phis
217  getline( infile, buf );
218  buf_vec = HcalQIEManager::splitString( buf );
219  for (std::vector<std::string>::const_iterator iter = buf_vec.begin(); iter != buf_vec.end(); iter++){
220  _lutset.phi_min.push_back(HcalLutManager::getInt(*iter));
221  }
222 
223  //get max phis
224  getline( infile, buf );
225  buf_vec = HcalQIEManager::splitString( buf );
226  for (std::vector<std::string>::const_iterator iter = buf_vec.begin(); iter != buf_vec.end(); iter++){
227  _lutset.phi_max.push_back(HcalLutManager::getInt(*iter));
228  }
229 
230  if ( _type == 1 ){ // for linearization LUTs get depth range (default)
231  //get min depths
232  getline( infile, buf );
233  buf_vec = HcalQIEManager::splitString( buf );
234  for (std::vector<std::string>::const_iterator iter = buf_vec.begin(); iter != buf_vec.end(); iter++){
235  _lutset.depth_min.push_back(HcalLutManager::getInt(*iter));
236  }
237 
238  //get max depths
239  getline( infile, buf );
240  buf_vec = HcalQIEManager::splitString( buf );
241  for (std::vector<std::string>::const_iterator iter = buf_vec.begin(); iter != buf_vec.end(); iter++){
242  _lutset.depth_max.push_back(HcalLutManager::getInt(*iter));
243  }
244  }
245 
246  bool first_lut_entry = true;
247  while (getline( infile, buf )) {
248  buf_vec = HcalQIEManager::splitString( buf );
249  for (unsigned int i = 0; i < buf_vec.size(); i++){
250  if (first_lut_entry){
251  std::vector<unsigned int> _l;
252  _lutset.lut.push_back(_l);
253  }
254  _lutset . lut[i] . push_back(HcalLutManager::getInt(buf_vec[i]));
255  }
256  first_lut_entry = false;
257  }
258  }
259 
260  edm::LogInfo("HcalLutManager") << "done.";
261 
262  return _lutset;
263 }
264 
265 
266 
267 std::map<int, boost::shared_ptr<LutXml> > HcalLutManager::getLutXmlFromAsciiMaster( std::string _filename, std::string _tag, int _crate, bool split_by_crate )
268 {
269  edm::LogInfo("HcalLutManager") << "Generating linearization (input) LUTs from ascii master file...";
270  std::map<int, boost::shared_ptr<LutXml> > _xml; // index - crate number
271 
272  LMap _lmap;
273  _lmap . read( "./backup/HCALmapHBEF.txt", "HBEF" );
274  _lmap . read( "./backup/HCALmapHO.txt", "HO" );
275  std::map<int,LMapRow> & _map = _lmap.get_map();
276  edm::LogInfo("HcalLutManager") << "LMap contains " << _map . size() << " channels";
277 
278  // read LUTs and their eta/phi/depth/subdet ranges
279  HcalLutSet _set = getLutSetFromFile( _filename );
280  int lut_set_size = _set.lut.size(); // number of different luts
281 
282  RooGKCounter _counter;
283  //loop over all HCAL channels
284  for( std::map<int,LMapRow>::const_iterator row=_map.begin(); row!=_map.end(); row++ ){
285  LutXml::Config _cfg;
286 
287  // search for the correct LUT for a given channel,
288  // higher LUT numbers have priority in case of overlapping
289  int lut_index=-1;
290  for ( int i=0; i<lut_set_size; i++ ){
291  if ( (row->second.crate == _crate || _crate == -1) && // -1 stands for all crates
292  _set.eta_min[i] <= row->second.side*row->second.eta &&
293  _set.eta_max[i] >= row->second.side*row->second.eta &&
294  _set.phi_min[i] <= row->second.phi &&
295  _set.phi_max[i] >= row->second.phi &&
296  _set.depth_min[i] <= row->second.depth &&
297  _set.depth_max[i] >= row->second.depth &&
298  get_subdetector(_set.subdet[i]) == row->second.det ){
299  lut_index=i;
300  }
301  }
302  if ( lut_index >= 0 ){
303  if ( _xml.count(row->second.crate) == 0 && split_by_crate ){
304  _xml.insert( std::pair<int,boost::shared_ptr<LutXml> >(row->second.crate,boost::shared_ptr<LutXml>(new LutXml())) );
305  }
306  else if ( _xml.count(0) == 0 && !split_by_crate ){
307  _xml.insert( std::pair<int,boost::shared_ptr<LutXml> >(0,boost::shared_ptr<LutXml>(new LutXml())) );
308  }
309  _cfg.ieta = row->second.side*row->second.eta;
310  _cfg.iphi = row->second.phi;
311  _cfg.depth = row->second.depth;
312  _cfg.crate = row->second.crate;
313  _cfg.slot = row->second.htr;
314  if (row->second.fpga . find("top") != std::string::npos) _cfg.topbottom = 1;
315  else if (row->second.fpga . find("bot") != std::string::npos) _cfg.topbottom = 0;
316  else edm::LogWarning("HcalLutManager") << "fpga out of range...";
317  // FIXME: probably fixed. fiber==htr_fi, not rm_fi in LMAP notation.
318  //_cfg.fiber = row->second.rm_fi;
319  _cfg.fiber = row->second.htr_fi;
320  _cfg.fiberchan = row->second.fi_ch;
321  if (_set.lut[lut_index].size() == 128) _cfg.lut_type = 1;
322  else _cfg.lut_type = 2;
323  _cfg.creationtag = _tag;
324  _cfg.creationstamp = get_time_stamp( time(nullptr) );
325  _cfg.targetfirmware = "1.0.0";
326  _cfg.formatrevision = "1"; //???
327  // "original" definition of GENERALIZEDINDEX from Mike Weinberger
328  // int generalizedIndex=id.ietaAbs()+1000*id.depth()+10000*id.iphi()+
329  // ((id.ieta()<0)?(0):(100))+((id.subdet()==HcalForward && id.ietaAbs()==29)?(4*10000):(0));
330  _cfg.generalizedindex =
331  _cfg.iphi*10000 + _cfg.depth*1000 +
332  (row->second.side>0)*100 + row->second.eta +
333  ((row->second.det==HcalForward && row->second.eta==29)?(4*10000):(0));
334  _cfg.lut = _set.lut[lut_index];
335  if (split_by_crate ){
336  _xml[row->second.crate]->addLut( _cfg, lut_checksums_xml );
337  _counter.count();
338  }
339  else{
340  _xml[0]->addLut( _cfg, lut_checksums_xml );
341  _counter.count();
342  }
343  }
344  }
345  edm::LogInfo("HcalLutManager") << "LUTs generated: " << _counter.getCount() << std::endl
346  << "Generating linearization (input) LUTs from ascii master file...DONE" << std::endl;
347  return _xml;
348 }
349 
350 //
351 //_____ get HO from ASCII master here ___________________________________
352 //
353 std::map<int, boost::shared_ptr<LutXml> > HcalLutManager::getLinearizationLutXmlFromAsciiMasterEmap( std::string _filename, std::string _tag, int _crate, bool split_by_crate )
354 {
355  edm::LogInfo("HcalLutManager") << "Generating linearization (input) LUTs from ascii master file...";
356  std::map<int, boost::shared_ptr<LutXml> > _xml; // index - crate number
357 
358  EMap _emap(emap);
359  std::vector<EMap::EMapRow> & _map = _emap.get_map();
360  edm::LogInfo("HcalLutManager") << "EMap contains " << _map . size() << " entries";
361 
362  // read LUTs and their eta/phi/depth/subdet ranges
363  HcalLutSet _set = getLutSetFromFile( _filename );
364  int lut_set_size = _set.lut.size(); // number of different luts
365  edm::LogInfo("HcalLutManager") << " ==> " << lut_set_size << " sets of different LUTs read from the master file";
366 
367  // setup "zero" LUT for channel masking
368  std::vector<unsigned int> zeroLut;
369  for (size_t adc = 0; adc < 128; adc++) zeroLut.push_back(0);
370 
371  RooGKCounter _counter;
372  //loop over all EMap channels
373  for( std::vector<EMap::EMapRow>::const_iterator row=_map.begin(); row!=_map.end(); row++ ){
374  if( (row->subdet.find("HB")!=string::npos ||
375  row->subdet.find("HE")!=string::npos ||
376  row->subdet.find("HO")!=string::npos ||
377  row->subdet.find("HF")!=string::npos ) &&
378  row->subdet.size()==2
379  ){
380  LutXml::Config _cfg;
381 
382  // search for the correct LUT for a given channel,
383  // higher LUT numbers have priority in case of overlapping
384  int lut_index=-1;
385  for ( int i=0; i<lut_set_size; i++ ){
386  if ( (row->crate == _crate || _crate == -1) && // -1 stands for all crates
387  _set.eta_min[i] <= row->ieta &&
388  _set.eta_max[i] >= row->ieta &&
389  _set.phi_min[i] <= row->iphi &&
390  _set.phi_max[i] >= row->iphi &&
391  _set.depth_min[i] <= row->idepth &&
392  _set.depth_max[i] >= row->idepth &&
393  _set.subdet[i].find(row->subdet)!=string::npos ){
394  lut_index=i;
395  }
396  }
397  if ( lut_index >= 0 ){
398  if ( _xml.count(row->crate) == 0 && split_by_crate ){
399  _xml.insert( std::pair<int,boost::shared_ptr<LutXml> >(row->crate,boost::shared_ptr<LutXml>(new LutXml())) );
400  }
401  else if ( _xml.count(0) == 0 && !split_by_crate ){
402  _xml.insert( std::pair<int,boost::shared_ptr<LutXml> >(0,boost::shared_ptr<LutXml>(new LutXml())) );
403  }
404  _cfg.ieta = row->ieta;
405  _cfg.iphi = row->iphi;
406  _cfg.depth = row->idepth;
407  _cfg.crate = row->crate;
408  _cfg.slot = row->slot;
409  if (row->topbottom . find("t") != std::string::npos) _cfg.topbottom = 1;
410  else if (row->topbottom . find("b") != std::string::npos) _cfg.topbottom = 0;
411  else if (row->topbottom . find("u") != std::string::npos) _cfg.topbottom = 2;
412  else edm::LogWarning("HcalLutManager") << "fpga out of range...";
413  _cfg.fiber = row->fiber;
414  _cfg.fiberchan = row->fiberchan;
415  _cfg.lut_type = 1;
416  _cfg.creationtag = _tag;
417  _cfg.creationstamp = get_time_stamp( time(nullptr) );
418  _cfg.targetfirmware = "1.0.0";
419  _cfg.formatrevision = "1"; //???
420  // "original" definition of GENERALIZEDINDEX from Mike Weinberger
421  // int generalizedIndex=id.ietaAbs()+1000*id.depth()+10000*id.iphi()+
422  // ((id.ieta()<0)?(0):(100))+((id.subdet()==HcalForward && id.ietaAbs()==29)?(4*10000):(0));
423  _cfg.generalizedindex =
424  _cfg.iphi*10000 + _cfg.depth*1000 +
425  (row->ieta>0)*100 + abs(row->ieta) +
426  (((row->subdet.find("HF")!=string::npos) && abs(row->ieta)==29)?(4*10000):(0));
427  //
428  // consider channel status here
429  DetId _detId(row->rawId);
430  uint32_t status_word = cq->getValues(_detId)->getValue();
431  if ((status_word & status_word_to_mask) > 0){
432  _cfg.lut = zeroLut;
433  }
434  else{
435  _cfg.lut = _set.lut[lut_index];
436  }
437  if (split_by_crate ){
438  _xml[row->crate]->addLut( _cfg, lut_checksums_xml );
439  _counter.count();
440  }
441  else{
442  _xml[0]->addLut( _cfg, lut_checksums_xml );
443  _counter.count();
444  }
445  }
446  }
447  }
448  edm::LogInfo("HcalLutManager") << "LUTs generated: " << _counter.getCount() << std::endl
449  << "Generating linearization (input) LUTs from ascii master file...DONE" << std::endl;
450  return _xml;
451 }
452 
453 
454 std::map<int, boost::shared_ptr<LutXml> > HcalLutManager::getLinearizationLutXmlFromAsciiMasterEmap_new( std::string _filename, std::string _tag, int _crate, bool split_by_crate )
455 {
456  edm::LogInfo("HcalLutManager") << "Generating linearization (input) LUTs from ascii master file...";
457  std::map<int, boost::shared_ptr<LutXml> > _xml; // index - crate number
458 
459  // read LUTs and their eta/phi/depth/subdet ranges
460  HcalLutSet _set = getLutSetFromFile( _filename );
461  int lut_set_size = _set.lut.size(); // number of different luts
462  edm::LogInfo("HcalLutManager") << " ==> " << lut_set_size << " sets of different LUTs read from the master file";
463 
464  RooGKCounter _counter;
465  //loop over all EMap channels
466  for( _iter.begin(); !_iter.end(); _iter.next() ){
467  HcalSubdetector _subdet = _iter.getHcalSubdetector();
468  if( (_subdet == HcalBarrel ||
469  _subdet == HcalEndcap ||
470  _subdet == HcalForward ||
471  _subdet == HcalOuter )
472  ){
473  int _ieta = _iter.getIeta();
474  int _iphi = _iter.getIphi();
475  int _depth = _iter.getDepth();
476 
477  // FIXME: this is probably wrong, raw ids are different
478  HcalElectronicsId _eId(_iter.getHcalGenericDetId().rawId());
479  int aCrate = _eId . readoutVMECrateId();
480  int aSlot = _eId . htrSlot();
481  int aTopBottom = _eId . htrTopBottom();
482  int aFiber = _eId . fiberIndex();
483  int aFiberChan = _eId . fiberChanId();
484 
485  LutXml::Config _cfg;
486 
487  // search for the correct LUT for a given channel,
488  // higher LUT numbers have priority in case of overlapping
489  int lut_index=-1;
490  for ( int i=0; i<lut_set_size; i++ ){
491  if ( (aCrate == _crate || _crate == -1) && // -1 stands for all crates
492  _set.eta_min[i] <= _ieta &&
493  _set.eta_max[i] >= _ieta &&
494  _set.phi_min[i] <= _iphi &&
495  _set.phi_max[i] >= _iphi &&
496  _set.depth_min[i] <= _depth &&
497  _set.depth_max[i] >= _depth &&
498  _set.subdet[i].find(_ass.getSubdetectorString(_subdet))!=string::npos ){
499  lut_index=i;
500  }
501  }
502  if ( lut_index >= 0 ){
503  if ( _xml.count(aCrate) == 0 && split_by_crate ){
504  _xml.insert( std::pair<int,boost::shared_ptr<LutXml> >(aCrate,boost::shared_ptr<LutXml>(new LutXml())) );
505  }
506  else if ( _xml.count(0) == 0 && !split_by_crate ){
507  _xml.insert( std::pair<int,boost::shared_ptr<LutXml> >(0,boost::shared_ptr<LutXml>(new LutXml())) );
508  }
509  _cfg.ieta = _ieta;
510  _cfg.iphi = _iphi;
511  _cfg.depth = _depth;
512  _cfg.crate = aCrate;
513  _cfg.slot = aSlot;
514  _cfg.topbottom = aTopBottom;
515  _cfg.fiber = aFiber;
516  _cfg.fiberchan = aFiberChan;
517  _cfg.lut_type = 1;
518  _cfg.creationtag = _tag;
519  _cfg.creationstamp = get_time_stamp( time(nullptr) );
520  _cfg.targetfirmware = "1.0.0";
521  _cfg.formatrevision = "1"; //???
522  // "original" definition of GENERALIZEDINDEX from Mike Weinberger
523  // int generalizedIndex=id.ietaAbs()+1000*id.depth()+10000*id.iphi()+
524  // ((id.ieta()<0)?(0):(100))+((id.subdet()==HcalForward && id.ietaAbs()==29)?(4*10000):(0));
525  _cfg.generalizedindex =
526  _cfg.iphi*10000 + _cfg.depth*1000 +
527  (_ieta>0)*100 + abs(_ieta) +
528  (((_subdet==HcalForward) && abs(_ieta)==29)?(4*10000):(0));
529  _cfg.lut = _set.lut[lut_index];
530  if (split_by_crate ){
531  _xml[aCrate]->addLut( _cfg, lut_checksums_xml );
532  _counter.count();
533  }
534  else{
535  _xml[0]->addLut( _cfg, lut_checksums_xml );
536  _counter.count();
537  }
538  }
539  }
540  }
541  edm::LogInfo("HcalLutManager") << "LUTs generated: " << _counter.getCount() << std::endl
542  << "Generating linearization (input) LUTs from ascii master file...DONE" << std::endl;
543  return _xml;
544 }
545 
546 
547 std::map<int, boost::shared_ptr<LutXml> > HcalLutManager::getCompressionLutXmlFromAsciiMaster( std::string _filename, std::string _tag, int _crate, bool split_by_crate )
548 {
549  edm::LogInfo("HcalLutManager") << "Generating compression (output) LUTs from ascii master file...";
550  std::map<int, boost::shared_ptr<LutXml> > _xml; // index - crate number
551 
552  edm::LogInfo("HcalLutManager") << "instantiating CaloTPGTranscoderULUT in order to check the validity of (ieta,iphi)...";
553  CaloTPGTranscoderULUT _coder;
554 
555  //EMap _emap("../../../CondFormats/HcalObjects/data/official_emap_v6.03_080817.txt");
556  //EMap _emap("../../../CondFormats/HcalObjects/data/official_emap_v6.04_080905.txt");
557  EMap _emap(emap);
558  std::vector<EMap::EMapRow> & _map = _emap.get_map();
559  edm::LogInfo("HcalLutManager") << "EMap contains " << _map . size() << " channels";
560 
561  // read LUTs and their eta/phi/depth/subdet ranges
562  HcalLutSet _set = getLutSetFromFile( _filename, 2 );
563  int lut_set_size = _set.lut.size(); // number of different luts
564  edm::LogInfo("HcalLutManager") << " ==> " << lut_set_size << " sets of different LUTs read from the master file";
565 
566  //loop over all EMap channels
567  RooGKCounter _counter;
568  for( std::vector<EMap::EMapRow>::const_iterator row=_map.begin(); row!=_map.end(); row++ ){
569  LutXml::Config _cfg;
570 
571  // search for the correct LUT for a given channel,
572  // higher LUT numbers have priority in case of overlapping
573  int lut_index=-1;
574  for ( int i=0; i<lut_set_size; i++ ){
575  if ( row->subdet . find("HT") != std::string::npos &&
576  (row->crate == _crate || _crate == -1) && // -1 stands for all crates
577  _set.eta_min[i] <= row->ieta &&
578  _set.eta_max[i] >= row->ieta &&
579  _set.phi_min[i] <= row->iphi &&
580  _set.phi_max[i] >= row->iphi &&
581  _coder.HTvalid(row->ieta, row->iphi, row->idepth / 10) ){
582  lut_index=i;
583  }
584  }
585  if ( lut_index >= 0 ){
586  if ( _xml.count(row->crate) == 0 && split_by_crate ){
587  _xml.insert( std::pair<int,boost::shared_ptr<LutXml> >(row->crate,boost::shared_ptr<LutXml>(new LutXml())) );
588  }
589  else if ( _xml.count(0) == 0 && !split_by_crate ){
590  _xml.insert( std::pair<int,boost::shared_ptr<LutXml> >(0,boost::shared_ptr<LutXml>(new LutXml())) );
591  }
592  _cfg.ieta = row->ieta;
593  _cfg.iphi = row->iphi;
594  _cfg.depth = row->idepth;
595  _cfg.crate = row->crate;
596  _cfg.slot = row->slot;
597  if (row->topbottom . find("t") != std::string::npos) _cfg.topbottom = 1;
598  else if (row->topbottom . find("b") != std::string::npos) _cfg.topbottom = 0;
599  else if (row->topbottom . find("u") != std::string::npos) _cfg.topbottom = 2;
600  else edm::LogWarning("HcalLutManager") << "fpga out of range...";
601  _cfg.fiber = row->fiber;
602  _cfg.fiberchan = row->fiberchan;
603  if (_set.lut[lut_index].size() == 128) _cfg.lut_type = 1;
604  else _cfg.lut_type = 2;
605  _cfg.creationtag = _tag;
606  _cfg.creationstamp = get_time_stamp( time(nullptr) );
607  _cfg.targetfirmware = "1.0.0";
608  _cfg.formatrevision = "1"; //???
609  // "original" definition of GENERALIZEDINDEX from Mike Weinberger
610  // int generalizedIndex=id.ietaAbs()+10000*id.iphi()+
611  // ((id.ieta()<0)?(0):(100));
612  _cfg.generalizedindex =
613  _cfg.iphi*10000+
614  (row->ieta>0)*100+abs(row->ieta);
615  _cfg.lut = _set.lut[lut_index];
616  if (split_by_crate ){
617  _xml[row->crate]->addLut( _cfg, lut_checksums_xml );
618  _counter.count();
619  }
620  else{
621  _xml[0]->addLut( _cfg, lut_checksums_xml );
622  _counter.count();
623  }
624  }
625  }
626  edm::LogInfo("HcalLutManager") << "LUTs generated: " << _counter.getCount() << std::endl
627  << "Generating compression (output) LUTs from ascii master file...DONE" << std::endl;
628  return _xml;
629 }
630 
631 
632 std::map<int, boost::shared_ptr<LutXml> > HcalLutManager::getLinearizationLutXmlFromCoder( const HcalTPGCoder & _coder, std::string _tag, bool split_by_crate )
633 {
634  edm::LogInfo("HcalLutManager") << "Generating linearization (input) LUTs from HcaluLUTTPGCoder...";
635  std::map<int, boost::shared_ptr<LutXml> > _xml; // index - crate number
636 
637  //EMap _emap("../../../CondFormats/HcalObjects/data/official_emap_v6.03_080817.txt");
638  //std::vector<EMap::EMapRow> & _map = _emap.get_map();
639  //std::cout << "EMap contains " << _map . size() << " entries" << std::endl;
640 
641  LMap _lmap;
642  _lmap . read( "backup/HCALmapHBEF.txt", "HBEF" );
643  // HO is not part of trigger, so TPGCoder cannot generate LUTs for it
644  //_lmap . read( "backup/HCALmapHO.txt", "HO" );
645  std::map<int,LMapRow> & _map = _lmap.get_map();
646  edm::LogInfo("HcalLutManager") << "LMap contains " << _map . size() << " channels";
647 
648  // read LUTs and their eta/phi/depth/subdet ranges
649  //HcalLutSet _set = getLinearizationLutSetFromCoder();
650  //int lut_set_size = _set.lut.size(); // number of different luts
651 
652  //loop over all HCAL channels
653  RooGKCounter _counter;
654  for( std::map<int,LMapRow>::const_iterator row=_map.begin(); row!=_map.end(); row++ ){
655  LutXml::Config _cfg;
656 
657  if ( _xml.count(row->second.crate) == 0 && split_by_crate ){
658  _xml.insert( std::pair<int,boost::shared_ptr<LutXml> >(row->second.crate,boost::shared_ptr<LutXml>(new LutXml())) );
659  }
660  else if ( _xml.count(0) == 0 && !split_by_crate ){
661  _xml.insert( std::pair<int,boost::shared_ptr<LutXml> >(0,boost::shared_ptr<LutXml>(new LutXml())) );
662  }
663  _cfg.ieta = row->second.side*row->second.eta;
664  _cfg.iphi = row->second.phi;
665  _cfg.depth = row->second.depth;
666  _cfg.crate = row->second.crate;
667  _cfg.slot = row->second.htr;
668  if (row->second.fpga . find("top") != std::string::npos) _cfg.topbottom = 1;
669  else if (row->second.fpga . find("bot") != std::string::npos) _cfg.topbottom = 0;
670  else edm::LogWarning("HcalLutManager") << "fpga out of range...";
671  // FIXME: probably fixed. fiber==htr_fi, not rm_fi in LMAP notation.
672  //_cfg.fiber = row->second.rm_fi;
673  _cfg.fiber = row->second.htr_fi;
674  _cfg.fiberchan = row->second.fi_ch;
675  _cfg.lut_type = 1;
676  _cfg.creationtag = _tag;
677  _cfg.creationstamp = get_time_stamp( time(nullptr) );
678  _cfg.targetfirmware = "1.0.0";
679  _cfg.formatrevision = "1"; //???
680  // "original" definition of GENERALIZEDINDEX from Mike Weinberger
681  // int generalizedIndex=id.ietaAbs()+1000*id.depth()+10000*id.iphi()+
682  // ((id.ieta()<0)?(0):(100))+((id.subdet()==HcalForward && id.ietaAbs()==29)?(4*10000):(0));
683  _cfg.generalizedindex =
684  _cfg.iphi*10000 + _cfg.depth*1000 +
685  (row->second.side>0)*100 + row->second.eta +
686  ((row->second.det==HcalForward && row->second.eta==29)?(4*10000):(0));
687 
688  //HcalDetId _detid(row->first);
689  HcalDetId _detid(row->second.det, row->second.side*row->second.eta, row->second.phi, row->second.depth);
690  //std::cout << "### DEBUG: rawid = " << _detid.rawId() << std::endl;
691 
692  //std::cout << "### DEBUG: subdetector = " << row->second.det << std::endl;
693  std::vector<unsigned short> coder_lut = _coder . getLinearizationLUT(_detid);
694  for (std::vector<unsigned short>::const_iterator _i=coder_lut.begin(); _i!=coder_lut.end();_i++){
695  unsigned int _temp = (unsigned int)(*_i);
696  //if (_temp!=0) std::cout << "DEBUG non-zero LUT!!!!!!!!!!!!!!!" << (*_i) << " " << _temp << std::endl;
697  //unsigned int _temp = 0;
698  _cfg.lut.push_back(_temp);
699  }
700  if (split_by_crate ){
701  _xml[row->second.crate]->addLut( _cfg, lut_checksums_xml );
702  _counter.count();
703  }
704  else{
705  _xml[0]->addLut( _cfg, lut_checksums_xml );
706  _counter.count();
707  }
708  }
709  edm::LogInfo("HcalLutManager") << "Generated LUTs: " << _counter.getCount() << std::endl
710  << "Generating linearization (input) LUTs from HcaluLUTTPGCoder...DONE" << std::endl;
711  return _xml;
712 }
713 
714 
715 std::map<int, boost::shared_ptr<LutXml> > HcalLutManager::getMasks(int masktype, std::string _tag, bool split_by_crate ){
716  edm::LogInfo("HcalLutManager") << "Generating TDC masks...";
717 
718  EMap _emap(emap);
719  std::vector<EMap::EMapRow> & _map = _emap.get_map();
720  edm::LogInfo("HcalLutManager") << "EMap contains new" << _map.size() << " entries";
721 
722  std::map<int, std::vector<uint64_t>> masks;
723 
724  for(const auto& row : _map){
725  std::string subdet=row.subdet;
726  if(subdet != "HF") continue;
727  int crate=row.crate;
728  int slot=row.slot;
729  int crot=100*crate+slot;
730  int fiber=row.fiber;
731  int channel=row.fiberchan;
732  unsigned int finel=4*fiber+channel;
733  if(masks.count(crot)==0) masks[crot]={};
734  if(finel>=masks[crot].size()) masks[crot].resize(finel+1);
735 
736  if(masktype==0){
737  HcalSubdetector _subdet;
738  if ( row.subdet.find("HB")!=string::npos ) _subdet = HcalBarrel;
739  else if ( row.subdet.find("HE")!=string::npos ) _subdet = HcalEndcap;
740  else if ( row.subdet.find("HO")!=string::npos ) _subdet = HcalOuter;
741  else if ( row.subdet.find("HF")!=string::npos ) _subdet = HcalForward;
742  else _subdet = HcalOther;
743  HcalDetId _detid(_subdet, row.ieta, row.iphi, row.idepth);
744  masks[crot][finel]= conditions->getHcalTPChannelParameter(_detid)->getMask();
745  }
746  else{
747  auto parameters = conditions->getHcalTPParameters();
748  masks[crot][finel]= masktype==1 ? parameters->getADCThresholdHF(): parameters->getTDCMaskHF();
749  }
750  }
751 
752  std::map<int, boost::shared_ptr<LutXml> > _xml; // index - crate number
753  RooGKCounter _counter;
754 
755  for(const auto &i: masks){
756  int crot=i.first;
757  int crate=crot/100;
758 
759  LutXml::Config _cfg;
760  _cfg.lut_type = 3+masktype;
761  _cfg.crate = crate;
762  _cfg.slot = crot%100;
763  _cfg.generalizedindex = crot;
764  _cfg.mask = i.second;
765  _cfg.creationtag = _tag;
766  _cfg.targetfirmware = "1.0.0";
767  _cfg.formatrevision = "1";
768 
769  int c= split_by_crate ? crate : 0;
770  if ( _xml.count(c) == 0 ) _xml[c]=boost::shared_ptr<LutXml>(new LutXml());
771 
772  _xml[c]->addLut(_cfg);
773  _counter.count();
774  }
775 
776  edm::LogInfo("HcalLutManager") << "Generated LUTs: " << _counter.getCount() << std::endl
777  << "Generating Masks...DONE" << std::endl;
778  return _xml;
779 }
780 
781 
782 
783 
784 std::map<int, boost::shared_ptr<LutXml> > HcalLutManager::getLinearizationLutXmlFromCoderEmap( const HcalTPGCoder & _coder, std::string _tag, bool split_by_crate )
785 {
786  edm::LogInfo("HcalLutManager") << "Generating linearization (input) LUTs from HcaluLUTTPGCoder...";
787  std::map<int, boost::shared_ptr<LutXml> > _xml; // index - crate number
788 
789  EMap _emap(emap);
790  std::vector<EMap::EMapRow> & _map = _emap.get_map();
791  edm::LogInfo("HcalLutManager") << "EMap contains " << _map . size() << " entries";
792 
793  RooGKCounter _counter;
794  //loop over all EMap channels
795  for( std::vector<EMap::EMapRow>::const_iterator row=_map.begin(); row!=_map.end(); row++ ){
796  if( (row->subdet.find("HB")!=string::npos ||
797  row->subdet.find("HE")!=string::npos ||
798  row->subdet.find("HF")!=string::npos ) &&
799  row->subdet.size()==2
800  ){
801  LutXml::Config _cfg;
802 
803  if ( _xml.count(row->crate) == 0 && split_by_crate ){
804  _xml.insert( std::pair<int,boost::shared_ptr<LutXml> >(row->crate,boost::shared_ptr<LutXml>(new LutXml())) );
805  }
806  else if ( _xml.count(0) == 0 && !split_by_crate ){
807  _xml.insert( std::pair<int,boost::shared_ptr<LutXml> >(0,boost::shared_ptr<LutXml>(new LutXml())) );
808  }
809  _cfg.ieta = row->ieta;
810  _cfg.iphi = row->iphi;
811  _cfg.depth = row->idepth;
812  _cfg.crate = row->crate;
813  _cfg.slot = row->slot;
814  if (row->topbottom . find("t") != std::string::npos) _cfg.topbottom = 1;
815  else if (row->topbottom . find("b") != std::string::npos) _cfg.topbottom = 0;
816  else if (row->topbottom . find("u") != std::string::npos) _cfg.topbottom = 2;
817  else edm::LogWarning("HcalLutManager") << "fpga out of range...";
818  _cfg.fiber = row->fiber;
819  _cfg.fiberchan = row->fiberchan;
820  _cfg.lut_type = 1;
821  _cfg.creationtag = _tag;
822  _cfg.creationstamp = get_time_stamp( time(nullptr) );
823  _cfg.targetfirmware = "1.0.0";
824  _cfg.formatrevision = "1"; //???
825  // "original" definition of GENERALIZEDINDEX from Mike Weinberger
826  // int generalizedIndex=id.ietaAbs()+1000*id.depth()+10000*id.iphi()+
827  // ((id.ieta()<0)?(0):(100))+((id.subdet()==HcalForward && id.ietaAbs()==29)?(4*10000):(0));
828  _cfg.generalizedindex =
829  _cfg.iphi*10000 + _cfg.depth*1000 +
830  (row->ieta>0)*100 + abs(row->ieta) +
831  (((row->subdet.find("HF")!=string::npos) && abs(row->ieta)==29)?(4*10000):(0));
832  HcalSubdetector _subdet;
833  if ( row->subdet.find("HB")!=string::npos ) _subdet = HcalBarrel;
834  else if ( row->subdet.find("HE")!=string::npos ) _subdet = HcalEndcap;
835  else if ( row->subdet.find("HO")!=string::npos ) _subdet = HcalOuter;
836  else if ( row->subdet.find("HF")!=string::npos ) _subdet = HcalForward;
837  else _subdet = HcalOther;
838  HcalDetId _detid(_subdet, row->ieta, row->iphi, row->idepth);
839 
840  for (const auto i : _coder.getLinearizationLUT(_detid)) _cfg.lut.push_back(i);
841 
842  if (split_by_crate ){
843  _xml[row->crate]->addLut( _cfg, lut_checksums_xml );
844  _counter.count();
845  }
846  else{
847  _xml[0]->addLut( _cfg, lut_checksums_xml );
848  _counter.count();
849  }
850  }
851  }
852  edm::LogInfo("HcalLutManager") << "Generated LUTs: " << _counter.getCount() << std::endl
853  << "Generating linearization (input) LUTs from HcaluLUTTPGCoder...DONE" << std::endl;
854  return _xml;
855 }
856 
857 
858 
859 std::map<int, boost::shared_ptr<LutXml> > HcalLutManager::getCompressionLutXmlFromCoder( const CaloTPGTranscoderULUT & _coder, std::string _tag, bool split_by_crate )
860 {
861  edm::LogInfo("HcalLutManager") << "Generating compression (output) LUTs from CaloTPGTranscoderULUT," << std::endl
862  << "initialized from Event Setup" << std::endl;
863  std::map<int, boost::shared_ptr<LutXml> > _xml; // index - crate number
864 
865  EMap _emap(emap);
866 
867  std::map<int,unsigned int> maxsize;
868 
869  std::vector<EMap::EMapRow> & _map = _emap.get_map();
870  edm::LogInfo("HcalLutManager") << "EMap contains " << _map . size() << " channels";
871 
872  //need to equalize compression LUT size in each crate-slot, needed for mixed uHTR
873  for(const auto& row : _map){
874  if ( row.subdet.find("HT") == std::string::npos) continue;
875  HcalTrigTowerDetId _detid(row.rawId);
876  if(!cq->topo()->validHT(_detid)) continue;
877  int crot=100*row.crate+row.slot;
878  unsigned int size=_coder.getCompressionLUT(_detid).size();
879  if(maxsize.count(crot)==0 || size>maxsize[crot]) maxsize[crot]=size;
880  }
881 
882 
883  RooGKCounter _counter;
884  for( std::vector<EMap::EMapRow>::const_iterator row=_map.begin(); row!=_map.end(); row++ ){
885  LutXml::Config _cfg;
886 
887  if ( row->subdet.find("HT") == std::string::npos) continue;
888 
889  HcalTrigTowerDetId _detid(row->rawId);
890 
891  if(!cq->topo()->validHT(_detid)) continue;
892 
893 
894  if ( _xml.count(row->crate) == 0 && split_by_crate ){
895  _xml.insert( std::pair<int,boost::shared_ptr<LutXml> >(row->crate,boost::shared_ptr<LutXml>(new LutXml())) );
896  }
897  else if ( _xml.count(0) == 0 && !split_by_crate ){
898  _xml.insert( std::pair<int,boost::shared_ptr<LutXml> >(0,boost::shared_ptr<LutXml>(new LutXml())) );
899  }
900 
901  _cfg.ieta = row->ieta;
902  _cfg.iphi = row->iphi;
903  _cfg.depth = row->idepth;
904  _cfg.crate = row->crate;
905  _cfg.slot = row->slot;
906  if (row->topbottom . find("t") != std::string::npos) _cfg.topbottom = 1;
907  else if (row->topbottom . find("b") != std::string::npos) _cfg.topbottom = 0;
908  else if (row->topbottom . find("u") != std::string::npos) _cfg.topbottom = 2;
909  else edm::LogWarning("HcalLutManager") << "fpga out of range...";
910  _cfg.fiber = row->fiber;
911  _cfg.fiberchan = row->fiberchan;
912  _cfg.lut_type = 2;
913  _cfg.creationtag = _tag;
914  _cfg.creationstamp = get_time_stamp( time(nullptr) );
915  _cfg.targetfirmware = "1.0.0";
916  _cfg.formatrevision = "1"; //???
917  _cfg.generalizedindex =_cfg.iphi*10000+ (row->ieta>0)*100+abs(row->ieta); //is this used for anything?
918 
919  _cfg.lut = _coder.getCompressionLUT(_detid);
920 
921  int crot=100*row->crate+row->slot;
922  unsigned int size=_cfg.lut.size();
923  if(size<maxsize[crot]){
924  edm::LogWarning("HcalLutManager") << " resizing LUT for " << _detid
925  << ", channel=[" <<_cfg.crate<<":"<<_cfg.slot<<":"<<_cfg.fiber<<":"<<_cfg.fiberchan
926  << "], using value=" << _cfg.lut[size-1] << std::endl;
927  for(unsigned int i=size; i<maxsize[crot]; ++i) _cfg.lut.push_back(_cfg.lut[size-1]);
928  }
929 
930  if (split_by_crate ){
931  _xml[row->crate]->addLut( _cfg, lut_checksums_xml );
932  _counter.count();
933  }
934  else{
935  _xml[0]->addLut( _cfg, lut_checksums_xml );
936  _counter.count();
937  }
938  }
939  edm::LogInfo("HcalLutManager") << "LUTs generated: " << _counter.getCount() << std::endl
940  << "Generating compression (output) LUTs from CaloTPGTranscoderULUT...DONE" << std::endl;
941 
942  return _xml;
943 }
944 
945 
946 
947 std::map<int, boost::shared_ptr<LutXml> > HcalLutManager::getCompressionLutXmlFromCoder( std::string _tag, bool split_by_crate )
948 {
949  edm::LogInfo("HcalLutManager") << "Generating compression (output) LUTs from CaloTPGTranscoderULUT";
950  std::map<int, boost::shared_ptr<LutXml> > _xml; // index - crate number
951 
952  //EMap _emap("../../../CondFormats/HcalObjects/data/official_emap_v5_080208.txt");
953  //EMap _emap("../../../CondFormats/HcalObjects/data/official_emap_v6.03_080817.txt");
954  //EMap _emap("../../../CondFormats/HcalObjects/data/official_emap_v6.04_080905.txt");
955  EMap _emap(emap);
956 
957  std::vector<EMap::EMapRow> & _map = _emap.get_map();
958  edm::LogInfo("HcalLutManager") << "EMap contains " << _map . size() << " channels";
959 
960  // read LUTs and their eta/phi/depth/subdet ranges
961  //HcalLutSet _set = getLutSetFromFile( _filename, 2 );
962  //int lut_set_size = _set.lut.size(); // number of different luts
963 
964  CaloTPGTranscoderULUT _coder;
965 
966  //loop over all EMap channels
967  RooGKCounter _counter;
968  for( std::vector<EMap::EMapRow>::const_iterator row=_map.begin(); row!=_map.end(); row++ ){
969  LutXml::Config _cfg;
970 
971  // only trigger tower channels
972  // and valid (ieta,iphi)
973  const int tp_version = row->idepth / 10;
974  if ( row->subdet . find("HT") != std::string::npos && _coder.HTvalid(row->ieta, row->iphi, tp_version) ){
975  if ( _xml.count(row->crate) == 0 && split_by_crate ){
976  _xml.insert( std::pair<int,boost::shared_ptr<LutXml> >(row->crate,boost::shared_ptr<LutXml>(new LutXml())) );
977  }
978  else if ( _xml.count(0) == 0 && !split_by_crate ){
979  _xml.insert( std::pair<int,boost::shared_ptr<LutXml> >(0,boost::shared_ptr<LutXml>(new LutXml())) );
980  }
981  _cfg.ieta = row->ieta;
982  _cfg.iphi = row->iphi;
983  _cfg.depth = row->idepth;
984  _cfg.crate = row->crate;
985  _cfg.slot = row->slot;
986  if (row->topbottom . find("t") != std::string::npos) _cfg.topbottom = 1;
987  else if (row->topbottom . find("b") != std::string::npos) _cfg.topbottom = 0;
988  else if (row->topbottom . find("u") != std::string::npos) _cfg.topbottom = 2;
989  else edm::LogWarning("HcalLutManager") << "fpga out of range...";
990  _cfg.fiber = row->fiber;
991  _cfg.fiberchan = row->fiberchan;
992  _cfg.lut_type = 2;
993  _cfg.creationtag = _tag;
994  _cfg.creationstamp = get_time_stamp( time(nullptr) );
995  _cfg.targetfirmware = "1.0.0";
996  _cfg.formatrevision = "1"; //???
997  // "original" definition of GENERALIZEDINDEX from Mike Weinberger
998  // int generalizedIndex=id.ietaAbs()+10000*id.iphi()+
999  // ((id.ieta()<0)?(0):(100));
1000  _cfg.generalizedindex =
1001  _cfg.iphi*10000+
1002  (row->ieta>0)*100+abs(row->ieta);
1003 
1004  // FIXME: work around bug in emap v6: rawId wasn't filled
1005  //HcalTrigTowerDetId _detid(row->rawId);
1006  HcalTrigTowerDetId _detid(row->ieta, row->iphi);
1007 
1008  _cfg.lut = _coder.getCompressionLUT(_detid);
1009 
1010  if (split_by_crate ){
1011  _xml[row->crate]->addLut( _cfg, lut_checksums_xml );
1012  _counter.count();
1013  }
1014  else{
1015  _xml[0]->addLut( _cfg, lut_checksums_xml );
1016  _counter.count();
1017  }
1018  }
1019  }
1020  edm::LogInfo("HcalLutManager") << "LUTs generated: " << _counter.getCount() << std::endl
1021  << "Generating compression (output) LUTs from CaloTPGTranscoderULUT...DONE" << std::endl;
1022  return _xml;
1023 }
1024 
1025 
1026 
1027 int HcalLutManager::writeLutXmlFiles( std::map<int, boost::shared_ptr<LutXml> > & _xml, std::string _tag, bool split_by_crate )
1028 {
1029  for (std::map<int,boost::shared_ptr<LutXml> >::const_iterator cr = _xml.begin(); cr != _xml.end(); cr++){
1030  std::stringstream output_file_name;
1031  if ( split_by_crate ){
1032  output_file_name << _tag << "_" << cr->first << ".xml";
1033  }
1034  else{
1035  output_file_name << _tag << ".xml";
1036  }
1037  cr->second->write( output_file_name.str() );
1038  }
1039  return 0;
1040 }
1041 
1042 int HcalLutManager::createLinLutXmlFiles( std::string _tag, std::string _lin_file, bool split_by_crate )
1043 {
1044  //std::cout << "DEBUG1: split_by_crate = " << split_by_crate << std::endl;
1045  std::map<int, boost::shared_ptr<LutXml> > xml;
1046  if ( !lut_checksums_xml ){
1047  lut_checksums_xml = new XMLDOMBlock( "CFGBrick", 1 );
1048  }
1049 
1050  if ( !_lin_file.empty() ){
1051  addLutMap( xml, getLinearizationLutXmlFromAsciiMasterEmap( _lin_file, _tag, -1, split_by_crate ) );
1052  }
1053  writeLutXmlFiles( xml, _tag, split_by_crate );
1054 
1055  std::string checksums_file = _tag + "_checksums.xml";
1056  lut_checksums_xml -> write( checksums_file );
1057 
1058  return 0;
1059 }
1060 
1061 int HcalLutManager::createAllLutXmlFiles( std::string _tag, std::string _lin_file, std::string _comp_file, bool split_by_crate )
1062 {
1063  //std::cout << "DEBUG1: split_by_crate = " << split_by_crate << std::endl;
1064  std::map<int, boost::shared_ptr<LutXml> > xml;
1065  if ( !lut_checksums_xml ){
1066  lut_checksums_xml = new XMLDOMBlock( "CFGBrick", 1 );
1067  }
1068 
1069  if ( !_lin_file.empty() ){
1070  //addLutMap( xml, getLutXmlFromAsciiMaster( _lin_file, _tag, -1, split_by_crate ) );
1071  addLutMap( xml, getLinearizationLutXmlFromAsciiMasterEmap( _lin_file, _tag, -1, split_by_crate ) );
1072  }
1073  if ( !_comp_file.empty() ){
1074  //std::cout << "DEBUG1!!!!" << std::endl;
1075  addLutMap( xml, getCompressionLutXmlFromAsciiMaster( _comp_file, _tag, -1, split_by_crate ) );
1076  //std::cout << "DEBUG2!!!!" << std::endl;
1077  }
1078  writeLutXmlFiles( xml, _tag, split_by_crate );
1079 
1080  std::string checksums_file = _tag + "_checksums.xml";
1081  lut_checksums_xml -> write( checksums_file );
1082 
1083  return 0;
1084 }
1085 
1087 {
1088  //std::cout << "DEBUG1: split_by_crate = " << split_by_crate << std::endl;
1089  std::map<int, boost::shared_ptr<LutXml> > xml;
1090  if ( !lut_checksums_xml ){
1091  lut_checksums_xml = new XMLDOMBlock( "CFGBrick", 1 );
1092  }
1093 
1094  addLutMap( xml, getCompressionLutXmlFromCoder( _tag, split_by_crate ) );
1095 
1096  writeLutXmlFiles( xml, _tag, split_by_crate );
1097 
1098  std::string checksums_file = _tag + "_checksums.xml";
1099  lut_checksums_xml -> write( checksums_file );
1100 
1101  return 0;
1102 }
1103 
1104 int HcalLutManager::createAllLutXmlFilesFromCoder( const HcalTPGCoder & _coder, std::string _tag, bool split_by_crate )
1105 {
1106  //std::cout << "DEBUG1: split_by_crate = " << split_by_crate << std::endl;
1107  std::map<int, boost::shared_ptr<LutXml> > xml;
1108  if ( !lut_checksums_xml ){
1109  lut_checksums_xml = new XMLDOMBlock( "CFGBrick", 1 );
1110  }
1111 
1112  //addLutMap( xml, getLinearizationLutXmlFromCoder( _coder, _tag, split_by_crate ) );
1113  addLutMap( xml, getLinearizationLutXmlFromCoderEmap( _coder, _tag, split_by_crate ) );
1114  addLutMap( xml, getCompressionLutXmlFromCoder( _tag, split_by_crate ) );
1115 
1116  writeLutXmlFiles( xml, _tag, split_by_crate );
1117 
1118  std::string checksums_file = _tag + "_checksums.xml";
1119  lut_checksums_xml -> write( checksums_file );
1120 
1121  return 0;
1122 }
1123 
1124 //
1125 //_____ use this for creating a full set of LUTs ________________________
1126 //
1127 int HcalLutManager::createLutXmlFiles_HBEFFromCoder_HOFromAscii( std::string _tag, const HcalTPGCoder & _coder, const CaloTPGTranscoderULUT & _transcoder, std::string _lin_file, bool split_by_crate )
1128 {
1129  std::map<int, boost::shared_ptr<LutXml> > xml;
1130  if ( !lut_checksums_xml ){
1131  lut_checksums_xml = new XMLDOMBlock( "CFGBrick", 1 );
1132  }
1133 
1134  if ( !_lin_file.empty() ){
1135  const std::map<int, boost::shared_ptr<LutXml> > _lin_lut_ascii_xml = getLinearizationLutXmlFromAsciiMasterEmap( _lin_file, _tag, -1, split_by_crate );
1136  addLutMap( xml, _lin_lut_ascii_xml );
1137  }
1138  const std::map<int, boost::shared_ptr<LutXml> > _lin_lut_xml = getLinearizationLutXmlFromCoderEmap( _coder, _tag, split_by_crate );
1139  addLutMap( xml, _lin_lut_xml );
1140  //
1141  const std::map<int, boost::shared_ptr<LutXml> > _comp_lut_xml = getCompressionLutXmlFromCoder( _transcoder, _tag, split_by_crate );
1142  addLutMap( xml, _comp_lut_xml );
1143 
1144  writeLutXmlFiles( xml, _tag, split_by_crate );
1145 
1146  std::string checksums_file = _tag + "_checksums.xml";
1147  lut_checksums_xml -> write( checksums_file );
1148 
1149  return 0;
1150 }
1151 
1152 
1154 {
1155  std::map<int, boost::shared_ptr<LutXml> > xml;
1156  if ( !lut_checksums_xml ){
1157  lut_checksums_xml = new XMLDOMBlock( "CFGBrick", 1 );
1158  }
1159 
1160  if ( !_lin_file.empty() ){
1161  const std::map<int, boost::shared_ptr<LutXml> > _lin_lut_ascii_xml = getLinearizationLutXmlFromAsciiMasterEmap( _lin_file, _tag, -1, split_by_crate );
1162  addLutMap( xml, _lin_lut_ascii_xml );
1163  }
1164  const std::map<int, boost::shared_ptr<LutXml> > _lin_lut_xml = getLinearizationLutXmlFromCoderEmap( _coder, _tag, split_by_crate );
1165  addLutMap( xml, _lin_lut_xml );
1166  //
1167  const std::map<int, boost::shared_ptr<LutXml> > _comp_lut_xml = getCompressionLutXmlFromCoder( _tag, split_by_crate );
1168  addLutMap( xml, _comp_lut_xml );
1169 
1170  writeLutXmlFiles( xml, _tag, split_by_crate );
1171 
1172  std::string checksums_file = _tag + "_checksums.xml";
1173  lut_checksums_xml -> write( checksums_file );
1174 
1175  return 0;
1176 }
1177 
1178 
1179 // use this to create HBEF only from coders (physics LUTs)
1181 {
1182  //std::cout << "DEBUG1: split_by_crate = " << split_by_crate << std::endl;
1183  std::map<int, boost::shared_ptr<LutXml> > xml;
1184  if ( !lut_checksums_xml ){
1185  lut_checksums_xml = new XMLDOMBlock( "CFGBrick", 1 );
1186  }
1187 
1188  if ( !_lin_file.empty() ){
1189  addLutMap( xml, getLutXmlFromAsciiMaster( _lin_file, _tag, -1, split_by_crate ) );
1190  }
1191  addLutMap( xml, getCompressionLutXmlFromCoder( _tag, split_by_crate ) );
1192  writeLutXmlFiles( xml, _tag, split_by_crate );
1193 
1194  std::string checksums_file = _tag + "_checksums.xml";
1195  lut_checksums_xml -> write( checksums_file );
1196 
1197  return 0;
1198 }
1199 
1200 
1201 
1202 void HcalLutManager::addLutMap(std::map<int, boost::shared_ptr<LutXml> > & result, const std::map<int, boost::shared_ptr<LutXml> > & other)
1203 {
1204  for ( std::map<int, boost::shared_ptr<LutXml> >::const_iterator lut=other.begin(); lut!=other.end(); lut++ ){
1205  edm::LogInfo("HcalLutManager") << "Added LUTs for crate " << lut->first;
1206  if ( result.count(lut->first)==0 ){
1207  result . insert( *lut );
1208  }
1209  else{
1210  *(result[lut->first]) += *(lut->second);
1211  }
1212  }
1213 }
1214 
1215 
1216 
1217 
1218 string HcalLutManager::get_time_stamp( time_t _time )
1219 {
1220  char timebuf[50];
1221  //strftime( timebuf, 50, "%c", gmtime( &_time ) );
1222  strftime( timebuf, 50, "%Y-%m-%d %H:%M:%S", gmtime( &_time ) );
1223  std::string creationstamp = timebuf;
1224 
1225  return creationstamp;
1226 }
1227 
1228 
1229 
1230 
1232 {
1233  local_connect( _filename, "backup/HCALmapHBEF.txt", "backup/HCALmapHO.txt" );
1234 
1235  //EMap _emap("../../../CondFormats/HcalObjects/data/official_emap_v6.04_080905.txt");
1236  EMap _emap(emap);
1237  std::vector<EMap::EMapRow> & _map = _emap.get_map();
1238  int map_size = _map . size();
1239  edm::LogInfo("HcalLutManager") << "EMap contains " << map_size << " channels";
1240 
1241  // make sure that all init is done
1242  std::vector<unsigned int> _lut;
1243  _lut = getLutFromXml( _tag, 1107313727, hcal::ConfigurationDatabase::LinearizerLUT );
1244 
1245 
1246  edm::LogInfo("HcalLutManager") << "Testing direct parsing of the LUT XML";
1247  struct timeval _t;
1248  gettimeofday( &_t, nullptr );
1249  double _time =(double)(_t . tv_sec) + (double)(_t . tv_usec)/1000000.0;
1250  test_direct_xml_parsing(_filename);
1251  gettimeofday( &_t, nullptr );
1252  edm::LogInfo("HcalLutManager") << "parsing took that much time: " << (double)(_t . tv_sec) + (double)(_t . tv_usec)/1000000.0 - _time;
1253 
1254 
1255  gettimeofday( &_t, nullptr );
1256  _time =(double)(_t . tv_sec) + (double)(_t . tv_usec)/1000000.0;
1257  edm::LogInfo("HcalLutManager") << "before loop over random LUTs: " << _time;
1258  int _raw_id;
1259 
1260  // loop over random LUTs
1261  for (int _iter=0; _iter<100; _iter++){
1262  gettimeofday( &_t, nullptr );
1263  //std::cout << "before getting a LUT: " << _t . tv_sec << "." << _t . tv_usec << std::endl;
1264 
1265  // select valid random emap channel
1266  while(true){
1267  int _key = (rand() % map_size);
1268  //_key = 3356;
1269  if( (_map[_key].subdet.find("HB")!=string::npos ||
1270  _map[_key].subdet.find("HE")!=string::npos ||
1271  _map[_key].subdet.find("HO")!=string::npos ||
1272  _map[_key].subdet.find("HF")!=string::npos ) &&
1273  _map[_key].subdet.size()==2
1274  ){
1275  HcalSubdetector _subdet;
1276  if ( _map[_key].subdet.find("HB")!=string::npos ) _subdet = HcalBarrel;
1277  else if ( _map[_key].subdet.find("HE")!=string::npos ) _subdet = HcalEndcap;
1278  else if ( _map[_key].subdet.find("HO")!=string::npos ) _subdet = HcalOuter;
1279  else if ( _map[_key].subdet.find("HF")!=string::npos ) _subdet = HcalForward;
1280  else _subdet = HcalOther;
1281  HcalDetId _detid(_subdet, _map[_key].ieta, _map[_key].iphi, _map[_key].idepth);
1282  _raw_id = _detid.rawId();
1283  break;
1284  }
1285  }
1286  _lut = getLutFromXml( _tag, _raw_id, hcal::ConfigurationDatabase::LinearizerLUT );
1287 
1288  gettimeofday( &_t, nullptr );
1289  }
1290  double d_time = _t.tv_sec+_t.tv_usec/1000000.0 - _time;
1291  edm::LogInfo("HcalLutManager") << "after the loop over random LUTs: " << _time+d_time << std::endl
1292  << "total time: " << d_time << std::endl;
1293 
1294  edm::LogInfo("HcalLutManager") << "LUT length = " << _lut . size();
1295  for ( std::vector<unsigned int>::const_iterator i = _lut . end() - 1; i != _lut . begin()-1; i-- )
1296  {
1297  edm::LogInfo("HcalLutManager") << (i-_lut.begin()) << " " << _lut[(i-_lut.begin())];
1298  break;
1299  }
1300 
1301  db -> disconnect();
1302 
1303  delete db;
1304  db = nullptr;
1305 
1306  return 0;
1307 }
1308 
1309 
1310 
1311 int HcalLutManager::read_lmap( std::string lmap_hbef_file, std::string lmap_ho_file )
1312 {
1313  delete lmap;
1314  lmap = new LMap();
1315  lmap -> read( lmap_hbef_file, "HBEF" );
1316  lmap -> read( lmap_ho_file, "HO" );
1317  edm::LogInfo("HcalLutManager") << "LMap contains " << lmap -> get_map() . size() << " channels (compare to 9072 of all HCAL channels)";
1318  return 0;
1319 }
1320 
1321 
1322 
1324 {
1325  delete db;
1326  db = new HCALConfigDB();
1327  db -> connect( lut_xml_file );
1328  return 0;
1329 }
1330 
1331 
1332 
1333 
1334 
1335 int HcalLutManager::local_connect( std::string lut_xml_file, std::string lmap_hbef_file, std::string lmap_ho_file )
1336 {
1337  read_lmap( lmap_hbef_file, lmap_ho_file );
1338  read_luts( lut_xml_file );
1339  return 0;
1340 }
1341 
1342 
1343 
1344 
1345 std::vector<unsigned int> HcalLutManager::getLutFromXml( std::string tag, uint32_t _rawid, hcal::ConfigurationDatabase::LUTType _lt )
1346 {
1347  edm::LogInfo("HcalLutManager") << "getLutFromXml (new version) is not implemented. Use getLutFromXml_old() for now";
1348 
1349  std::vector<unsigned int> result;
1350 
1351 
1352 
1353  return result;
1354 }
1355 
1356 
1357 // obsolete, use getLutFromXml() instead
1358 std::vector<unsigned int> HcalLutManager::getLutFromXml_old( std::string tag, uint32_t _rawid, hcal::ConfigurationDatabase::LUTType _lt )
1359 {
1360  if ( !lmap ){
1361  edm::LogError("HcalLutManager") << "Cannot find LUT without LMAP, exiting...";
1362  exit(-1);
1363  }
1364  if ( !db ){
1365  edm::LogError("HcalLutManager") << "Cannot find LUT, no source (local XML file), exiting...";
1366  exit(-1);
1367  }
1368 
1369  std::vector<unsigned int> result;
1370 
1371  std::map<int,LMapRow> & _map = lmap -> get_map();
1372  //std::cout << "HcalLutManager: LMap contains " << _map . size() << " channels (out of 9072 total)" << std::endl;
1373 
1374  HcalDetId _id( _rawid );
1375 
1376  unsigned int _crate, _slot, _fiber, _channel;
1377  std::string _fpga;
1378  int topbottom, luttype;
1379 
1380  // FIXME: check validity of _rawid
1381  if ( _map . find(_rawid) != _map.end() ){
1382  _crate = _map[_rawid] . crate;
1383  _slot = _map[_rawid] . htr;
1384  _fiber = _map[_rawid] . htr_fi;
1385  _channel = _map[_rawid] . fi_ch;
1386  _fpga = _map[_rawid] . fpga;
1387 
1388  if ( _fpga . find("top") != std::string::npos ) topbottom = 1;
1389  else if ( _fpga . find("bot") != std::string::npos ) topbottom = 0;
1390  else{
1391  edm::LogError("HcalLutManager") << "Irregular LMAP fpga value... do not know what to do - exiting";
1392  exit(-1);
1393  }
1394  if ( _lt == hcal::ConfigurationDatabase::LinearizerLUT ) luttype = 1;
1395  else luttype = 2;
1396 
1397  result = db -> getOnlineLUT( tag, _crate, _slot, topbottom, _fiber, _channel, luttype );
1398  }
1399 
1400  return result;
1401 }
1402 
1403 
1404 
1405 int HcalLutManager::get_xml_files_from_db( std::string tag, const std::string db_accessor, bool split_by_crate )
1406 {
1407  std::map<int, boost::shared_ptr<LutXml> > lut_map = get_brickSet_from_oracle( tag, db_accessor );
1408  if (split_by_crate){
1409  writeLutXmlFiles( lut_map, tag, split_by_crate );
1410  }
1411  else{
1412  LutXml result;
1413  for( std::map<int, boost::shared_ptr<LutXml> >::const_iterator xml = lut_map.begin(); xml != lut_map.end(); xml++ ){
1414  result += *(xml->second);
1415  }
1416  std::stringstream out_file;
1417  out_file << tag << ".xml";
1418  result . write(out_file.str());
1419  }
1420 
1421  return 0;
1422 }
1423 
1424 std::map<int, boost::shared_ptr<LutXml> > HcalLutManager::get_brickSet_from_oracle( std::string tag, const std::string _accessor )
1425 {
1426  HCALConfigDB * db = new HCALConfigDB();
1427  XMLProcessor::getInstance(); // initialize xerces-c engine
1428  //const std::string _accessor = "occi://CMS_HCL_PRTTYPE_HCAL_READER@anyhost/int2r?PASSWORD=HCAL_Reader_88,LHWM_VERSION=22";
1429  db -> connect( _accessor );
1430  oracle::occi::Connection * _connection = db -> getConnection();
1431 
1432  edm::LogInfo("HcalLutManager") << "Preparing to request the LUT CLOBs from the database...";
1433 
1434  //int crate = 0;
1435 
1436  //
1437  // _____ query is different for the old validation DB _________________
1438  //
1439  //std::string query = ("SELECT TRIG_PRIM_LOOKUPTBL_DATA_CLOB, CRATE FROM CMS_HCL_HCAL_CONDITION_OWNER.V_HCAL_TRIG_LOOKUP_TABLES");
1440  std::string query = ("SELECT TRIG_PRIM_LOOKUPTBL_DATA_CLOB, CRATE FROM CMS_HCL_HCAL_COND.V_HCAL_TRIG_LOOKUP_TABLES");
1441  //query+=toolbox::toString(" WHERE TAG_NAME='%s' AND CRATE=%d", tag.c_str(), crate);
1442  query+=toolbox::toString(" WHERE TAG_NAME='%s'", tag.c_str() );
1443 
1444  std::string brick_set;
1445 
1446  std::map<int, boost::shared_ptr<LutXml> > lut_map;
1447 
1448  try {
1449  //SELECT
1450  edm::LogInfo("HcalLutManager") << "Executing the query...";
1451  Statement* stmt = _connection -> createStatement();
1452  ResultSet *rs = stmt->executeQuery(query);
1453  edm::LogInfo("HcalLutManager") << "Executing the query... done";
1454 
1455  edm::LogInfo("HcalLutManager") << "Processing the query results...";
1456  //RooGKCounter _lines;
1457  while (rs->next()) {
1458  //_lines.count();
1459  oracle::occi::Clob clob = rs->getClob (1);
1460  int crate = rs->getInt(2);
1461  if ( crate != -1 ){ // not a brick with checksums
1462  edm::LogInfo("HcalLutManager") << "Getting LUTs for crate #" << crate << " out of the database...";
1463  brick_set = db -> clobToString(clob);
1464  /*
1465  // FIXME: DEBUG lut xml files from simple strings
1466  stringstream file_name;
1467  ofstream out_file;
1468  file_name << tag << "_" << crate << "_debug" << ".xml";
1469  out_file . open( file_name.str().c_str() );
1470  out_file << brick_set;
1471  out_file . close();
1472  */
1473  const char * bs = brick_set . c_str();
1474  MemBufInputSource * lut_clob = new MemBufInputSource( (const XMLByte *)bs, strlen( bs ), "lut_clob", false );
1475  boost::shared_ptr<LutXml> lut_xml = boost::shared_ptr<LutXml>( new LutXml( *lut_clob ) );
1476  lut_map[crate] = lut_xml;
1477  edm::LogInfo("HcalLutManager") << "done";
1478  }
1479  }
1480  //Always terminate statement
1481  _connection -> terminateStatement(stmt);
1482  //std::cout << "Query line count: " << _lines.getCount() << std::endl;
1483  } catch (SQLException& e) {
1484  XCEPT_RAISE(hcal::exception::ConfigurationDatabaseException,::toolbox::toString("Oracle exception : %s",e.getMessage().c_str()));
1485  }
1486 
1487  //std::cout << lut_map.size() << std::endl;
1488 
1489  db -> disconnect();
1490  //delete db;
1491  return lut_map;
1492 }
1493 
1494 
1496 {
1497  edm::LogInfo("HcalLutManager") << "Generating XML loader for LUTs...";
1498  //std::cout << _prefix << "..." << tag_name << std::endl;
1499 
1503 
1504  baseConf . tag_name = tag_name;
1505  //baseConf . comment_description = tag_name;
1506  baseConf . comment_description = comment;
1507  baseConf . iov_begin = "1";
1508  baseConf . iov_end = "-1";
1509 
1510  conf . version = version;
1511 
1512  std::stringstream _subversion;
1513  _subversion << subversion;
1514  conf . subversion = _subversion.str();
1515 
1516  CSconf . version = conf . version;
1517  CSconf . subversion = conf . subversion;
1518  CSconf . trig_prim_lookuptbl_data_file = _prefix + "_checksums.xml.dat";
1519  CSconf . comment_description = tag_name;
1520 
1521  XMLLUTLoader doc( &baseConf );
1522 
1523  std::vector<int> crate_number;
1524  std::vector<std::string> file_name = HcalQIEManager::splitString(file_list);
1525  for (std::vector<std::string>::const_iterator _f = file_name.begin(); _f != file_name.end(); _f++){
1526  int crate_begin = _f->rfind("_");
1527  int crate_end = _f->rfind(".xml.dat");
1528  crate_number . push_back(getInt(_f->substr(crate_begin+1,crate_end-crate_begin-1)));
1529  }
1530  //
1531  //_____ fix due to the new convention: version/subversion combo must be unique for every payload
1532  //
1533  char _buf[128];
1534  time_t _offset = time(nullptr);
1535  sprintf( _buf, "%d", (uint32_t)_offset );
1536  conf.version.append(".");
1537  conf.version.append(_buf);
1538  CSconf.version = conf.version;
1539  //
1540  for ( std::vector<std::string>::const_iterator _file = file_name . begin(); _file != file_name . end(); _file++ )
1541  {
1542  conf . trig_prim_lookuptbl_data_file = *_file;
1543  //conf . trig_prim_lookuptbl_data_file += ".dat";
1544  conf . crate = crate_number[ _file - file_name . begin() ];
1545  //
1546  //_____ fix due to the new convention: version/subversion combo must be unique for every payload
1547  //
1548  sprintf( _buf, "%.2d", conf.crate );
1549  conf.subversion.clear();
1550  conf.subversion.append(_buf);
1551  sprintf( _buf, "CRATE%.2d", conf . crate );
1552  std::string _namelabel;
1553  _namelabel . append( _buf );
1554  conf . name_label = _namelabel;
1555  doc . addLUT( &conf );
1556  }
1557 
1558  doc . addChecksums( &CSconf );
1559  //doc . write( _prefix + "_Loader.xml" );
1560  doc . write( tag_name + "_Loader.xml" );
1561 
1562  edm::LogInfo("HcalLutManager") << "Generating XML loader for LUTs... done.";
1563 
1564  return 0;
1565 }
1566 
1568  //EMap _emap("../../../CondFormats/HcalObjects/data/official_emap_v5_080208.txt");
1569  //EMap _emap("../../../CondFormats/HcalObjects/data/official_emap_v6.03_080817.txt");
1570  //EMap _emap("../../../CondFormats/HcalObjects/data/official_emap_v6.04_080905.txt");
1571  EMap _emap(emap);
1572  std::vector<EMap::EMapRow> & _map = _emap.get_map();
1573  std::stringstream s;
1574  s << "EMap contains " << _map . size() << " channels" << std::endl;
1575 
1576  //loop over all EMap channels
1577  //RooGKCounter _c;
1578  for( std::vector<EMap::EMapRow>::const_iterator row=_map.begin(); row!=_map.end(); row++ ){
1579 
1580  // only trigger tower channels
1581  if ( row->subdet . find("HT") != std::string::npos ){
1582  s << " -----> Subdet = " << row->subdet << std::endl;
1583 
1584  if (abs(row->ieta)>28){
1585  //if (row->iphi == 71){
1586  s << " ==> (ieta,iphi) = " << row->ieta << ", " << row->iphi << std::endl;
1587  }
1588  }
1589  }
1590  edm::LogInfo("HcalLutManager") << s.str();
1591 }
1592 
1593 
1594 
1595 
1596 
1598  /*
1599  XMLDOMBlock _xml(_filename);
1600  //DOMElement * data_set_elem = (DOMElement *)(document -> getElementsByTagName( XMLProcessor::_toXMLCh( "DATA_SET" ) ) -> item(0));
1601  DOMNodeList * brick_list = _xml . getDocument() -> getElementsByTagName( XMLProcessor::_toXMLCh( "CFGBrick" ));
1602 
1603  double n_bricks = brick_list->getLength();
1604  std::cout << "amount of LUT bricks: " << n_bricks << std::endl;
1605 
1606  for (int iter=0; iter!=n_bricks; iter++){
1607  DOMElement * _brick = (DOMElement *)(brick_list->item(iter));
1608 
1609  DOMElement * _param = 0;
1610  // loop over brick parameters
1611  int par_iter = 0;
1612  while(1){
1613  _param = (DOMElement *)(_brick->getElementsByTagName(XMLProcessor::_toXMLCh("Parameter")));
1614  std::string _name = _param->getAttribute( XMLProcessor::_toXMLCh( "name" ) );
1615  if (_name.find("IETA")==string::npos) break;
1616 
1617  std::string _tag = "Parameter";
1618  std::cout << "### Parameter IETA = " << _xml.getTagValue( _tag, 0, _brick);
1619  par_iter++;
1620  }
1621  }
1622  */
1623  return 0;
1624 }
1625 
1626 
1627 //
1628 //_____ attempt to include ZDC LUTs _____________________________________
1629 //
1630 int HcalLutManager::createLutXmlFiles_HBEFFromCoder_HOFromAscii_ZDC( std::string _tag, const HcalTPGCoder & _coder, const CaloTPGTranscoderULUT & _transcoder, std::string _lin_file, bool split_by_crate )
1631 {
1632  std::map<int, boost::shared_ptr<LutXml> > xml;
1633  if ( !lut_checksums_xml ){
1634  lut_checksums_xml = new XMLDOMBlock( "CFGBrick", 1 );
1635  }
1636 
1637  if ( !_lin_file.empty() ){
1638  const std::map<int, boost::shared_ptr<LutXml> > _lin_lut_ascii_xml = getLinearizationLutXmlFromAsciiMasterEmap( _lin_file, _tag, -1, split_by_crate );
1639  addLutMap( xml, _lin_lut_ascii_xml );
1640  }
1641  const std::map<int, boost::shared_ptr<LutXml> > _lin_lut_xml = getLinearizationLutXmlFromCoderEmap( _coder, _tag, split_by_crate );
1642  addLutMap( xml, _lin_lut_xml );
1643  //
1644  const std::map<int, boost::shared_ptr<LutXml> > _comp_lut_xml = getCompressionLutXmlFromCoder( _transcoder, _tag, split_by_crate );
1645  addLutMap( xml, _comp_lut_xml );
1646 
1647  for(auto masktype: {0,1,2}){
1648  const auto masks=getMasks(masktype, _tag, split_by_crate);
1649  addLutMap(xml, masks);
1650  }
1651  //
1652  const std::map<int, boost::shared_ptr<LutXml> > _zdc_lut_xml = getZdcLutXml( _tag, split_by_crate );
1653  addLutMap( xml, _zdc_lut_xml );
1654 
1655  writeLutXmlFiles( xml, _tag, split_by_crate );
1656 
1657  std::string checksums_file = _tag + "_checksums.xml";
1658  lut_checksums_xml -> write( checksums_file );
1659 
1660  return 0;
1661 }
1662 
1663 
1664 std::map<int, boost::shared_ptr<LutXml> > HcalLutManager::getZdcLutXml( std::string _tag,
1665  bool split_by_crate )
1666 {
1667  edm::LogInfo("HcalLutManager") << "Generating ZDC LUTs ...may the Force be with us...";
1668  std::map<int, boost::shared_ptr<LutXml> > _xml; // index - crate number
1669 
1670  EMap _emap(emap);
1671 
1672  ZdcLut zdc;
1673 
1674  std::vector<EMap::EMapRow> & _map = _emap.get_map();
1675  edm::LogInfo("HcalLutManager") << "EMap contains " << _map . size() << " channels";
1676 
1677  //loop over all EMap channels
1678  RooGKCounter _counter;
1679  for( std::vector<EMap::EMapRow>::const_iterator row=_map.begin(); row!=_map.end(); row++ ){
1680  LutXml::Config _cfg;
1681 
1682  // only ZDC channels
1683  if ( row->zdc_section . find("ZDC") != std::string::npos ){
1684  if ( _xml.count(row->crate) == 0 && split_by_crate ){
1685  _xml.insert( std::pair<int,boost::shared_ptr<LutXml> >(row->crate,boost::shared_ptr<LutXml>(new LutXml())) );
1686  }
1687  else if ( _xml.count(0) == 0 && !split_by_crate ){
1688  _xml.insert( std::pair<int,boost::shared_ptr<LutXml> >(0,boost::shared_ptr<LutXml>(new LutXml())) );
1689  }
1690  // FIXME: introduce proper tag names in ZDC bricks for logical channel info
1691  _cfg.ieta = row->zdc_channel; // int
1692  //_cfg.ieta = row->zdc_zside; // int
1693  //_cfg.iphi = row->zdc_section; // string
1694  _cfg.depth = row->idepth; // int
1695  _cfg.crate = row->crate;
1696  _cfg.slot = row->slot;
1697  if (row->topbottom . find("t") != std::string::npos) _cfg.topbottom = 1;
1698  else if (row->topbottom . find("b") != std::string::npos) _cfg.topbottom = 0;
1699  else edm::LogWarning("HcalLutManager") << "fpga out of range...";
1700  _cfg.fiber = row->fiber;
1701  _cfg.fiberchan = row->fiberchan;
1702  _cfg.lut_type = 1;
1703  _cfg.creationtag = _tag;
1704  _cfg.creationstamp = get_time_stamp( time(nullptr) );
1705  _cfg.targetfirmware = "1.0.0";
1706  _cfg.formatrevision = "1"; //???
1707  _cfg.generalizedindex = 0;
1708 
1709  //HcalZDCDetId _detid(row->zdc_section, (row->zdc_zside>0), row->zdc_channel);
1710 
1711  std::vector<int> coder_lut = zdc.get_lut(row->zdc_section,
1712  row->zdc_zside,
1713  row->zdc_channel);
1714  edm::LogInfo("HcalLutManager") << "***DEBUG: ZDC lut size: " << coder_lut.size();
1715  if (!coder_lut.empty()){
1716  for (std::vector<int>::const_iterator _i=coder_lut.begin(); _i!=coder_lut.end();_i++){
1717  unsigned int _temp = (unsigned int)(*_i);
1718  //if (_temp!=0) std::cout << "DEBUG non-zero LUT!!!!!!!!!!!!!!!" << (*_i) << " " << _temp << std::endl;
1719  //unsigned int _temp = 0;
1720  _cfg.lut.push_back(_temp);
1721  }
1722  //_cfg.lut = _set.lut[lut_index];
1723 
1724  if (split_by_crate ){
1725  _xml[row->crate]->addLut( _cfg, lut_checksums_xml );
1726  _counter.count();
1727  }
1728  else{
1729  _xml[0]->addLut( _cfg, lut_checksums_xml );
1730  _counter.count();
1731  }
1732  } //size of lut
1733  }
1734  }
1735  edm::LogInfo("HcalLutManager") << "LUTs generated: " << _counter.getCount() << std::endl
1736  << "Generating ZDC LUTs...DONE" << std::endl;
1737 
1738  return _xml;
1739 }
1740 
1741 
int adc(sample_type sample)
get the ADC sample (12 bits)
size
Write out results.
int initChannelIterator(std::vector< HcalGenericDetId > &map)
std::map< int, boost::shared_ptr< LutXml > > getLutXmlFromAsciiMaster(std::string _filename, std::string _tag, int _crate=-1, bool split_by_crate=true)
std::vector< std::vector< unsigned int > > lut
std::vector< int > get_lut(int emap_side, int emap_htr_fiber, int emap_fi_ch)
Definition: ZdcLut.cc:212
std::string targetfirmware
Definition: LutXml.h:39
Definition: LutXml.h:27
static int getLutSetFromFile_test(std::string _filename)
std::vector< int > phi_max
int generalizedindex
Definition: LutXml.h:40
int createLutXmlFiles_HBEFFromCoder_HOFromAscii_ZDC(std::string _tag, const HcalTPGCoder &_coder, const CaloTPGTranscoderULUT &_transcoder, std::string _lin_file, bool split_by_crate=true)
virtual bool HTvalid(const int ieta, const int iphi, const int version) const
std::map< int, boost::shared_ptr< LutXml > > get_brickSet_from_oracle(std::string tag, const std::string _accessor="occi://CMS_HCL_PRTTYPE_HCAL_READER@anyhost/int2r?PASSWORD=HCAL_Reader_88,LHWM_VERSION=22")
std::vector< std::string > subdet
Various manipulations with trigger Lookup Tables.
int init
Definition: HydjetWrapper.h:67
std::string & getLutXml(std::vector< unsigned int > &_lut)
std::map< int, boost::shared_ptr< LutXml > > getCompressionLutXmlFromAsciiMaster(std::string _filename, std::string _tag, int _crate=-1, bool split_by_crate=true)
virtual const std::vector< unsigned int > getCompressionLUT(const HcalTrigTowerDetId &id) const
Generation of ZDC Lookup tables and associate helper methods.
Definition: ZdcLut.h:25
std::vector< int > phi_min
std::vector< int > depth_max
int get_xml_files_from_db(std::string tag, const std::string db_accessor="occi://CMS_HCL_PRTTYPE_HCAL_READER@anyhost/int2r?PASSWORD=HCAL_Reader_88,LHWM_VERSION=22", bool split_by_crate=true)
unsigned long int getCount(void)
int getInt(ResultSet *rset, int ipar)
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:20
static HcalSubdetector get_subdetector(std::string _subdet)
std::map< int, boost::shared_ptr< LutXml > > getCompressionLutXmlFromCoder(std::string _tag, bool split_by_crate=true)
int createCompLutXmlFilesFromCoder(std::string _tag, bool split_by_crate=true)
int createLinLutXmlFiles(std::string _tag, std::string _lin_file, bool split_by_crate=true)
std::map< int, boost::shared_ptr< LutXml > > getLinearizationLutXmlFromCoder(const HcalTPGCoder &_coder, std::string _tag, bool split_by_crate=true)
Definition: query.py:1
std::map< int, LMapRow > & get_map(void)
Definition: LMap.cc:60
uint32_t rawId() const
get the raw id
Definition: DetId.h:43
int test_direct_xml_parsing(std::string _filename)
int topbottom
Definition: LutXml.h:35
std::vector< unsigned int > lut
Definition: LutXml.h:41
std::string creationtag
Definition: LutXml.h:36
void addLutMap(std::map< int, boost::shared_ptr< LutXml > > &result, const std::map< int, boost::shared_ptr< LutXml > > &other)
static std::vector< std::string > splitString(const std::string &fLine)
std::map< int, boost::shared_ptr< LutXml > > getZdcLutXml(std::string _tag, bool split_by_crate=true)
std::string toString(const char *format,...)
Definition: xdaq_compat.cc:4
int read_luts(std::string lut_xml_file)
std::vector< int > depth_min
HcalSubdetector
Definition: HcalAssistant.h:31
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
std::map< int, boost::shared_ptr< LutXml > > getLinearizationLutXmlFromAsciiMasterEmap_new(std::string _filename, std::string _tag, int _crate, bool split_by_crate=true)
int writeLutXmlFiles(std::map< int, boost::shared_ptr< LutXml > > &_xml, std::string _tag="default_tag", bool split_by_crate=true)
HcalLutSet getLutSetFromFile(std::string _filename, int _type=1)
#define end
Definition: vmac.h:37
bool insert(Storage &iStorage, ItemType *iItem, const IdTag &iIdTag)
Definition: HCMethods.h:49
void init(void)
std::string formatrevision
Definition: LutXml.h:38
virtual std::vector< unsigned short > getLinearizationLUT(HcalDetId id) const
Get the full linearization LUT (128 elements). Default implementation just uses adc2Linear to get all...
Definition: HcalTPGCoder.cc:3
Gather config data from online DB.
Definition: HCALConfigDB.h:21
Definition: DetId.h:18
int test_xml_access(std::string _tag, std::string _filename)
int create_lut_loader(std::string file_list, std::string _prefix, std::string tag_name, std::string comment="default comment", std::string version="V00-01-01", int subversion=1)
int createAllLutXmlFiles(std::string _tag, std::string _lin_file, std::string _comp_file, bool split_by_crate=true)
std::map< int, boost::shared_ptr< LutXml > > getLinearizationLutXmlFromCoderEmap(const HcalTPGCoder &_coder, std::string _tag, bool split_by_crate=true)
edm::Handle< T > connect(const T *&ptr, edm::EDGetTokenT< T > token, const edm::Event &evt)
std::map< int, boost::shared_ptr< LutXml > > getMasks(int var, std::string _tag, bool split_by_crate=true)
void count(void)
Definition: RooGKCounter.cc:66
#define begin
Definition: vmac.h:30
constexpr uint32_t masks[]
Definition: CaloRecHit.cc:12
static int getInt(std::string number)
static std::string get_time_stamp(time_t _time)
const HcalElectronicsMap * getHcalMapping() const
Signal rand(Signal arg)
Definition: vlib.cc:442
int local_connect(std::string lut_xml_file, std::string lmap_hbef_file, std::string lmap_ho_file)
std::vector< unsigned int > getLutFromXml(std::string tag, uint32_t _rawid, hcal::ConfigurationDatabase::LUTType _lt)
std::map< int, boost::shared_ptr< LutXml > > getLinearizationLutXmlFromAsciiMasterEmap(std::string _filename, std::string _tag, int _crate, bool split_by_crate=true)
int createAllLutXmlFilesLinAsciiCompCoder(std::string _tag, std::string _lin_file, bool split_by_crate=true)
std::vector< int > eta_max
std::vector< EMap::EMapRow > & get_map(void)
Definition: LMap.cc:306
Definition: LMap.h:94
int read_lmap(std::string lmap_hbef_file, std::string lmap_ho_file)
def write(self, setup)
int lut_type
Definition: LutXml.h:35
std::string creationstamp
Definition: LutXml.h:37
Definition: LMap.h:75
Readout chain identification for Hcal.
int createLutXmlFiles_HBEFFromCoder_HOFromAscii(std::string _tag, const HcalTPGCoder &_coder, std::string _lin_file, bool split_by_crate=true)
int fiberchan
Definition: LutXml.h:35
std::vector< int > eta_min
static XMLProcessor * getInstance()
Definition: XMLProcessor.h:145
int createAllLutXmlFilesFromCoder(const HcalTPGCoder &_coder, std::string _tag, bool split_by_crate=true)
void test_emap(void)
std::vector< unsigned int > getLutFromXml_old(std::string tag, uint32_t _rawid, hcal::ConfigurationDatabase::LUTType _lt)
std::vector< uint64_t > mask
Definition: LutXml.h:42
#define comment(par)
Definition: vmac.h:161