00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "StBTofHitMaker.h"
00017
00018 #include "StEventTypes.h"
00019 #include "StEvent/StBTofCollection.h"
00020 #include "StEvent/StBTofHeader.h"
00021 #include "StEvent/StBTofRawHit.h"
00022 #include "StBTofUtil/StBTofRawHitCollection.h"
00023 #include "StBTofUtil/StBTofHitCollection.h"
00024 #include "StBTofUtil/StBTofDaqMap.h"
00025 #include "StBTofUtil/StBTofINLCorr.h"
00026 #include "StEvent/StEvent.h"
00027 #include "StDAQMaker/StDAQReader.h"
00028
00029 #include "StRtsTable.h"
00030 #include "DAQ_TOF/daq_tof.h"
00031
00032 ClassImp(StBTofHitMaker);
00033
00034
00035 StBTofHitMaker::StBTofHitMaker(const char *name):StRTSBaseMaker("tof",name)
00036 , mStEvent(0),fTof(0)
00037 , mNValidTrays(-1)
00038 , mBTofCollection(0)
00039 , mRawHitCollection(0)
00040 , mHitCollection(0)
00041 , mBTofDaqMap(0)
00042 , mBTofINLCorr(0)
00043 , mBTofSortRawHit(0)
00044 {
00045 LOG_DEBUG << "StBTofHitMaker::ctor" << endm;
00046 }
00047
00048
00049 StBTofHitMaker::~StBTofHitMaker()
00050 { }
00051
00052
00053 void StBTofHitMaker::Clear(Option_t* option)
00054 {
00055 TofLeadingHits.clear();
00056 TofTrailingHits.clear();
00057
00058 for(int i=0;i<4;i++) mTriggerTimeStamp[i] = 0;
00059 }
00060
00061
00062 Int_t StBTofHitMaker::Init()
00063 {
00064 LOG_INFO << "Initialized conversion parameter VHRBIN2PS = "
00065 << VHRBIN2PS << "ps/ch" << endm;
00066 Clear("");
00067 return kStOK;
00068 }
00069
00070
00071 Int_t StBTofHitMaker::InitRun(Int_t runnumber)
00072 {
00074
00076 LOG_DEBUG << "Initializing DAQ map:" << endm;
00077 mBTofDaqMap = new StBTofDaqMap();
00078 mBTofDaqMap->Init(this);
00079
00080 mNValidTrays = mBTofDaqMap->numberOfValidTrays();
00081
00082 LOG_DEBUG << "Initializing INL table:" << endm;
00083 mBTofINLCorr = new StBTofINLCorr();
00084 mBTofINLCorr->initFromDbase(this);
00085
00086 LOG_DEBUG << "Initializing StBTofSortRawHit:" << endm;
00087 mBTofSortRawHit = new StBTofSortRawHit();
00088 mBTofSortRawHit->Init(this, mBTofDaqMap);
00089
00090 return kStOK;
00091 }
00092
00093
00094 Int_t StBTofHitMaker::FinishRun(Int_t runnumber)
00095 {
00096 if(mBTofDaqMap) delete mBTofDaqMap;
00097 mBTofDaqMap = 0;
00098
00099 if(mBTofINLCorr) delete mBTofINLCorr;
00100 mBTofINLCorr = 0;
00101
00102 return kStOK;
00103 }
00104
00105
00106 Int_t StBTofHitMaker::Finish()
00107 {
00108 Clear("");
00109 return kStOK;
00110 }
00111
00117 StBTofCollection *StBTofHitMaker::GetBTofCollection()
00118 {
00120 StBTofCollection *btofCollection = 0;
00121 mStEvent = dynamic_cast<StEvent *> (GetInputDS("StEvent"));
00122
00123 if (mStEvent) {
00124 btofCollection = mStEvent->btofCollection();
00125
00127 if ( !btofCollection ) {
00129 btofCollection = new StBTofCollection();
00130 mStEvent->setBTofCollection(btofCollection);
00131 }
00132 }
00133 else {LOG_WARN << "No StEvent found" << endm; }
00134
00135 return btofCollection;
00136 }
00137
00138 StRtsTable *StBTofHitMaker::GetNextRaw(int sec)
00139 {
00140 assert(0 && "RTS doesn't provide sector by sector legacy tof banks yet");
00141 StRtsTable *daqTofTable = GetNextLegacy(sec);
00142 if (daqTofTable) {
00143 fTof = (tof_t*)*DaqDta()->begin();
00144 }
00145 return daqTofTable;
00146 }
00147
00148 StRtsTable *StBTofHitMaker::GetNextRaw()
00149 {
00151 LOG_DEBUG << "GetNextRaw()" << endm;
00152
00153 StRtsTable *daqTofTable = GetNextLegacy();
00154 if (daqTofTable) {
00155 fTof = (tof_t*)*DaqDta()->begin();
00156 }
00157 return daqTofTable;
00158 }
00159
00160 Int_t StBTofHitMaker::Make()
00161 {
00162 mBTofCollection = GetBTofCollection();
00163 LOG_DEBUG << " getting the tof collection " << mBTofCollection << endm;
00164 if (mBTofCollection) {
00165 if ( GetNextRaw() ) {
00167 int errorType=UnpackTofRawData();
00168 if(errorType>0) {
00169 LOG_WARN<<"TOF_READER::UnPack TOF Data ERROR!"<<endm;
00170 }
00171 fillBTofRawHitCollection();
00172 fillBTofHeader();
00173 fillBTofHitCollection();
00174 fillStEvent();
00175 }
00176 }
00177 return kStOk;
00178 }
00179
00184
00185 Int_t StBTofHitMaker::UnpackTofRawData()
00186 {
00187 if(!fTof) return 1;
00188
00190 TofLeadingHits.clear();
00191 TofTrailingHits.clear();
00192
00193 for(int ifib=0;ifib<4;ifib++){
00194 int nword=fTof->ddl_words[ifib];
00195 if(nword <= 0) continue;
00196 int halftrayid = -99;
00197 int trayid = -99;
00198 mTriggerTimeStamp[ifib] = 0;
00199
00200
00201
00202 for (int iword=0;iword<nword;iword++) {
00203 unsigned int dataword=fTof->ddl[ifib][iword];
00204
00205
00207 if( (dataword&0xF0000000)>>28 == 0xD) continue;
00208 if( (dataword&0xF0000000)>>28 == 0xE) continue;
00209 if( (dataword&0xF0000000)>>28 == 0xA) {
00210
00211 continue;
00212 }
00213 if( (dataword&0xF0000000)>>28 == 0x2) {
00214 if(mTriggerTimeStamp[ifib]==0) mTriggerTimeStamp[ifib] = dataword;
00215 continue;
00216 }
00217 if( (dataword&0xF0000000)>>28 == 0xC) {
00218 halftrayid = dataword&0x01;
00219 trayid = (dataword&0x0FE)>>1;
00220 continue;
00221 }
00222 if(halftrayid<0) continue;
00223 if(trayid<1 || trayid >124) {
00224 LOG_ERROR<<"StBTofHitMaker::DATA ERROR!! unexpected trayID ! "<<endm;
00225 continue;
00226 }
00227 int edgeid =int( (dataword & 0xf0000000)>>28 );
00228 if((edgeid !=4) && (edgeid!=5)) continue;
00229
00230 int tdcid=(dataword & 0x0F000000)>>24;
00231 int tdigid=tdcid/4;
00232 int tdcchan=(dataword&0x00E00000)>>21;
00233
00234 TofRawHit temphit={0};
00235 memset(&temphit,0,sizeof(temphit));
00236 temphit.fiberid = (UChar_t)ifib;
00237 temphit.trayID = (UChar_t)trayid;
00238 unsigned int timeinbin = ((dataword&0x7ffff)<<2)+((dataword>>19)&0x03);
00239 temphit.tdc = timeinbin;
00241 temphit.globaltdcchan = (UChar_t)(tdcchan + (tdcid%4)*8+tdigid*24+halftrayid*96);
00242 temphit.dataword = dataword;
00243
00244
00245
00246 if(edgeid == 4) {
00247 TofLeadingHits.push_back(temphit);
00248 } else if (edgeid==5){
00249 TofTrailingHits.push_back(temphit);
00250 } else {
00251 LOG_WARN<<"StBTofHitMaker::Unknown TDC data type ! "<<endm;
00252 continue;
00253 }
00255 }
00256
00257 }
00258
00259 return -1;
00260 }
00261
00262
00266 void StBTofHitMaker::fillBTofRawHitCollection()
00267 {
00269 for (unsigned int i=0;i<TofLeadingHits.size();i++){
00270 char flag = (+1)*(TofLeadingHits[i].fiberid+1);
00271 unsigned char trayid = TofLeadingHits[i].trayID;
00272 unsigned char chn = TofLeadingHits[i].globaltdcchan;
00273 unsigned int tdc = TofLeadingHits[i].tdc;
00274 mBTofCollection->addRawHit(new StBTofRawHit(flag,trayid,chn,tdc));
00275 }
00276
00277 for (unsigned int i=0;i<TofTrailingHits.size();i++){
00278 char flag = (-1)*(TofTrailingHits[i].fiberid+1);
00279 unsigned char trayid = TofTrailingHits[i].trayID;
00280 unsigned char chn = TofTrailingHits[i].globaltdcchan;
00281 unsigned int tdc = TofTrailingHits[i].tdc;
00282 mBTofCollection->addRawHit(new StBTofRawHit(flag,trayid,chn,tdc));
00283 }
00284
00285 }
00286
00290 void StBTofHitMaker::fillBTofHeader()
00291 {
00293 StBTofHeader *tofHeader = new StBTofHeader();
00294 for(int i=0;i<4;i++)
00295 tofHeader->setTriggerTime(mTriggerTimeStamp[i], i);
00296
00297 mBTofCollection->setHeader(tofHeader);
00298 }
00299
00303 void StBTofHitMaker::fillBTofHitCollection()
00304 {
00305 mBTofSortRawHit->setBTofCollection(mBTofCollection);
00306
00307
00308 IntVec validtray = mBTofDaqMap->ValidTrays();
00309 for(size_t i=0;i<validtray.size();i++) {
00310 int trayId = validtray[i];
00311 IntVec validchannel = mBTofSortRawHit->GetValidChannel(trayId);
00312 if(Debug()) {
00313 LOG_INFO << " Number of fired hits on tray " << trayId << " = " << validchannel.size() << endm;
00314 }
00315
00316 for(size_t iv=0;iv<validchannel.size();iv++) {
00317 UIntVec leTdc = mBTofSortRawHit->GetLeadingTdc(trayId, validchannel[iv], kTRUE);
00318 UIntVec teTdc = mBTofSortRawHit->GetTrailingTdc(trayId, validchannel[iv], kTRUE);
00319
00320 if(!leTdc.size() || !teTdc.size()) continue;
00321
00322 int chan = validchannel[iv];
00323 IntVec map = mBTofDaqMap->TDIGChan2Cell(chan);
00324 int moduleId = map[0];
00325 int cellId = map[1];
00326
00327
00328
00329
00330 unsigned int tmptdc = leTdc[0];
00331 int bin = tmptdc&0x3ff;
00332 double tmptdc_f = tmptdc + mBTofINLCorr->getTrayINLCorr(trayId, chan, bin);
00333 double letime = tmptdc_f*VHRBIN2PS / 1000.;
00334
00335 tmptdc = teTdc[0];
00336 bin = tmptdc&0x3ff;
00337 tmptdc_f = tmptdc + mBTofINLCorr->getTrayINLCorr(trayId, chan, bin);
00338 double tetime = tmptdc_f*VHRBIN2PS / 1000.;
00339
00340 StBTofHit *aHit = new StBTofHit();
00341 aHit->setHardwarePosition(kBTofId);
00342 aHit->setTray((UChar_t)trayId);
00343 aHit->setModule((UChar_t)moduleId);
00344 aHit->setCell((UChar_t)cellId);
00345 aHit->setLeadingEdgeTime(letime);
00346 aHit->setTrailingEdgeTime(tetime);
00347 mBTofCollection->addHit(aHit);
00348
00349 }
00350 }
00351
00352
00353 for(int ivpd=0;ivpd<2;ivpd++) {
00354 StBeamDirection eastwest = (ivpd==0) ? west : east;
00355 int trayId = (ivpd==0) ? mWestVpdTrayId : mEastVpdTrayId;
00356 IntVec validtube = mBTofSortRawHit->GetValidChannel(trayId);
00357 if(Debug()) {
00358 LOG_INFO << " Number of fired hits on tray(vpd) " << trayId << " = " << validtube.size() << endm;
00359 }
00360
00361 if(!validtube.size()) continue;
00362 for(int i=0;i<mNVPD;i++) {
00363 int tubeId = i+1;
00364 int lechan = (ivpd==0) ? mBTofDaqMap->WestPMT2TDIGLeChan(tubeId) : mBTofDaqMap->EastPMT2TDIGLeChan(tubeId);
00365 int techan = (ivpd==0) ? mBTofDaqMap->WestPMT2TDIGTeChan(tubeId) : mBTofDaqMap->EastPMT2TDIGTeChan(tubeId);
00366 UIntVec leTdc = mBTofSortRawHit->GetLeadingTdc(trayId, lechan, kTRUE);
00367 UIntVec teTdc = mBTofSortRawHit->GetTrailingTdc(trayId, lechan, kTRUE);
00368
00369 if(leTdc.size() && teTdc.size()) {
00370
00371
00372 unsigned int tmptdc = leTdc[0];
00373 int bin = tmptdc&0x3ff;
00374 double tmptdc_f = tmptdc + mBTofINLCorr->getVpdINLCorr(eastwest, lechan, bin);
00375 double letime = tmptdc_f*VHRBIN2PS / 1000.;
00376
00377 tmptdc = teTdc[0];
00378 bin = tmptdc&0x3ff;
00379 tmptdc_f = tmptdc + mBTofINLCorr->getVpdINLCorr(eastwest, techan, bin);
00380 double tetime = tmptdc_f*VHRBIN2PS / 1000.;
00381
00382 StBTofHit *aHit = new StBTofHit();
00383 aHit->setTray((UChar_t)trayId);
00384 aHit->setModule(0);
00385 aHit->setCell((UChar_t)tubeId);
00386 aHit->setLeadingEdgeTime(letime);
00387 aHit->setTrailingEdgeTime(tetime);
00388 mBTofCollection->addHit(aHit);
00389 }
00390 }
00391 }
00392
00393
00394 }
00395
00396
00400 void StBTofHitMaker::fillStEvent() {
00401
00402 LOG_DEBUG << "fillStEvent() Starting..." << endm;
00403
00405 if(!mBTofCollection){
00406 LOG_WARN << "No BTofCollection ... creating one in StEvent" << endm;
00407 mBTofCollection = new StBTofCollection();
00408 mStEvent->setBTofCollection(mBTofCollection);
00409 }
00410
00412 StBTofCollection* btofCollection = mStEvent->btofCollection();
00413 if(btofCollection){
00414 if(btofCollection->rawHitsPresent()) {
00415 StSPtrVecBTofRawHit& rawTofVec = btofCollection->tofRawHits();
00416 LOG_INFO << "BTofRawHitCollection: " << rawTofVec.size() << " entries" << endm;
00417 if(Debug()) {
00418 for(size_t i=0;i<rawTofVec.size();i++) {
00419 LOG_DEBUG << (*rawTofVec[i]) << endm;
00420 }
00421 }
00422 }
00423 else {
00424 LOG_INFO << "No BTofRawHitCollection" << endm;
00425 }
00426
00427 if(btofCollection->hitsPresent()) {
00428 StSPtrVecBTofHit& tofVec = btofCollection->tofHits();
00429 LOG_INFO << "BTofHitCollection: " << tofVec.size() << " entries..." << endm;
00430 if(Debug()) {
00431 for(size_t i=0;i<tofVec.size();i++) {
00432 LOG_DEBUG << (*tofVec[i]) << endm;
00433 }
00434 }
00435 }
00436 else {
00437 LOG_INFO << "No BTofHitCollection" << endm;
00438 }
00439
00440 }
00441 else {
00442 LOG_WARN << "No BTofCollection" << endm;
00443 LOG_INFO << "No BTofRawHitCollection" << endm;
00444 LOG_INFO << "No BTofHitCollection" << endm;
00445 }
00446 }