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);
66 declareProperty(
"dump", m_dump =
false);
75 m_totEvtNumInCurFile = 0;
77 m_ranStepLenInCurrentFile.clear();
87 std::string query =
"SELECT FilePath,FileName,NumEvent FROM RanTrgData";
89 if (! m_run.empty() || m_runs.size()==2 || m_dates.size()==2)
91 query = query +
" WHERE ";
95 query = query +
" RunNo=" + m_run;
98 if( m_runs.size()==2 )
100 if(use_and) query = query +
" AND ";
101 query = query +
" RunNo>=" + m_runs[0] +
" AND RunNo<=" + m_runs[1];
104 if( m_dates.size()==2 )
106 if(use_and) query = query +
" AND ";
107 query = query +
" TimeSOR>='" + m_dates[0] +
"' AND TimeEOR<='" + m_dates[1] +
"'";
118 log =
new MsgStream(messageService(), name() );
121 StatusCode status = service(
"RealizationSvc",tmpReal);
122 if (!status.isSuccess())
124 (*log) << MSG::FATAL <<
" Could not initialize Realization Service" << endreq;
129 if(m_RealizationSvc->
UseDBFlag() ==
true && m_RealizationSvc->
ifReadRandTrg() ==
true && m_dbUserRequest ==
true) {
134 if(! m_pattern.empty())
136 for(
unsigned int k = 0; k < m_bgfiles.size(); k++) {
137 size_t pos_round = m_bgfiles[k].rfind(
"round");
138 (*log) << MSG::INFO <<
"m_bgfiles: "<<m_bgfiles[k]<<endreq;
139 if(pos_round!=string::npos){
140 m_bgfiles[k].replace(m_bgfiles[k].begin(), m_bgfiles[k].begin()+pos_round, m_pattern);
141 (*log) << MSG::INFO<<
"new random trigger data path: "<<m_bgfiles[k]<<endreq;
144 (*log) << MSG::ERROR<<
"string 'round' not found in random trigger path!"<<endreq;
149 if (! m_newdatadir.empty())
151 for(
unsigned int k = 0; k < m_bgfiles.size(); k++) {
153 std::strcpy (tmp, m_bgfiles[k].c_str());
154 string fname = basename(tmp);
155 m_bgfiles[k].replace(m_bgfiles[k].begin(), m_bgfiles[k].end(), m_newdatadir+
'/'+fname);
156 (*log) << MSG::INFO<<
"new random trigger data path: "<<m_bgfiles[k]<<endreq;
162 m_mdcCnv->
init(m_exRunFrom,m_exRunTo);
165 static const bool CREATEIFNOTTHERE(
true);
166 StatusCode RndmStatus = service(
"BesRndmGenSvc", p_BesRndmGenSvc, CREATEIFNOTTHERE);
167 if (!RndmStatus.isSuccess() || 0 == p_BesRndmGenSvc)
169 (*log) << MSG::ERROR <<
" Could not initialize Random Number Service" << endreq;
175 status = service(
"DataInfoSvc",tmpInfoSvc);
176 if (status.isSuccess()) {
177 (*log) << MSG::INFO <<
"get the DataInfoSvc" << endreq;
178 m_jobInfoSvc=
dynamic_cast<DataInfoSvc *
>(tmpInfoSvc);
180 (*log) << MSG::WARNING <<
"could not get the DataInfoSvc." << endreq;
184 if(m_RealizationSvc->
UseDBFlag() ==
false || m_dbUserRequest ==
true) {
185 if(m_bgfiles.empty()) {
186 (*log) << MSG::WARNING <<
"No background datafiles found" << endreq;
187 return StatusCode::SUCCESS;
192 m_bgfilesIndex.clear();
193 for(
unsigned int bg_index = 0; bg_index < m_bgfiles.size(); bg_index++) {
194 m_bgfilesIndex.push_back(m_bgfiles[bg_index] +
".idx");
196 if(m_skip ==
true && m_readBGMethod == 1) m_fr =
new RawFileReader(m_bgfiles, m_bgfilesIndex) ;
201 return StatusCode::FAILURE;
209 if(nt1) m_tuple1 = nt1;
211 m_tuple1 =
ntupleSvc()->book(
"FILE1/n1",CLID_ColumnWiseTuple,
"Field");
213 status = m_tuple1->addItem(
"time1", m_time1 );
214 status = m_tuple1->addItem(
"time2", m_time2 );
215 status = m_tuple1->addItem(
"time3", m_time3 );
218 (*log) << MSG::ERROR <<
" Cannot book N-tuple:" <<long(m_tuple1)<< endreq;
219 return StatusCode::FAILURE;
224 if(nt2) m_tuple2 = nt2;
226 m_tuple2 =
ntupleSvc()->book(
"FILE1/n2",CLID_ColumnWiseTuple,
"Field");
228 status = m_tuple2->addItem(
"tdc", m_tdc );
231 (*log) << MSG::ERROR <<
" Cannot book N-tuple:" <<long(m_tuple2)<< endreq;
232 return StatusCode::FAILURE;
237 if(nt3) m_tuple3 = nt3;
239 m_tuple3 =
ntupleSvc()->book(
"FILE1/n3",CLID_ColumnWiseTuple,
"Field");
241 status = m_tuple3->addItem(
"time4", m_time4 );
242 status = m_tuple3->addItem(
"time5", m_time5 );
245 (*log) << MSG::ERROR <<
" Cannot book N-tuple:" <<long(m_tuple3)<< endreq;
246 return StatusCode::FAILURE;
250 status = service(
"BesTimerSvc", m_timersvc);
251 if (status.isFailure()) {
252 (*log) << MSG::ERROR << name() <<
": Unable to locate BesTimer Service" << endreq;
253 return StatusCode::FAILURE;
255 m_timer = m_timersvc->
addItem(
"Read field Time");
256 m_timer1 = m_timersvc->
addItem(
"Read raw files");
260 CLHEP::HepRandomEngine* engine = p_BesRndmGenSvc->
GetEngine(
"MIX");
261 HepRandom::setTheEngine(engine);
262 HepRandom::showEngineStatus();
264 return StatusCode::SUCCESS;
275 SmartDataPtr<Event::EventHeader> evt(eventSvc(),
"/Event/EventHeader");
277 return StatusCode::FAILURE;
280 if(m_RealizationSvc->
UseDBFlag() ==
true && m_RealizationSvc->
ifReadRandTrg() ==
true && m_dbUserRequest ==
false) {
281 int runNo = evt -> runNumber();
286 std::vector<std::string> bgfiles = m_RealizationSvc->
getBgFileName();
287 if(bgfiles.size() == 0) {
288 (*log) << MSG::ERROR <<
"No random trigger files are found in the run " << m_runNo << std::endl;
292 if(! m_pattern.empty())
294 for(
unsigned int k = 0; k < bgfiles.size(); k++) {
295 size_t pos_round = bgfiles[k].rfind(
"round");
296 (*log) << MSG::INFO<<
"bgfiles: "<<bgfiles[k]<<endreq;
297 if(pos_round!=string::npos){
298 bgfiles[k].replace(bgfiles[k].begin(), bgfiles[k].begin()+pos_round, m_pattern);
299 (*log) << MSG::INFO<<
"new random trigger data path: "<<bgfiles[k]<<endreq;
302 (*log) << MSG::ERROR<<
"string 'round' not found in random trigger path!"<<endreq;
307 if (! m_newdatadir.empty())
309 for(
unsigned int k = 0; k < bgfiles.size(); k++) {
311 std::strcpy (tmp, bgfiles[k].c_str());
312 string fname = basename(tmp);
313 bgfiles[k].replace(bgfiles[k].begin(), bgfiles[k].end(), m_newdatadir+
'/'+fname);
314 (*log) << MSG::INFO<<
"new random trigger data path: "<<bgfiles[k]<<endreq;
319 std::vector<std::string> bgfilesIndex;
320 bgfilesIndex.clear();
321 for(
unsigned int bg_index = 0; bg_index < bgfiles.size(); bg_index++) {
322 bgfilesIndex.push_back(bgfiles[bg_index] +
".idx");
326 if(m_fr)
delete m_fr;
328 std::vector<int> ranTrgEvents = m_fr->
getEventNumber(bgfilesIndex);
332 m_bgfilesIndex.clear();
333 m_ranTrgEvents.clear();
334 for(
unsigned int bg_index = 0; bg_index < bgfiles.size(); bg_index++) {
335 if(ranTrgEvents[bg_index] > 0) {
336 m_bgfiles.push_back(bgfiles[bg_index]);
337 m_bgfilesIndex.push_back(bgfilesIndex[bg_index]);
338 m_ranTrgEvents.push_back(ranTrgEvents[bg_index]);
343 if(m_fr)
delete m_fr;
348 bgfilesIndex.clear();
349 ranTrgEvents.clear();
352 if(m_bgfiles.empty() || m_ranTrgEvents.empty()) {
353 (*log) << MSG::WARNING <<
"No background datafiles found!!!" << endreq;
354 return StatusCode::SUCCESS;
358 if(m_mixingMethod == 1) {
360 m_ranStepLenInCurrentFile.clear();
366 bool ifsucc =
file_sort(m_bgfiles,m_ranTrgEvents);
367 if( !ifsucc )
return StatusCode::FAILURE;
370 m_bgfilesIndex.clear();
371 for(
unsigned int bg_index = 0; bg_index < m_bgfiles.size(); bg_index++) {
372 m_bgfilesIndex.push_back(m_bgfiles[bg_index] +
".idx");
376 m_vRanEvtNumInSubSet.clear();
377 m_vStreamNumInSubSet.clear();
380 int ranEvtNumInSubSet = 0;
382 for(
unsigned int i = 0; i < m_ranTrgEvents.size(); i++) {
383 if(i == 0) set_no = m_numSets[i];
384 if((i != 0) && (set_no != m_numSets[i])) {
385 m_vRanEvtNumInSubSet.push_back(ranEvtNumInSubSet);
386 m_vStreamNumInSubSet.push_back(nstream);
387 ranEvtNumInSubSet = 0;
389 set_no = m_numSets[i];
392 m_totRanEvtNum += m_ranTrgEvents[i];
393 ranEvtNumInSubSet += m_ranTrgEvents[i];
395 if(i == m_ranTrgEvents.size() - 1) {
396 m_vRanEvtNumInSubSet.push_back(ranEvtNumInSubSet);
397 m_vStreamNumInSubSet.push_back(nstream);
402 int evtNumInRun = -1;
403 std::vector<int> vtotEvtNo = m_jobInfoSvc->
getTotEvtNo();
404 for(
unsigned int ii = 0; ii < vtotEvtNo.size(); ii+=2) {
405 if(std::abs(
runNo) == std::abs(vtotEvtNo[ii]))
406 evtNumInRun = vtotEvtNo[ii+1];
413 std::cout <<
"ERROR: In MixerAlg::execute() ---> The tau value or total run time is 0, please check it. Exit! " << std::endl;
417 bool using_exp =
true;
418 if(totalTime*100 < tau) using_exp =
false;
419 m_vStepLength.clear();
422 if(using_exp ==
true) ranNum = RandExponential::shoot(tau);
423 else ranNum = RandFlat::shoot(0., totalTime);
424 if(ranNum > totalTime)
continue;
425 ranNum = ranNum*m_totRanEvtNum/totalTime;
426 m_vStepLength.push_back((
int)ranNum);
427 if(m_vStepLength.size() == evtNumInRun*m_nevent)
break;
430 sort(m_vStepLength.begin(), m_vStepLength.end());
435 if(evtNumInRun <= 0 || m_totRanEvtNum <= 0) {
436 (*log) << MSG::ERROR <<
"The event number (or random trigger event number) in run " << evt->runNumber() <<
" is zero" << endreq;
437 return StatusCode::FAILURE;
444 map_stepLength.clear();
445 for(
unsigned int i = 0; i < m_ranTrgEvents.size(); i++) {
446 std::vector<int> vstepLength;
447 typedef pair<int, std::vector<int> > vpair;
448 map_stepLength.insert(vpair(i,vstepLength));
451 for(
unsigned int i = 0; i < m_ranTrgEvents.size(); ) {
453 int pre_ranEvtNumSubSet = 0;
454 int cur_ranEvtNumSubSet = 0;
455 set_no = m_numSets[i];
456 for(
int j = 0; j < set_no; j++) {
457 if(j != (set_no - 1)) pre_ranEvtNumSubSet += m_vRanEvtNumInSubSet[j];
458 cur_ranEvtNumSubSet += m_vRanEvtNumInSubSet[j];
461 for(
unsigned j = 0; j < m_vStepLength.size(); j++) {
463 if((m_vStepLength[j] >= pre_ranEvtNumSubSet) && (m_vStepLength[j] < cur_ranEvtNumSubSet)) {
464 int sub_stepLength = int((m_vStepLength[j]-pre_ranEvtNumSubSet)/m_vStreamNumInSubSet[set_no - 1]);
466 int begin_fileId = -1, end_fileId = -1;
467 for(std::map<
int,std::vector<int> >::iterator
iter = map_stepLength.begin();
iter != map_stepLength.end();
iter++) {
469 if(set_no == m_numSets[
iter->first]) {
470 if(begin_fileId == -1) begin_fileId =
iter->first;
474 end_fileId = begin_fileId + file_id;
475 bool add_succ =
false;
479 int random_file = int(RandFlat::shootInt(
long(begin_fileId),
long(end_fileId)));
480 if(sub_stepLength < m_ranTrgEvents[random_file]) {
481 map_stepLength[random_file].push_back(sub_stepLength);
488 (*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;
494 i += m_vStreamNumInSubSet[set_no - 1];
498 unsigned int ranSelectedNum = 0;
499 for(std::map<
int,std::vector<int> >::iterator
iter = map_stepLength.begin();
iter != map_stepLength.end();
iter++) {
500 ranSelectedNum += (
iter->second).size();
503 if(ranSelectedNum != m_vStepLength.size()) {
504 (*log) << MSG::ERROR <<
"In MixerAlg::excute()--> selected bg events number not equal to MC events" << endreq;
505 return StatusCode::FAILURE;
510 if(m_mixingMethod == 2) {
512 if (m_fr)
delete m_fr;
515 m_bgfilesIndex.clear();
516 for(
unsigned int bg_index = 0; bg_index < m_bgfiles.size(); bg_index++) {
517 m_bgfilesIndex.push_back(m_bgfiles[bg_index] +
".idx");
519 if(m_skip ==
true && m_readBGMethod == 1) m_fr =
new RawFileReader(m_bgfiles, m_bgfilesIndex) ;
524 return StatusCode::FAILURE;
528 m_raw_event->
reset();
537 SmartDataPtr<MdcDigiCol> mdcMcDigits(eventSvc(),
"/Event/Digi/MdcDigiCol");
539 (*log) << MSG::ERROR <<
"Unable to retrieve MdcDigiCol" << endreq;
541 (*log) << MSG::INFO <<
"MdcDigiCol retrieved of size "<< mdcMcDigits->size() << endreq;
543 SmartDataPtr<EmcDigiCol> emcMcDigits(eventSvc(),
"/Event/Digi/EmcDigiCol");
545 (*log) << MSG::ERROR <<
"Unable to retrieve EmcDigiCol" << endreq;
547 (*log) << MSG::INFO <<
"EmcDigiCol retrieved of size "<< emcMcDigits->size() << endreq;
549 SmartDataPtr<MucDigiCol> mucMcDigits(eventSvc(),
"/Event/Digi/MucDigiCol");
551 (*log) << MSG::ERROR <<
"Unable to retrieve MucDigiCol" << endreq;
553 (*log) << MSG::INFO <<
"MucDigiCol retrieved of size "<< mucMcDigits->size() << endreq;
555 SmartDataPtr<TofDigiCol> tofMcDigits(eventSvc(),
"/Event/Digi/TofDigiCol");
557 (*log) << MSG::ERROR <<
"Unable to retrieve TofDigiCol" << endreq;
559 (*log) << MSG::INFO <<
"TofDigiCol retrieved of size "<< tofMcDigits->size() << endreq;
561 for(
int ievent = 0; ievent<m_nevent; ievent++)
563 (*log) << MSG::INFO <<
"Mixing BG Event " << ievent << endreq;
571 if(m_mixingMethod == 1) {
572 if(m_RealizationSvc->
UseDBFlag() ==
true && m_dbUserRequest ==
false) {
573 if(m_skipCount >= m_ranStepLenInCurrentFile.size()) {
574 m_ranStepLenInCurrentFile.clear();
575 for(std::map<
int,std::vector<int> >::iterator
iter = map_stepLength.begin();
iter != map_stepLength.end();
iter++) {
576 if(currentBGFile ==
"") {
577 if((
iter->second).size() == 0)
continue;
578 if(m_fr)
delete m_fr;
580 if(m_readBGMethod == 1) m_fr =
new RawFileReader(m_bgfiles[
iter->first], m_bgfiles[
iter->first]+
".idx") ;
582 m_totEvtNumInCurFile = m_ranTrgEvents[
iter->first];
587 m_ranStepLenInCurrentFile =
iter->second;
592 if(currentBGFile == m_bgfiles[
iter->first]) {
594 if(
iter == map_stepLength.end())
return StatusCode::FAILURE;
595 if((
iter->second).size() == 0) {
598 if(
iter == map_stepLength.end())
return StatusCode::FAILURE;
599 if((
iter->second).size() > 0)
break;
602 if(m_fr)
delete m_fr;
604 if(m_readBGMethod == 1) m_fr =
new RawFileReader(m_bgfiles[
iter->first], m_bgfiles[
iter->first]+
".idx") ;
606 m_totEvtNumInCurFile = m_ranTrgEvents[
iter->first];
611 m_ranStepLenInCurrentFile =
iter->second;
620 if(m_skipCount == 0) nskip = m_ranStepLenInCurrentFile[m_skipCount];
621 else nskip = m_ranStepLenInCurrentFile[m_skipCount] - m_ranStepLenInCurrentFile[m_skipCount - 1];
623 m_nEventsToEnd = (m_totEvtNumInCurFile - 1) - m_ranStepLenInCurrentFile[m_skipCount];
625 if(m_skipCount == 0 && nskip == 0) nskip = 1;
629 if(m_RealizationSvc->
UseDBFlag() ==
false || m_dbUserRequest ==
true) nskip =
int (2*m_NSkip*(RandFlat::shoot())) + 1;
630 if(m_totalEvent == 0 && nskip == 0) nskip = 1;
632 if(m_mixingMethod == 2) {
633 nskip = int (2*m_NSkip*(RandFlat::shoot())) + 1;
642 if(m_readBGMethod == 0) {
644 for(
int j = 0; j < nskip; j++) {
648 (*log) << MSG::ERROR <<
"Cannot get next background event" << endreq;
649 return StatusCode::FAILURE;
653 if(m_readBGMethod == 1) {
655 next =
nextEvent(nskip,0,m_nEventsToEnd);
658 (*log) << MSG::ERROR <<
"Cannot get next background event" << endreq;
659 return StatusCode::FAILURE;
662 if(m_readBGMethod == 2) {
667 (*log) << MSG::ERROR <<
"Cannot get next background event" << endreq;
668 return StatusCode::FAILURE;
676 if(m_mixingMethod == 1) {
677 if ( !next && m_totalEvent == 0) {
678 (*log) << MSG::ERROR <<
"Cannot get next background event" << endreq;
679 return StatusCode::FAILURE;
683 if(m_mixingMethod == 2) {
685 (*log) << MSG::ERROR <<
"Cannot get next background event" << endreq;
686 return StatusCode::FAILURE;
690 mixDigi(mdcMcDigits, emcMcDigits, mucMcDigits, tofMcDigits);
702 return StatusCode::SUCCESS;
706 if( m_raw_event )
delete m_raw_event;
707 if( log )
delete log;
708 if( m_fr)
delete m_fr;
709 return StatusCode::SUCCESS;
716 m_raw_event->
reset();
723 const uint32_t* fragment;
724 if(m_skip ==
true && m_readBGMethod == 0) fragment = m_fr->
nextEvent();
725 if(m_skip ==
true && m_readBGMethod == 1) {
727 else fragment = m_fr->
nextEvent(nskip - 1);
729 if(m_skip ==
true && m_readBGMethod == 2) {
733 if(m_skip ==
false) fragment = m_fr->
nextEvent();
747 std::cerr <<
"Found invalid event (traceback):" << std::endl;
751 uint32_t fFragmentSize =
f.fragment_size_word();
752 (*log) << MSG::DEBUG <<
"[Event No. #" <<
f.global_id()
753 <<
"] " <<
f.fragment_size_word() <<
" words in "
754 <<
f.nchildren() <<
" subdetectors "
760 cout<<
"EventNo "<<
f.global_id()<<endl;
764 const uint32_t* ef=
NULL;
765 f.event_filter_info(ef);
767 (*log) << MSG::ERROR <<
"Event Filter Data Failed!!!" << endreq;
771 (*log) << MSG::DEBUG<<
"Event Filter Information*********" <<std::hex<<endreq
772 <<*ef<<
" "<<*(ef+1)<<
" "<<*(ef+2)<<
" "<<*(ef+3)<<std::dec<<endreq;
773 m_raw_event->
addReHltRaw((uint32_t*)ef, (uint32_t)4);
779 for (
int robi = 0; robi < nrobs; robi++) {
782 uint32_t* dataptr =
NULL;
787 source_id_number <<= 8;
788 source_id_number >>= 24;
791 switch(source_id_number) {
831 if(m_usingFilter ==
true) {
833 if(m_skip ==
true && m_readBGMethod == 0) {
834 return nextEvent(1, evtbyte, eventsToEnd);
836 if(m_skip ==
true && m_readBGMethod == 1) {
837 if(m_RealizationSvc->
UseDBFlag() ==
false || m_dbUserRequest ==
true)
return nextEvent(1, evtbyte, eventsToEnd);
838 if(eventsToEnd > 0 && m_RealizationSvc->
UseDBFlag() ==
true && m_dbUserRequest ==
false ) {
840 return nextEvent(1, evtbyte, eventsToEnd);
843 if(m_skip ==
true && m_readBGMethod == 2) {
844 return nextEvent(1, evtbyte, eventsToEnd);
846 if(m_skip ==
false)
return nextEvent(nskip, evtbyte, eventsToEnd);
857 if(m_skip ==
true && m_readBGMethod == 1) m_fr =
new RawFileReader(m_bgfiles, m_bgfilesIndex) ;
865 return nextEvent(nskip, evtbyte, eventsToEnd);
871 std::cerr << std::endl <<
"Uncaught eformat issue: " << ex.what() << std::endl;
874 std::cerr << std::endl <<
"Uncaught ERS issue: " << ex.what() << std::endl;
876 catch (std::exception& ex) {
877 std::cerr << std::endl <<
"Uncaught std exception: " << ex.what() << std::endl;
880 std::cerr << std::endl <<
"Uncaught unknown exception" << std::endl;
886template <
class T1,
class T2>
889 vector<T2*> newDigiCol;
890 typename T1::iterator mc;
891 typename T1::const_iterator
bg;
893 for(
bg = bgDigits.begin();
bg != bgDigits.end();
bg++ )
896 for(mc = mcDigits->begin(); mc != mcDigits->end(); mc++ )
898 if((*mc)->identify()==(*bg)->identify())
902 cout <<
"****************************************"<<endl;
903 cout <<
"MC id " << (*mc)->identify().get_value()
904 <<
" BG Id " << (*bg)->identify().get_value() << endl;
905 cout<<
"==> MC Digi : ";
906 (*mc)->fillStream(cout);
907 cout<<
"==> BG Digi : ";
908 (*bg)->fillStream(cout);
911 (*mc)->setTrackIndex((*mc)->getTrackIndex() - 999);
917 cout<<
"==> New MC Digi: ";
918 (*mc)->fillStream(cout);
919 cout <<
"****************************************"<<endl;
926 (*bg)->setTrackIndex(-1000);
927 newDigiCol.push_back(*
bg);
931 for(
bg=newDigiCol.begin();
bg!=newDigiCol.end();
bg++ )
932 mcDigits->push_back(*
bg);
937 vector<MdcDigi*> newDigiCol;
938 MdcDigiCol::const_iterator mc;
939 MdcDigiCol::const_iterator
bg;
941 for(
bg = bgDigits.begin();
bg != bgDigits.end();
bg++ )
943 if((*bg)->getChargeChannel() < 0x7FFFFFFF) (*bg)->setChargeChannel(0);
945 for(mc = mcDigits->begin(); mc != mcDigits->end(); mc++ )
947 if((*mc)->identify()==(*bg)->identify())
951 cout <<
"****************************************"<<endl;
952 cout <<
"MC id " << (*mc)->identify().get_value()
953 <<
" BG Id " << (*bg)->identify().get_value() << endl;
954 cout<<
"==> MC Digi : ";
955 (*mc)->fillStream(cout);
956 cout<<
"==> BG Digi : ";
957 (*bg)->fillStream(cout);
960 (*mc)->setTrackIndex((*mc)->getTrackIndex() - 999);
966 cout<<
"==> New MC Digi: ";
967 (*mc)->fillStream(cout);
968 cout <<
"****************************************"<<endl;
975 (*bg)->setTrackIndex(-1000);
976 newDigiCol.push_back(*
bg);
980 for(
bg=newDigiCol.begin();
bg!=newDigiCol.end();
bg++ )
981 mcDigits->push_back(*
bg);
986 vector<TofDigi*> newDigiCol;
988 TofDigiCol::const_iterator bgTof = bgDigits.begin();
989 for(; bgTof!=bgDigits.end(); bgTof++ )
991 (*bgTof)->setTrackIndex(-1000);
992 newDigiCol.push_back(*bgTof);
994 for(bgTof=newDigiCol.begin(); bgTof!=newDigiCol.end(); bgTof++ ) {
995 mcDigits->push_back(*bgTof);
1000 SmartDataPtr<EmcDigiCol>& emcMcDigits,
1001 SmartDataPtr<MucDigiCol>& mucMcDigits,
1002 SmartDataPtr<TofDigiCol>& tofMcDigits)
1012 int tdc_min = -9, tdc_max = -9, tdc_tot = 0, tdc_num = 0;
1013 for(MdcDigiCol::const_iterator
bg = mdcCol.begin();
bg != mdcCol.end();
bg++ )
1015 if((*bg)->getTimeChannel() < 0x7FFFFFFF) {
1016 tdc_tot += (*bg)->getTimeChannel();
1018 if(tdc_min < 0) tdc_min = (*bg)->getTimeChannel();
1020 if(tdc_min > (*bg)->getTimeChannel()) tdc_min = (*bg)->getTimeChannel();
1022 if(tdc_max < 0) tdc_max = (*bg)->getTimeChannel();
1024 if(tdc_max < (*bg)->getTimeChannel()) tdc_max = (*bg)->getTimeChannel();
1028 int tdc_mean = (int) ((
double)tdc_tot/(double)tdc_num);
1032 tdc_shift = tdc_mean - CLHEP::RandFlat::shootInt(
long(0),
long(80*24/0.09375));
1033 if((tdc_min - tdc_shift)>=0 && (tdc_max - tdc_shift) <=
int(80*24/0.09375))
break;
1035 if(tdc_num > m_maxLoop)
break;
1039 for(MdcDigiCol::const_iterator
bg = mdcCol.begin();
bg != mdcCol.end();
bg++ )
1041 if((*bg)->getTimeChannel() >= 0x7FFFFFFF)
continue;
1042 int newTDC = (*bg)->getTimeChannel() - tdc_shift;
1043 if(newTDC < 0 || newTDC >
int(80*24/0.09375)) newTDC = int(CLHEP::RandFlat::shoot()*80*24/0.09375);
1044 (*bg)->setTimeChannel(newTDC);
1057 combineDigits<EmcDigiCol, EmcDigi>(emcMcDigits, emcCol, log->level());
1063 combineDigits<MucDigiCol, MucDigi>(mucMcDigits, mucCol, log->level());
1077 m_mdcCnv->
convert(mdcBuf, digiCol);
1083 m_mucCnv->
convert(mucBuf, digiCol);
1089 m_emcCnv->
convert(emcBuf, digiCol);
1096 if( etfBuf.
nBuf()>0 ) {
1097 m_tofCnv->
convert(tofBuf, etfBuf, digiCol);
1100 m_tofCnv->
convert(tofBuf, digiCol);
1125 uint32_t nbuf = gtdBuf.
nBuf();
1127 for (uint32_t i = 0; i < nbuf; i++) {
1128 uint32_t* buf = gtdBuf(i);
1129 uint32_t bufSize = gtdBuf.
bufSize(i);
1131 while (bufSize - index > 1) {
1132 uint32_t blockSize = ( ((*(buf+index))>>14) & 0x3FF);
1133 uint32_t
id = ((*(buf+index))>>24);
1134 if (blockSize == 0 || (index+blockSize) > bufSize)
break;
1135 if ((
id> 0xD1 &&
id < 0xD8 &&
id != 0xD5) ||
id == 0xDA || (
id > 0xE1 &&
id < 0xED)) {
1136 trigGTD =
new TrigGTD(buf+index);
1137 gtdCol->push_back(trigGTD);
1143 TrigGTDCol::iterator
iter = gtdCol->begin();
1144 for (;
iter != gtdCol->end();
iter++ ) {
1145 const uint32_t boardId = (*iter)->getId();
1146 const uint32_t timeWindow = (*iter)->getTimeWindow();
1147 const uint32_t size = (*iter)->getDataSize();
1148 const uint32_t* trigData = (*iter)->getDataPtr();
1151 if(boardId == 0xd3) {
1152 if(size%timeWindow != 0) {
1153 std::cout <<
"GTL data is NOT completed, exit." << std::endl;
1156 for(uint32_t j = 0; j < size; j++) {
1157 uint32_t dataId = ((trigData[j] >> 24) & 0x7);
1158 if(dataId != 5)
continue;
1159 for(uint32_t i = 1, loop = 0; loop < 24; i <<= 1, loop++) {
1160 if((loop == 16) && (trigData[j] & i)) timing = 1;
1161 if((loop == 17) && (trigData[j] & i) && (timing != 1)) timing = 2;
1162 if((loop == 18) && (trigData[j] & i) && (timing == 0)) timing = 3;
1172 std::vector<std::string> tmp_files = files;
1173 std::vector<int> tmp_ranEvtNums = ranEvtNums;
1178 const char* file_index[100];
1181 for(
int i = 0; i < 100; i++) {
1187 if(tmp_files.size() >= 100) {
1188 std::cout <<
"ERROR: In MixerAlg::file_sort(), please change bigger array size" << std::endl;
1192 for(
unsigned int i = 0; i < tmp_files.size(); i++) {
1194 const char* file1 = tmp_files[i].c_str();
1195 const char* substr1 = strstr(file1,
"_file");
1196 int strlen1 = strlen(substr1);
1200 for(
int sub1 = 0; sub1 < strlen1; sub1++) {
1201 if(substr1[sub1] ==
'e') {
1202 cset1[0] = substr1[sub1+1];
1203 cset1[1] = substr1[sub1+2];
1204 cset1[2] = substr1[sub1+3];
1207 else if(substr1[sub1] ==
'-') {
1208 cnum1[0] = substr1[sub1+1];
1217 int set1 = atoi(cset1);
1218 int num1 = atoi(cnum1);
1219 int encode_set1 = set1*100 +
num1;
1221 for(
unsigned int j = 0; j < tmp_files.size(); j++) {
1222 if(i == j)
continue;
1223 const char* file2 = tmp_files[j].c_str();
1224 const char* substr2 = strstr(file2,
"_file");
1225 int strlen2 = strlen(substr2);
1228 for(
int sub2 = 0; sub2 < strlen2; sub2++) {
1229 if(substr2[sub2] ==
'e') {
1230 cset2[0] = substr2[sub2+1];
1231 cset2[1] = substr2[sub2+2];
1232 cset2[2] = substr2[sub2+3];
1235 else if(substr2[sub2] ==
'-') {
1236 cnum2[0] = substr2[sub2+1];
1244 int set2 = atoi(cset2);
1245 int num2 = atoi(cnum2);
1246 int encode_set2 = set2*100 + num2;
1247 if(encode_set1 > encode_set2) index++;
1249 file_index[index] = tmp_files[i].c_str();
1250 num_index[index] = tmp_ranEvtNums[i];
1251 set_index[index] = set1;
1255 for(
unsigned int i = 0; i < tmp_files.size(); i++) {
1256 files.push_back(file_index[i]);
1257 ranEvtNums.push_back(num_index[i]);
1258 if(setNo != set_index[i]) {
1259 setNo = set_index[i];
1260 int numSets_size = m_numSets.size();
1261 if(numSets_size == 0) m_numSets.push_back(1);
1262 if(numSets_size != 0) m_numSets.push_back(m_numSets[numSets_size - 1] + 1);
1265 int numSets_size = m_numSets.size();
1266 m_numSets.push_back(m_numSets[numSets_size - 1]);
TFile f("ana_bhabha660a_dqa_mcPat_zy_old.root")
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
const BufferHolder & getEtfBuf() 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 addReEtfDigi(uint32_t *digi, uint32_t size)
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()