class PandoraEMScaleStep(CalibrationStep): def __init__(self): CalibrationStep.__init__(self, "PandoraEMScale") self._marlin = None self._maxNIterations = 5 self._energyScaleAccuracy = 0.01 self._photonEnergy = 0 # step input self._inputEcalToEMGeV = None self._inputHcalToEMGeV = None # step output self._outputEcalToEMGeV = None self._outputHcalToEMGeV = None # command line requirement self._requireSteeringFile() self._requireCompactFile() self._requireIterations() self._requirePhotonFile() self._requireECalAccuracy() def description(self): return "Calibrate the electromagnetic scale of the ecal and the hcal. Outputs the constants ECalToEMGeVCalibration and HCalToEMGeVCalibration" def readCmdLine(self, parsed): # setup marlin self._marlin = Marlin(parsed.steeringFile) gearFile = self._manager.getGearConverter().convertToGear( parsed.compactFile) self._marlin.setGearFile(gearFile) self._marlin.setCompactFile(parsed.compactFile) self._marlin.setMaxRecordNumber(parsed.maxRecordNumber) self._marlin.setInputFiles( self._extractFileList(parsed.lcioPhotonFile, "slcio")) self._maxNIterations = int(parsed.maxNIterations) self._energyScaleAccuracy = float(parsed.ecalCalibrationAccuracy) self._photonEnergy = parsed.photonEnergy # setup pandora settings pandoraSettings = self._marlin.getProcessorParameter( self._marlinPandoraProcessor, "PandoraSettingsXmlFile") pandora = PandoraXML(pandoraSettings) pandora.setRemoveEnergyCorrections(True) newPandoraSettings = pandora.generateNewXmlFile() self._marlin.setProcessorParameter(self._marlinPandoraProcessor, "PandoraSettingsXmlFile", newPandoraSettings) def init(self, config): self._cleanupElement(config) self._marlin.loadInputParameters(config) self._loadStepOutputs(config) if len(self._runProcessors): self._marlin.turnOffProcessorsExcept(self._runProcessors) self._inputEcalToEMGeV = float( self._marlin.getProcessorParameter(self._marlinPandoraProcessor, "ECalToEMGeVCalibration")) self._inputHcalToEMGeV = float( self._marlin.getProcessorParameter(self._marlinPandoraProcessor, "HCalToEMGeVCalibration")) def run(self, config): # loop variables currentPrecision = 0. calibrationRescaleFactor = 1. calibrationRescaleFactorCumul = 1. accuracyReached = False ecalToEMGeV = self._inputEcalToEMGeV hcalToEMGeV = self._inputHcalToEMGeV emScaleCalibrator = PandoraEMScaleCalibrator() emScaleCalibrator.setPhotonEnergy(self._photonEnergy) for iteration in range(self._maxNIterations): # readjust iteration parameters ecalToEMGeV = ecalToEMGeV * calibrationRescaleFactor hcalToEMGeV = hcalToEMGeV * calibrationRescaleFactor pfoAnalysisFile = "./PfoAnalysis_{0}_iter{1}.root".format( self._name, iteration) # run marlin ... self._marlin.setProcessorParameter(self._marlinPandoraProcessor, "ECalToEMGeVCalibration", str(ecalToEMGeV)) self._marlin.setProcessorParameter(self._marlinPandoraProcessor, "HCalToEMGeVCalibration", str(hcalToEMGeV)) self._marlin.setProcessorParameter(self._pfoAnalysisProcessor, "RootFile", pfoAnalysisFile) self._marlin.run() # ... and calibration script emScaleCalibrator.setRootFile(pfoAnalysisFile) emScaleCalibrator.setPhotonEnergy(self._photonEnergy) emScaleCalibrator.run() newPhotonEnergy = emScaleCalibrator.getEcalToEMMean() calibrationRescaleFactor = float( self._photonEnergy) / newPhotonEnergy calibrationRescaleFactorCumul = calibrationRescaleFactorCumul * calibrationRescaleFactor currentPrecision = abs(1 - 1. / calibrationRescaleFactor) # write down iteration results self._writeIterationOutput( config, iteration, { "precision": currentPrecision, "rescale": calibrationRescaleFactor, "newPhotonEnergy": newPhotonEnergy }) # are we accurate enough ?? if currentPrecision < self._energyScaleAccuracy: print "{0}: ecal energy accuracy reached !".format(self._name) accuracyReached = True self._outputEcalToEMGeV = ecalToEMGeV self._outputHcalToEMGeV = hcalToEMGeV break if not accuracyReached: raise RuntimeError( "{0}: Couldn't reach the user accuracy ({1})".format( self._name, self._energyScaleAccuracy)) def writeOutput(self, config): output = self._getXMLStepOutput(config, create=True) self._writeProcessorParameter(output, self._marlinPandoraProcessor, "ECalToEMGeVCalibration", self._outputEcalToEMGeV) self._writeProcessorParameter(output, self._marlinPandoraProcessor, "HCalToEMGeVCalibration", self._outputHcalToEMGeV)
class PandoraHadScaleStep(CalibrationStep) : def __init__(self) : CalibrationStep.__init__(self, "PandoraHadScale") self._marlin = None self._hadScaleCalibrator = None self._maxNIterations = 5 self._ecalEnergyScaleAccuracy = 0.01 self._hcalEnergyScaleAccuracy = 0.01 self._kaon0LEnergy = 0 # step input self._inputEcalToHadGeVBarrel = None self._inputEcalToHadGeVEndcap = None self._inputHcalToHadGeV = None # step output self._outputEcalToHadGeVBarrel = None self._outputEcalToHadGeVEndcap = None self._outputHcalToHadGeV = None # command line requirement self._requireSteeringFile() self._requireCompactFile() self._requireIterations() self._requireKaon0LFile() self._requireHCalAccuracy() self._requireECalAccuracy() def description(self): return "Calibrate the hadronic scale of the ecal and the hcal. Outputs the constants ECalToHadGeVCalibrationBarrel, ECalToHadGeVCalibrationEndCap and HCalToHadGeVCalibration" def readCmdLine(self, parsed) : # setup marlin self._marlin = Marlin(parsed.steeringFile) gearFile = self._manager.getGearConverter().convertToGear(parsed.compactFile) self._marlin.setGearFile(gearFile) self._marlin.setCompactFile(parsed.compactFile) self._marlin.setMaxRecordNumber(parsed.maxRecordNumber) self._marlin.setInputFiles(self._extractFileList(parsed.lcioKaon0LFile, "slcio")) self._maxNIterations = int(parsed.maxNIterations) self._ecalEnergyScaleAccuracy = float(parsed.ecalCalibrationAccuracy) self._hcalEnergyScaleAccuracy = float(parsed.hcalCalibrationAccuracy) self._kaon0LEnergy = parsed.kaon0LEnergy # setup pandora settings pandoraSettings = self._marlin.getProcessorParameter(self._marlinPandoraProcessor, "PandoraSettingsXmlFile") pandora = PandoraXML(pandoraSettings) pandora.setRemoveEnergyCorrections(True) newPandoraSettings = pandora.generateNewXmlFile() self._marlin.setProcessorParameter(self._marlinPandoraProcessor, "PandoraSettingsXmlFile", newPandoraSettings) def init(self, config) : self._cleanupElement(config) self._marlin.loadInputParameters(config) self._loadStepOutputs(config) if len(self._runProcessors): self._marlin.turnOffProcessorsExcept(self._runProcessors) def run(self, config) : # loop variables currentEcalPrecision = 0. currentHcalPrecision = 0. ecalRescaleFactor = 1. hcalRescaleFactor = 1. ecalRescaleFactorCumul = 1. hcalRescaleFactorCumul = 1. ecalAccuracyReached = False hcalAccuracyReached = False ecalToHadGeVBarrel = float(self._marlin.getProcessorParameter(self._marlinPandoraProcessor, "ECalToHadGeVCalibrationBarrel")) ecalToHadGeVEndcap = float(self._marlin.getProcessorParameter(self._marlinPandoraProcessor, "ECalToHadGeVCalibrationEndCap")) hcalToHadGeV = float(self._marlin.getProcessorParameter(self._marlinPandoraProcessor, "HCalToHadGeVCalibration")) hadScaleCalibrator = PandoraHadScaleCalibrator() hadScaleCalibrator.setKaon0LEnergy(self._kaon0LEnergy) for iteration in range(self._maxNIterations) : # readjust iteration parameters if not ecalAccuracyReached: ecalToHadGeVBarrel = ecalToHadGeVBarrel*ecalRescaleFactor ecalToHadGeVEndcap = ecalToHadGeVEndcap*ecalRescaleFactor if not hcalAccuracyReached: hcalToHadGeV = hcalToHadGeV*hcalRescaleFactor pfoAnalysisFile = "./PfoAnalysis_{0}_iter{1}.root".format(self._name, iteration) # run marlin ... self._marlin.setProcessorParameter(self._marlinPandoraProcessor, "ECalToHadGeVCalibrationBarrel", str(ecalToHadGeVBarrel)) self._marlin.setProcessorParameter(self._marlinPandoraProcessor, "ECalToHadGeVCalibrationEndCap", str(ecalToHadGeVEndcap)) self._marlin.setProcessorParameter(self._marlinPandoraProcessor, "HCalToHadGeVCalibration", str(hcalToHadGeV)) self._marlin.setProcessorParameter(self._pfoAnalysisProcessor , "RootFile", pfoAnalysisFile) self._marlin.run() # ... and calibration script hadScaleCalibrator.setRootFile(pfoAnalysisFile) hadScaleCalibrator.run() if not ecalAccuracyReached : newEcalKaon0LEnergy = hadScaleCalibrator.getEcalToHad() ecalRescaleFactor = float(self._kaon0LEnergy) / newEcalKaon0LEnergy ecalRescaleFactorCumul = ecalRescaleFactorCumul*ecalRescaleFactor currentEcalPrecision = abs(1 - 1. / ecalRescaleFactor) if not hcalAccuracyReached : newHcalKaon0LEnergy = hadScaleCalibrator.getHcalToHad() hcalRescaleFactor = float(self._kaon0LEnergy) / newHcalKaon0LEnergy hcalRescaleFactorCumul = hcalRescaleFactorCumul*hcalRescaleFactor currentHcalPrecision = abs(1 - 1. / hcalRescaleFactor) # write down iteration results self._writeIterationOutput(config, iteration, {"ecalPrecision" : currentEcalPrecision, "ecalRescale" : ecalRescaleFactor, "newEcalKaon0LEnergy" : newEcalKaon0LEnergy, "hcalPrecision" : currentHcalPrecision, "hcalRescale" : hcalRescaleFactor, "newHcalKaon0LEnergy" : newHcalKaon0LEnergy}) # are we accurate enough ?? if currentEcalPrecision < self._ecalEnergyScaleAccuracy : ecalAccuracyReached = True self._outputEcalToHadGeVBarrel = ecalToHadGeVBarrel self._outputEcalToHadGeVEndcap = ecalToHadGeVEndcap # are we accurate enough ?? if currentHcalPrecision < self._hcalEnergyScaleAccuracy : hcalAccuracyReached = True self._outputHcalToHadGeV = hcalToHadGeV if ecalAccuracyReached and hcalAccuracyReached : break if not ecalAccuracyReached or not hcalAccuracyReached : raise RuntimeError("{0}: Couldn't reach the user accuracy".format(self._name)) def writeOutput(self, config) : output = self._getXMLStepOutput(config, create=True) self._writeProcessorParameter(output, self._marlinPandoraProcessor, "ECalToHadGeVCalibrationBarrel", self._outputEcalToHadGeVBarrel) self._writeProcessorParameter(output, self._marlinPandoraProcessor, "ECalToHadGeVCalibrationEndCap", self._outputEcalToHadGeVEndcap) self._writeProcessorParameter(output, self._marlinPandoraProcessor, "HCalToHadGeVCalibration", self._outputHcalToHadGeV)