CMS 3D CMS Logo

DTROS8FileReader.cc

Go to the documentation of this file.
00001 
00008 #include <IORawData/DTCommissioning/src/DTROS8FileReader.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 #include <DataFormats/FEDRawData/interface/FEDRawData.h>
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    
00027 using namespace std;
00028 using namespace edm;
00029 
00030 
00031 DTROS8FileReader::DTROS8FileReader(const edm::ParameterSet& pset) : 
00032   runNum(1), eventNum(0) {
00033       
00034   const string & filename = pset.getParameter<string>("fileName");
00035 
00036   inputFile.open(filename.c_str());
00037   if( inputFile.fail() ) {
00038     throw cms::Exception("InputFileMissing") 
00039       << "DTROS8FileReader: the input file: " << filename <<" is not present";
00040   }
00041 }
00042 
00043 
00044 DTROS8FileReader::~DTROS8FileReader(){
00045   inputFile.close();
00046 }
00047 
00048 
00049 bool DTROS8FileReader::fillRawData(EventID& eID,
00050                                    Timestamp& tstamp, 
00051                                    FEDRawDataCollection*& data){
00052   data = new FEDRawDataCollection();
00053 
00054   try {
00055     
00056 
00057     if( checkEndOfFile() ) throw 1; 
00058     
00059 
00060     // Get the total number of words from the 1st word in the payload
00061     int numberOfWords;
00062     int nread = 0;
00063     nread = inputFile.read(dataPointer<int>( &numberOfWords ), ros8WordLenght);
00064     if ( nread<=0 )  throw 1;
00065 
00066 
00067     // Get the event data (all words but the 1st)
00068     int* eventData = new int[numberOfWords];
00069     nread = inputFile.read(dataPointer<int>( eventData + 1 ), (numberOfWords-1) * ros8WordLenght );
00070     if ( nread<=0 ) throw 1;
00071     
00072 
00073     // Check that the event data size corresponds to the 1st word datum 
00074     if ( eventData[numberOfWords-1] != numberOfWords ) {
00075       cout << "[DTROS8FileReader]: word counter mismatch exception: "
00076            << numberOfWords << " " << eventData[numberOfWords-1] << endl;
00077       throw 99;
00078     }
00079 
00080     // The header added by the local DAQ occupies 8 words, starting from the 2nd 
00081     int* head = eventData + 1;
00082     
00083     /* 
00084       Header word 0: run number
00085       Header word 1: spill number
00086       Header word 2: event number
00087       Header word 3: reserved
00088       Header word 4: ROS data offset
00089       Header word 5: PU data offset
00090       Header word 6: reserved
00091       Header word 7: reserved
00092     */
00093 
00094     // WARNING: the event number is reset at a new spill
00095     eID = EventID( head[0], head[1]*head[2]);
00096 
00097     // The pointer to the ROS payload (the 1st word being the ROS words counter)
00098     int* rosData = eventData + head[4];
00099 
00100     // The ROS payload size
00101     int eventDataSize = *rosData * ros8WordLenght;
00102     // It has to be a multiple of 8 bytes. if not, adjust the size of the FED payload
00103     int adjustment = (eventDataSize/4)%2 == 1 ? 4 : 0;   
00104     
00105     //if ( (eventDataSize/4)%2 ) adjustment = 4;
00106 
00107 
00108     // The FED ID is always the first in the DT range
00109     FEDRawData& fedRawData = data->FEDData( FEDNumbering::getDTFEDIds().first );
00110     fedRawData.resize(eventDataSize+adjustment);
00111     
00112     // I pass only the ROS data to the Event
00113     copy(reinterpret_cast<unsigned char*>(rosData), 
00114          reinterpret_cast<unsigned char*>(rosData) + eventDataSize, fedRawData.data());
00115 
00116     // needed to get rid of memory leaks (?)
00117     delete[] eventData;
00118 
00119     return true;
00120   }
00121 
00122   catch( int i ) {
00123 
00124     if ( i == 1 ){
00125       cout << "[DTROS8FileReader]: END OF FILE REACHED. "
00126            << "No information read for the requested event" << endl;
00127       delete data; data=0;
00128       return false;
00129     }    
00130     else {
00131       cout << "[DTROS8FileReader]: PROBLEM WITH EVENT INFORMATION ON THE FILE. "
00132            << "EVENT DATA READING FAILED  code= " << i << endl;
00133       delete data; data=0;
00134       return false;
00135     }
00136 
00137   }
00138 
00139 }
00140 
00141 
00142 bool DTROS8FileReader::checkEndOfFile(){
00143 
00144   bool retval=false;
00145   if ( inputFile.eof() ) retval=true;
00146   return retval;
00147 
00148 }
00149 
00150 
00151 

Generated on Tue Jun 9 17:39:23 2009 for CMSSW by  doxygen 1.5.4