00001
00002
00003
00004
00005
00013
00014
00015
00016
00017
00018
00019
00020 #include <memory>
00021 #include "FWCore/Framework/interface/Frameworkfwd.h"
00022 #include "DataFormats/FEDRawData/interface/FEDRawData.h"
00023 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
00024 #include "IORawData/SiPixelInputSources/interface/PixelSLinkDataInputSource.h"
00025 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00026 #include "Utilities/StorageFactory/interface/StorageFactory.h"
00027 #include "Utilities/StorageFactory/interface/StorageAccount.h"
00028 #include "Utilities/StorageFactory/interface/IOTypes.h"
00029 #include <iostream>
00030
00031 using namespace edm;
00032
00033
00034 int PixelSLinkDataInputSource::getEventNumberFromFillWords(std::vector<uint64_t> buffer, uint32_t & totword ){
00035
00036
00037
00038
00039 int fif2cnt=0;
00040 int dumcnt=0;
00041 int gapcnt=0;
00042 unsigned long gap[8];
00043 unsigned long dum[8];
00044 unsigned long word[2]={0,0};
00045 unsigned long int chan=0;
00046 unsigned long int roc=0;
00047
00048 const unsigned long int rocmsk = 0x3e00000;
00049 const unsigned long int chnlmsk = 0xfc000000;
00050
00051 for(int jk=1;jk<9;jk++)gap[jk]=0;
00052 for(int jk=1;jk<9;jk++)dum[jk]=0;
00053
00054 int fifcnt=1;
00055 for(size_t kk=0; kk<buffer.size(); ++kk)
00056 {
00057
00058 word[0] = (unsigned long) buffer[kk];
00059 word[1] = (unsigned long) (buffer[kk]>>32);
00060
00061 for(size_t iw=0; iw<2; iw++)
00062 {
00063 chan= ((word[iw]&chnlmsk)>>26);
00064 roc= ((word[iw]&rocmsk)>>21);
00065
00066
00067 if(roc<25){
00068 if((chan>4)&&(chan<10)&&(fifcnt!=2)) {fif2cnt=0;fifcnt=2;}
00069 if((chan>9)&&(chan<14)&&(fifcnt!=3)) {fif2cnt=0;fifcnt=3;}
00070 if((chan>13)&&(chan<19)&&(fifcnt!=4)){fif2cnt=0;fifcnt=4;}
00071 if((chan>18)&&(chan<23)&&(fifcnt!=5)){fif2cnt=0;fifcnt=5;}
00072 if((chan>22)&&(chan<28)&&(fifcnt!=6)){fif2cnt=0;fifcnt=6;}
00073 if((chan>27)&&(chan<32)&&(fifcnt!=7)){fif2cnt=0;fifcnt=7;}
00074 if((chan>31)&&(fifcnt!=8)){fif2cnt=0;fifcnt=8;}
00075 fif2cnt++;
00076 }
00077 if(roc==26){gap[fifcnt]=(0x1000+(word[iw]&0xff));gapcnt++;}
00078
00079 if((roc==27)&&((fif2cnt+dumcnt)<6)){dumcnt++;dum[fifcnt]=(0x1000+(word[iw]&0xff));}
00080 else if((roc==27)&&((fif2cnt+dumcnt)>6)){dumcnt=1;fif2cnt=0;fifcnt++;}
00081 }
00082
00083
00084 if(((fif2cnt+dumcnt)==6)&&(dumcnt>0))
00085 {dumcnt=0;fif2cnt=0;fifcnt++;}
00086 if((gapcnt>0)&&((dumcnt+fif2cnt)>5))
00087 {gapcnt=0;fifcnt++;fif2cnt=0;dumcnt=0;}
00088 else if((gapcnt>0)&&((dumcnt+fif2cnt)<6)) gapcnt=0;
00089
00090 }
00091
00092 int status=0;
00093
00094 if(gap[1]>0) {totword=(gap[1]&0xff);status=1;}
00095 else if(gap[2]>0){totword=(gap[2]&0xff);status=1;}
00096 else if(dum[1]>0){totword=(dum[1]&0xff);status=1;}
00097 else if(dum[2]>0){totword=(dum[2]&0xff);status=1;}
00098
00099 if(gap[3]>0) {totword=totword|((gap[3]&0xff)<<8);status=status|0x2;}
00100 else if(gap[4]>0){totword=totword|((gap[4]&0xff)<<8);status=status|0x2;}
00101 else if(dum[3]>0){totword=totword|((dum[3]&0xff)<<8);status=status|0x2;}
00102 else if(dum[4]>0){totword=totword|((dum[4]&0xff)<<8);status=status|0x2;}
00103
00104 if(gap[5]>0) {totword=totword|((gap[5]&0xff)<<16);status=status|0x4;}
00105 else if(gap[6]>0){totword=totword|((gap[6]&0xff)<<16);status=status|0x4;}
00106 else if(dum[5]>0){totword=totword|((dum[5]&0xff)<<16);status=status|0x4;}
00107 else if(dum[6]>0){totword=totword|((dum[6]&0xff)<<16);status=status|0x4;}
00108
00109 if(gap[7]>0){totword=totword|((gap[7]&0xff)<<24);status=status|0x8;}
00110 else if(gap[8]>0){totword=totword|((gap[8]&0xff)<<24);status=status|0x8;}
00111 else if(dum[7]>0){totword=totword|((dum[7]&0xff)<<24);status=status|0x8;}
00112 else if(dum[8]>0){totword=totword|((dum[8]&0xff)<<24);status=status|0x8;}
00113 return(status);
00114
00115 }
00116
00117
00118 PixelSLinkDataInputSource::PixelSLinkDataInputSource(const edm::ParameterSet& pset,
00119 const edm::InputSourceDescription& desc) :
00120 ExternalInputSource(pset,desc),
00121 m_fedid(pset.getUntrackedParameter<int>("fedid")),
00122 m_fileindex(0),
00123 m_runnumber(pset.getUntrackedParameter<int>("runNumber",-1)),
00124 m_currenteventnumber(0),
00125 m_currenttriggernumber(0),
00126 m_eventnumber_shift(0)
00127 {
00128 produces<FEDRawDataCollection>();
00129
00130 if (m_fileindex>=fileNames().size()) {
00131 edm::LogInfo("") << "no more file to read " << std::endl;
00132 return;
00133 }
00134 std::string currentfilename = fileNames()[m_fileindex];
00135 edm::LogInfo("") << "now examining file "<< currentfilename ;
00136 m_fileindex++;
00137
00138 IOOffset size = -1;
00139 StorageFactory::get()->enableAccounting(true);
00140
00141 bool exists = StorageFactory::get() -> check(currentfilename.c_str(), &size);
00142
00143 edm::LogInfo("PixelSLinkDataInputSource") << "file size " << size << std::endl;
00144
00145 if(!exists){
00146 edm::LogInfo("") << "file " << currentfilename << " cannot be found.";
00147 return;
00148 }
00149
00150 storage.reset(StorageFactory::get()->open(currentfilename.c_str()));
00151
00152
00153
00154
00155 Storage & temp_file = *storage;
00156
00157 temp_file.read((char*)&m_data,8);
00158
00159 if((m_data >> 60) != 0x5){
00160 uint32_t runnum = m_data;
00161 if(m_runnumber!=-1)
00162 edm::LogInfo("") << "WARNING: observed run number encoded in S-Link dump. Overwriting run number as defined in .cfg file!!! Run number now set to " << runnum << " (was " << m_runnumber << ")";
00163 m_runnumber=runnum;
00164 }
00165 if(m_runnumber!=0)
00166 setRunNumber(m_runnumber);
00167 temp_file.read((char*)&m_data,8);
00168 m_currenteventnumber = (m_data >> 32)&0x00ffffff ;
00169 }
00170
00171
00172 PixelSLinkDataInputSource::~PixelSLinkDataInputSource() {
00173
00174
00175 }
00176
00177 bool PixelSLinkDataInputSource::produce(edm::Event& event) {
00178 bool lastevent=false;
00179 Storage & m_file = *storage;
00180
00181
00182 std::auto_ptr<FEDRawDataCollection> buffers( new FEDRawDataCollection );
00183
00184
00185 uint32_t eventnumber =(m_data >> 32)&0x00ffffff ;
00186
00187 do{
00188 std::vector<uint64_t> buffer;
00189
00190
00191
00192 unsigned int count=0;
00193 eventnumber = (m_data >> 32)&0x00ffffff ;
00194 if(m_currenteventnumber==0)
00195 m_currenteventnumber=eventnumber;
00196 edm::LogInfo("PixelSLinkDataInputSource::produce()") << "**** event number = " << eventnumber << " global event number " << m_currenteventnumber << " data " << std::hex << m_data << std::dec << std::endl;
00197 while ((m_data >> 60) != 0x5){
00198
00199 if (count==0){
00200 edm::LogWarning("") << "DATA CORRUPTION!" ;
00201 edm::LogWarning("") << "Expected to find header, but read: 0x"
00202 << std::hex<<m_data<<std::dec ;
00203 }
00204
00205 count++;
00206 int n=m_file.read((char*)&m_data,8);
00207 edm::LogWarning("") << "next data " << std::hex << m_data << std::dec << std::endl;
00208
00209 if (n!=8) {
00210 edm::LogInfo("") << "End of input file" ;
00211 return false;
00212 }
00213 }
00214
00215
00216 if (count>0) {
00217 edm::LogWarning("")<<"Had to read "<<count<<" words before finding header!"<<std::endl;
00218 }
00219
00220 if (m_fedid>-1) {
00221 m_data=(m_data&0xfffffffffff000ffLL)|((m_fedid&0xfff)<<8);
00222 }
00223
00224 unsigned int fed_id=(m_data>>8)&0xfff;
00225
00226 buffer.push_back(m_data);
00227
00228 do{
00229 m_file.read((char*)&m_data,8);
00230 buffer.push_back(m_data);
00231 }
00232 while((m_data >> 60) != 0xa);
00233
00234
00235 std::auto_ptr<FEDRawData> rawData(new FEDRawData(8*buffer.size()));
00236
00237 unsigned char* dataptr=rawData->data();
00238
00239 for (unsigned int i=0;i<buffer.size();i++){
00240 ((unsigned long long *)dataptr)[i]=buffer[i];
00241 }
00242 uint32_t thetriggernumber=0;
00243 int nfillwords = getEventNumberFromFillWords(buffer,thetriggernumber);
00244
00245 if(nfillwords>0){
00246 LogInfo("") << "n fill words = " << nfillwords << ", trigger numbers: " << thetriggernumber << "," << m_currenttriggernumber << std::endl;
00247 m_eventnumber_shift = thetriggernumber - m_currenttriggernumber;
00248 }
00249 m_currenttriggernumber = thetriggernumber;
00250 FEDRawData& fedRawData = buffers->FEDData( fed_id );
00251 fedRawData=*rawData;
00252
00253
00254 int n =m_file.read((char*)&m_data,8);
00255 if (n==0) {
00256 edm::LogInfo("") << "End of input file" ;
00257 lastevent=true;
00258 }
00259 m_currenteventnumber = (m_data >> 32)&0x00ffffff ;
00260 if(m_currenteventnumber<eventnumber)
00261 LogError("PixelSLinkDataInputSource") << " error, the previous event number (" << eventnumber << ") is LARGER than the next event number (" << m_currenteventnumber << ")" << std::endl;
00262
00263 }
00264 while( eventnumber == m_currenteventnumber);
00265
00266 uint32_t realeventno = synchronizeEvents();
00267 setEventNumber(realeventno);
00268 event.put(buffers);
00269
00270
00271 return true;
00272 }
00273
00274
00275 uint32_t PixelSLinkDataInputSource::synchronizeEvents(){
00276 int32_t result= m_currenteventnumber -1;
00277
00278 return(uint32_t) result;
00279 }