ICP  1
CRPTProxy.cpp
Go to the documentation of this file.
1 #include "stdafx.h"
2 #include "CRPTProxy.h"
3 
4 CRPTProxy::CRPTProxy() : Base("CRPTProxy"), m_crpt(NULL), m_crpt_fh(INVALID_HANDLE_VALUE), m_crpt_fm(NULL)
5 {
7 }
8 
9 
10 int CRPTProxy::flushSection(void* address, HANDLE file_handle, DAEstatus& status)
11 {
12  int i;
13 // ICPTimer timer("CRPTProxy::flushSection", status);
14  i = FlushViewOfFile(address, 0);
15  if (i == 0)
16  {
17  status.addVa(FAC_CRPT, SEV_ERROR, ERRTYPE_OUTOFMEM, "FlushViewOfFile failed %d", GetLastError());
18  return DAEstatus::Failure;
19  }
20  i = FlushFileBuffers(file_handle);
21  if (i == 0)
22  {
23  status.addVa(FAC_CRPT, SEV_ERROR, ERRTYPE_OUTOFMEM, "FlushFileBuffers failed %d", GetLastError());
24  return DAEstatus::Failure;
25  }
26  return DAEstatus::Success;
27 }
28 
29 
30 int CRPTProxy::flushCRPT(DAEstatus& status, bool flush_raw_data)
31 {
32 // ICPTimer timer("CRPTProxy::flushCRPT", status);
34  {
35  return DAEstatus::Failure;
36  }
37  if (flush_raw_data)
38  {
39  return m_crpt_data.flush();
40  }
41  else
42  {
43  return DAEstatus::Success;
44  }
45 }
46 
47 int CRPTProxy::unloadCRPT(bool delete_crpt_file, bool delete_crpt_data_file, DAEstatus& status)
48 {
49  ICPTimer timer("CRPTProxy::unloadCRPT", status);
50  if (m_crpt != NULL)
51  {
52  time(&(m_crpt->crpt_unload_time));
53  }
54  if (m_crpt != NULL && UnmapViewOfFile(m_crpt) == 0)
55  {
56  status.addInfo(FAC_CRPT, "Error unmapping file on exit");
57  }
58  if (m_crpt_fm != NULL && CloseHandle(m_crpt_fm) == 0)
59  {
60  status.addInfo(FAC_CRPT, "Error closing crpt mapping file handle on exit");
61  }
62  if (m_crpt_fh != INVALID_HANDLE_VALUE && CloseHandle(m_crpt_fh) == 0)
63  {
64  status.addInfo(FAC_CRPT, "Error closing crpt file handle on exit");
65  }
66  m_crpt_data.unload(delete_crpt_data_file);
68  if (delete_crpt_file)
69  {
70  status.addInfoVa(FAC_CRPT, "Deleting CRPT %s", m_crpt_file.c_str());
71  remove(m_crpt_file.c_str());
72  }
73  return 0;
74 }
75 
76 int CRPTProxy::loadOrCreateCRPT(const std::string& crpt_file, const std::string& crpt_name,
77  const std::string& crpt_data_file, const std::string& crpt_data_name, int crpt_data_size,
78  ISISDAE::DAEType dae_type, const std::string& comp_name, boost::function<int (DAEstatus&)> readRecoveryFile, DAEstatus& status)
79 {
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);
82 }
83 
84 int CRPTProxy::loadOrCreateCRPT(const char* crpt_file, const char* crpt_name,
85  const char* crpt_data_file, const char* crpt_data_name, int crpt_data_size,
86  ISISDAE::DAEType dae_type, const char* comp_name, boost::function<int (DAEstatus&)> readRecoveryFile, DAEstatus& status)
87 {
88  LARGE_INTEGER size;
89  ICPTimer timer("CRPTProxy::loadOrCreateCRPT", status);
90  unloadCRPT(status);
91  bool rebuild_crpt = false;
92  m_crpt_fh = CreateFile(crpt_file, GENERIC_READ | GENERIC_WRITE, 0, defaultNoInheritHandles(), OPEN_ALWAYS,
93  FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH /*| FILE_FLAG_NO_BUFFERING*/, NULL);
94  if (m_crpt_fh == INVALID_HANDLE_VALUE)
95  {
96  status.addVa(FAC_CRPT, SEV_ERROR, ERRTYPE_OUTOFMEM, "CreateFile failed for \"%s\" - WIN32 error code = %d", crpt_file, GetLastError());
97  unloadCRPT(status);
98  return DAEstatus::Failure;
99  }
100  if (GetFileSizeEx(m_crpt_fh, &size) == 0)
101  {
102  status.add(FAC_CRPT, SEV_ERROR, ERRTYPE_OUTOFMEM, "GetFileSize failed");
103  unloadCRPT(status);
104  return DAEstatus::Failure;
105  }
106  if ( size.QuadPart != sizeof(ISISCRPT_STRUCT) )
107  {
108  status.addVa(FAC_CRPT, SEV_WARNING, ERRTYPE_OUTOFMEM, "crpt size error: %I64d != %Id delete file %s and try again", size.QuadPart, sizeof(ISISCRPT_STRUCT), crpt_file);
109  CloseHandle(m_crpt_fh);
110  remove(crpt_file);
111  m_crpt_fh = CreateFile(crpt_file, GENERIC_READ | GENERIC_WRITE, 0, defaultNoInheritHandles(), CREATE_ALWAYS,
112  FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH /*| FILE_FLAG_NO_BUFFERING*/, NULL);
113  rebuild_crpt = true;
114  size.QuadPart = sizeof(ISISCRPT_STRUCT);
115  }
116  status.addInfoVa(FAC_CRPT, "CRPT \"%s\" size %Id KB", crpt_file, sizeof(ISISCRPT_STRUCT) / 1024);
117  m_crpt_fm = CreateFileMapping(m_crpt_fh, defaultNoInheritHandles(), PAGE_READWRITE, size.HighPart, size.LowPart, crpt_name);
118  if (m_crpt_fm == NULL || GetLastError() == ERROR_ALREADY_EXISTS)
119  {
120  status.addVa(FAC_CRPT, SEV_ERROR, ERRTYPE_OUTOFMEM, "CreateFileMapping failed for \"%s\" - WIN32 error code = %d", crpt_file, GetLastError());
121  unloadCRPT(status);
122  return DAEstatus::Failure;
123  }
124  m_crpt = (ISISCRPT_STRUCT*)MapViewOfFile(m_crpt_fm, FILE_MAP_WRITE, 0, 0, 0);
125  if (m_crpt == NULL)
126  {
127  unloadCRPT(status);
128  status.addVa(FAC_CRPT, SEV_ERROR, ERRTYPE_OUTOFMEM, "MapViewOfFile failed for \"%s\" - WIN32 error code = %d", crpt_file, GetLastError());
129  return DAEstatus::Failure;
130  }
131  m_crpt_file = crpt_file;
132  m_crpt_name = crpt_name;
133  m_dae_type = dae_type;
134  m_comp_name = comp_name;
135  m_crpt->dae_type = dae_type;
136  bool crpt_error = false;
137  if (rebuild_crpt)
138  {
139  ISISCRPT_STRUCT* junk = new (m_crpt) ISISCRPT_STRUCT(dae_type, comp_name);
140  if (readRecoveryFile != NULL)
141  {
142  readRecoveryFile(status);
143  }
144  else
145  {
146  status.addVa(FAC_CRPT, SEV_WARNING, ERRTYPE_OUTOFMEM, "unable to read recovery file");
147  crpt_error = true;
148  }
149  }
150  if ( m_crpt->version != ISISCRPT_VERSION )
151  {
152  status.addVa(FAC_CRPT, SEV_WARNING, ERRTYPE_OUTOFMEM, "CRPT version mismatch for \"%s\" - %d != %d", crpt_file, m_crpt->version, ISISCRPT_VERSION);
153  crpt_error = true;
154  }
155  if (!m_crpt->checkCRPT())
156  {
157  status.addVa(FAC_CRPT, SEV_WARNING, ERRTYPE_OUTOFMEM, "CRPT corrupt");
158  crpt_error = true;
159  }
160  // crpt_rcsid is NULL terminated but m_crpt->rscid is not
161  if (strlen(crpt_rcsid) >= sizeof(m_crpt->rcsid))
162  {
163  status.addWarningVa(FAC_CRPT, "CRPT RCS version too long for \"%s\"", crpt_file);
164  }
165  else
166  {
167  if (strncmp(m_crpt->rcsid, crpt_rcsid, strlen(crpt_rcsid)) != 0)
168  {
169  status.addWarningVa(FAC_CRPT, "CRPT RCS version mismatch for \"%s\": \"%s\" != \"%s\"", crpt_file, m_crpt->rcsid, crpt_rcsid);
170  crpt_error = true;
171  }
172  }
173  if (crpt_error)
174  {
175  status.addWarningVa(FAC_CRPT, "CRPT had error so rebuilding from recovery file");
176  ISISCRPT_STRUCT* junk2 = new (m_crpt) ISISCRPT_STRUCT(dae_type, comp_name);
177  if (readRecoveryFile != NULL)
178  {
179  readRecoveryFile(status);
180  }
181  else
182  {
183  status.addVa(FAC_CRPT, SEV_WARNING, ERRTYPE_OUTOFMEM, "unable to read recovery file");
184  crpt_error = true;
185  }
186  }
187  m_crpt_data.init();
189  time(&(m_crpt->crpt_load_time));
190  flushCRPT(status); // flush everything to make sure new file is full size on disk
191  status.addInfoVa(FAC_CRPT, "CRPT version %d (%s)", ISISCRPT_VERSION, crpt_rcsid);
192  // now load data file
193  if (m_crpt_data.loadOrCreate(crpt_data_file, crpt_data_name, crpt_data_size, status) == DAEstatus::Failure)
194  {
195  unloadCRPT(status);
196  return DAEstatus::Failure;
197  }
198  time(&(m_crpt->crpt_load_time));
199  flushCRPT(status, true); // flush everything to make sure new file is full size on disk
200  return DAEstatus::Success;
201 }
202 
203 
204 int CRPTProxy::loadCRPT(const char* crpt_file, const char* crpt_name,
205  const char* crpt_data_file, const char* crpt_data_name, int crpt_data_size, bool read_only, DAEstatus& status)
206 {
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)
212  {
213  status.addVa(FAC_CRPT, SEV_ERROR, ERRTYPE_OUTOFMEM, "CreateFile failed for \"%s\" - WIN32 error code = %d", crpt_file, GetLastError());
214  return DAEstatus::Failure;
215  }
216  HANDLE crpt_data_fh = CreateFile(crpt_data_file, open_mode, 0, defaultNoInheritHandles(), OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL /* file_attributes */, NULL);
217  if (crpt_data_fh == INVALID_HANDLE_VALUE)
218  {
219  CloseHandle(crpt_fh);
220  status.addVa(FAC_CRPT, SEV_ERROR, ERRTYPE_OUTOFMEM, "CreateFile failed for \"%s\" - WIN32 error code = %d", crpt_data_file, GetLastError());
221  return DAEstatus::Failure;
222  }
223  return loadCRPT(crpt_file, crpt_fh, crpt_name, crpt_data_file, crpt_data_fh, crpt_data_name, crpt_data_size, read_only, status);
224 }
225 
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)
228 {
229  ICPTimer timer("CRPTProxy::loadCRPT", status);
230  LARGE_INTEGER size;
231  unloadCRPT(status);
232  m_crpt_fh = crpt_fh;
233  int file_map_mode = (read_only ? PAGE_READONLY : PAGE_READWRITE);
234  int view_map_mode = (read_only ? FILE_MAP_READ : FILE_MAP_WRITE);
235  if (m_crpt_fh == INVALID_HANDLE_VALUE)
236  {
237  status.addVa(FAC_CRPT, SEV_ERROR, ERRTYPE_OUTOFMEM, "CreateFile failed - WIN32 error code = %d", GetLastError());
238  unloadCRPT(status);
239  return DAEstatus::Failure;
240  }
241  if (GetFileSizeEx(m_crpt_fh, &size) == 0)
242  {
243  status.add(FAC_CRPT, SEV_ERROR, ERRTYPE_OUTOFMEM, "GetFileSize failed");
244  unloadCRPT(status);
245  return DAEstatus::Failure;
246  }
247  if ( size.QuadPart != sizeof(ISISCRPT_STRUCT) )
248  {
249  status.addVa(FAC_CRPT, SEV_WARNING, ERRTYPE_OUTOFMEM, "crpt size error: %I64d != %Id", size.QuadPart, sizeof(ISISCRPT_STRUCT));
250  unloadCRPT(status);
251  return DAEstatus::Failure;
252  }
253  m_crpt_fm = CreateFileMapping(m_crpt_fh, defaultNoInheritHandles(), file_map_mode, size.HighPart, size.LowPart, crpt_name);
254  if (m_crpt_fm == NULL || GetLastError() == ERROR_ALREADY_EXISTS)
255  {
256  status.addVa(FAC_CRPT, SEV_ERROR, ERRTYPE_OUTOFMEM, "CreateFileMapping failed - WIN32 error code = %d", GetLastError());
257  unloadCRPT(status);
258  return DAEstatus::Failure;
259  }
260  m_crpt = (ISISCRPT_STRUCT*)MapViewOfFile(m_crpt_fm, view_map_mode, 0, 0, 0);
261  if (m_crpt == NULL)
262  {
263  status.addVa(FAC_CRPT, SEV_ERROR, ERRTYPE_OUTOFMEM, "MapViewOfFile failed - WIN32 error code = %d", GetLastError());
264  unloadCRPT(status);
265  return DAEstatus::Failure;
266  }
267 // can't do this yet as also called with a blank crpt
268 // if (strncmp(m_crpt->rcsid, crpt_rcsid, strlen(crpt_rcsid)) != 0)
269 // {
270 // status.addVa(FAC_CRPT, SEV_ERROR, ERRTYPE_OUTOFMEM, "CRPT RCS version mismatch for \"%s\": \"%s\" != \"%s\"", crpt_file, m_crpt->rcsid, crpt_rcsid);
271 // unloadCRPT(status);
272 // return DAEstatus::Failure;
273 // }
274  m_crpt_name = crpt_name;
275  m_crpt_file = crpt_file;
276 // m_dae_type = dae_type;
277 // m_comp_name = comp_name;
279  // now load data file
280  if (m_crpt_data.load(crpt_data_file, crpt_data_fh, crpt_data_name, crpt_data_size, read_only, status) == DAEstatus::Failure)
281  {
282  unloadCRPT(status);
283  return DAEstatus::Failure;
284  }
285  time(&(m_crpt->crpt_load_time));
286  return DAEstatus::Success;
287 }
288 
289 int CRPTProxy::mapCRPT(const std::string& crpt_name, const std::string& crpt_data_name, bool read_only, bool existing)
290 {
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 )
294  {
295  Sleep(2000);
296  }
297  m_crpt = (ISISCRPT_STRUCT*)MapViewOfFile(m_crpt_fm, map_opt, 0, 0, 0);
298  if (m_crpt == NULL)
299  {
300  LOGSTR_ERROR("MapViewOfFile");
301  CloseHandle(m_crpt_fm);
302  m_crpt_fm = NULL;
303  return DAEstatus::Failure;
304  }
305  if (m_crpt_data.map(crpt_data_name, read_only, existing) == DAEstatus::Failure)
306  {
307  LOGSTR_ERROR("m_crpt_data.map");
308  UnmapViewOfFile(m_crpt);
309  CloseHandle(m_crpt_fm);
310  m_crpt = NULL;
311  m_crpt_fm = NULL;
312  return DAEstatus::Failure;
313  }
314  if (!read_only)
315  {
316  time(&(m_crpt->crpt_load_time));
317  }
318  return DAEstatus::Success;
319 }
320 
321 #if 0
322 int CRPTProxy::clone(const std::string& crpt_file, const std::string& crpt_data_file, bool blank_raw_data, DAEstatus& status)
323 {
324  ICPTimer icptimer("CRPTProxy::clone", status);
325  unsigned len_used = m_crpt_data_size;
326 // unsigned len_used = m_crpt->getPeriodSize() * m_crpt->nper_daq; // max is m_crpt_data_size
327  LOGSTR_INFORMATION("Copying CRPT to " << crpt_file);
329  Poco::ActiveResult<int> res1 = ob.run(boost::bind(flushed_write, crpt_file, m_crpt, sizeof(ISISCRPT_STRUCT)));
330  Poco::SharedPtr<Poco::ActiveResult<int>> res2;
331  if (blank_raw_data)
332  {
333  LOGSTR_INFORMATION("Creating " << len_used << " words of blank histogram data in " << crpt_data_file);
334  res2 = new Poco::ActiveResult<int>(ob.run(boost::bind(createEmptyFileAndClose, crpt_data_file, len_used*sizeof(isisU32_t))));
335  }
336  else
337  {
338  LOGSTR_INFORMATION("Copying " << len_used << " words of CRPT histogram data to " << crpt_data_file);
339  res2 = new Poco::ActiveResult<int>(ob.run(boost::bind(flushed_write, crpt_data_file, m_crpt_data, len_used*sizeof(isisU32_t))));
340  }
341  res1.wait();
342  res2->wait();
343  if (res1.failed() || res1.data() != 0)
344  {
345  throw std::runtime_error("CRPTProxy::clone");
346  }
347  if (res2->failed() || res2->data() != 0)
348  {
349  throw std::runtime_error("CRPTProxy::clone");
350  }
351  return 0;
352 }
353 #endif
354 
355 int CRPTProxy::createFrom(CRPTProxy& proxy_clone, int id, bool blank_raw_data, DAEstatus& status)
356 {
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);
362  HANDLE crpt_fh = createEmptyFile(crpt_file, sizeof(ISISCRPT_STRUCT));
363  HANDLE crpt_data_fh = createEmptyFile(crpt_data_file, proxy_clone.dataFileSize() * sizeof(isisU32_t));
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);
366  return 0;
367 }
368 
369 int CRPTProxy::updateFrom(CRPTProxy& proxy_clone, bool copy_raw_data, DAEstatus& status)
370 {
371  ICPTimer timer("CRPTProxy::updateFrom", status);
372  memcpy(m_crpt, proxy_clone.m_crpt, sizeof(ISISCRPT_STRUCT));
373  if (copy_raw_data)
374  {
375  m_crpt_data.copy(proxy_clone.m_crpt_data);
376  }
377  flushCRPT(status, copy_raw_data);
380  m_status_log = proxy_clone.m_status_log;
381  m_event_log = proxy_clone.m_event_log;
382  m_debug_log = proxy_clone.m_debug_log;
383  m_monitoring_log = proxy_clone.m_monitoring_log;
384  m_timediff_log = proxy_clone.m_timediff_log;
385  return 0;
386 }
387 
388 int CRPTProxy::assignLogFiles(const std::string& prefix)
389 {
390  m_status_log = prefix + "_ICPstatus.txt";
391  m_event_log = prefix + "_ICPevent.txt";
392  m_debug_log = prefix + "_ICPdebug.txt";
393  m_monitoring_log = prefix + "_ICPmonitoring.txt";
394  m_timediff_log = prefix + "_ICPtimediff.txt";
395  return DAEstatus::Success;
396 }
397 
398 // also called in endRun() to update log file names for a new run number
399 int CRPTProxy::assignLogFiles(bool use_full_inst_name_for_files, const std::string& data_dir, int run_number_digits)
400 {
401  char buffer[256];
402  char log_name[80];
403  if (use_full_inst_name_for_files)
404  {
405  strncpy(log_name, m_crpt->inst_name, sizeof(log_name));
406  }
407  else
408  {
409  strncpy(log_name, m_crpt->inst_abrv, sizeof(log_name));
410  }
411  NULL_TERMINATE(log_name);
412  _snprintf(buffer, sizeof(buffer), "%s\\%s%0*d", data_dir.c_str(), log_name, run_number_digits, m_crpt->run_number);
413  NULL_TERMINATE(buffer);
414  return assignLogFiles(buffer);
415 }
416 
417 
418 static Poco::Mutex histo_mutex;
419 
420 void CRPTProxy::histogramEventsToCRPT(CRPTProxy& crpt, const DAEEventHeader* head, const DetectorEvent32* ev, int n, int event_source_id)
421 {
422  histogramEventsToCRPT(crpt.CRPT(), NULL, &(crpt.m_crpt_data), NULL, head, ev, n, event_source_id);
423 }
424 
425 void CRPTProxy::histogramEventsToCRPT(CRPTProxy& crpt1, CRPTProxy& crpt2, const DAEEventHeader* head, const DetectorEvent32* ev, int n, int event_source_id)
426 {
427  histogramEventsToCRPT(crpt1.CRPT(), crpt2.CRPT(), &(crpt1.m_crpt_data), &(crpt2.m_crpt_data), head, ev, n, event_source_id);
428 }
429 
430 //void CRPTProxy::histogramEventsToCRPT(const ISISCRPT_STRUCT* crpt, ISISCRPT_DATA* crpt_data, const DAEEventHeader* head, const DetectorEvent32* ev, int n, int event_source_id)
431 //{
432 // histogramEventsToCRPT(crpt, NULL, crpt_data, NULL, head, ev, n, event_source_id);
433 //}
434 
437 void CRPTProxy::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)
438 {
439  static bool random_shift = Poco::Util::Application::instance().config().getBool("isisicp.randomeventbinshift", true);
440 // unsigned permax = crpt->getPeriodSize();
441  int spec;
442  uint32_t base_offset, time_chan_offset;
443  isisU32_t* raw_data1 = crpt_data1->rawData();
444  isisU32_t* raw_data2 = (crpt_data2 != NULL ? crpt_data2->rawData() : NULL);
445  int card_index = crpt1->cardIndexFromPos(event_source_id);
446  for(int i=0; i<n; ++i)
447  {
448  spec = crpt1->dae1SpecForCardIndex(card_index, ev[i].spectrum);
449  if (spec >= 0 && head->period < crpt1->nper_daq)
450  {
451  base_offset = crpt1->spectrumCRPTOffsetImpl(spec, head->period, crpt_data1->rawDataSizeMax());
452  if (random_shift)
453  {
454  time_chan_offset = crpt1->randomisedChannelRebin(ev[i].time_channel, crpt1->spectrumDAETR(spec), crpt1->spectrumCRPTTR(spec));
455  }
456  else
457  {
458  time_chan_offset = crpt1->tcb_map[crpt1->spectrumCRPTTR(spec)-1][crpt1->spectrumDAETR(spec)-1][ev[i].time_channel]; // equivalent to using simpleChannelRebin()
459  }
460  if (sizeof(LONG) == sizeof(raw_data1[0]))
461  {
462  InterlockedIncrement(reinterpret_cast<LONG*>(&(raw_data1[base_offset + time_chan_offset])));
463  crpt_data1->incrementSpectrum(spec, head->period);
464  if (raw_data2 != NULL)
465  {
466  InterlockedIncrement(reinterpret_cast<LONG*>(&(raw_data2[base_offset + time_chan_offset])));
467  crpt_data2->incrementSpectrum(spec, head->period);
468  }
469  }
470  else
471  {
472  histo_mutex.lock();
473  ++(raw_data1[base_offset + time_chan_offset]);
474  crpt_data1->incrementSpectrum(spec, head->period);
475  if (raw_data2 != NULL)
476  {
477  ++(raw_data2[base_offset + time_chan_offset]);
478  crpt_data2->incrementSpectrum(spec, head->period);
479  }
480  histo_mutex.unlock();
481  }
482  }
483  }
484 }
485 
486 
488  {
489  Poco::Mutex::ScopedLock _lock(m_data_lock);
490  ICPTimer timer("CRPTProxy::zeroEventRawData", status);
491  isisU32_t* crpt_data = m_crpt_data.rawData();
492  for(int i=0; i < m_crpt->getNumSpectra(true); ++i)
493  {
494  if ( m_crpt->isEventSpectrum(i) )
495  {
496  int ntc = m_crpt->spectrumNTC(i);
497  for(int j=0; j < m_crpt->nper_daq; ++j)
498  {
499  memset(&(crpt_data[ spectrumCRPTOffset(i, j) ]), 0, (ntc+1)*sizeof(uint32_t));
501  }
502  }
503  }
504  }
505 
506 
507 #if 0
508 int CRPTProxy::copyDataTo(const std::string& filename, DAEstatus& status) const
509 {
510  ICPTimer timer("CRPTProxy::copyDataTo", status);
511  FILE* f;
512  size_t n;
513  f = fopen(filename.c_str(), "wbcN");
514  if (f == NULL)
515  {
516  status.add(FAC_CRPT, SEV_ERROR, ERRTYPE_OUTOFMEM, "cannot create new crpt disk file");
517  return DAEstatus::Failure;
518  }
519  n = fwrite(m_crpt_data, sizeof(isisU32_t), m_crpt_data_size, f);
520  fflush(f);
521  fclose(f);
522  if (n != m_crpt_data_size)
523  {
524  status.add(FAC_CRPT, SEV_ERROR, ERRTYPE_OUTOFMEM, "cannot write new crpt disk file");
525  return DAEstatus::Failure;
526  }
527  return DAEstatus::Success;
528 }
529 // create a new blank on-disk image and maps to it
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,
532  ISISDAE::DAEType dae_type, const std::string& comp_name, DAEstatus& status)
533 {
534  FILE* f;
535  size_t n;
536  ISISCRPT_STRUCT* tmp_crpt;
537  f = fopen(crpt_filename.c_str(), "wbcN");
538  if (f == NULL)
539  {
540  status.add(FAC_CRPT, SEV_ERROR, ERRTYPE_OUTOFMEM, "cannot create new crpt disk file");
541  return DAEstatus::Failure;
542  }
543  tmp_crpt = new ISISCRPT_STRUCT(dae_type, comp_name);
544  n = fwrite(tmp_crpt, sizeof(ISISCRPT_STRUCT), 1, f);
545  fflush(f);
546  fclose(f);
547  delete tmp_crpt;
548  if (n != 1)
549  {
550  status.add(FAC_CRPT, SEV_ERROR, ERRTYPE_OUTOFMEM, "cannot write new crpt disk file");
551  return DAEstatus::Failure;
552  }
553  return DAEstatus::Success;
554 }
555 
556  if ( (loadCRPT() == DAEstatus::Failure) || (m_crpt == NULL) )
557  {
558  m_status.add(FAC_CRPT, SEV_ERROR, ERRTYPE_OUTOFMEM, "cannot load new crpt");
559  return DAEstatus::Failure;
560  }
561  readRecoveryFile(m_status);
562  flushCRPT(m_status, true); // flush everything to make sure new file is full size on disk
563  return DAEstatus::Success;
564 #endif
#define NULL_TERMINATE(__array)
null terminate a char[n] static array
Definition: isiscrpt.h:74
time_t crpt_load_time
time ICP started
Definition: isiscrpt.h:245
ISISCRPT_DATA m_crpt_data
Definition: CRPTProxy.h:253
std::string m_monitoring_log
Definition: CRPTProxy.h:266
int createFrom(CRPTProxy &proxy_clone, int id, bool blank_raw_data, DAEstatus &status)
Definition: CRPTProxy.cpp:355
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)
Definition: CRPTProxy.cpp:437
#define ERRTYPE_OUTOFMEM
void zeroSpectrum(int spec, int)
Definition: CRPTProxy.h:28
int getNumSpectra(bool include_spectrum_zero=false) const
Definition: isiscrpt.cpp:926
int addVa(int facility, int severity, int errtype, const char *format,...)
Definition: DAEstatus.cpp:54
#define SEV_WARNING
std::string m_debug_log
Definition: CRPTProxy.h:265
void invalidateHandles()
Definition: CRPTProxy.h:274
void unload(bool delete_file)
Definition: CRPTProxy.h:75
isisU32_t * rawData()
Definition: CRPTProxy.h:55
time_t m_raw_data_update_time
Definition: CRPTProxy.h:261
int tcb_map[ISISCRPT_MAX_NTRG][ISISCRPT_MAX_NTRG][ISISCRPT_MAX_TIMECHANB]
Definition: isiscrpt.h:345
void init()
Definition: CRPTProxy.h:21
uint32_t spectrumCRPTOffsetImpl(int spec, int daq_period) const
Definition: isiscrpt.h:624
fixed header marker for DAEEventHeader
Definition: dae_events.h:44
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)
Definition: CRPTProxy.cpp:204
unsigned long isisU32_t
Definition: isisvme_types.h:8
int flushSection(void *address, HANDLE file_handle, DAEstatus &status)
Definition: CRPTProxy.cpp:10
char rcsid[ISISCRPT_MAX_RCSID+1]
Definition: isiscrpt.h:239
static const int Failure
Definition: DAEstatus.h:141
unsigned period
Definition: dae_events.h:57
int add(DAEstatus &dstatus, bool clear)
Definition: DAEstatus.cpp:131
#define ISISCRPT_VERSION
increment ISISCRPT_VERSION for any ISISCRPT_STRUCT changes that might not be detected by a change of ...
Definition: isiscrpt.h:5
std::string dataSectionName() const
Definition: CRPTProxy.h:301
SECURITY_ATTRIBUTES * defaultNoInheritHandles()
Definition: icputils.cpp:1121
static const int Success
Definition: DAEstatus.h:140
Poco::ActiveMethod< R, func_t, ActiveObject > run
Definition: icputils.h:643
int flush()
Definition: CRPTProxy.h:69
HANDLE createEmptyFile(const std::string &file_name, size_t file_size)
Definition: icputils.cpp:963
isisU32_t rawDataSizeMax() const
Definition: CRPTProxy.h:298
int assignLogFiles(const std::string &prefix)
Definition: CRPTProxy.cpp:388
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)
Definition: CRPTProxy.h:147
time_t crpt_unload_time
time CRPT loaded
Definition: isiscrpt.h:246
std::string m_event_log
Definition: CRPTProxy.h:264
int unloadCRPT(bool delete_crpt_file, bool delete_crpt_data_file, DAEstatus &status)
Definition: CRPTProxy.cpp:47
void copy(const ISISCRPT_DATA &clone)
Definition: CRPTProxy.h:43
int spectrumDAETR(int spec) const
Definition: isiscrpt.h:799
#define FAC_CRPT
HANDLE m_crpt_fm
Definition: CRPTProxy.h:256
int updateFrom(CRPTProxy &proxy_clone, bool copy_raw_data, DAEstatus &status)
Definition: CRPTProxy.cpp:369
int cardIndexFromPos(int pos) const
Definition: isiscrpt.h:727
#define SEV_ERROR
int dae_type
time CRPT unloaded (may not be set if program crashed)
Definition: isiscrpt.h:247
std::string m_comp_name
Definition: CRPTProxy.h:258
std::string m_crpt_name
name of memory section
Definition: CRPTProxy.h:260
#define LOGSTR_INFORMATION(__arg)
Definition: IsisBase.h:78
std::string m_timediff_log
Definition: CRPTProxy.h:267
#define LOGSTR_ERROR(__arg)
Definition: IsisBase.h:99
int loadOrCreate(const char *crpt_data_file, const char *crpt_data_name, int crpt_data_size, DAEstatus &status)
Definition: CRPTProxy.h:97
static const char * crpt_rcsid
Definition: isiscrpt.h:7
isisU32_t dataFileSize() const
Definition: CRPTProxy.h:320
int addWarningVa(int facility, const char *format,...)
Definition: DAEstatus.cpp:106
std::string m_status_log
time of last full scale update of raw_data
Definition: CRPTProxy.h:263
int mapCRPT(const std::string &crpt_name, const std::string &crpt_data_name, bool read_only, bool existing)
Definition: CRPTProxy.cpp:289
int spectrumCRPTTR(int spec) const
Definition: isiscrpt.h:794
char inst_abrv[ISISCRPT_INST_ABRV_LEN+1]
Definition: isiscrpt.h:352
int map(const std::string &crpt_data_name, bool read_only, bool)
Definition: CRPTProxy.h:188
int dae1SpecForCardIndex(int index, int dae2_spec) const
Definition: isiscrpt.h:701
unsigned time_channel
Definition: dae_events.h:78
parameter_map_t m_sample_parameters
Definition: CRPTProxy.h:270
int createEmptyFileAndClose(const std::string &file_name, size_t file_size)
Definition: icputils.cpp:1061
bool checkCRPT() const
Definition: isiscrpt.h:528
parameter_map_t m_beamline_parameters
Definition: CRPTProxy.h:271
std::string m_crpt_file
Definition: CRPTProxy.h:259
Poco::Mutex m_data_lock
Definition: CRPTProxy.h:251
ISISCRPT_STRUCT * m_crpt
Definition: CRPTProxy.h:252
bool isEventSpectrum(int spec) const
Definition: isiscrpt.h:821
HANDLE m_crpt_fh
Definition: CRPTProxy.h:255
ISISCRPT_STRUCT * CRPT()
Definition: CRPTProxy.h:348
static int junk(const std::string &hostname, GUID C, GUID G)
char inst_name[ISISCRPT_INST_LEN+1]
Definition: isiscrpt.h:351
static Poco::Mutex histo_mutex
Definition: CRPTProxy.cpp:418
void incrementSpectrum(int spec, int)
Definition: CRPTProxy.h:35
void zeroEventRawData(DAEstatus &status)
Definition: CRPTProxy.cpp:487
int flushCRPT(DAEstatus &status, bool flush_raw_data=false)
Definition: CRPTProxy.cpp:30
int addInfo(int facility, const std::string &text)
Definition: DAEstatus.cpp:86
int randomisedChannelRebin(int from_chan, int from_tr, int to_tr) const
Definition: isiscrpt.cpp:1045
int spectrumNTC(int spec) const
Definition: isiscrpt.h:804
int addInfoVa(int facility, const char *format,...)
Definition: DAEstatus.cpp:91
uint32_t spectrumCRPTOffset(int spec, int daq_period) const
Definition: CRPTProxy.h:406
ISISDAE::DAEType m_dae_type
Definition: CRPTProxy.h:257
std::string dataFileName() const
Definition: CRPTProxy.h:300
isisU32_t rawDataSizeMax() const
Definition: CRPTProxy.h:51
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)
Definition: CRPTProxy.cpp:76
static int flushed_write(const std::vector< std::string > &files, const std::vector< const void * > &data, const std::vector< int > &nbytes)
Definition: icputils.cpp:910
DAEType
Definition: isisdae.h:63