StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StFtpcRawWriter.cc
1 // $Id: StFtpcRawWriter.cc,v 1.10 2009/11/14 13:18:33 jcs Exp $
2 // $Log: StFtpcRawWriter.cc,v $
3 // Revision 1.10 2009/11/14 13:18:33 jcs
4 // change LOG_INFO messages to LOG_DEBUG messages
5 //
6 // Revision 1.9 2007/01/15 15:02:19 jcs
7 // replace printf, cout and gMesMgr with Logger
8 //
9 // Revision 1.8 2003/09/22 13:14:18 fsimon
10 // Fixed code to eliminate compiler warning
11 //
12 // Revision 1.7 2003/09/02 17:58:16 perev
13 // gcc 3.2 updates + WarnOff
14 //
15 // Revision 1.6 2003/01/29 12:06:34 fsimon
16 // Include switch to enable/disable turning of pad order for ASIC 2 in FTPC E
17 // Was an error on Y2001/2002 DAQ mapping
18 //
19 // Revision 1.5 2002/11/26 02:20:39 perev
20 // iostream added
21 //
22 // Revision 1.4 2002/09/13 13:36:17 fsimon
23 // Include correction for wrong pad numbering in central FEE Card in
24 // each sector of the East FTPC
25 //
26 // Revision 1.1 2000/11/23 10:16:43 hummler
27 // New FTPC slow simulator in pure maker form
28 //
29 //
31 // interface to write out ftpc raw data
32 // used by StFtpcSlowSimulator to write simulated data to tables
33 
34 
35 #include <Stiostream.h>
36 #include "StFtpcRawWriter.hh"
37 #include "StMessMgr.h"
38 
39 StFtpcRawWriter::StFtpcRawWriter(St_fcl_ftpcndx *ftpcndxIn,
40  St_fcl_ftpcsqndx *ftpcsqndxIn,
41  St_fcl_ftpcadc *ftpcadcIn,
42  const int inAsic2EastNotInverted)
43 {
44  ftpcndx=ftpcndxIn;
45  ftpcsqndx=ftpcsqndxIn;
46  ftpcadc=ftpcadcIn;
47  mAsic2EastNotInverted = inAsic2EastNotInverted;
48 
49  ndx=ftpcndx->GetTable();
50  numNdx=ftpcndx->GetNRows();
51  maxNdx=ftpcndx->GetTableSize();
52  sqndx=ftpcsqndx->GetTable();
53  numSqndx=ftpcsqndx->GetNRows();
54  maxSqndx=ftpcsqndx->GetTableSize();
55  adc=ftpcadc->GetTable();
56  numAdc=ftpcadc->GetNRows();
57  maxAdc=ftpcadc->GetTableSize();
58  LOG_INFO << "FTPC RawWriter created with Asic2EastNotInverted = " << mAsic2EastNotInverted <<endm;
59 }
60 
61 StFtpcRawWriter::~StFtpcRawWriter()
62 {
63  ftpcndx->SetNRows(numNdx);
64  ftpcsqndx->SetNRows(numSqndx);
65  ftpcadc->SetNRows(numAdc);
66 
67 
68  //ftpcsqndx->Print(0, ftpcsqndx->GetNRows());
69  //ftpcadc->Print(0, ftpcadc->GetNRows());
70 }
71 
72 int StFtpcRawWriter::writeArray(float *array,
73  int numberPadrows,
74  int numberSectors,
75  int numberPads,
76  int numberTimebins,
77  int threshold)
78 {
79  int pixel_per_seq=0;
80  int start_pixel=0;
81  int seq_index=0;
82  int adc_index=0;
83  int seq_flag=0;
84  int pad_flag;
85  int last_pad;
86 
87 
88  float *cArray = new float[numberPadrows
89  *numberSectors
90  *numberPads
91  *numberTimebins];
92 
93 
94  // LOG_DEBUG << " Creating copy array "<<numberPadrows<<"; "<<numberSectors<<"; "<<numberPads<<"; "<<numberTimebins<<endm;
95 
96 
97  // Correct different pad & sector numbering scheme for east & west FTPC
98 
99  for (int row=0; row<numberPadrows; row++) {
100  for (int sec=0; sec<numberSectors; sec++) {
101  for (int pad=0; pad<numberPads; pad++) {
102  for (int bin=0; bin<numberTimebins; bin++) {
103  int newi;
104  int i=bin
105  +numberTimebins*pad
106  +numberTimebins*numberPads*sec
107  +numberTimebins*numberPads*numberSectors*row;
108  if (row>=10)
109  if (mAsic2EastNotInverted && (pad>63)&&(pad<96)) // no turning for center FEE card in each sector for old data (prior to 2003)
110  newi=bin // Turning for simulator db access in StFtpcSlowSimReadout::GetHardPad
111  +numberTimebins*pad
112  +numberTimebins*numberPads*sec
113  +numberTimebins*numberPads*numberSectors*row;
114  else
115  newi=bin
116  +numberTimebins*(numberPads-pad-1)
117  +numberTimebins*numberPads*sec
118  +numberTimebins*numberPads*numberSectors*row;
119  else
120  newi=bin
121  +numberTimebins*(numberPads-pad-1)
122  +numberTimebins*numberPads*(numberSectors-sec-1)
123  +numberTimebins*numberPads*numberSectors*row;
124  cArray[newi]=array[i];
125  //if (array[i] >0.1) LOG_DEBUG << "Copying value "<< array[i] <<" for pad "<<pad << " Sec: " << sec <<" Row: "<< row <<endm;
126  }
127  }
128  }
129  }
130 
131  Double_t th_high = 5;
132  Double_t th_low = 2;
133  Int_t minAboveLow = 2;
134 
135 
136 
137  Int_t valid_seq = 0;
138  Int_t nAboveLow = 0;
139  Int_t highReached = 0;
140  pixel_per_seq=0;
141  start_pixel=0;
142  seq_index=0;
143  adc_index=0;
144  seq_flag=0;
145 
146  //LOG_DEBUG << "RawWriter using threshold high:" << th_high << " and threshold low "<< th_low<<endm;
147 
148 
149  // Fill sequences
150 
151  ndx[0].index=0;
152  for (int row=0; row<numberPadrows; row++) {
153  for (int sec=0; sec<numberSectors; sec++) {
154  last_pad=-100;
155  for (int pad=0; pad<numberPads; pad++) {
156  pad_flag=0;
157 
158  for (int bin=0; bin<numberTimebins; bin++) {
159  int i=bin
160  +numberTimebins*pad
161  +numberTimebins*numberPads*sec
162  +numberTimebins*numberPads*numberSectors*row;
163  //if (cArray[i] >1) LOG_DEBUG << "Table value "<< cArray[i] <<" for pad "<<pad << " Sec: " << sec <<" Row: "<< row <<endm;
164 
165  if((int) cArray[i] < th_low) {
166  valid_seq = 0;
167  nAboveLow = 0;
168  highReached = 0;
169  }
170 
171  if((int) cArray[i] >= th_low) {
172 
173  //LOG_DEBUG <<"Over threshold, seq_index = "<<seq_index <<endm;
174 
175  // Check if sequence is valid, apply ASIC parameters
176  if (!valid_seq){
177  for (int t = bin; t < numberTimebins; t ++) {
178  int s = t
179  +numberTimebins*pad
180  +numberTimebins*numberPads*sec
181  +numberTimebins*numberPads*numberSectors*row;
182  if (cArray[s]>= th_low) nAboveLow++;
183  if (cArray[s]>= th_high) highReached = 1;
184  if (highReached && (nAboveLow >= minAboveLow)) {
185  valid_seq = 1;
186  t = numberTimebins;
187  }
188  if (cArray[s]< th_low) {
189  valid_seq=0;
190  bin = t;
191  t = numberTimebins;
192  }
193  }
194  //if (valid_seq) LOG_DEBUG << "Sequence "<<seq_index<< " validated! Pad " << pad << " nAboveLow "<<nAboveLow <<endm;
195  }
196 
197 
198  if (!valid_seq) continue;
199 
200  // beginning of second FTPC?
201  if(row>=10 && ndx[1].index==0) {
202  if(maxNdx<2) {
203  LOG_WARN << "ndx overflow!" << endm;
204  }
205  else {
206  // set index for second FTPC
207  ndx[1].index=seq_index;
208  }
209  }
210 
211  // beginning of new sequence?
212  if(seq_flag == 0 || pixel_per_seq == 32) {
213  // is there a previous sequence?
214  if(pixel_per_seq > 0) {
215  // set index for previous sequence
216  sqndx[seq_index++].index = (pixel_per_seq-1) + (start_pixel<<6);
217  //LOG_DEBUG << "sqndx[" << seq_index-1 << "].index=" << sqndx[seq_index-1].index << endm;
218  //LOG_DEBUG << "pixel_per_seq=" << pixel_per_seq << " start_pixel=" << start_pixel << endm;
219  if(seq_index >= maxSqndx) {
220  // reset overflow
221  seq_index=maxSqndx -1;
222  LOG_WARN << "sqndx overflow!" << endm;
223  }
224 
225  pixel_per_seq=0;
226 
227  }
228 
229  // set beginning of this sequence
230  start_pixel=bin;
231 
232  // first sequence on pad?
233  if(pad_flag == 0) {
234  // make previous sequence last sequence on pad
235  if(seq_index>0) {
236  sqndx[seq_index-1].index += 32;
237  //LOG_DEBUG << "changed: sqndx[" << seq_index-1 << "].index=" << sqndx[seq_index-1].index << endm;
238  }
239  if(pad != last_pad+1) {
240  // set index for new pad
241  sqndx[seq_index++].index = 32768 + pad + 256*(6*row + sec);
242  //LOG_DEBUG << "sqndx[" << seq_index-1 << "].index=" << sqndx[seq_index-1].index << endm;
243  if(seq_index >= maxSqndx) {
244  // reset overflow
245  seq_index=maxSqndx;
246  LOG_WARN << "sqndx overflow!" << endm;
247  }
248  }
249  last_pad=pad;
250  }
251  }
252 
253 
254  adc[adc_index++].data=(char) cArray[i];
255  //LOG_DEBUG << "adc[" << adc_index-1 << "].data=" << (int) adc[adc_index-1].data << " at " <<row << " " << sec << " " << pad << " " << bin << endm;
256  if(adc_index >= maxAdc) {
257  // reset overflow
258  adc_index=maxAdc -1;
259  LOG_WARN << "adc overflow!" << endm;
260  }
261  pixel_per_seq++;
262  seq_flag=1;
263  pad_flag=1;
264  }
265  else {
266  seq_flag=0;
267  }
268  }
269  }
270  }
271  }
272 
273  // Delete copy array
274  delete[] cArray;
275 
276 
277  // is there a sequence?
278  if(pixel_per_seq > 0) {
279  // set index for last sequence
280  sqndx[seq_index++].index = (pixel_per_seq-1) + (start_pixel<<6)+32;
281 
282  // set table lengths
283  numNdx = 2;
284  numSqndx = seq_index;
285  numAdc = adc_index;
286  }
287  else {
288  LOG_ERROR << "Error! No sequences filled!" << endm;
289  }
290 
291  return 1;
292 }
293 
294