QCrateReceiverBuffer.hh

Go to the documentation of this file.
00001 
00002 #ifndef __QCRATERECEIVERBUFFER_HH_
00003 #define __QCRATERECEIVERBUFFER_HH_
00004 
00005 #include <string>
00006 #include <vector>
00007 #include <stdexcept>
00008 #include <unistd.h>
00009 #include <iostream>
00010 #include <zlib.h>
00011 #include <math.h> // for round()
00012 
00013 #include "QCuore.hh"
00014 #include "QError.hh"
00015 #include "QConnectedSocket.hh"
00016 
00021 #define DEFAULT_BUFFER_SIZE    200000
00022 
00027 #define MAX_CHANNEL_FILE_SIZE  20000000
00028 
00029 
00048 struct QBufferStructure_t {
00049    unsigned long fId;                
00050    unsigned long fNum;               
00051    unsigned long long fRead;         
00052    unsigned long long fWrite;        
00053    unsigned long long fReadFile;     
00054    unsigned long long fNotProcessed; 
00056    unsigned long long fNotBuilt;     
00058    unsigned long fCurrentRun;        
00059    bool fNotProcessedIsActive;       
00061    bool fReadPtrIsActive;            
00063    bool fNotBuiltIsActive;           
00065    bool fSaveFile;                   
00067    unsigned long fSamplingRateHz;     
00069    long fData[DEFAULT_BUFFER_SIZE]; 
00070 };
00071 
00072 
00073 
00104 class QCrateReceiverBuffer {
00105 public:
00106 
00123    QCrateReceiverBuffer(unsigned long ch, 
00124                         bool          attach, 
00125                         bool          OpenFiles, 
00126                         int           RunNumber=0, 
00127                         bool          offset_shm=false);
00128   
00135    virtual ~QCrateReceiverBuffer();
00136   
00141    unsigned long Size() const {return DEFAULT_BUFFER_SIZE;}
00142    
00144    char* Start() const
00145    {return 
00146        (char*)(&(p_buffer->fData[(p_buffer->fRead) % DEFAULT_BUFFER_SIZE]));}
00147 
00149    inline unsigned long GetNumberOfWords() const 
00150    {return p_buffer->fNum;}
00151 
00159    bool EnableTriggerPointer();
00160 
00162    void ReleaseTriggerPointer();
00163 
00171    bool EnableBuilderPointer();
00172 
00174    void ReleaseBuilderPointer();
00175 
00183    bool EnableReadPointer();
00184 
00186    void ReleaseReadPointer();
00187 
00192    void Release(unsigned int n);
00193     
00199    void Add(int n, const long* data);
00200   
00204    void AddFromNetwork(QConnectedSocket* cs);
00205 
00207    inline unsigned long GetLg() const {return p_buffer->fId;}
00208 
00210    inline unsigned long GetRunNumber() const {return p_buffer->fCurrentRun;}
00211 
00220    inline int ReadToEnd() const 
00221    {return DEFAULT_BUFFER_SIZE - 
00222        (unsigned long)((p_buffer->fRead) % DEFAULT_BUFFER_SIZE);} /* used by DataSender */
00223 
00225    void SetProcessed(unsigned int n);
00226 
00228    void SetBuilt(unsigned int n);
00229 
00231    inline unsigned long GetToBeProcessed() const
00232    {return (unsigned long)(p_buffer->fWrite - p_buffer->fNotProcessed);}
00233 
00235    inline unsigned long GetToBeBuilt() const
00236    {return (unsigned long)(p_buffer->fNotProcessed - p_buffer->fNotBuilt);}
00237 
00239    inline unsigned long GetToBeRead() const
00240    {return (unsigned long)(p_buffer->fReadFile - p_buffer->fRead);}
00241 
00243    inline unsigned long long GetProcessed() const
00244    {return p_buffer->fNotProcessed;}
00245 
00247    inline unsigned long long GetBuilt() const
00248    {return p_buffer->fNotBuilt;}
00249 
00251    inline unsigned long long GetRead() const
00252    {return p_buffer->fRead;}
00253 
00255    inline unsigned long long GetWritten() const
00256    {return p_buffer->fWrite;}
00257    
00263    long GetData(unsigned long long index) const;
00264 
00273    long GetSample(unsigned long long index) const;
00274 
00282    inline int GetTriggerFlags(unsigned long long index,
00283                               unsigned long      mask=0x0000000F) const
00284    {return ((GetData(index) >> 17) & mask);}
00285    
00291    void FlagTrigger(unsigned long long index, int tag);
00292 
00299    void SetTriggerEnabled(bool enabled);
00300 
00302    void SetSamplingRate(unsigned long rateHz) 
00303    {p_buffer->fSamplingRateHz = rateHz;}
00304 
00306    unsigned long GetSamplingRate() const
00307    {return p_buffer->fSamplingRateHz;}
00308 
00316    long long GetTimeOfSample(unsigned long long index) const;
00317 
00323    unsigned long long GetSampleOfTime(long long timeNs) const;
00324 
00325    // some info on output stream
00326    void Dump();
00327 
00328    //
00329    //SDD FIXME
00330    // Following 2 methods are for debugging purposes
00331    // Remember to remove them as soon as they are no more useful
00332    //
00333    // set single bit as analyzed by event builder
00334    inline void SetScanned(unsigned long long i) 
00335    {p_buffer->fData[(i % DEFAULT_BUFFER_SIZE)] |= (1<<27);}
00336    // set single bit as analyzed by trigger algorythm
00337    inline void SetAnalyzed(unsigned long long i)
00338    {p_buffer->fData[(i % DEFAULT_BUFFER_SIZE)] |= (1<<28);}
00339    // store the variable "counter" in bits 27-30 
00340    inline void SetCounter(unsigned long long i, unsigned int counter)
00341    {
00342      unsigned long mask = (counter%16)<<27;
00343      p_buffer->fData[(i % DEFAULT_BUFFER_SIZE)] |= mask;
00344    }
00345 
00346    
00347 private:
00348    
00350    inline void Release();
00351 
00353    inline void SetProcessed();
00354 
00356    inline void SetBuilt();
00357 
00359    inline unsigned long GetToBeWritten() const
00360    {return (unsigned long)(p_buffer->fNotBuilt - p_buffer->fReadFile);}
00361 
00363    void WriteFile();
00364 
00366    void OpenFile();
00367 
00369    void CloseFile(bool last);
00370 
00371    int fHandle;
00372    int fShmid;                   
00373    bool fOwn;                    
00375    QBufferStructure_t *p_buffer; 
00376    gzFile zFile;                 
00377    std::string fGzFileName;      
00378    unsigned int fPartialFile;    
00379    bool fReadPtrBooked;          
00381    bool fBuilderPtrBooked;       
00384    bool fTrgPtrBooked;           
00387 };
00388 
00389 
00390 
00391 
00392 //_____________________________________________________________________________
00393 inline void QCrateReceiverBuffer::SetProcessed()
00394 {
00395    p_buffer->fNotProcessed = p_buffer->fWrite;
00396   
00397    // update builder pointer if builder is inactive
00398    if(!p_buffer->fNotBuiltIsActive) SetBuilt();
00399 }
00400 
00401 
00402 //_____________________________________________________________________________
00403 inline void QCrateReceiverBuffer::SetBuilt() 
00404 {
00405    p_buffer->fNotBuilt = p_buffer->fNotProcessed;
00406 
00407    // also update fRead pointer if it is not in use
00408    if(!p_buffer->fReadPtrIsActive) Release();
00409 }
00410 
00411 
00412 //_____________________________________________________________________________
00413 inline void QCrateReceiverBuffer::Release() 
00414 {
00415    p_buffer->fNum -= GetToBeRead();
00416    p_buffer->fRead += GetToBeRead();
00417 }
00418 
00419 #endif

Generated on Tue Nov 16 10:49:56 2010 for CUORE Software by  doxygen 1.5.6