EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
r2dat.c
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file r2dat.c
1 
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdlib.h>
5 
6 struct frec {
7  float B[3];
8 };
9 
10 static void usage(char *exe)
11 {
12  printf("\n usage: %s <Rick's ASCII map name> <1|2|3|4|5>\n\n", exe);
13  printf(" -> converts Rick's ASCII file into the SolenoidMap{12345}.dat\n");
14  printf(" file which can be later imported by PandaRoot\n\n");
15 
16  exit(-1);
17 } /* usage */
18 
19 int main(int argc, char **argv)
20 {
21  if (argc != 3) usage(argv[0]);
22 
23  {
24  unsigned id = atoi(argv[2]);
25  if (!id || id > 5) usage(argv[0]);
26 
27  {
28  char *fname = argv[1], qname[FILENAME_MAX];
29 
30  snprintf(qname, FILENAME_MAX-1, "SolenoidMap%d.dat", id);
31 
32  {
33  FILE *fin = fopen(fname, "r"), *fout = fopen(qname, "w");
34 
35  if (!fin)
36  {
37  printf("Failed to open '%s' for reading!\n", fname);
38  exit(-1);
39  } /*if*/
40 
41  if (!fout)
42  {
43  printf("Failed to open '%s' for writing!\n", qname);
44  exit(-1);
45  } /*if*/
46 
47  {
48  // Assume, that XYZ coordinates are always in ascending order,
49  // so it suffices to count number of changes;
50  unsigned dummy, nodes[3] = {0, 0, 0};
51 
52  // Assume Rick's format will never change; first line should define
53  // number of nodes per XYZ coordinate;
54  if (fscanf(fin, "%d %d %d %d\n", nodes+0, nodes+1, nodes+2, &dummy) != 4)
55  {
56  printf("File '%s' has wrong format!\n", fname);
57  exit(-2);
58  } /*if*/
59 
60  // Loop through all the data lines;
61  {
62  float x[3], B[3];
63  unsigned lnum = nodes[0]*nodes[1]*nodes[2];
64 
65  // Skip 7 meaningless lines;
66  {
67  size_t count;
68  char *ptr = 0;
69 
70  for(unsigned ip=0; ip<7; ip++)
71  getline(&ptr, &count, fin);
72  }
73 
74  {
75  float xmin[3], xmax[3];
76  // NB: stack variable will not work (array may get too large)!;
77  frec *frecs = new frec[lnum];
78 
79  // Read exactly the declared number of data lines;
80  for(unsigned ip=0; ip<lnum; ip++)
81  {
82  if (fscanf(fin, "%f %f %f %f %f %f\n", x+0, x+1, x+2, B+0, B+1, B+2) != 6)
83  {
84  printf("File '%s' has wrong format!\n", fname);
85  exit(-2);
86  } /*if*/
87 
88  // Store data records;
89  memcpy(frecs[ip].B, B, 3*sizeof(float));
90 
91  // Update min/max values;
92  for(unsigned iq=0; iq<3; iq++)
93  {
94  if (!ip || x[iq] < xmin[iq]) xmin[iq] = x[iq];
95  if (!ip || x[iq] > xmax[iq]) xmax[iq] = x[iq];
96  } /*for iq*/
97  } /*for ip*/
98 
99  // Dump file header; units are [cm] in both cases;
100  fprintf(fout, "Solenoid\n");
101  fprintf(fout, "G\n");
102  for(unsigned iq=0; iq<3; iq++)
103  fprintf(fout, "%6.1f %6.1f %3d\n", xmin[iq], xmax[iq], nodes[iq]);
104 
105  // Dump the data records;
106  for(unsigned ip=0; ip<lnum; ip++)
107  {
108  struct frec *fptr = frecs + ip;
109 
110  fprintf(fout, "%10.3f %10.3f %10.3f\n", fptr->B[0], fptr->B[1], fptr->B[2]);
111  } /*for ip*/
112  }
113  }
114  }
115  }
116  }
117  }
118 
119  exit(0);
120 }