StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StFssSectorReader.cc
1 #include "StFssSectorReader.hh"
2 
3 #include <Stiostream.h>
4 #include "StMessMgr.h"
5 
6 StFssSectorReader::StFssSectorReader(int sector,
7  unsigned short *fcl_ftpcsqndx, int nSeq,
8  char *fcl_ftpcadc, int nAdc)
9 {
10  mSector=sector;
11  m_ftpcsqndx=fcl_ftpcsqndx;
12  m_ftpcadc=fcl_ftpcadc;
13  m_numSqndx=nSeq;
14  m_numAdc=nAdc;
15 
16 }
17 
18 StFssSectorReader::~StFssSectorReader()
19 {
20  //free memory allocated for Sequence arrays
21  for (int row=0; row<FTP_PADROWS; row++)
22  {
23  for (int pad=0; pad<FTP_MAXPADS; pad++)
24  {
25  void *memaddr = Pad_array[row][pad].seq;
26  if (memaddr) free(memaddr);
27  }
28  }
29 }
30 
31 int StFssSectorReader::getPadList(int PadRow, unsigned char **padList)
32 {
33  // Construct the padlist array for this PadRow
34  int pad;
35  if (PadRow == 0 || PadRow > FTP_PADROWS) return -1;
36 
37  // Fill in padrows
38  int npad=0;
39  for(pad=1; pad<=FTP_MAXPADS; pad++)
40  {
41  if (Pad_array[PadRow-1][pad-1].nseq)
42  {
43  padlist[PadRow-1][npad++] = pad;
44  }
45  }
46  // confusing syntax but correct
47  *padList = &padlist[PadRow-1][0];
48 
49  return npad;
50 }
51 
52 int StFssSectorReader::getSequences(int PadRow, int Pad, int *nSeq, Sequence **SeqData)
53 {
54  *nSeq = Pad_array[PadRow-1][Pad-1].nseq; // number of sequences this pad
55  *SeqData = Pad_array[PadRow-1][Pad-1].seq; // pass back pointer to Sequence array
56  return 0;
57 }
58 
59 int StFssSectorReader::initialize()
60 {
61  int i;
62  unsigned short *seqTable=m_ftpcsqndx;
63  char *adcTable=m_ftpcadc;
64 
65  // find position in software coordinates
66  int softSec=(mSector-1)%6;
67  int softRow=2*(int)((mSector-1)/6);
68 
69  // initialize pad arrays
70  int row, thisSoftSec=0, thisSoftRow=0;
71  for (row=0; row<FTP_PADROWS; row++)
72  {
73  Row_array[row].pad = &Pad_array[row][0];
74  Row_array[row].npads = 0; // have to fill in PadRow.npads as we go.
75  for (int pad=0; pad<FTP_MAXPADS; pad++)
76  {
77  Pad_array[row][pad].nseq=0;
78  Pad_array[row][pad].seq= (Sequence *)0;
79  }
80  }
81 
82  // search through the SEQD banks to build our tables of what's where
83 
84  int padrow=-1, pad=-1, lastbin=-2, oldstart=0;
85  // go through the SEQD twice. First time around just count the sequences
86  // so we can malloc the Sequence arrays as needed
87  for (i=0; i<m_numSqndx; i++)
88  {
89  int thisEntry=seqTable[i];
90  if ((thisEntry & 32768) == 32768) //padrow, pad header
91  {
92  thisSoftSec = ((thisEntry >>8) & 127);
93  thisSoftRow = (int) thisSoftSec / 6;
94  thisSoftSec -= 6 * thisSoftRow;
95  pad = (thisEntry & 255)+1;
96  // sequence in this sector ?
97  if(thisSoftSec==softSec &&
98  (thisSoftRow==softRow || thisSoftRow==softRow+1))
99  {
100  padrow = (thisSoftRow % 2) + 1;
101  Row_array[padrow-1].npads++; //increment #pads in padrow
102  oldstart = 0;
103  }
104  }
105  else
106  {
107  // sequence in this sector ?
108  if(thisSoftSec==softSec &&
109  (thisSoftRow==softRow || thisSoftRow==softRow+1))
110  {
111  int start = (thisEntry>>6) & 511;
112  int len = (thisEntry & 31) +1;
113  if (start >= oldstart)
114  { // still on same pad
115  if (start>lastbin+1)
116  Pad_array[padrow-1][pad-1].nseq++;
117  // don't increment nseq if sequences are adjacent!
118  lastbin = start+len-1;
119  oldstart = start;
120  if ((thisEntry & 32)==32)
121  {//last sequence ?
122  pad++; // default to next pad in this padrow
123  Row_array[padrow-1].npads++; //increment #pads in padrow
124  lastbin = -2; // set lastbin for new pad
125  oldstart=0;
126  }
127  }
128  else
129  { // starting new pad without bit 5 set!
130  LOG_WARN << "1: new pad detected with bit 5 clear!" << endm;
131  LOG_WARN << start << " < " << oldstart << endm;
132  return FALSE;
133  }
134  }
135  }
136  }
137 
138  //allocate memory for Sequence arrays
139  for (row=0; row<FTP_PADROWS; row++)
140  {
141  // int npads = Row_array[row].npads;
142  for (int pad=0; pad<FTP_MAXPADS; pad++)
143  {
144  int nseq = Pad_array[row][pad].nseq;
145  if (nseq)
146  { // only if there are sequences on this pad
147  Pad_array[row][pad].seq= (Sequence *)malloc(nseq*sizeof(Sequence));
148  if (Pad_array[row][pad].seq==NULL)
149  {
150  LOG_ERROR << "failed to malloc() Sequence structures " << endm;
151  return FALSE;
152  }
153  }
154  }
155  }
156 
157  //second pass
158  padrow=-1;
159  pad=-1;
160  lastbin=-2;
161  int pad_seq=0;
162  oldstart = 0;
163 
164  uint8_t *adc_locn = (uint8_t *)(adcTable);
165  for (i=0; i<m_numSqndx; i++)
166  {
167  int thisEntry=seqTable[i];
168  if ((thisEntry & 32768) == 32768) //padrow, pad header
169  {
170  thisSoftSec = ((thisEntry >>8) & 127);
171  thisSoftRow = (int) thisSoftSec / 6;
172  thisSoftSec -= 6 * thisSoftRow;
173  pad = (thisEntry & 255)+1;
174  // sequence in this sector ?
175  if(thisSoftSec==softSec &&
176  (thisSoftRow==softRow || thisSoftRow==softRow+1))
177  {
178  padrow = (thisSoftRow % 2) + 1;
179  pad_seq = 0;
180  oldstart=0;
181  }
182  }
183  else
184  {
185  int start = (thisEntry>>6) & 511;
186  int len = (thisEntry & 31) +1;
187  if (start >= oldstart)
188  { // still on same pad
189  //is this sequence adjacent to previous one?
190  if (start>lastbin+1)
191  { //no
192  // sequence in this sector ?
193  if(thisSoftSec==softSec &&
194  (thisSoftRow==softRow || thisSoftRow==softRow+1))
195  {
196  Pad_array[padrow-1][pad-1].seq[pad_seq].startTimeBin = start;
197  Pad_array[padrow-1][pad-1].seq[pad_seq].Length = len;
198  Pad_array[padrow-1][pad-1].seq[pad_seq].FirstAdc = adc_locn;
199  pad_seq++;
200  }
201  adc_locn +=len;
202  }
203  else
204  { // yes: just update the length
205  // sequence in this sector ?
206  if(thisSoftSec==softSec &&
207  (thisSoftRow==softRow || thisSoftRow==softRow+1))
208  {
209  Pad_array[padrow-1][pad-1].seq[pad_seq].Length += len;
210  }
211  adc_locn +=len;
212  }
213  lastbin = start+len-1;
214  if ((thisEntry&32)==32)
215  {//last sequence ?
216  pad++; // default to next pad in this padrow
217  pad_seq = 0;
218  lastbin = -2; // set lastbin for new pad
219  oldstart=0;
220  }
221  }
222  else
223  { // starting new pad without bit 5 set!
224  LOG_WARN << "2: new pad detected with bit 5 clear!" << endm;
225  LOG_WARN << start << " < " << oldstart << endm;
226  return FALSE;
227  }
228  }
229  } // end for loop over sequences
230 
231  return TRUE;
232 }
233