StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
TOF_Reader.cxx
1 /***************************************************************************
2 * $Id: TOF_Reader.cxx,v 2.6 2007/12/24 06:04:29 fine Exp $
3 * Author: Frank Geurts
4 ***************************************************************************
5 * Description: TOF Event Reader
6 ***************************************************************************
7 * $Log: TOF_Reader.cxx,v $
8 * Revision 2.6 2007/12/24 06:04:29 fine
9 * introduce OLDEVP namespace to allow ole and new EVP library concurrently
10 *
11 * Revision 2.5 2007/04/17 23:00:16 dongx
12 * replaced with standard STAR Loggers
13 *
14 * Revision 2.4 2005/04/13 16:02:38 dongx
15 * update for a warning message on the return value
16 *
17 * Revision 2.3 2005/04/12 17:22:36 dongx
18 * Update for year 5 new data format, written by Jing Liu.
19 * Previous interfaces are separated out for convenience.
20 *
21 * Revision 2.2 2004/08/07 02:43:32 perev
22 * more test for corruption added
23 *
24 * Revision 2.1 2004/01/28 02:47:45 dongx
25 * change for year4 run (pVPD+TOFp+TOFr')
26 * - Addtional TOFr' ADCs and TDCs put in
27 * - Add TOTs of TOFr' in, combined in TDCs
28 *
29 * Revision 2.0 2003/01/29 05:27:24 geurts
30 * New TOF reader capable of reading TOF year3 data (pVPD, TOFp and TOFr).
31 * - Added dedicated retrieval methods for different parts of the data.
32 * - Reader is still capable of dealing year2 (pVPD and TOFp) data.
33 *
34 * Revision 1.2 2001/09/28 18:45:43 llope
35 * modified for compatibility w/ updated StTofMaker
36 *
37 * Revision 1.1 2001/07/08 21:41:58 geurts
38 * Revision 1.2 2001/09/28 13:13:13 llope
39 * First release
40 *
41 **************************************************************************/
42 #include <assert.h>
43 #include "TOF_Reader.hh"
44 #include "StMessMgr.h"
45 #include "TString.h"
46 
47 using namespace OLDEVP;
48 
49 bool TOF_Reader::year2Data(){return (mTofRawDataVersion==1);}
50 bool TOF_Reader::year3Data(){return (mTofRawDataVersion==2);}
51 bool TOF_Reader::year4Data(){return (mTofRawDataVersion==3);}
52 //Jing Liu
53 bool TOF_Reader::year5Data(){return (mTofRawDataVersion==0);}
54 
55 void TOF_Reader::ProcessEvent(const Bank_TOFP * TofPTR) {
56 
57  int unpackerr=0;
58  if(year2Data()||year3Data()||year4Data()) {
59  unpackerr=UnpackYear2to4Data(TofPTR);
60  if(unpackerr>0) LOG_INFO<<"TOF_READER::UnPack Year2-4 data ERROR!"<<endm;
61  }
62 
63  if(year5Data()){
64  unpackerr=UnpackYear5Data(TofPTR);
65  if(unpackerr>0) LOG_INFO<<"TOF_READER::UnPack Year5 Data ERROR!"<<endm;
66  }
67 
68 }
69 
70 
71 TOF_Reader::TOF_Reader(EventReader *er, Bank_TOFP *pTOFP)
72 {
73  pBankTOFP = pTOFP;
74  ercpy = er;
75  if (!pBankTOFP->test_CRC()) {
76  LOG_DEBUG << Form("CRC error in TOFP: %s %d",__FILE__,__LINE__) << endm;
77  }
78  if (pBankTOFP->swap() < 0) {
79  LOG_DEBUG << Form("swap error in TOFP: %s %d",__FILE__,__LINE__) << endm;
80  }
81 
82  pBankTOFP->header.CRC = 0;
83  int Token = pBankTOFP->header.Token;
84  Bank_DATAP *dp = (Bank_DATAP *)ercpy->getDATAP();
85  if(Token !=dp->header.Token){
86  LOG_DEBUG << Form("Token mismatch between global %d and TOF %d",dp->header.Token,Token) << endm;
87  }
88 
89  mTofRawDataVersion = pBankTOFP->header.FormatNumber;
90 
91  // Year2: pVPD+TOFp; Year3:pVPD+TOFp+TOFr(72); Year4: pVPD+TOFp+TOFr'(120)
92  if (year2Data()){
93  mMaxAdcChannels=48;
94  mMaxTdcChannels=48;
95  }
96  else if (year3Data()) {
97  mMaxAdcChannels=48+12+72;
98  mMaxTdcChannels=48+72;
99  }
100  else {
101  mMaxAdcChannels=TOF_MAX_ADC_CHANNELS;
102  mMaxTdcChannels=TOF_MAX_TDC_CHANNELS;
103  }
104  mMaxScaChannels = TOF_MAX_SCA_CHANNELS;
105  mMaxA2dChannels = TOF_MAX_A2D_CHANNELS;
106 
107  ProcessEvent(pBankTOFP);
108 }
109 
110 
111 unsigned short TOF_Reader::GetAdc(int daqId){
112  if ((daqId<0) || (daqId>mMaxAdcChannels-1)){
113  LOG_INFO << "TOF_Reader::GetAdc slatId out of range " << daqId << endm;
114  return 0;
115  }
116  return mTheTofArray.AdcData[daqId];
117 }
118 unsigned short TOF_Reader::GetAdcFromSlat(int slatId){return GetAdc(slatId);}
119 
120 
121 unsigned short TOF_Reader::GetTdc(int daqId){
122  if ((daqId<0) || (daqId>mMaxTdcChannels-1)){
123  LOG_INFO << "TOF_Reader::GetTdc daqId out of range " << daqId << endm;
124  return 0;
125  }
126  return mTheTofArray.TdcData[daqId];
127 }
128 
129 unsigned short TOF_Reader::GetTdcFromSlat(int slatId){return GetTdc(slatId);}
130 
131 
132 unsigned short TOF_Reader::GetTofpAdc(int slatId){
133  if ((slatId<0) || (slatId>41)){
134  LOG_INFO << "TOF_Reader::GetTofpAdc slatId out of range " << slatId << endm;
135  return 0;
136  }
137  return mTheTofArray.AdcData[slatId];
138 }
139 
140 
141 unsigned short TOF_Reader::GetTofpTdc(int slatId){
142  if ((slatId<0) || (slatId>41)){
143  LOG_INFO << "TOF_Reader::GetTofpTdc slatId out of range " << slatId << endm;
144  return 0;
145  }
146  return mTheTofArray.TdcData[slatId];
147 }
148 
149 
150 unsigned short TOF_Reader::GetTofrAdc(int padId){
151  if (year2Data()){
152  LOG_INFO << "TOF_Reader: TOFr ADC data not available for year2 data" << endm;
153  return 0;
154  }
155  if (year3Data()) {
156  if((padId<0) || (padId>71)) {
157  LOG_INFO << "TOF_Reader::GetTofrAdc padId out of range " << padId << endm;
158  return 0;
159  }
160  }
161  if (year4Data()) {
162  if ((padId<0) || (padId>119)){
163  LOG_INFO << "TOF_Reader::GetTofrAdc padId out of range " << padId << endm;
164  return 0;
165  }
166  }
167  return mTheTofArray.AdcData[60+padId];
168 }
169 
170 
171 unsigned short TOF_Reader::GetTofrTdc(int padId){
172  if (year2Data()){
173  LOG_INFO << "TOF_Reader: TOFr TDC data not available for year2 data" << endm;
174  return 0;
175  }
176  if (year3Data()) {
177  if((padId<0) || (padId>71)) {
178  LOG_INFO << "TOF_Reader::GetTofrTdc padId out of range " << padId << endm;
179  return 0;
180  }
181  return mTheTofArray.TdcData[48+padId];
182  }
183  if (year4Data()) {
184  if ((padId<0) || (padId>119)){
185  LOG_INFO << "TOF_Reader::GetTofrTdc padId out of range " << padId << endm;
186  return 0;
187  }
188  int offset = 0;
189  if(padId<24) {
190  offset = 0;
191  } else if(padId<48) {
192  offset = 2;
193  } else if(padId<72) {
194  offset = 4;
195  } else {
196  offset = 8;
197  }
198  return mTheTofArray.TdcData[48+offset+padId];
199  }
200  assert(0); return 0;
201 }
202 
203 unsigned short TOF_Reader::GetTofrTOT(int totId){
204  if(year2Data()||year3Data()) {
205  LOG_INFO << "TOF_Reader:: TOFr TOT data not available for year3 and year3 data" << endm;
206  return 0;
207  }
208  if((totId<0) || (totId>9)) {
209  LOG_INFO << "TOF_Reader::GetTofrTOT totId out of range " << totId << endm;
210  }
211  unsigned short tdcId[10] = {72, 73, 98, 99, 124, 125, 176, 177, 178, 179};
212 
213  return mTheTofArray.TdcData[tdcId[totId]];
214 }
215 
216 unsigned short TOF_Reader::GetPvpdAdc(int pvpdId){
217  if ((pvpdId<0) || (pvpdId>5)){
218  LOG_INFO << "TOF_Reader::GetPvpdAdc pvpdId out of range " << pvpdId << endm;
219  return 0;
220  }
221  return mTheTofArray.AdcData[43+pvpdId];
222 }
223 
224 
225 unsigned short TOF_Reader::GetPvpdAdcHigh(int pvpdId){
226  if (year2Data()){
227  LOG_INFO << "TOF_Reader: pVPD high gain data not available for year2 data" << endm;
228  return 0;
229  }
230  if ((pvpdId<0) || (pvpdId>5)){
231  LOG_INFO << "TOF_Reader::GetPvpdAdcHigh pvpdId out of range " << pvpdId << endm;
232  return 0;
233  }
234  return mTheTofArray.TdcData[54+pvpdId];
235 }
236 
237 unsigned short TOF_Reader::GetPvpdTdc(int pvpdId){
238  if ((pvpdId<0) || (pvpdId>5)){
239  LOG_INFO << "TOF_Reader::GetPvpdTdc pvpdId out of range " << pvpdId << endm;
240  return 0;
241  }
242  return mTheTofArray.TdcData[43+pvpdId];
243 }
244 
245 unsigned short TOF_Reader::GetClockAdc(){return mTheTofArray.AdcData[42];}
246 
247 
248 short TOF_Reader::GetTc(int chanId){
249  if ((chanId<0) || (chanId>mMaxA2dChannels-1)){
250  LOG_INFO << "TOF_Reader::GetTc chanId out of range " << chanId << endm;
251  return 0;
252  }
253  return mTheTofArray.A2dData[chanId];
254 }
255 
256 
257 unsigned short TOF_Reader::GetSc(int chanId){
258  if ((chanId<0) || (chanId>mMaxScaChannels-1)){
259  LOG_INFO << "TOF_Reader::GetSc chanId out of range " << chanId << endm;
260  return 0;
261  }
262  return mTheTofArray.ScaData[chanId];
263 }
264 
265 
266 unsigned int TOF_Reader::GetEventNumber(){
267  return mTheTofArray.EventNumber;
268 }
269 
270 void TOF_Reader::printRawData(){
271  LOG_INFO << "StDaqLib/TOF/TOF_Reader Printing Raw Data...";
272  LOG_INFO << "\n --- ADCD: ";
273  for (int i=0;i<mMaxAdcChannels;i++) LOG_INFO << " " << mTheTofArray.AdcData[i];
274  LOG_INFO << "\n --- TDCD: ";
275  for (int i=0;i<mMaxTdcChannels;i++) LOG_INFO << " " << mTheTofArray.TdcData[i];
276  LOG_INFO << "\n --- A2DD: ";
277  for (int i=0;i<mMaxA2dChannels;i++) LOG_INFO << " " << mTheTofArray.A2dData[i];
278  LOG_INFO << "\n --- SCAD: ";
279  for (int i=0;i<mMaxScaChannels;i++) LOG_INFO << " " << mTheTofArray.ScaData[i];
280  LOG_INFO << "\nStDaqLib/TOF/TOF_Reader Done Printing Raw Data..." << endm;
281 }
282 
283 
284 //Jing Liu, FY05--- tofr5-------------------
285 unsigned int TOF_Reader::GetLdTdc(int daqId){
286  if ((daqId<0) || (daqId>mMaxTdcChannels-1)){
287  LOG_INFO << "TOF_Reader::GetTdc daqId out of range " << daqId << endm;
288  return 0;
289  }
290  return mTheTofArray.LdTdcData[daqId];
291 }
292 
293 unsigned int TOF_Reader::GetTrTdc(int daqId){
294  if ((daqId<0) || (daqId>mMaxTdcChannels-1)){
295  LOG_INFO << "TOF_Reader::GetTdc daqId out of range " << daqId << endm;
296  return 0;
297  }
298  return mTheTofArray.TrTdcData[daqId];
299 }
300 
301 unsigned int TOF_Reader::GetLdmTdc(int daqId,int n){
302  if ((daqId<0) || (daqId>mMaxTdcChannels-1)){
303  LOG_INFO << "TOF_Reader::GetTdc daqId out of range " << daqId << endm;
304  return 0;
305  }
306  int chan=0;
307  for(unsigned int i=0;i<mTheTofArray.TofLeadingHits.size();i++){
308  chan = mTheTofArray.TofLeadingHits[i].globaltdcchan;
309  if(chan == daqId) break;
310  }
311  return mTheTofArray.TofLeadingHits[chan+n].tdc;
312 }
313 
314 unsigned int TOF_Reader::GetTrmTdc(int daqId,int n){
315  if ((daqId<0) || (daqId>mMaxTdcChannels-1)){
316  LOG_INFO << "TOF_Reader::GetTdc daqId out of range " << daqId << endm;
317  return 0;
318  }
319  int chan=0;
320  for(unsigned int i=0;i<mTheTofArray.TofTrailingHits.size();i++){
321  chan = mTheTofArray.TofTrailingHits[i].globaltdcchan;
322  if(chan == daqId) break;
323  }
324  return mTheTofArray.TofTrailingHits[chan+n].tdc;
325 }
326 
327 unsigned short TOF_Reader::GetNLdHits(int daqId){
328  if ((daqId<0) || (daqId>mMaxTdcChannels-1)){
329  LOG_INFO << "TOF_Reader::GetTdc daqId out of range " << daqId << endm;
330  return 0;
331  }
332  return mTheTofArray.LdNHit[daqId];
333 }
334 
335 unsigned short TOF_Reader::GetNTrHits(int daqId){
336  if ((daqId<0) || (daqId>mMaxTdcChannels-1)){
337  LOG_INFO << "TOF_Reader::GetTdc daqId out of range " << daqId << endm;
338  return 0;
339  }
340  return mTheTofArray.TrNHit[daqId];
341 }
342 
343 unsigned int TOF_Reader::GetPvpdLdTdc(int pvpdId){
344  if ((pvpdId<0) || (pvpdId>5)){
345  LOG_INFO << "TOF_Reader::GetPvpdTdc pvpdId out of range " << pvpdId << endm;
346  return 0;
347  }
348  return mTheTofArray.LdTdcData[192+pvpdId];
349 }
350 
351 unsigned int TOF_Reader::GetPvpdTrTdc(int pvpdId){
352  if ((pvpdId<0) || (pvpdId>5)){
353  LOG_INFO << "TOF_Reader::GetPvpdTdc pvpdId out of range " << pvpdId << endm;
354  return 0;
355  }
356  return mTheTofArray.TrTdcData[192+pvpdId];
357 }
358 
359 unsigned int TOF_Reader::GetPvpdLdmTdc(int daqId,int n){
360  if ((daqId<0) || (daqId>5)){
361  LOG_INFO << "TOF_Reader::GetPvpdTdc daqId out of range " << daqId << endm;
362  return 0;
363  }
364  int chan=0;
365  for(unsigned int i=0;i<mTheTofArray.TofLeadingHits.size();i++){
366  chan = mTheTofArray.TofLeadingHits[i].globaltdcchan;
367  if(chan-192 == daqId) break;
368  }
369  return mTheTofArray.TofLeadingHits[chan+n].tdc;
370 }
371 
372 unsigned int TOF_Reader::GetPvpdTrmTdc(int daqId,int n){
373  if ((daqId<0) || (daqId>5)){
374  LOG_INFO << "TOF_Reader::GetPvpdTdc daqId out of range " << daqId << endm;
375  return 0;
376  }
377  int chan=0;
378  for(unsigned int i=0;i<mTheTofArray.TofTrailingHits.size();i++){
379  chan = mTheTofArray.TofTrailingHits[i].globaltdcchan;
380  if(chan-192 == daqId) break;
381  }
382  return mTheTofArray.TofTrailingHits[chan+n].tdc;
383 }
384 
385 unsigned int TOF_Reader::GetNLeadingHits() {
386  return (unsigned int)(mTheTofArray.TofLeadingHits.size());
387 }
388 
389 unsigned int TOF_Reader::GetLeadingEventNumber(int ihit) {
390  if(ihit<0 || ihit >= (int) mTheTofArray.TofLeadingHits.size()) return 0;
391  else
392  return mTheTofArray.TofLeadingHits[ihit].EventNumber;
393 }
394 
395 unsigned short TOF_Reader::GetLeadingFiberId(int ihit) {
396  if(ihit<0 || ihit >= (int )mTheTofArray.TofLeadingHits.size()) return 9999;
397  else
398  return mTheTofArray.TofLeadingHits[ihit].fiberid;
399 }
400 
401 unsigned short TOF_Reader::GetLeadingGlobalTdcChan(int ihit) {
402  if(ihit<0 || ihit >= (int) mTheTofArray.TofLeadingHits.size()) return 9999;
403  else
404  return mTheTofArray.TofLeadingHits[ihit].globaltdcchan;
405 }
406 
407 unsigned int TOF_Reader::GetLeadingTdc(int ihit) {
408  if(ihit<0 || ihit >= (int) mTheTofArray.TofLeadingHits.size()) return 0;
409  else
410  return mTheTofArray.TofLeadingHits[ihit].tdc;
411 }
412 
413 unsigned int TOF_Reader::GetNTrailingHits() {
414  return (unsigned int)(mTheTofArray.TofTrailingHits.size());
415 }
416 
417 unsigned int TOF_Reader::GetTrailingEventNumber(int ihit) {
418  if(ihit<0 || ihit >= (int) mTheTofArray.TofTrailingHits.size()) return 0;
419  else
420  return mTheTofArray.TofTrailingHits[ihit].EventNumber;
421 }
422 
423 unsigned short TOF_Reader::GetTrailingFiberId(int ihit) {
424  if(ihit<0 || ihit >= (int) mTheTofArray.TofTrailingHits.size()) return 9999;
425  else
426  return mTheTofArray.TofTrailingHits[ihit].fiberid;
427 }
428 
429 unsigned short TOF_Reader::GetTrailingGlobalTdcChan(int ihit) {
430  if(ihit<0 || ihit >= (int) mTheTofArray.TofTrailingHits.size()) return 9999;
431  else
432  return mTheTofArray.TofTrailingHits[ihit].globaltdcchan;
433 }
434 
435 unsigned int TOF_Reader::GetTrailingTdc(int ihit) {
436  if(ihit<0 || ihit >= (int) mTheTofArray.TofTrailingHits.size()) return 0;
437  else
438  return mTheTofArray.TofTrailingHits[ihit].tdc;
439 }
440 
441 int TOF_Reader::UnpackYear2to4Data(const Bank_TOFP * TofPTR) {
442 
443  unsigned short numberOfDataWords, slot, channel;
444  int dataDWord, value;
445  //TofPTR->print();
446  unsigned short Token = TofPTR->header.Token;
447  if (Token==0){
448  LOG_INFO << "TOF_Reader: do not know how to handle token==0"<<endm;
449  //return;
450  }
451  mTheTofArray.EventNumber = Token;
452 // LOG_INFO << " Token = " << Token << endm;
453  mTheTofArray.ByteSwapped = 0x04030201;
454 
455  int tofRawDataVersion = TofPTR->header.FormatNumber;
456 // LOG_INFO << " Raw Data Versions = " << tofRawDataVersion << endm;
457 
458  if ((tofRawDataVersion <1) || (tofRawDataVersion >3)){
459  LOG_INFO << "TOF_Reader: ERROR unknown raw data version " << tofRawDataVersion << endm;
460  return 1;
461  }
462 
463  //fg 1. introduce rawdata consistency checks below ...
464 
465  //Read ADC Bank
466 
467  TOFADCD * TofAdcD;
468  if (TofPTR->AdcPTR.length>0) {
469  TofAdcD = (TOFADCD *) ((unsigned long *)TofPTR + TofPTR->AdcPTR.offset);
470  //TofAdcD->print();
471  TofAdcD->swap();
472  if (TofAdcD->header.Token!=Token){
473  LOG_INFO << "TOF_Reader: Token mismatch TOFP "<< Token
474  << " ADCD " << TofAdcD->header.Token << endm;
475  mTheTofArray.EventNumber=0;
476  }
477  numberOfDataWords=TofAdcD->header.BankLength - (INT32)sizeof(TofAdcD->header)/4;
478  if (numberOfDataWords!=mMaxAdcChannels){
479  LOG_INFO << "TOF_Reader: ADCD #channels mismatch " << numberOfDataWords << endm;
480  if (numberOfDataWords>mMaxAdcChannels) numberOfDataWords=mMaxAdcChannels;
481  }
482  // decode and fill data structure
483  for (dataDWord=0; dataDWord < numberOfDataWords; dataDWord++) {
484  //slot = (int)TofAdcD->data[dataDWord].adc.slot;
485  //channel = (int)TofAdcD->data[dataDWord].adc.channel;
486  //value = (int)TofAdcD->data[dataDWord].adc.data;
487  slot = int( TofAdcD->data[dataDWord].data & 0x000000FF);
488  channel = int((TofAdcD->data[dataDWord].data & 0x0000FF00) >> 8);
489  value = int((TofAdcD->data[dataDWord].data & 0xFFFF0000) >> 16);
490  mTheTofArray.AdcData[dataDWord]=value;
491  }
492  }
493 
494  //Read TDC Bank
495  TOFTDCD * TofTdcD;
496  if (TofPTR->TdcPTR.length>0) {
497  TofTdcD = (TOFTDCD *) ((unsigned long *)TofPTR + TofPTR->TdcPTR.offset);
498  //TofTdcD->print();
499  TofTdcD->swap();
500  if (TofTdcD->header.Token!=Token){
501  LOG_INFO << "TOF_Reader: Token mismatch TOFP "<< Token
502  << " TDCD " << TofTdcD->header.Token << endm;
503  mTheTofArray.EventNumber=0;
504  }
505  numberOfDataWords=TofTdcD->header.BankLength - (INT32)sizeof(TofTdcD->header)/4;
506  if (numberOfDataWords!=mMaxTdcChannels){
507  LOG_INFO << "TOF_Reader: TDCD #channels mismatch " << numberOfDataWords << endm;
508  if (numberOfDataWords>mMaxTdcChannels) numberOfDataWords=mMaxTdcChannels;
509  }
510  // decode and fill data structure
511  for (dataDWord=0; dataDWord < numberOfDataWords; dataDWord++) {
512  //slot = (int)TofTdcD->data[dataDWord].tdc.slot;
513  //channel = (int)TofTdcD->data[dataDWord].tdc.channel;
514  //value = (int)TofTdcD->data[dataDWord].tdc.data;
515  slot = int( TofTdcD->data[dataDWord].data & 0x000000FF);
516  channel = int((TofTdcD->data[dataDWord].data & 0x0000FF00) >> 8);
517  value = int((TofTdcD->data[dataDWord].data & 0xFFFF0000) >> 16);
518  mTheTofArray.TdcData[dataDWord]=value;
519  }
520  }
521 
522  //Read A2D Bank...
523  TOFA2DD * TofA2dD;
524  if (TofPTR->A2dPTR.length>0) {
525  TofA2dD = (TOFA2DD *) ((unsigned long *)TofPTR + TofPTR->A2dPTR.offset);
526  //TofA2dD->print();
527  TofA2dD->swap();
528  if (TofA2dD->header.Token!=Token){
529  LOG_INFO << "TOF_Reader: Token mismatch TOFP "<< Token
530  << " A2DD " << TofA2dD->header.Token << endm;
531  mTheTofArray.EventNumber=0;
532  }
533  numberOfDataWords=TofA2dD->header.BankLength - (INT32)sizeof(TofA2dD->header)/4;
534  if (numberOfDataWords!=mMaxA2dChannels){
535  LOG_INFO << "TOF_Reader: A2DD #channels mismatch " << numberOfDataWords << endm;
536  if (numberOfDataWords>mMaxA2dChannels) numberOfDataWords=mMaxA2dChannels;
537  }
538  // decode and fill data structure
539  for (dataDWord=0; dataDWord < numberOfDataWords; dataDWord++) {
540  //slot = (int)TofA2dD->data[dataDWord].a2d.slot;
541  //channel = (int)TofA2dD->data[dataDWord].a2d.channel;
542  //value = (int)TofA2dD->data[dataDWord].a2d.data;
543  slot = int( TofA2dD->data[dataDWord].data & 0x000000FF);
544  channel = int((TofA2dD->data[dataDWord].data & 0x0000FF00) >> 8);
545  value = int(int(TofA2dD->data[dataDWord].data) >> 16);// A2D values might be negative
546  mTheTofArray.A2dData[dataDWord]=value;
547  }
548  }
549 
550  //Read SCA Bank
551  TOFSCAD * TofScaD;
552  if (TofPTR->ScaPTR.length>0) {
553  TofScaD = (TOFSCAD *) ((unsigned long *)TofPTR + TofPTR->ScaPTR.offset);
554  //TofScaD->print();
555  TofScaD->swap();
556  if (TofScaD->header.Token!=Token){
557  LOG_INFO << "TOF_Reader: Token mismatch TOFP "<< Token
558  << " SCAD " << TofScaD->header.Token << endm;
559  mTheTofArray.EventNumber=0;
560  }
561  numberOfDataWords=TofScaD->header.BankLength - (INT32)sizeof(TofScaD->header)/4;
562  if (numberOfDataWords!=mMaxScaChannels){
563  LOG_INFO << "TOF_Reader: SCAD #channels mismatch " << numberOfDataWords << endm;
564  if (numberOfDataWords>mMaxScaChannels) numberOfDataWords=mMaxScaChannels;
565  }
566  // decode and fill data structure
567  for (dataDWord=0; dataDWord < numberOfDataWords; dataDWord++) {
568  //channel = (int)TofScaD->data[dataDWord].sca.channel;
569  //value = (int)TofScaD->data[dataDWord].sca.data;
570  channel = int( TofScaD->data[dataDWord].data & 0x000000FF);
571  value = int((TofScaD->data[dataDWord].data & 0xFFFFFF00) >> 8);
572  mTheTofArray.ScaData[dataDWord]=value;
573  }
574  }
575  return -1;
576 }
577 
578 int TOF_Reader::UnpackYear5Data(const Bank_TOFP * TofPTR) {
579 
580  //LOG_INFO<<"TOF READER 2005! "<<endm;
581  //LOG_INFO<<"run no "<<ercpy->runno()<<endm;
582  //TofPTR->print();
583 
584  unsigned short Token = TofPTR->header.Token;
585  if (Token==0){
586  LOG_INFO << "TOF_Reader: do not know how to handle token==0"<<endm;
587  //return;
588  }
589  mTheTofArray.EventNumber = Token;
590  // LOG_INFO << " Token = " << Token << endm;
591  mTheTofArray.ByteSwapped = 0x04030201;
592 
593  int tofRawDataVersion = TofPTR->header.FormatNumber;
594 // LOG_INFO << " Raw Data Versions = " << tofRawDataVersion << endm;
595 
596  // run 5 - dongx
597  if ((tofRawDataVersion <0) || (tofRawDataVersion >3)){
598  LOG_INFO << "TOF_Reader: ERROR unknown raw data version " << tofRawDataVersion << endm;
599  //Jing Liu, the value of rawdataversion is 0 now, should change according somewhere....
600  //return;
601  }
602 
603  //fg 1. introduce rawdata consistency checks below ...
604  // Jing Liu, read tofr5 FY05 raw data here!! 02/16/2005.
605  // Initialize raw hits vector first.
606  mTheTofArray.TofLeadingHits.clear();
607  mTheTofArray.TofTrailingHits.clear();
608 
609  for(int ifib=0;ifib<4;ifib++) { // raw data are read out from 4 fibers.
610  if(ifib>2) continue; // for year5 run, only 3 fibers used.
611  //LOG_INFO << " offset = " << TofPTR->DDLRPTR[ifib].offset << endm;
612  //LOG_INFO << " length = " << dec << TofPTR->DDLRPTR[ifib].length-10 << endm;
613  if (TofPTR->DDLRPTR[ifib].length<=0) {
614  LOG_INFO<<"No data words in this fiber! "<<endm;
615  continue;
616  }
617  TOFDDLR *TofDdlr = (TOFDDLR *)((unsigned long *)TofPTR + TofPTR->DDLRPTR[ifib].offset);
618  int nword = TofPTR->DDLRPTR[ifib].length-10;
619  int halftrayid=0;
620  int timeinbin=0;
621 
622  int runnumber = ercpy->runno();
623  int fiboffset1[6]={1,2,3,4,5,6};
624  int fiboffset2[6]={4,5,6,1,2,3}; // we swith fiber 2,3 after run 6055081;
625  int fiboffset[6];
626  if(runnumber<6055081) for(int i=0;i<6;i++){fiboffset[i]=fiboffset1[i];}
627  if(runnumber>=6055081) for(int i=0;i<6;i++){fiboffset[i]=fiboffset2[i];}
628 
629  for (int iword=0;iword<nword;iword++) {
630  //LOG_INFO << hex << TofDdlr->data[iword] << endm;
631  int dataword=TofDdlr->data[iword];
632  // now process data word seperately, get TDC information from data words.
633  if( (dataword&0xF0000000)>>28 == 0xe) continue; // separator words, skip it.
634  if( (dataword&0xF0000000)>>28 == 0xc) halftrayid = dataword&0x01; // get halftray id!
635  // now get tdc chan, time from trailing and leading edge.
636  // some triger words will be skipped.
637  int edgeid =int( (dataword & 0xf0000000)>>28 );
638  if((edgeid !=LEADING) && (edgeid!=TRAILING)) continue; // if not leading edge or trailing data, skip it.
639  int tdcid = (dataword & 0x0f000000)>>24; // tdcid here is 0-15
640  int tdcchan=0;
641  //LOG_INFO<<"ifib="<<ifib<<" dataword=0x"<<hex<<dataword<<endm;
642  if(edgeid == LEADING) { // leading edge data
643  TofRawHit templdhit;
644  templdhit.fiberid=ifib;
645  tdcchan=(dataword&0x00E00000)>>21; // tdcchan is 0-7 here.
646  timeinbin=((dataword&0x7ffff)<<2)+((dataword>>19)&0x03); // time in tdc bin
647  templdhit.tdc=timeinbin;
648  // global channel number here !
649  if(ifib==0)templdhit.globaltdcchan=tdcchan + (tdcid&0x03)*8+(tdcid>>2)*24+halftrayid*96; // 0-191 for tray
650  if(ifib>=1) { // pvpd leading edge read out from tdc 0-chan 0, tdc 1-chan 0, tdc 1-chan 2
651  if(tdcid==0&&tdcchan==0)templdhit.globaltdcchan=191 + fiboffset[0+3*(ifib-1)];
652  if(tdcid==1&&tdcchan==0)templdhit.globaltdcchan=191 + fiboffset[1+3*(ifib-1)];
653  if(tdcid==1&&tdcchan==2)templdhit.globaltdcchan=191 + fiboffset[2+3*(ifib-1)];
654  }
655  mTheTofArray.TofLeadingHits.push_back(templdhit);
656  } else if (edgeid==TRAILING){ // trailing edge data
657  TofRawHit temptrhit;
658  temptrhit.fiberid=ifib;
659  tdcchan=(dataword&0x0F80000)>>19; // tdcchan is 0-23 here.
660  timeinbin = dataword & 0x7ffff;
661  temptrhit.tdc=timeinbin;
662  if(ifib==0)temptrhit.globaltdcchan=tdcchan +(tdcid>>2)*24+halftrayid*96; // 0-191 for tray
663  if(ifib>=1) {
664  if(tdcid==3&&tdcchan==0)temptrhit.globaltdcchan=191 + fiboffset[0+3*(ifib-1)];
665  if(tdcid==3&&tdcchan==8)temptrhit.globaltdcchan=191 + fiboffset[1+3*(ifib-1)];
666  if(tdcid==3&&tdcchan==10)temptrhit.globaltdcchan=191+ fiboffset[2+3*(ifib-1)];
667  }
668  mTheTofArray.TofTrailingHits.push_back(temptrhit);
669  } else {
670  LOG_INFO<<" UNKNOWN TDC data ! "<<endm;
671  return 1;
672  }
673  //LOG_INFO<<"ifib="<<ifib<<" dataword="<<hex<<dataword<<" tdcid="<<tdcid<<" tdcchan="<<tdcchan<<" time="<<dec<<timeinbin<<endm;
674  } // end loop data words
675  } // end loop fibers
676  // dump out info. for check
677 /*
678  for(unsigned int i=0;i<mTheTofArray.TofLeadingHits.size();i++){
679  LOG_INFO<<"leading: fiber="<<mTheTofArray.TofLeadingHits[i].fiberid<<" channel="<<mTheTofArray.TofLeadingHits[i].globaltdcchan<<" time="<<mTheTofArray.TofLeadingHits[i].tdc<<endm;
680  }
681  for(unsigned int i=0;i<mTheTofArray.TofTrailingHits.size();i++){
682  LOG_INFO<<"trailing: fiber="<<mTheTofArray.TofTrailingHits[i].fiberid<<" channel="<<mTheTofArray.TofTrailingHits[i].globaltdcchan<<" time="<<mTheTofArray.TofTrailingHits[i].tdc<<endm;
683  }
684 */
685  // count multi hit for each channels
686  for(unsigned int i=0;i<TOF_MAX_TDC_CHANNELS;i++){mTheTofArray.LdNHit[i]=0;mTheTofArray.LdTdcData[i]=0;}
687  for(unsigned int i=0;i<TOF_MAX_TDC_CHANNELS;i++){mTheTofArray.TrNHit[i]=0;mTheTofArray.TrTdcData[i]=0;}
688 
689  for(unsigned int i=0;i<mTheTofArray.TofLeadingHits.size();i++){
690  int chan = mTheTofArray.TofLeadingHits[i].globaltdcchan;
691  if(chan <=0) continue; // possible?
692  mTheTofArray.LdNHit[chan]++;
693  }
694  for(unsigned int i=0;i<mTheTofArray.TofTrailingHits.size();i++){
695  int chan = mTheTofArray.TofTrailingHits[i].globaltdcchan;
696  if(chan <=0) continue; // possible?
697  mTheTofArray.TrNHit[chan]++;
698  }
699  // Use only the first LE and TE time in the fixed array!!!!
700  int oldchan=0;
701  for(unsigned int i=0;i<mTheTofArray.TofLeadingHits.size();i++){
702  int chan = mTheTofArray.TofLeadingHits[i].globaltdcchan;
703  if(chan == oldchan) continue;
704  mTheTofArray.LdTdcData[chan]=mTheTofArray.TofLeadingHits[i].tdc;
705  oldchan=chan;
706  }
707  oldchan=0;
708  for(unsigned int i=0;i<mTheTofArray.TofTrailingHits.size();i++){
709  int chan = mTheTofArray.TofTrailingHits[i].globaltdcchan;
710  if(chan == oldchan) continue;
711  mTheTofArray.TrTdcData[chan]=mTheTofArray.TofTrailingHits[i].tdc;
712  oldchan=chan;
713  }
714  /*
715  for(int i=0;i<TOF_MAX_TDC_CHANNELS;i++){
716  if(mTheTofArray.LdTdcData[i]>0)LOG_INFO<<" ld chan ="<<i<<" time="<<mTheTofArray.LdTdcData[i]<<endm;
717  if(mTheTofArray.TrTdcData[i]>0)LOG_INFO<<" tr chan ="<<i<<" time="<<mTheTofArray.TrTdcData[i]<<endm;
718  }
719  */
720  return -1;
721 }
722 
723 //Jing Liu, end-----------------------
unsigned char globaltdcchan
1,2,......,120,for tray, 121, 122 for upvpd
unsigned char fiberid
data word before unpack