StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
cfgutil.cxx
1 // Utility functions for run configuration info
2 
3 #include <RC_Config.h>
4 #include <rtsSystems.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <ctype.h>
8 #include <rtsLog.h>
9 #include "cfgutil.h"
10 
11 // seems not thread safe,
12 // but this static stuff is fine
13 // because the same initialization is OK for all readers...
14 static int init=0;
15 static char evpgroups[32][40];
16 
17 int getRccnf(const char *fn, rccnf *desc)
18 {
19  char buff[80];
20 
21  //printf("Hello\n");
22 
23  memset(desc, 0, sizeof(rccnf));
24 
25  FILE *f = fopen(fn, "ro");
26  if(!f) return -1;
27 
28  if(fscanf(f,"run %d\n",&desc->run) != 1) {
29  fclose(f);
30  return -1;
31  }
32 
33  if(fscanf(f,"detmask 0x%x\n",&desc->detMask) != 1) {
34  printf("Blah\n");
35  fclose(f);
36  return -1;
37  }
38 
39  desc->detMask |= (1<<TRG_ID);
40  desc->detMask |= (1<<SC_ID);
41  desc->detMask &= ~(1<<DAQ_ID);
42 
43  // skip a bunch
44  int found=0;
45  while(fgets(buff,80,f)) {
46  if(strstr(buff, "evpgroups:") != buff) continue;
47  found = 1;
48  break;
49  }
50 
51  if(!found) {
52  fclose(f);
53  return -1;
54  }
55 
56  int in1,in2;
57 
58  while(fscanf(f,"%s %d %d\n",buff,&in1,&in2) == 3) {
59  desc->grpMask |= (1<<in1);
60  }
61 
62  fclose(f);
63  return 0;
64 }
65 
66 void initEvpGroups()
67 {
68  char str[80],str2[80];
69  memset(evpgroups, 0, sizeof(evpgroups));
70  int id;
71 
72  FILE *f = fopen("/RTS/conf/handler/evpGroups.txt", "ro");
73  if(!f) return;
74 
75  while(fgets(str, 80, f) == str) {
76  if(str[0] == '#') continue;
77 
78  sscanf(str, "%d %s\n",&id, str2);
79 
80  strcpy(evpgroups[id],str2);
81  }
82 
83  printf("Bye\n");
84 
85  fclose(f);
86  init = 1;
87 }
88 
89 
90 // single det
91 int s2did(char *str)
92 {
93  for(int i=0;i<32;i++) {
94  const char *t = rts2name(i);
95  if(t) {
96  if(strcasecmp(t,str) == 0) return i;
97  }
98  }
99 
100  return -1;
101 }
102 
103 // comma delimited
104 UINT32 str2detmask(const char *str)
105 {
106  char *_strtok_static;
107 
108  char s[256];
109  UINT32 mask = 0;
110  int id;
111 
112  strcpy(s,str);
113  char *t = strtok_r(s,",",&_strtok_static);
114 
115  while(t) {
116  id = s2did(t);
117  if(id >= 0) {
118  mask |= (1<<id);
119  }
120  else {
121  LOG(DBG, "Invalid detector: %s",t,0,0,0,0);
122  }
123 
124  t = strtok_r(NULL,",",&_strtok_static);
125  }
126 
127  return mask;
128 }
129 
130 // single group
131 int s2gid(const char *str)
132 {
133  for(int i=0;i<32;i++) {
134  if(strcasecmp(str,evpgroups[i]) == 0) return i;
135  }
136 
137  return -1;
138 }
139 
140 // comma delimited
141 UINT32 str2evpgroupmask(const char *str)
142 {
143  char *_strtok_static;
144  char s[256];
145  UINT32 mask = 0;
146  int id;
147 
148  if(init == 0) initEvpGroups();
149 
150  strcpy(s,str);
151  char *t = strtok_r(s,",",&_strtok_static);
152 
153  while(t) {
154  id = s2gid(t);
155 
156  if(id >= 0) {
157  mask |= (1<<id);
158  }
159  else {
160  LOG(ERR, "Invalid evpgroup: %s",t,0,0,0,0);
161  }
162 
163  t = strtok_r(NULL,",",&_strtok_static);
164  }
165 
166  return mask;
167 }
168 
169 #ifdef DEBUG_UTIL
170 
171 int main(int argc, char *argv[])
172 {
173  rtsLogLevel(DBG);
174  rtsLogOutput(RTS_LOG_STDERR);
175 
176  rccnf x;
177 
178  getRccnf("0.txt",&x);
179 
180  printf(":::%d : 0x%x : 0x%x\n",x.run,x.detMask,x.grpMask);
181 
182  LOG(ERR, "Hello",0,0,0,0,0);
183  char *str = "tpc,daq,trg,svt,horse";
184  char *str2 = "pedestal,b1,b3,b4";
185  printf("dets: %s 0x%x\n",str,str2detmask(str));
186  printf("evpgrps: %s 0x%x\n",str2,str2evpgroupmask(str2));
187 }
188 
189 #endif
Definition: cfgutil.h:4