Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:10:36

0001 /*  
0002  *
0003  *  \author G. Franzoni
0004  *
0005  */
0006 
0007 #include "CamacTBDataFormatter.h"
0008 
0009 // pro-memo:
0010 // "ff" = 1 Byte
0011 // 64 bits = 8 Bytes = 16 hex carachters
0012 // for now: event is  ( 114 words x 32 bits ) = 448 Bytes
0013 // size of unsigned long = 4 Bytes. Thus, 1 (32 bit) word = 1 (unsigned long)
0014 
0015 struct hodo_fibre_index {
0016   int nfiber;
0017   int ndet;
0018 };
0019 
0020 // nHodoscopes = 2; nFibres = 64
0021 const static struct hodo_fibre_index hodoFiberMap[2][64] = {{// Hodo 0
0022                                                              // unit 1A
0023                                                              {23, 44},
0024                                                              {29, 47},
0025                                                              {31, 48},
0026                                                              {21, 43},
0027                                                              {5, 35},
0028                                                              {15, 40},
0029                                                              {7, 36},
0030                                                              {13, 39},
0031                                                              {1, 33},
0032                                                              {11, 38},
0033                                                              {3, 34},
0034                                                              {9, 37},
0035                                                              {6, 3},
0036                                                              {16, 8},
0037                                                              {8, 4},
0038                                                              {14, 7},
0039                                                              // unit 1C
0040                                                              {17, 41},
0041                                                              {19, 42},
0042                                                              {27, 46},
0043                                                              {25, 45},
0044                                                              {32, 16},
0045                                                              {22, 11},
0046                                                              {24, 12},
0047                                                              {30, 15},
0048                                                              {12, 6},
0049                                                              {2, 1},
0050                                                              {4, 2},
0051                                                              {10, 5},
0052                                                              {28, 14},
0053                                                              {18, 9},
0054                                                              {20, 10},
0055                                                              {26, 13},
0056                                                              // unit 2A
0057                                                              {54, 27},
0058                                                              {56, 28},
0059                                                              {64, 32},
0060                                                              {62, 31},
0061                                                              {49, 57},
0062                                                              {59, 62},
0063                                                              {51, 58},
0064                                                              {57, 61},
0065                                                              {53, 59},
0066                                                              {63, 64},
0067                                                              {55, 60},
0068                                                              {61, 63},
0069                                                              {45, 55},
0070                                                              {39, 52},
0071                                                              {37, 51},
0072                                                              {47, 56},
0073                                                              // unit 2C
0074                                                              {34, 17},
0075                                                              {42, 21},
0076                                                              {44, 22},
0077                                                              {36, 18},
0078                                                              {50, 25},
0079                                                              {60, 30},
0080                                                              {58, 29},
0081                                                              {52, 26},
0082                                                              {38, 19},
0083                                                              {40, 20},
0084                                                              {48, 24},
0085                                                              {46, 23},
0086                                                              {41, 53},
0087                                                              {35, 50},
0088                                                              {33, 49},
0089                                                              {43, 54}},
0090                                                             {// Hodo 1
0091                                                              // unit 1A
0092                                                              {31, 48},
0093                                                              {29, 47},
0094                                                              {23, 44},
0095                                                              {21, 43},
0096                                                              {5, 35},
0097                                                              {7, 36},
0098                                                              {15, 40},
0099                                                              {13, 39},
0100                                                              {1, 33},
0101                                                              {3, 34},
0102                                                              {11, 38},
0103                                                              {9, 37},
0104                                                              {6, 3},
0105                                                              {8, 4},
0106                                                              {16, 8},
0107                                                              {14, 7},
0108                                                              // unit 1C
0109                                                              {17, 41},
0110                                                              {27, 46},
0111                                                              {19, 42},
0112                                                              {25, 45},
0113                                                              {24, 12},
0114                                                              {22, 11},
0115                                                              {32, 16},
0116                                                              {30, 15},
0117                                                              {4, 2},
0118                                                              {2, 1},
0119                                                              {12, 6},
0120                                                              {10, 5},
0121                                                              {20, 10},
0122                                                              {18, 9},
0123                                                              {28, 14},
0124                                                              {26, 13},
0125                                                              // unit 2A
0126                                                              {54, 27},
0127                                                              {64, 32},
0128                                                              {56, 28},
0129                                                              {62, 31},
0130                                                              {49, 57},
0131                                                              {51, 58},
0132                                                              {59, 62},
0133                                                              {57, 61},
0134                                                              {53, 59},
0135                                                              {55, 60},
0136                                                              {63, 64},
0137                                                              {61, 63},
0138                                                              {45, 55},
0139                                                              {47, 56},
0140                                                              {37, 51},
0141                                                              {39, 52},
0142                                                              // unit 2C
0143                                                              {34, 17},
0144                                                              {42, 21},
0145                                                              {36, 18},
0146                                                              {44, 22},
0147                                                              {50, 25},
0148                                                              {52, 26},
0149                                                              {58, 29},
0150                                                              {60, 30},
0151                                                              {38, 19},
0152                                                              {48, 24},
0153                                                              {40, 20},
0154                                                              {46, 23},
0155                                                              {41, 53},
0156                                                              {43, 54},
0157                                                              {33, 49},
0158                                                              {35, 50}}};
0159 
0160 CamacTBDataFormatter::CamacTBDataFormatter() { nWordsPerEvent = 148; }
0161 
0162 void CamacTBDataFormatter::interpretRawData(const FEDRawData& fedData,
0163                                             EcalTBEventHeader& tbEventHeader,
0164                                             EcalTBHodoscopeRawInfo& hodoRaw,
0165                                             EcalTBTDCRawInfo& tdcRawInfo) {
0166   const unsigned long* buffer = (reinterpret_cast<const unsigned long*>(fedData.data()));
0167   int fedLenght = fedData.size();  // in Bytes
0168 
0169   // check ultimate fed size and strip off fed-header and -trailer
0170   if (fedLenght != (nWordsPerEvent * 4)) {
0171     edm::LogError("CamacTBDataFormatter") << "CamacTBData has size " << fedLenght << " Bytes as opposed to expected "
0172                                           << (nWordsPerEvent * 4) << ". Returning.";
0173     return;
0174   }
0175 
0176   unsigned long a = 1;  // used to extract an 8 Bytes word from fed
0177   unsigned long b = 1;  // used to manipulate the 8 Bytes word and get what needed
0178 
0179   // initializing array of statuses
0180   for (int wordNumber = 0; wordNumber < nWordsPerEvent; wordNumber++) {
0181     statusWords[wordNumber] = true;
0182   }
0183 
0184   //  for (int wordNumber=0; wordNumber<nWordsPerEvent; wordNumber++)
0185   //    { checkStatus( buffer[wordNumber],  wordNumber);}
0186 
0187   //   for (int wordNumber=0; wordNumber<nWordsPerEvent; wordNumber++)
0188   //     {
0189   //       if (! statusWords[wordNumber])
0190   //    {
0191   //      edm::LogError("CamacTBDataFormatter") << "bad status in some of the event words; returning;";
0192   //    }
0193   //     }
0194 
0195   int wordCounter = 0;
0196   wordCounter += 4;
0197 
0198   // read first word
0199   a = buffer[wordCounter];
0200   wordCounter++;
0201   LogDebug("CamacTBDataFormatter") << "\n\nword:\t" << a;
0202 
0203   b = (a & 0xff000000);
0204   b = b >> 24;
0205   LogDebug("CamacTBDataFormatter") << "format  ver:\t" << b;
0206 
0207   b = (a & 0xff0000);
0208   b = b >> 16;
0209   LogDebug("CamacTBDataFormatter") << "major:\t" << b;
0210 
0211   b = (a & 0xff00);
0212   b = b >> 8;
0213   LogDebug("CamacTBDataFormatter") << "minor:\t" << b;
0214 
0215   LogDebug("CamacTBDataFormatter") << "\n\n word:\t" << a;
0216   LogDebug("CamacTBDataFormatter") << "time stamp secs: " << a;
0217 
0218   LogDebug("CamacTBDataFormatter") << "\n\n word:\t" << a;
0219   LogDebug("CamacTBDataFormatter") << "time stamp musecs: " << a;
0220 
0221   a = buffer[wordCounter];
0222   wordCounter++;
0223   LogDebug("CamacTBDataFormatter") << "\n\n word:\t" << a;
0224   b = (a & 0xffffff);
0225   LogDebug("CamacTBDataFormatter") << "LV1A: " << b;
0226   int lv1 = b;
0227 
0228   a = buffer[wordCounter];
0229   wordCounter++;
0230   LogDebug("CamacTBDataFormatter") << "\n\n word:\t" << a;
0231   b = (a & 0xffff0000);
0232   b = b >> 16;
0233   LogDebug("CamacTBDataFormatter") << "run number: " << b;
0234   int run = b;
0235   b = (a & 0xffff);
0236   LogDebug("CamacTBDataFormatter") << "spill number: " << b;
0237   int spill = b;
0238 
0239   a = buffer[wordCounter];
0240   wordCounter++;
0241   b = (a & 0xffff);
0242   LogDebug("CamacTBDataFormatter") << "event number in spill: " << b;
0243 
0244   a = buffer[wordCounter];
0245   wordCounter++;
0246   b = (a & 0xffffff);
0247   LogDebug("CamacTBDataFormatter") << "internal event number: " << b;
0248 
0249   a = buffer[wordCounter];
0250   wordCounter++;
0251   LogDebug("CamacTBDataFormatter") << "\n\n word:\t" << a;
0252   b = (a & 0xffff0000);
0253   b = b >> 16;
0254   LogDebug("CamacTBDataFormatter") << "vme errors: " << b;
0255   b = (a & 0xffff);
0256   LogDebug("CamacTBDataFormatter") << "camac errors: " << b;
0257 
0258   a = buffer[wordCounter];
0259   wordCounter++;
0260   LogDebug("CamacTBDataFormatter") << "\n\n word:\t" << a;
0261   b = a;
0262   LogDebug("CamacTBDataFormatter") << "extended (32 bits) run number: " << b;
0263 
0264   // skip 1 reserved words
0265   wordCounter += 1;
0266 
0267   /**********************************
0268   // acessing the hodoscope block
0269   **********************************/
0270 
0271   // getting 16 words buffer and checking words statuses
0272   unsigned long bufferHodo[16];
0273   bool hodoAreGood = true;
0274   for (int hodo = 0; hodo < 16; hodo++) {
0275     hodoAreGood = hodoAreGood && checkStatus(buffer[wordCounter], wordCounter);
0276 
0277     a = buffer[wordCounter];
0278     bufferHodo[hodo] = buffer[wordCounter];
0279     wordCounter++;
0280     b = (a & 0xffffff);
0281     LogDebug("CamacTBDataFormatter") << "hodo: " << hodo << "\t: " << b;
0282   }
0283 
0284   hodoRaw.setPlanes(0);
0285   // unpacking the hodo data
0286   if (hodoAreGood) {
0287     for (int iplane = 0; iplane < nHodoPlanes; iplane++) {
0288       int detType = 1;  // new mapping for electronics channels
0289 
0290       for (int fiber = 0; fiber < nHodoFibers; fiber++) {
0291         hodoHits[iplane][fiber] = 0;
0292       }
0293 
0294       int ch = 0;
0295 
0296       // loop on [4-24bits words] = 1 plane
0297       for (int j = 0; j < hodoRawLen; j++) {
0298         int word = bufferHodo[j + iplane * hodoRawLen] & 0xffff;
0299         for (int i = 1; i < 0x10000; i <<= 1) {
0300           if (word & i) {
0301             // map electronics channel to No of fibre
0302             hodoHits[iplane][hodoFiberMap[detType][ch].nfiber - 1]++;
0303           }
0304           ch++;
0305         }
0306       }
0307     }
0308 
0309     // building the hodo infos (returning decoded hodoscope hits information)
0310     hodoRaw.setPlanes((unsigned int)nHodoPlanes);
0311     for (int ipl = 0; ipl < nHodoPlanes; ipl++) {
0312       EcalTBHodoscopePlaneRawHits theHodoPlane;
0313       theHodoPlane.setChannels((unsigned int)nHodoFibers);
0314       for (int fib = 0; fib < nHodoFibers; fib++) {
0315         theHodoPlane.setHit((unsigned int)fib, (bool)hodoHits[ipl][fib]);
0316       }
0317       hodoRaw.setPlane((unsigned int)ipl, theHodoPlane);
0318     }
0319   } else {
0320     edm::LogWarning("CamacTBDataFormatter")
0321         << "hodoscope block has hardware problems or is partly unused at LV1: " << lv1 << " spill: " << spill
0322         << "run: " << run << ". Skipping digi.";
0323   }
0324 
0325   /**********************************
0326   // acessing the scalers block
0327   **********************************/
0328 
0329   // getting 72 words buffer and checking words statuses
0330 
0331   scalers_.clear();
0332   scalers_.reserve(36);
0333 
0334   bool scalersAreGood = true;
0335   for (int scaler = 0; scaler < 72; scaler++) {
0336     scalersAreGood = scalersAreGood && checkStatus(buffer[wordCounter], wordCounter);
0337 
0338     a = buffer[wordCounter];
0339     wordCounter++;
0340     b = (a & 0xffffff);
0341     LogDebug("CamacTBDataFormatter") << "scaler: " << scaler << "\t: " << b;
0342 
0343     // filling vector container with scalers words
0344     if ((scaler % 2) == 0)
0345       scalers_.push_back(b);
0346   }
0347   if (scalersAreGood) {
0348     tbEventHeader.setScalers(scalers_);
0349   } else {
0350     edm::LogWarning("CamacTBDataFormatter")
0351         << "scalers block has hardware problems  or is partly unused at LV1: " << lv1 << " spill: " << spill
0352         << "run: " << run;
0353   }
0354 
0355   /**********************************
0356   // acessing the fingers block
0357   **********************************/
0358 
0359   LogDebug("CamacTBDataFormatter") << "\n";
0360   bool fingersAreGood = true;
0361   for (int finger = 0; finger < 2; finger++) {
0362     fingersAreGood = fingersAreGood && checkStatus(buffer[wordCounter], wordCounter);
0363 
0364     a = buffer[wordCounter];
0365     wordCounter++;
0366     b = (a & 0xffffff);
0367     LogDebug("CamacTBDataFormatter") << "finger: " << finger << "\t: " << b;
0368   }
0369   if (fingersAreGood) {
0370     ;
0371   } else {
0372     edm::LogWarning("CamacTBDataFormatter")
0373         << "fingers block has hardware problems  or is partly unused at LV1: " << lv1 << " spill: " << spill
0374         << "run: " << run;
0375   }
0376 
0377   /**********************************
0378   // acessing the multi stop TDC block
0379   **********************************/
0380 
0381   a = buffer[wordCounter];
0382   wordCounter++;
0383   LogDebug("CamacTBDataFormatter") << "\n\n word:\t" << a;
0384   b = (a & 0x000000ff);
0385   LogDebug("CamacTBDataFormatter") << "number of words used in multi stop TDC words: " << b;
0386 
0387   int numberTDCwords = 16;
0388   bool multiStopTDCIsGood = true;
0389   for (int tdc = 0; tdc < numberTDCwords; tdc++) {
0390     multiStopTDCIsGood = multiStopTDCIsGood && checkStatus(buffer[wordCounter], wordCounter);
0391 
0392     a = buffer[wordCounter];
0393     wordCounter++;
0394     b = a;
0395     LogDebug("CamacTBDataFormatter") << "tdc: " << tdc << "\t: " << b;
0396   }
0397   if (multiStopTDCIsGood) {
0398     ;
0399   } else {
0400     edm::LogWarning("CamacTBDataFormatter")
0401         << "multi stop TDC block has hardware problems or is partly unused at LV1: " << lv1 << " spill: " << spill
0402         << "run: " << run;
0403   }
0404 
0405   // skip the unused words in multi stop TDC block
0406   wordCounter += (16 - numberTDCwords);
0407 
0408   /**********************************
0409   // acessing table in position bit
0410   **********************************/
0411   a = buffer[wordCounter];
0412   wordCounter++;
0413   b = (a & 0x00000001);  //1= table is in position; 0=table is moving
0414   bool tableIsMoving;
0415   if (b) {
0416     LogDebug("CamacTBDataFormatter") << " table is in position.";
0417     tableIsMoving = false;
0418   } else {
0419     LogDebug("CamacTBDataFormatter") << " table is moving.";
0420     tableIsMoving = true;
0421   }
0422   tbEventHeader.setTableIsMoving(tableIsMoving);
0423 
0424   wordCounter += 3;
0425 
0426   /**********************************
0427    // acessing ADC block
0428    **********************************/
0429   // skip 10 reserved words
0430   wordCounter += 10;
0431   bool ADCIsGood = true;
0432   a = buffer[wordCounter];
0433   wordCounter++;  // NOT read out
0434   b = (a & 0x00ffffff);
0435   LogDebug("CamacTBDataFormatter") << "ADC word1: " << a << "\t ADC2: " << b << " word is: " << (wordCounter - 1);
0436   //  ADCIsGood = true;
0437   //  ADCIsGood = ADCIsGood && checkStatus(buffer[wordCounter], wordCounter);
0438   ADCIsGood = checkStatus(buffer[wordCounter], wordCounter);
0439   a = buffer[wordCounter];
0440   wordCounter++;  // read out
0441   b = (a & 0xffffff);
0442   LogDebug("CamacTBDataFormatter") << "ADC word2, adc channel 11, ampli S6: " << a << "\t ADC2: " << b;
0443   if (ADCIsGood)
0444     tbEventHeader.setS6ADC(b);
0445   else
0446     tbEventHeader.setS6ADC(-1);
0447 
0448   /**********************************
0449    // acessing TDC block
0450    **********************************/
0451   // skip 6 reserved words
0452   wordCounter += 6;
0453   ADCIsGood&& checkStatus(buffer[wordCounter], wordCounter);
0454   a = buffer[wordCounter];
0455   wordCounter++;
0456   b = (a & 0xfffff);
0457   LogDebug("CamacTBDataFormatter") << "TDC word1: " << a << "\t TDC2: " << b;
0458   ADCIsGood&& checkStatus(buffer[wordCounter], wordCounter);
0459   a = buffer[wordCounter];
0460   wordCounter++;
0461   b = (a & 0xfffff);
0462   LogDebug("CamacTBDataFormatter") << "TDC word2: (ext_val_trig - LHC_clock) " << a
0463                                    << "\t (ext_val_trig - LHC_clock): " << b;
0464 
0465   tdcRawInfo.setSize(1);
0466   int sampleNumber = 1;
0467   EcalTBTDCSample theTdc(sampleNumber, b);
0468   tdcRawInfo.setSample(0, theTdc);
0469 
0470   a = buffer[wordCounter];
0471   wordCounter++;
0472   LogDebug("CamacTBDataFormatter") << "\n\n word:\t" << a;
0473   b = a;
0474   LogDebug("CamacTBDataFormatter") << "last word of event: " << b;
0475 }
0476 
0477 // given a data word with 8 msb as status, checks status
0478 
0479 bool CamacTBDataFormatter::checkStatus(unsigned long word, int wordNumber) {
0480   if (wordNumber < 1 || wordNumber > nWordsPerEvent) {
0481     edm::LogWarning("CamacTBDataFormatter::checkStatus")
0482         << "checking word number: " << wordNumber << " which is out of allowed range (" << nWordsPerEvent << ")";
0483     return false;
0484   }
0485 
0486   bool isOk = true;
0487 
0488   if (word & 0x80000000)  // daq item not used
0489   {
0490     edm::LogWarning("CamacTBDataFormatter::checkStatus") << "daq item not used at word: " << wordNumber;
0491     statusWords[wordNumber - 1] = false;
0492     isOk = false;
0493   }
0494 
0495   if (word & 0x40000000)  // vme error on data
0496   {
0497     edm::LogWarning("CamacTBDataFormatter::checkStatus") << "vme error on word: " << wordNumber;
0498     statusWords[wordNumber - 1] = false;
0499     isOk = false;
0500   }
0501 
0502   if (word & 0x20000000)  // vme error on status
0503   {
0504     edm::LogWarning("CamacTBDataFormatter::checkStatus") << "vme status error at word: " << wordNumber;
0505     statusWords[wordNumber - 1] = false;
0506     isOk = false;
0507   }
0508 
0509   if (word & 0x10000000)  // camac error (no X)
0510   {
0511     edm::LogWarning("CamacTBDataFormatter::checkStatus") << "camac error (no X) at word: " << wordNumber;
0512     statusWords[wordNumber - 1] = false;
0513     isOk = false;
0514   }
0515 
0516   if (word & 0x08000000)  // camac error (no Q)
0517   {
0518     edm::LogWarning("CamacTBDataFormatter::checkStatus") << "camac error (no Q) at word: " << wordNumber;
0519     statusWords[wordNumber - 1] = false;
0520     isOk = false;
0521   }
0522 
0523   // camac error check not done on purpose from Aug 8, to speed up Camac communication. This bit status is now ignored.
0524   //  if (word & 0x04000000) // no camac check error
0525   //    {
0526   //edm::LogWarning("CamacTBDataFormatter::checkStatus") << "no camac check error at word: "<<  wordNumber;
0527   //statusWords[wordNumber -1] = false;
0528   //isOk = false;
0529   //    }
0530 
0531   return isOk;
0532 }