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  TString myPath("$STAR/StRoot/StEvent/StEnumerations.h");
23  gSystem->ExpandPathName(myPath);
24 
25  int notExi = gSystem->AccessPathName(myPath.Data(),kFileExists);
26  if (notExi) { ids[0]=-1; return;}
27  FILE *fp = fopen(myPath.Data(),"r");
28  if (!fp) { ids[0]=-1; return;}
29  char buf[400];
30 
31  int kase = 0;
32  //
33  // Process the StEnumerations header file. Once StDetectorId is found
34  // we begin the work. Each instance of k<DetectorName> will be used to
35  // build a map, associating the enum value to the string name. The loop
36  // will be terminated once the enumeration block is ended (the "}") is
37  // found.
38  //
39  while (true) {
40  fgets(buf,200,fp);
41  int eof = feof(fp);
42  if (eof) break;
43  TString tb(buf);
44 
45  if(!kase) {
46  if (tb.Index("enum")<0) continue;
47  if (tb.Index("StDetectorId")<0) continue;
48  if (tb.Index("=")<0) continue;
49  if (tb.Index("kUnknownId")<0) continue;
50  kase = 1;
51  }
52  tb.ReplaceAll(" ","");
53  if (tb.Index("//")==0) continue;
54  int myK = tb.Index("k"); if (myK <0) break;
55  int myEq= tb.Index("="); if (myEq<0) break;
56  int myE = tb.Index(",");
57  if (myE<0) myE = tb.Index("}");
58  if (myE<0) break;
59  TString com(tb.Data()+myK,myEq-myK);
60  int id = gROOT->ProcessLineFast(com);
61  ids[0]++; // counts total number of entries
62  ids[ids[0]] = id;
63  cds[ids[0]] = new char[com.Length()+1];
64  strcpy(cds[ids[0]],com.Data());
65 
66  if (tb[myE]=='}') break;
67  }
68  fclose(fp);
69  for (int i=1;i<=ids[0];i++) {
70  printf("%d = %s\n",ids[i],cds[i]);
71  }
72 }
73 //_____________________________________________________________________________
74 const char *detectorNameById(StDetectorId id)
75 {
76  if (ids[0]<0) return "Unknown";
77  if (!ids[0] ) detectorIdInit();
78 
79  for (int i=1;i<=ids[0];i++) { if (ids[i]==id) return cds[i]+1;}
80  return "Unknown";
81 }
82 //_____________________________________________________________________________
83 StDetectorId detectorIdByName(const char *name)
84 {
85  if (ids[0]<0) return kUnknownId;
86  if (!ids[0] ) detectorIdInit();
87  TString tName(name); tName.ReplaceAll("Id","");
88  for (int i=1;i<=ids[0];i++){
89  TString tds(cds[i]+1); tds.ReplaceAll("Id","");
90  if (tName.Contains(tds,TString::kIgnoreCase)) return (StDetectorId)ids[i];
91  }
92  return kUnknownId;
93 }
94 //_____________________________________________________________________________
95 void detectorIdInit()
96 {
97  detectorId( ids, cds );
98  if ( ids[0] <= 0 ) {
99  LOG_FATAL << "Failed to parse StEnumerations.h / StDetectorId enumeration. Kaboom." << endm;
100  assert( 0 == 2015 );
101  };
102 }