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