Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-10-12 04:20:23

0001 #include <cstdlib>
0002 #include <fstream>
0003 #include <memory>
0004 #include <sstream>
0005 #include <sys/stat.h>
0006 #include <sys/time.h>
0007 #include <sys/types.h>
0008 #include <unistd.h>
0009 
0010 #include <cstdlib>  // For srand() and rand()
0011 
0012 #ifdef HAVE_XDAQ
0013 #include <toolbox/string.h>
0014 #else
0015 #include "CaloOnlineTools/HcalOnlineDb/interface/xdaq_compat.h"  // Replaces toolbox::toString
0016 #endif
0017 
0018 #include "OnlineDB/Oracle/interface/Oracle.h"
0019 
0020 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0021 
0022 #include "CaloOnlineTools/HcalOnlineDb/interface/HcalLutManager.h"
0023 #include "CaloOnlineTools/HcalOnlineDb/interface/ZdcLut.h"
0024 #include "CalibCalorimetry/HcalTPGAlgos/interface/XMLProcessor.h"
0025 #include "CalibCalorimetry/HcalTPGAlgos/interface/XMLDOMBlock.h"
0026 #include "CaloOnlineTools/HcalOnlineDb/interface/HcalQIEManager.h"
0027 #include "CaloOnlineTools/HcalOnlineDb/interface/LMap.h"
0028 #include "CaloOnlineTools/HcalOnlineDb/interface/XMLLUTLoader.h"
0029 #include "CaloOnlineTools/HcalOnlineDb/interface/RooGKCounter.h"
0030 #include "DataFormats/HcalDetId/interface/HcalDetId.h"
0031 
0032 #include "DataFormats/HcalDetId/interface/HcalSubdetector.h"
0033 XERCES_CPP_NAMESPACE_USE
0034 using namespace std;
0035 using namespace oracle::occi;
0036 using namespace hcal;
0037 
0038 /**
0039 
0040    \class HcalLutManager
0041    \brief Various manipulations with trigger Lookup Tables
0042    \author Gena Kukartsev, Brown University, March 14, 2008
0043 
0044 */
0045 
0046 HcalLutManager::HcalLutManager(void) { init(); }
0047 
0048 HcalLutManager::HcalLutManager(std::vector<HcalGenericDetId>& map) {
0049   init();
0050   _iter.init(map);
0051 }
0052 
0053 HcalLutManager::HcalLutManager(const HcalElectronicsMap* _emap,
0054                                const HcalChannelQuality* _cq,
0055                                uint32_t _status_word_to_mask) {
0056   init();
0057   emap = _emap;
0058   cq = _cq;
0059   status_word_to_mask = _status_word_to_mask;
0060 }
0061 
0062 HcalLutManager::HcalLutManager(const HcalDbService* _conditions,
0063                                const HcalChannelQuality* _cq,
0064                                uint32_t _status_word_to_mask) {
0065   init();
0066   conditions = _conditions;
0067   emap = conditions->getHcalMapping();
0068   cq = _cq;
0069   status_word_to_mask = _status_word_to_mask;
0070 }
0071 
0072 void HcalLutManager::init(void) {
0073   lut_xml = nullptr;
0074   lut_checksums_xml = nullptr;
0075   db = nullptr;
0076   lmap = nullptr;
0077   emap = nullptr;
0078   cq = nullptr;
0079   conditions = nullptr;
0080   status_word_to_mask = 0x0000;
0081 }
0082 
0083 HcalLutManager::~HcalLutManager(void) {
0084   delete lut_xml;
0085   delete lut_checksums_xml;
0086   delete db;
0087   delete lmap;
0088 }
0089 
0090 int HcalLutManager::initChannelIterator(std::vector<HcalGenericDetId>& map) {
0091   _iter.init(map);
0092   return _iter.size();
0093 }
0094 
0095 std::string& HcalLutManager::getLutXml(std::vector<unsigned int>& _lut) {
0096   if (lut_xml)
0097     delete lut_xml;
0098 
0099   lut_xml = new LutXml();
0100 
0101   LutXml::Config _config;
0102   _config.lut = _lut;
0103   lut_xml->addLut(_config);
0104   lut_xml->addLut(_config);
0105   lut_xml->addLut(_config);
0106 
0107   //return lut_xml->getString();
0108   return lut_xml->getCurrentBrick();
0109 }
0110 
0111 int HcalLutManager::getInt(std::string number) {
0112   int result;
0113   sscanf(number.c_str(), "%d", &result);
0114   return result;
0115 }
0116 
0117 HcalSubdetector HcalLutManager::get_subdetector(std::string _det) {
0118   HcalSubdetector result;
0119   if (_det.find("HB") != std::string::npos)
0120     result = HcalBarrel;
0121   else if (_det.find("HE") != std::string::npos)
0122     result = HcalEndcap;
0123   else if (_det.find("HF") != std::string::npos)
0124     result = HcalForward;
0125   else if (_det.find("HO") != std::string::npos)
0126     result = HcalOuter;
0127   else
0128     result = HcalOther;
0129 
0130   return result;
0131 }
0132 
0133 int HcalLutManager_test::getLutSetFromFile_test(std::string _filename) {
0134   HcalLutManager _manager;
0135   HcalLutSet _set = _manager.getLutSetFromFile(_filename);
0136   std::stringstream s;
0137   s << "===> Test of HcalLutSet HcalLutManager::getLutSetFromFile( std::string _filename )" << std::endl << std::endl;
0138   s << _set.label << std::endl;
0139   for (unsigned int i = 0; i != _set.subdet.size(); i++)
0140     s << _set.subdet[i] << "    ";
0141   s << std::endl;
0142   for (unsigned int i = 0; i != _set.eta_min.size(); i++)
0143     s << _set.eta_min[i] << "   ";
0144   s << std::endl;
0145   for (unsigned int i = 0; i != _set.eta_max.size(); i++)
0146     s << _set.eta_max[i] << "   ";
0147   s << std::endl;
0148   for (unsigned int i = 0; i != _set.phi_min.size(); i++)
0149     s << _set.phi_min[i] << "   ";
0150   s << std::endl;
0151   for (unsigned int i = 0; i != _set.phi_max.size(); i++)
0152     s << _set.phi_max[i] << "   ";
0153   s << std::endl;
0154   for (unsigned int i = 0; i != _set.depth_min.size(); i++)
0155     s << _set.depth_min[i] << " ";
0156   s << std::endl;
0157   for (unsigned int i = 0; i != _set.depth_max.size(); i++)
0158     s << _set.depth_max[i] << " ";
0159   s << std::endl;
0160   for (unsigned int j = 0; j != _set.lut[0].size(); j++) {
0161     for (unsigned int i = 0; i != _set.lut.size(); i++) {
0162       s << _set.lut[i][j] << "  ";
0163     }
0164     s << "---> " << j << std::endl;
0165   }
0166   edm::LogInfo("HcalLutManager") << s.str();
0167   return 0;
0168 }
0169 
0170 HcalLutSet HcalLutManager::getLutSetFromFile(std::string _filename, int _type) {
0171   HcalLutSet _lutset;
0172 
0173   ifstream infile(_filename.c_str());
0174   std::string buf;
0175 
0176   if (infile.is_open()) {
0177     edm::LogInfo("HcalLutManager") << "File " << _filename << " is open..." << std::endl
0178                                    << "Reading LUTs and their eta/phi/depth/subdet ranges...";
0179 
0180     // get label
0181     getline(infile, _lutset.label);
0182 
0183     if (_type == 1) {  // for linearization LUTs get subdetectors (default)
0184       //get subdetectors
0185       getline(infile, buf);
0186       _lutset.subdet = HcalQIEManager::splitString(buf);
0187     }
0188 
0189     //get min etas
0190     std::vector<std::string> buf_vec;
0191     getline(infile, buf);
0192     buf_vec = HcalQIEManager::splitString(buf);
0193     for (std::vector<std::string>::const_iterator iter = buf_vec.begin(); iter != buf_vec.end(); iter++) {
0194       _lutset.eta_min.push_back(HcalLutManager::getInt(*iter));
0195     }
0196 
0197     //get max etas
0198     getline(infile, buf);
0199     buf_vec = HcalQIEManager::splitString(buf);
0200     for (std::vector<std::string>::const_iterator iter = buf_vec.begin(); iter != buf_vec.end(); iter++) {
0201       _lutset.eta_max.push_back(HcalLutManager::getInt(*iter));
0202     }
0203 
0204     //get min phis
0205     getline(infile, buf);
0206     buf_vec = HcalQIEManager::splitString(buf);
0207     for (std::vector<std::string>::const_iterator iter = buf_vec.begin(); iter != buf_vec.end(); iter++) {
0208       _lutset.phi_min.push_back(HcalLutManager::getInt(*iter));
0209     }
0210 
0211     //get max phis
0212     getline(infile, buf);
0213     buf_vec = HcalQIEManager::splitString(buf);
0214     for (std::vector<std::string>::const_iterator iter = buf_vec.begin(); iter != buf_vec.end(); iter++) {
0215       _lutset.phi_max.push_back(HcalLutManager::getInt(*iter));
0216     }
0217 
0218     if (_type == 1) {  // for linearization LUTs get depth range (default)
0219       //get min depths
0220       getline(infile, buf);
0221       buf_vec = HcalQIEManager::splitString(buf);
0222       for (std::vector<std::string>::const_iterator iter = buf_vec.begin(); iter != buf_vec.end(); iter++) {
0223         _lutset.depth_min.push_back(HcalLutManager::getInt(*iter));
0224       }
0225 
0226       //get max depths
0227       getline(infile, buf);
0228       buf_vec = HcalQIEManager::splitString(buf);
0229       for (std::vector<std::string>::const_iterator iter = buf_vec.begin(); iter != buf_vec.end(); iter++) {
0230         _lutset.depth_max.push_back(HcalLutManager::getInt(*iter));
0231       }
0232     }
0233 
0234     bool first_lut_entry = true;
0235     while (getline(infile, buf)) {
0236       buf_vec = HcalQIEManager::splitString(buf);
0237       for (unsigned int i = 0; i < buf_vec.size(); i++) {
0238         if (first_lut_entry) {
0239           std::vector<unsigned int> _l;
0240           _lutset.lut.push_back(_l);
0241         }
0242         _lutset.lut[i].push_back(HcalLutManager::getInt(buf_vec[i]));
0243       }
0244       first_lut_entry = false;
0245     }
0246   }
0247 
0248   edm::LogInfo("HcalLutManager") << "done.";
0249 
0250   return _lutset;
0251 }
0252 
0253 std::map<int, std::shared_ptr<LutXml>> HcalLutManager::getLutXmlFromAsciiMaster(std::string _filename,
0254                                                                                 std::string _tag,
0255                                                                                 int _crate,
0256                                                                                 bool split_by_crate) {
0257   edm::LogInfo("HcalLutManager") << "Generating linearization (input) LUTs from ascii master file...";
0258   std::map<int, std::shared_ptr<LutXml>> _xml;  // index - crate number
0259 
0260   LMap _lmap;
0261   _lmap.read("./backup/HCALmapHBEF.txt", "HBEF");
0262   _lmap.read("./backup/HCALmapHO.txt", "HO");
0263   std::map<int, LMapRow>& _map = _lmap.get_map();
0264   edm::LogInfo("HcalLutManager") << "LMap contains " << _map.size() << " channels";
0265 
0266   // read LUTs and their eta/phi/depth/subdet ranges
0267   HcalLutSet _set = getLutSetFromFile(_filename);
0268   int lut_set_size = _set.lut.size();  // number of different luts
0269 
0270   RooGKCounter _counter;
0271   //loop over all HCAL channels
0272   for (std::map<int, LMapRow>::const_iterator row = _map.begin(); row != _map.end(); row++) {
0273     LutXml::Config _cfg;
0274 
0275     // search for the correct LUT for a given channel,
0276     // higher LUT numbers have priority in case of overlapping
0277     int lut_index = -1;
0278     for (int i = 0; i < lut_set_size; i++) {
0279       if ((row->second.crate == _crate || _crate == -1) &&  // -1 stands for all crates
0280           _set.eta_min[i] <= row->second.side * row->second.eta &&
0281           _set.eta_max[i] >= row->second.side * row->second.eta && _set.phi_min[i] <= row->second.phi &&
0282           _set.phi_max[i] >= row->second.phi && _set.depth_min[i] <= row->second.depth &&
0283           _set.depth_max[i] >= row->second.depth && get_subdetector(_set.subdet[i]) == row->second.det) {
0284         lut_index = i;
0285       }
0286     }
0287     if (lut_index >= 0) {
0288       if (_xml.count(row->second.crate) == 0 && split_by_crate) {
0289         _xml.insert(std::pair<int, std::shared_ptr<LutXml>>(row->second.crate, std::make_shared<LutXml>()));
0290       } else if (_xml.count(0) == 0 && !split_by_crate) {
0291         _xml.insert(std::pair<int, std::shared_ptr<LutXml>>(0, std::make_shared<LutXml>()));
0292       }
0293       _cfg.ieta = row->second.side * row->second.eta;
0294       _cfg.iphi = row->second.phi;
0295       _cfg.depth = row->second.depth;
0296       _cfg.crate = row->second.crate;
0297       _cfg.slot = row->second.htr;
0298       if (row->second.fpga.find("top") != std::string::npos)
0299         _cfg.topbottom = 1;
0300       else if (row->second.fpga.find("bot") != std::string::npos)
0301         _cfg.topbottom = 0;
0302       else
0303         edm::LogWarning("HcalLutManager") << "fpga out of range...";
0304       // FIXME: probably fixed. fiber==htr_fi, not rm_fi in LMAP notation.
0305       //_cfg.fiber = row->second.rm_fi;
0306       _cfg.fiber = row->second.htr_fi;
0307       _cfg.fiberchan = row->second.fi_ch;
0308       if (_set.lut[lut_index].size() == 128)
0309         _cfg.lut_type = 1;
0310       else
0311         _cfg.lut_type = 2;
0312       _cfg.creationtag = _tag;
0313       _cfg.creationstamp = get_time_stamp(time(nullptr));
0314       _cfg.targetfirmware = "1.0.0";
0315       _cfg.formatrevision = "1";  //???
0316       // "original" definition of GENERALIZEDINDEX from Mike Weinberger
0317       //    int generalizedIndex=id.ietaAbs()+1000*id.depth()+10000*id.iphi()+
0318       //        ((id.ieta()<0)?(0):(100))+((id.subdet()==HcalForward && id.ietaAbs()==29)?(4*10000):(0));
0319       _cfg.generalizedindex = _cfg.iphi * 10000 + _cfg.depth * 1000 + (row->second.side > 0) * 100 + row->second.eta +
0320                               ((row->second.det == HcalForward && row->second.eta == 29) ? (4 * 10000) : (0));
0321       _cfg.lut = _set.lut[lut_index];
0322       if (split_by_crate) {
0323         _xml[row->second.crate]->addLut(_cfg, lut_checksums_xml);
0324         _counter.count();
0325       } else {
0326         _xml[0]->addLut(_cfg, lut_checksums_xml);
0327         _counter.count();
0328       }
0329     }
0330   }
0331   edm::LogInfo("HcalLutManager") << "LUTs generated: " << _counter.getCount() << std::endl
0332                                  << "Generating linearization (input) LUTs from ascii master file...DONE" << std::endl;
0333   return _xml;
0334 }
0335 
0336 //
0337 //_____ get HO from ASCII master here ___________________________________
0338 //
0339 std::map<int, std::shared_ptr<LutXml>> HcalLutManager::getLinearizationLutXmlFromAsciiMasterEmap(std::string _filename,
0340                                                                                                  std::string _tag,
0341                                                                                                  int _crate,
0342                                                                                                  bool split_by_crate) {
0343   edm::LogInfo("HcalLutManager") << "Generating linearization (input) LUTs from ascii master file...";
0344   std::map<int, std::shared_ptr<LutXml>> _xml;  // index - crate number
0345 
0346   EMap _emap(emap);
0347   std::vector<EMap::EMapRow>& _map = _emap.get_map();
0348   edm::LogInfo("HcalLutManager") << "EMap contains " << _map.size() << " entries";
0349 
0350   // read LUTs and their eta/phi/depth/subdet ranges
0351   HcalLutSet _set = getLutSetFromFile(_filename);
0352   int lut_set_size = _set.lut.size();  // number of different luts
0353   edm::LogInfo("HcalLutManager") << "  ==> " << lut_set_size << " sets of different LUTs read from the master file";
0354 
0355   // setup "zero" LUT for channel masking
0356   std::vector<unsigned int> zeroLut;
0357   for (size_t adc = 0; adc < 128; adc++)
0358     zeroLut.push_back(0);
0359 
0360   RooGKCounter _counter;
0361   //loop over all EMap channels
0362   for (std::vector<EMap::EMapRow>::const_iterator row = _map.begin(); row != _map.end(); row++) {
0363     if ((row->subdet.find("HB") != string::npos || row->subdet.find("HE") != string::npos ||
0364          row->subdet.find("HO") != string::npos || row->subdet.find("HF") != string::npos) &&
0365         row->subdet.size() == 2) {
0366       LutXml::Config _cfg;
0367 
0368       // search for the correct LUT for a given channel,
0369       // higher LUT numbers have priority in case of overlapping
0370       int lut_index = -1;
0371       for (int i = 0; i < lut_set_size; i++) {
0372         if ((row->crate == _crate || _crate == -1) &&  // -1 stands for all crates
0373             _set.eta_min[i] <= row->ieta && _set.eta_max[i] >= row->ieta && _set.phi_min[i] <= row->iphi &&
0374             _set.phi_max[i] >= row->iphi && _set.depth_min[i] <= row->idepth && _set.depth_max[i] >= row->idepth &&
0375             _set.subdet[i].find(row->subdet) != string::npos) {
0376           lut_index = i;
0377         }
0378       }
0379       if (lut_index >= 0) {
0380         if (_xml.count(row->crate) == 0 && split_by_crate) {
0381           _xml.insert(std::pair<int, std::shared_ptr<LutXml>>(row->crate, std::make_shared<LutXml>()));
0382         } else if (_xml.count(0) == 0 && !split_by_crate) {
0383           _xml.insert(std::pair<int, std::shared_ptr<LutXml>>(0, std::make_shared<LutXml>()));
0384         }
0385         _cfg.ieta = row->ieta;
0386         _cfg.iphi = row->iphi;
0387         _cfg.depth = row->idepth;
0388         _cfg.crate = row->crate;
0389         _cfg.slot = row->slot;
0390         if (row->topbottom.find('t') != std::string::npos)
0391           _cfg.topbottom = 1;
0392         else if (row->topbottom.find('b') != std::string::npos)
0393           _cfg.topbottom = 0;
0394         else if (row->topbottom.find('u') != std::string::npos)
0395           _cfg.topbottom = 2;
0396         else
0397           edm::LogWarning("HcalLutManager") << "fpga out of range...";
0398         _cfg.fiber = row->fiber;
0399         _cfg.fiberchan = row->fiberchan;
0400         _cfg.lut_type = 1;
0401         _cfg.creationtag = _tag;
0402         _cfg.creationstamp = get_time_stamp(time(nullptr));
0403         _cfg.targetfirmware = "1.0.0";
0404         _cfg.formatrevision = "1";  //???
0405         // "original" definition of GENERALIZEDINDEX from Mike Weinberger
0406         //    int generalizedIndex=id.ietaAbs()+1000*id.depth()+10000*id.iphi()+
0407         //        ((id.ieta()<0)?(0):(100))+((id.subdet()==HcalForward && id.ietaAbs()==29)?(4*10000):(0));
0408         _cfg.generalizedindex =
0409             _cfg.iphi * 10000 + _cfg.depth * 1000 + (row->ieta > 0) * 100 + abs(row->ieta) +
0410             (((row->subdet.find("HF") != string::npos) && abs(row->ieta) == 29) ? (4 * 10000) : (0));
0411         //
0412         // consider channel status here
0413         DetId _detId(row->rawId);
0414         uint32_t status_word = cq->getValues(_detId)->getValue();
0415         if ((status_word & status_word_to_mask) > 0) {
0416           _cfg.lut = zeroLut;
0417         } else {
0418           _cfg.lut = _set.lut[lut_index];
0419         }
0420         if (split_by_crate) {
0421           _xml[row->crate]->addLut(_cfg, lut_checksums_xml);
0422           _counter.count();
0423         } else {
0424           _xml[0]->addLut(_cfg, lut_checksums_xml);
0425           _counter.count();
0426         }
0427       }
0428     }
0429   }
0430   edm::LogInfo("HcalLutManager") << "LUTs generated: " << _counter.getCount() << std::endl
0431                                  << "Generating linearization (input) LUTs from ascii master file...DONE" << std::endl;
0432   return _xml;
0433 }
0434 
0435 std::map<int, std::shared_ptr<LutXml>> HcalLutManager::getLinearizationLutXmlFromAsciiMasterEmap_new(
0436     std::string _filename, std::string _tag, int _crate, bool split_by_crate) {
0437   edm::LogInfo("HcalLutManager") << "Generating linearization (input) LUTs from ascii master file...";
0438   std::map<int, std::shared_ptr<LutXml>> _xml;  // index - crate number
0439 
0440   // read LUTs and their eta/phi/depth/subdet ranges
0441   HcalLutSet _set = getLutSetFromFile(_filename);
0442   int lut_set_size = _set.lut.size();  // number of different luts
0443   edm::LogInfo("HcalLutManager") << "  ==> " << lut_set_size << " sets of different LUTs read from the master file";
0444 
0445   RooGKCounter _counter;
0446   //loop over all EMap channels
0447   for (_iter.begin(); !_iter.end(); _iter.next()) {
0448     HcalSubdetector _subdet = _iter.getHcalSubdetector();
0449     if ((_subdet == HcalBarrel || _subdet == HcalEndcap || _subdet == HcalForward || _subdet == HcalOuter)) {
0450       int _ieta = _iter.getIeta();
0451       int _iphi = _iter.getIphi();
0452       int _depth = _iter.getDepth();
0453 
0454       // FIXME: this is probably wrong, raw ids are different
0455       HcalElectronicsId _eId(_iter.getHcalGenericDetId().rawId());
0456       int aCrate = _eId.readoutVMECrateId();
0457       int aSlot = _eId.htrSlot();
0458       int aTopBottom = _eId.htrTopBottom();
0459       int aFiber = _eId.fiberIndex();
0460       int aFiberChan = _eId.fiberChanId();
0461 
0462       LutXml::Config _cfg;
0463 
0464       // search for the correct LUT for a given channel,
0465       // higher LUT numbers have priority in case of overlapping
0466       int lut_index = -1;
0467       for (int i = 0; i < lut_set_size; i++) {
0468         if ((aCrate == _crate || _crate == -1) &&  // -1 stands for all crates
0469             _set.eta_min[i] <= _ieta && _set.eta_max[i] >= _ieta && _set.phi_min[i] <= _iphi &&
0470             _set.phi_max[i] >= _iphi && _set.depth_min[i] <= _depth && _set.depth_max[i] >= _depth &&
0471             _set.subdet[i].find(_ass.getSubdetectorString(_subdet)) != string::npos) {
0472           lut_index = i;
0473         }
0474       }
0475       if (lut_index >= 0) {
0476         if (_xml.count(aCrate) == 0 && split_by_crate) {
0477           _xml.insert(std::pair<int, std::shared_ptr<LutXml>>(aCrate, std::make_shared<LutXml>()));
0478         } else if (_xml.count(0) == 0 && !split_by_crate) {
0479           _xml.insert(std::pair<int, std::shared_ptr<LutXml>>(0, std::make_shared<LutXml>()));
0480         }
0481         _cfg.ieta = _ieta;
0482         _cfg.iphi = _iphi;
0483         _cfg.depth = _depth;
0484         _cfg.crate = aCrate;
0485         _cfg.slot = aSlot;
0486         _cfg.topbottom = aTopBottom;
0487         _cfg.fiber = aFiber;
0488         _cfg.fiberchan = aFiberChan;
0489         _cfg.lut_type = 1;
0490         _cfg.creationtag = _tag;
0491         _cfg.creationstamp = get_time_stamp(time(nullptr));
0492         _cfg.targetfirmware = "1.0.0";
0493         _cfg.formatrevision = "1";  //???
0494         // "original" definition of GENERALIZEDINDEX from Mike Weinberger
0495         //    int generalizedIndex=id.ietaAbs()+1000*id.depth()+10000*id.iphi()+
0496         //        ((id.ieta()<0)?(0):(100))+((id.subdet()==HcalForward && id.ietaAbs()==29)?(4*10000):(0));
0497         _cfg.generalizedindex = _cfg.iphi * 10000 + _cfg.depth * 1000 + (_ieta > 0) * 100 + abs(_ieta) +
0498                                 (((_subdet == HcalForward) && abs(_ieta) == 29) ? (4 * 10000) : (0));
0499         _cfg.lut = _set.lut[lut_index];
0500         if (split_by_crate) {
0501           _xml[aCrate]->addLut(_cfg, lut_checksums_xml);
0502           _counter.count();
0503         } else {
0504           _xml[0]->addLut(_cfg, lut_checksums_xml);
0505           _counter.count();
0506         }
0507       }
0508     }
0509   }
0510   edm::LogInfo("HcalLutManager") << "LUTs generated: " << _counter.getCount() << std::endl
0511                                  << "Generating linearization (input) LUTs from ascii master file...DONE" << std::endl;
0512   return _xml;
0513 }
0514 
0515 std::map<int, std::shared_ptr<LutXml>> HcalLutManager::getCompressionLutXmlFromAsciiMaster(std::string _filename,
0516                                                                                            std::string _tag,
0517                                                                                            int _crate,
0518                                                                                            bool split_by_crate) {
0519   edm::LogInfo("HcalLutManager") << "Generating compression (output) LUTs from ascii master file...";
0520   std::map<int, std::shared_ptr<LutXml>> _xml;  // index - crate number
0521 
0522   edm::LogInfo("HcalLutManager")
0523       << "instantiating CaloTPGTranscoderULUT in order to check the validity of (ieta,iphi)...";
0524   CaloTPGTranscoderULUT _coder;
0525 
0526   //EMap _emap("../../../CondFormats/HcalObjects/data/official_emap_v6.03_080817.txt");
0527   //EMap _emap("../../../CondFormats/HcalObjects/data/official_emap_v6.04_080905.txt");
0528   EMap _emap(emap);
0529   std::vector<EMap::EMapRow>& _map = _emap.get_map();
0530   edm::LogInfo("HcalLutManager") << "EMap contains " << _map.size() << " channels";
0531 
0532   // read LUTs and their eta/phi/depth/subdet ranges
0533   HcalLutSet _set = getLutSetFromFile(_filename, 2);
0534   int lut_set_size = _set.lut.size();  // number of different luts
0535   edm::LogInfo("HcalLutManager") << "  ==> " << lut_set_size << " sets of different LUTs read from the master file";
0536 
0537   //loop over all EMap channels
0538   RooGKCounter _counter;
0539   for (std::vector<EMap::EMapRow>::const_iterator row = _map.begin(); row != _map.end(); row++) {
0540     LutXml::Config _cfg;
0541 
0542     // search for the correct LUT for a given channel,
0543     // higher LUT numbers have priority in case of overlapping
0544     int lut_index = -1;
0545     for (int i = 0; i < lut_set_size; i++) {
0546       if (row->subdet.find("HT") != std::string::npos &&
0547           (row->crate == _crate || _crate == -1) &&  // -1 stands for all crates
0548           _set.eta_min[i] <= row->ieta && _set.eta_max[i] >= row->ieta && _set.phi_min[i] <= row->iphi &&
0549           _set.phi_max[i] >= row->iphi && _coder.HTvalid(row->ieta, row->iphi, row->idepth / 10)) {
0550         lut_index = i;
0551       }
0552     }
0553     if (lut_index >= 0) {
0554       if (_xml.count(row->crate) == 0 && split_by_crate) {
0555         _xml.insert(std::pair<int, std::shared_ptr<LutXml>>(row->crate, std::make_shared<LutXml>()));
0556       } else if (_xml.count(0) == 0 && !split_by_crate) {
0557         _xml.insert(std::pair<int, std::shared_ptr<LutXml>>(0, std::make_shared<LutXml>()));
0558       }
0559       _cfg.ieta = row->ieta;
0560       _cfg.iphi = row->iphi;
0561       _cfg.depth = row->idepth;
0562       _cfg.crate = row->crate;
0563       _cfg.slot = row->slot;
0564       if (row->topbottom.find('t') != std::string::npos)
0565         _cfg.topbottom = 1;
0566       else if (row->topbottom.find('b') != std::string::npos)
0567         _cfg.topbottom = 0;
0568       else if (row->topbottom.find('u') != std::string::npos)
0569         _cfg.topbottom = 2;
0570       else
0571         edm::LogWarning("HcalLutManager") << "fpga out of range...";
0572       _cfg.fiber = row->fiber;
0573       _cfg.fiberchan = row->fiberchan;
0574       if (_set.lut[lut_index].size() == 128)
0575         _cfg.lut_type = 1;
0576       else
0577         _cfg.lut_type = 2;
0578       _cfg.creationtag = _tag;
0579       _cfg.creationstamp = get_time_stamp(time(nullptr));
0580       _cfg.targetfirmware = "1.0.0";
0581       _cfg.formatrevision = "1";  //???
0582       // "original" definition of GENERALIZEDINDEX from Mike Weinberger
0583       //   int generalizedIndex=id.ietaAbs()+10000*id.iphi()+
0584       //       ((id.ieta()<0)?(0):(100));
0585       _cfg.generalizedindex = _cfg.iphi * 10000 + (row->ieta > 0) * 100 + abs(row->ieta);
0586       _cfg.lut = _set.lut[lut_index];
0587       if (split_by_crate) {
0588         _xml[row->crate]->addLut(_cfg, lut_checksums_xml);
0589         _counter.count();
0590       } else {
0591         _xml[0]->addLut(_cfg, lut_checksums_xml);
0592         _counter.count();
0593       }
0594     }
0595   }
0596   edm::LogInfo("HcalLutManager") << "LUTs generated: " << _counter.getCount() << std::endl
0597                                  << "Generating compression (output) LUTs from ascii master file...DONE" << std::endl;
0598   return _xml;
0599 }
0600 
0601 std::map<int, std::shared_ptr<LutXml>> HcalLutManager::getLinearizationLutXmlFromCoder(const HcalTPGCoder& _coder,
0602                                                                                        std::string _tag,
0603                                                                                        bool split_by_crate) {
0604   edm::LogInfo("HcalLutManager") << "Generating linearization (input) LUTs from HcaluLUTTPGCoder...";
0605   std::map<int, std::shared_ptr<LutXml>> _xml;  // index - crate number
0606 
0607   //EMap _emap("../../../CondFormats/HcalObjects/data/official_emap_v6.03_080817.txt");
0608   //std::vector<EMap::EMapRow> & _map = _emap.get_map();
0609   //std::cout << "EMap contains " << _map . size() << " entries" << std::endl;
0610 
0611   LMap _lmap;
0612   _lmap.read("backup/HCALmapHBEF.txt", "HBEF");
0613   // HO is not part of trigger, so TPGCoder cannot generate LUTs for it
0614   //_lmap . read( "backup/HCALmapHO.txt", "HO" );
0615   std::map<int, LMapRow>& _map = _lmap.get_map();
0616   edm::LogInfo("HcalLutManager") << "LMap contains " << _map.size() << " channels";
0617 
0618   // read LUTs and their eta/phi/depth/subdet ranges
0619   //HcalLutSet _set = getLinearizationLutSetFromCoder();
0620   //int lut_set_size = _set.lut.size(); // number of different luts
0621 
0622   //loop over all HCAL channels
0623   RooGKCounter _counter;
0624   for (std::map<int, LMapRow>::const_iterator row = _map.begin(); row != _map.end(); row++) {
0625     LutXml::Config _cfg;
0626 
0627     if (_xml.count(row->second.crate) == 0 && split_by_crate) {
0628       _xml.insert(std::pair<int, std::shared_ptr<LutXml>>(row->second.crate, std::make_shared<LutXml>()));
0629     } else if (_xml.count(0) == 0 && !split_by_crate) {
0630       _xml.insert(std::pair<int, std::shared_ptr<LutXml>>(0, std::make_shared<LutXml>()));
0631     }
0632     _cfg.ieta = row->second.side * row->second.eta;
0633     _cfg.iphi = row->second.phi;
0634     _cfg.depth = row->second.depth;
0635     _cfg.crate = row->second.crate;
0636     _cfg.slot = row->second.htr;
0637     if (row->second.fpga.find("top") != std::string::npos)
0638       _cfg.topbottom = 1;
0639     else if (row->second.fpga.find("bot") != std::string::npos)
0640       _cfg.topbottom = 0;
0641     else
0642       edm::LogWarning("HcalLutManager") << "fpga out of range...";
0643     // FIXME: probably fixed. fiber==htr_fi, not rm_fi in LMAP notation.
0644     //_cfg.fiber = row->second.rm_fi;
0645     _cfg.fiber = row->second.htr_fi;
0646     _cfg.fiberchan = row->second.fi_ch;
0647     _cfg.lut_type = 1;
0648     _cfg.creationtag = _tag;
0649     _cfg.creationstamp = get_time_stamp(time(nullptr));
0650     _cfg.targetfirmware = "1.0.0";
0651     _cfg.formatrevision = "1";  //???
0652     // "original" definition of GENERALIZEDINDEX from Mike Weinberger
0653     //    int generalizedIndex=id.ietaAbs()+1000*id.depth()+10000*id.iphi()+
0654     //        ((id.ieta()<0)?(0):(100))+((id.subdet()==HcalForward && id.ietaAbs()==29)?(4*10000):(0));
0655     _cfg.generalizedindex = _cfg.iphi * 10000 + _cfg.depth * 1000 + (row->second.side > 0) * 100 + row->second.eta +
0656                             ((row->second.det == HcalForward && row->second.eta == 29) ? (4 * 10000) : (0));
0657 
0658     //HcalDetId _detid(row->first);
0659     HcalDetId _detid(row->second.det, row->second.side * row->second.eta, row->second.phi, row->second.depth);
0660     //std::cout << "### DEBUG: rawid = " << _detid.rawId() << std::endl;
0661 
0662     //std::cout << "### DEBUG: subdetector = " << row->second.det << std::endl;
0663     std::vector<unsigned short> coder_lut = _coder.getLinearizationLUT(_detid);
0664     for (std::vector<unsigned short>::const_iterator _i = coder_lut.begin(); _i != coder_lut.end(); _i++) {
0665       unsigned int _temp = (unsigned int)(*_i);
0666       //if (_temp!=0) std::cout << "DEBUG non-zero LUT!!!!!!!!!!!!!!!" << (*_i) << "     " << _temp << std::endl;
0667       //unsigned int _temp = 0;
0668       _cfg.lut.push_back(_temp);
0669     }
0670     if (split_by_crate) {
0671       _xml[row->second.crate]->addLut(_cfg, lut_checksums_xml);
0672       _counter.count();
0673     } else {
0674       _xml[0]->addLut(_cfg, lut_checksums_xml);
0675       _counter.count();
0676     }
0677   }
0678   edm::LogInfo("HcalLutManager") << "Generated LUTs: " << _counter.getCount() << std::endl
0679                                  << "Generating linearization (input) LUTs from HcaluLUTTPGCoder...DONE" << std::endl;
0680   return _xml;
0681 }
0682 
0683 std::map<int, std::shared_ptr<LutXml>> HcalLutManager::getMasks(int masktype, std::string _tag, bool split_by_crate) {
0684   edm::LogInfo("HcalLutManager") << "Generating TDC masks...";
0685 
0686   EMap _emap(emap);
0687   std::vector<EMap::EMapRow>& _map = _emap.get_map();
0688   edm::LogInfo("HcalLutManager") << "EMap contains new" << _map.size() << " entries";
0689 
0690   std::map<int, std::vector<uint64_t>> masks;
0691 
0692   for (const auto& row : _map) {
0693     std::string subdet = row.subdet;
0694     if (subdet != "HF")
0695       continue;
0696     int crate = row.crate;
0697     int slot = row.slot;
0698     int crot = 100 * crate + slot;
0699     int fiber = row.fiber;
0700     int channel = row.fiberchan;
0701     unsigned int finel = 4 * fiber + channel;
0702     if (masks.count(crot) == 0)
0703       masks[crot] = {};
0704     if (finel >= masks[crot].size())
0705       masks[crot].resize(finel + 1);
0706 
0707     if (masktype == 0) {
0708       HcalSubdetector _subdet;
0709       if (row.subdet.find("HB") != string::npos)
0710         _subdet = HcalBarrel;
0711       else if (row.subdet.find("HE") != string::npos)
0712         _subdet = HcalEndcap;
0713       else if (row.subdet.find("HO") != string::npos)
0714         _subdet = HcalOuter;
0715       else if (row.subdet.find("HF") != string::npos)
0716         _subdet = HcalForward;
0717       else
0718         _subdet = HcalOther;
0719       HcalDetId _detid(_subdet, row.ieta, row.iphi, row.idepth);
0720       masks[crot][finel] = conditions->getHcalTPChannelParameter(_detid)->getMask();
0721     } else {
0722       auto parameters = conditions->getHcalTPParameters();
0723       masks[crot][finel] = masktype == 1 ? parameters->getADCThresholdHF() : parameters->getTDCMaskHF();
0724     }
0725   }
0726 
0727   std::map<int, std::shared_ptr<LutXml>> _xml;  // index - crate number
0728   RooGKCounter _counter;
0729 
0730   for (const auto& i : masks) {
0731     int crot = i.first;
0732     int crate = crot / 100;
0733 
0734     LutXml::Config _cfg;
0735     _cfg.lut_type = 5 + masktype;
0736     _cfg.crate = crate;
0737     _cfg.slot = crot % 100;
0738     _cfg.generalizedindex = crot;
0739     _cfg.mask = i.second;
0740     _cfg.creationtag = _tag;
0741     _cfg.targetfirmware = "1.0.0";
0742     _cfg.formatrevision = "1";
0743 
0744     int c = split_by_crate ? crate : 0;
0745     if (_xml.count(c) == 0)
0746       _xml[c] = std::make_shared<LutXml>();
0747 
0748     _xml[c]->addLut(_cfg);
0749     _counter.count();
0750   }
0751 
0752   edm::LogInfo("HcalLutManager") << "Generated LUTs: " << _counter.getCount() << std::endl
0753                                  << "Generating Masks...DONE" << std::endl;
0754   return _xml;
0755 }
0756 
0757 std::map<int, std::shared_ptr<LutXml>> HcalLutManager::getLinearizationLutXmlFromCoderEmap(const HcalTPGCoder& _coder,
0758                                                                                            std::string _tag,
0759                                                                                            bool split_by_crate) {
0760   edm::LogInfo("HcalLutManager") << "Generating linearization (input) LUTs from HcaluLUTTPGCoder...";
0761   std::map<int, std::shared_ptr<LutXml>> _xml;  // index - crate number
0762 
0763   EMap _emap(emap);
0764   std::vector<EMap::EMapRow>& _map = _emap.get_map();
0765   edm::LogInfo("HcalLutManager") << "EMap contains " << _map.size() << " entries";
0766 
0767   RooGKCounter _counter;
0768   //loop over all EMap channels
0769   for (std::vector<EMap::EMapRow>::const_iterator row = _map.begin(); row != _map.end(); row++) {
0770     if ((row->subdet.find("HB") != string::npos || row->subdet.find("HE") != string::npos ||
0771          row->subdet.find("HF") != string::npos) &&
0772         row->subdet.size() == 2) {
0773       LutXml::Config _cfg;
0774 
0775       if (_xml.count(row->crate) == 0 && split_by_crate) {
0776         _xml.insert(std::pair<int, std::shared_ptr<LutXml>>(row->crate, std::make_shared<LutXml>()));
0777       } else if (_xml.count(0) == 0 && !split_by_crate) {
0778         _xml.insert(std::pair<int, std::shared_ptr<LutXml>>(0, std::make_shared<LutXml>()));
0779       }
0780       _cfg.ieta = row->ieta;
0781       _cfg.iphi = row->iphi;
0782       _cfg.depth = row->idepth;
0783       _cfg.crate = row->crate;
0784       _cfg.slot = row->slot;
0785       if (row->topbottom.find('t') != std::string::npos)
0786         _cfg.topbottom = 1;
0787       else if (row->topbottom.find('b') != std::string::npos)
0788         _cfg.topbottom = 0;
0789       else if (row->topbottom.find('u') != std::string::npos)
0790         _cfg.topbottom = 2;
0791       else
0792         edm::LogWarning("HcalLutManager") << "fpga out of range...";
0793       _cfg.fiber = row->fiber;
0794       _cfg.fiberchan = row->fiberchan;
0795       _cfg.lut_type = 1;
0796       _cfg.creationtag = _tag;
0797       _cfg.creationstamp = get_time_stamp(time(nullptr));
0798       _cfg.targetfirmware = "1.0.0";
0799       _cfg.formatrevision = "1";  //???
0800       // "original" definition of GENERALIZEDINDEX from Mike Weinberger
0801       //    int generalizedIndex=id.ietaAbs()+1000*id.depth()+10000*id.iphi()+
0802       //        ((id.ieta()<0)?(0):(100))+((id.subdet()==HcalForward && id.ietaAbs()==29)?(4*10000):(0));
0803       _cfg.generalizedindex = _cfg.iphi * 10000 + _cfg.depth * 1000 + (row->ieta > 0) * 100 + abs(row->ieta) +
0804                               (((row->subdet.find("HF") != string::npos) && abs(row->ieta) == 29) ? (4 * 10000) : (0));
0805       HcalSubdetector _subdet;
0806       if (row->subdet.find("HB") != string::npos)
0807         _subdet = HcalBarrel;
0808       else if (row->subdet.find("HE") != string::npos)
0809         _subdet = HcalEndcap;
0810       else if (row->subdet.find("HO") != string::npos)
0811         _subdet = HcalOuter;
0812       else if (row->subdet.find("HF") != string::npos)
0813         _subdet = HcalForward;
0814       else
0815         _subdet = HcalOther;
0816       HcalDetId _detid(_subdet, row->ieta, row->iphi, row->idepth);
0817 
0818       for (const auto i : _coder.getLinearizationLUT(_detid))
0819         _cfg.lut.push_back(i);
0820 
0821       if (split_by_crate) {
0822         _xml[row->crate]->addLut(_cfg, lut_checksums_xml);
0823         _counter.count();
0824       } else {
0825         _xml[0]->addLut(_cfg, lut_checksums_xml);
0826         _counter.count();
0827       }
0828     }
0829   }
0830   edm::LogInfo("HcalLutManager") << "Generated LUTs: " << _counter.getCount() << std::endl
0831                                  << "Generating linearization (input) LUTs from HcaluLUTTPGCoder...DONE" << std::endl;
0832   return _xml;
0833 }
0834 
0835 std::map<int, std::shared_ptr<LutXml>> HcalLutManager::getHEFineGrainLUTs(std::string _tag, bool split_by_crate) {
0836   edm::LogInfo("HcalLutManager") << "Generating HE fine grain LUTs";
0837   std::map<int, std::shared_ptr<LutXml>> _xml;  // index - crate number
0838 
0839   EMap _emap(emap);
0840   std::vector<EMap::EMapRow>& _map = _emap.get_map();
0841   edm::LogInfo("HcalLutManager") << "EMap contains " << _map.size() << " entries";
0842 
0843   RooGKCounter _counter;
0844   //loop over all EMap channels
0845   for (std::vector<EMap::EMapRow>::const_iterator row = _map.begin(); row != _map.end(); row++) {
0846     if (row->subdet.find("HT") != string::npos && row->subdet.size() == 2) {
0847       int abseta = abs(row->ieta);
0848       const HcalTopology* topo = cq->topo();
0849       if (abseta <= topo->lastHBRing() or abseta > topo->lastHERing())
0850         continue;
0851       if (abseta >= topo->firstHEDoublePhiRing() and row->fiberchan % 2 == 1)
0852         continue;  //do only actual physical towers
0853       LutXml::Config _cfg;
0854 
0855       if (_xml.count(row->crate) == 0 && split_by_crate) {
0856         _xml.insert(std::pair<int, std::shared_ptr<LutXml>>(row->crate, std::make_shared<LutXml>()));
0857       } else if (_xml.count(0) == 0 && !split_by_crate) {
0858         _xml.insert(std::pair<int, std::shared_ptr<LutXml>>(0, std::make_shared<LutXml>()));
0859       }
0860       _cfg.ieta = row->ieta;
0861       _cfg.iphi = row->iphi;
0862       _cfg.depth = row->idepth;
0863       _cfg.crate = row->crate;
0864       _cfg.slot = row->slot;
0865       if (row->topbottom.find('t') != std::string::npos)
0866         _cfg.topbottom = 1;
0867       else if (row->topbottom.find('b') != std::string::npos)
0868         _cfg.topbottom = 0;
0869       else if (row->topbottom.find('u') != std::string::npos)
0870         _cfg.topbottom = 2;
0871       else
0872         edm::LogWarning("HcalLutManager") << "fpga out of range...";
0873       _cfg.fiber = row->fiber;
0874       _cfg.fiberchan = row->fiberchan;
0875       _cfg.lut_type = 4;
0876       _cfg.creationtag = _tag;
0877       _cfg.creationstamp = get_time_stamp(time(nullptr));
0878       _cfg.targetfirmware = "1.0.0";
0879       _cfg.formatrevision = "1";  //???
0880       _cfg.generalizedindex = _cfg.iphi * 10000 + _cfg.depth * 1000 + (row->ieta > 0) * 100 + abs(row->ieta) +
0881                               (((row->subdet.find("HF") != string::npos) && abs(row->ieta) == 29) ? (4 * 10000) : (0));
0882       // fine grain LUTs only relevant for HE
0883       HcalSubdetector _subdet = HcalEndcap;
0884       HcalDetId _detid(_subdet, row->ieta, row->iphi, row->idepth);
0885 
0886       HcalFinegrainBit fg_algo(conditions->getHcalTPParameters()->getFGVersionHBHE());
0887 
0888       // loop over all possible configurations,
0889       // computing the LUT for each
0890       const int n_fgab_bits = 2048;
0891       for (int i = 0; i < 2 * n_fgab_bits; i++) {
0892         HcalFinegrainBit::Tower tow;
0893         for (int k = 0; k < 6; k++) {
0894           tow[0][k] = (1 << k) & i;
0895           tow[1][k] = (1 << (k + 6)) & i;
0896         }
0897         _cfg.lut.push_back(fg_algo.compute(tow).to_ulong());
0898       }
0899 
0900       if (split_by_crate) {
0901         _xml[row->crate]->addLut(_cfg, lut_checksums_xml);
0902         _counter.count();
0903       } else {
0904         _xml[0]->addLut(_cfg, lut_checksums_xml);
0905         _counter.count();
0906       }
0907     }
0908   }
0909   edm::LogInfo("HcalLutManager") << "Generated LUTs: " << _counter.getCount() << std::endl
0910                                  << "Generating HE fine grain LUTs from HcaluLUTTPGCoder...DONE" << std::endl;
0911   return _xml;
0912 }
0913 
0914 std::map<int, std::shared_ptr<LutXml>> HcalLutManager::getCompressionLutXmlFromCoder(
0915     const CaloTPGTranscoderULUT& _coder, std::string _tag, bool split_by_crate) {
0916   edm::LogInfo("HcalLutManager") << "Generating compression (output) LUTs from CaloTPGTranscoderULUT," << std::endl
0917                                  << "initialized from Event Setup" << std::endl;
0918   std::map<int, std::shared_ptr<LutXml>> _xml;  // index - crate number
0919 
0920   EMap _emap(emap);
0921 
0922   std::map<int, unsigned int> maxsize;
0923 
0924   std::vector<EMap::EMapRow>& _map = _emap.get_map();
0925   edm::LogInfo("HcalLutManager") << "EMap contains " << _map.size() << " channels";
0926 
0927   //need to equalize compression LUT size in each crate-slot, needed for mixed uHTR
0928   for (const auto& row : _map) {
0929     if (row.subdet.find("HT") == std::string::npos)
0930       continue;
0931     HcalTrigTowerDetId _detid(row.rawId);
0932     if (!cq->topo()->validHT(_detid))
0933       continue;
0934     int crot = 100 * row.crate + row.slot;
0935     unsigned int size = _coder.getCompressionLUT(_detid).size();
0936     if (maxsize.count(crot) == 0 || size > maxsize[crot])
0937       maxsize[crot] = size;
0938   }
0939 
0940   RooGKCounter _counter;
0941   for (std::vector<EMap::EMapRow>::const_iterator row = _map.begin(); row != _map.end(); row++) {
0942     LutXml::Config _cfg;
0943 
0944     if (row->subdet.find("HT") == std::string::npos)
0945       continue;
0946 
0947     HcalTrigTowerDetId _detid(row->rawId);
0948 
0949     if (!cq->topo()->validHT(_detid))
0950       continue;
0951 
0952     if (_xml.count(row->crate) == 0 && split_by_crate) {
0953       _xml.insert(std::pair<int, std::shared_ptr<LutXml>>(row->crate, std::make_shared<LutXml>()));
0954     } else if (_xml.count(0) == 0 && !split_by_crate) {
0955       _xml.insert(std::pair<int, std::shared_ptr<LutXml>>(0, std::make_shared<LutXml>()));
0956     }
0957 
0958     _cfg.ieta = row->ieta;
0959     _cfg.iphi = row->iphi;
0960     _cfg.depth = row->idepth;
0961     _cfg.crate = row->crate;
0962     _cfg.slot = row->slot;
0963     if (row->topbottom.find('t') != std::string::npos)
0964       _cfg.topbottom = 1;
0965     else if (row->topbottom.find('b') != std::string::npos)
0966       _cfg.topbottom = 0;
0967     else if (row->topbottom.find('u') != std::string::npos)
0968       _cfg.topbottom = 2;
0969     else
0970       edm::LogWarning("HcalLutManager") << "fpga out of range...";
0971     _cfg.fiber = row->fiber;
0972     _cfg.fiberchan = row->fiberchan;
0973     _cfg.lut_type = 2;
0974     _cfg.creationtag = _tag;
0975     _cfg.creationstamp = get_time_stamp(time(nullptr));
0976     _cfg.targetfirmware = "1.0.0";
0977     _cfg.formatrevision = "1";                                                           //???
0978     _cfg.generalizedindex = _cfg.iphi * 10000 + (row->ieta > 0) * 100 + abs(row->ieta);  //is this used for anything?
0979 
0980     _cfg.lut = _coder.getCompressionLUT(_detid);
0981     auto pWeight = conditions->getHcalTPChannelParameter(_detid, false);
0982     if (pWeight) {
0983       _cfg.weight = pWeight->getauxi1();
0984       _cfg.codedvetothreshold = pWeight->getauxi2();
0985     }
0986 
0987     int crot = 100 * row->crate + row->slot;
0988     unsigned int size = _cfg.lut.size();
0989     if (size < maxsize[crot]) {
0990       edm::LogWarning("HcalLutManager") << " resizing LUT for " << _detid << ", channel=[" << _cfg.crate << ":"
0991                                         << _cfg.slot << ":" << _cfg.fiber << ":" << _cfg.fiberchan
0992                                         << "], using value=" << _cfg.lut[size - 1] << std::endl;
0993       for (unsigned int i = size; i < maxsize[crot]; ++i)
0994         _cfg.lut.push_back(_cfg.lut[size - 1]);
0995     }
0996 
0997     if (split_by_crate) {
0998       _xml[row->crate]->addLut(_cfg, lut_checksums_xml);
0999       _counter.count();
1000     } else {
1001       _xml[0]->addLut(_cfg, lut_checksums_xml);
1002       _counter.count();
1003     }
1004   }
1005   edm::LogInfo("HcalLutManager") << "LUTs generated: " << _counter.getCount() << std::endl
1006                                  << "Generating compression (output) LUTs from CaloTPGTranscoderULUT...DONE"
1007                                  << std::endl;
1008 
1009   return _xml;
1010 }
1011 
1012 std::map<int, std::shared_ptr<LutXml>> HcalLutManager::getCompressionLutXmlFromCoder(std::string _tag,
1013                                                                                      bool split_by_crate) {
1014   edm::LogInfo("HcalLutManager") << "Generating compression (output) LUTs from CaloTPGTranscoderULUT";
1015   std::map<int, std::shared_ptr<LutXml>> _xml;  // index - crate number
1016 
1017   //EMap _emap("../../../CondFormats/HcalObjects/data/official_emap_v5_080208.txt");
1018   //EMap _emap("../../../CondFormats/HcalObjects/data/official_emap_v6.03_080817.txt");
1019   //EMap _emap("../../../CondFormats/HcalObjects/data/official_emap_v6.04_080905.txt");
1020   EMap _emap(emap);
1021 
1022   std::vector<EMap::EMapRow>& _map = _emap.get_map();
1023   edm::LogInfo("HcalLutManager") << "EMap contains " << _map.size() << " channels";
1024 
1025   // read LUTs and their eta/phi/depth/subdet ranges
1026   //HcalLutSet _set = getLutSetFromFile( _filename, 2 );
1027   //int lut_set_size = _set.lut.size(); // number of different luts
1028 
1029   CaloTPGTranscoderULUT _coder;
1030 
1031   //loop over all EMap channels
1032   RooGKCounter _counter;
1033   for (std::vector<EMap::EMapRow>::const_iterator row = _map.begin(); row != _map.end(); row++) {
1034     LutXml::Config _cfg;
1035 
1036     // only trigger tower channels
1037     // and valid (ieta,iphi)
1038     const int tp_version = row->idepth / 10;
1039     if (row->subdet.find("HT") != std::string::npos && _coder.HTvalid(row->ieta, row->iphi, tp_version)) {
1040       if (_xml.count(row->crate) == 0 && split_by_crate) {
1041         _xml.insert(std::pair<int, std::shared_ptr<LutXml>>(row->crate, std::make_shared<LutXml>()));
1042       } else if (_xml.count(0) == 0 && !split_by_crate) {
1043         _xml.insert(std::pair<int, std::shared_ptr<LutXml>>(0, std::make_shared<LutXml>()));
1044       }
1045       _cfg.ieta = row->ieta;
1046       _cfg.iphi = row->iphi;
1047       _cfg.depth = row->idepth;
1048       _cfg.crate = row->crate;
1049       _cfg.slot = row->slot;
1050       if (row->topbottom.find('t') != std::string::npos)
1051         _cfg.topbottom = 1;
1052       else if (row->topbottom.find('b') != std::string::npos)
1053         _cfg.topbottom = 0;
1054       else if (row->topbottom.find('u') != std::string::npos)
1055         _cfg.topbottom = 2;
1056       else
1057         edm::LogWarning("HcalLutManager") << "fpga out of range...";
1058       _cfg.fiber = row->fiber;
1059       _cfg.fiberchan = row->fiberchan;
1060       _cfg.lut_type = 2;
1061       _cfg.creationtag = _tag;
1062       _cfg.creationstamp = get_time_stamp(time(nullptr));
1063       _cfg.targetfirmware = "1.0.0";
1064       _cfg.formatrevision = "1";  //???
1065       // "original" definition of GENERALIZEDINDEX from Mike Weinberger
1066       //   int generalizedIndex=id.ietaAbs()+10000*id.iphi()+
1067       //       ((id.ieta()<0)?(0):(100));
1068       _cfg.generalizedindex = _cfg.iphi * 10000 + (row->ieta > 0) * 100 + abs(row->ieta);
1069 
1070       // FIXME: work around bug in emap v6: rawId wasn't filled
1071       //HcalTrigTowerDetId _detid(row->rawId);
1072       HcalTrigTowerDetId _detid(row->ieta, row->iphi);
1073 
1074       _cfg.lut = _coder.getCompressionLUT(_detid);
1075 
1076       if (split_by_crate) {
1077         _xml[row->crate]->addLut(_cfg, lut_checksums_xml);
1078         _counter.count();
1079       } else {
1080         _xml[0]->addLut(_cfg, lut_checksums_xml);
1081         _counter.count();
1082       }
1083     }
1084   }
1085   edm::LogInfo("HcalLutManager") << "LUTs generated: " << _counter.getCount() << std::endl
1086                                  << "Generating compression (output) LUTs from CaloTPGTranscoderULUT...DONE"
1087                                  << std::endl;
1088   return _xml;
1089 }
1090 
1091 int HcalLutManager::writeLutXmlFiles(std::map<int, std::shared_ptr<LutXml>>& _xml,
1092                                      std::string _tag,
1093                                      bool split_by_crate) {
1094   for (std::map<int, std::shared_ptr<LutXml>>::const_iterator cr = _xml.begin(); cr != _xml.end(); cr++) {
1095     std::stringstream output_file_name;
1096     if (split_by_crate) {
1097       output_file_name << _tag << "_" << cr->first << ".xml";
1098     } else {
1099       output_file_name << _tag << ".xml";
1100     }
1101     cr->second->write(output_file_name.str());
1102   }
1103   return 0;
1104 }
1105 
1106 int HcalLutManager::createLinLutXmlFiles(std::string _tag, std::string _lin_file, bool split_by_crate) {
1107   //std::cout << "DEBUG1: split_by_crate = " << split_by_crate << std::endl;
1108   std::map<int, std::shared_ptr<LutXml>> xml;
1109   if (!lut_checksums_xml) {
1110     lut_checksums_xml = new XMLDOMBlock("CFGBrick", 1);
1111   }
1112 
1113   if (!_lin_file.empty()) {
1114     addLutMap(xml, getLinearizationLutXmlFromAsciiMasterEmap(_lin_file, _tag, -1, 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 int HcalLutManager::createAllLutXmlFiles(std::string _tag,
1125                                          std::string _lin_file,
1126                                          std::string _comp_file,
1127                                          bool split_by_crate) {
1128   //std::cout << "DEBUG1: split_by_crate = " << split_by_crate << std::endl;
1129   std::map<int, std::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     //addLutMap( xml, getLutXmlFromAsciiMaster( _lin_file, _tag, -1, split_by_crate ) );
1136     addLutMap(xml, getLinearizationLutXmlFromAsciiMasterEmap(_lin_file, _tag, -1, split_by_crate));
1137   }
1138   if (!_comp_file.empty()) {
1139     //std::cout << "DEBUG1!!!!" << std::endl;
1140     addLutMap(xml, getCompressionLutXmlFromAsciiMaster(_comp_file, _tag, -1, split_by_crate));
1141     //std::cout << "DEBUG2!!!!" << std::endl;
1142   }
1143   writeLutXmlFiles(xml, _tag, split_by_crate);
1144 
1145   std::string checksums_file = _tag + "_checksums.xml";
1146   lut_checksums_xml->write(checksums_file);
1147 
1148   return 0;
1149 }
1150 
1151 int HcalLutManager::createCompLutXmlFilesFromCoder(std::string _tag, bool split_by_crate) {
1152   //std::cout << "DEBUG1: split_by_crate = " << split_by_crate << std::endl;
1153   std::map<int, std::shared_ptr<LutXml>> xml;
1154   if (!lut_checksums_xml) {
1155     lut_checksums_xml = new XMLDOMBlock("CFGBrick", 1);
1156   }
1157 
1158   addLutMap(xml, getCompressionLutXmlFromCoder(_tag, split_by_crate));
1159 
1160   writeLutXmlFiles(xml, _tag, split_by_crate);
1161 
1162   std::string checksums_file = _tag + "_checksums.xml";
1163   lut_checksums_xml->write(checksums_file);
1164 
1165   return 0;
1166 }
1167 
1168 int HcalLutManager::createAllLutXmlFilesFromCoder(const HcalTPGCoder& _coder, std::string _tag, bool split_by_crate) {
1169   //std::cout << "DEBUG1: split_by_crate = " << split_by_crate << std::endl;
1170   std::map<int, std::shared_ptr<LutXml>> xml;
1171   if (!lut_checksums_xml) {
1172     lut_checksums_xml = new XMLDOMBlock("CFGBrick", 1);
1173   }
1174 
1175   //addLutMap( xml, getLinearizationLutXmlFromCoder( _coder, _tag, split_by_crate ) );
1176   addLutMap(xml, getLinearizationLutXmlFromCoderEmap(_coder, _tag, split_by_crate));
1177   addLutMap(xml, getCompressionLutXmlFromCoder(_tag, split_by_crate));
1178 
1179   writeLutXmlFiles(xml, _tag, split_by_crate);
1180 
1181   std::string checksums_file = _tag + "_checksums.xml";
1182   lut_checksums_xml->write(checksums_file);
1183 
1184   return 0;
1185 }
1186 
1187 //
1188 //_____ use this for creating a full set of LUTs ________________________
1189 //
1190 int HcalLutManager::createLutXmlFiles_HBEFFromCoder_HOFromAscii(std::string _tag,
1191                                                                 const HcalTPGCoder& _coder,
1192                                                                 const CaloTPGTranscoderULUT& _transcoder,
1193                                                                 std::string _lin_file,
1194                                                                 bool split_by_crate) {
1195   std::map<int, std::shared_ptr<LutXml>> xml;
1196   if (!lut_checksums_xml) {
1197     lut_checksums_xml = new XMLDOMBlock("CFGBrick", 1);
1198   }
1199 
1200   if (!_lin_file.empty()) {
1201     const std::map<int, std::shared_ptr<LutXml>> _lin_lut_ascii_xml =
1202         getLinearizationLutXmlFromAsciiMasterEmap(_lin_file, _tag, -1, split_by_crate);
1203     addLutMap(xml, _lin_lut_ascii_xml);
1204   }
1205   const std::map<int, std::shared_ptr<LutXml>> _lin_lut_xml =
1206       getLinearizationLutXmlFromCoderEmap(_coder, _tag, split_by_crate);
1207   addLutMap(xml, _lin_lut_xml);
1208   //
1209   const std::map<int, std::shared_ptr<LutXml>> _comp_lut_xml =
1210       getCompressionLutXmlFromCoder(_transcoder, _tag, split_by_crate);
1211   addLutMap(xml, _comp_lut_xml);
1212 
1213   const std::map<int, std::shared_ptr<LutXml>> _HE_FG_lut_xml = getHEFineGrainLUTs(_tag, split_by_crate);
1214   addLutMap(xml, _HE_FG_lut_xml);
1215 
1216   writeLutXmlFiles(xml, _tag, split_by_crate);
1217 
1218   std::string checksums_file = _tag + "_checksums.xml";
1219   lut_checksums_xml->write(checksums_file);
1220 
1221   return 0;
1222 }
1223 
1224 int HcalLutManager::createLutXmlFiles_HBEFFromCoder_HOFromAscii(std::string _tag,
1225                                                                 const HcalTPGCoder& _coder,
1226                                                                 std::string _lin_file,
1227                                                                 bool split_by_crate) {
1228   std::map<int, std::shared_ptr<LutXml>> xml;
1229   if (!lut_checksums_xml) {
1230     lut_checksums_xml = new XMLDOMBlock("CFGBrick", 1);
1231   }
1232 
1233   if (!_lin_file.empty()) {
1234     const std::map<int, std::shared_ptr<LutXml>> _lin_lut_ascii_xml =
1235         getLinearizationLutXmlFromAsciiMasterEmap(_lin_file, _tag, -1, split_by_crate);
1236     addLutMap(xml, _lin_lut_ascii_xml);
1237   }
1238   const std::map<int, std::shared_ptr<LutXml>> _lin_lut_xml =
1239       getLinearizationLutXmlFromCoderEmap(_coder, _tag, split_by_crate);
1240   addLutMap(xml, _lin_lut_xml);
1241   //
1242   const std::map<int, std::shared_ptr<LutXml>> _comp_lut_xml = getCompressionLutXmlFromCoder(_tag, split_by_crate);
1243   addLutMap(xml, _comp_lut_xml);
1244 
1245   writeLutXmlFiles(xml, _tag, split_by_crate);
1246 
1247   std::string checksums_file = _tag + "_checksums.xml";
1248   lut_checksums_xml->write(checksums_file);
1249 
1250   return 0;
1251 }
1252 
1253 // use this to create HBEF only from coders (physics LUTs)
1254 int HcalLutManager::createAllLutXmlFilesLinAsciiCompCoder(std::string _tag,
1255                                                           std::string _lin_file,
1256                                                           bool split_by_crate) {
1257   //std::cout << "DEBUG1: split_by_crate = " << split_by_crate << std::endl;
1258   std::map<int, std::shared_ptr<LutXml>> xml;
1259   if (!lut_checksums_xml) {
1260     lut_checksums_xml = new XMLDOMBlock("CFGBrick", 1);
1261   }
1262 
1263   if (!_lin_file.empty()) {
1264     addLutMap(xml, getLutXmlFromAsciiMaster(_lin_file, _tag, -1, split_by_crate));
1265   }
1266   addLutMap(xml, getCompressionLutXmlFromCoder(_tag, split_by_crate));
1267   writeLutXmlFiles(xml, _tag, split_by_crate);
1268 
1269   std::string checksums_file = _tag + "_checksums.xml";
1270   lut_checksums_xml->write(checksums_file);
1271 
1272   return 0;
1273 }
1274 
1275 void HcalLutManager::addLutMap(std::map<int, std::shared_ptr<LutXml>>& result,
1276                                const std::map<int, std::shared_ptr<LutXml>>& other) {
1277   for (std::map<int, std::shared_ptr<LutXml>>::const_iterator lut = other.begin(); lut != other.end(); lut++) {
1278     edm::LogInfo("HcalLutManager") << "Added LUTs for crate " << lut->first;
1279     if (result.count(lut->first) == 0) {
1280       result.insert(*lut);
1281     } else {
1282       *(result[lut->first]) += *(lut->second);
1283     }
1284   }
1285 }
1286 
1287 string HcalLutManager::get_time_stamp(time_t _time) {
1288   char timebuf[50];
1289   //strftime( timebuf, 50, "%c", gmtime( &_time ) );
1290   strftime(timebuf, 50, "%Y-%m-%d %H:%M:%S", gmtime(&_time));
1291   std::string creationstamp = timebuf;
1292 
1293   return creationstamp;
1294 }
1295 
1296 int HcalLutManager::test_xml_access(std::string _tag, std::string _filename) {
1297   local_connect(_filename, "backup/HCALmapHBEF.txt", "backup/HCALmapHO.txt");
1298 
1299   //EMap _emap("../../../CondFormats/HcalObjects/data/official_emap_v6.04_080905.txt");
1300   EMap _emap(emap);
1301   std::vector<EMap::EMapRow>& _map = _emap.get_map();
1302   int map_size = _map.size();
1303   edm::LogInfo("HcalLutManager") << "EMap contains " << map_size << " channels";
1304 
1305   // make sure that all init is done
1306   std::vector<unsigned int> _lut;
1307   _lut = getLutFromXml(_tag, 1107313727, hcal::ConfigurationDatabase::LinearizerLUT);
1308 
1309   edm::LogInfo("HcalLutManager") << "Testing direct parsing of the LUT XML";
1310   struct timeval _t;
1311   gettimeofday(&_t, nullptr);
1312   double _time = (double)(_t.tv_sec) + (double)(_t.tv_usec) / 1000000.0;
1313   test_direct_xml_parsing(_filename);
1314   gettimeofday(&_t, nullptr);
1315   edm::LogInfo("HcalLutManager") << "parsing took that much time: "
1316                                  << (double)(_t.tv_sec) + (double)(_t.tv_usec) / 1000000.0 - _time;
1317 
1318   gettimeofday(&_t, nullptr);
1319   _time = (double)(_t.tv_sec) + (double)(_t.tv_usec) / 1000000.0;
1320   edm::LogInfo("HcalLutManager") << "before loop over random LUTs: " << _time;
1321   int _raw_id;
1322 
1323   // loop over random LUTs
1324   for (int _iter = 0; _iter < 100; _iter++) {
1325     gettimeofday(&_t, nullptr);
1326     //std::cout << "before getting a LUT: " << _t . tv_sec << "." << _t . tv_usec << std::endl;
1327 
1328     // select valid random emap channel
1329     while (true) {
1330       int _key = (rand() % map_size);
1331       //_key = 3356;
1332       if ((_map[_key].subdet.find("HB") != string::npos || _map[_key].subdet.find("HE") != string::npos ||
1333            _map[_key].subdet.find("HO") != string::npos || _map[_key].subdet.find("HF") != string::npos) &&
1334           _map[_key].subdet.size() == 2) {
1335         HcalSubdetector _subdet;
1336         if (_map[_key].subdet.find("HB") != string::npos)
1337           _subdet = HcalBarrel;
1338         else if (_map[_key].subdet.find("HE") != string::npos)
1339           _subdet = HcalEndcap;
1340         else if (_map[_key].subdet.find("HO") != string::npos)
1341           _subdet = HcalOuter;
1342         else if (_map[_key].subdet.find("HF") != string::npos)
1343           _subdet = HcalForward;
1344         else
1345           _subdet = HcalOther;
1346         HcalDetId _detid(_subdet, _map[_key].ieta, _map[_key].iphi, _map[_key].idepth);
1347         _raw_id = _detid.rawId();
1348         break;
1349       }
1350     }
1351     _lut = getLutFromXml(_tag, _raw_id, hcal::ConfigurationDatabase::LinearizerLUT);
1352 
1353     gettimeofday(&_t, nullptr);
1354   }
1355   double d_time = _t.tv_sec + _t.tv_usec / 1000000.0 - _time;
1356   edm::LogInfo("HcalLutManager") << "after the loop over random LUTs: " << _time + d_time << std::endl
1357                                  << "total time: " << d_time << std::endl;
1358 
1359   edm::LogInfo("HcalLutManager") << "LUT length = " << _lut.size();
1360   for (std::vector<unsigned int>::const_iterator i = _lut.end() - 1; i != _lut.begin() - 1; i--) {
1361     edm::LogInfo("HcalLutManager") << (i - _lut.begin()) << "     " << _lut[(i - _lut.begin())];
1362     break;
1363   }
1364 
1365   db->disconnect();
1366 
1367   delete db;
1368   db = nullptr;
1369 
1370   return 0;
1371 }
1372 
1373 int HcalLutManager::read_lmap(std::string lmap_hbef_file, std::string lmap_ho_file) {
1374   delete lmap;
1375   lmap = new LMap();
1376   lmap->read(lmap_hbef_file, "HBEF");
1377   lmap->read(lmap_ho_file, "HO");
1378   edm::LogInfo("HcalLutManager") << "LMap contains " << lmap->get_map().size()
1379                                  << " channels (compare to 9072 of all HCAL channels)";
1380   return 0;
1381 }
1382 
1383 int HcalLutManager::read_luts(std::string lut_xml_file) {
1384   delete db;
1385   db = new HCALConfigDB();
1386   db->connect(lut_xml_file);
1387   return 0;
1388 }
1389 
1390 int HcalLutManager::local_connect(std::string lut_xml_file, std::string lmap_hbef_file, std::string lmap_ho_file) {
1391   read_lmap(lmap_hbef_file, lmap_ho_file);
1392   read_luts(lut_xml_file);
1393   return 0;
1394 }
1395 
1396 std::vector<unsigned int> HcalLutManager::getLutFromXml(std::string tag,
1397                                                         uint32_t _rawid,
1398                                                         hcal::ConfigurationDatabase::LUTType _lt) {
1399   edm::LogInfo("HcalLutManager") << "getLutFromXml (new version) is not implemented. Use getLutFromXml_old() for now";
1400 
1401   std::vector<unsigned int> result;
1402 
1403   return result;
1404 }
1405 
1406 // obsolete, use getLutFromXml() instead
1407 std::vector<unsigned int> HcalLutManager::getLutFromXml_old(std::string tag,
1408                                                             uint32_t _rawid,
1409                                                             hcal::ConfigurationDatabase::LUTType _lt) {
1410   if (!lmap) {
1411     edm::LogError("HcalLutManager") << "Cannot find LUT without LMAP, exiting...";
1412     exit(-1);
1413   }
1414   if (!db) {
1415     edm::LogError("HcalLutManager") << "Cannot find LUT, no source (local XML file), exiting...";
1416     exit(-1);
1417   }
1418 
1419   std::vector<unsigned int> result;
1420 
1421   std::map<int, LMapRow>& _map = lmap->get_map();
1422   //std::cout << "HcalLutManager: LMap contains " << _map . size() << " channels (out of 9072 total)" << std::endl;
1423 
1424   HcalDetId _id(_rawid);
1425 
1426   unsigned int _crate, _slot, _fiber, _channel;
1427   std::string _fpga;
1428   int topbottom, luttype;
1429 
1430   // FIXME: check validity of _rawid
1431   if (_map.find(_rawid) != _map.end()) {
1432     _crate = _map[_rawid].crate;
1433     _slot = _map[_rawid].htr;
1434     _fiber = _map[_rawid].htr_fi;
1435     _channel = _map[_rawid].fi_ch;
1436     _fpga = _map[_rawid].fpga;
1437 
1438     if (_fpga.find("top") != std::string::npos)
1439       topbottom = 1;
1440     else if (_fpga.find("bot") != std::string::npos)
1441       topbottom = 0;
1442     else {
1443       edm::LogError("HcalLutManager") << "Irregular LMAP fpga value... do not know what to do - exiting";
1444       exit(-1);
1445     }
1446     if (_lt == hcal::ConfigurationDatabase::LinearizerLUT)
1447       luttype = 1;
1448     else
1449       luttype = 2;
1450 
1451     result = db->getOnlineLUT(tag, _crate, _slot, topbottom, _fiber, _channel, luttype);
1452   }
1453 
1454   return result;
1455 }
1456 
1457 int HcalLutManager::get_xml_files_from_db(std::string tag, const std::string db_accessor, bool split_by_crate) {
1458   std::map<int, std::shared_ptr<LutXml>> lut_map = get_brickSet_from_oracle(tag, db_accessor);
1459   if (split_by_crate) {
1460     writeLutXmlFiles(lut_map, tag, split_by_crate);
1461   } else {
1462     LutXml result;
1463     for (std::map<int, std::shared_ptr<LutXml>>::const_iterator xml = lut_map.begin(); xml != lut_map.end(); xml++) {
1464       result += *(xml->second);
1465     }
1466     std::stringstream out_file;
1467     out_file << tag << ".xml";
1468     result.write(out_file.str());
1469   }
1470 
1471   return 0;
1472 }
1473 
1474 std::map<int, std::shared_ptr<LutXml>> HcalLutManager::get_brickSet_from_oracle(std::string tag,
1475                                                                                 const std::string _accessor) {
1476   HCALConfigDB* db = new HCALConfigDB();
1477   XMLProcessor::getInstance();  // initialize xerces-c engine
1478   //const std::string _accessor = "occi://CMS_HCL_PRTTYPE_HCAL_READER@anyhost/int2r?PASSWORD=HCAL_Reader_88,LHWM_VERSION=22";
1479   db->connect(_accessor);
1480   oracle::occi::Connection* _connection = db->getConnection();
1481 
1482   edm::LogInfo("HcalLutManager") << "Preparing to request the LUT CLOBs from the database...";
1483 
1484   //int crate = 0;
1485 
1486   //
1487   // _____ query is different for the old validation DB _________________
1488   //
1489   //std::string query = ("SELECT TRIG_PRIM_LOOKUPTBL_DATA_CLOB, CRATE FROM CMS_HCL_HCAL_CONDITION_OWNER.V_HCAL_TRIG_LOOKUP_TABLES");
1490   std::string query = ("SELECT TRIG_PRIM_LOOKUPTBL_DATA_CLOB, CRATE FROM CMS_HCL_HCAL_COND.V_HCAL_TRIG_LOOKUP_TABLES");
1491   //query+=toolbox::toString(" WHERE TAG_NAME='%s' AND CRATE=%d", tag.c_str(), crate);
1492   query += toolbox::toString(" WHERE TAG_NAME='%s'", tag.c_str());
1493 
1494   std::string brick_set;
1495 
1496   std::map<int, std::shared_ptr<LutXml>> lut_map;
1497 
1498   try {
1499     //SELECT
1500     edm::LogInfo("HcalLutManager") << "Executing the query...";
1501     Statement* stmt = _connection->createStatement();
1502     ResultSet* rs = stmt->executeQuery(query);
1503     edm::LogInfo("HcalLutManager") << "Executing the query... done";
1504 
1505     edm::LogInfo("HcalLutManager") << "Processing the query results...";
1506     //RooGKCounter _lines;
1507     while (rs->next()) {
1508       //_lines.count();
1509       oracle::occi::Clob clob = rs->getClob(1);
1510       int crate = rs->getInt(2);
1511       if (crate != -1) {  // not a brick with checksums
1512         edm::LogInfo("HcalLutManager") << "Getting LUTs for crate #" << crate << " out of the database...";
1513         brick_set = db->clobToString(clob);
1514         /*
1515     // FIXME: DEBUG lut xml files from simple strings
1516     stringstream file_name;
1517     ofstream out_file;
1518     file_name << tag << "_" << crate << "_debug" << ".xml";
1519     out_file . open( file_name.str().c_str() );
1520     out_file << brick_set;
1521     out_file . close();
1522     */
1523         const char* bs = brick_set.c_str();
1524         MemBufInputSource* lut_clob = new MemBufInputSource((const XMLByte*)bs, strlen(bs), "lut_clob", false);
1525         std::shared_ptr<LutXml> lut_xml = std::make_shared<LutXml>(*lut_clob);
1526         lut_map[crate] = lut_xml;
1527         edm::LogInfo("HcalLutManager") << "done";
1528       }
1529     }
1530     //Always terminate statement
1531     _connection->terminateStatement(stmt);
1532     //std::cout << "Query line count: " << _lines.getCount() << std::endl;
1533   } catch (SQLException& e) {
1534     XCEPT_RAISE(hcal::exception::ConfigurationDatabaseException,
1535                 ::toolbox::toString("Oracle  exception : %s", e.getMessage().c_str()));
1536   }
1537 
1538   //std::cout << lut_map.size() << std::endl;
1539 
1540   db->disconnect();
1541   //delete db;
1542   return lut_map;
1543 }
1544 
1545 int HcalLutManager::create_lut_loader(std::string file_list,
1546                                       std::string _prefix,
1547                                       std::string tag_name,
1548                                       std::string comment,
1549                                       std::string version,
1550                                       int subversion) {
1551   edm::LogInfo("HcalLutManager") << "Generating XML loader for LUTs...";
1552   //std::cout << _prefix << "..." << tag_name << std::endl;
1553 
1554   XMLLUTLoader::loaderBaseConfig baseConf;
1555   XMLLUTLoader::lutDBConfig conf;
1556   XMLLUTLoader::checksumsDBConfig CSconf;
1557 
1558   baseConf.tag_name = tag_name;
1559   //baseConf . comment_description = tag_name;
1560   baseConf.comment_description = comment;
1561   baseConf.iov_begin = "1";
1562   baseConf.iov_end = "-1";
1563 
1564   conf.version = version;
1565 
1566   std::stringstream _subversion;
1567   _subversion << subversion;
1568   conf.subversion = _subversion.str();
1569 
1570   CSconf.version = conf.version;
1571   CSconf.subversion = conf.subversion;
1572   CSconf.trig_prim_lookuptbl_data_file = _prefix + "_checksums.xml.dat";
1573   CSconf.comment_description = tag_name;
1574 
1575   XMLLUTLoader doc(&baseConf);
1576 
1577   std::vector<int> crate_number;
1578   std::vector<std::string> file_name = HcalQIEManager::splitString(file_list);
1579   for (std::vector<std::string>::const_iterator _f = file_name.begin(); _f != file_name.end(); _f++) {
1580     int crate_begin = _f->rfind("_");
1581     int crate_end = _f->rfind(".xml.dat");
1582     crate_number.push_back(getInt(_f->substr(crate_begin + 1, crate_end - crate_begin - 1)));
1583   }
1584   //
1585   //_____ fix due to the new convention: version/subversion combo must be unique for every payload
1586   //
1587   char _buf[128];
1588   time_t _offset = time(nullptr);
1589   sprintf(_buf, "%d", (uint32_t)_offset);
1590   conf.version.append(".");
1591   conf.version.append(_buf);
1592   CSconf.version = conf.version;
1593   //
1594   for (std::vector<std::string>::const_iterator _file = file_name.begin(); _file != file_name.end(); _file++) {
1595     conf.trig_prim_lookuptbl_data_file = *_file;
1596     //conf . trig_prim_lookuptbl_data_file += ".dat";
1597     conf.crate = crate_number[_file - file_name.begin()];
1598     //
1599     //_____ fix due to the new convention: version/subversion combo must be unique for every payload
1600     //
1601     sprintf(_buf, "%.2d", conf.crate);
1602     conf.subversion.clear();
1603     conf.subversion.append(_buf);
1604     sprintf(_buf, "CRATE%.2d", conf.crate);
1605     std::string _namelabel;
1606     _namelabel.append(_buf);
1607     conf.name_label = _namelabel;
1608     doc.addLUT(&conf);
1609   }
1610 
1611   doc.addChecksums(&CSconf);
1612   //doc . write( _prefix + "_Loader.xml" );
1613   doc.write(tag_name + "_Loader.xml");
1614 
1615   edm::LogInfo("HcalLutManager") << "Generating XML loader for LUTs... done.";
1616 
1617   return 0;
1618 }
1619 
1620 void HcalLutManager::test_emap(void) {
1621   //EMap _emap("../../../CondFormats/HcalObjects/data/official_emap_v5_080208.txt");
1622   //EMap _emap("../../../CondFormats/HcalObjects/data/official_emap_v6.03_080817.txt");
1623   //EMap _emap("../../../CondFormats/HcalObjects/data/official_emap_v6.04_080905.txt");
1624   EMap _emap(emap);
1625   std::vector<EMap::EMapRow>& _map = _emap.get_map();
1626   std::stringstream s;
1627   s << "EMap contains " << _map.size() << " channels" << std::endl;
1628 
1629   //loop over all EMap channels
1630   //RooGKCounter _c;
1631   for (std::vector<EMap::EMapRow>::const_iterator row = _map.begin(); row != _map.end(); row++) {
1632     // only trigger tower channels
1633     if (row->subdet.find("HT") != std::string::npos) {
1634       s << " -----> Subdet = " << row->subdet << std::endl;
1635 
1636       if (abs(row->ieta) > 28) {
1637         //if (row->iphi == 71){
1638         s << " ==> (ieta,iphi) = " << row->ieta << ",   " << row->iphi << std::endl;
1639       }
1640     }
1641   }
1642   edm::LogInfo("HcalLutManager") << s.str();
1643 }
1644 
1645 int HcalLutManager::test_direct_xml_parsing(std::string _filename) {
1646   /*
1647   XMLDOMBlock _xml(_filename);
1648   //DOMElement * data_set_elem = (DOMElement *)(document -> getElementsByTagName( XMLProcessor::_toXMLCh( "DATA_SET" ) ) -> item(0));
1649   DOMNodeList * brick_list = _xml . getDocument() ->  getElementsByTagName( XMLProcessor::_toXMLCh( "CFGBrick" ));
1650 
1651   double n_bricks = brick_list->getLength();
1652   std::cout << "amount of LUT bricks: " << n_bricks << std::endl;
1653 
1654   for (int iter=0; iter!=n_bricks; iter++){
1655     DOMElement * _brick = (DOMElement *)(brick_list->item(iter));
1656 
1657     DOMElement * _param = 0;
1658     // loop over brick parameters
1659     int par_iter = 0;
1660     while(1){
1661       _param = (DOMElement *)(_brick->getElementsByTagName(XMLProcessor::_toXMLCh("Parameter")));
1662       std::string _name = _param->getAttribute( XMLProcessor::_toXMLCh( "name" ) );
1663       if (_name.find("IETA")==string::npos) break;
1664 
1665       std::string _tag = "Parameter";
1666       std::cout << "### Parameter IETA = " << _xml.getTagValue( _tag, 0, _brick);
1667       par_iter++;
1668     }
1669   }
1670   */
1671   return 0;
1672 }
1673 
1674 //
1675 //_____ attempt to include ZDC LUTs _____________________________________
1676 //
1677 int HcalLutManager::createLutXmlFiles_HBEFFromCoder_HOFromAscii_ZDC(std::string _tag,
1678                                                                     const HcalTPGCoder& _coder,
1679                                                                     const CaloTPGTranscoderULUT& _transcoder,
1680                                                                     std::string _lin_file,
1681                                                                     bool split_by_crate) {
1682   std::map<int, std::shared_ptr<LutXml>> xml;
1683   if (!lut_checksums_xml) {
1684     lut_checksums_xml = new XMLDOMBlock("CFGBrick", 1);
1685   }
1686 
1687   if (!_lin_file.empty()) {
1688     const std::map<int, std::shared_ptr<LutXml>> _lin_lut_ascii_xml =
1689         getLinearizationLutXmlFromAsciiMasterEmap(_lin_file, _tag, -1, split_by_crate);
1690     addLutMap(xml, _lin_lut_ascii_xml);
1691   }
1692   const std::map<int, std::shared_ptr<LutXml>> _lin_lut_xml =
1693       getLinearizationLutXmlFromCoderEmap(_coder, _tag, split_by_crate);
1694   addLutMap(xml, _lin_lut_xml);
1695   //
1696   const std::map<int, std::shared_ptr<LutXml>> _comp_lut_xml =
1697       getCompressionLutXmlFromCoder(_transcoder, _tag, split_by_crate);
1698   addLutMap(xml, _comp_lut_xml);
1699 
1700   const std::map<int, std::shared_ptr<LutXml>> _HE_FG_lut_xml = getHEFineGrainLUTs(_tag, split_by_crate);
1701   addLutMap(xml, _HE_FG_lut_xml);
1702 
1703   for (auto masktype : {0, 1, 2}) {
1704     const auto masks = getMasks(masktype, _tag, split_by_crate);
1705     addLutMap(xml, masks);
1706   }
1707   //
1708   const auto _zdc_lut_xml = getZdcLutXml(_coder, _tag, split_by_crate, false);
1709   addLutMap(xml, _zdc_lut_xml);
1710 
1711   const auto _zdc_ootpu_lut_xml = getZdcLutXml(_coder, _tag, split_by_crate, true);
1712   addLutMap(xml, _zdc_ootpu_lut_xml);
1713 
1714   writeLutXmlFiles(xml, _tag, split_by_crate);
1715 
1716   std::string checksums_file = _tag + "_checksums.xml";
1717   lut_checksums_xml->write(checksums_file);
1718 
1719   return 0;
1720 }
1721 
1722 std::map<int, std::shared_ptr<LutXml>> HcalLutManager::getZdcLutXml(const HcalTPGCoder& _coder,
1723                                                                     std::string _tag,
1724                                                                     bool split_by_crate,
1725                                                                     bool ootpu_lut) {
1726   edm::LogInfo("HcalLutManager") << "Generating ZDC LUTs ...may the Force be with us...";
1727   std::map<int, std::shared_ptr<LutXml>> _xml;  // index - crate number
1728 
1729   EMap _emap(emap);
1730 
1731   std::vector<EMap::EMapRow>& _map = _emap.get_map();
1732   edm::LogInfo("HcalLutManager") << "EMap contains " << _map.size() << " channels";
1733 
1734   //loop over all EMap channels
1735   RooGKCounter _counter;
1736   for (std::vector<EMap::EMapRow>::const_iterator row = _map.begin(); row != _map.end(); row++) {
1737     LutXml::Config _cfg;
1738 
1739     // only ZDC channels
1740     if (row->zdc_section.find("ZDC") != std::string::npos) {
1741       if (_xml.count(row->crate) == 0 && split_by_crate) {
1742         _xml.insert(std::pair<int, std::shared_ptr<LutXml>>(row->crate, std::make_shared<LutXml>()));
1743       } else if (_xml.count(0) == 0 && !split_by_crate) {
1744         _xml.insert(std::pair<int, std::shared_ptr<LutXml>>(0, std::make_shared<LutXml>()));
1745       }
1746       //  FIXME: introduce proper tag names in ZDC bricks for logical channel info
1747       _cfg.ieta = row->zdc_channel;  // int
1748       //_cfg.ieta = row->zdc_zside; // int
1749       //_cfg.iphi = row->zdc_section; // string
1750       _cfg.depth = row->zdc_zside;  // int
1751       _cfg.crate = row->crate;
1752       _cfg.slot = row->slot;
1753       if (row->topbottom.find('t') != std::string::npos)
1754         _cfg.topbottom = 1;
1755       else if (row->topbottom.find('b') != std::string::npos)
1756         _cfg.topbottom = 0;
1757       else if (row->topbottom.find('u') != std::string::npos)
1758         _cfg.topbottom = 2;
1759       else
1760         edm::LogWarning("HcalLutManager") << "fpga out of range...";
1761 
1762       if (ootpu_lut)
1763         _cfg.fiber = row->fiber + 6;
1764       else
1765         _cfg.fiber = row->fiber;
1766 
1767       _cfg.fiberchan = row->fiberchan;
1768       _cfg.lut_type = 1;
1769       _cfg.creationtag = _tag;
1770       _cfg.creationstamp = get_time_stamp(time(nullptr));
1771       _cfg.targetfirmware = "1.0.0";
1772       _cfg.formatrevision = "1";  //???
1773       _cfg.generalizedindex = 0;
1774 
1775       HcalZDCDetId::Section section = HcalZDCDetId::Unknown;
1776       if (row->zdc_section == "ZDC EM") {
1777         section = HcalZDCDetId::EM;
1778         _cfg.iphi = 1;
1779       } else if (row->zdc_section == "ZDC HAD") {
1780         section = HcalZDCDetId::HAD;
1781         _cfg.iphi = 2;
1782       } else if (row->zdc_section == "ZDC LUM") {
1783         continue;
1784       } else if (row->zdc_section == "ZDC RPD") {
1785         continue;
1786       }
1787       HcalZDCDetId _detid(section, (row->zdc_zside > 0), row->zdc_channel);
1788 
1789       for (const auto i : _coder.getLinearizationLUT(_detid, ootpu_lut)) {
1790         _cfg.lut.push_back(i);
1791       }
1792 
1793       if (split_by_crate) {
1794         _xml[row->crate]->addLut(_cfg, lut_checksums_xml);
1795         _counter.count();
1796       } else {
1797         _xml[0]->addLut(_cfg, lut_checksums_xml);
1798         _counter.count();
1799       }
1800       //size of lut
1801     }
1802   }
1803 
1804   edm::LogInfo("HcalLutManager") << "LUTs generated: " << _counter.getCount() << std::endl
1805                                  << "Generating ZDC LUTs...DONE" << std::endl;
1806 
1807   return _xml;
1808 }