2#include "GaudiKernel/MsgStream.h"
3#include "GaudiKernel/INTuple.h"
4#include "GaudiKernel/INTupleSvc.h"
17#include "CLHEP/Random/Random.h"
18#include "CLHEP/Random/RandFlat.h"
19#include "CLHEP/Random/RandExponential.h"
37MixerAlg::MixerAlg(
const std::string& name, ISvcLocator* pSvcLocator) : Algorithm(name, pSvcLocator){
38 declareProperty(
"MixMdcDigi", b_mdc=
true);
39 declareProperty(
"MixEmcDigi", b_emc=
true);
40 declareProperty(
"MixMucDigi", b_muc=
true);
41 declareProperty(
"MixTofDigi", b_tof=
true);
43 declareProperty(
"DBUserRequest", m_dbUserRequest=
false);
44 declareProperty(
"RandomTrgRun", m_run);
45 declareProperty(
"RandomTrgRunRange", m_runs);
46 declareProperty(
"RandomTrgTimeRange", m_dates);
48 declareProperty(
"BackgroundDataFiles", m_bgfiles);
49 declareProperty(
"NumRanTrgEvents", m_ranTrgEvents);
50 declareProperty(
"NBgEventsToSignal", m_nevent=1);
52 declareProperty(
"ReplaceDataPath", m_pattern);
53 declareProperty(
"UseNewDataDir", m_newdatadir);
54 declareProperty(
"IfSkip", m_skip=
true);
55 declareProperty(
"NSkip", m_NSkip=150);
56 declareProperty(
"OutPut", m_ifOutPut=
false);
57 declareProperty(
"MixingMethod", m_mixingMethod=1);
58 declareProperty(
"MaxLoop", m_maxLoop=10);
59 declareProperty(
"SmearT0", m_ifSmearT0=
true);
60 declareProperty(
"ReadBGMethod", m_readBGMethod=1);
62 declareProperty(
"ExWireFromRun", m_exRunFrom = 0 );
63 declareProperty(
"ExWireToRun", m_exRunTo = 999999);
65 declareProperty(
"UsingFilter", m_usingFilter =
true);
74 m_totEvtNumInCurFile = 0;
76 m_ranStepLenInCurrentFile.clear();
86 std::string query =
"SELECT FilePath,FileName,NumEvent FROM RanTrgData";
88 if (! m_run.empty() || m_runs.size()==2 || m_dates.size()==2)
90 query = query +
" WHERE ";
94 query = query +
" RunNo=" + m_run;
97 if( m_runs.size()==2 )
99 if(use_and) query = query +
" AND ";
100 query = query +
" RunNo>=" + m_runs[0] +
" AND RunNo<=" + m_runs[1];
103 if( m_dates.size()==2 )
105 if(use_and) query = query +
" AND ";
106 query = query +
" TimeSOR>='" + m_dates[0] +
"' AND TimeEOR<='" + m_dates[1] +
"'";
117 log =
new MsgStream(messageService(), name() );
120 StatusCode status = service(
"RealizationSvc",tmpReal);
121 if (!status.isSuccess())
123 (*log) << MSG::FATAL <<
" Could not initialize Realization Service" << endreq;
128 if(m_RealizationSvc->
UseDBFlag() ==
true && m_RealizationSvc->
ifReadRandTrg() ==
true && m_dbUserRequest ==
true) {
133 if(! m_pattern.empty())
135 for(
unsigned int k = 0; k < m_bgfiles.size(); k++) {
136 size_t pos_round = m_bgfiles[k].rfind(
"round");
137 (*log) << MSG::INFO <<
"m_bgfiles: "<<m_bgfiles[k]<<endreq;
138 if(pos_round!=string::npos){
139 m_bgfiles[k].replace(m_bgfiles[k].begin(), m_bgfiles[k].begin()+pos_round, m_pattern);
140 (*log) << MSG::INFO<<
"new random trigger data path: "<<m_bgfiles[k]<<endreq;
143 (*log) << MSG::ERROR<<
"string 'round' not found in random trigger path!"<<endreq;
148 if (! m_newdatadir.empty())
150 for(
unsigned int k = 0; k < m_bgfiles.size(); k++) {
152 std::strcpy (tmp, m_bgfiles[k].c_str());
153 string fname = basename(tmp);
154 m_bgfiles[k].replace(m_bgfiles[k].begin(), m_bgfiles[k].end(), m_newdatadir+
'/'+fname);
155 (*log) << MSG::INFO<<
"new random trigger data path: "<<m_bgfiles[k]<<endreq;
161 m_mdcCnv->
init(m_exRunFrom,m_exRunTo);
164 static const bool CREATEIFNOTTHERE(
true);
165 StatusCode RndmStatus = service(
"BesRndmGenSvc", p_BesRndmGenSvc, CREATEIFNOTTHERE);
166 if (!RndmStatus.isSuccess() || 0 == p_BesRndmGenSvc)
168 (*log) << MSG::ERROR <<
" Could not initialize Random Number Service" << endreq;
174 status = service(
"DataInfoSvc",tmpInfoSvc);
175 if (status.isSuccess()) {
176 (*log) << MSG::INFO <<
"get the DataInfoSvc" << endreq;
177 m_jobInfoSvc=
dynamic_cast<DataInfoSvc *
>(tmpInfoSvc);
179 (*log) << MSG::WARNING <<
"could not get the DataInfoSvc." << endreq;
183 if(m_RealizationSvc->
UseDBFlag() ==
false || m_dbUserRequest ==
true) {
184 if(m_bgfiles.empty()) {
185 (*log) << MSG::WARNING <<
"No background datafiles found" << endreq;
186 return StatusCode::SUCCESS;
191 m_bgfilesIndex.clear();
192 for(
unsigned int bg_index = 0; bg_index < m_bgfiles.size(); bg_index++) {
193 m_bgfilesIndex.push_back(m_bgfiles[bg_index] +
".idx");
195 if(m_skip ==
true && m_readBGMethod == 1) m_fr =
new RawFileReader(m_bgfiles, m_bgfilesIndex) ;
200 return StatusCode::FAILURE;
208 if(nt1) m_tuple1 = nt1;
210 m_tuple1 =
ntupleSvc()->book(
"FILE1/n1",CLID_ColumnWiseTuple,
"Field");
212 status = m_tuple1->addItem(
"time1", m_time1 );
213 status = m_tuple1->addItem(
"time2", m_time2 );
214 status = m_tuple1->addItem(
"time3", m_time3 );
217 (*log) << MSG::ERROR <<
" Cannot book N-tuple:" <<long(m_tuple1)<< endreq;
218 return StatusCode::FAILURE;
223 if(nt2) m_tuple2 = nt2;
225 m_tuple2 =
ntupleSvc()->book(
"FILE1/n2",CLID_ColumnWiseTuple,
"Field");
227 status = m_tuple2->addItem(
"tdc", m_tdc );
230 (*log) << MSG::ERROR <<
" Cannot book N-tuple:" <<long(m_tuple2)<< endreq;
231 return StatusCode::FAILURE;
236 if(nt3) m_tuple3 = nt3;
238 m_tuple3 =
ntupleSvc()->book(
"FILE1/n3",CLID_ColumnWiseTuple,
"Field");
240 status = m_tuple3->addItem(
"time4", m_time4 );
241 status = m_tuple3->addItem(
"time5", m_time5 );
244 (*log) << MSG::ERROR <<
" Cannot book N-tuple:" <<long(m_tuple3)<< endreq;
245 return StatusCode::FAILURE;
249 status = service(
"BesTimerSvc", m_timersvc);
250 if (status.isFailure()) {
251 (*log) << MSG::ERROR << name() <<
": Unable to locate BesTimer Service" << endreq;
252 return StatusCode::FAILURE;
254 m_timer = m_timersvc->
addItem(
"Read field Time");
255 m_timer1 = m_timersvc->
addItem(
"Read raw files");
259 CLHEP::HepRandomEngine* engine = p_BesRndmGenSvc->
GetEngine(
"MIX");
260 HepRandom::setTheEngine(engine);
261 HepRandom::showEngineStatus();
263 return StatusCode::SUCCESS;
274 SmartDataPtr<Event::EventHeader> evt(eventSvc(),
"/Event/EventHeader");
276 return StatusCode::FAILURE;
279 if(m_RealizationSvc->
UseDBFlag() ==
true && m_RealizationSvc->
ifReadRandTrg() ==
true && m_dbUserRequest ==
false) {
280 int runNo = evt -> runNumber();
285 std::vector<std::string> bgfiles = m_RealizationSvc->
getBgFileName();
287 if(! m_pattern.empty())
289 for(
unsigned int k = 0; k < bgfiles.size(); k++) {
290 size_t pos_round = bgfiles[k].rfind(
"round");
291 (*log) << MSG::INFO<<
"bgfiles: "<<bgfiles[k]<<endreq;
292 if(pos_round!=string::npos){
293 bgfiles[k].replace(bgfiles[k].begin(), bgfiles[k].begin()+pos_round, m_pattern);
294 (*log) << MSG::INFO<<
"new random trigger data path: "<<bgfiles[k]<<endreq;
297 (*log) << MSG::ERROR<<
"string 'round' not found in random trigger path!"<<endreq;
302 if (! m_newdatadir.empty())
304 for(
unsigned int k = 0; k < bgfiles.size(); k++) {
306 std::strcpy (tmp, bgfiles[k].c_str());
307 string fname = basename(tmp);
308 bgfiles[k].replace(bgfiles[k].begin(), bgfiles[k].end(), m_newdatadir+
'/'+fname);
309 (*log) << MSG::INFO<<
"new random trigger data path: "<<bgfiles[k]<<endreq;
314 std::vector<std::string> bgfilesIndex;
315 bgfilesIndex.clear();
316 for(
unsigned int bg_index = 0; bg_index < bgfiles.size(); bg_index++) {
317 bgfilesIndex.push_back(bgfiles[bg_index] +
".idx");
321 if(m_fr)
delete m_fr;
323 std::vector<int> ranTrgEvents = m_fr->
getEventNumber(bgfilesIndex);
327 m_bgfilesIndex.clear();
328 m_ranTrgEvents.clear();
329 for(
unsigned int bg_index = 0; bg_index < bgfiles.size(); bg_index++) {
330 if(ranTrgEvents[bg_index] > 0) {
331 m_bgfiles.push_back(bgfiles[bg_index]);
332 m_bgfilesIndex.push_back(bgfilesIndex[bg_index]);
333 m_ranTrgEvents.push_back(ranTrgEvents[bg_index]);
338 if(m_fr)
delete m_fr;
343 bgfilesIndex.clear();
344 ranTrgEvents.clear();
347 if(m_bgfiles.empty() || m_ranTrgEvents.empty()) {
348 (*log) << MSG::WARNING <<
"No background datafiles found!!!" << endreq;
349 return StatusCode::SUCCESS;
353 if(m_mixingMethod == 1) {
355 m_ranStepLenInCurrentFile.clear();
361 bool ifsucc =
file_sort(m_bgfiles,m_ranTrgEvents);
362 if( !ifsucc )
return StatusCode::FAILURE;
365 m_bgfilesIndex.clear();
366 for(
unsigned int bg_index = 0; bg_index < m_bgfiles.size(); bg_index++) {
367 m_bgfilesIndex.push_back(m_bgfiles[bg_index] +
".idx");
371 m_vRanEvtNumInSubSet.clear();
372 m_vStreamNumInSubSet.clear();
375 int ranEvtNumInSubSet = 0;
377 for(
unsigned int i = 0; i < m_ranTrgEvents.size(); i++) {
378 if(i == 0) set_no = m_numSets[i];
379 if((i != 0) && (set_no != m_numSets[i])) {
380 m_vRanEvtNumInSubSet.push_back(ranEvtNumInSubSet);
381 m_vStreamNumInSubSet.push_back(nstream);
382 ranEvtNumInSubSet = 0;
384 set_no = m_numSets[i];
387 m_totRanEvtNum += m_ranTrgEvents[i];
388 ranEvtNumInSubSet += m_ranTrgEvents[i];
390 if(i == m_ranTrgEvents.size() - 1) {
391 m_vRanEvtNumInSubSet.push_back(ranEvtNumInSubSet);
392 m_vStreamNumInSubSet.push_back(nstream);
397 int evtNumInRun = -1;
398 std::vector<int> vtotEvtNo = m_jobInfoSvc->
getTotEvtNo();
399 for(
unsigned int ii = 0; ii < vtotEvtNo.size(); ii+=2) {
400 if(std::abs(
runNo) == std::abs(vtotEvtNo[ii]))
401 evtNumInRun = vtotEvtNo[ii+1];
408 std::cout <<
"ERROR: In MixerAlg::execute() ---> The tau value or total run time is 0, please check it. Exit! " << std::endl;
412 bool using_exp =
true;
413 if(totalTime*100 < tau) using_exp =
false;
414 m_vStepLength.clear();
417 if(using_exp ==
true) ranNum = RandExponential::shoot(tau);
418 else ranNum = RandFlat::shoot(0., totalTime);
419 if(ranNum > totalTime)
continue;
420 ranNum = ranNum*m_totRanEvtNum/totalTime;
421 m_vStepLength.push_back((
int)ranNum);
422 if(m_vStepLength.size() == evtNumInRun)
break;
425 sort(m_vStepLength.begin(), m_vStepLength.end());
430 if(evtNumInRun <= 0 || m_totRanEvtNum <= 0) {
431 (*log) << MSG::ERROR <<
"The event number (or random trigger event number) in run " << evt->runNumber() <<
" is zero" << endreq;
432 return StatusCode::FAILURE;
439 map_stepLength.clear();
440 for(
unsigned int i = 0; i < m_ranTrgEvents.size(); i++) {
441 std::vector<int> vstepLength;
442 typedef pair<int, std::vector<int> > vpair;
443 map_stepLength.insert(vpair(i,vstepLength));
446 for(
unsigned int i = 0; i < m_ranTrgEvents.size(); ) {
448 int pre_ranEvtNumSubSet = 0;
449 int cur_ranEvtNumSubSet = 0;
450 set_no = m_numSets[i];
451 for(
int j = 0; j < set_no; j++) {
452 if(j != (set_no - 1)) pre_ranEvtNumSubSet += m_vRanEvtNumInSubSet[j];
453 cur_ranEvtNumSubSet += m_vRanEvtNumInSubSet[j];
456 for(
unsigned j = 0; j < m_vStepLength.size(); j++) {
458 if((m_vStepLength[j] >= pre_ranEvtNumSubSet) && (m_vStepLength[j] < cur_ranEvtNumSubSet)) {
459 int sub_stepLength = int((m_vStepLength[j]-pre_ranEvtNumSubSet)/m_vStreamNumInSubSet[set_no - 1]);
461 int begin_fileId = -1, end_fileId = -1;
462 for(std::map<
int,std::vector<int> >::iterator
iter = map_stepLength.begin();
iter != map_stepLength.end();
iter++) {
464 if(set_no == m_numSets[
iter->first]) {
465 if(begin_fileId == -1) begin_fileId =
iter->first;
469 end_fileId = begin_fileId + file_id;
470 bool add_succ =
false;
474 int random_file = int(RandFlat::shootInt(
long(begin_fileId),
long(end_fileId)));
475 if(sub_stepLength < m_ranTrgEvents[random_file]) {
476 map_stepLength[random_file].push_back(sub_stepLength);
483 (*log) << MSG::ALWAYS <<
"Loop time is larger than MAX_LOOP_TIMES(" <<
MAX_LOOP_TIMES <<
") in MixAlg, when assigning step length for each bg file." << endreq;
489 i += m_vStreamNumInSubSet[set_no - 1];
493 unsigned int ranSelectedNum = 0;
494 for(std::map<
int,std::vector<int> >::iterator
iter = map_stepLength.begin();
iter != map_stepLength.end();
iter++) {
495 ranSelectedNum += (
iter->second).size();
498 if(ranSelectedNum != m_vStepLength.size()) {
499 (*log) << MSG::ERROR <<
"In MixerAlg::excute()--> selected bg events number not equal to MC events" << endreq;
500 return StatusCode::FAILURE;
505 if(m_mixingMethod == 2) {
507 if (m_fr)
delete m_fr;
510 m_bgfilesIndex.clear();
511 for(
unsigned int bg_index = 0; bg_index < m_bgfiles.size(); bg_index++) {
512 m_bgfilesIndex.push_back(m_bgfiles[bg_index] +
".idx");
514 if(m_skip ==
true && m_readBGMethod == 1) m_fr =
new RawFileReader(m_bgfiles, m_bgfilesIndex) ;
519 return StatusCode::FAILURE;
523 m_raw_event->
reset();
532 SmartDataPtr<MdcDigiCol> mdcMcDigits(eventSvc(),
"/Event/Digi/MdcDigiCol");
534 (*log) << MSG::ERROR <<
"Unable to retrieve MdcDigiCol" << endreq;
536 (*log) << MSG::INFO <<
"MdcDigiCol retrieved of size "<< mdcMcDigits->size() << endreq;
538 SmartDataPtr<EmcDigiCol> emcMcDigits(eventSvc(),
"/Event/Digi/EmcDigiCol");
540 (*log) << MSG::ERROR <<
"Unable to retrieve EmcDigiCol" << endreq;
542 (*log) << MSG::INFO <<
"EmcDigiCol retrieved of size "<< emcMcDigits->size() << endreq;
544 SmartDataPtr<MucDigiCol> mucMcDigits(eventSvc(),
"/Event/Digi/MucDigiCol");
546 (*log) << MSG::ERROR <<
"Unable to retrieve MucDigiCol" << endreq;
548 (*log) << MSG::INFO <<
"MucDigiCol retrieved of size "<< mucMcDigits->size() << endreq;
550 SmartDataPtr<TofDigiCol> tofMcDigits(eventSvc(),
"/Event/Digi/TofDigiCol");
552 (*log) << MSG::ERROR <<
"Unable to retrieve TofDigiCol" << endreq;
554 (*log) << MSG::INFO <<
"TofDigiCol retrieved of size "<< tofMcDigits->size() << endreq;
556 for(
int ievent = 0; ievent<m_nevent; ievent++)
558 (*log) << MSG::INFO <<
"Mixing BG Event " << ievent << endreq;
566 if(m_mixingMethod == 1) {
567 if(m_RealizationSvc->
UseDBFlag() ==
true && m_dbUserRequest ==
false) {
568 if(m_skipCount >= m_ranStepLenInCurrentFile.size()) {
569 m_ranStepLenInCurrentFile.clear();
570 for(std::map<
int,std::vector<int> >::iterator
iter = map_stepLength.begin();
iter != map_stepLength.end();
iter++) {
571 if(currentBGFile ==
"") {
572 if((
iter->second).size() == 0)
continue;
573 if(m_fr)
delete m_fr;
575 if(m_readBGMethod == 1) m_fr =
new RawFileReader(m_bgfiles[
iter->first], m_bgfiles[
iter->first]+
".idx") ;
577 m_totEvtNumInCurFile = m_ranTrgEvents[
iter->first];
582 m_ranStepLenInCurrentFile =
iter->second;
587 if(currentBGFile == m_bgfiles[
iter->first]) {
589 if(
iter == map_stepLength.end())
return StatusCode::FAILURE;
590 if((
iter->second).size() == 0) {
593 if(
iter == map_stepLength.end())
return StatusCode::FAILURE;
594 if((
iter->second).size() > 0)
break;
597 if(m_fr)
delete m_fr;
599 if(m_readBGMethod == 1) m_fr =
new RawFileReader(m_bgfiles[
iter->first], m_bgfiles[
iter->first]+
".idx") ;
601 m_totEvtNumInCurFile = m_ranTrgEvents[
iter->first];
606 m_ranStepLenInCurrentFile =
iter->second;
615 if(m_skipCount == 0) nskip = m_ranStepLenInCurrentFile[m_skipCount];
616 else nskip = m_ranStepLenInCurrentFile[m_skipCount] - m_ranStepLenInCurrentFile[m_skipCount - 1];
618 m_nEventsToEnd = (m_totEvtNumInCurFile - 1) - m_ranStepLenInCurrentFile[m_skipCount];
620 if(m_skipCount == 0 && nskip == 0) nskip = 1;
624 if(m_RealizationSvc->
UseDBFlag() ==
false || m_dbUserRequest ==
true) nskip =
int (2*m_NSkip*(RandFlat::shoot())) + 1;
625 if(m_totalEvent == 0 && nskip == 0) nskip = 1;
627 if(m_mixingMethod == 2) {
628 nskip = int (2*m_NSkip*(RandFlat::shoot())) + 1;
637 if(m_readBGMethod == 0) {
639 for(
int j = 0; j < nskip; j++) {
643 (*log) << MSG::ERROR <<
"Cannot get next background event" << endreq;
644 return StatusCode::FAILURE;
648 if(m_readBGMethod == 1) {
650 next =
nextEvent(nskip,0,m_nEventsToEnd);
653 (*log) << MSG::ERROR <<
"Cannot get next background event" << endreq;
654 return StatusCode::FAILURE;
657 if(m_readBGMethod == 2) {
662 (*log) << MSG::ERROR <<
"Cannot get next background event" << endreq;
663 return StatusCode::FAILURE;
671 if(m_mixingMethod == 1) {
672 if ( !next && m_totalEvent == 0) {
673 (*log) << MSG::ERROR <<
"Cannot get next background event" << endreq;
674 return StatusCode::FAILURE;
678 if(m_mixingMethod == 2) {
680 (*log) << MSG::ERROR <<
"Cannot get next background event" << endreq;
681 return StatusCode::FAILURE;
685 mixDigi(mdcMcDigits, emcMcDigits, mucMcDigits, tofMcDigits);
697 return StatusCode::SUCCESS;
701 if( m_raw_event )
delete m_raw_event;
702 if( log )
delete log;
703 if( m_fr)
delete m_fr;
704 return StatusCode::SUCCESS;
711 m_raw_event->
reset();
718 const uint32_t* fragment;
719 if(m_skip ==
true && m_readBGMethod == 0) fragment = m_fr->
nextEvent();
720 if(m_skip ==
true && m_readBGMethod == 1) {
722 else fragment = m_fr->
nextEvent(nskip - 1);
724 if(m_skip ==
true && m_readBGMethod == 2) {
728 if(m_skip ==
false) fragment = m_fr->
nextEvent();
742 std::cerr <<
"Found invalid event (traceback):" << std::endl;
747 (*log) << MSG::DEBUG <<
"[Event No. #" << f.
global_id()
755 const uint32_t* ef=NULL;
758 (*log) << MSG::ERROR <<
"Event Filter Data Failed!!!" << endreq;
762 (*log) << MSG::DEBUG<<
"Event Filter Information*********" <<std::hex<<endreq
763 <<*ef<<
" "<<*(ef+1)<<
" "<<*(ef+2)<<
" "<<*(ef+3)<<std::dec<<endreq;
764 m_raw_event->
addReHltRaw((uint32_t*)ef, (uint32_t)4);
770 for (
int robi = 0; robi < nrobs; robi++) {
773 uint32_t* dataptr = NULL;
778 source_id_number <<= 8;
779 source_id_number >>= 24;
782 switch(source_id_number) {
819 if(m_usingFilter ==
true) {
821 if(m_skip ==
true && m_readBGMethod == 0) {
822 return nextEvent(1, evtbyte, eventsToEnd);
824 if(m_skip ==
true && m_readBGMethod == 1) {
825 if(m_RealizationSvc->
UseDBFlag() ==
false || m_dbUserRequest ==
true)
return nextEvent(1, evtbyte, eventsToEnd);
826 if(eventsToEnd > 0 && m_RealizationSvc->
UseDBFlag() ==
true && m_dbUserRequest ==
false ) {
828 return nextEvent(1, evtbyte, eventsToEnd);
831 if(m_skip ==
true && m_readBGMethod == 2) {
832 return nextEvent(1, evtbyte, eventsToEnd);
834 if(m_skip ==
false)
return nextEvent(nskip, evtbyte, eventsToEnd);
845 if(m_skip ==
true && m_readBGMethod == 1) m_fr =
new RawFileReader(m_bgfiles, m_bgfilesIndex) ;
853 return nextEvent(nskip, evtbyte, eventsToEnd);
859 std::cerr << std::endl <<
"Uncaught eformat issue: " << ex.what() << std::endl;
862 std::cerr << std::endl <<
"Uncaught ERS issue: " << ex.what() << std::endl;
864 catch (std::exception& ex) {
865 std::cerr << std::endl <<
"Uncaught std exception: " << ex.what() << std::endl;
868 std::cerr << std::endl <<
"Uncaught unknown exception" << std::endl;
874template <
class T1,
class T2>
877 vector<T2*> newDigiCol;
878 typename T1::iterator mc;
879 typename T1::const_iterator
bg;
881 for(
bg = bgDigits.begin();
bg != bgDigits.end();
bg++ )
884 for(mc = mcDigits->begin(); mc != mcDigits->end(); mc++ )
886 if((*mc)->identify()==(*bg)->identify())
890 cout <<
"****************************************"<<endl;
891 cout <<
"MC id " << (*mc)->identify().get_value()
892 <<
" BG Id " << (*bg)->identify().get_value() << endl;
893 cout<<
"==> MC Digi : ";
894 (*mc)->fillStream(cout);
895 cout<<
"==> BG Digi : ";
896 (*bg)->fillStream(cout);
899 (*mc)->setTrackIndex((*mc)->getTrackIndex() - 999);
905 cout<<
"==> New MC Digi: ";
906 (*mc)->fillStream(cout);
907 cout <<
"****************************************"<<endl;
914 (*bg)->setTrackIndex(-1000);
915 newDigiCol.push_back(*
bg);
919 for(
bg=newDigiCol.begin();
bg!=newDigiCol.end();
bg++ )
920 mcDigits->push_back(*
bg);
925 vector<MdcDigi*> newDigiCol;
926 MdcDigiCol::const_iterator mc;
927 MdcDigiCol::const_iterator
bg;
929 for(
bg = bgDigits.begin();
bg != bgDigits.end();
bg++ )
931 if((*bg)->getChargeChannel() < 0x7FFFFFFF) (*bg)->setChargeChannel(0);
933 for(mc = mcDigits->begin(); mc != mcDigits->end(); mc++ )
935 if((*mc)->identify()==(*bg)->identify())
939 cout <<
"****************************************"<<endl;
940 cout <<
"MC id " << (*mc)->identify().get_value()
941 <<
" BG Id " << (*bg)->identify().get_value() << endl;
942 cout<<
"==> MC Digi : ";
943 (*mc)->fillStream(cout);
944 cout<<
"==> BG Digi : ";
945 (*bg)->fillStream(cout);
948 (*mc)->setTrackIndex((*mc)->getTrackIndex() - 999);
954 cout<<
"==> New MC Digi: ";
955 (*mc)->fillStream(cout);
956 cout <<
"****************************************"<<endl;
963 (*bg)->setTrackIndex(-1000);
964 newDigiCol.push_back(*
bg);
968 for(
bg=newDigiCol.begin();
bg!=newDigiCol.end();
bg++ )
969 mcDigits->push_back(*
bg);
974 vector<TofDigi*> newDigiCol;
976 TofDigiCol::const_iterator bgTof = bgDigits.begin();
977 for(; bgTof!=bgDigits.end(); bgTof++ )
979 (*bgTof)->setTrackIndex(-1000);
980 newDigiCol.push_back(*bgTof);
982 for(bgTof=newDigiCol.begin(); bgTof!=newDigiCol.end(); bgTof++ ) {
983 mcDigits->push_back(*bgTof);
988 SmartDataPtr<EmcDigiCol>& emcMcDigits,
989 SmartDataPtr<MucDigiCol>& mucMcDigits,
990 SmartDataPtr<TofDigiCol>& tofMcDigits)
1000 int tdc_min = -9, tdc_max = -9, tdc_tot = 0, tdc_num = 0;
1001 for(MdcDigiCol::const_iterator
bg = mdcCol.begin();
bg != mdcCol.end();
bg++ )
1003 if((*bg)->getTimeChannel() < 0x7FFFFFFF) {
1004 tdc_tot += (*bg)->getTimeChannel();
1006 if(tdc_min < 0) tdc_min = (*bg)->getTimeChannel();
1008 if(tdc_min > (*bg)->getTimeChannel()) tdc_min = (*bg)->getTimeChannel();
1010 if(tdc_max < 0) tdc_max = (*bg)->getTimeChannel();
1012 if(tdc_max < (*bg)->getTimeChannel()) tdc_max = (*bg)->getTimeChannel();
1016 int tdc_mean = (int) ((
double)tdc_tot/(double)tdc_num);
1020 tdc_shift = tdc_mean - CLHEP::RandFlat::shootInt(
long(0),
long(80*24/0.09375));
1021 if((tdc_min - tdc_shift)>=0 && (tdc_max - tdc_shift) <=
int(80*24/0.09375))
break;
1023 if(tdc_num > m_maxLoop)
break;
1027 for(MdcDigiCol::const_iterator
bg = mdcCol.begin();
bg != mdcCol.end();
bg++ )
1029 if((*bg)->getTimeChannel() >= 0x7FFFFFFF)
continue;
1030 int newTDC = (*bg)->getTimeChannel() - tdc_shift;
1031 if(newTDC < 0 || newTDC >
int(80*24/0.09375)) newTDC = int(CLHEP::RandFlat::shoot()*80*24/0.09375);
1032 (*bg)->setTimeChannel(newTDC);
1045 combineDigits<EmcDigiCol, EmcDigi>(emcMcDigits, emcCol, log->level());
1051 combineDigits<MucDigiCol, MucDigi>(mucMcDigits, mucCol, log->level());
1065 m_mdcCnv->
convert(mdcBuf, digiCol);
1071 m_mucCnv->
convert(mucBuf, digiCol);
1077 m_emcCnv->
convert(emcBuf, digiCol);
1083 m_tofCnv->
convert(tofBuf, digiCol);
1107 uint32_t nbuf = gtdBuf.
nBuf();
1109 for (uint32_t i = 0; i < nbuf; i++) {
1110 uint32_t* buf = gtdBuf(i);
1111 uint32_t bufSize = gtdBuf.
bufSize(i);
1113 while (bufSize - index > 1) {
1114 uint32_t blockSize = ( ((*(buf+index))>>14) & 0x3FF);
1115 uint32_t
id = ((*(buf+index))>>24);
1116 if (blockSize == 0 || (index+blockSize) > bufSize)
break;
1117 if ((
id> 0xD1 &&
id < 0xD8 &&
id != 0xD5) ||
id == 0xDA || (
id > 0xE1 &&
id < 0xED)) {
1118 trigGTD =
new TrigGTD(buf+index);
1119 gtdCol->push_back(trigGTD);
1125 TrigGTDCol::iterator
iter = gtdCol->begin();
1126 for (;
iter != gtdCol->end();
iter++ ) {
1127 const uint32_t boardId = (*iter)->getId();
1128 const uint32_t timeWindow = (*iter)->getTimeWindow();
1129 const uint32_t size = (*iter)->getDataSize();
1130 const uint32_t* trigData = (*iter)->getDataPtr();
1133 if(boardId == 0xd3) {
1134 if(size%timeWindow != 0) {
1135 std::cout <<
"GTL data is NOT completed, exit." << std::endl;
1138 for(uint32_t j = 0; j < size; j++) {
1139 uint32_t dataId = ((trigData[j] >> 24) & 0x7);
1140 if(dataId != 5)
continue;
1141 for(uint32_t i = 1, loop = 0; loop < 24; i <<= 1, loop++) {
1142 if((loop == 16) && (trigData[j] & i)) timing = 1;
1143 if((loop == 17) && (trigData[j] & i) && (timing != 1)) timing = 2;
1144 if((loop == 18) && (trigData[j] & i) && (timing == 0)) timing = 3;
1154 std::vector<std::string> tmp_files = files;
1155 std::vector<int> tmp_ranEvtNums = ranEvtNums;
1160 const char* file_index[100];
1163 for(
int i = 0; i < 100; i++) {
1169 if(tmp_files.size() >= 100) {
1170 std::cout <<
"ERROR: In MixerAlg::file_sort(), please change bigger array size" << std::endl;
1174 for(
unsigned int i = 0; i < tmp_files.size(); i++) {
1176 const char* file1 = tmp_files[i].c_str();
1177 char* substr1 = strstr(file1,
"_file");
1178 int strlen1 = strlen(substr1);
1182 for(
int sub1 = 0; sub1 < strlen1; sub1++) {
1183 if(substr1[sub1] ==
'e') {
1184 cset1[0] = substr1[sub1+1];
1185 cset1[1] = substr1[sub1+2];
1186 cset1[2] = substr1[sub1+3];
1189 else if(substr1[sub1] ==
'-') {
1190 cnum1[0] = substr1[sub1+1];
1199 int set1 = atoi(cset1);
1200 int num1 = atoi(cnum1);
1201 int encode_set1 = set1*100 +
num1;
1203 for(
unsigned int j = 0; j < tmp_files.size(); j++) {
1204 if(i == j)
continue;
1205 const char* file2 = tmp_files[j].c_str();
1206 char* substr2 = strstr(file2,
"_file");
1207 int strlen2 = strlen(substr2);
1210 for(
int sub2 = 0; sub2 < strlen2; sub2++) {
1211 if(substr2[sub2] ==
'e') {
1212 cset2[0] = substr2[sub2+1];
1213 cset2[1] = substr2[sub2+2];
1214 cset2[2] = substr2[sub2+3];
1217 else if(substr2[sub2] ==
'-') {
1218 cnum2[0] = substr2[sub2+1];
1226 int set2 = atoi(cset2);
1227 int num2 = atoi(cnum2);
1228 int encode_set2 = set2*100 + num2;
1229 if(encode_set1 > encode_set2) index++;
1231 file_index[index] = tmp_files[i].c_str();
1232 num_index[index] = tmp_ranEvtNums[i];
1233 set_index[index] = set1;
1237 for(
unsigned int i = 0; i < tmp_files.size(); i++) {
1238 files.push_back(file_index[i]);
1239 ranEvtNums.push_back(num_index[i]);
1240 if(setNo != set_index[i]) {
1241 setNo = set_index[i];
1242 int numSets_size = m_numSets.size();
1243 if(numSets_size == 0) m_numSets.push_back(1);
1244 if(numSets_size != 0) m_numSets.push_back(m_numSets[numSets_size - 1] + 1);
1247 int numSets_size = m_numSets.size();
1248 m_numSets.push_back(m_numSets[numSets_size - 1]);
ObjectVector< EmcDigi > EmcDigiCol
ObjectVector< MdcDigi > MdcDigiCol
void combineMdcDigits(SmartDataPtr< MdcDigiCol > &mcDigits, MdcDigiCol &bgDigits, int verbosity)
void combineTofDigits(SmartDataPtr< TofDigiCol > &mcDigits, TofDigiCol &bgDigits, int verbosity)
void combineDigits(SmartDataPtr< T1 > &mcDigits, T1 &bgDigits, int verbosity)
void combineMdcDigits(SmartDataPtr< MdcDigiCol > &mcDigits, MdcDigiCol &bgDigits, int verbosity)
void combineTofDigits(SmartDataPtr< TofDigiCol > &mcDigits, TofDigiCol &bgDigits, int verbosity)
ObjectVector< MucDigi > MucDigiCol
ObjectVector< TofDigi > TofDigiCol
ObjectVector< TrigGTD > TrigGTDCol
float elapsed(void) const
uint32_t bufSize(int i) const
std::vector< int > getTotEvtNo()
void setEventType(const unsigned int i)
const string & getEventName() const
static EmcConverter * instance(int runMode=2)
StatusCode convert(const BufferHolder &src, EmcDigiCol *des)
virtual CLHEP::HepRandomEngine * GetEngine(const std::string &StreamName)=0
Interface to the CLHEP engine.
virtual BesTimer * addItem(const std::string &name)=0
static MdcConverter * instance(int runMode=2)
void init(int runFrom, int runTo)
StatusCode convert(const BufferHolder &src, MdcDigiCol *des)
std::string prepareDbQuery()
bool file_sort(std::vector< std::string > &files, std::vector< int > &ranEvtNums)
void mixDigi(SmartDataPtr< MdcDigiCol > &mdcMcDigits, SmartDataPtr< EmcDigiCol > &emcMcDigits, SmartDataPtr< MucDigiCol > &mucMcDigits, SmartDataPtr< TofDigiCol > &tofMcDigits)
void decodeMuc(MucDigiCol *digiCol)
void decodeEmc(EmcDigiCol *digiCol)
bool nextEvent(int nskip=0, int evtbyte=0, int eventsToEnd=0)
MixerAlg(const std::string &name, ISvcLocator *pSvcLocator)
void decodeMdc(MdcDigiCol *digiCol)
void decodeTof(TofDigiCol *digiCol)
static MucConverter * instance()
StatusCode convert(const BufferHolder &src, MucDigiCol *des)
void addReTofDigi(uint32_t *digi, uint32_t size)
void setRunNo(uint32_t run_no)
const BufferHolder & getHltBuf() const
void addReMdcDigi(uint32_t *digi, uint32_t size)
void addReTrigGTD(uint32_t *digi, uint32_t size)
const BufferHolder & getEmcBuf() const
void addReMucDigi(uint32_t *digi, uint32_t size)
const BufferHolder & getGTDBuf() const
void addMcParticle(uint32_t *buf, uint32_t size)
const BufferHolder & getMdcBuf() const
const BufferHolder & getMucBuf() const
const BufferHolder & getTofBuf() const
void setEventNo(uint32_t event_no)
void addReEmcDigi(uint32_t *digi, uint32_t size)
void addReHltRaw(uint32_t *digi, uint32_t size)
virtual void print() const
const uint32_t * roughlyNextEvent(int nIgnore, int evtByte=0)
const uint32_t * nextEvent()
const uint32_t * currentEvent() const
static std::vector< int > getEventNumber(const VFileNames_t &idxfnames)
std::string currentFile()
virtual void print() const
std::vector< std::string > getBgFileName()
static RootInterface * Instance(MsgStream log)
singleton behaviour
virtual std::string getCurrentFileName()
StatusCode convert(const BufferHolder &src, TofDigiCol *des, LumiDigiCol *des2=0)
static TofConverter * instance()