CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CSCFileReader.cc
Go to the documentation of this file.
1 #include "CSCFileReader.h"
2 
3 //#include <iostream.h>
4 
5 #include <errno.h>
6 #include <stdlib.h>
7 #include <cstring>
8 
16 
19 
20 #include <vector>
21 #include <string>
22 #include <iosfwd>
23 #include <sstream>
24 #include <iostream>
25 #include <algorithm>
26 
27 #define nRUIs 40
28 #define nFUs 4
29 
31  LogDebug("CSCFileReader|ctor")<<"Started ...";
32  // Below some data members are recycled for both cases: RUIs and FUs
33  // this is ok as long as eighter of RUI or FU are are provided in .cfg (not both)
34  nActiveRUIs = 0;
35  nActiveFUs = 0;
36  for(int unit=0; unit<nRUIs; unit++){
37  std::ostringstream ruiName, fuName;
38  ruiName<<"RUI"<<(unit<10?"0":"")<<unit<<std::ends;
39  fuName <<"FU" <<unit<<std::ends;
40  std::vector<std::string> ruiFiles = pset.getUntrackedParameter< std::vector<std::string> >(ruiName.str().c_str(),std::vector<std::string>(0));
41  std::vector<std::string> fuFiles = pset.getUntrackedParameter< std::vector<std::string> >(fuName.str().c_str(),std::vector<std::string>(0));
42  if( ruiFiles.begin() != ruiFiles.end() ) nActiveRUIs++;
43  if( fuFiles.begin() != fuFiles.end() ) nActiveFUs++;
44  }
45  if( nActiveFUs && nActiveRUIs )
46  throw cms::Exception("CSCFileReader|configuration")<<"RUIs and FUs in conflict: either RUI or FU may be defined at a time, not both";
47  if( !nActiveFUs && !nActiveRUIs )
48  throw cms::Exception("CSCFileReader|configuration")<<"Module lacks configuration";
49 
50 
51  // Get list of RUI input files from .cfg file
52  for(int rui=0; rui<nRUIs && !nActiveFUs; rui++){
53  std::ostringstream name;
54  name<<"RUI"<<(rui<10?"0":"")<<rui<<std::ends;
55 
56  // Obtain list of files associated with current RUI
57  fileNames[rui] = pset.getUntrackedParameter< std::vector<std::string> >(name.str().c_str(),std::vector<std::string>(0));
58  currentFile[rui] = fileNames[rui].begin();
59 
60  // If list of files is not empty, open first file
61  if( currentFile[rui] != fileNames[rui].end() ){
62  try {
63  RUI[rui].open(currentFile[rui]->c_str());
64  } catch ( std::runtime_error err ){
65  throw cms::Exception("CSCFileReader")<<"InputFileMissing: "<<err.what()<<" (errno="<<errno<<")";
66  }
67  nActiveRUIs++;
68  }
69 
70  // Filter out possible corruptions
72  // Do not select anything in particular
73  RUI[rui].select(0);
74 
75  currentL1A[rui] = -1;
76  }
77 
78  // Get list of FU input files from .cfg file
79  for(int fu=0; fu<nFUs && !nActiveRUIs; fu++){
80  std::ostringstream name;
81  name<<"FU"<<fu<<std::ends;
82 
83  // Obtain list of files associated with current FU
84  fileNames[fu] = pset.getUntrackedParameter< std::vector<std::string> >(name.str().c_str(),std::vector<std::string>(0));
85  currentFile[fu] = fileNames[fu].begin();
86 
87  // If list of files is not empty, open first file
88  if( currentFile[fu] != fileNames[fu].end() ){
89  try {
90  FU[fu].open(currentFile[fu]->c_str());
91  } catch ( std::runtime_error err ){
92  throw cms::Exception("CSCFileReader")<<"InputFileMissing: "<<err.what()<<" (errno="<<errno<<")";
93  }
94  nActiveFUs++;
95  }
96 
97  // Filter out possible corruptions
99  // Do not select anything in particular
100  FU[fu].select(0);
101 
102  currentL1A[fu] = -1;
103  }
104 
105  if( nActiveRUIs && !nActiveFUs ){
106  // Assign RUIs to FEDs
107  for(int fed=FEDNumbering::MINCSCFEDID; fed<=FEDNumbering::MAXCSCFEDID; fed++){
108  std::ostringstream name;
109  name<<"FED"<<fed<<std::ends;
110  std::vector<std::string> rui_list = pset.getUntrackedParameter< std::vector<std::string> >(name.str().c_str(),std::vector<std::string>(0));
111  for(std::vector<std::string>::const_iterator rui=rui_list.begin(); rui!=rui_list.end(); rui++)
112  FED[fed].push_back((unsigned int)atoi(rui->c_str()+rui->length()-2));
113  }
114  // Do the same for Track-Finder FED
116  std::ostringstream name;
117  name<<"FED"<<fed<<std::ends;
118  std::vector<std::string> rui_list = pset.getUntrackedParameter< std::vector<std::string> >(name.str().c_str(),std::vector<std::string>(0));
119  for(std::vector<std::string>::const_iterator rui=rui_list.begin(); rui!=rui_list.end(); rui++)
120  FED[fed].push_back((unsigned int)atoi(rui->c_str()+rui->length()-2));
121  }
122  }
123  // Starting point
124  firstEvent = pset.getUntrackedParameter<int>("firstEvent",0);
125  nEvents = 0;
126  expectedNextL1A = -1;
127 
128  // If Track-Finder was in readout specify its position in the record or set -1 otherwise
129  // Current agriment is that if there is a TF event it is first DDU record
130  tfDDUnumber = pset.getUntrackedParameter<int>("tfDDUnumber",-1);
131 
132  // For efficiency reasons create this big chunk of data only once
133  tmpBuf = new unsigned short[200000*nRUIs+4*4];
134  // Event buffer and its length for every FU
135  fuEvent[0]=0; fuEventSize[0]=0;
136  fuEvent[1]=0; fuEventSize[1]=0;
137  fuEvent[2]=0; fuEventSize[2]=0;
138  fuEvent[3]=0; fuEventSize[3]=0;
139  // Event buffer and its length for every RU
140  for(int rui=0; rui<nRUIs; rui++){
141  ruBuf[rui] = 0;
142  ruBufSize[rui] = 0;
143  }
144  LogDebug("CSCFileReader|ctor")<<"... and finished";
145 }
146 
148 
149 int CSCFileReader::readRUI(int rui, const unsigned short* &buf, size_t &length){
150  if( currentFile[rui] == fileNames[rui].end() ) return -1;
151  do {
152  try {
153  length = RUI[rui].next(buf);
154  } catch ( std::runtime_error err ){
155  throw cms::Exception("CSCFileReader|reading")<<"EndOfStream: "<<err.what()<<" (errno="<<errno<<")";
156  }
157  if( length==0 ){ // end of file, try next one
158  if( ++currentFile[rui] != fileNames[rui].end() ){
159  try {
160  RUI[rui].open(currentFile[rui]->c_str());
161  } catch ( std::runtime_error err ){
162  throw cms::Exception("CSCFileReader|reading")<<"InputFileMissing: "<<err.what()<<" (errno="<<errno<<")";
163  }
164  } else return -1;
165  }
166  } while( length==0 );
167  return buf[2]|((buf[3]&0xFF)<<16);
168 }
169 
170 int CSCFileReader::readFU(int fu, const unsigned short* &buf, size_t &length){
171  if( currentFile[fu] == fileNames[fu].end() ) return -1;
172  do {
173  try {
174  length = FU[fu].next(buf);
175  } catch ( std::runtime_error err ){
176  throw cms::Exception("CSCFileReader|reading")<<"EndOfStream: "<<err.what()<<" (errno="<<errno<<")";
177  }
178  if( length==0 ){ // end of file, try next one
179  if( ++currentFile[fu] != fileNames[fu].end() ){
180  try {
181  FU[fu].open(currentFile[fu]->c_str());
182  } catch ( std::runtime_error err ){
183  throw cms::Exception("CSCFileReader|reading")<<"InputFileMissing: "<<err.what()<<" (errno="<<errno<<")";
184  }
185  } else return -1;
186  }
187  } while( length==0 );
188  // Take L1A from first DDU header in the DCC record (shift=8)
189  return buf[2+8]|((buf[3+8]&0xFF)<<16);
190 }
191 
193  int eventNumber =-1; // Will determine below
194 
195  do {
196  // Read next event from RUIs
197  for(int rui=0; rui<nRUIs; rui++){
198  // read event from the RUI only in two cases:
199  // 1) it is readable (currentL1A>0) and we expect next event from the RUI
200  // 2) it is first time (expectedNextL1A<0)
201  if((currentL1A[rui]>0 && currentL1A[rui]<expectedNextL1A) || expectedNextL1A<0 )
202  currentL1A[rui] = readRUI(rui,ruBuf[rui],ruBufSize[rui]);
203  }
204  eventNumber =-1;
205 
206  // Select lowest L1A from all RUIs and don't expect next event from RUIs that currently hold higher L1A
207  for(int rui=0; rui<nRUIs; rui++)
208  if( currentL1A[rui]>=0 && (eventNumber>currentL1A[rui] || eventNumber==-1) ) eventNumber=currentL1A[rui];
209  // No readable RUIs => fall out
210  if( eventNumber<0 ) return -1;
211  // Expect next event to be incremented by 1 wrt. to the current event
212  expectedNextL1A = eventNumber+1;
213 
214  } while(nEvents++<firstEvent);
215 
216  for(std::map<unsigned int,std::list<unsigned int> >::const_iterator fed=FED.begin(); fed!=FED.end(); fed++)
217  if( fed->first<(unsigned int)FEDNumbering::MINCSCTFFEDID ){
218  // Now let's pretend that DDU data were wrapped with DCC Header (2 64-bit words) and Trailer (2 64-bit words):
219  unsigned short *dccBuf=tmpBuf, *dccCur=dccBuf;
220  dccCur[3] = 0x5000; dccCur[2] = 0x0000; dccCur[1] = 0x0000; dccCur[0] = 0x005F; // Fake DCC Header 1
221  dccCur[7] = 0xD900; dccCur[6] = 0x0000; dccCur[5] = 0x0000; dccCur[4] = 0x0017; // Fake DCC Header 2
222  dccCur += 8;
223 
224  for(std::list<unsigned int>::const_iterator rui=fed->second.begin(); rui!=fed->second.end(); rui++){
225 //cout<<"Event:"<<eventNumber<<" FED:"<<fed->first<<" RUI:"<<*(fed->second.begin())<<" currL1A:"<<currentL1A[*rui]<<endl;
226  if( currentL1A[*rui]==eventNumber ){
227  if(dccCur-dccBuf+ruBufSize[*rui]>=200000*nRUIs+8) throw cms::Exception("CSCFileReader|eventBuffer")<<"OutOfBuffer: Event size exceeds maximal size allowed!";
228  memcpy(dccCur,ruBuf[*rui],ruBufSize[*rui]*sizeof(unsigned short));
229  dccCur += ruBufSize[*rui];
230  }
231  }
232  dccCur[3] = 0xEF00; dccCur[2] = 0x0000; dccCur[1] = 0x0000; dccCur[0] = 0x0000; // Fake DCC Trailer 2
233  dccCur[7] = 0xAF00; dccCur[6] = 0x0000; dccCur[5] = 0x0000; dccCur[4] = 0x0007; // Fake DCC Trailer 2
234  dccCur += 8;
235 
236  FEDRawData& fedRawData = data->FEDData(fed->first);
237  fedRawData.resize((dccCur-dccBuf)*sizeof(unsigned short));
238  std::copy((unsigned char*)dccBuf,(unsigned char*)dccCur,fedRawData.data());
239  } else {
240  for(std::list<unsigned int>::const_iterator rui=fed->second.begin(); rui!=fed->second.end(); rui++){
241  FEDRawData& fedRawData = data->FEDData(fed->first);
242  fedRawData.resize(ruBufSize[*rui]*sizeof(unsigned short));
243  std::copy((unsigned char*)ruBuf[*rui],(unsigned char*)(ruBuf[*rui]+ruBufSize[*rui]),fedRawData.data());
244  }
245  }
246 
247  return eventNumber;
248 }
249 
251  int eventNumber =-1; // Will determine below
252 
253  // If this is a first time - read one event from each FU
254  if( expectedNextL1A<0 )
255  for(int fu=0; fu<nFUs; fu++)
256  currentL1A[fu] = readFU(fu,fuEvent[fu],fuEventSize[fu]);
257 
258  // Keep buffers for every FU ready at all times
259  // When buffer from some FU is ready to go as the next event,
260  // release it, but read next one
261  int readyToGo = -1;
262  for(int fu=0; fu<nFUs; fu++){
263  // If FU is readable and (first loop of this cycle or current FU holds smallest L1A)
264  if(currentL1A[fu]>=0 && (eventNumber<0 || currentL1A[fu]<eventNumber)){
265  readyToGo = fu;
266  eventNumber = currentL1A[fu];
267  }
268  }
269  // No readable FUs => fall out
270  if( readyToGo<0 ) return -1;
271 
272  expectedNextL1A = eventNumber + 1;
273 
274  // Compose event from DDC record striped of Track-Finder DDU and a separate TF DDU event
275  unsigned long long *start = (unsigned long long *)fuEvent[readyToGo];
276  unsigned long long *end = 0;
277  enum {Header=1,Trailer=2};
278  unsigned int eventStatus = 0;
279  for(int dduRecord=0; dduRecord<=tfDDUnumber; dduRecord++){
280  unsigned long long word_0=0, word_1=0, word_2=0;
281  size_t dduWordCount = 0;
282  while( !end && dduWordCount<fuEventSize[readyToGo] ){
283  unsigned long long *dduWord = start;
284 
285  while( dduWordCount<fuEventSize[readyToGo] ){
286  word_0 = word_1; // delay by 2 DDU words
287  word_1 = word_2; // delay by 1 DDU word
288  word_2 = *dduWord;// current DDU word
289  if( (word_2&0xFFFFFFFFFFFF0000LL)==0x8000000180000000LL ){
290  if( eventStatus&Header ){ // Second header
291  word_2 = word_1;
292  end = dduWord;
293  break;
294  }
295  start = dduWord;
296  }
297  if( (word_0&0xFFFFFFFFFFFF0000LL)==0x8000FFFF80000000LL ){
298  eventStatus |= Trailer;
299  end = ++dduWord;
300  break;
301  }
302  // Increase counters by one DDU word
303  dduWord++;
304  dduWordCount++;
305  }
306  }
307  // If reach max length
308  if( dduWordCount==fuEventSize[readyToGo] ){
309  end = (unsigned long long *)(fuEvent[readyToGo]+fuEventSize[readyToGo]);
310  break;
311  }
312  }
313  // Include 0x5xxx preHeader if exists
314  if( start>(unsigned long long *)fuEvent[readyToGo] && (*(start-1)&0xF000000000000000LL)==0x5000000000000000LL ) start--;
315 
316  // If Track-Finder DDU was in readout
317  if( tfDDUnumber>=0 ){
318  // Cut out Track-Finder DDU from the buffer
319  if( !end ) throw cms::Exception("CSCFileReader|lookingForTF")<<" Sanity check failed (end==0)! Should never happen";
320 
321  FEDRawData& tfRawData = data->FEDData(FEDNumbering::MINCSCTFFEDID);
322  tfRawData.resize((end-start)*sizeof(unsigned long long));
323  std::copy((unsigned char*)start,(unsigned char*)end,tfRawData.data());
324 
325  // Create a new buffer from everything before and after TF DDU
326  unsigned short *event = tmpBuf;
327  memcpy(event,fuEvent[readyToGo],((unsigned short*)start-fuEvent[readyToGo])*sizeof(unsigned short));
328  event += ((unsigned short*)start-fuEvent[readyToGo]);
329  memcpy(event,end,(fuEvent[readyToGo]+fuEventSize[readyToGo]-(unsigned short*)end)*sizeof(unsigned short));
330  event += fuEvent[readyToGo]+fuEventSize[readyToGo]-(unsigned short*)end;
331  FEDRawData& fedRawData = data->FEDData(FEDNumbering::MINCSCFEDID);
332  fedRawData.resize((fuEventSize[readyToGo]-((unsigned short*)end-(unsigned short*)start))*sizeof(unsigned short));
333  std::copy((unsigned char*)tmpBuf,(unsigned char*)event,fedRawData.data());
334  } else {
335  FEDRawData& fedRawData = data->FEDData(FEDNumbering::MINCSCFEDID);
336  fedRawData.resize((fuEventSize[readyToGo])*sizeof(unsigned short));
337  std::copy((unsigned char*)fuEvent[readyToGo],(unsigned char*)(fuEvent[readyToGo]+fuEventSize[readyToGo]),fedRawData.data());
338  }
339 
340  currentL1A[readyToGo] = readFU(readyToGo,fuEvent[readyToGo],fuEventSize[readyToGo]);
341 
342  return eventNumber;
343 }
344 
346  data = new FEDRawDataCollection();
347 
348  int runNumber = 0; // Unknown at the level of EMu local DAQ
349  int eventNumber =-1; // Will determine below
350 
351  if( !nActiveFUs && nActiveRUIs ){
352  eventNumber = buildEventFromRUIs(data);
353  } else {
354  eventNumber = nextEventFromFUs(data);
355  }
356 
357  if( eventNumber<0 ) return false;
358 
359  eID = edm::EventID(runNumber,1U,eventNumber);
360 
361  return true;
362 }
363 
364 #undef nRUIs
365 #undef nFUs
#define LogDebug(id)
T getUntrackedParameter(std::string const &, T const &) const
const unsigned short * fuEvent[4]
Definition: CSCFileReader.h:25
int open(const char *filename)
tuple start
Check for commandline option errors.
Definition: dqm_diff.py:58
virtual ~CSCFileReader(void)
std::vector< std::string > fileNames[40]
Definition: CSCFileReader.h:17
dictionary map
Definition: Association.py:205
std::map< unsigned int, std::list< unsigned int > > FED
Definition: CSCFileReader.h:33
#define nRUIs
int fillRawData(edm::EventID &eID, edm::Timestamp &tstamp, FEDRawDataCollection *&data)
size_t ruBufSize[40]
Definition: CSCFileReader.h:28
#define nFUs
const FEDRawData & FEDData(int fedid) const
retrieve data for fed
void resize(size_t newsize)
Definition: FEDRawData.cc:33
int buildEventFromRUIs(FEDRawDataCollection *data)
string unit
Definition: csvLumiCalc.py:46
void select(unsigned int criteria)
Definition: FileReaderDDU.h:28
void select(unsigned int criteria)
Definition: FileReaderDCC.h:28
std::vector< std::string >::const_iterator currentFile[40]
Definition: CSCFileReader.h:18
size_t next(const unsigned short *&buf)
size_t fuEventSize[4]
Definition: CSCFileReader.h:26
#define end
Definition: vmac.h:38
int readRUI(int rui, const unsigned short *&buf, size_t &length)
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger but the state exists so we define the behavior If all triggers are the negative crieriion will lead to accepting the event(this again matches the behavior of"!*"before the partial wildcard feature was incorporated).The per-event"cost"of each negative criterion with multiple relevant triggers is about the same as!*was in the past
FileReaderDCC FU[4]
Definition: CSCFileReader.h:31
void reject(unsigned int criteria)
Definition: FileReaderDDU.h:30
size_t next(const unsigned short *&buf, int prescaling=1)
unsigned short * tmpBuf
Definition: CSCFileReader.h:24
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:29
int readFU(int fu, const unsigned short *&buf, size_t &length)
void reject(unsigned int criteria)
Definition: FileReaderDCC.h:30
int currentL1A[40]
Definition: CSCFileReader.h:21
int nextEventFromFUs(FEDRawDataCollection *data)
FileReaderDDU RUI[40]
Definition: CSCFileReader.h:30
int open(const char *filename)
const unsigned short * ruBuf[40]
Definition: CSCFileReader.h:27
CSCFileReader(const edm::ParameterSet &pset)