Commit f61c0ee9 authored by Yuelong Yu's avatar Yuelong Yu
Browse files

adapt to new PNI io lib interface

parent 12ce01db
......@@ -3,38 +3,463 @@
#include <tango.h>
#include "AGIPD.h"
#include "NXFileWriter.h"
namespace AGIPD_ns
{
{
using namespace NXFileNS;
class AcquisitionThread:public omni_thread,public Tango::LogAdapter
{
public:
AcquisitionThread(AGIPD*,AGIPDInterface*,omni_mutex&);
~AcquisitionThread();
AcquisitionThread(AGIPD* _objAGIPD,AGIPDInterface* _objDet,omni_mutex& _mutexAcq)
:omni_thread(),Tango::LogAdapter(_objAGIPD),m_objAGIPD(_objAGIPD),m_objDet(_objDet),m_mutexAcq(_mutexAcq),m_bFirstFrame(true),m_lStartFrame(-1)
{
m_objNXFileWriter = new NXFileWriter();
start_undetached();
}
~AcquisitionThread()
{
delete m_objNXFileWriter;
m_objNXFileWriter = NULL;
}
void *run_undetached(void*)
{
while(1)
{
usleep(100);
{
omni_mutex_lock l(m_mutexAcq);
void *run_undetached(void*);
if(m_objAGIPD->m_bSysExit)
break;
//if starts acquisition
if(m_objAGIPD->m_bStartAcq)
AcquisitionStart();
else if(m_objAGIPD->m_bStopAcq) // acquisition stops
AcquisitionStop();
else if(m_objAGIPD->m_bAcquiringData)//acquiring data from detector
AcquiringData();
}
}
omni_thread::exit();
}
private:
void AcquisitionStart();
void AcquisitionStop();
void AcquiringData();
void AcquisitionStart()
{
m_objAGIPD->SetState(Tango::MOVING);
UpdateParameters();
if(m_bSaveAllImages)
if(!CreateFile())//check if file is created successfully
{
m_objAGIPD->m_bAcquiringData = false;
m_objAGIPD->m_bStopAcq = true;
}
m_objAGIPD->m_bAcquiringData = true;
m_objAGIPD->m_bStartAcq = false;
m_objDet->StartImaging();
}
void AcquisitionStop()
{
if(m_bSaveAllImages)
{
if(m_lAcquiredImgs != 0)
{
(*(m_objAGIPD->attr_FileStartNum_read))++;
m_objAGIPD->UpdateFileName();
}
int nRet = m_objNXFileWriter->CloseFile();
DisplayError(string("Close file"),nRet);
}
m_objDet->StopImaging();
m_objAGIPD->m_bStopAcq = false;
m_objAGIPD->m_bAcquiringData = false;
m_objAGIPD->SetState(Tango::ON);
}
void AcquiringData()
{
if(m_objDet->GetQueuedImageNumbers(0))
{
m_ptrShData = m_objDet->GetImageData(m_lCurrentFrameNo,m_shFrameErrorCode);
if(CheckImageValid(m_ptrShData,m_lCurrentFrameNo,m_shFrameErrorCode))
{
ResetFrameNumbers();
if(m_bSaveAllImages)
WriteData<short>(m_ptrShData,m_lCurrentFrameNo,m_shFrameErrorCode);
if(m_bLiveMode)
UpdateLiveImage();
UpdateAcquisitioParams();
}
}
}
/**
* @brief craete nexus file
*/
bool CreateFile()
{
int nRet = 0;
nRet = m_objNXFileWriter->FileConfig(m_strFileName
,m_bOverwrite
,m_nDataType
,m_nY,m_nX,m_nDepth
,m_nCacheSize
,m_bBuildInCompressor
,m_bCompressionEnabled
,m_bCompressionShuffleEnabled
,m_usCompressionRate
,m_bSplitEnabled
,m_nFileSizeLimit);
DisplayError(string("File config"),nRet);
if(nRet !=0 )
return false;
nRet = m_objNXFileWriter->CreateFile();
DisplayError(string("File create"),nRet);
if(nRet != 0)
return false;
nRet = m_objNXFileWriter->CreateGroup(string("/"),
string("entry"),
string("NXentry"));
DisplayError(string("group entry create"),nRet);
if(nRet != 0)
return false;
nRet = m_objNXFileWriter->CreateGroup(string("/entry/"),
string("instrument"),
string("NXinstrument"));
DisplayError(string("group instrument create"),nRet);
if(nRet != 0)
return false;
nRet = m_objNXFileWriter->CreateGroup(string("/entry/instrument/"),
string("detector"),
string("NXdetector"));
DisplayError(string("group detector create"),nRet);
if(nRet != 0)
return false;
nRet = m_objNXFileWriter->CreateGroup(string("/entry/instrument/detector/"),
string("collection"),
string("NXcollection"));
DisplayError(string("group collection create"),nRet);
if(nRet != 0)
return false;
return DumpSettings();
}
void UpdateParameters()
{
////nexus file related params
string strFileDir = m_objAGIPD->DevStringToString(*(m_objAGIPD->attr_SaveFilePath_read));
string strFileName = m_objAGIPD->DevStringToString(*(m_objAGIPD->attr_SaveFileName_read));
if(strFileDir.back() != '/')
m_strFileName = strFileDir+string("/")+strFileName;
else
m_strFileName = strFileDir+strFileName;
m_nX = m_objAGIPD->m_nSizeX;
m_nY = m_objAGIPD->m_nSizeY;
m_nDepth = m_objAGIPD->m_shDepth;
if(m_nDepth == 12)
m_nDataType = 1; //short
else if(m_nDepth == 24)
m_nDataType = 2; //int
m_bCompressionEnabled = *(m_objAGIPD->attr_CompressionEnabled_read);
m_bCompressionShuffleEnabled = *(m_objAGIPD->attr_CompressorShuffle_read);
m_usCompressionRate = *(m_objAGIPD->attr_CompressionRate_read);
m_bBuildInCompressor =false;
m_bOverwrite = true;
m_nCacheSize = 1;
m_bSplitEnabled = false;
m_nFileSizeLimit = 0;
m_strFilePostfix = string("nxs");
void WriteData(short* ptrShData, long lFrame, short shErrCode);
m_lRequestFrameNo = *(m_objAGIPD->attr_FrameNumbers_read);
m_bSaveAllImages = *(m_objAGIPD->attr_SaveAllImages_read);
m_bLiveMode = *(m_objAGIPD->attr_LiveMode_read);
m_lAcquiredImgs = 0;
m_lLossFrames = 0;
m_lCurrentFrameNo = -2;
m_bFirstFrame = true;
m_lStartFrame = -1;
*(m_objAGIPD->attr_LatestImageNumber_read) = 0;
*(m_objAGIPD->attr_TotalLossFrames_read) = 0;
*(m_objAGIPD->attr_LiveFrameNoAnalog_read) = 0;
*(m_objAGIPD->attr_LiveFrameNoDigital_read) = 0;
}
void DumpSettings();
template <typename T>
bool WriteData(T* tData,long lFrameNo,short shErrCode)
{
int nRet;
vector<T> vData(tData,tData+m_nX*m_nY);
nRet = m_objNXFileWriter->WriteData<T>(string("/entry/instrument/detector/data"),
vData,
string("/entry/instrument/detector/sequence_number"),
(long)lFrameNo,
string("/entry/instrument/detector/collection/error_code"),
(short)shErrCode);
if(nRet != 0)
return false;
return true;
}
/**
* @brief update live image
*/
void UpdateLiveImage()
{
//analog one
if((m_lCurrentFrameNo%2)!=0)
{
//in live mode
if(m_bLiveMode && ((m_lCurrentFrameNo+1)%704 == 0))
{
copy(m_ptrShData,m_ptrShData+m_nX*m_nY,m_objAGIPD->attr_LiveDataAnalog_read);
*(m_objAGIPD->attr_LiveFrameNoAnalog_read) = m_lCurrentFrameNo;
}
}
else //digital one
{
if(m_bLiveMode && ( m_lCurrentFrameNo%704 == 0))
{
copy(m_ptrShData,m_ptrShData+m_nX*m_nY,m_objAGIPD->attr_LiveDataDigital_read);
*(m_objAGIPD->attr_LiveFrameNoDigital_read) = m_lCurrentFrameNo;
}
}
}
template <typename T>
bool CheckImageValid(T* tData, long lFrameNo,short shErrCode)
{
//image invalid
return !((tData == NULL)
&& (lFrameNo == -1)
&& (shErrCode == -1)
&& (m_lAcquiredImgs < m_lRequestFrameNo));
}
void ResetFrameNumbers()
{
//reset frame numbers
if(m_bFirstFrame)
{
m_lStartFrame = m_lCurrentFrameNo;
m_bFirstFrame = false;
}
m_lCurrentFrameNo = m_lCurrentFrameNo-m_lStartFrame+1;
}
void UpdateAcquisitioParams()
{
//check if image is error
if(m_shFrameErrorCode!=0)
{
m_lLossFrames++;
*(m_objAGIPD->attr_TotalLossFrames_read) = m_lLossFrames;
}
*(m_objAGIPD->attr_LatestImageNumber_read) = m_lCurrentFrameNo;
m_lAcquiredImgs++;
//acq has finished
if(m_lAcquiredImgs >= m_lRequestFrameNo)
{
m_objAGIPD->m_bAcquiringData = false;
m_objAGIPD->m_bStartAcq = false;
m_objAGIPD->m_bStopAcq = true;
cout<<"Acquisition Finished!!!"<<endl;
cout<<"Already acquired images: "<<m_lAcquiredImgs<<endl;
}
}
bool DumpSettings()
{
int nRet;
//ADCLatency
nRet = m_objNXFileWriter->DumpAllAttributesFromTango(string("/entry/instrument/detector/collection/"),"adc_latency",*(m_objAGIPD->attr_ADCLatency_read));
DisplayError(string("write adc_lantency"),nRet);
if(nRet != 0)
return false;
//ADCTrigger
nRet = m_objNXFileWriter->DumpAllAttributesFromTango(string("/entry/instrument/detector/collection/"),"adc_tigger",*(m_objAGIPD->attr_ADCTrigger_read));
DisplayError(string("write adc_trigger"),nRet);
if(nRet != 0)
return false;
//ASICCS
nRet = m_objNXFileWriter->DumpAllAttributesFromTango(string("/entry/instrument/detector/collection/"),"asic_cs",string(*(m_objAGIPD->attr_ASICCS_read)));
DisplayError(string("write asic_cs"),nRet);
if(nRet != 0)
return false;
//ASICCS
nRet = m_objNXFileWriter->DumpAllAttributesFromTango(string("/entry/instrument/detector/collection/"),"asic_ps",string(*(m_objAGIPD->attr_ASICPS_read)));
DisplayError(string("write asic_ps"),nRet);
if(nRet != 0)
return false;
//integration time
nRet = m_objNXFileWriter->DumpAllAttributesFromTango(string("/entry/instrument/detector/collection/"),"integration_time",(int)(*(m_objAGIPD->attr_IntegrationTime_read)));
DisplayError(string("write integration_time"),nRet);
if(nRet != 0)
return false;
//SaveFilePath
nRet = m_objNXFileWriter->DumpAllAttributesFromTango(string("/entry/instrument/detector/collection/"),"save_file_path",string(*(m_objAGIPD->attr_SaveFilePath_read)));
DisplayError(string("write save_file_path"),nRet);
if(nRet != 0)
return false;
//FrameNumbers
nRet = m_objNXFileWriter->DumpAllAttributesFromTango(string("/entry/instrument/detector/collection/"),"frame_numbers",(int)(*(m_objAGIPD->attr_FrameNumbers_read)));
DisplayError(string("write frame_numbers"),nRet);
if(nRet != 0)
return false;
//IntegrationOffset
nRet = m_objNXFileWriter->DumpAllAttributesFromTango(string("/entry/instrument/detector/collection/"),"integration_offset",(int)(*(m_objAGIPD->attr_IntegrationOffset_read)));
DisplayError(string("write integration_offset"),nRet);
if(nRet != 0)
return false;
//LiveMode
nRet = m_objNXFileWriter->DumpAllAttributesFromTango(string("/entry/instrument/detector/collection/"),"live_mode",*(m_objAGIPD->attr_LiveMode_read));
DisplayError(string("write live_mode"),nRet);
if(nRet != 0)
return false;
//PatternFile
nRet = m_objNXFileWriter->DumpAllAttributesFromTango(string("/entry/instrument/detector/collection/"),"pattern_file",string(*(m_objAGIPD->attr_PatternFile_read)));
DisplayError(string("write pattern_file"),nRet);
if(nRet != 0)
return false;
//SaveAllImages
nRet = m_objNXFileWriter->DumpAllAttributesFromTango(string("/entry/instrument/detector/collection/"),"save_all_images",*(m_objAGIPD->attr_SaveAllImages_read));
DisplayError(string("write save_all_images"),nRet);
if(nRet != 0)
return false;
//CompressionEnabled
nRet = m_objNXFileWriter->DumpAllAttributesFromTango(string("/entry/instrument/detector/collection/"),"compression_enabled",*(m_objAGIPD->attr_CompressionEnabled_read));
DisplayError(string("write compression_enabled"),nRet);
if(nRet != 0)
return false;
//CompressionShuffle
nRet = m_objNXFileWriter->DumpAllAttributesFromTango(string("/entry/instrument/detector/collection/"),"compressor_shuffle",(int)(*(m_objAGIPD->attr_CompressorShuffle_read)));
DisplayError(string("write compressor_shuffle"),nRet);
if(nRet != 0)
return false;
//CompressionRate
nRet = m_objNXFileWriter->DumpAllAttributesFromTango(string("/entry/instrument/detector/collection/"),"compression_rate",(int)(*(m_objAGIPD->attr_CompressionRate_read)));
DisplayError(string("write compression_rate"),nRet);
if(nRet != 0)
return false;
//FileSplitEnabled
nRet = m_objNXFileWriter->DumpAllAttributesFromTango(string("/entry/instrument/detector/collection/"),"file_split_enabled",*(m_objAGIPD->attr_FileSplitEnabled_read));
DisplayError(string("write file_split_enabled"),nRet);
if(nRet != 0)
return false;
//FileSizeLimit
nRet = m_objNXFileWriter->DumpAllAttributesFromTango(string("/entry/instrument/detector/collection/"),"file_size_limit",(int)(*(m_objAGIPD->attr_FileSizeLimit_read)));
DisplayError(string("write file_size_limit"),nRet);
if(nRet != 0)
return false;
//ChukSize
nRet = m_objNXFileWriter->DumpAllAttributesFromTango(string("/entry/instrument/detector/collection/"),"chunk_size",(int)(*(m_objAGIPD->attr_ChunkSize_read)));
DisplayError(string("write chunk_size"),nRet);
if(nRet != 0)
return false;
//TotalLossFrames
nRet = m_objNXFileWriter->DumpAllAttributesFromTango(string("/entry/instrument/detector/collection/"),"total_loss_frames",(int)(*(m_objAGIPD->attr_TotalLossFrames_read)));
DisplayError(string("write total_loss_frames"),nRet);
if(nRet != 0)
return false;
}
void DisplayError(string strMsg,int nRet)
{
if(nRet != 0)
cout<<"Error:"<<strMsg<<".code:"<<nRet<<endl;
}
private:
/// member variables
AGIPDInterface* m_objDet;
AGIPD* m_objAGIPD;
omni_mutex& m_mutexAcq;
long m_lFrameNo;
short m_shErrCode;
long m_lRequestFrameNo;
long m_lCurrentFrameNo;
short m_shFrameErrorCode;
short* m_ptrShData;
long m_lAcquiredImgs;
bool m_bFirstFrame;
long m_lStartFrame;
bool m_bFirstFrame;
long m_lStartFrame;
bool m_bLiveMode;
long m_lLossFrames;
bool m_bSaveAllImages;
NXFileWriter* m_objNXFileWriter;
////nexus file related params
string m_strFileName;
string m_strFilePostfix;
bool m_bOverwrite;
//0:char
//1:short
//2:int
int m_nDataType;
int m_nX,
m_nY,
m_nDepth;
int m_nCacheSize;
bool m_bBuildInCompressor,
m_bCompressionEnabled,
m_bCompressionShuffleEnabled,
m_bPreCompression;
unsigned short m_usCompressionRate;
int m_nFileSizeLimit;
bool m_bSplitEnabled;
};
}
......
......@@ -29,68 +29,64 @@ LIB_PNIIO_CONFIG = $(shell pkg-config --libs pniio)
CPPFLAGS_PNIIO_CONFIG = $(shell pkg-config --cflags pniio)
ifndef _ARCH
_ARCH = $(shell uname -m)
endif
ifeq ($(_ARCH), x86_64)
libdir=lib64
else
libdir=lib
endif
# ifndef _ARCH
# _ARCH = $(shell uname -m)
# endif
# ifeq ($(_ARCH), x86_64)
# libdir=lib64
# else
# libdir=lib
# endif
libdir = lib
INC_DIR_USER= -I/usr/local/include/tango \
-I/usr/local/include \
-I. \
-I../../bin/usr/include/agipd
INC_DIR_USER= -I/usr/include/tango \
-I/usr/include \
-I. \
-I../../bin/usr/include/agipd
LIB_DIR_USER= -L /usr/local/$(libdir) -L../../bin/usr/lib
LIB_DIR_USER= -L /usr/$(libdir) -L../../bin/usr/lib
LFLAGS_USR= $(RELEASE_TYPE) -Wall $(LIB_DIR_USER) \
$(LIB_PNIIO_CONFIG) \
-ltango \
-llog4tango \
-lCOS4\
-lomniORB4 \
-lomniDynamic4 \
-lomnithread \
-ldl \
-lpthread \
-lstdc++ \
-lagipd
$(LIB_PNIIO_CONFIG) \
-ltango \
-llog4tango \
-lCOS4\
-lomniORB4 \
-lomniDynamic4 \
-lomnithread \
-ldl \
-lpthread \
-lstdc++ \
-lagipd \
-lhdf5_hl
#used for time measurement during compression mode
#CXXFLAGS_USR =-O2 -DTIMEMEASUREMENT -std=c++0x $(RELEASE_TYPE) -D_REENTRANT \
$(CPPFLAGS_PNIIO_CONFIG) $(INC_DIR_USER)
$(CPPFLAGS_PNIIO_CONFIG) $(INC_DIR_USER)
CXXFLAGS_USR =-O3 -std=c++0x $(RELEASE_TYPE) -D_REENTRANT \
$(CPPFLAGS_PNIIO_CONFIG) $(INC_DIR_USER)
$(CPPFLAGS_PNIIO_CONFIG) $(INC_DIR_USER)
SVC_INC = $(PACKAGE_NAME).h $(PACKAGE_NAME)Class.h \
AcquisitionThread.h \
DetectorSimulation.h \
NXFileSavingBuilder.h \
NXFileSaving.h
DetectorSimulation.h \
AcquisitionThread.h \
NXFileWriter.h \
NXWrapper.h
SVC_OBJS = $(OBJS_DIR)/main.o \
$(OBJS_DIR)/ClassFactory.o \
$(OBJS_DIR)/$(PACKAGE_NAME)StateMachine.o \
$(OBJS_DIR)/$(PACKAGE_NAME)Class.o \
$(OBJS_DIR)/$(PACKAGE_NAME).o \
$(OBJS_DIR)/AcquisitionThread.o \
$(OBJS_DIR)/DetectorSimulation.o \
$(OBJS_DIR)/NXFileSaving.o \
$(OBJS_DIR)/NXFileSavingBuilder.o
$(OBJS_DIR)/ClassFactory.o \
$(OBJS_DIR)/$(PACKAGE_NAME)StateMachine.o \
$(OBJS_DIR)/$(PACKAGE_NAME)Class.o \
$(OBJS_DIR)/$(PACKAGE_NAME).o \
$(OBJS_DIR)/DetectorSimulation.o
$(OBJS_DIR)/%.o: %.cpp $(SVC_INC)
$(CXX) $(CXXFLAGS_USR) -c $< -o $(OBJS_DIR)/$*.o
$(CXX) $(CXXFLAGS_USR) -c $< -o $(OBJS_DIR)/$*.o
all:$(PACKAGE_NAME)
$(PACKAGE_NAME): $(SVC_OBJS)
$(CXX) $(SVC_OBJS) -o $(BIN_DIR)/$(PACKAGE_NAME) $(LFLAGS_USR)
\ No newline at end of file
$(CXX) $(SVC_OBJS) -o $(BIN_DIR)/$(PACKAGE_NAME) $(LFLAGS_USR)
Supports Markdown
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