1 |
#ifndef PAMVMCRAWMGR_H |
2 |
#define PAMVMCRAWMGR_H |
3 |
#include "CRC.h" |
4 |
#include <iostream> |
5 |
#include <fstream> |
6 |
#include <vector> |
7 |
#include <TObject.h> |
8 |
#include "TString.h" |
9 |
|
10 |
using namespace std; |
11 |
|
12 |
typedef vector<char> PamVMCBuffer; |
13 |
typedef vector<UChar_t> UCBuffer; |
14 |
typedef vector<UShort_t> USBuffer; |
15 |
|
16 |
class PamVMCRawMgr: public TObject { |
17 |
|
18 |
private: |
19 |
|
20 |
static PamVMCRawMgr * frm; |
21 |
|
22 |
UInt_t fCounter; |
23 |
UInt_t fCounterPhys; |
24 |
UInt_t fOBT; |
25 |
|
26 |
TString fFilename; |
27 |
std::ofstream fFile; |
28 |
|
29 |
|
30 |
UInt_t fLen; |
31 |
|
32 |
UCBuffer fEvtDataPSCU; //event |
33 |
UCBuffer fEvtDataPadding; |
34 |
UInt_t fEvtPadding; |
35 |
|
36 |
//++++++++++// |
37 |
// BUFFER // |
38 |
//++++++++++// |
39 |
|
40 |
PamVMCBuffer* fbuffer; |
41 |
|
42 |
protected: |
43 |
PamVMCRawMgr(const char *filename) { |
44 |
fFilename=TString(filename)+".pam"; |
45 |
//fFilename = filename; //to be redefined in options |
46 |
fCounter=fCounterPhys=fOBT=0; |
47 |
fbuffer = new PamVMCBuffer(0); |
48 |
fFile.open(fFilename.Data(),ios::out | ios::binary); |
49 |
}; |
50 |
|
51 |
public: |
52 |
|
53 |
~PamVMCRawMgr(){ |
54 |
|
55 |
delete fbuffer; |
56 |
//delete fFilename; |
57 |
|
58 |
} |
59 |
|
60 |
static PamVMCRawMgr * Instance(const char *filename = 0); |
61 |
|
62 |
UInt_t GetCounter(){ return fCounter; } |
63 |
|
64 |
void AddCounter(){ fCounter++; } |
65 |
|
66 |
void SetCounter(UInt_t count){ fCounter = count; } |
67 |
|
68 |
void AddCounterPhys(){ fCounterPhys++; } |
69 |
|
70 |
UInt_t GetCounterPhys(){ return fCounterPhys; } |
71 |
|
72 |
void SetCounterPhys(UInt_t phcount){ fCounterPhys = phcount; } |
73 |
|
74 |
UInt_t GetOBT(){ return fOBT; } |
75 |
|
76 |
void SetOBT(UInt_t obt){ fOBT = obt; } |
77 |
|
78 |
UInt_t GetLen(){ return fLen; } |
79 |
|
80 |
void SetLen(UInt_t len){ fLen = len; } |
81 |
|
82 |
void WritePSCU(const UCBuffer* b){ CopyUCharToBuff(b); } |
83 |
|
84 |
void WritePadding(const UCBuffer* b){ CopyUCharToBuff(b); } |
85 |
|
86 |
void WriteEvent(){ |
87 |
fEvtPadding = 0; |
88 |
//fLen=fbuffer->size(); |
89 |
//AddPadding(fEvtPadding,&fEvtDataPadding); |
90 |
UInt_t length = fbuffer->size()+fEvtPadding; // DATA+Padding size; |
91 |
DigitizePSCU(length, 0x10, &fEvtDataPSCU); |
92 |
|
93 |
//cout<<"PADDING "<<fEvtPadding<<" bytes"<<endl; |
94 |
UCBuffer::const_iterator p = fEvtDataPSCU.begin(); |
95 |
while( p!=fEvtDataPSCU.end() ) |
96 |
{ |
97 |
char t = char(*p); |
98 |
fFile.write(&t,sizeof(char)); |
99 |
p++; |
100 |
} |
101 |
fEvtDataPSCU.clear(); |
102 |
|
103 |
WriteToFile(); //WRITE BUFFER CONTENT TO FILE |
104 |
|
105 |
if(fEvtPadding){ |
106 |
UCBuffer::const_iterator p = fEvtDataPadding.begin(); |
107 |
while( p!=fEvtDataPadding.end() ) |
108 |
{ |
109 |
char t = char(*p); |
110 |
fFile.write(&t,sizeof(char)); |
111 |
p++; |
112 |
} |
113 |
fEvtDataPadding.clear(); |
114 |
} |
115 |
} |
116 |
|
117 |
void WriteToFile(){ |
118 |
cout<<"BUFFER SIZE: "<<fbuffer->size()<<endl; |
119 |
PamVMCBuffer::const_iterator p = fbuffer->begin(); |
120 |
while( p!=fbuffer->end() ) |
121 |
{ |
122 |
fFile.write(&(*p),sizeof(char)); |
123 |
p++; |
124 |
} |
125 |
fbuffer->clear(); //cleaning MAIN buffer |
126 |
} |
127 |
|
128 |
void FinishRun(){ |
129 |
WriteToFile(); |
130 |
Int_t end = EOF; |
131 |
char t = char(end); |
132 |
fFile.write(&t, sizeof(char)); |
133 |
fFile.close(); |
134 |
} |
135 |
|
136 |
void CopyUCharToBuff (const UCBuffer * b){ |
137 |
UCBuffer::const_iterator p = b->begin(); |
138 |
cout<<"size of UChar DATA:"<<b->size()<<endl; |
139 |
while( p!=b->end() ) |
140 |
{ |
141 |
//cout<<Int_t(*p)<<endl; |
142 |
fbuffer->push_back(char(*p)); |
143 |
p++; |
144 |
} |
145 |
} |
146 |
|
147 |
|
148 |
void CopyUShortToBuff (const USBuffer * b){ |
149 |
USBuffer::const_iterator p = b->begin(); |
150 |
cout<<"size of UShort DATA:"<<b->size()<<endl; |
151 |
UShort_t Data; |
152 |
UChar_t tmp[2]; |
153 |
while( p!=b->end() ) |
154 |
{ |
155 |
Data=(*p); |
156 |
memcpy(tmp,&Data,sizeof(UShort_t)); |
157 |
//cout<<Int_t(*p)<<" "<<hex<<tmp[1]<<" "<<hex<<tmp[0]<<endl; |
158 |
fbuffer->push_back(char(tmp[1])); |
159 |
fbuffer->push_back(char(tmp[0])); |
160 |
p++; |
161 |
} |
162 |
} |
163 |
|
164 |
|
165 |
void AddPadding(UInt_t & pad, UCBuffer * b){ |
166 |
|
167 |
Float_t pd0 = (fLen+16)/32.; |
168 |
Float_t pd1 = pd0 - (Float_t)Int_t(pd0); |
169 |
Float_t padfrac = 32. - pd1 * 32.; |
170 |
|
171 |
UInt_t padbytes = (UInt_t)padfrac; |
172 |
b->clear(); |
173 |
if ( padbytes > 0 && padbytes < 32 ){ |
174 |
// |
175 |
// here the padding length |
176 |
// |
177 |
pad = padbytes+32; |
178 |
// |
179 |
// random padding bytes |
180 |
// |
181 |
UShort_t Data; |
182 |
UChar_t tmp[2]; |
183 |
for (Int_t ur=0; ur<16; ur++){ |
184 |
Data=(UShort_t)rand(); |
185 |
memcpy(tmp,&Data,sizeof(UShort_t)); |
186 |
//cout<<"DATA"<<hex<<Data<<endl; |
187 |
//tmp[1] should be first (swapping bytes) |
188 |
//b->push_back(tmp[1]); |
189 |
//b->push_back(tmp[0]); |
190 |
}; |
191 |
}; |
192 |
|
193 |
} |
194 |
|
195 |
|
196 |
void DigitizePSCU(UInt_t length, UChar_t type, UCBuffer * b){ |
197 |
// |
198 |
UChar_t buff[16]; |
199 |
// |
200 |
// CPU signature |
201 |
// |
202 |
buff[0] = 0xFA; |
203 |
buff[1] = 0xFE; |
204 |
buff[2] = 0xDE; |
205 |
// |
206 |
// packet type (twice) |
207 |
// |
208 |
buff[3] = type; |
209 |
buff[4] = type; |
210 |
// |
211 |
// counter |
212 |
// |
213 |
fCounter++; |
214 |
while ( fCounter > 16777215 ){ |
215 |
fCounter-=16777215; |
216 |
}; |
217 |
// |
218 |
buff[5] = (UChar_t)(fCounter >> 16); |
219 |
buff[6] = (UChar_t)(fCounter >> 8); |
220 |
buff[7] = (UChar_t)fCounter; |
221 |
// |
222 |
// on board time |
223 |
// |
224 |
ULong64_t obt = fOBT + 30LL; |
225 |
// |
226 |
while ( obt > 4294967295LL ){ |
227 |
obt -= 4294967295LL; |
228 |
}; |
229 |
fOBT = UInt_t(obt); |
230 |
// |
231 |
buff[8] = (UChar_t)(fOBT >> 24); |
232 |
buff[9] = (UChar_t)(fOBT >> 16); |
233 |
buff[10] = (UChar_t)(fOBT >> 8); |
234 |
buff[11] = (UChar_t)fOBT; |
235 |
// |
236 |
// Packet length |
237 |
// |
238 |
fLen = length; |
239 |
// |
240 |
buff[12] = (UChar_t)(fLen >> 16); |
241 |
buff[13] = (UChar_t)(fLen >> 8) ; |
242 |
buff[14] = (UChar_t)fLen; |
243 |
// |
244 |
// CPU header CRC |
245 |
// |
246 |
buff[15] = (BYTE)CM_Compute_CRC16((UINT16)0, (BYTE*)&buff, (UINT32)15); |
247 |
|
248 |
cout<<"Digitizer::DigitizePSCU... OK " <<endl; |
249 |
|
250 |
b->clear(); |
251 |
|
252 |
for (Int_t i=0; i<16; i++) b->push_back(buff[i]); |
253 |
} |
254 |
|
255 |
|
256 |
void PrintBinaryUns(unsigned char val){ |
257 |
for(Int_t i =7; i>=0; i--){ |
258 |
if(val & (1<<i)) |
259 |
cout<<"1"; |
260 |
else |
261 |
cout<<"0"; |
262 |
}; |
263 |
cout<<endl; |
264 |
} |
265 |
|
266 |
|
267 |
}; |
268 |
#endif |