StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StBTofSortRawHit.cxx
1 /*******************************************************************
2  *
3  * $Id: StBTofSortRawHit.cxx,v 1.10 2010/12/10 21:37:45 geurts Exp $
4  *
5  * Author: Xin Dong
6  *****************************************************************
7  *
8  * Description: Utilities to group the raw Tof hits for convenient use
9  *
10  *****************************************************************
11  *
12  *******************************************************************/
13 
14 #include <iostream>
15 #include "StMessMgr.h"
16 #include "StBTofSortRawHit.h"
17 #include "StEvent.h"
18 #include "StBTofCollection.h"
19 #include "StBTofRawHit.h"
20 #include "StBTofDaqMap.h"
21 #include "tables/St_tofTrgWindow_Table.h"
22 #include "tables/St_vpdDelay_Table.h"
23 
24 StBTofSortRawHit::StBTofSortRawHit() {
25  mDaqMap = 0;
26  mDebug = kFALSE;
27  Reset();
28 }
29 
30 StBTofSortRawHit::~StBTofSortRawHit() {
31  Reset();
32 }
33 
34 void StBTofSortRawHit::Reset() {
35  for(int i=0;i<mNTRAY;i++) mRawHitVec[i].clear();
36  memset(mTriggerTime,0,sizeof(mTriggerTime));
37 }
38 
39 void StBTofSortRawHit::Init() {
40  Reset();
41  memset(mTriggerTimeWindow,0,sizeof(mTriggerTimeWindow));
42  memset(mVpdDelay,0,sizeof(mVpdDelay));
43 }
44 
45 void StBTofSortRawHit::Init(StMaker *maker, StBTofDaqMap *daqMap) {
46  // initial time windows from dbase
47  Init();
48 
49  // for test set by hand now
51  LOG_INFO << "[StBTofSortRawHit] retrieving BTOF trigger time window cuts" << endm;
52  TDataSet *mDbTOFDataSet = maker->GetDataBase("Calibrations/tof/tofTrgWindow");
53  St_tofTrgWindow* tofTrgWindow = static_cast<St_tofTrgWindow*>(mDbTOFDataSet->Find("tofTrgWindow"));
54  if(!tofTrgWindow) {
55  LOG_ERROR << "unable to get tof Module map table" << endm;
56  return; // kStErr;
57  }
58  tofTrgWindow_st* trgWin = static_cast<tofTrgWindow_st*>(tofTrgWindow->GetArray());
59  for (Int_t i=0;i<mNTRAY;i++) {
60  mTriggerTimeWindow[i][0] = (Float_t)trgWin[i].trgWindow_Min;
61  mTriggerTimeWindow[i][1] = (Float_t)trgWin[i].trgWindow_Max;
62  if(maker->Debug()) {
63  LOG_DEBUG << " Tray = " << i+1 << " Trigger Window = " << mTriggerTimeWindow[i][0] << " " << mTriggerTimeWindow[i][1] << endm;
64  }
65  }
66 
67  mDaqMap = daqMap;
68 
69  if(maker->Debug()) mDebug = kTRUE;
70 
71  LOG_INFO << "[StBTofSortRawHit] retrieving VPD delay settings" << endm;
72  mDbTOFDataSet = maker->GetDataBase("Calibrations/tof/vpdDelay");
73 
74  //fg
75  if (mDbTOFDataSet){
76  St_vpdDelay *vpdDelayTable = static_cast<St_vpdDelay*>(mDbTOFDataSet->Find("vpdDelay"));
77  if (!vpdDelayTable) {
78  LOG_ERROR << "unable to find vpdDelay table" << endm;
79  return;
80  }
81  vpdDelay_st* vpdDelay = static_cast<vpdDelay_st*>(vpdDelayTable->GetArray());
82  if (!vpdDelay) {
83  LOG_ERROR << "unable to get vpdDelay data" << endm;
84  return;
85  }
86  for (int i=0;i<2*mNVPD;i++){
87  mVpdDelay[i] = vpdDelay->delay[i];
88  }
89  }
90  else {
91  // Note: this construction addresses RT#1996 and allows backward compatibility with older database
92  // timestamps at which these database structures did not exist.
93  LOG_WARN << "unable to find vpdDelay dataset: assuming Run-9 (200GeV) default values for vpdDelay" << endm;
94  float delay[2*mNVPD]={
95  0,-0.564753,-4.62291,-4.84402,-4.05943,6.32389,-9.4035,-10.3113,-17.0374,-17.3734,-6.04608,-11.9614,-12.7579,8.79609,3.8467,-17.2994,-17.6424,-21.4749,-22.9736,
96  0,-2.1707, -4.8195, -6.5161, -4.3109, 6.3116, -8.8655,-10.1037,-16.5970,-17.9588,-5.2079, -12.1249,-12.2412,8.4001, 5.5702,-16.5936,-16.4152,-21.3076,-21.1452};
97 
98  for (int i=0;i<2*mNVPD;i++){
99  mVpdDelay[i] = delay[i];
100  }
101  }
102 
103  return;
104 }
105 
107 
109  Reset();
110 
111  if(tofColl && tofColl->tofHeader()) {
112  for(int i=0;i<mNFIBER;i++) {
113  mTriggerTime[i] = tofColl->tofHeader()->triggerTime(i);
114  }
115  if(mDebug) {
116  LOG_INFO << " Trigger Time Stamps " << endm;
117  LOG_INFO << mTriggerTime[0] << " " << mTriggerTime[1] << " " <<
118  mTriggerTime[2] << " " << mTriggerTime[3] << endm;
119  }
120  }
121 
122  if(tofColl && tofColl->rawHitsPresent()) {
123 
124  StSPtrVecBTofRawHit &tofRawHits = tofColl->tofRawHits();
125 
126  // test
127  if(mDebug) {
128  LOG_INFO << " INPUT ...... " << endm;
129  for(size_t i=0; i<tofRawHits.size(); i++) {
130  LOG_INFO << " flag=" << tofRawHits[i]->flag()
131  << " tray=" << tofRawHits[i]->tray() << " channel="
132  << tofRawHits[i]->channel() << " tdc="
133  << tofRawHits[i]->tdc() << endm;
134  }
135  }
136  //
137 
138  for(size_t i=0; i<tofRawHits.size(); i++) {
139  if(!tofRawHits[i]->leadingEdge()) continue; // leading
140  int itray = tofRawHits[i]->tray();
141  int ichan = tofRawHits[i]->channel();
142  int ifiber = tofRawHits[i]->fiberId();
143  if (itray<=0 || itray>mNTRAY || ichan<0 || ichan>=mNCHAN || ifiber<0 || ifiber>=mNFIBER ) {
144  LOG_FATAL << " StBTofSortRawHit::setBTofCollection:: "
145  << ": itray=" << itray
146  << ": ichan=" << ichan
147  << ": ifiber=" << ifiber
148  << endm;
149  continue;
150  }
151  bool iexist = kFALSE;
152  for(size_t ii=0; ii<mRawHitVec[itray-1].size(); ii++) {
153  if(itray==mRawHitVec[itray-1][ii].tray && ichan==mRawHitVec[itray-1][ii].channel) {
154  iexist = kTRUE;
155  break;
156  }
157  }
158  if(iexist) continue;
159  TOFRawHit aRawHit;
160  aRawHit.fiberId = ifiber;
161  aRawHit.tray = itray;
162  aRawHit.channel = ichan;
163  aRawHit.leadingTdc.push_back(tofRawHits[i]->tdc());
164  for(size_t j=i+1;j<tofRawHits.size();j++) {
165  if(tofRawHits[j]->leadingEdge() &&
166  itray==tofRawHits[j]->tray() && ichan==tofRawHits[j]->channel()) {
167  aRawHit.leadingTdc.push_back(tofRawHits[j]->tdc());
168  }
169  }
170 
171  // trailing
172  if(itray<=120) { // trays
173  for(size_t j=0;j<tofRawHits.size();j++) {
174  if(tofRawHits[j]->trailingEdge() &&
175  itray==tofRawHits[j]->tray() && ichan==tofRawHits[j]->channel()) {
176  aRawHit.trailingTdc.push_back(tofRawHits[j]->tdc());
177  }
178  }
179  } else {
180  if(!mDaqMap) {
181  LOG_INFO << " No Daq Map for VPD, continue!" << endm;
182  continue;
183  }
184  int iTube = (itray==121) ? mDaqMap->TDIGLeChan2WestPMT(ichan) : mDaqMap->TDIGLeChan2EastPMT(ichan);
185  for(size_t j=0;j<tofRawHits.size();j++) {
186  if(tofRawHits[j]->trailingEdge() && itray==tofRawHits[j]->tray()) {
187  int techan = (int)(tofRawHits[j]->channel());
188  int jTube = (itray==121) ? mDaqMap->TDIGTeChan2WestPMT(techan) : mDaqMap->TDIGTeChan2EastPMT(techan);
189  if(iTube==jTube) {
190  aRawHit.trailingTdc.push_back(tofRawHits[j]->tdc());
191  }
192  }
193  }
194  }
195  if(aRawHit.trailingTdc.size()) mRawHitVec[itray-1].push_back(aRawHit);
196  }
197  } else {
198  LOG_WARN << " No Tof Collection !!! " << endm;
199  }
200 
201  if(mDebug) {
202  for(int i=0;i<122;i++) {
203  for(size_t m=0;m<mRawHitVec[i].size();m++) {
204  LOG_DEBUG << " tray = " << i+1 << " " << mRawHitVec[i][m].tray << " channel = " << mRawHitVec[i][m].channel << endm;
205  LOG_DEBUG << " leading tdcs = ";
206  for(size_t j=0;j<mRawHitVec[i][m].leadingTdc.size();j++) {
207  LOG_DEBUG << " " << mRawHitVec[i][m].leadingTdc[j];
208  }
209  LOG_DEBUG << endm;
210  LOG_DEBUG << " trailing tdcs = ";
211  for(size_t j=0;j<mRawHitVec[i][m].trailingTdc.size();j++) {
212  LOG_DEBUG << " " << mRawHitVec[i][m].trailingTdc[j];
213  }
214  LOG_DEBUG << endm;
215  }
216  }
217  }
218 }
219 
221  IntVec chanVec;
222  for(size_t i=0 ; i<mRawHitVec[tray-1].size() ; i++) {
223  if( mRawHitVec[tray-1][i].tray == tray )
224  chanVec.push_back(mRawHitVec[tray-1][i].channel);
225  }
226  return chanVec;
227 }
228 
229 UIntVec StBTofSortRawHit::GetLeadingTdc(int tray, int channel, bool triggerevent)
230 {
231  UIntVec leTdc;
232  for(size_t i=0 ; i<mRawHitVec[tray-1].size() ; i++) {
233  if(mRawHitVec[tray-1][i].tray!=tray) continue;
234  if(mRawHitVec[tray-1][i].channel!=channel) continue;
235  int fiberId = mRawHitVec[tray-1][i].fiberId;
236  double ftime = -1e5;
237  for(size_t j=0; j<mRawHitVec[tray-1][i].leadingTdc.size(); j++) {
238  float trgTime = 25.*(mTriggerTime[fiberId] & 0xfff);
239  float timeDiff = mRawHitVec[tray-1][i].leadingTdc[j]*25./1024 - trgTime;
240  while(timeDiff<0) timeDiff += 51200;
241  if(tray<=120){ //trays, keep all hits
242  if(triggerevent){
243  if(timeDiff>=mTriggerTimeWindow[tray-1][0]&&timeDiff<=mTriggerTimeWindow[tray-1][1])
244  leTdc.push_back(mRawHitVec[tray-1][i].leadingTdc[j]);
245  }
246  else {
247  leTdc.push_back(mRawHitVec[tray-1][i].leadingTdc[j]);
248  }
249  }
250  else {
251  if(triggerevent){ //vpds, keep physical hits in trigger window
252 
253  // add Vpd delay
254  int iTube = (tray==121) ? mDaqMap->TDIGLeChan2WestPMT(channel) : mDaqMap->TDIGLeChan2EastPMT(channel);
255  timeDiff -= mVpdDelay[(tray-121)*mNVPD+(iTube-1)];
256  while(timeDiff<0) timeDiff += 51200;
257 
258  double stime = timeDiff;
259  if(stime > ftime+300.){ //all hits thereafter within 300ns are not physical
260  if(timeDiff>=mTriggerTimeWindow[tray-1][0]&&timeDiff<=mTriggerTimeWindow[tray-1][1])
261  leTdc.push_back(mRawHitVec[tray-1][i].leadingTdc[j]);
262  ftime = stime;
263  }
264  }
265  else {
266  leTdc.push_back(mRawHitVec[tray-1][i].leadingTdc[j]);
267  }
268  }
269  }
270  }
271  return leTdc;
272 }
273 
274 UIntVec StBTofSortRawHit::GetTrailingTdc(int tray, int channel, bool triggerevent)
275 {
276  UIntVec teTdc;
277  for(size_t i=0 ; i<mRawHitVec[tray-1].size() ; i++) {
278  if(mRawHitVec[tray-1][i].tray!=tray) continue;
279  if(mRawHitVec[tray-1][i].channel!=channel) continue;
280  int fiberId = mRawHitVec[tray-1][i].fiberId;
281  double ftime = -1e5;
282  for(size_t j=0; j<mRawHitVec[tray-1][i].trailingTdc.size(); j++) {
283  float trgTime = 25.*(mTriggerTime[fiberId] & 0xfff);
284  float timeDiff = mRawHitVec[tray-1][i].trailingTdc[j]*25./1024 - trgTime;
285  while(timeDiff<0) timeDiff += 51200;
286  if(tray<=120){ //trays, keep all hits
287  if(triggerevent){
288  if(timeDiff>=mTriggerTimeWindow[tray-1][0]&&timeDiff<=mTriggerTimeWindow[tray-1][1]+25.) // trailing edge - allow 25 ns shaping time
289  teTdc.push_back(mRawHitVec[tray-1][i].trailingTdc[j]);
290  }
291  else {
292  teTdc.push_back(mRawHitVec[tray-1][i].trailingTdc[j]);
293  }
294  }
295  else {
296  if(triggerevent){ //vpds, keep physical hits in trigger window
297 
298  // add Vpd delay - Note: channel should be the corresponding leading edge channel
299  int iTube = (tray==121) ? mDaqMap->TDIGLeChan2WestPMT(channel) : mDaqMap->TDIGLeChan2EastPMT(channel);
300  timeDiff -= mVpdDelay[(tray-121)*mNVPD+(iTube-1)];
301  while(timeDiff<0) timeDiff += 51200;
302 
303  double stime = timeDiff;
304  if(stime > ftime+300.){ //all hits thereafter within 300ns are not physical
305  if(timeDiff>=mTriggerTimeWindow[tray-1][0]&&timeDiff<=mTriggerTimeWindow[tray-1][1]+25.) // trailing edge - allow 25ns shaping time
306  teTdc.push_back(mRawHitVec[tray-1][i].trailingTdc[j]);
307  ftime = stime;
308  }
309  }
310  else {
311  teTdc.push_back(mRawHitVec[tray-1][i].trailingTdc[j]);
312  }
313  }
314  }
315  }
316  return teTdc;
317 }
Int_t TDIGLeChan2WestPMT(const Int_t iTdc)
To convert TDIG leading channel number to west VPD PMT number.
void setBTofCollection(StBTofCollection *tofColl)
set to use the VPD delays
Int_t TDIGLeChan2EastPMT(const Int_t iTdc)
To convert TDIG leading channel number to east VPD PMT number.
Int_t TDIGTeChan2EastPMT(const Int_t iTdc)
To convert TDIG trailing channel number to east VPD PMT number.
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...
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...
Int_t TDIGTeChan2WestPMT(const Int_t iTdc)
To convert TDIG trailing channel number to west VPD PMT number.
virtual TDataSet * Find(const char *path) const
Definition: TDataSet.cxx:362