CMS 3D CMS Logo

GenABIO.cc

Go to the documentation of this file.
00001 // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: t; tab-width: 8; -*-
00002 
00010 #include <iostream>
00011 #include <fstream>
00012 #include <sstream>
00013 #include <vector>
00014 #include <cassert>
00015 
00016 #include "ecalDccMap.h"
00017 
00018 using namespace std;
00019 
00022 const static int nBarrelEtaBins = 170;
00025 const static int nBarrelPhiBins = 360;
00028 const static int nEndcapXBins = 100;
00031 const static int nEndcapYBins = 100;
00034 const static int supercrystalEdge = 5;
00037 const static int nSupercrystalXBins = nEndcapXBins/supercrystalEdge;
00040 const static int nSupercrystalYBins = nEndcapYBins/supercrystalEdge;
00043 const static int nBarrelTowerEtaBins = nBarrelEtaBins/5;
00046 const static int nBarrelTowerPhiBins = nBarrelPhiBins/5;
00049 const static int nEndcaps = 2;
00052 const static int nEndcapTTInEta = 11;
00055 const static int nBarrelTTInEta = 34;
00058 const static int nTTInEta =
00059 2*nEndcapTTInEta+nBarrelTTInEta;
00062 const static int nTTInPhi = 72;
00065 const int nABInEta=4;
00068 const int nABInPhi=3;
00071 const int nDCCEE = 9;
00072 const int nABABCh = 8;//nbr of AB input/output ch. on an AB
00073 const int nABTCCCh = 12;//nbr of TCC inputs on an AB
00074 const int nDCCCh = 12;//nbr of DCC outputs on an AB
00075 const int nTCCInEta = 6; //nbr of TCC bins along eta
00076 const int nAB = nABInPhi*nABInEta;
00077 const int nTTInABAlongEta=nTTInEta/nABInEta;
00078 const int nTTInABAlongPhi=nTTInPhi/nABInPhi;
00079 const int iTTEtaMin[nABInEta] = {0,11,28,45};
00080 const int iTTEtaMax[nABInEta] = {10,27,44,55};
00081 const int iTTEtaSign[nABInEta] = {-1,-1,1,1};
00082 
00083 //Eta bounds for TCC partionning
00084 //a TCC covers from iTCCEtaBounds[iTCCEta] included to
00085 //iTCCEtaBounds[iTCCEta+1] excluded.
00086 const int iTCCEtaBounds[nTCCInEta+1] = {0,7,11,28,45,49,56};
00087 
00088 const char* abTTFFilePrefix = "TTF_AB";
00089 const char* abTTFFilePostfix = ".txt";
00090 const char* abSRFFilePrefix = "AF_AB";
00091 const char* abSRFFilePostfix = ".txt";
00092 const char* abIOFilePrefix = "IO_AB";
00093 const char* abIOFilePostfix = ".txt";
00094 
00095 const char* srfFilename = "SRF.txt";
00096 const char* ttfFilename = "TTF.txt";
00097 const char* xconnectFilename = "xconnect_universal.txt";
00098 
00099 const char srpFlagMarker[] = {'.', 'S', 'N', 'C', '4','5','6','7'};
00100 const char tccFlagMarker[] = {'.', 'S', '?', 'C', '4', '5', '6', '7'};
00101 
00102 char srp2roFlags[128];
00103 
00104 typedef enum {suppress=0, sr2, sr1, full, fsuppress, fsr2, fsr1, ffull} roAction_t;
00105 char roFlagMarker[] = {/*suppress*/'.', /*sr1*/'z', /*sr1*/'Z', /*full*/'F',
00106                        /*fsuppress*/'4', /*fsr2*/'5', /*fsr1*/'6', /*ffull*/'7'
00107 };
00108 
00109 const int nactions = 8;
00110 //can be overwritten according by cmd line arguments
00111 roAction_t actions[nactions] = {/*LI->*/sr2,/*S->*/full,/*N->*/ full,/*C->*/full,
00112                                 /*fLI->*/sr2,/*fS->*/sr2,/*fN->*/sr2,/*fC->*/sr2}; 
00113 
00114 //list of SC deserves by an endcap DCC [0(EE-)|1(EE+)][iDCCPhi]
00115 vector<pair<int, int> > ecalDccSC[nEndcaps][nDCCEE];
00116 
00117 void fillABTTFFiles(const char ttFlags[nTTInEta][nTTInPhi],
00118                     ofstream files[]);
00119 void fillABSRPFiles(const char barrelSrFlags[nBarrelTTInEta][nTTInPhi],
00120                     const char endcapSrFlags[nEndcaps][nSupercrystalXBins]
00121                     [nSupercrystalYBins],
00122                     ofstream files[]);
00123 void fillABIOFiles(const char ttFlags[nTTInEta][nTTInPhi],
00124                    const char barrelSrFlags[nBarrelTTInEta][nTTInPhi],
00125                    const char endcapSrFlags[nEndcaps][nSupercrystalXBins]
00126                    [nSupercrystalYBins],
00127                    ofstream files[]);
00128 inline int abNum(int iABEta, int iABPhi){return 3*iABEta+iABPhi;}
00129 
00130 bool readTTF(FILE* file, char ttFlags[nTTInEta][nTTInPhi]);
00131 bool readSRF(FILE* file,
00132              char barrelSrFlags[nBarrelTTInEta][nTTInPhi],
00133              char endcapSrFlags[nEndcaps][nSupercrystalXBins]
00134              [nSupercrystalYBins]);
00135 
00136 void writeABTTFFileHeader(ofstream& f, int abNum);
00137 void writeABSRFFileHeader(ofstream& f, int abNum);
00138 void writeABIOFileHeader(ofstream& f, int abNum);
00139 string getFlagStream(char flags[nTTInEta][nTTInPhi], int iEtaMin, int iEtaMax,
00140                      int iPhiMin, int iPhiMax);
00141 string getABTCCInputStream(const char tccFlags[nTTInEta][nTTInPhi],
00142                            int iABEta, int iABPhi, int iTCCCh);
00143 void getABTTPhiBounds(int iABPhi, int& iTTPhiMin, int& iTTPhiMax);
00144 string getABABOutputStream(const char tccFlags[nTTInEta][nTTInPhi],
00145                            int iABEta, int iABPhi, int iABCh);
00146 string getABABInputStream(const char tccFlags[nTTInEta][nTTInPhi], int iABEta, 
00147                           int iABPhi, int iABCh);
00148 string getABDCCOutputStream(const char barrelSrFlags[nBarrelTTInEta][nTTInPhi],
00149                             const char endcapSrFlags[nEndcaps][nSupercrystalXBins]
00150                             [nSupercrystalYBins],
00151                             int iABEta, int iABPhi, int DCCCh);
00152 void abConnect(int iAB,int iABCh,int& iOtherAB,int& iOtherABCh);
00153 
00154 int iEvent = 0;
00155 
00156 int theAB = -1;
00157 
00158 int main(int argc, char* argv[]){
00159   char barrelSrFlags[nBarrelTTInEta][nTTInPhi];
00160   char endcapSrFlags[nEndcaps][nEndcapXBins/5][nEndcapYBins/5];
00161   char ttFlags[nTTInEta][nTTInPhi];
00162   ofstream abTTFFiles[nAB];
00163   ofstream abSRFFiles[nAB];
00164   ofstream abIOFiles[nAB];
00165 
00166   int iarg = 0;
00167   while(++iarg<argc){
00168     if(strcmp(argv[iarg],"-h")==0||strcmp(argv[iarg],"--help")==0){
00169       cout << "Usage: GenABIO [OPTIONS]\n\n"
00170         "Produces TT and SR flag files for each SRP board from TTF.txt and "
00171         "SRF.txt global flag files. Requires the SRP cross-connect description"
00172         " description file (xconnect_universal.txt). TTF.txt, SRF.txt and "
00173         "xconnect_universal.txt must be in the directory the command is "
00174         "launched.\n\n"
00175         "OPTIONS:\n"
00176         "  -A, --actions IJKLMNOP. IJKLMNOP I..P integers from 0 to 7.\n"
00177         "                I: action flag for low interest RUs\n"
00178         "                J: action flag for single RUs\n"
00179         "                K: action flag for neighbour RUs\n"
00180         "                L: action flag for centers RUs\n"
00181         "                M: action flag for forced low interest RUs\n"
00182         "                N: action flag for forced single RUs\n"
00183         "                O: action flag for forced neighbour RUs\n"
00184         "                P: action flag for forced centers RUs\n\n"
00185         " -h, --help display this help\n"
00186         " -a n, --ab n specifies indices of the AB whose file must be "
00187         "produced. The ab number runs from 1 to 12. Use -1 to produce files "
00188         "for every AB\n\n"
00189         ;
00190       
00191       return 0;
00192     }
00193     
00194     if(!strcmp(argv[iarg],"-A") || !strcmp(argv[iarg],"-A")){//actions
00195       if(++iarg>=argc){ cout << "Option error. Try -h\n"; return 1; }
00196       for(int i=0; i<8; ++i){
00197         int act = argv[iarg][i]-'0';
00198         if(act<0 || act>=nactions){
00199           cout << "Error. Action argument is invalid.\n";
00200           return 1;
00201         } else{
00202           actions[i] = (roAction_t)act;
00203         }
00204       }
00205       continue;
00206     }
00207     if(!strcmp(argv[iarg],"-a")||!strcmp(argv[iarg],"--ab")){
00208       if(++iarg>=argc){ cout << "Option error. Try -h\n"; return 1; }
00209       theAB = strtoul(argv[iarg], 0, 0);
00210       if(theAB>=0) --theAB;
00211       if(theAB<-1 || theAB>11){
00212         cout << "AB number is incorrect. Try -h option to get help.\n";
00213       }
00214       continue;
00215     }
00216   }
00217 
00218   for(size_t i=0; i<sizeof(srp2roFlags)/sizeof(srp2roFlags[0]); srp2roFlags[i++]='?');
00219   for(size_t i=0; i<sizeof(actions)/sizeof(actions[0]); ++i){
00220     srp2roFlags[(int)srpFlagMarker[i]] = roFlagMarker[actions[i]];
00221   }
00222 
00223   for(int iEE=0; iEE < nEndcaps; ++iEE){
00224     for(int iY = 0; iY < nSupercrystalXBins; ++iY){
00225       for(int iX = 0; iX < nSupercrystalYBins; ++iX){
00226         int iDCCPhi = dccPhiIndexOfRU(iEE==0?0:2,iX,iY);
00227         if(iDCCPhi>=0){//SC exists
00228           ecalDccSC[iEE][iDCCPhi].push_back(pair<int,int>(iX,iY));
00229         }
00230       }
00231     }
00232   }
00233   
00234   stringstream s;
00235   for(int iAB=0; iAB< nAB; ++iAB){
00236     if(theAB!=-1 && theAB!=iAB) continue;
00237     s.str("");
00238     s << abTTFFilePrefix << (iAB<9?"0":"") << iAB+1 << abTTFFilePostfix;
00239     abTTFFiles[iAB].open(s.str().c_str(), ios::out);
00240     writeABTTFFileHeader(abTTFFiles[iAB], iAB);
00241     s.str("");
00242     s << abSRFFilePrefix << (iAB<9?"0":"") << iAB+1 << abSRFFilePostfix;
00243     abSRFFiles[iAB].open(s.str().c_str(), ios::out);
00244     writeABSRFFileHeader(abSRFFiles[iAB], iAB);
00245     s.str("");
00246     s << abIOFilePrefix << (iAB<9?"0":"") << iAB+1 << abIOFilePostfix;
00247     abIOFiles[iAB].open(s.str().c_str(), ios::out);
00248     writeABIOFileHeader(abIOFiles[iAB], iAB);
00249   }
00250 
00251   FILE* srfFile = fopen(srfFilename, "r");
00252   if(srfFile==NULL){
00253     cerr << "Failed to open SRF file, " << srfFilename << endl;
00254     exit(EXIT_FAILURE);
00255   }
00256 
00257   FILE* ttfFile = fopen(ttfFilename, "r");
00258   if(ttfFile==NULL){
00259     cerr << "Failed to open TTF file, " << ttfFilename << endl;
00260     exit(EXIT_FAILURE);
00261   }
00262   
00263   iEvent = 0;
00264   while(readSRF(srfFile,barrelSrFlags,endcapSrFlags)
00265         && readTTF(ttfFile,ttFlags)){
00266     if(iEvent%100==0){
00267       cout << "Event " << iEvent << endl;
00268     }
00269     fillABTTFFiles(ttFlags,abTTFFiles);
00270     fillABSRPFiles(barrelSrFlags, endcapSrFlags, abSRFFiles);
00271     fillABIOFiles(ttFlags,barrelSrFlags,endcapSrFlags,abIOFiles);
00272     ++iEvent;
00273   }
00274   
00275   return 0;
00276 }
00277 
00281 void  fillABTTFFiles(const char ttFlags[nTTInEta][nTTInPhi],
00282                      ofstream files[]){
00283   for(int iABEta=0; iABEta<nABInEta; ++iABEta){
00284     for(int iABPhi=0; iABPhi<nABInPhi; ++iABPhi){
00285       int iAB = abNum(iABEta,iABPhi);
00286       int iTTPhiMin;
00287       int iTTPhiMax;
00288       getABTTPhiBounds(iABPhi, iTTPhiMin, iTTPhiMax);
00289       //      writeEventHeader(files[iAB], iEvent, nTTInABAlongPhi);
00290       files[iAB] << "# Event " << iEvent << "\n";
00291       
00292       for(int i = 0 ; i <= iTTEtaMax[iABEta]-iTTEtaMin[iABEta];
00293           ++i){
00294         int iTTEta;
00295         if(iTTEtaSign[iABEta]>0){
00296           iTTEta = iTTEtaMin[iABEta] + i;
00297         } else{
00298           iTTEta = iTTEtaMax[iABEta] - i;
00299         }
00300         for(int iTTPhi = iTTPhiMin;
00301             mod(iTTPhiMax-iTTPhi,nTTInPhi) < nTTInABAlongPhi;
00302             iTTPhi = mod(++iTTPhi, nTTInPhi)){
00303           files[iAB] << ttFlags[iTTEta][iTTPhi];
00304         }
00305         files[iAB] << "\n";
00306       }
00307       files[iAB] << "#\n";
00308       //writeEventTrailer(files[iAB], nTTInABAlongPhi);
00309     }
00310   }  
00311 }
00312 
00313 void  fillABSRPFiles(const char barrelSrFlags[nBarrelTTInEta][nTTInPhi],
00314                      const char endcapSrFlags[nEndcaps][nSupercrystalXBins]
00315                      [nSupercrystalYBins],
00316                      ofstream files[nAB]){
00317   //event headers:
00318   for(int iAB=0; iAB < nAB; ++iAB){
00319     files[iAB] << "# Event " << iEvent << "\n";
00320   }
00321   
00322   bool lineAppended[nAB];
00323   for(int i=0; i< nAB; lineAppended[i++]=false)/*empty*/;
00324   
00325   //EE:
00326   for(int iEE = 0; iEE < nEndcaps; ++iEE){
00327     for(int iX = 0; iX < nSupercrystalXBins; ++iX){
00328       for(int iY=0; iY < nSupercrystalYBins; ++iY){
00329         //        int iDCC = dccIndex(iEE==0?0:2,iX*5,iY*5);
00330         int iDCC = dccIndexOfRU(iEE==0?0:2,iX,iY);
00331         if(iDCC>=0){
00332           int iAB = abOfDcc(iDCC);
00333           if(!lineAppended[iAB]){
00334             for(int i=0; i< iY; ++i) files[iAB] << ' ';
00335           }
00336           files[iAB] << srp2roFlags[(int)endcapSrFlags[iEE][iX][iY]];
00337           lineAppended[iAB] = true;
00338         }
00339       }//next iY
00340       for(int iFile=0; iFile< nAB; ++iFile){
00341         if(lineAppended[iFile]){
00342           files[iFile] << "\n";
00343           lineAppended[iFile] = false;
00344         }
00345       }
00346     }//next iX
00347   }
00348   
00349   //EB:
00350   for(int iABEta=1; iABEta<3; ++iABEta){
00351     for(int iABPhi=0; iABPhi<nABInPhi; ++iABPhi){
00352       int iAB = abNum(iABEta,iABPhi);
00353       int iTTPhiMin;
00354       int iTTPhiMax;
00355       getABTTPhiBounds(iABPhi, iTTPhiMin, iTTPhiMax);
00356       //writeEventHeader(files[iAB], iEvent, nTTInABAlongPhi);
00357       for(int i = 0 ; i <= iTTEtaMax[iABEta]-iTTEtaMin[iABEta];
00358           ++i){
00359         int iTTEta;
00360         if(iTTEtaSign[iABEta]>0){
00361           iTTEta = iTTEtaMin[iABEta] + i;
00362         } else{
00363           iTTEta = iTTEtaMax[iABEta] - i;
00364         }
00365         for(int iTTPhi = iTTPhiMin;
00366             mod(iTTPhiMax-iTTPhi,nTTInPhi) < nTTInABAlongPhi;
00367             iTTPhi = mod(++iTTPhi, nTTInPhi)){
00368           files[iAB] << srp2roFlags[(int)barrelSrFlags[iTTEta-nEndcapTTInEta][iTTPhi]];
00369         }
00370         files[iAB] << "\n";
00371       }
00372       //      writeEventTrailer(files[iAB], nTTInABAlongPhi);
00373       files[iAB] << "#\n";
00374     }
00375   }
00376 
00377   //file trailers
00378   for(int iAB=0; iAB < nAB; ++iAB){
00379     files[iAB] << "#\n";
00380   }
00381 }
00382 
00383 void  fillABIOFiles(const char ttFlags[nTTInEta][nTTInPhi],
00384                     const char barrelSrFlags[nBarrelTTInEta][nTTInPhi],
00385                     const char endcapSrFlags[nEndcaps][nSupercrystalXBins][nSupercrystalYBins],
00386                     ofstream files[]){
00387   for(int iABEta=0; iABEta < nABInEta; ++iABEta){
00388     for(int iABPhi=0; iABPhi < nABInPhi; ++iABPhi){
00389       int iAB = abNum(iABEta, iABPhi);
00390       //      writeABIOFileHeader(files[iAB], iAB);
00391       files[iAB] << "# Event " << iEvent << "\n"; 
00392       //TCC inputs:
00393       for(int iTCC=0; iTCC< nABTCCCh; ++iTCC){
00394         files[iAB] << "ITCC" << iTCC+1 << ":"
00395                    << getABTCCInputStream(ttFlags, iABEta, iABPhi, iTCC)
00396                    << "\n";
00397       }
00398       //AB inputs:
00399       for(int iABCh=0; iABCh<nABABCh; ++iABCh){
00400         files[iAB] << "IAB" <<  iABCh+1 << ":"
00401                    << getABABInputStream(ttFlags, iABEta, iABPhi, iABCh)
00402                    << "\n";
00403       }
00404       //AB outputs:
00405       for(int iABCh=0; iABCh<nABABCh; ++iABCh){
00406         files[iAB] << "OAB" <<  iABCh+1 << ":"
00407                    << getABABOutputStream(ttFlags, iABEta, iABPhi, iABCh)
00408                    << "\n";
00409       }
00410       //DCC output:
00411       for(int iDCCCh=0; iDCCCh<nDCCCh; ++iDCCCh){
00412         files[iAB] << "ODCC";
00413         files[iAB]<< (iDCCCh<=8?"0":"") << iDCCCh+1 << ":"
00414                   << getABDCCOutputStream(barrelSrFlags, endcapSrFlags, iABEta, iABPhi,iDCCCh)
00415                   << "\n";
00416       }
00417       files[iAB] << "#\n";
00418     }
00419   }
00420 }
00421 
00422 /*
00423   stringstream filename;
00424   filename.str("");
00425   filename << abTTFFilePrefix << abNum(iABEta, iABPhi) <<abTTFFilePostfix;
00426   ofstream file(filename.str(), ios::ate);
00427 
00428 */
00429 
00430 bool readTTF(FILE* f, char ttFlags[nTTInEta][nTTInPhi]){
00431   char* buffer = NULL;
00432   size_t bufferSize = 0;
00433   int read;
00434   if(f==NULL) exit(EXIT_FAILURE);
00435   int line=0;
00436   int iEta = 0;
00437   while(iEta<nTTInEta && (read=getline(&buffer, &bufferSize,f))!=-1){
00438     ++line;
00439     char* pos = buffer;
00440     while(*pos==' ' || *pos=='\t') ++pos; //skip spaces
00441     if(*pos!='#' && *pos!='\n'){//not a comment line nor an empty line
00442       if(read-1!=nTTInPhi){
00443         cerr << "Error: line " << line
00444              << " of file "<< ttfFilename << " has incorrect length"
00445           //             << " (" << read-1 << " instead of " << nTTInPhi << ")"
00446              << endl;
00447         exit(EXIT_FAILURE);
00448       }
00449       for(int iPhi=0; iPhi<nTTInPhi; ++iPhi){
00450         ttFlags[iEta][iPhi] = buffer[iPhi];
00451         //         if(ttFlags[iEta][iPhi]!='.'){
00452         //           cout << __FILE__ << ":" << __LINE__ << ": "
00453         //                << iEta << "," << iPhi
00454         //                << " " << ttFlags[iEta][iPhi] << "\n";
00455         //         }
00456       }
00457       ++iEta;
00458     }
00459   }
00460   //returns true if all TT were read (not at end of file)
00461   return (iEta==nTTInEta)?true:false;
00462 }
00463 
00464 bool readSRF(FILE* f,
00465              char barrelSrFlags[nBarrelTTInEta][nTTInPhi],
00466              char endcapSrFlags[nEndcaps][nSupercrystalXBins]
00467              [nSupercrystalYBins]){
00468   char* buffer = NULL;
00469   size_t bufferSize = 0;
00470   int read;
00471   if(f==NULL) exit(EXIT_FAILURE);
00472   int line=0;
00473   int iEta = 0;
00474   int iXm = 0;
00475   int iXp = 0;
00476   int iReadLine = 0;//number of read line, comment lines excluded
00477   //number of non-comment lines to read:
00478   const int nReadLine = nBarrelTTInEta + nEndcaps*nSupercrystalXBins; 
00479   while(iReadLine<nReadLine && (read=getline(&buffer, &bufferSize,f))!=-1){
00480     ++line;
00481     char* pos = buffer;
00482     while(*pos==' ' || *pos=='\t') ++pos; //skip spaces
00483     if(*pos!='#' && *pos!='\n'){//not a comment line nor an empty line
00484       //go back to beginning of line:
00485       pos = buffer;
00486       if(iReadLine<nSupercrystalXBins){//EE- reading
00487         if(read-1!=nSupercrystalYBins){
00488           cerr << "Error: line " << line
00489                << " of file "<< srfFilename << " has incorrect length"
00490                << " (" << read-1 << " instead of " << nSupercrystalYBins << ")"
00491                << endl;
00492           exit(EXIT_FAILURE);
00493         }
00494         for(int iY=0; iY<nSupercrystalYBins; ++iY){
00495           endcapSrFlags[0][iXm][iY] = buffer[iY];
00496         }
00497         ++iXm;
00498       } else if(iReadLine<nSupercrystalYBins+nBarrelTTInEta){//EB reading
00499         if(read-1!=nTTInPhi){
00500           cerr << "Error: line " << line
00501                << " of file "<< srfFilename << " has incorrect length"
00502                << " (" << read-1 << " instead of " << nTTInPhi << ")"
00503                << endl;
00504           exit(EXIT_FAILURE);
00505         }
00506         for(int iPhi=0; iPhi<nTTInPhi; ++iPhi){
00507           barrelSrFlags[iEta][iPhi] = buffer[iPhi];
00508         }
00509         ++iEta;
00510       } else if(iReadLine<2*nSupercrystalXBins+nBarrelTTInEta){ //EE+ reading
00511         if(read-1!=nSupercrystalYBins){
00512           cerr << "Error: line " << line
00513                << " of file "<< srfFilename << " has incorrect length"
00514                << " (" << read-1 << " instead of " << nSupercrystalYBins << ")"
00515                << endl;
00516           exit(EXIT_FAILURE);
00517         }
00518         for(int iY=0; iY<nSupercrystalYBins; ++iY){
00519           endcapSrFlags[1][iXp][iY] = buffer[iY];
00520         }
00521         ++iXp;
00522       }
00523       ++iReadLine;      
00524     }//not a comment or empty line
00525   }
00526   //returns 0 if all TT were read:
00527   return (iReadLine==nReadLine)?true:false;
00528 }
00529 
00530 
00531 // void writeEventHeader(ofstream& f, int iEvent, int nPhi){
00532 //   //event header:
00533 //   stringstream header;
00534 //   header.str("");
00535 //   header << " event " << iEvent << " ";
00536 //   f << "+";
00537 //   for(int iPhi = 0; iPhi < nPhi; ++iPhi){
00538 //     if(iPhi == (int)(nPhi-header.str().size())/2){
00539 //       f << header.str();
00540 //       iPhi += header.str().size()-1;
00541 //     } else{
00542 //       f << "-";
00543 //     }
00544 //   }
00545 //   f << "+\n";
00546 // }
00547 
00548 // void writeEventTrailer(ofstream& f, int nPhi){
00549 //   f << "+";
00550 //   for(int iPhi = 0; iPhi < nPhi; ++iPhi) f << "-";
00551 //   f << "+\n";
00552 // }
00553 
00554 void writeABTTFFileHeader(ofstream& f, int abNum){
00555   time_t t;
00556   time(&t);
00557   const char* date = ctime(&t);
00558   f << "# TTF flag map covered by AB " << abNum+1 <<"\n#\n"
00559     "# Generated on : " << date << "#\n"
00560     "# +---> Phi          " << srpFlagMarker[0] << ": 000 (low interest)\n"
00561     "# |                  " << srpFlagMarker[1] << ": 001 (single)\n"     
00562     "# |                  " << srpFlagMarker[2] << ": 010 (neighbour)\n"    
00563     "# V |Eta|            " << srpFlagMarker[3] << ": 011 (center)\n"     
00564     "#\n";
00565 }
00566 
00567 void writeABSRFFileHeader(ofstream& f, int abNum){
00568   time_t t;
00569   time(&t);
00570   const char* date = ctime(&t);
00571   const char* xLabel;
00572   const char* yLabel;
00573   if(abNum<3 || abNum>8){//endcap
00574     xLabel = "Y  ";
00575     yLabel = "X    ";
00576   } else{//barrel
00577     xLabel = "Phi";
00578     yLabel = "|Eta|";
00579   }
00580   f << "# SRF flag map covered by AB " << abNum+1 <<"\n#\n"
00581     "# Generated on : " << date << "#\n"
00582     "# +---> " << xLabel << "          " << roFlagMarker[0] << ": 000 (suppress)\n"          
00583     "# |                  "              << roFlagMarker[1] << ": 010 (SR Threshold 2)\n"     
00584     "# |                  "              << roFlagMarker[2] << ": 001 (SR Threshold 1)\n"     
00585     "# V " << yLabel << "            "   << roFlagMarker[3] << ": 011 (Full readout)\n"     
00586     "#\n"
00587     "# action table (when forced):\n"
00588     "# LI-> " << roFlagMarker[actions[0]] << " (" << roFlagMarker[actions[4]]<< ")" << "\n"
00589     "# S -> " << roFlagMarker[actions[1]] << " (" << roFlagMarker[actions[5]]<< ")" << "\n"
00590     "# N -> " << roFlagMarker[actions[2]] << " (" << roFlagMarker[actions[6]]<< ")" << "\n"
00591     "# C -> " << roFlagMarker[actions[3]] << " (" << roFlagMarker[actions[7]]<< ")" << "\n";
00592 }
00593 
00594 void writeABIOFileHeader(ofstream& f, int abNum){
00595   time_t t;
00596   time(&t);
00597   const char* date = ctime(&t);
00598   f << "# AB " << abNum+1 <<" I/O \n#\n"
00599     "# Generated on : " << date << "#\n"
00600     "# " << srpFlagMarker[0] << ": 000 (low interest)   " << tccFlagMarker[0] << ": 000 (low interest)   " << roFlagMarker[0] << ": 000 (suppress)\n"
00601     "# " << srpFlagMarker[1] << ": 001 (single)         " << tccFlagMarker[1] << ": 001 (mid interest)   " << roFlagMarker[1] << ": 010 (SR Threshold 2)\n"
00602     "# " << srpFlagMarker[2] << ": 010 (neighbour)      " << tccFlagMarker[2] << ": 010 (not valid)      " << roFlagMarker[2] << ": 001 (SR Threshold 1)\n"   
00603     "# " << srpFlagMarker[3] << ": 011 (center)         " << tccFlagMarker[3] << ": 011 (high interest)  " <<   roFlagMarker[3] << ": 011 (Full readout)\n"
00604     "#\n"
00605     "# action table (when forced):\n"
00606     "# LI-> " << roFlagMarker[actions[0]] << " (" << roFlagMarker[actions[4]]<< ")" << "\n"
00607     "# S -> " << roFlagMarker[actions[1]] << " (" << roFlagMarker[actions[5]]<< ")" << "\n"
00608     "# N -> " << roFlagMarker[actions[2]] << " (" << roFlagMarker[actions[6]]<< ")" << "\n"
00609     "# C -> " << roFlagMarker[actions[3]] << " (" << roFlagMarker[actions[7]]<< ")" << "\n"
00610     "#\n"; 
00611 }
00612 
00613 string getFlagStream(const char flags[nTTInEta][nTTInPhi], int iEtaMin,
00614                      int iEtaMax, int iPhiMin, int iPhiMax){
00615   assert(0<=iEtaMin && iEtaMin<=iEtaMax && iEtaMax<nTTInEta);
00616   if(iEtaMin<=nTTInEta/2 && iEtaMax>nTTInEta){
00617     cerr << "Implementation Errror:"
00618          << __FILE__ << ":" << __LINE__ 
00619          << ": A flag stream cannot covers parts of both half-ECAL!"
00620          << endl;
00621     exit(EXIT_FAILURE);
00622   }
00623 
00624   bool zPos = (iEtaMin>=nTTInEta/2);
00625 
00626   stringstream buffer;
00627   buffer.str("");
00628   for(int jEta = 0; jEta <= iEtaMax-iEtaMin; ++jEta){
00629     //loops on iEta in |eta| increasing order:
00630     int iEta;
00631     if(zPos){
00632       iEta = iEtaMin + jEta;
00633     } else{
00634       iEta = iEtaMax - jEta;
00635     }
00636 
00637     for(int iPhi = mod(iPhiMin,nTTInPhi);
00638         mod(iPhiMax+1-iPhi,nTTInPhi) != 0;
00639         iPhi = mod(++iPhi, nTTInPhi)){
00640       buffer << flags[iEta][iPhi];
00641     }
00642   }
00643   
00644   return buffer.str();
00645 }
00646 
00647 string getABTCCInputStream(const char tccFlags[nTTInEta][nTTInPhi],
00648                            int iABEta, int iABPhi, int iTCCCh){
00649   //gets eta bounds for this tcc channel:
00650   int iTCCEta;
00651   if(iABEta==1 || iABEta==2){//barrel
00652     if(iTCCCh>5) return ""; //only 6 TCCs per AB for barrel
00653     iTCCEta =  1 + iABEta;
00654   } else{//endcap
00655     if(iABEta==0){//EE-
00656       iTCCEta = (iTCCCh<6)?1:0;
00657     } else{//EE+
00658       iTCCEta = (iTCCCh<6)?4:5;
00659     }
00660   }      
00661   int iEtaMin = iTCCEtaBounds[iTCCEta];
00662   int iEtaMax = iTCCEtaBounds[iTCCEta+1]-1;
00663 
00664   //gets phi bounds:
00665   int iPhiMin;
00666   int iPhiMax;
00667   getABTTPhiBounds(iABPhi, iPhiMin, iPhiMax);
00668   //phi is increasing with TTC channel number
00669   //a TTC covers a 4TT-wide phi-sector
00670   //=>iPhiMin(iTTCCh) = iPhiMin(AB) + 4*iTTCCh for iTCCCh<6 
00671   iPhiMin += 4*(iTCCCh%6);
00672   iPhiMax = iPhiMin + 4 - 1;
00673   
00674   return getFlagStream(tccFlags, iEtaMin, iEtaMax, iPhiMin, iPhiMax);
00675 }
00676 
00677 string getABABOutputStream(const char tccFlags[nTTInEta][nTTInPhi],
00678                            int iABEta, int iABPhi, int iABCh){
00679   stringstream buffer;
00680   buffer.str("");
00681   bool barrel = (iABEta==1 || iABEta==2); //true for barrel, false for endcap
00682   switch(iABCh){
00683   case 0:
00684     //to AB ch #0 are sent the 16 1st TCC flags received on TCC input Ch. 0
00685     buffer << getABTCCInputStream(tccFlags, iABEta, iABPhi, 0).substr(0,16);
00686     break;
00687   case 1:
00688     //to AB ch #1 are sent the 16 1st TCC flags received on TCC input Ch. 0 to 5:
00689     for(int iTCCCh=0; iTCCCh<6; ++iTCCCh){
00690       buffer <<
00691         getABTCCInputStream(tccFlags, iABEta, iABPhi, iTCCCh).substr(0,16);
00692     }
00693     break;
00694   case 2:
00695     //to AB ch #2 are sent the 16 1st TCC flags received on TCC input Ch. 5:
00696     buffer << getABTCCInputStream(tccFlags, iABEta, iABPhi, 5).substr(0,16);
00697     break;
00698   case 3:
00699     //to AB ch #3 are sent TCC flags received on TCC input Ch. 0 and 6:
00700     buffer << getABTCCInputStream(tccFlags, iABEta, iABPhi, 0);
00701     buffer << getABTCCInputStream(tccFlags, iABEta, iABPhi, 6);
00702     break;
00703   case 4:
00704     //to AB ch #4 are sent TCC flags received on TCC input Ch 5 and 11:
00705     buffer << getABTCCInputStream(tccFlags, iABEta, iABPhi, 5);
00706     buffer << getABTCCInputStream(tccFlags, iABEta, iABPhi, 11);
00707     break;
00708   case 5:
00709     //for endcaps AB output ch 5 is not used.
00710     //for barrel, to AB ch #5 are sent the 16 last TCC flags received on TCC
00711     //input Ch. 0:
00712     if(barrel){//in barrel
00713       string s = getABTCCInputStream(tccFlags, iABEta, iABPhi, 0);
00714       assert(s.size()>=16);
00715       buffer << s.substr(s.size()-16,16);
00716     }
00717     break;
00718   case 6:
00719     //for endcaps AB output ch 6 is not used.
00720     //for barrel, to AB ch #6 are sent the 16 last TCC flags received on TCC
00721     //input Ch. 0 to 5:
00722     if(barrel){//in barrel
00723       for(int iTCCCh=0; iTCCCh<6; ++iTCCCh){
00724         string s = getABTCCInputStream(tccFlags, iABEta, iABPhi, iTCCCh); 
00725         buffer << s.substr(s.size()-16,16);
00726       }
00727     }
00728     break;
00729   case 7:
00730     //for endcaps AB output ch 7 is not used.
00731     //for barrel, to AB ch #7 are sent the 16 last TCC flags received on TCC
00732     //input Ch. 5:
00733     if(barrel){//in barrel
00734       string s = getABTCCInputStream(tccFlags, iABEta, iABPhi, 5);
00735       assert(s.size()>=16);
00736       buffer << s.substr(s.size()-16,16);
00737     }
00738     break;
00739   default:
00740     assert(false);
00741   }
00742   return buffer.str();
00743 }
00744 
00745 string getABABInputStream(const char tccFlags[nTTInEta][nTTInPhi], int iABEta, 
00746                           int iABPhi, int iABCh){
00747   int iAB = abNum(iABEta, iABPhi);
00748   int iOtherAB; //AB which this channel is connected to
00749   int iOtherABCh; //ch # on the other side of the AB-AB link
00750   abConnect(iAB,iABCh,iOtherAB,iOtherABCh);
00751   int iOtherABEta = iOtherAB/3;
00752   int iOtherABPhi = iOtherAB%3;
00753   return getABABOutputStream(tccFlags, iOtherABEta, iOtherABPhi, iOtherABCh);
00754 }
00755 
00756 
00757 void getABTTPhiBounds(int iABPhi, int& iTTPhiMin, int& iTTPhiMax){
00758   iTTPhiMin = mod(-6+iABPhi*nTTInABAlongPhi,nTTInPhi);
00759   iTTPhiMax = mod(iTTPhiMin+nTTInABAlongPhi-1, nTTInPhi);
00760 }
00761 
00762 void abConnect(int iAB,int iABCh,int& iOtherAB,int& iOtherABCh){
00763   static bool firstCall = true;
00764   static int xconnectMap[nAB][nABABCh][2];
00765   if(firstCall){
00766     FILE* f = fopen(xconnectFilename, "r");
00767     if(f==NULL){
00768       cerr << "Error. Failed to open xconnect definition file,"
00769            << xconnectFilename << endl;
00770       exit(EXIT_FAILURE);
00771     }
00772     //skips two first lines:
00773     for(int i=0; i<2; ++i){
00774       int c;
00775       while((c=getc(f))!='\n' && c >=0);
00776     }
00777     int ilink = 0 ;
00778     while(!feof(f)){
00779       int abIn;
00780       int pinIn;
00781       int abOut;
00782       int pinOut;
00783       if(4==fscanf(f, "%d\t%d\t%d\t%d", &abIn, &pinIn, &abOut, &pinOut)){
00784         xconnectMap[abIn][pinIn][0] = abOut;
00785         xconnectMap[abIn][pinIn][1] = pinOut;
00786         ++ilink;
00787       }
00788     }
00789     if(ilink!=nAB*nABABCh){
00790       cerr << "Error cross-connect definition file " << xconnectFilename
00791            << " contains an unexpected number of link definition."
00792            << endl;
00793       exit(EXIT_FAILURE);
00794     }
00795     firstCall = false;
00796   }
00797 
00798   iOtherAB = xconnectMap[iAB][iABCh][0];
00799   iOtherABCh = xconnectMap[iAB][iABCh][1];
00800 }
00801 
00802 string getABDCCOutputStream(const char barrelSrFlags[nBarrelTTInEta][nTTInPhi],
00803                             const char endcapSrFlags[nEndcaps][nSupercrystalXBins]
00804                             [nSupercrystalYBins],
00805                             int iABEta, int iABPhi, int iDCCCh){
00806   bool barrel = (iABEta==1||iABEta==2);
00807   if(barrel){
00808     //same as TCC with same ch number but with TCC flags replaced by SRP flags:
00809     string stream = getABTCCInputStream(barrelSrFlags-nEndcapTTInEta, iABEta, iABPhi, iDCCCh);
00810     //converts srp flags to readout flags:
00811     for(size_t i=0; i< stream.size(); ++i){
00812       stream[i] = srp2roFlags[(int)stream[i]];
00813     }
00814     return stream;
00815   } else{//endcap
00816     if(iDCCCh<3){//used DCC output channel
00817       //endcap index:
00818       int iEE=(iABEta==0)?0:1;
00819       stringstream buffer("");
00820       //3 DCC per AB and AB DCC output channel in
00821       //increasing DCC phi position:
00822       int iDCCPhi = iABPhi*3+iDCCCh;
00823       for(size_t iSC=0; iSC < ecalDccSC[iEE][iDCCPhi].size(); ++iSC){
00824         pair<int,int> sc = ecalDccSC[iEE][iDCCPhi][iSC];
00825         buffer << srp2roFlags[(int)endcapSrFlags[iEE][sc.first][sc.second]];
00826       }
00827       return buffer.str();
00828     } else{//unused output channel
00829       return "";
00830     }
00831   }
00832 }

Generated on Tue Jun 9 17:46:15 2009 for CMSSW by  doxygen 1.5.4