CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_2_9_HLT1_bphpatch4/src/EventFilter/CSCRawToDigi/src/CSCDCCEventData.cc

Go to the documentation of this file.
00001 
00009 #include "EventFilter/CSCRawToDigi/interface/CSCDCCEventData.h"
00010 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00011 #include <iostream>
00012 #include <cstdio>
00013 #include "EventFilter/CSCRawToDigi/src/bitset_append.h"
00014 
00015 bool CSCDCCEventData::debug = false;
00016 
00017 CSCDCCEventData::CSCDCCEventData(int sourceId, int nDDUs, int bx, int l1a) 
00018 : theDCCHeader(bx, l1a, sourceId) 
00019 {
00020   theDDUData.reserve(nDDUs);
00021 } 
00022 
00023 CSCDCCEventData::CSCDCCEventData(unsigned short *buf, CSCDCCExaminer* examiner)
00024 {
00025   unpack_data(buf, examiner);
00026 }
00027 
00028 CSCDCCEventData::~CSCDCCEventData() 
00029 {
00030 }
00031 
00032 
00033 void CSCDCCEventData::unpack_data(unsigned short *buf, CSCDCCExaminer* examiner) 
00034 {
00035  
00036 /*
00037   for (int i=0;i<20;i++) {
00038     printf("%04x %04x %04x %04x\n",buf[i+3],buf[i+2],buf[i+1],buf[i]); 
00039     i+=3;
00040   }
00041 */
00042 
00043   theDDUData.clear();
00044   if (debug) 
00045     LogTrace ("CSCDCCEventData|CSCRawToDigi") << "CSCDCCEventData::unpack_data() is called";
00046 
00047   // decode DCC header (128 bits)
00048   if (debug) 
00049     LogTrace ("CSCDCCEventData|CSCRawToDigi") << "unpacking dcc header...";
00050   memcpy(&theDCCHeader, buf, theDCCHeader.sizeInWords()*2);
00051   //theDCCHeader = CSCDCCHeader(buf); // direct unpacking instead of bitfields
00052   buf += theDCCHeader.sizeInWords();
00053 
00054   //std::cout <<"Sandrik DCC Id = " << theDCCHeader.getCDFSourceId()  << std::endl;
00055  
00057   while ( (buf[7]==0x8000)&&(buf[6]==0x0001)&&(buf[5]==0x8000))
00058     {
00059        CSCDDUEventData dduEventData(buf, examiner);
00060 //      CSCDDUEventData dduEventData(buf);
00061 
00062       if (debug) LogTrace ("CSCDCCEventData|CSCRawToDigi") << " checking ddu data integrity ";
00063       if (dduEventData.check()) 
00064         {
00065           theDDUData.push_back(dduEventData);
00066           buf += dduEventData.sizeInWords();
00067         } 
00068       else
00069         {
00070           if (debug) LogTrace("CSCDCCEventData|CSCRawToDigi") <<"DDU Data Check failed!  ";
00071           break;
00072         }
00073       
00074     }
00075   
00076   if (debug)
00077     {
00078       LogTrace ("CSCDCCEventData|CSCRawToDigi") << "unpacking dcc trailer ";
00079       LogTrace ("CSCDCCEventData|CSCRawToDigi") << std::hex << buf[3] <<" "
00080                                        << buf[2]<<" " << buf[1]<<" " << buf[0];
00081     }
00082             
00083   //decode dcc trailer (128 bits)
00084   if (debug) LogTrace ("CSCDCCEventData|CSCRawToDigi") <<"decoding DCC trailer";
00085   memcpy(&theDCCTrailer, buf, theDCCTrailer.sizeInWords()*2);
00086   if (debug) LogTrace("CSCDCCEventData|CSCRawToDigi") << "checking DDU Trailer" << theDCCTrailer.check(); 
00087   buf += theDCCTrailer.sizeInWords();
00088 
00089   //std::cout << " DCC Size: " << std::dec << theSizeInWords << std::endl;
00090   //std::cout << "LastBuf: "  << std::hex << inputBuf[theSizeInWords-4] << std::endl;
00091 }
00092           
00093 
00094 bool CSCDCCEventData::check() const 
00095 {
00096   // the trailer counts in 64-bit words
00097   if (debug) 
00098     {
00099       LogTrace ("CSCDCCEventData|CSCRawToDigi") << "size in Words () = " << std::dec << sizeInWords();
00100     }
00101 
00102   return  theDCCHeader.check() && theDCCTrailer.check();
00103 }
00104 
00105 
00106 void CSCDCCEventData::addChamber(CSCEventData & chamber, int dduID, int dduSlot, int dduInput, int dmbID)
00107 {
00108   // first, find this DDU
00109   std::vector<CSCDDUEventData>::iterator dduItr;
00110   int dduIndex = -1;
00111   int nDDUs = theDDUData.size();
00112   for(int i = 0; dduIndex == -1 && i < nDDUs; ++i)
00113   {
00114     if(theDDUData[i].header().source_id() == dduID) dduIndex = i;
00115   }
00116   if(dduIndex == -1)
00117   {
00118     // make a new one
00119     CSCDDUHeader newDDUHeader(dccHeader().getCDFBunchCounter(), 
00120                               dccHeader().getCDFEventNumber(), dduID);
00121     theDDUData.push_back(CSCDDUEventData(newDDUHeader));
00122     dduIndex = nDDUs;
00123     dccHeader().setDAV(dduSlot);
00124   }
00125   theDDUData[dduIndex].add( chamber, dmbID, dduInput );
00126 }
00127  
00128 
00129 boost::dynamic_bitset<> CSCDCCEventData::pack() 
00130 {
00131   boost::dynamic_bitset<> result( theDCCHeader.sizeInWords()*16);
00132   result = bitset_utilities::ushortToBitset(theDCCHeader.sizeInWords()*16, theDCCHeader.data());
00133   //std::cout <<"SANDRIK DCC size of header  in words"<< theDCCHeader.sizeInWords()*16 <<std::endl;  
00134   //std::cout <<"SANDRIK DCC size of header in bits"<< result.size()<<std::endl;
00135   //for(size_t i = 0; i < result.size(); ++i) {
00136   //  std::cout<<result[i];
00137   //  if (((i+1)%32)==0) std::cout<<std::endl;
00138   //}
00139   
00140   for(size_t i = 0; i < theDDUData.size(); ++i) 
00141     {
00142       result = bitset_utilities::append(result,theDDUData[i].pack());
00143       //std::cout <<"SANDRIK here is ddu data check ";
00144       //theDDUData[i].header().check();
00145       //std::cout <<std::endl;
00146       //bitset_utilities::printWords(result);
00147     }
00148   
00149   //std::cout <<"SANDRIK packed dcc size is "<<result.size()<<std::endl;
00150   //for(size_t i = 0; i < result.size(); ++i) {
00151   //  std::cout<<result[i];
00152   //  if (((i+1)%32)==0) std::cout<<std::endl;
00153   //}
00154 
00155   boost::dynamic_bitset<> dccTrailer = bitset_utilities::ushortToBitset(theDCCTrailer.sizeInWords()*16,
00156                                                                         theDCCTrailer.data());
00157   result = bitset_utilities::append(result,dccTrailer);
00158   //  bitset_utilities::printWords(result);
00159   return result;
00160 }
00161