00001
00012
00013 #include "GaudiKernel/AlgFactory.h"
00014 #include "GaudiKernel/SvcFactory.h"
00015 #include "GaudiKernel/ISvcLocator.h"
00016 #include "GaudiKernel/MsgStream.h"
00017 #include "GaudiKernel/Incident.h"
00018 #include "GaudiKernel/IIncidentSvc.h"
00019 #include "GaudiKernel/IDataProviderSvc.h"
00020 #include "GaudiKernel/DataObject.h"
00021 #include "GaudiKernel/SmartDataPtr.h"
00022 #include "GaudiKernel/Bootstrap.h"
00023
00024 #include "Trigger/BesGlobalTrigSvc.h"
00025 #include "Trigger/BesMdcTrig.h"
00026 #include "Trigger/BesTofTrig.h"
00027 #include "Trigger/BesEmcTrig.h"
00028 #include "Trigger/BesMucTrig.h"
00029 #include "Trigger/BesTMTrig.h"
00030 #include "CLHEP/Units/PhysicalConstants.h"
00031 #include <iostream>
00032 #include <fstream>
00033 #include <string>
00034 using namespace CLHEP;
00035 using namespace std;
00036
00037 const std::string BesGlobalTrigSvc::COND_NAME[] = {
00038 "NClus1",
00039 "NClus2",
00040 "BClusBB",
00041 "EClusBB",
00042 "Clus_Z",
00043 "BClus_PHI",
00044 "EClus_PHI",
00045 "BEtot_H",
00046 "EEtot_H",
00047 "Etot_L",
00048 "Etot_M",
00049 "BL_Z",
00050 "NBClus1",
00051 "NEClus1",
00052 "BL_BBLK",
00053 "BL_EBLK",
00054 "ETofBB",
00055 "BTofBB",
00056 "NETof2",
00057 "NETof1",
00058 "NBTof2",
00059 "NBTof1",
00060 "NTof1",
00061 "CTrk_BB",
00062 "NCTrk2",
00063 "NCTrk1",
00064 "BTrk_BB",
00065 "NBTrk2",
00066 "NBTrk1",
00067 "ATrk_BB",
00068 "NATrk2",
00069 "NATrk1",
00070 "MUONBB3478",
00071 "MUONBB37",
00072 "NotDefined",
00073 "NotDefined",
00074 "NotDefined",
00075 "NotDefined",
00076 "STrk_BB",
00077 "NStrkN",
00078 "NStrk2",
00079 "NStrk1",
00080 "LTrk_BB",
00081 "NLtrkN",
00082 "NLtrk2",
00083 "NLtrk1",
00084 "NItrk2",
00085 "NItrk1"
00086 };
00087
00088
00089
00090
00091
00092
00093 BesGlobalTrigSvc::BesGlobalTrigSvc( const std::string& name,
00094 ISvcLocator* svc ) : Service( name, svc ),m_MdcTrig(0),m_TofTrig(0),m_EmcTrig(0),m_MucTrig(0),m_TMTrig(0)
00095 {
00096 trigTableFile = std::string(getenv( "TRIGGERROOT" ));
00097 trigTableFile += std::string("/share/TriggerTable.txt");
00098 declareProperty("TrigTableCon", trigTableFile);
00099 declareProperty("TofLayerControl", m_tofLayer = 2);
00100 declareProperty("L1TC_GATE", L1TC_GATE = 70);
00101 declareProperty("L1TC_THRESH", L1TC_THRESH = 130);
00102 declareProperty("L1ETOT_L", L1ETOT_L = 0x56);
00103 declareProperty("L1ETOT_M", L1ETOT_M = 0xba);
00104 declareProperty("L1ETOT_BR", L1ETOT_BR = 0x19a);
00105 declareProperty("L1ETOT_EC", L1ETOT_EC = 0x19a);
00106 declareProperty("L1EBL_BR", L1EBL_BR = 0x30);
00107 declareProperty("L1EBL_EC", L1EBL_EC = 0x1ec);
00108 declareProperty("L1EDIFF_BR", L1EDIFF_BR = 0xf6);
00109 declareProperty("L1EDIFF_EC", L1EDIFF_EC = 0xe1);
00110 declareProperty("L1BLK_GATE", L1BLK_GATE = 0x30);
00111 declareProperty("L1EBL_Z", L1EBL_Z = 0xa4);
00112 declareProperty("MAXMDCTRACK", MAXMDCTRACK = 4);
00113 declareProperty("TM_ENDCAP_MODE", tm_endcap = 0);
00114 declareProperty("Energy_Ratio", Energy_Ratio = 1.);
00115 }
00116 BesGlobalTrigSvc::~BesGlobalTrigSvc()
00117 {
00118 }
00119
00120
00121
00122 StatusCode BesGlobalTrigSvc::initialize()
00123 {
00124 MsgStream log(msgSvc(), name());
00125
00126 StatusCode sc = Service::initialize();
00127 if( sc.isFailure() ) return sc;
00128
00129 StatusCode status;
00130
00131 IIncidentSvc* incsvc;
00132 status = service("IncidentSvc", incsvc);
00133 int priority = 100;
00134 if( status.isSuccess() ){
00135 incsvc -> addListener(this, "NewRun", priority);
00136 }
00137
00138 IRealizationSvc *tmpReal;
00139 status = service("RealizationSvc",tmpReal);
00140 if (!status.isSuccess())
00141 {
00142 log << MSG::FATAL << " Could not initialize Realization Service" << endreq;
00143 } else {
00144 m_RealizationSvc=dynamic_cast<RealizationSvc*>(tmpReal);
00145 }
00146
00147 m_MdcTrig = new BesMdcTrig();
00148 m_TofTrig = new BesTofTrig();
00149 m_EmcTrig = new BesEmcTrig();
00150 m_MucTrig = new BesMucTrig();
00151 m_TMTrig = new BesTMTrig();
00152
00153 if(!m_MdcTrig) log<<MSG::FATAL<<"can not get BesMdcTrig pointer "<<endreq;
00154 if(!m_TofTrig) log<<MSG::FATAL<<"can not get BesTofTrig pointer "<<endreq;
00155 if(!m_EmcTrig) log<<MSG::FATAL<<"can not get BesEmcTrig pointer "<<endreq;
00156 if(!m_MucTrig) log<<MSG::FATAL<<"can not get BesMucTrig pointer "<<endreq;
00157 if(!m_TMTrig) log<<MSG::FATAL<<"can not get BesTMTrig pointer "<<endreq;
00158
00159 trigChannel.clear();
00160 trigCondName.clear();
00161 trigTable.clear();
00162 channelNo.clear();
00163
00164 if(m_RealizationSvc->UseDBFlag() == false) {
00165 ifstream infile;
00166 infile.open(trigTableFile.c_str(),ios_base::in);
00167 if(!infile) log<<MSG::FATAL<<"can not open Trigger Table file: "<<trigTableFile<<endreq;
00168 char line[ 255 ];
00169
00170 do{
00171 infile.getline( line, 255 );
00172 } while( line[0] != 'C' );
00173
00174 std::string chan;
00175 int num;
00176 char* token = strtok( line, " " );
00177 do{
00178 if ( token ) {
00179 chan = token;
00180 token = strtok( NULL, " " );
00181 if(chan!="CHANNEL") trigChannel.push_back(chan);
00182 }
00183 else continue;
00184 }while ( token != NULL );
00185
00186 infile.getline( line, 255 );
00187 token = strtok( line, " " );
00188 do{
00189 if( token ) {
00190 chan = token;
00191 token = strtok(NULL, " ");
00192 num = atoi(chan.c_str());
00193 channelNo.push_back(num);
00194 }
00195 else continue;
00196 }while ( token!=NULL);
00197
00198 while(infile) {
00199 infile.getline( line, 255 );
00200 if(line[0]==' ') continue;
00201 if(line[0]=='#') break;
00202 token = strtok( line, " " );
00203 do{
00204 if(token) {
00205 chan = token;
00206 token = strtok(NULL," ");
00207 if(chan=="1"||chan=="0"||chan=="-") { trigTable.push_back(chan); }
00208 else {
00209 trigCondName.push_back(chan);
00210 }
00211 }
00212 else continue;
00213 }while (token!=NULL);
00214 }
00215
00216 for(unsigned int i=0;i<trigChannel.size();i++){ cout<<" "<<trigChannel[i]<<" ";}
00217 cout<<endl;
00218 for(unsigned int i=0;i<channelNo.size();i++){ cout<<" "<<channelNo[i]<<" ";}
00219 cout<<endl;
00220 for(unsigned int j=0;j<trigCondName.size();j++)
00221 {
00222 cout<<trigCondName[j]<<" ";
00223 for(unsigned int i=0+j*trigChannel.size();i<trigChannel.size()+j*trigChannel.size();i++)
00224 { cout<<trigTable[i]<<" ";}
00225 cout<<endl;
00226 }
00227 infile.close();
00228 }
00229
00230 return StatusCode::SUCCESS;
00231 }
00232
00233 StatusCode BesGlobalTrigSvc::finalize()
00234 {
00235 MsgStream log( msgSvc(), name() );
00236 log << MSG::INFO << "Service finalized successfully" << endreq;
00237 return StatusCode::SUCCESS;
00238 }
00239
00240
00241
00242
00243 StatusCode BesGlobalTrigSvc::queryInterface( const InterfaceID& riid,
00244 void** ppvInterface )
00245 {
00246 if ( IBesGlobalTrigSvc::interfaceID().versionMatch(riid) ) {
00247 *ppvInterface = (IBesGlobalTrigSvc*)this;
00248 }else{
00249 return Service::queryInterface(riid, ppvInterface);
00250 }
00251 addRef();
00252 return StatusCode::SUCCESS;
00253 }
00254
00255 void BesGlobalTrigSvc::handle(const Incident& inc) {
00256 MsgStream log( msgSvc(), name() );
00257 log << MSG::DEBUG << "handle: " << inc.type() << endreq;
00258 if ( inc.type() != "NewRun" ){
00259 return;
00260 }
00261 log << MSG::DEBUG << "Begin New Run" << endreq;
00262 if(m_RealizationSvc->UseDBFlag() == true && m_RealizationSvc->ifReadTrg() == true) {
00263
00264 std::vector<uint32_t> trgTable_DB = m_RealizationSvc->getTrgTable();
00265
00266 trigChannel.clear();
00267 trigCondName.clear();
00268 trigTable.clear();
00269 channelNo.clear();
00270
00271 for(unsigned int i = 0, mask = 1; i <16; i++,mask<<=1) {
00272 if(trgTable_DB[0] & mask) {
00273 if(i == 9) channelNo.push_back(0);
00274 else channelNo.push_back(1);
00275 }
00276 else channelNo.push_back(0);
00277 std::ostringstream osname;
00278 osname << "Channel_"<<i;
00279 std::string name = osname.str();
00280 trigChannel.push_back(name);
00281 }
00282
00283 string table_tmp[16][64];
00284 for(unsigned int i = 1; i <65; i++) {
00285 for(unsigned int j = 0; j <16; j++) {
00286 int bit2 = (trgTable_DB[i]>>2*j) & 0x3;
00287 int row = (int) (i-1)/4;
00288 int column = ((i-1)%4)*16 + j;
00289 if(bit2 == 0) table_tmp[row][column] = "1";
00290 else if(bit2 == 1) table_tmp[row][column] = "0";
00291 else table_tmp[row][column] = "-";
00292 }
00293 }
00294
00295 for(int j = 0; j < 48; j++) {
00296 trigCondName.push_back(COND_NAME[j]);
00297 for(int i = 0; i < 16; i++) {
00298 trigTable.push_back(table_tmp[i][j]);
00299 }
00300 }
00301
00302 for(unsigned int i=0;i<trigChannel.size();i++){ cout<<" "<<trigChannel[i]<<" ";}
00303 cout<<endl;
00304 for(unsigned int i=0;i<channelNo.size();i++){ cout<<" "<<channelNo[i]<<" ";}
00305 cout<<endl;
00306 for(unsigned int j=0;j<trigCondName.size();j++)
00307 {
00308 cout<<setw(15)<<trigCondName[j]<<" ";
00309 for(unsigned int i=0+j*trigChannel.size();i<trigChannel.size()+j*trigChannel.size();i++)
00310 { cout<<trigTable[i]<<" ";}
00311 cout<<endl;
00312 }
00313 }
00314
00315 }
00316
00317 int BesGlobalTrigSvc::getL1ETOT_L() {
00318 L1ETOT_L = m_RealizationSvc->getVthEtotL();
00319 return L1ETOT_L;
00320 }
00321
00322 int BesGlobalTrigSvc::getL1ETOT_M() {
00323 L1ETOT_M = m_RealizationSvc->getVthEtotM();
00324 return L1ETOT_M;
00325 }
00326
00327 int BesGlobalTrigSvc::getL1ETOT_BR() {
00328 L1ETOT_BR = m_RealizationSvc->getVthBEtotH();
00329 return L1ETOT_BR;
00330 }
00331
00332 int BesGlobalTrigSvc::getL1ETOT_EC() {
00333 L1ETOT_EC = m_RealizationSvc->getVthEEtotH();
00334 return L1ETOT_EC;
00335 }
00336
00337 int BesGlobalTrigSvc::getL1EBL_BR() {
00338 L1EBL_BR = m_RealizationSvc->getVthBalBLK();
00339 return L1EBL_BR;
00340 }
00341
00342 int BesGlobalTrigSvc::getL1EBL_EC() {
00343 L1EBL_EC = m_RealizationSvc->getVthBalEEMC();
00344 return L1EBL_EC;
00345 }
00346
00347 int BesGlobalTrigSvc::getL1EDIFF_BR() {
00348 L1EDIFF_BR = m_RealizationSvc->getVthDiffB();
00349 return L1EDIFF_BR;
00350 }
00351
00352 int BesGlobalTrigSvc::getL1EDIFF_EC() {
00353 L1EDIFF_EC = m_RealizationSvc->getVthDiffE();
00354 return L1EDIFF_EC;
00355 }
00356
00357 int BesGlobalTrigSvc::getL1BLK_GATE() {
00358 L1BLK_GATE = m_RealizationSvc->getVthBalBLK();
00359 return L1BLK_GATE;
00360 }
00361
00362 int BesGlobalTrigSvc::getL1EBL_Z() {
00363 L1EBL_Z = m_RealizationSvc->getVthBLZ();
00364 return L1EBL_Z;
00365 }
00366
00367 void BesGlobalTrigSvc::startMdcTrig() {
00368 if(m_MdcTrig) m_MdcTrig->startMdcTrig();
00369 }
00370
00371 void BesGlobalTrigSvc::startTofTrig() {
00372 if(m_TofTrig) m_TofTrig->startTofTrig();
00373 }
00374
00375 void BesGlobalTrigSvc::startEmcTrig() {
00376 if(m_EmcTrig) m_EmcTrig->startEmcTrig();
00377 }
00378
00379 void BesGlobalTrigSvc::startTMTrig() {
00380 if(m_TMTrig) m_TMTrig->startTMTrig();
00381 }
00382
00383 StatusCode BesGlobalTrigSvc::GlobalTrig()
00384 {
00385
00386 ifpass = false;
00387 trigOut.clear();
00388
00389 for(int i = 0; i < 16; i++) {
00390 if(i < 16) m_trigChannel[i] = 0;
00391 }
00392
00393 MsgStream log(msgSvc(), name());
00394
00395
00396 muc_vlayerSeg = m_MucTrig->getNlayerSeg();
00397
00398 muc_nlayerEE = m_MucTrig->getNlayerPart(0);
00399 muc_nlayerBR = m_MucTrig->getNlayerPart(1);
00400 muc_nlayerWE = m_MucTrig->getNlayerPart(2);
00401 muc_vhitLayer = m_MucTrig->getNhitLayer();
00402 muc_vhitSeg = m_MucTrig->getNhitSeg();
00403 muc_nhitEE = m_MucTrig->getNhitPart(0);
00404 muc_nhitBR = m_MucTrig->getNhitPart(1);
00405 muc_nhitWE = m_MucTrig->getNhitPart(2);
00406 muc_nhitTotal = m_MucTrig->getNhitTotal();
00407
00408
00409 StatusCode status;
00410
00411
00412
00413 for(unsigned int i = 0; i < 48; i++) {
00414 for(unsigned int j = 0; j < trigChannel.size(); j++) {
00415 if(trigCond[i]) trigOut.push_back(1);
00416 else trigOut.push_back(0);
00417 }
00418 }
00419
00420
00421
00422 for(unsigned int i=0; i<channelNo.size(); i++)
00423 {
00424 if(channelNo.size() > 16) {
00425 log << MSG::FATAL <<"Trigger Channel is greater than 16!!!!!" << endreq;
00426 return StatusCode::FAILURE;
00427 }
00428 int condition = 0;
00429 int conditionNo = 0;
00430 if(channelNo[i]==0)
00431 {
00432 m_trigChannel[i] = 0;
00433 continue;
00434 }
00435 else
00436 {
00437 log<<MSG::INFO<<trigChannel[i]<<": ";
00438 for(unsigned int j=0; j<trigCondName.size(); j++)
00439 {
00440 if(trigTable[i+j*channelNo.size()]=="1"&&trigOut[i+j*channelNo.size()]==1) condition++;
00441 if(trigTable[i+j*channelNo.size()]=="0"&&trigOut[i+j*channelNo.size()]==0) condition++;
00442 if(trigTable[i+j*channelNo.size()]=="1") log<<MSG::INFO<<trigCondName[j]<<" ";
00443 if(trigTable[i+j*channelNo.size()]=="0") log<<MSG::INFO<<"anti_"<<trigCondName[j]<<" ";
00444 }
00445 for(unsigned int k=0; k<trigCondName.size(); k++)
00446 {
00447 if(trigTable[i+k*channelNo.size()]=="1"||trigTable[i+k*channelNo.size()]=="0") {
00448 log<<MSG::INFO<<trigOut[i+k*channelNo.size()]<<" ";
00449 conditionNo++; }
00450 }
00451 log<<MSG::INFO<<endreq;
00452 if((condition == conditionNo) && conditionNo!=0) {
00453 ifpass = true;
00454 m_trigChannel[i] = 1;
00455 }
00456 else {
00457 m_trigChannel[i] = 0;
00458 }
00459 }
00460 }
00461
00462 return status;
00463 }
00464
00465 StatusCode BesGlobalTrigSvc::setTrigCondition()
00466 {
00467 for(int i = 0; i < 48; i++) {
00468 trigCond[i] = false;
00469 m_trigCondition[i] = 0;
00470 }
00471
00472 trigCond[0] = emc_NClus1;
00473 trigCond[1] = emc_NClus2;
00474 trigCond[2] = emc_BClusBB;
00475 trigCond[3] = emc_EClusBB;
00476 trigCond[4] = emc_Clus_Z;
00477 trigCond[5] = emc_BClus_PHI;
00478 trigCond[6] = emc_EClus_PHI;
00479 trigCond[7] = emc_BEtot_H;
00480 trigCond[8] = emc_EEtot_H;
00481 trigCond[9] = emc_Etot_L;
00482 trigCond[10] = emc_Etot_M;
00483 trigCond[11] = emc_BL_Z;
00484 trigCond[12] = emc_NBClus1;
00485 trigCond[13] = emc_NEClus1;
00486 trigCond[14] = emc_BL_BBLK;
00487 trigCond[15] = emc_BL_EBLK;
00488 trigCond[16] = tof_ETofBB;
00489 trigCond[17] = tof_BTofBB;
00490 trigCond[18] = tof_NETof2;
00491 trigCond[19] = tof_NETof1;
00492 trigCond[20] = tof_NBTof2;
00493 trigCond[21] = tof_NBTof1;
00494 trigCond[22] = tof_NTof1;
00495 trigCond[23] = tm_CTrk_BB;
00496 trigCond[24] = tm_NCTrk2;
00497 trigCond[25] = tm_NCTrk1;
00498 trigCond[26] = tm_BTrk_BB;
00499 trigCond[27] = tm_NBTrk2;
00500 trigCond[28] = tm_NBTrk1;
00501 trigCond[29] = tm_ATrk_BB;
00502 trigCond[30] = tm_NATrk2;
00503 trigCond[31] = tm_NATrk1;
00504 trigCond[32] = muc_BB3478;
00505 trigCond[33] = muc_BB37;
00506 trigCond[34] = false;
00507 trigCond[35] = false;
00508 trigCond[36] = false;
00509 trigCond[37] = false;
00510 trigCond[38] = mdc_STrk_BB;
00511 trigCond[39] = mdc_NStrkN;
00512 trigCond[40] = mdc_NStrk2;
00513 trigCond[41] = mdc_NStrk1;
00514 trigCond[42] = mdc_LTrk_BB;
00515 trigCond[43] = mdc_NLtrkN;
00516 trigCond[44] = mdc_NLtrk2;
00517 trigCond[45] = mdc_NLtrk1;
00518 trigCond[46] = mdc_NItrk2;
00519 trigCond[47] = mdc_NItrk1;
00520
00521 MsgStream log(msgSvc(), name());
00522 if(trigCondName.size() != 48) {
00523 log << MSG::FATAL << "The trigger condition CAN NOT be successfully read!!!" << endreq;
00524 return StatusCode::FAILURE;
00525 }
00526
00527 for(unsigned int i = 0; i < 48; i++) {
00528 if(trigCond[i]) m_trigCondition[i] = 1;
00529 }
00530
00531 return StatusCode::SUCCESS;
00532 }