00001 00008 #include <IORawData/DTCommissioning/src/DTROS25FileReader.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 DTROS25FileReader::DTROS25FileReader(const edm::ParameterSet& pset) : 00032 runNumber(1), eventNumber(0) { 00033 00034 const string & filename = pset.getUntrackedParameter<string>("fileName"); 00035 00036 inputFile.open(filename.c_str()); 00037 if( inputFile.fail() ) { 00038 throw cms::Exception("InputFileMissing") 00039 << "DTROS25FileReader: the input file: " << filename <<" is not present"; 00040 } 00041 } 00042 00043 00044 DTROS25FileReader::~DTROS25FileReader(){ 00045 inputFile.close(); 00046 } 00047 00048 00049 bool DTROS25FileReader::fillRawData(EventID& eID, 00050 Timestamp& tstamp, 00051 FEDRawDataCollection*& data){ 00052 data = new FEDRawDataCollection(); 00053 00054 vector<uint32_t> eventData; 00055 size_t estimatedEventDimension = 102400; // dimensione hardcoded 00056 eventData.reserve(estimatedEventDimension); 00057 uint32_t word = 0; 00058 00059 00060 try { 00061 00062 bool marked = false; 00063 00064 // getting the data word by word from the file 00065 // do it until you get the ROS25 trailer 00066 while ( !isTrailer(word) ) { 00067 00068 // get the first word 00069 int nread = inputFile.read(dataPointer<uint32_t>( &word ), rosWordLenght); 00070 00071 // WARNING!!! ||swapping it|| (Check whether it is necessary) 00072 swap(word); 00073 00074 if ( nread<=0 ) throw 1; 00075 00076 // get the ROS25 header 00077 if (isHeader(word)) marked=true; 00078 00079 // from now on fill the eventData with the ROS data 00080 if (marked) { 00081 eventData.push_back(word); 00082 00083 } 00084 } 00085 00086 // next event reading will start with meaningless trailer+header from DTLocalDAQ 00087 // those will be skipped automatically when seeking for the ROS25 header 00088 00089 //if (eventData.size() > estimatedEventDimension) throw 2; 00090 00091 // Setting the Event ID 00092 eID = EventID( runNumber, eventNumber); 00093 00094 // eventDataSize = (Number Of Words)* (Word Size) 00095 int eventDataSize = eventData.size()*rosWordLenght; 00096 // It has to be a multiple of 8 bytes. if not, adjust the size of the FED payload 00097 int adjustment = (eventDataSize/4)%2 == 1 ? 4 : 0; 00098 00099 // The FED ID is always the first in the DT range 00100 FEDRawData& fedRawData = data->FEDData( FEDNumbering::getDTFEDIds().first ); 00101 fedRawData.resize(eventDataSize+adjustment); 00102 00103 copy(reinterpret_cast<unsigned char*>(&eventData[0]), 00104 reinterpret_cast<unsigned char*>(&eventData[0]) + eventDataSize, fedRawData.data()); 00105 00106 return true; 00107 } 00108 00109 catch( int i ) { 00110 00111 if ( i == 1 ){ 00112 cout<<"[DTROS25FileReader]: ERROR! failed to get the trailer"<<endl; 00113 delete data; data=0; 00114 return false; 00115 } 00116 else { 00117 cout<<"[DTROS25FileReader]:" 00118 <<" ERROR! ROS data exceeding estimated event dimension. Event size = " 00119 <<eventData.size()<<endl; 00120 delete data; data=0; 00121 return false; 00122 } 00123 00124 } 00125 00126 } 00127 00128 void DTROS25FileReader::swap(uint32_t & word) { 00129 00130 twoNibble* newWorld = reinterpret_cast<twoNibble*>(&word); 00131 00132 uint16_t msBits_tmp = newWorld->msBits; 00133 newWorld->msBits = newWorld->lsBits; 00134 newWorld->lsBits = msBits_tmp; 00135 } 00136 00137 00138 bool DTROS25FileReader::isHeader(uint32_t word) { 00139 00140 bool it_is = false; 00141 if ( (word >> 24 ) == 31 ) { 00142 it_is = true; 00143 eventNumber++; 00144 } 00145 00146 return it_is; 00147 } 00148 00149 00150 bool DTROS25FileReader::isTrailer(uint32_t word) { 00151 00152 bool it_is = false; 00153 if ( (word >> 24 ) == 63 ) { 00154 it_is = true; 00155 } 00156 00157 return it_is; 00158 } 00159 00160 00161 bool DTROS25FileReader::checkEndOfFile(){ 00162 00163 bool retval=false; 00164 if ( inputFile.eof() ) retval=true; 00165 return retval; 00166 00167 } 00168 00169 00170