ResponseManager.cc
1 #include "ResponseManager.h"
2 #include "SystemResponse.h"
3 #include <stdio.h>
4 #include <fstream>
5 #include "AnalysisWaveform.h"
6 #include <sys/types.h>
7 #include <dirent.h>
8 #include <assert.h>
9 #include "FFTtools.h"
10 #include "TGraph.h"
11 #include "AnitaGeomTool.h"
12 
13 
19 int AnitaResponse::ResponseManager::loadResponsesFromDir(const char * raw_dir, int npad, unsigned int evTime)
20 {
21  DIR *dp;
22  struct dirent *ep;
23 
24  //where do we look for the dir?
25  // Then try data/responses/
26  // Then try ${ANITA_UTIL_INSTALL_DIR}/share/UCorrelator/responses
27 
28  TString dir;
29  TString indexF;
30  std::string str;
31  std::string tempStr;
32  long tempTime;
33 
34  hasIndex = false;
35  dir.Form(raw_dir);
36  dp = opendir(dir.Data());
37  if(dp)
38  {
39  indexF.Form("%s/index.txt", dir.Data());
40  std::ifstream inf(indexF.Data());
41  if(inf)
42  {
43  closedir(dp);
44  dp = NULL;
45  hasIndex = true;
46  while(inf >> tempStr >> tempTime)
47  {
48  if(evTime < tempTime)
49  {
50  str = tempStr;
51  break;
52  }
53  }
54  indexF.Form(dir.Data());
55  dir.Form("%s/%s", indexF.Data(), str.c_str());
56  dp = opendir(dir.Data());
57  }
58  }
59 
60  if (!dp)
61  {
62  dir.Form("./data/responses/%s",raw_dir);
63  dp = opendir(dir.Data());
64  if(dp)
65  {
66 
67  indexF.Form("%s/index.txt", dir.Data());
68  std::ifstream inf2(indexF.Data());
69  if(inf2)
70  {
71  closedir(dp);
72  dp = NULL;
73  hasIndex = true;
74  while(inf2 >> tempStr >> tempTime)
75  {
76  if(evTime < tempTime)
77  {
78  str = tempStr;
79  break;
80  }
81  }
82  indexF.Form(dir.Data());
83  dir.Form("%s/%s", indexF.Data(), str.c_str());
84  dp = opendir(dir.Data());
85  }
86  }
87  }
88 
89  if (!dp)
90  {
91  dir.Form("%s/share/AnitaAnalysisFramework/responses/%s", getenv("ANITA_UTIL_INSTALL_DIR"), raw_dir);
92  dp = opendir(dir.Data());
93  if(dp)
94  {
95  indexF.Form("%s/index.txt", dir.Data());
96  std::ifstream inf3(indexF.Data());
97  if(inf3)
98  {
99  closedir(dp);
100  dp = NULL;
101  hasIndex = true;
102  while(inf3 >> tempStr >> tempTime)
103  {
104  if(evTime < tempTime)
105  {
106  str = tempStr;
107  break;
108  }
109  }
110  indexF.Form(dir.Data());
111  dir.Form("%s/%s", indexF.Data(), str.c_str());
112  dp = opendir(dir.Data());
113  }
114  }
115  }
116 
117  if (!dp)
118  {
119  fprintf(stderr,"Could not open response dir %s\n",raw_dir);
120  fprintf(stderr,"Last directory checked: %s\n", dir.Data());
121  return 1;
122  }
123 
124  std::map<const char *, AbstractResponse *> prefix_map;
125 
126  while ( (ep = readdir(dp)) )
127  {
128 
129  char * entry = ep->d_name;
130  char * prefix = strdup(entry);
131 
132  if (entry[0]=='.') continue;
133 
134  // printf("ResponseManager found: %s\n",entry);
135  // find dot
136 
137  char * dot = strstr(prefix,".");
138  if (!dot)
139  {
140  fprintf(stderr, "Entry %s contains no .\n", entry);
141  free(prefix);
142  continue;
143  }
144 
145  //pointer to the suffix
146  char * suffix = dot+1;
147 
148  //truncate the prefix
149  *dot = 0;
150 
151  double angle = 0;
152 
153  char * angstr=strstr(prefix,"_");
154 
155  //get angle (search for _)
156  if (angstr)
157  {
158  angle = atof(angstr+1); //parse the angle
159  *prefix = 0; //chop it off from the prefix
160  }
161 
163 
164  // do we have something with the same prefix but different angle? If so, we should add to it
165  if (prefix_map.count(prefix))
166  {
167  r = prefix_map[prefix];
168  }
169  else
170  {
171  r = 0;
172  }
173 
174 
175  //now create a response from this
176  if (!strcasecmp(suffix,"imp"))
177  {
178  TString fname;
179  fname.Form("%s/%s", dir.Data(), entry);
180  TGraph imp(fname);
181  if (!r)
182  {
183  r = new Response(&imp, npad);
184  response_store.push_back(r);
185  }
186  else
187  {
188  assert(imp.GetN());
189  AnalysisWaveform aw (imp.GetN(), imp.GetY(), imp.GetX()[1] - imp.GetX()[0], imp.GetX()[0]);
190  aw.padEven(npad);
191  ((AnitaResponse::Response*) r)->addResponseAtAngle(angle, aw.freq());
192  }
193  }
194 
195  else if (!strcasecmp(suffix,"freq"))
196  {
197  // TODO writer parser
198 
199  fprintf(stderr,"Reading of .freq format not implemented yet!\n");
200 
201  }
202 
203  else if (!strcasecmp(suffix,"iir"))
204  {
205  //TODO write parser
206  fprintf(stderr,"Reading of .iir format not implemented yet!\n");
207  }
208 
209 
210 
211  //now figure out what to apply it to
212 
213 
214  int start_ant, stop_ant, start_pol, stop_pol;
215 
216  if (!strcasecmp(prefix,"all"))
217  {
218  start_pol = 0; start_ant = 0;
219  stop_pol = 1; stop_ant = NUM_SEAVEYS-1;
220  }
221 
222  else if (!strcasecmp(prefix,"allHpol"))
223  {
224  start_pol = 0; start_ant = 0;
225  stop_pol = 0; stop_ant = NUM_SEAVEYS-1;
226  }
227 
228  else if (!strcasecmp(prefix,"allVpol"))
229  {
230  start_pol = 1; start_ant = 0;
231  stop_pol = 1; stop_ant = NUM_SEAVEYS-1;
232  }
233  else
234  {
235  int phi;
236  char ring;
237  char pol;
238 
239  sscanf(prefix,"%02d%c%c", &phi,&ring,&pol);
240 
241  if (pol == 'h' || pol == 'H')
242  {
243  start_pol = 0;
244  stop_pol = 0;
245  }
246  else if (pol == 'v' || pol == 'V')
247  {
248  start_pol = 1;
249  stop_pol = 1;
250  }
251  else
252  {
253  fprintf(stderr,"Something wrong with %s\n",prefix);
254  free(prefix);
255  continue;
256  }
257 
258  AnitaRing::AnitaRing_t the_ring;
259 
260  if (ring == 'T' || ring == 't')
261  {
262  the_ring = AnitaRing::kTopRing;
263  }
264  else if (ring == 'M' || ring == 'm')
265  {
266  the_ring = AnitaRing::kMiddleRing;
267  }
268  else if (ring == 'B' || ring == 'b')
269  {
270  the_ring = AnitaRing::kBottomRing;
271  }
272 
273  else
274  {
275  fprintf(stderr,"Something wrong with %s\n",prefix);
276  free(prefix);
277  continue;
278  }
279 
280  int ant = AnitaGeomTool::Instance()->getAntFromPhiRing(phi-1, the_ring);
281  start_ant = ant;
282  stop_ant = ant;
283  }
284 
285  // printf("\t%d %d %d %d\n", start_ant, stop_ant, start_pol, stop_pol);
286  for (int pol = start_pol; pol <= stop_pol; pol++)
287  {
288  for (int ant = start_ant; ant <= stop_ant; ant++)
289  {
290  responses[ant][pol] = r;
291  }
292  }
293 
294 
295  free(prefix);
296  }
297 
298  for (int ant = 0; ant < NUM_SEAVEYS; ant++)
299  {
300  for (int pol = 0; pol <2; pol++)
301  {
302  if (! responses[ant][pol])
303  {
304  printf("%d %d\n",ant,pol);
305  }
306  assert(responses[ant][pol]);
307  }
308  }
309 
310 
311  closedir(dp);
312 
313 
314  return 0;
315 }
316 
317 // AnitaResponse::ResponseManager::ResponseManager(const AnitaResponse::AnalysisConfig *cfg )
318 // {
319 // memset(responses,0,sizeof(responses));
320 
321 // if (cfg->response_option)
322 // {
323 // loadResponsesFromDir(AnalysisConfig::getResponseString(cfg->response_option),cfg->response_npad);
324 // }
325 
326 // method = cfg->deconvolution_method;
327 
328 // }
329 
330 void AnitaResponse::ResponseManager::checkTime(unsigned int evTime)
331 {
332  if(!hasIndex) return;
333  DIR *dp;
334  struct dirent *ep;
335 
336  TString dir ;
337 
338  dir.Form(whichDir);
339  dp = opendir(dir.Data());
340  TString indexF;
341  std::string tempStr;
342  std::string loadStr;
343  long tempTime;
344  long timePrev = 0;
345 
346  bool diffResponses = false;
347  if(!evTime) diffResponses = true;
348 
349  if(dp)
350  {
351  indexF.Form("%s/index.txt", dir.Data());
352  std::ifstream inf(indexF.Data());
353  if(inf)
354  {
355  indexF.Form("%s", dir.Data());
356  while(inf >> tempStr >> tempTime)
357  {
358  if(evTime > timePrev && evTime < tempTime) loadStr = tempStr;
359  if(lastTime > timePrev && lastTime < tempTime)
360  {
361  if(evTime < timePrev || evTime > tempTime)
362  {
363  diffResponses = true;
364  }
365  }
366  timePrev = tempTime;
367  }
368  }
369  }
370 
371  if (!dp)
372  {
373  dir.Form("./data/responses/%s",whichDir);
374  dp = opendir(dir.Data());
375  if(dp)
376  {
377  indexF.Form("%s/index.txt", dir.Data());
378  std::ifstream inf2(indexF.Data());
379  if(inf2)
380  {
381  indexF.Form("%s", dir.Data());
382  while(inf2 >> tempStr >> tempTime)
383  {
384  if(evTime > timePrev && evTime < tempTime) loadStr = tempStr;
385  if(lastTime > timePrev && lastTime < tempTime)
386  {
387  if(evTime < timePrev || evTime > tempTime)
388  {
389  diffResponses = true;
390  }
391  }
392  timePrev = tempTime;
393  }
394  }
395  }
396  if (!dp)
397  {
398  dir.Form("%s/share/AnitaAnalysisFramework/responses/%s", getenv("ANITA_UTIL_INSTALL_DIR"), whichDir);
399  dp = opendir(dir.Data());
400  if(dp)
401  {
402  indexF.Form("%s/index.txt", dir.Data());
403  std::ifstream inf3(indexF.Data());
404  if(inf3)
405  {
406  indexF.Form("%s", dir.Data());
407  while(inf3 >> tempStr >> tempTime)
408  {
409  if(evTime > timePrev && evTime < tempTime) loadStr = tempStr;
410  if(lastTime > timePrev && lastTime < tempTime)
411  {
412  if(evTime < timePrev || evTime > tempTime)
413  {
414  diffResponses = true;
415  }
416  }
417  timePrev = tempTime;
418  }
419  }
420  }
421  }
422  }
423 
424  lastTime = evTime;
425  if(diffResponses)
426  {
427  dir.Form("%s/%s", indexF.Data(), loadStr.c_str());
428  for (size_t i = 0; i < response_store.size(); i++)
429  {
430  delete response_store[i];
431  }
432  response_store.clear();
433  loadResponsesFromDir(dir, savePad, evTime);
434  fprintf(stderr, "changed responses\n");
435  }
436  if(dp) closedir(dp);
437 }
438 
439 
440 
441 AnitaResponse::ResponseManager::ResponseManager(const char * dir, int npad, const AnitaResponse::DeconvolutionMethod* methodPtr, unsigned int evTime)
442 {
443  memset(responses,0,sizeof(responses));
444  loadResponsesFromDir(dir,npad, evTime);
445  method = methodPtr == NULL ? &AnitaResponse::kDefaultDeconvolution : methodPtr;
446  lastTime = evTime;
447  whichDir = dir;
448  savePad = npad;
449  // method = &kDefaultDeconvolution;
450 }
451 
452 AnitaResponse::ResponseManager::~ResponseManager()
453 {
454 
455  for (size_t i = 0; i < response_store.size(); i++)
456  {
457  delete response_store[i];
458  }
459 }
enum AnitaRing::EAnitaRing AnitaRing_t
Ring enumeration.
void padEven(int factor, int where=1)
static Int_t getAntFromPhiRing(Int_t phi, AnitaRing::AnitaRing_t ring)
get antenna number from phi and ring
static AnitaGeomTool * Instance(int anita_version=0)
Instance generator. If version_number == 0, uses AnitaVersion::get();.
This class is intended to be the main storage vessel for ANITA waveforms. It is similar in principle ...