Commit f7bf0067 authored by dino's avatar dino
Browse files

The class CGaspBuffer from the PRISMA library has been copied to agapro/common...

The class CGaspBuffer from the PRISMA library has been copied to agapro/common with the new name GaspBuffer

git-svn-id: svn://gal-serv.lnl.infn.it/agata/trunk/narval_emulator@1208 170316e4-aea8-4b27-aad4-0380ec0519c9
parent 5c535718
......@@ -61,6 +61,7 @@ ${ACTORS}/common/adetParams.cpp
${ACTORS}/common/TstampFilter.cpp
${ACTORS}/common/TextReader.cpp
${ACTORS}/common/ConfReader.cpp
${ACTORS}/common/GaspBuffer.cpp
${ACTORS}/common/MixFFT.cpp
${ACTORS}/producers/Crystal/CrystalProducer.cpp
${ACTORS}/producers/Crystal/CrystalProducerATCA.cpp
......
......@@ -858,6 +858,14 @@
RelativePath="..\common\CycleServer.h"
>
</File>
<File
RelativePath="..\common\GaspBuffer.cpp"
>
</File>
<File
RelativePath="..\common\GaspBuffer.h"
>
</File>
<File
RelativePath="..\common\Misc.cpp"
>
......
#include "GaspBuffer.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <ios>
#define BUFLENGTH 8192
using namespace std;
GaspBuffer::GaspBuffer()
{
pData = new unsigned short int[BUFLENGTH];
ResetBuffer();
cerr << " Buffer allocated and ready." << endl;
currentRecord = 0;
runNumber = 0;
bufferPosition = 0;
maxFileSize = 2040109465; // 95% of 2GB
fileNumber = 0;
}
GaspBuffer::~GaspBuffer()
{
if(pData) delete [] pData;
}
void GaspBuffer::ResetBuffer()
{
memset( pData, 0, BUFLENGTH*sizeof(short int) );
}
void GaspBuffer::Reset()
{
ResetBuffer();
}
void GaspBuffer::RecordHeader()
{
//G4cerr << " New header " << G4endl;
int size = 16;
unsigned short int* header = new unsigned short int[size];
memset( header, 0, size*sizeof(short int) );
header[1] = (unsigned short int)(currentRecord >> 8);
header[2] = (unsigned short int) runNumber;
if( firstHeader ) {
header[3] = (unsigned short int)0x4748; // reduced GASP format
firstHeader = false;
}
else
header[3] = (unsigned short int)0x4758; // reduced GASP format
memcpy( pData, header, size*sizeof(short int) );
//for(int ii=0; ii<size; ii++ )
// pData[ii] = header[ii];
bufferPosition = size;
}
void GaspBuffer::BeginOfRun(int number)
{
SetRunNumber( number+1 );
fileNumber = 0;
firstHeader = true;
OpenFile();
RecordHeader();
//FlushBuffer(); // why should it write an empty buffer ??
}
void GaspBuffer::OpenFile()
{
char specname[32];
if( fileNumber > 0 )
sprintf(specname, "RUN.%4.4d.%2.2d", runNumber, fileNumber);
else
sprintf(specname, "RUN.%4.4d", runNumber);
fileName = filePrefix + std::string(specname);
fout.open(fileName.c_str(), ios::out | ios::binary);
if(!fout.is_open()) {
cerr << " Could not open output file (" << fileName << ")! " << endl;
return;
}
else {
cerr << " Run file " << fileName << " opened." << endl;
fileNumber++;
}
}
void GaspBuffer::CloseFile()
{
fout.close();
cerr << " Run file " << fileName << " closed." << endl;
}
void GaspBuffer::SplitFile()
{
CloseFile();
OpenFile();
}
void GaspBuffer::EndOfRun()
{
FlushBuffer();
CloseFile();
}
void GaspBuffer::FlushBuffer()
{
if( ( sizeof(short int)*BUFLENGTH + (unsigned int)GetFileSize() ) >= (unsigned int)maxFileSize )
SplitFile();
fout.write((const char *)pData, sizeof(short int)*BUFLENGTH);
memset( pData, 0, BUFLENGTH*sizeof(short int) );
currentRecord++;
//cerr << " Flushed buffer ... " << currentBuffer << endl;
RecordHeader();
}
void GaspBuffer::AddToBuffer( unsigned short int* event, int size )
{
if( (bufferPosition + size) >= BUFLENGTH )
FlushBuffer();
//cerr << " currentBuffer is " << currentBuffer << " " << bufferPosition << endl;
//for(int ii=0; ii<size; ii++ )
// pData[bufferPosition+ii] = event[ii];
// will overflow if bufferPosition+size >= BUFLENGTH
memcpy( pData+bufferPosition, event, size*sizeof(short int) );
bufferPosition += size;
}
#ifndef GASPBUFFER_H_INCLUDED
#define GASPBUFFER_H_INCLUDED 1
// This class written by Enrico Farnea to write GASP formatted data
// is normally part of the PRISMA library.
// It has been copied to agapro/common to use it independent of the PRISMA stuff.
// To avoid clashes, the name the copy has ben changed from CGaspBuffer to GaspBuffer
#include <vector>
#include <fstream>
#include <string.h>
using namespace std;
class GaspBuffer
{
public:
GaspBuffer();
~GaspBuffer();
private:
unsigned short int *pData;
private:
int currentRecord;
int bufferPosition;
private:
int runNumber;
bool firstHeader;
private:
fstream fout;
private:
std::string filePrefix;
std::string fileName;
int maxFileSize;
int fileNumber;
public:
void Reset ();
void AddToBuffer ( unsigned short int *, int );
public:
void SetPrefix (std::string pfx) {filePrefix = pfx;}
void BeginOfRun ( int );
void EndOfRun ();
public:
void SetRunNumber ( int );
private:
void FlushBuffer ();
void ResetBuffer ();
void RecordHeader ();
private:
void OpenFile();
void CloseFile();
void SplitFile();
public:
long long GetFileSize ();
};
inline void GaspBuffer::SetRunNumber( int number )
{
runNumber = number;
}
inline long long GaspBuffer::GetFileSize()
{
return fout.tellp();
}
#endif
......@@ -185,7 +185,7 @@ const int defTriggerSample = 10; // 60-10=50 samples passed to the PSA
//#define TRF_ROOTTREE // enable the root tree in this actor (ENABLED IN THE MAKEFILE)
//#define TRF_WRITE_TRACKED // enable writing the tracked gammas into the Oft_TrackedEnergies ascii file
//#define TRF_WRITE_GSORT // enable writing input and tracked data in gasp format
#define TRF_WRITE_GSORT // enable writing input and tracked data in gasp format
#define TFR_WRITE_HITS_MGT // enable writing the input hits in mgt format
#define TRF_MULTIHIST // enable MultiHist spectra
//#define TRF_FromGRU // enable GRU/ViGRU
......
......@@ -398,7 +398,7 @@ void GlobalFilter::process_initialise (UInt_t *error_code)
}
gaspEvent = new unsigned short [8192];
memset(gaspEvent, 0, sizeof(unsigned short)*8192);
gaspBuffer = new CGaspBuffer();
gaspBuffer = new GaspBuffer();
gaspBuffer->SetPrefix(fOdirPrefix+"Gsort_");
gaspBuffer->BeginOfRun( gaspLUT[599]-1 ); // run number
}
......
......@@ -39,7 +39,7 @@
#endif
#ifdef GLF_WRITE_GSORT
#include "CGaspBuffer.hh"
#include "GaspBuffer.h"
#endif // GLF_WRITE_GSORT
//! Base class for the global filter to check event integrity
......@@ -405,7 +405,7 @@ public:
#ifdef GLF_WRITE_GSORT
private:
CGaspBuffer * gaspBuffer;
GaspBuffer * gaspBuffer;
unsigned short * gaspEvent;
int gaspEvLen;
int gaspLUT[600];
......
......@@ -449,7 +449,7 @@ void TrackingFilter::process_initialise (UInt_t *error_code)
}
gaspEvent = new unsigned short [8192];
memset(gaspEvent, 0, sizeof(unsigned short)*8192);
gaspBuffer = new CGaspBuffer();
gaspBuffer = new GaspBuffer();
gaspBuffer->SetPrefix(fOdirPrefix+"Gsort_");
gaspBuffer->BeginOfRun( gaspLUT[599]-1 ); // run number
}
......
......@@ -52,7 +52,7 @@
#endif
#ifdef TRF_WRITE_GSORT
#include "CGaspBuffer.hh"
#include "GaspBuffer.h"
#endif // TRF_WRITE_GSORT
//! Base class for Tracking filters
......@@ -432,7 +432,7 @@ public:
#ifdef TRF_WRITE_GSORT
private:
CGaspBuffer * gaspBuffer;
GaspBuffer * gaspBuffer;
unsigned short * gaspEvent;
int gaspEvLen;
int gaspLUT[600];
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment