StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StBTofHitMaker.cxx
1 /***************************************************************************
2  *
3  * $Id: StBTofHitMaker.cxx,v 1.22 2017/10/20 17:50:32 smirnovd Exp $
4  *
5  * Author: Valeri Fine, BNL Feb 2008
6  ***************************************************************************
7  *
8  * Description: Create the TOF data coming from the RTS_READER
9  *
10  * Input: RTS_Reader
11  * Output: TOF data
12  *
13  ***************************************************************************
14  *
15  **************************************************************************/
16 #include "StBTofHitMaker.h"
17 
18 #include "StEventTypes.h"
19 #include "StEvent/StBTofCollection.h"
20 #include "StEvent/StBTofHeader.h"
21 #include "StEvent/StBTofRawHit.h"
22 #include "StBTofUtil/StBTofRawHitCollection.h"
23 #include "StBTofUtil/StBTofHitCollection.h"
24 #include "StBTofUtil/StBTofDaqMap.h"
25 #include "StBTofUtil/StBTofINLCorr.h"
26 #include "StEvent/StEvent.h"
27 #include "StDAQMaker/StDAQReader.h"
28 
29 #include "StRtsTable.h"
30 #include "DAQ_TOF/daq_tof.h"
31 
32 
33 //_____________________________________________________________
34 StBTofHitMaker::StBTofHitMaker(const char *name):StRTSBaseMaker("tof",name)
35  , mStEvent(0),fTof(0)
36  , mNValidTrays(-1)
37  , mBTofCollection(0)
38  , mRawHitCollection(0)
39  , mHitCollection(0)
40  , mBTofDaqMap(0)
41  , mBTofINLCorr(0)
42  , mBTofSortRawHit(0)
43 {
44  LOG_DEBUG << "StBTofHitMaker::ctor" << endm;
45 }
46 
47 //_____________________________________________________________
48 StBTofHitMaker::~StBTofHitMaker()
49 { }
50 
51 //_____________________________________________________________
52 void StBTofHitMaker::Clear(Option_t* option)
53 {
54  TofLeadingHits.clear();
55  TofTrailingHits.clear();
56 
57  for(int i=0;i<4;i++) mTriggerTimeStamp[i] = 0;
58 }
59 
60 //_____________________________________________________________
61 Int_t StBTofHitMaker::Init()
62 {
63  LOG_INFO << "Initialized conversion parameter VHRBIN2PS = "
64  << VHRBIN2PS << "ps/ch" << endm;
65  Clear("");
66  return kStOK;
67 }
68 
69 //_____________________________________________________________
70 Int_t StBTofHitMaker::InitRun(Int_t runnumber)
71 {
73  // TOF Daq map and INL initialization -- load from StBTofUtil
75  LOG_DEBUG << "Initializing DAQ map:" << endm;
76  mBTofDaqMap = new StBTofDaqMap();
77  mBTofDaqMap->Init(this);
78 
79  mNValidTrays = mBTofDaqMap->numberOfValidTrays();
80 
81  LOG_DEBUG << "Initializing INL table:" << endm;
82  mBTofINLCorr = new StBTofINLCorr();
83  mBTofINLCorr->initFromDbase(this);
84 
85  LOG_DEBUG << "Initializing StBTofSortRawHit:" << endm;
86  mBTofSortRawHit = new StBTofSortRawHit();
87  mBTofSortRawHit->Init(this, mBTofDaqMap);
88 
89  // Find out what year we're in: relevant for GMT-related TDIG Id modifications for Run 13+
90  mYear = (Int_t)runnumber/1e6 - 1;
91 
92  return kStOK;
93 }
94 
95 //_____________________________________________________________
96 Int_t StBTofHitMaker::FinishRun(Int_t runnumber)
97 {
98  if(mBTofDaqMap) delete mBTofDaqMap;
99  mBTofDaqMap = 0;
100 
101  if(mBTofINLCorr) delete mBTofINLCorr;
102  mBTofINLCorr = 0;
103 
104  return kStOK;
105 }
106 
107 //-------------------------------------------------------------
109 {
110  Clear("");
111  return kStOK;
112 }
113 //_____________________________________________________________
120 {
122  StBTofCollection *btofCollection = 0;
123  mStEvent = dynamic_cast<StEvent *> (GetInputDS("StEvent"));
124 
125  if (mStEvent) {
126  btofCollection = mStEvent->btofCollection();
127 
129  if ( !btofCollection ) {
131  btofCollection = new StBTofCollection();
132  mStEvent->setBTofCollection(btofCollection);
133  }
134  }
135  else {LOG_WARN << "No StEvent found" << endm; }
136 
137  return btofCollection;
138 }
139 //_____________________________________________________________
141 {
142  assert(0 && "RTS doesn't provide sector by sector legacy tof banks yet");
143  StRtsTable *daqTofTable = GetNextLegacy(sec);
144  if (daqTofTable) {
145  fTof = (tof_t*)*DaqDta()->begin();
146  }
147  return daqTofTable;
148 }
149 //_____________________________________________________________
151 {
153  LOG_DEBUG << "GetNextRaw()" << endm;
154 
155  StRtsTable *daqTofTable = GetNextLegacy();
156  if (daqTofTable) {
157  fTof = (tof_t*)*DaqDta()->begin();
158  }
159  return daqTofTable;
160 }
161 //_____________________________________________________________
163 {
164  mBTofCollection = GetBTofCollection();
165  LOG_DEBUG << " getting the tof collection " << mBTofCollection << endm;
166  if (mBTofCollection) {
167  if ( GetNextRaw() ) {
169  int errorType=UnpackTofRawData();
170  if(errorType>0) {
171  LOG_WARN<<"TOF_READER::UnPack TOF Data ERROR!"<<endm;
172  }
173  fillBTofRawHitCollection();
174  fillBTofHeader();
175  fillBTofHitCollection();
176  fillStEvent();
177  }
178  }
179  return kStOk;
180 }
181 //____________________________________________________
186 //____________________________________________________
187 Int_t StBTofHitMaker::UnpackTofRawData()
188 {
189  if(!fTof) return 1;
190 
192  TofLeadingHits.clear();
193  TofTrailingHits.clear();
194 
195  for(int ifib=0;ifib<4;ifib++){ // 4 fibers
196  int nword=fTof->ddl_words[ifib];
197  if(nword <= 0) continue;
198  int halftrayid = -99;
199  int trayid = -99;
200  mTriggerTimeStamp[ifib] = 0;
201 
202 // printf("RDO %d: words %d:\n",ifib+1,fTof->ddl_words[ifib]) ;
203 
204  for (int iword=0;iword<nword;iword++) {
205  unsigned int dataword=fTof->ddl[ifib][iword];
206 
207 
209  if( (dataword&0xF0000000)>>28 == 0xD) continue;
210  if( (dataword&0xF0000000)>>28 == 0xE) continue;
211  if( (dataword&0xF0000000)>>28 == 0xA) {
212  continue;
214  }
215  if( (dataword&0xF0000000)>>28 == 0x2) {
216  if(mTriggerTimeStamp[ifib]==0) mTriggerTimeStamp[ifib] = dataword; // Save the first trigger time in each fiber
217  continue;
218  }
219  if( (dataword&0xF0000000)>>28 == 0xC) {
220  halftrayid = dataword&0x01;
221  trayid = (dataword&0x0FE)>>1;
222  continue;
223  }
224  if(halftrayid<0) continue;
225  if(trayid<1 || trayid >124) {
226  LOG_ERROR<<"StBTofHitMaker::DATA ERROR!! unexpected trayID ! "<<endm;
227  continue;
228  }
229  int edgeid =int( (dataword & 0xf0000000)>>28 );
230  if((edgeid !=4) && (edgeid!=5)) continue;
231 
232  int tdcid=(dataword & 0x0F000000)>>24;
233  int tdigid=tdcid/4;
234  int tdcchan=(dataword&0x00E00000)>>21;
235  TofRawHit temphit={0};
237  memset(&temphit,0,sizeof(temphit));
238  temphit.fiberid = (UChar_t)ifib;
239  temphit.trayID = (UChar_t)trayid;
240  unsigned int timeinbin = ((dataword&0x7ffff)<<2)+((dataword>>19)&0x03);
241  temphit.tdc = timeinbin;
244  bool isGMT = (trayid==8) || (trayid==23) || (trayid==93) || (trayid==108);
246  if ((mYear >= 13) && (isGMT) && (halftrayid==0) && (tdigid==0)){
247  temphit.globaltdcchan = (UChar_t)(tdcchan + (tdcid%4)*8+ 1*24 +halftrayid*96);
248  }
250  else if ((mYear >= 13) && (isGMT) && (((halftrayid==0)&&(tdigid==1)) || ((halftrayid==1)&&(tdigid==3)))){
251  LOG_ERROR<<"Unexpected TDIG-Id (" << tdigid+(4*halftrayid) << ") in TOF GMT tray (" << trayid << "). Should not happen" << endm;
252  }
253  else{
254  temphit.globaltdcchan = (UChar_t)(tdcchan + (tdcid%4)*8+tdigid*24+halftrayid*96);
255  }
256 
257  temphit.dataword = dataword;
258 
259 // printf("\t%d: 0x%08X [%u dec]\n",iword,fTof->ddl[ifib][iword],fTof->ddl[ifib][iword]) ;
260 
261  if(edgeid == 4) {
262  TofLeadingHits.push_back(temphit);
263  } else if (edgeid==5){
264  TofTrailingHits.push_back(temphit);
265  } else {
266  LOG_WARN<<"StBTofHitMaker::Unknown TDC data type ! "<<endm;
267  continue;
268  }
270  }
271  }
273  return -1;
275 }
276 
277 //____________________________________________
281 void StBTofHitMaker::fillBTofRawHitCollection()
282 {
284  for (unsigned int i=0;i<TofLeadingHits.size();i++){
285  char flag = (+1)*(TofLeadingHits[i].fiberid+1);
286  unsigned char trayid = TofLeadingHits[i].trayID;
287  unsigned char chn = TofLeadingHits[i].globaltdcchan;
288  unsigned int tdc = TofLeadingHits[i].tdc;
289  mBTofCollection->addRawHit(new StBTofRawHit(flag,trayid,chn,tdc));
290  }
291 
292  for (unsigned int i=0;i<TofTrailingHits.size();i++){
293  char flag = (-1)*(TofTrailingHits[i].fiberid+1);
294  unsigned char trayid = TofTrailingHits[i].trayID;
295  unsigned char chn = TofTrailingHits[i].globaltdcchan;
296  unsigned int tdc = TofTrailingHits[i].tdc;
297  mBTofCollection->addRawHit(new StBTofRawHit(flag,trayid,chn,tdc));
298  }
299 
300 }
301 //____________________________________________
305 void StBTofHitMaker::fillBTofHeader()
306 {
308  StBTofHeader *tofHeader = new StBTofHeader();
309  for(int i=0;i<4;i++)
310  tofHeader->setTriggerTime(mTriggerTimeStamp[i], i);
311  // others
312  mBTofCollection->setHeader(tofHeader);
313 }
314 //____________________________________________
318 void StBTofHitMaker::fillBTofHitCollection()
319 {
320  mBTofSortRawHit->setBTofCollection(mBTofCollection);
321 
322  // multi-tray system
323  IntVec validtray = mBTofDaqMap->ValidTrays();
324  for(size_t i=0;i<validtray.size();i++) {
325  int trayId = validtray[i];
326  IntVec validchannel = mBTofSortRawHit->GetValidChannel(trayId);
327  if(Debug()) {
328  LOG_INFO << " Number of fired hits on tray " << trayId << " = " << validchannel.size() << endm;
329  }
330 
331  for(size_t iv=0;iv<validchannel.size();iv++) {
332  UIntVec leTdc = mBTofSortRawHit->GetLeadingTdc(trayId, validchannel[iv], kTRUE);
333  UIntVec teTdc = mBTofSortRawHit->GetTrailingTdc(trayId, validchannel[iv], kTRUE);
334 
335  if(!leTdc.size() || !teTdc.size()) continue;
336 
337  int chan = validchannel[iv];
338  IntVec map = mBTofDaqMap->TDIGChan2Cell(chan);
339  int moduleId = map[0];
340  int cellId = map[1];
341 
342  // store raw hit from trays and vpds into StBTofHit
343  //
344  // correct for INL
345  unsigned int tmptdc = leTdc[0]; // select the first hit
346  int bin = tmptdc&0x3ff;
347  double tmptdc_f = tmptdc + mBTofINLCorr->getTrayINLCorr(trayId, chan, bin);
348  double letime = tmptdc_f*VHRBIN2PS / 1000.;
349 
350  tmptdc = teTdc[0];
351  bin = tmptdc&0x3ff;
352  tmptdc_f = tmptdc + mBTofINLCorr->getTrayINLCorr(trayId, chan, bin);
353  double tetime = tmptdc_f*VHRBIN2PS / 1000.;
354 
355  StBTofHit *aHit = new StBTofHit();
356  aHit->setHardwarePosition(kBTofId);
357  aHit->setTray((UChar_t)trayId);
358  aHit->setModule((UChar_t)moduleId);
359  aHit->setCell((UChar_t)cellId);
360  aHit->setLeadingEdgeTime(letime);
361  aHit->setTrailingEdgeTime(tetime);
362  mBTofCollection->addHit(aHit);
363 
364  } // end channel
365  } // end tray
366 
367  // vpd -> StBTofHit
368  for(int ivpd=0;ivpd<2;ivpd++) { // west and east sides
369  StBeamDirection eastwest = (ivpd==0) ? west : east;
370  int trayId = (ivpd==0) ? mWestVpdTrayId : mEastVpdTrayId;
371  IntVec validtube = mBTofSortRawHit->GetValidChannel(trayId);
372  if(Debug()) {
373  LOG_INFO << " Number of fired hits on tray(vpd) " << trayId << " = " << validtube.size() << endm;
374  }
375 
376  if(!validtube.size()) continue;
377  for(int i=0;i<mNVPD;i++) {
378  int tubeId = i+1;
379  int lechan = (ivpd==0) ? mBTofDaqMap->WestPMT2TDIGLeChan(tubeId) : mBTofDaqMap->EastPMT2TDIGLeChan(tubeId);
380  int techan = (ivpd==0) ? mBTofDaqMap->WestPMT2TDIGTeChan(tubeId) : mBTofDaqMap->EastPMT2TDIGTeChan(tubeId);
381  UIntVec leTdc = mBTofSortRawHit->GetLeadingTdc(trayId, lechan, kTRUE);
382  UIntVec teTdc = mBTofSortRawHit->GetTrailingTdc(trayId, lechan, kTRUE); // channel number should be le, sorted in StBTofSortRawHit
383 
384  if(leTdc.size() && teTdc.size()) {
385 
386  // correct for INL
387  unsigned int tmptdc = leTdc[0]; // select the first hit
388  int bin = tmptdc&0x3ff;
389  double tmptdc_f = tmptdc + mBTofINLCorr->getVpdINLCorr(eastwest, lechan, bin);
390  double letime = tmptdc_f*VHRBIN2PS / 1000.;
391 
392  tmptdc = teTdc[0];
393  bin = tmptdc&0x3ff;
394  tmptdc_f = tmptdc + mBTofINLCorr->getVpdINLCorr(eastwest, techan, bin);
395  double tetime = tmptdc_f*VHRBIN2PS / 1000.;
396 
397  StBTofHit *aHit = new StBTofHit();
398  aHit->setTray((UChar_t)trayId);
399  aHit->setModule(0);
400  aHit->setCell((UChar_t)tubeId);
401  aHit->setLeadingEdgeTime(letime);
402  aHit->setTrailingEdgeTime(tetime);
403  mBTofCollection->addHit(aHit);
404  }
405  }
406  }
407  //
408 
409 }
410 
411 //_________________________________________________________________________
415 void StBTofHitMaker::fillStEvent() {
416 
417  LOG_DEBUG << "fillStEvent() Starting..." << endm;
418 
420  if(!mBTofCollection){
421  LOG_WARN << "No BTofCollection ... creating one in StEvent" << endm;
422  mBTofCollection = new StBTofCollection();
423  mStEvent->setBTofCollection(mBTofCollection);
424  }
425 
427  StBTofCollection* btofCollection = mStEvent->btofCollection();
428  if(btofCollection){
429  if(btofCollection->rawHitsPresent()) {
430  StSPtrVecBTofRawHit& rawTofVec = btofCollection->tofRawHits();
431  LOG_INFO << "BTofRawHitCollection: " << rawTofVec.size() << " entries" << endm;
432  if(Debug()) {
433  for(size_t i=0;i<rawTofVec.size();i++) {
434  LOG_DEBUG << (*rawTofVec[i]) << endm;
435  }
436  }
437  }
438  else {
439  LOG_INFO << "No BTofRawHitCollection" << endm;
440  }
441 
442  if(btofCollection->hitsPresent()) {
443  StSPtrVecBTofHit& tofVec = btofCollection->tofHits();
444  LOG_INFO << "BTofHitCollection: " << tofVec.size() << " entries..." << endm;
445  if(Debug()) {
446  for(size_t i=0;i<tofVec.size();i++) {
447  LOG_DEBUG << (*tofVec[i]) << endm;
448  }
449  }
450  }
451  else {
452  LOG_INFO << "No BTofHitCollection" << endm;
453  }
454 
455  }
456  else {
457  LOG_WARN << "No BTofCollection" << endm;
458  LOG_INFO << "No BTofRawHitCollection" << endm;
459  LOG_INFO << "No BTofHitCollection" << endm;
460  }
461 }
void Init(StMaker *maker)
Initial function, need a maker to access the data base.
Int_t numberOfValidTrays()
Returns the number of valid trays.
Definition: StBTofDaqMap.h:116
Int_t WestPMT2TDIGTeChan(const Int_t iTube)
To convert west VPD PMT number to TDIG trailing channel number.
unsigned char globaltdcchan
1,2,......,120,for tray, 121, 122 for upvpd
void Clear(Option_t *option="")
User defined functions.
Int_t WestPMT2TDIGLeChan(const Int_t iTube)
To convert west VPD PMT number to TDIG leading channel number.
Class StRTSBaseMaker - is an abstract StMaker to define the interface to access the DAQ data from the...
StBTofHitMaker(const char *name="tof_raw")
Default constructor.
IntVec ValidTrays()
Returns the list of valid tray Ids.
void setBTofCollection(StBTofCollection *tofColl)
set to use the VPD delays
Definition: daq_tof.h:7
Int_t EastPMT2TDIGTeChan(const Int_t iTube)
To convert east VPD PMT number to TDIG trailing channel number.
unsigned char fiberid
data word before unpack
unsigned int dataword
tdc time (in bin) per hit.
IntVec GetValidChannel(int tray)
Returns the valid channel Ids for a tray.
UIntVec GetLeadingTdc(int tray, int channel, bool triggerevent=true)
Returns the leading Tdcs for one channel. triggerevent used as a switch for physical hits selection...
float getTrayINLCorr(int trayId, int globalTdcChan, int bin)
To get the INL correction tables for trays.
UIntVec GetTrailingTdc(int tray, int channel, bool triggerevent=true)
Returns the trailing Tdcs for one channel. triggerevent used as a switch for physical hits selection...
StBTofCollection * GetBTofCollection()
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.
IntVec TDIGChan2Cell(const Int_t iTdc)
To convert TDIG channel number to module/cell number.
StRtsTable * GetNextRaw()
This is an overloaded member function, provided for convenience.
Definition: Stypes.h:40
virtual StRtsTable * GetNextLegacy()
This is an overloaded member function, provided for convenience.
unsigned char trayID
0 1 2,3
Int_t EastPMT2TDIGLeChan(const Int_t iTube)
float getVpdINLCorr(StBeamDirection eastwest, int globalTdcChan, int bin)
To get the INL correction tables for vpds.
Definition: Stypes.h:41