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) {
40 if (*lineptr ==
NULL || *
n == 0) {
42 *lineptr = (
char *)malloc(*
n);
43 if (*lineptr ==
NULL) {
59 if (cur_len + 1 >= *
n) {
61 size_t needed = 2 * *
n + 1;
64 if (needed_max < needed)
66 if (cur_len + 1 >= needed) {
71 new_lineptr = (
char *)realloc(*lineptr, needed);
72 if (new_lineptr ==
NULL) {
77 *lineptr = new_lineptr;
81 (*lineptr)[cur_len] =
i;
87 (*lineptr)[cur_len] =
'\0';
202 inline int abNum(
int iABEta,
int iABPhi) {
return 3 * iABEta + iABPhi; }
222 void abConnect(
int iAB,
int iABCh,
int &iOtherAB,
int &iOtherABCh);
232 ofstream abTTFFiles[
nAB];
233 ofstream abSRFFiles[
nAB];
234 ofstream abIOFiles[
nAB];
237 while (++iarg <
argc) {
238 if (strcmp(
argv[iarg],
"-h") == 0 || strcmp(
argv[iarg],
"--help") == 0) {
239 cout <<
"Usage: GenABIO [OPTIONS]\n\n"
240 "Produces TT and SR flag files for each SRP board from TTF.txt "
242 "SRF.txt global flag files. Requires the SRP cross-connect "
244 " description file (xconnect_universal.txt). TTF.txt, SRF.txt and "
245 "xconnect_universal.txt must be in the directory the command is "
248 " -A, --actions IJKLMNOP. IJKLMNOP I..P integers from 0 to 7.\n"
249 " I: action flag for low interest RUs\n"
250 " J: action flag for single RUs\n"
251 " K: action flag for neighbour RUs\n"
252 " L: action flag for centers RUs\n"
253 " M: action flag for forced low interest RUs\n"
254 " N: action flag for forced single RUs\n"
255 " O: action flag for forced neighbour RUs\n"
256 " P: action flag for forced centers RUs\n\n"
257 " -h, --help display this help\n"
258 " -a n, --ab n specifies indices of the AB whose file must be "
259 "produced. The ab number runs from 1 to 12. Use -1 to produce "
266 if (!strcmp(
argv[iarg],
"-A") || !strcmp(
argv[iarg],
"-A")) {
267 if (++iarg >=
argc) {
268 cout <<
"Option error. Try -h\n";
271 for (
int i = 0;
i < 8; ++
i) {
272 int act =
argv[iarg][
i] -
'0';
274 cout <<
"Error. Action argument is invalid.\n";
282 if (!strcmp(
argv[iarg],
"-a") || !strcmp(
argv[iarg],
"--ab")) {
283 if (++iarg >=
argc) {
284 cout <<
"Option error. Try -h\n";
290 if (theAB < -1 || theAB > 11) {
291 cout <<
"AB number is incorrect. Try -h option to get help.\n";
303 for (
int iEE = 0; iEE <
nEndcaps; ++iEE) {
308 ecalDccSC[iEE][iDCCPhi].push_back(pair<int, int>(iX, iY));
315 for (
int iAB = 0; iAB <
nAB; ++iAB) {
320 abTTFFiles[iAB].open(
s.str().c_str(),
ios::out);
324 abSRFFiles[iAB].open(
s.str().c_str(),
ios::out);
328 abIOFiles[iAB].open(
s.str().c_str(),
ios::out);
333 if (srfFile ==
nullptr) {
339 if (ttfFile ==
nullptr) {
345 while (
readSRF(srfFile, barrelSrFlags, endcapSrFlags) &&
readTTF(ttfFile, ttFlags)) {
351 fillABIOFiles(ttFlags, barrelSrFlags, endcapSrFlags, abIOFiles);
362 for (
int iABEta = 0; iABEta <
nABInEta; ++iABEta) {
363 for (
int iABPhi = 0; iABPhi <
nABInPhi; ++iABPhi) {
364 int iAB =
abNum(iABEta, iABPhi);
380 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)
403 for (
int iEE = 0; iEE <
nEndcaps; ++iEE) {
410 if (!lineAppended[iAB]) {
411 for (
int i = 0;
i < iY; ++
i)
415 lineAppended[iAB] =
true;
418 for (
int iFile = 0; iFile <
nAB; ++iFile) {
419 if (lineAppended[iFile]) {
420 files[iFile] <<
"\n";
421 lineAppended[iFile] =
false;
428 for (
int iABEta = 1; iABEta < 3; ++iABEta) {
429 for (
int iABPhi = 0; iABPhi <
nABInPhi; ++iABPhi) {
430 int iAB =
abNum(iABEta, iABPhi);
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);
469 for (
int iTCC = 0; iTCC <
nABTCCCh; ++iTCC) {
473 for (
int iABCh = 0; iABCh <
nABABCh; ++iABCh) {
477 for (
int iABCh = 0; iABCh <
nABABCh; ++iABCh) {
481 for (
int iDCCCh = 0; iDCCCh <
nDCCCh; ++iDCCCh) {
482 files[iAB] <<
"ODCC";
483 files[iAB] << (iDCCCh <= 8 ?
"0" :
"") << iDCCCh + 1 <<
":"
501 size_t bufferSize = 0;
510 while (*
pos ==
' ' || *
pos ==
'\t')
512 if (*
pos !=
'#' && *
pos !=
'\n') {
515 <<
" has incorrect length"
521 for (
int iPhi = 0; iPhi <
nTTInPhi; ++iPhi) {
540 size_t bufferSize = 0;
551 while (iReadLine < nReadLine && (
read = getline(&
buffer, &bufferSize,
f)) != -1) {
554 while (*
pos ==
' ' || *
pos ==
'\t')
556 if (*
pos !=
'#' && *
pos !=
'\n') {
566 endcapSrFlags[0][iXm][iY] =
buffer[iY];
573 <<
" (" <<
read - 1 <<
" instead of " <<
nTTInPhi <<
")" << endl;
576 for (
int iPhi = 0; iPhi <
nTTInPhi; ++iPhi) {
587 endcapSrFlags[1][iXp][iY] =
buffer[iY];
595 return (iReadLine == nReadLine) ?
true :
false;
624 const char *date = ctime(&
t);
625 f <<
"# TTF flag map covered by AB " <<
abNum + 1
632 <<
": 000 (low interest)\n"
635 <<
": 001 (single)\n"
638 <<
": 010 (neighbour)\n"
641 <<
": 011 (center)\n"
648 const char *date = ctime(&
t);
651 if (abNum < 3 || abNum > 8) {
658 f <<
"# SRF flag map covered by AB " <<
abNum + 1
665 <<
": 000 (suppress)\n"
668 <<
": 010 (SR Threshold 2)\n"
671 <<
": 001 (SR Threshold 1)\n"
674 <<
": 011 (Full readout)\n"
676 "# action table (when forced):\n"
694 const char *date = ctime(&
t);
702 <<
": 000 (suppress)\n"
705 <<
": 010 (SR Threshold 2)\n"
708 <<
": 001 (SR Threshold 1)\n"
711 <<
": 011 (Full readout)\n"
713 "# action table (when forced):\n"
731 if (iEtaMin <= nTTInEta / 2 && iEtaMax >
nTTInEta) {
732 cerr <<
"Implementation Errror:" << __FILE__ <<
":" << __LINE__
733 <<
": A flag stream cannot covers parts of both half-ECAL!" << endl;
761 if (iABEta == 1 || iABEta == 2) {
764 iTCCEta = 1 + iABEta;
767 iTCCEta = (iTCCCh < 6) ? 1 : 0;
769 iTCCEta = (iTCCCh < 6) ? 4 : 5;
782 iPhiMin += 4 * (iTCCCh % 6);
783 iPhiMax = iPhiMin + 4 - 1;
791 bool barrel = (iABEta == 1 || iABEta == 2);
800 for (
int iTCCCh = 0; iTCCCh < 6; ++iTCCCh) {
825 buffer <<
s.substr(
s.size() - 16, 16);
833 for (
int iTCCCh = 0; iTCCCh < 6; ++iTCCCh) {
835 buffer <<
s.substr(
s.size() - 16, 16);
846 buffer <<
s.substr(
s.size() - 16, 16);
856 int iAB =
abNum(iABEta, iABPhi);
859 abConnect(iAB, iABCh, iOtherAB, iOtherABCh);
860 int iOtherABEta = iOtherAB / 3;
861 int iOtherABPhi = iOtherAB % 3;
870 void abConnect(
int iAB,
int iABCh,
int &iOtherAB,
int &iOtherABCh) {
871 static bool firstCall =
true;
880 for (
int i = 0;
i < 2; ++
i) {
882 while ((
c = getc(
f)) !=
'\n' &&
c >= 0)
891 if (4 == fscanf(
f,
"%d\t%d\t%d\t%d", &abIn, &pinIn, &abOut, &pinOut)) {
892 xconnectMap[abIn][pinIn][0] = abOut;
893 xconnectMap[abIn][pinIn][1] = pinOut;
899 <<
" contains an unexpected number of link definition." << endl;
905 iOtherAB = xconnectMap[iAB][iABCh][0];
906 iOtherABCh = xconnectMap[iAB][iABCh][1];
914 bool barrel = (iABEta == 1 || iABEta == 2);
919 for (
size_t i = 0;
i <
stream.size(); ++
i) {
926 int iEE = (iABEta == 0) ? 0 : 1;
930 int iDCCPhi = iABPhi * 3 + iDCCCh;
931 for (
size_t iSC = 0; iSC <
ecalDccSC[iEE][iDCCPhi].size(); ++iSC) {
932 pair<int, int> sc =
ecalDccSC[iEE][iDCCPhi][iSC];