From 4d069f990ce00965b83c86ca2a39f0457d1ad1fd Mon Sep 17 00:00:00 2001 From: Kamil Sedlak Date: Tue, 22 Nov 2011 12:03:49 +0000 Subject: [PATCH] 22.11.2011 Kamil Sedlak Two files which I forgot to submitt to svn last time are now added. --- musrSimAna/musrWriteDump.cxx | 209 +++++++++++++++++++++++++++++++++++ musrSimAna/musrWriteDump.hh | 59 ++++++++++ 2 files changed, 268 insertions(+) create mode 100644 musrSimAna/musrWriteDump.cxx create mode 100644 musrSimAna/musrWriteDump.hh diff --git a/musrSimAna/musrWriteDump.cxx b/musrSimAna/musrWriteDump.cxx new file mode 100644 index 0000000..0ca9982 --- /dev/null +++ b/musrSimAna/musrWriteDump.cxx @@ -0,0 +1,209 @@ +#define musrWriteDump_cxx +#include "musrWriteDump.hh" + + +FILE* musrWriteDump::fpw = NULL; +std::ofstream musrWriteDump::textDumpFile; +unsigned int musrWriteDump::get_lwords = 0; +unsigned int musrWriteDump::blt_data[MAX_NUMBER_LWORDS]; + +musrWriteDump::musrWriteDump(char* name, int clock_ch, int maxTimeBinJitter) { + char filename[255]; + sprintf(filename,"data/TDC_V1190_200_dump_%s.bin",name); + fpw = fopen(filename,"wb"); + if (fpw==NULL) { + std::cout<<"musrWriteDump::musrWriteDump: ERROR!!! output dump file "< S T O P F O R C E D !!!"< S T O P F O R C E D !!!"<(0,-1)); +} + +//---------------------------------------------------------------------------- +musrWriteDump::~musrWriteDump() { + // write out remaining ~20 evnets into the dump file + flush_to_dump(); + + if (fpw != NULL) { + fclose(fpw); + fpw = NULL; + } + if (textDumpFile.is_open()) textDumpFile.close(); +} +//---------------------------------------------------------------------------- +void musrWriteDump::send_to_dump(unsigned int channel, Long64_t tdcBin, int flush) { + // First do some specific changes: + if (channel==51) channel=11; + if (channel==52) channel=12; + if (channel==102) channel=0; + + if (channel>=0) { + kamilHitMultimap.insert(std::pair(tdcBin,channel) ); + } + + if (kamilHitMultimap.size()>20) { + it = kamilHitMultimap.begin(); + if (MAX_TIME_JITTER>0) { + // randomise the hits in time in the dump file - usefull for the tests + // of Andrea's analysis program. + it1 = kamilHitMultimap.begin(); + it2 = it1; it2++; + it3 = it2; it3++; + it4 = it3; it4++; + it5 = it4; it5++; + it6 = it5; it6++; + double prob = (float)rand()/RAND_MAX; + std::cout<<"IIIIIIIIIIIIIIII prob= "<second<1) { + it = kamilHitMultimap.begin(); + Long64_t tdcBin = it->first; + int ch_num = it->second; + if (ch_num==-1) { // clock channel + kamilHitMultimap.insert(std::pair(tdcBin + CLOCK_INTERVAL,-1)); + ch_num = clock_channel; + } + int tdcDifference = tdcBin - tdcBin0; + while (tdcDifference>=TDC_BIT_RANGE) { + tdcBin0 += TDC_BIT_RANGE; + tdcDifference = tdcBin - tdcBin0; + } + + if (kamilHitMultimap.size()==2) write_to_dump(ch_num, tdcDifference, true); + else write_to_dump(ch_num, tdcDifference, false); + + kamilHitMultimap.erase(it); + } +} +//---------------------------------------------------------------------------- + +/* + * write_to_dump write TDC event to dump file + * + * channel TDC channel + * tdctime TDC event time + * flush <> 0 flush buffer to file + * + * status returned: + * > 0 : number of events flushed to file + * 0 : event written to bu ffer + * -1 : invalid channel or tdctime + * -2 : invalid file handle + * -3 : error writing to file + */ + +int musrWriteDump::write_to_dump(unsigned int channel, unsigned int tdctime, int flush) { + int status; + + // std::cout<= 0) && (channel < N_TDC_CHANNELS)) { + if ((tdctime >= 0) && (tdctime < TDC_BIT_RANGE)) { +#ifdef OS_LINUX + V1190_DATA *v1190_data; +#endif + +#ifdef OS_LINUX + v1190_data = (V1190_DATA *) &blt_data[get_lwords]; + + v1190_data->channel = channel; + v1190_data->data = tdctime; + +#else // bit fields do not work for windows + +#ifdef TDCV1290 + blt_data[get_lwords] = tdctime + (channel << 21); +#else + blt_data[get_lwords] = tdctime + (channel << 19); +#endif + +#endif + + get_lwords++; + status = 0; + } + } + + // buffer filled or flush buffer to file + if ((get_lwords >= MAX_NUMBER_LWORDS) || (flush && (get_lwords > 0))) { + + if (fpw != NULL) { + status = (int) get_lwords; + +/* + * TDC control register: BERR EN bit set -> Bus error to finish block transfer or during + * the empty buffer readout + * + * Possible values of get_lwords: + * 0 flag previous TDC buffer was empty + * 32*1024 flag previous TDC buffer was completely filled + * + * 1-1023 TDC buffer is empty now + * generates return status code 0x211 : VME buffer empty now flag + * 1024 TDC buffer data available + * generates return status code 0x000 : Status OK + */ + + // write number of events currently in buffer to file + if (fwrite(&get_lwords, sizeof(unsigned int), 1, fpw) != 1) { + printf("ERROR %d writing # of data words to dump file\n",ferror(fpw)); + status = -3; + } + + // write buffer contents to file + if (fwrite(blt_data, sizeof(unsigned int), get_lwords, fpw) != get_lwords) { + printf("ERROR %d writing blt_data to dump file\n",ferror(fpw)); + status = -3; + } + } else { + status = -2; + } + + get_lwords = 0; + } + + return status; +} + +//---------------------------------------------------------------------------- diff --git a/musrSimAna/musrWriteDump.hh b/musrSimAna/musrWriteDump.hh new file mode 100644 index 0000000..d83d0a9 --- /dev/null +++ b/musrSimAna/musrWriteDump.hh @@ -0,0 +1,59 @@ +#ifndef musrWriteDump_h +#define musrWriteDump_h 1 +#include +#include +#include +#include + +class musrWriteDump { + public : + musrWriteDump(char* name, int clock_ch, int maxTimeBinJitter); + ~musrWriteDump(); + void send_to_dump(unsigned int channel, Long64_t tdcBin, int flush); + void flush_to_dump(); + + private: + int write_to_dump(unsigned int channel, unsigned int tdctime, int flush); + + static const int N_TDC_CHANNELS = 32; + static const int TDC_BIT_RANGE = 524288; + static const int CLOCK_INTERVAL = 512000; + static const int MAX_NUMBER_LWORDS = 1024; + int MAX_TIME_JITTER; + int clock_channel; + Long64_t tdcBin0; + + // 32 bit unsigned integer + static unsigned int get_lwords; // number of lwords in blt_data + static unsigned int blt_data[MAX_NUMBER_LWORDS]; // data buffer + + static FILE *fpw; // handle to write TDC dump file + static std::ofstream textDumpFile; + + typedef unsigned short int WORD; + typedef unsigned long int DWORD; + typedef struct _v1190data { + // TDC V1290 + // DWORD data:21; // TDC V1290A measurement (21bit@25ps, 19bit @100ps & @200ps, + // WORD channel:5; // 17bit@800ps) 32 channel for TDC V1290A + // // 16 channel for TDC V1290N + // TDC V1190 + DWORD data:19; // TDC V1190 measurement (19bit@100 & @200, 17bit@800ps) + WORD channel:7; // 128 channel for TDC V1190A, 64 channel for TDC V1190B + // + WORD edge:1; // 0 -> leading edge, 1 -> trailing edge + WORD buffer:3; // 000 -> TDC measurement, 100 -> TDC error + WORD filler:2; // 00 -> TDC data + } V1190_DATA; + + std::multimap kamilHitMultimap; + std::multimap ::iterator it; + std::multimap ::iterator it1; + std::multimap ::iterator it2; + std::multimap ::iterator it3; + std::multimap ::iterator it4; + std::multimap ::iterator it5; + std::multimap ::iterator it6; +}; + +#endif