StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StMtdTrigUtil.cxx
1 #include <iostream>
2 #include "StMessMgr.h"
3 
4 #include "StEnumerations.h"
5 #include "StEvent.h"
6 #include "StEvent/StTriggerData.h"
7 #include "StMtdCollection.h"
8 #include "StMtdHeader.h"
9 #include "StMuDSTMaker/COMMON/StMuDstMaker.h"
10 #include "StMuDSTMaker/COMMON/StMuDst.h"
11 #include "StMuDSTMaker/COMMON/StMuEvent.h"
12 #include "StMuDSTMaker/COMMON/StMuMtdHeader.h"
13 #include "StPicoDstMaker/StPicoDstMaker.h"
14 #include "StPicoEvent/StPicoDst.h"
15 #include "StPicoEvent/StPicoEvent.h"
16 #include "StPicoEvent/StPicoMtdTrigger.h"
17 #include "tables/St_mtdModuleToQTmap_Table.h"
18 #include "tables/St_mtdQTSlewingCorr_Table.h"
19 #include "tables/St_mtdQTSlewingCorrPart2_Table.h"
20 
21 #include "StMtdTrigUtil.h"
22 
23 ClassImp(StMtdTrigUtil)
24 
25 //_____________________________________________________________________________
26 StMtdTrigUtil::StMtdTrigUtil(const Char_t *name) : StMaker(name)
27 {
28  mRunYear = -1;
29  mStEvent = NULL;
30  mMuDst = NULL;
31  mPicoDst = NULL;
32  mPosCorrToQTtac = kTRUE;
33  reset();
34 }
35 
36 //_____________________________________________________________________________
37 void StMtdTrigUtil::reset()
38 {
39  mVpdTacSum = 0;
40  mTF201TriggerBit = 0;
41 
42  memset(mTHUBtime,0,sizeof(mTHUBtime));
43  memset(mQtTacSum,0,sizeof(mQtTacSum));
44  memset(mMT101Tac,0,sizeof(mMT101Tac));
45  memset(mMT101Id,0,sizeof(mMT101Id));
46  memset(mQtPosTrig,-1,sizeof(mQtPosTrig));
47  memset(mQtPosHighestTwo,-1,sizeof(mQtPosHighestTwo));
48  memset(mQtTacSumHighestTwo,0,sizeof(mQtTacSumHighestTwo));
49 }
50 
51 //_____________________________________________________________________________
52 StMtdTrigUtil::~StMtdTrigUtil()
53 {
54 }
55 
56 //_____________________________________________________________________________
57 Int_t StMtdTrigUtil::Init()
58 {
59  return kStOK;
60 }
61 
62 //_____________________________________________________________________________
64 {
65  Int_t iret = kStOK;
66 
67  // initilization
68  reset();
69 
70  // Check the availability of input data
71  if(GetInputDS("StEvent"))
72  {
73  LOG_DEBUG << "Running on StEvent ..." << endm;
74  mStEvent = (StEvent*) GetInputDS("StEvent");
75  extractTrigInfo((StTriggerData*)mStEvent->triggerData());
76  }
77  else if(GetMaker("MuDst"))
78  {
79  LOG_DEBUG << "Running on MuDst ..." << endm;
80  StMuDstMaker *muDstMaker = (StMuDstMaker*) GetMaker("MuDst");
81  mMuDst = muDstMaker->muDst();
82  extractTrigInfo(const_cast<StTriggerData*>(mMuDst->event()->triggerData()));
83  }
84  else if(GetMaker("picoDst"))
85  {
86  LOG_DEBUG << "Running on PicoDst ..." << endm;
87  StPicoDstMaker *picoDstMaker = (StPicoDstMaker*) GetMaker("picoDst");
88  mPicoDst = picoDstMaker->picoDst();
89  extractTrigInfo(mPicoDst);
90  }
91  else
92  {
93  LOG_ERROR << "Input file format unrecongnized ..." << endm;
94  return kStErr;
95  }
96  findFireQT();
97  return iret;
98 }
99 
100 //_____________________________________________________________________________
101 void StMtdTrigUtil::extractTrigInfo(StTriggerData *trigData)
102 {
103  // check availability of input data
104  if(!trigData)
105  {
106  LOG_ERROR << "No trigger data available ..." << endm;
107  return;
108  }
109 
110  int runnumber = -1;
111  if(mStEvent) runnumber = mStEvent->runId();
112  else if(mMuDst) runnumber = mMuDst->event()->runNumber();
113  else
114  {
115  LOG_ERROR << "Invalid input data format ..." << endm;
116  return;
117  }
118  int year = runnumber / 1e6 + 1999;
119  if ((runnumber % 1000000) / 1000 >= 273) year += 1;
120 
121  // THUB time
122  if(mStEvent)
123  {
124  StMtdCollection *mtdCollection = mStEvent->mtdCollection();
125  if(mtdCollection)
126  {
127  StMtdHeader *mtdHeader = mtdCollection->mtdHeader();
128  if(mtdHeader)
129  {
130  mTHUBtime[0] = 25 * (mtdHeader->triggerTime(0)&0xfff);
131  mTHUBtime[1] = 25 * (mtdHeader->triggerTime(1)&0xfff);
132  }
133  }
134  }
135  else
136  {
137  StMuMtdHeader *muMtdHeader = mMuDst->mtdHeader();
138  if(muMtdHeader)
139  {
140  mTHUBtime[0] = 25 * ( muMtdHeader->triggerTime(1) & 0xfff );
141  mTHUBtime[1] = 25 * ( muMtdHeader->triggerTime(2) & 0xfff );
142  }
143  }
144 
145  // VPD TacSum
146  mVpdTacSum = trigData->vpdEarliestTDCHighThr(east,0) + trigData->vpdEarliestTDCHighThr(west,0);
147 
148  // QT
149  Int_t mtd_qt_tac_max = 4095;
150  Int_t mtd_qt_tac_min = 100;
151  if (runnumber >= 16045067) mtd_qt_tac_min = 80;
152  if (runnumber >= 18070005) mtd_qt_tac_min = 200; // change due to new boards
153  Int_t mtd_qt_tac_diff_range_abs = 600;
154  if (year == 2015) mtd_qt_tac_diff_range_abs = 1023;
155 
156  Int_t mtdQTtac[kNQTboard][16];
157  Int_t mtdQTadc[kNQTboard][16];
158  for(Int_t i=0; i<32; i++)
159  {
160  Int_t type = (i/4)%2;
161  if(year<=2015)
162  {
163  if(type==1)
164  {
165  mtdQTtac[0][i-i/4*2-2] = trigData->mtdAtAddress(i,0);
166  mtdQTtac[1][i-i/4*2-2] = trigData->mtdgemAtAddress(i,0);
167  mtdQTtac[2][i-i/4*2-2] = trigData->mtd3AtAddress(i,0);
168  mtdQTtac[3][i-i/4*2-2] = trigData->mtd4AtAddress(i,0);
169  }
170  else
171  {
172  mtdQTadc[0][i-i/4*2] = trigData->mtdAtAddress(i,0);
173  mtdQTadc[1][i-i/4*2] = trigData->mtdgemAtAddress(i,0);
174  mtdQTadc[2][i-i/4*2] = trigData->mtd3AtAddress(i,0);
175  mtdQTadc[3][i-i/4*2] = trigData->mtd4AtAddress(i,0);
176  }
177  }
178  else
179  {
180  for(int im=0; im<kNQTboard; im++)
181  {
182  if(type==0) mtdQTadc[im][i-i/4*2] = trigData->mtdQtAtCh(im+1,i,0);
183  else mtdQTtac[im][i-i/4*2-2] = trigData->mtdQtAtCh(im+1,i,0);
184  }
185  }
186  }
187 
188  Int_t j[2] = {0, 0};
189  Int_t a[2] = {0, 0};
190  for(Int_t im=0; im<kNQTboard; im++)
191  {
192  if(year<=2015 && im>3) continue;
193  for(int i=0; i<8; i++)
194  {
195  if(year==2016 && i%2 == 0) // monitor channel
196  {
197  mQtTacSum[im][i] = 0;
198  continue;
199  }
200 
201 
202  // Apply slewing correction
203  for (Int_t k=0; k<2; k++)
204  {
205  j[k] = mtdQTtac[im][i*2+k];
206  a[k] = mtdQTadc[im][i*2+k];
207 
208  Int_t slew_bin = -1;
209  if (a[k]>0 && a[k]<=mQTSlewBinEdge[im][i*2+k][0])
210  {
211  slew_bin = 0;
212  }
213  else
214  {
215  for (Int_t l=1; l<8; l++)
216  {
217  if (a[k] > mQTSlewBinEdge[im][i*2+k][l-1] && a[k]<=mQTSlewBinEdge[im][i*2+k][l])
218  {
219  slew_bin = l;
220  break;
221  }
222  }
223  }
224  if (slew_bin >= 0)
225  {
226  j[k] += mQTSlewCorr[im][i * 2 + k][slew_bin];
227  }
228  }
229 
230  // no "equal" in online algorithm
231  if (j[0] <= mtd_qt_tac_min || j[0] >= mtd_qt_tac_max ||
232  j[1] <= mtd_qt_tac_min || j[1] >= mtd_qt_tac_max ||
233  fabs(j[0] - j[1]) >= mtd_qt_tac_diff_range_abs)
234  {
235  mQtTacSum[im][i] = 0;
236  continue;
237  }
238 
239  // Apply position correction
240  Int_t sumTac = j[0] + j[1];
241  if(mPosCorrToQTtac)
242  {
243  Int_t module = mQTtoModule[im][i];
244  if(module<0) sumTac = 0;
245  else sumTac = Int_t( j[0] + j[1] + abs(module-3)*1./8 * (j[0]-j[1]) );
246  }
247  mQtTacSum[im][i] = sumTac;
248  }
249  }
250 
251  // MT101
252  for(Int_t i = 0; i < kNQTboard; i++)
253  {
254  //if(year<=2015 && i>3) continue;
255  int idx = 0;
256  if(year==2016) idx = i / 2 * 3 + i % 2 * 16;
257  else idx = i * 3;
258  mMT101Tac[i][0] = (trigData->mtdDsmAtCh(idx, 0)) + ((trigData->mtdDsmAtCh(idx + 1, 0) & 0x3) << 8);
259  mMT101Id[i][0] = (trigData->mtdDsmAtCh(idx + 1, 0) & 0xc) >> 2;
260  mMT101Tac[i][1] = (trigData->mtdDsmAtCh(idx + 1, 0) >> 4) + ((trigData->mtdDsmAtCh(idx + 2, 0) & 0x3f) << 4);
261  mMT101Id[i][1] = (trigData->mtdDsmAtCh(idx + 2, 0) & 0xc0) >> 6;
262  }
263 
264  // TF201
265  Int_t dsmBit1 = trigData->dsmTF201Ch(0);
266  Int_t dsmBit2 = 0;
267  if (year == 2016) dsmBit2 = trigData->dsmTF201Ch(6);
268  for (Int_t i = 0; i < 4; i++)
269  {
270  for (Int_t j = 0; j < 2; j++)
271  {
272  if (year == 2016)
273  {
274  int qt = i * 2;
275  mTF201TriggerBit |= ((dsmBit1 >> (i * 2 + j + 4)) & 0x1) << (qt * 2 + j);
276  qt = i * 2 + 1;
277  mTF201TriggerBit |= ((dsmBit2 >> (i * 2 + j + 4)) & 0x1) << (qt * 2 + j);
278  }
279  else
280  {
281  int qt = i;
282  mTF201TriggerBit |= ((dsmBit1 >> (i * 2 + j + 4)) & 0x1) << (qt * 2 + j);
283  }
284  }
285  }
286 }
287 
288 //_____________________________________________________________________________
289 void StMtdTrigUtil::extractTrigInfo(StPicoDst *picoDst)
290 {
291  if(!picoDst)
292  {
293  LOG_ERROR << "No picoDst files available ... " << endm;
294  return;
295  }
296 
297  StPicoMtdTrigger *mtdTrig = picoDst->mtdTrigger(0);
298  if(!mtdTrig)
299  {
300  LOG_ERROR << "No StPicoMtdTrigger available ... " << endm;
301  return;
302  }
303 
304  // VPD tac
305  mVpdTacSum = mtdTrig->getVpdTacSum();
306 
307  // QT
308  for(Int_t im=0; im<kNQTboard; im++)
309  {
310  for(Int_t i=0; i<8; i++)
311  {
312  mQtTacSum[im][i] = mtdTrig->getQTtacSum(im+1,i+1);
313  }
314  }
315 
316  // MT101
317  for(Int_t i = 0; i < kNQTboard; i++)
318  {
319  for(Int_t j=0; j<2; j++)
320  {
321  mMT101Tac[i][j] = mtdTrig->getMT101Tac(i+1,j);
322  mMT101Id[i][j] = mtdTrig->getMT101Id(i+1,j);
323  }
324  }
325 
326  // TF201
327  mTF201TriggerBit = mtdTrig->getTF201TriggerBit();
328 }
329 
330 
331 
332 //_____________________________________________________________________________
333 void StMtdTrigUtil::findFireQT()
334 {
335  // QT
336  Int_t mxq_tacsum[kNQTboard][2];
337  Int_t mxq_tacsum_pos[kNQTboard][2];
338  for(Int_t i=0; i<kNQTboard; i++)
339  {
340  for(Int_t j=0; j<2; j++)
341  {
342  mxq_tacsum[i][j] = 0; // to reject all zero values
343  mxq_tacsum_pos[i][j] = -1;
344  }
345  }
346 
347  for(Int_t im=0; im<kNQTboard; im++)
348  {
349  for(Int_t i=0; i<8; i++)
350  {
351  Int_t sumTac = mQtTacSum[im][i];
352 
353  if(mxq_tacsum[im][0] < sumTac)
354  {
355  mxq_tacsum[im][1] = mxq_tacsum[im][0];
356  mxq_tacsum[im][0] = sumTac;
357 
358  mxq_tacsum_pos[im][1] = mxq_tacsum_pos[im][0];
359  mxq_tacsum_pos[im][0] = i+1;
360  }
361  else if (mxq_tacsum[im][1] < sumTac)
362  {
363  mxq_tacsum[im][1] = sumTac;
364  mxq_tacsum_pos[im][1] = i+1;
365  }
366  }
367  mQtPosHighestTwo[im][0] = mxq_tacsum_pos[im][0];
368  mQtPosHighestTwo[im][1] = mxq_tacsum_pos[im][1];
369  mQtTacSumHighestTwo[im][0] = mxq_tacsum[im][0];
370  mQtTacSumHighestTwo[im][1] = mxq_tacsum[im][1];
371  }
372 
373  for(Int_t im = 0; im < kNQTboard; im++)
374  {
375  for(Int_t j=0; j<2; j++)
376  {
377  if((mTF201TriggerBit>>(im*2+j))&0x1)
378  {
379  mQtPosTrig[im][j] = mQtPosHighestTwo[im][j];
380  }
381  else
382  {
383  mQtPosTrig[im][j] = -1;
384  }
385  }
386  }
387 }
388 
389 //_____________________________________________________________________________
390 Bool_t StMtdTrigUtil::isQtFireTrigger(const Int_t qt, const Int_t pos)
391 {
392  if(qt<1) return kFALSE;
393  return (pos==mQtPosTrig[qt-1][0] || pos==mQtPosTrig[qt-1][1]);
394 }
395 
396 //_____________________________________________________________________________
397 Bool_t StMtdTrigUtil::isQtHighestTwo(const int qt, const int pos)
398 {
399  if(qt<1) return kFALSE;
400  return (pos==mQtPosHighestTwo[qt-1][0] || pos==mQtPosHighestTwo[qt-1][1]);
401 }
402 
403 //_____________________________________________________________________________
404 Int_t StMtdTrigUtil::getHitTimeInQT(const int backleg, const int module)
405 {
406  if(backleg<1 || backleg>30 || module<1 || module>5) return kFALSE;
407  return mQtTacSum[mModuleToQT[backleg-1][module-1]-1][mModuleToQTPos[backleg-1][module-1]-1];
408 }
409 
410 //_____________________________________________________________________________
411 Int_t StMtdTrigUtil::getHitTimeDiffToVPDInQT(const int backleg, const int module)
412 {
413  return getHitTimeInQT(backleg,module)/8 - mVpdTacSum/8 + 1024;
414 }
415 
416 //_____________________________________________________________________________
417 Int_t StMtdTrigUtil::getHitTimeDiffToVPDInMT101(const int backleg, const int module)
418 {
419  Int_t qt = mModuleToQT[backleg-1][module-1];
420  Int_t pos = mModuleToQTPos[backleg-1][module-1];
421  Int_t index = -1;
422  if(pos==mQtPosHighestTwo[qt-1][0]) index = 0;
423  if(pos==mQtPosHighestTwo[qt-1][1]) index = 1;
424  if(index==-1) return 0;
425  else return (getMT101Tac(qt, index) - mVpdTacSum/8 + 1024);
426 }
427 
428 //_____________________________________________________________________________
429 Bool_t StMtdTrigUtil::isHitFireTrigger(const Int_t backleg, const Int_t module)
430 {
431  if(backleg<1 || backleg>30 || module<1 || module>5) return kFALSE;
432  return isQtFireTrigger( mModuleToQT[backleg-1][module-1], mModuleToQTPos[backleg-1][module-1] );
433 }
434 
435 //_____________________________________________________________________________
436 Bool_t StMtdTrigUtil::isHitHighestTwo(const Int_t backleg, const Int_t module)
437 {
438  if(backleg<1 || backleg>30 || module<1 || module>5) return kFALSE;
439  return isQtHighestTwo( mModuleToQT[backleg-1][module-1], mModuleToQTPos[backleg-1][module-1]);
440 }
441 
442 //_____________________________________________________________________________
443 Int_t StMtdTrigUtil::InitRun(const Int_t runNumber)
444 {
445  // initialize maps
446  memset(mModuleToQT,-1,sizeof(mModuleToQT));
447  memset(mModuleToQTPos,-1,sizeof(mModuleToQTPos));
448  memset(mQTtoModule,-1,sizeof(mQTtoModule));
449 
450  // obtain maps from DB
451  LOG_INFO << "Retrieving mtdModuleToQTmap table from database ..." << endm;
452  TDataSet *dataset = GetDataBase("Geometry/mtd/mtdModuleToQTmap");
453  St_mtdModuleToQTmap *mtdModuleToQTmap = static_cast<St_mtdModuleToQTmap*>(dataset->Find("mtdModuleToQTmap"));
454  if(!mtdModuleToQTmap)
455  {
456  LOG_ERROR << "No mtdModuleToQTmap table found in database" << endm;
457  return kStErr;
458  }
459  mtdModuleToQTmap_st *mtdModuleToQTtable = static_cast<mtdModuleToQTmap_st*>(mtdModuleToQTmap->GetTable());
460 
461  for(Int_t i=0; i<gMtdNBacklegs; i++)
462  {
463  for(Int_t j=0; j<gMtdNModules; j++)
464  {
465  Int_t index = i*5 + j;
466  Int_t qt = mtdModuleToQTtable->qtBoardId[index];
467  Int_t channel = mtdModuleToQTtable->qtChannelId[index];
468  mModuleToQT[i][j] = qt;
469  if(channel<0)
470  {
471  mModuleToQTPos[i][j] = channel;
472  }
473  else
474  {
475  if(channel%8==1) mModuleToQTPos[i][j] = 1 + channel/8 * 2;
476  else mModuleToQTPos[i][j] = 2 + channel/8 * 2;
477  }
478  if(mModuleToQT[i][j]>0 && mModuleToQTPos[i][j]>0)
479  mQTtoModule[mModuleToQT[i][j]-1][mModuleToQTPos[i][j]-1] = j + 1;
480  }
481  }
482 
483  // online slewing correction for QT board
484  memset(mQTSlewBinEdge,-1,sizeof(mQTSlewBinEdge));
485  memset(mQTSlewCorr,-1,sizeof(mQTSlewCorr));
486  LOG_INFO << "Retrieving mtdQTSlewingCorr table from database ..." << endm;
487 
488  dataset = GetDataBase("Calibrations/mtd/mtdQTSlewingCorr");
489  if(!dataset)
490  {
491  LOG_ERROR << "No dataset for mtdQTSlewingCorr found in database" << endm;
492  return kStErr;
493  }
494  St_mtdQTSlewingCorr *mtdQTSlewingCorr = static_cast<St_mtdQTSlewingCorr*>(dataset->Find("mtdQTSlewingCorr"));
495  if(!mtdQTSlewingCorr)
496  {
497  LOG_ERROR << "No mtdQTSlewingCorr table found in database" << endm;
498  return kStErr;
499  }
500  mtdQTSlewingCorr_st *mtdQTSlewingCorrtable = static_cast<mtdQTSlewingCorr_st*>(mtdQTSlewingCorr->GetTable());
501  for(int j=0; j<4; j++)
502  {
503  for(int i=0; i<16; i++)
504  {
505  for(Int_t k=0; k<8; k++)
506  {
507  Int_t index = j*16*8 + i*8 + k;
508  mQTSlewBinEdge[j][i][k] = (int) mtdQTSlewingCorrtable->slewingBinEdge[index];
509  mQTSlewCorr[j][i][k] = (int) mtdQTSlewingCorrtable->slewingCorr[index];
510  }
511  }
512  }
513 
514  LOG_INFO << "Retrieving mtdQTSlewingCorrPart2 table from database ..." << endm;
515  dataset = GetDataBase("Calibrations/mtd/mtdQTSlewingCorrPart2");
516  if(dataset)
517  {
518  St_mtdQTSlewingCorrPart2 *mtdQTSlewingCorr2 = static_cast<St_mtdQTSlewingCorrPart2*>(dataset->Find("mtdQTSlewingCorrPart2"));
519  mtdQTSlewingCorrPart2_st *mtdQTSlewingCorrtable2 = static_cast<mtdQTSlewingCorrPart2_st*>(mtdQTSlewingCorr2->GetTable());
520  for(int j=0; j<4; j++)
521  {
522  for(int i=0; i<16; i++)
523  {
524  for(Int_t k=0; k<8; k++)
525  {
526  Int_t index = j*16*8 + i*8 + k;
527  mQTSlewBinEdge[j+4][i][k] = (int) mtdQTSlewingCorrtable2->slewingBinEdge[index];
528  mQTSlewCorr[j+4][i][k] = (int) mtdQTSlewingCorrtable2->slewingCorr[index];
529  }
530  }
531  }
532  }
533 
534  LOG_INFO << "===== End retrieving mtdQTSlewingCorr =====" << endm;
535 
536  return kStOK;
537 }
538 
539 //
545 
Class that converts MuDst into PicoDst.
StMuDst * muDst()
Definition: StMuDstMaker.h:425
Class storing MTD trigger information including VPD, QT, MT101, TF201.
UShort_t getMT101Tac(const Int_t qt, const Int_t index) const
UInt_t getTF201TriggerBit() const
Main class that keeps TClonesArrays with main classes.
Definition: StPicoDst.h:40
StPicoDst * picoDst()
Returns null pointer if no StPicoDst.
UShort_t getMT101Id(const Int_t qt, const Int_t index) const
static StPicoMtdTrigger * mtdTrigger(Int_t i)
Return pointer to i-th MTD trigger data.
Definition: StPicoDst.h:69
static StMuEvent * event()
returns pointer to current StMuEvent (class holding the event wise information, e.g. event number, run number)
Definition: StMuDst.h:320
Definition: Stypes.h:40
UShort_t getQTtacSum(const Int_t qt, const Int_t pos) const
Definition: Stypes.h:44
UShort_t getVpdTacSum() const
VPD tag sum.
virtual TDataSet * Find(const char *path) const
Definition: TDataSet.cxx:362