StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
EMC_BarrelReader.cxx
1 #include "EMC_BarrelReader.hh"
2 #include <assert.h>
3 #define MAX_ADC 0xFFF
4 #include "Stiostream.h"
5 #include <time.h>
6 #include "TTimeStamp.h"
7 #include <stdlib.h>
8 #include <Stiostream.h>
9 #include <stdio.h>
10 #include "StMessMgr.h"
11 #include "RTS/include/rts.h" // swap16(), swap32()
12 //ofstream fout("decode.out");
13 using namespace OLDEVP;
14 
15 EMC_BarrelReader::EMC_BarrelReader(EventReader *er,Bank_EMCP *pEMCP):pBankEMCP(pEMCP),ercpy(er)
16 {
17  EventInfo info=er->getEventInfo();
18  TTimeStamp ts(info.UnixTime);
19  UInt_t year,month,day,hour1,min,sec;
20  ts.GetDate(1,0,&year,&month,&day);
21  ts.GetTime(1,0,&hour1,&min,&sec);
22  UInt_t date= day + 100*(month+100*year );
23  UInt_t hour= sec + 100*(min +100*hour1);
24 
25  decoder=new StEmcDecoder(date,hour);
26  cout<<"EMC_Barrelreader** Event time (Unix time) = "<<info.UnixTime<<endl;
27  Initialize();
28 }
30 {
31  if(decoder)
32  delete decoder;
33 }
36 {
37  mTheTowerAdcR.NTowerHits=0;
38  for(int i=0;i<4800;i++)
39  {
40  mTheTowerAdcR.TowerADCArray[i]=0;
41  mTheTowerAdcD.TowerADCArray[i]=0;
42  mTheTowerPedR.TowerADCArray[i]=0;
43  mTheTowerRMSR.TowerADCArray[i]=0;
44  }
45  for(int i=0;i<120;i++)
46  {
47  mTheTowerAdcR.TDCHeader[i]=0;
48  mTheTowerAdcD.TDCHeader[i]=0;
49  mTheTowerPedR.TDCHeader[i]=0;
50  mTheTowerRMSR.TDCHeader[i]=0;
51  }
52 
53  // Initialize TOWERDATA array to 0's
54  for(int i = 0 ; i <120 ; i++)
55  for(int j = 0 ; j < 20; j++)
56  for(int k = 0 ; k < 2; k++)
57  {
58  mTheTowerAdcR.TowerMatrix[i][j][k] = 0;
59  mTheTowerAdcD.TowerMatrix[i][j][k] = 0;
60  mTheTowerPedR.TowerMatrix[i][j][k] = 0;
61  mTheTowerRMSR.TowerMatrix[i][j][k] = 0;
62  }
63 }
64 
65 Bank_EMCSECP* EMC_BarrelReader::getBarrelSection(const Bank_EMCP* pBankEMCP,int section)
66 {
67 
68  if((!pBankEMCP->EMCSecPointer[section].offset) || (!pBankEMCP->EMCSecPointer[section].length))
69  {
70  char str0[40];
71  sprintf(str0,"getBarrelSection(hs %d)",section);
72  ercpy->fprintError(INFO_MISSING_BANK,__FILE__,__LINE__,str0);
73  cout<<" getBarrelSection** , no offset or length for section**"<<section<<endl;
74  return NULL;
75  }
76 
77  Bank_EMCSECP * ptr=(Bank_EMCSECP*)(((INT32 *)pBankEMCP)+pBankEMCP->EMCSecPointer[0].offset);
78 
79  //put some checks
80  if(strncmp(ptr->header.BankType,"EMCSECP",7))
81  {
82  char str0[40];
83  cout<<" error in header name**"<<endl;
84  sprintf(str0,"getBarrelsection(hs )");
85  ercpy->fprintError(ERR_BAD_HEADER,__FILE__,__LINE__, str0);
86  return NULL;
87  }
88  if(!ptr->test_CRC())
89  {
90  char str0[40];
91  cout<<"error in CRC**"<<endl;
92  sprintf(str0,"getBarrelsection(hs )");
93  ercpy->fprintError(ERR_CRC,__FILE__,__LINE__,str0);
94  return NULL;
95  }
96  printf("Byte order of header for EMCSECP Before swap**: %x\n",ptr->header.ByteOrder);
97  if(ptr->swap() < 0)
98  {
99  char str0[40];
100  cout<<"error in swap**"<<endl;
101  sprintf(str0,"getBarrelsection(hs)");
102  ercpy->fprintError(ERR_SWAP,__FILE__,__LINE__,str0);
103  return NULL;
104  }
105 
106  ptr->header.CRC = 0;
107  return ptr;
108 }
110 Bank_EMCRBP* EMC_BarrelReader::getBarrelTowerFiber(Bank_EMCSECP* secp,int section)
111 {
112  if((!secp->FiberPointer[section].offset) || (!secp->FiberPointer[section].length))
113  {
114  char str0[40];
115  sprintf(str0,"getBarrelTowerFiber(hs %d)",section);
116  ercpy->fprintError(INFO_MISSING_BANK,__FILE__,__LINE__,str0);
117  cout<<" getBarrelSection** , no offset or length for section**"<<section<<endl;
118  return NULL;
119  }
120 
121  Bank_EMCRBP* ptr =(Bank_EMCRBP*)(((INT32 *)secp)+secp->FiberPointer[section].offset);
122 
123  // some checks and swap
124 
125  if(strncmp(ptr->header.BankType,"EMCRBP",6))
126  {
127  char str0[40];
128  cout<<" error in header name**"<<endl;
129  sprintf(str0,"getBarrelTowerFiber(hs %d)",section);
130  ercpy->fprintError(ERR_BAD_HEADER,__FILE__,__LINE__, str0);
131  return NULL;
132  }
133  if(!ptr->test_CRC())
134  {
135  char str0[40];
136  cout<<"error in CRC**"<<endl;
137  sprintf(str0,"getBarrelTowerFiber(hs %d)",section);
138  ercpy->fprintError(ERR_CRC,__FILE__,__LINE__,str0);
139  return NULL;
140  }
141  if(ptr->swap() < 0)
142  {
143  char str0[40];
144  cout<<"error in swap**"<<endl;
145  sprintf(str0,"getBarrelTowerFiber(hs %d)",section);
146  ercpy->fprintError(ERR_SWAP,__FILE__,__LINE__,str0);
147  return NULL;
148  }
149  ptr->header.CRC = 0;
150  return ptr;
151 }
153 Bank_TOWERADCR* EMC_BarrelReader::getBarrelADC(Bank_EMCRBP* rbp)
154 {
155  if((!rbp->EMCADCR.offset) || (!rbp->EMCADCR.length))
156  {
157  char str0[40];
158  sprintf(str0,"getBarrelADC(hs)");
159  ercpy->fprintError(INFO_MISSING_BANK,__FILE__,__LINE__,str0);
160  cout<<" getBarrelADC** , no offset or length for ADCR**"<<endl;
161  return NULL;
162  }
163  Bank_TOWERADCR* pADCR =(Bank_TOWERADCR*)(((INT32 *)rbp)+rbp->EMCADCR.offset);
164 
165  // some checks and swap
166 
167  if(strncmp(pADCR->header.BankType,"EMCADCR",7))
168  {
169  char str0[40];
170  cout<<" error in header name**"<<endl;
171  sprintf(str0,"getBarrelADC(hs)");
172  ercpy->fprintError(ERR_BAD_HEADER,__FILE__,__LINE__, str0);
173  return NULL;
174  }
175  if(!pADCR->test_CRC())
176  {
177  char str0[40];
178  cout<<"error in CRC**"<<endl;
179  sprintf(str0,"getBarrelADC(hs)");
180  ercpy->fprintError(ERR_CRC,__FILE__,__LINE__,str0);
181  return NULL;
182  }
183  if(pADCR->swap() < 0)
184  {
185  char str0[40];
186  cout<<"error in swap**"<<endl;
187  sprintf(str0,"getBarrelADC(hs)");
188  ercpy->fprintError(ERR_SWAP,__FILE__,__LINE__,str0);
189  return NULL;
190  }
191 
192  pADCR->header.CRC = 0;
193  return pADCR;
194 }
196 int EMC_BarrelReader::FillBarrelTower(Bank_TOWERADCR* pADCR)
197 {
198  mTheTowerAdcR.BankType="TOWRADCR\n";
199  mTheTowerAdcR.DetFlag=1; // Detector flag for BEMC=1,
200  mTheTowerAdcR.EventNumber=pADCR->header.Token; // Token number
201  mTheTowerAdcR.PedFlag=0; // Pedestal subtracted (1) or not (0)
202  mTheTowerAdcR.TDCErrorFlag=1; // Error from TDC (0=good)
203  mTheTowerAdcR.NTDCChannels=0; // Number of valid TDC channels
204 
205  for(int TDC=0;TDC<120;TDC++)
206  mTheTowerAdcR.TDCHeader[TDC] = pADCR->TDCHeader[TDC];
207 
208  for(int TDC=0;TDC<30;TDC++) // filling TDC channels information
209  {
210  mTheTowerAdcR.TDCCount[TDC]=pADCR->TDCHeader[TDC];
211  mTheTowerAdcR.TDCError[TDC]=pADCR->TDCHeader[TDC+30];
212  mTheTowerAdcR.TDCToken[TDC]=pADCR->TDCHeader[TDC+60];
213  mTheTowerAdcR.TDCTrigger[TDC]=(pADCR->TDCHeader[TDC+90] & 0xF00) >> 8;
214  mTheTowerAdcR.TDCCrateId[TDC]=(pADCR->TDCHeader[TDC+90] & 0x0FF);
215  /*cout <<" TDC channel "<<TDC
216  <<" Byte count = "<<mTheTowerAdcR.TDCCount[TDC]
217  <<" Error = "<<mTheTowerAdcR.TDCError[TDC]
218  <<" Token = "<<mTheTowerAdcR.TDCToken[TDC]
219  <<" Trigger = "<<mTheTowerAdcR.TDCTrigger[TDC]
220  <<" CrateId = "<<mTheTowerAdcR.TDCCrateId[TDC]<<endl;*/
221  int CId = 0;
222  decoder->GetTowerCrateFromTDC(TDC,CId);
223 
224  //if(mTheTowerAdcR.TDCError[TDC]==0 &&
225  // mTheTowerAdcR.TDCCount[TDC]==164 &&
226  // mTheTowerAdcR.TDCCrateId[TDC]==CId)
227  {
228  mTheTowerAdcR.TDCErrorFlag=0; // at least one TDC channel is Ok
229  mTheTowerAdcR.NTDCChannels++; // Number of valid TDC channels
230 
231  for(int i=0;i<160;i++) // loop over data in this TDC channel if there is no error
232  {
233  int index=i*30+TDC;
234 
235  mTheTowerAdcR.TDCData[TDC][i]=pADCR->fiberData[index];
236 
237  //Fill towerrawdata struct
238  mTheTowerAdcR.TowerADCArray[index]=pADCR->fiberData[index];
239 
240  //Total number of towers for valid TDC channels
241  mTheTowerAdcR.NTowerHits++;
242 
243  int index_jose=-1;
244  int stat_index=decoder->GetTowerIdFromDaqId(index,index_jose);
245  // Index-jose runs here from 1 to 4800
246  //cout <<"index "<<index<<" index_jose "<<index_jose<<" stat_index "<<stat_index<<endl;
247 
248  int m=0,e=0,s=0;
249  if(index_jose!=-1)
250  {
251  if(stat_index)
252  {
253  int binstat=decoder->GetTowerBin(index_jose,m,e,s);
254  //LOG_DEBUG <<"index = "<<index<<" soft = "<<index_jose<<" module = "<<m<<" eta = "<<e<<" sub = "<<s<<" adc = "<<pADCR->fiberData[index]<<endm;
255 
256  if(!binstat)
257  cout<<" problem in bin conversion "<<index<<endl;
258  else
259  mTheTowerAdcR.TowerMatrix[m-1][e-1][s-1]=pADCR->fiberData[index];
260  }//stat_index
261  }
262  }
263  }
264  }
265 
266  return 1;
267 }
269 int EMC_BarrelReader::ProcessBarrelTower(const Bank_EMCP* EmcPTR, const Bank_TRGP* TrgPTR)
270 {
271  // first check if tower data is in trgp
272  if (TrgPTR) {
273  bool swap = TrgPTR->header.ByteOrder != 0x04030201;
274  int offset = TrgPTR->theData.offset;
275  if (swap) offset = swap32(offset);
276  char* cTTT = (char*)TrgPTR + offset * 4 + sizeof(TrgPTR->header);
277  if (cTTT) {
279  int byteCount_Version = TTT->byteCount_Version;
280  if (swap) byteCount_Version = swap32(TTT->byteCount_Version);
281  if ((byteCount_Version & 0xff) == 0x10) {
282  int offset = TTT->OffsetBlock[y8TRG_INDEX].offset;
283  if (swap) offset = swap32(offset);
284  char trg_version = cTTT[offset + 3];
285  if (trg_version == 0x32) {
286  int offset = TTT->OffsetBlock[y8BTOW_INDEX].offset;
287  int length = TTT->OffsetBlock[y8BTOW_INDEX].length;
288 
289  if (swap) {
290  offset = swap32(offset);
291  length = swap32(length);
292  }
293 
294  if (length != y8BTOW_LEN) {
295  LOG_ERROR << "Bad BTOW length found in TrgTowerTrnfer block" << endm;
296  return 0;
297  }
298 
299  Bank_TOWERADCR* fakeADCRptr = (Bank_TOWERADCR*)((char*)TTT + offset - sizeof(Bank_Header));
300 
301  return FillBarrelTower(fakeADCRptr);
302  }
303  }
304  }
305  }
306 
307  if (!EmcPTR) return 0;
308 
309  // now look in Bank_EMCP
310  Bank_EMCSECP* barreltower=getBarrelSection(EmcPTR,0);
311  if(barreltower) {
312  Bank_EMCRBP* towerfiber=getBarrelTowerFiber(barreltower,0);
313  if(towerfiber) {
314  Bank_TOWERADCR* toweradc=getBarrelADC(towerfiber);
315 
316  if(toweradc) {
317  FillBarrelTower(toweradc);
318  return 1;
319  }
320  else { LOG_INFO <<" ADCR absent , looking for ADCD"<<endm; }
321 
322  toweradc=0;
323  }
324  else { LOG_INFO <<" BANK_EMRBP absent**"<<endm; }
325  }
326  else { LOG_INFO <<" BANK_EMCSECP absent**"<<endm; }
327 
328  return 0; // if we got here we couldn't find tower data
329 }
331 void EMC_BarrelReader::PrintTowerArray()
332 {
333 
334  cout<<"BankType **"<<mTheTowerAdcR.BankType<<endl;
335  cout<<"Det flag **"<<mTheTowerAdcR.DetFlag<<endl;
336  cout<<"Event no**"<<mTheTowerAdcR.EventNumber<<endl;
337  cout<<"Pedflag**"<<mTheTowerAdcR.PedFlag<<endl;
338  cout<<"TDCERR **"<<mTheTowerAdcR.TDCErrorFlag<<endl;
339  for(int i=0;i<120;i++)
340  for(int j=0;j<20;j++)
341  for(int k=0;k<2;k++)
342  if(mTheTowerAdcR.TowerMatrix[i][j][k]>0)
343  cout<<"ADCDATA** mod"<<i+1<<"eta "<<j+1<<"phi "<<k+1<<"ADC "<<mTheTowerAdcR.TowerMatrix[i][j][k]<<endl;
344 }
346 Bank_BTOWERADCR& EMC_BarrelReader::getBTOWERADCR()
347 {
348  return mTheTowerAdcR;
349 }
unsigned short TDCData[30][160]
data from each TDC channel
Definition: EMC_Reader.hh:71
unsigned int TDCErrorFlag
Error from TDC.
Definition: EMC_Reader.hh:68
unsigned int PedFlag
Pedestal subtracted or not (??)
Definition: EMC_Reader.hh:65
int GetTowerBin(int softId, int &m, int &e, int &s) const
Convert from softId to m,e,s for towers only.
unsigned short TDCCrateId[30]
vector with crate Id for each TDC channel
Definition: EMC_Reader.hh:75
int GetTowerCrateFromTDC(int TDC, int &crate) const
Get crate number from TDC channel for towers.
~EMC_BarrelReader()
EMC_BarrelReader destructor.
unsigned short TDCCount[30]
vector with byte count for each TDC channel
Definition: EMC_Reader.hh:76
const char * BankType
Will be filled with a 9 char array (8 letters + NULL)
Definition: EMC_Reader.hh:62
unsigned short TowerADCArray[4800]
Matrix of ADC&#39;s as obtained from daq.
Definition: EMC_Reader.hh:79
EMC_BarrelReader(EventReader *er, Bank_EMCP *pEMCP)
EMC_BarrelReader constructor.
short TDCHeader[120]
TDC header - error flag etc.
Definition: EMC_Reader.hh:51
unsigned short TDCHeader[120]
This is the TDC event header.
Definition: EMC_Reader.hh:80
int GetTowerIdFromDaqId(int RDO, int &softId) const
Get Sofwtare Id from Daq Id for towers.
unsigned int NTowerHits
Total number of valid channels.
Definition: EMC_Reader.hh:67
void Initialize()
Initialization of arrays.
unsigned int EventNumber
Token number.
Definition: EMC_Reader.hh:64
short fiberData[4800]
stuff on the fiber - (Raw data)
Definition: EMC_Reader.hh:52
unsigned short TowerMatrix[120][20][2]
Matrix of ADC&#39;s in Physical Positions.
Definition: EMC_Reader.hh:78
unsigned short TDCToken[30]
vector with crate token for each TDC channel
Definition: EMC_Reader.hh:73
unsigned int NTDCChannels
Total number of valid TDC channels.
Definition: EMC_Reader.hh:69
unsigned short TDCTrigger[30]
vector with trigger number for each TDC channel
Definition: EMC_Reader.hh:74
unsigned int DetFlag
DAQ Detector flag for BEMC, PRS, SMDE or SMDP.
Definition: EMC_Reader.hh:63
unsigned short TDCError[30]
vector with TDC error for each TDC channel
Definition: EMC_Reader.hh:72