267{
268
269 if(m_ifOutPut) {
271 }
272
273
274 SmartDataPtr<Event::EventHeader> evt(eventSvc(),"/Event/EventHeader");
275 if( !evt ){
276 return StatusCode::FAILURE;
277 }
278
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();
286 if(bgfiles.size() == 0) {
287 (*log) << MSG::ERROR << "No random trigger files are found in the run " << m_runNo << std::endl;
288 exit(-1);
289 }
290
291 if(! m_pattern.empty())
292 {
293 for(unsigned int k = 0; k < bgfiles.size(); k++) {
294 size_t pos_round = bgfiles[k].rfind("round");
295 (*log) << MSG::INFO<<"bgfiles: "<<bgfiles[k]<<endreq;
296 if(pos_round!=string::npos){
297 bgfiles[k].replace(bgfiles[k].begin(), bgfiles[k].begin()+pos_round, m_pattern);
298 (*log) << MSG::INFO<<"new random trigger data path: "<<bgfiles[k]<<endreq;
299 }
300 else{
301 (*log) << MSG::ERROR<<"string 'round' not found in random trigger path!"<<endreq;
302 exit(-1);
303 }
304 }
305 }
306 if (! m_newdatadir.empty())
307 {
308 for(unsigned int k = 0; k < bgfiles.size(); k++) {
309 char tmp[255];
310 std::strcpy (tmp, bgfiles[k].
c_str());
311 string fname = basename(tmp);
312 bgfiles[k].replace(bgfiles[k].begin(), bgfiles[k].end(), m_newdatadir+'/'+fname);
313 (*log) << MSG::INFO<<"new random trigger data path: "<<bgfiles[k]<<endreq;
314 }
315 }
316
317
318 std::vector<std::string> bgfilesIndex;
319 bgfilesIndex.clear();
320 for(unsigned int bg_index = 0; bg_index < bgfiles.size(); bg_index++) {
321 bgfilesIndex.push_back(bgfiles[bg_index] + ".idx");
322 }
323
324
325 if(m_fr) delete m_fr;
327 std::vector<int> ranTrgEvents = m_fr->
getEventNumber(bgfilesIndex);
328
329
330 m_bgfiles.clear();
331 m_bgfilesIndex.clear();
332 m_ranTrgEvents.clear();
333 for(unsigned int bg_index = 0; bg_index < bgfiles.size(); bg_index++) {
334 if(ranTrgEvents[bg_index] > 0) {
335 m_bgfiles.push_back(bgfiles[bg_index]);
336 m_bgfilesIndex.push_back(bgfilesIndex[bg_index]);
337 m_ranTrgEvents.push_back(ranTrgEvents[bg_index]);
338 }
339 }
340
341
342 if(m_fr) delete m_fr;
344
345
346 bgfiles.clear();
347 bgfilesIndex.clear();
348 ranTrgEvents.clear();
349
350
351 if(m_bgfiles.empty() || m_ranTrgEvents.empty()) {
352 (*log) << MSG::WARNING << "No background datafiles found!!!" << endreq;
353 return StatusCode::SUCCESS;
354 }
355
356 if(m_skip == true) {
357 if(m_mixingMethod == 1) {
358
359 m_ranStepLenInCurrentFile.clear();
360 currentBGFile = "";
361 m_skipCount = 0;
362 m_totalEvent = 0;
363
364
365 bool ifsucc =
file_sort(m_bgfiles,m_ranTrgEvents);
366 if( !ifsucc ) return StatusCode::FAILURE;
367
368
369 m_bgfilesIndex.clear();
370 for(unsigned int bg_index = 0; bg_index < m_bgfiles.size(); bg_index++) {
371 m_bgfilesIndex.push_back(m_bgfiles[bg_index] + ".idx");
372 }
373
374
375 m_vRanEvtNumInSubSet.clear();
376 m_vStreamNumInSubSet.clear();
377 m_totRanEvtNum = 0;
378 int set_no = -1;
379 int ranEvtNumInSubSet = 0;
380 int nstream = 0;
381 for(unsigned int i = 0; i < m_ranTrgEvents.size(); i++) {
382 if(i == 0) set_no = m_numSets[i];
383 if((i != 0) && (set_no != m_numSets[i])) {
384 m_vRanEvtNumInSubSet.push_back(ranEvtNumInSubSet);
385 m_vStreamNumInSubSet.push_back(nstream);
386 ranEvtNumInSubSet = 0;
387 nstream = 0;
388 set_no = m_numSets[i];
389 }
390
391 m_totRanEvtNum += m_ranTrgEvents[i];
392 ranEvtNumInSubSet += m_ranTrgEvents[i];
393 nstream++;
394 if(i == m_ranTrgEvents.size() - 1) {
395 m_vRanEvtNumInSubSet.push_back(ranEvtNumInSubSet);
396 m_vStreamNumInSubSet.push_back(nstream);
397 }
398 }
399
400
401 int evtNumInRun = -1;
402 std::vector<int> vtotEvtNo = m_jobInfoSvc->
getTotEvtNo();
403 for(unsigned int ii = 0; ii < vtotEvtNo.size(); ii+=2) {
404 if(std::abs(
runNo) == std::abs(vtotEvtNo[ii]))
405 evtNumInRun = vtotEvtNo[ii+1];
406 }
407
408
412 std::cout << "ERROR: In MixerAlg::execute() ---> The tau value or total run time is 0, please check it. Exit! " << std::endl;
413 exit(1);
414 }
415
416 bool using_exp = true;
417 if(totalTime*100 < tau) using_exp = false;
418 m_vStepLength.clear();
419 while(1) {
420 double ranNum;
421 if(using_exp == true) ranNum = RandExponential::shoot(tau);
422 else ranNum = RandFlat::shoot(0., totalTime);
423 if(ranNum > totalTime) continue;
424 ranNum = ranNum*m_totRanEvtNum/totalTime;
425 m_vStepLength.push_back((int)ranNum);
426 if(m_vStepLength.size() == evtNumInRun) break;
427 }
428
429 sort(m_vStepLength.begin(), m_vStepLength.end());
430
431
432
433
434 if(evtNumInRun <= 0 || m_totRanEvtNum <= 0) {
435 (*log) << MSG::ERROR << "The event number (or random trigger event number) in run " << evt->runNumber() << " is zero" << endreq;
436 return StatusCode::FAILURE;
437 }
438
439
440
441
442
443 map_stepLength.clear();
444 for(unsigned int i = 0; i < m_ranTrgEvents.size(); i++) {
445 std::vector<int> vstepLength;
446 typedef pair<int, std::vector<int> > vpair;
447 map_stepLength.insert(vpair(i,vstepLength));
448 }
449
450 for(unsigned int i = 0; i < m_ranTrgEvents.size(); ) {
451
452 int pre_ranEvtNumSubSet = 0;
453 int cur_ranEvtNumSubSet = 0;
454 set_no = m_numSets[i];
455 for(int j = 0; j < set_no; j++) {
456 if(j != (set_no - 1)) pre_ranEvtNumSubSet += m_vRanEvtNumInSubSet[j];
457 cur_ranEvtNumSubSet += m_vRanEvtNumInSubSet[j];
458 }
459
460 for(unsigned j = 0; j < m_vStepLength.size(); j++) {
461
462 if((m_vStepLength[j] >= pre_ranEvtNumSubSet) && (m_vStepLength[j] < cur_ranEvtNumSubSet)) {
463 int sub_stepLength = int((m_vStepLength[j]-pre_ranEvtNumSubSet)/m_vStreamNumInSubSet[set_no - 1]);
464 int file_id = 0;
465 int begin_fileId = -1, end_fileId = -1;
466 for(std::map<
int,std::vector<int> >::iterator
iter = map_stepLength.begin();
iter != map_stepLength.end();
iter++) {
467
468 if(set_no == m_numSets[
iter->first]) {
469 if(begin_fileId == -1) begin_fileId =
iter->first;
470 file_id++;
471 }
472 }
473 end_fileId = begin_fileId + file_id;
474 bool add_succ = false;
475 long loop_count = 0;
476
477 while(1) {
478 int random_file = int(RandFlat::shootInt(long(begin_fileId), long(end_fileId)));
479 if(sub_stepLength < m_ranTrgEvents[random_file]) {
480 map_stepLength[random_file].push_back(sub_stepLength);
481 add_succ = true;
482 loop_count = 0;
483 }
484 if(add_succ) break;
485 loop_count++;
487 (*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;
488 exit(1);
489 }
490 }
491 }
492 }
493 i += m_vStreamNumInSubSet[set_no - 1];
494 }
495
496
497 unsigned int ranSelectedNum = 0;
498 for(std::map<
int,std::vector<int> >::iterator
iter = map_stepLength.begin();
iter != map_stepLength.end();
iter++) {
499 ranSelectedNum += (
iter->second).size();
500
501 }
502 if(ranSelectedNum != m_vStepLength.size()) {
503 (*log) << MSG::ERROR << "In MixerAlg::excute()--> selected bg events number not equal to MC events" << endreq;
504 return StatusCode::FAILURE;
505 }
506 }
507 }
508
509 if(m_mixingMethod == 2) {
510
511 if (m_fr) delete m_fr;
512 m_fr = NULL;
513 try {
514 m_bgfilesIndex.clear();
515 for(unsigned int bg_index = 0; bg_index < m_bgfiles.size(); bg_index++) {
516 m_bgfilesIndex.push_back(m_bgfiles[bg_index] + ".idx");
517 }
518 if(m_skip ==
true && m_readBGMethod == 1) m_fr =
new RawFileReader(m_bgfiles, m_bgfilesIndex) ;
520 }
523 return StatusCode::FAILURE;
524 }
525 }
526
527 m_raw_event->
reset();
528 }
529 }
530 if(m_ifOutPut) {
534 }
535
536 SmartDataPtr<MdcDigiCol> mdcMcDigits(eventSvc(),"/Event/Digi/MdcDigiCol");
537 if( ! mdcMcDigits )
538 (*log) << MSG::ERROR << "Unable to retrieve MdcDigiCol" << endreq;
539 else
540 (*log) << MSG::INFO << "MdcDigiCol retrieved of size "<< mdcMcDigits->size() << endreq;
541
542 SmartDataPtr<EmcDigiCol> emcMcDigits(eventSvc(),"/Event/Digi/EmcDigiCol");
543 if( ! emcMcDigits )
544 (*log) << MSG::ERROR << "Unable to retrieve EmcDigiCol" << endreq;
545 else
546 (*log) << MSG::INFO << "EmcDigiCol retrieved of size "<< emcMcDigits->size() << endreq;
547
548 SmartDataPtr<MucDigiCol> mucMcDigits(eventSvc(),"/Event/Digi/MucDigiCol");
549 if( ! mucMcDigits )
550 (*log) << MSG::ERROR << "Unable to retrieve MucDigiCol" << endreq;
551 else
552 (*log) << MSG::INFO << "MucDigiCol retrieved of size "<< mucMcDigits->size() << endreq;
553
554 SmartDataPtr<TofDigiCol> tofMcDigits(eventSvc(),"/Event/Digi/TofDigiCol");
555 if( ! tofMcDigits )
556 (*log) << MSG::ERROR << "Unable to retrieve TofDigiCol" << endreq;
557 else
558 (*log) << MSG::INFO << "TofDigiCol retrieved of size "<< tofMcDigits->size() << endreq;
559
560 for(int ievent = 0; ievent<m_nevent; ievent++)
561 {
562 (*log) << MSG::INFO << "Mixing BG Event " << ievent << endreq;
563
564
565
566
568 if(m_skip == true) {
569 int nskip = 0;
570 if(m_mixingMethod == 1) {
571 if(m_RealizationSvc->
UseDBFlag() ==
true && m_dbUserRequest ==
false) {
572 if(m_skipCount >= m_ranStepLenInCurrentFile.size()) {
573 m_ranStepLenInCurrentFile.clear();
574 for(std::map<
int,std::vector<int> >::iterator
iter = map_stepLength.begin();
iter != map_stepLength.end();
iter++) {
575 if(currentBGFile == "") {
576 if((
iter->second).size() == 0)
continue;
577 if(m_fr) delete m_fr;
578 try {
579 if(m_readBGMethod == 1) m_fr =
new RawFileReader(m_bgfiles[
iter->first], m_bgfiles[
iter->first]+
".idx") ;
581 m_totEvtNumInCurFile = m_ranTrgEvents[
iter->first];
582 }
585 }
586 m_ranStepLenInCurrentFile =
iter->second;
587 m_skipCount = 0;
589 break;
590 }
591 if(currentBGFile == m_bgfiles[
iter->first]) {
593 if(
iter == map_stepLength.end())
return StatusCode::FAILURE;
594 if((
iter->second).size() == 0) {
595 while(1) {
597 if(
iter == map_stepLength.end())
return StatusCode::FAILURE;
598 if((
iter->second).size() > 0)
break;
599 }
600 }
601 if(m_fr) delete m_fr;
602 try {
603 if(m_readBGMethod == 1) m_fr =
new RawFileReader(m_bgfiles[
iter->first], m_bgfiles[
iter->first]+
".idx") ;
605 m_totEvtNumInCurFile = m_ranTrgEvents[
iter->first];
606 }
609 }
610 m_ranStepLenInCurrentFile =
iter->second;
611 m_skipCount = 0;
613 break;
614 }
615 }
616 }
617
618
619 if(m_skipCount == 0) nskip = m_ranStepLenInCurrentFile[m_skipCount];
620 else nskip = m_ranStepLenInCurrentFile[m_skipCount] - m_ranStepLenInCurrentFile[m_skipCount - 1];
621
622 m_nEventsToEnd = (m_totEvtNumInCurFile - 1) - m_ranStepLenInCurrentFile[m_skipCount];
623
624 if(m_skipCount == 0 && nskip == 0) nskip = 1;
625
626 m_skipCount++;
627 }
628 if(m_RealizationSvc->
UseDBFlag() ==
false || m_dbUserRequest ==
true) nskip =
int (2*m_NSkip*(RandFlat::shoot())) + 1;
629 if(m_totalEvent == 0 && nskip == 0) nskip = 1;
630 }
631 if(m_mixingMethod == 2) {
632 nskip = int (2*m_NSkip*(RandFlat::shoot())) + 1;
633 }
634 if(m_ifOutPut) {
638 }
639
640
641 if(m_readBGMethod == 0) {
642
643 for(int j = 0; j < nskip; j++) {
645 if ( ! next )
646 {
647 (*log) << MSG::ERROR << "Cannot get next background event" << endreq;
648 return StatusCode::FAILURE;
649 }
650 }
651 }
652 if(m_readBGMethod == 1) {
653
655 if ( ! next )
656 {
657 (*log) << MSG::ERROR << "Cannot get next background event" << endreq;
658 return StatusCode::FAILURE;
659 }
660 }
661 if(m_readBGMethod == 2) {
662
664 if ( ! next )
665 {
666 (*log) << MSG::ERROR << "Cannot get next background event" << endreq;
667 return StatusCode::FAILURE;
668 }
669 }
670 }
671 else {
673 }
674
675 if(m_mixingMethod == 1) {
676 if ( !next && m_totalEvent == 0) {
677 (*log) << MSG::ERROR << "Cannot get next background event" << endreq;
678 return StatusCode::FAILURE;
679 }
680 }
681
682 if(m_mixingMethod == 2) {
683 if ( !next ) {
684 (*log) << MSG::ERROR << "Cannot get next background event" << endreq;
685 return StatusCode::FAILURE;
686 }
687 }
688
689 mixDigi(mdcMcDigits, emcMcDigits, mucMcDigits, tofMcDigits);
690 }
691
692 m_totalEvent++;
693
694 if(m_ifOutPut) {
697 m_tuple1->write();
698 }
699
700
701 return StatusCode::SUCCESS;
702}
float elapsed(void) const
std::vector< int > getTotEvtNo()
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)
bool nextEvent(int nskip=0, int evtbyte=0, int eventsToEnd=0)
virtual void print() const
static std::vector< int > getEventNumber(const VFileNames_t &idxfnames)
std::string currentFile()
std::vector< std::string > getBgFileName()
static RootInterface * Instance(MsgStream log)
singleton behaviour
virtual std::string getCurrentFileName()