CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch13/src/EventFilter/ShmBuffer/src/FUShmRawCell.cc

Go to the documentation of this file.
00001 
00002 //
00003 // FUShmRawCell
00004 // ------------
00005 //
00006 //            09/11/2006 Philipp Schieferdecker <philipp.schieferdecker@cern.ch>
00008 
00009 
00010 #include "EventFilter/ShmBuffer/interface/FUShmRawCell.h"
00011 
00012 #include <iostream>
00013 #include <iomanip>
00014 #include <cstring>
00015 
00016 #define NSUPERFRAG_MAX   80
00017 #define NFED_MAX       1024
00018 
00019 
00020 using namespace std;
00021 using namespace evf;
00022 
00023 
00025 // construction/destruction
00027 
00028 //______________________________________________________________________________
00029 FUShmRawCell::FUShmRawCell(unsigned int payloadSize)
00030   : payloadSize_(payloadSize)
00031   , nFed_(NFED_MAX)
00032   , nSuperFrag_(NSUPERFRAG_MAX)
00033   , lumiSection_(0)
00034 {
00035   fedSizeOffset_=sizeof(FUShmRawCell);
00036   unsigned int* fedSizeAddr;
00037   fedSizeAddr=(unsigned int*)((unsigned long)this+fedSizeOffset_);
00038   new(fedSizeAddr) unsigned int[nFed_];
00039   
00040   fedOffset_=fedSizeOffset_+sizeof(unsigned int)*nFed_;
00041   unsigned int* fedAddr;
00042   fedAddr=(unsigned int*)((unsigned long)this+fedOffset_);
00043   new(fedAddr) unsigned int[nFed_];
00044   
00045   superFragSizeOffset_=fedOffset_+sizeof(unsigned int)*nFed_;
00046   unsigned int* superFragSizeAddr;
00047   superFragSizeAddr=(unsigned int*)((unsigned long)this+superFragSizeOffset_);
00048   new(superFragSizeAddr) unsigned int[nSuperFrag_];
00049   
00050   superFragOffset_=superFragSizeOffset_+sizeof(unsigned int)*nSuperFrag_;
00051   unsigned char* superFragAddr;
00052   superFragAddr=(unsigned char*)((unsigned long)this+superFragOffset_);
00053   new(superFragAddr) unsigned char[nSuperFrag_];
00054   
00055   payloadOffset_=superFragOffset_+sizeof(unsigned int)*nSuperFrag_;
00056   unsigned char* payloadAddr;
00057   payloadAddr=(unsigned char*)((unsigned long)this+payloadOffset_);
00058   new(payloadAddr) unsigned char[payloadSize_];
00059 }
00060 
00061 
00062 //______________________________________________________________________________
00063 FUShmRawCell::~FUShmRawCell()
00064 {
00065 
00066 }
00067 
00068 
00070 // implementation of member functions
00072 
00073 //______________________________________________________________________________
00074 void FUShmRawCell::initialize(unsigned int index)
00075 {
00076   index_=index;
00077   lumiSection_=0;
00078 }
00079 
00080 
00081 //______________________________________________________________________________
00082 unsigned int FUShmRawCell::fedSize(unsigned int i) const
00083 {
00084   if (i>=nFed()) {cout<<"invalid fed index '"<<i<<"'."<<endl; return 0; }
00085   unsigned int* fedSizeAddr;
00086   fedSizeAddr=(unsigned int*)((unsigned long)this+fedSizeOffset_);
00087   fedSizeAddr+=i;
00088   unsigned int result=*fedSizeAddr;
00089   return result;
00090 }
00091 
00092 
00093 //______________________________________________________________________________
00094 unsigned char* FUShmRawCell::fedAddr(unsigned int i) const
00095 {
00096   if (i>=nFed()) {cout<<"invalid fed index '"<<i<<"'."<<endl; return 0; }
00097   unsigned int* fedOffsetAddr;
00098   fedOffsetAddr=(unsigned int*)((unsigned long)this+fedOffset_);
00099   fedOffsetAddr+=i;
00100   unsigned int   fedOffset=*fedOffsetAddr;
00101   unsigned char* result=(unsigned char*)((unsigned long)payloadAddr()+fedOffset);
00102   return result;
00103 }
00104 
00105 
00106 //______________________________________________________________________________
00107 unsigned int FUShmRawCell::superFragSize(unsigned int i) const
00108 {
00109   if (i>=nSuperFrag()) {cout<<"invalid sf index '"<<i<<"'."<<endl; return 0; }
00110   unsigned int* superFragSizeAddr;
00111   superFragSizeAddr=(unsigned int*)((unsigned long)this+superFragSizeOffset_);
00112   superFragSizeAddr+=i;
00113   unsigned int result=*superFragSizeAddr;
00114   return result;
00115 }
00116 
00117 
00118 //______________________________________________________________________________
00119 unsigned char* FUShmRawCell::superFragAddr(unsigned int i) const
00120 {
00121   if (i>=nSuperFrag()) {cout<<"invalid fed index '"<<i<<"'."<<endl; return 0; }
00122   unsigned int* superFragOffsetAddr;
00123   superFragOffsetAddr=(unsigned int*)((unsigned long)this+superFragOffset_);
00124   superFragOffsetAddr+=i;
00125   unsigned int   superFragOffset=*superFragOffsetAddr;
00126   unsigned char* result=(unsigned char*)((unsigned long)payloadAddr()+superFragOffset);
00127   return result;
00128 }
00129 
00130 
00131 //______________________________________________________________________________
00132 unsigned char* FUShmRawCell::payloadAddr() const
00133 {
00134   unsigned char* result=(unsigned char*)((unsigned long)this+payloadOffset_);
00135   return result;
00136 }
00137 
00138 
00139 //______________________________________________________________________________
00140 unsigned int FUShmRawCell::eventSize() const
00141 {
00142   return payloadPosition_;
00143 }
00144 
00145 
00146 //______________________________________________________________________________
00147 void FUShmRawCell::clear()
00148 {
00149   fuResourceId_=0xffffffff;
00150   buResourceId_=0xffffffff;
00151   lumiSection_ =0;
00152   unsigned int* fedSizeAddr;
00153   fedSizeAddr=(unsigned int*)((unsigned long)this+fedSizeOffset_);
00154   for (unsigned int i=0;i<nFed();i++) *fedSizeAddr++=0;
00155 
00156   unsigned int* superFragSizeAddr;
00157   superFragSizeAddr=(unsigned int*)((unsigned long)this+superFragSizeOffset_);
00158   for (unsigned int i=0;i<nSuperFrag();i++) *superFragSizeAddr++=0;
00159 
00160   payloadPosition_=0;
00161 }
00162 
00163 
00164 //______________________________________________________________________________
00165 void FUShmRawCell::dump() const
00166 {
00167   for (unsigned int i=0;i<nFed();i++) {
00168     cout<<"fed "<<i<<": "<<flush;
00169     unsigned char* addr=fedAddr(i);
00170     unsigned int   size=fedSize(i);
00171     cout.fill(0);
00172     cout<<setiosflags(ios::right);
00173     for (unsigned int j=0;j<size;j++)
00174       cout<<setw(2)<<hex<<(int)addr[j]<<dec<<" "<<flush;
00175     cout<<endl;
00176   }
00177 }
00178 
00179 
00180 //______________________________________________________________________________
00181 unsigned int FUShmRawCell::readFed(unsigned int i,
00182                                    unsigned char* buffer) const
00183 {
00184   unsigned int   size=fedSize(i);
00185   unsigned char* addr=fedAddr(i);
00186   memcpy(buffer,addr,size);
00187   return size;
00188 }
00189 
00190 
00191 //______________________________________________________________________________
00192 unsigned char* FUShmRawCell::writeData(unsigned char* data,
00193                                        unsigned int   dataSize)
00194 {
00195   if (payloadPosition_+dataSize>payloadSize_) {
00196     cout<<"FUShmRawCell::writeData: data to be written does not fit!"<<endl;
00197     return 0;
00198   }
00199   
00200   // result = addr of data to be written *in* the cell
00201   unsigned char* result=
00202     (unsigned char*)((unsigned long)this+payloadOffset_+payloadPosition_);
00203   memcpy(result,data,dataSize);
00204   payloadPosition_+=dataSize;
00205   return result;
00206 }
00207 
00208 //______________________________________________________________________________
00209 void FUShmRawCell::setLumiSection(unsigned int ls)
00210 {
00211   lumiSection_=ls;
00212 }
00213 //______________________________________________________________________________
00214 bool FUShmRawCell::markFed(unsigned int i,
00215                            unsigned int size,
00216                            unsigned char* addr)
00217 {
00218   if (i>=nFed())
00219     {cout<<"invalid fed index '"<<i<<"'."<<endl; return false; }
00220   if (addr<payloadAddr())
00221     { cout<<"invalid fed addr '0x"<<hex<<(unsigned long)addr<<dec<<"'."<<endl; return false; }
00222 
00223   unsigned long offset=(unsigned long)addr-(unsigned long)payloadAddr();
00224 
00225   if (offset>=payloadSize())
00226     { cout<<"invalid fed addr '0x"<<hex<<(unsigned long)addr<<dec<<"'."<<endl; return false; }
00227 
00228   unsigned int* fedSizeAddr;
00229   fedSizeAddr=(unsigned int*)((unsigned long)this+fedSizeOffset_);
00230   fedSizeAddr+=i;
00231   *fedSizeAddr=size;
00232 
00233   unsigned int* fedAddr;
00234   fedAddr=(unsigned int*)((unsigned long)this+fedOffset_);
00235   fedAddr+=i;
00236   *fedAddr=offset;
00237 
00238   return true;
00239 }
00240 
00241 
00242 //______________________________________________________________________________
00243 bool FUShmRawCell::markSuperFrag(unsigned int i,
00244                                  unsigned int size,
00245                                  unsigned char* addr)
00246 {
00247   if (i>=nSuperFrag())
00248     {cout<<"invalid sf index '"<<i<<"'."<<endl; return false; }
00249   if (addr<payloadAddr())
00250     {cout<<"invalid sf addr '0x"<<hex<<(unsigned long)addr<<dec<<"'."<<endl;return false;}
00251 
00252   unsigned int offset=(unsigned long)addr-(unsigned long)payloadAddr();
00253 
00254   if (offset>=payloadSize())
00255     {cout<<"invalid sf addr '0x"<<hex<<(unsigned long)addr<<dec<<"'."<<endl;return false;}
00256 
00257   unsigned int* superFragSizeAddr;
00258   superFragSizeAddr=(unsigned int*)((unsigned long)this+superFragSizeOffset_);
00259   superFragSizeAddr+=i;
00260   *superFragSizeAddr=size;
00261 
00262   unsigned int* superFragAddr;
00263   superFragAddr=(unsigned int*)((unsigned long)this+superFragOffset_);
00264   superFragAddr+=i;
00265   *superFragAddr=offset;
00266 
00267   return true;
00268 }
00269                                   
00270 
00271 //______________________________________________________________________________
00272 unsigned int FUShmRawCell::size(unsigned int payloadSize)
00273 {
00274   return 
00275     sizeof(FUShmRawCell)+
00276     sizeof(unsigned int)*2*(NFED_MAX+NSUPERFRAG_MAX)+
00277     sizeof(unsigned char)*payloadSize;
00278 }