00001
00002
00003
00004
00005 #include "StChain.h"
00006 #include "St_DataSetIter.h"
00007
00008 #include <Stiostream.h>
00009 #include <math.h>
00010
00011 #include "TFile.h"
00012 #include "TArrayF.h"
00013
00014 #include <StMessMgr.h>
00015
00016
00017
00018 #include <StEEmcUtil/database/StEEmcDb.h>
00019 #include <StEEmcUtil/database/EEmcDbItem.h>
00020 #include <StEEmcUtil/database/EEmcDbCrate.h>
00021
00022
00023
00024 #include "StEmcUtil/geometry/StEmcGeom.h"
00025 #include "StEmcUtil/database/StBemcTables.h"
00026 #include "StEmcUtil/database/StEmcDecoder.h"
00027
00028
00029 #include "StEventTypes.h"
00030 #include "StEvent/StEvent.h"
00031 #include "StEvent/StEmcCollection.h"
00032 #include "StEvent/StEmcDetector.h"
00033 #include "StEvent/StTriggerId.h"
00034 #include "StEvent/StTriggerIdCollection.h"
00035
00036
00037 #include "StMuDSTMaker/COMMON/StMuDstMaker.h"
00038 #include "StMuDSTMaker/COMMON/StMuDst.h"
00039 #include "StMuDSTMaker/COMMON/StMuTriggerIdCollection.h"
00040 #include "StMuDSTMaker/COMMON/StMuEvent.h"
00041
00042
00043 #include "StDaqLib/TRG/trgStructures.h"
00044
00045
00046 #include "L2algoUtil/L2EmcDb.h"
00047 #include "L2algoUtil/L2DbConfig.h"
00048 #include "L2algoUtil/L2DbTime.h"
00049
00050
00051 #include "StMuDSTMaker/EZTREE/EztTrigBlob.h"
00052 #include "St_db_Maker/St_db_Maker.h"
00053
00054
00055 #include "StTriggerSimuMaker.h"
00056 #include "StTriggerUtilities/Eemc/StEemcTriggerSimu.h"
00057 #include "StTriggerUtilities/Eemc/EMCdsm2Tree.h"
00058
00059 #include "StGenericL2Emulator.h"
00060
00061
00062 #define MaxBtowRdo (L2EmcDb::BTOW_MAXFEE*L2EmcDb::BTOW_DATSIZE)
00063 #define MaxEtowRdo (L2EmcDb::ETOW_MAXFEE*L2EmcDb::ETOW_DATSIZE)
00064
00065 ClassImp(StGenericL2Emulator)
00066
00067 StGenericL2Emulator::StGenericL2Emulator(){
00068 mBTOW_BANK =new unsigned short [MaxBtowRdo];
00069 mETOW_BANK =new unsigned short [MaxEtowRdo];
00070 mTrigData = new TrgDataType;
00071 mUseMuDst=true;
00072 setMC(false);
00073
00074 mSetupPath="wrong2";
00075 mOutPath="wrong3";
00076 mYear=-1;
00077 mYearMonthDay=-2;
00078 mHourMinSec=-3;
00079 }
00080
00081
00082
00083
00084 StGenericL2Emulator::~StGenericL2Emulator(){
00085 delete [] mBTOW_BANK;
00086 delete [] mETOW_BANK;
00087
00088 }
00089
00090
00091
00092
00093
00094
00095
00096 void StGenericL2Emulator::init(){
00097 mTotInpEve=0;
00098
00099 mDbE = (StEEmcDb*)StMaker::GetChain()->GetDataSet("StEEmcDb");
00100 assert(mDbE);
00101 mGeomB = StEmcGeom::instance("bemc");
00102
00103 mL2EmcDb=0;
00104
00105 LOG_INFO << Form("generic:init() , use: MuDst=1 (StEvent=0)=%d isMC=%d",mUseMuDst,mMCflag) <<endm;
00106 }
00107
00108
00109
00110
00111 void
00112 StGenericL2Emulator::make(){
00113 int L0trgSwitch=1;
00114 #if 0 // filter some events base on L0-trigger decision, if you want
00115 StTriggerSimuMaker *L0trgSim=(StTriggerSimuMaker *)StMaker::GetChain()->GetMaker("StarTrigSimu");
00116 assert(L0trgSim);
00117
00118 EMCdsm2Tree *dsm2tree=L0trgSim->eemc->dsm2TreeADC;
00119 LOG_INFO<<Form("sim L0,dsm2,EEMC: EJP2bit=%d; EEtot1bit=%d , val=%d",
00120 dsm2tree->getOutEndcapJP2bit(), dsm2tree->getOutEndcapSum1bit(),dsm2tree->getIntEndcapSum())<<endm;
00121
00122 if(dsm2tree->getOutEndcapJP2bit()==0) return;
00123
00124
00125 L0trgSwitch=1;
00126 #endif
00127
00128
00129 mTotInpEve++;
00130
00131
00132 if(mUseMuDst) {
00133 doBanksFromMuDst();
00134 } else {
00135 doBanksFromStRawData();
00136 }
00137
00138
00139 int ia;
00140 for(ia=0;ia<mL2algoN;ia++) {
00141 if(mL2algo[ia]==0) continue;
00142 mL2algo[ia]-> doEvent(L0trgSwitch, mTotInpEve, (TrgDataType*)mTrigData,mBTOW_in, mBTOW_BANK, mETOW_in, mETOW_BANK);
00143 }
00144
00145
00146
00147 addTriggerList();
00148
00149 return;
00150 }
00151
00152
00153
00154
00155 void
00156 StGenericL2Emulator::initRun1(){
00157
00158
00159 St_db_Maker* mydb = (St_db_Maker*) StMaker::GetChain()->GetMaker("StarDb");
00160 assert(mydb);
00161 mYear=mydb->GetDateTime().GetYear();
00162 mYearMonthDay=mydb->GetDateTime().GetDate();
00163 mHourMinSec=mydb->GetDateTime().GetTime();
00164
00165
00166
00167 mSetPath=Form("%sL2/%d/db/",mSetupPath.Data(),mYear);
00168 LOG_INFO <<"initRun1() "<<"DB setPath="<<mSetPath.Data()<<" outPath="<<mOutPath.Data()<<endm;
00169
00170
00171 L2DbConfig confDB1(mSetPath+"/L2DbTime.dat");
00172 L2DbTime * confL2 = confDB1.getConfiguration( mYearMonthDay, mHourMinSec );
00173 assert( confL2 );
00174
00175
00176
00177 if(mL2EmcDb) delete mL2EmcDb;
00178 mL2EmcDb=new L2EmcDb((char*)mSetPath.Data(),(char*)mOutPath.Data());
00179
00180 mL2EmcDb->setPedFile ( confL2->getPedFile() );
00181 mL2EmcDb->setMaskFile( confL2->getMaskFile() );
00182
00183
00184
00185 StBemcTables *myTable=new StBemcTables;
00186 StMaker* maker= StMaker::GetChain()->GetMaker("StarDb");
00187 myTable->loadTables(maker );
00188
00189 mMappB = new StEmcDecoder(mydb->GetDateTime().GetDate(),mydb->GetDateTime().GetTime());
00190 LOG_INFO << "initRun1() done"<<endm;
00191
00192
00193 }
00194
00195
00196
00197 void
00198 StGenericL2Emulator::initRun2(int runNo){
00199
00200
00201 enum {mxPar=10};
00202 int intsPar[mxPar];
00203 float floatsPar[mxPar];
00204
00205 LOG_INFO << Form("initRun2() run#=%d begin",runNo)<<endm;
00206 L2DbConfig confDB2(mSetPath+"/L2TriggerIds.dat");
00207
00208 int ia;
00209 for(ia=0;ia<mL2algoN;ia++) {
00210
00211 if (mL2algo[ia]==0) continue;
00212 TString algoName=mL2algo[ia]->getName();
00213 L2DbTime *config = confDB2.getConfiguration(mYearMonthDay,mHourMinSec,algoName);
00214 if(config==0) {
00215 LOG_ERROR << Form("\n************\ninitRun2() failed L2-%s configuration for yyyy=%d hhmmss=%d,\n On explicit request from Renee, the L2 emulator will continue with disabled this particulra L2-algo.\nThis will result with false positives - ignore emulated trigger results for this algo,\nIt would be much better to fix the setup and provide missing record.\nYou have been warned, Jan B.\n***********\n",algoName.Data(),mYearMonthDay,mHourMinSec)<<endm;
00216 mL2algo[ia]=0; continue;
00217
00218 }
00219 TString aa1 = config->getBuf1();
00220 TString aa2 = config->getBuf2();
00221 Int_t trgId = atoi(aa2.Data());
00222 LOG_INFO<<Form("L2algo=%s=initRun2(), trigID=%d setup=%s= ",algoName.Data(),trgId,aa1.Data())<<endm;
00223
00224 TString fullPath=Form("%sL2/%d/algos/%s", mSetupPath.Data(), mYear,aa1.Data());
00225 L2VirtualAlgo::readParams(fullPath, mxPar, intsPar, floatsPar);
00226 assert(mL2algo[ia]->initRun( runNo,intsPar,floatsPar)==0);
00227 mL2algo[ia]->setOflTrigID(trgId);
00228 }
00229 LOG_INFO << "initRun2() done"<<endm;
00230 }
00231
00232
00233
00234 StTriggerSimuDecision
00235 StGenericL2Emulator::isTrigger(int trigId) {
00236 uint j;
00237 for(j=0; j<mAcceptTriggerList.size();j++) {
00238 if(trigId==mAcceptTriggerList[j]) return kYes;
00239 }
00240 for(j=0; j<mVetoTriggerList.size();j++) {
00241 if(trigId==mVetoTriggerList[j]) return kNo;
00242 }
00243 return kDoNotCare;
00244 }
00245
00246
00247
00248 void
00249 StGenericL2Emulator::finish() {
00250
00251 LOG_INFO <<"Finish()=======\n totEveSeen="<< mTotInpEve<<endm;
00252
00253 int ia;
00254 for(ia=0;ia<mL2algoN;ia++)
00255 if(mL2algo[ia]) mL2algo[ia]->finishRun();
00256 LOG_INFO <<"Finish()======= end"<<endm;
00257
00258 }
00259
00260
00261 void
00262 StGenericL2Emulator::clear( ){
00263 mBTOW_in=mETOW_in=0;
00264 memset(mBTOW_BANK,0,MaxBtowRdo*sizeof(unsigned short));
00265 memset(mETOW_BANK,0,MaxEtowRdo*sizeof(unsigned short));
00266 memset(mTrigData,0,sizeof(TrgDataType));
00267 mAcceptTriggerList.clear();
00268 mVetoTriggerList.clear();
00269 }
00270
00271
00272
00273
00274
00275 void StGenericL2Emulator::printBEtowers(){
00276 StEvent* mEvent = (StEvent*)StMaker::GetChain()-> GetInputDS("StEvent");
00277 assert(mEvent);
00278 StEmcCollection* emcCollection = mEvent->emcCollection();
00279
00280 StEmcDetector* twB = emcCollection->detector(kBarrelEmcTowerId);
00281 StEmcDetector* twE = emcCollection->detector(kEndcapEmcTowerId);
00282 if(twE==0) {
00283 printf(" StGenericL2Emulator found no E-EMC tower data in StEvent, skip event\n");
00284 return ;
00285 }
00286
00287 int i;
00288
00289 if(twB) {
00290 printf(" StGenericL2Emulator:: B_EMC Tower HITS ...\n");
00291 for ( i = 1; i <= (int)twB->numberOfModules(); i++) {
00292 StSPtrVecEmcRawHit& emcTowerHits = twB->module(i)->hits();
00293 uint j;
00294 for ( j = 0; j < emcTowerHits.size(); j++) {
00295 int adc= emcTowerHits[j]->adc();
00296 int mod= emcTowerHits[j]->module();
00297 int sub= emcTowerHits[j]->sub();
00298 int eta= emcTowerHits[j]->eta();
00299 float energy= emcTowerHits[j]->energy();
00300 printf("j=%d, mod=%d, sub=%d, eta=%d adc=%d ener=%f\n",j,mod,sub,eta,adc,energy);
00301 }
00302 }
00303 } else {
00304 printf("StGenericL2Emulator found no B-EMC tower data in StEvent, skip event\n");
00305 }
00306
00307 if(twE) {
00308 printf("StGenericL2Emulator:: E_EMC Tower HITS ... %d\n",twE->numberOfModules());
00309 for ( i = 0; i < (int)twE->numberOfModules(); i++) {
00310
00311 StEmcModule* stmod = twE->module(i);
00312 if(stmod==0) continue;
00313 StSPtrVecEmcRawHit& emcTowerHits = stmod->hits();
00314 uint j;
00315 for ( j = 0; j < emcTowerHits.size(); j++) {
00316 int adc= emcTowerHits[j]->adc();
00317 int sec= emcTowerHits[j]->module()+1;
00318 int sub= emcTowerHits[j]->sub()+'A';
00319 int eta= emcTowerHits[j]->eta()+1;
00320 float energy= emcTowerHits[j]->energy();
00321 printf("j=%d, sec=%d, sub=%c, eta=%d adc=%d ener=%f\n",j,sec,sub,eta,adc,energy);
00322 }
00323 }
00324 } else {
00325 printf("StGenericL2Emulator found no E-EMC tower data in StEvent, skip event\n");
00326 }
00327
00328 }
00329
00330
00331
00332
00333 void
00334 StGenericL2Emulator::doBanksFromStRawData(){
00335 assert(mUseMuDst==false);
00336 return;
00337 assert(1==2);
00338
00339 StEvent *mEvent = (StEvent *)StMaker::GetChain()-> GetInputDS("StEvent");
00340 if (!mEvent) {
00341 LOG_ERROR<< "StGenericL2Emulator::getStEmcDetector() -- no StEvent found" << endm; return ;
00342 }
00343 StEmcCollection *emcColl = mEvent->emcCollection();
00344 if (!emcColl) {
00345 return ;
00346 }
00347
00348 int icr;
00349
00350
00351 StEmcRawData *rawB = mEvent->emcCollection()->bemcRawData();
00352 assert(rawB);
00353 icr=0;
00354 printf(" BTOW size=%d\n",rawB->sizeData(icr));
00355 assert(rawB->sizeData(icr) <=MaxBtowRdo);
00356 unsigned short* adc=rawB->data(icr);
00357 int i;
00358
00359 for(i=0;i<rawB->sizeData(icr);i++) mBTOW_BANK[i]=adc[i];
00360
00361
00362 StEmcRawData *rawE = mEvent->emcCollection()->eemcRawData();
00363 assert(rawE);
00364 assert(rawE->sizeData(icr) <=MaxEtowRdo);
00365
00366 for(icr=0;icr<mDbE->getNFiber();icr++) {
00367 const EEmcDbCrate *fiber=mDbE->getFiber(icr);
00368 if(fiber->crID>6) continue;
00369 if(rawE->sizeHeader(icr)<=0) continue;
00370 assert(fiber->useIt);
00371 printf(" ETOW crID=%d type=%c size=%d\n",fiber->crID,fiber->type,rawE->sizeData(icr));
00372 assert(fiber->crID==icr+1);
00373 unsigned short* adc=rawE->data(icr);
00374 int i;
00375 for(i=0;i<rawE->sizeData(icr);i++) {
00376 int rdo=icr + i*L2EmcDb::ETOW_MAXFEE;
00377 assert(rdo>=0 && rdo<MaxEtowRdo);
00378 mETOW_BANK[rdo]=adc[i];
00379 }
00380
00381 }
00382
00383 }
00384
00385
00386
00387
00388
00389
00390
00391
00392 void
00393 StGenericL2Emulator::doBanksFromMuDst(){
00394
00395 assert(mUseMuDst==true);
00396
00397 StEvent *mEvent = (StEvent*)StMaker::GetChain()-> GetInputDS("StEvent");
00398 assert(mEvent);
00399
00400 StMuDstMaker *muMk = (StMuDstMaker*)StMaker::GetChain()->GetMaker("MuDst");
00401 assert(muMk);
00402
00403 StMuEmcCollection* muEmc = muMk->muDst()->muEmcCollection();
00404
00405
00406 int nE=0;
00407 int i;
00408 for (i=0; i < muEmc->getNEndcapTowerADC(); i++) {
00409 int sec,eta,sub,rawAdc;
00410 muEmc->getEndcapTowerADC(i,rawAdc,sec,sub,eta);
00411 assert(sec>0 && sec<=MaxSectors);
00412
00413 const EEmcDbItem *x=mDbE->getTile(sec,'A'+sub-1,eta,'T');
00414 assert(x);
00415 assert(x->crate>0);
00416 assert(x->crate<=6);
00417 int rdo=x->crate-1 + x->chan*L2EmcDb::ETOW_MAXFEE;
00418 mETOW_BANK[rdo]=rawAdc;
00419 nE++;
00420 }
00421 mETOW_in=1;
00422 LOG_DEBUG << Form("doBanksFromMuDst() , ETOW nAdc=%d",nE)<<endm; assert(nE==720);
00423
00424
00425
00426
00427 if( mEvent)
00428 {
00429 StEmcCollection *emc = mEvent->emcCollection();
00430 if (emc)
00431 {
00432 StEmcDetector* detector=emc->detector(kBarrelEmcTowerId);
00433 if(detector)
00434 {
00435 for(Int_t m = 1; m <= 120; ++m)
00436 {
00437 StEmcModule* module = detector->module(m);
00438 if(module)
00439 {
00440 StSPtrVecEmcRawHit& rawHit=module->hits();
00441 for(UInt_t k = 0; k < rawHit.size(); ++k)
00442 {
00443 if(rawHit[k])
00444 {
00445 int did, RDO;
00446
00447 Int_t m=rawHit[k]->module();
00448 Int_t e=rawHit[k]->eta();
00449 Int_t s=abs(rawHit[k]->sub());
00450 Int_t adc=rawHit[k]->adc();
00451
00452
00453 mGeomB->getId(m,e,s,did);
00454 mMappB->GetDaqIdFromTowerId(did,RDO);
00455 mBTOW_BANK[RDO]=adc;
00456 }
00457 }
00458 }
00459 }
00460 }
00461 }
00462 mBTOW_in=1;
00463 }
00464 else
00465 {
00466
00467 int nB=0;
00468 int id;
00469 for (id=1; id <=4800 ; id++)
00470 {
00471 int rawAdc= muEmc->getTowerADC(id);
00472 int RDO;
00473 assert(mMappB->GetDaqIdFromTowerId(id,RDO)==1);
00474 mBTOW_BANK[RDO]=rawAdc;
00475 nB++;
00476 }
00477
00478 mBTOW_in=1;
00479
00480
00481
00482
00483
00484
00485 LOG_INFO << Form("doBanksFromMuDst() , BTOW nAdc=%d",nB)<<endm;
00486 assert(nB==4800);
00487
00488 }
00489
00490 }
00491
00492
00493
00494 void
00495 StGenericL2Emulator::printBEblocks(){
00496 int i;
00497
00498 printf("printBEblocks(), just begin & end of each block, mBTOW_in=%d mETOW_in=%d\n",mBTOW_in,mETOW_in);
00499
00500 if(mBTOW_in) {
00501 for(i=0;i<10;i++) printf("BTOWi=%d ADC=%d\n",i,mBTOW_BANK[i]);
00502 for(i=4790;i<4800;i++) printf("BTOWi=%d ADC=%d\n",i,mBTOW_BANK[i]);
00503 }
00504
00505 if( mETOW_in){
00506 for(i=0;i<10;i++) printf("ETOWi=%d ADC=%d\n",i,mETOW_BANK[i]);
00507 for(i=710;i<720;i++) printf("ETOWi=%d ADC=%d\n",i,mETOW_BANK[i]);
00508 }
00509 }
00510
00511
00512
00513
00514 void
00515 StGenericL2Emulator::addTriggerList() {
00516
00517 int ia;
00518 for(ia=0;ia<mL2algoN;ia++) {
00519 if (mL2algo[ia]==0) continue;
00520 if (mL2algo[ia]->getOflTrigID()==0) continue;
00521 if (mL2algo[ia]->accepted())
00522 mAcceptTriggerList.push_back(mL2algo[ia]->getOflTrigID());
00523 else
00524 mVetoTriggerList.push_back(mL2algo[ia]->getOflTrigID());
00525 }
00526
00527 LOG_DEBUG << Form("addTriggerList() yesSize=%d vetoSize=%d",mAcceptTriggerList.size(),mVetoTriggerList.size())<<endm;
00528 }
00529
00530 const unsigned int * StGenericL2Emulator::result() const {
00531 return ( (TrgDataType*)mTrigData)->TrgSum.L2Result;
00532 }
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615