StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
code2007.cxx
1 // This is 2007 specific code for restoring packed(zero suppressed) trigger
2 // data from daq file to full size trigger structure, and byte swap if needed.
3 
4 #include <assert.h>
5 #include "trgStructures2007.h"
6 #include "TRG_Reader.hh"
7 using namespace OLDEVP;
8 
9 int TRG_Reader::UnpackTrg2007(Bank_TRGP *pTRGP){
10  int returnValue,npre,npost,swap,res;
11 
12  if ( ! pTRGP){
13  printf("TRG_Reader::UnpackTrg2007 - not pTRGP\n");
14  return -1;
15  }
16 
17  assert(pTRGP->header.ByteOrder==0x01020304 ||
18  pTRGP->header.ByteOrder==0x04030201);
19  if(pTRGP->header.ByteOrder==0x04030201) {
20  swap=1;
21  returnValue=0;
22  }else{
23  swap=1;
24  returnValue=pTRGP->header.swap();
25  }
26  if(pTRGP->header.ByteOrder!=0x04030201){
27  printf("TRG_Reader::UnpackTrg2007: Swap Header error %s %d.\n",__FILE__,__LINE__);
28  assert(0);
29  }
30  printf("TRG_Reader::UnpackTrg2007: Token from Bank=%d\n",pTRGP->header.Token);
31 
32  //Get lengthes
33  // int size_bank = pTRGP->header.BankLength * 4;
34  int size_off = pTRGP->theData.offset * 4;
35  int size_head = sizeof(pTRGP->header);
36  int size_trg = sizeof(TrgDataType2007);
37  int size_desc = sizeof(EvtDescData2007);
38  int size_sum = sizeof(TrgSumData2007);
39  int size_raw = sizeof(RawTrgDet2007);
40  // printf("TRG_Reader::UnpackTrg2007: offset, header, trg, desc, sum, raw = %d %d %d %d %d %d\n",
41  // ,size_off,size_head,size_trg,size_desc,size_sum,size_raw);
42 
43  //Create memory space for unpacked trigger bank
44  if(pBankUnp!=0) delete[] pBankUnp;
45  int sizeUnp = size_off + size_head + size_trg;
46  pBankUnp = new char[sizeUnp];
47  char *trgd = pBankUnp + size_off + size_head; //pointer for trgdata
49 
50  //Copy Header, EvtDesc and TrgSum and byte swap
51  memcpy(pBankUnp, pTRGP, size_off+size_head+size_desc+size_sum);
52 
53  //Swap EvtDesc and TrgSum
54  if(swap){
55  res=Swap2007_DescSum(trgd);
56  if(res<0) {
57  printf("TRG_Reader::UnpackTrg2007: Swap DescSum error %s %d.\n",__FILE__,__LINE__);
58  return -1;
59  }
60  }
61  npre = p->EvtDesc.npre;
62  npost = p->EvtDesc.npost;
63  printf("TRG_Reader::UnpackTrg2007: TCUdataBytes = %d\n",p->EvtDesc.TCUdataBytes);
64  printf("TRG_Reader::UnpackTrg2007: Token = %d\n",p->EvtDesc.TrgToken);
65  printf("TRG_Reader::UnpackTrg2007: Npre=%d Npost=%d\n",npre,npost);
66 
67  if(p->EvtDesc.TrgToken>4096) {
68  printf("TRG_Reader::UnpackTrg2007: Found Token beyond 4096\n");
69  assert(0);
70  }
71  if(npre>5 || npre<0 || npost>5 || npost<0) {
72  printf("TRG_Reader::UnpackTrg2007: Trigger data has more than 5 pre/post\n");
73  assert(0);
74  }
75 
76  //Set pointers for daq data and new unpacked data
77  char* p_daq = (char *)pTRGP + size_off + size_head + size_desc + size_sum;
78  char* p_unp = (char *)pBankUnp + size_off + size_head + size_desc + size_sum;
79 
80  //Zero out all raw data memory before copying partial data
81  memset(p_unp, 0, 11*size_raw);
82 
83  //Loop over # of crossings available and copy
84  for(int i=0; i<1+npre+npost; i++){
85  unsigned short *nbytes = (unsigned short *)p_daq;
86  if(swap) pTRGD->swapHerb2bytes(nbytes,1); //byte swap for # of bytes
87  int n = *nbytes;
88  printf("TRG_Reader::UnpackTrg2007: Nprepost=%d RawDat Size=%d (byte) [%d]\n",i,n,size_raw);
89  if(swap){
90  //printf("-> Swapping on %p\n",pTRGD);
91  pTRGD->swapHerb2bytes(nbytes,1); //swap back so that later whole things will swap correctly
92  }
93 
94  if (n>size_raw){
95  printf("**********************************************************************************\n");
96  printf("TRG_Reader::UnpackTrg2007\n");
97  printf("\tWARNING! TrgRaw bank has improper reported size %d > %d\n",n,size_raw);
98  printf("\tSkipping event\n");
99  printf("**********************************************************************************\n");
100  return -1;
101  }
102 
103  printf("-> Doing memcpy to %p from %p\n",p_unp,p_daq);
104  memcpy(p_unp, p_daq, n);
105 
106  p_daq += n;
107  p_unp += size_raw;
108  }
109 
110  //Byte Swap (if needed) for all raw data section
111  printf("-> Checking if swap is necessary\n");
112  if(swap){
113  printf("-> Swap2007_Raw on %p\n",trgd);
114  res = Swap2007_Raw(trgd);
115  if(res<0){
116  printf("TRG_Reader::UnpackTrg2007: Swap RawData error %s %d.\n",__FILE__,__LINE__);
117  return -1;
118  }
119  }
120 
121  //Switch bank pointer to fully restored data
122  printf("-> pBankTRGP set to (Bank_TRGP *) %p\n",pBankUnp);
123  pBankTRGP = (Bank_TRGP *)pBankUnp;
124 
125  return 0;
126 };
127 
128 int TRG_Reader::Swap2007_DescSum(char *ptr){
130 
131  pTRGD->swapHerb2bytes(&(p->EvtDesc.TCUdataBytes),1);
132  pTRGD->swapHerb4bytes(&(p->EvtDesc.bunchXing_hi),1);
133  pTRGD->swapHerb4bytes(&(p->EvtDesc.bunchXing_lo),1);
134  pTRGD->swapHerb2bytes(&(p->EvtDesc.actionWdDetectorBitMask),1);
135  pTRGD->swapHerb2bytes(&(p->EvtDesc.TrgToken),1);
136  pTRGD->swapHerb2bytes(&(p->EvtDesc.addBits),1);
137  pTRGD->swapHerb2bytes(&(p->EvtDesc.DSMInput),1);
138  pTRGD->swapHerb2bytes(&(p->EvtDesc.externalBusy),1);
139  pTRGD->swapHerb2bytes(&(p->EvtDesc.modifiedBusyStatus),1);
140  pTRGD->swapHerb2bytes(&(p->EvtDesc.physicsWord),1);
141  pTRGD->swapHerb2bytes(&(p->EvtDesc.TriggerWord),1);
142  pTRGD->swapHerb2bytes(&(p->EvtDesc.DSMAddress),1);
143  pTRGD->swapHerb2bytes(&(p->EvtDesc.contaminationBusyStatus),1);
144  pTRGD->swapHerb2bytes(&(p->EvtDesc.npre),1);
145  pTRGD->swapHerb2bytes(&(p->EvtDesc.npost),1);
146  pTRGD->swapHerb2bytes(&(p->EvtDesc.dummy),1);
147 
148  pTRGD->swapHerb2bytes(&(p->TrgSum.TrgSumBytes),1);
149  pTRGD->swapHerb2bytes(&(p->TrgSum.TrgSumHeader),1);
150  pTRGD->swapHerb4bytes(&(p->TrgSum.L1Sum[0]),2);
151  pTRGD->swapHerb4bytes(&(p->TrgSum.L2Sum[0]),2);
152  pTRGD->swapHerb2bytes(&(p->TrgSum.L0SumBytes),1);
153  pTRGD->swapHerb2bytes(&(p->TrgSum.L0SumHeader),1);
154  pTRGD->swapHerb2bytes(&(p->TrgSum.DSMdata.MTD[0]),8);
155  pTRGD->swapHerb2bytes(&(p->TrgSum.DSMdata.VPD[0]),8);
156  pTRGD->swapHerb2bytes(&(p->TrgSum.DSMdata.CPA[0]),16);
157  pTRGD->swapHerb2bytes(&(p->TrgSum.DSMdata.CTB[0]),8);
158  pTRGD->swapHerb2bytes(&(p->TrgSum.DSMdata.lastDSM[0]),8);
159  pTRGD->swapHerb2bytes(&(p->TrgSum.DSMdata.VTX[0]),8);
160  pTRGD->swapHerb2bytes(&(p->TrgSum.DSMdata.EMC[0]),8);
161  pTRGD->swapHerb2bytes(&(p->TrgSum.DSMdata.BCdata[0]),16);
162  pTRGD->swapHerb2bytes(&(p->TrgSum.DSMdata.specialTriggers[0]),8);
163  pTRGD->swapHerb2bytes(&(p->TrgSum.DSMdata.FPD[0]),8);
164  pTRGD->swapHerb2bytes(&(p->TrgSum.L1SumBytes),1);
165  pTRGD->swapHerb2bytes(&(p->TrgSum.L1SumHeader),1);
166  pTRGD->swapHerb4bytes(&(p->TrgSum.L1Result[0]),32);
167  pTRGD->swapHerb2bytes(&(p->TrgSum.L2SumBytes),1);
168  pTRGD->swapHerb2bytes(&(p->TrgSum.L2SumHeader),1);
169  pTRGD->swapHerb4bytes(&(p->TrgSum.L2Result[0]),64);
170 
171  int npre = p->EvtDesc.npre;
172  int npost = p->EvtDesc.npost;
173  if(npre<0 || npre>5 || npost<0 || npost>5) return -1;
174 
175  return 0;
176 };
177 
178 int TRG_Reader::Swap2007_Raw(char *ptr) {
179  int numToSwap,i;
181 
182  numToSwap=1+p->EvtDesc.npost+p->EvtDesc.npre;
183 
184  for(i=0;i<numToSwap;i++) { // loop over NPRE, NPOST as well
185  pTRGD->swapHerb2bytes(&(p->rawTriggerDet[i].RawDetBytes),1);
186  pTRGD->swapHerb2bytes(&(p->rawTriggerDet[i].CTBdataBytes),1);
187  pTRGD->swapHerb2bytes(&(p->rawTriggerDet[i].MIXdataBytes),1);
188  pTRGD->swapHerb4bytes(&(p->rawTriggerDet[i].MIXfiller),1);
189  pTRGD->swapHerb2bytes(&(p->rawTriggerDet[i].BEMCdataBytes),1);
190  pTRGD->swapHerb4bytes(&(p->rawTriggerDet[i].BEMCfiller),1);
191  pTRGD->swapHerb2bytes(&(p->rawTriggerDet[i].BEMClayer1[0]),48);
192  pTRGD->swapHerb2bytes(&(p->rawTriggerDet[i].EEMCdataBytes),1);
193  pTRGD->swapHerb4bytes(&(p->rawTriggerDet[i].EEMCfiller),1);
194  pTRGD->swapHerb2bytes(&(p->rawTriggerDet[i].EEMClayer1[0]),16);
195  pTRGD->swapHerb2bytes(&(p->rawTriggerDet[i].FPDdataBytes),1);
196  pTRGD->swapHerb4bytes(&(p->rawTriggerDet[i].FPDfiller),1);
197  pTRGD->swapHerb2bytes(&(p->rawTriggerDet[i].FPDEastNSLayer1[0]),8);
198  pTRGD->swapHerb2bytes(&(p->rawTriggerDet[i].BBCdataBytes),1);
199  pTRGD->swapHerb4bytes(&(p->rawTriggerDet[i].BBCfiller),1);
200  pTRGD->swapHerb2bytes(&(p->rawTriggerDet[i].BBClayer1[0]),16);
201  pTRGD->swapHerb2bytes(&(p->rawTriggerDet[i].ZDClayer1[0]),8);
202  pTRGD->swapHerb2bytes(&(p->rawTriggerDet[i].QQTdataBytes),1);
203  pTRGD->swapHerb4bytes(&(p->rawTriggerDet[i].QQTfiller),1);
204  pTRGD->swapHerb4bytes(&(p->rawTriggerDet[i].QQTdata[0]),1600);
205  int nqt = p->rawTriggerDet[i].QQTdataBytes/4;
206  int ac10 = p->rawTriggerDet[i].QQTdata[nqt-1];
207  printf("NQTdata = %d, AC10 = 0x%x\n",nqt,ac10);
208  if(nqt>0 && ac10 != 0xAC10){
209  printf("Last word of QT data is not 0xAC10 but 0x%x\n ",ac10);
210  return -1;
211  }
212  }
213  return 0;
214 };
215 
225 void TRG_Reader::SanityCheck2007(char *ptr, int check_s=1) {
226  unsigned short x;
228 
229  x=p->TrgSum.L1SumBytes; assert(x==0x0084||x==0x8400);
230  x=p->TrgSum.L2SumBytes; assert(x==0x0084||x==0x8400);
231 
232  if (check_s){
233  assert( p->rawTriggerDet[0].RawDetHeader[0] =='R');
234  assert( p->rawTriggerDet[0].RawDetHeader[1] =='D');
235  assert( p->rawTriggerDet[0].CTBdataHeader[0] =='C');
236  assert( p->rawTriggerDet[0].CTBdataHeader[1] =='T');
237  assert( p->rawTriggerDet[0].BEMCdataHeader[0]=='E');
238  assert( p->rawTriggerDet[0].BEMCdataHeader[1]=='M');
239  } else {
240  cout << "TRG_Reader::SanityCheck2007 : Data position sanity check is disabled" << endl;
241  }
242 };