CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
BUEvent.cc
Go to the documentation of this file.
1 //
3 // BUEvent
4 // -------
5 //
6 // 03/26/2007 Philipp Schieferdecker <philipp.schieferdecker@cern.ch>
8 
9 
11 #include <assert.h>
13 
14 #include "interface/shared/fed_header.h"
15 #include "interface/shared/fed_trailer.h"
16 
17 #include <iostream>
18 #include <iomanip>
19 #include <fstream>
20 #include <sstream>
21 #include <cstring>
22 
23 using namespace std;
24 using namespace evf;
25 
26 
27 
29 // initialize static member data
31 
32 //______________________________________________________________________________
33 bool BUEvent::computeCrc_=true;
34 
35 
37 // construction/destruction
39 
40 //______________________________________________________________________________
41 BUEvent::BUEvent(unsigned int buResourceId,unsigned int bufferSize)
42  : buResourceId_(buResourceId)
43  , evtNumber_(0xffffffff)
44  , evtSize_(0)
45  , bufferSize_(bufferSize)
46  , nFed_(0)
47  , fedId_(0)
48  , fedPos_(0)
49  , fedSize_(0)
50  , buffer_(0)
51 {
52  fedId_ = new unsigned int[1024];
53  fedPos_ = new unsigned int[1024];
54  fedSize_= new unsigned int[1024];
55  buffer_ = new unsigned char[bufferSize];
56 }
57 
58 
59 //______________________________________________________________________________
61 {
62  if (0!=fedId_) delete [] fedId_;
63  if (0!=fedPos_) delete [] fedPos_;
64  if (0!=fedSize_) delete [] fedSize_;
65  if (0!=buffer_) delete [] buffer_;
66 }
67 
68 
70 // implementation of member functions
72 
73 //______________________________________________________________________________
74 void BUEvent::initialize(unsigned int evtNumber)
75  {
76  evtNumber_=evtNumber & 0xFFFFFF; // 24 bits only available in the FED headers
77  evtSize_=0;
78  nFed_=0;
79  }
80 
81 
82 //______________________________________________________________________________
83 bool BUEvent::writeFed(unsigned int id,unsigned char* data,unsigned int size)
84 {
85  if (evtSize_+size > bufferSize_) {
86  cout<<"BUEvent::writeFed() ERROR: buffer overflow."<<endl;
87  return false;
88  }
89 
90  if (nFed_==1024) {
91  cout<<"BUEvent::writeFed() ERROR: too many feds (max=1024)."<<endl;
92  return false;
93  }
94 
95  fedId_[nFed_] =id;
98  if (0!=data) memcpy(fedAddr(nFed_),data,size);
99  ++nFed_;
100  evtSize_+=size;
101  return true;
102 }
103 
104 
105 //______________________________________________________________________________
106 bool BUEvent::writeFedHeader(unsigned int i)
107 {
108  if (i>=nFed_) {
109  cout<<"BUEvent::writeFedHeader() ERROR: invalid fed index '"<<i<<"'."<<endl;
110  return false;
111  }
112 
113  fedh_t *fedHeader=(fedh_t*)fedAddr(i);
114  fedHeader->eventid =evtNumber();
115  fedHeader->eventid|=0x50000000;
116  fedHeader->sourceid=(fedId(i) << 8) & FED_SOID_MASK;
117 
118  return true;
119 }
120 
121 
122 //______________________________________________________________________________
123 bool BUEvent::writeFedTrailer(unsigned int i)
124 {
125  if (i>=nFed_) {
126  cout<<"BUEvent::writeFedTrailer() ERROR: invalid fed index '"<<i<<"'."<<endl;
127  return false;
128  }
129 
130  fedt_t *fedTrailer=(fedt_t*)(fedAddr(i)+fedSize(i)-sizeof(fedt_t));
131  fedTrailer->eventsize =fedSize(i);
132  fedTrailer->eventsize/=8; //wc in fed trailer in 64bit words
133  fedTrailer->eventsize|=0xa0000000;
134  fedTrailer->conscheck =0x0;
135 
136  if (BUEvent::computeCrc()) {
137  unsigned short crc=evf::compute_crc(fedAddr(i),fedSize(i));
138  fedTrailer->conscheck=(crc<<FED_CRCS_SHIFT);
139  }
140 
141  return true;
142 }
143 
144 
145 //______________________________________________________________________________
146 unsigned char* BUEvent::fedAddr(unsigned int i) const
147 {
148  return (buffer_+fedPos_[i]);
149 }
150 
151 
152 //________________________________________________________________________________
154 {
155  ostringstream oss; oss<<"/tmp/autobu_evt"<<evtNumber()<<".dump";
156  ofstream fout(oss.str().c_str());
157  fout.fill('0');
158 
159  fout<<"#\n# evt "<<evtNumber()<<"\n#\n"<<endl;
160  for (unsigned int i=0;i<nFed();i++) {
161  if (fedSize(i)==0) continue;
162  fout<<"# fedid "<<fedId(i)<<endl;
163  unsigned char* addr=fedAddr(i);
164  for (unsigned int j=0;j<fedSize(i);j++) {
165  fout<<setiosflags(ios::right)<<setw(2)<<hex<<(int)(*addr)<<dec;
166  if ((j+1)%8) fout<<" "; else fout<<endl;
167  ++addr;
168  }
169  fout<<endl;
170  }
171  fout.close();
172 }
int i
Definition: DBlmapReader.cc:9
void initialize(unsigned int evtNumber)
Definition: BUEvent.cc:74
unsigned char * buffer_
Definition: BUEvent.h:54
struct fedt_struct fedt_t
unsigned int evtNumber() const
Definition: BUEvent.h:28
unsigned int evtNumber_
Definition: BUEvent.h:47
bool writeFed(unsigned int id, unsigned char *data, unsigned int size)
Definition: BUEvent.cc:83
unsigned int * fedId_
Definition: BUEvent.h:51
bool writeFedHeader(unsigned int i)
Definition: BUEvent.cc:106
unsigned int sourceid
Definition: fed_header.h:32
unsigned int conscheck
Definition: fed_trailer.h:32
unsigned char * fedAddr(unsigned int i) const
Definition: BUEvent.cc:146
unsigned int nFed_
Definition: BUEvent.h:50
unsigned short compute_crc(unsigned char *buffer, unsigned int bufSize)
Definition: CRC16.h:67
int j
Definition: DBlmapReader.cc:9
unsigned int fedId(unsigned int i) const
Definition: BUEvent.h:32
#define FED_SOID_MASK
Definition: fed_header.h:42
unsigned int bufferSize_
Definition: BUEvent.h:49
static bool computeCrc()
Definition: BUEvent.h:36
bool writeFedTrailer(unsigned int i)
Definition: BUEvent.cc:123
#define FED_CRCS_SHIFT
Definition: fed_trailer.h:51
unsigned int * fedPos_
Definition: BUEvent.h:52
unsigned int * fedSize_
Definition: BUEvent.h:53
unsigned int eventid
Definition: fed_header.h:33
unsigned int fedSize(unsigned int i) const
Definition: BUEvent.h:33
unsigned int eventsize
Definition: fed_trailer.h:33
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
virtual ~BUEvent()
Definition: BUEvent.cc:60
tuple cout
Definition: gather_cfg.py:121
unsigned int nFed() const
Definition: BUEvent.h:31
unsigned int bufferSize() const
Definition: BUEvent.h:30
tuple size
Write out results.
unsigned int evtSize_
Definition: BUEvent.h:48
void dump()
Definition: BUEvent.cc:153