CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/src/EventFilter/AutoBU/src/BUEvent.cc

Go to the documentation of this file.
00001 
00002 //
00003 // BUEvent
00004 // -------
00005 //
00006 //            03/26/2007 Philipp Schieferdecker <philipp.schieferdecker@cern.ch>
00008 
00009 
00010 #include "EventFilter/AutoBU/interface/BUEvent.h"
00011 #include <assert.h>
00012 #include "FWCore/Utilities/interface/CRC16.h"
00013 
00014 #include "interface/shared/fed_header.h"
00015 #include "interface/shared/fed_trailer.h"
00016 
00017 #include <iostream>
00018 #include <iomanip>
00019 #include <fstream>
00020 #include <sstream>
00021 #include <cstring>
00022 
00023 using namespace std;
00024 using namespace evf;
00025 
00026 
00027 
00029 // initialize static member data 
00031 
00032 //______________________________________________________________________________
00033 bool BUEvent::computeCrc_=true;
00034 
00035 
00037 // construction/destruction
00039 
00040 //______________________________________________________________________________
00041 BUEvent::BUEvent(unsigned int buResourceId,unsigned int bufferSize)
00042   : buResourceId_(buResourceId)
00043   , evtNumber_(0xffffffff)
00044   , evtSize_(0)
00045   , bufferSize_(bufferSize)
00046   , nFed_(0)
00047   , fedId_(0)
00048   , fedPos_(0)
00049   , fedSize_(0)
00050   , buffer_(0)
00051 {
00052   fedId_  = new unsigned int[1024];
00053   fedPos_ = new unsigned int[1024];
00054   fedSize_= new unsigned int[1024];
00055   buffer_ = new unsigned char[bufferSize];
00056 }
00057 
00058 
00059 //______________________________________________________________________________
00060 BUEvent::~BUEvent()
00061 {
00062   if (0!=fedId_)   delete [] fedId_;
00063   if (0!=fedPos_)  delete [] fedPos_;
00064   if (0!=fedSize_) delete [] fedSize_;
00065   if (0!=buffer_)  delete [] buffer_;
00066 }
00067 
00068 
00070 // implementation of member functions
00072 
00073 //______________________________________________________________________________
00074 void BUEvent::initialize(unsigned int evtNumber)
00075  {
00076    evtNumber_=evtNumber & 0xFFFFFF; // 24 bits only available in the FED headers
00077    evtSize_=0;
00078    nFed_=0;
00079  }
00080 
00081 
00082 //______________________________________________________________________________
00083 bool BUEvent::writeFed(unsigned int id,unsigned char* data,unsigned int size)
00084 {
00085   if (evtSize_+size > bufferSize_) {
00086     cout<<"BUEvent::writeFed() ERROR: buffer overflow."<<endl;
00087     return false;
00088   }
00089   
00090   if (nFed_==1024) {
00091     cout<<"BUEvent::writeFed() ERROR: too many feds (max=1024)."<<endl;
00092     return false;
00093   }
00094   
00095   fedId_[nFed_]  =id;
00096   fedPos_[nFed_] =evtSize_;
00097   fedSize_[nFed_]=size;
00098   if (0!=data) memcpy(fedAddr(nFed_),data,size);
00099   ++nFed_;
00100   evtSize_+=size;
00101   return true;
00102 }
00103 
00104 
00105 //______________________________________________________________________________
00106 bool BUEvent::writeFedHeader(unsigned int i)
00107 {
00108   if (i>=nFed_) {
00109     cout<<"BUEvent::writeFedHeader() ERROR: invalid fed index '"<<i<<"'."<<endl;
00110     return false;
00111   }
00112   
00113   fedh_t *fedHeader=(fedh_t*)fedAddr(i);
00114   fedHeader->eventid =evtNumber();
00115   fedHeader->eventid|=0x50000000;
00116   fedHeader->sourceid=(fedId(i) << 8) & FED_SOID_MASK;
00117   
00118   return true;
00119 }
00120 
00121 
00122 //______________________________________________________________________________
00123 bool BUEvent::writeFedTrailer(unsigned int i)
00124 {
00125   if (i>=nFed_) {
00126     cout<<"BUEvent::writeFedTrailer() ERROR: invalid fed index '"<<i<<"'."<<endl;
00127     return false;
00128   }
00129   
00130   fedt_t *fedTrailer=(fedt_t*)(fedAddr(i)+fedSize(i)-sizeof(fedt_t));
00131   fedTrailer->eventsize =fedSize(i);
00132   fedTrailer->eventsize/=8; //wc in fed trailer in 64bit words
00133   fedTrailer->eventsize|=0xa0000000;
00134   fedTrailer->conscheck =0x0;
00135   
00136   if (BUEvent::computeCrc()) {
00137     unsigned short crc=evf::compute_crc(fedAddr(i),fedSize(i));
00138     fedTrailer->conscheck=(crc<<FED_CRCS_SHIFT);
00139   }
00140 
00141   return true;
00142 }
00143 
00144 
00145 //______________________________________________________________________________
00146 unsigned char* BUEvent::fedAddr(unsigned int i) const
00147 {
00148   return (buffer_+fedPos_[i]);
00149 }
00150 
00151 
00152 //________________________________________________________________________________
00153 void BUEvent::dump()
00154 {
00155   ostringstream oss; oss<<"/tmp/autobu_evt"<<evtNumber()<<".dump";
00156   ofstream fout(oss.str().c_str());
00157   fout.fill('0');
00158   
00159   fout<<"#\n# evt "<<evtNumber()<<"\n#\n"<<endl;
00160   for (unsigned int i=0;i<nFed();i++) {
00161     if (fedSize(i)==0) continue;
00162     fout<<"# fedid "<<fedId(i)<<endl;
00163     unsigned char* addr=fedAddr(i);
00164     for (unsigned int j=0;j<fedSize(i);j++) {
00165       fout<<setiosflags(ios::right)<<setw(2)<<hex<<(int)(*addr)<<dec;
00166       if ((j+1)%8) fout<<" "; else fout<<endl;
00167       ++addr;
00168     }
00169     fout<<endl;
00170   }
00171   fout.close();
00172 }