CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/IORawData/DTCommissioning/src/DTSpyReader.cc

Go to the documentation of this file.
00001 
00008 #include <IORawData/DTCommissioning/src/DTSpyReader.h>
00009 #include <IORawData/DTCommissioning/src/DTFileReaderHelpers.h>
00010 
00011 #include <DataFormats/FEDRawData/interface/FEDHeader.h>
00012 #include <DataFormats/FEDRawData/interface/FEDTrailer.h>
00013 #include <DataFormats/FEDRawData/interface/FEDNumbering.h>
00014 
00015 #include "DataFormats/Provenance/interface/EventID.h"
00016 #include <DataFormats/Provenance/interface/Timestamp.h>
00017 
00018 #include <DataFormats/FEDRawData/interface/FEDRawDataCollection.h>
00019 
00020 #include <FWCore/ParameterSet/interface/ParameterSet.h>
00021 
00022 #include <string>
00023 #include <iosfwd>
00024 #include <iostream>
00025 #include <algorithm>
00026 #include <stdio.h>
00027 
00028    
00029 using namespace std;
00030 using namespace edm;
00031 
00032 
00033 DTSpyReader::DTSpyReader(const edm::ParameterSet& pset) : 
00034   runNumber(1), eventNumber(0) {
00035 
00036   // instatiating Sandro's spy (My name is Bond, Sandro Bond)
00037   mySpy = new  DTSpy(); 
00038 
00040   string connectionParameters = pset.getUntrackedParameter<string>("connectionParameters");
00041   mySpy->Connect(connectionParameters.c_str(),10000);  
00042 
00043   cout<<endl;
00044   cout<<"DT Local DAQ online spy. Connected to IP "<<connectionParameters.c_str()
00045       <<". Waiting for the data to be flushed"<<endl;
00046   cout<<endl;
00047 
00048   debug = pset.getUntrackedParameter<bool>("debug",false);
00049   dduID = pset.getUntrackedParameter<int32_t>("dduID",770); // NOT needed
00050 }
00051 
00052 
00053 DTSpyReader::~DTSpyReader() {
00054   delete mySpy;
00055 }
00056 
00057 
00058 int DTSpyReader::fillRawData(EventID& eID, Timestamp& tstamp, FEDRawDataCollection*& data){
00059   
00060   // ask for a new buffer
00061   mySpy->getNextBuffer();
00062 
00063   // get the pointer to the beginning of the buffer
00064   const char * rawDTData = mySpy->getEventPointer();
00065   
00066   const uint32_t * rawDTData32 = reinterpret_cast<const uint32_t*>(rawDTData);
00067 
00068   // instantiate the FEDRawDataCollection
00069   data = new FEDRawDataCollection();
00070 
00071   vector<uint64_t> eventData;  uint64_t word = 0;  
00072   int wordCount = 0; int wordCountCheck = 0;
00073 
00074   bool headerTag = false; bool dataTag = true;
00075   
00076   // Advance at long-word steps until the trailer is reached. 
00077   // Skipped whatever else is in the buffer (e.g. another event..)
00078   while ( !isTrailer(word, dataTag, wordCount) ) {
00079     
00080     // dma gets 4 32-bits words and create a 64 bit one
00081     word = dmaUnpack(rawDTData32, dataTag);
00082 
00083     // look for the DDU header
00084     if (isHeader(word,dataTag))  headerTag=true;
00085 
00086     // check whether the first word is a DDU header
00087     if ( wordCountCheck > 0 && !headerTag && debug) 
00088       cout<<"[DTSpyReader]: WARNING: header still not found!!"<<endl;
00089 
00090     // from now on fill the eventData with the ROS data
00091     if (headerTag) {
00092       
00093       // swapping only the 32 bits words
00094       if (dataTag) swap(word);
00095       // WARNING also the ddu status words have been swapped!
00096       // Control the correct interpretation in DDUUnpacker
00097       
00098       eventData.push_back(word);
00099       wordCount++; 
00100     }
00101 
00102     // advancing by 4 32-bits words
00103     rawDTData32 += 4;
00104 
00105     // counting the total number of group of 128 bits (=4*32) in the buffer 
00106     wordCountCheck++; 
00107   }
00108 
00109   // Setting the Event ID
00110   runNumber = mySpy->getRunNo(); 
00111   eID = EventID( runNumber, 1U, eventNumber);
00112   
00113   // eventDataSize = (Number Of Words)* (Word Size)
00114   int eventDataSize = eventData.size()*dduWordLength; 
00115   
00116   if (debug) cout<<" DDU ID = "<<dduID<<endl;
00117  
00118   FEDRawData& fedRawData = data->FEDData( dduID );
00119   fedRawData.resize(eventDataSize);
00120   
00121   copy(reinterpret_cast<unsigned char*>(&eventData[0]),
00122        reinterpret_cast<unsigned char*>(&eventData[0]) + eventDataSize, fedRawData.data());
00123 
00124 
00125   mySpy->setlastPointer( (char*) rawDTData32 );
00126 
00127   return true;
00128     
00129 }
00130 
00131 void DTSpyReader::swap(uint64_t & word) {
00132   
00133   twoNibble64* newWorld = reinterpret_cast<twoNibble64*>(&word);
00134 
00135   uint32_t msBits_tmp = newWorld->msBits;
00136   newWorld->msBits = newWorld->lsBits;
00137   newWorld->lsBits = msBits_tmp;
00138 }
00139 
00140 
00141 uint64_t DTSpyReader::dmaUnpack(const uint32_t* dmaData, bool & isData) {
00142   
00143   uint32_t unpackedData[2] = {0, 0};
00144   // adjust 4 32-bits words  into 2 32-bits words
00145   unpackedData[0] |= dmaData[3] & 0x3ffff;
00146   unpackedData[0] |= (dmaData[2] << 18 ) & 0xfffc0000;
00147   unpackedData[1] |= (dmaData[2] >> 14 ) & 0x0f;
00148   unpackedData[1] |= (dmaData[1] << 4 ) & 0x3ffff0;
00149   unpackedData[1] |= (dmaData[0] << 22 ) & 0xffc00000;
00150 
00151   isData = ( dmaData[0] >> 10 ) & 0x01;
00152 
00153   //printf ("Lower part: %x \n", unpackedData[0]);
00154   //printf ("Upper part: %x \n", unpackedData[1]);
00155 
00156   // push_back to a 64 word
00157   uint64_t dduWord = ( uint64_t(unpackedData[1]) << 32 ) | unpackedData[0];
00158 
00159   return dduWord;
00160 }
00161 
00162 bool DTSpyReader::isHeader(uint64_t word, bool dataTag) {
00163 
00164   bool it_is = false;
00165   FEDHeader candidate(reinterpret_cast<const unsigned char*>(&word));
00166   if ( candidate.check() ) {
00167     // if ( candidate.check() && !dataTag) {
00168     it_is = true;
00169     dduID = candidate.sourceID();
00170     eventNumber++;
00171   }
00172   return it_is;
00173 }
00174 
00175 
00176 bool DTSpyReader::isTrailer(uint64_t word, bool dataTag, int wordCount) {
00177 
00178   bool it_is = false;
00179   FEDTrailer candidate(reinterpret_cast<const unsigned char*>(&word));
00180   if ( candidate.check() ) {
00181     //  if ( candidate.check() && !dataTag) {
00182     if ( wordCount == candidate.lenght())
00183       it_is = true;
00184   }
00185   return it_is;
00186 }
00187 
00188 
00189 
00190 
00191 
00192