268{
269
270 if(m_ifOutPut) {
272 }
273
274
275 SmartDataPtr<Event::EventHeader> evt(eventSvc(),"/Event/EventHeader");
276 if( !evt ){
277 return StatusCode::FAILURE;
278 }
279
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;
289 exit(-1);
290 }
291
292 if(! m_pattern.empty())
293 {
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;
300 }
301 else{
302 (*log) << MSG::ERROR<<"string 'round' not found in random trigger path!"<<endreq;
303 exit(-1);
304 }
305 }
306 }
307 if (! m_newdatadir.empty())
308 {
309 for(unsigned int k = 0; k < bgfiles.size(); k++) {
310 char tmp[255];
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;
315 }
316 }
317
318
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");
323 }
324
325
326 if(m_fr) delete m_fr;
328 std::vector<int> ranTrgEvents = m_fr->
getEventNumber(bgfilesIndex);
329
330
331 m_bgfiles.clear();
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]);
339 }
340 }
341
342
343 if(m_fr) delete m_fr;
345
346
347 bgfiles.clear();
348 bgfilesIndex.clear();
349 ranTrgEvents.clear();
350
351
352 if(m_bgfiles.empty() || m_ranTrgEvents.empty()) {
353 (*log) << MSG::WARNING << "No background datafiles found!!!" << endreq;
354 return StatusCode::SUCCESS;
355 }
356
357 if(m_skip == true) {
358 if(m_mixingMethod == 1) {
359
360 m_ranStepLenInCurrentFile.clear();
361 currentBGFile = "";
362 m_skipCount = 0;
363 m_totalEvent = 0;
364
365
366 bool ifsucc =
file_sort(m_bgfiles,m_ranTrgEvents);
367 if( !ifsucc ) return StatusCode::FAILURE;
368
369
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");
373 }
374
375
376 m_vRanEvtNumInSubSet.clear();
377 m_vStreamNumInSubSet.clear();
378 m_totRanEvtNum = 0;
379 int set_no = -1;
380 int ranEvtNumInSubSet = 0;
381 int nstream = 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;
388 nstream = 0;
389 set_no = m_numSets[i];
390 }
391
392 m_totRanEvtNum += m_ranTrgEvents[i];
393 ranEvtNumInSubSet += m_ranTrgEvents[i];
394 nstream++;
395 if(i == m_ranTrgEvents.size() - 1) {
396 m_vRanEvtNumInSubSet.push_back(ranEvtNumInSubSet);
397 m_vStreamNumInSubSet.push_back(nstream);
398 }
399 }
400
401
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];
407 }
408
409
413 std::cout << "ERROR: In MixerAlg::execute() ---> The tau value or total run time is 0, please check it. Exit! " << std::endl;
414 exit(1);
415 }
416
417 bool using_exp = true;
418 if(totalTime*100 < tau) using_exp = false;
419 m_vStepLength.clear();
420 while(1) {
421 double ranNum;
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;
428 }
429
430 sort(m_vStepLength.begin(), m_vStepLength.end());
431
432
433
434
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;
438 }
439
440
441
442
443
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));
449 }
450
451 for(unsigned int i = 0; i < m_ranTrgEvents.size(); ) {
452
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];
459 }
460
461 for(unsigned j = 0; j < m_vStepLength.size(); j++) {
462
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]);
465 int file_id = 0;
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++) {
468
469 if(set_no == m_numSets[
iter->first]) {
470 if(begin_fileId == -1) begin_fileId =
iter->first;
471 file_id++;
472 }
473 }
474 end_fileId = begin_fileId + file_id;
475 bool add_succ = false;
476 long loop_count = 0;
477
478 while(1) {
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);
482 add_succ = true;
483 loop_count = 0;
484 }
485 if(add_succ) break;
486 loop_count++;
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;
489 exit(1);
490 }
491 }
492 }
493 }
494 i += m_vStreamNumInSubSet[set_no - 1];
495 }
496
497
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();
501
502 }
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;
506 }
507 }
508 }
509
510 if(m_mixingMethod == 2) {
511
512 if (m_fr) delete m_fr;
514 try {
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");
518 }
519 if(m_skip ==
true && m_readBGMethod == 1) m_fr =
new RawFileReader(m_bgfiles, m_bgfilesIndex) ;
521 }
524 return StatusCode::FAILURE;
525 }
526 }
527
528 m_raw_event->
reset();
529 }
530 }
531 if(m_ifOutPut) {
535 }
536
537 SmartDataPtr<MdcDigiCol> mdcMcDigits(eventSvc(),"/Event/Digi/MdcDigiCol");
538 if( ! mdcMcDigits )
539 (*log) << MSG::ERROR << "Unable to retrieve MdcDigiCol" << endreq;
540 else
541 (*log) << MSG::INFO << "MdcDigiCol retrieved of size "<< mdcMcDigits->size() << endreq;
542
543 SmartDataPtr<EmcDigiCol> emcMcDigits(eventSvc(),"/Event/Digi/EmcDigiCol");
544 if( ! emcMcDigits )
545 (*log) << MSG::ERROR << "Unable to retrieve EmcDigiCol" << endreq;
546 else
547 (*log) << MSG::INFO << "EmcDigiCol retrieved of size "<< emcMcDigits->size() << endreq;
548
549 SmartDataPtr<MucDigiCol> mucMcDigits(eventSvc(),"/Event/Digi/MucDigiCol");
550 if( ! mucMcDigits )
551 (*log) << MSG::ERROR << "Unable to retrieve MucDigiCol" << endreq;
552 else
553 (*log) << MSG::INFO << "MucDigiCol retrieved of size "<< mucMcDigits->size() << endreq;
554
555 SmartDataPtr<TofDigiCol> tofMcDigits(eventSvc(),"/Event/Digi/TofDigiCol");
556 if( ! tofMcDigits )
557 (*log) << MSG::ERROR << "Unable to retrieve TofDigiCol" << endreq;
558 else
559 (*log) << MSG::INFO << "TofDigiCol retrieved of size "<< tofMcDigits->size() << endreq;
560
561 for(int ievent = 0; ievent<m_nevent; ievent++)
562 {
563 (*log) << MSG::INFO << "Mixing BG Event " << ievent << endreq;
564
565
566
567
569 if(m_skip == true) {
570 int nskip = 0;
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;
579 try {
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];
583 }
586 }
587 m_ranStepLenInCurrentFile =
iter->second;
588 m_skipCount = 0;
590 break;
591 }
592 if(currentBGFile == m_bgfiles[
iter->first]) {
594 if(
iter == map_stepLength.end())
return StatusCode::FAILURE;
595 if((
iter->second).size() == 0) {
596 while(1) {
598 if(
iter == map_stepLength.end())
return StatusCode::FAILURE;
599 if((
iter->second).size() > 0)
break;
600 }
601 }
602 if(m_fr) delete m_fr;
603 try {
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];
607 }
610 }
611 m_ranStepLenInCurrentFile =
iter->second;
612 m_skipCount = 0;
614 break;
615 }
616 }
617 }
618
619
620 if(m_skipCount == 0) nskip = m_ranStepLenInCurrentFile[m_skipCount];
621 else nskip = m_ranStepLenInCurrentFile[m_skipCount] - m_ranStepLenInCurrentFile[m_skipCount - 1];
622
623 m_nEventsToEnd = (m_totEvtNumInCurFile - 1) - m_ranStepLenInCurrentFile[m_skipCount];
624
625 if(m_skipCount == 0 && nskip == 0) nskip = 1;
626
627 m_skipCount++;
628 }
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;
631 }
632 if(m_mixingMethod == 2) {
633 nskip = int (2*m_NSkip*(RandFlat::shoot())) + 1;
634 }
635 if(m_ifOutPut) {
639 }
640
641
642 if(m_readBGMethod == 0) {
643
644 for(int j = 0; j < nskip; j++) {
646 if ( ! next )
647 {
648 (*log) << MSG::ERROR << "Cannot get next background event" << endreq;
649 return StatusCode::FAILURE;
650 }
651 }
652 }
653 if(m_readBGMethod == 1) {
654
656 if ( ! next )
657 {
658 (*log) << MSG::ERROR << "Cannot get next background event" << endreq;
659 return StatusCode::FAILURE;
660 }
661 }
662 if(m_readBGMethod == 2) {
663
665 if ( ! next )
666 {
667 (*log) << MSG::ERROR << "Cannot get next background event" << endreq;
668 return StatusCode::FAILURE;
669 }
670 }
671 }
672 else {
674 }
675
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;
680 }
681 }
682
683 if(m_mixingMethod == 2) {
684 if ( !next ) {
685 (*log) << MSG::ERROR << "Cannot get next background event" << endreq;
686 return StatusCode::FAILURE;
687 }
688 }
689
690 mixDigi(mdcMcDigits, emcMcDigits, mucMcDigits, tofMcDigits);
691 }
692
693 m_totalEvent++;
694
695 if(m_ifOutPut) {
698 m_tuple1->write();
699 }
700
701
702 return StatusCode::SUCCESS;
703}
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()