]> git.uio.no Git - u/mrichter/AliRoot.git/blob - RALICE/icepack/iceconvert/rdmc_mhit.c
08-mar-2006 NvE Time offset correction in IceF2k extended to allow also a user define...
[u/mrichter/AliRoot.git] / RALICE / icepack / iceconvert / rdmc_mhit.c
1
2 /******** implements functions for the mhit structure *********/
3
4 #include <string.h>
5 #include <stdlib.h>
6
7 #include "rdmc.h"
8
9
10 void rdmc_init_mhit(mhit *h){
11   h->ch = h->str = h->mt = h->ma = h->id = RDMC_NA ;
12   h->t= -RDMC_BIG;
13   h->amp = h->tot = RDMC_NA ;
14   h->weight = 1.;
15   rdmc_init_mhit_stat(&(h->hstat));
16   h->nuser=0;
17   h->user=NULL;
18   h->comment=NULL;
19   h->tmp=NULL;
20 }
21
22 void rdmc_init_mhit_stat(mhit_stat_t *s){
23   s->n_tdc_edges=0;
24   s->tdc_flag=0;
25 }
26
27 void rdmc_clear_mhit(mhit *h){
28   rdmc_free_mhit(h);
29   rdmc_init_mhit(h);
30 }
31
32 void rdmc_free_mhit(mhit *h){
33   int i;
34   if (h->user !=NULL){
35     for(i=0 ; i < h->nuser ; i++){
36       rdmc_free_mevt_special(&(h->user[i]));
37     }
38     free(h->user);
39     h->nuser=0;
40     h->user=NULL;
41   }
42   if (h->tmp !=NULL){
43     free(h->tmp);
44     h->tmp=NULL;
45   }
46   if (h->comment !=NULL){
47     free(h->comment);
48     h->comment=NULL;
49   }
50 }
51
52
53 /****************************************************************************
54  * merge two hits.  the amplitude of the merged hit is added only if ch=-1
55  * else the amplitude of the channel ch is used.
56  * If there is no such channel, a zero amplitude is assumed
57  ****************************************************************************/
58
59 void rdmc_merge_hits(mhit *in_1, mhit *in_2, int ch)
60 {
61
62   float ampa,tfirst,tota;
63   int idt,ida;
64
65   if (in_1 == in_2) return; /* the hits are the SAME! */
66
67   if ((ch == -1) || ((ch == in_1->ch) && (ch == in_2->ch) )){ /* use both */
68     if ((in_1->amp >= 0.) && (in_2->amp >= 0.))
69       ampa=  in_1->amp + in_2->amp;  /* sum of amplitudes*/
70     else if (in_1->amp >= 0.)
71       ampa = in_1->amp;
72     else if  (in_2->amp >= 0.)
73       ampa = in_2->amp;
74     else
75       ampa = -1.;
76     if (ampa > 0)
77       ida = (in_1->amp >= in_2->amp) ? (in_1->ma) : (in_2->ma) ; 
78     else
79       ida = -1;  /* amp muon id*/
80   } else if ((ch == in_1->ch) && (in_1->amp >= 0.)) { /* only first */
81     ampa = in_1->amp;
82     ida = (ampa > 0)? in_1->ma : -1;
83   } else if ((ch == in_2->ch) && (in_2->amp >= 0.)) { /* only second */
84     ampa = in_2->amp;
85     ida = (ampa > 0)? in_2->ma : -1;
86     in_1->ch = ch; /* set correct (for amp) channel number */
87   }  else {
88     ampa = -1.0;
89     ida = -1;
90   }
91
92   tfirst = (in_1->t <= in_2->t ) ? (in_1->t) : (in_2->t) ; /* keep first time*/
93   idt = (in_1->t <= in_2->t ) ? (in_1->mt) : (in_2->mt) ; /* time muon id*/
94
95   in_1->amp = ampa;  /* new amplitude */
96   in_2->amp = 0.;
97
98   if ((in_1->tot >= 0.)&&(in_2->tot >= 0.))
99     tota=  in_1->tot + in_2->tot;  /* sum of totlitudes*/
100   else
101     if (in_1->tot >= 0.)
102       tota = in_1->tot;
103     else
104       if  (in_2->tot >= 0.)
105         tota = in_2->tot;
106       else
107         tota =-1.;
108   if (ida <0 )
109     if (tota > 0 )
110       ida = (in_1->tot >= in_2->tot ) ? (in_1->ma) : (in_2->ma) ; 
111   /* amp muon id*/
112
113   in_1->tot = tota;  /* new amplitude */
114   in_2->tot = 0.;
115
116   in_1->t = tfirst;
117   in_2->t = 0.;
118   in_1->mt = idt;
119   in_2->mt = 0;
120   in_1->ma = ida;
121   in_2->ma = 0;
122
123   in_1->hstat.n_tdc_edges = 
124     (in_1->hstat.n_tdc_edges >=  in_2->hstat.n_tdc_edges )
125     ? in_1->hstat.n_tdc_edges
126     : in_2->hstat.n_tdc_edges;
127
128   in_1->hstat.tdc_flag = 
129     (in_1->hstat.tdc_flag >=  in_2->hstat.tdc_flag )
130     ? in_1->hstat.tdc_flag
131     : in_2->hstat.tdc_flag;
132
133   in_1->weight = in_1->weight * in_2->weight ;
134
135   return;
136
137 } /* rdmc_merge_hits() */
138
139 /****************************************************************************
140  * copies the hit *in to *out by overwriting.
141  * If in == out, nothing is done.
142  ****************************************************************************/
143 void rdmc_cp_mhit(mhit *out, mhit *in)
144 {
145   if ((in == NULL) || (out == NULL) || (in == out))
146     return;
147   
148   memcpy(out, in, sizeof(mhit));
149   if (out->nuser){
150     int i;
151     mevt_special_t *from,*to;
152     out->user = (mevt_special_t *) malloc(in->nuser*sizeof(mevt_special_t));
153     if (!(out->user)){
154       out->nuser = 0;
155     } else {
156       from = in->user;
157       to = out->user;
158       for (i = 0 ; i < in->nuser ; i++){
159         rdmc_cp_mevt_special(to,from);
160         to++ ; from++;
161       }
162     }
163   }
164
165   if (in->comment) {
166     out->comment = (char *) malloc(strlen(in->comment)+1);
167     if (!out->comment)
168       strcpy(out->comment, in->comment);
169   }
170
171   if (out->tmp){ /* if there is a new stack delete it */
172     out->tmp=NULL;
173   }
174
175
176 } /* rdmc_cp_mhit() */