Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-25 02:29:09

0001 import os
0002 import logging
0003 
0004 from . import tools
0005 import FWCore.ParameterSet.Config as cms
0006 from .DTWorkflow import DTWorkflow
0007 
0008 log = logging.getLogger(__name__)
0009 
0010 class DTvdriftWorkflow( DTWorkflow ):
0011     """ This class creates and performce / submits vdrift workflow jobs"""
0012     def __init__(self, options):
0013         # call parent constructor
0014         super( DTvdriftWorkflow, self ).__init__( options )
0015 
0016         self.outpath_command_tag = "VdriftCalibration"
0017         output_file_dict = { "segment" : "DTVDriftHistos.root",
0018                              "meantimer" : "DTTMaxHistos.root",
0019                             }
0020         self.outpath_workflow_mode_dict = { "segment" : "Segments",
0021                                             "meantimer" : "MeanTimer",
0022                                             }
0023         self.output_file = output_file_dict[self.options.workflow_mode]
0024         self.output_files = [self.output_file]
0025 
0026     def prepare_workflow(self):
0027         """ Generalized function to prepare workflow dependent on workflow mode"""
0028         function_name = "prepare_" + self.options.workflow_mode + "_" + self.options.command
0029 
0030         try:
0031             fill_function = getattr(self, function_name)
0032         except AttributeError:
0033             errmsg = "Class `{}` does not implement `{}`"
0034             raise NotImplementedError( errmsg.format(self.__class__.__name__,
0035                                                      function_name))
0036         log.debug("Preparing workflow with function %s" % function_name)
0037         # call chosen function
0038         fill_function()
0039 
0040     def prepare_segment_submit(self):
0041         self.pset_name = 'dtVDriftSegmentCalibration_cfg.py'
0042         self.pset_template = 'CalibMuon.DTCalibration.dtVDriftSegmentCalibration_cfg'
0043         if self.options.datasettype == "Cosmics":
0044             self.pset_template = 'CalibMuon.DTCalibration.dtVDriftSegmentCalibration_cosmics_cfg'
0045 
0046         self.process = tools.loadCmsProcess(self.pset_template)
0047         self.process.GlobalTag.globaltag = self.options.globaltag
0048         self.process.dtVDriftSegmentCalibration.rootFileName = self.output_file
0049 
0050         if self.options.inputCalibDB:
0051             err = "Option inputCalibDB not available for segment."
0052             err += "Maybe you want to use option inputTtrigDB"
0053             raise ValueError(err)
0054         self.prepare_common_submit()
0055         if self.options.inputTtrigDB:
0056             label = ''
0057             if self.options.datasettype == "Cosmics":
0058                 label = 'cosmics'
0059             connect = os.path.basename(self.options.inputTtrigDB)
0060             self.addPoolDBESSource( process = self.process,
0061                                     moduleName = 'tTrigDB',
0062                                     record = 'DTTtrigRcd',
0063                                     tag = 'ttrig',
0064                                     connect = str("sqlite_file:%s" % connect),
0065                                     label = label
0066                                     )
0067             self.input_files.append( os.path.abspath(self.options.inputTtrigDB) )
0068         self.write_pset_file()
0069 
0070     def prepare_segment_check(self):
0071         self.load_options_command("submit")
0072 
0073     def prepare_segment_write(self):
0074         self.pset_name = 'dtVDriftSegmentWriter_cfg.py'
0075         self.pset_template = 'CalibMuon.DTCalibration.dtVDriftSegmentWriter_cfg'
0076         tag = self.prepare_common_write()
0077         merged_file = os.path.join(self.result_path, self.output_file)
0078         self.process = tools.loadCmsProcess(self.pset_template)
0079 
0080         if self.options.inputVDriftDB:
0081             self.add_local_vdrift_db(self)
0082         vdrift_db = "vDrift_segment"+ tag + ".db"
0083         vdrift_db = os.path.join(self.result_path, vdrift_db)
0084         self.process.dtVDriftSegmentWriter.vDriftAlgoConfig.rootFileName = "file:///" + merged_file
0085         self.process.PoolDBOutputService.connect = 'sqlite_file:%s' % vdrift_db
0086         self.process.source.firstRun = cms.untracked.uint32(self.options.run)
0087         self.process.GlobalTag.globaltag = cms.string(str(self.options.globaltag))
0088         self.write_pset_file()
0089 
0090     def prepare_segment_dump(self):
0091         self.pset_name = 'dumpDBToFile_vdrift_cfg.py'
0092         self.pset_template = 'CalibMuon.DTCalibration.dumpDBToFile_vdrift_cfg'
0093         if self.options.input_dumpDB:
0094             try:
0095                 test = self.result_path
0096                 self.load_options_command("write")
0097             except:
0098                 pass
0099             dbpath = os.path.abspath(self.options.input_dumpDB)
0100         else:
0101             crabtask = self.crabFunctions.CrabTask(crab_config = self.crab_config_filepath,
0102                                                initUpdate = False)
0103             tag = crabtask.crabConfig.Data.outputDatasetTag
0104             dbpath = os.path.abspath( os.path.join(self.result_path,
0105                                                    "vDrift_segment"+ tag + ".db"))
0106         self.prepare_common_dump(dbpath)
0107         self.write_pset_file()
0108 
0109     def prepare_segment_all(self):
0110         # individual prepare functions for all tasks will be called in
0111         # main implementation of all
0112         self.all_commands=["submit", "check", "write", "dump"]
0113 
0114     ####################################################################
0115     #                            Mean Timer                            #
0116     ####################################################################
0117     def prepare_meantimer_submit(self):
0118         self.pset_name = 'dtVDriftMeanTimerCalibration_cfg.py'
0119         self.pset_template = 'CalibMuon.DTCalibration.dtVDriftMeanTimerCalibration_cfg'
0120         if self.options.datasettype == "Cosmics":
0121             self.pset_template = 'CalibMuon.DTCalibration.dtVDriftMeanTimerCalibration_cosmics_cfg'
0122 
0123         self.process = tools.loadCmsProcess(self.pset_template)
0124         self.process.GlobalTag.globaltag = self.options.globaltag
0125         self.process.dtVDriftMeanTimerCalibration.rootFileName = self.output_file
0126 
0127         if self.options.inputCalibDB:
0128             err = "Option inputCalibDB not available for meantimer."
0129             err += "Maybe you want to use option inputTtrigDB"
0130             raise ValueError(err)
0131         self.prepare_common_submit()
0132         if self.options.inputTtrigDB:
0133             label = ''
0134             if self.options.datasettype == "Cosmics":
0135                 label = 'cosmics'
0136             connect = os.path.basename(self.options.inputTtrigDB)
0137             self.addPoolDBESSource( process = self.process,
0138                                     moduleName = 'tTrigDB',
0139                                     record = 'DTTtrigRcd',
0140                                     tag = 'ttrig',
0141                                     connect = str("sqlite_file:%s" % connect),
0142                                     label = label
0143                                     )
0144             self.input_files.append( os.path.abspath(self.options.inputTtrigDB) )
0145         self.write_pset_file()
0146 
0147     def prepare_meantimer_check(self):
0148         self.load_options_command("submit")
0149 
0150     def prepare_meantimer_write(self):
0151         self.pset_name = 'dtVDriftMeanTimerWriter_cfg.py'
0152         self.pset_template = 'CalibMuon.DTCalibration.dtVDriftMeanTimerWriter_cfg'
0153         tag = self.prepare_common_write()
0154         merged_file = os.path.join(self.result_path, self.output_file)
0155         self.process = tools.loadCmsProcess(self.pset_template)
0156 
0157         if self.options.inputVDriftDB:
0158             self.add_local_vdrift_db(self)
0159         vdrift_db = "vDrift_meantimer" + tag + ".db"
0160         vdrift_db = os.path.join(self.result_path, vdrift_db)
0161         self.process.dtVDriftMeanTimerWriter.vDriftAlgoConfig.rootFileName = "file:///" + merged_file
0162         self.process.PoolDBOutputService.connect = 'sqlite_file:%s' % vdrift_db
0163         self.process.source.firstRun = cms.untracked.uint32(self.options.run)
0164         self.process.GlobalTag.globaltag = cms.string(str(self.options.globaltag))
0165         self.write_pset_file()
0166 
0167     def prepare_meantimer_dump(self):
0168         self.pset_name = 'dumpDBToFile_vdrift_cfg.py'
0169         self.pset_template = 'CalibMuon.DTCalibration.dumpDBToFile_vdrift_cfg'
0170         if self.options.input_dumpDB:
0171             try:
0172                 test = self.result_path
0173                 self.load_options_command("write")
0174             except:
0175                 pass
0176             dbpath = os.path.abspath(self.options.input_dumpDB)
0177         else:
0178             crabtask = self.crabFunctions.CrabTask(crab_config = self.crab_config_filepath,
0179                                                initUpdate = False)
0180             tag = crabtask.crabConfig.Data.outputDatasetTag
0181             dbpath = os.path.abspath( os.path.join(self.result_path,
0182                                                    "vDrift_meantimer" + tag + ".db"))
0183         self.prepare_common_dump(dbpath)
0184         self.write_pset_file()
0185 
0186     def prepare_meantimer_all(self):
0187         # individual prepare functions for all tasks will be called in
0188         # main implementation of all
0189         self.all_commands=["submit", "check", "write", "dump"]
0190 
0191     ####################################################################
0192     #                           CLI creation                           #
0193     ####################################################################
0194     @classmethod
0195     def add_parser_options(cls, subparser_container):
0196         vdrift_parser = subparser_container.add_parser( "vdrift",
0197         #parents=[mutual_parent_parser, common_parent_parser],
0198         help = "" ) # What does ttrig
0199 
0200         ################################################################
0201         #                Sub parser options for workflow modes         #
0202         ################################################################
0203         vdrift_subparsers = vdrift_parser.add_subparsers( dest="workflow_mode",
0204             help="Possible workflow modes",)
0205         ## Add all workflow modes for ttrig
0206         vdrift_segment_subparser = vdrift_subparsers.add_parser( "segment",
0207             #parents=[mutual_parent_parser, common_parent_parser],
0208             help = "" )
0209         vdrift_meantimer_subparser = vdrift_subparsers.add_parser( "meantimer",
0210             #parents=[mutual_parent_parser, common_parent_parser],
0211             help = "" )
0212         ################################################################
0213         #        Sub parser options for workflow mode segment          #
0214         ################################################################
0215         vdrift_segment_subparsers = vdrift_segment_subparser.add_subparsers( dest="command",
0216             help="Possible commands for segments")
0217         vdrift_segment_submit_parser = vdrift_segment_subparsers.add_parser(
0218             "submit",
0219             parents=[super(DTvdriftWorkflow,cls).get_common_options_parser(),
0220                     super(DTvdriftWorkflow,cls).get_submission_options_parser(),
0221                     super(DTvdriftWorkflow,cls).get_local_input_db_options_parser(),
0222                     super(DTvdriftWorkflow,cls).get_input_db_options_parser()],
0223             help = "Submit job to the GRID via crab3")
0224         vdrift_segment_submit_parser.add_argument("--inputTtrigDB",
0225             help="Local alternative calib ttrig db")
0226 
0227         vdrift_segment_check_parser = vdrift_segment_subparsers.add_parser(
0228             "check",
0229             parents=[super(DTvdriftWorkflow,cls).get_common_options_parser(),
0230                     super(DTvdriftWorkflow,cls).get_check_options_parser()],
0231             help = "Check status of submitted jobs")
0232 
0233         vdrift_segment_write_parser = vdrift_segment_subparsers.add_parser(
0234             "write",
0235             parents=[super(DTvdriftWorkflow,cls).get_common_options_parser(),
0236                      super(DTvdriftWorkflow,cls).get_write_options_parser()
0237                     ],
0238             help = "Write result from root output to text file")
0239 
0240         vdrift_segment_dump_parser = vdrift_segment_subparsers.add_parser(
0241             "dump",
0242             parents=[super(DTvdriftWorkflow,cls).get_common_options_parser(),
0243                      super(DTvdriftWorkflow,cls).get_dump_options_parser()],
0244             help = "Dump database to text file")
0245 
0246         vdrift_segment_all_parser = vdrift_segment_subparsers.add_parser(
0247             "all",
0248             parents=[super(DTvdriftWorkflow,cls).get_common_options_parser(),
0249                      super(DTvdriftWorkflow,cls).get_submission_options_parser(),
0250                      super(DTvdriftWorkflow,cls).get_check_options_parser(),
0251                      super(DTvdriftWorkflow,cls).get_input_db_options_parser(),
0252                      super(DTvdriftWorkflow,cls).get_local_input_db_options_parser(),
0253                      super(DTvdriftWorkflow,cls).get_write_options_parser(),
0254                      super(DTvdriftWorkflow,cls).get_dump_options_parser()
0255                     ],
0256             help = "Perform all steps: submit, check, write, dump in this order")
0257         vdrift_segment_all_parser.add_argument("--inputTtrigDB",
0258             help="Local alternative calib ttrig db")
0259         ################################################################
0260         #       Sub parser options for workflow mode meantimer         #
0261         ################################################################
0262         vdrift_meantimer_subparsers = vdrift_meantimer_subparser.add_subparsers( dest="command",
0263             help="Possible commands for meantimers")
0264         vdrift_meantimer_submit_parser = vdrift_meantimer_subparsers.add_parser(
0265             "submit",
0266             parents=[super(DTvdriftWorkflow,cls).get_common_options_parser(),
0267                     super(DTvdriftWorkflow,cls).get_submission_options_parser(),
0268                     super(DTvdriftWorkflow,cls).get_local_input_db_options_parser(),
0269                     super(DTvdriftWorkflow,cls).get_input_db_options_parser()],
0270             help = "Submit job to the GRID via crab3")
0271         vdrift_meantimer_submit_parser.add_argument("--inputTtrigDB",
0272             help="Local alternative calib ttrig db")
0273 
0274         vdrift_meantimer_check_parser = vdrift_meantimer_subparsers.add_parser(
0275             "check",
0276             parents=[super(DTvdriftWorkflow,cls).get_common_options_parser(),
0277                     super(DTvdriftWorkflow,cls).get_check_options_parser()],
0278             help = "Check status of submitted jobs")
0279 
0280         vdrift_meantimer_write_parser = vdrift_meantimer_subparsers.add_parser(
0281             "write",
0282             parents=[super(DTvdriftWorkflow,cls).get_common_options_parser(),
0283                      super(DTvdriftWorkflow,cls).get_write_options_parser()
0284                     ],
0285             help = "Write result from root output to text file")
0286 
0287         vdrift_meantimer_dump_parser = vdrift_meantimer_subparsers.add_parser(
0288             "dump",
0289             parents=[super(DTvdriftWorkflow,cls).get_common_options_parser(),
0290                      super(DTvdriftWorkflow,cls).get_dump_options_parser()],
0291             help = "Dump database to text file")
0292 
0293         vdrift_meantimer_all_parser = vdrift_meantimer_subparsers.add_parser(
0294             "all",
0295             parents=[super(DTvdriftWorkflow,cls).get_common_options_parser(),
0296                      super(DTvdriftWorkflow,cls).get_submission_options_parser(),
0297                      super(DTvdriftWorkflow,cls).get_check_options_parser(),
0298                      super(DTvdriftWorkflow,cls).get_input_db_options_parser(),
0299                      super(DTvdriftWorkflow,cls).get_local_input_db_options_parser(),
0300                      super(DTvdriftWorkflow,cls).get_write_options_parser(),
0301                      super(DTvdriftWorkflow,cls).get_dump_options_parser()
0302                     ],
0303             help = "Perform all steps: submit, check, write, dump in this order")
0304         vdrift_meantimer_all_parser.add_argument("--inputTtrigDB",
0305             help="Local alternative calib ttrig db")