-
Notifications
You must be signed in to change notification settings - Fork 0
/
readPicoRoot.cpp
383 lines (275 loc) · 9.1 KB
/
readPicoRoot.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <limits.h>
#include <libusbtc08/usbtc08.h>
#include <string>
#include <iostream>
#include <csignal>
#include <getopt.h>
#include <sys/time.h> /* struct timeval, select() */
#include "TFile.h"
#include "TTree.h"
using namespace std;
ULong64_t sys_time0 = 0;
ULong64_t sys_time_temp = 0;
ULong64_t sys_time = 0;
ULong64_t sys_time_last = 0;
ULong64_t sec_time_2020;
UInt_t day_loops;
time_t rawtime;
struct tm *timinfo;
#define Sleep(a) usleep(1000*a)
#define scanf_s scanf
#define fscanf_s fscanf
#define memcpy_s(a,b,c,d) memcpy(a,c,d)
typedef enum enBOOL
{
FALSE, TRUE
} BOOL;
ULong64_t getTime()
{
time(&rawtime);
timinfo = gmtime(&rawtime);
sec_time_2020 = ((timinfo->tm_year-120)*365*86400) + (timinfo->tm_yday*86400) + (timinfo->tm_hour*3600) + (timinfo->tm_min*60) + timinfo->tm_sec;
struct timeval t1;
struct timezone tz;
gettimeofday(&t1, &tz);
sys_time_temp = (t1.tv_sec) * 1000000 + t1.tv_usec;
if(sys_time0 == 0)
sys_time0 = sys_time_temp;
sys_time = sys_time_temp - sys_time0 + (day_loops*24*60*60*1000000);
//We're in a new day;
if(sys_time < 0)
{
day_loops++;
sys_time = sys_time_temp - sys_time0 + (day_loops*24*60*60*1000000);
}
return sys_time;
}
#define PREF4 __stdcall
#define BUFFER_SIZE 1000 // Buffer size to be used for streaming mode captures
using namespace std;
bool external_kill = false;
void signalHandler( int signum ) {
cout << "Interrupt signal (" << signum << ") received.\n";
cout << "Going to shut things down (" << signum << ") properly (I hope).\n";
external_kill = true;
}
void usage()
{
cout << "USAGE:" << std::endl
<< "./readPicoRoot --duration_sec <nseconds> [options]" << std::endl << std::endl
<< "OPTIONS:" << std::endl
<< "-d <nseconds> , --duration_sec <nseconds>" << std::endl
<< "\t Set the duration of the acquisition"<< std::endl << std::endl
<< "-h, --help" << std::endl
<< "\t Show this help menu" << std::endl << std::endl
<< "-o <output dir> , --output_dir <output dir>" << std::endl
<< "\t Set the path used for the output rootfile etc" << std::endl << std::endl
<< "-r <run number> , --run_number <run number>" << std::endl
<< "\t Set the run number used for filenameing etc" << std::endl << std::endl
<< "-u <time in seconds> , --update_interval <time in seconds>" << std::endl
<< "\t Set the time interval between re-plotting of the hists and or traces" << std::endl << std::endl;
}
int16_t handle = 0; /* The handle to a TC-08 returned by usb_tc08_open_unit() or usb_tc08_open_unit_progress() */
int8_t selection = 0; /* User selection from the main menu */
float temp[USBTC08_MAX_CHANNELS + 1] = {0.0}; /* Buffer to store single temperature readings from the TC-08 */
float * temp_buffer[USBTC08_MAX_CHANNELS + 1]; /* 2D array to be used for streaming data capture */
int32_t times_buffer[BUFFER_SIZE] = {0}; /* Array to hold the time of the conversion of the first channel for each set of readings in streaming mode captures */
int16_t overflows[USBTC08_MAX_CHANNELS + 1] = {0}; /* Array to hold overflow flags for each channel in streaming mode captures */
int32_t channel = 0; /* Loop counter for channels */
uint32_t reading = 0; /* Loop counter for readings */
int32_t retVal = 0; /* Return value from driver calls indication success / error */
USBTC08_INFO unitInfo; /* Struct to hold unit information */
uint32_t numberOfReadings = 0; /* Number of readings to collect in streaming mode */
int32_t readingsCollected = 0; /* Number of readings collected at a time in streaming mode */
uint32_t totalReadings[USBTC08_MAX_CHANNELS + 1] = {0}; /* Total readings collected in streaming mode */
TTree *picotemp = 0;
bool tryread;
bool busyreading;
void read_temp()
{
if(busyreading == false)
{
busyreading = true;
usb_tc08_get_single(handle, temp, NULL, USBTC08_UNITS_CENTIGRADE);
printf(" done!\n\nCJC : %3.2f C\n", temp[0]);
for (channel = (int32_t) USBTC08_CHANNEL_1; channel < USBTC08_MAX_CHANNELS + 1; channel++)
{
printf("Channel %d: %3.2f C\n", channel, temp[channel]);
}
picotemp->Fill();
busyreading = false;
}
}
int main(int argc, char** argv)
{
bool firstread = true;
tryread = false;
busyreading = false;
//gErrorIgnoreLevel = kError;
signal(SIGTERM, signalHandler);
signal(SIGINT, signalHandler);
ULong64_t acquisition_runtime = 0;
string f_path = "./";
bool showhelp = false;
bool bdefault = false;
bool bunknown = false;
ULong64_t plotinterval = 10;
Int_t runnumber = 111;
int c;
while(true)
{
int option_index = 0;
static struct option long_options[] =
{
{"duration_sec",required_argument,NULL, 'd'},
{"output_dir",required_argument,NULL, 'o'},
{"run_number",required_argument,NULL, 'r'},
{"update_interval ",required_argument,NULL, 'u'},
{"help",no_argument,NULL, 'h'},
{0, 0, 0, 0}
};
c = getopt_long_only(argc, argv, "d:o:r:u:h",long_options, &option_index);
if (c == -1)
break;
switch (c)
{
case 'd':
acquisition_runtime = atoi(optarg)*1000000;;
break;
case 'h':
showhelp = true;
break;
case 'o':
f_path = string(optarg);
break;
case 'r':
runnumber = atoi(optarg);
break;
case 'u':
plotinterval = atoi(optarg)*1000000;
break;
case '?':
bunknown = true;
break;
default:
bdefault = true;
}
}
if(showhelp || bunknown || bdefault)
{
usage();
exit(0);
}
if(acquisition_runtime < 1)
{
printf("Please indicate a run duration with the -d option\n");fflush(stdout);
usage();
exit(0);
}
char fname[1000];
sprintf(fname,"%s/pico.root",f_path.c_str());
TFile *fraw = new TFile(fname,"RECREATE");
picotemp = new TTree("picotemp","picotemp");
picotemp->Branch("sec_time_2020",&sec_time_2020,"sec_time_2020/l");
picotemp->Branch("temp",temp,"temp[9]/F");
picotemp->Branch("runnumber",&runnumber,"runnumber/I");
int32_t minimumIntervalMs = 0; /* Minimum time interval between samples. */
/* Print header information */
printf ("Pico Technology USB TC-08 Console Example Program\n");
printf ("-------------------------------------------------\n\n");
printf ("Looking for USB TC-08 devices on the system.\n\n");
printf ("Progress: ");
/* Try to open one USB TC-08 unit, if available
* The simplest way to open the unit like is this:
*
* handle = usb_tc08_open_unit();
*
* but that will cause your program to wait while the driver downloads
* firmware to any connected TC-08 units. If you're making an
* interactive application, it's better to use
* usb_tc08_open_unit_async() which returns immediately and allows you to
* display some sort of progress indication to the user as shown below:
*/
retVal = usb_tc08_open_unit_async();
/* Make sure no errors occurred opening the unit */
if (!retVal)
{
printf ("\n\nError opening unit. Exiting.\n");
return -1;
}
/* Display a text "progress bar" while waiting for the unit to open */
while ((retVal = usb_tc08_open_unit_progress(&handle, NULL)) == USBTC08_PROGRESS_PENDING)
{
/* Update our "progress bar" */
printf("|");
fflush(stdout);
Sleep(200);
}
/* Determine whether a unit has been opened */
if (retVal != USBTC08_PROGRESS_COMPLETE || handle <= 0)
{
printf ("\n\nNo USB TC-08 units could be opened. Exiting.\n");
return -1;
}
else
{
printf ("\n\nUSB TC-08 opened successfully.\n");
}
/* Get the unit information */
unitInfo.size = sizeof(unitInfo);
usb_tc08_get_unit_info(handle, &unitInfo);
printf("\nUnit information:\n");
printf("Driver: %s \nSerial: %s \nCal date: %s \n", unitInfo.DriverVersion, unitInfo.szSerial, unitInfo.szCalDate);
/* Set up all channels */
retVal = usb_tc08_set_channel(handle, 0,'C');
for (channel = 1; channel < (USBTC08_MAX_CHANNELS + 1); channel++)
{
retVal &= usb_tc08_set_channel(handle, channel,'K');
}
/* Make sure this was successful */
if (retVal)
{
printf("\nEnabled all channels, selected Type K thermocouple.\n");
}
else
{
printf ("\n\nError setting up channels. Exiting.\n");
usb_tc08_close_unit(handle);
Sleep(2000);
return -1;
}
getTime();
Double_t sys_time_last = sys_time;
while(!external_kill && sys_time < acquisition_runtime)
{
if(sys_time - sys_time_last > plotinterval || firstread)
{
tryread = true;
}
else
{
tryread = false;
}
if(tryread == true && busyreading == false)
{
if(firstread)
firstread = false;
cout << Double_t(sys_time)/1e6 << endl;
sys_time_last = sys_time;
read_temp();
}
Sleep(300);
getTime();
}
fraw->Write();
usb_tc08_close_unit(handle);
return 0;
}