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
287 if(! m_pattern.empty())
288 {
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;
295 }
296 else{
297 (*log) << MSG::ERROR<<"string 'round' not found in random trigger path!"<<endreq;
298 exit(-1);
299 }
300 }
301 }
302 if (! m_newdatadir.empty())
303 {
304 for(unsigned int k = 0; k < bgfiles.size(); k++) {
305 char tmp[255];
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;
310 }
311 }
312
313
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");
318 }
319
320
321 if(m_fr) delete m_fr;
323 std::vector<int> ranTrgEvents = m_fr->
getEventNumber(bgfilesIndex);
324
325
326 m_bgfiles.clear();
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]);
334 }
335 }
336
337
338 if(m_fr) delete m_fr;
340
341
342 bgfiles.clear();
343 bgfilesIndex.clear();
344 ranTrgEvents.clear();
345
346
347 if(m_bgfiles.empty() || m_ranTrgEvents.empty()) {
348 (*log) << MSG::WARNING << "No background datafiles found!!!" << endreq;
349 return StatusCode::SUCCESS;
350 }
351
352 if(m_skip == true) {
353 if(m_mixingMethod == 1) {
354
355 m_ranStepLenInCurrentFile.clear();
356 currentBGFile = "";
357 m_skipCount = 0;
358 m_totalEvent = 0;
359
360
361 bool ifsucc =
file_sort(m_bgfiles,m_ranTrgEvents);
362 if( !ifsucc ) return StatusCode::FAILURE;
363
364
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");
368 }
369
370
371 m_vRanEvtNumInSubSet.clear();
372 m_vStreamNumInSubSet.clear();
373 m_totRanEvtNum = 0;
374 int set_no = -1;
375 int ranEvtNumInSubSet = 0;
376 int nstream = 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;
383 nstream = 0;
384 set_no = m_numSets[i];
385 }
386
387 m_totRanEvtNum += m_ranTrgEvents[i];
388 ranEvtNumInSubSet += m_ranTrgEvents[i];
389 nstream++;
390 if(i == m_ranTrgEvents.size() - 1) {
391 m_vRanEvtNumInSubSet.push_back(ranEvtNumInSubSet);
392 m_vStreamNumInSubSet.push_back(nstream);
393 }
394 }
395
396
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];
402 }
403
404
408 std::cout << "ERROR: In MixerAlg::execute() ---> The tau value or total run time is 0, please check it. Exit! " << std::endl;
409 exit(1);
410 }
411
412 bool using_exp = true;
413 if(totalTime*100 < tau) using_exp = false;
414 m_vStepLength.clear();
415 while(1) {
416 double ranNum;
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;
423 }
424
425 sort(m_vStepLength.begin(), m_vStepLength.end());
426
427
428
429
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;
433 }
434
435
436
437
438
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));
444 }
445
446 for(unsigned int i = 0; i < m_ranTrgEvents.size(); ) {
447
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];
454 }
455
456 for(unsigned j = 0; j < m_vStepLength.size(); j++) {
457
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]);
460 int file_id = 0;
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++) {
463
464 if(set_no == m_numSets[
iter->first]) {
465 if(begin_fileId == -1) begin_fileId =
iter->first;
466 file_id++;
467 }
468 }
469 end_fileId = begin_fileId + file_id;
470 bool add_succ = false;
471 long loop_count = 0;
472
473 while(1) {
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);
477 add_succ = true;
478 loop_count = 0;
479 }
480 if(add_succ) break;
481 loop_count++;
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;
484 exit(1);
485 }
486 }
487 }
488 }
489 i += m_vStreamNumInSubSet[set_no - 1];
490 }
491
492
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();
496
497 }
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;
501 }
502 }
503 }
504
505 if(m_mixingMethod == 2) {
506
507 if (m_fr) delete m_fr;
508 m_fr = NULL;
509 try {
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");
513 }
514 if(m_skip ==
true && m_readBGMethod == 1) m_fr =
new RawFileReader(m_bgfiles, m_bgfilesIndex) ;
516 }
519 return StatusCode::FAILURE;
520 }
521 }
522
523 m_raw_event->
reset();
524 }
525 }
526 if(m_ifOutPut) {
530 }
531
532 SmartDataPtr<MdcDigiCol> mdcMcDigits(eventSvc(),"/Event/Digi/MdcDigiCol");
533 if( ! mdcMcDigits )
534 (*log) << MSG::ERROR << "Unable to retrieve MdcDigiCol" << endreq;
535 else
536 (*log) << MSG::INFO << "MdcDigiCol retrieved of size "<< mdcMcDigits->size() << endreq;
537
538 SmartDataPtr<EmcDigiCol> emcMcDigits(eventSvc(),"/Event/Digi/EmcDigiCol");
539 if( ! emcMcDigits )
540 (*log) << MSG::ERROR << "Unable to retrieve EmcDigiCol" << endreq;
541 else
542 (*log) << MSG::INFO << "EmcDigiCol retrieved of size "<< emcMcDigits->size() << endreq;
543
544 SmartDataPtr<MucDigiCol> mucMcDigits(eventSvc(),"/Event/Digi/MucDigiCol");
545 if( ! mucMcDigits )
546 (*log) << MSG::ERROR << "Unable to retrieve MucDigiCol" << endreq;
547 else
548 (*log) << MSG::INFO << "MucDigiCol retrieved of size "<< mucMcDigits->size() << endreq;
549
550 SmartDataPtr<TofDigiCol> tofMcDigits(eventSvc(),"/Event/Digi/TofDigiCol");
551 if( ! tofMcDigits )
552 (*log) << MSG::ERROR << "Unable to retrieve TofDigiCol" << endreq;
553 else
554 (*log) << MSG::INFO << "TofDigiCol retrieved of size "<< tofMcDigits->size() << endreq;
555
556 for(int ievent = 0; ievent<m_nevent; ievent++)
557 {
558 (*log) << MSG::INFO << "Mixing BG Event " << ievent << endreq;
559
560
561
562
564 if(m_skip == true) {
565 int nskip = 0;
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;
574 try {
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];
578 }
581 }
582 m_ranStepLenInCurrentFile =
iter->second;
583 m_skipCount = 0;
585 break;
586 }
587 if(currentBGFile == m_bgfiles[
iter->first]) {
589 if(
iter == map_stepLength.end())
return StatusCode::FAILURE;
590 if((
iter->second).size() == 0) {
591 while(1) {
593 if(
iter == map_stepLength.end())
return StatusCode::FAILURE;
594 if((
iter->second).size() > 0)
break;
595 }
596 }
597 if(m_fr) delete m_fr;
598 try {
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];
602 }
605 }
606 m_ranStepLenInCurrentFile =
iter->second;
607 m_skipCount = 0;
609 break;
610 }
611 }
612 }
613
614
615 if(m_skipCount == 0) nskip = m_ranStepLenInCurrentFile[m_skipCount];
616 else nskip = m_ranStepLenInCurrentFile[m_skipCount] - m_ranStepLenInCurrentFile[m_skipCount - 1];
617
618 m_nEventsToEnd = (m_totEvtNumInCurFile - 1) - m_ranStepLenInCurrentFile[m_skipCount];
619
620 if(m_skipCount == 0 && nskip == 0) nskip = 1;
621
622 m_skipCount++;
623 }
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;
626 }
627 if(m_mixingMethod == 2) {
628 nskip = int (2*m_NSkip*(RandFlat::shoot())) + 1;
629 }
630 if(m_ifOutPut) {
634 }
635
636
637 if(m_readBGMethod == 0) {
638
639 for(int j = 0; j < nskip; j++) {
641 if ( ! next )
642 {
643 (*log) << MSG::ERROR << "Cannot get next background event" << endreq;
644 return StatusCode::FAILURE;
645 }
646 }
647 }
648 if(m_readBGMethod == 1) {
649
651 if ( ! next )
652 {
653 (*log) << MSG::ERROR << "Cannot get next background event" << endreq;
654 return StatusCode::FAILURE;
655 }
656 }
657 if(m_readBGMethod == 2) {
658
660 if ( ! next )
661 {
662 (*log) << MSG::ERROR << "Cannot get next background event" << endreq;
663 return StatusCode::FAILURE;
664 }
665 }
666 }
667 else {
669 }
670
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;
675 }
676 }
677
678 if(m_mixingMethod == 2) {
679 if ( !next ) {
680 (*log) << MSG::ERROR << "Cannot get next background event" << endreq;
681 return StatusCode::FAILURE;
682 }
683 }
684
685 mixDigi(mdcMcDigits, emcMcDigits, mucMcDigits, tofMcDigits);
686 }
687
688 m_totalEvent++;
689
690 if(m_ifOutPut) {
693 m_tuple1->write();
694 }
695
696
697 return StatusCode::SUCCESS;
698}
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()