StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
RecHeaderFormats.cxx
1 /***************************************************************************
2  * $Id: RecHeaderFormats.cxx,v 1.8 2018/06/10 17:16:01 smirnovd Exp $
3  * Author: M.W. Schulz, Jeff Landgraf, M.J. LeVine
4  ***************************************************************************
5  * Description: Bank header formats common to all detectors in STAR:
6  * methods
7  *
8  * History of modifications
9  * 06/11/99
10  * Loop index changed from l to ll to compensate for the problems this
11  * compiler has with the scope ov variables that are in parentheses
12  * Place in the code marked with the above comment.
13  *
14  ***************************************************************************
15  * $Log: RecHeaderFormats.cxx,v $
16  * Revision 1.8 2018/06/10 17:16:01 smirnovd
17  * Add missing namespace OLDEVP
18  *
19  * Revision 1.7 2014/06/25 15:33:16 jeromel
20  * Code not used but erradicated use of flush
21  *
22  * Revision 1.6 2007/12/24 06:04:17 fine
23  * introduce OLDEVP namespace to allow ole and new EVP library concurrently
24  *
25  * Revision 1.5 1999/12/07 23:10:31 levine
26  * changes to silence the gcc compiler warnings
27  *
28  * Revision 1.4 1999/07/04 01:47:59 levine
29  * minor changes to make solaris CC compiler happy
30  *
31  * Revision 1.3 1999/07/02 04:37:41 levine
32  * Many changes - see change logs in individual programs
33  *
34  *
35  **************************************************************************/
36 
37 
38 
39 #include <stdio.h>
40 #include <assert.h>
41 #include <sys/types.h>
42 
43 #include "RecHeaderFormats.hh"
44 #include "CRC.hh"
45 #include "swaps.hh"
46 #include <string.h>
47 
48 #define NOCRCCHECKING
49 
50 // Functions valid on all BANKS
51 using namespace OLDEVP;
52 
53 char* OLDEVP::name2str(char* type) /* for everyone !!!! */
54 {
55  static char name[9] = {0} ;
56  memcpy(name,type,8) ;
57  name[8] = 0 ;
58  return(name) ;
59 };
60 
61 // first some trash can functions
62 void OLDEVP::dump_data(char* buffer,int size,int width )
63 {
64  uint* data_base = (uint*)buffer ;
65  uint* data ;
66  char* data_char ;
67 
68  if(!size) return ;
69  data = data_base;
70  data_char = (char*) data ;
71  for(int w = 0 ; w < size ; w++)
72  {
73  if((w > 0) && (!(w%width)))
74  {
75  printf(" || ");
76  data_char = (char*)(&data[w - width]);
77  for(int l = 0 ; l < width*4 ; l++)
78  {
79  if(((uint)data_char[l]) < 0x21 || ((uint)data_char[l]) > 0x176)
80  printf(" ") ;
81  else
82  printf("%c",data_char[l]) ;
83  }
84  printf("\n") ;
85  }
86  printf("0X%08X ",data[w]) ;
87  }
88  int left_over =size%width ;
89  if(!left_over)
90  {
91  printf(" || ") ;
92  data_char = (char*)(&data[size - width ] ) ;
93  for(int l = 0 ; l < width*4 ; l++)
94  {
95  if(((uint)data_char[l]) < 0x21 || ((uint)data_char[l]) > 0x176)
96  printf(" ") ;
97  else
98  printf("%c",data_char[l]) ;
99  }
100  printf("\n") ;
101  }
102  if(left_over)
103  {
104  //Loop index changed from l to ll to compensate for the problems this
105  //compiler has with the scope ov variables that are in parentheses
106  for(int ll = 0 ; ll < (width - left_over) ; ll++) printf(" ") ;
107  printf(" || ") ;
108  data_char = (char*)(&data[size - left_over ] ) ;
109  for(int l = 0 ; l < left_over*4 ; l++)
110  {
111  if(((uint)data_char[l]) < 0x21 || ((uint)data_char[l]) > 0x176)
112  printf(" ") ;
113  else
114  printf("%c",data_char[l]) ;
115  }
116  printf("\n") ;
117  }
118 }
119 /*---------------------------------------------------------------------------*/
120 
121 //***********************************************
122 int Logical_Record_Header::swap()
123 {
124  // start only at the bank length the BankType character array not swapped
125  int iret = swap_raw(ByteOrder,&BankLength,
126  ((sizeof(Logical_Record_Header))/4 - 2));
127  return(iret) ;
128 };
129 
130 void Logical_Record_Header::print()
131 {
132  if(ByteOrder != 0x04030201)
133  {
135  memcpy(&lbh , this ,sizeof(lbh)) ;
136  int iret = lbh.swap() ;
137  if(iret < 0 ) printf("swapping failed %s %d\n",__FILE__,__LINE__) ;
138  lbh.print() ;
139  return ;
140  }
141  printf("Logical Record Header:\n");
142  printf("BankType Length RunNum FormatVersion ByteOrder RES1 RES2 RES3 CRC\n") ;
143  printf("%-8s %06d %03d %04d.%04d 0X%08X %04d %05d 0X%08X 0X%08X\n",
144  name2str(BankType),BankLength,RunNumber,FormatVersion >> 16 ,
145  FormatVersion & 0x0000FFFF, ByteOrder, reserved1,
146  reserved2, reserved3 , CRC) ;
147 
148 };
149 
150 // Does not check or reset the CRC
151 int Logical_Record::swap()
152 {
153  // get the length and swap it
154  int words = header.BankLength ;
155  int iret = swap_raw(header.ByteOrder,&words,1) ;
156 
157  // swap returns zero means no swapping needed
158  if(!iret) return(0);
159 
160  // swap returns less than zero means error
161  if(iret < 0 ) return(iret) ;
162 
163  // swap the body first... don't want to overwrite the byte order to early
164  // Don't swap record type
165  iret = swap_raw(header.ByteOrder,&RecordLength,2);
166  assert(iret > 0); // checked the byte order above
167  iret = swap_raw(header.ByteOrder,&CRC,1);
168  assert(iret > 0);
169 
170  // swap the header
171  header.swap();
172 
173  return(iret) ;
174 };
175 
176 // For the purposes of the header CRC LRH // LR is just another bank
177 void Logical_Record::set_CRC()
178 {
179  header.CRC = 0 ;
180  header.CRC = compute_crc_bank((Bank *)this) ;
181 };
182 
183 int Logical_Record::test_CRC()
184 {
185 #ifdef NOCRCCHECKING
186  return -1;
187 #endif
188 
189  if(header.CRC == 0) return(-1) ;
190  unsigned int old_crc = header.CRC ;
191  unsigned int test = compute_crc_bank((Bank *)this) ;
192  header.CRC = old_crc ; // reconstruct the old CRC
193  swap_raw(header.ByteOrder ,(int*)&old_crc, 1);
194  if(old_crc != test) return(0) ;
195  return(-1) ;
196 };
197 
198 void Logical_Record::print(int level)
199 {
200  header.print();
201 
202  int words = header.BankLength ;
203  swap_raw(header.ByteOrder,&words,1);
204 
205  dump_data(((char *)this) + sizeof(header),words - sizeof(header)/4);
206 }
207 
208 //***********************************************
209 int Bank_Header::swap()
210 {
211  // start only at the bank length the BankType character array not swapped
212  int iret = swap_raw(ByteOrder,&BankLength,((sizeof(Bank_Header))/4 - 2));
213  return(iret) ;
214 };
215 
216 void Bank_Header::print()
217 {
218  if(ByteOrder != 0x04030201)
219  {
220  Bank_Header lbh ;
221  memcpy(&lbh , this ,sizeof(lbh)) ;
222  int iret = lbh.swap() ;
223  if(iret < 0 )printf("swapping failed %s %d\n",__FILE__,__LINE__) ;
224  lbh.print() ;
225  return ;
226  }
227  printf("Bank Header:\n");
228  printf("BankType Length BankId FormatVersion ByteOrder FormatNumber Token RES CRC\n") ;
229  printf("%-8s %06d %03d %04d.%04d 0X%08X %04d %05d 0X%08X 0X%08X\n",
230  name2str(BankType),BankLength,BankId,FormatVersion >> 16 ,
231  FormatVersion & 0x0000FFFF, ByteOrder, FormatNumber,
232  Token, reserved1 , CRC) ;
233 
234 };
235 
236 //*****************************************************
237 // Override for those banks which require byte or
238 // integer swapping
239 //
240 // Does not check or reset the CRC
241 int Bank::swap()
242 {
243  // get the length and swap it
244  int words = header.BankLength ;
245  int iret = swap_raw(header.ByteOrder,&words,1) ;
246 
247  // swap returns zero means no swapping needed
248  if(!iret) return(0);
249 
250  // swap returns less than zero means error
251  if(iret < 0 ) return(iret) ;
252 
253  // swap the body first... don't want to overwrite the byte order to early
254  words -= (sizeof(header)/4) ;
255  int* start = (int*)this + (sizeof(header)/4) ;
256  iret = swap_raw(header.ByteOrder,start,words) ;
257  assert(iret > 0); // checked the byte order above
258 
259  // swap the header
260  header.swap();
261 
262  return(iret) ;
263 };
264 
265 void Bank::set_CRC()
266 {
267  header.CRC = 0 ;
268  header.CRC = compute_crc_bank(this) ;
269 };
270 
271 int Bank::test_CRC()
272 {
273 #ifdef NOCRCCHECKING
274  return -1;
275 #endif
276 
277  if(!header.CRC)return(-1) ;
278  unsigned int old_crc = header.CRC ;
279  unsigned int test = compute_crc_bank(this) ;
280  header.CRC = old_crc ; // reconstruct the old CRC
281 
282  swap_raw(header.ByteOrder ,(int*)&old_crc, 1);
283  if(old_crc != test) return(0) ;
284  return(-1) ;
285 };
286 
287 void Bank::print(int level)
288 {
289  header.print();
290 
291  int words = header.BankLength ;
292  swap_raw(header.ByteOrder,&words,1);
293 
294  printf( "XXXXXX: length= %d, %d\n", words, words*4);
295  dump_data(((char *)this) + sizeof(header),words - sizeof(header)/4);
296 }
297 
298 
299 
300 
301