CMS 3D CMS Logo

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