00001
00002
00010 #include <iostream>
00011 #include <fstream>
00012 #include <sstream>
00013 #include <vector>
00014 #include <cassert>
00015 #include <cstdlib>
00016 #include <cstring>
00017
00018 #include "ecalDccMap.h"
00019
00020 #ifdef __APPLE__
00021 #include <errno.h>
00022
00023
00024 #ifndef SIZE_MAX
00025 # define SIZE_MAX ((size_t) -1)
00026 #endif
00027 #ifndef SSIZE_MAX
00028 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
00029 #endif
00030 namespace {
00031 ssize_t getline (char **lineptr, size_t *n, FILE *fp)
00032 {
00033 ssize_t result;
00034 size_t cur_len = 0;
00035
00036 if (lineptr == NULL || n == NULL || fp == NULL)
00037 {
00038 errno = EINVAL;
00039 return -1;
00040 }
00041
00042 if (*lineptr == NULL || *n == 0)
00043 {
00044 *n = 120;
00045 *lineptr = (char *) malloc (*n);
00046 if (*lineptr == NULL)
00047 {
00048 result = -1;
00049 goto end;
00050 }
00051 }
00052
00053 for (;;)
00054 {
00055 int i;
00056
00057 i = getc (fp);
00058 if (i == EOF)
00059 {
00060 result = -1;
00061 break;
00062 }
00063
00064
00065 if (cur_len + 1 >= *n)
00066 {
00067 size_t needed_max =
00068 SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX;
00069 size_t needed = 2 * *n + 1;
00070 char *new_lineptr;
00071
00072 if (needed_max < needed)
00073 needed = needed_max;
00074 if (cur_len + 1 >= needed)
00075 {
00076 result = -1;
00077 goto end;
00078 }
00079
00080 new_lineptr = (char *) realloc (*lineptr, needed);
00081 if (new_lineptr == NULL)
00082 {
00083 result = -1;
00084 goto end;
00085 }
00086
00087 *lineptr = new_lineptr;
00088 *n = needed;
00089 }
00090
00091 (*lineptr)[cur_len] = i;
00092 cur_len++;
00093
00094 if (i == '\n')
00095 break;
00096 }
00097 (*lineptr)[cur_len] = '\0';
00098 result = cur_len ? (ssize_t) cur_len : result;
00099
00100 end:
00101 return result;
00102 }
00103 }
00104 #endif
00105
00106
00107 using namespace std;
00108
00111 const static int nBarrelEtaBins = 170;
00114 const static int nBarrelPhiBins = 360;
00117 const static int nEndcapXBins = 100;
00120 const static int nEndcapYBins = 100;
00123 const static int supercrystalEdge = 5;
00126 const static int nSupercrystalXBins = nEndcapXBins/supercrystalEdge;
00129 const static int nSupercrystalYBins = nEndcapYBins/supercrystalEdge;
00132 const static int nBarrelTowerEtaBins = nBarrelEtaBins/5;
00135 const static int nBarrelTowerPhiBins = nBarrelPhiBins/5;
00138 const static int nEndcaps = 2;
00141 const static int nEndcapTTInEta = 11;
00144 const static int nBarrelTTInEta = 34;
00147 const static int nTTInEta =
00148 2*nEndcapTTInEta+nBarrelTTInEta;
00151 const static int nTTInPhi = 72;
00154 const int nABInEta=4;
00157 const int nABInPhi=3;
00160 const int nDCCEE = 9;
00161 const int nABABCh = 8;
00162 const int nABTCCCh = 12;
00163 const int nDCCCh = 12;
00164 const int nTCCInEta = 6;
00165 const int nAB = nABInPhi*nABInEta;
00166 const int nTTInABAlongEta=nTTInEta/nABInEta;
00167 const int nTTInABAlongPhi=nTTInPhi/nABInPhi;
00168 const int iTTEtaMin[nABInEta] = {0,11,28,45};
00169 const int iTTEtaMax[nABInEta] = {10,27,44,55};
00170 const int iTTEtaSign[nABInEta] = {-1,-1,1,1};
00171
00172
00173
00174
00175 const int iTCCEtaBounds[nTCCInEta+1] = {0,7,11,28,45,49,56};
00176
00177 const char* abTTFFilePrefix = "TTF_AB";
00178 const char* abTTFFilePostfix = ".txt";
00179 const char* abSRFFilePrefix = "AF_AB";
00180 const char* abSRFFilePostfix = ".txt";
00181 const char* abIOFilePrefix = "IO_AB";
00182 const char* abIOFilePostfix = ".txt";
00183
00184 const char* srfFilename = "SRF.txt";
00185 const char* ttfFilename = "TTF.txt";
00186 const char* xconnectFilename = "xconnect_universal.txt";
00187
00188 const char srpFlagMarker[] = {'.', 'S', 'N', 'C', '4','5','6','7'};
00189 const char tccFlagMarker[] = {'.', 'S', '?', 'C', '4', '5', '6', '7'};
00190
00191 char srp2roFlags[128];
00192
00193 typedef enum {suppress=0, sr2, sr1, full, fsuppress, fsr2, fsr1, ffull} roAction_t;
00194 char roFlagMarker[] = {'.', 'z', 'Z', 'F',
00195 '4', '5', '6', '7'
00196 };
00197
00198 const int nactions = 8;
00199
00200 roAction_t actions[nactions] = {sr2,full, full,full,
00201 sr2,sr2,sr2,sr2};
00202
00203
00204 vector<pair<int, int> > ecalDccSC[nEndcaps][nDCCEE];
00205
00206 void fillABTTFFiles(const char ttFlags[nTTInEta][nTTInPhi],
00207 ofstream files[]);
00208 void fillABSRPFiles(const char barrelSrFlags[nBarrelTTInEta][nTTInPhi],
00209 const char endcapSrFlags[nEndcaps][nSupercrystalXBins]
00210 [nSupercrystalYBins],
00211 ofstream files[]);
00212 void fillABIOFiles(const char ttFlags[nTTInEta][nTTInPhi],
00213 const char barrelSrFlags[nBarrelTTInEta][nTTInPhi],
00214 const char endcapSrFlags[nEndcaps][nSupercrystalXBins]
00215 [nSupercrystalYBins],
00216 ofstream files[]);
00217 inline int abNum(int iABEta, int iABPhi){return 3*iABEta+iABPhi;}
00218
00219 bool readTTF(FILE* file, char ttFlags[nTTInEta][nTTInPhi]);
00220 bool readSRF(FILE* file,
00221 char barrelSrFlags[nBarrelTTInEta][nTTInPhi],
00222 char endcapSrFlags[nEndcaps][nSupercrystalXBins]
00223 [nSupercrystalYBins]);
00224
00225 void writeABTTFFileHeader(ofstream& f, int abNum);
00226 void writeABSRFFileHeader(ofstream& f, int abNum);
00227 void writeABIOFileHeader(ofstream& f, int abNum);
00228 string getFlagStream(char flags[nTTInEta][nTTInPhi], int iEtaMin, int iEtaMax,
00229 int iPhiMin, int iPhiMax);
00230 string getABTCCInputStream(const char tccFlags[nTTInEta][nTTInPhi],
00231 int iABEta, int iABPhi, int iTCCCh);
00232 void getABTTPhiBounds(int iABPhi, int& iTTPhiMin, int& iTTPhiMax);
00233 string getABABOutputStream(const char tccFlags[nTTInEta][nTTInPhi],
00234 int iABEta, int iABPhi, int iABCh);
00235 string getABABInputStream(const char tccFlags[nTTInEta][nTTInPhi], int iABEta,
00236 int iABPhi, int iABCh);
00237 string getABDCCOutputStream(const char barrelSrFlags[nBarrelTTInEta][nTTInPhi],
00238 const char endcapSrFlags[nEndcaps][nSupercrystalXBins]
00239 [nSupercrystalYBins],
00240 int iABEta, int iABPhi, int DCCCh);
00241 void abConnect(int iAB,int iABCh,int& iOtherAB,int& iOtherABCh);
00242
00243 int iEvent = 0;
00244
00245 int theAB = -1;
00246
00247 int main(int argc, char* argv[]){
00248 char barrelSrFlags[nBarrelTTInEta][nTTInPhi];
00249 char endcapSrFlags[nEndcaps][nEndcapXBins/5][nEndcapYBins/5];
00250 char ttFlags[nTTInEta][nTTInPhi];
00251 ofstream abTTFFiles[nAB];
00252 ofstream abSRFFiles[nAB];
00253 ofstream abIOFiles[nAB];
00254
00255 int iarg = 0;
00256 while(++iarg<argc){
00257 if(strcmp(argv[iarg],"-h")==0||strcmp(argv[iarg],"--help")==0){
00258 cout << "Usage: GenABIO [OPTIONS]\n\n"
00259 "Produces TT and SR flag files for each SRP board from TTF.txt and "
00260 "SRF.txt global flag files. Requires the SRP cross-connect description"
00261 " description file (xconnect_universal.txt). TTF.txt, SRF.txt and "
00262 "xconnect_universal.txt must be in the directory the command is "
00263 "launched.\n\n"
00264 "OPTIONS:\n"
00265 " -A, --actions IJKLMNOP. IJKLMNOP I..P integers from 0 to 7.\n"
00266 " I: action flag for low interest RUs\n"
00267 " J: action flag for single RUs\n"
00268 " K: action flag for neighbour RUs\n"
00269 " L: action flag for centers RUs\n"
00270 " M: action flag for forced low interest RUs\n"
00271 " N: action flag for forced single RUs\n"
00272 " O: action flag for forced neighbour RUs\n"
00273 " P: action flag for forced centers RUs\n\n"
00274 " -h, --help display this help\n"
00275 " -a n, --ab n specifies indices of the AB whose file must be "
00276 "produced. The ab number runs from 1 to 12. Use -1 to produce files "
00277 "for every AB\n\n"
00278 ;
00279
00280 return 0;
00281 }
00282
00283 if(!strcmp(argv[iarg],"-A") || !strcmp(argv[iarg],"-A")){
00284 if(++iarg>=argc){ cout << "Option error. Try -h\n"; return 1; }
00285 for(int i=0; i<8; ++i){
00286 int act = argv[iarg][i]-'0';
00287 if(act<0 || act>=nactions){
00288 cout << "Error. Action argument is invalid.\n";
00289 return 1;
00290 } else{
00291 actions[i] = (roAction_t)act;
00292 }
00293 }
00294 continue;
00295 }
00296 if(!strcmp(argv[iarg],"-a")||!strcmp(argv[iarg],"--ab")){
00297 if(++iarg>=argc){ cout << "Option error. Try -h\n"; return 1; }
00298 theAB = strtoul(argv[iarg], 0, 0);
00299 if(theAB>=0) --theAB;
00300 if(theAB<-1 || theAB>11){
00301 cout << "AB number is incorrect. Try -h option to get help.\n";
00302 }
00303 continue;
00304 }
00305 }
00306
00307 for(size_t i=0; i<sizeof(srp2roFlags)/sizeof(srp2roFlags[0]); srp2roFlags[i++]='?');
00308 for(size_t i=0; i<sizeof(actions)/sizeof(actions[0]); ++i){
00309 srp2roFlags[(int)srpFlagMarker[i]] = roFlagMarker[actions[i]];
00310 }
00311
00312 for(int iEE=0; iEE < nEndcaps; ++iEE){
00313 for(int iY = 0; iY < nSupercrystalXBins; ++iY){
00314 for(int iX = 0; iX < nSupercrystalYBins; ++iX){
00315 int iDCCPhi = dccPhiIndexOfRU(iEE==0?0:2,iX,iY);
00316 if(iDCCPhi>=0){
00317 ecalDccSC[iEE][iDCCPhi].push_back(pair<int,int>(iX,iY));
00318 }
00319 }
00320 }
00321 }
00322
00323 stringstream s;
00324 for(int iAB=0; iAB< nAB; ++iAB){
00325 if(theAB!=-1 && theAB!=iAB) continue;
00326 s.str("");
00327 s << abTTFFilePrefix << (iAB<9?"0":"") << iAB+1 << abTTFFilePostfix;
00328 abTTFFiles[iAB].open(s.str().c_str(), ios::out);
00329 writeABTTFFileHeader(abTTFFiles[iAB], iAB);
00330 s.str("");
00331 s << abSRFFilePrefix << (iAB<9?"0":"") << iAB+1 << abSRFFilePostfix;
00332 abSRFFiles[iAB].open(s.str().c_str(), ios::out);
00333 writeABSRFFileHeader(abSRFFiles[iAB], iAB);
00334 s.str("");
00335 s << abIOFilePrefix << (iAB<9?"0":"") << iAB+1 << abIOFilePostfix;
00336 abIOFiles[iAB].open(s.str().c_str(), ios::out);
00337 writeABIOFileHeader(abIOFiles[iAB], iAB);
00338 }
00339
00340 FILE* srfFile = fopen(srfFilename, "r");
00341 if(srfFile==NULL){
00342 cerr << "Failed to open SRF file, " << srfFilename << endl;
00343 exit(EXIT_FAILURE);
00344 }
00345
00346 FILE* ttfFile = fopen(ttfFilename, "r");
00347 if(ttfFile==NULL){
00348 cerr << "Failed to open TTF file, " << ttfFilename << endl;
00349 exit(EXIT_FAILURE);
00350 }
00351
00352 iEvent = 0;
00353 while(readSRF(srfFile,barrelSrFlags,endcapSrFlags)
00354 && readTTF(ttfFile,ttFlags)){
00355 if(iEvent%100==0){
00356 cout << "Event " << iEvent << endl;
00357 }
00358 fillABTTFFiles(ttFlags,abTTFFiles);
00359 fillABSRPFiles(barrelSrFlags, endcapSrFlags, abSRFFiles);
00360 fillABIOFiles(ttFlags,barrelSrFlags,endcapSrFlags,abIOFiles);
00361 ++iEvent;
00362 }
00363
00364 return 0;
00365 }
00366
00370 void fillABTTFFiles(const char ttFlags[nTTInEta][nTTInPhi],
00371 ofstream files[]){
00372 for(int iABEta=0; iABEta<nABInEta; ++iABEta){
00373 for(int iABPhi=0; iABPhi<nABInPhi; ++iABPhi){
00374 int iAB = abNum(iABEta,iABPhi);
00375 int iTTPhiMin;
00376 int iTTPhiMax;
00377 getABTTPhiBounds(iABPhi, iTTPhiMin, iTTPhiMax);
00378
00379 files[iAB] << "# Event " << iEvent << "\n";
00380
00381 for(int i = 0 ; i <= iTTEtaMax[iABEta]-iTTEtaMin[iABEta];
00382 ++i){
00383 int iTTEta;
00384 if(iTTEtaSign[iABEta]>0){
00385 iTTEta = iTTEtaMin[iABEta] + i;
00386 } else{
00387 iTTEta = iTTEtaMax[iABEta] - i;
00388 }
00389 for(int iTTPhi = iTTPhiMin;
00390 mod(iTTPhiMax-iTTPhi,nTTInPhi) < nTTInABAlongPhi;
00391 iTTPhi = mod(++iTTPhi, nTTInPhi)){
00392 files[iAB] << ttFlags[iTTEta][iTTPhi];
00393 }
00394 files[iAB] << "\n";
00395 }
00396 files[iAB] << "#\n";
00397
00398 }
00399 }
00400 }
00401
00402 void fillABSRPFiles(const char barrelSrFlags[nBarrelTTInEta][nTTInPhi],
00403 const char endcapSrFlags[nEndcaps][nSupercrystalXBins]
00404 [nSupercrystalYBins],
00405 ofstream files[nAB]){
00406
00407 for(int iAB=0; iAB < nAB; ++iAB){
00408 files[iAB] << "# Event " << iEvent << "\n";
00409 }
00410
00411 bool lineAppended[nAB];
00412 for(int i=0; i< nAB; lineAppended[i++]=false);
00413
00414
00415 for(int iEE = 0; iEE < nEndcaps; ++iEE){
00416 for(int iX = 0; iX < nSupercrystalXBins; ++iX){
00417 for(int iY=0; iY < nSupercrystalYBins; ++iY){
00418
00419 int iDCC = dccIndexOfRU(iEE==0?0:2,iX,iY);
00420 if(iDCC>=0){
00421 int iAB = abOfDcc(iDCC);
00422 if(!lineAppended[iAB]){
00423 for(int i=0; i< iY; ++i) files[iAB] << ' ';
00424 }
00425 files[iAB] << srp2roFlags[(int)endcapSrFlags[iEE][iX][iY]];
00426 lineAppended[iAB] = true;
00427 }
00428 }
00429 for(int iFile=0; iFile< nAB; ++iFile){
00430 if(lineAppended[iFile]){
00431 files[iFile] << "\n";
00432 lineAppended[iFile] = false;
00433 }
00434 }
00435 }
00436 }
00437
00438
00439 for(int iABEta=1; iABEta<3; ++iABEta){
00440 for(int iABPhi=0; iABPhi<nABInPhi; ++iABPhi){
00441 int iAB = abNum(iABEta,iABPhi);
00442 int iTTPhiMin;
00443 int iTTPhiMax;
00444 getABTTPhiBounds(iABPhi, iTTPhiMin, iTTPhiMax);
00445
00446 for(int i = 0 ; i <= iTTEtaMax[iABEta]-iTTEtaMin[iABEta];
00447 ++i){
00448 int iTTEta;
00449 if(iTTEtaSign[iABEta]>0){
00450 iTTEta = iTTEtaMin[iABEta] + i;
00451 } else{
00452 iTTEta = iTTEtaMax[iABEta] - i;
00453 }
00454 for(int iTTPhi = iTTPhiMin;
00455 mod(iTTPhiMax-iTTPhi,nTTInPhi) < nTTInABAlongPhi;
00456 iTTPhi = mod(++iTTPhi, nTTInPhi)){
00457 files[iAB] << srp2roFlags[(int)barrelSrFlags[iTTEta-nEndcapTTInEta][iTTPhi]];
00458 }
00459 files[iAB] << "\n";
00460 }
00461
00462 files[iAB] << "#\n";
00463 }
00464 }
00465
00466
00467 for(int iAB=0; iAB < nAB; ++iAB){
00468 files[iAB] << "#\n";
00469 }
00470 }
00471
00472 void fillABIOFiles(const char ttFlags[nTTInEta][nTTInPhi],
00473 const char barrelSrFlags[nBarrelTTInEta][nTTInPhi],
00474 const char endcapSrFlags[nEndcaps][nSupercrystalXBins][nSupercrystalYBins],
00475 ofstream files[]){
00476 for(int iABEta=0; iABEta < nABInEta; ++iABEta){
00477 for(int iABPhi=0; iABPhi < nABInPhi; ++iABPhi){
00478 int iAB = abNum(iABEta, iABPhi);
00479
00480 files[iAB] << "# Event " << iEvent << "\n";
00481
00482 for(int iTCC=0; iTCC< nABTCCCh; ++iTCC){
00483 files[iAB] << "ITCC" << iTCC+1 << ":"
00484 << getABTCCInputStream(ttFlags, iABEta, iABPhi, iTCC)
00485 << "\n";
00486 }
00487
00488 for(int iABCh=0; iABCh<nABABCh; ++iABCh){
00489 files[iAB] << "IAB" << iABCh+1 << ":"
00490 << getABABInputStream(ttFlags, iABEta, iABPhi, iABCh)
00491 << "\n";
00492 }
00493
00494 for(int iABCh=0; iABCh<nABABCh; ++iABCh){
00495 files[iAB] << "OAB" << iABCh+1 << ":"
00496 << getABABOutputStream(ttFlags, iABEta, iABPhi, iABCh)
00497 << "\n";
00498 }
00499
00500 for(int iDCCCh=0; iDCCCh<nDCCCh; ++iDCCCh){
00501 files[iAB] << "ODCC";
00502 files[iAB]<< (iDCCCh<=8?"0":"") << iDCCCh+1 << ":"
00503 << getABDCCOutputStream(barrelSrFlags, endcapSrFlags, iABEta, iABPhi,iDCCCh)
00504 << "\n";
00505 }
00506 files[iAB] << "#\n";
00507 }
00508 }
00509 }
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519 bool readTTF(FILE* f, char ttFlags[nTTInEta][nTTInPhi]){
00520 char* buffer = NULL;
00521 size_t bufferSize = 0;
00522 int read;
00523 if(f==NULL) exit(EXIT_FAILURE);
00524 int line=0;
00525 int iEta = 0;
00526 while(iEta<nTTInEta && (read=getline(&buffer, &bufferSize,f))!=-1){
00527 ++line;
00528 char* pos = buffer;
00529 while(*pos==' ' || *pos=='\t') ++pos;
00530 if(*pos!='#' && *pos!='\n'){
00531 if(read-1!=nTTInPhi){
00532 cerr << "Error: line " << line
00533 << " of file "<< ttfFilename << " has incorrect length"
00534
00535 << endl;
00536 exit(EXIT_FAILURE);
00537 }
00538 for(int iPhi=0; iPhi<nTTInPhi; ++iPhi){
00539 ttFlags[iEta][iPhi] = buffer[iPhi];
00540
00541
00542
00543
00544
00545 }
00546 ++iEta;
00547 }
00548 }
00549
00550 return (iEta==nTTInEta)?true:false;
00551 }
00552
00553 bool readSRF(FILE* f,
00554 char barrelSrFlags[nBarrelTTInEta][nTTInPhi],
00555 char endcapSrFlags[nEndcaps][nSupercrystalXBins]
00556 [nSupercrystalYBins]){
00557 char* buffer = NULL;
00558 size_t bufferSize = 0;
00559 int read;
00560 if(f==NULL) exit(EXIT_FAILURE);
00561 int line=0;
00562 int iEta = 0;
00563 int iXm = 0;
00564 int iXp = 0;
00565 int iReadLine = 0;
00566
00567 const int nReadLine = nBarrelTTInEta + nEndcaps*nSupercrystalXBins;
00568 while(iReadLine<nReadLine && (read=getline(&buffer, &bufferSize,f))!=-1){
00569 ++line;
00570 char* pos = buffer;
00571 while(*pos==' ' || *pos=='\t') ++pos;
00572 if(*pos!='#' && *pos!='\n'){
00573
00574 pos = buffer;
00575 if(iReadLine<nSupercrystalXBins){
00576 if(read-1!=nSupercrystalYBins){
00577 cerr << "Error: line " << line
00578 << " of file "<< srfFilename << " has incorrect length"
00579 << " (" << read-1 << " instead of " << nSupercrystalYBins << ")"
00580 << endl;
00581 exit(EXIT_FAILURE);
00582 }
00583 for(int iY=0; iY<nSupercrystalYBins; ++iY){
00584 endcapSrFlags[0][iXm][iY] = buffer[iY];
00585 }
00586 ++iXm;
00587 } else if(iReadLine<nSupercrystalYBins+nBarrelTTInEta){
00588 if(read-1!=nTTInPhi){
00589 cerr << "Error: line " << line
00590 << " of file "<< srfFilename << " has incorrect length"
00591 << " (" << read-1 << " instead of " << nTTInPhi << ")"
00592 << endl;
00593 exit(EXIT_FAILURE);
00594 }
00595 for(int iPhi=0; iPhi<nTTInPhi; ++iPhi){
00596 barrelSrFlags[iEta][iPhi] = buffer[iPhi];
00597 }
00598 ++iEta;
00599 } else if(iReadLine<2*nSupercrystalXBins+nBarrelTTInEta){
00600 if(read-1!=nSupercrystalYBins){
00601 cerr << "Error: line " << line
00602 << " of file "<< srfFilename << " has incorrect length"
00603 << " (" << read-1 << " instead of " << nSupercrystalYBins << ")"
00604 << endl;
00605 exit(EXIT_FAILURE);
00606 }
00607 for(int iY=0; iY<nSupercrystalYBins; ++iY){
00608 endcapSrFlags[1][iXp][iY] = buffer[iY];
00609 }
00610 ++iXp;
00611 }
00612 ++iReadLine;
00613 }
00614 }
00615
00616 return (iReadLine==nReadLine)?true:false;
00617 }
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643 void writeABTTFFileHeader(ofstream& f, int abNum){
00644 time_t t;
00645 time(&t);
00646 const char* date = ctime(&t);
00647 f << "# TTF flag map covered by AB " << abNum+1 <<"\n#\n"
00648 "# Generated on : " << date << "#\n"
00649 "# +---> Phi " << srpFlagMarker[0] << ": 000 (low interest)\n"
00650 "# | " << srpFlagMarker[1] << ": 001 (single)\n"
00651 "# | " << srpFlagMarker[2] << ": 010 (neighbour)\n"
00652 "# V |Eta| " << srpFlagMarker[3] << ": 011 (center)\n"
00653 "#\n";
00654 }
00655
00656 void writeABSRFFileHeader(ofstream& f, int abNum){
00657 time_t t;
00658 time(&t);
00659 const char* date = ctime(&t);
00660 const char* xLabel;
00661 const char* yLabel;
00662 if(abNum<3 || abNum>8){
00663 xLabel = "Y ";
00664 yLabel = "X ";
00665 } else{
00666 xLabel = "Phi";
00667 yLabel = "|Eta|";
00668 }
00669 f << "# SRF flag map covered by AB " << abNum+1 <<"\n#\n"
00670 "# Generated on : " << date << "#\n"
00671 "# +---> " << xLabel << " " << roFlagMarker[0] << ": 000 (suppress)\n"
00672 "# | " << roFlagMarker[1] << ": 010 (SR Threshold 2)\n"
00673 "# | " << roFlagMarker[2] << ": 001 (SR Threshold 1)\n"
00674 "# V " << yLabel << " " << roFlagMarker[3] << ": 011 (Full readout)\n"
00675 "#\n"
00676 "# action table (when forced):\n"
00677 "# LI-> " << roFlagMarker[actions[0]] << " (" << roFlagMarker[actions[4]]<< ")" << "\n"
00678 "# S -> " << roFlagMarker[actions[1]] << " (" << roFlagMarker[actions[5]]<< ")" << "\n"
00679 "# N -> " << roFlagMarker[actions[2]] << " (" << roFlagMarker[actions[6]]<< ")" << "\n"
00680 "# C -> " << roFlagMarker[actions[3]] << " (" << roFlagMarker[actions[7]]<< ")" << "\n";
00681 }
00682
00683 void writeABIOFileHeader(ofstream& f, int abNum){
00684 time_t t;
00685 time(&t);
00686 const char* date = ctime(&t);
00687 f << "# AB " << abNum+1 <<" I/O \n#\n"
00688 "# Generated on : " << date << "#\n"
00689 "# " << srpFlagMarker[0] << ": 000 (low interest) " << tccFlagMarker[0] << ": 000 (low interest) " << roFlagMarker[0] << ": 000 (suppress)\n"
00690 "# " << srpFlagMarker[1] << ": 001 (single) " << tccFlagMarker[1] << ": 001 (mid interest) " << roFlagMarker[1] << ": 010 (SR Threshold 2)\n"
00691 "# " << srpFlagMarker[2] << ": 010 (neighbour) " << tccFlagMarker[2] << ": 010 (not valid) " << roFlagMarker[2] << ": 001 (SR Threshold 1)\n"
00692 "# " << srpFlagMarker[3] << ": 011 (center) " << tccFlagMarker[3] << ": 011 (high interest) " << roFlagMarker[3] << ": 011 (Full readout)\n"
00693 "#\n"
00694 "# action table (when forced):\n"
00695 "# LI-> " << roFlagMarker[actions[0]] << " (" << roFlagMarker[actions[4]]<< ")" << "\n"
00696 "# S -> " << roFlagMarker[actions[1]] << " (" << roFlagMarker[actions[5]]<< ")" << "\n"
00697 "# N -> " << roFlagMarker[actions[2]] << " (" << roFlagMarker[actions[6]]<< ")" << "\n"
00698 "# C -> " << roFlagMarker[actions[3]] << " (" << roFlagMarker[actions[7]]<< ")" << "\n"
00699 "#\n";
00700 }
00701
00702 string getFlagStream(const char flags[nTTInEta][nTTInPhi], int iEtaMin,
00703 int iEtaMax, int iPhiMin, int iPhiMax){
00704 assert(0<=iEtaMin && iEtaMin<=iEtaMax && iEtaMax<nTTInEta);
00705 if(iEtaMin<=nTTInEta/2 && iEtaMax>nTTInEta){
00706 cerr << "Implementation Errror:"
00707 << __FILE__ << ":" << __LINE__
00708 << ": A flag stream cannot covers parts of both half-ECAL!"
00709 << endl;
00710 exit(EXIT_FAILURE);
00711 }
00712
00713 bool zPos = (iEtaMin>=nTTInEta/2);
00714
00715 stringstream buffer;
00716 buffer.str("");
00717 for(int jEta = 0; jEta <= iEtaMax-iEtaMin; ++jEta){
00718
00719 int iEta;
00720 if(zPos){
00721 iEta = iEtaMin + jEta;
00722 } else{
00723 iEta = iEtaMax - jEta;
00724 }
00725
00726 for(int iPhi = mod(iPhiMin,nTTInPhi);
00727 mod(iPhiMax+1-iPhi,nTTInPhi) != 0;
00728 iPhi = mod(++iPhi, nTTInPhi)){
00729 buffer << flags[iEta][iPhi];
00730 }
00731 }
00732
00733 return buffer.str();
00734 }
00735
00736 string getABTCCInputStream(const char tccFlags[nTTInEta][nTTInPhi],
00737 int iABEta, int iABPhi, int iTCCCh){
00738
00739 int iTCCEta;
00740 if(iABEta==1 || iABEta==2){
00741 if(iTCCCh>5) return "";
00742 iTCCEta = 1 + iABEta;
00743 } else{
00744 if(iABEta==0){
00745 iTCCEta = (iTCCCh<6)?1:0;
00746 } else{
00747 iTCCEta = (iTCCCh<6)?4:5;
00748 }
00749 }
00750 int iEtaMin = iTCCEtaBounds[iTCCEta];
00751 int iEtaMax = iTCCEtaBounds[iTCCEta+1]-1;
00752
00753
00754 int iPhiMin;
00755 int iPhiMax;
00756 getABTTPhiBounds(iABPhi, iPhiMin, iPhiMax);
00757
00758
00759
00760 iPhiMin += 4*(iTCCCh%6);
00761 iPhiMax = iPhiMin + 4 - 1;
00762
00763 return getFlagStream(tccFlags, iEtaMin, iEtaMax, iPhiMin, iPhiMax);
00764 }
00765
00766 string getABABOutputStream(const char tccFlags[nTTInEta][nTTInPhi],
00767 int iABEta, int iABPhi, int iABCh){
00768 stringstream buffer;
00769 buffer.str("");
00770 bool barrel = (iABEta==1 || iABEta==2);
00771 switch(iABCh){
00772 case 0:
00773
00774 buffer << getABTCCInputStream(tccFlags, iABEta, iABPhi, 0).substr(0,16);
00775 break;
00776 case 1:
00777
00778 for(int iTCCCh=0; iTCCCh<6; ++iTCCCh){
00779 buffer <<
00780 getABTCCInputStream(tccFlags, iABEta, iABPhi, iTCCCh).substr(0,16);
00781 }
00782 break;
00783 case 2:
00784
00785 buffer << getABTCCInputStream(tccFlags, iABEta, iABPhi, 5).substr(0,16);
00786 break;
00787 case 3:
00788
00789 buffer << getABTCCInputStream(tccFlags, iABEta, iABPhi, 0);
00790 buffer << getABTCCInputStream(tccFlags, iABEta, iABPhi, 6);
00791 break;
00792 case 4:
00793
00794 buffer << getABTCCInputStream(tccFlags, iABEta, iABPhi, 5);
00795 buffer << getABTCCInputStream(tccFlags, iABEta, iABPhi, 11);
00796 break;
00797 case 5:
00798
00799
00800
00801 if(barrel){
00802 string s = getABTCCInputStream(tccFlags, iABEta, iABPhi, 0);
00803 assert(s.size()>=16);
00804 buffer << s.substr(s.size()-16,16);
00805 }
00806 break;
00807 case 6:
00808
00809
00810
00811 if(barrel){
00812 for(int iTCCCh=0; iTCCCh<6; ++iTCCCh){
00813 string s = getABTCCInputStream(tccFlags, iABEta, iABPhi, iTCCCh);
00814 buffer << s.substr(s.size()-16,16);
00815 }
00816 }
00817 break;
00818 case 7:
00819
00820
00821
00822 if(barrel){
00823 string s = getABTCCInputStream(tccFlags, iABEta, iABPhi, 5);
00824 assert(s.size()>=16);
00825 buffer << s.substr(s.size()-16,16);
00826 }
00827 break;
00828 default:
00829 assert(false);
00830 }
00831 return buffer.str();
00832 }
00833
00834 string getABABInputStream(const char tccFlags[nTTInEta][nTTInPhi], int iABEta,
00835 int iABPhi, int iABCh){
00836 int iAB = abNum(iABEta, iABPhi);
00837 int iOtherAB;
00838 int iOtherABCh;
00839 abConnect(iAB,iABCh,iOtherAB,iOtherABCh);
00840 int iOtherABEta = iOtherAB/3;
00841 int iOtherABPhi = iOtherAB%3;
00842 return getABABOutputStream(tccFlags, iOtherABEta, iOtherABPhi, iOtherABCh);
00843 }
00844
00845
00846 void getABTTPhiBounds(int iABPhi, int& iTTPhiMin, int& iTTPhiMax){
00847 iTTPhiMin = mod(-6+iABPhi*nTTInABAlongPhi,nTTInPhi);
00848 iTTPhiMax = mod(iTTPhiMin+nTTInABAlongPhi-1, nTTInPhi);
00849 }
00850
00851 void abConnect(int iAB,int iABCh,int& iOtherAB,int& iOtherABCh){
00852 static bool firstCall = true;
00853 static int xconnectMap[nAB][nABABCh][2];
00854 if(firstCall){
00855 FILE* f = fopen(xconnectFilename, "r");
00856 if(f==NULL){
00857 cerr << "Error. Failed to open xconnect definition file,"
00858 << xconnectFilename << endl;
00859 exit(EXIT_FAILURE);
00860 }
00861
00862 for(int i=0; i<2; ++i){
00863 int c;
00864 while((c=getc(f))!='\n' && c >=0);
00865 }
00866 int ilink = 0 ;
00867 while(!feof(f)){
00868 int abIn;
00869 int pinIn;
00870 int abOut;
00871 int pinOut;
00872 if(4==fscanf(f, "%d\t%d\t%d\t%d", &abIn, &pinIn, &abOut, &pinOut)){
00873 xconnectMap[abIn][pinIn][0] = abOut;
00874 xconnectMap[abIn][pinIn][1] = pinOut;
00875 ++ilink;
00876 }
00877 }
00878 if(ilink!=nAB*nABABCh){
00879 cerr << "Error cross-connect definition file " << xconnectFilename
00880 << " contains an unexpected number of link definition."
00881 << endl;
00882 exit(EXIT_FAILURE);
00883 }
00884 firstCall = false;
00885 }
00886
00887 iOtherAB = xconnectMap[iAB][iABCh][0];
00888 iOtherABCh = xconnectMap[iAB][iABCh][1];
00889 }
00890
00891 string getABDCCOutputStream(const char barrelSrFlags[nBarrelTTInEta][nTTInPhi],
00892 const char endcapSrFlags[nEndcaps][nSupercrystalXBins]
00893 [nSupercrystalYBins],
00894 int iABEta, int iABPhi, int iDCCCh){
00895 bool barrel = (iABEta==1||iABEta==2);
00896 if(barrel){
00897
00898 string stream = getABTCCInputStream(barrelSrFlags-nEndcapTTInEta, iABEta, iABPhi, iDCCCh);
00899
00900 for(size_t i=0; i< stream.size(); ++i){
00901 stream[i] = srp2roFlags[(int)stream[i]];
00902 }
00903 return stream;
00904 } else{
00905 if(iDCCCh<3){
00906
00907 int iEE=(iABEta==0)?0:1;
00908 stringstream buffer("");
00909
00910
00911 int iDCCPhi = iABPhi*3+iDCCCh;
00912 for(size_t iSC=0; iSC < ecalDccSC[iEE][iDCCPhi].size(); ++iSC){
00913 pair<int,int> sc = ecalDccSC[iEE][iDCCPhi][iSC];
00914 buffer << srp2roFlags[(int)endcapSrFlags[iEE][sc.first][sc.second]];
00915 }
00916 return buffer.str();
00917 } else{
00918 return "";
00919 }
00920 }
00921 }