CMS 3D CMS Logo

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

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