Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

RawDataBlock.cxx

Go to the documentation of this file.
00001 
00002 // $Id: RawDataBlock.cxx,v 1.19 2005/04/29 22:32:06 minoscvs Exp $
00003 // 
00004 // RawDataBlock 
00005 // 
00006 // RawDataBlock is the base class for a block of raw MINOS data 
00007 // The block is an unstructured collection of raw long (32-bit) words.
00008 // The use of long words is important for letting the underlying ROOT I/O
00009 // deal with the big vs. little endian-ism.  The consequence of this
00010 // is to require the data suppliers to block their data (and pad if
00011 // necessary) into 32-bit units.  This should not impose any serious
00012 // costs as they are free to pack the block as they wish.
00013 //
00014 // Derived versions specialize how to unpack that data.
00015 //
00016 // Author:  R. Hatcher 2000.04.19
00017 //
00019 
00020 #include "RawData/RawDataBlock.h"
00021 
00022 #include <cassert>
00023 #include <string.h>
00024 
00025 #include "MessageService/MsgService.h"
00026 #include "MessageService/MsgFormat.h"
00027 //CVSID("$Id: RawDataBlock.cxx,v 1.19 2005/04/29 22:32:06 minoscvs Exp $");
00028 
00029 #include "RawData/RawBlockRegistry.h"
00030 REGISTERRAWBLOCK(RawDataBlock,0,0);
00031 
00032 ClassImp(RawDataBlock)
00033 
00034 //_____________________________________________________________________________
00035 
00036 Bool_t RawDataBlock::fgForceHexDump = false;
00037 
00038 //_____________________________________________________________________________
00039 
00040 std::ostream& operator<<(std::ostream& os, const RawDataBlock& r) 
00041 { return r.FormatToOStream(os); }
00042 
00043 //_____________________________________________________________________________
00044 RawDataBlock::RawDataBlock()
00045    : fSize(0), fRawBlock(0)
00046 {
00047    // Default constructor
00048 }
00049 
00050 //_____________________________________________________________________________
00051 RawDataBlock::RawDataBlock(const Int_t *block)
00052    : fSize(0), fRawBlock(0)
00053 {
00054    // Normal constructor
00055    //
00056    // build from flat buffer simply by duplicating data
00057    //
00058    if ( ! block ) return;
00059 
00060    fSize = (*block);
00061 
00062    if (fSize > 0) {
00063       fRawBlock = new Int_t [fSize];
00064       memcpy(fRawBlock,block,sizeof(Int_t)*fSize);
00065    } else {
00066       fSize = 0;
00067    }
00068 }
00069 
00070 //_____________________________________________________________________________
00071 RawDataBlock::RawDataBlock(const RawDataBlock &rhs)
00072    : TObject(rhs), fSize(0), fRawBlock(0)
00073 {
00074    // deep copy constructor
00075    //
00076    // build from rhs's buffer simply by duplicating data
00077    //
00078    fSize = rhs.GetSize();
00079 
00080    if (fSize > 0) {
00081       const Int_t* block = rhs.GetData();
00082       fRawBlock = new Int_t [fSize];
00083       memcpy(fRawBlock,block,sizeof(Int_t)*fSize);
00084    } else {
00085       fSize = 0;
00086    }
00087 }
00088 
00089 //_____________________________________________________________________________
00090 RawDataBlock::~RawDataBlock()
00091 {
00092    if (fSize > 0) delete [] fRawBlock;
00093 }
00094 
00095 //_____________________________________________________________________________
00096 RawDataBlock& RawDataBlock::operator=(const RawDataBlock& rhs)
00097 {
00098   // deep copy assignment
00099    //
00100    // build from rhs's buffer simply by duplicating data
00101    //
00102 
00103    // don't try assigning to self
00104    if (this == &rhs) return *this;
00105 
00106    // delete any existing info
00107    if (fSize > 0) { delete [] fRawBlock; fRawBlock = 0; }
00108 
00109    fSize = rhs.GetSize();
00110 
00111    if (fSize > 0) {
00112       const Int_t* block = rhs.GetData();
00113       fRawBlock = new Int_t [fSize];
00114       memcpy(fRawBlock,block,sizeof(Int_t)*fSize);
00115    } else {
00116       fSize = 0;
00117    }
00118 
00119    return *this;
00120 }
00121 //_____________________________________________________________________________
00122 Int_t*  RawDataBlock::AppendToBuffer(Int_t *bstart) const
00123 {
00124    // serialize this out to a buffer
00125    memcpy(bstart,fRawBlock,sizeof(Int_t)*fSize);
00126    Int_t *buff_end = bstart + fSize;
00127    return buff_end;
00128 }
00129 
00130 //_____________________________________________________________________________
00131 void RawDataBlock::Print(Option_t *option) const
00132 {
00133    FormatToOStream(cout,option); 
00134 }
00135 
00136 //_____________________________________________________________________________
00137 std::ostream& RawDataBlock::FormatToOStream(std::ostream& os, 
00138                                             Option_t *option) const
00139 {
00140    // print out the block 
00141    // if option == 'x' or 'X' dump it in hex
00142 
00143    RawBlockId rbid = GetBlockId();
00144 
00145    os << GetName()
00146       << " size=" << fSize
00147       << endl
00148       << " checksum=0x" 
00149       << hex << setw(8) << setfill('0') 
00150       << GetChecksum() << setfill(' ') << dec
00151       << ((TestChecksum()) ? " (error)" : " (okay)")
00152       << endl
00153       << " " << rbid.AsString() 
00154       << " 0x" << hex << setw(8) << setfill('0') 
00155       << rbid.GetEncoded() << setfill(' ') << dec
00156       << endl;
00157 
00158    Bool_t inhex = (strchr(option,'x')!=0 || strchr(option,'X')!=0);
00159 
00160    if ( fSize>0 && ( inhex || fgForceHexDump ) ) {
00161 
00162       os << hex << setfill('0');
00163 
00164       // format into columns, suppress duplicate lines unless 
00165       // it is the last line
00166       // -- this could probably be done in a cleaner fashion
00167       Int_t  ncol        = 4;
00168       Int_t  ncol_last   = (fSize+ncol-1)%ncol + 1;
00169       Bool_t lastmatch   = false;
00170       Bool_t lastline    = false;
00171       Int_t *next_word   = fRawBlock;
00172       Int_t  nfull_lines = fSize/ncol;
00173       if (ncol_last == ncol) nfull_lines--; // if exact, pretend last is partial
00174       Int_t *start_last  = fRawBlock + ncol*nfull_lines;
00175       Int_t  nbytes = 0;
00176 
00177       while (next_word < fRawBlock+fSize) {
00178          // adjust down the number of columns if this is the last line
00179          if (next_word == start_last) {
00180             lastline = true;
00181             ncol = ncol_last;
00182          }
00183 
00184          Int_t *prev = next_word - ncol;
00185          if (prev >= fRawBlock && !lastline ) { 
00186             // check for match of previous line
00187             Bool_t linematch = true;
00188             Int_t *chek = next_word;
00189             for (int i=0; i<ncol; i++)
00190                linematch = ( *prev++ == *chek++ ) ? linematch : false;
00191             if (linematch) {
00192                next_word += ncol;
00193                nbytes += sizeof(Int_t)*ncol;
00194                if (!lastmatch) {
00195                   // was a match, but previous line wasn't also "*"
00196                   // and it isn't the very last line
00197                   os << "  *\n";
00198                }
00199                lastmatch = true;
00200                continue;
00201             } 
00202             else lastmatch = false;
00203 
00204          }
00205          // print offset info
00206          os << "0x" << setw(6) << nbytes << ":";
00207          // print elements of line (advance pointer and count)
00208          for (int i=0; i<ncol; i++) {
00209             os << " " << setw(8) << *next_word;
00210             next_word++;
00211             nbytes += sizeof(Int_t);
00212          }
00213          // end the line
00214          os << "\n";
00215       }
00216 
00217    }
00218 
00219    // make sure everythings back to normal
00220    os << setfill(' ') << dec;
00221    return os;
00222 }
00223 
00224 //_____________________________________________________________________________

Generated on Sat Nov 7 01:27:16 2009 for loon by  doxygen 1.3.9.1