StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StEnumerations.cxx
1 #include <assert.h>
2 #include "StEnumerations.h"
3 #include "TROOT.h"
4 #include "TSystem.h"
5 #include "TString.h"
6 
7 #include "St_base/StMessMgr.h"
8 
9 int ids[100]={0};
10 char *cds[100]={0};
11 static void detectorIdInit();
12 //_____________________________________________________________________________
13 void detectorId(int *ids=0, char** cds=0)
14 {
15  int myIds[100];
16  char *myCds[100];
17  if (!ids) { ids = myIds; cds = myCds; }
18 
19  memset(ids,0,sizeof(ids[0])*100);
20  memset(cds,0,sizeof(cds[0])*100);
21 
22  // Look for local enumerations (when developing) before global
23  TString myPath("./StRoot/StEvent/StEnumerations.h");
24  int notExi = gSystem->AccessPathName(myPath.Data(),kFileExists);
25  if (notExi) {
26  myPath = "$STAR/StRoot/StEvent/StEnumerations.h";
27  gSystem->ExpandPathName(myPath);
28  notExi = gSystem->AccessPathName(myPath.Data(),kFileExists);
29  if (notExi) { ids[0]=-1; return;}
30  }
31  FILE *fp = fopen(myPath.Data(),"r");
32  if (!fp) { ids[0]=-1; return;}
33  char buf[400];
34 
35  int kase = 0;
36  //
37  // Process the StEnumerations header file. Once StDetectorId is found
38  // we begin the work. Each instance of k<DetectorName> will be used to
39  // build a map, associating the enum value to the string name. The loop
40  // will be terminated once the enumeration block is ended (the "}") is
41  // found.
42  //
43  while (true) {
44  fgets(buf,200,fp);
45  int eof = feof(fp);
46  if (eof) break;
47  TString tb(buf);
48 
49  if(!kase) {
50  if (tb.Index("enum")<0) continue;
51  if (tb.Index("StDetectorId")<0) continue;
52  if (tb.Index("=")<0) continue;
53  if (tb.Index("kUnknownId")<0) continue;
54  kase = 1;
55  }
56  tb.ReplaceAll(" ","");
57  if (tb.Index("//")==0) continue;
58  int myK = tb.Index("k"); if (myK <0) break;
59  int myEq= tb.Index("="); if (myEq<0) break;
60  int myE = tb.Index(",");
61  if (myE<0) myE = tb.Index("}");
62  if (myE<0) break;
63  TString com(tb.Data()+myK,myEq-myK);
64  int id = gROOT->ProcessLineFast(com);
65  ids[0]++; // counts total number of entries
66  ids[ids[0]] = id;
67  cds[ids[0]] = new char[com.Length()+1];
68  strcpy(cds[ids[0]],com.Data());
69 
70  if (tb[myE]=='}') break;
71  }
72  fclose(fp);
73  for (int i=1;i<=ids[0];i++) {
74  printf("%d = %s\n",ids[i],cds[i]);
75  }
76 }
77 //_____________________________________________________________________________
78 const char *detectorNameById(StDetectorId id)
79 {
80  if (ids[0]<0) return "Unknown";
81  if (!ids[0] ) detectorIdInit();
82 
83  for (int i=1;i<=ids[0];i++) { if (ids[i]==id) return cds[i]+1;}
84  return "Unknown";
85 }
86 //_____________________________________________________________________________
87 StDetectorId detectorIdByName(const char *name)
88 {
89  if (ids[0]<0) return kUnknownId;
90  if (!ids[0] ) detectorIdInit();
91  TString tName(name); tName.ReplaceAll("Id","");
92  for (int i=1;i<=ids[0];i++){
93  TString tds(cds[i]+1); tds.ReplaceAll("Id","");
94  if (tName.Contains(tds,TString::kIgnoreCase)) return (StDetectorId)ids[i];
95  }
96  return kUnknownId;
97 }
98 //_____________________________________________________________________________
99 void detectorIdInit()
100 {
101  detectorId( ids, cds );
102  if ( ids[0] <= 0 ) {
103  LOG_FATAL << "Failed to parse StEnumerations.h / StDetectorId enumeration. Kaboom." << endm;
104  assert( 0 == 2015 );
105  };
106 }