CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PixelSLinkDataInputSource.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: SiPixelInputSources
4 // Class: PixelSLinkDataInputSource
5 //
13 //
14 // Original Author: Freya Blekman
15 // Created: Fri Sep 7 15:46:34 CEST 2007
16 // $Id: PixelSLinkDataInputSource.cc,v 1.21 2012/11/19 20:22:07 wmtan Exp $
17 //
18 //
19 
20 #include <memory>
29 #include <iostream>
30 
31 using namespace edm;
32 
33 // function to get the trigger number from fill words
34 int PixelSLinkDataInputSource::getEventNumberFromFillWords(std::vector<uint64_t> buffer, uint32_t & totword ){
35  // buffer validity, should already be pretty clean as this is exactly what goes into the FEDRawDataobject.
36 
37  // code copied directly from A. Ryd's fill word checker in PixelFEDInterface::PwordSlink64
38 
39  int fif2cnt=0;
40  int dumcnt=0;
41  int gapcnt=0;
42  uint32_t gap[9];
43  uint32_t dum[9];
44  uint32_t word[2]={0,0};
45  uint32_t chan=0;
46  uint32_t roc=0;
47 
48  const uint32_t rocmsk = 0x3e00000;
49  const uint32_t chnlmsk = 0xfc000000;
50 
51  for(int jk=1;jk<9;jk++)gap[jk]=0;
52  for(int jk=1;jk<9;jk++)dum[jk]=0;
53 
54  int fifcnt=1;
55  for(size_t kk=0; kk<buffer.size(); ++kk)
56  {
57 
58  word[0] = (uint32_t) buffer[kk];
59  word[1] = (uint32_t) (buffer[kk]>>32);
60 
61  for(size_t iw=0; iw<2; iw++)
62  {
63  chan= ((word[iw]&chnlmsk)>>26);
64  roc= ((word[iw]&rocmsk)>>21);
65 
66  //count non-error words
67  if(roc<25){
68  if((chan>4)&&(chan<10)&&(fifcnt!=2)) {fif2cnt=0;fifcnt=2;}
69  if((chan>9)&&(chan<14)&&(fifcnt!=3)) {fif2cnt=0;fifcnt=3;}
70  if((chan>13)&&(chan<19)&&(fifcnt!=4)){fif2cnt=0;fifcnt=4;}
71  if((chan>18)&&(chan<23)&&(fifcnt!=5)){fif2cnt=0;fifcnt=5;}
72  if((chan>22)&&(chan<28)&&(fifcnt!=6)){fif2cnt=0;fifcnt=6;}
73  if((chan>27)&&(chan<32)&&(fifcnt!=7)){fif2cnt=0;fifcnt=7;}
74  if((chan>31)&&(fifcnt!=8)){fif2cnt=0;fifcnt=8;}
75  fif2cnt++;
76  }
77  if(roc==26){gap[fifcnt]=(0x1000+(word[iw]&0xff));gapcnt++;}
78 
79  if((roc==27)&&((fif2cnt+dumcnt)<6)){dumcnt++;dum[fifcnt]=(0x1000+(word[iw]&0xff));}
80  else if((roc==27)&&((fif2cnt+dumcnt)>6)){dumcnt=1;fif2cnt=0;fifcnt++;}
81  }
82 
83  //word check complete
84  if(((fif2cnt+dumcnt)==6)&&(dumcnt>0)) //done with this fifo
85  {dumcnt=0;fif2cnt=0;fifcnt++;}
86  if((gapcnt>0)&&((dumcnt+fif2cnt)>5))//done with this fifo
87  {gapcnt=0;fifcnt++;fif2cnt=0;dumcnt=0;}
88  else if((gapcnt>0)&&((dumcnt+fif2cnt)<6)) gapcnt=0;
89 
90  }//end of fifo-3 word loop-see what we got!
91 
92  int status=0;
93 
94  if(gap[1]>0) {totword=(gap[1]&0xff);status=1;}
95  else if(gap[2]>0){totword=(gap[2]&0xff);status=1;}
96  else if(dum[1]>0){totword=(dum[1]&0xff);status=1;}
97  else if(dum[2]>0){totword=(dum[2]&0xff);status=1;}
98 
99  if(gap[3]>0) {totword=totword|((gap[3]&0xff)<<8);status=status|0x2;}
100  else if(gap[4]>0){totword=totword|((gap[4]&0xff)<<8);status=status|0x2;}
101  else if(dum[3]>0){totword=totword|((dum[3]&0xff)<<8);status=status|0x2;}
102  else if(dum[4]>0){totword=totword|((dum[4]&0xff)<<8);status=status|0x2;}
103 
104  if(gap[5]>0) {totword=totword|((gap[5]&0xff)<<16);status=status|0x4;}
105  else if(gap[6]>0){totword=totword|((gap[6]&0xff)<<16);status=status|0x4;}
106  else if(dum[5]>0){totword=totword|((dum[5]&0xff)<<16);status=status|0x4;}
107  else if(dum[6]>0){totword=totword|((dum[6]&0xff)<<16);status=status|0x4;}
108 
109  if(gap[7]>0){totword=totword|((gap[7]&0xff)<<24);status=status|0x8;}
110  else if(gap[8]>0){totword=totword|((gap[8]&0xff)<<24);status=status|0x8;}
111  else if(dum[7]>0){totword=totword|((dum[7]&0xff)<<24);status=status|0x8;}
112  else if(dum[8]>0){totword=totword|((dum[8]&0xff)<<24);status=status|0x8;}
113  return(status);
114 
115 }
116 
117 // constructor
119  const edm::InputSourceDescription& desc) :
120  ProducerSourceFromFiles(pset,desc,true),
121  m_fedid(pset.getUntrackedParameter<int>("fedid")),
122  m_fileindex(0),
123  m_runnumber(pset.getUntrackedParameter<int>("runNumber",-1)),
124  m_currenteventnumber(0),
125  m_currenttriggernumber(0),
126  m_eventnumber_shift(0)
127 {
128  produces<FEDRawDataCollection>();
129 
130  if (m_fileindex>=fileNames().size()) {
131  edm::LogInfo("") << "no more file to read " << std::endl;
132  return;// ???
133  }
134  std::string currentfilename = fileNames()[m_fileindex];
135  edm::LogInfo("") << "now examining file "<< currentfilename ;
136  m_fileindex++;
137  // reading both castor and other ('normal'/dcap) files.
138  IOOffset size = -1;
140 
141  edm::LogInfo("PixelSLinkDataInputSource") << " unsigned long int size = " << sizeof(unsigned long int) <<"\n unsigned long size = " << sizeof(unsigned long)<<"\n unsigned long long size = " << sizeof(unsigned long long) << "\n uint32_t size = " << sizeof(uint32_t) << "\n uint64_t size = " << sizeof(uint64_t) << std::endl;
142 
143  bool exists = StorageFactory::get() -> check(currentfilename.c_str(), &size);
144 
145  edm::LogInfo("PixelSLinkDataInputSource") << "file size " << size << std::endl;
146 
147  if(!exists){
148  edm::LogInfo("") << "file " << currentfilename << " cannot be found.";
149  return;
150  }
151  // now open the file stream:
152  storage.reset(StorageFactory::get()->open(currentfilename.c_str()));
153  // (throw if storage is 0)
154 
155  // check run number by opening up data file...
156 
157  Storage & temp_file = *storage;
158  // IOSize n =
159  temp_file.read((char*)&m_data,8);
160  if((m_data >> 60) != 0x5){
161  uint32_t runnum = m_data;
162  if(m_runnumber!=-1)
163  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 << ")";
164  m_runnumber=runnum;
165  }
166  temp_file.read((char*)&m_data,8);
167  m_currenteventnumber = (m_data >> 32)&0x00ffffff ;
168 }
169 
170 // destructor
172 
173 
174 }
175 
177  Storage & m_file = *storage;
178 
179  // create product (raw data)
180  buffers.reset( new FEDRawDataCollection );
181 
182  // uint32_t currenteventnumber = (m_data >> 32)&0x00ffffff;
183  uint32_t eventnumber =(m_data >> 32)&0x00ffffff ;
184 
185  do{
186  std::vector<uint64_t> buffer;
187 
188 
189 
190  uint16_t count=0;
191  eventnumber = (m_data >> 32)&0x00ffffff ;
192  if(m_currenteventnumber==0)
193  m_currenteventnumber=eventnumber;
194  edm::LogInfo("PixelSLinkDataInputSource::produce()") << "**** event number = " << eventnumber << " global event number " << m_currenteventnumber << " data " << std::hex << m_data << std::dec << std::endl;
195  while ((m_data >> 60) != 0x5){
196  // std::cout << std::hex << m_data << std::dec << std::endl;
197  if (count==0){
198  edm::LogWarning("") << "DATA CORRUPTION!" ;
199  edm::LogWarning("") << "Expected to find header, but read: 0x"
200  << std::hex<<m_data<<std::dec ;
201  }
202 
203  count++;
204  int n=m_file.read((char*)&m_data,8);
205  edm::LogWarning("") << "next data " << std::hex << m_data << std::dec << std::endl;
206 
207  if (n!=8) {
208  edm::LogInfo("") << "End of input file" ;
209  return false;
210  }
211  }
212 
213 
214  if (count>0) {
215  edm::LogWarning("")<<"Had to read "<<count<<" words before finding header!"<<std::endl;
216  }
217 
218  if (m_fedid>-1) {
219  m_data=(m_data&0xfffffffffff000ffLL)|((m_fedid&0xfff)<<8);
220  }
221 
222  uint16_t fed_id=(m_data>>8)&0xfff;
223  // std::cout << "fed id = " << fed_id << std::endl;
224  buffer.push_back(m_data);
225 
226  do{
227  m_file.read((char*)&m_data,8);
228  buffer.push_back(m_data);
229  }
230  while((m_data >> 60) != 0xa);
231  // std::cout << "read " << buffer.size() << " long words" << std::endl;
232 
233  std::auto_ptr<FEDRawData> rawData(new FEDRawData(8*buffer.size()));
234  // FEDRawData * rawData = new FEDRawData(8*buffer.size());
235  unsigned char* dataptr=rawData->data();
236 
237  for (uint16_t i=0;i<buffer.size();i++){
238  ((uint64_t *)dataptr)[i]=buffer[i];
239  }
240  uint32_t thetriggernumber=0;
241  int nfillwords = 0;//getEventNumberFromFillWords(buffer,thetriggernumber);
242 
243  if(nfillwords>0){
244  LogInfo("") << "n fill words = " << nfillwords << ", trigger numbers: " << thetriggernumber << "," << m_currenttriggernumber << std::endl;
245  m_eventnumber_shift = thetriggernumber - m_currenttriggernumber;
246  }
247  m_currenttriggernumber = thetriggernumber;
248  FEDRawData& fedRawData = buffers->FEDData( fed_id );
249  fedRawData=*rawData;
250 
251  // read the first data member of the next blob to check on event number
252  int n =m_file.read((char*)&m_data,8);
253  if (n==0) {
254  edm::LogInfo("") << "End of input file" ;
255  }
256  m_currenteventnumber = (m_data >> 32)&0x00ffffff ;
257  if(m_currenteventnumber<eventnumber)
258  LogError("PixelSLinkDataInputSource") << " error, the previous event number (" << eventnumber << ") is LARGER than the next event number (" << m_currenteventnumber << ")" << std::endl;
259 
260  }
261  while( eventnumber == m_currenteventnumber);
262 
263  uint32_t realeventno = synchronizeEvents();
264  if(m_runnumber!=0)
265  id = edm::EventID(m_runnumber, id.luminosityBlock(), realeventno);
266  else
267  id = edm::EventID(id.run(), id.luminosityBlock(), realeventno);
268  return true;
269 }
270 
271 // produce() method. This is the worker method that is called every event.
273  event.put(buffers);
274  buffers.reset();
275 }
276 
277 // this function sets the m_globaleventnumber quantity. It uses the m_currenteventnumber and m_currenttriggernumber values as input
279  int32_t result= m_currenteventnumber -1;
280 
281  return(uint32_t) result;
282 }
int i
Definition: DBlmapReader.cc:9
bool enableAccounting(bool enabled)
virtual IOSize read(void *into, IOSize n, IOOffset pos)
Definition: Storage.cc:17
RunNumber_t run() const
virtual void produce(edm::Event &event)
std::vector< std::string > const & fileNames() const
Definition: FromFiles.h:22
static StorageFactory * get(void)
int getEventNumberFromFillWords(std::vector< uint64_t > data, uint32_t &totword)
PixelSLinkDataInputSource(const edm::ParameterSet &pset, const edm::InputSourceDescription &desc)
bool check(const DataFrame &df, bool capcheck, bool dvercheck)
tuple result
Definition: query.py:137
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
unsigned long long TimeValue_t
Definition: Timestamp.h:27
unsigned long long uint64_t
Definition: Time.h:15
int64_t IOOffset
Definition: IOTypes.h:19
std::auto_ptr< FEDRawDataCollection > buffers
LuminosityBlockNumber_t luminosityBlock() const
tuple runnum
Definition: summaryLumi.py:210
tuple status
Definition: ntuplemaker.py:245
std::unique_ptr< Storage > storage
virtual bool setRunAndEventInfo(edm::EventID &id, edm::TimeValue_t &time)
tuple size
Write out results.