574 def run(self, maxEvents=None):
575 """
576 This follows the same pattern as PSC (Psc.cxx):
577 1. Create ApplicationMgr via BootstrapHelper
578 2. Set JobOptionsSvcType, JobOptionsType, JobOptionsPath
579 3. configure() -> initialize() -> prepareForStart() -> start() ->
580 hltUpdateAfterFork() -> run() -> stop() -> finalize() -> terminate()
581 """
582 from Gaudi.Main import BootstrapHelper
583
584
585 if self.job_options_type == "FILE" and self.properties is None:
586 with open(self.job_options_path, 'r') as f:
587 jocat = json.load(f)
588 self.properties = jocat.get('properties', {})
589
590 bsh = BootstrapHelper()
591 app = bsh.createApplicationMgr()
592 self._app = app
593
594
595 if self.job_options_type == "FILE" and self.properties:
596 app_props = self.properties.
get(
'ApplicationMgr', {})
597 for k, v in app_props.items():
598 if k not in ('JobOptionsSvcType', 'JobOptionsType', 'JobOptionsPath'):
599 log.debug("Setting ApplicationMgr.%s = %s", k, v)
600 app.setProperty(k, str(v) if not isinstance(v, str) else v)
601
602
603 log.info("Configuring TrigConf::JobOptionsSvc with TYPE=%s, PATH=%s",
604 self.job_options_type, self.job_options_path)
605 app.setProperty("JobOptionsSvcType", "TrigConf::JobOptionsSvc")
606 app.setProperty("JobOptionsType", self.job_options_type)
607 app.setProperty("JobOptionsPath", self.job_options_path)
608
609
610 app.configure()
611
612
613
614 if maxEvents is not None:
615 log.info("Setting EvtMax=%d (overriding DB value)", maxEvents)
616 app.setProperty('EvtMax', str(maxEvents))
617
618
619
620 from GaudiPython.Bindings import iProperty
621
622
623 if maxEvents is not None:
624 log.info("Setting EFInterfaceSvc.NumEvents=%d (overriding DB value)", maxEvents)
625 iProperty("EFInterfaceSvc").NumEvents = maxEvents
626
627
628 log.info("Setting threading: ThreadPoolSize=%d, NSlots=%d", self.num_threads, self.num_slots)
629 iProperty("AvalancheSchedulerSvc").ThreadPoolSize = self.num_threads
630 iProperty("EventDataSvc").NSlots = self.num_slots
631
632
633 if self.ef_files:
634 log.info("Setting EFInterfaceSvc.Files = %s", self.ef_files)
635 iProperty("EFInterfaceSvc").Files = self.ef_files
636 iProperty(
"EFInterfaceSvc").T0ProjectTag = self.run_params.
get(
'T0_project_tag',
'')
637 iProperty(
"EFInterfaceSvc").BeamType = self.run_params.
get(
'beam_type', 0)
638 iProperty(
"EFInterfaceSvc").BeamEnergy = self.run_params.
get(
'beam_energy', 0)
639 iProperty(
"EFInterfaceSvc").TriggerType = self.run_params.
get(
'trigger_type', 0)
640 iProperty(
"EFInterfaceSvc").Stream = self.run_params.
get(
'stream',
'')
641 iProperty(
"EFInterfaceSvc").Lumiblock = self.run_params.
get(
'lumiblock', 0)
642 iProperty(
"EFInterfaceSvc").DetMask = self.run_params.
get(
'detector_mask',
'')
643
644
645
646 from TrigPSC import PscConfig
647 if PscConfig.forcePSK:
648 log.info("PscConfig.forcePSK is set - configuring HLTPrescaleCondAlg to read from DB instead of COOL")
649 iProperty("HLTPrescaleCondAlg").Source = "DB"
650
651
652
653 conditions_run = self.run_params.
get(
'conditions_run')
654 if conditions_run is not None:
655 log.info("Setting HltEventLoopMgr.forceRunNumber=%d for conditions lookup", conditions_run)
656 iProperty("HltEventLoopMgr").forceRunNumber = conditions_run
657
658
659 sc = app.initialize()
660 if not sc.isSuccess():
661 log.error("Failed to initialize AppMgr")
662 return sc
663
664
665 try:
666 from TrigServices.TrigServicesHelper import TrigServicesHelper
668 except ImportError as e:
669 log.error("TrigServicesHelper not available: %s", e)
670 log.error("Cannot proceed without TrigServicesHelper - required for HLTEventLoopMgr lifecycle")
671 raise RuntimeError("TrigServicesHelper not available") from e
672
673
674 try:
675 run_number = self.run_params['run_number']
676 det_mask = self.run_params['detector_mask']
677 sor_time = self.run_params['sor_time']
678 solenoid_current = self.run_params['solenoid_current']
679 toroids_current = self.run_params['toroids_current']
680
681 log.info("Calling prepareForStart with run=%d, det_mask=0x%s, sor_time=%s",
682 run_number, det_mask, sor_time)
683
684 success = helper.prepareForStart(
685 run_number=run_number,
686 det_mask=det_mask,
687 sor_time=sor_time,
688 solenoid_current=solenoid_current,
689 toroids_current=toroids_current
690 )
691 if not success:
692 log.error("prepareForStart failed")
693 raise RuntimeError("prepareForStart failed")
694 log.info("prepareForStart completed successfully")
695 except Exception as e:
696 log.error("Error calling prepareForStart: %s", e)
697 traceback.print_exc()
698 raise
699
700
701 sc = app.start()
702 if not sc.isSuccess():
703 log.error("Failed to start AppMgr")
704 return sc
705
706
707
708 try:
709 log.info("Calling prepareForRun to initialize COOL folder helper")
710 success = helper.prepareForRun()
711 if not success:
712 log.error("prepareForRun failed")
713 raise RuntimeError("prepareForRun failed")
714 log.info("prepareForRun completed successfully")
715 except Exception as e:
716 log.error("Error calling prepareForRun: %s", e)
717 traceback.print_exc()
718 raise
719
720
721
722 try:
723 log.info("Calling hltUpdateAfterFork to initialize scheduler (worker_id=1)")
724 success = helper.hltUpdateAfterFork(worker_id=1)
725 if not success:
726 log.error("hltUpdateAfterFork failed")
727 raise RuntimeError("hltUpdateAfterFork failed")
728 log.info("hltUpdateAfterFork completed successfully")
729 except Exception as e:
730 log.error("Error calling hltUpdateAfterFork: %s", e)
731 traceback.print_exc()
732 raise
733
734
735
736 nevt = maxEvents if maxEvents is not None else -1
737 sc = app.run(nevt)
738
739 if not sc.isSuccess():
740 log.error("Failure running application")
741 return sc
742
743
744 sc = app.stop()
745 if not sc.isSuccess():
746 log.error("Failed to stop AppMgr")
747 return sc
748
749
750 sc = app.finalize()
751 if not sc.isSuccess():
752 log.error("Failed to finalize AppMgr")
753 return sc
754
755
756 sc = app.terminate()
757 return sc
758
759
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[])