//*-- Author :    Damir Buskulic   20/05/99

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// VFramesBuffer                                                        //
//                                                                      //
// Buffer of frames used as a cache when reading frames                 //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#include "VFramesBuffer.h"
#include "VFrUtil.h"

ClassImp(VFramesBuffer)

//______________________________________________________________________________
VFramesBuffer::VFramesBuffer(VFrameChannel* vfrch)
{
// Frames buffer constructor
   int i;
   mBufSize = 10;
   mFramesBuf = new FrameH*[mBufSize];
   for (i=0;i<mBufSize;i++) mFramesBuf[i] = 0; // no index in array
   mMetaData = new VMetaData[mBufSize];
//   for (i=0;i<mBufSize;i++) mMetaIndex[i]; // no metadata in array
   mIsComplete = new Int_t[mBufSize];
   for (i=0;i<mBufSize;i++) mIsComplete[i] = -1; // no index in array
   mBufCur = -1;
   mFrameChannel = vfrch;
}

//______________________________________________________________________________
VFramesBuffer::~VFramesBuffer()
{
// Frames Buffer destructor
   for (int i=0;i<mBufSize;i++) { if (mFramesBuf[i]) FrameFree(mFramesBuf[i]);}
   delete [] mFramesBuf;
   delete [] mMetaData;
   delete [] mIsComplete;
}

//______________________________________________________________________________
 void VFramesBuffer::PutInBuffer(FrameH* frame, VMetaData* meta)
{
// Puts frame in the buffer, drops the oldest one.
   
   mBufCur++;
   if (mBufCur>=mBufSize) mBufCur = 0;
   if (mFramesBuf[mBufCur]>0) FrameFree(mFramesBuf[mBufCur]);
   mFramesBuf[mBufCur] = frame;
   mMetaData[mBufCur] = (*meta);
}

//______________________________________________________________________________
 void VFramesBuffer::Rearrange(Int_t ifrinbuf)
{
// Rearrange the buffer so that the element ifrinbuf becomes the first.
// This element is in general the last retrieved one
     
   FrameH** frbuftmp;
   VMetaData* frmetatmp;
   Int_t* friscomplete;

   if (ifrinbuf!=mBufCur) {
      frbuftmp = new FrameH*[mBufSize];
      frmetatmp = new VMetaData[mBufSize];
      friscomplete = new Int_t[mBufSize];
      Int_t ibuf = mBufCur;
      for (Int_t j=0;j<mBufSize-1;j++) {
         ibuf++;
         if (ibuf>mBufSize-1) ibuf=0;
         if (ibuf == ifrinbuf) ibuf++;
         if (ibuf>mBufSize-1) ibuf=0;
         frbuftmp[j] = mFramesBuf[ibuf];
         frmetatmp[j] = mMetaData[ibuf];
         friscomplete[j] = mIsComplete[ibuf];
      }
      frbuftmp[mBufSize-1] = mFramesBuf[ifrinbuf];
      frmetatmp[mBufSize-1] = mMetaData[ifrinbuf];
      friscomplete[mBufSize-1] = mIsComplete[ifrinbuf];
      mBufCur = mBufSize-1;
      delete [] mFramesBuf;
      delete [] mMetaData;
      delete [] mIsComplete;
      mFramesBuf = frbuftmp;
      mMetaData = frmetatmp;
      mIsComplete = friscomplete;
   }
}

//______________________________________________________________________________
 FrameH* VFramesBuffer::GetFrame(VMetaData* meta)
{
// Gets frame pointed to by metadata at position imeta.
   
   FrameH* frameout;
   Int_t ifrinbuf;
   
// Search for the frame in the buffer
   ifrinbuf=mBufSize-1;
   while (mMetaData[ifrinbuf]!=(*meta) && ifrinbuf>=0) ifrinbuf--;
   if (ifrinbuf<0) {
// If not found in buffer, retrieve from disk
      frameout = mFrameChannel->GetFrameDirectB(meta);
      if (frameout) {
         PutInBuffer(frameout,meta);
         mIsComplete[mBufCur] = 1;
      }         
   } else {
// If found in buffer, after check that it is a complete frame, return it.
      if (!mIsComplete[ifrinbuf]) {
         frameout = mFrameChannel->GetFrameDirectB(meta);
         if (frameout) {
            FrameFree(mFramesBuf[ifrinbuf]);
            mFramesBuf[ifrinbuf] = frameout;
         } else return 0;
      }
      frameout = mFramesBuf[ifrinbuf];
// Rearrange buffer as the newest retrieved was not the one pointed to
// by mBufCur
      Rearrange(ifrinbuf);
   }
   
   return frameout;
}

