StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
PMD_Reader.cxx
1 /***************************************************************************
2  * Author: Susanta and Subhasis
3  *
4  * Description: Description : Reads PMD Raw and Pedestals Data and fills PMD Specific Bank.
5  * PMD has been taken as Extended Detector as defined by Tonko.
6  * Designed based on Event Pool Reader of PMD ( Ref: Tonko )
7  **************************************************************************/
8 
9 #include "PMD_Reader.hh"
10 #include <assert.h>
11 #define MAX_ADC 0xFFF
12 
13 using namespace OLDEVP;
14 
15 int PMD_Reader::ProcessEvent(const Bank_PMDP * PmdPTR)
16 {
17  int sec, type, ret;
18  unsigned int Token;
19  Token=PmdPTR->header.Token;
20  printf("PMD Token = %d\n", Token);
21 
22  mThePmd.mode = 0;
23  mThePmd.channels = 0;
24  mThePmd.max_channels = 2*PMD_CRAMS_MAX*2*PMD_CRAMS_CH_MAX ;
25 
26 
27  for(sec=0; sec<2; sec++) {
28  if(PmdPTR->sec[sec].length == 0) continue;
29  Bank_PMDSECP *secp = (Bank_PMDSECP *)(((INT32 *)PmdPTR) + PmdPTR->sec[sec].offset);
30  // cout << "in Process Event==>"<<secp->header.BankType << endl;
31  if(strncmp(secp->header.BankType,"PMDSECP",7))
32  {
33  char str0[40];
34  cout<<" PMDSECP error in header name**"<<endl;
35  sprintf(str0,"Not PMDSECP");
36  ercpy->fprintError(ERR_BAD_HEADER,__FILE__,__LINE__, str0); return 0;
37  }
38 
39  if(!secp->test_CRC())
40  {
41  char str0[40];
42  cout<<"error in CRC**"<<endl;
43  sprintf(str0,"Not PMDSECP");
44  ercpy->fprintError(ERR_CRC,__FILE__,__LINE__,str0); return 0;
45  }
46  if(secp->swap() < 0)
47  {
48  char str0[40];
49  cout<<"error in swap**"<<endl;
50  sprintf(str0," PMDSECP");
51  ercpy->fprintError(ERR_SWAP,__FILE__,__LINE__,str0); return 0;
52  }
53  secp->swap(); // Swapping is required to get the offset
54  for( type=0; type < 4; type ++) {
55  // cout<<"sec="<< sec <<"type" <<type << "length="<<secp->type[type].length << endl;
56  if(secp->type[type].length ==0 ) continue;
57  switch(type){
58  case PMD_ADCD_N :
59  {
60  // cout<<"adcd off=" << secp->type[type].offset << endl;
61  Bank_PMDADCD *adcd = (Bank_PMDADCD *)(((INT32 *)secp) + secp->type[type].offset);
62  //cout << "in Process Event==>"<<adcd->header.BankType << endl;
63  if(strncmp(adcd->header.BankType,"PMDADCD",7))
64  {
65  char str0[40];
66  cout<<" PMDADCD error in header name**"<<endl;
67  sprintf(str0,"Not PMDADCD");
68  ercpy->fprintError(ERR_BAD_HEADER,__FILE__,__LINE__, str0); return 0;
69  }
70  ret = adcReader(sec, adcd);
71  if(ret) mThePmd.channels += ret;
72  // printf("after adcReader ret=%d\n", ret);
73  }
74  break;
75  case PMD_PEDR_N :
76  {
77  // cout<<"sec"<<sec<<"type="<< type<<"pedr off=" << secp->type[type].offset << endl;
78  Bank_PMDPEDR *pedr = (Bank_PMDPEDR *)(((INT32 *)secp) + secp->type[type].offset);
79  // cout << "in Process Event==>"<<pedr->header.BankType << endl;
80  if(strncmp(pedr->header.BankType,"PMDPEDR",7))
81  {
82  char str0[40];
83  cout<<" PMDPEDR error in header name**"<<endl;
84  sprintf(str0,"Not PMDPEDR");
85  ercpy->fprintError(ERR_BAD_HEADER,__FILE__,__LINE__, str0); return 0;
86  }
87  ret = pedReader(sec, type, pedr);
88  if(ret) mThePmd.channels += ret;
89  mThePmd.mode = 1;
90  }
91  break;
92 
93  case PMD_RMSR_N :
94  {
95  //cout<<"sec"<<sec<<"type="<< type<<"rmsr off=" << secp->type[type].offset << endl;
96  Bank_PMDPEDR *rmsr = (Bank_PMDPEDR *)(((INT32 *)secp) + secp->type[type].offset);
97  // cout << "in Process Event==>"<<pedr->header.BankType << endl;
98  if(strncmp(rmsr->header.BankType,"PMDRMSR",7))
99  {
100  char str0[40];
101  cout<<" PMDRMSR error in header name**"<<endl;
102  sprintf(str0,"Not PMDRMSR");
103  ercpy->fprintError(ERR_BAD_HEADER,__FILE__,__LINE__, str0); return 0;
104  }
105  ret = pedReader(sec, type, rmsr);
106  if(ret) mThePmd.channels += ret;
107  mThePmd.mode = 1;
108  }
109 
110  break;
111  case PMD_THRR_N :
112  {
113  Bank_PMDPEDR *thrr = (Bank_PMDPEDR *)(((INT32 *)secp) + secp->type[type].offset);
114  // cout << "in Process Event==>"<<pedr->header.BankType << endl;
115  if(strncmp(thrr->header.BankType,"PMDTHRR",7))
116  {
117  char str0[40];
118  cout<<" PMDTHRR error in header name**"<<endl;
119  sprintf(str0,"Not PMDTHRR");
120  ercpy->fprintError(ERR_BAD_HEADER,__FILE__,__LINE__, str0); return 0;
121  }
122  ret = pedReader(sec, type, thrr);
123  if(ret) mThePmd.channels += ret;
124  mThePmd.mode = 1;
125  }
126  break;
127  }
128  }
129  }
130  return 1;
131 }
132 
133 
135 {
136  adcd->swap();
137  int items = adcd->header.BankLength -(INT32)sizeof(Bank_Header)/4 ;
138 
139  unsigned int *datum ;
140  unsigned int j ;
141  unsigned int cram, blk ;
142  unsigned int ch_num ;
143  unsigned int ch ;
144 
145  if(items <= 0) return 0 ;
146 
147 
148  datum = (unsigned int *)adcd->data ;
149 
150  ch_num = 0 ;
151 
152 //printf("PMD sec %d: items %d\n",sec,items) ;
153 //
154  unsigned int *end_datum = datum + items ;
155 
156  for(end_datum=datum+items;datum<end_datum;) {
157  unsigned int tmp = *datum ;
158  datum++ ;
159 
160  cram = (tmp & 0xFF000000) >> 24 ;
161  blk = (tmp & 0x00FF0000) >> 16 ;
162  ch = (tmp & 0x0000FFFF) ;
163 
164  mThePmd.no_of_channels_in_cram_blk[sec][cram][blk] = ch;
165 
166  if(cram >= PMD_CRAMS_MAX) {
167  printf("PMD: Bad Cram number %d\n",cram) ;
168  return 0 ;
169  }
170 
171  if((blk != 0) && (blk != 1)) {
172  printf("PMD: Bad Block %d in Cram %d\n",blk,cram) ;
173  return 0 ;
174  }
175 
176  if(ch > PMD_CRAMS_CH_MAX) {
177  printf("PMD: too many channels in Cram %d, Block %d: %d\n",cram,blk,ch) ;
178  return 0 ;
179  }
180 
181  for(j=0;j<ch;j++) {
182  unsigned int val ;
183 
184  val = (*datum) ;
185  datum++ ;
186 
187  if(val & 0x40000000) {
188  unsigned int channel = (val & 0x7ff000) >> 12 ;
189  val &= 0xFFF ;
190 // printf(" %4d: %4d == %4d\n",j,channel,val) ;
191  if(channel >= PMD_CRAMS_CH_MAX) {
192  printf("PMD: channel too big in Cram %d, Block %d: %d\n",cram,blk,channel) ;
193  return 0 ;
194  }
195  mThePmd.adc[sec][cram][blk][channel] = val ;
196  ch_num++ ;
197  }
198  }
199  }
200 return ch_num ;
201 
202 }
203 
204 
205 
207 {
208  pBankPMDP = pPMDP; //copy into class data member for use by other methods
209  ercpy = er; // squirrel away pointer eventreader for our friends
210  printf(" This PMD_Reader in ctor , %s. \n", __FILE__ );
211 
212  pBankPMDP->header.BankType[7]=0;
213  cout<<"header bank type "<<pBankPMDP->header.BankType<<endl;
214  if (!pBankPMDP->test_CRC()) {
215  printf("CRC error in PMDP: %s %d\n",__FILE__,__LINE__) ;
216  }
217 
218  if (pBankPMDP->swap() < 0) {
219  printf("swap error in PMDP: %s %d\n",__FILE__,__LINE__) ;
220  }
221 
222  pBankPMDP->header.CRC = 0;
223 
224  //do whatever else needs to be done
225  int Token = pBankPMDP->header.Token;
226  Bank_DATAP *dp = (Bank_DATAP *)ercpy->getDATAP();
227  if(Token !=dp->header.Token){
228  printf("Token mismatch between global %d and PMD %d\n",dp->header.Token,Token);
229  }
230 
231  for(int sec=0; sec < 2; sec++){
232  for(int cram=0; cram<PMD_CRAMS_MAX; cram++){
233  for(int blk=0; blk < 2; blk ++){
234  for(int channel=0; channel<PMD_CRAMS_CH_MAX; channel++){
235  mThePmd.adc[sec][cram][blk][channel] = 0;
236  mThePmd.ped[sec][cram][blk][channel] = 0;
237  mThePmd.rms[sec][cram][blk][channel] = 0;
238  mThePmd.thr[sec][cram][blk][channel] = 0;
239  }
240  }
241  }
242  }
243 // mTheRichArray.ByteSwapped = 0x04030201; // This is to signal Endianness if struct is saved
244 
245 
246  ProcessEvent(pBankPMDP); // switch locations in little endian machines
247 
248  }
249 Bank_DATA PMD_Reader::getPMD_ADC()
250 {
251  return mThePmd;
252 }
253 
254 int PMD_Reader::NPMDHits()
255 {
256  /* if(mPmdPresent)
257  {
258  if(strncmp(mTheTowerAdcR.BankType,"TOWRADCR",8))
259  {
260  cout<<"EMC_Reader::NTowerHits() -> error in header"<<endl;
261  return 0;
262  }
263 
264  int nhits=mThePmdAdcD.NPPMDHits;
265 */
266  int nhits = 10;
267  return nhits;
268 // }
269  //else return 0;
270 }
271 
272 int PMD_Reader::NCPVHits()
273 {
274  /* if(mPmdPresent)
275  {
276  if(strncmp(mTheTowerAdcR.BankType,"TOWRADCR",8))
277  {
278  cout<<"EMC_Reader::NTowerHits() -> error in header"<<endl;
279  return 0;
280  }
281 
282  int nhits=mThePmdAdcD.NPPMDHits;
283 */
284  int nhits = 10;
285  return nhits;
286 // }
287  //else return 0;
288 }
289 
290 int PMD_Reader::pedReader(int sec, int type, Bank_PMDPEDR *pedr)
291 {
292  pedr->swap();
293  int items = pedr->header.BankLength -(INT32)sizeof(Bank_Header)/4 ;
294  printf("intem = %d, 1st=%d, 3nd=%d\n", items, pedr->header.BankLength, (INT32)sizeof(Bank_Header)/4);
295  unsigned int *datum ;
296  int cram, blk, ch ;
297  int ch_num ;
298  int valid ;
299 
300  if(items <= 0) return 0 ;
301 
302  datum = (unsigned int *)pedr->data ;
303 
304  ch_num = 0 ;
305 
306  //printf("PMD sec %d: items %d\n",sec,items) ;
307 
308  unsigned int *end_datum = datum + items ;
309 
310  for(end_datum=datum+items;datum<end_datum;) {
311  int channel ;
312  unsigned int tmp = (*datum) ;
313  datum++ ;
314 
315  cram = (tmp & 0x7F00) >> 8 ;
316  blk = (tmp & 0x00FF) ;
317 
318  valid = tmp & 0x8000 ;
319 
320 
321  ch = PMD_CRAMS_CH_MAX ;
322 
323  if(!valid) {
324  //printf("Datum not valid at %d == 0x%04X",end_datum, tmp) ;
325 
326  datum += ch ;
327  continue ;
328  }
329 
330 
331  if(cram >= PMD_CRAMS_MAX) {
332  printf("PMD: Bad CRAM number %d",cram) ;
333  return 0 ;
334  }
335 
336  if((blk != 0) && (blk != 1)) {
337  printf("PMD: Bad Block %d in CRAM %d",blk,cram) ;
338  return 0 ;
339  }
340 
341 
342  for(channel=0;channel<ch;channel++) {
343  unsigned int val ;
344 
345  val = (*datum) ;
346  datum++ ;
347 
348 
349  //printf(" %4d == %4d (0x%04X)\n",channel,val,val) ;
350 
351  switch(type) {
352  case PMD_PEDR_N :
353  mThePmd.ped[sec][cram][blk][channel] = val;
354  break ;
355  case PMD_RMSR_N :
356  mThePmd.rms[sec][cram][blk][channel] = val ;
357  break ;
358  case PMD_THRR_N :
359  mThePmd.thr[sec][cram][blk][channel] = val ;
360  break ;
361  }
362  ch_num++ ;
363 
364  }
365  }
366 
367  return ch_num ;
368 }
PMD_Reader(EventReader *er, Bank_PMDP *pPMDP)
Process PMD+CPV events.
Definition: PMD_Reader.cxx:206
int adcReader(int sec, Bank_PMDADCD *adcd)
PMD_Reader destructor.
Definition: PMD_Reader.cxx:134