1 #ifndef FEDRawData_DaqData_h
2 #define FEDRawData_DaqData_h
55 template <
class Format>
64 if (v.size() == 0)
throw string(
"DaqData: empty input data vector provided: ");
66 int Nfields = Format::getNumberOfFields();
67 if (v.size()%Nfields != 0)
throw string(
"DaqData: ERROR. You must provide a number of input values compatibles with the requested format: ");
70 int ObjSize = Format::getFieldLastBit(Nfields-1)+1;
79 std::vector<unsigned int>::iterator vit = v.begin();
81 int bitsalreadyfilled = 0;
88 int Nbytes = (int) ceil((
double)(Format::getFieldLastBit(Nfields-1)+1-8+bitsalreadyfilled)/8);
90 if ((Totbytes+=Nbytes) >
size_)
throw string(
"Exceeded allocated buffer size");
96 data_.insert(std::pair<
int, std::vector<unsigned int> >(
i,std::vector<unsigned int>(vit-Nfields,vit) ));
99 p+=(Nbytes+(8-bitsalreadyfilled)/8) ;
100 Totbytes+=(8-bitsalreadyfilled)/8 ;
102 if(bitsalreadyfilled==0) Totbytes--;
115 catch (std::string
s){
117 cout<<
"DaqData - Exception caught: " << s <<endl;
118 cout<<
"Object compression failed! No data has been constructed for format: "<< string(
typeid(Format).
name())<<endl;
131 if (sizeinbytes==0)
throw std::string(
"Buffer size is zero");
133 int Nfields = Format::getNumberOfFields();
134 int ObjSize = Format::getFieldLastBit(Nfields-1)+1;
140 if ((sizeinbytes*8)%ObjSize != 0) {
141 cout<<
"DaqData: there will be " << (sizeinbytes*8)%ObjSize <<
" meaningless bits at the end of the buffer"<<endl;
149 const unsigned char *
p = ptr;
150 int bitsalreadyfilled = 0;
156 std::vector<unsigned int> objdata;
157 objdata.reserve(Nfields);
160 int Nbytes = (int) ceil((
double)(Format::getFieldLastBit(Nfields-1)+1-8+bitsalreadyfilled)/8);
163 if ((Totbytes+=Nbytes) > sizeinbytes)
throw std::string(
"Exceeded allocated buffer size");
169 data_.insert(std::pair<
int, std::vector<unsigned int> >(
i,objdata) );
172 p+= (Nbytes + (8-bitsalreadyfilled)/8);
173 Totbytes+= (8-bitsalreadyfilled)/8;
177 if(bitsalreadyfilled==0) Totbytes--;
190 catch (std::string
s){
192 std::cout<<
"DaqData - Exception caught: " << s <<std::endl;
193 std::cout<<
"Object uncompression failed! No data has been constructed for format: "<<std::string(
typeid(Format).
name())<<endl;
208 std::vector<unsigned int> vec;
209 for(
int j=0;
j<Format::getNumberOfFields();
j++) vec.push_back(a.
getValue(
j,
i));
210 data_.insert(std::pair<
int, std::vector<unsigned int> >(
i, vec));
236 unsigned int getValue(
int indfield,
int indobj=0)
const {
238 if (indobj<
nobjects_ && indfield < Format::getNumberOfFields()) {
239 std::map < int, std::vector<unsigned int> >::const_iterator it =
data_.find(indobj);
240 if (it !=
data_.end())
return ((*it).second)[indfield];
242 cout<<
"DaqData - Strange: object should exist but was not found "<<endl;
246 else cout<<
"DaqData - Non existent field or object"<<endl;
254 std::vector<unsigned int> & objdata,
255 int & bitsalreadyfilled) {
257 int Nfields = Format::getNumberOfFields();
259 int bitstoaccomodate=Format::getFieldLastBit(0)+1;
262 unsigned int value = 0;
264 while (ifield < Nfields) {
266 if(bitstoaccomodate > 8 - bitsalreadyfilled ) {
281 if(bitstoaccomodate > 8) value += ( ( (*ptr << bitsalreadyfilled) & 0xff ) << (bitstoaccomodate - 8) );
282 else value += ( ( (*ptr << bitsalreadyfilled) & 0xff) >> (8 - bitstoaccomodate) );
290 bitstoaccomodate -= (8-bitsalreadyfilled);
291 bitsalreadyfilled = 0;
297 else if(bitstoaccomodate < (8 - bitsalreadyfilled) && bitstoaccomodate >0){
309 value += ( ( (*ptr << bitsalreadyfilled) & 0xff ) >> (8 - bitstoaccomodate) ) ;
315 objdata.push_back(value);
317 bitsalreadyfilled+=bitstoaccomodate;
321 if(ifield==Nfields-1)
return;
326 bitstoaccomodate=Format::getFieldLastBit(ifield)-Format::getFieldLastBit(ifield-1);
332 else if(bitstoaccomodate == (8 - bitsalreadyfilled) && bitstoaccomodate >0){
343 value += ( ( (*ptr << bitsalreadyfilled) & 0xff ) >> (8 - bitstoaccomodate) ) ;
349 objdata.push_back(value);
355 if(ifield==Nfields-1)
return;
361 bitstoaccomodate=Format::getFieldLastBit(ifield)-Format::getFieldLastBit(ifield-1);
366 else throw std::string(
" unexpected situation during uncompression");
375 void compressObject(
const unsigned char * ptr, std::vector<unsigned int>::iterator & vit,
int & bitsalreadyfilled) {
377 int Nfields = Format::getNumberOfFields();
379 int bitstoaccomodate=Format::getFieldLastBit(0)+1;
381 if (*vit >
pow(2.,bitstoaccomodate)-1)
throw string(
"The value is too large to fit in the field ");
386 *ptr &= 0xff + 1 - (
unsigned int)
pow(2.,8-bitsalreadyfilled);
388 while (ifield < Nfields) {
391 if(bitstoaccomodate > 8 - bitsalreadyfilled ) {
396 *ptr += (((*vit) >> (bitstoaccomodate - (8 - bitsalreadyfilled))) & 0xff);
401 bitstoaccomodate -= (8-bitsalreadyfilled);
402 bitsalreadyfilled = 0;
404 *ptr &= 0xff + 1 - (
unsigned int)
pow(2.,8-bitsalreadyfilled);
410 else if(bitstoaccomodate < (8 - bitsalreadyfilled) && bitstoaccomodate >0){
414 *ptr += ( (((*vit) << 8-bitstoaccomodate) & 0xff) >> (bitsalreadyfilled) );
421 bitsalreadyfilled+=bitstoaccomodate;
425 if(ifield==Nfields-1)
return;
430 bitstoaccomodate=Format::getFieldLastBit(ifield)-Format::getFieldLastBit(ifield-1);
431 if (*vit >
pow(2.,bitstoaccomodate)-1)
throw string(
"The value is too large to fit in the field ");
437 else if(bitstoaccomodate == (8 - bitsalreadyfilled) && bitstoaccomodate >0){
442 *ptr += ( (((*vit) << 8-bitstoaccomodate) & 0xff) >> (bitsalreadyfilled) );
453 if(ifield==Nfields-1)
return;
458 *ptr &= 0xff + 1 - (
unsigned int)
pow(2.,8-bitsalreadyfilled);
461 bitstoaccomodate=Format::getFieldLastBit(ifield)-Format::getFieldLastBit(ifield-1);
463 if (*vit >
pow(2.,bitstoaccomodate)-1)
throw string(
"The value is too large to fit in the field ");
468 else throw string(
" unexpected situation during compression");
477 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)