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;
217 inline int abNum(
int iABEta,
int iABPhi){
return 3*iABEta+iABPhi;}
229 int iPhiMin,
int iPhiMax);
231 int iABEta,
int iABPhi,
int iTCCCh);
234 int iABEta,
int iABPhi,
int iABCh);
236 int iABPhi,
int iABCh);
240 int iABEta,
int iABPhi,
int DCCCh);
241 void abConnect(
int iAB,
int iABCh,
int& iOtherAB,
int& iOtherABCh);
251 ofstream abTTFFiles[
nAB];
252 ofstream abSRFFiles[
nAB];
253 ofstream abIOFiles[
nAB];
257 if(strcmp(argv[iarg],
"-h")==0||strcmp(argv[iarg],
"--help")==0){
258 cout <<
"Usage: GenABIO [OPTIONS]\n\n"
259 "Produces TT and SR flag files for each SRP board from TTF.txt and "
260 "SRF.txt global flag files. Requires the SRP cross-connect description"
261 " description file (xconnect_universal.txt). TTF.txt, SRF.txt and "
262 "xconnect_universal.txt must be in the directory the command is "
265 " -A, --actions IJKLMNOP. IJKLMNOP I..P integers from 0 to 7.\n"
266 " I: action flag for low interest RUs\n"
267 " J: action flag for single RUs\n"
268 " K: action flag for neighbour RUs\n"
269 " L: action flag for centers RUs\n"
270 " M: action flag for forced low interest RUs\n"
271 " N: action flag for forced single RUs\n"
272 " O: action flag for forced neighbour RUs\n"
273 " P: action flag for forced centers RUs\n\n"
274 " -h, --help display this help\n"
275 " -a n, --ab n specifies indices of the AB whose file must be "
276 "produced. The ab number runs from 1 to 12. Use -1 to produce files "
283 if(!strcmp(argv[iarg],
"-A") || !strcmp(argv[iarg],
"-A")){
284 if(++iarg>=argc){
cout <<
"Option error. Try -h\n";
return 1; }
285 for(
int i=0;
i<8; ++
i){
286 int act = argv[iarg][
i]-
'0';
288 cout <<
"Error. Action argument is invalid.\n";
296 if(!strcmp(argv[iarg],
"-a")||!strcmp(argv[iarg],
"--ab")){
297 if(++iarg>=argc){
cout <<
"Option error. Try -h\n";
return 1; }
298 theAB = strtoul(argv[iarg], 0, 0);
300 if(theAB<-1 || theAB>11){
301 cout <<
"AB number is incorrect. Try -h option to get help.\n";
312 for(
int iEE=0; iEE <
nEndcaps; ++iEE){
317 ecalDccSC[iEE][iDCCPhi].push_back(pair<int,int>(iX,iY));
324 for(
int iAB=0; iAB<
nAB; ++iAB){
328 abTTFFiles[iAB].open(s.str().c_str(),
ios::out);
332 abSRFFiles[iAB].open(s.str().c_str(),
ios::out);
336 abIOFiles[iAB].open(s.str().c_str(),
ios::out);
353 while(
readSRF(srfFile,barrelSrFlags,endcapSrFlags)
360 fillABIOFiles(ttFlags,barrelSrFlags,endcapSrFlags,abIOFiles);
372 for(
int iABEta=0; iABEta<
nABInEta; ++iABEta){
373 for(
int iABPhi=0; iABPhi<
nABInPhi; ++iABPhi){
374 int iAB =
abNum(iABEta,iABPhi);
379 files[iAB] <<
"# Event " <<
iEvent <<
"\n";
385 iTTEta = iTTEtaMin[iABEta] +
i;
389 for(
int iTTPhi = iTTPhiMin;
391 iTTPhi =
mod(++iTTPhi, nTTInPhi)){
392 files[iAB] << ttFlags[iTTEta][iTTPhi];
407 for(
int iAB=0; iAB <
nAB; ++iAB){
411 bool lineAppended[
nAB];
412 for(
int i=0;
i<
nAB; lineAppended[
i++]=
false);
415 for(
int iEE = 0; iEE <
nEndcaps; ++iEE){
422 if(!lineAppended[iAB]){
423 for(
int i=0;
i< iY; ++
i)
files[iAB] <<
' ';
426 lineAppended[iAB] =
true;
429 for(
int iFile=0; iFile<
nAB; ++iFile){
430 if(lineAppended[iFile]){
431 files[iFile] <<
"\n";
432 lineAppended[iFile] =
false;
439 for(
int iABEta=1; iABEta<3; ++iABEta){
440 for(
int iABPhi=0; iABPhi<
nABInPhi; ++iABPhi){
441 int iAB =
abNum(iABEta,iABPhi);
450 iTTEta = iTTEtaMin[iABEta] +
i;
454 for(
int iTTPhi = iTTPhiMin;
456 iTTPhi =
mod(++iTTPhi, nTTInPhi)){
467 for(
int iAB=0; iAB <
nAB; ++iAB){
476 for(
int iABEta=0; iABEta <
nABInEta; ++iABEta){
477 for(
int iABPhi=0; iABPhi <
nABInPhi; ++iABPhi){
478 int iAB =
abNum(iABEta, iABPhi);
480 files[iAB] <<
"# Event " <<
iEvent <<
"\n";
482 for(
int iTCC=0; iTCC<
nABTCCCh; ++iTCC){
483 files[iAB] <<
"ITCC" << iTCC+1 <<
":"
488 for(
int iABCh=0; iABCh<
nABABCh; ++iABCh){
489 files[iAB] <<
"IAB" << iABCh+1 <<
":"
494 for(
int iABCh=0; iABCh<
nABABCh; ++iABCh){
495 files[iAB] <<
"OAB" << iABCh+1 <<
":"
500 for(
int iDCCCh=0; iDCCCh<
nDCCCh; ++iDCCCh){
501 files[iAB] <<
"ODCC";
502 files[iAB]<< (iDCCCh<=8?
"0":
"") << iDCCCh+1 <<
":"
521 size_t bufferSize = 0;
526 while(iEta<
nTTInEta && (read=getline(&buffer, &bufferSize,f))!=-1){
529 while(*pos==
' ' || *pos==
'\t') ++
pos;
530 if(*pos!=
'#' && *pos!=
'\n'){
531 if(read-1!=nTTInPhi){
532 cerr <<
"Error: line " << line
533 <<
" of file "<<
ttfFilename <<
" has incorrect length"
538 for(
int iPhi=0; iPhi<
nTTInPhi; ++iPhi){
539 ttFlags[iEta][iPhi] = buffer[iPhi];
558 size_t bufferSize = 0;
568 while(iReadLine<nReadLine && (read=getline(&buffer, &bufferSize,f))!=-1){
571 while(*pos==
' ' || *pos==
'\t') ++
pos;
572 if(*pos!=
'#' && *pos!=
'\n'){
575 if(iReadLine<nSupercrystalXBins){
576 if(read-1!=nSupercrystalYBins){
577 cerr <<
"Error: line " << line
578 <<
" of file "<<
srfFilename <<
" has incorrect length"
579 <<
" (" << read-1 <<
" instead of " << nSupercrystalYBins <<
")"
584 endcapSrFlags[0][iXm][iY] = buffer[iY];
588 if(read-1!=nTTInPhi){
589 cerr <<
"Error: line " << line
590 <<
" of file "<<
srfFilename <<
" has incorrect length"
591 <<
" (" << read-1 <<
" instead of " << nTTInPhi <<
")"
595 for(
int iPhi=0; iPhi<
nTTInPhi; ++iPhi){
596 barrelSrFlags[iEta][iPhi] = buffer[iPhi];
600 if(read-1!=nSupercrystalYBins){
601 cerr <<
"Error: line " << line
602 <<
" of file "<<
srfFilename <<
" has incorrect length"
603 <<
" (" << read-1 <<
" instead of " << nSupercrystalYBins <<
")"
608 endcapSrFlags[1][iXp][iY] = buffer[iY];
616 return (iReadLine==nReadLine)?
true:
false;
646 const char* date = ctime(&t);
647 f <<
"# TTF flag map covered by AB " << abNum+1 <<
"\n#\n"
648 "# Generated on : " << date <<
"#\n"
649 "# +---> Phi " <<
srpFlagMarker[0] <<
": 000 (low interest)\n"
659 const char* date = ctime(&t);
662 if(abNum<3 || abNum>8){
669 f <<
"# SRF flag map covered by AB " << abNum+1 <<
"\n#\n"
670 "# Generated on : " << date <<
"#\n"
671 "# +---> " << xLabel <<
" " <<
roFlagMarker[0] <<
": 000 (suppress)\n"
674 "# V " << yLabel <<
" " <<
roFlagMarker[3] <<
": 011 (Full readout)\n"
676 "# action table (when forced):\n"
686 const char* date = ctime(&t);
687 f <<
"# AB " << abNum+1 <<
" I/O \n#\n"
688 "# Generated on : " << date <<
"#\n"
694 "# action table (when forced):\n"
703 int iEtaMax,
int iPhiMin,
int iPhiMax){
704 assert(0<=iEtaMin && iEtaMin<=iEtaMax && iEtaMax<
nTTInEta);
705 if(iEtaMin<=nTTInEta/2 && iEtaMax>
nTTInEta){
706 cerr <<
"Implementation Errror:"
707 << __FILE__ <<
":" << __LINE__
708 <<
": A flag stream cannot covers parts of both half-ECAL!"
713 bool zPos = (iEtaMin>=nTTInEta/2);
717 for(
int jEta = 0; jEta <= iEtaMax-iEtaMin; ++jEta){
721 iEta = iEtaMin + jEta;
723 iEta = iEtaMax - jEta;
726 for(
int iPhi =
mod(iPhiMin,nTTInPhi);
727 mod(iPhiMax+1-iPhi,nTTInPhi) != 0;
728 iPhi =
mod(++iPhi, nTTInPhi)){
729 buffer <<
flags[iEta][iPhi];
737 int iABEta,
int iABPhi,
int iTCCCh){
740 if(iABEta==1 || iABEta==2){
741 if(iTCCCh>5)
return "";
742 iTCCEta = 1 + iABEta;
745 iTCCEta = (iTCCCh<6)?1:0;
747 iTCCEta = (iTCCCh<6)?4:5;
760 iPhiMin += 4*(iTCCCh%6);
761 iPhiMax = iPhiMin + 4 - 1;
763 return getFlagStream(tccFlags, iEtaMin, iEtaMax, iPhiMin, iPhiMax);
767 int iABEta,
int iABPhi,
int iABCh){
770 bool barrel = (iABEta==1 || iABEta==2);
778 for(
int iTCCCh=0; iTCCCh<6; ++iTCCCh){
803 assert(s.size()>=16);
804 buffer << s.substr(s.size()-16,16);
812 for(
int iTCCCh=0; iTCCCh<6; ++iTCCCh){
814 buffer << s.substr(s.size()-16,16);
824 assert(s.size()>=16);
825 buffer << s.substr(s.size()-16,16);
835 int iABPhi,
int iABCh){
836 int iAB =
abNum(iABEta, iABPhi);
839 abConnect(iAB,iABCh,iOtherAB,iOtherABCh);
840 int iOtherABEta = iOtherAB/3;
841 int iOtherABPhi = iOtherAB%3;
848 iTTPhiMax =
mod(iTTPhiMin+nTTInABAlongPhi-1,
nTTInPhi);
851 void abConnect(
int iAB,
int iABCh,
int& iOtherAB,
int& iOtherABCh){
852 static bool firstCall =
true;
857 cerr <<
"Error. Failed to open xconnect definition file,"
862 for(
int i=0;
i<2; ++
i){
864 while((c=getc(f))!=
'\n' && c >=0);
872 if(4==fscanf(f,
"%d\t%d\t%d\t%d", &abIn, &pinIn, &abOut, &pinOut)){
873 xconnectMap[abIn][pinIn][0] = abOut;
874 xconnectMap[abIn][pinIn][1] = pinOut;
880 <<
" contains an unexpected number of link definition."
887 iOtherAB = xconnectMap[iAB][iABCh][0];
888 iOtherABCh = xconnectMap[iAB][iABCh][1];
894 int iABEta,
int iABPhi,
int iDCCCh){
895 bool barrel = (iABEta==1||iABEta==2);
900 for(
size_t i=0;
i< stream.size(); ++
i){
907 int iEE=(iABEta==0)?0:1;
908 stringstream buffer(
"");
911 int iDCCPhi = iABPhi*3+iDCCCh;
912 for(
size_t iSC=0; iSC <
ecalDccSC[iEE][iDCCPhi].size(); ++iSC){
913 pair<int,int> sc =
ecalDccSC[iEE][iDCCPhi][iSC];
914 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 nBarrelPhiBins
static const int nEndcapXBins
const char * abTTFFilePrefix
static const int nBarrelTowerEtaBins
std::vector< Variable::Flags > flags
const char tccFlagMarker[]
const char * abSRFFilePrefix
const char * abIOFilePostfix
int main(int argc, char **argv)
void getABTTPhiBounds(int iABPhi, int &iTTPhiMin, int &iTTPhiMax)
static const int nBarrelTowerPhiBins
static const int nBarrelEtaBins
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
list mod
Load physics model.
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
const int nTTInABAlongEta
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)
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)