00001 #include "DataFormats/FEDRawData/interface/FEDRawData.h" 00002 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h" 00003 00004 #include "EcalTBDaqFileReader.h" 00005 #include "EcalTBDaqSimpleFile.h" 00006 #include "EcalTBDaqRFIOFile.h" 00007 00008 #include "FWCore/MessageLogger/interface/MessageLogger.h" 00009 00010 #include<iostream> 00011 #include<vector> 00012 #include<fstream> 00013 #include <string> 00014 00015 using namespace std; 00016 using namespace edm; 00017 00018 EcalTBDaqFileReader::EcalTBDaqFileReader(): initialized_(false), inputFile_(0) { 00019 LogDebug("EcalTBInputService") << "@SUB=EcalTBDaqFileReader"; 00020 00021 // Reset cachedData_ 00022 cachedData_.len=0; 00023 cachedData_.fedData=0; 00024 00025 } 00026 00027 EcalTBDaqFileReader::~EcalTBDaqFileReader(){ 00028 00029 LogDebug("EcalTBInputService") << "@SUB=EcalTBDaqFileReader"; 00030 00031 if(inputFile_) { 00032 inputFile_->close(); 00033 delete inputFile_; 00034 inputFile_=0; 00035 } 00036 00037 //Cleaning the event 00038 if (cachedData_.len > 0) { 00039 delete[] cachedData_.fedData; 00040 cachedData_.len = 0; 00041 } 00042 00043 } 00044 00045 void EcalTBDaqFileReader::setInitialized(bool value){initialized_=value;} 00046 00047 bool EcalTBDaqFileReader::isInitialized(){ 00048 00049 return initialized_; 00050 00051 } 00052 00053 void EcalTBDaqFileReader::initialize(const string & file, bool isBinary){ 00054 00055 isBinary_ = isBinary; 00056 std::string protocol=file.substr( 0, file.find( ":" )) ; 00057 std::string filename=file.substr( file.find( ":" )+1, file.size()); 00058 00059 // case of ASCII input data file 00060 if (initialized_) { 00061 // cout << "EcalTB DaqFileReader was already initialized... reinitializing it " << endl; 00062 if(inputFile_) { 00063 inputFile_->close(); 00064 delete inputFile_; 00065 inputFile_=0; 00066 } 00067 } 00068 00069 if (protocol == "file") 00070 inputFile_ = dynamic_cast<EcalTBDaqFile*>(new EcalTBDaqSimpleFile(filename, isBinary_)); 00071 else if (protocol == "rfio") 00072 inputFile_ = dynamic_cast<EcalTBDaqFile*>(new EcalTBDaqRFIOFile(filename, isBinary_)); 00073 00074 // Initialize cachedData_ 00075 cachedData_.len=0; 00076 cachedData_.fedData=0; 00077 initialized_ = true; 00078 } 00079 00080 bool EcalTBDaqFileReader::fillDaqEventData() { 00081 // cout<< "EcalTBDaqFileReader::fillDaqEventData() beginning " << endl; 00082 const int MAXFEDID = 1024; 00083 00084 //Cleaning the event before filling 00085 if (cachedData_.len > 0) { 00086 delete[] cachedData_.fedData; 00087 cachedData_.len = 0; 00088 } 00089 00090 pair<int,int> fedInfo; //int =FED id, int = event data length 00091 try 00092 { 00093 00094 //checkEndOfEvent()replaced by tru + getEventTrailer(); 00095 00096 if( inputFile_->checkEndOfFile() ) throw 1; 00097 00098 // FedDataPair is struct: event lenght + pointer to beginning of event 00099 if (!inputFile_->getEventData(cachedData_)) 00100 throw 2; 00101 // extracting header information from event 00102 setFEDHeader(); 00103 00104 fedInfo.first=getFedId(); 00105 fedInfo.second = cachedData_.len; 00106 00107 // cout << " fillDaqEventData Fed Id " << fedInfo.first << " getEventLength() " 00108 // << getEventLength() << " run " << getRunNumber() << " Ev " 00109 // << getEventNumber() << endl; 00110 00111 00112 // EventID ev( getRunNumber(), getEventNumber() ); 00113 // cID=ev; 00114 00115 if(fedInfo.first<0) 00116 { 00117 LogError("EcalTBInputService") << "@SUB=EcalTBDaqFileReader::addFEDRawData" << "negative FED Id. Adding no data"; 00118 throw 2; 00119 } 00120 else if (fedInfo.first>MAXFEDID) 00121 { 00122 LogError("EcalTBInputService") << "@SUB=EcalTBDaqFileReader::addFEDRawData" << "FED Id(" << fedInfo.first << ") greater than maximum allowed (" << MAXFEDID << "). Adding no data"; 00123 throw 3; 00124 } 00125 return true; 00126 } 00127 catch(int i) 00128 { 00129 if (i==1) 00130 { 00131 LogInfo("EcalTBInputService") << "@SUB=EcalTBDaqFileReader::fillDaqEventData" << "END OF FILE REACHED. No information read for the requested event"; 00132 return false; 00133 } 00134 else 00135 { 00136 LogError("EcalTBInputService") << "@SUB=EcalTBDaqFileReader::fillDaqEventData" << "unkown exception"; 00137 return false; 00138 } 00139 } 00140 } 00141 00142 void EcalTBDaqFileReader::setFEDHeader() { 00143 00144 // cout<<"getting FED Header "<< endl; 00145 int headsize=16; 00146 headValues_.clear(); 00147 00148 unsigned long* buf = reinterpret_cast<unsigned long*>(cachedData_.fedData); 00149 // int val=0; 00150 00151 for ( int i=0; i< headsize/4; ++i) { 00152 // cout << i << " " << hex << buf << dec << endl ; 00153 00154 if ( i==0) { 00155 headValues_.push_back((*buf>>8)&0xFFF); // DCC id 00156 00157 } else if ( i==1) { 00158 headValues_.push_back( (*buf)&0xFFFFFF); // Lv1 number 00159 // cout << " LV1 " << ((*buf)&0xFFFFFF) << endl; 00160 } else if ( i==2) { 00161 headValues_.push_back( ((*buf)&0xFFFFFF)*8 ); // Event length 00162 // cout << " Event length " << ((*buf)&0xFFFFFF)*8 << endl; 00163 } else if ( i==3) { 00164 // int runN= (*buf)&0xFFFFFF; 00165 // cout << " runN " << runN << endl; 00166 headValues_.push_back( (*buf)&0xFFFFFF); // Run NUmber 00167 00168 } 00169 buf+=1; 00170 } 00171 00172 } 00173 00174 bool EcalTBDaqFileReader::checkEndOfFile() const 00175 { 00176 return inputFile_->checkEndOfFile(); 00177 } 00178