//______________________________________________________________________________
 FrVect* VFramesBuffer::GetVectCopy(VMetaData* meta, char* vectname)
// Get a copy of the vector in the frame pointed to by metadata at position
// imeta which name is "vectname". 
//   The string vectname indicates the type (adc, proc, sim) and
// the name of the series to be extracted. The convention for the format
// is "type.name".
//   For example "adc.IFO_DMRO" is a good format.
{return 0;}   

//______________________________________________________________________________
 FrVect* VFramesBuffer::GetVect(VMetaData* meta, char* vectname)
{
// Get vector in the frame pointed to by metadata at position imeta
// which name is "vectname". 
//   The string vectname indicates the type (adc, proc, sim) and
// the name of the series to be extracted. The convention for the format
// is "type.name".
//   For example "adc.IFO_DMRO" is a good format.
   
   FrVect* vectout;
   Int_t ifrinbuf;
   FrameH* frame1vect;
   FrameH* frame;
   Int_t vecttype;
   FrAdcData*  adcdat=0;
   FrProcData* procdat=0;
   FrSimData*  simdat=0;
   FrRawData*  rawdat=0;
   
// Search for the right frame in the buffer
   ifrinbuf=mBufSize-1;
   while (mMetaData[ifrinbuf]!=(*meta) && ifrinbuf>=0) ifrinbuf--;
   if (ifrinbuf<0) {
// If not found in buffer, retrieve from disk an empty frame
      frame1vect = mFrameChannel->GetFrameDirectB(meta,"empty");
      if (!frame1vect) return 0;
      PutInBuffer(frame1vect,meta);
      mIsComplete[mBufCur] = 0;
      ifrinbuf = mBufCur;
   }
// If frame found in buffer, check if the vector is present in this frame
   vectout = VGetVect(mFramesBuf[ifrinbuf],vectname);
// If not present, load it from disk and assign it to this frame
   if (!vectout && !mIsComplete[ifrinbuf]) {
      vecttype = VGetVectType(vectname);
      frame = mFramesBuf[ifrinbuf];
      if (vecttype==1 || !vecttype) {
    // adc case
         adcdat = mFrameChannel->GetAdcDirectB(meta, vectname);
      // connect the adc to the existing frame
         if (adcdat && frame) {
            if(!frame->rawData) FrRawDataNew(frame);
            rawdat = frame->rawData;
            if(!rawdat && vecttype) return(0);
            adcdat->next = rawdat->firstAdc;
            rawdat->firstAdc = adcdat;
            vectout = VGetVect(frame,vectname); // use VGetVect to force call to FrAdcDataFind
                                                // this will recalculate GTime of the vector
         } else if (vecttype) return 0;

      } 
      if (vecttype==2 || !vecttype) {
   // proc case
         procdat = mFrameChannel->GetProcDirectB(meta, vectname);
      // connect the proc to the existing frame
         if (procdat && frame) {
            procdat->next = frame->procData;
            frame->procData = procdat;
            vectout = VGetVect(frame,vectname);
         } else if (vecttype) return 0;
         
      } 
      if (vecttype==3 || !vecttype) {
   // sim case
         simdat = mFrameChannel->GetSimDirectB(meta, vectname);
      // connect the sim to the existing frame
         if (simdat && frame) {
            simdat->next = frame->simData;
            frame->simData = simdat;
            vectout = VGetVect(frame,vectname);
         } else if (vecttype) return 0;
      }
      if (!vectout) return 0;
   } else if(!vectout && mIsComplete[ifrinbuf]) return 0;  // should never happen (a complete frame with an unknown vector)

// Rearrange buffer as the newest retrieved was not the one pointed to
// by mBufCur
   Rearrange(ifrinbuf);
   
   return vectout;
}
//______________________________________________________________________________
 FrameH* VFramesBuffer::GetFrameCopy(VMetaData* meta)
{
// Gets a copy of the frame pointed to by metadata at position imeta.
// This is used when one wants to keep control over deletion of his frames

   FrameH* frame, *frcopy;
   
   frame = GetFrame(meta);
   if (frame) {
      frcopy = FrameCopy(frame);
   } else {
      frcopy = 0;
   }
   return frcopy;
}


- ROOT page - VEGA page - Class index - Top of the page

This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to , or contact with any questions or problems regarding ROOT or VEGA.