00001 #include <fstream>
00002 #include <sstream>
00003 #include "CaloOnlineTools/HcalOnlineDb/interface/HcalLutManager.h"
00004 #include "CaloOnlineTools/HcalOnlineDb/interface/XMLDOMBlock.h"
00005 #include "CaloOnlineTools/HcalOnlineDb/interface/HcalQIEManager.h"
00006 #include "CaloOnlineTools/HcalOnlineDb/interface/LMap.h"
00007
00008 using namespace std;
00009
00018 HcalLutManager::HcalLutManager( void )
00019 {
00020 init();
00021 }
00022
00023
00024
00025 void HcalLutManager::init( void )
00026 {
00027 lut_xml = NULL;
00028 }
00029
00030
00031
00032 HcalLutManager::~HcalLutManager( void )
00033 {
00034 if (lut_xml) delete lut_xml;
00035 }
00036
00037
00038
00039 std::string & HcalLutManager::getLutXml( std::vector<unsigned int> & _lut )
00040 {
00041
00042 if (lut_xml) delete lut_xml;
00043
00044 lut_xml = new LutXml();
00045
00046 LutXml::Config _config;
00047 _config.lut = _lut;
00048 lut_xml -> addLut( _config );
00049 lut_xml -> addLut( _config );
00050 lut_xml -> addLut( _config );
00051
00052
00053 return lut_xml->getCurrentBrick();
00054
00055 }
00056
00057
00058 int HcalLutManager::getInt( string number )
00059 {
00060 int result;
00061 sscanf(number.c_str(), "%d", &result);
00062 return result;
00063 }
00064
00065 HcalSubdetector HcalLutManager::get_subdetector( string _det )
00066 {
00067 HcalSubdetector result;
00068 if ( _det.find("HB") != string::npos ) result = HcalBarrel;
00069 else if ( _det.find("HE") != string::npos ) result = HcalEndcap;
00070 else if ( _det.find("HF") != string::npos ) result = HcalForward;
00071 else if ( _det.find("HO") != string::npos ) result = HcalOuter;
00072 else result = HcalOther;
00073
00074 return result;
00075 }
00076
00077
00078 int HcalLutManager_test::getLutSetFromFile_test( string _filename )
00079 {
00080 HcalLutManager _manager;
00081 HcalLutSet _set = _manager . getLutSetFromFile( _filename );
00082 cout << "===> Test of HcalLutSet HcalLutManager::getLutSetFromFile( string _filename )" << endl << endl;
00083 cout << _set . label << endl;
00084 for (unsigned int i = 0; i != _set.subdet.size(); i++) cout << _set.subdet[i] << " ";
00085 cout << endl;
00086 for (unsigned int i = 0; i != _set.eta_min.size(); i++) cout << _set.eta_min[i] << " ";
00087 cout << endl;
00088 for (unsigned int i = 0; i != _set.eta_max.size(); i++) cout << _set.eta_max[i] << " ";
00089 cout << endl;
00090 for (unsigned int i = 0; i != _set.phi_min.size(); i++) cout << _set.phi_min[i] << " ";
00091 cout << endl;
00092 for (unsigned int i = 0; i != _set.phi_max.size(); i++) cout << _set.phi_max[i] << " ";
00093 cout << endl;
00094 for (unsigned int i = 0; i != _set.depth_min.size(); i++) cout << _set.depth_min[i] << " ";
00095 cout << endl;
00096 for (unsigned int i = 0; i != _set.depth_max.size(); i++) cout << _set.depth_max[i] << " ";
00097 cout << endl;
00098 for (unsigned int j = 0; j != _set.lut[0].size(); j++){
00099 for (unsigned int i = 0; i != _set.lut.size(); i++){
00100 cout << _set.lut[i][j] << " ";
00101 }
00102 cout << "---> " << j << endl;
00103 }
00104 }
00105
00106
00107 HcalLutSet HcalLutManager::getLutSetFromFile( string _filename )
00108 {
00109 HcalLutSet _lutset;
00110
00111 ifstream infile( _filename . c_str() );
00112 string buf;
00113
00114 if ( infile . is_open() ){
00115 cout << "File " << _filename << " is open..." << endl;
00116 cout << "Reading LUTs and their eta/phi/depth/subdet ranges...";
00117
00118
00119 getline( infile, _lutset . label );
00120
00121
00122 getline( infile, buf );
00123 _lutset . subdet = HcalQIEManager::splitString( buf );
00124
00125
00126 vector<string> buf_vec;
00127 getline( infile, buf );
00128 buf_vec = HcalQIEManager::splitString( buf );
00129 for (vector<string>::const_iterator iter = buf_vec.begin(); iter != buf_vec.end(); iter++){
00130 _lutset.eta_min.push_back(HcalLutManager::getInt(*iter));
00131 }
00132
00133
00134 getline( infile, buf );
00135 buf_vec = HcalQIEManager::splitString( buf );
00136 for (vector<string>::const_iterator iter = buf_vec.begin(); iter != buf_vec.end(); iter++){
00137 _lutset.eta_max.push_back(HcalLutManager::getInt(*iter));
00138 }
00139
00140
00141 getline( infile, buf );
00142 buf_vec = HcalQIEManager::splitString( buf );
00143 for (vector<string>::const_iterator iter = buf_vec.begin(); iter != buf_vec.end(); iter++){
00144 _lutset.phi_min.push_back(HcalLutManager::getInt(*iter));
00145 }
00146
00147
00148 getline( infile, buf );
00149 buf_vec = HcalQIEManager::splitString( buf );
00150 for (vector<string>::const_iterator iter = buf_vec.begin(); iter != buf_vec.end(); iter++){
00151 _lutset.phi_max.push_back(HcalLutManager::getInt(*iter));
00152 }
00153
00154
00155 getline( infile, buf );
00156 buf_vec = HcalQIEManager::splitString( buf );
00157 for (vector<string>::const_iterator iter = buf_vec.begin(); iter != buf_vec.end(); iter++){
00158 _lutset.depth_min.push_back(HcalLutManager::getInt(*iter));
00159 }
00160
00161
00162 getline( infile, buf );
00163 buf_vec = HcalQIEManager::splitString( buf );
00164 for (vector<string>::const_iterator iter = buf_vec.begin(); iter != buf_vec.end(); iter++){
00165 _lutset.depth_max.push_back(HcalLutManager::getInt(*iter));
00166 }
00167
00168 bool first_lut_entry = true;
00169 while ( getline( infile, buf ) > 0 ){
00170 buf_vec = HcalQIEManager::splitString( buf );
00171 for (unsigned int i = 0; i < buf_vec.size(); i++){
00172 if (first_lut_entry){
00173 vector<unsigned int> _l;
00174 _lutset.lut.push_back(_l);
00175 }
00176 _lutset . lut[i] . push_back(HcalLutManager::getInt(buf_vec[i]));
00177 }
00178 first_lut_entry = false;
00179 }
00180 }
00181
00182 cout << "done." << endl;
00183
00184 return _lutset;
00185 }
00186
00187 std::string HcalLutManager::getLutXmlFromAsciiMaster( string _filename, string _tag, int _crate, bool split_by_crate )
00188 {
00189
00190 map<int, shared_ptr<LutXml> > _xml;
00191
00192 LMap _lmap;
00193 _lmap . read( "HCALmapHBEF.txt", "HBEF" );
00194 _lmap . read( "HCALmapHO.txt", "HO" );
00195 map<int,LMapRow> & _map = _lmap.get_map();
00196 cout << "LMap contains " << _map . size() << " channels" << endl;
00197
00198
00199 HcalLutSet _set = getLutSetFromFile( _filename );
00200 int lut_set_size = _set.lut.size();
00201
00202
00203 for( map<int,LMapRow>::const_iterator row=_map.begin(); row!=_map.end(); row++ ){
00204 LutXml::Config _cfg;
00205
00206
00207
00208 int lut_index=-1;
00209 for ( int i=0; i<lut_set_size; i++ ){
00210 if ( (row->second.crate == _crate || _crate == -1) &&
00211 _set.eta_min[i] <= row->second.side*row->second.eta &&
00212 _set.eta_max[i] >= row->second.side*row->second.eta &&
00213 _set.phi_min[i] <= row->second.phi &&
00214 _set.phi_max[i] >= row->second.phi &&
00215 _set.depth_min[i] <= row->second.depth &&
00216 _set.depth_max[i] >= row->second.depth &&
00217 get_subdetector(_set.subdet[i]) == row->second.det ){
00218 lut_index=i;
00219 }
00220 }
00221 if ( lut_index >= 0 ){
00222 if ( _xml.count(row->second.crate) == 0 && split_by_crate ){
00223 _xml.insert( pair<int,shared_ptr<LutXml> >(row->second.crate,shared_ptr<LutXml>(new LutXml())) );
00224 }
00225 else if ( _xml.count(0) == 0 ){
00226 _xml.insert( pair<int,shared_ptr<LutXml> >(0,shared_ptr<LutXml>(new LutXml())) );
00227 }
00228 _cfg.ieta = row->second.side*row->second.eta;
00229 _cfg.iphi = row->second.phi;
00230 _cfg.depth = row->second.depth;
00231 _cfg.crate = row->second.crate;
00232 _cfg.slot = row->second.htr;
00233 if (row->second.fpga . find("top") != string::npos) _cfg.topbottom = 1;
00234 else if (row->second.fpga . find("bot") != string::npos) _cfg.topbottom = 0;
00235 else cout << "Warning! fpga out of range..." << endl;
00236 _cfg.fiber = row->second.rm_fi;
00237 _cfg.fiberchan = row->second.fi_ch;
00238 if (_set.lut[lut_index].size() == 128) _cfg.lut_type = 1;
00239 else _cfg.lut_type = 2;
00240 _cfg.creationtag = _tag;
00241 _cfg.creationstamp = get_time_stamp( time(0) );
00242 _cfg.targetfirmware = "1.0.0";
00243 _cfg.formatrevision = "1";
00244 _cfg.generalizedindex =
00245 _cfg.iphi*10000+_cfg.depth*1000+
00246 (row->second.side>0)*100+row->second.eta;
00247 _cfg.lut = _set.lut[lut_index];
00248 if (split_by_crate ){
00249 _xml[row->second.crate]->addLut( _cfg );
00250 }
00251 else{
00252 _xml[0]->addLut( _cfg );
00253 }
00254 }
00255 }
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266 string result;
00267 for (map<int,shared_ptr<LutXml> >::const_iterator cr = _xml.begin(); cr != _xml.end(); cr++){
00268 result = cr->second->getString();
00269 stringstream output_file_name;
00270 if ( split_by_crate ){
00271 output_file_name << _tag << "_" << cr->first << ".xml";
00272 }
00273 else{
00274 output_file_name << _tag << ".xml";
00275 }
00276 cr->second->write( output_file_name.str().c_str() );
00277 }
00278
00279 return result;
00280 }
00281
00282
00283
00284 string HcalLutManager::get_time_stamp( time_t _time )
00285 {
00286 char timebuf[50];
00287
00288 strftime( timebuf, 50, "%Y-%m-%d %H:%M:%S", gmtime( &_time ) );
00289 string creationstamp = timebuf;
00290
00291 return creationstamp;
00292 }