27 int rdsize = rawdata.
size() / 8;
31 int DCCid = TheMapping -> DCCid(ebdetid);
35 int nsamples = dataframe.
size();
40 "TowerBlockFormatter::DigiToRaw : Invalid iFE " << iFE << endl;
43 map<int, map<int,int> >::iterator fen =
FEDorder.find(FEDid);
44 map<int, map<int,int> >::iterator fed =
FEDmap.find(FEDid);
47 if (
debug_)
cout <<
"New FED in TowerBlockFormatter " <<
dec << FEDid <<
" 0x" << hex << FEDid << endl;
49 pair<map<int, map<int,int> >::iterator,
bool> t1 =
FEDorder.insert(
map<
int, map<int,int> >::
value_type(FEDid,FEorder));
51 pair<map<int, map<int,int> >::iterator,
bool> t2 =
FEDmap.insert(
map<
int, map<int,int> >::
value_type(FEDid,FEmap));
56 map<int, int>& FEorder = (*fen).second;
57 map<int, int>& FEmap = (*fed).second;
59 map<int,int>::iterator fe = FEorder.find(iFE);
62 if (fe != FEorder.end()) {
63 FE_order = (*fe).second;
64 map<int,int>::iterator
ff = FEmap.find(FE_order);
65 if (ff == FEmap.end())
cout <<
"Error with maps... " << endl;
66 FE_index = (*ff).second;
67 if (
debug_)
cout <<
"FE already there, FE_index = " <<
dec << FE_index <<
" FEorder " << FE_order << endl;
70 if (
debug_)
cout <<
"New FE in TowerBlockFormatter FE " <<
dec << iFE <<
" 0x" << hex << iFE <<
" in FED id " <<
dec << FEDid << endl;
72 int number_FEs = FEorder.size() -1;
73 FE_order = number_FEs+1;
75 if (! t2.second)
cout <<
" FE insertion failed...";
78 FE_index = (*fe).second;
79 if (
debug_)
cout <<
"Build the Tower Block header for FE id " << iFE <<
" start at line " << rdsize << endl;
80 if (
debug_)
cout <<
"This is the Fe number (order) " << number_FEs+1 << endl;
81 rawdata.
resize( 8*rdsize + 8);
82 unsigned char* pData = rawdata.
data();
83 pData[8*FE_index] = iFE & 0xFF;
84 pData[8*FE_index+1] = (nsamples & 0x7F);
85 pData[8*FE_index+2] = bx & 0xFF;
86 pData[8*FE_index+3] = (bx >>8) & 0x0F;
87 pData[8*FE_index+3] |= 0xa0;
88 pData[8*FE_index+4] = lv1 & 0xFF;
89 pData[8*FE_index+5] = (lv1 >>8) & 0x0F;
90 pData[8*FE_index+6] = 1;
91 pData[8*FE_index+7] = 0xc0;
100 int ichannel = elid.
xtalId();
102 if (
debug_)
cout <<
"Now add crystal : strip channel " <<
dec << istrip <<
" " << ichannel << endl;
105 unsigned char* pData = rawdata.
data();
107 vector<unsigned char> vv(&pData[0],&pData[rawdata.
size()]);
110 int n_add = 2 + 2*nsamples;
111 if (n_add % 8 != 0) n_add = n_add/8 +1;
114 if (
debug_)
cout <<
"will add " << n_add <<
" lines of 64 bits at line " << (FE_index+1) << endl;
116 unsigned char* ppData = rawdata.
data();
118 vector<unsigned char>::iterator iter = vv.begin() + 8*(FE_index+1);
120 vector<unsigned char> toadd(n_add*8);
124 toadd[0] = (istrip & 0x7) + ((ichannel & 0x7)<<4);
125 toadd[1] = (tzs & 0x1) <<12;
127 for (
int isample=0; isample < (n_add*8-2)/2; isample++) {
128 if (isample < nsamples) {
129 uint16_t word = (dataframe.
sample(isample)).raw();
130 toadd[2 + isample*2] = word & 0x00FF;
131 toadd[2 + isample*2 +1] = (word & 0xFF00)>>8;
134 toadd[2 + isample*2] = 0;
135 toadd[2 + isample*2 +1] = 0;
137 if (isample % 2 == 0) toadd[2 + isample*2 +1] |= 0xc0;
140 vv.insert(iter,toadd.begin(),toadd.end());
144 for (
int i=0;
i < (
int)vv.size();
i++) {
149 cout <<
"pData for this FED is now " << endl;
154 for (
int i=FE_order+1;
i < (
int)FEorder.size();
i++) {
156 if (
debug_)
cout <<
"FEmap updated for fe number " <<
dec <<
i << endl;
157 if (
debug_)
cout <<
" FEmap[" <<
i <<
"] = " << FEmap[
i] << endl;
161 int blocklength = ppData[8*FE_index+6]
162 + ((ppData[8*FE_index+7] & 0x1)<<8);
163 blocklength += n_add;
164 ppData[8*FE_index+6] = blocklength & 0xFF;
165 ppData[8*FE_index+7] |= (blocklength & 0x100)>>8;
179 if (
debug_)
cout <<
"enter in TowerBlockFormatter::EndEvent. First reorder the FE's. " << endl;
181 for (
int idcc=1; idcc <= 54; idcc++) {
189 FEDRawData& fedData = productRawData -> FEDData(FEDid);
190 if (fedData.
size() <= 16)
continue;
192 if (
debug_)
cout <<
"This is FEDid = " << FEDid << endl;
194 unsigned char * pData = fedData.
data();
198 int length = fedData.
size() / 8;
199 int iDAQ_header(-1), iDCC_header(-1), iTCCBlock_header(-1),
200 iSRBlock_header(-1), iTowerBlock_header(-1), iDAQ_trailer(-1);
202 for (
int i=length-1;
i > -1;
i--) {
203 if ( ( (words[
i] >> 60) & 0xF) == 0x5 ) iDAQ_header=
i;
204 if ( ( (words[
i] >> 62) & 0x3) == 0x0 ) iDCC_header=
i;
205 if ( ( (words[
i] >> 61) & 0x7) == 0x3 ) iTCCBlock_header=
i;
206 if ( ( (words[
i] >> 61) & 0x7) == 0x4 ) iSRBlock_header=
i;
207 if ( ( (words[
i] >> 62) & 0x3) == 0x3 ) iTowerBlock_header=
i;
208 if ( ( (words[
i] >> 60) & 0xF) == 0xA ) iDAQ_trailer=
i;
211 if (iTowerBlock_header < 0) iTowerBlock_header = iDAQ_trailer;
212 if (iSRBlock_header < 0) iSRBlock_header = iTowerBlock_header;
213 if (iTCCBlock_header < 0) iTCCBlock_header = iSRBlock_header;
216 cout <<
"iDAQ_header = " << iDAQ_header << endl;
217 cout <<
" iDCC_header = " << iDCC_header << endl;
218 cout <<
" iTCCBlock_header = " << iTCCBlock_header << endl;
219 cout <<
" iSRBlock_header = " << iSRBlock_header << endl;
220 cout <<
" iTowerBlock_header = " << iTowerBlock_header << endl;
221 cout <<
" iDAQ_trailer = " << iDAQ_trailer << endl;
224 std::map<int, int> FrontEnd;
225 std::map<int, std::vector<Word64> > Map_xtal_data;
227 int iTowerBlock_header_keep = iTowerBlock_header;
229 while (iTowerBlock_header < iDAQ_trailer) {
230 int fe = words[iTowerBlock_header] & 0xFF;
231 int nlines = (words[iTowerBlock_header] >> 48) & 0x1FF;
232 if (
debug_)
cout <<
"This is FE number " << fe <<
"needs nlines = " << nlines << endl;
233 FrontEnd[fe] = nlines;
234 std::vector<Word64> xtal_data;
235 for (
int j=0; j < nlines; j++) {
236 Word64 ww = words[iTowerBlock_header+j];
237 xtal_data.push_back(ww);
239 Map_xtal_data[fe] = xtal_data;
240 iTowerBlock_header += nlines;
244 cout <<
"vector of FrontEnd : " << FrontEnd.size() << endl;
245 for (std::map<int, int>::const_iterator it=FrontEnd.begin();
246 it != FrontEnd.end(); it++) {
247 int fe = it ->
first;
249 cout <<
"FE line " << fe <<
" " << l << endl;
253 iTowerBlock_header = iTowerBlock_header_keep;
254 for (std::map<int, int>::const_iterator it=FrontEnd.begin();
255 it != FrontEnd.end(); it++) {
256 int fe = it ->
first;
257 int nlines = it ->
second;
258 if (
debug_)
cout <<
"iTowerBlock_header = " << iTowerBlock_header << endl;
259 vector<Word64> xtal_data = Map_xtal_data[fe];
260 for (
int j=0; j < nlines; j++) {
261 words[iTowerBlock_header+j] = xtal_data[j];
262 if (
debug_)
cout <<
"update line " << iTowerBlock_header+j << endl;
265 int jFE = pData[8*(iTowerBlock_header)];
266 cout <<
"Front End on RD : " << jFE << endl;
268 iTowerBlock_header += nlines;
275 if (
debug_)
cout <<
"now reorder the xtals within the FEs" << endl;
277 iTowerBlock_header = iTowerBlock_header_keep;
279 for (std::map<int, int>::const_iterator it=FrontEnd.begin();
280 it != FrontEnd.end(); it++) {
282 int fe = it ->
first;
283 if (fe > 68)
cout <<
"Problem... fe = " << fe <<
" in FEDid = " << FEDid << endl;
284 if (
debug_)
cout <<
" This is for FE = " << fe << endl;
285 int nlines = it ->
second;
286 int timesamples = pData[8*iTowerBlock_header+1] & 0x7F;
287 int n4=timesamples-3;
289 if ( n4 % 4 != 0) n_lines4 ++;
290 if (n_lines4<0) n_lines4=0;
291 int Nxtal_max = (nlines-1)/(1+n_lines4);
294 map< int, map<int, vector<Word64> > > Strip_Map;
296 while (Nxtal < Nxtal_max) {
298 int i_xtal = iTowerBlock_header+1 + Nxtal*(1+n_lines4);
299 int strip = words[i_xtal] & 0x7;
300 int xtal = ( words[i_xtal] >>4) & 0x7;
302 map< int, map<int, vector<Word64> > >::iterator iit = Strip_Map.find(strip);
304 map<int, vector<Word64> > NewMap;
305 map<int, vector<Word64> > Xtal_Map;
307 if (iit == Strip_Map.end()) {
314 std::vector<Word64> xtal_data;
315 for (
int j=0; j < n_lines4 +1; j++) {
316 Word64 ww = words[i_xtal +j];
317 xtal_data.push_back(ww);
319 Xtal_Map[xtal] = xtal_data;
320 Strip_Map[
strip] = Xtal_Map;
328 for (
map<
int,
map<
int, vector<Word64> > >::const_iterator jt = Strip_Map.begin();
329 jt != Strip_Map.end(); jt++) {
332 if (
debug_)
cout <<
" this is strip number " << strip << endl;
333 map<int, vector<Word64> > Xtal_Map = jt ->
second;
335 for (
map<
int, vector<Word64> >::const_iterator kt = Xtal_Map.begin();
336 kt != Xtal_Map.end(); kt++) {
337 int xtal = kt ->
first;
338 if (
debug_)
cout <<
" this is xtal number " << xtal << endl;
339 vector<Word64> xtal_data = kt ->
second;
341 int mlines = (
int)xtal_data.size();
342 if (
debug_)
cout <<
" mlines = " << mlines << endl;
343 for (
int j=0; j < mlines; j++) {
344 int line = iTowerBlock_header+1+idx+j;
345 if (line >= iDAQ_trailer)
cout <<
"smth wrong... line " << line <<
" trailer " << iDAQ_trailer << endl;
346 words[
line] = xtal_data[j] ;
347 if (
debug_)
cout <<
" updated line " << iTowerBlock_header+idx+j << endl;
358 iTowerBlock_header += nlines;
362 if (
debug_)
cout <<
" DONE FOR FED " << FEDid << endl;
364 Map_xtal_data.clear();
394 int rdsize = rawdata.
size() / 8;
398 int DCCid = elid.
dccId();
402 if (
debug_)
cout <<
"enter in TowerBlockFormatter::DigiToRaw DCCid FEDid iFE " <<
403 dec << DCCid <<
" " << FEDid <<
" " << iFE << endl;
405 int nsamples = dataframe.
size();
407 if (iFE <= 0 || iFE > 68) {
408 cout <<
"invalid iFE for EndCap DCCid iFE " << DCCid <<
" " << iFE << endl;
413 map<int, map<int,int> >::iterator fen =
FEDorder.find(FEDid);
414 map<int, map<int,int> >::iterator fed =
FEDmap.find(FEDid);
417 if (
debug_)
cout <<
"New FED in TowerBlockFormatter " <<
dec << FEDid <<
" 0x" << hex << FEDid << endl;
418 map<int,int> FEorder;
419 pair<map<int, map<int,int> >::iterator,
bool> t1 =
FEDorder.insert(
map<
int, map<int,int> >::
value_type(FEDid,FEorder));
421 pair<map<int, map<int,int> >::iterator,
bool> t2 =
FEDmap.insert(
map<
int, map<int,int> >::
value_type(FEDid,FEmap));
426 map<int, int>& FEorder = (*fen).second;
427 map<int, int>& FEmap = (*fed).second;
429 map<int,int>::iterator fe = FEorder.find(iFE);
432 if (fe != FEorder.end()) {
433 FE_order = (*fe).second;
434 map<int,int>::iterator
ff = FEmap.find(FE_order);
435 if (ff == FEmap.end())
cout <<
"Error with maps... " << endl;
436 FE_index = (*ff).second;
437 if (
debug_)
cout <<
"FE already there, FE_index = " <<
dec << FE_index <<
" FEorder " << FE_order << endl;
440 if (
debug_)
cout <<
"New FE in TowerBlockFormatter FE " <<
dec << iFE <<
" 0x" << hex << iFE <<
" in FED id " <<
dec << FEDid << endl;
442 int number_FEs = FEorder.size() -1;
443 FE_order = number_FEs+1;
445 if (! t2.second)
cout <<
" FE insertion failed...";
448 FE_index = (*fe).second;
449 if (
debug_)
cout <<
"Build the Tower Block header for FE id " << iFE <<
" start at line " << rdsize << endl;
450 if (
debug_)
cout <<
"This is the Fe number (order) " << number_FEs+1 << endl;
451 rawdata.
resize( 8*rdsize + 8);
452 unsigned char* pData = rawdata.
data();
454 pData[8*FE_index] = iFE & 0xFF;
455 pData[8*FE_index+1] = (nsamples & 0x7F);
456 pData[8*FE_index+2] = bx & 0xFF;
457 pData[8*FE_index+3] = (bx >>8) & 0x0F;
458 pData[8*FE_index+3] |= 0xa0;
459 pData[8*FE_index+4] = lv1 & 0xFF;
460 pData[8*FE_index+5] = (lv1 >>8) & 0x0F;
461 pData[8*FE_index+6] = 1;
462 pData[8*FE_index+7] = 0xc0;
471 int ichannel = elid.
xtalId();
473 if (
debug_)
cout <<
"Now add crystal strip channel " <<
dec << istrip <<
" " << ichannel << endl;
475 unsigned char* pData = rawdata.
data();
477 vector<unsigned char> vv(&pData[0],&pData[rawdata.
size()]);
480 int n_add = 2 + 2*nsamples;
481 if (n_add % 8 != 0) n_add = n_add/8 +1;
484 if (
debug_)
cout <<
"nsamples = " <<
dec << nsamples << endl;
485 if (
debug_)
cout <<
"will add " << n_add <<
" lines of 64 bits at line " << (FE_index+1) << endl;
487 unsigned char* ppData = rawdata.
data();
489 vector<unsigned char>::iterator iter = vv.begin() + 8*(FE_index+1);
491 vector<unsigned char> toadd(n_add*8);
495 toadd[0] = (istrip & 0x7) + ((ichannel & 0x7)<<4);
496 toadd[1] = (tzs & 0x1) <<12;
498 for (
int isample=0; isample < (n_add*8-2)/2; isample++) {
499 if (isample < nsamples) {
500 uint16_t word = (dataframe.
sample(isample)).raw();
501 toadd[2 + isample*2] = word & 0x00FF;
502 toadd[2 + isample*2 +1] = (word & 0xFF00)>>8;
505 toadd[2 + isample*2] = 0;
506 toadd[2 + isample*2 +1] = 0;
508 if (isample % 2 == 0) toadd[2 + isample*2 +1] |= 0xc0;
511 vv.insert(iter,toadd.begin(),toadd.end());
515 for (
int i=0;
i < (
int)vv.size();
i++) {
520 cout <<
"pData for this FED is now " << endl;
525 for (
int i=FE_order+1;
i < (
int)FEorder.size();
i++) {
527 if (
debug_)
cout <<
"FEmap updated for fe number " <<
dec <<
i << endl;
528 if (
debug_)
cout <<
" FEmap[" <<
i <<
"] = " << FEmap[
i] << endl;
532 int blocklength = ppData[8*FE_index+6]
533 + ((ppData[8*FE_index+7] & 0x1)<<8);
534 blocklength += n_add;
535 ppData[8*FE_index+6] = blocklength & 0xFF;
536 ppData[8*FE_index+7] |= (blocklength & 0x100)>>8;
int xtalId() const
get the channel id
int stripId() const
get the tower id
Ecal readout channel identification [32:20] Unused (so far) [19:13] DCC id [12:6] tower [5:3] strip [...
EcalMGPASample sample(int i) const
int towerId() const
get the tower id
size_t size() const
Lenght of the data buffer in bytes.
U second(std::pair< T, U > const &p)
Container::value_type value_type
void resize(size_t newsize)
int dccId() const
get the DCC (Ecal Local DCC value not global one) id
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.