StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
code2008.cxx
1 // This is 2008 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 "trgStructures2008.h"
6 #include "TRG_Reader.hh"
7 using namespace OLDEVP;
8 
9 int TRG_Reader::UnpackTrg2008(Bank_TRGP *pTRGP){
10  int returnValue,npre,npost,swap,res;
11 
12  if ( ! pTRGP){
13  printf("TRG_Reader::UnpackTrg2008 - 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::UnpackTrg2008: Swap Header error %s %d.\n", __FILE__ , __LINE__ );
28  assert(0);
29  }
30  printf("TRG_Reader::UnpackTrg2008: 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(TrgDataType2008);
37  int size_desc = sizeof(EvtDescData2008);
38  int size_sum = sizeof(TrgSumData2008);
39  int size_raw = sizeof(RawTrgDet2008);
40  // printf("TRG_Reader::UnpackTrg2008: 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  // New TrgTowerTrnfer structure and find trigger data itself
44  char* cttt = (char*)pTRGP + size_off + size_head;
46  if(swap){
47  res=Swap2008_TrgTowerTrnfer(cttt);
48  if(res<0) {
49  printf("TRG_Reader::UnpackTrg2008: Swap TrgTowerTrnfer error %s %d.\n", __FILE__ , __LINE__ );
50  return -1;
51  }
52  }
53  int offset = ttt->OffsetBlock[y8TRG_INDEX].offset;
54  int length = ttt->OffsetBlock[y8TRG_INDEX].length;
55  printf("TRG_Reader::UnpackTrg2008: TrgTowerTrnfer byet_version=0x%x offset=%d length=%d\n",
56  ttt->byteCount_Version,offset,length);
57  if(length<=0) {
58  printf("TRG_Reader::UnpackTrg2008: No Trigger Data %s %d.\n", __FILE__ , __LINE__ );
59  return 0;
60  }
61  char* trg_version = (char*)pTRGP + size_off + size_head + offset + 3;
62  printf("TRG_Reader::UnpackTrg2008: trg_version = 0x%x\n",*trg_version);
63  if(*trg_version != 0x32) {
64  printf("TRG_Reader::UnpackTrg2008: Trigger version error %s %d.\n",*trg_version, __FILE__ , __LINE__ );
65  return -1;
66  }
67 
68  //Create memory space for unpacked trigger bank
69  if(pBankUnp!=0) delete[] pBankUnp;
70  int sizeUnp = size_off + size_head + size_trg;
71  pBankUnp = new char[sizeUnp];
72  char *trgd = pBankUnp + size_off + size_head; //pointer for trgdata
74 
75  //Copy Header, EvtDesc and TrgSum and byte swap
76  memcpy(pBankUnp, pTRGP, size_off+size_head);
77  memcpy(pBankUnp+size_off+size_head, (char*)pTRGP+size_off+size_head+offset, size_desc+size_sum);
78  if(swap){
79  res=Swap2008_DescSum(trgd);
80  }
81  npre = p->EvtDesc.npre;
82  npost = p->EvtDesc.npost;
83  printf("TRG_Reader::UnpackTrg2008: TCUdataBytes = %d Token = %d Npre/Npost=%d/%d\n",
84  p->EvtDesc.TCUdataBytes, p->EvtDesc.TrgToken, npre,npost);
85  if(swap && res<0) {
86  printf("TRG_Reader::UnpackTrg2008: Swap DescSum error %s %d.\n", __FILE__ , __LINE__ );
87  return -1;
88  }
89 
90  if(p->EvtDesc.TrgToken>4096) {
91  printf("TRG_Reader::UnpackTrg2008: Found Token beyond 4096\n");
92  assert(0);
93  }
94  if(npre>5 || npre<0 || npost>5 || npost<0) {
95  printf("TRG_Reader::UnpackTrg2008: Trigger data has more than 5 pre/post\n");
96  assert(0);
97  }
98 
99  //Set pointers for daq data and new unpacked data
100  char* p_daq = (char *)pTRGP + size_off + size_head + size_desc + size_sum + offset;
101  char* p_unp = (char *)pBankUnp + size_off + size_head + size_desc + size_sum;
102 
103  //Zero out all raw data memory before copying partial data
104  memset(p_unp, 0, 11*size_raw);
105 
106  //Loop over # of crossings available and copy
107  for(int i=0; i<1+npre+npost; i++){
108  unsigned short *nbytes = (unsigned short *)p_daq;
109  if(swap) pTRGD->swapHerb2bytes(nbytes,1); //byte swap for # of bytes
110  int n = *nbytes;
111  //printf("TRG_Reader::UnpackTrg2008: Nprepost=%d RawDat Size=%d (byte) [%d]\n",i,n,size_raw);
112  if(swap){
113  //printf("-> Swapping on %p\n",pTRGD);
114  pTRGD->swapHerb2bytes(nbytes,1); //swap back so that later whole things will swap correctly
115  }
116 
117  if (n>size_raw){
118  printf("**********************************************************************************\n");
119  printf("TRG_Reader::UnpackTrg2008\n");
120  printf("\tWARNING! TrgRaw bank has improper reported size %d > %d\n",n,size_raw);
121  printf("\tSkipping event\n");
122  printf("**********************************************************************************\n");
123  return -1;
124  }
125 
126  //printf("-> Doing memcpy to %p from %p\n",p_unp,p_daq);
127  memcpy(p_unp, p_daq, n);
128 
129  p_daq += n;
130  p_unp += size_raw;
131  }
132 
133  //Byte Swap (if needed) for all raw data section
134  //printf("-> Checking if swap is necessary\n");
135  if(swap){
136  //printf("-> Swap2008_Raw on %p\n",trgd);
137  res = Swap2008_Raw(trgd);
138  if(res<0){
139  printf("TRG_Reader::UnpackTrg2008: Swap RawData error %s %d.\n", __FILE__ , __LINE__ );
140  return -1;
141  }
142  }
143 
144  //Switch bank pointer to fully restored data
145  //printf("-> pBankTRGP set to (Bank_TRGP *) %p\n",pBankUnp);
146  pBankTRGP = (Bank_TRGP *)pBankUnp;
147 
148  return 0;
149 };
150 
151 int TRG_Reader::Swap2008_TrgTowerTrnfer(char *ptr){
153  pTRGD->swapHerb4bytes(&(p->byteCount_Version),1);
154  pTRGD->swapHerb4bytes(&(p->OffsetBlock[0].offset),2 * y8MAX_OFFSET);
155  return 0;
156 };
157 
158 int TRG_Reader::Swap2008_DescSum(char *ptr){
160 
161  pTRGD->swapHerb2bytes(&(p->EvtDesc.TCUdataBytes),1);
162  pTRGD->swapHerb4bytes(&(p->EvtDesc.bunchXing_hi),1);
163  pTRGD->swapHerb4bytes(&(p->EvtDesc.bunchXing_lo),1);
164  pTRGD->swapHerb2bytes(&(p->EvtDesc.actionWdDetectorBitMask),1);
165  pTRGD->swapHerb2bytes(&(p->EvtDesc.TrgToken),1);
166  pTRGD->swapHerb2bytes(&(p->EvtDesc.addBits),1);
167  pTRGD->swapHerb2bytes(&(p->EvtDesc.DSMInput),1);
168  pTRGD->swapHerb2bytes(&(p->EvtDesc.externalBusy),1);
169  pTRGD->swapHerb2bytes(&(p->EvtDesc.modifiedBusyStatus),1);
170  pTRGD->swapHerb2bytes(&(p->EvtDesc.physicsWord),1);
171  pTRGD->swapHerb2bytes(&(p->EvtDesc.TriggerWord),1);
172  pTRGD->swapHerb2bytes(&(p->EvtDesc.DSMAddress),1);
173  pTRGD->swapHerb2bytes(&(p->EvtDesc.contaminationBusyStatus),1);
174  pTRGD->swapHerb2bytes(&(p->EvtDesc.npre),1);
175  pTRGD->swapHerb2bytes(&(p->EvtDesc.npost),1);
176  pTRGD->swapHerb2bytes(&(p->EvtDesc.dummy),1);
177 
178  pTRGD->swapHerb2bytes(&(p->TrgSum.TrgSumBytes),1);
179  pTRGD->swapHerb2bytes(&(p->TrgSum.TrgSumHeader),1);
180  pTRGD->swapHerb4bytes(&(p->TrgSum.L1Sum[0]),2);
181  pTRGD->swapHerb4bytes(&(p->TrgSum.L2Sum[0]),2);
182  pTRGD->swapHerb2bytes(&(p->TrgSum.L0SumBytes),1);
183  pTRGD->swapHerb2bytes(&(p->TrgSum.L0SumHeader),1);
184  pTRGD->swapHerb2bytes(&(p->TrgSum.DSMdata.MTD[0]),8);
185  pTRGD->swapHerb2bytes(&(p->TrgSum.DSMdata.VPD[0]),8);
186  pTRGD->swapHerb2bytes(&(p->TrgSum.DSMdata.CPA[0]),16);
187  pTRGD->swapHerb2bytes(&(p->TrgSum.DSMdata.CTB[0]),8);
188  pTRGD->swapHerb2bytes(&(p->TrgSum.DSMdata.lastDSM[0]),8);
189  pTRGD->swapHerb2bytes(&(p->TrgSum.DSMdata.VTX[0]),8);
190  pTRGD->swapHerb2bytes(&(p->TrgSum.DSMdata.EMC[0]),8);
191  pTRGD->swapHerb2bytes(&(p->TrgSum.DSMdata.BCdata[0]),16);
192  pTRGD->swapHerb2bytes(&(p->TrgSum.DSMdata.specialTriggers[0]),8);
193  pTRGD->swapHerb2bytes(&(p->TrgSum.DSMdata.FPD[0]),8);
194  pTRGD->swapHerb2bytes(&(p->TrgSum.L1SumBytes),1);
195  pTRGD->swapHerb2bytes(&(p->TrgSum.L1SumHeader),1);
196  pTRGD->swapHerb4bytes(&(p->TrgSum.L1Result[0]),32);
197  pTRGD->swapHerb2bytes(&(p->TrgSum.L2SumBytes),1);
198  pTRGD->swapHerb2bytes(&(p->TrgSum.L2SumHeader),1);
199  pTRGD->swapHerb4bytes(&(p->TrgSum.L2Result[0]),64);
200 
201  int npre = p->EvtDesc.npre;
202  int npost = p->EvtDesc.npost;
203  if(npre<0 || npre>5 || npost<0 || npost>5) return -1;
204 
205  return 0;
206 };
207 
208 int TRG_Reader::Swap2008_Raw(char *ptr) {
209  int numToSwap,i;
211 
212  numToSwap=1+p->EvtDesc.npost+p->EvtDesc.npre;
213 
214  for(i=0;i<numToSwap;i++) { // loop over NPRE, NPOST as well
215  pTRGD->swapHerb2bytes(&(p->rawTriggerDet[i].RawDetBytes),1);
216  pTRGD->swapHerb2bytes(&(p->rawTriggerDet[i].CTBdataBytes),1);
217  pTRGD->swapHerb2bytes(&(p->rawTriggerDet[i].MIXdataBytes),1);
218  pTRGD->swapHerb4bytes(&(p->rawTriggerDet[i].MIXfiller),1);
219  pTRGD->swapHerb2bytes(&(p->rawTriggerDet[i].BEMCdataBytes),1);
220  pTRGD->swapHerb4bytes(&(p->rawTriggerDet[i].BEMCfiller),1);
221  pTRGD->swapHerb2bytes(&(p->rawTriggerDet[i].BEMClayer1[0]),48);
222  pTRGD->swapHerb2bytes(&(p->rawTriggerDet[i].EEMCdataBytes),1);
223  pTRGD->swapHerb4bytes(&(p->rawTriggerDet[i].EEMCfiller),1);
224  pTRGD->swapHerb2bytes(&(p->rawTriggerDet[i].EEMClayer1[0]),16);
225  pTRGD->swapHerb2bytes(&(p->rawTriggerDet[i].FPDdataBytes),1);
226  pTRGD->swapHerb4bytes(&(p->rawTriggerDet[i].FPDfiller),1);
227  pTRGD->swapHerb2bytes(&(p->rawTriggerDet[i].FPDEastNSLayer1[0]),8);
228  pTRGD->swapHerb2bytes(&(p->rawTriggerDet[i].BBCdataBytes),1);
229  pTRGD->swapHerb4bytes(&(p->rawTriggerDet[i].BBCfiller),1);
230  pTRGD->swapHerb2bytes(&(p->rawTriggerDet[i].BBClayer1[0]),16);
231  pTRGD->swapHerb2bytes(&(p->rawTriggerDet[i].ZDClayer1[0]),8);
232  pTRGD->swapHerb2bytes(&(p->rawTriggerDet[i].QQTdataBytes),1);
233  pTRGD->swapHerb4bytes(&(p->rawTriggerDet[i].QQTfiller),1);
234  pTRGD->swapHerb4bytes(&(p->rawTriggerDet[i].QQTdata[0]),1600);
235  int nqt = p->rawTriggerDet[i].QQTdataBytes/4;
236  int ac10 = p->rawTriggerDet[i].QQTdata[nqt-1];
237  printf("NQTdata = %d, Last word check = 0x%x (should be ac10)\n",nqt,ac10);
238  if(nqt>0 && ac10 != 0xAC10){
239  printf("Last word of QT data is not 0xAC10 but 0x%x\n ",ac10);
240  return -1;
241  }
242  }
243  return 0;
244 };
245 
255 void TRG_Reader::SanityCheck2008(char *ptr, int check_s=1) {
256  unsigned short x;
258 
259  x=p->TrgSum.L1SumBytes; assert(x==0x0084||x==0x8400);
260  x=p->TrgSum.L2SumBytes; assert(x==0x0084||x==0x8400);
261 
262  if (check_s){
263  assert( p->rawTriggerDet[0].RawDetHeader[0] =='R');
264  assert( p->rawTriggerDet[0].RawDetHeader[1] =='D');
265  assert( p->rawTriggerDet[0].CTBdataHeader[0] =='C');
266  assert( p->rawTriggerDet[0].CTBdataHeader[1] =='T');
267  assert( p->rawTriggerDet[0].BEMCdataHeader[0]=='E');
268  assert( p->rawTriggerDet[0].BEMCdataHeader[1]=='M');
269  } else {
270  cout << "TRG_Reader::SanityCheck2008 : Data position sanity check is disabled" << endl;
271  }
272 };