StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
gl3EMC.cxx
1 #ifndef TRG_VERSION
2 #define TRG_VERSION 0x32
3 #endif
4 
5 #include "gl3EMC.h"
6 
7 #include "gl3Event.h"
8 
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <fcntl.h>
12 #include <unistd.h>
13 #include <errno.h>
14 #include <stdio.h>
15 #include <math.h>
16 
17 #ifndef OLD_DAQ_READER
18 #include <DAQ_READER/daq_dta.h>
19 #endif /* OLD_DAQ_READER */
20 
21 #include "daqFormats.h"
22 #include "l3BankUtils.h"
23 #include "l3Swap.h"
24 #include <rtsLog.h>
25 
26 #ifndef OLD_DAQ_READER
27 #include <DAQ_EMC/daq_emc.h>
28 #endif /* OLD_DAQ_READER */
29 
30 gl3EMC::gl3EMC(l3EmcCalibration *BarrelCalib, l3EmcCalibration *EndcapCalib)
31 {
32  barrelCalib = BarrelCalib;
33  endcapCalib = EndcapCalib;
34 
35  if (barrelCalib)
36  nBarrelTowers = barrelCalib->getNTowers();
37  else
38  nBarrelTowers = 0;
39 
40  if (endcapCalib)
41  nEndcapTowers = endcapCalib->getNTowers();
42  else
43  nEndcapTowers = 0;
44 
45  nTotalTowers = nBarrelTowers + nEndcapTowers;
46 
47  tower = new gl3EmcTower[nTotalTowers];
48  barrelTower = tower;
49  endcapTower = tower + nBarrelTowers;
50 
51  for (int i=0; i<nBarrelTowers; i++) {
52  barrelTower[i].setTowerInfo(barrelCalib->getTowerInfo(i));
53  }
54 
55  for (int i=0; i<nEndcapTowers; i++) {
56  endcapTower[i].setTowerInfo(endcapCalib->getTowerInfo(i));
57  }
58 
59  reset();
60 }
61 
62 gl3EMC::~gl3EMC() {
63  delete[] tower;
64 }
65 
66 
67 #ifdef OLD_DAQ_READER
68 int gl3EMC::readFromEvpReader(evpReader *evp, char *mem)
69 #else /* OLD_DAQ_READER */
70 int gl3EMC::readFromEvpReader(daqReader *rdr, char *mem)
71 #endif /* OLD_DAQ_READER */
72 {
73  int i,j;
74 #ifdef OLD_DAQ_READER
75  int ret = emcReader(mem);
76 #endif /* ! OLD_DAQ_READER */
77 
78 #ifdef OLD_DAQ_READER
79  if(ret <= 0) {
80 #else /* OLD_DAQ_READER */
81  daq_dta *dd = rdr->det("emc_pseudo")->get("legacy");
82 
83  if(!dd) {
84 #endif /* OLD_DAQ_READER */
85  LOG(NOTE, "No EMC data present...",0,0,0,0,0);
86  return 0;
87  }
88 
89 #ifndef OLD_DAQ_READER
90  dd->iterate();
91  emc_t *pEMC = (emc_t *)dd->Void;
92 
93 #endif /* OLD_DAQ_READER */
94  // First zero out whatever we have...
95  for(i=0;i<nBarrelTowers;i++) {
96  barrelTower[i].setADC(0);
97  barrelTower[i].setNTracks(0);
98  }
99 
100  for(i=0;i<nEndcapTowers;i++) {
101  endcapTower[i].setADC(0);
102  endcapTower[i].setNTracks(0);
103  }
104 
105  // Do some checks...
106  if(barrelCalib) {
107  if(nBarrelTowers < BTOW_MAXFEE * BTOW_DATSIZE) {
108  LOG(WARN, "nBarrelTowers = %d, smaller than %d",nBarrelTowers,BTOW_MAXFEE * BTOW_DATSIZE,0,0,0);
109  }
110  }
111 
112  if(endcapCalib) {
113  if(nEndcapTowers < ETOW_MAXFEE * ETOW_DATSIZE) {
114  LOG(WARN, "nEndcapTowers = %d, smaller than %d",nEndcapTowers,ETOW_MAXFEE * ETOW_DATSIZE,0,0,0);
115  }
116  }
117 
118  // Read the btow, if its there...
119  if(barrelCalib) {
120 #ifdef OLD_DAQ_READER
121  if(emc.btow_in) {
122 #else /* OLD_DAQ_READER */
123  if(pEMC->btow_in) {
124 #endif /* OLD_DAQ_READER */
125  LOG(DBG, "Reading BTOW data",0,0,0,0,0);
126  for(i=0;i<BTOW_MAXFEE*BTOW_DATSIZE;i++) {
127 
128  int daqid = i; // linearized
129  int id = barrelCalib->daqToId(daqid); // emc uses different linearization
130 
131 #ifdef OLD_DAQ_READER
132  if(emc.btow[i] != 0)
133  LOG(DBG, "i=%d id=%d adc=%d",daqid,id,emc.btow[i]);
134 #else /* OLD_DAQ_READER */
135  if(pEMC->btow[i] != 0)
136  LOG(DBG, "i=%d id=%d adc=%d",daqid,id,pEMC->btow[i]);
137 #endif /* OLD_DAQ_READER */
138 
139  if(id >= nBarrelTowers) continue;
140 
141 #ifdef OLD_DAQ_READER
142  barrelTower[id].setADC(emc.btow[i]);
143 #else /* OLD_DAQ_READER */
144  barrelTower[id].setADC(pEMC->btow[i]);
145 #endif /* OLD_DAQ_READER */
146  barrelTower[id].setNTracks(0);
147  }
148  }
149  }
150 
151  // Read the etow, if its there...
152  if(endcapCalib) {
153 #ifdef OLD_DAQ_READER
154  if(emc.etow_in) {
155 #else /* OLD_DAQ_READER */
156  if(pEMC->etow_in) {
157 #endif /* OLD_DAQ_READER */
158  LOG(DBG, "Reading etow data",0,0,0,0,0);
159  for(i=0;i<ETOW_MAXFEE;i++) {
160  for(j=0;j<ETOW_DATSIZE;j++) {
161 
162  int daqid = i + j*ETOW_MAXFEE; // linearized (note linearization seems backward to me!)
163  int id = endcapCalib->daqToId(daqid); // emc uses different linearization
164 
165  if(id >= nEndcapTowers) continue;
166 
167 #ifdef OLD_DAQ_READER
168  if(emc.etow[i][j] != 0)
169  LOG(DBG, "etow: i=%d j=%d daqid=%d id=%d adc=%d",i,j,daqid,id, emc.etow[i][j]);
170 #else /* OLD_DAQ_READER */
171  if(pEMC->etow[i][j] != 0)
172  LOG(DBG, "etow: i=%d j=%d daqid=%d id=%d adc=%d",i,j,daqid,id, pEMC->etow[i][j]);
173 #endif /* OLD_DAQ_READER */
174 
175 #ifdef OLD_DAQ_READER
176  endcapTower[id].setADC(emc.etow[i][j]);
177 #else /* OLD_DAQ_READER */
178  endcapTower[id].setADC(pEMC->etow[i][j]);
179 #endif /* OLD_DAQ_READER */
180  endcapTower[id].setNTracks(0);
181  }
182  }
183  }
184  }
185  return 0;
186 }
187 
188 
189 int gl3EMC::readRawData(L3_P *l3p)
190 {
191 
192  int ret=0;
193 
194  if (l3p->emc[0].len > 10000) {
195  LOG(ERR,"Illegal length of BTOW contribution",0,0,0,0,0);
196  return 0;
197  }
198 
199  if (l3p->emc[3].len > 10000) {
200  LOG(ERR," ---> ERROR <--- Illegal length of ETOW contribution\n",0,0,0,0,0);
201  return 0;
202  }
203 
204  // Read BTOW
205  if (l3p->emc[0].len) {
206  if (readEMCSECP((EMCSECP*)offlen2ptr(l3p, l3p->emc[0]))) {
207  // something went wrong reading the data -> zero all towers
208 
209  for (int i=0; i < nBarrelTowers; i++) {
210  barrelTower[i].setADC(0);
211  barrelTower[i].setNTracks(0);
212  }
213  }
214  }
215 
216  if (l3p->emc[3].len) {
217  ret = readEMCSECP((EMCSECP*)offlen2ptr(l3p, l3p->emc[3]));
218  }
219 
220 
221  if (ret) {
222  // something went wrong reading the data, maybe there is some
223  // garbage in the data, so zero it
224 
225  LOG(ERR,"Error reading endcap",0,0,0,0,0);
226 
227  for (int i=0; i < nEndcapTowers; i++) {
228  endcapTower[i].setADC(0);
229  endcapTower[i].setNTracks(0);
230  }
231  }
232 
233  Ebarrel = 0.0;
234  for (int i=0; i < nBarrelTowers; i++) {
235  Ebarrel += barrelTower[i].getEnergy();
236  }
237 
238  Eendcap = 0.0;
239  for (int i=0; i < nEndcapTowers; i++) {
240  Eendcap += endcapTower[i].getEnergy();
241  }
242 
243  Etotal = Ebarrel + Eendcap;
244 
245  return 0;
246 }
247 
248 
249 int gl3EMC::readEMCSECP(EMCSECP* secp)
250 {
251 
252  enum {barrel, endcap} secpType;
253 
254  l3EmcCalibration *calib = NULL;
255  gl3EmcTower *twr = NULL;
256  int nCrates;
257 
258  if (!secp) {
259  return -2;
260  }
261 
262  if(strncmp(secp->bh.bank_type, CHAR_EMCSECP, 8) == 0) {
263  secpType = barrel;
264  calib = barrelCalib;
265  twr = barrelTower;
266  nCrates = 30;
267 
268  } else if (strncmp(secp->bh.bank_type, CHAR_EECSECP, 8) == 0) {
269  secpType = endcap;
270  calib = endcapCalib;
271  twr = endcapTower;
272  nCrates = 6;
273 
274  } else {
275  LOG(ERR,"Unknown bank type '%s'", secp->bh.bank_type,0,0,0,0);
276  return -3;
277  }
278 
279  if (calib == NULL) {
280  return -4;
281  }
282 
283 
284  // This really seems to be a data contribution, so let's read it
285 
286  int nFibers = (swap32(secp->bh.length)-10)/2;
287 
288  for (int i=0; i<nFibers; i++) {
289  EMCRBP* rbp = (EMCRBP*)offlen2ptr(secp, secp->fiber[i]);
290  if (!rbp) continue;
291 
292  int nBANKS = (swap32(rbp->bh.length)-10)/2;
293 
294  if (nBANKS < 1) {
295  LOG(ERR, "EMC data coming in without BTOW bank?!?!?\n",0,0,0,0,0);
296  return -5;
297  }
298 
299  bankHeader *dataBank = (bankHeader *)
300  offlen2ptr(rbp, rbp->banks[0]);
301 
302  if (!dataBank) continue;
303 
304  // Skip some headers:
305  // - 40 bytes bank header,
306  // - 4 bytes dummy
307  // - 128 bytes fiber header
308 
309  unsigned short* data = (unsigned short*)
310  (((char *)dataBank) + 40 + 4 + 128);
311 
312  // Check preamble
313  for(int c=0; c<nCrates; c++) { // Loop over all crates
314 
315  unsigned short cr_count = data[c+30*0];
316  unsigned short cr_errflag = data[c+30*1];
317  // unsigned short cr_token = data[c+30*2];
318  // unsigned short cr_trgcrt = data[c+30*3];
319 
320  if (secpType == barrel && c<15) {
321 
322  //l3Log("%d %d", c, cr_count);
323 
324  if (cr_count != 164) errFlags |= 1;
325  if (cr_errflag) errFlags |= 1;
326 
327  }
328 
329 
330  }
331 
332  // Now skip the preamble:
333  data += nCrates*4; // 4 shorts per crate
334 
335  for (int daqid=0; daqid < calib->getNTowers(); daqid++) {
336  int id = calib->daqToId(daqid);
337 
338  twr[id].setADC(data[daqid]);
339  twr[id].setNTracks(0);
340 
341  }
342  }
343 
344 
345  return 0;
346 }
347 
348 int gl3EMC::matchTracks(gl3Event *event)
349 {
350  LOG(ERR, "matchTracks currently not available",0,0,0,0,0);
351 
352  return -1;
353 
354  // l3xyzCoordinate extra;
355 
356  // for (int i=0; i<event->getNTracks(); i++) {
357  // gl3Track *track = event->getTrack(i);
358 
359  // extra = track->extraRadius(radius);
360 
361  // int id = etaPhiToId(extra.Geteta(), extra.Getphi());
362 
363  // tower[id].incrNTracks();
364  // }
365 
366  // return 0;
367 }
368 
369 void gl3EMC::reset()
370 {
371 
372  errFlags = 0;
373 
374  Etotal = 0.;
375  Ebarrel = 0.;
376  Eendcap = 0.;
377 
378  for (int i=0; i<nBarrelTowers; i++) {
379  barrelTower[i].setADC(0);
380  }
381 
382  for (int i=0; i<nEndcapTowers; i++) {
383  endcapTower[i].setADC(0);
384  }
385 
386 
387 }
Definition: daq_emc.h:52