1 #ifndef FEDRawData_DaqData_h
2 #define FEDRawData_DaqData_h
53 template <
class Format>
62 if (v.size() == 0)
throw string(
"DaqData: empty input data vector provided: ");
64 int Nfields = Format::getNumberOfFields();
65 if (v.size()%Nfields != 0)
throw string(
"DaqData: ERROR. You must provide a number of input values compatibles with the requested format: ");
68 int ObjSize = Format::getFieldLastBit(Nfields-1)+1;
77 std::vector<unsigned int>::iterator vit = v.begin();
79 int bitsalreadyfilled = 0;
86 int Nbytes = (int) ceil((
double)(Format::getFieldLastBit(Nfields-1)+1-8+bitsalreadyfilled)/8);
88 if ((Totbytes+=Nbytes) >
size_)
throw string(
"Exceeded allocated buffer size");
94 data_.insert(std::pair<
int, std::vector<unsigned int> >(
i,std::vector<unsigned int>(vit-Nfields,vit) ));
97 p+=(Nbytes+(8-bitsalreadyfilled)/8) ;
98 Totbytes+=(8-bitsalreadyfilled)/8 ;
100 if(bitsalreadyfilled==0) Totbytes--;
115 cout<<
"DaqData - Exception caught: " << s <<endl;
116 cout<<
"Object compression failed! No data has been constructed for format: "<<
string(
typeid(Format).
name())<<endl;
129 if (sizeinbytes==0)
throw std::string(
"Buffer size is zero");
131 int Nfields = Format::getNumberOfFields();
132 int ObjSize = Format::getFieldLastBit(Nfields-1)+1;
138 if ((sizeinbytes*8)%ObjSize != 0) {
139 cout<<
"DaqData: there will be " << (sizeinbytes*8)%ObjSize <<
" meaningless bits at the end of the buffer"<<endl;
147 const unsigned char *
p = ptr;
148 int bitsalreadyfilled = 0;
154 std::vector<unsigned int> objdata;
155 objdata.reserve(Nfields);
158 int Nbytes = (int) ceil((
double)(Format::getFieldLastBit(Nfields-1)+1-8+bitsalreadyfilled)/8);
161 if ((Totbytes+=Nbytes) > sizeinbytes)
throw std::string(
"Exceeded allocated buffer size");
167 data_.insert(std::pair<
int, std::vector<unsigned int> >(
i,objdata) );
170 p+= (Nbytes + (8-bitsalreadyfilled)/8);
171 Totbytes+= (8-bitsalreadyfilled)/8;
175 if(bitsalreadyfilled==0) Totbytes--;
190 std::cout<<
"DaqData - Exception caught: " << s <<std::endl;
191 std::cout<<
"Object uncompression failed! No data has been constructed for format: "<<
std::string(
typeid(Format).
name())<<endl;
206 std::vector<unsigned int> vec;
207 for(
int j=0;
j<Format::getNumberOfFields();
j++) vec.push_back(a.
getValue(
j,
i));
208 data_.insert(std::pair<
int, std::vector<unsigned int> >(
i, vec));
234 unsigned int getValue(
int indfield,
int indobj=0)
const {
236 if (indobj<
nobjects_ && indfield < Format::getNumberOfFields()) {
237 std::map < int, std::vector<unsigned int> >::const_iterator it =
data_.find(indobj);
238 if (it !=
data_.end())
return ((*it).second)[indfield];
240 cout<<
"DaqData - Strange: object should exist but was not found "<<endl;
244 else cout<<
"DaqData - Non existent field or object"<<endl;
252 std::vector<unsigned int> & objdata,
253 int & bitsalreadyfilled) {
255 int Nfields = Format::getNumberOfFields();
257 int bitstoaccomodate=Format::getFieldLastBit(0)+1;
260 unsigned int value = 0;
262 while (ifield < Nfields) {
264 if(bitstoaccomodate > 8 - bitsalreadyfilled ) {
279 if(bitstoaccomodate > 8) value += ( ( (*ptr << bitsalreadyfilled) & 0xff ) << (bitstoaccomodate - 8) );
280 else value += ( ( (*ptr << bitsalreadyfilled) & 0xff) >> (8 - bitstoaccomodate) );
288 bitstoaccomodate -= (8-bitsalreadyfilled);
289 bitsalreadyfilled = 0;
295 else if(bitstoaccomodate < (8 - bitsalreadyfilled) && bitstoaccomodate >0){
307 value += ( ( (*ptr << bitsalreadyfilled) & 0xff ) >> (8 - bitstoaccomodate) ) ;
313 objdata.push_back(value);
315 bitsalreadyfilled+=bitstoaccomodate;
319 if(ifield==Nfields-1)
return;
324 bitstoaccomodate=Format::getFieldLastBit(ifield)-Format::getFieldLastBit(ifield-1);
330 else if(bitstoaccomodate == (8 - bitsalreadyfilled) && bitstoaccomodate >0){
341 value += ( ( (*ptr << bitsalreadyfilled) & 0xff ) >> (8 - bitstoaccomodate) ) ;
347 objdata.push_back(value);
353 if(ifield==Nfields-1)
return;
359 bitstoaccomodate=Format::getFieldLastBit(ifield)-Format::getFieldLastBit(ifield-1);
364 else throw std::string(
" unexpected situation during uncompression");
373 void compressObject(
const unsigned char * ptr, std::vector<unsigned int>::iterator & vit,
int & bitsalreadyfilled) {
375 int Nfields = Format::getNumberOfFields();
377 int bitstoaccomodate=Format::getFieldLastBit(0)+1;
379 if (*vit >
pow(2.,bitstoaccomodate)-1)
throw string(
"The value is too large to fit in the field ");
384 *ptr &= 0xff + 1 - (
unsigned int)
pow(2.,8-bitsalreadyfilled);
386 while (ifield < Nfields) {
389 if(bitstoaccomodate > 8 - bitsalreadyfilled ) {
394 *ptr += (((*vit) >> (bitstoaccomodate - (8 - bitsalreadyfilled))) & 0xff);
399 bitstoaccomodate -= (8-bitsalreadyfilled);
400 bitsalreadyfilled = 0;
402 *ptr &= 0xff + 1 - (
unsigned int)
pow(2.,8-bitsalreadyfilled);
408 else if(bitstoaccomodate < (8 - bitsalreadyfilled) && bitstoaccomodate >0){
412 *ptr += ( (((*vit) << 8-bitstoaccomodate) & 0xff) >> (bitsalreadyfilled) );
419 bitsalreadyfilled+=bitstoaccomodate;
423 if(ifield==Nfields-1)
return;
428 bitstoaccomodate=Format::getFieldLastBit(ifield)-Format::getFieldLastBit(ifield-1);
429 if (*vit >
pow(2.,bitstoaccomodate)-1)
throw string(
"The value is too large to fit in the field ");
435 else if(bitstoaccomodate == (8 - bitsalreadyfilled) && bitstoaccomodate >0){
440 *ptr += ( (((*vit) << 8-bitstoaccomodate) & 0xff) >> (bitsalreadyfilled) );
451 if(ifield==Nfields-1)
return;
456 *ptr &= 0xff + 1 - (
unsigned int)
pow(2.,8-bitsalreadyfilled);
459 bitstoaccomodate=Format::getFieldLastBit(ifield)-Format::getFieldLastBit(ifield-1);
461 if (*vit >
pow(2.,bitstoaccomodate)-1)
throw string(
"The value is too large to fit in the field ");
466 else throw string(
" unexpected situation during compression");
475 std::map < int, std::vector<unsigned int> >
data_;
void uncompressObject(const unsigned char *ptr, std::vector< unsigned int > &objdata, int &bitsalreadyfilled)
DaqData(const DaqData &a)
std::map< int, std::vector< unsigned int > > data_
DaqData(std::vector< unsigned int > &v)
unsigned int getValue(int indfield, int indobj=0) const
Power< A, B >::type pow(const A &a, const B &b)
void compressObject(const unsigned char *ptr, std::vector< unsigned int >::iterator &vit, int &bitsalreadyfilled)
DaqData(const unsigned char *ptr, int sizeinbytes)