]> git.uio.no Git - u/mrichter/AliRoot.git/blame - RALICE/icepack/iceconvert/dumand.c
12-oct-2005 NvE CleanTasks() invoked before execution of sub-tasks in IceConvert...
[u/mrichter/AliRoot.git] / RALICE / icepack / iceconvert / dumand.c
CommitLineData
f67e2651 1/*
2 * Read/write files in the SiEGMuND native format
3 */
4
5char *dumand_rdmc_cvsid =
6"$Header: /net/local/cvsroot/siegmund/rdmc/dumand.c,v 1.29 2004/04/15 14:45:51 wiebusch Exp $";
7
8#define _USE_MATH_DEFINES
9#include <math.h>
10#include <string.h>
11#include <stdlib.h>
12#include <ctype.h>
13
14////////////////#include <unistd.h>
15
16
17#include <fcntl.h>
18#include <sys/stat.h>
19
20#if defined(OSF1) || defined(AIX) || defined (SunOS)
21#include <errno.h>
22#endif
23
24#if defined(OSF1) || defined (SunOS) || defined (IRIX)
25#include <alloca.h>
26#endif
27
28#include "rdmc.h"
29
30#ifdef DUMAND_ASCII_F
31
32#include "rdmc_local.h"
33
34#include "dumand.h"
35
36#if RDMC_MAXTOKEN_PER_LINE < 100
37#define DUMAND_USER_MAX RDMC_MAXTOKEN_PER_LINE
38#else
39#define DUMAND_USER_MAX 100
40#endif
41
42#define DUMAND_MAXUSER_PER_LINE 15
43
44
45#ifndef DEBUG
46#define DEBUG 0 /* DEBUG 1 writes some info to stderr */
47#endif
48
49#define ERRLINE (-__LINE__)
50
51#define PID180 (M_PI/180.)
52
53static int rdmc_a_M(const char *s, mcfile *fp); /* read the several */
54static int rdmc_a_G(const char *s, array *ar); /* from a dumand-like */
55static int rdmc_a_P(const char *s, array *ar); /* format */
56static int rdmc_a_K(const char *s, array *ar);
57static int rdmc_a_Q(const char *s, array *ar);
58static int rdmc_a_E(const char *s, mevt *ev);
59static int rdmc_a_H(const char *s, mhit *h);
60static int rdmc_a_T(const char *s, mtrack *t);
61static int rdmc_a_F(const char *s, mtrack *t, mevt_special_t *fit);
62static int rdmc_a_U(const char *s, mevt_special_t **user, int *nuser);
63static int rdmc_a_C(const char *s, char **comment);
64#if 0
65static int a_skip_C(mcfile *fp); /* skip comment lines */
66#endif
67static int parse_fit_def_ascii(array *ar, const char *s);
68/* parse the fit definitions */
69static int declare_rdmc_dumand_user(array *ar);
70
71
72/****************************************************************************/
73/* The function rarr_ascii() reads the header of a dumand like file */
74/****************************************************************************/
75
76int rdmc_rarr_ascii(mcfile *fp, array *ar)
77{
78 char s[RDMC_MAXLINE]; /* input line */
79 int c; /* first char of input line */
80 int ich = 0; /* loop variable */
81 int *iclust; /* pointer to current channel number in string */
82 int retval; /* returnvalue */
83
84 rdmc_init_array(ar); /* reset the array */
85 ar->nrun=fp->info.dum.nrun;
86 for (*s=c=getc(fp->fp); strchr("CGQ", *s) && (c != EOF); *s=c=getc(fp->fp)) {
87 if (fgets(s+1, RDMC_MAXLINE-1, fp->fp) == NULL)
88 return EOF;
89 fp->fpos++;
90
91 switch (*s) {
92 case 'C':
93 if ((retval = rdmc_a_C(s,&(ar->comment))))
94 return retval;
95 break;
96 case 'Q':
97 if ((retval = rdmc_a_Q(s, ar))) /* read trigger info */
98 return retval;
99 break;
100 case 'G':
101 if ((retval = rdmc_a_G(s, ar))) /* read array info */
102 return retval;
103 break;
104 } /* switch *s */
105 } /* while noe P nor V or E */
106
107 ungetc(c, fp->fp); /* ungetch the last char */
108
109 if (ar->nstr == 0) ar->nstr = 1; /* minimal one string */
110
111 iclust = calloc(ar->nstr,sizeof(int)); /* alloc mem for channel nr array */
112
113 for (*s = c = getc(fp->fp); (*s != 'E') && (c != EOF); *s=c=getc(fp->fp)) {
114 /* for all lines *not* beginning with 'E' */
115 ungetc(c, fp->fp);
116 do { /* read optional comment lines */
117 *s = c = getc(fp->fp);
118 if (c == EOF)
119 return EOF;
120 if (toupper((int) *s) != 'C')
121 break;
122
123 if (fgets(s+1, RDMC_MAXLINE-1, fp->fp) == NULL)
124 return EOF;
125 fp->fpos++;
126 } while (rdmc_a_C(s,&(ar->comment)) == 0);
127
128 if (*s == 'E') break;
129 ungetc(c, fp->fp);
130
131 fp->fpos += 1;
132 if (fgets(s,RDMC_MAXLINE,fp->fp) == NULL) /* try to read a line */
133 return EOF;
134
135 if ((retval = rdmc_a_P(s,ar)) != 0) /* read pmt position */
136 return retval;
137
138 if ((retval = rdmc_a_K(s,ar)) != 0) /* read pmt calib */
139 return retval;
140
141 if ((retval = rdmc_a_Q(s,ar)) != 0) /* read trigger pars */
142 return retval;
143
144#if 0 /* does not work */
145 if ((retval = rdmc_a_C(s,&(ar->comment))) != 0) /* read trigger pars */
146 return retval;
147#endif
148
149 } /* while !e */
150
151 if (c != EOF)
152 ungetc(*s, fp->fp);
153
154 for (ich = 0; ich < ar->nch; ich++) {
155
156 if ((ar->str[ich] < 0) && (ar->str[ich] <= ar->nstr)){
157 /* str nr is in range */
158
159 if (ar->clust[ich] == -1) /* if there was no valid channel number */
160 ar->clust[ich] = iclust[ar->str[ich] - 1]; /* generate one */
161
162 iclust[ar->str[ich] - 1] = ar->clust[ich] + 1; /* increase channel nr */
163 } /* if str <= nstr */
164
165 } /* for ich */
166
167 free (iclust); /* free the mem for the ch nr array */
168
169 /* this is a dirty patch */
170 /* at the time of reading a header in dumand format we do not know */
171 /* the number of fits ! */
172 /* The (only ?) possible solution is to parse the history lines */
173 parse_fit_def_ascii(ar,fp->creator); /* parse the fit definitions */
174
175 /* this durty patch just declares a default USER Tag rdmc when reading
176 dumand format */
177 declare_rdmc_dumand_user(ar);
178
179 /* now patch the fp->M creation time into ar->tbegin */
180 ar->tbegin=fp->info.dum.time;
181
182 return 0; /* successfull return */
183
184} /* function rarr_ascii() */
185
186/****************************************************************************/
187/* revt_ascii() reads the next dumand-like event */
188/****************************************************************************/
189
190int rdmc_revt_ascii(mcfile *fp, mevt *ev, const array *ar)
191{
192
193 char s[RDMC_MAXLINE]; /* input line */
194 int c; /* first char */
195 int ihit = 0, itrack = 0; /* hit/track counter */
196 int i;
197
198 rdmc_clear_mevt(ev); /* clear old event info */
199
200 if (feof(fp->fp)) /* if file end already reached */
201 return EOF;
202
203 ev->nrun = ar->nrun; /* copy the run number from fp */
204
205 do { /* read optional comments */
206 *s = c = getc(fp->fp);
207 if (c == EOF)
208 return EOF;
209 if (toupper((int) *s) != 'C') {
210 ungetc(c, fp->fp);
211 break;
212 }
213 if (fgets(s+1, RDMC_MAXLINE-1, fp->fp) == NULL)
214 return EOF;
215 fp->fpos++;
216 } while (rdmc_a_C(s,&(ev->comment)) == 0);
217
218 *s = c = getc(fp->fp); /* read a char */
219 if (c == EOF)
220 return EOF;
221 while (*s != 'E') { /* it should be an 'E' ("event") */
222 if (fgets(s+1, RDMC_MAXLINE-1, fp->fp) == NULL)
223 return EOF;
224 fp->fpos++;
225 *s = c = getc(fp->fp);
226 if (c == EOF)
227 return EOF;
228 } /* while != 'E' */
229
230 fp->fpos += 1;
231 if (fgets(s+1,RDMC_MAXLINE-1,fp->fp) == NULL) /* try to read first line */
232 return EOF;
233
234 if (rdmc_a_E(s, ev) != 0) /* scan for the event info */
235 return ERRLINE;
236
237 if (ev->ntrack > 0){
238 ev->gen = malloc(ev->ntrack*sizeof(mtrack));
239 /* allocate mem for generated tracks */
240 for (i=0 ; i < ev->ntrack ; i++)
241 rdmc_init_mtrack(ev->gen+i);
242 }
243
244 if (ev->nhits > 0){ /* allocate mem for the hits */
245 ev->h = malloc(ev->nhits * sizeof(mhit));
246 for (i=0 ; i < ev->nhits ; i++)
247 rdmc_init_mhit(ev->h + i);
248 }
249
250 for (*s = c = getc(fp->fp); (*s != 'E') && (c != EOF); *s=c=getc(fp->fp)) {
251 /* for all lines *not* beginning with 'E' */
252 fp->fpos += 1;
253 if (fgets(s+1,RDMC_MAXLINE-1,fp->fp) == NULL) { /* try to read a line */
254 c = EOF;
255 break;
256 }
257
258 switch (s[0]) { /* switch for the first char of the line */
259 case 'F': /* fit line */
260 {
261 mtrack fit;
262 mevt_special_t res;
263
264 rdmc_init_mtrack(&fit);
265 rdmc_init_fit_jk(&res,RDMC_NA);
266 if (rdmc_a_F(s, &fit, &res) != 0)
267 return ERRLINE;
268 fit.tag=ev->nfit+1; /* nfit is still 0..nfit -1 but we want 1..nfit*/
269 res.id=ev->nfit; /* reference to fit_def so 0..nfit-1 is OK */
270 if (rdmc_add_fit(ev,&fit,&res,ev->nfit))
271 return ERRLINE;
272 }
273 break;
274 case 'H': /* hit */
275 if (++ihit > ev->nhits) /* dont fill more hits than allocated */
276 return ERRLINE;
277 if (rdmc_a_H(s, &(ev->h[ihit-1])) != 0) /* scan this hit */
278 return ERRLINE;
279 ev->h[ihit-1].id=ihit;
280 break;
281 case 'T': /* generating track */
282 if (++itrack > ev->ntrack) /* dont fill more tracks than allocated */
283 return ERRLINE;
284 if (rdmc_a_T(s, &(ev->gen[itrack-1])) != 0) /* scan this track */
285 return ERRLINE;
286 break;
287 case 'U': /* user-def'd */
288 if (rdmc_a_U(s, &(ev->user), &(ev->nuser)) != 0)
289 /* look for rdmc-compatible user fields */
290 return ERRLINE;
291 break; /* is ignored */
292 case 'C':
293 rdmc_a_C(s, &(ev->comment));
294 break;
295 case 'V': /* header flags */
296 case 'M': /* are */
297 case 'G': /* NOT */
298 case 'P': /* allowed */
299 return ERRLINE;
300 default:
301 break;
302 } /* switch s[0] */
303
304 } /* for all lines not beginning with 'E' */
305
306 if (ev->nhits > ihit) ev->nhits = ihit; /* set the real number of hits */
307 if (ev->ntrack > itrack) ev->ntrack = itrack; /* set real nr of tracks */
308
309 if (c != EOF)
310 ungetc(*s, fp->fp);
311
312 ev->nch=rdmc_count_nch(ev); /* calc the number of hitted channels */
313 ev->nstr=rdmc_count_nstr(ev); /* calc the number of hitted channels */
314
315 return 0;
316
317} /* function revt_ascii() */
318
319/****************************************************************************/
320/* function warr_ascii() writes the array info to a dumand-like file */
321/* This function writes out the head of a DUMAND ascii file */
322/* opposite to reading the input file it writes not only */
323/* the Geometry banks ('G', 'P') , but also the ('V' and 'M' flags) */
324/* so the function whd_ascii does not exist */
325/****************************************************************************/
326
327int rdmc_warr_ascii(const mcfile *fp,const array *geo)
328{
329 int iom; /* om index in loop */
330 int itrigger; /* trigger index in loop */
331
332 fprintf(fp->fp, "V %i\n", /* write the V flag */
333 DUMAND_ASCII_VERSION); /* version number */
334
335 rdmc_wrcomment_ascii(fp,fp->creator);
336 rdmc_wrcomment_ascii(fp,fp->comment);
337 /* write M-line */
338 fprintf(fp->fp,"M %c %s %i %i %i %i %i %i\n"
339 ,fp->info.dum.mc_id
340 ,fp->info.dum.mc_vers
341 ,geo->nrun
342 ,geo->id
343 ,fp->info.dum.igen
344 ,fp->info.dum.igtrack
345 ,fp->info.dum.daswrun
346 ,rdmc_o_rdateconv(fp->info.dum.time) /* write the time in YYMMDD format */
347 );
348
349
350 for (itrigger = 0; itrigger < geo->n_trigger; itrigger++) {
351 fprintf(fp->fp, "Q U %s -1 -1 -1. -1.\n",
352 geo->def_trig[itrigger].tag);
353 }
354
355 if (geo->comment != NULL) /* if there is a common comment */
356 rdmc_wrcomment_ascii(fp, geo->comment);
357
358 if ((geo->nch>0)||(geo->id > 0)) { /* if there was a detector geometry */
359 fprintf(fp->fp,"G %i %i %i %.2f %.2f %.1f\n",
360 geo->id,geo->nch,geo->nstr,
361 geo->lattitude, geo->longitude, geo->depth); /* 'G' flag */
362 }
363/************** write now the positions ('P') of all channels ***************/
364
365 for (iom = 0 ; iom < geo->nch ; iom++) {
366
367 int ori; /* change orientation from cos theta to 1 for up and 2 down */
368
369 ori = 0;
370 if (geo->costh[iom] > 0.5) ori = 1; /* up */
371 if (geo->costh[iom] < -0.5) ori = 2; /* down */
372
373 fprintf(fp->fp,"P %i %i %i %i %li %li %li %i %i %f %f\n",
374 iom+1,geo->type[iom],
375 geo->serial[iom],
376 geo->str[iom],
377 rdmc_nint(geo->x[iom] * 1000.),
378 rdmc_nint(geo->y[iom] * 1000.),
379 rdmc_nint(geo->z[iom] * 1000.),
380 ori,
381 geo->clust[iom] + 1,
382 geo->thresh[iom],
383 geo->sensit[iom]);
384 } /* for iom */
385
386/**** write now the calibration ('K') of all channels, if there is one *******/
387
388 for (iom = 0 ; iom < geo->nch ; iom++) {
389 if ( (geo->is_calib.adc)
390 || (geo->is_calib.tdc) ){
391 fprintf(fp->fp,"K %i %.5f %.3f %.3f %.2f %.5f %.2f\n",
392 iom+1,
393 geo->cal[iom].beta_t,
394 geo->cal[iom].t_0,
395 geo->cal[iom].alpha_t,
396 geo->cal[iom].ped,
397 geo->cal[iom].beta_a,
398 geo->cal[iom].kappa);
399 }
400 } /* for iom */
401
402 return 0;
403
404} /* warr_ascii() */
405
406/****************************************************************************/
407/* function wevt_ascii() writes an event to a dumand-like file */
408/****************************************************************************/
409
410int rdmc_wevt_ascii(const mcfile *fp,const mevt *event, const array *ar)
411{
412 int itra,ifit,ihit; /* MC-track looop varialble */
413 int iuser; /* loop over user-def'd fields */
414
415 fprintf(fp->fp,"E %i %i %i %i %i %i %i %li %li %i %s\n", /* the 'E' flag */
416 event->enr,
417 event->nhits,
418 (int)rdmc_mjd_to_unixtime(event->mjd, event->secs),/* the time in secs */
419 event->nsecs, /* the nsec part of the time */
420 0,0,0, /* dummies for the Dumand time */
421 event->trigger,
422 rdmc_nint(event->t_offset),event->ntrack,"Q1");
423
424 for (itra=0 ; itra<event->ntrack ; itra++) { /* write out the 'T' flags */
425 fprintf(fp->fp,"T %i %li %li %li %g %g %g %li %.2f %i %li\n"
426 ,event->gen[itra].tag
427 ,rdmc_nint(event->gen[itra].x * 1000 )
428 ,rdmc_nint(event->gen[itra].y * 1000 )
429 ,rdmc_nint(event->gen[itra].z * 1000 )
430 ,event->gen[itra].px
431 ,event->gen[itra].py
432 ,event->gen[itra].pz
433 ,rdmc_nint( event->gen[itra].e )
434 ,event->gen[itra].t
435 ,event->gen[itra].id
436 ,rdmc_nint(event->gen[itra].length * 1000));
437 }
438
439 for (ihit=0 ; ihit<event->nhits ; ihit++) { /* write now the 'H' flags */
440 fprintf(fp->fp,"H %i %i %g %.2f %.2f %s %i"
441 ,event->h[ihit].str
442 ,event->h[ihit].ch+1
443 ,event->h[ihit].tot
444 ,event->h[ihit].amp
445 ,event->h[ihit].t
446 ,"U0"
447 ,event->h[ihit].mt);
448 if (event->h[ihit].ma > 0)
449 fprintf(fp->fp, " %i\n",event->h[ihit].ma);
450 else
451 fprintf(fp->fp, "\n");
452 } /* for ihit */
453
454 for (ifit=0 ; ifit<event->nfit ; ifit++) { /*write now the 'F' flags */
455
456 if ( (event->fresult != NULL) /* if there is an jk record */
457 && (0 <= event->fresult[ifit].id )
458 && (ar->n_fit > event->fresult[ifit].id ) /* there is a fit defined */
459 && (rdmc_is_this_jk(&(ar->def_fit[event->fresult[ifit].id])
460 ,&(event->fresult[ifit])) ) ) {
461
462 fprintf(fp->fp
463 ,"F %i %li %li %li %g %g %g %li %.2f %g %g %g %g %g %i %i %li\n"
464 ,(int) rdmc_nint(event->fresult[ifit].val[JK_FITID])
465 ,rdmc_nint(event->rec[ifit].x * 1000. )
466 ,rdmc_nint(event->rec[ifit].y * 1000. )
467 ,rdmc_nint(event->rec[ifit].z * 1000. )
468 ,event->rec[ifit].px
469 ,event->rec[ifit].py
470 ,event->rec[ifit].pz
471 ,rdmc_nint( event->rec[ifit].e )
472 ,event->rec[ifit].t
473 ,event->fresult[ifit].val[JK_RCHI2]
474 ,event->fresult[ifit].val[JK_PROB]
475 ,event->fresult[ifit].val[JK_SIGTH]
476 ,event->fresult[ifit].val[JK_COVMIN]
477 ,event->fresult[ifit].val[JK_COVMAX]
478 ,(int) rdmc_nint(event->fresult[ifit].val[JK_CUTFLAG])
479 ,event->rec[ifit].id
480 ,rdmc_nint(event->rec[ifit].length*1000.));
481 }else{
482 fprintf(fp->fp
483 ,"F %i %li %li %li %g %g %g %li %.2f %g %g %g %g %g %i %i %li\n"
484 ,RDMC_NA
485 ,rdmc_nint(event->rec[ifit].x * 1000. )
486 ,rdmc_nint(event->rec[ifit].y * 1000. )
487 ,rdmc_nint(event->rec[ifit].z * 1000. )
488 ,event->rec[ifit].px
489 ,event->rec[ifit].py
490 ,event->rec[ifit].pz
491 ,rdmc_nint( event->rec[ifit].e )
492 ,event->rec[ifit].t
493 ,(float) RDMC_NA
494 ,(float) RDMC_NA
495 ,(float) RDMC_NA
496 ,(float) RDMC_NA
497 ,(float) RDMC_NA
498 , RDMC_NA
499 ,event->rec[ifit].id
500 ,rdmc_nint(event->rec[ifit].length*1000.));
501 }
502 }
503
504
505 if (event->nuser > 0) { /* if there is a user field */
506 int ival;
507 for (iuser = 0; iuser < event->nuser; iuser++) { /* for all user fields */
508 fprintf(fp->fp, "U"); /* write the field id */
509 for(ival=0 ; ival < event->user[iuser].nval ; ival++){
510 fprintf(fp->fp," %g",event->user[iuser].val[ival]); /* write the field */
511 }
512 fprintf(fp->fp, "\n"); /* end the line */
513 } /* for iuser */
514 } /* if nuser > 0 */
515
516 if (event->comment != NULL) /* if there is an event comment */
517 rdmc_wrcomment_ascii(fp, event->comment);
518
519 return 0;
520
521} /* function wevt_ascii() */
522
523/****************************************************************************/
524/* wrcomment_ascii() writes a comment line to an ASCII file */
525/****************************************************************************/
526
527int rdmc_wrcomment_ascii(const mcfile *fp, const char *s)
528{
529 static char *line=NULL; /* temporary copy of the string */
530 char *eol; /* end of current line */
531 char *bol; /* beginning of the line */
532
533
534 if(s){
535#ifndef CRAY
536 line = alloca((strlen(s)+1)*sizeof(char));
537#else
538 line = malloc((strlen(s)+1)*sizeof(char));
539#endif
540 strcpy(line,s); /* copy the string */
541 for (bol = eol = line; eol; bol = eol+1) { /* for each line */
542 eol = strchr(bol,'\n'); /* search line end */
543 if (eol) *eol = '\0'; /* replace it with '\0' */
544 if (*bol != '\0') /* if it contains something */
545 fprintf(fp->fp, "C %s\n", bol+1); /* print the line */
546 }
547 }
548#ifdef CRAY
549 free(line); /* Jacobsen added semicolon here */
550#endif
551
552 return 0;
553
554} /* wrcomment_ascii() */
555
556/****************************************************************************/
557/* rhd_ascii() reads the format relevant informations for dumand-like */
558/* formats */
559/****************************************************************************/
560
561int rdmc_rhd_ascii(mcfile *fp)
562{
563 char s[RDMC_MAXLINE]; /* input line */
564 int c; /* first char */
565
566 do { /* read comment lines */
567 *s = c = getc(fp->fp);
568 if (c == EOF)
569 return EOF;
570 if (toupper((int) *s) != 'C') {
571 ungetc(c, fp->fp);
572 break;
573 }
574 if (fgets(s+1, RDMC_MAXLINE-1, fp->fp) == NULL)
575 return EOF;
576 fp->fpos++;
577 } while (rdmc_a_C(s,&(fp->creator)) == 0);
578
579 *s = c = getc(fp->fp);
580 if (c == EOF)
581 return EOF;
582 if (*s != 'M') { /* if there is *not* a 'M' line */
583 ungetc(*s, fp->fp); /* ungetch the last char */
584 return 0;
585 } /* if *s != M */
586
587
588 if (fgets(s+1,RDMC_MAXLINE-1,fp->fp) == NULL) /* try to read next line */
589 return EOF;
590 fp->fpos += 1;
591
592 if (rdmc_a_M(s,fp) != 0) /* monte carlo infos */
593 return ERRLINE;
594
595 return 0;
596
597} /* function rhd_ascii() */
598
599/****************************************************************************/
600/* skipevt_ascii() skips the next event from a dumand-like file */
601/****************************************************************************/
602
603int rdmc_skipevt_ascii(mcfile *fp)
604{
605 char s[RDMC_MAXLINE]; /* input line */
606 int c; /* first char */
607
608 *s = c = getc(fp->fp); /* read a char */
609 if (c == EOF)
610 return EOF;
611
612 fp->fpos += 1;
613 if (fgets(s+1,RDMC_MAXLINE-1,fp->fp) == NULL) /* try to read first line */
614 return EOF;
615
616 for (*s =c = getc(fp->fp); (*s != 'E') && (c != EOF); *s = c = getc(fp->fp)){
617 /* for all lines *not* beginning with 'E' */
618 fp->fpos += 1;
619 if (fgets(s+1,RDMC_MAXLINE-1,fp->fp) == NULL) /* read the line */
620 return EOF;
621 }
622
623 if (c != EOF)
624 ungetc(*s, fp->fp);
625
626#if 0
627 fp->enr++; /* increase event number */
628#endif
629
630 return 0;
631
632} /* function skipevt_ascii() */
633
634
635/****************************************************************************/
636/* functions for reading the dumand-like files */
637/****************************************************************************/
638
639int rdmc_a_V(const char *s, mcfile *fp) /* scans the V card */
640{
641
642 int form = 0;
643
644 if(sscanf(s,"V %i ",&form) != 1)
645 return ERRLINE;
646 fp->fmajor = form;
647 fp->fminor = 0;
648
649 return 0;
650
651} /* function rdmc_a_V() */
652
653/****************************************************************************/
654
655static int rdmc_a_C(const char *s, char **comment) /* read comment line */
656{
657
658 while (*s == ' ')
659 s++;
660
661 if (toupper(*s != 'C'))
662 return 1;
663
664 s += 2;
665
666 /*now append it */
667 rdmc_concat_comment(comment,s,DUMAND_ASCII_F);
668
669 return 0;
670
671} /* function rdmc_a_C() */
672
673/****************************************************************************/
674
675static int rdmc_a_G(const char *s, array *ar) /* scans the G line */
676{
677
678 int form;
679
680 form = sscanf(s,"G %i %i %i %f %f %f",
681 &(ar->id), &(ar->nch), &(ar->nstr),
682 &(ar->lattitude), &(ar->longitude), &(ar->depth));
683
684 switch(form) {
685 case 0: ar->id = 0;
686 case 1: ar->nch = 0;
687 case 2: ar->nstr = 0;
688 case 3: ar->lattitude = 0.0;
689 case 4: ar->longitude = 0.0;
690 case 5: ar->depth = 0.0;
691 default: break;
692 } /* switch form */
693
694 return 0;
695
696} /* function rdmc_a_G() */
697
698/****************************************************************************/
699
700static int rdmc_a_M(const char *s, mcfile *fp)
701{
702 int form;
703 char id;
704 int irun,igeo,igen,igtrack,daswrun,date;
705 char mc_version[RDMC_MAXLINE];
706
707 if (s[0] != 'M' )
708 return -1;
709
710 if( (form = sscanf(s,"M %c %s %i %i %i %i %i %i ",
711 &id,mc_version,&irun,&igeo,&igen,&igtrack,&daswrun,&date) ) != 8) {
712 if (form != 7 ){
713 return ERRLINE;
714 }
715 else {
716 date= 700101;
717 }
718 }
719 switch (id) {
720 case 'D': /*allowed id's */
721 case 'U':
722 case 'G':
723 case 'M':
724 fp->info.dum.mc_id = id;
725 break;
726 case 'B': /* old data */
727 fp->info.dum.mc_id = 'D';
728 break;
729 case 'J': /* old MC */
730 case 'S':
731 fp->info.dum.mc_id = 'M';
732 break;
733 default: /* unknown */
734 fp->info.dum.mc_id = 'U';
735 break;
736 }
737 strcpy(fp->info.dum.mc_vers,mc_version);
738 fp->info.dum.nrun = irun;
739 fp->info.dum.igen =igen;
740 fp->info.dum.igtrack = igtrack ;
741 fp->info.dum.time = rdmc_o_dateconv(date);
742 fp->info.dum.daswrun = daswrun;
743 switch (fp->info.dum.mc_id) {
744 case 'G':
745 fp->info.dum.igeo =0;
746 fp->info.dum.igtrack =-1;
747 break;
748 case 'D': /* data */
749 fp->info.dum.igtrack =-1;
750 fp->info.dum.igen =0;
751 fp->info.dum.daswrun =-1;
752 break;
753 case 'M':
754 fp->info.dum.igtrack =-1;
755 break;
756 }
757 return 0;
758
759} /* function rdmc_a_M() */
760
761/****************************************************************************/
762
763static int rdmc_a_P(const char *s, array *ar)
764{
765
766 int form;
767 long ch, type, serial, str, iclust;
768 float x,y,z;
769 const float cs_ori[3] = {0.0, 1.0, -1.0};
770 float thresh, rsense, ori;
771
772 form = sscanf(s,"P %li %li %li %li %f %f %f %f %li %f %f"
773 ,&ch
774 ,&type
775 ,&serial
776 ,&str
777 ,&x
778 ,&y
779 ,&z
780 ,&ori
781 ,&iclust
782 ,&thresh
783 ,&rsense);
784 if (form == 0) return 0; /* not a "P" line, ignore */
785 if ((form <8) || (form >11)) return ERRLINE;
786 ch--; /* I want a 'C' style of indizes */
787 if ((ch < 0) ||(ch >= RDMC_MAXCHANNELS) || (ch >= ar->nch))
788 return ERRLINE;
789
790 ar->str[ch] = str;
791 ar->type[ch] = type;
792 ar->x[ch] = x/1000.0;
793 ar->y[ch] = y/1000.0;
794 ar->z[ch] = z/1000.0;
795 if ((ori < 0) || (ori > 2))
796 return ERRLINE;
797
798 if (form > 8) /* if there was an icluster info */
799 ar->clust[ch] = iclust - 1; /* fill it as channel number */
800 else /* if no icluster info */
801 ar->clust[ch] = -1;
802
803 ar->costh[ch] = cs_ori[(int)ori];
804 ar->serial[ch] = serial;
805 if (form > 9)
806 ar->thresh[ch] = thresh;
807 else
808 ar->thresh[ch] = RDMC_SMALL;
809 if (form > 10)
810 ar->sensit[ch] = rsense;
811 else
812 ar->sensit[ch] = 1.0;
813
814 /* now set the geo cal flag */
815 ar->is_calib.geo=1;
816 return 0;
817
818} /* function rdmc_a_P() */
819
820/****************************************************************************/
821
822static int rdmc_a_K(const char *s, array *ar)
823{
824
825 int form;
826 array_calib_t cal;
827 int ich;
828
829
830 form = sscanf(s,"K %i %f %f %f %f %f %f",
831 &ich,
832 &(cal.beta_t), &(cal.t_0), &(cal.alpha_t),
833 &(cal.ped), &(cal.beta_a), &(cal.kappa));
834 switch (form) {
835 case 1: cal.beta_t = 0.0;
836 case 2: cal.t_0 = 0.0;
837 case 3: cal.alpha_t = 0.0;
838 case 4: cal.ped = 0.0;
839 case 5: cal.beta_a = 0.0;
840 case 6: cal.kappa = 0.0;
841 case 7:
842 cal.flag = RDMC_CALIB_TDC | RDMC_CALIB_ADC ;
843 break;
844 default:
845 return 0;
846 }
847
848 if ((ich > 0) && (ich <= RDMC_MAXCHANNELS))
849 ar->cal[ich-1] = cal;
850
851 /* now set the cal flag */
852 ar->is_calib.tdc=1;
853 ar->is_calib.adc=1;
854
855 return 0;
856
857} /* function rdmc_a_K() */
858
859/****************************************************************************/
860
861static int rdmc_a_Q(const char *s, array *ar)
862{
863 int form;
864 array_hdef_t trig;
865 char id;
866
867 rdmc_init_array_hdef(&(trig));
868
869 form = sscanf(s,"Q %c %s %s %s %s %s",
870 &(id), trig.tag
871 , trig.pars[0], trig.pars[1]
872 , trig.pars[2], trig.pars[3] );
873 switch (form) {
874 case 3:
875 case 4:
876 case 5:
877 case 6:
878 case 2:
879 break;
880 default:
881 return 0;
882 }
883
884 trig.id = ar->n_trigger+1;
885 trig.nwords = 0;
886 trig.npars = form -2 ;
887
888 if (ar->n_trigger+1 < RDMC_MAXTRIGID){
889 rdmc_add_trigger_def(ar,&trig,ar->n_trigger);
890 }
891
892 return 0;
893
894} /* function rdmc_a_Q() */
895
896/****************************************************************************/
897
898static int rdmc_a_E(const char *s, mevt *ev)
899{
900
901 char trig[RDMC_MAXLINE];
902 int form;
903 unsigned long trigger;
904 int sec;
905 unsigned nsec;
906 unsigned DUMtLSW; /* dummies */
907 int DUMtMSW, rperiodtime; /* dummies */
908
909 form = sscanf(s,"E %i %i %i %u %i %u %i %li %g %i %s ",
910 &(ev->enr),&(ev->nhits),
911 &sec, &nsec,
912 &DUMtMSW, &DUMtLSW,
913 &rperiodtime,
914 &(trigger), &(ev->t_offset), &(ev->ntrack),
915 trig);
916 switch( form) {
917 case 2: sec = 0;
918 case 3: nsec = 0;
919 case 4: DUMtMSW = 0;
920 case 5: DUMtLSW = 0;
921 case 6: rperiodtime = 0;
922 case 7: trigger = 0;
923 case 8: ev->t_offset=0.;
924 case 9: ev->ntrack = 0;
925 case 10: strcpy(trig,"0");
926 case 11:
927 break;
928 default:
929 return ERRLINE;
930 break;
931 }
932
933 if (sec == -1) sec = 0; /* -1 means: no time was stored */
934 if (DUMtMSW == -1) {DUMtMSW = 0; DUMtLSW = 0; }
935 if (rperiodtime == -1) rperiodtime = 0;
936
937 ev->mjd = rdmc_unixtime_to_mjd(sec); /* convert unix time into mjd */
938 ev->secs = rdmc_unixtime_to_mjdsecs(sec); /* and seconds after mjd */
939 ev->nsecs = nsec;
940
941 /* now add the trigger */
942 {
943 mevt_special_t ptrig;
944 int itrig;
945 rdmc_init_mevt_special(&ptrig,0);
946 for (itrig = 0 ; itrig <RDMC_MAXTRIGID ; itrig++){
947 if ( (trigger) & (1 << itrig)){
948 ptrig.id = itrig;
949 rdmc_add_trigger(ev, &ptrig, ev->ntrig, itrig);
950 }
951 }
952 }
953
954
955 return 0;
956
957} /* function rdmc_a_E() */
958
959/****************************************************************************/
960
961static int rdmc_a_H(const char *s, mhit *h)
962{
963
964 int form;
965 char htrig[RDMC_MAXLINE];
966
967 form = sscanf(s,"H %i %i %g %g %g %s %i %i",
968 &(h->str),&(h->ch),
969 &(h->tot),
970 &(h->amp),
971 &(h->t),
972 htrig,
973 &(h->mt),
974 &(h->ma));
975 switch (form) {
976 case 5:
977 strcpy(htrig, "U0");
978 case 6:
979 h->mt = RDMC_NA;
980 case 7:
981 h->ma = RDMC_NA;
982 case 8:
983 break;
984 default:
985 return ERRLINE;
986 }
987 h->ch--; /* for 'c' style of channel number (0,...) */
988#if 0 /* this is stupid */
989 h->mt = abs(h->mt); /* it's possible to have negative track id's */
990 h->ma = abs(h->ma); /* it's possible to have negative track id's */
991#endif
992 return 0;
993
994} /* function rdmc_a_H() */
995
996/****************************************************************************/
997
998static int rdmc_a_T(const char *s, mtrack *t)
999{
1000
1001 int form;
1002
1003 rdmc_init_mtrack(t);
1004 form = sscanf(s,"T %i %f %f %f %f %f %f %f %f %i %f "
1005 ,&(t->tag)
1006 ,&(t->x),&(t->y),&(t->z)
1007 ,&(t->px),&(t->py),&(t->pz)
1008 ,&(t->e),&(t->t)
1009 ,&(t->id),&(t->length));
1010 switch (form)
1011 {
1012 case 10:
1013 t->length = RDMC_NA;
1014 case 11:
1015 t->length *= 1e-3; /* dumand format: millimeter, in rdmc: meter */
1016 if (t->length < 0.)
1017 t->length = RDMC_NA; /*10. * t->e/100000. ; */
1018 t->x *= 1.e-3;
1019 t->y *= 1.e-3;
1020 t->z *= 1.e-3;
1021 rdmc_tauinv_tr(t); /* calc cos theta and phi */
1022 break;
1023 default:
1024 return ERRLINE;
1025 }
1026
1027 return 0;
1028
1029} /* function rdmc_a_T() */
1030
1031/****************************************************************************/
1032
1033static int rdmc_a_F(const char *s, mtrack *t, mevt_special_t *krabi)
1034{
1035
1036 int form;
1037
1038 form = sscanf(s,"F %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %i %g"
1039 ,&(krabi->val[JK_FITID])
1040 ,&(t->x),&(t->y),&(t->z)
1041 ,&(t->px),&(t->py),&(t->pz)
1042 ,&(t->e)
1043 ,&(t->t)
1044 ,&(krabi->val[JK_RCHI2])
1045 ,&(krabi->val[JK_PROB])
1046 ,&(krabi->val[JK_SIGTH])
1047 ,&(krabi->val[JK_COVMIN])
1048 ,&(krabi->val[JK_COVMAX])
1049 ,&(krabi->val[JK_CUTFLAG])
1050 ,&(t->id)
1051 ,&(t->length) );
1052
1053
1054 krabi->val[JK_CHI2]=RDMC_NA;
1055 switch (form) /* minimum 10 maximal 17 */
1056 {
1057 case 10:
1058 krabi->val[JK_PROB] = RDMC_NA;
1059 case 11:
1060 krabi->val[JK_SIGTH] = RDMC_NA ;
1061 case 12:
1062 krabi->val[JK_COVMIN] = RDMC_NA;
1063 case 13:
1064 krabi->val[JK_COVMAX] = RDMC_NA ;
1065 case 14:
1066 krabi->val[JK_CUTFLAG] = RDMC_NA ;
1067 case 15:
1068 t->id = MUON_MINUS ;
1069 case 16:
1070 t->length = RDMC_NA;
1071 case 17:
1072
1073 t->length *= 1e-3; /* dumand format: millimeter, in rdmc: meter */
1074 if (t->length < 0.)
1075 t->length = RDMC_NA; /*10. * t->e/100000. ; */
1076 t->x *= 1.e-3;
1077 t->y *= 1.e-3;
1078 t->z *= 1.e-3;
1079 rdmc_tauinv_tr(t); /* calc cos theta and phi */
1080 break;
1081 default:
1082 return ERRLINE;
1083 }
1084
1085 return 0;
1086
1087} /* function rdmc_a_F() */
1088
1089/****************************************************************************/
1090/* rdmc_a_U() reads optional user fields from a comment line */
1091/* it reallocates the the user field */
1092/****************************************************************************/
1093
1094static int rdmc_a_U(const char *s, mevt_special_t **user, int *nuser)
1095{
1096 char *t=NULL;
1097 char *tp;
1098 mevt_special_t temp_user;
1099 int iuser,imod;
1100 int i;
1101 int r=0;
1102
1103 if (s[0] != 'U')
1104 return 0;
1105
1106#ifndef CRAY
1107 t = alloca(strlen(s)+1);
1108#else
1109 t = malloc(strlen(s)+1);
1110#endif
1111 strcpy(t,s);
1112
1113 rdmc_init_mevt_special(&temp_user, RDMC_MAXUSER_PER_LINE);
1114
1115 /* get number of previous these users */
1116 iuser = 0;
1117 for (i = 0; i < *nuser; i++){
1118 iuser += (*user)[i].nval;
1119 }
1120 /* iuser is now the index for the new user or the number of previous ones */
1121
1122 /* now get the last user into temp_user */
1123 if (iuser > 0){
1124 rdmc_cp_mevt_special(&temp_user, &((*user)[*nuser-1]));
1125 r |= rdmc_del_mevt_special(user, nuser, (*nuser)-1);
1126 imod = temp_user.nval;
1127 } else {
1128 imod = 0;
1129 temp_user.id = iuser % DUMAND_USER_MAX;
1130 }
1131
1132 /* now parse the line and add users */
1133 for (tp = strtok(t," \t\n"); tp && (iuser < DUMAND_USER_MAX);
1134 tp = strtok(NULL," \t\n")) {
1135 iuser++;
1136 /* check if current user is filled */
1137 if (imod >= DUMAND_MAXUSER_PER_LINE) {
1138 r |= rdmc_add_mevt_special(user,nuser,&temp_user,*nuser);
1139 rdmc_init_mevt_special(&temp_user,RDMC_MAXUSER_PER_LINE);
1140 temp_user.id = iuser%DUMAND_USER_MAX;
1141 imod = 0;
1142 }
1143 /* now fill the values */
1144 temp_user.val[imod] = atof(tp);
1145 temp_user.nval = imod++;
1146 }
1147 /* now commit a last uncommitted block */
1148 if (iuser > 0)
1149 r |= rdmc_add_mevt_special(user,nuser,&temp_user,*nuser);
1150
1151#ifdef CRAY
1152 free(t);
1153#endif
1154 return r; /* there should not occure an error */
1155
1156} /* function rdmc_a_U() */
1157
1158#if 0
1159/****************************************************************************/
1160/* a_skip_C() skips all comment lines and points to the next line in ASCII */
1161/****************************************************************************/
1162static int a_skip_C(mcfile *fp)
1163{
1164
1165 int c;
1166 char s[RDMC_MAXLINE];
1167
1168 while ((c = getc(fp->fp)) == 'C'){
1169 ungetc(c, fp->fp);
1170 fp->fpos += 1;
1171 fgets(s, RDMC_MAXLINE-1, fp->fp);
1172 }
1173
1174 if (c == EOF)
1175 return EOF;
1176 else {
1177 ungetc(c, fp->fp);
1178 return 0;
1179 }
1180
1181} /* function a_skip_C() */
1182#endif
1183
1184/****************************************************************************/
1185/* declare_rdmc_user() declares a default user tag */
1186/****************************************************************************/
1187
1188static int declare_rdmc_dumand_user(array *ar){
1189 array_hdef_t dumand_user_def;
1190 int i;
1191 int r=0;
1192 int iblock=0;
1193
1194 rdmc_init_array_hdef(&dumand_user_def);
1195
1196 for (i=0 ; i < DUMAND_USER_MAX ;i++){
1197 dumand_user_def.nwords++;
1198 sprintf(dumand_user_def.words[i%DUMAND_MAXUSER_PER_LINE],"u%i",i+1);
1199 if (((i+1)%DUMAND_MAXUSER_PER_LINE)==0){
1200 iblock++;
1201 dumand_user_def.id=iblock;
1202 sprintf(dumand_user_def.tag,"dumand%i",iblock);
1203 r |= rdmc_add_user_def(ar, &dumand_user_def, ar->n_user);
1204 rdmc_init_array_hdef(&dumand_user_def);
1205 }
1206 }
1207 /* clean up the rest */
1208 if (((i+1)%DUMAND_MAXUSER_PER_LINE) !=0 ){
1209 iblock++;
1210 dumand_user_def.id=iblock;
1211 sprintf(dumand_user_def.tag,"dumand%i",iblock);
1212 r |= rdmc_add_user_def(ar, &dumand_user_def, ar->n_user);
1213 }
1214
1215 return r;
1216}
1217
1218/****************************************************************************/
1219/* parse_fit_def_ascii() parses the creator line for "recoos" */
1220/****************************************************************************/
1221
1222static int parse_fit_def_ascii(array *ar, const char *s)
1223{
1224 char *line=NULL; /* temporary copy of the string */
1225 char *reco; /* reco pointer */
1226 char *eol; /* end of current line */
1227 char *bol; /* beginning of the line */
1228 array_hdef_t dum_fitdef;
1229
1230 if(s){
1231#ifndef CRAY
1232 line = alloca(sizeof(char)*(1+strlen(s)));
1233#else
1234 line = malloc(sizeof(char)*(1+strlen(s)));
1235#endif
1236 strcpy(line,s); /* copy the string */
1237
1238 for (bol = eol = line; eol; bol = eol+1) { /* for each line */
1239 eol = strchr(bol,'\n'); /* search line end */
1240 if (eol) *eol = '\0'; /* replace it with '\0' */
1241 if (*bol != '\0'){ /* if it contains something */
1242 if( (reco=strstr(bol,"recoos (")) != NULL ){
1243 /* there is a recoos in this history line */
1244 rdmc_jk_to_fitdef(&(dum_fitdef),ar->n_fit+1);
1245 rdmc_add_fit_def(ar,&(dum_fitdef), ar->n_fit);
1246 }
1247 }
1248 }
1249#ifdef CRAY
1250 free(line);
1251#endif
1252 }
1253 return 0;
1254
1255} /* parse_fit_def_ascii() */
1256
1257
1258
1259#endif /* DUMAND_ASCII_F */
1260/****************************************************************************/
1261/********************************** E O F ***********************************/
1262/****************************************************************************/
1263/*
1264 This is just for EMACS:
1265 Local Variables:
1266 compile-command: "cd .. ; make -k rdmc"
1267 End:
1268*/