ICP  1
daeset.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <sstream>
3 
4 #include <time.h>
5 #include "isisdae.h"
6 
7 static ISISDAE* dae = 0;
8 static bool simulate_vme = (getenv("SIMULATE_VME") != 0 ? true : false);
9 static std::ostringstream *dae_pOut, *dae_pInf, *dae_pWarn,
10  *dae_pErr, *dae_pDbg;
11 
12 extern "C"
13 {
14  void __stdcall DAE_DEVICE(short& tempchan, int& simulate);
15  void __stdcall DAESET(short& zaachan,int crat[],int daep[],
16  int dat1[], int modn[],int mpos[],int& ndet,int& nper,
17  int& ntrg,int pmap[],int& nfpp, int& pre1, int spec[],
18  int tcb1[], int timr[], int ntcftr[], int nosftr[], int& vaxmax,
19  int& status, int& daebegerror, int& logstatus, int& delay,
20  int& synch,int vetoes[], int& cper, int& save_permax,
21  int save_tpoffset[], int& datamode, int& tables_only);
22  void __stdcall DAEPAUSE(short& zaachan, isisU32_t& igfc,
23  isisU32_t& irfc, int& status);
24  void __stdcall DAERESUME(short& zaachan, int& cper, int& save_permax,
25  int& save_tpoffset, int& status);
26  void __stdcall DAEEND(isisU32_t dat1[], int daep[], int ntcftr[], int nosftr[],
27  int& nper, int& ntrg, short& zaachan, isisU32_t& igfc, isisU32_t& irfc,
28  int& status);
29  int __stdcall RIO(isis32_t& bus_addr, isisU16_t* word, isis16_t& device);
30  int __stdcall WIO(isis32_t& bus_addr, isisU16_t* word, isis16_t& device);
31  int __stdcall RIOD(isis32_t& bus_addr, isisU16_t* word, isis32_t& nwrds, isis16_t& device);
32  int __stdcall WIOD(isis32_t& bus_addr, isisU16_t* word, isis32_t& nwrds, isis16_t& device);
33  int rio_c(long bus_addr, isisU16_t* word, short device);
34  int read_crpt_c(int **start, int& section, int& size);
35  int open_dae_cds(const char* name);
36  int __stdcall WMEM(isis32_t& bus_addr, isisU16_t* word, isisU16_t& device);
37  int __stdcall RMEM(isis32_t& bus_addr, isisU16_t* word, isisU16_t& device);
38  int __stdcall WMEMD(isis32_t& bus_addr, isisU16_t* word, isis32_t& nwrds, isisU16_t& device);
39  int __stdcall RMEMD(isis32_t& bus_addr, isisU16_t* word, isis32_t& nwrds, isisU16_t& device);
40  int __stdcall RMEMVI(isis32_t& bus_addr, isisU32_t* buffer, isisU32_t& length,
41  isis16_t& device, isis32_t& wrd_len);
42  int __stdcall DAECP(int code);
43  int read_crpt_array_c(int start, int length, int section[]);
44  int set_dae_streams(std::ostringstream* pOut, std::ostringstream* pInf, std::ostringstream* pWarn,
45  std::ostringstream* pErr, std::ostringstream* pDbg);
46 };
47 
48 
49 int __stdcall WIO(isis32_t& bus_addr, isisU16_t* word, isis16_t& device)
50 {
51  OUTPUT_MESSAGE(*dae_pErr,"WIO error" << std::endl);
52  return 0;
53 }
54 
55 int __stdcall WIOD(isis32_t& bus_addr, isisU16_t* word, isis32_t& nwrds, isis16_t& device)
56 {
57  OUTPUT_MESSAGE(*dae_pErr,"WIOD error" << std::endl);
58  return 0;
59 }
60 
61 int rio_c(long bus_addr, isisU16_t* word, short device)
62 {
63  return RIO(bus_addr, word, device);
64 }
65 
66 int read_crpt_c(int **start, int& section, int& size)
67 {
68  OUTPUT_MESSAGE(*dae_pErr,"read_crpt_c error" << std::endl);
69  return 0;
70 }
71 
72 int open_dae_cds(const char* name)
73 {
74  if (dae == 0)
75  {
77  if ( getenv("ISISVME_DEVICE") != NULL )
78  {
79  dae->open(getenv("ISISVME_DEVICE"));
80  }
81  else
82  {
83  dae->open("VXI0::MEMACC");
84  }
85  }
86  return 1;
87 }
88 
89 int __stdcall WMEM(isis32_t& bus_addr, isisU16_t* word, isisU16_t& device)
90 {
91  OUTPUT_MESSAGE(*dae_pErr,"wmem error" << std::endl);
92  return 0;
93 }
94 
95 int __stdcall WMEMD(isis32_t& bus_addr, isisU16_t* word, isis32_t& nwrds, isisU16_t& device)
96 {
97  OUTPUT_MESSAGE(*dae_pErr,"wmemd error" << std::endl);
98  return 0;
99 }
100 
101 int __stdcall RMEM(isis32_t& bus_addr, isisU16_t* word, isisU16_t& device)
102 {
103  isisU32_t temp;
104  dae->readHistogramMemory(bus_addr, &temp, 1);
105  *word = (isisU16_t)temp;
106  return 1;
107 }
108 
109 int __stdcall RMEMD(isis32_t& bus_addr, isisU16_t* word, isis32_t& nwrds, isisU16_t& device)
110 {
111  if ( ((unsigned long)word % sizeof(isisU32_t)) == 0 )
112  {
113  dae->readHistogramMemory(bus_addr, (isisU32_t*)word, nwrds / 2);
114  return 1;
115  }
116  else
117  {
118  OUTPUT_MESSAGE(*dae_pErr,"RMEMD error - address not 32bit aligned" << std::endl);
119  return 0;
120  }
121 }
122 
123 int __stdcall RMEMVI(isis32_t& bus_addr, isisU32_t* buffer, isisU32_t& length,
124  isis16_t& device, isis32_t& wrd_len)
125 {
126  if (wrd_len == 4)
127  {
128  dae->readHistogramMemory(bus_addr, buffer, length);
129  return 1;
130  }
131  else
132  {
133  OUTPUT_MESSAGE(*dae_pErr,"RMEMVI: illegal word length " << wrd_len << std::endl);
134  return 0;
135  }
136 }
137 
138 int __stdcall RIO(isis32_t& bus_addr, isisU16_t* word, isis16_t& device)
139 {
140  switch(bus_addr)
141  {
142 
143  case 1544:
144  dae->getGoodFramesLower(word);
145  break;
146 
147  case 1548:
148  dae->getGoodFramesUpper(word);
149  break;
150 
151  case 1552:
152  dae->getRawFramesLower(word);
153  break;
154 
155  case 1556:
156  dae->getRawFramesUpper(word);
157  break;
158 
159  case 2208:
160  dae->getRawPPPLower(word);
161  break;
162 
163  case 2210:
164  dae->getRawPPPMiddle(word);
165  break;
166 
167  case 2212:
168  dae->getRawPPPUpper(word);
169  break;
170 
171  case 2224:
172  dae->getGoodPPPLower(word);
173  break;
174 
175  case 2226:
176  dae->getGoodPPPMiddle(word);
177  break;
178 
179  case 2228:
180  dae->getGoodPPPUpper(word);
181  break;
182 
183  default:
184  OUTPUT_MESSAGE(*dae_pErr,"RIO: unknown address " << bus_addr << std::endl);
185  return 0;
186  break;
187  }
188  return 1;
189 }
190 
191 int __stdcall RIOD(isis32_t& bus_addr, isisU16_t* word, isisU32_t& nwrds, isis16_t& device)
192 {
193  OUTPUT_MESSAGE(*dae_pErr,"riod error" << std::endl);
194  return 0;
195 }
196 
197 void __stdcall DAESET(short& zaachan,int crat[],int daep[], int dat1[],
198  int modn[],int mpos[],int& ndet,int& nper,
199  int& ntrg,int pmap[],int& nfpp, int& pre1, int spec[],
200  int tcb1[], int timr[], int ntcftr[], int nosftr[], int& vaxmax,
201  int& status, int& daebegerror, int& logstatus, int& delay,
202  int& synch,int vetoes[], int& cper, int& save_permax,
203  int save_tpoffset[], int& datamode, int& tables_only)
204 {
205 // crat[ndet] daep[2] dat1[vaxmax] modn[ndet] mpos[ndet]
206 // pmap[256] spec[ndet] tcp1[ntc1+1] timr[ndet] vetos[5]
207 // ntcftr[16] nosftr[16] save_tpoffset[16]
208  int trcn[16];
209  int i, j, len;
210  OUTPUT_MESSAGE(*dae_pInf,"Entering VME DAEBEG" << std::endl);
211  if (dae == 0)
212  {
213  OUTPUT_MESSAGE(*dae_pErr,"Error - no DAE assigned" << std::endl);
214  return;
215  }
216  status = 0;
217  logstatus = 0;
218  daebegerror = 0;
219  if (ndet > 13*64*1024)
220  {
221  OUTPUT_MESSAGE(*dae_pErr,"Too many detectors" << std::endl);
222  daebegerror=6; // too many detectors
223  return;
224  }
225  int ic, maxcrate = 0;
226  for(i=0; i<ndet; i++)
227  {
228  if (crat[i] > maxcrate)
229  {
230  maxcrate = crat[i];
231  }
232  }
233  for(i=0; i<16; i++)
234  {
235  trcn[i] = 0;
236  }
237  for(j=0; j<ndet; j++)
238  {
239  ic = crat[j];
240  if (trcn[ic] != 0)
241  {
242  if (trcn[ic] != timr[j])
243  {
244  OUTPUT_MESSAGE(*dae_pErr,"Time regime problem" << std::endl);
245  daebegerror = 1;
246  return;
247  }
248  }
249  else
250  {
251  trcn[ic] = timr[j];
252  }
253  }
254  for(i=0; i<maxcrate; i++)
255  {
256  if (trcn[i] == 0)
257  {
258  OUTPUT_MESSAGE(*dae_pInf,"Warning: Time regime " << trcn[i] << " for crate " << i << std::endl);
259 // daebegerror = 2;
260 // return;
261  }
262  if (datamode == 1 && trcn[i] > 1)
263  {
264  OUTPUT_MESSAGE(*dae_pErr,"Time regime > 1 for crate " << i << std::endl);
265  daebegerror = 3;
266  return;
267  }
268  }
269  int permax = 0;
270  for(i=0; i<ntrg; i++)
271  {
272  permax += (ntcftr[i]+1) * (nosftr[i]+1);
273  }
274  save_permax = permax;
275  len = permax * nper; // len * daep[0] bytes of memory
276  if (len * daep[0] > daep[1])
277  {
278  OUTPUT_MESSAGE(*dae_pErr,"Not enough DAE memory" << std::endl);
279  daebegerror = 4;
280  return;
281  }
282  if (len > vaxmax)
283  {
284  OUTPUT_MESSAGE(*dae_pErr,"Not enough CRPT memory" << std::endl);
285  daebegerror = 5;
286  return;
287  }
288  OUTPUT_MESSAGE(*dae_pInf,"DAE memory = " << (len * daep[0]) / (1024 * 1024) << " Mb" << std::endl);
289  OUTPUT_MESSAGE(*dae_pInf,"NPER = " << nper << std::endl);
290  dae->stopRun();
291  dae->resetRunController();
292  if (tables_only == 0)
293  {
294  dae->clearHistogramMemory();
295  }
296  else
297  {
298  OUTPUT_MESSAGE(*dae_pInf,"TABLES ONLY - memory not zerod" << std::endl);
299  }
300  // TCG
301  // prescale
302  OUTPUT_MESSAGE(*dae_pInf, "Programming Time channels" << std::endl);
303 // need to allow for the prescale and delay
304  unsigned long * new_tcb1 = new unsigned long[ntcftr[0]+1];
305  int pulse_generator = 0; // are we using a pulse generator to simulate a frame synch delay?
306  if (getenv("PULSE_GENERATOR") != 0)
307  {
308  pulse_generator = atol(getenv("PULSE_GENERATOR"));
309  }
310  int frame_delay;
311  if (pulse_generator == 1)
312  {
313  frame_delay = ((tcb1[0] * pre1) / 32);
314  OUTPUT_MESSAGE(*dae_pInf,"PULSE_GENERATOR: frame delay = " << 4 * delay + frame_delay << " us" << std::endl);
315  for(i=0; i <= ntcftr[0]; i++)
316  {
317 // tcb are in clock pulses, delay is in us hence * 32
318  new_tcb1[i] = tcb1[i] * pre1 - frame_delay * 32;
319  if ( (new_tcb1[i] & 0xff000000) != 0 )
320  {
321  OUTPUT_MESSAGE(*dae_pWarn,"Warning: TCG value larger than 24 bits" << std::endl);
322  }
323  }
324  }
325  else
326  {
327  OUTPUT_MESSAGE(*dae_pInf, "Frame sync delay = " << delay * 4 << " us" << std::endl);
328  for(i=0; i <= ntcftr[0]; i++)
329  {
330 // tcb are in clock pulses, delay is in us hence * 32
331 // new_tcb1[i] = tcb1[i] * pre1 + (4 * delay) * 32;
332 // get 0.21us for free (7 clock pulses)
333  new_tcb1[i] = tcb1[i] * pre1 - 7;
334  if ( (new_tcb1[i] & 0xff000000) != 0 )
335  {
336  OUTPUT_MESSAGE(*dae_pWarn,"Warning: TCG value larger than 24 bits" << std::endl);
337  }
338  }
339  }
340  OUTPUT_MESSAGE(*dae_pInf,"setting " << ntcftr[0] << " time channels from " << (double)new_tcb1[0] / 32.0 << " to " << (double)new_tcb1[ntcftr[0]] / 32.0 << " us" << std::endl);
341  dae->setTimeChannels(new_tcb1, ntcftr[0]);
342  delete[] new_tcb1;
343  dae->setFrameSyncDelay(4 * delay);
344  OUTPUT_MESSAGE(*dae_pInf,"Programming POSLUT" << std::endl);
345  dae->programDAE1POSLUT(crat, maxcrate, modn, mpos, spec, ndet, nper);
346 //
347  dae->setFrameSync(synch == 1);
348 //
349  OUTPUT_MESSAGE(*dae_pInf, "Setting vetos" << std::endl);
350  dae->setDAE1Vetos(vetoes, 3);
351  logstatus = 1; // OK
352  OUTPUT_MESSAGE(*dae_pInf , "Starting run" << std::endl);
353  if (tables_only == 0)
354  {
355  dae->startRun(true);
356  }
357  else
358  {
359  dae->startRun(false); // don't clear frames and ppp
360  }
361  status = 1;
362  OUTPUT_MESSAGE(*dae_pInf,"Leaving VME DAEBEG" << std::endl);
363 }
364 
365 void __stdcall DAEPAUSE(short& zaachan, isisU32_t& igfc, isisU32_t& irfc, int& status)
366 {
367  OUTPUT_MESSAGE(*dae_pInf , "Entering VME DAEPAUSE" << std::endl);
368  status = 1;
369  dae->stopRun();
370  dae->getRawFrames(&irfc);
371  dae->getGoodFrames(&igfc);
372 // return 0 as the frame count to avoid overcounting. DAE1 zerod these
373 // counters on stop/start data collection, but DAE-II doesn't
374  irfc = igfc = 0;
375  OUTPUT_MESSAGE(*dae_pInf , "Leaving VME DAEPAUSE" << std::endl);
376 
377 }
378 
379 void __stdcall DAERESUME(short& zaachan, int& cper, int& save_permax, int& save_tpoffset, int& status)
380 {
381  OUTPUT_MESSAGE(*dae_pInf , "Entering VME DAERESUME" << std::endl);
382  status = 1;
383  dae->changePeriod(cper);
384  dae->startRun(false);
385  OUTPUT_MESSAGE(*dae_pInf , "Leaving VME DAERESUME" << std::endl);
386 }
387 
388 void __stdcall DAEEND(isisU32_t dat1[], int daep[], int ntcftr[], int nosftr[],
389  int& nper, int& ntrg, short& zaachan, isisU32_t& igfc, isisU32_t& irfc,
390  int& status)
391 {
392  int i;
393  bool differ = false;
394  isisU32_t* temp_buffer;
395  OUTPUT_MESSAGE(*dae_pInf , "Entering VME DAEEND" << std::endl);
396  dae->stopRun();
397  dae->getRawFrames(&irfc);
398  dae->getGoodFrames(&igfc);
399  int permax = 0;
400  for(i=0; i<ntrg; i++)
401  {
402  *dae_pInf << "Time regime " << i+1 << " NTC " << ntcftr[i] << " NOS " << nosftr[i] << std::endl;
403  permax += (ntcftr[i] + 1) * (nosftr[i] + 1);
404  }
405  int len = permax * nper;
406  *dae_pInf << nper << " periods of size " << permax << " words" << std::endl;
407  temp_buffer = 0 /* new isisU32_t[len] */ ;
408 // these two reads should be the same
409 // std::cerr << "Trying readAllDAE1Spectra()" << std::endl;
410 // dae->readAllDAE1Spectra(temp_buffer);
411 
412  SYSTEMTIME s;
413  GetSystemTime(&s);
414  *dae_pInf << "Trying readHistogramMemory() " << (double)s.wSecond + (double)s.wMilliseconds/1000.0 << std::endl;
415  dae->readHistogramMemory(0, dat1, len);
416  GetSystemTime(&s);
417  *dae_pInf << "Done readHistogramMemory() " << (double)s.wSecond + (double)s.wMilliseconds/1000.0 << std::endl;
418 #if 0
419  std::cerr << "Checking for differences ..." << std::endl;
420  for(i=0; i< len; i++)
421  {
422  if (temp_buffer[i] != dat1[i])
423  {
424  differ = true;
425 // std::cerr << i << " " << hex << temp_buffer[i] << " " << dat1[i] << dec << std::endl;
426  }
427  }
428  if (differ)
429  {
430  std::cerr << "they differ";
431  }
432  std::cerr << std::endl;
433 #endif
434  delete[] temp_buffer;
435  status = 1;
436  OUTPUT_MESSAGE(*dae_pInf , "Leaving VME DAEEND" << std::endl);
437 }
438 
439 int __stdcall DAECP(int code)
440 {
441  OUTPUT_MESSAGE(*dae_pErr , "error - called VME DAECP" << std::endl);
442  return 0;
443 }
444 
445 int read_crpt_array_c(int start, int length, int section[])
446 {
447  OUTPUT_MESSAGE(*dae_pErr , "error - called VME read_crpt_array_c" << std::endl);
448  return 1;
449 }
450 
int __stdcall RIOD(isis32_t &bus_addr, isisU16_t *word, isis32_t &nwrds, isis16_t &device)
void __stdcall DAESET(short &zaachan, int crat[], int daep[], int dat1[], int modn[], int mpos[], int &ndet, int &nper, int &ntrg, int pmap[], int &nfpp, int &pre1, int spec[], int tcb1[], int timr[], int ntcftr[], int nosftr[], int &vaxmax, int &status, int &daebegerror, int &logstatus, int &delay, int &synch, int vetoes[], int &cper, int &save_permax, int save_tpoffset[], int &datamode, int &tables_only)
Definition: daeset.cpp:197
short isis16_t
Definition: isisvme_types.h:13
int setDAE1Vetos(int vetos[], int n, DAEstatus &status)
Definition: isisdae.cpp:1362
int getGoodPPPLower(isisU32_t *value, DAEstatus &status)
Definition: isisdae.cpp:1043
static std::ostringstream * dae_pOut
Definition: daeset.cpp:9
int __stdcall RMEM(isis32_t &bus_addr, isisU16_t *word, isisU16_t &device)
Definition: daeset.cpp:101
void __stdcall DAERESUME(short &zaachan, int &cper, int &save_permax, int &save_tpoffset, int &status)
Definition: daeset.cpp:379
int stopRun(StopRunMode mode, DAEstatus &status)
Definition: isisdae.cpp:877
int getGoodPPPUpper(isisU32_t *value, DAEstatus &status)
Definition: isisdae.cpp:1048
int read_crpt_array_c(int start, int length, int section[])
Definition: daeset.cpp:445
int __stdcall WIOD(isis32_t &bus_addr, isisU16_t *word, isis32_t &nwrds, isis16_t &device)
Definition: daeset.cpp:55
static std::ostringstream * dae_pWarn
Definition: daeset.cpp:9
unsigned long isisU32_t
Definition: isisvme_types.h:8
isis32_t open(const char *dae_name, DAEstatus &status)
Definition: isisdae.cpp:613
static bool simulate_vme
Definition: daeset.cpp:8
int clearHistogramMemory(DAEstatus &status)
Definition: isisdae.cpp:815
int open_dae_cds(const char *name)
Definition: daeset.cpp:72
int getRawPPPLower(isisU32_t *value, DAEstatus &status)
Definition: isisdae.cpp:1033
int read_crpt_c(int **start, int &section, int &size)
Definition: daeset.cpp:66
int __stdcall RMEMVI(isis32_t &bus_addr, isisU32_t *buffer, isisU32_t &length, isis16_t &device, isis32_t &wrd_len)
Definition: daeset.cpp:123
static ISISDAE * dae
Definition: daeset.cpp:7
static std::ostringstream * dae_pErr
Definition: daeset.cpp:9
static std::ostringstream * dae_pInf
Definition: daeset.cpp:9
int resetRunController(DAEstatus &status)
Definition: isisdae.cpp:987
int setTimeChannels(int crat, isisU32_t *tcb, int ntc, DAEstatus &status)
Definition: isisdae.cpp:341
int set_dae_streams(std::ostringstream *pOut, std::ostringstream *pInf, std::ostringstream *pWarn, std::ostringstream *pErr, std::ostringstream *pDbg)
static std::ostringstream * dae_pDbg
Definition: daeset.cpp:9
int startRun(bool clear_counters, int run_number, time_t &start_time, DAEstatus &status)
Definition: isisdae.cpp:843
int getRawFrames(isisU32_t *value, DAEstatus &status)
Definition: isisdae.cpp:997
unsigned short isisU16_t
Definition: isisvme_types.h:7
int __stdcall DAECP(int code)
Definition: daeset.cpp:439
int __stdcall WIO(isis32_t &bus_addr, isisU16_t *word, isis16_t &device)
Definition: daeset.cpp:49
int __stdcall RMEMD(isis32_t &bus_addr, isisU16_t *word, isis32_t &nwrds, isisU16_t &device)
Definition: daeset.cpp:109
int changePeriod(int period, int daq_period, DAEstatus &status)
Definition: isisdae.cpp:221
void __stdcall DAE_DEVICE(short &tempchan, int &simulate)
int getRawPPPUpper(isisU32_t *value, DAEstatus &status)
Definition: isisdae.cpp:1038
int setFrameSync(FrameSync fs, int muon_pulse, DAEstatus &status)
Definition: isisdae.cpp:1491
int __stdcall RIO(isis32_t &bus_addr, isisU16_t *word, isis16_t &device)
Definition: daeset.cpp:138
int programDAE1POSLUT(int crat[], int maxcrate, int modn[], int mpos[], int spec[], int ndet, int nper_daq, DAEstatus &status)
Definition: isisdae.cpp:365
int readHistogramMemory(unsigned long start, isisU32_t *buffer, int len, DAEstatus &status)
Definition: isisdae.cpp:1220
int setFrameSyncDelay(isisU32_t delay, DAEstatus &status)
Definition: isisdae.cpp:1501
int rio_c(long bus_addr, isisU16_t *word, short device)
Definition: daeset.cpp:61
long isis32_t
Definition: isisvme_types.h:14
int __stdcall WMEM(isis32_t &bus_addr, isisU16_t *word, isisU16_t &device)
Definition: daeset.cpp:89
void __stdcall DAEEND(isisU32_t dat1[], int daep[], int ntcftr[], int nosftr[], int &nper, int &ntrg, short &zaachan, isisU32_t &igfc, isisU32_t &irfc, int &status)
Definition: daeset.cpp:388
void __stdcall DAEPAUSE(short &zaachan, isisU32_t &igfc, isisU32_t &irfc, int &status)
Definition: daeset.cpp:365
int __stdcall WMEMD(isis32_t &bus_addr, isisU16_t *word, isis32_t &nwrds, isisU16_t &device)
Definition: daeset.cpp:95
int getGoodFrames(isisU32_t *value, DAEstatus &status)
Definition: isisdae.cpp:1028