4 CRPTProxy::CRPTProxy() : Base(
"CRPTProxy"), m_crpt(NULL), m_crpt_fh(INVALID_HANDLE_VALUE), m_crpt_fm(NULL)
14 i = FlushViewOfFile(address, 0);
20 i = FlushFileBuffers(file_handle);
49 ICPTimer timer(
"CRPTProxy::unloadCRPT", status);
60 status.
addInfo(
FAC_CRPT,
"Error closing crpt mapping file handle on exit");
77 const std::string& crpt_data_file,
const std::string& crpt_data_name,
int crpt_data_size,
80 return loadOrCreateCRPT(crpt_file.c_str(), crpt_name.c_str(), crpt_data_file.c_str(), crpt_data_name.c_str(), crpt_data_size, dae_type,
81 comp_name.c_str(), readRecoveryFile, status);
85 const char* crpt_data_file,
const char* crpt_data_name,
int crpt_data_size,
89 ICPTimer timer(
"CRPTProxy::loadOrCreateCRPT", status);
91 bool rebuild_crpt =
false;
93 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH , NULL);
100 if (GetFileSizeEx(
m_crpt_fh, &size) == 0)
112 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH , NULL);
118 if (
m_crpt_fm == NULL || GetLastError() == ERROR_ALREADY_EXISTS)
136 bool crpt_error =
false;
140 if (readRecoveryFile != NULL)
142 readRecoveryFile(status);
177 if (readRecoveryFile != NULL)
179 readRecoveryFile(status);
205 const char* crpt_data_file,
const char* crpt_data_name,
int crpt_data_size,
bool read_only,
DAEstatus& status)
207 ICPTimer timer(
"CRPTProxy::loadCRPT", status);
208 int open_mode = ( read_only ? (GENERIC_READ) : (GENERIC_READ | GENERIC_WRITE) );
209 int file_attributes = ( read_only ? (FILE_ATTRIBUTE_NORMAL) : (FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH) );
210 HANDLE crpt_fh = CreateFile(crpt_file, open_mode, 0,
defaultNoInheritHandles(), OPEN_EXISTING, file_attributes, NULL);
211 if (crpt_fh == INVALID_HANDLE_VALUE)
216 HANDLE crpt_data_fh = CreateFile(crpt_data_file, open_mode, 0,
defaultNoInheritHandles(), OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL , NULL);
217 if (crpt_data_fh == INVALID_HANDLE_VALUE)
219 CloseHandle(crpt_fh);
223 return loadCRPT(crpt_file, crpt_fh, crpt_name, crpt_data_file, crpt_data_fh, crpt_data_name, crpt_data_size, read_only, status);
226 int CRPTProxy::loadCRPT(
const char* crpt_file, HANDLE crpt_fh,
const char* crpt_name,
const char* crpt_data_file,
227 HANDLE crpt_data_fh,
const char* crpt_data_name,
int crpt_data_size,
bool read_only,
DAEstatus& status)
229 ICPTimer timer(
"CRPTProxy::loadCRPT", status);
233 int file_map_mode = (read_only ? PAGE_READONLY : PAGE_READWRITE);
234 int view_map_mode = (read_only ? FILE_MAP_READ : FILE_MAP_WRITE);
241 if (GetFileSizeEx(
m_crpt_fh, &size) == 0)
254 if (
m_crpt_fm == NULL || GetLastError() == ERROR_ALREADY_EXISTS)
289 int CRPTProxy::mapCRPT(
const std::string& crpt_name,
const std::string& crpt_data_name,
bool read_only,
bool existing)
291 int map_opt = (read_only ? FILE_MAP_READ : FILE_MAP_WRITE);
293 while( (
m_crpt_fm = OpenFileMapping(map_opt, FALSE, crpt_name.c_str())) == NULL )
322 int CRPTProxy::clone(
const std::string& crpt_file,
const std::string& crpt_data_file,
bool blank_raw_data,
DAEstatus& status)
324 ICPTimer icptimer(
"CRPTProxy::clone", status);
325 unsigned len_used = m_crpt_data_size;
330 Poco::SharedPtr<Poco::ActiveResult<int>> res2;
333 LOGSTR_INFORMATION(
"Creating " << len_used <<
" words of blank histogram data in " << crpt_data_file);
338 LOGSTR_INFORMATION(
"Copying " << len_used <<
" words of CRPT histogram data to " << crpt_data_file);
343 if (res1.failed() || res1.data() != 0)
345 throw std::runtime_error(
"CRPTProxy::clone");
347 if (res2->failed() || res2->data() != 0)
349 throw std::runtime_error(
"CRPTProxy::clone");
357 ICPTimer timer(
"CRPTProxy::createFrom", status);
358 std::string crpt_file = Poco::format(
"%s%d", proxy_clone.
m_crpt_file,
id);
359 std::string crpt_data_file = Poco::format(
"%s%d", proxy_clone.
dataFileName(), id);
360 std::string crpt_name = Poco::format(
"%s%d", proxy_clone.
m_crpt_name,
id);
361 std::string crpt_data_name = Poco::format(
"%s%d", proxy_clone.
dataSectionName(), id);
364 loadCRPT(crpt_file.c_str(), crpt_fh, crpt_name.c_str(), crpt_data_file.c_str(), crpt_data_fh, crpt_data_name.c_str(), proxy_clone.
rawDataSizeMax(),
false, status);
365 updateFrom(proxy_clone, !blank_raw_data, status);
371 ICPTimer timer(
"CRPTProxy::updateFrom", status);
403 if (use_full_inst_name_for_files)
412 _snprintf(buffer,
sizeof(buffer),
"%s\\%s%0*d", data_dir.c_str(), log_name, run_number_digits,
m_crpt->
run_number);
439 static bool random_shift = Poco::Util::Application::instance().config().getBool(
"isisicp.randomeventbinshift",
true);
442 uint32_t base_offset, time_chan_offset;
446 for(
int i=0; i<n; ++i)
460 if (
sizeof(LONG) ==
sizeof(raw_data1[0]))
462 InterlockedIncrement(reinterpret_cast<LONG*>(&(raw_data1[base_offset + time_chan_offset])));
464 if (raw_data2 != NULL)
466 InterlockedIncrement(reinterpret_cast<LONG*>(&(raw_data2[base_offset + time_chan_offset])));
473 ++(raw_data1[base_offset + time_chan_offset]);
475 if (raw_data2 != NULL)
477 ++(raw_data2[base_offset + time_chan_offset]);
480 histo_mutex.unlock();
490 ICPTimer timer(
"CRPTProxy::zeroEventRawData", status);
508 int CRPTProxy::copyDataTo(
const std::string& filename,
DAEstatus& status)
const
510 ICPTimer timer(
"CRPTProxy::copyDataTo", status);
513 f = fopen(filename.c_str(),
"wbcN");
522 if (n != m_crpt_data_size)
530 int ISISCRPT_STRUCT::createNew(
const std::string& crpt_filename,
const std::string& crpt_section_name,
531 const std::string& crpt_data_filename,
const std::string& crpt_data_section_name,
537 f = fopen(crpt_filename.c_str(),
"wbcN");
561 readRecoveryFile(m_status);
562 flushCRPT(m_status,
true);
#define NULL_TERMINATE(__array)
null terminate a char[n] static array
time_t crpt_load_time
time ICP started
ISISCRPT_DATA m_crpt_data
std::string m_monitoring_log
int createFrom(CRPTProxy &proxy_clone, int id, bool blank_raw_data, DAEstatus &status)
static void histogramEventsToCRPT(const ISISCRPT_STRUCT *crpt1, const ISISCRPT_STRUCT *crpt2, ISISCRPT_DATA *crpt_data1, ISISCRPT_DATA *crpt_data2, const DAEEventHeader *head, const DetectorEvent32 *ev, int n, int event_source_id)
void zeroSpectrum(int spec, int)
int getNumSpectra(bool include_spectrum_zero=false) const
int addVa(int facility, int severity, int errtype, const char *format,...)
void unload(bool delete_file)
time_t m_raw_data_update_time
int tcb_map[ISISCRPT_MAX_NTRG][ISISCRPT_MAX_NTRG][ISISCRPT_MAX_TIMECHANB]
uint32_t spectrumCRPTOffsetImpl(int spec, int daq_period) const
int loadCRPT(const char *crpt_file, const char *crpt_name, const char *crpt_data_file, const char *crpt_data_name, int crpt_data_size, bool read_only, DAEstatus &status)
int flushSection(void *address, HANDLE file_handle, DAEstatus &status)
char rcsid[ISISCRPT_MAX_RCSID+1]
int add(DAEstatus &dstatus, bool clear)
#define ISISCRPT_VERSION
increment ISISCRPT_VERSION for any ISISCRPT_STRUCT changes that might not be detected by a change of ...
std::string dataSectionName() const
SECURITY_ATTRIBUTES * defaultNoInheritHandles()
Poco::ActiveMethod< R, func_t, ActiveObject > run
HANDLE createEmptyFile(const std::string &file_name, size_t file_size)
isisU32_t rawDataSizeMax() const
int assignLogFiles(const std::string &prefix)
int load(const char *crpt_data_file, HANDLE crpt_data_fh, const char *crpt_data_name, int crpt_data_size, bool read_only, DAEstatus &status)
time_t crpt_unload_time
time CRPT loaded
int unloadCRPT(bool delete_crpt_file, bool delete_crpt_data_file, DAEstatus &status)
void copy(const ISISCRPT_DATA &clone)
int spectrumDAETR(int spec) const
int updateFrom(CRPTProxy &proxy_clone, bool copy_raw_data, DAEstatus &status)
int cardIndexFromPos(int pos) const
int dae_type
time CRPT unloaded (may not be set if program crashed)
std::string m_crpt_name
name of memory section
#define LOGSTR_INFORMATION(__arg)
std::string m_timediff_log
#define LOGSTR_ERROR(__arg)
int loadOrCreate(const char *crpt_data_file, const char *crpt_data_name, int crpt_data_size, DAEstatus &status)
static const char * crpt_rcsid
isisU32_t dataFileSize() const
int addWarningVa(int facility, const char *format,...)
std::string m_status_log
time of last full scale update of raw_data
int mapCRPT(const std::string &crpt_name, const std::string &crpt_data_name, bool read_only, bool existing)
int spectrumCRPTTR(int spec) const
char inst_abrv[ISISCRPT_INST_ABRV_LEN+1]
int map(const std::string &crpt_data_name, bool read_only, bool)
int dae1SpecForCardIndex(int index, int dae2_spec) const
parameter_map_t m_sample_parameters
int createEmptyFileAndClose(const std::string &file_name, size_t file_size)
parameter_map_t m_beamline_parameters
bool isEventSpectrum(int spec) const
static int junk(const std::string &hostname, GUID C, GUID G)
char inst_name[ISISCRPT_INST_LEN+1]
static Poco::Mutex histo_mutex
void incrementSpectrum(int spec, int)
void zeroEventRawData(DAEstatus &status)
int flushCRPT(DAEstatus &status, bool flush_raw_data=false)
int addInfo(int facility, const std::string &text)
int randomisedChannelRebin(int from_chan, int from_tr, int to_tr) const
int spectrumNTC(int spec) const
int addInfoVa(int facility, const char *format,...)
uint32_t spectrumCRPTOffset(int spec, int daq_period) const
ISISDAE::DAEType m_dae_type
std::string dataFileName() const
isisU32_t rawDataSizeMax() const
int loadOrCreateCRPT(const std::string &crpt_file, const std::string &crpt_name, const std::string &crpt_data_file, const std::string &crpt_data_name, int crpt_data_size, ISISDAE::DAEType dae_type, const std::string &comp_name, boost::function< int(DAEstatus &)> readRecoveryFile, DAEstatus &status)
static int flushed_write(const std::vector< std::string > &files, const std::vector< const void * > &data, const std::vector< int > &nbytes)