20 #if !defined(__linux__) && !(defined(__APPLE__) && __DARWIN_C_LEVEL >= 200809L)
25 # define SIZE_MAX ((size_t) -1)
28 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
31 ssize_t getline (
char **lineptr,
size_t *
n, FILE *
fp)
42 if (*lineptr ==
NULL || *n == 0)
45 *lineptr = (
char *) malloc (*n);
65 if (cur_len + 1 >= *n)
69 size_t needed = 2 * *n + 1;
72 if (needed_max < needed)
74 if (cur_len + 1 >= needed)
80 new_lineptr = (
char *) realloc (*lineptr, needed);
81 if (new_lineptr ==
NULL)
87 *lineptr = new_lineptr;
91 (*lineptr)[cur_len] =
i;
97 (*lineptr)[cur_len] =
'\0';
98 result = cur_len ? (ssize_t) cur_len : result;
204 inline int abNum(
int iABEta,
int iABPhi){
return 3*iABEta+iABPhi;}
216 int iPhiMin,
int iPhiMax);
218 int iABEta,
int iABPhi,
int iTCCCh);
221 int iABEta,
int iABPhi,
int iABCh);
223 int iABPhi,
int iABCh);
227 int iABEta,
int iABPhi,
int DCCCh);
228 void abConnect(
int iAB,
int iABCh,
int& iOtherAB,
int& iOtherABCh);
238 ofstream abTTFFiles[
nAB];
239 ofstream abSRFFiles[
nAB];
240 ofstream abIOFiles[
nAB];
244 if(strcmp(argv[iarg],
"-h")==0||strcmp(argv[iarg],
"--help")==0){
245 cout <<
"Usage: GenABIO [OPTIONS]\n\n"
246 "Produces TT and SR flag files for each SRP board from TTF.txt and "
247 "SRF.txt global flag files. Requires the SRP cross-connect description"
248 " description file (xconnect_universal.txt). TTF.txt, SRF.txt and "
249 "xconnect_universal.txt must be in the directory the command is "
252 " -A, --actions IJKLMNOP. IJKLMNOP I..P integers from 0 to 7.\n"
253 " I: action flag for low interest RUs\n"
254 " J: action flag for single RUs\n"
255 " K: action flag for neighbour RUs\n"
256 " L: action flag for centers RUs\n"
257 " M: action flag for forced low interest RUs\n"
258 " N: action flag for forced single RUs\n"
259 " O: action flag for forced neighbour RUs\n"
260 " P: action flag for forced centers RUs\n\n"
261 " -h, --help display this help\n"
262 " -a n, --ab n specifies indices of the AB whose file must be "
263 "produced. The ab number runs from 1 to 12. Use -1 to produce files "
270 if(!strcmp(argv[iarg],
"-A") || !strcmp(argv[iarg],
"-A")){
271 if(++iarg>=argc){
cout <<
"Option error. Try -h\n";
return 1; }
272 for(
int i=0;
i<8; ++
i){
273 int act = argv[iarg][
i]-
'0';
275 cout <<
"Error. Action argument is invalid.\n";
283 if(!strcmp(argv[iarg],
"-a")||!strcmp(argv[iarg],
"--ab")){
284 if(++iarg>=argc){
cout <<
"Option error. Try -h\n";
return 1; }
285 theAB = strtoul(argv[iarg], 0, 0);
287 if(theAB<-1 || theAB>11){
288 cout <<
"AB number is incorrect. Try -h option to get help.\n";
299 for(
int iEE=0; iEE <
nEndcaps; ++iEE){
304 ecalDccSC[iEE][iDCCPhi].push_back(pair<int,int>(iX,iY));
311 for(
int iAB=0; iAB<
nAB; ++iAB){
315 abTTFFiles[iAB].open(s.str().c_str(),
ios::out);
319 abSRFFiles[iAB].open(s.str().c_str(),
ios::out);
323 abIOFiles[iAB].open(s.str().c_str(),
ios::out);
340 while(
readSRF(srfFile,barrelSrFlags,endcapSrFlags)
347 fillABIOFiles(ttFlags,barrelSrFlags,endcapSrFlags,abIOFiles);
359 for(
int iABEta=0; iABEta<
nABInEta; ++iABEta){
360 for(
int iABPhi=0; iABPhi<
nABInPhi; ++iABPhi){
361 int iAB =
abNum(iABEta,iABPhi);
366 files[iAB] <<
"# Event " <<
iEvent <<
"\n";
372 iTTEta = iTTEtaMin[iABEta] +
i;
376 for(
int iTTPhi = iTTPhiMin;
378 iTTPhi =
mod(++iTTPhi, nTTInPhi)){
379 files[iAB] << ttFlags[iTTEta][iTTPhi];
394 for(
int iAB=0; iAB <
nAB; ++iAB){
398 bool lineAppended[
nAB];
399 for(
int i=0;
i<
nAB; lineAppended[
i++]=
false);
402 for(
int iEE = 0; iEE <
nEndcaps; ++iEE){
409 if(!lineAppended[iAB]){
410 for(
int i=0;
i< iY; ++
i)
files[iAB] <<
' ';
413 lineAppended[iAB] =
true;
416 for(
int iFile=0; iFile<
nAB; ++iFile){
417 if(lineAppended[iFile]){
418 files[iFile] <<
"\n";
419 lineAppended[iFile] =
false;
426 for(
int iABEta=1; iABEta<3; ++iABEta){
427 for(
int iABPhi=0; iABPhi<
nABInPhi; ++iABPhi){
428 int iAB =
abNum(iABEta,iABPhi);
437 iTTEta = iTTEtaMin[iABEta] +
i;
441 for(
int iTTPhi = iTTPhiMin;
443 iTTPhi =
mod(++iTTPhi, nTTInPhi)){
454 for(
int iAB=0; iAB <
nAB; ++iAB){
463 for(
int iABEta=0; iABEta <
nABInEta; ++iABEta){
464 for(
int iABPhi=0; iABPhi <
nABInPhi; ++iABPhi){
465 int iAB =
abNum(iABEta, iABPhi);
467 files[iAB] <<
"# Event " <<
iEvent <<
"\n";
469 for(
int iTCC=0; iTCC<
nABTCCCh; ++iTCC){
470 files[iAB] <<
"ITCC" << iTCC+1 <<
":"
475 for(
int iABCh=0; iABCh<
nABABCh; ++iABCh){
476 files[iAB] <<
"IAB" << iABCh+1 <<
":"
481 for(
int iABCh=0; iABCh<
nABABCh; ++iABCh){
482 files[iAB] <<
"OAB" << iABCh+1 <<
":"
487 for(
int iDCCCh=0; iDCCCh<
nDCCCh; ++iDCCCh){
488 files[iAB] <<
"ODCC";
489 files[iAB]<< (iDCCCh<=8?
"0":
"") << iDCCCh+1 <<
":"
508 size_t bufferSize = 0;
513 while(iEta<
nTTInEta && (read=getline(&buffer, &bufferSize,f))!=-1){
516 while(*pos==
' ' || *pos==
'\t') ++pos;
517 if(*pos!=
'#' && *pos!=
'\n'){
518 if(read-1!=nTTInPhi){
519 cerr <<
"Error: line " << line
520 <<
" of file "<<
ttfFilename <<
" has incorrect length"
525 for(
int iPhi=0; iPhi<
nTTInPhi; ++iPhi){
526 ttFlags[iEta][iPhi] = buffer[iPhi];
545 size_t bufferSize = 0;
555 while(iReadLine<nReadLine && (read=getline(&buffer, &bufferSize,f))!=-1){
558 while(*pos==
' ' || *pos==
'\t') ++pos;
559 if(*pos!=
'#' && *pos!=
'\n'){
562 if(iReadLine<nSupercrystalXBins){
563 if(read-1!=nSupercrystalYBins){
564 cerr <<
"Error: line " << line
565 <<
" of file "<<
srfFilename <<
" has incorrect length"
566 <<
" (" << read-1 <<
" instead of " << nSupercrystalYBins <<
")"
571 endcapSrFlags[0][iXm][iY] = buffer[iY];
575 if(read-1!=nTTInPhi){
576 cerr <<
"Error: line " << line
577 <<
" of file "<<
srfFilename <<
" has incorrect length"
578 <<
" (" << read-1 <<
" instead of " << nTTInPhi <<
")"
582 for(
int iPhi=0; iPhi<
nTTInPhi; ++iPhi){
583 barrelSrFlags[iEta][iPhi] = buffer[iPhi];
587 if(read-1!=nSupercrystalYBins){
588 cerr <<
"Error: line " << line
589 <<
" of file "<<
srfFilename <<
" has incorrect length"
590 <<
" (" << read-1 <<
" instead of " << nSupercrystalYBins <<
")"
595 endcapSrFlags[1][iXp][iY] = buffer[iY];
603 return (iReadLine==nReadLine)?
true:
false;
633 const char* date = ctime(&t);
634 f <<
"# TTF flag map covered by AB " << abNum+1 <<
"\n#\n"
635 "# Generated on : " << date <<
"#\n"
636 "# +---> Phi " <<
srpFlagMarker[0] <<
": 000 (low interest)\n"
646 const char* date = ctime(&t);
649 if(abNum<3 || abNum>8){
656 f <<
"# SRF flag map covered by AB " << abNum+1 <<
"\n#\n"
657 "# Generated on : " << date <<
"#\n"
658 "# +---> " << xLabel <<
" " <<
roFlagMarker[0] <<
": 000 (suppress)\n"
661 "# V " << yLabel <<
" " <<
roFlagMarker[3] <<
": 011 (Full readout)\n"
663 "# action table (when forced):\n"
673 const char* date = ctime(&t);
674 f <<
"# AB " << abNum+1 <<
" I/O \n#\n"
675 "# Generated on : " << date <<
"#\n"
681 "# action table (when forced):\n"
690 int iEtaMax,
int iPhiMin,
int iPhiMax){
692 if(iEtaMin<=nTTInEta/2 && iEtaMax>
nTTInEta){
693 cerr <<
"Implementation Errror:"
694 << __FILE__ <<
":" << __LINE__
695 <<
": A flag stream cannot covers parts of both half-ECAL!"
700 bool zPos = (iEtaMin>=nTTInEta/2);
704 for(
int jEta = 0; jEta <= iEtaMax-iEtaMin; ++jEta){
708 iEta = iEtaMin + jEta;
710 iEta = iEtaMax - jEta;
713 for(
int iPhi =
mod(iPhiMin,nTTInPhi);
714 mod(iPhiMax+1-iPhi,nTTInPhi) != 0;
715 iPhi =
mod(++iPhi, nTTInPhi)){
716 buffer <<
flags[iEta][iPhi];
724 int iABEta,
int iABPhi,
int iTCCCh){
727 if(iABEta==1 || iABEta==2){
728 if(iTCCCh>5)
return "";
729 iTCCEta = 1 + iABEta;
732 iTCCEta = (iTCCCh<6)?1:0;
734 iTCCEta = (iTCCCh<6)?4:5;
747 iPhiMin += 4*(iTCCCh%6);
748 iPhiMax = iPhiMin + 4 - 1;
750 return getFlagStream(tccFlags, iEtaMin, iEtaMax, iPhiMin, iPhiMax);
754 int iABEta,
int iABPhi,
int iABCh){
757 bool barrel = (iABEta==1 || iABEta==2);
765 for(
int iTCCCh=0; iTCCCh<6; ++iTCCCh){
791 buffer << s.substr(s.size()-16,16);
799 for(
int iTCCCh=0; iTCCCh<6; ++iTCCCh){
801 buffer << s.substr(s.size()-16,16);
812 buffer << s.substr(s.size()-16,16);
822 int iABPhi,
int iABCh){
823 int iAB =
abNum(iABEta, iABPhi);
826 abConnect(iAB,iABCh,iOtherAB,iOtherABCh);
827 int iOtherABEta = iOtherAB/3;
828 int iOtherABPhi = iOtherAB%3;
835 iTTPhiMax =
mod(iTTPhiMin+nTTInABAlongPhi-1,
nTTInPhi);
838 void abConnect(
int iAB,
int iABCh,
int& iOtherAB,
int& iOtherABCh){
839 static bool firstCall =
true;
844 cerr <<
"Error. Failed to open xconnect definition file,"
849 for(
int i=0;
i<2; ++
i){
851 while((c=getc(f))!=
'\n' && c >=0);
859 if(4==fscanf(f,
"%d\t%d\t%d\t%d", &abIn, &pinIn, &abOut, &pinOut)){
860 xconnectMap[abIn][pinIn][0] = abOut;
861 xconnectMap[abIn][pinIn][1] = pinOut;
867 <<
" contains an unexpected number of link definition."
874 iOtherAB = xconnectMap[iAB][iABCh][0];
875 iOtherABCh = xconnectMap[iAB][iABCh][1];
881 int iABEta,
int iABPhi,
int iDCCCh){
882 bool barrel = (iABEta==1||iABEta==2);
887 for(
size_t i=0;
i< stream.size(); ++
i){
894 int iEE=(iABEta==0)?0:1;
895 stringstream buffer(
"");
898 int iDCCPhi = iABPhi*3+iDCCCh;
899 for(
size_t iSC=0; iSC <
ecalDccSC[iEE][iDCCPhi].size(); ++iSC){
901 buffer <<
srp2roFlags[(int)endcapSrFlags[iEE][sc.first][sc.second]];
const int iTTEtaMin[nABInEta]
bool readTTF(FILE *file, char ttFlags[nTTInEta][nTTInPhi])
static const int nEndcapYBins
void writeABSRFFileHeader(ofstream &f, int abNum)
roAction_t actions[nactions]
string getABABInputStream(const char tccFlags[nTTInEta][nTTInPhi], int iABEta, int iABPhi, int iABCh)
static const int nSupercrystalXBins
static const int nEndcapXBins
const char * abTTFFilePrefix
std::vector< Variable::Flags > flags
const char tccFlagMarker[]
const char * abSRFFilePrefix
const char * abIOFilePostfix
void getABTTPhiBounds(int iABPhi, int &iTTPhiMin, int &iTTPhiMax)
const int iTTEtaMax[nABInEta]
void writeABIOFileHeader(ofstream &f, int abNum)
void fillABTTFFiles(const char ttFlags[nTTInEta][nTTInPhi], ofstream files[])
int dccIndexOfRU(int iDet, int i, int j)
static const int nTTInEta
static const int nSupercrystalYBins
const char * abIOFilePrefix
const int iTTEtaSign[nABInEta]
string getABABOutputStream(const char tccFlags[nTTInEta][nTTInPhi], int iABEta, int iABPhi, int iABCh)
string getFlagStream(char flags[nTTInEta][nTTInPhi], int iEtaMin, int iEtaMax, int iPhiMin, int iPhiMax)
static const int nBarrelTTInEta
void fillABIOFiles(const char ttFlags[nTTInEta][nTTInPhi], const char barrelSrFlags[nBarrelTTInEta][nTTInPhi], const char endcapSrFlags[nEndcaps][nSupercrystalXBins][nSupercrystalYBins], ofstream files[])
string getABTCCInputStream(const char tccFlags[nTTInEta][nTTInPhi], int iABEta, int iABPhi, int iTCCCh)
static const int nEndcaps
const int nTTInABAlongPhi
const char * xconnectFilename
static const int supercrystalEdge
static const int nTTInPhi
const char * abTTFFilePostfix
void writeABTTFFileHeader(ofstream &f, int abNum)
const char * abSRFFilePostfix
void abConnect(int iAB, int iABCh, int &iOtherAB, int &iOtherABCh)
static const int nEndcapTTInEta
vector< pair< int, int > > ecalDccSC[nEndcaps][nDCCEE]
const int iTCCEtaBounds[nTCCInEta+1]
int abNum(int iABEta, int iABPhi)
T mod(const T &a, const T &b)
void fillABSRPFiles(const char barrelSrFlags[nBarrelTTInEta][nTTInPhi], const char endcapSrFlags[nEndcaps][nSupercrystalXBins][nSupercrystalYBins], ofstream files[])
int dccPhiIndexOfRU(int iDet, int i, int j)
bool readSRF(FILE *file, char barrelSrFlags[nBarrelTTInEta][nTTInPhi], char endcapSrFlags[nEndcaps][nSupercrystalXBins][nSupercrystalYBins])
const char srpFlagMarker[]
string getABDCCOutputStream(const char barrelSrFlags[nBarrelTTInEta][nTTInPhi], const char endcapSrFlags[nEndcaps][nSupercrystalXBins][nSupercrystalYBins], int iABEta, int iABPhi, int DCCCh)