CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10_patch2/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 {
00034   fedSizeOffset_=sizeof(FUShmRawCell);
00035   unsigned int* fedSizeAddr;
00036   fedSizeAddr=(unsigned int*)((unsigned long)this+fedSizeOffset_);
00037   new(fedSizeAddr) unsigned int[nFed_];
00038   
00039   fedOffset_=fedSizeOffset_+sizeof(unsigned int)*nFed_;
00040   unsigned int* fedAddr;
00041   fedAddr=(unsigned int*)((unsigned long)this+fedOffset_);
00042   new(fedAddr) unsigned int[nFed_];
00043   
00044   superFragSizeOffset_=fedOffset_+sizeof(unsigned int)*nFed_;
00045   unsigned int* superFragSizeAddr;
00046   superFragSizeAddr=(unsigned int*)((unsigned long)this+superFragSizeOffset_);
00047   new(superFragSizeAddr) unsigned int[nSuperFrag_];
00048   
00049   superFragOffset_=superFragSizeOffset_+sizeof(unsigned int)*nSuperFrag_;
00050   unsigned char* superFragAddr;
00051   superFragAddr=(unsigned char*)((unsigned long)this+superFragOffset_);
00052   new(superFragAddr) unsigned char[nSuperFrag_];
00053   
00054   payloadOffset_=superFragOffset_+sizeof(unsigned int)*nSuperFrag_;
00055   unsigned char* payloadAddr;
00056   payloadAddr=(unsigned char*)((unsigned long)this+payloadOffset_);
00057   new(payloadAddr) unsigned char[payloadSize_];
00058 }
00059 
00060 
00061 //______________________________________________________________________________
00062 FUShmRawCell::~FUShmRawCell()
00063 {
00064 
00065 }
00066 
00067 
00069 // implementation of member functions
00071 
00072 //______________________________________________________________________________
00073 void FUShmRawCell::initialize(unsigned int index)
00074 {
00075   index_=index;
00076   lumiSection_=0;
00077   eventType_=evt::NOP;
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   eventType_   =evt::NOP;
00153   unsigned int* fedSizeAddr;
00154   fedSizeAddr=(unsigned int*)((unsigned long)this+fedSizeOffset_);
00155   for (unsigned int i=0;i<nFed();i++) *fedSizeAddr++=0;
00156 
00157   unsigned int* superFragSizeAddr;
00158   superFragSizeAddr=(unsigned int*)((unsigned long)this+superFragSizeOffset_);
00159   for (unsigned int i=0;i<nSuperFrag();i++) *superFragSizeAddr++=0;
00160 
00161   payloadPosition_=0;
00162 }
00163 
00164 
00165 //______________________________________________________________________________
00166 void FUShmRawCell::dump() const
00167 {
00168   for (unsigned int i=0;i<nFed();i++) {
00169     cout<<"fed "<<i<<": "<<flush;
00170     unsigned char* addr=fedAddr(i);
00171     unsigned int   size=fedSize(i);
00172     cout.fill(0);
00173     cout<<setiosflags(ios::right);
00174     for (unsigned int j=0;j<size;j++)
00175       cout<<setw(2)<<hex<<(int)addr[j]<<dec<<" "<<flush;
00176     cout<<endl;
00177   }
00178 }
00179 
00180 
00181 //______________________________________________________________________________
00182 unsigned int FUShmRawCell::readFed(unsigned int i,
00183                                    unsigned char* buffer) const
00184 {
00185   unsigned int   size=fedSize(i);
00186   unsigned char* addr=fedAddr(i);
00187   memcpy(buffer,addr,size);
00188   return size;
00189 }
00190 
00191 
00192 //______________________________________________________________________________
00193 unsigned char* FUShmRawCell::writeData(unsigned char* data,
00194                                        unsigned int   dataSize)
00195 {
00196   if (payloadPosition_+dataSize>payloadSize_) {
00197     cout<<"FUShmRawCell::writeData: data to be written does not fit!"<<endl;
00198     return 0;
00199   }
00200   
00201   // result = addr of data to be written *in* the cell
00202   unsigned char* result=
00203     (unsigned char*)((unsigned long)this+payloadOffset_+payloadPosition_);
00204   memcpy(result,data,dataSize);
00205   payloadPosition_+=dataSize;
00206   return result;
00207 }
00208 
00209 //______________________________________________________________________________
00210 void FUShmRawCell::setLumiSection(unsigned int ls)
00211 {
00212   lumiSection_=ls;
00213 }
00214 //______________________________________________________________________________
00215 bool FUShmRawCell::markFed(unsigned int i,
00216                            unsigned int size,
00217                            unsigned char* addr)
00218 {
00219   if (i>=nFed())
00220     {cout<<"invalid fed index '"<<i<<"'."<<endl; return false; }
00221   if (addr<payloadAddr())
00222     { cout<<"invalid fed addr '0x"<<hex<<(unsigned long)addr<<dec<<"'."<<endl; return false; }
00223 
00224   unsigned long offset=(unsigned long)addr-(unsigned long)payloadAddr();
00225 
00226   if (offset>=payloadSize())
00227     { cout<<"invalid fed addr '0x"<<hex<<(unsigned long)addr<<dec<<"'."<<endl; return false; }
00228 
00229   unsigned int* fedSizeAddr;
00230   fedSizeAddr=(unsigned int*)((unsigned long)this+fedSizeOffset_);
00231   fedSizeAddr+=i;
00232   *fedSizeAddr=size;
00233 
00234   unsigned int* fedAddr;
00235   fedAddr=(unsigned int*)((unsigned long)this+fedOffset_);
00236   fedAddr+=i;
00237   *fedAddr=offset;
00238 
00239   return true;
00240 }
00241 
00242 
00243 //______________________________________________________________________________
00244 bool FUShmRawCell::markSuperFrag(unsigned int i,
00245                                  unsigned int size,
00246                                  unsigned char* addr)
00247 {
00248   if (i>=nSuperFrag())
00249     {cout<<"invalid sf index '"<<i<<"'."<<endl; return false; }
00250   if (addr<payloadAddr())
00251     {cout<<"invalid sf addr '0x"<<hex<<(unsigned long)addr<<dec<<"'."<<endl;return false;}
00252 
00253   unsigned int offset=(unsigned long)addr-(unsigned long)payloadAddr();
00254 
00255   if (offset>=payloadSize())
00256     {cout<<"invalid sf addr '0x"<<hex<<(unsigned long)addr<<dec<<"'."<<endl;return false;}
00257 
00258   unsigned int* superFragSizeAddr;
00259   superFragSizeAddr=(unsigned int*)((unsigned long)this+superFragSizeOffset_);
00260   superFragSizeAddr+=i;
00261   *superFragSizeAddr=size;
00262 
00263   unsigned int* superFragAddr;
00264   superFragAddr=(unsigned int*)((unsigned long)this+superFragOffset_);
00265   superFragAddr+=i;
00266   *superFragAddr=offset;
00267 
00268   return true;
00269 }
00270                                   
00271 
00272 //______________________________________________________________________________
00273 unsigned int FUShmRawCell::size(unsigned int payloadSize)
00274 {
00275   return 
00276     sizeof(FUShmRawCell)+
00277     sizeof(unsigned int)*2*(NFED_MAX+NSUPERFRAG_MAX)+
00278     sizeof(unsigned char)*payloadSize;
00279 }