MIDAS
Loading...
Searching...
No Matches
odbhist.cxx File Reference
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
Include dependency graph for odbhist.cxx:

Go to the source code of this file.

Classes

struct  EVENT_HEADER
 

Macros

#define SUCCESS   1
 
#define TRUE   1
 
#define FALSE   0
 
#define EVENTID_BOR   ((short int) 0x8000)
 
#define EVENTID_EOR   ((short int) 0x8001)
 
#define EVENTID_MESSAGE   ((short int) 0x8002)
 

Typedefs

typedef unsigned int DWORD
 
typedef int INT
 
typedef int BOOL
 
typedef char str[256]
 

Functions

BOOL equal_ustring (char *str1, char *str2)
 
int odb_hist (char *file_name, int run_number, char *var_name, int quiet, double *value, int eoln, int boln, int print)
 
int load_pars_from_file (char filename[256])
 
int extract (char *mid_file, char *odb_file)
 
int main (int argc, char *argv[])
 

Variables

str var_params [100]
 
DWORD status
 
DWORD start_run
 
DWORD end_run
 
DWORD run
 
INT i
 
INT quiet
 
INT add
 
INT j
 
INT k
 
INT eoln
 
INT boln
 
char file_name [256]
 
char var_name [256]
 
char mid_name [256]
 
double total [100]
 
double value [100]
 

Macro Definition Documentation

◆ EVENTID_BOR

#define EVENTID_BOR   ((short int) 0x8000)

Begin-of-run

Definition at line 348 of file odbhist.cxx.

◆ EVENTID_EOR

#define EVENTID_EOR   ((short int) 0x8001)

End-of-run

Definition at line 349 of file odbhist.cxx.

◆ EVENTID_MESSAGE

#define EVENTID_MESSAGE   ((short int) 0x8002)

Message events

Definition at line 350 of file odbhist.cxx.

◆ FALSE

#define FALSE   0

Definition at line 28 of file odbhist.cxx.

◆ SUCCESS

#define SUCCESS   1

Definition at line 26 of file odbhist.cxx.

◆ TRUE

#define TRUE   1

Definition at line 27 of file odbhist.cxx.

Typedef Documentation

◆ BOOL

Definition at line 32 of file odbhist.cxx.

◆ DWORD

Definition at line 30 of file odbhist.cxx.

◆ INT

Definition at line 31 of file odbhist.cxx.

◆ str

typedef char str[256]

Definition at line 33 of file odbhist.cxx.

Function Documentation

◆ equal_ustring()

BOOL equal_ustring ( char str1,
char str2 
)

Definition at line 46 of file odbhist.cxx.

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}
#define TRUE
Definition odbhist.cxx:27
#define FALSE
Definition odbhist.cxx:28
TH1X EXPRT * h1_book(const char *name, const char *title, int bins, double min, double max)
Definition rmidas.h:24
Here is the call graph for this function:
Here is the caller graph for this function:

◆ extract()

int extract ( char mid_file,
char odb_file 
)

Definition at line 352 of file odbhist.cxx.

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}
INT run_number[2]
Definition mana.cxx:246
DWORD n[4]
Definition mana.cxx:247
#define read(n, a, f)
#define write(n, a, f, d)
#define EVENTID_BOR
Definition odbhist.cxx:348
short int event_id
Definition midas.h:852
DWORD data_size
Definition midas.h:856
Here is the call graph for this function:
Here is the caller graph for this function:

◆ load_pars_from_file()

int load_pars_from_file ( char  filename[256])

Definition at line 212 of file odbhist.cxx.

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}
str var_params[100]
Definition odbhist.cxx:37
INT j
Definition odbhist.cxx:40
char file_name[256]
Definition odbhist.cxx:41
DWORD end_run
Definition odbhist.cxx:39
DWORD start_run
Definition odbhist.cxx:39
INT quiet
Definition odbhist.cxx:40
INT add
Definition odbhist.cxx:40
Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( int  argc,
char argv[] 
)

Definition at line 430 of file odbhist.cxx.

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()
double total[100]
Definition odbhist.cxx:42
int load_pars_from_file(char filename[256])
Definition odbhist.cxx:212
INT boln
Definition odbhist.cxx:40
DWORD run
Definition odbhist.cxx:39
double value[100]
Definition odbhist.cxx:42
INT k
Definition odbhist.cxx:40
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
#define SUCCESS
Definition odbhist.cxx:26
INT eoln
Definition odbhist.cxx:40
char mid_name[256]
Definition odbhist.cxx:41
DWORD status
Definition odbhist.cxx:39
char var_name[256]
Definition odbhist.cxx:41
Here is the call graph for this function:

◆ odb_hist()

int odb_hist ( char file_name,
int  run_number,
char var_name,
int  quiet,
double value,
int  eoln,
int  boln,
int  print 
)

Definition at line 67 of file odbhist.cxx.

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}
INT index
Definition mana.cxx:271
BOOL equal_ustring(char *str1, char *str2)
Definition odbhist.cxx:46
char str[256]
Definition odbhist.cxx:33
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ add

INT add

Definition at line 40 of file odbhist.cxx.

◆ boln

INT boln

Definition at line 40 of file odbhist.cxx.

◆ end_run

DWORD end_run

Definition at line 39 of file odbhist.cxx.

◆ eoln

INT eoln

Definition at line 40 of file odbhist.cxx.

◆ file_name

char file_name[256]

Definition at line 41 of file odbhist.cxx.

◆ i

INT i

Definition at line 40 of file odbhist.cxx.

◆ j

INT j

Definition at line 40 of file odbhist.cxx.

◆ k

INT k

Definition at line 40 of file odbhist.cxx.

◆ mid_name

char mid_name[256]

Definition at line 41 of file odbhist.cxx.

◆ quiet

INT quiet

Definition at line 40 of file odbhist.cxx.

◆ run

DWORD run

Definition at line 39 of file odbhist.cxx.

◆ start_run

DWORD start_run

Definition at line 39 of file odbhist.cxx.

◆ status

DWORD status

Definition at line 39 of file odbhist.cxx.

◆ total

double total[100]

Definition at line 42 of file odbhist.cxx.

◆ value

double value[100]

Definition at line 42 of file odbhist.cxx.

◆ var_name

char var_name[256]

Definition at line 41 of file odbhist.cxx.

◆ var_params

str var_params[100]

Definition at line 37 of file odbhist.cxx.