CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_2_9_HLT1_bphpatch4/src/CaloOnlineTools/HcalOnlineDb/src/HcalLutManager.cc

Go to the documentation of this file.
00001 #include <fstream>
00002 #include <sstream>
00003 #include <sys/time.h>
00004 #include <stdlib.h>
00005 #include <sys/types.h>
00006 #include <sys/stat.h>
00007 #include <unistd.h>
00008 
00009 #include <cstdlib>  // For srand() and rand()
00010 
00011 #include "xgi/Utils.h"
00012 #include "toolbox/string.h"
00013 #include "OnlineDB/Oracle/interface/Oracle.h"
00014 
00015 #include "CaloOnlineTools/HcalOnlineDb/interface/HcalLutManager.h"
00016 #include "CaloOnlineTools/HcalOnlineDb/interface/ZdcLut.h"
00017 #include "CalibCalorimetry/HcalTPGAlgos/interface/XMLProcessor.h"
00018 #include "CalibCalorimetry/HcalTPGAlgos/interface/XMLDOMBlock.h"
00019 #include "CaloOnlineTools/HcalOnlineDb/interface/HcalQIEManager.h"
00020 #include "CaloOnlineTools/HcalOnlineDb/interface/LMap.h"
00021 #include "CaloOnlineTools/HcalOnlineDb/interface/XMLLUTLoader.h"
00022 #include "CaloOnlineTools/HcalOnlineDb/interface/RooGKCounter.h"
00023 #include "DataFormats/HcalDetId/interface/HcalDetId.h"
00024 
00025 #include "DataFormats/HcalDetId/interface/HcalSubdetector.h"
00026 
00027 using namespace std;
00028 using namespace oracle::occi;
00029 using namespace hcal;
00030 
00039 HcalLutManager::HcalLutManager( void )
00040 {    
00041   init();
00042 }
00043 
00044 
00045 HcalLutManager::HcalLutManager(std::vector<HcalGenericDetId> & map)
00046 {
00047   init();
00048   _iter . init(map);
00049 }
00050 
00051 
00052 HcalLutManager::HcalLutManager(const HcalElectronicsMap * _emap,
00053                                const HcalChannelQuality * _cq,
00054                                uint32_t _status_word_to_mask)
00055 {
00056   init();
00057   emap = _emap;
00058   cq   = _cq;
00059   status_word_to_mask = _status_word_to_mask;
00060 }
00061 
00062 
00063 void HcalLutManager::init( void )
00064 {    
00065   lut_xml = 0;
00066   lut_checksums_xml = 0;
00067   db = 0;
00068   lmap = 0;
00069   emap = 0;
00070   cq   = 0;
00071   status_word_to_mask = 0x0000;
00072 }
00073 
00074 
00075 
00076 HcalLutManager::~HcalLutManager( void )
00077 {    
00078   delete lut_xml;
00079   delete lut_checksums_xml;
00080   delete db;
00081   delete lmap;
00082 }
00083 
00084 
00085 int HcalLutManager::initChannelIterator(std::vector<HcalGenericDetId> & map)
00086 {
00087   _iter . init(map);
00088   return _iter.size();
00089 }
00090 
00091 
00092 std::string & HcalLutManager::getLutXml( std::vector<unsigned int> & _lut )
00093 {
00094 
00095   if (lut_xml) delete lut_xml;
00096 
00097   lut_xml = new LutXml();
00098 
00099   LutXml::Config _config;
00100   _config.lut = _lut;
00101   lut_xml -> addLut( _config );
00102   lut_xml -> addLut( _config );
00103   lut_xml -> addLut( _config );
00104 
00105   //return lut_xml->getString();
00106   return lut_xml->getCurrentBrick();
00107 
00108 }
00109 
00110 
00111 int HcalLutManager::getInt( std::string number )
00112 {
00113   int result;
00114   sscanf(number.c_str(), "%d", &result);
00115   return result;
00116 }
00117 
00118 HcalSubdetector HcalLutManager::get_subdetector( std::string _det )
00119 {
00120   HcalSubdetector result;
00121   if      ( _det.find("HB") != std::string::npos ) result = HcalBarrel;
00122   else if ( _det.find("HE") != std::string::npos ) result = HcalEndcap;
00123   else if ( _det.find("HF") != std::string::npos ) result = HcalForward;
00124   else if ( _det.find("HO") != std::string::npos ) result = HcalOuter;
00125   else                                        result = HcalOther;  
00126 
00127   return result;
00128 }
00129 
00130 
00131 int HcalLutManager_test::getLutSetFromFile_test( std::string _filename )
00132 {
00133   HcalLutManager _manager;
00134   HcalLutSet _set = _manager . getLutSetFromFile( _filename );
00135   std::cout << "===> Test of HcalLutSet HcalLutManager::getLutSetFromFile( std::string _filename )" << std::endl << std::endl;
00136   std::cout << _set . label << std::endl;
00137   for (unsigned int i = 0; i != _set.subdet.size(); i++) std::cout << _set.subdet[i] << "       ";
00138   std::cout << std::endl;
00139   for (unsigned int i = 0; i != _set.eta_min.size(); i++) std::cout << _set.eta_min[i] << "     ";
00140   std::cout << std::endl;
00141   for (unsigned int i = 0; i != _set.eta_max.size(); i++) std::cout << _set.eta_max[i] << "     ";
00142   std::cout << std::endl;
00143   for (unsigned int i = 0; i != _set.phi_min.size(); i++) std::cout << _set.phi_min[i] << "     ";
00144   std::cout << std::endl;
00145   for (unsigned int i = 0; i != _set.phi_max.size(); i++) std::cout << _set.phi_max[i] << "     ";
00146   std::cout << std::endl;
00147   for (unsigned int i = 0; i != _set.depth_min.size(); i++) std::cout << _set.depth_min[i] << " ";
00148   std::cout << std::endl;
00149   for (unsigned int i = 0; i != _set.depth_max.size(); i++) std::cout << _set.depth_max[i] << " ";
00150   std::cout << std::endl;
00151   for (unsigned int j = 0; j != _set.lut[0].size(); j++){
00152     for (unsigned int i = 0; i != _set.lut.size(); i++){
00153       std::cout << _set.lut[i][j] << "  ";
00154     }
00155     std::cout << "---> " << j << std::endl;
00156   }
00157   return 0;
00158 }
00159 
00160 
00161 HcalLutSet HcalLutManager::getLutSetFromFile( std::string _filename, int _type )
00162 {
00163   HcalLutSet _lutset;
00164 
00165   ifstream infile( _filename . c_str() );
00166   std::string buf;
00167 
00168   if ( infile . is_open() ){
00169     std::cout << "File " << _filename << " is open..." << std::endl;
00170     std::cout << "Reading LUTs and their eta/phi/depth/subdet ranges...";
00171 
00172     // get label
00173     getline( infile, _lutset . label );
00174 
00175     if ( _type == 1 ){ // for linearization LUTs get subdetectors (default)
00176       //get subdetectors
00177       getline( infile, buf );
00178       _lutset . subdet = HcalQIEManager::splitString( buf );
00179     }
00180 
00181     //get min etas
00182     std::vector<std::string> buf_vec;
00183     getline( infile, buf );
00184     buf_vec = HcalQIEManager::splitString( buf );
00185     for (std::vector<std::string>::const_iterator iter = buf_vec.begin(); iter != buf_vec.end(); iter++){
00186       _lutset.eta_min.push_back(HcalLutManager::getInt(*iter));
00187     }
00188 
00189     //get max etas
00190     getline( infile, buf );
00191     buf_vec = HcalQIEManager::splitString( buf );
00192     for (std::vector<std::string>::const_iterator iter = buf_vec.begin(); iter != buf_vec.end(); iter++){
00193       _lutset.eta_max.push_back(HcalLutManager::getInt(*iter));
00194     }
00195 
00196     //get min phis
00197     getline( infile, buf );
00198     buf_vec = HcalQIEManager::splitString( buf );
00199     for (std::vector<std::string>::const_iterator iter = buf_vec.begin(); iter != buf_vec.end(); iter++){
00200       _lutset.phi_min.push_back(HcalLutManager::getInt(*iter));
00201     }
00202 
00203     //get max phis
00204     getline( infile, buf );
00205     buf_vec = HcalQIEManager::splitString( buf );
00206     for (std::vector<std::string>::const_iterator iter = buf_vec.begin(); iter != buf_vec.end(); iter++){
00207       _lutset.phi_max.push_back(HcalLutManager::getInt(*iter));
00208     }
00209 
00210     if ( _type == 1 ){ // for linearization LUTs get depth range (default)
00211       //get min depths
00212       getline( infile, buf );
00213       buf_vec = HcalQIEManager::splitString( buf );
00214       for (std::vector<std::string>::const_iterator iter = buf_vec.begin(); iter != buf_vec.end(); iter++){
00215         _lutset.depth_min.push_back(HcalLutManager::getInt(*iter));
00216       }
00217       
00218       //get max depths
00219       getline( infile, buf );
00220       buf_vec = HcalQIEManager::splitString( buf );
00221       for (std::vector<std::string>::const_iterator iter = buf_vec.begin(); iter != buf_vec.end(); iter++){
00222         _lutset.depth_max.push_back(HcalLutManager::getInt(*iter));
00223       }
00224     }
00225 
00226     bool first_lut_entry = true;
00227     while ( getline( infile, buf ) > 0 ){
00228       buf_vec = HcalQIEManager::splitString( buf );
00229       for (unsigned int i = 0; i < buf_vec.size(); i++){
00230         if (first_lut_entry){
00231           std::vector<unsigned int> _l;
00232           _lutset.lut.push_back(_l);
00233         }
00234         _lutset . lut[i] . push_back(HcalLutManager::getInt(buf_vec[i]));
00235       }
00236       first_lut_entry = false;
00237     }
00238   }
00239 
00240   std::cout << "done." << std::endl;
00241 
00242   return _lutset;
00243 }
00244 
00245 
00246 
00247 std::map<int, boost::shared_ptr<LutXml> > HcalLutManager::getLutXmlFromAsciiMaster( std::string _filename, std::string _tag, int _crate, bool split_by_crate )
00248 {
00249   std::cout << "Generating linearization (input) LUTs from ascii master file..." << std::endl;
00250   std::map<int, boost::shared_ptr<LutXml> > _xml; // index - crate number
00251 
00252   LMap _lmap;
00253   _lmap . read( "./backup/HCALmapHBEF.txt", "HBEF" );
00254   _lmap . read( "./backup/HCALmapHO.txt", "HO" );
00255   std::map<int,LMapRow> & _map = _lmap.get_map();
00256   std::cout << "LMap contains " << _map . size() << " channels" << std::endl;
00257 
00258   // read LUTs and their eta/phi/depth/subdet ranges
00259   HcalLutSet _set = getLutSetFromFile( _filename );
00260   int lut_set_size = _set.lut.size(); // number of different luts
00261 
00262   RooGKCounter _counter;
00263   //loop over all HCAL channels
00264   for( std::map<int,LMapRow>::const_iterator row=_map.begin(); row!=_map.end(); row++ ){
00265     LutXml::Config _cfg;
00266 
00267     // search for the correct LUT for a given channel,
00268     // higher LUT numbers have priority in case of overlapping
00269     int lut_index=-1;
00270     for ( int i=0; i<lut_set_size; i++ ){
00271       if ( (row->second.crate == _crate || _crate == -1) && // -1 stands for all crates
00272            _set.eta_min[i] <= row->second.side*row->second.eta &&
00273            _set.eta_max[i] >= row->second.side*row->second.eta &&
00274            _set.phi_min[i] <= row->second.phi &&
00275            _set.phi_max[i] >= row->second.phi &&
00276            _set.depth_min[i] <= row->second.depth &&
00277            _set.depth_max[i] >= row->second.depth &&
00278            get_subdetector(_set.subdet[i]) == row->second.det ){
00279         lut_index=i;
00280       }
00281     }
00282     if ( lut_index >= 0 ){
00283       if ( _xml.count(row->second.crate) == 0 && split_by_crate ){
00284         _xml.insert( std::pair<int,boost::shared_ptr<LutXml> >(row->second.crate,boost::shared_ptr<LutXml>(new LutXml())) );
00285       }
00286       else if ( _xml.count(0) == 0 && !split_by_crate ){
00287         _xml.insert( std::pair<int,boost::shared_ptr<LutXml> >(0,boost::shared_ptr<LutXml>(new LutXml())) );
00288       }
00289       _cfg.ieta = row->second.side*row->second.eta;
00290       _cfg.iphi = row->second.phi;
00291       _cfg.depth = row->second.depth;
00292       _cfg.crate = row->second.crate;
00293       _cfg.slot = row->second.htr;
00294       if (row->second.fpga . find("top") != std::string::npos) _cfg.topbottom = 1;
00295       else if (row->second.fpga . find("bot") != std::string::npos) _cfg.topbottom = 0;
00296       else std::cout << "Warning! fpga out of range..." << std::endl;
00297       // FIXME: probably fixed. fiber==htr_fi, not rm_fi in LMAP notation.
00298       //_cfg.fiber = row->second.rm_fi;
00299       _cfg.fiber = row->second.htr_fi;
00300       _cfg.fiberchan = row->second.fi_ch;
00301       if (_set.lut[lut_index].size() == 128) _cfg.lut_type = 1;
00302       else _cfg.lut_type = 2;
00303       _cfg.creationtag = _tag;
00304       _cfg.creationstamp = get_time_stamp( time(0) );
00305       _cfg.targetfirmware = "1.0.0";
00306       _cfg.formatrevision = "1"; //???
00307       // "original" definition of GENERALIZEDINDEX from Mike Weinberger
00308       //    int generalizedIndex=id.ietaAbs()+1000*id.depth()+10000*id.iphi()+
00309       //        ((id.ieta()<0)?(0):(100))+((id.subdet()==HcalForward && id.ietaAbs()==29)?(4*10000):(0));
00310       _cfg.generalizedindex =
00311         _cfg.iphi*10000 + _cfg.depth*1000 +
00312         (row->second.side>0)*100 + row->second.eta +
00313         ((row->second.det==HcalForward && row->second.eta==29)?(4*10000):(0));
00314       _cfg.lut = _set.lut[lut_index];
00315       if (split_by_crate ){
00316         _xml[row->second.crate]->addLut( _cfg, lut_checksums_xml );  
00317         _counter.count();
00318       }
00319       else{
00320         _xml[0]->addLut( _cfg, lut_checksums_xml );  
00321         _counter.count();
00322       }
00323     }
00324   }
00325   std::cout << "LUTs generated: " << _counter.getCount() << std::endl;
00326   std::cout << "Generating linearization (input) LUTs from ascii master file...DONE" << std::endl;
00327   return _xml;
00328 }
00329 
00330 //
00331 //_____ get HO from ASCII master here ___________________________________
00332 //
00333 std::map<int, boost::shared_ptr<LutXml> > HcalLutManager::getLinearizationLutXmlFromAsciiMasterEmap( std::string _filename, std::string _tag, int _crate, bool split_by_crate )
00334 {
00335   std::cout << "Generating linearization (input) LUTs from ascii master file..." << std::endl;
00336   std::map<int, boost::shared_ptr<LutXml> > _xml; // index - crate number
00337 
00338   EMap _emap(emap);
00339   std::vector<EMap::EMapRow> & _map = _emap.get_map();
00340   std::cout << "EMap contains " << _map . size() << " entries" << std::endl;
00341 
00342   // read LUTs and their eta/phi/depth/subdet ranges
00343   HcalLutSet _set = getLutSetFromFile( _filename );
00344   int lut_set_size = _set.lut.size(); // number of different luts
00345   std::cout << "  ==> " << lut_set_size << " sets of different LUTs read from the master file" << std::endl;
00346 
00347   // setup "zero" LUT for channel masking
00348   std::vector<unsigned int> zeroLut;
00349   for (size_t adc = 0; adc < 128; adc++) zeroLut.push_back(0);
00350 
00351   RooGKCounter _counter;
00352   //loop over all EMap channels
00353   for( std::vector<EMap::EMapRow>::const_iterator row=_map.begin(); row!=_map.end(); row++ ){
00354     if( (row->subdet.find("HB")!=string::npos ||
00355          row->subdet.find("HE")!=string::npos ||
00356          row->subdet.find("HO")!=string::npos ||
00357          row->subdet.find("HF")!=string::npos ) &&
00358         row->subdet.size()==2
00359         ){
00360       LutXml::Config _cfg;
00361       
00362       // search for the correct LUT for a given channel,
00363       // higher LUT numbers have priority in case of overlapping
00364       int lut_index=-1;
00365       for ( int i=0; i<lut_set_size; i++ ){
00366         if ( (row->crate == _crate || _crate == -1) && // -1 stands for all crates
00367              _set.eta_min[i] <= row->ieta &&
00368              _set.eta_max[i] >= row->ieta &&
00369              _set.phi_min[i] <= row->iphi &&
00370              _set.phi_max[i] >= row->iphi &&
00371              _set.depth_min[i] <= row->idepth &&
00372              _set.depth_max[i] >= row->idepth &&
00373              _set.subdet[i].find(row->subdet)!=string::npos ){
00374           lut_index=i;
00375         }
00376       }
00377       if ( lut_index >= 0 ){
00378         if ( _xml.count(row->crate) == 0 && split_by_crate ){
00379           _xml.insert( std::pair<int,boost::shared_ptr<LutXml> >(row->crate,boost::shared_ptr<LutXml>(new LutXml())) );
00380         }
00381         else if ( _xml.count(0) == 0 && !split_by_crate ){
00382           _xml.insert( std::pair<int,boost::shared_ptr<LutXml> >(0,boost::shared_ptr<LutXml>(new LutXml())) );
00383         }
00384         _cfg.ieta = row->ieta;
00385         _cfg.iphi = row->iphi;
00386         _cfg.depth = row->idepth;
00387         _cfg.crate = row->crate;
00388         _cfg.slot = row->slot;
00389         if (row->topbottom . find("t") != std::string::npos) _cfg.topbottom = 1;
00390         else if (row->topbottom . find("b") != std::string::npos) _cfg.topbottom = 0;
00391         else std::cout << "Warning! fpga out of range..." << std::endl;
00392         _cfg.fiber = row->fiber;
00393         _cfg.fiberchan = row->fiberchan;
00394         _cfg.lut_type = 1;
00395         _cfg.creationtag = _tag;
00396         _cfg.creationstamp = get_time_stamp( time(0) );
00397         _cfg.targetfirmware = "1.0.0";
00398         _cfg.formatrevision = "1"; //???
00399         // "original" definition of GENERALIZEDINDEX from Mike Weinberger
00400         //    int generalizedIndex=id.ietaAbs()+1000*id.depth()+10000*id.iphi()+
00401         //        ((id.ieta()<0)?(0):(100))+((id.subdet()==HcalForward && id.ietaAbs()==29)?(4*10000):(0));
00402         _cfg.generalizedindex =
00403           _cfg.iphi*10000 + _cfg.depth*1000 +
00404           (row->ieta>0)*100 + abs(row->ieta) +
00405           (((row->subdet.find("HF")!=string::npos) && abs(row->ieta)==29)?(4*10000):(0));
00406         //
00407         // consider channel status here
00408         DetId _detId(row->rawId);
00409         uint32_t status_word = cq->getValues(_detId)->getValue();
00410         if ((status_word & status_word_to_mask) > 0){
00411           _cfg.lut = zeroLut;
00412         }
00413         else{
00414           _cfg.lut = _set.lut[lut_index];
00415         }
00416         if (split_by_crate ){
00417           _xml[row->crate]->addLut( _cfg, lut_checksums_xml );  
00418           _counter.count();
00419         }
00420         else{
00421           _xml[0]->addLut( _cfg, lut_checksums_xml );  
00422           _counter.count();
00423         }
00424       }
00425     }
00426   }
00427   std::cout << "LUTs generated: " << _counter.getCount() << std::endl;
00428   std::cout << "Generating linearization (input) LUTs from ascii master file...DONE" << std::endl;
00429   return _xml;
00430 }
00431 
00432 
00433 std::map<int, boost::shared_ptr<LutXml> > HcalLutManager::getLinearizationLutXmlFromAsciiMasterEmap_new( std::string _filename, std::string _tag, int _crate, bool split_by_crate )
00434 {
00435   std::cout << "Generating linearization (input) LUTs from ascii master file..." << std::endl;
00436   std::map<int, boost::shared_ptr<LutXml> > _xml; // index - crate number
00437 
00438   // read LUTs and their eta/phi/depth/subdet ranges
00439   HcalLutSet _set = getLutSetFromFile( _filename );
00440   int lut_set_size = _set.lut.size(); // number of different luts
00441   std::cout << "  ==> " << lut_set_size << " sets of different LUTs read from the master file" << std::endl;
00442 
00443   RooGKCounter _counter;
00444   //loop over all EMap channels
00445   for( _iter.begin(); !_iter.end(); _iter.next() ){
00446     HcalSubdetector _subdet = _iter.getHcalSubdetector();
00447     if( (_subdet == HcalBarrel ||
00448          _subdet == HcalEndcap ||
00449          _subdet == HcalForward ||
00450          _subdet == HcalOuter )
00451         ){
00452       int _ieta  = _iter.getIeta();
00453       int _iphi  = _iter.getIphi();
00454       int _depth = _iter.getDepth();
00455 
00456       // FIXME: this is probably wrong, raw ids are different
00457       HcalElectronicsId _eId(_iter.getHcalGenericDetId().rawId());
00458       int aCrate      = _eId . readoutVMECrateId();
00459       int aSlot       = _eId . htrSlot();
00460       int aTopBottom  = _eId . htrTopBottom();
00461       int aFiber      = _eId . fiberIndex();
00462       int aFiberChan  = _eId . fiberChanId();
00463 
00464       LutXml::Config _cfg;
00465       
00466       // search for the correct LUT for a given channel,
00467       // higher LUT numbers have priority in case of overlapping
00468       int lut_index=-1;
00469       for ( int i=0; i<lut_set_size; i++ ){
00470         if ( (aCrate == _crate || _crate == -1) && // -1 stands for all crates
00471              _set.eta_min[i] <= _ieta &&
00472              _set.eta_max[i] >= _ieta &&
00473              _set.phi_min[i] <= _iphi &&
00474              _set.phi_max[i] >= _iphi &&
00475              _set.depth_min[i] <= _depth &&
00476              _set.depth_max[i] >= _depth &&
00477              _set.subdet[i].find(_ass.getSubdetectorString(_subdet))!=string::npos ){
00478           lut_index=i;
00479         }
00480       }
00481       if ( lut_index >= 0 ){
00482         if ( _xml.count(aCrate) == 0 && split_by_crate ){
00483           _xml.insert( std::pair<int,boost::shared_ptr<LutXml> >(aCrate,boost::shared_ptr<LutXml>(new LutXml())) );
00484         }
00485         else if ( _xml.count(0) == 0 && !split_by_crate ){
00486           _xml.insert( std::pair<int,boost::shared_ptr<LutXml> >(0,boost::shared_ptr<LutXml>(new LutXml())) );
00487         }
00488         _cfg.ieta = _ieta;
00489         _cfg.iphi = _iphi;
00490         _cfg.depth = _depth;
00491         _cfg.crate = aCrate;
00492         _cfg.slot = aSlot;
00493         _cfg.topbottom = aTopBottom;
00494         _cfg.fiber = aFiber;
00495         _cfg.fiberchan = aFiberChan;
00496         _cfg.lut_type = 1;
00497         _cfg.creationtag = _tag;
00498         _cfg.creationstamp = get_time_stamp( time(0) );
00499         _cfg.targetfirmware = "1.0.0";
00500         _cfg.formatrevision = "1"; //???
00501         // "original" definition of GENERALIZEDINDEX from Mike Weinberger
00502         //    int generalizedIndex=id.ietaAbs()+1000*id.depth()+10000*id.iphi()+
00503         //        ((id.ieta()<0)?(0):(100))+((id.subdet()==HcalForward && id.ietaAbs()==29)?(4*10000):(0));
00504         _cfg.generalizedindex =
00505           _cfg.iphi*10000 + _cfg.depth*1000 +
00506           (_ieta>0)*100 + abs(_ieta) +
00507           (((_subdet==HcalForward) && abs(_ieta)==29)?(4*10000):(0));
00508         _cfg.lut = _set.lut[lut_index];
00509         if (split_by_crate ){
00510           _xml[aCrate]->addLut( _cfg, lut_checksums_xml );  
00511           _counter.count();
00512         }
00513         else{
00514           _xml[0]->addLut( _cfg, lut_checksums_xml );  
00515           _counter.count();
00516         }
00517       }
00518     }
00519   }
00520   std::cout << "LUTs generated: " << _counter.getCount() << std::endl;
00521   std::cout << "Generating linearization (input) LUTs from ascii master file...DONE" << std::endl;
00522   return _xml;
00523 }
00524 
00525 
00526 std::map<int, boost::shared_ptr<LutXml> > HcalLutManager::getCompressionLutXmlFromAsciiMaster( std::string _filename, std::string _tag, int _crate, bool split_by_crate )
00527 {
00528   std::cout << "Generating compression (output) LUTs from ascii master file..." << std::endl;
00529   std::map<int, boost::shared_ptr<LutXml> > _xml; // index - crate number
00530 
00531   std::cout << "instantiating CaloTPGTranscoderULUT in order to check the validity of (ieta,iphi)..." << std::endl;
00532   CaloTPGTranscoderULUT _coder;
00533 
00534   //EMap _emap("../../../CondFormats/HcalObjects/data/official_emap_v6.03_080817.txt");
00535   //EMap _emap("../../../CondFormats/HcalObjects/data/official_emap_v6.04_080905.txt");
00536   EMap _emap(emap);
00537   std::vector<EMap::EMapRow> & _map = _emap.get_map();
00538   std::cout << "EMap contains " << _map . size() << " channels" << std::endl;
00539 
00540   // read LUTs and their eta/phi/depth/subdet ranges
00541   HcalLutSet _set = getLutSetFromFile( _filename, 2 );
00542   int lut_set_size = _set.lut.size(); // number of different luts
00543   std::cout << "  ==> " << lut_set_size << " sets of different LUTs read from the master file" << std::endl;
00544 
00545   //loop over all EMap channels
00546   RooGKCounter _counter;
00547   for( std::vector<EMap::EMapRow>::const_iterator row=_map.begin(); row!=_map.end(); row++ ){
00548     LutXml::Config _cfg;
00549 
00550     // search for the correct LUT for a given channel,
00551     // higher LUT numbers have priority in case of overlapping
00552     int lut_index=-1;
00553     for ( int i=0; i<lut_set_size; i++ ){
00554       if ( row->subdet . find("HT") != std::string::npos &&
00555            (row->crate == _crate || _crate == -1) && // -1 stands for all crates
00556            _set.eta_min[i] <= row->ieta &&
00557            _set.eta_max[i] >= row->ieta &&
00558            _set.phi_min[i] <= row->iphi &&
00559            _set.phi_max[i] >= row->iphi &&
00560            _coder.HTvalid(row->ieta, row->iphi) ){
00561         lut_index=i;
00562       }
00563     }
00564     if ( lut_index >= 0 ){
00565       if ( _xml.count(row->crate) == 0 && split_by_crate ){
00566         _xml.insert( std::pair<int,boost::shared_ptr<LutXml> >(row->crate,boost::shared_ptr<LutXml>(new LutXml())) );
00567       }
00568       else if ( _xml.count(0) == 0 && !split_by_crate ){
00569         _xml.insert( std::pair<int,boost::shared_ptr<LutXml> >(0,boost::shared_ptr<LutXml>(new LutXml())) );
00570       }
00571       _cfg.ieta = row->ieta;
00572       _cfg.iphi = row->iphi;
00573       _cfg.depth = row->idepth;
00574       _cfg.crate = row->crate;
00575       _cfg.slot = row->slot;
00576       if (row->topbottom . find("t") != std::string::npos) _cfg.topbottom = 1;
00577       else if (row->topbottom . find("b") != std::string::npos) _cfg.topbottom = 0;
00578       else std::cout << "Warning! fpga out of range..." << std::endl;
00579       _cfg.fiber = row->fiber;
00580       _cfg.fiberchan = row->fiberchan;
00581       if (_set.lut[lut_index].size() == 128) _cfg.lut_type = 1;
00582       else _cfg.lut_type = 2;
00583       _cfg.creationtag = _tag;
00584       _cfg.creationstamp = get_time_stamp( time(0) );
00585       _cfg.targetfirmware = "1.0.0";
00586       _cfg.formatrevision = "1"; //???
00587       // "original" definition of GENERALIZEDINDEX from Mike Weinberger
00588       //   int generalizedIndex=id.ietaAbs()+10000*id.iphi()+
00589       //       ((id.ieta()<0)?(0):(100));
00590       _cfg.generalizedindex =
00591         _cfg.iphi*10000+
00592         (row->ieta>0)*100+abs(row->ieta);
00593       _cfg.lut = _set.lut[lut_index];
00594       if (split_by_crate ){
00595         _xml[row->crate]->addLut( _cfg, lut_checksums_xml );  
00596         _counter.count();
00597       }
00598       else{
00599         _xml[0]->addLut( _cfg, lut_checksums_xml );  
00600         _counter.count();
00601       }
00602     }
00603   }
00604   std::cout << "LUTs generated: " << _counter.getCount() << std::endl;
00605   std::cout << "Generating compression (output) LUTs from ascii master file...DONE" << std::endl;
00606   return _xml;
00607 }
00608 
00609 
00610 std::map<int, boost::shared_ptr<LutXml> > HcalLutManager::getLinearizationLutXmlFromCoder( const HcalTPGCoder & _coder, std::string _tag, bool split_by_crate )
00611 {
00612   std::cout << "Generating linearization (input) LUTs from HcaluLUTTPGCoder..." << std::endl;
00613   std::map<int, boost::shared_ptr<LutXml> > _xml; // index - crate number
00614 
00615   //EMap _emap("../../../CondFormats/HcalObjects/data/official_emap_v6.03_080817.txt");
00616   //std::vector<EMap::EMapRow> & _map = _emap.get_map();
00617   //std::cout << "EMap contains " << _map . size() << " entries" << std::endl;
00618 
00619   LMap _lmap;
00620   _lmap . read( "backup/HCALmapHBEF.txt", "HBEF" );
00621   // HO is not part of trigger, so TPGCoder cannot generate LUTs for it
00622   //_lmap . read( "backup/HCALmapHO.txt", "HO" );
00623   std::map<int,LMapRow> & _map = _lmap.get_map();
00624   std::cout << "LMap contains " << _map . size() << " channels" << std::endl;
00625 
00626   // read LUTs and their eta/phi/depth/subdet ranges
00627   //HcalLutSet _set = getLinearizationLutSetFromCoder();
00628   //int lut_set_size = _set.lut.size(); // number of different luts
00629 
00630   //loop over all HCAL channels
00631   RooGKCounter _counter;
00632   for( std::map<int,LMapRow>::const_iterator row=_map.begin(); row!=_map.end(); row++ ){
00633     LutXml::Config _cfg;
00634     
00635     if ( _xml.count(row->second.crate) == 0 && split_by_crate ){
00636       _xml.insert( std::pair<int,boost::shared_ptr<LutXml> >(row->second.crate,boost::shared_ptr<LutXml>(new LutXml())) );
00637     }
00638     else if ( _xml.count(0) == 0 && !split_by_crate ){
00639       _xml.insert( std::pair<int,boost::shared_ptr<LutXml> >(0,boost::shared_ptr<LutXml>(new LutXml())) );
00640     }
00641     _cfg.ieta = row->second.side*row->second.eta;
00642     _cfg.iphi = row->second.phi;
00643     _cfg.depth = row->second.depth;
00644     _cfg.crate = row->second.crate;
00645     _cfg.slot = row->second.htr;
00646     if (row->second.fpga . find("top") != std::string::npos) _cfg.topbottom = 1;
00647     else if (row->second.fpga . find("bot") != std::string::npos) _cfg.topbottom = 0;
00648     else std::cout << "Warning! fpga out of range..." << std::endl;
00649     // FIXME: probably fixed. fiber==htr_fi, not rm_fi in LMAP notation.
00650     //_cfg.fiber = row->second.rm_fi;
00651     _cfg.fiber = row->second.htr_fi;
00652     _cfg.fiberchan = row->second.fi_ch;
00653     _cfg.lut_type = 1;
00654     _cfg.creationtag = _tag;
00655     _cfg.creationstamp = get_time_stamp( time(0) );
00656     _cfg.targetfirmware = "1.0.0";
00657     _cfg.formatrevision = "1"; //???
00658     // "original" definition of GENERALIZEDINDEX from Mike Weinberger
00659     //    int generalizedIndex=id.ietaAbs()+1000*id.depth()+10000*id.iphi()+
00660     //        ((id.ieta()<0)?(0):(100))+((id.subdet()==HcalForward && id.ietaAbs()==29)?(4*10000):(0));
00661     _cfg.generalizedindex =
00662       _cfg.iphi*10000 + _cfg.depth*1000 +
00663       (row->second.side>0)*100 + row->second.eta +
00664       ((row->second.det==HcalForward && row->second.eta==29)?(4*10000):(0));
00665 
00666     //HcalDetId _detid(row->first);
00667     HcalDetId _detid(row->second.det, row->second.side*row->second.eta, row->second.phi, row->second.depth);
00668     //std::cout << "### DEBUG: rawid = " << _detid.rawId() << std::endl;    
00669 
00670     //std::cout << "### DEBUG: subdetector = " << row->second.det << std::endl;    
00671     std::vector<unsigned short>  coder_lut = _coder . getLinearizationLUT(_detid);
00672     for (std::vector<unsigned short>::const_iterator _i=coder_lut.begin(); _i!=coder_lut.end();_i++){
00673       unsigned int _temp = (unsigned int)(*_i);
00674       //if (_temp!=0) std::cout << "DEBUG non-zero LUT!!!!!!!!!!!!!!!" << (*_i) << "     " << _temp << std::endl;
00675       //unsigned int _temp = 0;
00676       _cfg.lut.push_back(_temp);
00677     }
00678     if (split_by_crate ){
00679       _xml[row->second.crate]->addLut( _cfg, lut_checksums_xml );  
00680       _counter.count();
00681     }
00682     else{
00683       _xml[0]->addLut( _cfg, lut_checksums_xml );  
00684       _counter.count();
00685     }
00686   }
00687   std::cout << "Generated LUTs: " << _counter.getCount() << std::endl;
00688   std::cout << "Generating linearization (input) LUTs from HcaluLUTTPGCoder...DONE" << std::endl;
00689   return _xml;
00690 }
00691 
00692 
00693 
00694 
00695 std::map<int, boost::shared_ptr<LutXml> > HcalLutManager::getLinearizationLutXmlFromCoderEmap( const HcalTPGCoder & _coder, std::string _tag, bool split_by_crate )
00696 {
00697   std::cout << "Generating linearization (input) LUTs from HcaluLUTTPGCoder..." << std::endl;
00698   std::map<int, boost::shared_ptr<LutXml> > _xml; // index - crate number
00699 
00700   EMap _emap(emap);
00701   std::vector<EMap::EMapRow> & _map = _emap.get_map();
00702   std::cout << "EMap contains " << _map . size() << " entries" << std::endl;
00703 
00704   std::vector<unsigned int> zeroLut;
00705   for (size_t adc = 0; adc < 128; adc++) zeroLut.push_back(0);
00706 
00707   RooGKCounter _counter;
00708   //loop over all EMap channels
00709   for( std::vector<EMap::EMapRow>::const_iterator row=_map.begin(); row!=_map.end(); row++ ){
00710     if( (row->subdet.find("HB")!=string::npos ||
00711          row->subdet.find("HE")!=string::npos ||
00712          row->subdet.find("HF")!=string::npos ) &&
00713         row->subdet.size()==2
00714        ){
00715       LutXml::Config _cfg;
00716       
00717       if ( _xml.count(row->crate) == 0 && split_by_crate ){
00718         _xml.insert( std::pair<int,boost::shared_ptr<LutXml> >(row->crate,boost::shared_ptr<LutXml>(new LutXml())) );
00719       }
00720       else if ( _xml.count(0) == 0 && !split_by_crate ){
00721         _xml.insert( std::pair<int,boost::shared_ptr<LutXml> >(0,boost::shared_ptr<LutXml>(new LutXml())) );
00722       }
00723       _cfg.ieta = row->ieta;
00724       _cfg.iphi = row->iphi;
00725       _cfg.depth = row->idepth;
00726       _cfg.crate = row->crate;
00727       _cfg.slot = row->slot;
00728       if (row->topbottom . find("t") != std::string::npos) _cfg.topbottom = 1;
00729       else if (row->topbottom . find("b") != std::string::npos) _cfg.topbottom = 0;
00730       else std::cout << "Warning! fpga out of range..." << std::endl;
00731       _cfg.fiber = row->fiber;
00732       _cfg.fiberchan = row->fiberchan;
00733       _cfg.lut_type = 1;
00734       _cfg.creationtag = _tag;
00735       _cfg.creationstamp = get_time_stamp( time(0) );
00736       _cfg.targetfirmware = "1.0.0";
00737       _cfg.formatrevision = "1"; //???
00738       // "original" definition of GENERALIZEDINDEX from Mike Weinberger
00739       //    int generalizedIndex=id.ietaAbs()+1000*id.depth()+10000*id.iphi()+
00740       //        ((id.ieta()<0)?(0):(100))+((id.subdet()==HcalForward && id.ietaAbs()==29)?(4*10000):(0));
00741       _cfg.generalizedindex =
00742         _cfg.iphi*10000 + _cfg.depth*1000 +
00743         (row->ieta>0)*100 + abs(row->ieta) +
00744         (((row->subdet.find("HF")!=string::npos) && abs(row->ieta)==29)?(4*10000):(0));
00745       HcalSubdetector _subdet;
00746       if ( row->subdet.find("HB")!=string::npos ) _subdet = HcalBarrel;
00747       else if ( row->subdet.find("HE")!=string::npos ) _subdet = HcalEndcap;
00748       else if ( row->subdet.find("HO")!=string::npos ) _subdet = HcalOuter;
00749       else if ( row->subdet.find("HF")!=string::npos ) _subdet = HcalForward;
00750       else _subdet = HcalOther;
00751       HcalDetId _detid(_subdet, row->ieta, row->iphi, row->idepth);
00752       //
00753       // consider channel status here
00754       uint32_t status_word = cq->getValues(_detid)->getValue();
00755       if ((status_word & status_word_to_mask) > 0){
00756         _cfg.lut = zeroLut;
00757       }
00758       else{
00759         std::vector<unsigned short>  coder_lut = _coder . getLinearizationLUT(_detid);
00760         for (std::vector<unsigned short>::const_iterator _i=coder_lut.begin(); _i!=coder_lut.end();_i++){
00761           unsigned int _temp = (unsigned int)(*_i);
00762           _cfg.lut.push_back(_temp);
00763         }
00764       }
00765       if (split_by_crate ){
00766         _xml[row->crate]->addLut( _cfg, lut_checksums_xml );  
00767         _counter.count();
00768       }
00769       else{
00770         _xml[0]->addLut( _cfg, lut_checksums_xml );  
00771         _counter.count();
00772       }
00773     }
00774   }
00775   std::cout << "Generated LUTs: " << _counter.getCount() << std::endl;
00776   std::cout << "Generating linearization (input) LUTs from HcaluLUTTPGCoder...DONE" << std::endl;
00777   return _xml;
00778 }
00779 
00780 
00781 
00782 std::map<int, boost::shared_ptr<LutXml> > HcalLutManager::getCompressionLutXmlFromCoder( const CaloTPGTranscoderULUT & _coder, std::string _tag, bool split_by_crate )
00783 {
00784   std::cout << "Generating compression (output) LUTs from CaloTPGTranscoderULUT," << std::endl;
00785   std::cout << "initialized from Event Setup" << std::endl;
00786   std::map<int, boost::shared_ptr<LutXml> > _xml; // index - crate number
00787 
00788   //EMap _emap("../../../CondFormats/HcalObjects/data/official_emap_v6.04_080905.txt");
00789   EMap _emap(emap);
00790 
00791   std::vector<EMap::EMapRow> & _map = _emap.get_map();
00792   std::cout << "EMap contains " << _map . size() << " channels" << std::endl;
00793 
00794   // read LUTs and their eta/phi/depth/subdet ranges
00795   //HcalLutSet _set = getLutSetFromFile( _filename, 2 );
00796   //int lut_set_size = _set.lut.size(); // number of different luts
00797 
00798   //loop over all EMap channels
00799   RooGKCounter _counter;
00800   for( std::vector<EMap::EMapRow>::const_iterator row=_map.begin(); row!=_map.end(); row++ ){
00801     LutXml::Config _cfg;
00802 
00803     // only trigger tower channels
00804     // and valid (ieta,iphi)
00805     if ( row->subdet . find("HT") != std::string::npos && _coder.HTvalid(row->ieta, row->iphi) ){
00806       if ( _xml.count(row->crate) == 0 && split_by_crate ){
00807         _xml.insert( std::pair<int,boost::shared_ptr<LutXml> >(row->crate,boost::shared_ptr<LutXml>(new LutXml())) );
00808       }
00809       else if ( _xml.count(0) == 0 && !split_by_crate ){
00810         _xml.insert( std::pair<int,boost::shared_ptr<LutXml> >(0,boost::shared_ptr<LutXml>(new LutXml())) );
00811       }
00812       _cfg.ieta = row->ieta;
00813       _cfg.iphi = row->iphi;
00814       _cfg.depth = row->idepth;
00815       _cfg.crate = row->crate;
00816       _cfg.slot = row->slot;
00817       if (row->topbottom . find("t") != std::string::npos) _cfg.topbottom = 1;
00818       else if (row->topbottom . find("b") != std::string::npos) _cfg.topbottom = 0;
00819       else std::cout << "Warning! fpga out of range..." << std::endl;
00820       _cfg.fiber = row->fiber;
00821       _cfg.fiberchan = row->fiberchan;
00822       _cfg.lut_type = 2;
00823       _cfg.creationtag = _tag;
00824       _cfg.creationstamp = get_time_stamp( time(0) );
00825       _cfg.targetfirmware = "1.0.0";
00826       _cfg.formatrevision = "1"; //???
00827       // "original" definition of GENERALIZEDINDEX from Mike Weinberger
00828       //   int generalizedIndex=id.ietaAbs()+10000*id.iphi()+
00829       //       ((id.ieta()<0)?(0):(100));
00830       _cfg.generalizedindex =
00831         _cfg.iphi*10000+
00832         (row->ieta>0)*100+abs(row->ieta);
00833       
00834       // FIXME: work around bug in emap v6: rawId wasn't filled
00835       //HcalTrigTowerDetId _detid(row->rawId);
00836       HcalTrigTowerDetId _detid(row->ieta, row->iphi);
00837       
00838       std::vector<unsigned char> coder_lut = _coder.getCompressionLUT(_detid);
00839       for (std::vector<unsigned char>::const_iterator _i=coder_lut.begin(); _i!=coder_lut.end();_i++){
00840         unsigned int _temp = (unsigned int)(*_i);
00841         //if (_temp!=0) std::cout << "DEBUG non-zero LUT!!!!!!!!!!!!!!!" << (*_i) << "     " << _temp << std::endl;
00842         //unsigned int _temp = 0;
00843         _cfg.lut.push_back(_temp);
00844       }
00845       //_cfg.lut = _set.lut[lut_index];
00846       
00847       if (split_by_crate ){
00848         _xml[row->crate]->addLut( _cfg, lut_checksums_xml );  
00849         _counter.count();
00850       }
00851       else{
00852         _xml[0]->addLut( _cfg, lut_checksums_xml );  
00853         _counter.count();
00854       }
00855     }
00856   }
00857   std::cout << "LUTs generated: " << _counter.getCount() << std::endl;
00858   std::cout << "Generating compression (output) LUTs from CaloTPGTranscoderULUT...DONE" << std::endl;
00859   return _xml;
00860 }
00861 
00862 
00863 
00864 std::map<int, boost::shared_ptr<LutXml> > HcalLutManager::getCompressionLutXmlFromCoder( std::string _tag, bool split_by_crate )
00865 {
00866   std::cout << "Generating compression (output) LUTs from CaloTPGTranscoderULUT" << std::endl;
00867   std::map<int, boost::shared_ptr<LutXml> > _xml; // index - crate number
00868 
00869   //EMap _emap("../../../CondFormats/HcalObjects/data/official_emap_v5_080208.txt");
00870   //EMap _emap("../../../CondFormats/HcalObjects/data/official_emap_v6.03_080817.txt");
00871   //EMap _emap("../../../CondFormats/HcalObjects/data/official_emap_v6.04_080905.txt");
00872   EMap _emap(emap);
00873 
00874   std::vector<EMap::EMapRow> & _map = _emap.get_map();
00875   std::cout << "EMap contains " << _map . size() << " channels" << std::endl;
00876 
00877   // read LUTs and their eta/phi/depth/subdet ranges
00878   //HcalLutSet _set = getLutSetFromFile( _filename, 2 );
00879   //int lut_set_size = _set.lut.size(); // number of different luts
00880 
00881   CaloTPGTranscoderULUT _coder;
00882 
00883   //loop over all EMap channels
00884   RooGKCounter _counter;
00885   for( std::vector<EMap::EMapRow>::const_iterator row=_map.begin(); row!=_map.end(); row++ ){
00886     LutXml::Config _cfg;
00887 
00888     // only trigger tower channels
00889     // and valid (ieta,iphi)
00890     if ( row->subdet . find("HT") != std::string::npos && _coder.HTvalid(row->ieta, row->iphi) ){
00891       if ( _xml.count(row->crate) == 0 && split_by_crate ){
00892         _xml.insert( std::pair<int,boost::shared_ptr<LutXml> >(row->crate,boost::shared_ptr<LutXml>(new LutXml())) );
00893       }
00894       else if ( _xml.count(0) == 0 && !split_by_crate ){
00895         _xml.insert( std::pair<int,boost::shared_ptr<LutXml> >(0,boost::shared_ptr<LutXml>(new LutXml())) );
00896       }
00897       _cfg.ieta = row->ieta;
00898       _cfg.iphi = row->iphi;
00899       _cfg.depth = row->idepth;
00900       _cfg.crate = row->crate;
00901       _cfg.slot = row->slot;
00902       if (row->topbottom . find("t") != std::string::npos) _cfg.topbottom = 1;
00903       else if (row->topbottom . find("b") != std::string::npos) _cfg.topbottom = 0;
00904       else std::cout << "Warning! fpga out of range..." << std::endl;
00905       _cfg.fiber = row->fiber;
00906       _cfg.fiberchan = row->fiberchan;
00907       _cfg.lut_type = 2;
00908       _cfg.creationtag = _tag;
00909       _cfg.creationstamp = get_time_stamp( time(0) );
00910       _cfg.targetfirmware = "1.0.0";
00911       _cfg.formatrevision = "1"; //???
00912       // "original" definition of GENERALIZEDINDEX from Mike Weinberger
00913       //   int generalizedIndex=id.ietaAbs()+10000*id.iphi()+
00914       //       ((id.ieta()<0)?(0):(100));
00915       _cfg.generalizedindex =
00916         _cfg.iphi*10000+
00917         (row->ieta>0)*100+abs(row->ieta);
00918       
00919       // FIXME: work around bug in emap v6: rawId wasn't filled
00920       //HcalTrigTowerDetId _detid(row->rawId);
00921       HcalTrigTowerDetId _detid(row->ieta, row->iphi);
00922       
00923       std::vector<unsigned char> coder_lut = _coder.getCompressionLUT(_detid);
00924       for (std::vector<unsigned char>::const_iterator _i=coder_lut.begin(); _i!=coder_lut.end();_i++){
00925         unsigned int _temp = (unsigned int)(*_i);
00926         //if (_temp!=0) std::cout << "DEBUG non-zero LUT!!!!!!!!!!!!!!!" << (*_i) << "     " << _temp << std::endl;
00927         //unsigned int _temp = 0;
00928         _cfg.lut.push_back(_temp);
00929       }
00930       //_cfg.lut = _set.lut[lut_index];
00931       
00932       if (split_by_crate ){
00933         _xml[row->crate]->addLut( _cfg, lut_checksums_xml );  
00934         _counter.count();
00935       }
00936       else{
00937         _xml[0]->addLut( _cfg, lut_checksums_xml );  
00938         _counter.count();
00939       }
00940     }
00941   }
00942   std::cout << "LUTs generated: " << _counter.getCount() << std::endl;
00943   std::cout << "Generating compression (output) LUTs from CaloTPGTranscoderULUT...DONE" << std::endl;
00944   return _xml;
00945 }
00946 
00947 
00948 
00949 int HcalLutManager::writeLutXmlFiles( std::map<int, boost::shared_ptr<LutXml> > & _xml, std::string _tag, bool split_by_crate )
00950 {
00951   for (std::map<int,boost::shared_ptr<LutXml> >::const_iterator cr = _xml.begin(); cr != _xml.end(); cr++){
00952     std::stringstream output_file_name;
00953     if ( split_by_crate ){
00954       output_file_name << _tag << "_" << cr->first << ".xml";
00955     }
00956     else{
00957       output_file_name << _tag << ".xml";
00958     }
00959     cr->second->write( output_file_name.str().c_str() );
00960   }
00961   return 0;
00962 }
00963 
00964 int HcalLutManager::createLinLutXmlFiles( std::string _tag, std::string _lin_file, bool split_by_crate )
00965 {
00966   //std::cout << "DEBUG1: split_by_crate = " << split_by_crate << std::endl;
00967   std::map<int, boost::shared_ptr<LutXml> > xml;
00968   if ( !lut_checksums_xml ){
00969     lut_checksums_xml = new XMLDOMBlock( "CFGBrick", 1 );
00970   }
00971   
00972   if ( _lin_file.size() != 0 ){
00973     addLutMap( xml, getLinearizationLutXmlFromAsciiMasterEmap( _lin_file, _tag, -1, split_by_crate ) );
00974   }
00975   writeLutXmlFiles( xml, _tag, split_by_crate );
00976 
00977   std::string checksums_file = _tag + "_checksums.xml";
00978   lut_checksums_xml -> write( checksums_file . c_str() );
00979 
00980   return 0;
00981 }
00982 
00983 int HcalLutManager::createAllLutXmlFiles( std::string _tag, std::string _lin_file, std::string _comp_file, bool split_by_crate )
00984 {
00985   //std::cout << "DEBUG1: split_by_crate = " << split_by_crate << std::endl;
00986   std::map<int, boost::shared_ptr<LutXml> > xml;
00987   if ( !lut_checksums_xml ){
00988     lut_checksums_xml = new XMLDOMBlock( "CFGBrick", 1 );
00989   }
00990   
00991   if ( _lin_file.size() != 0 ){
00992     //addLutMap( xml, getLutXmlFromAsciiMaster( _lin_file, _tag, -1, split_by_crate ) );
00993     addLutMap( xml, getLinearizationLutXmlFromAsciiMasterEmap( _lin_file, _tag, -1, split_by_crate ) );
00994   }
00995   if ( _comp_file.size() != 0 ){
00996     //std::cout << "DEBUG1!!!!" << std::endl;
00997     addLutMap( xml, getCompressionLutXmlFromAsciiMaster( _comp_file, _tag, -1, split_by_crate ) );
00998     //std::cout << "DEBUG2!!!!" << std::endl;
00999   }
01000   writeLutXmlFiles( xml, _tag, split_by_crate );
01001 
01002   std::string checksums_file = _tag + "_checksums.xml";
01003   lut_checksums_xml -> write( checksums_file . c_str() );
01004 
01005   return 0;
01006 }
01007 
01008 int HcalLutManager::createCompLutXmlFilesFromCoder( std::string _tag, bool split_by_crate )
01009 {
01010   //std::cout << "DEBUG1: split_by_crate = " << split_by_crate << std::endl;
01011   std::map<int, boost::shared_ptr<LutXml> > xml;
01012   if ( !lut_checksums_xml ){
01013     lut_checksums_xml = new XMLDOMBlock( "CFGBrick", 1 );
01014   }
01015   
01016   addLutMap( xml, getCompressionLutXmlFromCoder( _tag, split_by_crate ) );
01017 
01018   writeLutXmlFiles( xml, _tag, split_by_crate );
01019 
01020   std::string checksums_file = _tag + "_checksums.xml";
01021   lut_checksums_xml -> write( checksums_file . c_str() );
01022 
01023   return 0;
01024 }
01025 
01026 int HcalLutManager::createAllLutXmlFilesFromCoder( const HcalTPGCoder & _coder, std::string _tag, bool split_by_crate )
01027 {
01028   //std::cout << "DEBUG1: split_by_crate = " << split_by_crate << std::endl;
01029   std::map<int, boost::shared_ptr<LutXml> > xml;
01030   if ( !lut_checksums_xml ){
01031     lut_checksums_xml = new XMLDOMBlock( "CFGBrick", 1 );
01032   }
01033   
01034   //addLutMap( xml, getLinearizationLutXmlFromCoder( _coder, _tag, split_by_crate ) );
01035   addLutMap( xml, getLinearizationLutXmlFromCoderEmap( _coder, _tag, split_by_crate ) );
01036   addLutMap( xml, getCompressionLutXmlFromCoder( _tag, split_by_crate ) );
01037 
01038   writeLutXmlFiles( xml, _tag, split_by_crate );
01039 
01040   std::string checksums_file = _tag + "_checksums.xml";
01041   lut_checksums_xml -> write( checksums_file . c_str() );
01042 
01043   return 0;
01044 }
01045 
01046 //
01047 //_____ use this for creating a full set of LUTs ________________________
01048 //
01049 int HcalLutManager::createLutXmlFiles_HBEFFromCoder_HOFromAscii( std::string _tag, const HcalTPGCoder & _coder, const CaloTPGTranscoderULUT & _transcoder, std::string _lin_file, bool split_by_crate )
01050 {
01051   std::map<int, boost::shared_ptr<LutXml> > xml;
01052   if ( !lut_checksums_xml ){
01053     lut_checksums_xml = new XMLDOMBlock( "CFGBrick", 1 );
01054   }
01055   
01056   if ( _lin_file.size() != 0 ){
01057     const std::map<int, boost::shared_ptr<LutXml> > _lin_lut_ascii_xml = getLinearizationLutXmlFromAsciiMasterEmap( _lin_file, _tag, -1, split_by_crate );
01058     addLutMap( xml, _lin_lut_ascii_xml );
01059   }
01060   const std::map<int, boost::shared_ptr<LutXml> > _lin_lut_xml = getLinearizationLutXmlFromCoderEmap( _coder, _tag, split_by_crate );
01061   addLutMap( xml, _lin_lut_xml );
01062   //
01063   const std::map<int, boost::shared_ptr<LutXml> > _comp_lut_xml = getCompressionLutXmlFromCoder( _transcoder, _tag, split_by_crate );
01064   addLutMap( xml, _comp_lut_xml );
01065   
01066   writeLutXmlFiles( xml, _tag, split_by_crate );
01067   
01068   std::string checksums_file = _tag + "_checksums.xml";
01069   lut_checksums_xml -> write( checksums_file . c_str() );
01070   
01071   return 0;
01072 }
01073 
01074 
01075 int HcalLutManager::createLutXmlFiles_HBEFFromCoder_HOFromAscii( std::string _tag, const HcalTPGCoder & _coder, std::string _lin_file, bool split_by_crate )
01076 {
01077   std::map<int, boost::shared_ptr<LutXml> > xml;
01078   if ( !lut_checksums_xml ){
01079     lut_checksums_xml = new XMLDOMBlock( "CFGBrick", 1 );
01080   }
01081   
01082   if ( _lin_file.size() != 0 ){
01083     const std::map<int, boost::shared_ptr<LutXml> > _lin_lut_ascii_xml = getLinearizationLutXmlFromAsciiMasterEmap( _lin_file, _tag, -1, split_by_crate );
01084     addLutMap( xml, _lin_lut_ascii_xml );
01085   }
01086   const std::map<int, boost::shared_ptr<LutXml> > _lin_lut_xml = getLinearizationLutXmlFromCoderEmap( _coder, _tag, split_by_crate );
01087   addLutMap( xml, _lin_lut_xml );
01088   //
01089   const std::map<int, boost::shared_ptr<LutXml> > _comp_lut_xml = getCompressionLutXmlFromCoder( _tag, split_by_crate );
01090   addLutMap( xml, _comp_lut_xml );
01091   
01092   writeLutXmlFiles( xml, _tag, split_by_crate );
01093   
01094   std::string checksums_file = _tag + "_checksums.xml";
01095   lut_checksums_xml -> write( checksums_file . c_str() );
01096   
01097   return 0;
01098 }
01099 
01100 
01101 // use this to create HBEF only from coders (physics LUTs)
01102 int HcalLutManager::createAllLutXmlFilesLinAsciiCompCoder( std::string _tag, std::string _lin_file, bool split_by_crate )
01103 {
01104   //std::cout << "DEBUG1: split_by_crate = " << split_by_crate << std::endl;
01105   std::map<int, boost::shared_ptr<LutXml> > xml;
01106   if ( !lut_checksums_xml ){
01107     lut_checksums_xml = new XMLDOMBlock( "CFGBrick", 1 );
01108   }
01109   
01110   if ( _lin_file.size() != 0 ){
01111     addLutMap( xml, getLutXmlFromAsciiMaster( _lin_file, _tag, -1, split_by_crate ) );
01112   }
01113   addLutMap( xml, getCompressionLutXmlFromCoder( _tag, split_by_crate ) );
01114   writeLutXmlFiles( xml, _tag, split_by_crate );
01115 
01116   std::string checksums_file = _tag + "_checksums.xml";
01117   lut_checksums_xml -> write( checksums_file . c_str() );
01118 
01119   return 0;
01120 }
01121 
01122 
01123 
01124 void HcalLutManager::addLutMap(std::map<int, boost::shared_ptr<LutXml> > & result, const std::map<int, boost::shared_ptr<LutXml> > & other)
01125 {
01126   for ( std::map<int, boost::shared_ptr<LutXml> >::const_iterator lut=other.begin(); lut!=other.end(); lut++ ){
01127     std::cout << "Added LUTs for crate " << lut->first << std::endl;
01128     if ( result.count(lut->first)==0 ){
01129       result . insert( *lut );
01130     }
01131     else{
01132       *(result[lut->first]) += *(lut->second);
01133     }
01134   }
01135 }
01136 
01137 
01138 
01139 
01140 string HcalLutManager::get_time_stamp( time_t _time )
01141 {
01142   char timebuf[50];
01143   //strftime( timebuf, 50, "%c", gmtime( &_time ) );
01144   strftime( timebuf, 50, "%Y-%m-%d %H:%M:%S", gmtime( &_time ) );
01145   std::string creationstamp = timebuf;
01146 
01147   return creationstamp;
01148 }
01149 
01150 
01151 
01152 
01153 int HcalLutManager::test_xml_access( std::string _tag, std::string _filename )
01154 {
01155   local_connect( _filename, "backup/HCALmapHBEF.txt", "backup/HCALmapHO.txt" );
01156 
01157   //EMap _emap("../../../CondFormats/HcalObjects/data/official_emap_v6.04_080905.txt");
01158   EMap _emap(emap);
01159   std::vector<EMap::EMapRow> & _map = _emap.get_map();
01160   int map_size = _map . size();
01161   std::cout << "EMap contains " << map_size << " channels" << std::endl;
01162 
01163   // make sure that all init is done
01164   std::vector<unsigned int> _lut;
01165   _lut = getLutFromXml( _tag, 1107313727, hcal::ConfigurationDatabase::LinearizerLUT );
01166 
01167 
01168   std::cout << std::endl << "Testing direct parsing of the LUT XML" << std::endl;
01169   struct timeval _t;
01170   gettimeofday( &_t, NULL );
01171   double _time =(double)(_t . tv_sec) + (double)(_t . tv_usec)/1000000.0;
01172   test_direct_xml_parsing(_filename);
01173   gettimeofday( &_t, NULL );
01174   std::cout << "parsing took that much time: " << (double)(_t . tv_sec) + (double)(_t . tv_usec)/1000000.0 - _time << std::endl;
01175 
01176 
01177   gettimeofday( &_t, NULL );
01178   _time =(double)(_t . tv_sec) + (double)(_t . tv_usec)/1000000.0;
01179   std::cout << "before loop over random LUTs: " << _time << std::endl;
01180   int _raw_id;
01181 
01182   // loop over random LUTs
01183   for (int _iter=0; _iter<100; _iter++){
01184     gettimeofday( &_t, NULL );
01185     //std::cout << "before getting a LUT: " << _t . tv_sec << "." << _t . tv_usec << std::endl;
01186 
01187     // select valid random emap channel
01188     while(1){
01189       int _key = (rand() % map_size);
01190       //_key = 3356;
01191       if( (_map[_key].subdet.find("HB")!=string::npos ||
01192            _map[_key].subdet.find("HE")!=string::npos ||
01193            _map[_key].subdet.find("HO")!=string::npos ||
01194            _map[_key].subdet.find("HF")!=string::npos ) &&
01195           _map[_key].subdet.size()==2
01196           ){
01197         HcalSubdetector _subdet;
01198         if ( _map[_key].subdet.find("HB")!=string::npos ) _subdet = HcalBarrel;
01199         else if ( _map[_key].subdet.find("HE")!=string::npos ) _subdet = HcalEndcap;
01200         else if ( _map[_key].subdet.find("HO")!=string::npos ) _subdet = HcalOuter;
01201         else if ( _map[_key].subdet.find("HF")!=string::npos ) _subdet = HcalForward;
01202         else _subdet = HcalOther;
01203         HcalDetId _detid(_subdet, _map[_key].ieta, _map[_key].iphi, _map[_key].idepth);
01204         _raw_id = _detid.rawId();
01205         break;
01206       }
01207     }
01208     _lut = getLutFromXml( _tag, _raw_id, hcal::ConfigurationDatabase::LinearizerLUT );
01209     
01210     gettimeofday( &_t, NULL );
01211   }
01212   double d_time = _t.tv_sec+_t.tv_usec/1000000.0 - _time;
01213   std::cout << "after the loop over random LUTs: " << _time+d_time << std::endl;  
01214   std::cout << "total time: " << d_time << std::endl;  
01215   
01216   std::cout << "LUT length = " << _lut . size() << std::endl;
01217   for ( std::vector<unsigned int>::const_iterator i = _lut . end() - 1; i != _lut . begin()-1; i-- )
01218     {
01219       std::cout << (i-_lut.begin()) << "     " << _lut[(i-_lut.begin())] << std::endl;
01220       break;
01221     }
01222   
01223   db -> disconnect();
01224   
01225   delete db;
01226   db = 0;
01227   
01228   return 0;
01229 }
01230 
01231 
01232 
01233 int HcalLutManager::read_lmap( std::string lmap_hbef_file, std::string lmap_ho_file )
01234 {
01235   delete lmap;
01236   lmap = new LMap();
01237   lmap -> read( lmap_hbef_file, "HBEF" );
01238   lmap -> read( lmap_ho_file, "HO" );
01239   std::cout << "LMap contains " << lmap -> get_map() . size() << " channels (compare to 9072 of all HCAL channels)" << std::endl;
01240   return 0;
01241 }
01242 
01243 
01244 
01245 int HcalLutManager::read_luts( std::string lut_xml_file )
01246 {
01247   delete db;
01248   db = new HCALConfigDB();
01249   db -> connect( lut_xml_file );
01250   return 0;
01251 }
01252 
01253 
01254 
01255 
01256 
01257 int HcalLutManager::local_connect( std::string lut_xml_file, std::string lmap_hbef_file, std::string lmap_ho_file )
01258 {
01259   read_lmap( lmap_hbef_file, lmap_ho_file );
01260   read_luts( lut_xml_file );
01261   return 0;
01262 }
01263 
01264 
01265 
01266 
01267 std::vector<unsigned int> HcalLutManager::getLutFromXml( std::string tag, uint32_t _rawid, hcal::ConfigurationDatabase::LUTType _lt )
01268 {
01269   std::cout << "getLutFromXml (new version) is not implemented. Use getLutFromXml_old() for now" << std::endl;
01270 
01271   std::vector<unsigned int> result;
01272 
01273 
01274 
01275   return result;
01276 }
01277 
01278 
01279 // obsolete, use getLutFromXml() instead
01280 std::vector<unsigned int> HcalLutManager::getLutFromXml_old( std::string tag, uint32_t _rawid, hcal::ConfigurationDatabase::LUTType _lt )
01281 {
01282   if ( !lmap ){
01283     std::cout << "HcalLutManager: cannot find LUT without LMAP, exiting..." << std::endl;
01284     exit(-1);
01285   }
01286   if ( !db ){
01287     std::cout << "HcalLutManager: cannot find LUT, no source (local XML file), exiting..." << std::endl;
01288     exit(-1);
01289   }
01290 
01291   std::vector<unsigned int> result;
01292 
01293   std::map<int,LMapRow> & _map = lmap -> get_map();
01294   //std::cout << "HcalLutManager: LMap contains " << _map . size() << " channels (out of 9072 total)" << std::endl;
01295 
01296   HcalDetId _id( _rawid );
01297     
01298   unsigned int _crate, _slot, _fiber, _channel;
01299   std::string _fpga;
01300   int topbottom, luttype;
01301 
01302   // FIXME: check validity of _rawid
01303   if ( _map . find(_rawid) != _map.end() ){
01304     _crate   = _map[_rawid] . crate;
01305     _slot    = _map[_rawid] . htr;
01306     _fiber   = _map[_rawid] . htr_fi;
01307     _channel = _map[_rawid] . fi_ch;
01308     _fpga    = _map[_rawid] . fpga;
01309     
01310     if ( _fpga . find("top") != std::string::npos ) topbottom = 1;
01311     else if ( _fpga . find("bot") != std::string::npos ) topbottom = 0;
01312     else{
01313       std::cout << "HcalLutManager: irregular LMAP fpga value... do not know what to do - exiting" << std::endl;
01314       exit(-1);
01315     }
01316     if ( _lt == hcal::ConfigurationDatabase::LinearizerLUT ) luttype = 1;
01317     else luttype = 2;
01318     
01319     result = db -> getOnlineLUT( tag, _crate, _slot, topbottom, _fiber, _channel, luttype );
01320   }
01321   
01322   return result;
01323 }
01324 
01325 
01326 
01327 int HcalLutManager::get_xml_files_from_db( std::string tag, const std::string db_accessor, bool split_by_crate )
01328 {
01329   std::map<int, boost::shared_ptr<LutXml> > lut_map = get_brickSet_from_oracle( tag, db_accessor );
01330   if (split_by_crate){
01331     writeLutXmlFiles( lut_map, tag, split_by_crate );
01332   }      
01333   else{
01334     LutXml result;
01335     for( std::map<int, boost::shared_ptr<LutXml> >::const_iterator xml = lut_map.begin(); xml != lut_map.end(); xml++ ){
01336       result += *(xml->second);
01337     }
01338     std::stringstream out_file;
01339     out_file << tag << ".xml";
01340     result . write(out_file.str());    
01341   }
01342 
01343   return 0;
01344 }
01345 
01346 std::map<int, boost::shared_ptr<LutXml> > HcalLutManager::get_brickSet_from_oracle( std::string tag, const std::string _accessor )
01347 {
01348   HCALConfigDB * db = new HCALConfigDB();
01349   XMLProcessor::getInstance(); // initialize xerces-c engine
01350   //const std::string _accessor = "occi://CMS_HCL_PRTTYPE_HCAL_READER@anyhost/int2r?PASSWORD=HCAL_Reader_88,LHWM_VERSION=22";
01351   db -> connect( _accessor );
01352   oracle::occi::Connection * _connection = db -> getConnection();  
01353 
01354   std::cout << "Preparing to request the LUT CLOBs from the database..." << std::endl;
01355 
01356   //int crate = 0;
01357   
01358   //
01359   // _____ query is different for the old validation DB _________________
01360   //
01361   //std::string query = ("SELECT TRIG_PRIM_LOOKUPTBL_DATA_CLOB, CRATE FROM CMS_HCL_HCAL_CONDITION_OWNER.V_HCAL_TRIG_LOOKUP_TABLES");
01362   std::string query = ("SELECT TRIG_PRIM_LOOKUPTBL_DATA_CLOB, CRATE FROM CMS_HCL_HCAL_COND.V_HCAL_TRIG_LOOKUP_TABLES");
01363   //query+=toolbox::toString(" WHERE TAG_NAME='%s' AND CRATE=%d", tag.c_str(), crate);
01364   query+=toolbox::toString(" WHERE TAG_NAME='%s'", tag.c_str() );
01365 
01366   std::string brick_set;
01367 
01368   std::map<int, boost::shared_ptr<LutXml> > lut_map;
01369 
01370   try {
01371     //SELECT
01372     std::cout << "Executing the query..." << std::endl;
01373     Statement* stmt = _connection -> createStatement();
01374     ResultSet *rs = stmt->executeQuery(query.c_str());
01375     std::cout << "Executing the query... done" << std::endl;
01376     
01377     std::cout << "Processing the query results..." << std::endl;
01378     //RooGKCounter _lines;
01379     while (rs->next()) {
01380       //_lines.count();
01381       oracle::occi::Clob clob = rs->getClob (1);
01382       int crate = rs->getInt(2);
01383       if ( crate != -1 ){ // not a brick with checksums
01384         cout << "Getting LUTs for crate #" << crate << " out of the database...";
01385         brick_set = db -> clobToString(clob);
01386         /*
01387         // FIXME: DEBUG lut xml files from simple strings
01388         stringstream file_name;
01389         ofstream out_file;
01390         file_name << tag << "_" << crate << "_debug" << ".xml";
01391         out_file . open( file_name.str().c_str() );
01392         out_file << brick_set;
01393         out_file . close();
01394         */
01395         const char * bs = brick_set . c_str();
01396         MemBufInputSource * lut_clob = new MemBufInputSource( (const XMLByte *)bs, strlen( bs ), "lut_clob", false );
01397         boost::shared_ptr<LutXml> lut_xml = boost::shared_ptr<LutXml>( new LutXml( *lut_clob ) );
01398         lut_map[crate] = lut_xml;
01399         std::cout << " done" << std::endl;
01400       }
01401     }
01402     //Always terminate statement
01403     _connection -> terminateStatement(stmt);
01404     //std::cout << "Query line count: " << _lines.getCount() << std::endl;
01405   } catch (SQLException& e) {
01406     XCEPT_RAISE(hcal::exception::ConfigurationDatabaseException,::toolbox::toString("Oracle  exception : %s",e.getMessage().c_str()));
01407   }
01408 
01409   //std::cout << lut_map.size() << std::endl;
01410 
01411   db -> disconnect();
01412   //delete db;
01413   return lut_map;
01414 }
01415 
01416 
01417 int HcalLutManager::create_lut_loader( std::string file_list, std::string _prefix, std::string tag_name, std::string comment, std::string version, int subversion )
01418 {
01419   std::cout << "Generating XML loader for LUTs..." << std::endl;
01420   //std::cout << _prefix << "..." << tag_name << std::endl;
01421 
01422   XMLLUTLoader::loaderBaseConfig baseConf;
01423   XMLLUTLoader::lutDBConfig conf;
01424   XMLLUTLoader::checksumsDBConfig CSconf;
01425 
01426   baseConf . tag_name = tag_name;
01427   //baseConf . comment_description = tag_name;
01428   baseConf . comment_description = comment;
01429   baseConf . iov_begin = "1";
01430   baseConf . iov_end = "-1";
01431 
01432   conf . version = version;
01433 
01434   std::stringstream _subversion;
01435   _subversion << subversion;
01436   conf . subversion = _subversion.str();
01437 
01438   CSconf . version = conf . version;
01439   CSconf . subversion = conf . subversion;
01440   CSconf . trig_prim_lookuptbl_data_file = _prefix + "_checksums.xml.dat";
01441   CSconf . comment_description = tag_name;
01442 
01443   XMLLUTLoader doc( &baseConf );
01444 
01445   std::vector<int> crate_number;
01446   std::vector<std::string> file_name = HcalQIEManager::splitString(file_list);
01447   for (std::vector<std::string>::const_iterator _f = file_name.begin(); _f != file_name.end(); _f++){
01448     int crate_begin = _f->rfind("_");
01449     int crate_end = _f->rfind(".xml.dat");
01450     crate_number . push_back(getInt(_f->substr(crate_begin+1,crate_end-crate_begin-1)));
01451   }
01452   //
01453   //_____ fix due to the new convention: version/subversion combo must be unique for every payload
01454   //
01455   char _buf[128];
01456   time_t _offset = time(NULL);
01457   sprintf( _buf, "%d", (uint32_t)_offset );
01458   conf.version.append(".");
01459   conf.version.append(_buf);
01460   CSconf.version = conf.version;
01461   //
01462   for ( std::vector<std::string>::const_iterator _file = file_name . begin(); _file != file_name . end(); _file++ )
01463     {
01464       conf . trig_prim_lookuptbl_data_file = *_file;
01465       //conf . trig_prim_lookuptbl_data_file += ".dat";
01466       conf . crate = crate_number[ _file - file_name . begin() ];
01467       //
01468       //_____ fix due to the new convention: version/subversion combo must be unique for every payload
01469       //
01470       sprintf( _buf, "%.2d", conf.crate );
01471       conf.subversion.clear();
01472       conf.subversion.append(_buf);
01473       sprintf( _buf, "CRATE%.2d", conf . crate );
01474       std::string _namelabel;
01475       _namelabel . append( _buf );
01476       conf . name_label = _namelabel;
01477       doc . addLUT( &conf );
01478     }
01479   
01480   doc . addChecksums( &CSconf );
01481   //doc . write( _prefix + "_Loader.xml" );
01482   doc . write( tag_name + "_Loader.xml" );
01483 
01484   std::cout << "Generating XML loader for LUTs... done." << std::endl;
01485 
01486   return 0;
01487 }
01488 
01489 void HcalLutManager::test_emap( void ){
01490   //EMap _emap("../../../CondFormats/HcalObjects/data/official_emap_v5_080208.txt");
01491   //EMap _emap("../../../CondFormats/HcalObjects/data/official_emap_v6.03_080817.txt");
01492   //EMap _emap("../../../CondFormats/HcalObjects/data/official_emap_v6.04_080905.txt");
01493   EMap _emap(emap);
01494   std::vector<EMap::EMapRow> & _map = _emap.get_map();
01495   std::cout << "EMap contains " << _map . size() << " channels" << std::endl;
01496   
01497   //loop over all EMap channels
01498   //RooGKCounter _c;
01499   for( std::vector<EMap::EMapRow>::const_iterator row=_map.begin(); row!=_map.end(); row++ ){
01500     
01501     // only trigger tower channels
01502     if ( row->subdet . find("HT") != std::string::npos ){
01503       std::cout << " -----> Subdet = " << row->subdet << std::endl;
01504       
01505       if (abs(row->ieta)>28){
01506         //if (row->iphi == 71){
01507         cout << " ==> (ieta,iphi) = " << row->ieta << ",        " << row->iphi << std::endl;
01508       }
01509     }
01510   }
01511 }
01512 
01513 
01514 
01515 
01516 
01517 int HcalLutManager::test_direct_xml_parsing( std::string _filename ){
01518   /*
01519   XMLDOMBlock _xml(_filename);
01520   //DOMElement * data_set_elem = (DOMElement *)(document -> getElementsByTagName( XMLProcessor::_toXMLCh( "DATA_SET" ) ) -> item(0));  
01521   DOMNodeList * brick_list = _xml . getDocument() ->  getElementsByTagName( XMLProcessor::_toXMLCh( "CFGBrick" ));  
01522 
01523   double n_bricks = brick_list->getLength();
01524   std::cout << "amount of LUT bricks: " << n_bricks << std::endl;
01525 
01526   for (int iter=0; iter!=n_bricks; iter++){
01527     DOMElement * _brick = (DOMElement *)(brick_list->item(iter));
01528     
01529     DOMElement * _param = 0;
01530     // loop over brick parameters
01531     int par_iter = 0;
01532     while(1){
01533       _param = (DOMElement *)(_brick->getElementsByTagName(XMLProcessor::_toXMLCh("Parameter")));
01534       std::string _name = _param->getAttribute( XMLProcessor::_toXMLCh( "name" ) );
01535       if (_name.find("IETA")==string::npos) break;
01536 
01537       std::string _tag = "Parameter";
01538       std::cout << "### Parameter IETA = " << _xml.getTagValue( _tag, 0, _brick);
01539       par_iter++;
01540     }
01541   }
01542   */
01543   return 0;
01544 }
01545 
01546 
01547 //
01548 //_____ attempt to include ZDC LUTs _____________________________________
01549 //
01550 int HcalLutManager::createLutXmlFiles_HBEFFromCoder_HOFromAscii_ZDC( std::string _tag, const HcalTPGCoder & _coder, const CaloTPGTranscoderULUT & _transcoder, std::string _lin_file, bool split_by_crate )
01551 {
01552   std::map<int, boost::shared_ptr<LutXml> > xml;
01553   if ( !lut_checksums_xml ){
01554     lut_checksums_xml = new XMLDOMBlock( "CFGBrick", 1 );
01555   }
01556   
01557   if ( _lin_file.size() != 0 ){
01558     const std::map<int, boost::shared_ptr<LutXml> > _lin_lut_ascii_xml = getLinearizationLutXmlFromAsciiMasterEmap( _lin_file, _tag, -1, split_by_crate );
01559     addLutMap( xml, _lin_lut_ascii_xml );
01560   }
01561   const std::map<int, boost::shared_ptr<LutXml> > _lin_lut_xml = getLinearizationLutXmlFromCoderEmap( _coder, _tag, split_by_crate );
01562   addLutMap( xml, _lin_lut_xml );
01563   //
01564   const std::map<int, boost::shared_ptr<LutXml> > _comp_lut_xml = getCompressionLutXmlFromCoder( _transcoder, _tag, split_by_crate );
01565   addLutMap( xml, _comp_lut_xml );
01566   //
01567   const std::map<int, boost::shared_ptr<LutXml> > _zdc_lut_xml = getZdcLutXml( _tag, split_by_crate );
01568   addLutMap( xml, _zdc_lut_xml );
01569   
01570   writeLutXmlFiles( xml, _tag, split_by_crate );
01571   
01572   std::string checksums_file = _tag + "_checksums.xml";
01573   lut_checksums_xml -> write( checksums_file . c_str() );
01574   
01575   return 0;
01576 }
01577 
01578 
01579 std::map<int, boost::shared_ptr<LutXml> > HcalLutManager::getZdcLutXml( std::string _tag,
01580                                                                  bool split_by_crate )
01581 {
01582   std::cout << "Generating ZDC LUTs ...may the Force be with us..." << std::endl;
01583   std::map<int, boost::shared_ptr<LutXml> > _xml; // index - crate number
01584 
01585   EMap _emap(emap);
01586 
01587   ZdcLut zdc;
01588 
01589   std::vector<EMap::EMapRow> & _map = _emap.get_map();
01590   std::cout << "EMap contains " << _map . size() << " channels" << std::endl;
01591 
01592   //loop over all EMap channels
01593   RooGKCounter _counter;
01594   for( std::vector<EMap::EMapRow>::const_iterator row=_map.begin(); row!=_map.end(); row++ ){
01595     LutXml::Config _cfg;
01596 
01597     // only ZDC channels
01598     if ( row->zdc_section . find("ZDC") != std::string::npos ){
01599       if ( _xml.count(row->crate) == 0 && split_by_crate ){
01600         _xml.insert( std::pair<int,boost::shared_ptr<LutXml> >(row->crate,boost::shared_ptr<LutXml>(new LutXml())) );
01601       }
01602       else if ( _xml.count(0) == 0 && !split_by_crate ){
01603         _xml.insert( std::pair<int,boost::shared_ptr<LutXml> >(0,boost::shared_ptr<LutXml>(new LutXml())) );
01604       }
01605       //  FIXME: introduce proper tag names in ZDC bricks for logical channel info
01606       _cfg.ieta = row->zdc_channel; // int
01607       //_cfg.ieta = row->zdc_zside; // int
01608       //_cfg.iphi = row->zdc_section; // string
01609       _cfg.depth = row->idepth; // int
01610       _cfg.crate = row->crate;
01611       _cfg.slot = row->slot;
01612       if (row->topbottom . find("t") != std::string::npos) _cfg.topbottom = 1;
01613       else if (row->topbottom . find("b") != std::string::npos) _cfg.topbottom = 0;
01614       else std::cout << "Warning! fpga out of range..." << std::endl;
01615       _cfg.fiber = row->fiber;
01616       _cfg.fiberchan = row->fiberchan;
01617       _cfg.lut_type = 1;
01618       _cfg.creationtag = _tag;
01619       _cfg.creationstamp = get_time_stamp( time(0) );
01620       _cfg.targetfirmware = "1.0.0";
01621       _cfg.formatrevision = "1"; //???
01622       _cfg.generalizedindex = 0;
01623       
01624       //HcalZDCDetId _detid(row->zdc_section, (row->zdc_zside>0), row->zdc_channel);
01625       
01626       std::vector<int> coder_lut = zdc.get_lut(row->zdc_section,
01627                                                row->zdc_zside,
01628                                                row->zdc_channel);
01629       std::cout << "***DEBUG: ZDC lut size: " << coder_lut.size() << std::endl;
01630       if (coder_lut.size()!=0){
01631         for (std::vector<int>::const_iterator _i=coder_lut.begin(); _i!=coder_lut.end();_i++){
01632           unsigned int _temp = (unsigned int)(*_i);
01633           //if (_temp!=0) std::cout << "DEBUG non-zero LUT!!!!!!!!!!!!!!!" << (*_i) << "     " << _temp << std::endl;
01634           //unsigned int _temp = 0;
01635           _cfg.lut.push_back(_temp);
01636         }
01637         //_cfg.lut = _set.lut[lut_index];
01638         
01639         if (split_by_crate ){
01640           _xml[row->crate]->addLut( _cfg, lut_checksums_xml );  
01641           _counter.count();
01642         }
01643         else{
01644           _xml[0]->addLut( _cfg, lut_checksums_xml );  
01645           _counter.count();
01646         }
01647       } //size of lut
01648     }
01649   }
01650   std::cout << "LUTs generated: " << _counter.getCount() << std::endl;
01651   std::cout << "Generating ZDC LUTs...DONE" << std::endl;
01652 
01653   return _xml;
01654 }
01655 
01656