StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StMtdHitMaker.cxx
1 /***************************************************************************
2  *
3  * $Id: StMtdHitMaker.cxx,v 1.25 2016/07/27 15:31:15 marr Exp $
4  *
5  * Author: Frank Geurts (Rice)
6  ***************************************************************************
7  *
8  * Description: Create the MTD data coming from the RTS_READER
9  *
10  * Input: RTS_Reader
11  * Output: MTD StEvent objects
12  *
13  ***************************************************************************
14  *
15  **************************************************************************/
16 #include "StMtdHitMaker.h"
17 #include "StEventTypes.h"
18 #include "StEvent/StMtdCollection.h"
19 #include "StEvent/StMtdHeader.h"
20 #include "StEvent/StMtdRawHit.h"
21 #include "StEvent/StEvent.h"
22 #include "StDAQMaker/StDAQReader.h"
23 #include "StRtsTable.h"
24 #include "DAQ_MTD/daq_mtd.h"
25 #include "StBTofUtil/StBTofINLCorr.h"
26 #include "StMuDSTMaker/COMMON/StMuDstMaker.h"
27 #include "StMuDSTMaker/COMMON/StMuEvent.h"
28 #include "StMuDSTMaker/COMMON/StMuDst.h"
29 #include "StMuDSTMaker/COMMON/StMuTrack.h"
30 #include "StMuDSTMaker/COMMON/StMuMtdCollection.h"
31 #include "StMuDSTMaker/COMMON/StMuMtdRawHit.h"
32 #include "StMuDSTMaker/COMMON/StMuMtdHeader.h"
33 #include "tables/St_mtdTrayToTdigMap_Table.h"
34 #include "tables/St_mtdTrayIdMap_Table.h"
35 #include "tables/St_mtdTdigIdMap_Table.h"
36 #include "tables/St_mtdTriggerTimeCut_Table.h"
37 
38 ClassImp(StMtdHitMaker);
39 
40 //_____________________________________________________________
41 StMtdHitMaker::StMtdHitMaker(const char *name):StRTSBaseMaker("mtd",name),
42  mStEvent(0),fMtd(0), mUseMuDst(0),
43  mCosmicFlag(kFALSE), mCosmicTrigTimeWinFile(""),
44  mTriggerWndSelection(kTRUE), mSwapBacklegInRun13(0),
45  mReverseStripInRun14(kFALSE),
46  mYear(-1),
47  mNValidTrays(-1),
48  mMtdCollection(0),
49  mINLCorr(0)
50 {
51  LOG_DEBUG << "StMtdHitMaker::ctor" << endm;
52 
53  Int_t mStripMap[24]={21,12,32,20,14,35,25,13,30,24,11,31,34,22,10,37,27,17,33,23,16,36,26,15};
54  for(int i=0;i<24;i++) mtdStrip[i] = mStripMap[i];
55  hxhyhz = new TH3D("hxhyhz","hxhyhz",10,19.5,29.5,10,-0.5,9.5,30,-0.5,29.5);
56 }
57 
58 
59 //_____________________________________________________________
60 StMtdHitMaker::~StMtdHitMaker()
61 {
62 }
63 
64 
65 //_____________________________________________________________
66 void StMtdHitMaker::Clear(Option_t* option)
67 {
68  MtdLeadingHits.clear();
69  MtdTrailingHits.clear();
70  memset(mTriggerTimeStamp,0,2);
71 }
72 
73 
74 //_____________________________________________________________
75 Int_t StMtdHitMaker::Init()
76 {
77  Clear("");
78 
79  memset(mTray2TdigMap,-1,sizeof(mTray2TdigMap));
80  memset(mTrayId,0,sizeof(mTrayId));
81  memset(mTdigId,0,sizeof(mTdigId));
82 
83  for(Int_t i=0;i<gMtdNModulesAll;i++)
84  {
85  mTriggerTimeWindow[i][0] = 0;
86  mTriggerTimeWindow[i][1] = 9999;
87  }
88  return kStOK;
89 }
90 
91 
92 //_____________________________________________________________
93 Int_t StMtdHitMaker::InitRun(Int_t runnumber)
94 {
96  mYear= (Int_t)(runnumber/1000000 -1);
97 
98  LOG_INFO << "Process data from year " << mYear << endm;
99 
100  // Extract MTD maps from database
101  LOG_INFO << "Retrieving mtdTrayToTdigMap table from database ..." << endm;
102  TDataSet *dataset = GetDataBase("Geometry/mtd/mtdTrayToTdigMap");
103  St_mtdTrayToTdigMap *mtdTrayToTdigMap = static_cast<St_mtdTrayToTdigMap*>(dataset->Find("mtdTrayToTdigMap"));
104  if(!mtdTrayToTdigMap)
105  {
106  LOG_ERROR << "No mtdTrayToTdigMap table found in database" << endm;
107  return kStErr;
108  }
109  mtdTrayToTdigMap_st *mTrayToTdigTable = static_cast<mtdTrayToTdigMap_st*>(mtdTrayToTdigMap->GetTable());
110  for(Int_t i=0; i<gMtdNBacklegs; i++)
111  {
112  for(Int_t j=0; j<gMtdNModules; j++)
113  mTray2TdigMap[i][j] = (Int_t)(mTrayToTdigTable->tdigId[i*5+j]);
114  }
115 
116  LOG_INFO << "Retrieving mtdTrayIdMap table from database ..." << endm;
117  dataset = GetDataBase("Geometry/mtd/mtdTrayIdMap");
118  St_mtdTrayIdMap *mtdTrayIdMap = static_cast<St_mtdTrayIdMap*>(dataset->Find("mtdTrayIdMap"));
119  if(!mtdTrayIdMap)
120  {
121  LOG_ERROR << "No mtdTrayIdMap table found in database" << endm;
122  return kStErr;
123  }
124  mtdTrayIdMap_st *mtdTrayIdTable = static_cast<mtdTrayIdMap_st*>(mtdTrayIdMap->GetTable());
125  for(Int_t i=0; i<gMtdNBacklegs; i++)
126  {
127  for(Int_t j=0; j<gMtdNModules; j++)
128  mTrayId[i][j] = (Int_t)(mtdTrayIdTable->trayId[i*5+j]);
129  }
130 
131  LOG_INFO << "Retrieving mtdTdigIdMap table from database ..." << endm;
132  dataset = GetDataBase("Geometry/mtd/mtdTdigIdMap");
133  St_mtdTdigIdMap *mtdTdigIdMap = static_cast<St_mtdTdigIdMap*>(dataset->Find("mtdTdigIdMap"));
134  if(!mtdTdigIdMap)
135  {
136  LOG_ERROR << "No mtdTdigIdMap table found in database" << endm;
137  return kStErr;
138  }
139  mtdTdigIdMap_st *mtdTdigIdTable = static_cast<mtdTdigIdMap_st*>(mtdTdigIdMap->GetTable());
140  for(Int_t i=0; i<gMtdNModulesAll; i++)
141  {
142  mTdigId[i] = (Int_t)(mtdTdigIdTable->tdigBoardId[i]);
143  }
144 
145  LOG_INFO << "Retrieving mtdTriggerTimeCut table from database ..." << endm;
146  dataset = GetDataBase("Calibrations/mtd/mtdTriggerTimeCut");
147  St_mtdTriggerTimeCut *mtdTriggerTimeCut = static_cast<St_mtdTriggerTimeCut*>(dataset->Find("mtdTriggerTimeCut"));
148  if(!mtdTriggerTimeCut)
149  {
150  LOG_ERROR << "No mtdTriggerTimeCut table found in database" << endm;
151  return kStErr;
152  }
153  mtdTriggerTimeCut_st *mtdTriggerTimeTable = static_cast<mtdTriggerTimeCut_st*>(mtdTriggerTimeCut->GetTable());
154  for(Int_t i=0; i<gMtdNModulesAll; i++)
155  {
156  mTriggerTimeWindow[i][0] = mtdTriggerTimeTable->minTriggerTime[i];
157  mTriggerTimeWindow[i][1] = mtdTriggerTimeTable->maxTriggerTime[i];
158  LOG_DEBUG << "(" << i/5 << "," << i%5 << "): " << mTriggerTimeWindow[i][0] << " - " << mTriggerTimeWindow[i][1] << endm;
159  }
160 
161  // pick up local trigger time window cuts for cosmic ray data
162  if(mTriggerWndSelection && mCosmicFlag && mCosmicTrigTimeWinFile.Length()>0)
163  {
164  LOG_WARN << "Local trigger time window cuts are picked up for cosmic ray data." << endm;
165  ifstream inData;
166  inData.open(mCosmicTrigTimeWinFile.Data());
167  Int_t backleg, module;
168 
169  for(Int_t i=0; i<gMtdNModulesAll; i++)
170  {
171  inData >> backleg >> module >> mTriggerTimeWindow[i][0] >> mTriggerTimeWindow[i][1];
172  }
173  inData.close();
174  }
175 
177  LOG_DEBUG << "Initializing INL table:" << endm;
178  mINLCorr = new StBTofINLCorr();
179  mINLCorr->initFromDbase(this);
180 
181  return kStOK;
182 }
183 
184 
185 //_____________________________________________________________
186 Int_t StMtdHitMaker::FinishRun(Int_t runnumber)
187 {
189  memset(mTray2TdigMap,-1,sizeof(mTray2TdigMap));
190  memset(mTrayId,0,sizeof(mTrayId));
191  memset(mTdigId,0,sizeof(mTdigId));
192 
193  if(mINLCorr) delete mINLCorr;
194  mINLCorr = 0;
195 
196  return kStOK;
197 }
198 
199 
200 //-------------------------------------------------------------
202 {
203  Clear("");
204  return kStOK;
205 }
206 
207 
208 //_____________________________________________________________
215 {
217  LOG_DEBUG << " getting the mtd collection " << mMtdCollection << endm;
218  mStEvent = dynamic_cast<StEvent *> (GetInputDS("StEvent"));
219  if(mStEvent)
220  {
221  mUseMuDst = kFALSE;
222  mMtdCollection = mStEvent->mtdCollection();
223  if(!mMtdCollection)
224  {
225  mMtdCollection = new StMtdCollection();
226  mStEvent->setMtdCollection(mMtdCollection);
227  }
228  if ( GetNextRaw() )
229  {
231  int errorType=UnpackMtdRawData();
232  if(errorType>0)
233  {
234  LOG_WARN<<"MTD_READER::Unpack MTD Data ERROR!"<<endm;
235  return 0;
236  }
237  }
238  }
239  else
240  {
241  mUseMuDst = kTRUE;
242  StMuDstMaker *mMuDstMaker = (StMuDstMaker *)GetMaker("MuDst");
243  if(!mMuDstMaker)
244  {
245  LOG_INFO << "No MuDstMaker ..bye-bye..."<<endm;
246  return 0;
247  }
248  StMuDst* mMuDst=mMuDstMaker->muDst();
249  if(!mMuDst)
250  {
251  LOG_INFO << "No Mudst ... bye-bye" <<endm;
252  return 0;
253  }
254 
255  mMtdCollection = new StMtdCollection();
256 
257  StMuMtdCollection* muMtdCollection = mMuDst->MtdCollection();
258  StMuMtdHeader* muMtdHeader = NULL;
259  Int_t nMtdRawHits = 0;
260 
261  if(muMtdCollection)
262  {
263  muMtdHeader = muMtdCollection->mtdHeader();
264  nMtdRawHits = muMtdCollection->rawHitsPresent();
265  }
266  else
267  {
268  muMtdHeader = mMuDst->mtdHeader();
269  nMtdRawHits = mMuDst->numberOfBMTDRawHit();
270  }
271 
272  for(int i=0;i<nTHUB;i++)
273  {
274  mTriggerTimeStamp[i] = muMtdHeader->triggerTime(i+1);
275  }
276 
277  StMuMtdRawHit *ahit = NULL;
278  for(Int_t i=0; i<nMtdRawHits; i++)
279  {
280  if(muMtdCollection) ahit = muMtdCollection->RawMtdHit(i);
281  else ahit = mMuDst->mtdRawHit(i);
282  Int_t backleg = (Int_t)ahit->backleg();
283  Int_t channel = (Int_t)ahit->channel();
284 
285  if(mYear==13 && mSwapBacklegInRun13 !=0 )
286  {
287  // 0 - do not swap;
288  // 1 - swapping scheme for first part of Run13, i.e. days 76-126
289  // 2 - swapping scheme for second part of Run13, i.e. days 129-161
290  if(mSwapBacklegInRun13==1)
291  {
292  if(backleg==25) backleg = 26;
293  else if (backleg==26) backleg = 25;
294  }
295  else if (mSwapBacklegInRun13==2)
296  {
297  if(ahit->flag()<0)
298  {
299  if(backleg==25) backleg = 26;
300  else if (backleg==26) backleg = 25;
301  }
302  }
303  else
304  {
305  LOG_FATAL << "Please check your setup for mSwapBacklegInRun13" << endm;
306  return 0;
307  }
308  }
309 
310  if(mYear>=14 && mReverseStripInRun14)
311  {
312  // The ribbon cable is reversed for backleg 7, module 5 since Run14
313  if(backleg==7)
314  {
315  Int_t module = (channel-1)/24 + 1;
316  if(module==5)
317  {
318  Int_t cell = (channel-1)%24;
319  if(cell>=0 && cell<=5) cell = 5 - cell;
320  else if (cell>=6 && cell<=11) cell = 17 - cell;
321  else if (cell>=12 && cell<=17) cell = 29 - cell;
322  else if (cell>=18 && cell<=23) cell = 41 - cell;
323  channel = cell + 1 + (module-1)*24;
324  }
325  }
326  }
327  mMtdCollection->addRawHit(new StMtdRawHit(ahit->flag(),(UChar_t)backleg,(UChar_t)channel,ahit->tdc()));
328  }
329  }
330  return mMtdCollection;
331 }
332 
333 
334 //_____________________________________________________________
336 {
337  assert(0 && "RTS doesn't provide sector by sector legacy mtd banks yet");
338  StRtsTable *daqMtdTable = GetNextLegacy(sec);
339  if (daqMtdTable)
340  {
341  fMtd = (mtd_t*)*DaqDta()->begin();
342  }
343  return daqMtdTable;
344 }
345 
346 
347 //_____________________________________________________________
349 {
351  LOG_DEBUG << "GetNextRaw()" << endm;
352 
353  StRtsTable *daqMtdTable = GetNextLegacy();
354  if (daqMtdTable)
355  {
356  fMtd = (mtd_t*)*DaqDta()->begin();
357  }
358  return daqMtdTable;
359 }
360 
361 
362 //_____________________________________________________________
364 
365  if(GetMtdCollection())
366  {
368  fillMtdHeader();
370  fillStEvent();
371 
372  return kStOk;
373  }
374  else
375  return kStErr;
376 
377 }
378 
379 
380 //____________________________________________________
385 //____________________________________________________
387 {
388  if(!fMtd) return 1;
389 
391  MtdLeadingHits.clear();
392  MtdTrailingHits.clear();
393 
395  for(int ifib=0;ifib<nTHUB;ifib++)
396  {
397  int nword=fMtd->ddl_words[ifib];
398  if(nword <= 0) continue;
399  int halfbacklegid = -99;
400  int backlegid = -99;
401  mTriggerTimeStamp[ifib] = 0;
402 
403  LOG_DEBUG << "RDO " << ifib+1 << ": words " << fMtd->ddl_words[ifib] << endm;
404 
406  for (int iword=0;iword<nword;iword++)
407  {
408  unsigned int dataword=fMtd->ddl[ifib][iword];
409  LOG_DEBUG << "DATAWORD: 0x" << hex << dataword << dec << endm;
410 
412  if( (dataword&0xF0000000)>>28 == 0x2)
413  {
414  // Save the first trigger time in each fiber
415  if(mTriggerTimeStamp[ifib]==0) mTriggerTimeStamp[ifib] = dataword;
416  continue;
417  }
418 
419  if( (dataword&0xF0000000)>>28 == 0xD) continue;
420  if( (dataword&0xF0000000)>>28 == 0xE) continue;
421  if( (dataword&0xF0000000)>>28 == 0xA) continue;
422 
424  if( (dataword&0xF0000000)>>28 == 0xC)
425  {
426  halfbacklegid = dataword&0x01;
427  backlegid = (dataword&0x0FE)>>1;
428 
429  // swap backleg 25 and 26 for run 13
430  if(mYear == 13)
431  {
432  if(backlegid==25) backlegid = 26;
433  else if (backlegid==26) backlegid = 25;
434  }
435 
436  continue;
437  }
438  // range checks
439  if(halfbacklegid<0) continue;
440  if(backlegid<1 || backlegid>gMtdNBacklegs)
441  {
442  LOG_ERROR<<"StMtdHitMaker::DATA ERROR!! unexpected backlegid "<< backlegid << " !" << endm;
443  continue;
444  }
445 
446  if( (dataword&0xF0000000)>>28 == 0x6) continue; //error
447 
449  int edgeid =int( (dataword & 0xF0000000)>>28 );
450  if((edgeid !=4) && (edgeid!=5)) continue;
451 
453 
455  int tdcid=(dataword & 0x0F000000)>>24;
456 
458  int tdigid=((tdcid & 0xC)>>2) + halfbacklegid*4;
459 
461  //int ihptdcid = tdcid&0x3;
462 
464  int tdcchan=(dataword&0x00E00000)>>21;
465 
467  unsigned int timeinbin = ((dataword&0x7ffff)<<2)+((dataword>>19)&0x03);
468 
469 
471  int itray=0;
472  for (int i=1;i<=5;i++)
473  {
474  if (mTray2TdigMap[backlegid-1][i-1] == tdigid)
475  {
476  itray = i; break;
477  }
478  }
479  if(itray<1||itray>5) LOG_FATAL<<" Wrong tray ID or mTray2TdigMap missing! backleg = "<<backlegid<<" trayid = "<<itray<<" tdigid ="<<tdigid<<endm;
480 
482  MtdRawHit temphit={0};
483  memset(&temphit,0,sizeof(temphit));
484  temphit.fiberid = (UChar_t)ifib;
485  temphit.backlegid = (UChar_t)backlegid;
486  temphit.tdc = timeinbin;
488  if (mYear<12)
489  temphit.globaltdcchan = (UChar_t)(tdcChan2globalStrip11(tdigid,tdcid,tdcchan,backlegid));
490  else
491  temphit.globaltdcchan = (UChar_t)(tdcChan2globalStrip(itray, tdigid,tdcid,tdcchan));
492 
493  if(mYear>=14 && backlegid==7)
494  {
495  Int_t channel = (Int_t) temphit.globaltdcchan;
496  Int_t module = (channel-1)/24 + 1;
497  if(module==5)
498  {
499  Int_t cell = (channel-1)%24;
500  if(cell>=0 && cell<=5) cell = 5 - cell;
501  else if (cell>=6 && cell<=11) cell = 17 - cell;
502  else if (cell>=12 && cell<=17) cell = 29 - cell;
503  else if (cell>=18 && cell<=23) cell = 41 - cell;
504  channel = cell + 1 + (module-1)*24;
505  temphit.globaltdcchan = (UChar_t) channel;
506  }
507 
508  }
509 
510  temphit.dataword = dataword;
511 
512  if(edgeid == 4)
513  {
514  MtdLeadingHits.push_back(temphit);
515  }
516  else if (edgeid==5)
517  {
518  MtdTrailingHits.push_back(temphit);
519  }
520  else
521  {
522  LOG_WARN<<"StMtdHitMaker::Unknown TDC data type ! "<<endm;
523  continue;
524  }
526  }
527  }
529  return -1;
531 }
532 
533 
534 //____________________________________________
538 Int_t StMtdHitMaker::tdcChan2globalStrip(int itray, int tdigBoardId, int tdcId, int tdcChan)
539 {
540 
542  if (mYear<12)
543  {
544  LOG_WARN << "calling Run12++ tdc mapping for Run" << mYear << ". Not good." << endm;
545  }
546 
547  Int_t globalStripId=-1;
548  int Hnum=tdcId%4+1;
549  int globalTdcChan=Hnum*10+tdcChan;
550  for(int i=0;i<24;i++)
551  {
552  if(mtdStrip[i]==globalTdcChan) {globalStripId=i+1;break;}
553  }
554  if(tdigBoardId>3)
555  globalStripId = (globalStripId>12)? globalStripId-12:globalStripId+12;
556 
558  globalStripId += (itray-1)*24;
559 
560  return globalStripId;
561 }
562 
563 
564 //____________________________________________
568 Int_t StMtdHitMaker::tdcChan2globalStrip11(int tdigBoardId,int tdcId,int tdcChan,int backlegId)
569 {
570 
572  if (mYear>11)
573  {
574  LOG_WARN << "calling pre-Run12 tdc mapping for Run" << mYear << ". Not good." << endm;
575  }
576 
578  Int_t globalStripId=-1;
579  if(backlegId==26)
580  {
581  if (tdcId>3) tdcId=tdcId-4; //scale to H#
582  int globalTdcChan=(tdcId+1)*10+tdcChan;
583  for(int i=0;i<24;i++)
584  {
585  if(mtdStrip[i]==globalTdcChan) {globalStripId=i+1; break;}
586  }
587  }
588 
589  if(backlegId==1)
590  {
591  int globalTdcChan=(tdcId+1)*10+tdcChan;
592  int mtdStripRun11[18]= {34,22,10,37,27,17, 32,20,30,24,11,31, 33,23,16,36,26,15};
593  for(int i=0;i<18;i++)
594  {
595  if(mtdStripRun11[i]==globalTdcChan) {globalStripId=i+1;break;}
596  }
597  }
598 
599  return globalStripId;
600 }
601 
602 
603 //____________________________________________
608 {
610  if(!mUseMuDst)
611  {
612  for (unsigned int i=0;i<MtdLeadingHits.size();i++)
613  {
614  char flag = (+1)*(MtdLeadingHits[i].fiberid+1);
615  unsigned char backlegid = MtdLeadingHits[i].backlegid;
616  unsigned char chn = MtdLeadingHits[i].globaltdcchan;
617  unsigned int tdc = MtdLeadingHits[i].tdc;
618  LOG_DEBUG << " mtd raw hit LE flag:" << (short)flag << " backlegid:"<< (short)backlegid << " chn:" << (short)chn << " tdc:"<< tdc << endm;
619  mMtdCollection->addRawHit(new StMtdRawHit(flag,backlegid,chn,tdc));
620  }
621 
622  for (unsigned int i=0;i<MtdTrailingHits.size();i++)
623  {
624  char flag = (-1)*(MtdTrailingHits[i].fiberid+1);
625  unsigned char backlegid = MtdTrailingHits[i].backlegid;
626  unsigned char chn = MtdTrailingHits[i].globaltdcchan;
627  unsigned int tdc = MtdTrailingHits[i].tdc;
628  LOG_DEBUG << " mtd raw hit TE flag:" << (short)flag << " backlegid:"<< (short)backlegid << " chn:" << (short)chn << " tdc:"<< tdc << endm;
629  mMtdCollection->addRawHit(new StMtdRawHit(flag,backlegid,chn,tdc));
630  }
631  }
632  else
633  {
634  MtdLeadingHits.clear();
635  MtdTrailingHits.clear();
636  StSPtrVecMtdRawHit& mtdRawHitVec = mMtdCollection->mtdRawHits();
637  for(unsigned int i=0;i<mtdRawHitVec.size();i++)
638  {
639  StMtdRawHit* aRawHit=mtdRawHitVec[i];
640  MtdRawHit rawHit;
641  rawHit.tdc=aRawHit->tdc();
642  rawHit.backlegid=aRawHit->backleg();
643  rawHit.fiberid=aRawHit->fiberId();
644  rawHit.globaltdcchan=aRawHit->channel();
645  if(aRawHit->flag()>0)
646  {
647  MtdLeadingHits.push_back(rawHit);
648  }
649  else
650  {
651  MtdTrailingHits.push_back(rawHit);
652  }
653  }
654  }
655 
657 }
658 
659 
660 //____________________________________________
665 {
666  for(int i=0;i<gMtdNModulesAll;i++) mSingleHitVec[i].clear();
667  for(UInt_t i=0;i<MtdLeadingHits.size();i++)
668  {
669  unsigned char ifiber = MtdLeadingHits[i].fiberid;
670  int ibackleg = MtdLeadingHits[i].backlegid;
671  unsigned char chn = MtdLeadingHits[i].globaltdcchan;
672  unsigned int tdc = MtdLeadingHits[i].tdc;
673  int itray = (chn-1)/24+1;
674  int ichan = (chn-1)%24;
675  if (ibackleg>gMtdNBacklegs || itray<=0 || itray>gMtdNModules || ichan<0 || ichan>=gMtdNChannels || ifiber>mNFIBER )
676  {
677  LOG_FATAL << " StMtdHitMaker::fillMtdSingleHits() "
678  << ": ibackleg=" << ibackleg
679  << ": itray=" << itray
680  << ": ichan=" << ichan
681  << ": ifiber=" << ifiber*1
682  << endm;
683  continue;
684  }
685  bool iexist = kFALSE;
686  int igtray = (ibackleg-1)*gMtdNModules+itray;
687  for(size_t ii=0; ii<mSingleHitVec[igtray-1].size(); ii++)
688  {
689  if(ibackleg==mSingleHitVec[igtray-1][ii].backleg&&itray==mSingleHitVec[igtray-1][ii].tray && ichan==mSingleHitVec[igtray-1][ii].channel) {
690  iexist = kTRUE;
691  break;
692  }
693  }
694  if(iexist) continue;
695  MTDSingleHit aSingleHit;
696  aSingleHit.fiberId = ifiber;
697  aSingleHit.backleg = ibackleg;
698  aSingleHit.tray = itray;
699  aSingleHit.channel = ichan;
700  aSingleHit.leadingEdgeTime.push_back(tdc);
701  for(size_t j=i+1;j<MtdLeadingHits.size();j++)
702  {
703  unsigned char jchn = MtdLeadingHits[j].globaltdcchan;
704  int jbackleg = MtdLeadingHits[j].backlegid;
705  int jtray = (jchn-1)/24+1;
706  int jchan = (jchn-1)%24;
707  int jtdc = MtdLeadingHits[j].tdc;
708  if(jbackleg == ibackleg
709  && jtray == itray
710  && jchan == ichan
711  && jtdc)
712  {
713  aSingleHit.leadingEdgeTime.push_back(jtdc);
714  }
715  }
716  for(size_t j=0;j<MtdTrailingHits.size();j++)
717  {
718  unsigned char jchn = MtdTrailingHits[j].globaltdcchan;
719  int jbackleg = MtdTrailingHits[j].backlegid;
720  int jtray = (jchn-1)/24+1;
721  int jchan = (jchn-1)%24;
722  int jtdc = MtdTrailingHits[j].tdc;
723  if(jbackleg == ibackleg
724  && jtray == itray
725  && jchan == ichan
726  && jtdc)
727  {
728  aSingleHit.trailingEdgeTime.push_back(jtdc);
729  }
730  }
731  if(aSingleHit.trailingEdgeTime.size()) mSingleHitVec[igtray-1].push_back(aSingleHit);
732  }
733 
734  //debug
735  if(Debug())
736  {
737  for(int i=0;i<gMtdNModulesAll;i++)
738  {
739  for(size_t m=0;m<mSingleHitVec[i].size();m++)
740  {
741  LOG_INFO<<" idx backleg = "<<i/5+1<<" tray = "<< i%5+1 <<" channel = "<<mSingleHitVec[i][m].channel<<endm;
742  LOG_INFO<<" vec backleg = "<<mSingleHitVec[i][m].backleg<<" tray = "<< mSingleHitVec[i][m].tray <<" channel = "<<mSingleHitVec[i][m].channel<<endm;
743  LOG_INFO<<" leading tdcs = " <<endm;
744  for(size_t j=0;j<mSingleHitVec[i][m].leadingEdgeTime.size();j++)
745  {
746  LOG_INFO << " " << mSingleHitVec[i][m].leadingEdgeTime[j] << endm;
747  }
748  LOG_INFO<<" trailing tdcs = " <<endm;
749  for(size_t j=0;j<mSingleHitVec[i][m].trailingEdgeTime.size();j++)
750  {
751  LOG_INFO << " " << mSingleHitVec[i][m].trailingEdgeTime[j] <<endm;
752  }
753 
754  }
755  }
756  }
757 }
758 
759 //____________________________________________
761 {
762  IntVec gTrayVec;
763  for(int i=0;i<gMtdNModulesAll;i++)
764  {
765  if(mSingleHitVec[i].size()>0)
766  {
767  gTrayVec.push_back(i+1);
768  }
769  }
770  return gTrayVec;
771 }
772 
773 
774 //____________________________________________
775 IntVec StMtdHitMaker::GetValidChannel(int backleg, int tray, int &fiber)
776 {
777  IntVec chanVec;
778  int igtray = (backleg-1)*gMtdNModules+tray;
779  if(igtray>gMtdNModulesAll) return chanVec;
780 
781  for(size_t i=0 ; i<mSingleHitVec[igtray-1].size() ; i++)
782  {
783  if( mSingleHitVec[igtray-1][i].tray == tray
784  && mSingleHitVec[igtray-1][i].backleg == backleg )
785  {
786  chanVec.push_back(mSingleHitVec[igtray-1][i].channel);
787  fiber = mSingleHitVec[igtray-1][i].fiberId;
788  }
789  }
790  return chanVec;
791 }
792 
793 
794 //____________________________________________
795 UIntVec StMtdHitMaker::GetLeadingTdc(int backleg, int tray, int channel)
796 {
797  UIntVec leTdc;
798  int igtray = (backleg-1)*gMtdNModules+tray;
799  for(size_t i=0 ; i<mSingleHitVec[igtray-1].size() ; i++)
800  {
801  if(mSingleHitVec[igtray-1][i].backleg!=backleg) continue;
802  if(mSingleHitVec[igtray-1][i].tray!=tray) continue;
803  if(mSingleHitVec[igtray-1][i].channel!=channel) continue;
804  for(size_t j=0; j<mSingleHitVec[igtray-1][i].leadingEdgeTime.size(); j++)
805  {
806  leTdc.push_back(mSingleHitVec[igtray-1][i].leadingEdgeTime[j]);
807  }
808  }
809  return leTdc;
810 }
811 
812 
813 //____________________________________________
814 UIntVec StMtdHitMaker::GetTrailingTdc(int backleg, int tray, int channel)
815 {
816  UIntVec teTdc;
817  int igtray = (backleg-1)*gMtdNModules+tray;
818  for(size_t i=0 ; i<mSingleHitVec[igtray-1].size() ; i++)
819  {
820  if(mSingleHitVec[igtray-1][i].backleg!=backleg) continue;
821  if(mSingleHitVec[igtray-1][i].tray!=tray) continue;
822  if(mSingleHitVec[igtray-1][i].channel!=channel) continue;
823  for(size_t j=0; j<mSingleHitVec[igtray-1][i].trailingEdgeTime.size(); j++)
824  {
825  teTdc.push_back(mSingleHitVec[igtray-1][i].trailingEdgeTime[j]);
826  }
827  }
828  return teTdc;
829 }
830 
831 
832 //____________________________________________
837 {
839  int shouldHaveRejectEvent = -1;
840  unsigned int tpcSectorMask = 0;
841  if(mUseMuDst)
842  {
843  StMuDstMaker *mMuDstMaker = (StMuDstMaker *)GetMaker("MuDst");
844  StMuDst* mMuDst=mMuDstMaker->muDst();
845  StMuMtdCollection* muMtdCollection = mMuDst->MtdCollection();
846  StMuMtdHeader* muMtdHeader = NULL;
847  if(muMtdCollection)
848  muMtdHeader = muMtdCollection->mtdHeader();
849  else
850  muMtdHeader = mMuDst->mtdHeader();
851 
852  shouldHaveRejectEvent = muMtdHeader->shouldHaveRejectEvent();
853  tpcSectorMask = muMtdHeader->tpcSectorMask();
854  }
855  else
856  {
857  TDataSet* ds = FindDataSet("MtdShouldHaveRejectEvent");
858  shouldHaveRejectEvent = (ds ? atoi(ds->GetTitle()) : -1);
859  ds = FindDataSet("TpcSectorsByMtd");
860  tpcSectorMask = (ds ? (unsigned int) atoi(ds->GetTitle()) : 0U);
861  }
862 
863  StMtdHeader *mtdHeader = new StMtdHeader();
864  for(int i=0;i<nTHUB;i++)
865  {
866  mtdHeader->setTriggerTime(mTriggerTimeStamp[i], i);
867  LOG_DEBUG << "Trigger Time Stamp "<< i+1 <<": " << (unsigned int)mTriggerTimeStamp[i] << endm;
868  }
869  mtdHeader->setShouldHaveRejectEvent(shouldHaveRejectEvent);
870  mtdHeader->setTpcSectorMask(tpcSectorMask);
871 
872  mMtdCollection->setHeader(mtdHeader);
873 }
874 
875 
876 //____________________________________________
881 {
882  vector<MTDOneSideHit> mOneSideHits;
883  IntVec validtray = GetValidTrays();
884  LOG_DEBUG << " Number of fired trays " << validtray.size() <<endm;
885  for(size_t i=0;i<validtray.size();i++)
886  {
887  int igtray = validtray[i];
888  int backleg = (igtray-1)/gMtdNModules+1;
889  int tray = (igtray-1)%gMtdNModules+1;
890  int fiber = -1;
891  IntVec validchan = GetValidChannel(backleg,tray,fiber);
892  LOG_DEBUG << "fired backleg " << backleg << " tray " << tray << endm;
893  LOG_DEBUG <<" total channels "<<validchan.size() <<endm;
894  for(size_t iv=0;iv<validchan.size();iv++)
895  {
896  UIntVec leTdc = GetLeadingTdc(backleg, tray, validchan[iv]);
897  UIntVec teTdc = GetTrailingTdc(backleg, tray, validchan[iv]);
898 
899  LOG_DEBUG <<" leTdc.size():"<<leTdc.size()<<" teTdc.size():"<<teTdc.size()<<endl;
900  if(!leTdc.size() || !teTdc.size()) continue;
901  int chan = validchan[iv]; // global tdc chan (0-23)
902  int channel = chan;
903  int tdigboardid = getTdigBoardId(backleg,tray,chan);
904  int ilocalchan = getLocalTdcChan(backleg,tray,chan);
905  unsigned int tmptdc = leTdc[0];
906  int bin = tmptdc&0x3ff;
907  double tmptdc_f = tmptdc+mINLCorr->getTdigINLCorr(tdigboardid,ilocalchan,bin);
908  double letime = tmptdc_f*VHRBIN2PS / 1000.;
909 
910  tmptdc = teTdc[0];
911  bin = tmptdc&0x3ff;
912  tmptdc_f = tmptdc+mINLCorr->getTdigINLCorr(tdigboardid,ilocalchan,bin);
913  double tetime = tmptdc_f*VHRBIN2PS / 1000.;
914 
915  MTDOneSideHit tmpHit;
916  tmpHit.fiberId = fiber;
917  tmpHit.backleg = backleg;
918  tmpHit.tray = tray;
919  tmpHit.channel = channel;
920  tmpHit.leadingEdgeTime = letime;
921  tmpHit.trailingEdgeTime = tetime;
922  mOneSideHits.push_back(tmpHit);
923  } // end channel
924  } // end tray
925 
926  int nMtdHits = 0;
927  int nHits = mOneSideHits.size();
928  LOG_DEBUG <<" one side hits # = "<<nHits<<endm;
929  for(int i=0;i<nHits;i++)
930  {
931  int iBackLeg = mOneSideHits[i].backleg;
932  int iTray = mOneSideHits[i].tray;
933  int iCell = mOneSideHits[i].channel;
934  int iFiber = mOneSideHits[i].fiberId;
935  float iLeadingEdgeTime = mOneSideHits[i].leadingEdgeTime;
936  float iTrailingEdgeTime = mOneSideHits[i].trailingEdgeTime;
937  for(int j=i+1;j<nHits;j++)
938  {
939  int jBackLeg = mOneSideHits[j].backleg;
940  int jTray = mOneSideHits[j].tray;
941  int jCell = mOneSideHits[j].channel;
942  int jFiber = mOneSideHits[j].fiberId;
943  float jLeadingEdgeTime = mOneSideHits[j].leadingEdgeTime;
944  float jTrailingEdgeTime = mOneSideHits[j].trailingEdgeTime;
945  if(abs(iCell-jCell)!=12) continue;
946  if(iBackLeg != jBackLeg) continue;
947  if(iTray != jTray) continue;
948  if(iFiber != jFiber) continue;
949  UChar_t mBackLeg = iBackLeg;
950  UChar_t mModule = iTray;
951  UChar_t mCell = 99;
952  pair<Double_t,Double_t> mLeadingEdgeTime;
953  pair<Double_t,Double_t> mTrailingEdgeTime;
954 
955  if(iCell<12)
956  {
957  mCell = iCell;
958  mLeadingEdgeTime.first = iLeadingEdgeTime;//west
959  mLeadingEdgeTime.second = jLeadingEdgeTime;//east
960  mTrailingEdgeTime.first = iTrailingEdgeTime;//west
961  mTrailingEdgeTime.second = jTrailingEdgeTime;//east
962  }
963  else
964  {
965  mCell = jCell;
966  mLeadingEdgeTime.first = jLeadingEdgeTime;//west
967  mLeadingEdgeTime.second = iLeadingEdgeTime;//east
968  mTrailingEdgeTime.first = jTrailingEdgeTime;//west
969  mTrailingEdgeTime.second = iTrailingEdgeTime;//east
970  }
971 
972  if(mTriggerWndSelection)
973  {
974  // trigger time window cuts
975  float timeDiff = (mLeadingEdgeTime.first+mLeadingEdgeTime.second)/2 - 25.*(mTriggerTimeStamp[iFiber] & 0xfff);
976  while(timeDiff<0) timeDiff += 51200;
977  int igtray = (iBackLeg-1)*gMtdNModules+iTray;
978  if(timeDiff<mTriggerTimeWindow[igtray-1][0] || timeDiff>mTriggerTimeWindow[igtray-1][1]) continue;
979  }
980 
981  StMtdHit* aHit= new StMtdHit();
982  aHit->setBackleg(mBackLeg);
983  aHit->setModule(mModule);
984  aHit->setCell(mCell);
985  aHit->setLeadingEdgeTime(mLeadingEdgeTime);
986  aHit->setTrailingEdgeTime(mTrailingEdgeTime);
987  mMtdCollection->addHit(aHit);
988  nMtdHits++;
989  LOG_DEBUG << " backleg " << mBackLeg*1
990  << " module "<< mModule*1
991  << " cell " <<mCell*1
992  << " leading.first "<<mLeadingEdgeTime.first
993  << " leading.second "<<mLeadingEdgeTime.second
994  << " trailing.first "<<mTrailingEdgeTime.first
995  << " trailing.second "<<mTrailingEdgeTime.second<<endm;
996 
997  }
998  }
999  LOG_DEBUG << " matched hits " << nMtdHits <<endm;
1000 }
1001 
1002 //_________________________________________________________________________
1007 {
1008  LOG_DEBUG << "fillStEvent() Starting..." << endm;
1009 
1011  if(!mMtdCollection)
1012  {
1013  LOG_WARN << "No MtdCollection ... creating an empty one in StEvent" << endm;
1014  mMtdCollection = new StMtdCollection();
1015  if(!mUseMuDst)
1016  mStEvent->setMtdCollection(mMtdCollection);
1017  }
1018 
1019  if(mUseMuDst)
1020  {
1021  StMuDstMaker *mMuDstMaker = (StMuDstMaker *)GetMaker("MuDst");
1022  StMuDst* mMuDst=mMuDstMaker->muDst();
1023  mMuDst->setMtdArray(mMtdCollection);
1024  }
1025 
1026  //
1027  StMtdCollection* mtdCollection = mMtdCollection;
1028  if(mtdCollection)
1029  {
1030  if(mtdCollection->rawHitsPresent())
1031  {
1032  StSPtrVecMtdRawHit& rawMtdVec = mtdCollection->mtdRawHits();
1033 
1034  if(!mUseMuDst || Debug())
1035  {
1036  LOG_INFO << rawMtdVec.size() << " MTD raw hits in event ..." << endm;
1037  }
1038 
1039  if(Debug())
1040  {
1041  for(size_t i=0;i<rawMtdVec.size();i++)
1042  {
1043  LOG_DEBUG << (*rawMtdVec[i]) << endm;
1044  }
1045  }
1046  }
1047  else
1048  {
1049  LOG_DEBUG << "No MtdRawHitCollection" << endm;
1050  }
1051 
1052  if(mtdCollection->hitsPresent())
1053  {
1054  StSPtrVecMtdHit& mtdVec = mtdCollection->mtdHits();
1055 
1056  if(!mUseMuDst || Debug())
1057  {
1058  LOG_INFO << mtdVec.size() << " MTD hits in event ..." << endm;
1059  }
1060 
1061  if(Debug())
1062  {
1063  for(size_t i=0;i<mtdVec.size();i++)
1064  {
1065  LOG_DEBUG << (*mtdVec[i]) << endm;
1066  }
1067  }
1068  }
1069  else {
1070  LOG_DEBUG << "No MtdHitCollection" << endm;
1071  }
1072  }
1073  else
1074  {
1075  LOG_WARN << "No MtdCollection" << endm;
1076  LOG_WARN << "No MtdRawHitCollection" << endm;
1077  LOG_WARN << "No MtdHitCollection" << endm;
1078  }
1079 }
1080 
1081 
1082 //____________________________________________
1083 Int_t StMtdHitMaker::getTdigBoardId(Int_t backlegid, Int_t tray, Int_t chn)
1084 {
1085  Int_t tdigboardid = mTdigId[mTrayId[backlegid-1][tray-1]-200];
1086  return tdigboardid;
1087 }
1088 
1089 
1090 //____________________________________________
1091 Int_t StMtdHitMaker::getLocalTdcChan(Int_t backlegid, Int_t tray, Int_t chn)
1092 {
1093  int channel = chn;
1094  int itdigid = mTray2TdigMap[backlegid-1][tray-1];
1095  if(itdigid>3) channel = (channel>11)?channel-12:channel+12;
1096 
1097  int iglobalChan = mtdStrip[channel];
1098  int ilocalChan = (iglobalChan/10-1)*8+iglobalChan%10;
1099 
1100  return ilocalChan;
1101 }
1102 
1103 //
1104 // $Id: StMtdHitMaker.cxx,v 1.25 2016/07/27 15:31:15 marr Exp $
1105 // $Log: StMtdHitMaker.cxx,v $
1106 // Revision 1.25 2016/07/27 15:31:15 marr
1107 // Fix coverity check: check the range of backlegid
1108 //
1109 // Revision 1.24 2015/05/01 20:04:33 marr
1110 // Use AddData() to pass the information from event filtering stage
1111 //
1112 // Revision 1.23 2015/04/25 03:04:05 marr
1113 // Fill the two new data members mShouldHaveRejectEvent and mTpcSectorMask in
1114 // MTD header
1115 //
1116 // Revision 1.22 2015/01/22 22:10:58 marr
1117 // Fix the reserved ribbon cable in backleg 7 module 5 since 2014 run. This is
1118 // not fixed on the hardware level in 2015 run.
1119 //
1120 // Revision 1.21 2015/01/06 20:37:36 marr
1121 // 1. Add an option to load trigger time window cuts from local files for cosmic ray data.
1122 // This is motivated by the fact that the cuts are different for cosmic ray and collision data.
1123 // 2. The time of a MTD hit is calcualted as: (mLeadingEdgeTime.first+mLeadingEdgeTime.second)/2
1124 // 3. Add the scheme to swap backlegs 25 & 26 for the second part of Run13 data when running
1125 // MuDst in afterburner mode. Note that the scheme is different for the first part of Run13 data.
1126 // Different schemes can be select using function setSwapBacklegInRun13(Int_t swap).
1127 // 4. Clean up in header file
1128 //
1129 // Revision 1.20 2014/09/25 14:36:25 marr
1130 // Do not automatically print out hit information to log file when running on muDst
1131 //
1132 // Revision 1.19 2014/09/19 17:49:33 marr
1133 // 1) Use the constants from StMtdUtil/StMtdConstants.h
1134 // 2) Apply trigger time window cuts
1135 //
1136 // Revision 1.18 2014/08/25 17:06:20 marr
1137 // Read in both MC hits and real hits during embedding
1138 //
1139 // Revision 1.17 2014/07/21 20:17:04 marr
1140 // Add # of MTD hit information to the log file
1141 //
1142 // Revision 1.16 2014/06/24 20:01:24 marr
1143 // Able to process Run12 UU muDst where only the muMtdCollection is stored
1144 //
1145 // Revision 1.15 2014/06/23 17:27:08 marr
1146 // Reformatting
1147 //
1148 // Revision 1.14 2014/05/29 15:31:53 marr
1149 //
1150 // 1. Obtain the Tdig <-> Tray <-> Tdigboard map from the database
1151 // 2. Able to run on muDst in afterburner mode
1152 // 3. Obtain the MTD hits directly from the muDst instead of the hit array
1153 // 4. Automatically swap backlegs 25 & 26 when running bfc chain. A flag with default value of "kFALSE" is added to control the swapping in the afterburner mode when running on muDst.
1154 //
1155 // Revision 1.13 2014/05/27 20:04:27 jeromel
1156 // No change - removed trailing space (as a demo for cvs comments)
1157 //
1158 // Revision 1.12 2014/05/27 20:03:01 jeromel
1159 // Adding cvs tags
1160 //
1161 //
void fillMtdSingleHits()
Int_t InitRun(Int_t)
StMuDst * muDst()
Definition: StMuDstMaker.h:425
Class StRTSBaseMaker - is an abstract StMaker to define the interface to access the DAQ data from the...
UChar_t globaltdcchan
1,2,...,117
Definition: StMtdHitMaker.h:36
UChar_t backlegid
0 1 2,3
Definition: StMtdHitMaker.h:35
Int_t FinishRun(Int_t)
void Clear(Option_t *option="")
User defined functions.
void fillMtdHitCollection()
StRtsTable * GetNextRaw()
This is an overloaded member function, provided for convenience.
UChar_t fiberid
data word before unpack
Definition: StMtdHitMaker.h:34
UInt_t dataword
tdc time (in bin) per hit.
Definition: StMtdHitMaker.h:33
IntVec GetValidTrays()
ship collection to StEvent and check
float getTdigINLCorr(int tdigId, int tdcChannel, int bin)
direct access to INL correction table for any TDIG-Id
StMtdCollection * GetMtdCollection()
Int_t UnpackMtdRawData()
void initFromDbase(StMaker *maker)
Initial function to access the data base and retrieve INL tables.
StRtsTable * DaqDta()
Return the current DAQ data block. This member function is provided for convenience.
Int_t tdcChan2globalStrip11(Int_t, Int_t, Int_t, Int_t)
Int_t tdcChan2globalStrip(Int_t, Int_t, Int_t, Int_t)
Definition: Stypes.h:40
StMtdHitMaker(const char *name="mtd_raw")
Default constructor.
virtual StRtsTable * GetNextLegacy()
This is an overloaded member function, provided for convenience.
void setMtdArray(StMtdCollection *mtd_coll)
Definition: StMuDst.cxx:632
Definition: daq_mtd.h:9
Definition: Stypes.h:44
Definition: Stypes.h:41
void fillMtdRawHitCollection()
virtual TDataSet * Find(const char *path) const
Definition: TDataSet.cxx:362