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';
88 result = cur_len ? (ssize_t)cur_len : result;
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";
287 theAB = strtoul(argv[iarg],
nullptr, 0);
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);
369 files[iAB] <<
"# Event " <<
iEvent <<
"\n";
374 iTTEta = iTTEtaMin[iABEta] +
i;
379 iTTPhi =
mod(++iTTPhi, nTTInPhi)) {
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);
438 iTTEta = iTTEtaMin[iABEta] +
i;
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 <<
":" <<
getABTCCInputStream(ttFlags, iABEta, iABPhi, iTCC) <<
"\n";
473 for (
int iABCh = 0; iABCh <
nABABCh; ++iABCh) {
474 files[iAB] <<
"IAB" << iABCh + 1 <<
":" <<
getABABInputStream(ttFlags, iABEta, iABPhi, iABCh) <<
"\n";
477 for (
int iABCh = 0; iABCh <
nABABCh; ++iABCh) {
478 files[iAB] <<
"OAB" << iABCh + 1 <<
":" <<
getABABOutputStream(ttFlags, iABEta, iABPhi, iABCh) <<
"\n";
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;
507 while (iEta <
nTTInEta && (read = getline(&buffer, &bufferSize, f)) != -1) {
510 while (*pos ==
' ' || *pos ==
'\t')
512 if (*pos !=
'#' && *pos !=
'\n') {
513 if (read - 1 != nTTInPhi) {
515 <<
" has incorrect length" 521 for (
int iPhi = 0; iPhi <
nTTInPhi; ++iPhi) {
522 ttFlags[iEta][iPhi] = buffer[iPhi];
533 return (iEta ==
nTTInEta) ?
true :
false;
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') {
559 if (iReadLine < nSupercrystalXBins) {
560 if (read - 1 != nSupercrystalYBins) {
561 cerr <<
"Error: line " << line <<
" of file " <<
srfFilename <<
" has incorrect length" 562 <<
" (" << read - 1 <<
" instead of " << nSupercrystalYBins <<
")" << endl;
566 endcapSrFlags[0][iXm][iY] = buffer[iY];
571 if (read - 1 != nTTInPhi) {
572 cerr <<
"Error: line " << line <<
" of file " <<
srfFilename <<
" has incorrect length" 573 <<
" (" << read - 1 <<
" instead of " << nTTInPhi <<
")" << endl;
576 for (
int iPhi = 0; iPhi <
nTTInPhi; ++iPhi) {
577 barrelSrFlags[iEta][iPhi] = buffer[iPhi];
581 if (read - 1 != nSupercrystalYBins) {
582 cerr <<
"Error: line " << line <<
" of file " <<
srfFilename <<
" has incorrect length" 583 <<
" (" << read - 1 <<
" instead of " << nSupercrystalYBins <<
")" << endl;
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);
695 f <<
"# AB " << abNum + 1
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" 730 assert(0 <= iEtaMin && iEtaMin <= iEtaMax && iEtaMax <
nTTInEta);
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;
737 bool zPos = (iEtaMin >= nTTInEta / 2);
741 for (
int jEta = 0; jEta <= iEtaMax -
iEtaMin; ++jEta) {
745 iEta = iEtaMin + jEta;
747 iEta = iEtaMax - jEta;
750 for (
int iPhi =
mod(iPhiMin, nTTInPhi);
mod(iPhiMax + 1 - iPhi, nTTInPhi) != 0; iPhi =
mod(++iPhi, nTTInPhi)) {
751 buffer <<
flags[iEta][iPhi];
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;
785 return getFlagStream(tccFlags, iEtaMin, iEtaMax, iPhiMin, iPhiMax);
791 bool barrel = (iABEta == 1 || iABEta == 2);
800 for (
int iTCCCh = 0; iTCCCh < 6; ++iTCCCh) {
824 assert(s.size() >= 16);
825 buffer << s.substr(s.size() - 16, 16);
833 for (
int iTCCCh = 0; iTCCCh < 6; ++iTCCCh) {
835 buffer << s.substr(s.size() - 16, 16);
845 assert(s.size() >= 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;
867 iTTPhiMax =
mod(iTTPhiMin + nTTInABAlongPhi - 1,
nTTInPhi);
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) {
933 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)
int main(int argc, char *argv[])
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)