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