622 def run(self, maxEvents=None):
623 """
624 1. Create ApplicationMgr via BootstrapHelper
625 2. Set JobOptionsSvcType, JobOptionsType, JobOptionsPath
626 3. configure() -> initialize() -> prepareForStart() -> start() ->
627 hltUpdateAfterFork() -> run() -> stop() -> finalize() -> terminate()
628 """
629 from Gaudi.Main import BootstrapHelper
630
631
632 if self.job_options_type == "FILE" and self.properties is None:
633 with open(self.job_options_path, 'r') as f:
634 jocat = json.load(f)
635 self.properties = jocat.get('properties', {})
636
637 bsh = BootstrapHelper()
638 app = bsh.createApplicationMgr()
639 self._app = app
640
641 app.setProperty("MessageSvcType", "TrigMessageSvc")
642
643
644 if self.job_options_type == "FILE" and self.properties:
645 app_props = self.properties.
get(
'ApplicationMgr', {})
646 for k, v in app_props.items():
647 if k not in ('JobOptionsSvcType', 'JobOptionsType', 'JobOptionsPath'):
648 log.debug("Setting ApplicationMgr.%s = %s", k, v)
649 app.setProperty(k, str(v) if not isinstance(v, str) else v)
650
651
652 log.info("Configuring TrigConf::JobOptionsSvc with TYPE=%s, PATH=%s",
653 self.job_options_type, self.job_options_path)
654 app.setProperty("JobOptionsSvcType", "TrigConf::JobOptionsSvc")
655 app.setProperty("JobOptionsType", self.job_options_type)
656 app.setProperty("JobOptionsPath", self.job_options_path)
657
658
659 app.configure()
660
661
662
663 if maxEvents is not None:
664 log.info("Setting EvtMax=%d (overriding DB value)", maxEvents)
665 app.setProperty('EvtMax', str(maxEvents))
666
667
668 self.overrides.apply()
669
670
671
672 if self.overrides.service_types.get('THistSvc') == 'THistSvc':
673 from GaudiPython.Bindings import iProperty
674 from TriggerJobOpts.TriggerHistSvcConfig import setTHistSvcOutput
675 output = []
676 setTHistSvcOutput(output)
677 iProperty("THistSvc").Output = output
678
679
680 if self.overrides.commands:
681 log.info("Executing postcommand(s)")
682 for cmd in self.overrides.commands:
683 log.info(" %s", cmd)
684 exec(cmd, globals(), {'app': app})
685
686
687 sc = app.initialize()
688 if not sc.isSuccess():
689 log.error("Failed to initialize AppMgr")
690 return sc
691
692
693 try:
694 from TrigServices.TrigServicesHelper import TrigServicesHelper
696 except ImportError as e:
697 log.error("TrigServicesHelper not available: %s", e)
698 log.error("Cannot proceed without TrigServicesHelper - required for HLTEventLoopMgr lifecycle")
699 raise RuntimeError("TrigServicesHelper not available") from e
700
701
702 try:
703 run_number = self.run_params['run_number']
704 det_mask = self.run_params['detector_mask']
705 sor_time = self.run_params['sor_time']
706 solenoid_current = self.run_params['solenoid_current']
707 toroids_current = self.run_params['toroids_current']
708 beam_type = self.run_params['beam_type']
709 beam_energy = self.run_params['beam_energy']
710 lb_number = self.run_params['lb_number']
711
712 log.info("Calling prepareForStart with run=%d, det_mask=0x%s, sor_time=%s",
713 run_number, det_mask, sor_time)
714
715 success = helper.prepareForStart(
716 run_number=run_number,
717 det_mask=det_mask,
718 sor_time=sor_time,
719 lb_number=lb_number,
720 beam_type=beam_type,
721 beam_energy=beam_energy,
722 solenoid_current=solenoid_current,
723 toroids_current=toroids_current
724 )
725 if not success:
726 log.error("prepareForStart failed")
727 raise RuntimeError("prepareForStart failed")
728 log.info("prepareForStart completed successfully")
729 except Exception as e:
730 log.error("Error calling prepareForStart: %s", e)
731 traceback.print_exc()
732 raise
733
734
735 sc = app.start()
736 if not sc.isSuccess():
737 log.error("Failed to start AppMgr")
738 return sc
739
740
741
742 try:
743 log.info("Calling prepareForRun to initialize COOL folder helper")
744 success = helper.prepareForRun()
745 if not success:
746 log.error("prepareForRun failed")
747 raise RuntimeError("prepareForRun failed")
748 log.info("prepareForRun completed successfully")
749 except Exception as e:
750 log.error("Error calling prepareForRun: %s", e)
751 traceback.print_exc()
752 raise
753
754
755
756 try:
757 log.info("Calling hltUpdateAfterFork to initialize scheduler (worker_id=1)")
758 success = helper.hltUpdateAfterFork(worker_id=1)
759 if not success:
760 log.error("hltUpdateAfterFork failed")
761 raise RuntimeError("hltUpdateAfterFork failed")
762 log.info("hltUpdateAfterFork completed successfully")
763 except Exception as e:
764 log.error("Error calling hltUpdateAfterFork: %s", e)
765 traceback.print_exc()
766 raise
767
768
769
770 nevt = maxEvents if maxEvents is not None else -1
771 sc = app.run(nevt)
772
773 if not sc.isSuccess():
774 log.error("Failure running application")
775 return sc
776
777
778 sc = app.stop()
779 if not sc.isSuccess():
780 log.error("Failed to stop AppMgr")
781 return sc
782
783
784 sc = app.finalize()
785 if not sc.isSuccess():
786 log.error("Failed to finalize AppMgr")
787 return sc
788
789
790 sc = app.terminate()
791 return sc
792
793
Helper class to call ITrigEventLoopMgr methods from Python.
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
int run(int argc, char *argv[])