MIDAS
Loading...
Searching...
No Matches
odbhist.cxx
Go to the documentation of this file.
1/********************************************************************\
2
3 Name: odbhist.c
4 Created by: Stefan Ritt, Ilia Chachkhunashvili
5
6 Contents: MIDAS history display utility
7
8 $Id$
9
10\********************************************************************/
11
12#include <stdio.h>
13#include <string.h>
14#include <stdlib.h>
15#include <ctype.h>
16#include <fcntl.h>
17
18#ifdef _MSC_VER
19#include <io.h>
20#else
21#include <unistd.h>
22#endif
23
24/*------------------------------------------------------------------*/
25
26#define SUCCESS 1
27#define TRUE 1
28#define FALSE 0
29
30typedef unsigned int DWORD;
31typedef int INT;
32typedef int BOOL;
33typedef char str[256];
34
35/*------------------------------------------------------------------*/
36
37str var_params[100]; /* varables to print 100 each of 256 char lenth */
38
41char file_name[256], var_name[256], mid_name[256];
42double total[100], value[100];
43
44/*------------------------------------------------------------------*/
45
47{
48 if (str1 == NULL && str2 != NULL)
49 return FALSE;
50 if (str1 != NULL && str2 == NULL)
51 return FALSE;
52 if (str1 == NULL && str2 == NULL)
53 return TRUE;
54
55 while (*str1)
56 if (toupper(*str1++) != toupper(*str2++))
57 return FALSE;
58
59 if (*str2)
60 return FALSE;
61
62 return TRUE;
63}
64
65/*------------------------------------------------------------------*/
66
67int odb_hist(char *file_name, int run_number, char *var_name, int quiet,
68 double *value, int eoln, int boln, int print)
69/********************************************************************\
70
71 Routine: odb_hist
72
73 Purpose: Print ODB variable for a single .odb file
74
75 Input:
76 char *file_name Name of ODB file
77 INT run_number Run number for ODB file
78 char *var_name Name of variable to print
79 int quiet If set, don't print run number
80 int eoln end of line used to detemine when to print "\n"
81 int boln begin of line used to determin when to print
82 run number if it's needed
83 int print used to print or not the value of variable.
84 This it's used for "Emils' function", so it
85 will take and return the value of variable
86 without printing it.
87
88 Note:
89 There are two variables eoln and boln because there can be begin
90 and end of line together when we have just one variable.
91
92 Output:
93 double *value ODB value in double format
94
95 Function value:
96 SUCCESS Successful completion
97
98\********************************************************************/
99{
100 FILE *f;
101 char str[256], path[256], key_name[256], line[256];
102 int i, index, a, k;
103
104 /* assemble file name */
105 snprintf(str, sizeof(str), file_name, run_number);
106
107 /* split var_name into path and key with index */
108 strcpy(path, "");
109 for (i = strlen(var_name) - 1; i >= 0 && var_name[i] != '/'; i--);
110 if (var_name[i] == '/') {
111 strcpy(path, "[");
112 strcat(path, var_name);
113 path[i + 1] = 0;
114 strcat(path, "]\n");
115 strcpy(key_name, var_name + i + 1);
116 }
117 if (strchr(key_name, '[')) {
118 index = atoi(strchr(key_name, '[') + 1);
119 *strchr(key_name, '[') = 0;
120 } else
121 index = -1;
122
123 f = fopen(str, "r");
124 if (f == NULL)
125 return 0;
126
127 if ((!quiet) && boln && print)
128 printf("%5d: ", run_number);
129
130 /* search path */
131 do {
132 char* s;
133 s = fgets(line, sizeof(line), f);
134 if (s == NULL)
135 break;
136 if (line[0] == '[')
137 if (equal_ustring(line, path)) {
138 /* look for key */
139 do {
140 s = fgets(line, sizeof(line), f);
141 if (s == NULL)
142 break;
143 if (strchr(line, '=') != NULL) {
144 strcpy(str, line);
145 *(strchr(str, '=') - 1) = 0;
146
147 /* check if key name matches */
148 if (equal_ustring(str, key_name)) {
149 if (index == -1) {
150 /* non-arrays */
151 strcpy(str, strchr(line, '=') + 2);
152 if (strchr(str, ':') != NULL) {
153 strcpy(str, strchr(str, ':') + 2);
154 if (strchr(str, '\n') != NULL)
155 *strchr(str, '\n') = 0;
156 if (str[0] == '[' && strchr(str, ']') != NULL)
157 strcpy(str, strchr(str, ']') + 2);
158 if (print)
159 printf("%s", str);
160 *value = strtod(str, NULL);
161 goto finish;
162 }
163 } else {
164 /* arrays */
165 for (i = 0; i <= index; i++) {
166 s = fgets(line, sizeof(line), f);
167 if (s == NULL)
168 break;
169 }
170 if (line[0] == '[' && atoi(line + 1) == index) {
171 strcpy(str, strchr(line, ']') + 2);
172 if (strchr(str, '\n') != NULL)
173 *strchr(str, '\n') = 0;
174 if (print)
175 printf("%s", str);
176 *value = strtod(str, NULL);
177 }
178 goto finish;
179 }
180
181 }
182 }
183
184 } while (line[0] != '[' || line[1] != '/');
185
186 }
187 } while (!feof(f));
188
189 finish:
190 if (print) {
191 if (eoln)
192 printf("\n");
193 else {
194 a = 0;
195 while (str[a] != '\0')
196 a++;
197 k = a;
198 while ((str[k] != '.') && (k >= 0))
199 k--;
200 for (i = 0; i < (10 - (a - k)); i++)
201 printf(" ");
202 printf("\t");
203 }
204 fclose(f);
205 }
206
207 return SUCCESS;
208}
209
210/*------------------------------------------------------------------*/
211
212int load_pars_from_file(char filename[256])
213/********************************************************************\
214
215 Routine: load_pars_from_file
216
217 Purpose: Load parameters for odbhist from file
218
219 Input:
220 char *filename Name of configuration file
221
222 Output:
223 <implicit through global variables>
224
225 Function value:
226 1 Successful completion
227 0 Error
228
229\********************************************************************/
230{
231 FILE *f1;
232 char line[256];
233 int result;
234 int getstr;
235
236 getstr = 1;
237
238 f1 = fopen(filename, "r");
239 if (f1 != NULL) {
240 result = 1;
241 while (result != 0) {
242 if (getstr) {
243 if (fgets(line, sizeof(line), f1) == NULL)
244 break;
245 } else
246 getstr = 1;
247
248 if (line[0] == '[') {
249 switch (line[1]) {
250 case 'a':
251 if (fgets(line, sizeof(line), f1) != NULL) {
252 switch (line[0]) {
253 case '1':
254 add = TRUE;
255 break;
256 case '0':
257 add = FALSE;
258 break;
259 default:
260 result = 0;
261 }
262 } else
263 result = 0;
264 break;
265
266 case 'q':
267 if (fgets(line, sizeof(line), f1) != NULL) {
268 switch (line[0]) {
269 case '1':
270 quiet = TRUE;
271 break;
272 case '0':
273 quiet = FALSE;
274 break;
275 default:
276 result = 0;
277 }
278 } else
279 result = 0;
280 break;
281
282 case 'f':
283 if ((fgets(line, sizeof(line), f1) != NULL) && (line[0] != '['))
284 strcpy(file_name, line);
285 else
286 result = 0;
287 break;
288
289 case 'v':
290 j = -1;
291 while (fgets(line, sizeof(line), f1) != NULL && line[0] != '[') {
292 if (line[0] != '\n')
293 strcpy(var_params[++j], line);
294 }
295 if (j == -1)
296 result = 0;
297 else
298 getstr = 0;
299
300 /* to get correct number of variables in "j" global variable */
301 if (line[0] != '[')
302 j--;
303 break;
304
305 case 'r':
306 if ((fgets(line, sizeof(line), f1) != NULL) && (line[0] != '['))
307 start_run = atoi(line);
308 else {
309 result = 0;
310 break;
311 }
312 if ((fgets(line, sizeof(line), f1) != NULL) && (line[0] != '['))
313 end_run = atoi(line);
314 else {
315 result = 0;
316 break;
317 }
318
319 break;
320
321 default:
322 result = 0;
323 }
324 }
325 } /* while */
326 } /* if */
327 else {
328 result = 0;
329 printf("\n ERROR:\nCan't open file %s\n", filename);
330 }
331 if (result != 0)
332 if (fclose(f1))
333 result = 0;
334
335 return result;
336}
337
338/*------------------------------------------------------------------*/
339
340typedef struct {
341 short int event_id;
342 short int trigger_mask;
345 DWORD data_size;
347
348#define EVENTID_BOR ((short int) 0x8000)
349#define EVENTID_EOR ((short int) 0x8001)
350#define EVENTID_MESSAGE ((short int) 0x8002)
352int extract(char *mid_file, char *odb_file)
353/********************************************************************\
354
355 Routine: extract
356
357 Purpose: Extract ODB file from MID file
358
359 Input:
360 char *mid_file Midas file name, usually runxxxxx.mid
361 char *odb_file ODB file name, usually runxxxxx.odb
362
363 Function value:
364 1 Successful completion
365 0 Error
366
367\********************************************************************/
368{
369 int fhm, fho, run_number;
370 unsigned int n;
371 EVENT_HEADER header;
372 char *buffer, *p, odb_name[256];
373
374 fhm = open(mid_file, O_RDONLY, 0644);
375 if (fhm < 0) {
376 printf("Cannot open file \"%s\"\n", mid_file);
377 return 0;
378 }
379
380 if (strchr(odb_file, '%')) {
381 p = mid_file;
382 while (*p && !isdigit(*p))
383 p++;
384 run_number = atoi(p);
386 } else
387 strcpy(odb_name, odb_file);
388
389 fho = open(odb_name, O_WRONLY | O_CREAT | O_APPEND, 0644);
390 if (fho < 0) {
391 printf("Cannot open file \"%s\"\n", odb_name);
392 return 0;
393 }
394
395 n = read(fhm, &header, sizeof(header));
396 if (n != sizeof(header)) {
397 printf("Cannot read event header from file \"%s\"\n", mid_file);
398 return 0;
399 }
400
401 if (header.event_id != EVENTID_BOR) {
402 printf("First event in \"%s\" is not a BOR event\n", mid_file);
403 return 0;
404 }
405
406 buffer = (char*)malloc(header.data_size);
407
408 n = read(fhm, buffer, header.data_size);
409 if (n < header.data_size) {
410 printf("Cannot read %d bytes from \"%s\"\n", header.data_size, mid_file);
411 return 0;
412 }
413
414 n = write(fho, buffer, header.data_size);
415 if (n < header.data_size) {
416 printf("Cannot write %d bytes to \"%s\"\n", header.data_size, odb_name);
417 return 0;
418 }
419
420 close(fhm);
421 close(fho);
422 free(buffer);
423
424 printf("\"%s\" successfully created\n", odb_name);
425 return 1;
426}
427
428/*------------------------------------------------------------------*/
429
430int main(int argc, char *argv[])
431{
432 int cfg, print, n_files;
433
434 strcpy(var_name, "/Runinfo/Run number");
435 strcpy(file_name, "run%05d.odb");
436 start_run = end_run = 0;
437 quiet = FALSE;
438 add = FALSE;
439 print = 1; /* print = 1 means that variable will be printed */
440
441 k = 0;
442 cfg = 0;
443 j = -1;
444 n_files = 0;
445 mid_name[0] = 0;
446
447 /* parse command line parameters */
448
449 for (i = 1; i < argc; i++) {
450 if (argv[i][0] == '-')
451 if (argv[i][1] == 'c') {
452 printf("%s", argv[i + 1]);
453 printf("\n");
454 if (!(load_pars_from_file(argv[i + 1])))
455 goto usage;
456 else
457 cfg = 1;
458 }
459 }
460
461 if (argc <= 1)
462 goto usage;
463
464 for (i = 1; i < argc; i++) {
465 if (argv[i][0] == '-') {
466 if (argv[i][1] == 'q')
467 quiet = TRUE;
468 else if (argv[i][1] == 'a')
469 add = TRUE;
470 else {
471 if (i + 1 >= argc || argv[i + 1][0] == '-')
472 goto usage;
473 if (argv[i][1] == 'r') {
474 start_run = atoi(argv[++i]);
475 end_run = atoi(argv[++i]);
476 } else if (argv[i][1] == 'v') {
477 j = -1;
478 while (i + 1 < argc && argv[i + 1][0] != '-')
479 if (argv[i + 1][0] != '-') {
480 i++;
481 j++;
482 if (argv[i][0] != '-')
483 strcpy(var_params[j], argv[i]);
484 }
485 } else if (argv[i][1] == 'f')
486 strcpy(file_name, argv[++i]);
487 else if (argv[i][1] == 'e')
488 strcpy(mid_name, argv[++i]);
489 else
490 goto usage;
491 }
492 } else if (!cfg) {
493
494 usage:
495 printf("\nusage: odbhist -r <run1> <run2> -v <varname>[index]\n");
496 printf(" [-f <filename>] [-q] [-a] [-c <file>] [-e <file>]\n");
497 printf(" <run1> <run2> Range of run numbers (inclusive)\n");
498 printf
499 (" <varname> ODB variable name like \"/Runinfo/Run number\"\n");
500 printf(" [index] Index if <varname> is an array\n");
501 printf(" <filename> run%%05d.odb by default\n");
502 printf(" -e <file> Extract ODB file from MID file\n");
503 printf(" -q Don't display run number\n");
504 printf(" -a Add numbers for all runs\n");
505 printf(" -c load configuration from file\n");
506 printf(" (parameters loaded from cfg file will be\n");
507 printf(" overwriten by parameters from command line)\n");
508 return 0;
509
510 }
511 }
512
513 if (mid_name[0]) {
515 return 1;
516 }
517
518 if (end_run < start_run) {
519 printf("Second run is %d and must be larger or equal than first run %d\n",
521 return 0;
522 }
523
524 if (j == -1)
525 goto usage;
526
527 /* printing of header is needed here */
528 for (run = start_run; run <= end_run; run++) {
529 for (k = 0; k <= j; k++) {
530 if (k == j)
531 eoln = 1;
532 else
533 eoln = 0;
534
535 if (k == 0)
536 boln = 1;
537 else
538 boln = 0;
539
540 strcpy(var_name, var_params[k]);
541 value[k] = 0;
542
544 if (status != SUCCESS)
545 break;
546
547 total[k] += value[k];
548 n_files++;
549 } /*for k */
550 } /* for run */
551
552 if (add) {
553 printf("\nTotal: ");
554 if (quiet)
555 printf("\n");
556 for (k = 0; k <= j; k++)
557 printf("%lf\t", total[k]);
558 printf("\n");
559 }
560
561 if (n_files == 0) {
562 printf("No files found in selected range\n");
563 }
564
565 return 1;
566}
static void usage()
unsigned int DWORD
Definition mcstd.h:51
int main()
Definition hwtest.cxx:23
INT run_number[2]
Definition mana.cxx:246
DWORD n[4]
Definition mana.cxx:247
INT index
Definition mana.cxx:271
DWORD BOOL
Definition midas.h:105
int INT
Definition midas.h:129
#define trigger_mask
#define read(n, a, f)
#define event_id
#define write(n, a, f, d)
#define serial_number
#define time_stamp
str var_params[100]
Definition odbhist.cxx:37
int BOOL
Definition odbhist.cxx:32
BOOL equal_ustring(char *str1, char *str2)
Definition odbhist.cxx:46
double total[100]
Definition odbhist.cxx:42
int load_pars_from_file(char filename[256])
Definition odbhist.cxx:212
INT j
Definition odbhist.cxx:40
INT boln
Definition odbhist.cxx:40
DWORD run
Definition odbhist.cxx:39
int INT
Definition odbhist.cxx:31
double value[100]
Definition odbhist.cxx:42
INT k
Definition odbhist.cxx:40
char str[256]
Definition odbhist.cxx:33
char file_name[256]
Definition odbhist.cxx:41
DWORD end_run
Definition odbhist.cxx:39
int extract(char *mid_file, char *odb_file)
Definition odbhist.cxx:352
int odb_hist(char *file_name, int run_number, char *var_name, int quiet, double *value, int eoln, int boln, int print)
Definition odbhist.cxx:67
INT i
Definition odbhist.cxx:40
unsigned int DWORD
Definition odbhist.cxx:30
DWORD start_run
Definition odbhist.cxx:39
#define TRUE
Definition odbhist.cxx:27
#define SUCCESS
Definition odbhist.cxx:26
#define FALSE
Definition odbhist.cxx:28
INT eoln
Definition odbhist.cxx:40
char mid_name[256]
Definition odbhist.cxx:41
INT quiet
Definition odbhist.cxx:40
INT add
Definition odbhist.cxx:40
#define EVENTID_BOR
Definition odbhist.cxx:348
DWORD status
Definition odbhist.cxx:39
char var_name[256]
Definition odbhist.cxx:41
TH1X EXPRT * h1_book(const char *name, const char *title, int bins, double min, double max)
Definition rmidas.h:24
short int event_id
Definition midas.h:852
DWORD data_size
Definition midas.h:856