StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
SpinDbIO.C
1 // $Id: SpinDbIO.C,v 1.1 2005/09/30 23:47:48 balewski Exp $:
2 
3 #include <stdlib.h>
4 #include <stdio.h>
5 
6 #include <string.h>
7 #include <errno.h>
8 #include <unistd.h>
9 #include <ctype.h>
10 
11 #include "SpinDbIO.h"
12 #include "spinDbAPI.h"
13 
14 #include "spinDbV124.hh"
15 #include "spinDbStar.hh"
16 #include "spinDbBXmask.hh"
17 
18 // ================================================================
19 //
20 // ================================================================
21 SpinDbIOBase::SpinDbIOBase (int nelem, int size)
22 {
23  bytes = nelem*size;
24  nElem = nelem;
25  bytePtr = new char[bytes];
26  indexArr = new int[nElem];
27  comment = NULL;
28  for(int i=0;i<nElem;i++) indexArr[i]=i; // default index
29 }
30 
31 SpinDbIOBase::~SpinDbIOBase() {
32  if(bytePtr ) delete [] bytePtr ;
33  if(indexArr) delete [] indexArr;
34 };
35  // =======================================================
36 // ========================================================
37 
38 SpinDbV124IO::SpinDbV124IO(int n) : SpinDbIOBase(n,sizeof(spinDbV124)) {};
39 
40 inline spinDbV124*
41 SpinDbV124IO::data (int i) { return ( (spinDbV124 *)bytePtr + i ); };
42 
43 //-----------------------------------------------------
44 //-----------------------------------------------------
45 int
46 SpinDbV124IO::read(FILE *f) {
47  // fprintf(stdout,"Jestem w SpinDbV124IO file->DB\n");
48  memset(bytePtr,0x00,bytes);
49  spinDbV124 *t = data();
50 
51  int i=0;
52  int nV=0;
53  for(i=0;i<SPINDbMaxRing;i++) nV+=fscanf(f,"%d",&t->bucketOffset[i]);
54  for(i=0;i<SPINDbMaxRing;i++) nV+=fscanf(f,"%d",&t->rotatorState[i]);
55  if(nV!=4) return 0; //crash it
56  // fprintf(stdout,"Read in %d values\n",nV);
57 
58  for(i=0;i<SPINDbMaxBuckets;i++) {
59  char cx[10];
60  int ix;
61  nV=fscanf(f,"%8s %d ",cx,&ix);
62  // fprintf(stdout,"%8s %d %d\n",cx,ix,nV);
63  if(nV!=2) return 0; //crash it
64  if(ix!=i+1) return 0; //crash it
65  int j;
66  unsigned char val=0;
67  for(j=0;j<8;j++) {
68  if(cx[j]=='1') val+=(1<<(7-j));
69  else if(cx[j]!='0')return 0; //crash it
70  // fprintf(stdout,"j=%d, c=%c val=0x%x\n",j,cx[j],val);
71  }
72  t->v124bits[i]=val;
73  //fprintf(stdout,"tb=%d, val=0x%x\n",ix,val);
74  //if(ix>=3) break; // tmp
75  }
76 
77  if(comment) strncpy(t->comment,comment,SPINDbMaxComment-1);
78 
79  //write(stdout); fprintf(f,"\ncomm=%s=\n",t->comment);// tmp
80  return 1;
81 
82 }
83 
84 //-----------------------------------------------------
85 //-----------------------------------------------------
86 int
87 SpinDbV124IO::write(FILE *f) {
88  // fprintf(stdout,"Jestem w SpinDbV124IO DB-->FILE\n");
89  spinDbV124 *t = data(0);
90  int i=0;
91 
92  for(i=0;i<SPINDbMaxRing;i++) fprintf(f,"%d ",t->bucketOffset[i]);
93  fprintf(f,"\n");
94  for(i=0;i<SPINDbMaxRing;i++) fprintf(f,"%d ",t->rotatorState[i]);
95  fprintf(f,"\n");
96  for(i=0;i<SPINDbMaxBuckets;i++) {
97  unsigned char val= t->v124bits[i];
98  int j;
99  for(j=0;j<8;j++) {
100  int bb=0;
101  if(val& (1<<(7-j))) bb=1;
102  fprintf(stdout,"%d",bb);
103  }
104  fprintf(stdout," %3d\n",i+1);
105  }
106  setComment(t->comment);
107  return 1;
108 }
109 
110 
111 // =========================================================
112 // =========================================================
113 
114 SpinDbStarIO::SpinDbStarIO(int n) : SpinDbIOBase(n,sizeof(spinDbStar)) {};
115 
116 
117 inline spinDbStar*
118 SpinDbStarIO::data (int i) { return ( (spinDbStar *)bytePtr + i ); };
119 
120 //-----------------------------------------------------
121 //-----------------------------------------------------
122 int
123 SpinDbStarIO::read(FILE *f) {
124  // fprintf(stdout,"Jestem w SpinDbStarIO file->DB\n");
125  memset(bytePtr,0x00,bytes);
126  spinDbStar *t = data();
127  memset(t,0,sizeof(spinDbStar));
128 
129  int nV=fscanf(f,"%d %d",&t->bXoff7, &t->bXoff48);
130  if(nV!=2) return 0; //crash it
131  fprintf(stdout,"Read in %d values\n",nV);
132 
133  if(comment) strncpy(t->comment,comment,SPINDbMaxComment-1);
134 
135  return 1;
136 
137 }
138 
139 //-----------------------------------------------------
140 //-----------------------------------------------------
141 int
142 SpinDbStarIO::write(FILE *f) {
143  // fprintf(stdout,"Jestem w SpinDbStarIO DB-->FILE\n");
144  spinDbStar *t = data(0);
145 
146  fprintf(f,"%d %d\n",t->bXoff7, t->bXoff48);
147  setComment(t->comment);
148  return 1;
149 }
150 
151 
152 
153 
154 // =======================================================
155 // =======================================================
156 
157 SpinDbBXmaskIO::SpinDbBXmaskIO(int n) : SpinDbIOBase(n,sizeof(spinDbBXmask)) {};
158 
159 
160 inline spinDbBXmask*
161 SpinDbBXmaskIO::data (int i) { return ( (spinDbBXmask *)bytePtr + i ); };
162 
163 //-----------------------------------------------------
164 //-----------------------------------------------------
165 int
166 SpinDbBXmaskIO::read(FILE *f) {
167  fprintf(stdout,"Jestem w SpinDbBXmaskIO file->DB\n");
168  memset(bytePtr,0x00,bytes);
169  spinDbBXmask *t = data();
170  memset(t,0,sizeof(spinDbBXmask));
171  int nV=0;
172  do {
173  int val;
174  int ret=fscanf(f,"%d ",&val);
175  if(ret==EOF) break;
176  if(ret!=1) return 0; //crash it
177  if(val<0 || val>=SPINDbMaxBXings) return 0; //crash it
178  t->bXmask[val]=1;
179  nV++;
180  } while(1);
181 
182  fprintf(stdout,"Read in %d values\n",nV);
183 
184  if(comment) strncpy(t->comment,comment,SPINDbMaxComment-1);
185 
186  write(stdout); fprintf(f,"\ncomm=%s=\n",t->comment);// tmp
187  return 1;
188 
189 }
190 
191 //-----------------------------------------------------
192 //-----------------------------------------------------
193 int
194 SpinDbBXmaskIO::write(FILE *f) {
195  fprintf(stdout,"Jestem w SpinDbBXmaskIO DB-->FILE\n");
196  spinDbBXmask *t = data(0);
197  int i,k=0;
198  for(i=0;i<SPINDbMaxBXings;i++){
199  if(t->bXmask[i]==0) continue;
200  k++;
201  fprintf(f,"%d ",i);
202  if(k%16==0)fprintf(f,"\n");
203  }
204  fprintf(f,"\n");
205  setComment(t->comment);
206  return 1;
207 }
208 
209 
210 
211 
212 // $Log: SpinDbIO.C,v $
213 // Revision 1.1 2005/09/30 23:47:48 balewski
214 // start
215 //