CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
GenABIO.cc
Go to the documentation of this file.
1 // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: t; tab-width: 8; -*-
2 
10 #include <iostream>
11 #include <fstream>
12 #include <sstream>
13 #include <vector>
14 #include <cassert>
15 #include <cstdlib>
16 #include <cstring>
17 
18 #include "ecalDccMap.h"
19 
20 #if !defined(__linux__) && !(defined(__APPLE__) && __DARWIN_C_LEVEL >= 200809L)
21 #include <errno.h>
22 /* getline implementation is copied from glibc. */
23 
24 #ifndef SIZE_MAX
25 # define SIZE_MAX ((size_t) -1)
26 #endif
27 #ifndef SSIZE_MAX
28 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
29 #endif
30 namespace {
31 ssize_t getline (char **lineptr, size_t *n, FILE *fp)
32 {
33  ssize_t result;
34  size_t cur_len = 0;
35 
36  if (lineptr == NULL || n == NULL || fp == NULL)
37  {
38  errno = EINVAL;
39  return -1;
40  }
41 
42  if (*lineptr == NULL || *n == 0)
43  {
44  *n = 120;
45  *lineptr = (char *) malloc (*n);
46  if (*lineptr == NULL)
47  {
48  result = -1;
49  goto end;
50  }
51  }
52 
53  for (;;)
54  {
55  int i;
56 
57  i = getc (fp);
58  if (i == EOF)
59  {
60  result = -1;
61  break;
62  }
63 
64  /* Make enough space for len+1 (for final NUL) bytes. */
65  if (cur_len + 1 >= *n)
66  {
67  size_t needed_max =
68  SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX;
69  size_t needed = 2 * *n + 1; /* Be generous. */
70  char *new_lineptr;
71 
72  if (needed_max < needed)
73  needed = needed_max;
74  if (cur_len + 1 >= needed)
75  {
76  result = -1;
77  goto end;
78  }
79 
80  new_lineptr = (char *) realloc (*lineptr, needed);
81  if (new_lineptr == NULL)
82  {
83  result = -1;
84  goto end;
85  }
86 
87  *lineptr = new_lineptr;
88  *n = needed;
89  }
90 
91  (*lineptr)[cur_len] = i;
92  cur_len++;
93 
94  if (i == '\n')
95  break;
96  }
97  (*lineptr)[cur_len] = '\0';
98  result = cur_len ? (ssize_t) cur_len : result;
99 
100 end:
101  return result;
102 }
103 }
104 #endif
105 
106 
107 using namespace std;
108 
111 const static int nEndcapXBins = 100;
114 const static int nEndcapYBins = 100;
117 const static int supercrystalEdge = 5;
126 const static int nEndcaps = 2;
129 const static int nEndcapTTInEta = 11;
132 const static int nBarrelTTInEta = 34;
135 const static int nTTInEta =
139 const static int nTTInPhi = 72;
142 const int nABInEta=4;
145 const int nABInPhi=3;
148 const int nDCCEE = 9;
149 const int nABABCh = 8;//nbr of AB input/output ch. on an AB
150 const int nABTCCCh = 12;//nbr of TCC inputs on an AB
151 const int nDCCCh = 12;//nbr of DCC outputs on an AB
152 const int nTCCInEta = 6; //nbr of TCC bins along eta
153 const int nAB = nABInPhi*nABInEta;
155 const int iTTEtaMin[nABInEta] = {0,11,28,45};
156 const int iTTEtaMax[nABInEta] = {10,27,44,55};
157 const int iTTEtaSign[nABInEta] = {-1,-1,1,1};
158 
159 //Eta bounds for TCC partionning
160 //a TCC covers from iTCCEtaBounds[iTCCEta] included to
161 //iTCCEtaBounds[iTCCEta+1] excluded.
162 const int iTCCEtaBounds[nTCCInEta+1] = {0,7,11,28,45,49,56};
163 
164 const char* abTTFFilePrefix = "TTF_AB";
165 const char* abTTFFilePostfix = ".txt";
166 const char* abSRFFilePrefix = "AF_AB";
167 const char* abSRFFilePostfix = ".txt";
168 const char* abIOFilePrefix = "IO_AB";
169 const char* abIOFilePostfix = ".txt";
170 
171 const char* srfFilename = "SRF.txt";
172 const char* ttfFilename = "TTF.txt";
173 const char* xconnectFilename = "xconnect_universal.txt";
174 
175 const char srpFlagMarker[] = {'.', 'S', 'N', 'C', '4','5','6','7'};
176 const char tccFlagMarker[] = {'.', 'S', '?', 'C', '4', '5', '6', '7'};
177 
178 char srp2roFlags[128];
179 
181 char roFlagMarker[] = {/*suppress*/'.', /*sr1*/'z', /*sr1*/'Z', /*full*/'F',
182  /*fsuppress*/'4', /*fsr2*/'5', /*fsr1*/'6', /*ffull*/'7'
183 };
184 
185 const int nactions = 8;
186 //can be overwritten according by cmd line arguments
187 roAction_t actions[nactions] = {/*LI->*/sr2,/*S->*/full,/*N->*/ full,/*C->*/full,
188  /*fLI->*/sr2,/*fS->*/sr2,/*fN->*/sr2,/*fC->*/sr2};
189 
190 //list of SC deserves by an endcap DCC [0(EE-)|1(EE+)][iDCCPhi]
191 vector<pair<int, int> > ecalDccSC[nEndcaps][nDCCEE];
192 
193 void fillABTTFFiles(const char ttFlags[nTTInEta][nTTInPhi],
194  ofstream files[]);
195 void fillABSRPFiles(const char barrelSrFlags[nBarrelTTInEta][nTTInPhi],
196  const char endcapSrFlags[nEndcaps][nSupercrystalXBins]
198  ofstream files[]);
199 void fillABIOFiles(const char ttFlags[nTTInEta][nTTInPhi],
200  const char barrelSrFlags[nBarrelTTInEta][nTTInPhi],
201  const char endcapSrFlags[nEndcaps][nSupercrystalXBins]
203  ofstream files[]);
204 inline int abNum(int iABEta, int iABPhi){return 3*iABEta+iABPhi;}
205 
206 bool readTTF(FILE* file, char ttFlags[nTTInEta][nTTInPhi]);
207 bool readSRF(FILE* file,
208  char barrelSrFlags[nBarrelTTInEta][nTTInPhi],
209  char endcapSrFlags[nEndcaps][nSupercrystalXBins]
211 
212 void writeABTTFFileHeader(ofstream& f, int abNum);
213 void writeABSRFFileHeader(ofstream& f, int abNum);
214 void writeABIOFileHeader(ofstream& f, int abNum);
215 string getFlagStream(char flags[nTTInEta][nTTInPhi], int iEtaMin, int iEtaMax,
216  int iPhiMin, int iPhiMax);
217 string getABTCCInputStream(const char tccFlags[nTTInEta][nTTInPhi],
218  int iABEta, int iABPhi, int iTCCCh);
219 void getABTTPhiBounds(int iABPhi, int& iTTPhiMin, int& iTTPhiMax);
220 string getABABOutputStream(const char tccFlags[nTTInEta][nTTInPhi],
221  int iABEta, int iABPhi, int iABCh);
222 string getABABInputStream(const char tccFlags[nTTInEta][nTTInPhi], int iABEta,
223  int iABPhi, int iABCh);
224 string getABDCCOutputStream(const char barrelSrFlags[nBarrelTTInEta][nTTInPhi],
225  const char endcapSrFlags[nEndcaps][nSupercrystalXBins]
227  int iABEta, int iABPhi, int DCCCh);
228 void abConnect(int iAB,int iABCh,int& iOtherAB,int& iOtherABCh);
229 
230 int iEvent = 0;
231 
232 int theAB = -1;
233 
234 int main(int argc, char* argv[]){
235  char barrelSrFlags[nBarrelTTInEta][nTTInPhi];
236  char endcapSrFlags[nEndcaps][nEndcapXBins/5][nEndcapYBins/5];
237  char ttFlags[nTTInEta][nTTInPhi];
238  ofstream abTTFFiles[nAB];
239  ofstream abSRFFiles[nAB];
240  ofstream abIOFiles[nAB];
241 
242  int iarg = 0;
243  while(++iarg<argc){
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 "
250  "launched.\n\n"
251  "OPTIONS:\n"
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 "
264  "for every AB\n\n"
265  ;
266 
267  return 0;
268  }
269 
270  if(!strcmp(argv[iarg],"-A") || !strcmp(argv[iarg],"-A")){//actions
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';
274  if(act<0 || act>=nactions){
275  cout << "Error. Action argument is invalid.\n";
276  return 1;
277  } else{
278  actions[i] = (roAction_t)act;
279  }
280  }
281  continue;
282  }
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);
286  if(theAB>=0) --theAB;
287  if(theAB<-1 || theAB>11){
288  cout << "AB number is incorrect. Try -h option to get help.\n";
289  }
290  continue;
291  }
292  }
293 
294  for(size_t i=0; i<sizeof(srp2roFlags)/sizeof(srp2roFlags[0]); srp2roFlags[i++]='?');
295  for(size_t i=0; i<sizeof(actions)/sizeof(actions[0]); ++i){
297  }
298 
299  for(int iEE=0; iEE < nEndcaps; ++iEE){
300  for(int iY = 0; iY < nSupercrystalXBins; ++iY){
301  for(int iX = 0; iX < nSupercrystalYBins; ++iX){
302  int iDCCPhi = dccPhiIndexOfRU(iEE==0?0:2,iX,iY);
303  if(iDCCPhi>=0){//SC exists
304  ecalDccSC[iEE][iDCCPhi].push_back(pair<int,int>(iX,iY));
305  }
306  }
307  }
308  }
309 
310  stringstream s;
311  for(int iAB=0; iAB< nAB; ++iAB){
312  if(theAB!=-1 && theAB!=iAB) continue;
313  s.str("");
314  s << abTTFFilePrefix << (iAB<9?"0":"") << iAB+1 << abTTFFilePostfix;
315  abTTFFiles[iAB].open(s.str().c_str(), ios::out);
316  writeABTTFFileHeader(abTTFFiles[iAB], iAB);
317  s.str("");
318  s << abSRFFilePrefix << (iAB<9?"0":"") << iAB+1 << abSRFFilePostfix;
319  abSRFFiles[iAB].open(s.str().c_str(), ios::out);
320  writeABSRFFileHeader(abSRFFiles[iAB], iAB);
321  s.str("");
322  s << abIOFilePrefix << (iAB<9?"0":"") << iAB+1 << abIOFilePostfix;
323  abIOFiles[iAB].open(s.str().c_str(), ios::out);
324  writeABIOFileHeader(abIOFiles[iAB], iAB);
325  }
326 
327  FILE* srfFile = fopen(srfFilename, "r");
328  if(srfFile==NULL){
329  cerr << "Failed to open SRF file, " << srfFilename << endl;
330  exit(EXIT_FAILURE);
331  }
332 
333  FILE* ttfFile = fopen(ttfFilename, "r");
334  if(ttfFile==NULL){
335  cerr << "Failed to open TTF file, " << ttfFilename << endl;
336  exit(EXIT_FAILURE);
337  }
338 
339  iEvent = 0;
340  while(readSRF(srfFile,barrelSrFlags,endcapSrFlags)
341  && readTTF(ttfFile,ttFlags)){
342  if(iEvent%100==0){
343  cout << "Event " << iEvent << endl;
344  }
345  fillABTTFFiles(ttFlags,abTTFFiles);
346  fillABSRPFiles(barrelSrFlags, endcapSrFlags, abSRFFiles);
347  fillABIOFiles(ttFlags,barrelSrFlags,endcapSrFlags,abIOFiles);
348  ++iEvent;
349  }
350 
351  return 0;
352 }
353 
357 void fillABTTFFiles(const char ttFlags[nTTInEta][nTTInPhi],
358  ofstream files[]){
359  for(int iABEta=0; iABEta<nABInEta; ++iABEta){
360  for(int iABPhi=0; iABPhi<nABInPhi; ++iABPhi){
361  int iAB = abNum(iABEta,iABPhi);
362  int iTTPhiMin;
363  int iTTPhiMax;
364  getABTTPhiBounds(iABPhi, iTTPhiMin, iTTPhiMax);
365  // writeEventHeader(files[iAB], iEvent, nTTInABAlongPhi);
366  files[iAB] << "# Event " << iEvent << "\n";
367 
368  for(int i = 0 ; i <= iTTEtaMax[iABEta]-iTTEtaMin[iABEta];
369  ++i){
370  int iTTEta;
371  if(iTTEtaSign[iABEta]>0){
372  iTTEta = iTTEtaMin[iABEta] + i;
373  } else{
374  iTTEta = iTTEtaMax[iABEta] - i;
375  }
376  for(int iTTPhi = iTTPhiMin;
377  mod(iTTPhiMax-iTTPhi,nTTInPhi) < nTTInABAlongPhi;
378  iTTPhi = mod(++iTTPhi, nTTInPhi)){
379  files[iAB] << ttFlags[iTTEta][iTTPhi];
380  }
381  files[iAB] << "\n";
382  }
383  files[iAB] << "#\n";
384  //writeEventTrailer(files[iAB], nTTInABAlongPhi);
385  }
386  }
387 }
388 
389 void fillABSRPFiles(const char barrelSrFlags[nBarrelTTInEta][nTTInPhi],
390  const char endcapSrFlags[nEndcaps][nSupercrystalXBins]
392  ofstream files[nAB]){
393  //event headers:
394  for(int iAB=0; iAB < nAB; ++iAB){
395  files[iAB] << "# Event " << iEvent << "\n";
396  }
397 
398  bool lineAppended[nAB];
399  for(int i=0; i< nAB; lineAppended[i++]=false)/*empty*/;
400 
401  //EE:
402  for(int iEE = 0; iEE < nEndcaps; ++iEE){
403  for(int iX = 0; iX < nSupercrystalXBins; ++iX){
404  for(int iY=0; iY < nSupercrystalYBins; ++iY){
405  // int iDCC = dccIndex(iEE==0?0:2,iX*5,iY*5);
406  int iDCC = dccIndexOfRU(iEE==0?0:2,iX,iY);
407  if(iDCC>=0){
408  int iAB = abOfDcc(iDCC);
409  if(!lineAppended[iAB]){
410  for(int i=0; i< iY; ++i) files[iAB] << ' ';
411  }
412  files[iAB] << srp2roFlags[(int)endcapSrFlags[iEE][iX][iY]];
413  lineAppended[iAB] = true;
414  }
415  }//next iY
416  for(int iFile=0; iFile< nAB; ++iFile){
417  if(lineAppended[iFile]){
418  files[iFile] << "\n";
419  lineAppended[iFile] = false;
420  }
421  }
422  }//next iX
423  }
424 
425  //EB:
426  for(int iABEta=1; iABEta<3; ++iABEta){
427  for(int iABPhi=0; iABPhi<nABInPhi; ++iABPhi){
428  int iAB = abNum(iABEta,iABPhi);
429  int iTTPhiMin;
430  int iTTPhiMax;
431  getABTTPhiBounds(iABPhi, iTTPhiMin, iTTPhiMax);
432  //writeEventHeader(files[iAB], iEvent, nTTInABAlongPhi);
433  for(int i = 0 ; i <= iTTEtaMax[iABEta]-iTTEtaMin[iABEta];
434  ++i){
435  int iTTEta;
436  if(iTTEtaSign[iABEta]>0){
437  iTTEta = iTTEtaMin[iABEta] + i;
438  } else{
439  iTTEta = iTTEtaMax[iABEta] - i;
440  }
441  for(int iTTPhi = iTTPhiMin;
442  mod(iTTPhiMax-iTTPhi,nTTInPhi) < nTTInABAlongPhi;
443  iTTPhi = mod(++iTTPhi, nTTInPhi)){
444  files[iAB] << srp2roFlags[(int)barrelSrFlags[iTTEta-nEndcapTTInEta][iTTPhi]];
445  }
446  files[iAB] << "\n";
447  }
448  // writeEventTrailer(files[iAB], nTTInABAlongPhi);
449  files[iAB] << "#\n";
450  }
451  }
452 
453  //file trailers
454  for(int iAB=0; iAB < nAB; ++iAB){
455  files[iAB] << "#\n";
456  }
457 }
458 
459 void fillABIOFiles(const char ttFlags[nTTInEta][nTTInPhi],
460  const char barrelSrFlags[nBarrelTTInEta][nTTInPhi],
461  const char endcapSrFlags[nEndcaps][nSupercrystalXBins][nSupercrystalYBins],
462  ofstream files[]){
463  for(int iABEta=0; iABEta < nABInEta; ++iABEta){
464  for(int iABPhi=0; iABPhi < nABInPhi; ++iABPhi){
465  int iAB = abNum(iABEta, iABPhi);
466  // writeABIOFileHeader(files[iAB], iAB);
467  files[iAB] << "# Event " << iEvent << "\n";
468  //TCC inputs:
469  for(int iTCC=0; iTCC< nABTCCCh; ++iTCC){
470  files[iAB] << "ITCC" << iTCC+1 << ":"
471  << getABTCCInputStream(ttFlags, iABEta, iABPhi, iTCC)
472  << "\n";
473  }
474  //AB inputs:
475  for(int iABCh=0; iABCh<nABABCh; ++iABCh){
476  files[iAB] << "IAB" << iABCh+1 << ":"
477  << getABABInputStream(ttFlags, iABEta, iABPhi, iABCh)
478  << "\n";
479  }
480  //AB outputs:
481  for(int iABCh=0; iABCh<nABABCh; ++iABCh){
482  files[iAB] << "OAB" << iABCh+1 << ":"
483  << getABABOutputStream(ttFlags, iABEta, iABPhi, iABCh)
484  << "\n";
485  }
486  //DCC output:
487  for(int iDCCCh=0; iDCCCh<nDCCCh; ++iDCCCh){
488  files[iAB] << "ODCC";
489  files[iAB]<< (iDCCCh<=8?"0":"") << iDCCCh+1 << ":"
490  << getABDCCOutputStream(barrelSrFlags, endcapSrFlags, iABEta, iABPhi,iDCCCh)
491  << "\n";
492  }
493  files[iAB] << "#\n";
494  }
495  }
496 }
497 
498 /*
499  stringstream filename;
500  filename.str("");
501  filename << abTTFFilePrefix << abNum(iABEta, iABPhi) <<abTTFFilePostfix;
502  ofstream file(filename.str(), ios::ate);
503 
504 */
505 
506 bool readTTF(FILE* f, char ttFlags[nTTInEta][nTTInPhi]){
507  char* buffer = NULL;
508  size_t bufferSize = 0;
509  int read;
510  if(f==NULL) exit(EXIT_FAILURE);
511  int line=0;
512  int iEta = 0;
513  while(iEta<nTTInEta && (read=getline(&buffer, &bufferSize,f))!=-1){
514  ++line;
515  char* pos = buffer;
516  while(*pos==' ' || *pos=='\t') ++pos; //skip spaces
517  if(*pos!='#' && *pos!='\n'){//not a comment line nor an empty line
518  if(read-1!=nTTInPhi){
519  cerr << "Error: line " << line
520  << " of file "<< ttfFilename << " has incorrect length"
521  // << " (" << read-1 << " instead of " << nTTInPhi << ")"
522  << endl;
523  exit(EXIT_FAILURE);
524  }
525  for(int iPhi=0; iPhi<nTTInPhi; ++iPhi){
526  ttFlags[iEta][iPhi] = buffer[iPhi];
527  // if(ttFlags[iEta][iPhi]!='.'){
528  // cout << __FILE__ << ":" << __LINE__ << ": "
529  // << iEta << "," << iPhi
530  // << " " << ttFlags[iEta][iPhi] << "\n";
531  // }
532  }
533  ++iEta;
534  }
535  }
536  //returns true if all TT were read (not at end of file)
537  return (iEta==nTTInEta)?true:false;
538 }
539 
540 bool readSRF(FILE* f,
541  char barrelSrFlags[nBarrelTTInEta][nTTInPhi],
542  char endcapSrFlags[nEndcaps][nSupercrystalXBins]
544  char* buffer = NULL;
545  size_t bufferSize = 0;
546  int read;
547  if(f==NULL) exit(EXIT_FAILURE);
548  int line=0;
549  int iEta = 0;
550  int iXm = 0;
551  int iXp = 0;
552  int iReadLine = 0;//number of read line, comment lines excluded
553  //number of non-comment lines to read:
554  const int nReadLine = nBarrelTTInEta + nEndcaps*nSupercrystalXBins;
555  while(iReadLine<nReadLine && (read=getline(&buffer, &bufferSize,f))!=-1){
556  ++line;
557  char* pos = buffer;
558  while(*pos==' ' || *pos=='\t') ++pos; //skip spaces
559  if(*pos!='#' && *pos!='\n'){//not a comment line nor an empty line
560  //go back to beginning of line:
561  pos = buffer;
562  if(iReadLine<nSupercrystalXBins){//EE- reading
563  if(read-1!=nSupercrystalYBins){
564  cerr << "Error: line " << line
565  << " of file "<< srfFilename << " has incorrect length"
566  << " (" << read-1 << " instead of " << nSupercrystalYBins << ")"
567  << endl;
568  exit(EXIT_FAILURE);
569  }
570  for(int iY=0; iY<nSupercrystalYBins; ++iY){
571  endcapSrFlags[0][iXm][iY] = buffer[iY];
572  }
573  ++iXm;
574  } else if(iReadLine<nSupercrystalYBins+nBarrelTTInEta){//EB reading
575  if(read-1!=nTTInPhi){
576  cerr << "Error: line " << line
577  << " of file "<< srfFilename << " has incorrect length"
578  << " (" << read-1 << " instead of " << nTTInPhi << ")"
579  << endl;
580  exit(EXIT_FAILURE);
581  }
582  for(int iPhi=0; iPhi<nTTInPhi; ++iPhi){
583  barrelSrFlags[iEta][iPhi] = buffer[iPhi];
584  }
585  ++iEta;
586  } else if(iReadLine<2*nSupercrystalXBins+nBarrelTTInEta){ //EE+ reading
587  if(read-1!=nSupercrystalYBins){
588  cerr << "Error: line " << line
589  << " of file "<< srfFilename << " has incorrect length"
590  << " (" << read-1 << " instead of " << nSupercrystalYBins << ")"
591  << endl;
592  exit(EXIT_FAILURE);
593  }
594  for(int iY=0; iY<nSupercrystalYBins; ++iY){
595  endcapSrFlags[1][iXp][iY] = buffer[iY];
596  }
597  ++iXp;
598  }
599  ++iReadLine;
600  }//not a comment or empty line
601  }
602  //returns 0 if all TT were read:
603  return (iReadLine==nReadLine)?true:false;
604 }
605 
606 
607 // void writeEventHeader(ofstream& f, int iEvent, int nPhi){
608 // //event header:
609 // stringstream header;
610 // header.str("");
611 // header << " event " << iEvent << " ";
612 // f << "+";
613 // for(int iPhi = 0; iPhi < nPhi; ++iPhi){
614 // if(iPhi == (int)(nPhi-header.str().size())/2){
615 // f << header.str();
616 // iPhi += header.str().size()-1;
617 // } else{
618 // f << "-";
619 // }
620 // }
621 // f << "+\n";
622 // }
623 
624 // void writeEventTrailer(ofstream& f, int nPhi){
625 // f << "+";
626 // for(int iPhi = 0; iPhi < nPhi; ++iPhi) f << "-";
627 // f << "+\n";
628 // }
629 
630 void writeABTTFFileHeader(ofstream& f, int abNum){
631  time_t t;
632  time(&t);
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"
637  "# | " << srpFlagMarker[1] << ": 001 (single)\n"
638  "# | " << srpFlagMarker[2] << ": 010 (neighbour)\n"
639  "# V |Eta| " << srpFlagMarker[3] << ": 011 (center)\n"
640  "#\n";
641 }
642 
643 void writeABSRFFileHeader(ofstream& f, int abNum){
644  time_t t;
645  time(&t);
646  const char* date = ctime(&t);
647  const char* xLabel;
648  const char* yLabel;
649  if(abNum<3 || abNum>8){//endcap
650  xLabel = "Y ";
651  yLabel = "X ";
652  } else{//barrel
653  xLabel = "Phi";
654  yLabel = "|Eta|";
655  }
656  f << "# SRF flag map covered by AB " << abNum+1 <<"\n#\n"
657  "# Generated on : " << date << "#\n"
658  "# +---> " << xLabel << " " << roFlagMarker[0] << ": 000 (suppress)\n"
659  "# | " << roFlagMarker[1] << ": 010 (SR Threshold 2)\n"
660  "# | " << roFlagMarker[2] << ": 001 (SR Threshold 1)\n"
661  "# V " << yLabel << " " << roFlagMarker[3] << ": 011 (Full readout)\n"
662  "#\n"
663  "# action table (when forced):\n"
664  "# LI-> " << roFlagMarker[actions[0]] << " (" << roFlagMarker[actions[4]]<< ")" << "\n"
665  "# S -> " << roFlagMarker[actions[1]] << " (" << roFlagMarker[actions[5]]<< ")" << "\n"
666  "# N -> " << roFlagMarker[actions[2]] << " (" << roFlagMarker[actions[6]]<< ")" << "\n"
667  "# C -> " << roFlagMarker[actions[3]] << " (" << roFlagMarker[actions[7]]<< ")" << "\n";
668 }
669 
670 void writeABIOFileHeader(ofstream& f, int abNum){
671  time_t t;
672  time(&t);
673  const char* date = ctime(&t);
674  f << "# AB " << abNum+1 <<" I/O \n#\n"
675  "# Generated on : " << date << "#\n"
676  "# " << srpFlagMarker[0] << ": 000 (low interest) " << tccFlagMarker[0] << ": 000 (low interest) " << roFlagMarker[0] << ": 000 (suppress)\n"
677  "# " << srpFlagMarker[1] << ": 001 (single) " << tccFlagMarker[1] << ": 001 (mid interest) " << roFlagMarker[1] << ": 010 (SR Threshold 2)\n"
678  "# " << srpFlagMarker[2] << ": 010 (neighbour) " << tccFlagMarker[2] << ": 010 (not valid) " << roFlagMarker[2] << ": 001 (SR Threshold 1)\n"
679  "# " << srpFlagMarker[3] << ": 011 (center) " << tccFlagMarker[3] << ": 011 (high interest) " << roFlagMarker[3] << ": 011 (Full readout)\n"
680  "#\n"
681  "# action table (when forced):\n"
682  "# LI-> " << roFlagMarker[actions[0]] << " (" << roFlagMarker[actions[4]]<< ")" << "\n"
683  "# S -> " << roFlagMarker[actions[1]] << " (" << roFlagMarker[actions[5]]<< ")" << "\n"
684  "# N -> " << roFlagMarker[actions[2]] << " (" << roFlagMarker[actions[6]]<< ")" << "\n"
685  "# C -> " << roFlagMarker[actions[3]] << " (" << roFlagMarker[actions[7]]<< ")" << "\n"
686  "#\n";
687 }
688 
689 string getFlagStream(const char flags[nTTInEta][nTTInPhi], int iEtaMin,
690  int iEtaMax, int iPhiMin, int iPhiMax){
691  assert(0<=iEtaMin && iEtaMin<=iEtaMax && iEtaMax<nTTInEta);
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!"
696  << endl;
697  exit(EXIT_FAILURE);
698  }
699 
700  bool zPos = (iEtaMin>=nTTInEta/2);
701 
702  stringstream buffer;
703  buffer.str("");
704  for(int jEta = 0; jEta <= iEtaMax-iEtaMin; ++jEta){
705  //loops on iEta in |eta| increasing order:
706  int iEta;
707  if(zPos){
708  iEta = iEtaMin + jEta;
709  } else{
710  iEta = iEtaMax - jEta;
711  }
712 
713  for(int iPhi = mod(iPhiMin,nTTInPhi);
714  mod(iPhiMax+1-iPhi,nTTInPhi) != 0;
715  iPhi = mod(++iPhi, nTTInPhi)){
716  buffer << flags[iEta][iPhi];
717  }
718  }
719 
720  return buffer.str();
721 }
722 
723 string getABTCCInputStream(const char tccFlags[nTTInEta][nTTInPhi],
724  int iABEta, int iABPhi, int iTCCCh){
725  //gets eta bounds for this tcc channel:
726  int iTCCEta;
727  if(iABEta==1 || iABEta==2){//barrel
728  if(iTCCCh>5) return ""; //only 6 TCCs per AB for barrel
729  iTCCEta = 1 + iABEta;
730  } else{//endcap
731  if(iABEta==0){//EE-
732  iTCCEta = (iTCCCh<6)?1:0;
733  } else{//EE+
734  iTCCEta = (iTCCCh<6)?4:5;
735  }
736  }
737  int iEtaMin = iTCCEtaBounds[iTCCEta];
738  int iEtaMax = iTCCEtaBounds[iTCCEta+1]-1;
739 
740  //gets phi bounds:
741  int iPhiMin;
742  int iPhiMax;
743  getABTTPhiBounds(iABPhi, iPhiMin, iPhiMax);
744  //phi is increasing with TTC channel number
745  //a TTC covers a 4TT-wide phi-sector
746  //=>iPhiMin(iTTCCh) = iPhiMin(AB) + 4*iTTCCh for iTCCCh<6
747  iPhiMin += 4*(iTCCCh%6);
748  iPhiMax = iPhiMin + 4 - 1;
749 
750  return getFlagStream(tccFlags, iEtaMin, iEtaMax, iPhiMin, iPhiMax);
751 }
752 
753 string getABABOutputStream(const char tccFlags[nTTInEta][nTTInPhi],
754  int iABEta, int iABPhi, int iABCh){
755  stringstream buffer;
756  buffer.str("");
757  bool barrel = (iABEta==1 || iABEta==2); //true for barrel, false for endcap
758  switch(iABCh){
759  case 0:
760  //to AB ch #0 are sent the 16 1st TCC flags received on TCC input Ch. 0
761  buffer << getABTCCInputStream(tccFlags, iABEta, iABPhi, 0).substr(0,16);
762  break;
763  case 1:
764  //to AB ch #1 are sent the 16 1st TCC flags received on TCC input Ch. 0 to 5:
765  for(int iTCCCh=0; iTCCCh<6; ++iTCCCh){
766  buffer <<
767  getABTCCInputStream(tccFlags, iABEta, iABPhi, iTCCCh).substr(0,16);
768  }
769  break;
770  case 2:
771  //to AB ch #2 are sent the 16 1st TCC flags received on TCC input Ch. 5:
772  buffer << getABTCCInputStream(tccFlags, iABEta, iABPhi, 5).substr(0,16);
773  break;
774  case 3:
775  //to AB ch #3 are sent TCC flags received on TCC input Ch. 0 and 6:
776  buffer << getABTCCInputStream(tccFlags, iABEta, iABPhi, 0);
777  buffer << getABTCCInputStream(tccFlags, iABEta, iABPhi, 6);
778  break;
779  case 4:
780  //to AB ch #4 are sent TCC flags received on TCC input Ch 5 and 11:
781  buffer << getABTCCInputStream(tccFlags, iABEta, iABPhi, 5);
782  buffer << getABTCCInputStream(tccFlags, iABEta, iABPhi, 11);
783  break;
784  case 5:
785  //for endcaps AB output ch 5 is not used.
786  //for barrel, to AB ch #5 are sent the 16 last TCC flags received on TCC
787  //input Ch. 0:
788  if(barrel){//in barrel
789  string s = getABTCCInputStream(tccFlags, iABEta, iABPhi, 0);
790  assert(s.size()>=16);
791  buffer << s.substr(s.size()-16,16);
792  }
793  break;
794  case 6:
795  //for endcaps AB output ch 6 is not used.
796  //for barrel, to AB ch #6 are sent the 16 last TCC flags received on TCC
797  //input Ch. 0 to 5:
798  if(barrel){//in barrel
799  for(int iTCCCh=0; iTCCCh<6; ++iTCCCh){
800  string s = getABTCCInputStream(tccFlags, iABEta, iABPhi, iTCCCh);
801  buffer << s.substr(s.size()-16,16);
802  }
803  }
804  break;
805  case 7:
806  //for endcaps AB output ch 7 is not used.
807  //for barrel, to AB ch #7 are sent the 16 last TCC flags received on TCC
808  //input Ch. 5:
809  if(barrel){//in barrel
810  string s = getABTCCInputStream(tccFlags, iABEta, iABPhi, 5);
811  assert(s.size()>=16);
812  buffer << s.substr(s.size()-16,16);
813  }
814  break;
815  default:
816  assert(false);
817  }
818  return buffer.str();
819 }
820 
821 string getABABInputStream(const char tccFlags[nTTInEta][nTTInPhi], int iABEta,
822  int iABPhi, int iABCh){
823  int iAB = abNum(iABEta, iABPhi);
824  int iOtherAB; //AB which this channel is connected to
825  int iOtherABCh; //ch # on the other side of the AB-AB link
826  abConnect(iAB,iABCh,iOtherAB,iOtherABCh);
827  int iOtherABEta = iOtherAB/3;
828  int iOtherABPhi = iOtherAB%3;
829  return getABABOutputStream(tccFlags, iOtherABEta, iOtherABPhi, iOtherABCh);
830 }
831 
832 
833 void getABTTPhiBounds(int iABPhi, int& iTTPhiMin, int& iTTPhiMax){
834  iTTPhiMin = mod(-6+iABPhi*nTTInABAlongPhi,nTTInPhi);
835  iTTPhiMax = mod(iTTPhiMin+nTTInABAlongPhi-1, nTTInPhi);
836 }
837 
838 void abConnect(int iAB,int iABCh,int& iOtherAB,int& iOtherABCh){
839  static bool firstCall = true;
840  static int xconnectMap[nAB][nABABCh][2];
841  if(firstCall){
842  FILE* f = fopen(xconnectFilename, "r");
843  if(f==NULL){
844  cerr << "Error. Failed to open xconnect definition file,"
845  << xconnectFilename << endl;
846  exit(EXIT_FAILURE);
847  }
848  //skips two first lines:
849  for(int i=0; i<2; ++i){
850  int c;
851  while((c=getc(f))!='\n' && c >=0);
852  }
853  int ilink = 0 ;
854  while(!feof(f)){
855  int abIn;
856  int pinIn;
857  int abOut;
858  int pinOut;
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;
862  ++ilink;
863  }
864  }
865  if(ilink!=nAB*nABABCh){
866  cerr << "Error cross-connect definition file " << xconnectFilename
867  << " contains an unexpected number of link definition."
868  << endl;
869  exit(EXIT_FAILURE);
870  }
871  firstCall = false;
872  }
873 
874  iOtherAB = xconnectMap[iAB][iABCh][0];
875  iOtherABCh = xconnectMap[iAB][iABCh][1];
876 }
877 
878 string getABDCCOutputStream(const char barrelSrFlags[nBarrelTTInEta][nTTInPhi],
879  const char endcapSrFlags[nEndcaps][nSupercrystalXBins]
881  int iABEta, int iABPhi, int iDCCCh){
882  bool barrel = (iABEta==1||iABEta==2);
883  if(barrel){
884  //same as TCC with same ch number but with TCC flags replaced by SRP flags:
885  string stream = getABTCCInputStream(barrelSrFlags-nEndcapTTInEta, iABEta, iABPhi, iDCCCh);
886  //converts srp flags to readout flags:
887  for(size_t i=0; i< stream.size(); ++i){
888  stream[i] = srp2roFlags[(int)stream[i]];
889  }
890  return stream;
891  } else{//endcap
892  if(iDCCCh<3){//used DCC output channel
893  //endcap index:
894  int iEE=(iABEta==0)?0:1;
895  stringstream buffer("");
896  //3 DCC per AB and AB DCC output channel in
897  //increasing DCC phi position:
898  int iDCCPhi = iABPhi*3+iDCCCh;
899  for(size_t iSC=0; iSC < ecalDccSC[iEE][iDCCPhi].size(); ++iSC){
900  pair<int,int> sc = ecalDccSC[iEE][iDCCPhi][iSC];
901  buffer << srp2roFlags[(int)endcapSrFlags[iEE][sc.first][sc.second]];
902  }
903  return buffer.str();
904  } else{//unused output channel
905  return "";
906  }
907  }
908 }
const int iTTEtaMin[nABInEta]
Definition: GenABIO.cc:155
bool readTTF(FILE *file, char ttFlags[nTTInEta][nTTInPhi])
Definition: GenABIO.cc:506
int i
Definition: DBlmapReader.cc:9
Definition: GenABIO.cc:180
Definition: GenABIO.cc:180
static const int nEndcapYBins
Definition: GenABIO.cc:114
void writeABSRFFileHeader(ofstream &f, int abNum)
Definition: GenABIO.cc:643
roAction_t actions[nactions]
Definition: GenABIO.cc:187
string getABABInputStream(const char tccFlags[nTTInEta][nTTInPhi], int iABEta, int iABPhi, int iABCh)
Definition: GenABIO.cc:821
static const int nSupercrystalXBins
Definition: GenABIO.cc:120
const int nDCCEE
Definition: GenABIO.cc:148
static const int nEndcapXBins
Definition: GenABIO.cc:111
assert(m_qm.get())
const char * abTTFFilePrefix
Definition: GenABIO.cc:164
std::vector< Variable::Flags > flags
Definition: MVATrainer.cc:135
#define NULL
Definition: scimark2.h:8
const char tccFlagMarker[]
Definition: GenABIO.cc:176
const char * abSRFFilePrefix
Definition: GenABIO.cc:166
const char * abIOFilePostfix
Definition: GenABIO.cc:169
char srp2roFlags[128]
Definition: GenABIO.cc:178
const int nABInEta
Definition: GenABIO.cc:142
tuple result
Definition: mps_fire.py:83
void getABTTPhiBounds(int iABPhi, int &iTTPhiMin, int &iTTPhiMax)
Definition: GenABIO.cc:833
int iEvent
Definition: GenABIO.cc:230
const int nTCCInEta
Definition: GenABIO.cc:152
Definition: GenABIO.cc:180
const int iTTEtaMax[nABInEta]
Definition: GenABIO.cc:156
void writeABIOFileHeader(ofstream &f, int abNum)
Definition: GenABIO.cc:670
void fillABTTFFiles(const char ttFlags[nTTInEta][nTTInPhi], ofstream files[])
Definition: GenABIO.cc:357
int theAB
Definition: GenABIO.cc:232
Definition: GenABIO.cc:180
int abOfDcc(int iDCC)
Definition: ecalDccMap.h:104
int dccIndexOfRU(int iDet, int i, int j)
Definition: ecalDccMap.h:88
Definition: GenABIO.cc:180
roAction_t
Definition: GenABIO.cc:180
double f[11][100]
static const int nTTInEta
Definition: GenABIO.cc:135
static const int nSupercrystalYBins
Definition: GenABIO.cc:123
const char * abIOFilePrefix
Definition: GenABIO.cc:168
#define end
Definition: vmac.h:37
const int iTTEtaSign[nABInEta]
Definition: GenABIO.cc:157
const int nABTCCCh
Definition: GenABIO.cc:150
char roFlagMarker[]
Definition: GenABIO.cc:181
string getABABOutputStream(const char tccFlags[nTTInEta][nTTInPhi], int iABEta, int iABPhi, int iABCh)
Definition: GenABIO.cc:753
const int nDCCCh
Definition: GenABIO.cc:151
string getFlagStream(char flags[nTTInEta][nTTInPhi], int iEtaMin, int iEtaMax, int iPhiMin, int iPhiMax)
Definition: GenABIO.cc:689
static const int nBarrelTTInEta
Definition: GenABIO.cc:132
void fillABIOFiles(const char ttFlags[nTTInEta][nTTInPhi], const char barrelSrFlags[nBarrelTTInEta][nTTInPhi], const char endcapSrFlags[nEndcaps][nSupercrystalXBins][nSupercrystalYBins], ofstream files[])
Definition: GenABIO.cc:459
string getABTCCInputStream(const char tccFlags[nTTInEta][nTTInPhi], int iABEta, int iABPhi, int iTCCCh)
Definition: GenABIO.cc:723
static const int nEndcaps
Definition: GenABIO.cc:126
const int nactions
Definition: GenABIO.cc:185
const int nTTInABAlongPhi
Definition: GenABIO.cc:154
#define SIZE_MAX
Definition: GenABIO.cc:25
const char * xconnectFilename
Definition: GenABIO.cc:173
tuple argc
Definition: dir2webdir.py:38
static const int supercrystalEdge
Definition: GenABIO.cc:117
static const int nTTInPhi
Definition: GenABIO.cc:139
const char * abTTFFilePostfix
Definition: GenABIO.cc:165
void writeABTTFFileHeader(ofstream &f, int abNum)
Definition: GenABIO.cc:630
#define SSIZE_MAX
Definition: GenABIO.cc:28
const int nABInPhi
Definition: GenABIO.cc:145
const char * abSRFFilePostfix
Definition: GenABIO.cc:167
void abConnect(int iAB, int iABCh, int &iOtherAB, int &iOtherABCh)
Definition: GenABIO.cc:838
tuple cout
Definition: gather_cfg.py:145
static const int nEndcapTTInEta
Definition: GenABIO.cc:129
const char * srfFilename
Definition: GenABIO.cc:171
vector< pair< int, int > > ecalDccSC[nEndcaps][nDCCEE]
Definition: GenABIO.cc:191
const int iTCCEtaBounds[nTCCInEta+1]
Definition: GenABIO.cc:162
const char * ttfFilename
Definition: GenABIO.cc:172
const int nAB
Definition: GenABIO.cc:153
int abNum(int iABEta, int iABPhi)
Definition: GenABIO.cc:204
T mod(const T &a, const T &b)
Definition: ecalDccMap.h:4
void fillABSRPFiles(const char barrelSrFlags[nBarrelTTInEta][nTTInPhi], const char endcapSrFlags[nEndcaps][nSupercrystalXBins][nSupercrystalYBins], ofstream files[])
const int nABABCh
Definition: GenABIO.cc:149
int dccPhiIndexOfRU(int iDet, int i, int j)
Definition: ecalDccMap.h:38
bool readSRF(FILE *file, char barrelSrFlags[nBarrelTTInEta][nTTInPhi], char endcapSrFlags[nEndcaps][nSupercrystalXBins][nSupercrystalYBins])
Definition: GenABIO.cc:540
const char srpFlagMarker[]
Definition: GenABIO.cc:175
string getABDCCOutputStream(const char barrelSrFlags[nBarrelTTInEta][nTTInPhi], const char endcapSrFlags[nEndcaps][nSupercrystalXBins][nSupercrystalYBins], int iABEta, int iABPhi, int DCCCh)
Definition: GenABIO.cc:878