00001 #include <iostream>
00002 #include <fstream>
00003 #include<sstream>
00004 #include <exception>
00005
00006 #include "pit_configuration.h"
00007
00008 using namespace std;
00009
00010 pit_configuration::pit_configuration() {
00011
00012
00013
00014 logPitConfiguration = &(log4cpp::Category::getInstance("pitLog.configuration"));
00015 logPitConfiguration->info("Constructor of pit_configuration");
00016
00017
00018 this->setDefaultCoordinates();
00019 this->setDefaultRequiredChannels();
00020
00021 }
00022
00023
00024
00025
00026 pit_configuration& pit_configuration::getInstance() {
00027 static pit_configuration singleton;
00028 return (singleton);
00029 }
00030
00031
00032
00033
00034 void pit_configuration::setDefaultRequiredChannels(void){
00035 for (int board = 0 ; board < NUMBER_OPTIN; board ++){
00036 for (int link = 0 ; link < NUMBER_OPTICAL_LINKS ; link ++){
00037 for(int foChannel=0; foChannel< NUMBER_FO_CHANNELS; foChannel++)
00038 requiredChannels[board][link][foChannel]=true;
00039 }
00040 }
00041
00042 }
00043
00044
00045
00046
00047 void pit_configuration::printPitCoordinateTable() const{
00048
00049 t_sideCoord side = A;
00050
00051 for (unsigned int sector=0; sector < NUMBER_SECTORS; sector ++) {
00052 for (unsigned int half_stave=0; half_stave < NUMBER_HALF_STAVES_SECTOR; half_stave++) {
00053 cout << "Side A sector " << sector << " half stave " << half_stave;
00054 cout << " => OPTIN board " << pit_coordinate[side][sector][half_stave].optinNumber;
00055 cout << " link number " << pit_coordinate[side][sector][half_stave].linkNumber;
00056 cout << endl;
00057 }
00058 }
00059
00060 side = C;
00061 for (unsigned int sector=0; sector < NUMBER_SECTORS; sector ++) {
00062 for (unsigned int half_stave=0; half_stave < NUMBER_HALF_STAVES_SECTOR; half_stave++) {
00063 cout << "Side C sector " << sector << " half stave " << half_stave;
00064 cout << " => OPTIN board " << pit_coordinate[side][sector][half_stave].optinNumber;
00065 cout << " link number " << pit_coordinate[side][sector][half_stave].linkNumber;
00066 cout << endl;
00067 }
00068 }
00069
00070
00071 }
00072
00073
00074
00075
00076
00077 void pit_configuration::printSpdCoordinateTable() const{
00078 for(int optinBoard = 0 ; optinBoard <NUMBER_OPTIN ; optinBoard++ ) {
00079 for (int opticalLink = 0; opticalLink<NUMBER_OPTICAL_LINKS; opticalLink++ ) {
00080 cout << "Board " << optinBoard << " link " << opticalLink;
00081 if ( spd_coordinate[optinBoard][opticalLink].side==A) cout << " => side A ";
00082 if ( spd_coordinate[optinBoard][opticalLink].side==C) cout << " => side C ";
00083 cout << " sector " << spd_coordinate[optinBoard][opticalLink].sector;
00084 cout << " half_stave " << spd_coordinate[optinBoard][opticalLink].half_stave;
00085 cout << endl;
00086 }
00087 }
00088
00089 }
00090
00091
00092
00093
00094
00095
00096
00097 bool pit_configuration::parseRequiredChannelLine(const string &line){
00098
00099
00100 unsigned int sector, halfstave;
00101 char side ;
00102 unsigned board, link;
00103 string requiredFos;
00104
00105
00106 if (line[0]==COMMENT_CHAR)return false;
00107
00108
00109 stringstream stream(line);
00110
00111
00112 stream >> sector >> side >> halfstave >> requiredFos;
00113
00114 link = getLinkNumber(side, sector, halfstave);
00115 board = getBoardNumber(side,sector,halfstave);
00116 this->setRequired( board, link , requiredFos);
00117
00118 return true;
00119
00120 }
00121
00122
00123
00124
00125 void pit_configuration::setRequired(unsigned int board,unsigned int link, const string &foChannelsValues ){
00126 unsigned int foChannel;
00127
00128 for (foChannel = 0 ; (foChannel < NUMBER_FO_CHANNELS) || (foChannel < foChannelsValues.size()) ; foChannel ++){
00129 bool value;
00130 if (foChannelsValues[foChannel]=='1')value = true;
00131 else if (foChannelsValues[foChannel]=='0')value = false;
00132 else throw (out_of_range("(setRequired) if setting required fastor channels with a binary string use only '0' and '1's"));
00133
00134 this->setRequired(board, link , foChannel, value);
00135 }
00136
00137 for (int index = foChannel; index < NUMBER_FO_CHANNELS; index++){
00138 this->setRequired(board, link,index,false);
00139 }
00140 }
00141
00142
00143
00144
00145 void pit_configuration::setRequired(unsigned int board,unsigned int link,unsigned int foChannel, bool value){
00146 if (board >= NUMBER_OPTIN || link >= NUMBER_OPTICAL_LINKS || foChannel >= NUMBER_FO_CHANNELS ){
00147 throw (out_of_range("(setRequired) board number, link or fastor channel out of range"));
00148 }
00149
00150 this->requiredChannels[board][link][foChannel]= value;
00151 }
00152
00153
00154
00155
00156
00157
00158 void pit_configuration::openRequiredChannelsFile(const char * filename){
00159 fstream tableFile;
00160
00161 logPitConfiguration->info("Opening required channels list file %s", filename);
00162
00163 try {
00164 int nLines=0;
00165 tableFile.open(filename, fstream::in);
00166
00167
00168 if (!tableFile.is_open()){
00169 string errorString("(openRequiredChannelsFile) could not open file ");
00170 errorString += filename;
00171 throw (errorString);
00172 }
00173
00174 string line;
00175
00176
00177 while (getline(tableFile, line)){
00178 if (parseRequiredChannelLine(line)) nLines++;
00179 }
00180
00181
00182 if (nLines != NUMBER_HALF_STAVES){
00183 stringstream errorMsg;
00184 errorMsg << "(openRequiredChannelsFile) only "<< nLines << "found in required channels configuration file";
00185 throw( errorMsg.str());
00186 }
00187 tableFile.close();
00188 return;
00189 }
00190 catch(out_of_range error){
00191 logPitConfiguration->error(error.what());
00192 }
00193 catch(string error){
00194 logPitConfiguration->error(error);
00195 }
00196 catch(...){
00197
00198 }
00199
00200 logPitConfiguration->warn("(openRequiredChannelsFile) exception raised, falling back to default values");
00201 this->setDefaultRequiredChannels();
00202 if ( tableFile.is_open() )tableFile.close();
00203 }
00204
00205
00207
00210
00211 bool pit_configuration::parseCoordinateLine(const string &line){
00212
00213
00214
00215 unsigned int optinBoard, optinChannel;
00216 unsigned int sector, halfStave;
00217 char charSide;
00218 t_sideCoord side;
00219
00220
00221 if (line[0]==COMMENT_CHAR) return false;
00222
00223
00224 stringstream stream(line);
00225
00226
00227 stream >> optinBoard>>optinChannel>>sector>>charSide>>halfStave;
00228
00229 if (charSide=='A')side =A;
00230 else side =C;
00231
00232
00233 this->setCoordinate( optinBoard, optinChannel, sector,(t_sideCoord) side, halfStave);
00234 return true;
00235 }
00236
00237
00238
00239
00240
00241 void pit_configuration::openCoordinateTableFile(const char *filename){
00242 fstream tableFile;
00243 logPitConfiguration->info("Opening coordinate table file %s", filename);
00244
00245 try {
00246 int nLines=0;
00247 tableFile.open(filename, fstream::in);
00248
00249
00250 if (!tableFile.is_open()){
00251 string errorString("(openCoordinateTableFile) could not open file ");
00252 errorString += filename;
00253 logPitConfiguration->error(errorString);
00254 throw (errorString);
00255 }
00256
00257
00258 string line;
00259
00260
00261 while (getline(tableFile, line)){
00262 if (parseCoordinateLine(line))nLines++;
00263 }
00264
00265
00266 if (nLines != NUMBER_OPTICAL_LINKS){
00267 stringstream errorMsg;
00268 errorMsg << "(openCoordinateTableFile) only "<< nLines << "found in required channels configuration file";
00269 throw( errorMsg.str());
00270 }
00271
00272 return;
00273 }
00274 catch(out_of_range error){
00275 logPitConfiguration->error(error.what());
00276 }
00277 catch(string error){
00278 logPitConfiguration->error(error);
00279 }
00280 catch(...){
00281
00282 }
00283
00284 logPitConfiguration->warn("(openCoordinateTableFile) exception raised, falling back to default values");
00285 this->setDefaultCoordinates();
00286
00287 if ( tableFile.is_open() )tableFile.close();
00288 }
00289
00290
00291 void pit_configuration::setDefaultCoordinates(void){
00292
00293 t_sideCoord side;
00294 unsigned int sector;
00295 unsigned int half_stave;
00296
00303
00304 for(int optinBoard = 0 ; optinBoard <NUMBER_OPTIN/2 ; optinBoard++ ) {
00305
00306 for (int opticalLink = 0; opticalLink<NUMBER_OPTICAL_LINKS/2; opticalLink++ ){
00307 side = A;
00308 sector = 2*optinBoard;
00309 half_stave = opticalLink;
00310
00311 this->setCoordinate(optinBoard, opticalLink, sector, side, half_stave );
00312
00313 }
00314 for (int opticalLink = NUMBER_OPTICAL_LINKS/2 ; opticalLink<NUMBER_OPTICAL_LINKS; opticalLink++) {
00315 side = A;
00316 sector = 2*optinBoard + 1;
00317 half_stave = opticalLink - NUMBER_OPTICAL_LINKS/2;
00318
00319 this->setCoordinate(optinBoard, opticalLink, sector, side, half_stave);
00320
00321 }
00322 }
00323
00324
00325 for(int optinBoard = NUMBER_OPTIN/2 ; optinBoard <NUMBER_OPTIN ; optinBoard++ ) {
00326
00327
00328 for (int opticalLink = 0; opticalLink<NUMBER_OPTICAL_LINKS/2; opticalLink++ ){
00329 side = C;
00330 sector = 2*(optinBoard-NUMBER_OPTIN/2);
00331 half_stave = opticalLink;
00332
00333 this->setCoordinate(optinBoard, opticalLink, sector, side, half_stave);
00334
00335 }
00336 for (int opticalLink = NUMBER_OPTICAL_LINKS/2 ; opticalLink<NUMBER_OPTICAL_LINKS; opticalLink++) {
00337 side = C;
00338 sector = 2*(optinBoard-NUMBER_OPTIN/2) + 1;
00339 half_stave = opticalLink - NUMBER_OPTICAL_LINKS/2 ;
00340
00341 this->setCoordinate(optinBoard, opticalLink, sector, side, half_stave);
00342 }
00343 }
00344
00345 }
00346
00347 void pit_configuration::setCoordinate(unsigned int optinBoard, unsigned int optinChannel,
00348 unsigned int sector, t_sideCoord side, unsigned int halfStave){
00349
00350
00351 if ( (optinBoard >= NUMBER_OPTIN) || (optinChannel >= NUMBER_OPTICAL_LINKS) ||
00352 (sector >= NUMBER_SECTORS) || (side >= NUMBER_SIDES) || (halfStave >= NUMBER_HALF_STAVES_SECTOR)){
00353
00354
00355 logPitConfiguration->crit("(setCoordinate) out of range %d,%d,%d,%d,%d",
00356 optinBoard, optinChannel, side, sector, halfStave);
00357
00358 throw out_of_range("(setCoordinate)optin board or optin channel out of range");
00359
00360 }
00361
00362
00363
00364 spd_coordinate[optinBoard][optinChannel].side = side;
00365 spd_coordinate[optinBoard][optinChannel].sector= sector;
00366 spd_coordinate[optinBoard][optinChannel].half_stave= halfStave;
00367
00368 pit_coordinate[side][sector][halfStave].optinNumber = optinBoard;
00369 pit_coordinate[side][sector][halfStave].linkNumber = optinChannel;
00370
00371 }
00372
00373 unsigned int pit_configuration::getBoardNumber(char side , unsigned int sector , unsigned int halfstave)const {
00374 t_sideCoord coordSide;
00375
00376 if (side =='A'|| side =='a'){
00377 coordSide=A;
00378 }
00379 else if (side =='C'|| side =='c'){
00380 coordSide=C;
00381 }
00382 else {
00383 logPitConfiguration->error("(pit_configuration::getBoardNumber) side has to be 'A' or 'C' ");
00384 throw out_of_range("(pit_configuration::getBoardNumber): side has to be 'A' or 'C' ");
00385 }
00386
00387 return getBoardNumber(coordSide, sector, halfstave);
00388 }
00389
00390 unsigned int pit_configuration::getLinkNumber(char side , unsigned int sector, unsigned int halfstave)const {
00391 t_sideCoord coordSide;
00392
00393 if (side =='A'|| side =='a'){
00394 coordSide=A;
00395 }
00396 else if (side =='C'|| side =='c'){
00397 coordSide=C;
00398 }
00399 else {
00400 logPitConfiguration->error("(pit_configuration::getLinkNumber): side has to be 'A' or 'C' ");
00401 throw out_of_range ("(pit_configuration::getLinkNumber): side has to be 'A' or 'C' ");
00402 }
00403
00404 return getLinkNumber(coordSide, sector, halfstave);
00405 }
00406
00407 unsigned int pit_configuration::getBoardNumber(t_sideCoord side,unsigned int sector,unsigned int halfstave)const{
00408
00409 if ((sector >= NUMBER_SECTORS) || (halfstave >= NUMBER_HALF_STAVES_SECTOR)){
00410 logPitConfiguration->error("(pit_configuration::getBoardNumber): out of range %d,%d",sector,halfstave);
00411 throw out_of_range ("(pit_configuration::getBoardNumber): sector or halfstave out of range");
00412 }
00413 return pit_coordinate[side][sector][halfstave].optinNumber;
00414 }
00415
00416 unsigned int pit_configuration::getLinkNumber(t_sideCoord side,unsigned int sector,unsigned int halfstave)const{
00417 if ((sector >= NUMBER_SECTORS) || (halfstave >= NUMBER_HALF_STAVES_SECTOR)){
00418 logPitConfiguration->error("(pit_configuration::getLinkNumber) out of range %d,%d",sector,halfstave);
00419 throw out_of_range ("(pit_configuration::getLinkNumber): sector or halfstave out of range");
00420 }
00421 return pit_coordinate[side][sector][halfstave].linkNumber;
00422 }
00423
00424 bool pit_configuration::getFastorRequired(unsigned int board, unsigned int link, unsigned int foChannel )const{
00425 if ( (board >= NUMBER_OPTIN) || (link >= NUMBER_OPTICAL_LINKS) || foChannel >= (NUMBER_FO_CHANNELS)){
00426 logPitConfiguration->error("(pit_configuration::getFastorRequired): out of range %d,%d,%d", board,link,foChannel);
00427 throw out_of_range("(pit_configuration::getFastorRequired): board, link or fastor channel out of range");
00428 }
00429
00430 return requiredChannels[board][link][foChannel];
00431 }
00432
00433 string pit_configuration::getFastorRequired(unsigned int board, unsigned int link)const{
00434 string out("");
00435
00436 for (int foChannel = 0 ; foChannel < NUMBER_FO_CHANNELS; foChannel ++){
00437
00438 if (requiredChannels[board][link][foChannel] ) out.push_back('1');
00439 else out.push_back('0');
00440 }
00441
00442 return out;
00443 }
00444 void pit_configuration::printRequiredChannelList()const{
00445 for(int board = 0; board < NUMBER_OPTIN ; board ++){
00446 for (int link = 0 ; link < NUMBER_OPTICAL_LINKS ; link ++){
00447 cout << "optin board "<< board << ", optical Link " << link<<" = " << getFastorRequired(board, link).c_str() << endl;
00448 }
00449 }
00450
00451 }
00452
00453 bool pit_configuration::getLinkRequired(unsigned int board,unsigned int link)const{
00454
00455 string foRequired =this->getFastorRequired(board, link);
00456
00457 if (foRequired=="0000000000") return true;
00458 else return false;
00459 }
00460 void pit_configuration::dumpCoordinateTableToFile(const char *filename)const{
00461 fstream coordinateListFile;
00462 logPitConfiguration->info("dumping coordinate list to %s",filename);
00463 coordinateListFile.open(filename, fstream::out);
00464
00465 if (!coordinateListFile.is_open()){
00466 logPitConfiguration->error("(dumpCoordinateTableToFile)could not open %s",filename);
00467 return;
00468 }
00469
00470 coordinateListFile<<"#coordinate file table\n";
00471 coordinateListFile<<"# board\tlink\tsector\tside\thalfstave\n";
00472
00473 char charSide;
00474
00475 for (int board = 0 ; board < NUMBER_OPTIN ; board ++){
00476 for (int link = 0 ; link < NUMBER_OPTICAL_LINKS; link ++){
00477
00478 if (getSide(board, link)==A)charSide='A';
00479 else charSide='C';
00480
00481 coordinateListFile<<"\t"<< board<< '\t'<< link;
00482 coordinateListFile<<"\t"<<getSector(board, link);
00483 coordinateListFile<<"\t\t"<<charSide;
00484 coordinateListFile<<"\t"<<getHalfStave(board,link)<<endl;
00485 }
00486 }
00487 }
00488
00489 void pit_configuration::dumpRequiredLinksToFile(const char * filename)const{
00490 fstream requiredListFile;
00491
00492 logPitConfiguration->info("dumping required link list to %s",filename);
00493 requiredListFile.open(filename, fstream::out);
00494 if (!requiredListFile.is_open()){
00495 logPitConfiguration->error("(dumpRequiredLinksToFile)could not open file %s",filename);
00496
00497 return;
00498 }
00499
00500 requiredListFile<<"#required fastors file table\n";
00501 requiredListFile<<"# sector\tside\thalfstave\tfastorlist\n";
00502
00503 char charSide;
00504 unsigned int link, board;
00505
00506 for (int sector = 0 ; sector < NUMBER_SECTORS ; sector ++){
00507 for (int side = 0 ; side < NUMBER_SIDES; side ++){
00508 for (int halfstave = 0 ; halfstave < NUMBER_HALF_STAVES_SECTOR; halfstave ++){
00509 if (side == 0) charSide='A';
00510 else charSide='C';
00511 link=getLinkNumber(charSide, sector, halfstave);
00512 board= getBoardNumber(charSide, sector, halfstave);
00513 requiredListFile << '\t'<<sector<<'\t'<<charSide<<'\t'<<halfstave;
00514 requiredListFile << "\t\t"<< getFastorRequired(board, link).c_str()<< endl;
00515 }
00516 }
00517 }
00518 }